From cb6c4af763c450bc0588e256bf68b567733e14ba Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sat, 5 Nov 2011 08:52:19 -0400 Subject: [PATCH] [#25] Refactored common MemberBuilder validator call assertion logic into a common module --- test/MemberBuilder/MethodTest.js | 57 ++++++++++----------------- test/MemberBuilder/PropTest.js | 57 ++++++++++----------------- test/MemberBuilder/inc-common.js | 67 ++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 72 deletions(-) create mode 100644 test/MemberBuilder/inc-common.js diff --git a/test/MemberBuilder/MethodTest.js b/test/MemberBuilder/MethodTest.js index d025f51..767f754 100644 --- a/test/MemberBuilder/MethodTest.js +++ b/test/MemberBuilder/MethodTest.js @@ -22,6 +22,8 @@ * @package test */ +var shared = require( __dirname + '/inc-common' ); + require( 'common' ).testCase( { caseSetUp: function() @@ -30,45 +32,28 @@ require( 'common' ).testCase( this.testArgs = function( args, name, value, keywords ) { - var pub = _self.members[ 'public' ], - - // prev data - pval_expect = null, - pkeywords_expect = null, - pval_given = null, - pkeywords_given = null - ; - - if ( pub[ name ] ) + shared.testArgs( _self, args, name, value, keywords, function( + prev_default, pval_given, pkey_given + ) { - pval_expect = pub[ name ]; - pkeywords_expect = pval_expect.___$$keywords$$; // XXX + var expected = _self.members[ 'public' ][ name ]; - pval_given = args[ 3 ].member; - pkeywords_given = args[ 4 ]; - } + if ( !expected ) + { + return prev_default; + } - _self.assertEqual( name, args[ 0 ], - 'Incorrect name passed to method validator' - ); - - _self.assertStrictEqual( value, args[ 1 ], - 'Incorrect value passed to method validator' - ); - - _self.assertStrictEqual( keywords, args[ 2 ], - 'Incorrect keywords passed to method validator' - ); - - _self.assertStrictEqual( pval_expect, pval_given, - 'Previous data should contain prev value if overriding, ' + - 'otherwise null' - ); - - _self.assertStrictEqual( pkeywords_expect, pkeywords_given, - 'Previous keywords should contain prev keyword if ' + - 'overriding, otherwise null' - ); + return { + value: { + expected: expected, + given: pval_given.member, + }, + keywords: { + expected: expected.___$$keywords$$, // XXX + given: pkey_given, + }, + }; + } ); }; }, diff --git a/test/MemberBuilder/PropTest.js b/test/MemberBuilder/PropTest.js index 1b5f651..7844cc5 100644 --- a/test/MemberBuilder/PropTest.js +++ b/test/MemberBuilder/PropTest.js @@ -22,6 +22,8 @@ * @package test */ +var shared = require( __dirname + '/inc-common' ); + require( 'common' ).testCase( { caseSetUp: function() @@ -30,45 +32,28 @@ require( 'common' ).testCase( this.testArgs = function( args, name, value, keywords ) { - var pub = _self.members[ 'public' ], - - // prev data - pval_expect = null, - pkeywords_expect = null, - pval_given = null, - pkeywords_given = null - ; - - if ( pub[ name ] ) + shared.testArgs( _self, args, name, value, keywords, function( + prev_default, pval_given, pkey_given + ) { - pval_expect = pub[ name ][ 0 ]; - pkeywords_expect = pub[ name ][ 1 ]; + var expected = _self.members[ 'public' ][ name ]; - pval_given = args[ 3 ].member[ 0 ]; - pkeywords_given = args[ 4 ]; - } + if ( !expected ) + { + return prev_default; + } - _self.assertEqual( name, args[ 0 ], - 'Incorrect name passed to property validator' - ); - - _self.assertStrictEqual( value, args[ 1 ], - 'Incorrect value passed to property validator' - ); - - _self.assertStrictEqual( keywords, args[ 2 ], - 'Incorrect keywords passed to property validator' - ); - - _self.assertStrictEqual( pval_expect, pval_given, - 'Previous data should contain prev value if overriding, ' + - 'otherwise null' - ); - - _self.assertStrictEqual( pkeywords_expect, pkeywords_given, - 'Previous keywords should contain prev keyword if ' + - 'overriding, otherwise null' - ); + return { + value: { + expected: expected[ 0 ], + given: pval_given.member[ 0 ], + }, + keywords: { + expected: expected[ 1 ], + given: pkey_given, + }, + }; + } ); }; }, diff --git a/test/MemberBuilder/inc-common.js b/test/MemberBuilder/inc-common.js new file mode 100644 index 0000000..0a7250d --- /dev/null +++ b/test/MemberBuilder/inc-common.js @@ -0,0 +1,67 @@ +/** + * Shared functions for MemberBuilder tests + * + * Copyright (C) 2010 Mike Gerwitz + * + * This file is part of ease.js. + * + * ease.js is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser 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 Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * @author Mike Gerwitz + * @package test + */ + +/** + * Perform common assertions on validator arguments + * + * @param {Object} testcase test case being executed + * @param {arguments} args arguments to check + * @param {string} name member name + * @param {*} value expected value + * @param {Object} keywords expected keywords + * @param {function()} prevLookup function to use to look up prev member data + * + * @return {undefined} + */ +exports.testArgs = function( testcase, args, name, value, keywords, prevLookup ) +{ + var prev = { + value: { expected: null, given: args[ 3 ] }, + keywords: { expected: null, given: args[ 4 ] }, + }; + + prev = prevLookup( prev, prev.value.given, prev.keywords.given ); + + testcase.assertEqual( name, args[ 0 ], + 'Incorrect name passed to validator' + ); + + testcase.assertStrictEqual( value, args[ 1 ], + 'Incorrect value passed to validator' + ); + + testcase.assertStrictEqual( keywords, args[ 2 ], + 'Incorrect keywords passed to validator' + ); + + testcase.assertStrictEqual( prev.value.expected, prev.value.given, + 'Previous data should contain prev value if overriding, ' + + 'otherwise null' + ); + + testcase.assertStrictEqual( prev.keywords.expected, prev.keywords.given, + 'Previous keywords should contain prev keyword if ' + + 'overriding, otherwise null' + ); +};