summaryrefslogtreecommitdiffstats
path: root/t/expressions.t
blob: fae632ccb91d4256ea5b2a0db5275e876376249f (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env perl
use strict;
use warnings;
use lib 't/lib';
use Test::More;
use Test::Handlebars;

render_ok(
    '<h1>{{title}}</h1>',
    { title => 'Xslate rocks' },
    '<h1>Xslate rocks</h1>',
    "basic variables"
);

render_ok(
    '<h1>{{article.title}}</h1>',
    { article => { title => 'Hash references rock' } },
    '<h1>Hash references rock</h1>',
    ". separator"
);

render_ok(
    '<h1>{{article/title}}</h1>',
    { article => { title => 'Deprecated syntax does not' } },
    '<h1>Deprecated syntax does not</h1>',
    "/ separator"
);

render_ok(
    '<h1>{{page.article.title}}</h1> - {{date}}',
    {
        page => {
            article => { title => 'Multilevel field access' },
        },
        date => '2012-10-01',
    },
    '<h1>Multilevel field access</h1> - 2012-10-01',
    "multilevel field access with ."
);

render_ok(
    '{{#article}}<h1>{{title}}</h1> - {{../date}}{{/article}}',
    { article => { title => 'Backtracking' }, date => '2012-10-01' },
    '<h1>Backtracking</h1> - 2012-10-01',
    "backtracking with ../"
);

render_ok(
    <<'TEMPLATE',
{{#page}}
{{#article}}<h1>{{title}}</h1> - {{../../date}}{{/article}}
{{/page}}
TEMPLATE
    {
        page => {
            article => { title => 'Multilevel Backtracking' },
        },
        date => '2012-10-01',
    },
    <<'RENDERED',
<h1>Multilevel Backtracking</h1> - 2012-10-01
RENDERED
    "multilevel backtracking with ../"
);

render_ok(
    '{{#article}}<h1>{{title}}</h1> - {{../metadata.date}}{{/article}}',
    {
        article  => { title => 'Backtracking' },
        metadata => { date  => '2012-10-01' },
    },
    '<h1>Backtracking</h1> - 2012-10-01',
    "backtracking into other hash variables with ../ and ."
);

render_ok(
    '{{articles.[10].comments}}',
    {
        articles => {
            10 => { comments => "First post!" },
        },
    },
    'First post!',
    "field access with non-identifiers"
);

render_ok(
    '{{articles.[.foo\som#th"ing].comments}}',
    {
        articles => {
            '.foo\som#th"ing' => { comments => "First post!" },
        },
    },
    'First post!',
    "field access with non-identifiers"
);

render_ok(
    '{{articles.[10].comments}}',
    {
        articles => [
            (({}) x 10),
            { comments => "First post!" },
        ],
    },
    'First post!',
    "array dereferencing"
);

render_ok(
    '{{.}} {{this}}',
    "foo",
    "foo foo",
    "top level current context"
);

render_ok(
    '{{#thing}}{{.}} {{this}}{{/thing}}',
    {
        thing => [ "foo" ],
    },
    "foo foo",
    "nested current context"
);

render_ok(
    '{{#hellos}}{{this/text}}{{/hellos}}',
    {
        hellos => [
            { text => 'hello' },
            { text => 'Hello' },
            { text => 'HELLO' },
        ],
    },
    'helloHelloHELLO',
    "'this' with paths"
);

render_ok(
    '{{#thing}}{{{.}}} {{{this}}}{{/thing}}',
    {
        thing => [ "<foo>" ],
    },
    "<foo> <foo>",
    "{{{.}}}"
);

render_ok(
    '{{foo-bar}}',
    {
        'foo-bar' => "FOOBAR",
    },
    'FOOBAR',
    "- is a valid character"
);

render_ok(
    '{{foo.foo-bar}}',
    {
        foo => {
            'foo-bar' => "FOOBAR",
        },
    },
    'FOOBAR',
    "- is a valid character"
);

done_testing;