use strict; use Test::More; BEGIN { plan tests => 26; } no warnings; # SAVERRR used only once open SAVEERR, ">&STDERR" or die "can't dup stderr"; use warnings; my $errors; close STDERR; open STDERR, '>', \$errors or die "can't stderr: $!"; eval 'use XML::XPathScript'; open STDERR, ">&SAVEERR"; # return to normal is $errors => undef, 'late inclusion of XML::XPathScript'; sub test_xml { my( $xml, $style, $result, $comment ) = @_; my $xps = new XML::XPathScript( xml => $xml, stylesheet => $style ); my $buffer; $xps->process( \$buffer ); is $buffer => $result, $comment ; } test_xml( 'dummy', 'working', 'working', 'empty run' ); test_xml( 'dummy', '<%= apply_templates() %>', 'dummy', 'simple in/out'); test_xml( 'dummy', '<% print "Hello!" %>', 'Hello!', 'rogue print statement' ); test_xml( '', <<'EOT', "comment: hello world \n", 'processing a comment' ); <% $t->{'#comment'}{pre} = "comment:"; %><%= apply_templates() %> EOT test_xml( '', <<'EOT', "\n", 'masking a comment' ); <% $t->{'#comment'}{testcode} = sub{ 0 } %><%= apply_templates() %> EOT ############################################################ # Interpolation my $xml = "Hello"; my $xps = <<'EOT'; <% set_interpolation( 0 ); $t->{node}{testcode} = sub { my( $n, $t ) = @_; $t->{pre} = '{@color}'; return DO_SELF_ONLY }; %> <%= apply_templates() %> EOT test_xml( $xml, $xps, "\n{\@color}\n", 'Interpolation (disabled)' ); $xps = <<'EOT'; <% $XML::XPathScript::DoNotInterpolate = 0; $t->{node}{testcode} = sub { my( $n, $t ) = @_; $t->{pre} = '{@color}'; return DO_SELF_ONLY() }; %> <%= apply_templates() %> EOT test_xml( $xml, $xps, "\nblue\n", 'Interpolation (enabled)' ); ############################################################ # double interpolation $xps = <<'EOT'; <% $XML::XPathScript::DoNotInterpolate = 0; $t->{node}{testcode} = sub { my( $n, $t ) = @_; $t->{pre} = '{@color}:{@color}'; return DO_SELF_ONLY() }; %> <%= apply_templates() %> EOT test_xml( $xml, $xps, "\nblue:blue\n", 'Double interpolation' ); ############################################################ # interpolation regex test_xml( '', <<'XPS' , "stuff\n", 'interpolation regex' ); <% set_interpolation_regex( qr/\[\[(.*?)\]\]/ ); $t->{doc}{pre} = '[[@arg]]'; %><%= apply_templates() %> XPS test_xml( '', <<'EOT', "!?\n", 'Prechildren and Postchildren tags, with children' ); <% $t->{doc}{prechildren} = '!'; $t->{doc}{postchildren} = '?'; $t->{doc}{showtag} = 1; %><%= apply_templates() %> EOT test_xml( '', <<'EOT', "\n", 'Prechildren and Postchildren tags, without children' ); <% $t->{doc}{prechildren} = '!'; $t->{doc}{postchildren} = '?'; $t->{doc}{showtag} = 1; %><%= apply_templates() %> EOT test_xml( '', <<'EOT', "!?!?\n", 'Prechild and Postchild tags' ); <% $t->{doc}{prechild} = '!'; $t->{doc}{postchild} = '?'; $t->{doc}{showtag} = 1; %><%= apply_templates() %> EOT test_xml( 'empty', '', "#include works!\n", '' ); test_xml( 'empty', '', "#include works!\n\n", '2 levels of ' ); close STDERR; open STDERR, '>', \{ my $x }; test_xml( 'empty', '', "Ooops.\n", 'recursive ' ); test_xml( 'empty', '', "Ooops.\n\n", '2 levels of + recursion' ); open STDERR, ">&SAVEERR"; # return to normal # override of printform $xps = new XML::XPathScript( xml => '', stylesheet => 'how about a shout-o-matic?' ); my $buffer; $xps->process( sub{ $buffer .= uc shift } ); is $buffer => 'HOW ABOUT A SHOUT-O-MATIC?', 'override of printform'; test_xml( '', <<'EOXPS', "only b: \n", 'xpath testcode return statement' ); <% $t->{doc}{pre} = 'only b: '; $t->{doc}{testcode} = sub{ 'b'; } %><%= apply_templates() %> EOXPS # encoding #test_xml( 'Ϩ', '<%= apply_templates() %>', '', 'Encoding' ); # testing for proper STDOUT management { my $xps = new XML::XPathScript( xml => 'hello', stylesheet => '<%= apply_templates()%>' ); my $output_file = 't/output.xml'; local *STDOUT; die "file $output_file shouldn't be there" if -f $output_file; open STDOUT, ">$output_file" or die $!; $xps->process; close STDOUT; open FILE, $output_file or die "$!"; is => 'hello', 'STDOUT management'; close FILE; unlink $output_file or die $!; } # get_xpath_of_node() { my $xps = new XML::XPathScript( xml => 'hello world ! ', stylesheet => <<'STYLESHEET' ); <% $t->{'*'}{pre}=""; $t->{'text()'}{testcode} = sub { my ($self, $t)=@_; $t->{pre} = get_xpath_of_node($self)."\n"; return DO_SELF_ONLY; }; %><%= apply_templates() %> STYLESHEET my $result=""; $xps->process(\$result); ok($result eq <<'EXPECTED') or warn $result; /coucou[1]/bloh[1]/blah[2]/text()[1] /coucou[1]/bloh[1]/blah[2]/em[1]/text()[1] /coucou[1]/bloh[1]/blah[2]/text()[2] EXPECTED } test_xml( '0', '<%= apply_templates() %>', '0', 'string "0" appears' ); { my $xps = <<'XPS'; <% $t->{foo}{showtag} = 1; $t->{foo}{pre} = "before "; $t->{foo}{intro} = " post-opening "; $t->{foo}{prechildren} = "pre-children "; $t->{foo}{postchildren} = " post-children"; $t->{foo}{extro} = " pre-closing "; $t->{foo}{post} = " post-closing"; %><%= apply_templates() %> XPS test_xml( '', $xps, "before post-opening pre-children post-children pre-closing post-closing\n", 'full template with child'); test_xml( '', $xps, "before post-opening pre-closing post-closing\n", 'full template without child'); } { close STDERR; my $errors; open STDERR, '>', \$errors; my $exp; test_xml( '', '<% my $nothing; print $nothing; %>', '', $exp = 'printing undefs should not trigger a warning' ); is $errors => undef, $exp; open STDERR, ">&SAVEERR"; # return to normal }