jstonic/test/std/ArrayTest.js

104 lines
2.5 KiB
JavaScript

/**
* Tests pre-es5 array methods
*
* 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/>.
*/
var Sut = require( '../../' ).std.Array,
expect = require( 'chai' ).expect;
describe( 'std.Array', function()
{
describe( '#isArray', function()
{
/**
* The idea is that this should succeed in whatever environment this
* test case is being run.
*/
it( 'uses either ES5 Array.isArray or std.Array._isArray', function()
{
expect( [ Array.isArray, Sut._isArray ] )
.to.include( Sut.isArray );
} );
} );
/**
* Our own implementation
*
* Many of these tests exist not because there are holes in the
* implementation, but to prevent regressions.
*/
describe( '#_isArray', function()
{
it( 'recognizes array primitive', function()
{
_ischk( [], true );
} );
it( 'rejects objects', function()
{
// 0 key is to be tricky
_ischk( { 0: true }, false );
} );
it( 'rejects functions', function()
{
_ischk( function() {}, false );
} );
it( 'rejects strings', function()
{
_ischk( "", false );
} );
it( 'rejects numbers', function()
{
_ischk( 0, false );
} );
it( 'rejects infinity', function()
{
_ischk( Infinity, false );
} );
it( 'rejects NaN', function()
{
_ischk( NaN, false );
} );
it( 'rejects undefined', function()
{
_ischk( undefined, false );
} );
it( 'rejects null', function()
{
_ischk( null, false );
} );
} );
} );
function _ischk( value, expected )
{
expect( Sut._isArray( value ) ).to.equal( expected );
}