Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <gerwitm@lovullo.com>2016-06-22 16:38:15 -0400
committerMike Gerwitz <gerwitm@lovullo.com>2016-06-23 09:13:50 -0400
commitab6be11bb60d5dcf0faa5b3f9b467c5f81bc5f6d (patch)
tree59292eab2483017131ae0002d01bafb1ebe6fd27 /test/validate
parentd6cecf1ff6d086dcdb2773a232faefcdc1195e04 (diff)
downloadliza-ab6be11bb60d5dcf0faa5b3f9b467c5f81bc5f6d.tar.gz
liza-ab6be11bb60d5dcf0faa5b3f9b467c5f81bc5f6d.tar.bz2
liza-ab6be11bb60d5dcf0faa5b3f9b467c5f81bc5f6d.zip
Liberate common validator-formatter test function
This has been modified from the original to use Chai instead of the `assert' module. * test/validate/formatter/common.js: Added.
Diffstat (limited to 'test/validate')
-rw-r--r--test/validate/formatter/common.js95
1 files changed, 95 insertions, 0 deletions
diff --git a/test/validate/formatter/common.js b/test/validate/formatter/common.js
new file mode 100644
index 0000000..af36ebe
--- /dev/null
+++ b/test/validate/formatter/common.js
@@ -0,0 +1,95 @@
+/**
+ * Common validator-formatter test functions
+ *
+ * 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( '../../../' ),
+ VFmt = liza.validate.ValidatorFormatter,
+ Class = require( 'easejs' ).Class,
+ expect = require( 'chai' ).expect;
+
+
+module.exports = {
+ testValidate: function( sut, tests )
+ {
+ it( 'is a ValidatorFormatter', function()
+ {
+ expect( Class.isA( VFmt, sut ) )
+ .to.equal( true, 'Is not a ValidatorFormatter' );
+ } );
+
+
+ Object.keys( tests ).forEach( function( data )
+ {
+ var failure = false,
+ err = function() { failure = true; },
+ result = '',
+ fmt = '',
+ expected = tests[ data ];
+
+ try
+ {
+ result = sut.parse( data );
+ }
+ catch ( e )
+ {
+ failure = true;
+ }
+
+ if ( expected === false )
+ {
+ it( 'fails on ' + data, function()
+ {
+ expect( failure ).to.equal(
+ true,
+ ( data + " should yield error" )
+ );
+ } );
+
+ return;
+ }
+
+ fmt = sut.retrieve( result );
+
+ var expval = ( ( expected[ 0 ] === true ) ? data : expected[ 0 ] ),
+ retval = ( ( expected[ 1 ] === true )
+ ? data
+ : ( expected[ 1 ] === undefined ) ? data : expected[ 1 ]
+ );
+
+ // convert to store in bucket
+ it ( 'parses "' + data + '" as "' + expval + '"', function()
+ {
+ expect( result ).to.equal(
+ expval,
+ ( data + " !-> " + expval + ' (=>' + result + ')' )
+ );
+ } );
+
+ // retrieve value, formatted
+ it ( 'retrieves "' + result + '" as "' + retval + '"', function()
+ {
+ expect( fmt ).to.equal(
+ retval,
+ ( result + " !~> " + retval + ' (=>' + fmt + ')' )
+ );
+ } );
+ } );
+ }
+};