Skip to content

Commit 92b7053

Browse files
committed
engine_mustache_tests file broken out of pattern_assembler.
1 parent 80c28e8 commit 92b7053

File tree

3 files changed

+297
-235
lines changed

3 files changed

+297
-235
lines changed

core/lib/pattern_assembler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,15 @@ var pattern_assembler = function () {
286286
currentPattern.extendedTemplate = currentPattern.template;
287287

288288
//find how many partials there may be for the given pattern
289-
var foundPatternPartials = currentPattern.findPartials(currentPattern);
289+
var foundPatternPartials = currentPattern.findPartials();
290290

291291
//find any listItem blocks that within the pattern, even if there are no partials
292292
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
293293

294294
// expand any partials present in this pattern; that is, drill down into
295295
// the template and replace their calls in this template with rendered
296296
// results
297+
297298
if (currentPattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) {
298299
// eslint-disable-next-line
299300
expandPartials(foundPatternPartials, list_item_hunter, patternlab, currentPattern);

test/engine_mustache_tests.js

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
"use strict";
2+
3+
var path = require('path');
4+
var pa = require('../core/lib/pattern_assembler');
5+
var Pattern = require('../core/lib/object_factory').Pattern;
6+
var testPatternsPath = path.resolve(__dirname, 'files', '_patterns');
7+
var eol = require('os').EOL;
8+
9+
// fake pattern lab constructor:
10+
// sets up a fake patternlab object, which is needed by the pattern processing
11+
// apparatus.
12+
function fakePatternLab() {
13+
var fpl = {
14+
partials: {},
15+
patterns: [],
16+
footer: '',
17+
header: '',
18+
listitems: {},
19+
listItemArray: [],
20+
data: {
21+
link: {}
22+
},
23+
config: require('../patternlab-config.json'),
24+
package: {}
25+
};
26+
27+
// patch the pattern source so the pattern assembler can correctly determine
28+
// the "subdir"
29+
fpl.config.paths.source.patterns = testPatternsPath;
30+
31+
return fpl;
32+
}
33+
34+
// function for testing sets of partials
35+
function testFindPartials(test, partialTests) {
36+
test.expect(partialTests.length + 1);
37+
38+
// setup current pattern from what we would have during execution
39+
// docs on partial syntax are here:
40+
// http://patternlab.io/docs/pattern-including.html
41+
var currentPattern = Pattern.create(
42+
'01-molecules/00-testing/00-test-mol.mustache', // relative path now
43+
null, // data
44+
{
45+
template: partialTests.join(eol)
46+
}
47+
);
48+
49+
// act
50+
var results = currentPattern.findPartials();
51+
52+
// assert
53+
test.equals(results.length, partialTests.length);
54+
partialTests.forEach(function (testString, index) {
55+
test.equals(results[index], testString);
56+
});
57+
58+
test.done();
59+
}
60+
61+
function testFindPartialsWithStyleModifiers(test, partialTests) {
62+
test.expect(partialTests.length + 1);
63+
64+
// setup current pattern from what we would have during execution
65+
// docs on partial syntax are here:
66+
// http://patternlab.io/docs/pattern-including.html
67+
var currentPattern = Pattern.create(
68+
'01-molecules/00-testing/00-test-mol.mustache', // relative path now
69+
null, // data
70+
{
71+
template: partialTests.join(eol)
72+
}
73+
);
74+
75+
// act
76+
var results = currentPattern.findPartialsWithStyleModifiers();
77+
78+
// assert
79+
test.equals(results.length, partialTests.length);
80+
partialTests.forEach(function (testString, index) {
81+
test.equals(results[index], testString);
82+
});
83+
84+
test.done();
85+
}
86+
87+
function testFindPartialsWithPatternParameters(test, partialTests) {
88+
test.expect(partialTests.length + 1);
89+
90+
// setup current pattern from what we would have during execution
91+
// docs on partial syntax are here:
92+
// http://patternlab.io/docs/pattern-including.html
93+
var currentPattern = Pattern.create(
94+
'01-molecules/00-testing/00-test-mol.mustache', // relative path now
95+
null, // data
96+
{
97+
template: partialTests.join(eol)
98+
}
99+
);
100+
101+
// act
102+
var results = currentPattern.findPartialsWithPatternParameters();
103+
104+
// assert
105+
test.equals(results.length, partialTests.length);
106+
partialTests.forEach(function (testString, index) {
107+
test.equals(results[index], testString);
108+
});
109+
110+
test.done();
111+
}
112+
113+
exports['engine_mustache'] = {
114+
'find_pattern_partials finds one simple partial': function (test) {
115+
testFindPartials(test, [
116+
"{{> molecules-comment-header}}"
117+
]);
118+
},
119+
120+
'find_pattern_partials finds simple partials under stressed circumstances': function (test) {
121+
testFindPartials(test, [
122+
"{{>molecules-comment-header}}",
123+
"{{> " + eol + " molecules-comment-header" + eol + "}}",
124+
"{{> molecules-weird-spacing }}"
125+
]);
126+
},
127+
128+
'find_pattern_partials finds one simple verbose partial': function (test) {
129+
testFindPartials(test, [
130+
'{{> 00-atoms/00-global/06-test }}'
131+
]);
132+
},
133+
134+
'find_pattern_partials finds partials with parameters': function (test) {
135+
testFindPartials(test, [
136+
"{{> molecules-single-comment(description: true) }}",
137+
"{{> molecules-single-comment(description: 42) }}",
138+
"{{> molecules-single-comment(description: '42') }}",
139+
"{{> molecules-single-comment(description: \"42\") }}",
140+
"{{> molecules-single-comment(description: 'test', anotherThing: 'retest') }}",
141+
"{{> molecules-single-comment(description: false, anotherThing: \"retest\") }}",
142+
'{{> molecules-single-comment(description:"A life is like a \"garden\". Perfect moments can be had, but not preserved, except in memory.") }}'
143+
]);
144+
},
145+
146+
'find_pattern_partials finds simple partials with style modifiers': function (test) {
147+
testFindPartials(test, [
148+
'{{> molecules-single-comment:foo }}',
149+
'{{> molecules-single-comment:foo|bar }}'
150+
]);
151+
},
152+
'find_pattern_partials finds mixed partials': function (test) {
153+
testFindPartials(test, [
154+
'{{> molecules-single-comment:foo(description: "test", anotherThing: true) }}',
155+
'{{> molecules-single-comment:foo|bar(description: true) }}'
156+
]);
157+
},
158+
159+
'find_pattern_partials finds one simple partial with styleModifier': function (test) {
160+
testFindPartialsWithStyleModifiers(test, [
161+
"{{> molecules-comment-header:test}}"
162+
]);
163+
},
164+
'find_pattern_partials finds partial with many styleModifiers': function (test) {
165+
testFindPartialsWithStyleModifiers(test, [
166+
"{{> molecules-comment-header:test|test2|test3}}"
167+
]);
168+
},
169+
'find_pattern_partials finds partials with differing styleModifiers': function (test) {
170+
testFindPartialsWithStyleModifiers(test, [
171+
"{{> molecules-comment-header:test|test2|test3}}",
172+
"{{> molecules-comment-header:foo-1}}",
173+
"{{> molecules-comment-header:bar_1}}"
174+
]);
175+
},
176+
'find_pattern_partials finds partials with styleModifiers when parameters present': function (test) {
177+
testFindPartialsWithStyleModifiers(test, [
178+
"{{> molecules-comment-header:test|test2|test3(description: true)}}",
179+
"{{> molecules-comment-header:foo-1(description: 'foo')}}",
180+
"{{> molecules-comment-header:bar_1(descrition: 'bar', anotherThing: 10102010) }}"
181+
]);
182+
},
183+
184+
'find_pattern_partials_with_parameters finds one simple partial with parameters': function (test) {
185+
testFindPartialsWithPatternParameters(test, [
186+
"{{> molecules-comment-header(description: 'test')}}"
187+
]);
188+
},
189+
'find_pattern_partials_with_parameters finds partials with parameters': function (test) {
190+
testFindPartialsWithPatternParameters(test, [
191+
"{{> molecules-single-comment(description: true) }}",
192+
"{{> molecules-single-comment(description: 42) }}",
193+
"{{> molecules-single-comment(description: '42') }}",
194+
"{{> molecules-single-comment(description: \"42\") }}",
195+
"{{> molecules-single-comment(description: 'test', anotherThing: 'retest') }}",
196+
"{{> molecules-single-comment(description: false, anotherThing: \"retest\") }}",
197+
'{{> molecules-single-comment(description:"A life is like a \"garden\". Perfect moments can be had, but not preserved, except in memory.") }}'
198+
]);
199+
},
200+
'find_pattern_partials finds partials with parameters when styleModifiers present': function (test) {
201+
testFindPartialsWithPatternParameters(test, [
202+
"{{> molecules-comment-header:test|test2|test3(description: true)}}",
203+
"{{> molecules-comment-header:foo-1(description: 'foo')}}",
204+
"{{> molecules-comment-header:bar_1(descrition: 'bar', anotherThing: 10102010) }}"
205+
]);
206+
}
207+
208+
};
209+
210+
211+
// don't run these tests unless mustache is installed
212+
var engineLoader = require('../core/lib/pattern_engines');
213+
if (!engineLoader.mustache) {
214+
console.log("Mustache engine not installed, skipping tests.");
215+
delete exports.engine_mustache;
216+
}

0 commit comments

Comments
 (0)