1use strict;
2use warnings;
3use Test::More (tests => 10);
4
5use_ok('HTML::Template');
6
7my $tmpl_string = "";
8my $template = HTML::Template->new_scalar_ref(\$tmpl_string);
9
10# test for non existing param
11eval { my $value = $template->param('FOOBAR') };
12like(
13    $@,
14    qr/HTML::Template : Attempt to get nonexistent parameter/,
15    "attempt to get nonexistent parameter caught with die_on_bad_params == 1"
16);
17
18$template = HTML::Template->new_scalar_ref(\$tmpl_string, die_on_bad_params => 0);
19
20ok(!defined($template->param('FOOBAR')), "if bad params permitted, bad param results in undef");
21
22$template->param('FOOBAR' => undef);
23
24ok(!defined($template->param('FOOBAR')), "undef param results in undef");
25
26# test for bad call to ->param with non scalar/non-hashref arg
27# dha wants to send it a puppy
28eval { my $value = $template->param(bless ['FOOBAR'], "Puppy") };
29like($@, qr/Single reference arg to param\(\) must be a hash-ref/, "Single reference arg to param() must be a hash-ref!");
30
31# test for passing
32eval { $template->param(bless {'FOOBAR' => 42}, "Puppy") };
33ok(!$@, "param() doesn't die with blessed hash as first arg");
34
35# make sure we can't pass a reference to a reference
36eval { $template->param(\{}) };
37like($@, qr/must be a hash-ref/i, 'reference to a hash-ref is still bad');
38
39# odd number of params
40eval { my $value = $template->param('foo' => 1, 'bar') };
41like($@, qr/You gave me an odd number of parameters to param/, "odd number of args to param");
42
43# value is a reference to a reference
44$template = HTML::Template->new_scalar_ref(\'<tmpl_var foo>');
45eval { $template->param(foo => \{}) };
46like($@, qr/a reference to a reference/i, 'trying to use a reference to a reference');
47
48# setting multiple values, multiple calls
49$template = HTML::Template->new_scalar_ref(\'<tmpl_var foo> <tmpl_var bar> <tmpl_var baz> <tmpl_loop frob><tmpl_var fooey>-<tmpl_var blah> </tmpl_loop>');
50$template->param(foo => 1, baz => 2);
51$template->param(bar => 2, baz => 3, frob => [{fooey => 'a', blah => 'b'}, {fooey => 'c', blah => 'd'}]);
52is($template->output, '1 2 3 a-b c-d ', 'can set multiple params at once');
53
54=head1 NAME
55
56t/10-param.t
57
58=head1 OBJECTIVE
59
60Test edge cases in use of C<HTML::Template::param()>.  More tests will
61probably be added as we understand this function better.
62
63=cut
64
65