test replace inside
[catagits/HTML-Zoom.git] / t / actions.t
index 80a909a..f369b13 100644 (file)
@@ -46,7 +46,55 @@ sub run_for (&;$) {
   )
 }
 
-(my $expect = $tmpl) =~ s/(?=<div)/O HAI/;
+my ($expect, @ev);
+
+($expect = $tmpl) =~ s/class="main"/class="foo"/;
+
+is(
+  run_for { $_->set_attribute({ name => 'class', value => 'foo' }) },
+  $expect,
+  'set attribute on existing attribute'
+);
+
+($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
+
+is(
+  run_for { $_->set_attribute({ name => 'foo', value => 'bar' }) },
+  $expect,
+  'set attribute on non existing attribute'
+);
+
+($expect = $tmpl) =~ s/class="main"/class="main foo"/;
+
+is(
+  run_for { $_->add_attribute({ name => 'class', value => 'foo' }) },
+  $expect,
+  'add attribute on existing attribute'
+);
+
+($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
+
+is(
+  run_for { $_->add_attribute({ name => 'foo', value => 'bar' }) },
+  $expect,
+  'add attribute on non existing attribute'
+);
+
+($expect = $tmpl) =~ s/ class="main"//;
+
+is(
+  run_for { $_->remove_attribute({ name => 'class' }) },
+  $expect,
+  'remove attribute on existing attribute'
+);
+
+is(
+  run_for { $_->remove_attribute({ name => 'foo' }) },
+  $tmpl,
+  'remove attribute on non existing attribute'
+);
+
+($expect = $tmpl) =~ s/(?=<div)/O HAI/;
 
 my $ohai = [ { type => 'TEXT', raw => 'O HAI' } ];
 
@@ -89,4 +137,55 @@ is(
   'replace ok'
 );
 
+@ev = ();
+
+is(
+  run_for { $_->collect({ into => \@ev }) },
+  '<body>
+  
+</body>
+',
+  'collect removes without passthrough'
+);
+
+is(
+  HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev),
+  '<div class="main">
+    <span class="hilight name">Bob</span>
+    <span class="career">Builder</span>
+    <hr />
+  </div>',
+  'collect collected right events'
+);
+
+@ev = ();
+
+is(
+  run_for { $_->collect({ into => \@ev, inside => 1 }) },
+  '<body>
+  <div class="main"></div>
+</body>
+',
+  'collect w/inside removes correctly'
+);
+
+is(
+  HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev),
+  '
+    <span class="hilight name">Bob</span>
+    <span class="career">Builder</span>
+    <hr />
+  ',
+  'collect w/inside collects correctly'
+);
+
+is(
+  run_for { $_->replace($ohai, { inside => 1 }) },
+  '<body>
+  <div class="main">O HAI</div>
+</body>
+',
+  'replace w/inside'
+);
+
 done_testing;