Add test for 'flush_before' option
Jakub Narebski [Mon, 10 Jan 2011 13:35:06 +0000 (14:35 +0100)]
'flush_before' is actually option to ->collect() filter, which
underlies the ->replace() and ->repeat() filters.

Working version of example code from HTML::Zoom manpage (search for
'flush_before') is used in this test.

t/flush.t [new file with mode: 0644]

diff --git a/t/flush.t b/t/flush.t
new file mode 100644 (file)
index 0000000..bf60b64
--- /dev/null
+++ b/t/flush.t
@@ -0,0 +1,52 @@
+use strict;
+use warnings FATAL => 'all';
+
+use HTML::Zoom;
+use HTML::Zoom::CodeStream;
+
+use Test::More;
+
+
+# turns iterator into stream
+sub code_stream (&) {
+  my $code = shift;
+  return sub {
+    HTML::Zoom::CodeStream->new({
+      code => $code,
+    });
+  }
+}
+
+my $tmpl = <<'TMPL';
+<body>
+  <div class="item">
+    <div class="item-name"></div>
+  </div>
+</body>
+TMPL
+
+my $zoom = HTML::Zoom->from_html($tmpl);
+my @list = qw(foo bar baz);
+
+foreach my $flush (0..1) {
+
+  # from HTML::Zoom manpage, slightly modified
+  my $z2 = $zoom->select('.item')->repeat(code_stream {
+    if (my $name = shift @list) {
+      return sub { $_->select('.item-name')->replace_content($name) }
+    } else {
+      return
+    }
+  }, { flush_before => $flush });
+
+  my $fh = $z2->to_fh;
+  my $lineno = 0;
+  while (my $chunk = $fh->getline) {
+    $lineno++;
+    # debugging here
+  }
+
+  cmp_ok($lineno, '==', 1+$flush, "flush_before => $flush is $lineno chunks");
+}
+
+done_testing;