X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Factions.t;h=4e3315be269a8751224db40f4cdcf694d6f6ce9b;hb=a7643d601cc39d5bc5a6b5c9cf9c4b15a3140f1a;hp=8e315d5e9bc42930f3fb865874412df1de7a3d5c;hpb=41153e878cdf40d366ccee57716acd49f26733b1;p=catagits%2FHTML-Zoom.git diff --git a/t/actions.t b/t/actions.t index 8e315d5..4e3315b 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; @@ -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; @@ -46,7 +45,113 @@ sub run_for (&;$) { ) } -(my $expect = $tmpl) =~ s/(?=set_attribute( 'class' => 'foo' ) }, + $expect, + 'set attribute on existing attribute' +); + +($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/; + +is( + run_for { $_->set_attribute( 'foo' => 'bar' ) }, + $expect, + 'set attribute on non existing attribute' +); + +($expect = $tmpl) =~ s/class="main"/class="main foo"/; + +is( + run_for { $_->add_to_attribute( 'class' => 'foo' ) }, + $expect, + 'add attribute on existing attribute' +); + +($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/; + +is( + run_for { $_->add_to_attribute( 'foo' => '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/ 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' } ]; @@ -67,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/
/
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( @@ -89,7 +194,7 @@ is( 'replace ok' ); -my @ev; +@ev = (); is( run_for { $_->collect({ into => \@ev }) }, @@ -110,50 +215,180 @@ is( 'collect collected right events' ); -($expect = $tmpl) =~ s/class="main"/class="foo"/; +@ev = (); is( - run_for { $_->set_attribute({ name => 'class', value => 'foo' }) }, - $expect, - 'set attribute on existing attribute' + run_for { $_->collect({ into => \@ev, content => 1 }) }, + ' +
+ +', + 'collect w/content removes correctly' ); -($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/; +is( + HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev), + ' + Bob + Builder +
+ ', + 'collect w/content collects correctly' +); is( - run_for { $_->set_attribute({ name => 'foo', value => 'bar' }) }, - $expect, - 'set attribute on non existing attribute' + run_for { $_->replace($ohai, { content => 1 }) }, + ' +
O HAI
+ +', + 'replace w/content' ); -($expect = $tmpl) =~ s/class="main"/class="main foo"/; +($expect = $tmpl) =~ s/(?=<\/div>)/O HAI/; is( - run_for { $_->add_attribute({ name => 'class', value => 'foo' }) }, + run_for { $_->append_content($ohai) }, $expect, - 'add attribute on existing attribute' + 'append content ok' ); -($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/; +my $r_content = sub { my $r = shift; sub { $_->replace($r, { content => 1 }) } }; is( - run_for { $_->add_attribute({ name => 'foo', value => 'bar' }) }, - $expect, - 'add attribute on non existing attribute' + run_for { + $_->repeat( + [ + sub { + filter + filter($_ => '.name' => $r_content->('mst')) + => '.career' => $r_content->('Chainsaw Wielder') + }, + sub { + filter + filter($_ => '.name' => $r_content->('mdk')) + => '.career' => $r_content->('Adminion') + }, + ] + ) + }, + q{ +
+ mst + Chainsaw Wielder +
+
+ mdk + Adminion +
+
+ +}, + 'repeat ok' ); -($expect = $tmpl) =~ s/ class="main"//; +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{ +
+ mst + Chainsaw Wielder +
+ + mdk + Adminion +
+
+ +}, + 'repeat_content ok' +); is( - run_for { $_->remove_attribute({ name => 'class' }) }, - $expect, - 'remove attribute on existing attribute' + 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{ +
+ mst + Chainsaw Wielder +
+ mdk + Adminion + +
+ +}, + 'repeat_content with filter ok' ); is( - run_for { $_->remove_attribute({ name => 'foo' }) }, - $tmpl, - 'remove attribute on non existing attribute' + 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;