-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
partial-test.js
175 lines (164 loc) · 6.4 KB
/
partial-test.js
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
169
170
171
172
173
174
175
/* eslint-disable func-names */
require('./helper');
describe('Partials spec', function () {
beforeEach(function () {
Mustache.clearCache();
});
it('The greater-than operator should expand to the named partial.', function () {
var template = '"{{>text}}"';
var data = {};
var partials = {'text':'from partial'};
var expected = '"from partial"';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('The empty string should be used when the named partial is not found.', function () {
var template = '"{{>text}}"';
var data = {};
var partials = {};
var expected = '""';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('The greater-than operator should operate within the current context.', function () {
var template = '"{{>partial}}"';
var data = {'text':'content'};
var partials = {'partial':'*{{text}}*'};
var expected = '"*content*"';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('Inline partials should not be indented', function () {
var template = ' <div>{{> partial}}</div>';
var data = {};
var partials = {'partial':'This is a partial.'};
var expected = ' <div>This is a partial.</div>';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('Inline partials should not be indented (multiline)', function () {
var template = ' <div>{{> partial}}</div>';
var data = {};
var partials = {'partial':'This is a\npartial.'};
var expected = ' <div>This is a\n partial.</div>';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('The greater-than operator should properly recurse.', function () {
var template = '{{>node}}';
var data = {'content':'X','nodes':[{'content':'Y','nodes':[]}]};
var partials = {'node':'{{content}}<{{#nodes}}{{>node}}{{/nodes}}>'};
var expected = 'X<Y<>>';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('The greater-than operator should not alter surrounding whitespace.', function () {
var template = '| {{>partial}} |';
var data = {};
var partials = {'partial':'\t|\t'};
var expected = '| \t|\t |';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('"\r\n" should be considered a newline for standalone tags.', function () {
var template = '|\r\n{{>partial}}\r\n|';
var data = {};
var partials = {'partial':'>'};
var expected = '|\r\n>|';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('Standalone tags should not require a newline to precede them.', function () {
var template = ' {{>partial}}\n>';
var data = {};
var partials = {'partial':'>\n>'};
var expected = ' >\n >>';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('Superfluous in-tag whitespace should be ignored.', function () {
var template = '|{{> partial }}|';
var data = {'boolean':true};
var partials = {'partial':'[]'};
var expected = '|[]|';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('Each line of the partial should be indented before rendering.', function () {
var template = '\\\n {{>partial}}\n/\n';
var data = {
'content': '<\n->'
};
var partials = {
'partial': '|\n{{{content}}}\n|\n'
};
var expected = '\\\n |\n <\n->\n |\n/\n';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('Standalone tags should not require a newline to follow them.', function () {
var template = '>\n {{>partial}}';
var data = {
};
var partials = {
'partial': '>\n>'
};
var expected = '>\n >\n >';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('Whitespace should be left untouched.', function () {
var template = ' {{data}} {{> partial}}\n';
var data = {
'data': '|'
};
var partials = {
'partial': '>\n>'
};
var expected = ' | >\n>\n';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('Partial without indentation should inherit functions.', function () {
var template = '{{> partial }}';
var data = {
toUpperCase: function () {
return function (label) {
return label.toUpperCase();
};
}
};
var partials = {partial: 'aA-{{ #toUpperCase }}Input{{ /toUpperCase }}-Aa'};
var expected = 'aA-INPUT-Aa';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('Partial with indentation should inherit functions.', function () {
var template = ' {{> partial }}';
var data = {
toUpperCase: function () {
return function (label) {
return label.toUpperCase();
};
}
};
var partials = {partial: 'aA-{{ #toUpperCase }}Input{{ /toUpperCase }}-Aa'};
var expected = ' aA-INPUT-Aa';
var renderResult = Mustache.render(template, data, partials);
assert.equal(renderResult, expected);
});
it('Nested partials should support custom delimiters.', function () {
var tags = ['[[', ']]'];
var template = '[[> level1 ]]';
var partials = {
level1: 'partial 1\n[[> level2]]',
level2: 'partial 2\n[[> level3]]',
level3: 'partial 3\n[[> level4]]',
level4: 'partial 4\n[[> level5]]',
level5: 'partial 5',
};
var expected = 'partial 1\npartial 2\npartial 3\npartial 4\npartial 5';
var renderResult = Mustache.render(template, {}, partials, tags);
assert.equal(renderResult, expected);
});
});