Skip to content

Commit b51aca0

Browse files
Merge pull request #528 from /issues/512-convert-test
Convert pattern_engines_tests.js to use tap/tape
2 parents 2fe8dd3 + 0acbb6c commit b51aca0

File tree

1 file changed

+154
-154
lines changed

1 file changed

+154
-154
lines changed

test/pattern_engines_tests.js

+154-154
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,164 @@
1-
(function () {
2-
'use strict';
3-
4-
var patternEngines = require('../core/lib/pattern_engines');
5-
var Pattern = require('../core/lib/object_factory').Pattern;
6-
7-
// the mustache test pattern, stolen from object_factory unit tests
8-
var mustacheTestPattern = new Pattern('source/_patterns/00-atoms/00-global/00-colors-alt.mustache', {d: 123});
9-
var mustacheTestPseudoPatternBasePattern = new Pattern('source/_patterns/04-pages/00-homepage.mustache', {d: 123});
10-
var mustacheTestPseudoPattern = new Pattern('source/_patterns/04-pages/00-homepage~emergency.json', {d: 123});
11-
mustacheTestPseudoPattern.isPseudoPattern = true;
12-
mustacheTestPseudoPattern.basePattern = mustacheTestPseudoPatternBasePattern;
13-
var engineNames = Object.keys(patternEngines);
14-
15-
16-
exports['patternEngines support functions'] = {
17-
'getEngineNameForPattern returns "mustache" from test pattern': function (test) {
18-
var engineName = patternEngines.getEngineNameForPattern(mustacheTestPattern);
19-
test.equals(engineName, 'mustache');
20-
test.done();
21-
},
22-
'getEngineNameForPattern returns "mustache" for a plain string template as a backwards compatibility measure': function (test) {
23-
test.expect(1);
24-
test.equals(patternEngines.getEngineNameForPattern('plain text string'), 'mustache');
25-
test.done();
26-
},
27-
'getEngineNameForPattern returns "mustache" for an artificial empty template': function (test) {
28-
test.expect(1);
29-
var emptyPattern = Pattern.createEmpty();
30-
test.equals(patternEngines.getEngineNameForPattern(emptyPattern), 'mustache');
31-
test.done();
32-
},
33-
'getEngineForPattern returns a reference to the mustache engine from test pattern': function (test) {
34-
var engine = patternEngines.getEngineForPattern(mustacheTestPattern);
35-
test.equals(engine, patternEngines.mustache);
36-
test.done();
37-
},
38-
'getEngineForPattern returns a reference to the mustache engine from test pseudo-pattern': function (test) {
39-
var engine = patternEngines.getEngineForPattern(mustacheTestPseudoPattern);
40-
test.equals(engine, patternEngines.mustache);
41-
test.done();
42-
},
43-
'isPseudoPatternJSON correctly identifies pseudo-pattern JSON filenames': function(test) {
44-
// each test case
45-
var filenames = {
46-
'00-homepage~emergency.json': true,
47-
'~emergency.json': true,
48-
'00-homepage~emergency.js': false,
49-
'00-homepage-emergency.js': false,
50-
'00-homepage.hbs': false,
51-
'00-homepage.json': false,
52-
'greatpic.jpg': false
53-
};
54-
// expect one test per test case
55-
test.expect(Object.keys(filenames).length);
56-
57-
// loop over each test case and test it
58-
Object.keys(filenames).forEach(function (filename) {
59-
var expectedResult = filenames[filename],
60-
actualResult = patternEngines.isPseudoPatternJSON(filename),
61-
testMessage = 'isPseudoPatternJSON should return ' + expectedResult + ' for ' + filename;
62-
test.strictEqual(actualResult, expectedResult, testMessage);
63-
});
64-
65-
// done
66-
test.done();
67-
},
68-
'isPatternFile correctly identifies pattern files and rejects non-pattern files': function(test){
69-
// each test case
70-
var filenames = {
71-
'00-comment-thread.mustache': true,
72-
'00-comment-thread.fakeextthatdoesntexist': false,
73-
'00-comment-thread': false,
74-
'_00-comment-thread.mustache': true,
75-
'.00-comment-thread.mustache': false,
76-
'00-comment-thread.json': false,
77-
'00-homepage~emergency.json': true
78-
};
79-
// expect one test per test case
80-
test.expect(Object.keys(filenames).length);
81-
82-
// loop over each test case and test it
83-
Object.keys(filenames).forEach(function (filename) {
84-
var expectedResult = filenames[filename],
85-
actualResult = patternEngines.isPatternFile(filename),
86-
testMessage = 'isPatternFile should return ' + expectedResult + ' for ' + filename;
87-
test.strictEqual(actualResult, expectedResult, testMessage);
88-
});
89-
90-
// done
91-
test.done();
92-
}
1+
'use strict';
2+
3+
var tap = require('tap');
4+
5+
var patternEngines = require('../core/lib/pattern_engines');
6+
var Pattern = require('../core/lib/object_factory').Pattern;
7+
8+
// the mustache test pattern, stolen from object_factory unit tests
9+
var mustacheTestPattern = new Pattern('source/_patterns/00-atoms/00-global/00-colors-alt.mustache', {d: 123});
10+
var mustacheTestPseudoPatternBasePattern = new Pattern('source/_patterns/04-pages/00-homepage.mustache', {d: 123});
11+
var mustacheTestPseudoPattern = new Pattern('source/_patterns/04-pages/00-homepage~emergency.json', {d: 123});
12+
mustacheTestPseudoPattern.isPseudoPattern = true;
13+
mustacheTestPseudoPattern.basePattern = mustacheTestPseudoPatternBasePattern;
14+
var engineNames = Object.keys(patternEngines);
15+
16+
tap.test('getEngineNameForPattern returns "mustache" from test pattern', function (test) {
17+
var engineName = patternEngines.getEngineNameForPattern(mustacheTestPattern);
18+
test.equals(engineName, 'mustache');
19+
test.end();
20+
});
21+
22+
tap.test('getEngineNameForPattern returns "mustache" for a plain string template as a backwards compatibility measure', function (test) {
23+
test.plan(1);
24+
test.equals(patternEngines.getEngineNameForPattern('plain text string'), 'mustache');
25+
test.end();
26+
});
27+
28+
tap.test('getEngineNameForPattern returns "mustache" for an artificial empty template', function (test) {
29+
test.plan(1);
30+
var emptyPattern = Pattern.createEmpty();
31+
test.equals(patternEngines.getEngineNameForPattern(emptyPattern), 'mustache');
32+
test.end();
33+
});
34+
35+
tap.test('getEngineForPattern returns a reference to the mustache engine from test pattern', function (test) {
36+
var engine = patternEngines.getEngineForPattern(mustacheTestPattern);
37+
test.equals(engine, patternEngines.mustache);
38+
test.end();
39+
});
40+
41+
tap.test('getEngineForPattern returns a reference to the mustache engine from test pseudo-pattern', function (test) {
42+
var engine = patternEngines.getEngineForPattern(mustacheTestPseudoPattern);
43+
test.equals(engine, patternEngines.mustache);
44+
test.end();
45+
});
46+
47+
tap.test('isPseudoPatternJSON correctly identifies pseudo-pattern JSON filenames', function(test) {
48+
// each test case
49+
var filenames = {
50+
'00-homepage~emergency.json': true,
51+
'~emergency.json': true,
52+
'00-homepage~emergency.js': false,
53+
'00-homepage-emergency.js': false,
54+
'00-homepage.hbs': false,
55+
'00-homepage.json': false,
56+
'greatpic.jpg': false
57+
};
58+
// expect one test per test case
59+
test.plan(Object.keys(filenames).length);
60+
61+
// loop over each test case and test it
62+
Object.keys(filenames).forEach(function (filename) {
63+
var expectedResult = filenames[filename],
64+
actualResult = patternEngines.isPseudoPatternJSON(filename),
65+
testMessage = 'isPseudoPatternJSON should return ' + expectedResult + ' for ' + filename;
66+
test.strictEqual(actualResult, expectedResult, testMessage);
67+
});
68+
69+
// done
70+
test.end();
71+
});
72+
73+
tap.test('isPatternFile correctly identifies pattern files and rejects non-pattern files', function(test){
74+
// each test case
75+
var filenames = {
76+
'00-comment-thread.mustache': true,
77+
'00-comment-thread.fakeextthatdoesntexist': false,
78+
'00-comment-thread': false,
79+
'_00-comment-thread.mustache': true,
80+
'.00-comment-thread.mustache': false,
81+
'00-comment-thread.json': false,
82+
'00-homepage~emergency.json': true
9383
};
84+
// expect one test per test case
85+
test.plan(Object.keys(filenames).length);
86+
87+
// loop over each test case and test it
88+
Object.keys(filenames).forEach(function (filename) {
89+
var expectedResult = filenames[filename],
90+
actualResult = patternEngines.isPatternFile(filename),
91+
testMessage = 'isPatternFile should return ' + expectedResult + ' for ' + filename;
92+
test.strictEqual(actualResult, expectedResult, testMessage);
93+
});
94+
95+
// done
96+
test.end();
97+
});
98+
99+
// testProps() utility function: given an object, and a hash of expected
100+
// 'property name':'property type' pairs, verify that the object contains each
101+
// expected property, and that each property is of the expected type.
102+
function testProps(object, propTests, test) {
94103

95-
// testProps() utility function: given an object, and a hash of expected
96-
// 'property name':'property type' pairs, verify that the object contains each
97-
// expected property, and that each property is of the expected type.
98-
function testProps(object, propTests, test) {
99-
100-
// function to test each expected property is present and the correct type
101-
function testProp(propName, types) {
102-
103-
var possibleTypes;
104-
105-
// handle "types" being a string or an array of strings
106-
if (types instanceof Array) {
107-
possibleTypes = types;
108-
} else {
109-
// "types" is just a single string, load it into an array; the rest of
110-
// the code expects it!
111-
possibleTypes = [types];
112-
}
113-
114-
var isOneOfTheseTypes = possibleTypes.map(function (type) {
115-
return typeof object[propName] === type;
116-
}).reduce(function(isPrevType, isCurrentType) {
117-
return isPrevType || isCurrentType;
118-
});
119-
120-
test.ok(object.hasOwnProperty(propName), '"' + propName + '" prop should be present');
121-
test.ok(isOneOfTheseTypes, '"' + propName + '" prop should be one of types ' + possibleTypes);
104+
// function to test each expected property is present and the correct type
105+
function testProp(propName, types) {
106+
107+
var possibleTypes;
108+
109+
// handle "types" being a string or an array of strings
110+
if (types instanceof Array) {
111+
possibleTypes = types;
112+
} else {
113+
// "types" is just a single string, load it into an array; the rest of
114+
// the code expects it!
115+
possibleTypes = [types];
122116
}
123117

124-
// go over each property test and run it
125-
Object.keys(propTests).forEach(function (propName) {
126-
var propType = propTests[propName];
127-
testProp(propName, propType);
118+
var isOneOfTheseTypes = possibleTypes.map(function (type) {
119+
return typeof object[propName] === type;
120+
}).reduce(function(isPrevType, isCurrentType) {
121+
return isPrevType || isCurrentType;
128122
});
123+
124+
test.ok(object.hasOwnProperty(propName), '"' + propName + '" prop should be present');
125+
test.ok(isOneOfTheseTypes, '"' + propName + '" prop should be one of types ' + possibleTypes);
129126
}
130127

131-
exports['patternEngines initialization'] = {
132-
'patternEngines object contains at least the default mustache engine': function (test) {
133-
test.expect(1);
134-
test.ok(patternEngines.hasOwnProperty('mustache'));
135-
test.done();
136-
},
137-
'patternEngines object reports that it supports the .mustache extension': function (test) {
138-
test.expect(1);
139-
test.ok(patternEngines.isFileExtensionSupported('.mustache'));
140-
test.done();
141-
}
142-
};
128+
// go over each property test and run it
129+
Object.keys(propTests).forEach(function (propName) {
130+
var propType = propTests[propName];
131+
testProp(propName, propType);
132+
});
133+
}
134+
135+
tap.test('patternEngines object contains at least the default mustache engine', function (test) {
136+
test.plan(1);
137+
test.ok(patternEngines.hasOwnProperty('mustache'));
138+
test.end();
139+
});
140+
141+
tap.test('patternEngines object reports that it supports the .mustache extension', function (test) {
142+
test.plan(1);
143+
test.ok(patternEngines.isFileExtensionSupported('.mustache'));
144+
test.end();
145+
});
146+
147+
// make one big test group for each pattern engine
148+
engineNames.forEach(function (engineName) {
149+
tap.test('engine ' + engineName + ' contains expected properties and methods', function (test) {
150+
151+
var propertyTests = {
152+
'engine': ['object', 'function'],
153+
'engineName': 'string',
154+
'engineFileExtension': 'string',
155+
'renderPattern': 'function',
156+
'findPartials': 'function'
157+
};
143158

144-
// make one big test group for each pattern engine
145-
engineNames.forEach(function (engineName) {
146-
exports[engineName + ' patternEngine'] = {
147-
'engine contains expected properties and methods': function (test) {
148-
149-
var propertyTests = {
150-
'engine': ['object', 'function'],
151-
'engineName': 'string',
152-
'engineFileExtension': 'string',
153-
'renderPattern': 'function',
154-
'findPartials': 'function'
155-
};
156-
157-
test.expect(Object.keys(propertyTests).length * 2);
158-
testProps(patternEngines[engineName], propertyTests, test);
159-
test.done();
160-
}
161-
};
159+
test.plan(Object.keys(propertyTests).length * 2);
160+
testProps(patternEngines[engineName], propertyTests, test);
161+
test.end();
162162
});
163+
});
163164

164-
})();

0 commit comments

Comments
 (0)