X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Factions.t;h=7647fd79e902b468b6bb901a423e06027a2ada27;hb=ee05be727a50b74d2322b9dbaee232ce428e3d3e;hp=c89a5b8c44d7457654c50e93c11e932f891a0f41;hpb=1cf035405cb81b780825c083cf7f5c0767b19243;p=catagits%2FHTML-Zoom.git diff --git a/t/actions.t b/t/actions.t index c89a5b8..7647fd7 100644 --- a/t/actions.t +++ b/t/actions.t @@ -9,7 +9,7 @@ use HTML::Zoom::FilterStream; my $tmpl = < -
+
Bob Builder
@@ -50,15 +50,23 @@ my ($expect, @ev); ($expect = $tmpl) =~ s/class="main"/class="foo"/; is( - run_for { $_->set_attribute({ name => 'class', value => 'foo' }) }, + run_for { $_->set_attribute( 'class' => 'foo' ) }, $expect, 'set attribute on existing attribute' ); +($expect = $tmpl) =~ s/name="cow" class="main"/name="bar" class="foo"/; + +is( + run_for { $_->set_attribute({ 'class' => 'foo', 'name' => 'bar'}) }, + $expect, + 'set attributes using hashref form' +); + ($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/; is( - run_for { $_->set_attribute({ name => 'foo', value => 'bar' }) }, + run_for { $_->set_attribute( 'foo' => 'bar' ) }, $expect, 'set attribute on non existing attribute' ); @@ -66,15 +74,31 @@ is( ($expect = $tmpl) =~ s/class="main"/class="main foo"/; is( - run_for { $_->add_to_attribute({ name => 'class', value => 'foo' }) }, + run_for { $_->add_to_attribute( 'class' => 'foo' ) }, $expect, 'add attribute on existing attribute' ); +($expect = $tmpl) =~ s/class="main"/class="main foo"/; + +is( + run_for { $_->add_class('foo') }, + $expect, + 'add attribute on existing attribute (shorthand)' +); + +($expect = $tmpl) =~ s/class="main"/class="main" id="foo"/; + +is( + run_for { $_->set_id('foo') }, + $expect, + 'set_id (shorthand)' +); + ($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/; is( - run_for { $_->add_to_attribute({ name => 'foo', value => 'bar' }) }, + run_for { $_->add_to_attribute( 'foo' => 'bar' ) }, $expect, 'add attribute on non existing attribute' ); @@ -93,6 +117,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/(?= 'TEXT', raw => 'O HAI' } ]; @@ -149,7 +231,7 @@ is( is( HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev), - '
+ '
Bob Builder
@@ -162,7 +244,7 @@ is( is( run_for { $_->collect({ into => \@ev, content => 1 }) }, ' -
+
', 'collect w/content removes correctly' @@ -181,7 +263,7 @@ is( is( run_for { $_->replace($ohai, { content => 1 }) }, ' -
O HAI
+
O HAI
', 'replace w/content' @@ -215,11 +297,11 @@ is( ) }, q{ -
+
mst Chainsaw Wielder
-
+
mdk Adminion
@@ -247,7 +329,7 @@ is( ) }, q{ -
+
mst Chainsaw Wielder
@@ -287,7 +369,7 @@ is( ) }, q{ -
+
mst Chainsaw Wielder
@@ -320,7 +402,7 @@ is( ) }, q{ -
+
mst Chainsaw Wielder