summaryrefslogtreecommitdiffstats
path: root/t/blocks.t
blob: cde7665d1e23849ca14c7d76b2441a297aa0821b (plain) (blame)
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
113
114
115
116
#!/usr/bin/env perl
use strict;
use warnings;
use lib 't/lib';
use Test::More;
use Test::Handlebars;

render_ok(
    'This is {{#shown}}shown{{/shown}}',
    { shown => 1 },
    'This is shown',
    "true block variable"
);

render_ok(
    'This is {{#shown}}shown{{/shown}}',
    { shown => 0 },
    'This is ',
    "false block variable"
);

render_ok(
    'This is {{#shown}}shown{{/shown}}',
    { shown => [({}) x 3] },
    'This is shownshownshown',
    "array block variable"
);

render_ok(
    'This is {{#shown}}{{content}}{{/shown}}',
    { shown => { content => 'SHOWN' } },
    'This is SHOWN',
    "nested hash block variable"
);

render_ok(
    'This is {{#shown}}{{content}}{{/shown}}',
    {
        shown => [
            { content => '3' },
            { content => '2' },
            { content => '1' },
            { content => 'Shown' },
        ],
    },
    'This is 321Shown',
    "nested array of hashes block variable"
);

render_ok(
    '{{#goodbyes}}{{@index}}. {{text}}! {{/goodbyes}}cruel {{world}}!',
    {
        goodbyes => [
            { text => 'goodbye' },
            { text => 'Goodbye' },
            { text => 'GOODBYE' },
        ],
        world => 'world',
    },
    '0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!',
    "\@index variable"
);

render_ok(
    '{{#foo}}{{/foo}}bar',
    {
        foo => [ 1, 2, 3 ],
    },
    'bar',
    "empty block"
);

render_ok(
    '{{#people}}{{name}}{{^}}{{none}}{{/people}}',
    {
        none => 'No people',
    },
    'No people',
    "inverted block shorthand"
);

render_ok(
    '{{#people}}{{name}}{{^}}{{none}}{{/people}}',
    {
        none   => 'No people',
        people => [],
    },
    'No people',
    "inverted block shorthand (empty array)"
);

render_ok(
    <<'TEMPLATE',
{{#people}}
{{.}}
{{^}}
{{none}}
{{/people}}
TEMPLATE
    {
        none   => 'No people',
        people => [
            'Jesse Luehrs',
            'Shawn Moore',
            'Stevan Little',
        ],
    },
    <<'RENDERED',
Jesse Luehrs
Shawn Moore
Stevan Little
RENDERED
    "inverted block shorthand (non-empty array)"
);

done_testing;