1use strict; 2use warnings; 3 4use Test::More; 5 6use lib 't/lib'; 7 8use WQTest; 9 10WQTest::test { 11 my $class = shift; 12 13 subtest 'data()' => sub { test_data($class) }; 14 15 subtest 'name()' => sub { test_name($class) }; 16 17 subtest 'id()' => sub { test_id($class) }; 18 19}; 20 21sub test_data { 22 my $class = shift; 23 24 my $wq = $class->new_from_html(q{ 25 <div> 26 <a></a> 27 </div> 28 }); 29 30 subtest setter => sub { 31 $wq->find('a')->data( foo => 'bar' ); 32 pass; 33 }; 34 35 subtest 'getter' => sub { 36 is $wq->find('a')->data('foo') => 'bar'; 37 }; 38 39} 40 41sub test_name { 42 my $class = shift; 43 44 my $wq = $class->new_from_html(q{ 45 <div> 46 <a name="foo"></a> 47 <b></b> 48 <c name="bar" /> 49 </div> 50 }); 51 52 subtest 'getter' => sub { 53 is_deeply [ $wq->find('a,b,c')->name ], [ 'foo', undef, 'bar' ], "getter, list context"; 54 is scalar $wq->find('a,b,c')->name, 'foo', "getter, scalar context"; 55 }; 56 57 subtest setter => sub { 58 $wq->find('a,b,c')->name( 'quux' ); 59 is $wq->find($_)->name => 'quux' for 'a'..'c'; 60 } 61} 62 63 64sub test_id { 65 my $class = shift; 66 67 my $wq = $class->new_from_html(q{ 68 <div> 69 <a></a> 70 <b id="foo"></b> 71 <c id="bar">1</c> 72 <c>2</c> 73 <c id="baz">3</c> 74 </div> 75}); 76 77 is_deeply [ $wq->find('a')->id ] => [ undef ], "no id, list context"; 78 is scalar $wq->find('a')->id => undef, "no id, scalar context"; 79 80 is $wq->find('#foo')->id => 'foo', 'single element'; 81 is scalar($wq->find('#foo')->id) => 'foo', 'single element, scalar context'; 82 83 is_deeply [ $wq->find('c')->id ], [ 'bar', undef, 'baz' ], 'many elements, list context'; 84 is_deeply scalar $wq->find('c')->id, 'bar', 'many elements, scalar context'; 85 86 $wq->find('b')->id('fool'); 87 is $wq->find('#fool')->tagname => 'b', 'change id, scalar'; 88 89 isa_ok $wq->find('c')->id('buz'), 'Web::Query'; 90 91 is $wq->find('c')->id('buz')->size => 1, 'only the first element'; 92 is $wq->find('#buz')->text => 1, "change first element"; 93 94 my $i = 0; 95 $wq->find('c')->id(sub{ 'new_'.$i++ }); 96 97 is $wq->find('#new_'.$_)->size => 1 for 0..2; 98} 99