| 1 |
|
| 2 |
# 020_output.t - Test the output from HTML::Template::Dumper, |
| 3 |
# Data::Dumper formatter |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
use Test::More tests => 7; |
| 8 |
use HTML::Template::Dumper; |
| 9 |
use Data::Dumper 'Dumper'; |
| 10 |
use Struct::Compare; |
| 11 |
use IO::Scalar; |
| 12 |
|
| 13 |
my %tmpl_params = ( |
| 14 |
scalar1 => 'one', |
| 15 |
scalar2 => 'two', |
| 16 |
loop1 => [ |
| 17 |
{ |
| 18 |
loop_var1 => 'loop_one', |
| 19 |
loop_var2 => 'loop_two', |
| 20 |
internal_loop => [ |
| 21 |
{ loop_var3 => '1' }, |
| 22 |
{ loop_var3 => '2' }, |
| 23 |
], |
| 24 |
}, |
| 25 |
{ |
| 26 |
loop_var1 => 'loop_three', |
| 27 |
loop_var2 => 'loop_four', |
| 28 |
internal_loop => [ |
| 29 |
{ loop_var3 => '3' }, |
| 30 |
{ loop_var3 => '4' }, |
| 31 |
], |
| 32 |
}, |
| 33 |
], |
| 34 |
); |
| 35 |
my $tmpl_data = <<'TMPL'; |
| 36 |
<TMPL_VAR scalar1> |
| 37 |
<TMPL_VAR scalar2> |
| 38 |
<TMPL_LOOP loop1> |
| 39 |
<TMPL_VAR loop_var1> |
| 40 |
<TMPL_VAR loop_var2> |
| 41 |
<TMPL_LOOP internal_loop> |
| 42 |
<TMPL_VAR loop_var3> |
| 43 |
</TMPL_LOOP> |
| 44 |
</TMPL_LOOP> |
| 45 |
TMPL |
| 46 |
# Need to set Data::Dumper options the same as in the |
| 47 |
# Data::Dumper formatter |
| 48 |
# |
| 49 |
local $Data::Dumper::Indent = 0; |
| 50 |
local $Data::Dumper::Purity = 1; |
| 51 |
local $Data::Dumper::Terse = 1; |
| 52 |
my $expected_output = Dumper \%tmpl_params; |
| 53 |
|
| 54 |
|
| 55 |
my $tmpl = HTML::Template::Dumper->new( |
| 56 |
scalarref => \$tmpl_data, |
| 57 |
); |
| 58 |
$tmpl->param( \%tmpl_params ); |
| 59 |
$tmpl->set_output_format( 'Data_Dumper' ); |
| 60 |
ok( $tmpl->get_output_format() eq 'HTML::Template::Dumper::Data_Dumper', |
| 61 |
"Setting output format" ); |
| 62 |
|
| 63 |
# This test is very fragile and any problems with it are |
| 64 |
# just as likely to be a problem with the test as a problem |
| 65 |
# with the module. Consider removing it in favor of doing |
| 66 |
# only the parse() test. The only justification of it is |
| 67 |
# that we want to test output() seperate from parse(). |
| 68 |
# |
| 69 |
ok( $tmpl->output() eq $expected_output, |
| 70 |
"Output is as expected" ); |
| 71 |
|
| 72 |
my $test_data; |
| 73 |
my $test_handle = IO::Scalar->new(\$test_data); |
| 74 |
$tmpl->output( print_to => $test_handle ); |
| 75 |
$test_handle->close; |
| 76 |
|
| 77 |
ok( $test_data eq $expected_output, |
| 78 |
"Output is as expected on file handle" ); |
| 79 |
|
| 80 |
my $got = $tmpl->parse( $tmpl->output() ); |
| 81 |
ok( compare( $got, \%tmpl_params ), "Compare to a hashref" ); |
| 82 |
|
| 83 |
|
| 84 |
# YAML tests. Be sure to skip them if YAML isn't installed. |
| 85 |
# |
| 86 |
SKIP: { |
| 87 |
eval { require YAML }; |
| 88 |
skip 'YAML not installed', 3 if $@; |
| 89 |
|
| 90 |
$tmpl->set_output_format( 'YAML' ); |
| 91 |
ok( $tmpl->get_output_format() eq 'HTML::Template::Dumper::YAML', |
| 92 |
"Setting output format" ); |
| 93 |
|
| 94 |
my $expected_output = YAML::Dump(\%tmpl_params); |
| 95 |
|
| 96 |
# Another fragile test. |
| 97 |
ok( $tmpl->output eq $expected_output, |
| 98 |
"Output is as expected for YAML" ); |
| 99 |
|
| 100 |
ok( compare( $tmpl->parse( $tmpl->output ), \%tmpl_params ), |
| 101 |
"Compare to a hashref" ); |
| 102 |
} |
| 103 |
|
| 104 |
|