1
0
Fork 0

Add accept/reject styler

* src/validate/formatter/AcceptReject.js: Added.
* test/validate/formatter/AcceptRejectTest.js: Added.
master
Mike Gerwitz 2016-06-27 11:37:27 -04:00
parent 31f3ab010e
commit b90f6d7474
2 changed files with 172 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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 );
}
}
} );

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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'
);
} );