dwim selects and bugfixes, new transform_attribute method and handled trailing '...
[catagits/HTML-Zoom.git] / t / repeat_bugfixes.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use HTML::Zoom;
5 use Test::More;
6
7 ok my $zoom = HTML::Zoom->from_html(<<HTML);
8 <html>
9   <head>
10     <title>Hi!</title>
11   </head>
12   <body id="content-area">
13     <h1>Test</h1>
14     <div>
15       <p class="first-para">Some Stuff</p>
16       <p class="body-para">More Stuff</p>
17       <p class="body-para">Even More Stuff</p>
18       <ol>
19         <li class="first-item odd">First</li>
20         <li class="body-items even">Stuff A</li>
21         <li class="body-items odd">Stuff B</li>
22         <li class="body-items even">Stuff C</li>
23         <li class="last-item odd">Last</li>
24       </ol>
25       <ul class="items">
26         <li class="first">space</li>
27         <li class="second">space</li>
28       </ul>
29       <p class="body-para">Even More stuff</p>
30       <p class="last-para">Some Stuff</p>
31     </div>
32     <div id="blocks">
33       <h2 class="h2-item">Sub Item</h2>
34     </div>
35     <ul id="people">
36         <li class="name">name</li>
37         <li class="email">email</li>
38     </ul>
39     <ul id="people2">
40         <li class="name">name</li>
41         <li class="email">email</li>
42     </ul>
43     <ul id="object">
44         <li class="aa">AA</li>
45         <li class="bb">BB</li>
46     </ul>
47     <p id="footer">Copyright 2222</p>
48   </body>
49 </html>
50 HTML
51
52 ## Stub for testing the fill method to be
53
54 ok my $title = sub {
55   my ($z, $content) = @_;
56   $z = $z->select('title')->replace_content($content);
57   return $z;
58 };
59
60 {
61   ok my $z = HTML::Zoom
62     ->from_html(q[<ul><li>Test</li></ul>])
63     ->select('ul')
64     ->repeat_content(
65       [
66         sub { $_->select('li')->replace_content('Real Life1') },
67         sub { $_->select('li')->replace_content('Real Life2') },
68         sub { $_->select('li')->replace_content('Real Life3') },
69       ],
70     )
71     ->to_html;
72
73   is $z, '<ul><li>Real Life1</li><li>Real Life2</li><li>Real Life3</li></ul>',
74     'Got correct from repeat_content';
75 }
76
77 {
78   ok my $z = HTML::Zoom
79     ->from_html(q[<ul><li>Test</li></ul>])
80     ->select('ul')
81     ->repeat_content([
82       map { my $i = $_; +sub {$_->select('li')->replace_content("Real Life$i")} } (1,2,3)
83     ])
84     ->to_html;
85
86   is $z, '<ul><li>Real Life1</li><li>Real Life2</li><li>Real Life3</li></ul>',
87     'Got correct from repeat_content';
88 }
89
90
91 use HTML::Zoom::CodeStream;
92 sub code_stream (&) {
93   my $code = shift;
94   return sub {
95     HTML::Zoom::CodeStream->new({
96       code => $code,
97     });
98   }
99 }
100
101 {
102   my @list = qw(foo bar baz);
103   ok my $z = HTML::Zoom
104     ->from_html(q[<ul><li>Test</li></ul>])
105     ->select('ul')
106     ->repeat_content(code_stream {
107       if (my $name = shift @list) {
108         return sub { $_->select('li')->replace_content($name) };
109       } else {
110         return
111       }
112     })
113     ->to_html;
114   
115   is $z, '<ul><li>foo</li><li>bar</li><li>baz</li></ul>',
116     'Got correct from repeat_content';
117 }
118
119 {
120   my @list = qw(foo bar baz);
121   ok my $z = HTML::Zoom
122     ->from_html(q[<ul><li>Test</li></ul>])
123     ->select('ul')
124     ->repeat_content(sub {
125       HTML::Zoom::CodeStream->new({
126         code => sub {
127           if (my $name = shift @list) {
128             return sub { $_->select('li')->replace_content($name) };
129           } else {
130             return
131           }
132         }
133       });
134     })
135     ->to_html;
136   
137   is $z, '<ul><li>foo</li><li>bar</li><li>baz</li></ul>',
138     'Got correct from repeat_content';
139 }
140
141 done_testing;