Fix ->repeat with iterator, make t/repeat.t pass (formerly t/todo-repeat.t) repeat
Jakub Narebski [Wed, 12 Jan 2011 11:47:54 +0000 (12:47 +0100)]
The change to ->repeat method in HTML::Zoom::FilterBuilder assumes
that when it is passed code reference, it is meant to be used as
iterator (i.e. turned into stream using 'stream_from_code').

This made t/todo-repeat.t pass.  This test got renamed to t/repeat.t
now that it no longer needs to be skipped.  Instead of just testing
that there are no errors, it checks that ->repeat(sub { ... }) works
correctly.  Added test for equivalent ->repeat([ ... ]).

The t/flush.t test no longer needs workaround for ->repeat() (and it
doesn't work with workaround now, hmmm...).

lib/HTML/Zoom/FilterBuilder.pm
t/flush.t
t/repeat.t [new file with mode: 0644]
t/todo-repeat.t [deleted file]

index 50398b3..eff9b2d 100644 (file)
@@ -246,7 +246,9 @@ sub repeat {
     };
   }
   my $repeater = sub {
-    my $s = $self->_stream_from_proto($repeat_for);
+    my $s = ref($repeat_for) eq 'CODE'
+      ? $self->_stream_from_code($repeat_for) # $repeat_for is iterator
+      : $self->_stream_from_proto($repeat_for);
     # We have to test $repeat_between not @between here because
     # at the point we're constructing our return stream @between
     # hasn't been populated yet - but we can test @between in the
index bf60b64..8b1aef9 100644 (file)
--- a/t/flush.t
+++ b/t/flush.t
@@ -2,21 +2,8 @@ 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">
@@ -31,7 +18,7 @@ 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 {
+  my $z2 = $zoom->select('.item')->repeat(sub {
     if (my $name = shift @list) {
       return sub { $_->select('.item-name')->replace_content($name) }
     } else {
diff --git a/t/repeat.t b/t/repeat.t
new file mode 100644 (file)
index 0000000..7e363e7
--- /dev/null
@@ -0,0 +1,46 @@
+use strict;
+use HTML::Zoom;
+use Test::More;
+
+my $z = HTML::Zoom->from_html(<<'HTML');
+<html>
+<body>
+<div id="foo"><p></p></div>
+</body>
+</html>
+HTML
+
+my @list = qw(foo bar baz);
+my $iter = sub { shift @list };
+
+my $actual = $z->select("#foo")->repeat(sub {
+    my $e = $iter->() or return;
+    return sub {
+      $_->select("#foo")->set_attribute({name => 'id', value => $e})
+        ->select("p")->replace_content($e);
+    };
+})->to_html;
+my $expected = <<'HTML';
+<html>
+<body>
+<div id="foo"><p>foo</p></div><div id="bar"><p>bar</p></div><div id="baz"><p>baz</p></div>
+</body>
+</html>
+HTML
+
+is($actual, $expected, 'repeat with iterator works');
+
+@list = qw(foo bar baz);
+$actual = $z->select("#foo")->repeat([
+  map {
+    my $e = $_;
+    sub {
+      $_->select("#foo")->set_attribute({name => 'id', value => $e})
+        ->select("p")->replace_content($e);
+    }
+  } @list
+])->to_html;
+
+is($actual, $expected, 'repeat with array of transforms works');
+
+done_testing;
diff --git a/t/todo-repeat.t b/t/todo-repeat.t
deleted file mode 100644 (file)
index 54a773d..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-use strict;
-use HTML::Zoom;
-use Test::More skip_all => "Totally doesn't work yet";
-
-my $z = HTML::Zoom->from_html(<<HTML);
-<html>
-<body>
-<div id="foo"><p/></div>
-</body>
-</html>
-HTML
-
-my @list = qw(foo bar baz);
-my $iter = sub { shift @list };
-
-$z->select("#foo")->repeat(sub {
-    my $e = $iter->() or return;
-    return sub { $_->select("p")->replace_content($e) };
-})->to_html;
-
-ok 1;
-
-done_testing;