root/Web-Scraper/trunk/t/04_callback.t

Revision 2246 (checked in by miyagawa, 13 years ago)

store $node to $_ in the callback

Line 
1 use strict;
2 use Test::Base;
3
4 use Web::Scraper;
5 plan tests => 1 * blocks;
6
7 filters {
8     selector => 'chomp',
9     expected => 'yaml',
10 };
11
12 run {
13     my $block = shift;
14     my @value;
15     my $s = scraper {
16         process $block->selector, sub {
17             my $node = shift;
18             push @value, eval $block->callback;
19             fail $@ if $@;
20         };
21     };
22     $s->scrape($block->html);
23     is_deeply \@value, $block->expected, $block->name;
24 };
25
26 __DATA__
27
28 ===
29 --- html
30 <ul>
31 <li class="foo"><span title="baz">bar1</span></li>
32 <li class="foo"><span title="bad">bar3</span></li>
33 </ul>
34 --- selector
35 li.foo span
36 --- callback
37 $node->attr('title')
38 --- expected
39 - baz
40 - bad
41
42 ===
43 --- html
44 <ul>
45 <li class="foo"><span title="baz">bar1</span></li>
46 <li class="foo"><span title="bad">bar3</span></li>
47 </ul>
48 --- selector
49 li.foo span
50 --- callback
51 $_->attr('title')
52 --- expected
53 - baz
54 - bad
Note: See TracBrowser for help on using the browser.