/
00-basic.t
61 lines (51 loc) · 2.04 KB
/
00-basic.t
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
use Test;
use Template::Mojo;
sub render($tmpl, *@a) {
Template::Mojo.new($tmpl).render(|@a)
}
my @cases = (
['', ''],
['hello', 'hello'],
["empty\n\nline", "empty\n\nline", 'empty line'],
["almost empty\n \nline", "almost empty\n \nline", 'almost empty line'],
['hello <%%> world', 'hello world'],
['hello <%= "world" %>', 'hello world'],
[['answer = <%= $^a + $^b %>', 40, 2], 'answer = 42'],
['hello <% "blarg" %> world', 'hello world'],
["% for 1..3 \{\nhello\n% \}\n", "hello\nhello\nhello\n", 'for 1..3'],
["hello\n%# die 'this is an harmless comment'\nworld", "hello\nworld", '# comment in tag'],
["<a href='foo'>bar</a>", "<a href='foo'>bar</a>"],
[["a happy <%= \$^a %>\n", 'bar'], "a happy bar\n", 'a happy bar'],
[["% my (\$a, \$b) = \@_\n<%= \$a %> and <%= \$b %>", 5, 7], '5 and 7', 'code'],
["% 0\n an indented line\n%= 'foo'", " an indented line\nfoo", 'indented line'],
);
plan 3 + @cases.elems;
for @cases -> $c {
my ($tmpl, @params) = $c[0].WHAT === Str ?? ($c[0]) !! $c[0].list;
is render($tmpl, @params), $c[1], ($c[2] // $c[0]);
}
my $err;
{
$err = '';
Template::Mojo.new('<%');
CATCH {
default { $err = $_ };
}
}
ok $err ~~ X::Template::Mojo::ParseError, 'Bad template exception';
{
$err = '';
Template::Mojo.new('<%= $^a + $^b %>').render(23);
CATCH {
default { $err = $_ };
}
}
ok $err ~~ /expected\s2/ && $err ~~ /got\s1/, 'not enough arguments';
{
$err = '';
Template::Mojo.new('<%= $^a + $^b %>').render(23, 1, 4);
CATCH {
default { $err = $_ };
}
}
ok $err ~~ /expected\s2/ && $err ~~ /got\s3/, 'too many arguments';