File: 03complex.t

package info (click to toggle)
libhtml-template-expr-perl 0.07-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 248 kB
  • sloc: perl: 285; makefile: 2
file content (111 lines) | stat: -rw-r--r-- 4,914 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
use Test::More qw(no_plan);
use HTML::Template::Expr;

my $template = HTML::Template::Expr->new(path => ['t/templates'],
                                      filename => 'complex.tmpl',
                                     );
is($template->query(name => 'unused'), 'VAR', "query(name => unused)");
my %params = map { $_ => 1 } $template->param();
ok(exists $params{unused}, "param(unused)");

$template->param(foo => 11,
                 bar => 0,
                 fname => 'president',
                 lname => 'clinton',
                 unused => 0);
my $output = $template->output();
like($output, qr/Foo is greater than 10/i, "greater than");
unlike($output, qr/Bar and Foo/i, "and");
like($output, qr/Bar or Foo/i, "or");
like($output, qr/Bar - Foo = -11/i, "subtraction");
like($output, qr/Foo - Bar \+ 10 = 21/i, "math strings");
like($output, qr/Math Works, Alright/i, "math");
like($output, qr/My name is President Clinton/, "string op 1");
like($output, qr/Resident Alien is phat/, "string op 2");
like($output, qr/Resident has 8 letters, which is less than 10 and greater than 5/, "string length");
like($output, qr/Multiple ors works/, 'multiple or test');
like($output, qr/Addition and comparison 1/);
unlike($output, qr/Addition and comparison 2/);
like($output, qr/Addition and comparison 3/);
unlike($output, qr/And 0 works/);

$template = HTML::Template::Expr->new(path => ['t/templates'],
                                      filename => 'loop.tmpl',
                                     );
$template->param(simple => [
                            { foo => 10 },
                            { foo => 100 },
                            { foo => 1000 }
                           ]);
$template->param(color => 'blue');
$template->param(complex => [ 
                             { 
                              fname => 'Yasunari',
                              lname => 'Kawabata',
                              inner => [
                                        { stat_name => 'style', 
                                          stat_value => 100 ,
                                        },
                                        { stat_name => 'shock',
                                          stat_value => 1,
                                        },
                                        { stat_name => 'poetry',
                                          stat_value => 100
                                        },
                                        { stat_name => 'machismo',
                                          stat_value => 50
                                        },
                                       ],
                             },
                             { 
                              fname => 'Yukio',
                              lname => 'Mishima',
                              inner => [
                                        { stat_name => 'style', 
                                          stat_value => 50,
                                        },
                                        { stat_name => 'shock',
                                          stat_value => 100,
                                        },
                                        { stat_name => 'poetry',
                                          stat_value => 1
                                        },
                                        { stat_name => 'machismo',
                                          stat_value => 100
                                        },
                                       ],
                             },
                            ]);

$output = $template->output();
like($output, qr/Foo is less than 10.\s+Foo is greater than 10.\s+Foo is greater than 10./, "math in loops");


# test user-defined functions
my $repeat = sub { $_[0] x $_[1] };

$template = HTML::Template::Expr->new(path => ['t/templates'],
                                      filename => 'func.tmpl',
                                      functions => {
                                                    repeat => $repeat,
                                                   },
                                     );
$template->param(repeat_me => 'foo ');
$output = $template->output();
like($output, qr/foo foo foo foo/, "user defined function");
like($output, qr/FOO FOO FOO FOO/, "user defined function with uc()");


# test numeric functions
$template = HTML::Template::Expr->new(path => ['t/templates'],
                                      filename => 'numerics.tmpl',
                                     );
$template->param(float => 5.1,
                 four => 4);
$output = $template->output;
like($output, qr/INT: 5/, "int()");
like($output, qr/SQRT: 2/, "sqrt()");
like($output, qr/SQRT2: 4/, "sqrt() 2");
like($output, qr/SUM: 14/, "int(4 + 10.1)");
like($output, qr/SPRINTF: 14.1000/, "sprintf('%0.4f', (10.1 + 4))");