add transform_attribute, which runs a coderef on the value of an attribute, and can...
[catagits/HTML-Zoom.git] / t / actions.t
index c89a5b8..6e1541b 100644 (file)
@@ -93,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' } ];