diff --git a/src/validate/formatter/AcceptReject.js b/src/validate/formatter/AcceptReject.js new file mode 100644 index 0000000..64b0dbe --- /dev/null +++ b/src/validate/formatter/AcceptReject.js @@ -0,0 +1,79 @@ +/** + * Accept/reject styling + * + * Copyright (C) 2016 LoVullo Associates, Inc. + * + * This file is part of liza. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +var Trait = require( 'easejs' ).Trait, + ValidatorFormatter = require( '../ValidatorFormatter' ); + + +/** + * Applies supertype for each item in a delimited string + */ +module.exports = Trait( 'AcceptReject' ) + .implement( ValidatorFormatter ) + .extend( +{ + /** + * Format accept/reject string as an integer boolean + * + * @param {string} data data to parse + * + * @return {string} data formatted for storage + */ + 'virtual abstract override public parse': function( data ) + { + switch ( data ) + { + case '0': + case 'Rejected': + return '0'; + + case '1': + case 'Accepted': + return '1'; + + default: + return this.__super( data ); + } + }, + + + /** + * Format boolean integer as accept/reject string + * + * @param {string} data data to format for display + * + * @return {string} data formatted for display + */ + 'virtual abstract override public retrieve': function( data ) + { + switch ( data ) + { + case '0': + return 'Rejected'; + + case '1': + return 'Accepted'; + + default: + return this.__super( data ); + } + } +} ); diff --git a/test/validate/formatter/AcceptRejectTest.js b/test/validate/formatter/AcceptRejectTest.js new file mode 100644 index 0000000..3c9a6cc --- /dev/null +++ b/test/validate/formatter/AcceptRejectTest.js @@ -0,0 +1,93 @@ +/** + * Accept/reject formatter test + * + * Copyright (C) 2016 LoVullo Associates, Inc. + * + * This file is part of liza. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +var liza = require( '../../../' ), + Class = require( 'easejs' ).Class, + Sut = liza.validate.formatter.AcceptReject, + EchoFormatter = liza.validate.formatter.EchoFormatter, + ValidatorFormatter = liza.validate.ValidatorFormatter, + common = require( './common' ), + expect = require( 'chai' ).expect; + +var DummyFormatter = Class.implement( ValidatorFormatter ) + .extend( +{ + 'virtual parse': function( data ) + { + return '+' + data; + }, + + 'virtual retrieve': function( data ) + { + return '-' + data; + }, +} ); + + +describe( 'validate.formatter.Number', function() +{ + common.testValidate( EchoFormatter.use( Sut )(), { + "": [ '' ], + "0": [ "0", "Rejected" ], + "1": [ "1", "Accepted" ], + "Rejected": [ "0", "Rejected" ], + "Accepted": [ "1", "Accepted" ], + + // arbitrary text left alone + "foo": [ "foo" ], + } ); + + + describe( '#parse', function() + { + it( 'considers accept/reject before supertype formatting', function() + { + // will equal +Accepted if supertype is called first + expect( DummyFormatter.use( Sut )().parse( 'Accepted' ) ) + .to.equal( '1' ); + + // will equal +1 if supertype is called first + expect( DummyFormatter.use( Sut )().parse( '1' ) ) + .to.equal( '1' ); + } ); + } ); + + + describe( '#retrieve', function() + { + it( 'considers accept/reject before supertype formatting', function() + { + // will equal -1 if supertype is called first + expect( DummyFormatter.use( Sut )().retrieve( '1' ) ) + .to.equal( 'Accepted' ); + } ); + } ); + + + common.testMixin( + EchoFormatter, + Sut, + 'asdf', + 'given', + 'asdfgiven', + 'asdfgiven' + ); +} );