HTML-Object

 view release on metacpan or  search on metacpan

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

$HTML::Object::FATAL_ERROR = 0;

# jQuery utility methods

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 'makeArray' => sub
{
    my $blank = <<EOT;
<!DOCTYPE html>
<html lang="en-GB">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <div>First</div>
        <div>Second</div>
        <div>Third</div>
        <div>Fourth</div>
    </body>
</html>
EOT
    my $parser = HTML::Object::DOM->new;
    SKIP:
    {
        my $doc = $parser->parse_data( $blank ) || do
        {
            skip( "Unable to parse blank html: " . $parser->error, 3 );
        };
        HTML::Object::DOM->set_dom( $doc );
        my $elems = $('div');
        my $arr = xQuery->makeArray( $elems );
        if( !defined( $arr ) )
        {
            diag( "Error: ", xQuery->error );
        }
        my @arr2 = reverse( @$arr );
        # $(@arr2, { xq_debug => 4 })->appendTo( 'body' );
        $(@arr2)->appendTo( 'body' );



( run in 0.437 second using v1.01-cache-2.11-cpan-524268b4103 )