util.types added
This spawned from a refactoring for the Eventable conformance test case; these will be useful elsewhere.master
parent
ef4040e13a
commit
0d6a2fc652
|
@ -0,0 +1,64 @@
|
|||
/**
|
||||
* Collections of common ECMAScript 3 primitive values
|
||||
*
|
||||
* Copyright (C) 2014 Mike Gerwitz
|
||||
*
|
||||
* This file is part of jsTonic.
|
||||
*
|
||||
* jstonic 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/>.
|
||||
*
|
||||
* These values are intended to provide data useful in assertions for type
|
||||
* checking; they cover the primitive ECMAScript 3 types.
|
||||
*
|
||||
* Where the primitive type has values that may evaluate to true or false
|
||||
* in a conditional (such that `!!value` is `true` or `false), examples of
|
||||
* both such values are provided.
|
||||
*/
|
||||
|
||||
let _t_bool = [ true, false ],
|
||||
_t_func = [ ()=>{} ],
|
||||
_t_num = [ NaN, Infinity, 0, 1 ],
|
||||
_t_obj = [ null, {}, [] ],
|
||||
_t_str = [ '', 'string' ],
|
||||
_t_undef = [ undefined ],
|
||||
|
||||
_t_all = [].concat(
|
||||
_t_bool, _t_func, _t_num, _t_obj, _t_str, _t_undef
|
||||
);
|
||||
|
||||
let _tgen = () => [].concat.apply( arguments );
|
||||
|
||||
|
||||
module.exports = {
|
||||
primitives: [ 'boolean', 'function', 'number',
|
||||
'object', 'string', 'undefined' ],
|
||||
|
||||
typevals: {
|
||||
all: _t_all,
|
||||
bool: _t_bool,
|
||||
func: _t_func,
|
||||
num: _t_num,
|
||||
obj: _t_obj,
|
||||
str: _t_str,
|
||||
undef: _t_undef,
|
||||
|
||||
nonbool: _tgen( _t_str, _t_undef, _t_num, _t_obj, _t_func ),
|
||||
nonfunc: _tgen( _t_str, _t_bool, _t_undef, _t_num, _t_obj ),
|
||||
nonnum: _tgen( _t_str, _t_bool, _t_undef, _t_obj, _t_func ),
|
||||
nonobj: _tgen( _t_str, _t_bool, _t_undef, _t_num, _t_func ),
|
||||
nonstr: _tgen( _t_bool, _t_undef, _t_num, _t_obj, _t_func ),
|
||||
nonundef: _tgen( _t_str, _t_bool, _t_num, _t_obj, _t_func ),
|
||||
},
|
||||
};
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
/**
|
||||
* Tests integrity of the collections of common ECMAScript primitive values
|
||||
*
|
||||
* Copyright (C) 2014 Mike Gerwitz
|
||||
*
|
||||
* This file is part of jsTonic.
|
||||
*
|
||||
* jstonic 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/>.
|
||||
*/
|
||||
|
||||
let sut = require( '../../' ).util.types,
|
||||
expect = require( 'chai' ).expect;
|
||||
|
||||
let typevals = sut.typevals;
|
||||
|
||||
|
||||
describe( 'util.types', () =>
|
||||
{
|
||||
let types_table = sut.primitives.reduce(
|
||||
( ( obj, value ) => ( obj[ value ] = true ) && obj ),
|
||||
{}
|
||||
);
|
||||
|
||||
describe( 'typevals', () =>
|
||||
{
|
||||
let tassert = ( type, values ) =>
|
||||
{
|
||||
it( 'is present in primitives set', () =>
|
||||
expect( types_table[ type ] ).to.be.true );
|
||||
|
||||
it( `are all of type '${type}'`, () =>
|
||||
values.forEach( value =>
|
||||
expect( typeof value ).to.equal( type ) ) );
|
||||
};
|
||||
|
||||
let tf = values =>
|
||||
it( 'contains both true and false values', () =>
|
||||
{
|
||||
let t, f;
|
||||
values.forEach( value => value ? t = true : f = true );
|
||||
expect( t && f ).is.true;
|
||||
} );
|
||||
|
||||
describe( 'bool', () =>
|
||||
{
|
||||
tassert( 'boolean', typevals.bool );
|
||||
tf( typevals.bool );
|
||||
} );
|
||||
|
||||
describe( 'func', () =>
|
||||
tassert( 'function', typevals.func ) );
|
||||
|
||||
describe( 'num', () =>
|
||||
{
|
||||
tassert( 'number', typevals.num );
|
||||
tf( typevals.num );
|
||||
} );
|
||||
|
||||
describe( 'obj', () =>
|
||||
{
|
||||
tassert( 'object', typevals.obj );
|
||||
tf( typevals.obj );
|
||||
} );
|
||||
|
||||
describe( 'str', () =>
|
||||
{
|
||||
tassert( 'string', typevals.str );
|
||||
tf( typevals.str );
|
||||
} );
|
||||
|
||||
describe( 'undef', () =>
|
||||
tassert( 'undefined', typevals.undef ) );
|
||||
|
||||
describe( 'all', () =>
|
||||
{
|
||||
it( 'contains all primitive types', () =>
|
||||
{
|
||||
let found = {};
|
||||
|
||||
typevals.all.forEach( value =>
|
||||
found[ typeof value ] = true );
|
||||
|
||||
sut.primitives.forEach( type =>
|
||||
expect( found[ type ] ).to.be.true );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
Loading…
Reference in New Issue