Add test for 'flush_before' option
[catagits/HTML-Zoom.git] / t / flush.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use HTML::Zoom;
5 use HTML::Zoom::CodeStream;
6
7 use Test::More;
8
9
10 # turns iterator into stream
11 sub code_stream (&) {
12   my $code = shift;
13   return sub {
14     HTML::Zoom::CodeStream->new({
15       code => $code,
16     });
17   }
18 }
19
20 my $tmpl = <<'TMPL';
21 <body>
22   <div class="item">
23     <div class="item-name"></div>
24   </div>
25 </body>
26 TMPL
27
28 my $zoom = HTML::Zoom->from_html($tmpl);
29 my @list = qw(foo bar baz);
30
31 foreach my $flush (0..1) {
32
33   # from HTML::Zoom manpage, slightly modified
34   my $z2 = $zoom->select('.item')->repeat(code_stream {
35     if (my $name = shift @list) {
36       return sub { $_->select('.item-name')->replace_content($name) }
37     } else {
38       return
39     }
40   }, { flush_before => $flush });
41
42   my $fh = $z2->to_fh;
43   my $lineno = 0;
44   while (my $chunk = $fh->getline) {
45     $lineno++;
46     # debugging here
47   }
48
49   cmp_ok($lineno, '==', 1+$flush, "flush_before => $flush is $lineno chunks");
50 }
51
52 done_testing;