Fix ->repeat with iterator, make t/repeat.t pass (formerly t/todo-repeat.t)
[catagits/HTML-Zoom.git] / t / repeat.t
1 use strict;
2 use HTML::Zoom;
3 use Test::More;
4
5 my $z = HTML::Zoom->from_html(<<'HTML');
6 <html>
7 <body>
8 <div id="foo"><p></p></div>
9 </body>
10 </html>
11 HTML
12
13 my @list = qw(foo bar baz);
14 my $iter = sub { shift @list };
15
16 my $actual = $z->select("#foo")->repeat(sub {
17     my $e = $iter->() or return;
18     return sub {
19       $_->select("#foo")->set_attribute({name => 'id', value => $e})
20         ->select("p")->replace_content($e);
21     };
22 })->to_html;
23 my $expected = <<'HTML';
24 <html>
25 <body>
26 <div id="foo"><p>foo</p></div><div id="bar"><p>bar</p></div><div id="baz"><p>baz</p></div>
27 </body>
28 </html>
29 HTML
30
31 is($actual, $expected, 'repeat with iterator works');
32
33 @list = qw(foo bar baz);
34 $actual = $z->select("#foo")->repeat([
35   map {
36     my $e = $_;
37     sub {
38       $_->select("#foo")->set_attribute({name => 'id', value => $e})
39         ->select("p")->replace_content($e);
40     }
41   } @list
42 ])->to_html;
43
44 is($actual, $expected, 'repeat with array of transforms works');
45
46 done_testing;