File: 020_output.t

package info (click to toggle)
libhtml-template-dumper-perl 0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 192 kB
  • sloc: perl: 247; makefile: 7
file content (112 lines) | stat: -rw-r--r-- 2,644 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

# 020_output.t - Test the output from HTML::Template::Dumper, 
# 	Data::Dumper formatter

use strict;
use warnings;
use Test::More tests => 7;
use HTML::Template::Dumper;
use Data::Dumper 'Dumper';
use Struct::Compare;
use IO::Scalar;

my %tmpl_params = (
	scalar1  => 'one', 
	scalar2  => 'two', 
	loop1    => [
		{
			loop_var1 => 'loop_one', 
			loop_var2 => 'loop_two', 
			internal_loop => [
				{ loop_var3 => '1' }, 
				{ loop_var3 => '2' }, 
			], 
		}, 
		{
			loop_var1 => 'loop_three', 
			loop_var2 => 'loop_four', 
			internal_loop => [
				{ loop_var3 => '3' }, 
				{ loop_var3 => '4' }, 
			], 
		}, 
	], 
);
my $tmpl_data = <<'TMPL';
<TMPL_VAR scalar1>
<TMPL_VAR scalar2>
<TMPL_LOOP loop1>
	<TMPL_VAR loop_var1>
	<TMPL_VAR loop_var2>
	<TMPL_LOOP internal_loop>
		<TMPL_VAR loop_var3>
	</TMPL_LOOP>
</TMPL_LOOP>
TMPL
# Need to set Data::Dumper options the same as in the 
# Data::Dumper formatter
# 
local $Data::Dumper::Indent = 0;
local $Data::Dumper::Purity = 1;
local $Data::Dumper::Terse = 1;
my $expected_output = Dumper \%tmpl_params;


my $tmpl = HTML::Template::Dumper->new(
	scalarref => \$tmpl_data, 
);
$tmpl->param( \%tmpl_params );
$tmpl->set_output_format( 'Data_Dumper' );
ok( $tmpl->get_output_format() eq 'HTML::Template::Dumper::Data_Dumper',
	"Setting output format" );

# This test is very fragile and any problems with it are 
# just as likely to be a problem with the test as a problem 
# with the module.  Consider removing it in favor of doing 
# only the parse() test.  The only justification of it is 
# that we want to test output() seperate from parse(). 
# 
TODO: {
local $TODO = "output test is fragile and relies on hash order";

ok( $tmpl->output() eq $expected_output, 
	"Output is as expected" );
}

TODO: {
local $TODO = "file handle test is buggy";

my $test_data;
my $test_handle = IO::Scalar->new(\$test_data);
$tmpl->output( print_to => $test_handle );
$test_handle->close;

ok( $test_data eq $expected_output, 
	"Output is as expected on file handle" );
}

my $got = $tmpl->parse( $tmpl->output() );
ok( compare( $got, \%tmpl_params ), "Compare to a hashref" );


# YAML tests.  Be sure to skip them if YAML isn't installed.
# 
SKIP: {
	eval { require YAML };
	skip 'YAML not installed', 3 if $@;

	$tmpl->set_output_format( 'YAML' );
	ok( $tmpl->get_output_format() eq 'HTML::Template::Dumper::YAML',
		"Setting output format" );
	
	my $expected_output = YAML::Dump(\%tmpl_params);

	# Another fragile test.
	ok( $tmpl->output eq $expected_output, 
		"Output is as expected for YAML" );
	
	ok( compare( $tmpl->parse( $tmpl->output ), \%tmpl_params ), 
		"Compare to a hashref" );
}