X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Factions.t;h=1b8b1fbad85ef877b35f0b3e30485b8a9fc1c7e6;hb=c84b68f396fb5b0e81dbae8927519b2e06138734;hp=790962f1dc5053494ad841f8688dc4c401b7c3a4;hpb=dae33531219f600d126f485532fd20e5ad1b4be1;p=catagits%2FHTML-Zoom.git diff --git a/t/actions.t b/t/actions.t index 790962f..1b8b1fb 100644 --- a/t/actions.t +++ b/t/actions.t @@ -1,5 +1,4 @@ -use strict; -use warnings FATAL => 'all'; +use strictures 1; use Test::More; use HTML::Zoom::Parser::BuiltIn; @@ -10,7 +9,7 @@ use HTML::Zoom::FilterStream; my $tmpl = < -
+
Bob Builder
@@ -18,9 +17,9 @@ my $tmpl = < 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; @@ -51,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_attr({ 'class' => 'foo', 'name' => 'bar'}) }, + $expect, + 'set attributes using hashref form (shorthand)' +); + ($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' ); @@ -67,15 +74,31 @@ is( ($expect = $tmpl) =~ s/class="main"/class="main foo"/; is( - run_for { $_->add_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_attribute({ name => 'foo', value => 'bar' }) }, + run_for { $_->add_to_attribute( 'foo' => 'bar' ) }, $expect, 'add attribute on non existing attribute' ); @@ -94,6 +117,72 @@ is( 'remove attribute on non existing attribute' ); +($expect = $tmpl) =~ s/class="main"/class=""/; + +is( + run_for { $_->remove_from_attribute({ class => 'main' }) }, + $expect, + 'remove name from 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' } ]; @@ -150,7 +239,7 @@ is( is( HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev), - '
+ '
Bob Builder
@@ -163,7 +252,7 @@ is( is( run_for { $_->collect({ into => \@ev, content => 1 }) }, ' -
+
', 'collect w/content removes correctly' @@ -182,7 +271,7 @@ is( is( run_for { $_->replace($ohai, { content => 1 }) }, ' -
O HAI
+
O HAI
', 'replace w/content' @@ -216,11 +305,11 @@ is( ) }, q{ -
+
mst Chainsaw Wielder
-
+
mdk Adminion
@@ -248,7 +337,7 @@ is( ) }, q{ -
+
mst Chainsaw Wielder
@@ -268,12 +357,12 @@ is( $_->repeat_content( [ sub { - HTML::Zoom::CodeStream->from_array( + HTML::Zoom::ArrayStream->new({ array => [ (filter filter($_ => '.name' => $r_content->('mst')) => '.career' => $r_content->('Chainsaw Wielder')), - HTML::Zoom::CodeStream->from_array(@between) - )->flatten + HTML::Zoom::ArrayStream->new({ array => \@between }) + ] })->flatten }, sub { filter @@ -288,7 +377,7 @@ is( ) }, q{ -
+
mst Chainsaw Wielder
@@ -301,4 +390,37 @@ is( '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{ +
+ mst + Chainsaw Wielder +
+ mdk + Adminion + +
+ +}, + 'repeat_content using repeat_between ok' +); + done_testing;