additional test
[catagits/HTML-Zoom.git] / t / dwim-autoload.t
CommitLineData
94a3ddd9 1use strict;
2use warnings FATAL => 'all';
3
4use HTML::Zoom;
5use Test::More;
6
7ok 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>
50HTML
51
52ok ! eval { $zoom->non_method('.first-param' => 'First!'); 1},
53 'Properly die on bad method';
54
55ok my $title = sub {
56 my ($z, $content) = @_;
57 $z = $z->replace_content(title =>$content);
58 return $z;
59};
60
61ok my $dwim = $zoom
62 ->$title('Hello NYC')
63 ->replace_content('.first-para' => 'First!')
64 ->replace_content('.last-para' => 'Last!')
65 ->add_to_attribute(p => class => 'para')
66 ->prepend_content('.first-item' => [{type=>'TEXT', raw=>'FIRST: '}])
67 ->prepend_content('.last-item' => [{type=>'TEXT', raw=>'LAST: '}])
68 ->replace_content('title' => 'Hello World')
69 ->repeat_content(
70 '.items' => [
71 map {
72 my $v = $_;
73 sub {
74 $_->replace_content('.first' => $v)
75 ->replace_content('.second' => $v);
76 },
77 } (111,222)
78 ]
79 )
80 ->to_html;
81
82like(
83 HTML::Zoom
84 ->from_html(q[<title>Hi!</title>])
85 ->$title('Hello NYC')
86 ->to_html,
87 qr/Hello NYC/,
88 'Got correct title(custom subref)'
89);
90
91like(
92 HTML::Zoom
93 ->from_html(q[<p>Hi!</p>])
94 ->replace_content(p=>'Ask not what your country can do for you...')
95 ->to_html,
96 qr/Ask not what your country can do for you\.\.\./,
97 'Got correct from replace_content'
98);
99
100like(
101 HTML::Zoom
102 ->from_html(q[<p class="first">Hi!</p>])
103 ->add_to_attribute('p', {name => 'class', value => 'para'})
104 ->to_html,
105 qr/first para/,
106 'Got correct from add_to_attribute'
107);
108
109like(
110 HTML::Zoom
111 ->from_html(q[<p class="first">Hi!</p>])
112 ->add_to_attribute('p', class => 'para')
113 ->to_html,
114 qr/first para/,
115 'Got correct from add_to_attribute'
116);
117
118like(
119 HTML::Zoom
120 ->from_html(q[<p class="first">Hi!</p>])
121 ->set_attribute('p', class => 'para')
122 ->to_html,
123 qr/class="para"/,
124 'Got correct from set_attribute'
125);
126
127like(
128 HTML::Zoom
129 ->from_html(q[<p class="first">Hi!</p>])
130 ->set_attribute('p', {name => 'class', value => 'para'})
131 ->to_html,
132 qr/class="para"/,
133 'Got correct from set_attribute'
134);
135
136is(
137 HTML::Zoom
138 ->from_html(q[<p class="first">Hi!</p>])
139 ->add_before(p=>[{type=>'TEXT', raw=>'added before'}])
140 ->to_html,
141 'added before<p class="first">Hi!</p>',
142 'Got correct from add_before'
143);
144
145is(
146 HTML::Zoom
147 ->from_html(q[<p class="first">Hi!</p>])
148 ->add_after(p=>[{type=>'TEXT', raw=>'added after'}])
149 ->to_html,
150 '<p class="first">Hi!</p>added after',
151 'Got correct from add_after'
152);
153
154is(
155 HTML::Zoom
156 ->from_html(q[<p class="first">Hi!</p>])
157 ->prepend_content(p=>[{type=>'TEXT', raw=>'prepend_content'}])
158 ->to_html,
159 '<p class="first">prepend_contentHi!</p>',
160 'Got correct from prepend_content'
161);
162
163is(
164 HTML::Zoom
165 ->from_html(q[<p class="first">Hi!</p>])
166 ->append_content(p=>[{type=>'TEXT', raw=>'append_content'}])
167 ->to_html,
168 '<p class="first">Hi!append_content</p>',
169 'Got correct from append_content'
170);
171
172{
173 my @body;
174 ok my $body = HTML::Zoom
175 ->from_html(q[<div>My Stuff Is Here</div>])
176 ->collect_content(div => { into => \@body, passthrough => 1})
177 ->to_html, 'Collected Content';
178
179 is $body, "<div>My Stuff Is Here</div>", "Got expected";
180
181 is(
182 HTML::Zoom->from_events(\@body)->to_html,
183 'My Stuff Is Here',
184 'Collected the right stuff',
185 );
186}
187
188like(
189 HTML::Zoom
190 ->from_html(q[<ul class="items"><li class="first">first</li><li class="second">second</li></ul>])
191 ->repeat_content(
192 '.items' => [
193 map {
194 my $v = $_;
195 sub {
196 $_->replace_content('.first' => $v)
197 ->replace_content('.second' => $v);
198 },
199 } (111,222)
200 ]
201 )
202 ->to_html,
203 qr[<ul class="items"><li class="first">111</li><li class="second">111</li><li class="first">222</li><li class="second">222</li></ul>],
204 'Got correct list'
205);
206
207{
208 ok my $z = HTML::Zoom
209 ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
210 ->add_before(p=>'added before');
211
212 is $z->to_html, '<div>Life, whatever</div>added before<p class="first">Hi!</p>',
213 'Got correct from add_before';
214}
215
216{
217 ok my $z = HTML::Zoom
218 ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
219 ->add_after(p=>'added after');
220
221 is $z->to_html, '<div>Life, whatever</div><p class="first">Hi!</p>added after',
222 'Got correct from add_after';
223}
224
225{
226 ok my $z = HTML::Zoom
227 ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
228 ->append_content(p=>'appended');
229
230 is $z->to_html, '<div>Life, whatever</div><p class="first">Hi!appended</p>',
231 'Got correct from append_content';
232}
233
234{
235 ok my $z = HTML::Zoom
236 ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
237 ->prepend_content(p=>'prepended');
238
239
240 is $z->to_html, '<div>Life, whatever</div><p class="first">prependedHi!</p>',
241 'Got correct from prepend_content';
242}
243
244{
245 ok my $z = HTML::Zoom
246 ->from_html(q[<ul><li>Test</li></ul>])
247 ->select('ul')
248 ->repeat_content(
249 [
250 sub { $_->select('li')->replace_content('Real Life1') },
251 sub { $_->select('li')->replace_content('Real Life2') },
252 sub { $_->select('li')->replace_content('Real Life3') },
253 ],
254 )
255 ->to_html;
256
257 is $z, '<ul><li>Real Life1</li><li>Real Life2</li><li>Real Life3</li></ul>',
258 'Got correct from repeat_content';
259}
260
261{
262 ok my $z = HTML::Zoom
263 ->from_html(q[<ul><li>Test</li></ul>])
264 ->select('ul')
265 ->repeat_content([
266 map { my $i = $_; +sub {$_->select('li')->replace_content("Real Life$i")} } (1,2,3)
267 ])
268 ->to_html;
269
270 is $z, '<ul><li>Real Life1</li><li>Real Life2</li><li>Real Life3</li></ul>',
271 'Got correct from repeat_content';
272}
273
274
275use HTML::Zoom::CodeStream;
276sub code_stream (&) {
277 my $code = shift;
278 return sub {
279 HTML::Zoom::CodeStream->new({
280 code => $code,
281 });
282 }
283}
284
285{
286 my @list = qw(foo bar baz);
287 ok my $z = HTML::Zoom
288 ->from_html(q[<ul><li>Test</li></ul>])
289 ->select('ul')
290 ->repeat_content(code_stream {
291 if (my $name = shift @list) {
292 return sub { $_->select('li')->replace_content($name) };
293 } else {
294 return
295 }
296 })
297 ->to_html;
298
299 is $z, '<ul><li>foo</li><li>bar</li><li>baz</li></ul>',
300 'Got correct from repeat_content';
301}
302
303{
304 my @list = qw(foo bar baz);
305 ok my $z = HTML::Zoom
306 ->from_html(q[<ul><li>Test</li></ul>])
307 ->select('ul')
308 ->repeat_content(sub {
309 HTML::Zoom::CodeStream->new({
310 code => sub {
311 if (my $name = shift @list) {
312 return sub { $_->select('li')->replace_content($name) };
313 } else {
314 return
315 }
316 }
317 });
318 })
319 ->to_html;
320
321 is $z, '<ul><li>foo</li><li>bar</li><li>baz</li></ul>',
322 'Got correct from repeat_content';
323}
324
6af04df5 325{
326 ok my $dwim = HTML::Zoom
327 ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>])
328 ->replace_content({
329 'li.foo' => ['foo'],
330 'li.bar' => ['bar'],
331 })->to_html;
332 is $dwim, '<ul><li class="foo">foo</li><li class="bar">bar</li></ul>',
efc6c054 333 'Multiple selectors from hashref (via replace_content)';
334}
335
336{
337 ok my $dwim = HTML::Zoom
338 ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>])
339 ->set_attribute({
340 'li.foo' => [ class => 'baz' ],
341 'li.bar' => [ class => 'qux' ],
342 })->to_html;
343 is $dwim, '<ul><li class="baz"></li><li class="qux"></li></ul>',
344 'Multiple selectors from hashref (via set_attribute)';
6af04df5 345}
346
94a3ddd9 347done_testing;