some simple shorthand helpers.
[catagits/HTML-Zoom.git] / t / actions.t
CommitLineData
1cf03540 1use strictures 1;
456a815d 2use Test::More;
3
4use HTML::Zoom::Parser::BuiltIn;
5use HTML::Zoom::Producer::BuiltIn;
6use HTML::Zoom::SelectorParser;
7use HTML::Zoom::FilterBuilder;
8use HTML::Zoom::FilterStream;
9
10my $tmpl = <<END;
11<body>
12 <div class="main">
13 <span class="hilight name">Bob</span>
14 <span class="career">Builder</span>
15 <hr />
16 </div>
17</body>
18END
19
d80786d0 20sub src_stream { HTML::Zoom::Parser::BuiltIn->new->html_to_stream($tmpl); }
456a815d 21
d80786d0 22sub html_sink { HTML::Zoom::Producer::BuiltIn->new->html_from_stream($_[0]) }
456a815d 23
24my $fb = HTML::Zoom::FilterBuilder->new;
25
26my $sp = HTML::Zoom::SelectorParser->new;
27
28sub filter {
29 my ($stream, $sel, $cb) = @_;
30 return HTML::Zoom::FilterStream->new({
31 stream => $stream,
32 match => $sp->parse_selector($sel),
33 filter => do { local $_ = $fb; $cb->($fb) }
34 });
35}
36
37sub run_for (&;$) {
38 my $cb = shift;
39 (html_sink
40 (filter
41 src_stream,
42 (shift or '.main'),
43 $cb
44 )
45 )
46}
47
8f962884 48my ($expect, @ev);
49
50($expect = $tmpl) =~ s/class="main"/class="foo"/;
51
52is(
53 run_for { $_->set_attribute({ name => 'class', value => 'foo' }) },
54 $expect,
55 'set attribute on existing attribute'
56);
57
bf07288f 58($expect = $tmpl) =~ s/class="main"/class="foo"/;
59
60is(
61 run_for { $_->set_attr({ name => 'class', value => 'foo' }) },
62 $expect,
63 'set attribute on existing attribute (shorthand)'
64);
65
8f962884 66($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
67
68is(
69 run_for { $_->set_attribute({ name => 'foo', value => 'bar' }) },
70 $expect,
71 'set attribute on non existing attribute'
72);
73
74($expect = $tmpl) =~ s/class="main"/class="main foo"/;
75
76is(
2daa653a 77 run_for { $_->add_to_attribute({ name => 'class', value => 'foo' }) },
8f962884 78 $expect,
79 'add attribute on existing attribute'
80);
81
bf07288f 82($expect = $tmpl) =~ s/class="main"/class="main foo"/;
83
84is(
85 run_for { $_->add_class('foo') },
86 $expect,
87 'add attribute on existing attribute (shorthand)'
88);
89
90($expect = $tmpl) =~ s/class="main"/class="main" id="foo"/;
91
92is(
93 run_for { $_->set_id('foo') },
94 $expect,
95 'set_id (shorthand)'
96);
97
8f962884 98($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
99
100is(
2daa653a 101 run_for { $_->add_to_attribute({ name => 'foo', value => 'bar' }) },
8f962884 102 $expect,
103 'add attribute on non existing attribute'
104);
105
106($expect = $tmpl) =~ s/ class="main"//;
107
108is(
109 run_for { $_->remove_attribute({ name => 'class' }) },
110 $expect,
111 'remove attribute on existing attribute'
112);
113
114is(
115 run_for { $_->remove_attribute({ name => 'foo' }) },
116 $tmpl,
117 'remove attribute on non existing attribute'
118);
119
5cac799e 120($expect = $tmpl) =~ s/ class="main"//;
121
122is(
123 run_for {
124 $_->transform_attribute({
125 name => 'class',
126 code => sub {
127 my $a = shift;
128 return if $a eq 'main';
129 return $a;
130 },
131 })
132 },
133 $expect,
134 'transform_attribute deletes the attr if code returns undef',
135 );
136
137($expect = $tmpl) =~ s/ class="main"/ class="moan"/;
138
139is(
140 run_for {
141 $_->transform_attribute({
142 name => 'class',
143 code => sub {
144 ( my $b = shift ) =~ s/main/moan/;
145 $b
146 },
147 })
148 },
149 $expect,
150 'transform_attribute transforms something',
151 );
152
153($expect = $tmpl) =~ s/ class="main"/ class="main" noggin="zonk"/;
154
155is(
156 run_for {
157 $_->transform_attribute({
158 name => 'noggin',
159 code => sub { 'zonk' },
160 })
161 },
162 $expect,
163 'transform_attribute adds attribute if not there before',
164 );
165
166is(
167 run_for {
168 $_->transform_attribute({
169 name => 'noggin',
170 code => sub { },
171 })
172 },
173 $tmpl,
174 'transform_attribute on nonexistent att does not add it if code returns undef',
175 );
176
177
8f962884 178($expect = $tmpl) =~ s/(?=<div)/O HAI/;
456a815d 179
180my $ohai = [ { type => 'TEXT', raw => 'O HAI' } ];
181
182is(
183 run_for { $_->add_before($ohai) },
184 $expect,
185 'add_before ok'
186);
187
188($expect = $tmpl) =~ s/(?<=<\/div>)/O HAI/;
189
190is(
191 run_for { $_->add_after($ohai) },
192 $expect,
193 'add_after ok'
194);
195
196($expect = $tmpl) =~ s/(?<=class="main">)/O HAI/;
197
198is(
865bb5d2 199 run_for { $_->prepend_content($ohai) },
456a815d 200 $expect,
865bb5d2 201 'prepend_content ok'
456a815d 202);
203
204($expect = $tmpl) =~ s/<hr \/>/<hr>O HAI<\/hr>/;
205
206is(
865bb5d2 207 (run_for { $_->prepend_content($ohai) } 'hr'),
456a815d 208 $expect,
865bb5d2 209 'prepend_content ok with in place close'
456a815d 210);
211
212is(
213 run_for { $_->replace($ohai) },
214'<body>
215 O HAI
216</body>
217',
218 'replace ok'
219);
220
8f962884 221@ev = ();
c86c0ce2 222
223is(
224 run_for { $_->collect({ into => \@ev }) },
225 '<body>
226
227</body>
228',
229 'collect removes without passthrough'
230);
231
232is(
233 HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev),
234 '<div class="main">
235 <span class="hilight name">Bob</span>
236 <span class="career">Builder</span>
237 <hr />
238 </div>',
239 'collect collected right events'
240);
41153e87 241
8f962884 242@ev = ();
41153e87 243
244is(
865bb5d2 245 run_for { $_->collect({ into => \@ev, content => 1 }) },
8f962884 246 '<body>
247 <div class="main"></div>
248</body>
249',
865bb5d2 250 'collect w/content removes correctly'
41153e87 251);
252
253is(
8f962884 254 HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev),
255 '
256 <span class="hilight name">Bob</span>
257 <span class="career">Builder</span>
258 <hr />
259 ',
865bb5d2 260 'collect w/content collects correctly'
41153e87 261);
262
11cc25dd 263is(
865bb5d2 264 run_for { $_->replace($ohai, { content => 1 }) },
11cc25dd 265 '<body>
266 <div class="main">O HAI</div>
267</body>
268',
865bb5d2 269 'replace w/content'
11cc25dd 270);
271
a25b8d4d 272($expect = $tmpl) =~ s/(?=<\/div>)/O HAI/;
273
274is(
865bb5d2 275 run_for { $_->append_content($ohai) },
a25b8d4d 276 $expect,
865bb5d2 277 'append content ok'
a25b8d4d 278);
279
865bb5d2 280my $r_content = sub { my $r = shift; sub { $_->replace($r, { content => 1 }) } };
3cdbc13f 281
282is(
283 run_for {
284 $_->repeat(
285 [
286 sub {
287 filter
865bb5d2 288 filter($_ => '.name' => $r_content->('mst'))
289 => '.career' => $r_content->('Chainsaw Wielder')
3cdbc13f 290 },
291 sub {
292 filter
865bb5d2 293 filter($_ => '.name' => $r_content->('mdk'))
294 => '.career' => $r_content->('Adminion')
3cdbc13f 295 },
296 ]
297 )
298 },
299 q{<body>
300 <div class="main">
301 <span class="hilight name">mst</span>
302 <span class="career">Chainsaw Wielder</span>
303 <hr />
304 </div><div class="main">
305 <span class="hilight name">mdk</span>
306 <span class="career">Adminion</span>
307 <hr />
308 </div>
309</body>
310},
311 'repeat ok'
312);
313
865bb5d2 314is(
315 run_for {
316 $_->repeat_content(
317 [
318 sub {
319 filter
320 filter($_ => '.name' => $r_content->('mst'))
321 => '.career' => $r_content->('Chainsaw Wielder')
322 },
323 sub {
324 filter
325 filter($_ => '.name' => $r_content->('mdk'))
326 => '.career' => $r_content->('Adminion')
327 },
328 ]
329 )
330 },
331 q{<body>
332 <div class="main">
333 <span class="hilight name">mst</span>
334 <span class="career">Chainsaw Wielder</span>
335 <hr />
336
337 <span class="hilight name">mdk</span>
338 <span class="career">Adminion</span>
339 <hr />
340 </div>
341</body>
342},
343 'repeat_content ok'
344);
3cdbc13f 345
dae33531 346is(
347 run_for {
348 my @between;
349 $_->repeat_content(
350 [
351 sub {
b5a48c47 352 HTML::Zoom::ArrayStream->new({ array => [
dae33531 353 (filter
354 filter($_ => '.name' => $r_content->('mst'))
355 => '.career' => $r_content->('Chainsaw Wielder')),
b5a48c47 356 HTML::Zoom::ArrayStream->new({ array => \@between })
357 ] })->flatten
dae33531 358 },
359 sub {
360 filter
361 filter($_ => '.name' => $r_content->('mdk'))
362 => '.career' => $r_content->('Adminion')
363 },
364 ],
365 { filter => sub {
366 filter $_[0] => 'hr' => sub { $_->collect({ into => \@between }) }
367 }
368 }
369 )
370 },
371 q{<body>
372 <div class="main">
373 <span class="hilight name">mst</span>
374 <span class="career">Chainsaw Wielder</span>
375 <hr />
376 <span class="hilight name">mdk</span>
377 <span class="career">Adminion</span>
378
379 </div>
380</body>
381},
382 'repeat_content with filter ok'
383);
384
f8ed299b 385is(
386 run_for {
387 my @between;
388 $_->repeat_content(
389 [
390 sub {
391 filter
392 filter($_ => '.name' => $r_content->('mst'))
393 => '.career' => $r_content->('Chainsaw Wielder')
394 },
395 sub {
396 filter
397 filter($_ => '.name' => $r_content->('mdk'))
398 => '.career' => $r_content->('Adminion')
399 },
400 ],
401 { repeat_between => 'hr' }
402 )
403 },
404 q{<body>
405 <div class="main">
406 <span class="hilight name">mst</span>
407 <span class="career">Chainsaw Wielder</span>
408 <hr />
409 <span class="hilight name">mdk</span>
410 <span class="career">Adminion</span>
411
412 </div>
413</body>
414},
415 'repeat_content using repeat_between ok'
416);
417
456a815d 418done_testing;