add transform_attribute, which runs a coderef on the value of an attribute, and can...
[catagits/HTML-Zoom.git] / t / actions.t
index 3bab29c..6e1541b 100644 (file)
@@ -1,9 +1,6 @@
-use strict;
-use warnings FATAL => 'all';
+use strictures 1;
 use Test::More;
 
-use Devel::Dwarn;
-
 use HTML::Zoom::Parser::BuiltIn;
 use HTML::Zoom::Producer::BuiltIn;
 use HTML::Zoom::SelectorParser;
@@ -20,9 +17,9 @@ my $tmpl = <<END;
 </body>
 END
 
-sub src_stream { HTML::Zoom::Parser::BuiltIn->html_to_stream($tmpl); }
+sub src_stream { HTML::Zoom::Parser::BuiltIn->new->html_to_stream($tmpl); }
 
-sub html_sink { HTML::Zoom::Producer::BuiltIn->html_from_stream($_[0]) }
+sub html_sink { HTML::Zoom::Producer::BuiltIn->new->html_from_stream($_[0]) }
 
 my $fb = HTML::Zoom::FilterBuilder->new;
 
@@ -69,7 +66,7 @@ is(
 ($expect = $tmpl) =~ s/class="main"/class="main foo"/;
 
 is(
-  run_for { $_->add_attribute({ name => 'class', value => 'foo' }) },
+  run_for { $_->add_to_attribute({ name => 'class', value => 'foo' }) },
   $expect,
   'add attribute on existing attribute'
 );
@@ -77,7 +74,7 @@ is(
 ($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
 
 is(
-  run_for { $_->add_attribute({ name => 'foo', value => 'bar' }) },
+  run_for { $_->add_to_attribute({ name => 'foo', value => 'bar' }) },
   $expect,
   'add attribute on non existing attribute'
 );
@@ -96,6 +93,64 @@ is(
   'remove attribute on non existing attribute'
 );
 
+($expect = $tmpl) =~ s/ class="main"//;
+
+is(
+  run_for {
+      $_->transform_attribute({
+          name => 'class',
+          code => sub {
+              my $a = shift;
+              return if $a eq 'main';
+              return $a;
+          },
+      })
+  },
+  $expect,
+  'transform_attribute deletes the attr if code returns undef',
+  );
+
+($expect = $tmpl) =~ s/ class="main"/ class="moan"/;
+
+is(
+  run_for {
+      $_->transform_attribute({
+          name => 'class',
+          code => sub {
+              ( my $b = shift ) =~ s/main/moan/;
+              $b
+          },
+      })
+  },
+  $expect,
+  'transform_attribute transforms something',
+  );
+
+($expect = $tmpl) =~ s/ class="main"/ class="main" noggin="zonk"/;
+
+is(
+  run_for {
+      $_->transform_attribute({
+          name => 'noggin',
+          code => sub { 'zonk' },
+      })
+  },
+  $expect,
+  'transform_attribute adds attribute if not there before',
+  );
+
+is(
+  run_for {
+      $_->transform_attribute({
+          name => 'noggin',
+          code => sub { },
+      })
+  },
+  $tmpl,
+  'transform_attribute on nonexistent att does not add it if code returns undef',
+  );
+
+
 ($expect = $tmpl) =~ s/(?=<div)/O HAI/;
 
 my $ohai = [ { type => 'TEXT', raw => 'O HAI' } ];
@@ -117,17 +172,17 @@ is(
 ($expect = $tmpl) =~ s/(?<=class="main">)/O HAI/;
 
 is(
-  run_for { $_->prepend_inside($ohai) },
+  run_for { $_->prepend_content($ohai) },
   $expect,
-  'prepend_inside ok'
+  'prepend_content ok'
 );
 
 ($expect = $tmpl) =~ s/<hr \/>/<hr>O HAI<\/hr>/;
 
 is(
-  (run_for { $_->prepend_inside($ohai) } 'hr'),
+  (run_for { $_->prepend_content($ohai) } 'hr'),
   $expect,
-  'prepend_inside ok with in place close'
+  'prepend_content ok with in place close'
 );
 
 is(
@@ -163,12 +218,12 @@ is(
 @ev = ();
 
 is(
-  run_for { $_->collect({ into => \@ev, inside => 1 }) },
+  run_for { $_->collect({ into => \@ev, content => 1 }) },
   '<body>
   <div class="main"></div>
 </body>
 ',
-  'collect w/inside removes correctly'
+  'collect w/content removes correctly'
 );
 
 is(
@@ -178,31 +233,27 @@ is(
     <span class="career">Builder</span>
     <hr />
   ',
-  'collect w/inside collects correctly'
+  'collect w/content collects correctly'
 );
 
 is(
-  run_for { $_->replace($ohai, { inside => 1 }) },
+  run_for { $_->replace($ohai, { content => 1 }) },
   '<body>
   <div class="main">O HAI</div>
 </body>
 ',
-  'replace w/inside'
+  'replace w/content'
 );
 
 ($expect = $tmpl) =~ s/(?=<\/div>)/O HAI/;
 
 is(
-  run_for { $_->append_inside($ohai) },
+  run_for { $_->append_content($ohai) },
   $expect,
-  'append inside ok'
+  'append content ok'
 );
 
-if (1) {
-
-warn "\n\n----\n\n";
-
-my $r_inside = sub { my $r = shift; sub { $_->replace($r, { inside => 1 }) } };
+my $r_content = sub { my $r = shift; sub { $_->replace($r, { content => 1 }) } };
 
 is(
   run_for {
@@ -210,13 +261,13 @@ is(
       [
         sub {
           filter
-            filter($_ => '.name' => $r_inside->('mst'))
-            => '.career' => $r_inside->('Chainsaw Wielder')
+            filter($_ => '.name' => $r_content->('mst'))
+            => '.career' => $r_content->('Chainsaw Wielder')
         },
         sub {
           filter
-            filter($_ => '.name' => $r_inside->('mdk'))
-            => '.career' => $r_inside->('Adminion')
+            filter($_ => '.name' => $r_content->('mdk'))
+            => '.career' => $r_content->('Adminion')
         },
       ]
     )
@@ -236,6 +287,108 @@ is(
   'repeat ok'
 );
 
-}
+is(
+  run_for {
+    $_->repeat_content(
+      [
+        sub {
+          filter
+            filter($_ => '.name' => $r_content->('mst'))
+            => '.career' => $r_content->('Chainsaw Wielder')
+        },
+        sub {
+          filter
+            filter($_ => '.name' => $r_content->('mdk'))
+            => '.career' => $r_content->('Adminion')
+        },
+      ]
+    )
+  },
+  q{<body>
+  <div class="main">
+    <span class="hilight name">mst</span>
+    <span class="career">Chainsaw Wielder</span>
+    <hr />
+  
+    <span class="hilight name">mdk</span>
+    <span class="career">Adminion</span>
+    <hr />
+  </div>
+</body>
+},
+  'repeat_content ok'
+);
+
+is(
+  run_for {
+    my @between;
+    $_->repeat_content(
+      [
+        sub {
+          HTML::Zoom::ArrayStream->new({ array => [
+            (filter
+              filter($_ => '.name' => $r_content->('mst'))
+              => '.career' => $r_content->('Chainsaw Wielder')),
+            HTML::Zoom::ArrayStream->new({ array => \@between })
+          ] })->flatten
+        },
+        sub {
+          filter
+            filter($_ => '.name' => $r_content->('mdk'))
+            => '.career' => $r_content->('Adminion')
+        },
+      ],
+      { filter => sub {
+          filter $_[0] => 'hr' => sub { $_->collect({ into => \@between }) }
+        }
+      }
+    )
+  },
+  q{<body>
+  <div class="main">
+    <span class="hilight name">mst</span>
+    <span class="career">Chainsaw Wielder</span>
+    <hr />
+    <span class="hilight name">mdk</span>
+    <span class="career">Adminion</span>
+    
+  </div>
+</body>
+},
+  'repeat_content with filter ok'
+);
+
+is(
+  run_for {
+    my @between;
+    $_->repeat_content(
+      [
+        sub {
+          filter
+            filter($_ => '.name' => $r_content->('mst'))
+            => '.career' => $r_content->('Chainsaw Wielder')
+        },
+        sub {
+          filter
+            filter($_ => '.name' => $r_content->('mdk'))
+            => '.career' => $r_content->('Adminion')
+        },
+      ],
+      { repeat_between => 'hr' }
+    )
+  },
+  q{<body>
+  <div class="main">
+    <span class="hilight name">mst</span>
+    <span class="career">Chainsaw Wielder</span>
+    <hr />
+    <span class="hilight name">mdk</span>
+    <span class="career">Adminion</span>
+    
+  </div>
+</body>
+},
+  'repeat_content using repeat_between ok'
+);
 
 done_testing;