HTML-Object

 view release on metacpan or  search on metacpan

t/30.xquery-05-utilities-pure.t  view on Meta::CPAN

$HTML::Object::FATAL_ERROR = 0;

# jQuery utility methods - pure (no DOM tree required)

subtest 'inArray' => sub
{
    my $i;
    $i = $.inArray( 5 + 5, [ "8", "9", "10", 10 . '' ] );
    is( $i, 2, 'inArray insensitive to digits or letters' );

    my $arr = [ 4, "Pete", 8, "John" ];
    $i = xQuery->inArray( "John", $arr );
    is( $i, 3, 'inArray searching for a word' );
    $i = xQuery->inArray( 4, $arr );
    is( $i, 0, 'inArray searching for an integer' );
    $i = xQuery->inArray( "Karl", $arr );
    is( $i, -1, 'inArray word not found' );
    $i = xQuery->inArray( "Pete", $arr, 2 );
    is( $i, -1, 'inArray word found, but not after specified fromIndex' );
};

subtest 'isArray' => sub
{
    ok( $.isArray([]), 'isArray' );
};

subtest 'isEmptyObject' => sub
{
    ok( xQuery->isEmptyObject({}), 'isEmptyObject' );
    ok( !xQuery->isEmptyObject({ foo => "bar" }), 'isEmptyObject' );
};

subtest 'isFunction' => sub
{
    sub stub {}
    my $objs = [
        sub{},
        { 'x' => 15, 'y' => 20 },
        undef,
        'stub',
        'sub',
    ];
    ok( xQuery->isFunction( $objs->[0] ), 'isFunction -> sub{}' );
    ok( !xQuery->isFunction( $objs->[1] ), 'isFunction -> $hash' );
    ok( !xQuery->isFunction( $objs->[2] ), 'isFunction -> undef' );
    ok( xQuery->isFunction( $objs->[3] ), 'isFunction -> "stub"' );
    ok( !xQuery->isFunction( $objs->[4] ), 'isFunction -> "sub"' );
};

subtest 'isNumeric' => sub
{
    # true (numeric)
    ok( $.isNumeric( "-10" ), '-10' );
    ok( $.isNumeric( "0" ), '"0"' );
    ok( $.isNumeric( 0xFF ), '0xFF' );
    ok( $.isNumeric( "0xFF" ), '"0xFF"' );
    ok( $.isNumeric( "8e5" ), '8e5' );
    ok( $.isNumeric( "3.1415" ), '3.1415' );
    ok( $.isNumeric( +10 ), '+10' );
    ok( $.isNumeric( 0144 ), 'octal 0144' );
    ok( $.isNumeric( 'nan' ), 'NaN' );
    ok( $.isNumeric( 'inf' ), 'Infinity' );

    # false (non-numeric)
    ok( !$.isNumeric( "-0x42" ), '-0x42' );
    ok( !$.isNumeric( "7.2acdgs" ), '7.2acdgs' );
    ok( !$.isNumeric( "" ), '""' );
    ok( !$.isNumeric( {} ), '{}' );
    ok( !$.isNumeric( undef ), 'undef' );
};

subtest 'isPlainObject' => sub
{
    ok( xQuery->isPlainObject({}), '{}' );
    ok( !xQuery->isPlainObject( "test" ), '"test"' );
};

subtest 'isWindow' => sub
{
    require HTML::Object::DOM::Window;
    require HTML::Object::DOM::Element::IFrame;
    my $window = HTML::Object::DOM::Window->new;
    my $iframe = HTML::Object::DOM::Element::IFrame->new;
    ok( $.isWindow( $window ), '$.isWindow( $window )' );
    ok( !$.isWindow( $iframe ), '$.isWindow( $iframe )' );
};

subtest 'map' => sub
{
    my $arr = [ "a", "b", "c", "d", "e" ];
    local $" = ', ';
    $arr = xQuery->map( $arr, sub
    {
        my( $n, $i ) = @_;
        return( uc( $n ) . $i );
    });
    is( "@$arr", 'A0, B1, C2, D3, E4', 'map return 1 element' );
    $arr = $.map( $arr, sub
    {
        my( $a ) = @_;
        return( $a . $a );
    });
    is( "@$arr", 'A0A0, B1B1, C2C2, D3D3, E4E4', 'map return 1 element' );
    $arr = $.map( [ 0, 1, 2 ], sub
    {
        my( $n ) = @_;
        return( $n + 4 );
    });
    is( "@$arr", '4, 5, 6', 'map change number value' );
    $arr = $.map( [ 0, 1, 2 ], sub
    {
        my( $n ) = @_;
        return( $n > 0 ? $n + 1 : undef );
    });
    is( "@$arr", '2, 3', 'map chnge or remove' );
    $arr = $.map( [ 0, 1, 2 ], sub
    {
        my( $n ) = @_;
        return( [ $n, $n + 1 ] );
        # You can also return as a list
        # return( $n, $n + 1 );



( run in 0.705 second using v1.01-cache-2.11-cpan-7fcb06a456a )