Skip to content

Commit

Permalink
remove todo, add tests, #204
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Feb 27, 2019
1 parent 87733d6 commit 09595a1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
11 changes: 7 additions & 4 deletions js/ValidatorDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,17 @@ define( require => {
},

/**
* @param {Object} validator - object which may or may not contain validation keys
* @param {ValidatorDef} validator - object which may or may not contain validation keys
* @returns {boolean}
* @public
*/
containsValidatorKey( validator ) {

// TODO: garbage-free implementation, see https://github.com/phetsims/axon/issues/204
return _.intersection( VALIDATOR_KEYS, Object.keys( validator ) ).length > 0;
for ( let i = 0; i < VALIDATOR_KEYS.length; i++ ) {
if ( validator.hasOwnProperty( VALIDATOR_KEYS[ i ] ) ) {
return true;
}
}
return false;
},

/**
Expand Down
48 changes: 42 additions & 6 deletions js/ValidatorDefTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,52 @@ define( require => {
QUnit.module( 'Validator' );

// Note that many validation tests are in PropertyTests
QUnit.test( 'Test validate and ValidatorDef', assert => {
QUnit.test( 'Test validate and ValidatorDef.isValidValue', assert => {

assert.ok( validate( 3, { validValues: [ 1, 2, 3 ] } ) );
assert.ok( validate( [], { valueType: Array } ) );
window.assert && assert.throws( () => !validate( 4, { validValues: [ 1, 2, 3 ] } ), 'invalid number' );
window.assert && assert.throws( () => !validate( 'hello', { valueType: Array } ), 'string isn\'t Array' );
window.assert && assert.throws( () => validate( 4, { validValues: [ 1, 2, 3 ] } ), 'invalid number' );
window.assert && assert.throws( () => validate( 'hello', { valueType: Array } ), 'string isn\'t Array' );

window.assert && assert.throws( () => !ValidatorDef.validateValidator( {
assert.ok( ValidatorDef.isValueValid( 3, { validValues: [ 1, 2, 3 ] } ) );
assert.ok( ValidatorDef.isValueValid( [], { valueType: Array } ) );

assert.ok( ValidatorDef.isValueValid( 7, { valueType: 'number', isValidValue: v => v > 5 } ) );
assert.ok( !ValidatorDef.isValueValid( 7, { valueType: 'number', isValidValue: v => v > 7 } ) );
assert.ok( !ValidatorDef.isValueValid( 7, { valueType: 'number', isValidValue: v => v < 3 } ) );

} );

QUnit.test( 'Test containsValidatorKey', assert => {
assert.ok( ValidatorDef.containsValidatorKey( { validValues: [] }, 'has key validValues' ) );
assert.ok( !ValidatorDef.containsValidatorKey( { shmalidValues: [] }, 'does not have key: validValues' ) );
assert.ok( ValidatorDef.containsValidatorKey( {
validValues: [],
valueType: []
}, 'does have keys: valueType and validValues' ) );
assert.ok( ValidatorDef.containsValidatorKey( {
validValue: [],
valueType: []
}, 'still have valueType and be ok even though it doesn\'t have validValues' ) );
} );


QUnit.test( 'Test isValidValidator and validateValidator', assert => {
window.assert && assert.throws( () => ValidatorDef.validateValidator( {
valueType: Array,
isValidValue: 4
} ), 'isValidValue should be function' );

window.assert && assert.throws( () => ValidatorDef.isValidValidator( {
valueType: Array,
validValues: [ 'hi' ]

}, { assertions: true } ), 'validValues contains invalid value' );


assert.ok( ValidatorDef.isValidValidator( { valueType: 'number' } ), 'good valueType' );
assert.ok( !ValidatorDef.isValidValidator( { validValue: 'number' } ), 'no validator keys supplied' );
assert.ok( !ValidatorDef.isValidValidator( { validValue: 4 } ), 'no validator keys supplied' );
assert.ok( !ValidatorDef.isValidValidator( { valueType: 'blaradysharady' } ), 'invalid valueType string' );
assert.ok( ValidatorDef.isValidValidator( { isValidValue: () => {} } ), 'isValidValue is a function' );
assert.ok( !ValidatorDef.isValidValidator( { isValidValue: 'hi' } ), 'isValidValue should not be string' );
} );
} );

0 comments on commit 09595a1

Please sign in to comment.