jstonic/test/util/typesTest.js

101 lines
2.8 KiB
JavaScript

/**
* 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 );
} );
} );
} );
} );