Skip to content

Commit 4e14487

Browse files
committed
chore: add unit tests for cli options.
1 parent 01ab830 commit 4e14487

File tree

10 files changed

+167
-12
lines changed

10 files changed

+167
-12
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{#someHelper true}}
2+
<div>Some known helper</div>
3+
{{#anotherHelper true}}
4+
<div>Another known helper</div>
5+
{{/anotherHelper}}
6+
{{/someHelper}}

spec/expected/compiled.string.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
2+
return "<div>Test String</div>";
3+
},"useData":true}

spec/expected/empty.amd.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/expected/empty.amd.namespace.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define(['handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = CustomNamespace.templates = CustomNamespace.templates || {};
3+
return templates['empty'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
4+
return "";
5+
},"useData":true});
6+
});

spec/expected/empty.amd.simple.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
2+
return "";
3+
},"useData":true}

spec/expected/help.menu.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Precompile handlebar templates.
2+
Usage: handlebars [template|directory]...
3+
4+
Options:
5+
--help Outputs this message [boolean]
6+
-f, --output Output File [string]
7+
--map Source Map File [string]
8+
-a, --amd Exports amd style (require.js) [boolean]
9+
-c, --commonjs Exports CommonJS style, path to Handlebars module [string] [default: null]
10+
-h, --handlebarPath Path to handlebar.js (only valid for amd-style) [string] [default: ""]
11+
-k, --known Known helpers [string]
12+
-o, --knownOnly Known helpers only [boolean]
13+
-m, --min Minimize output [boolean]
14+
-n, --namespace Template namespace [string] [default: "Handlebars.templates"]
15+
-s, --simple Output template function only. [boolean]
16+
-N, --name Name of passed string templates. Optional if running in a simple mode. Required when operating on
17+
multiple templates. [string]
18+
-i, --string Generates a template from the passed CLI argument.
19+
"-" is treated as a special value and causes stdin to be read for the template value. [string]
20+
-r, --root Template root. Base value that will be stripped from template names. [string]
21+
-p, --partial Compiling a partial template [boolean]
22+
-d, --data Include data when compiling [boolean]
23+
-e, --extension Template extension. [string] [default: "handlebars"]
24+
-b, --bom Removes the BOM (Byte Order Mark) from the beginning of the templates. [boolean]
25+
-v, --version Show version number [boolean]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
define(['handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3+
return templates['known.helpers'] = template({"1":function(container,depth0,helpers,partials,data) {
4+
var stack1, lookupProperty = container.lookupProperty || function(parent, propertyName) {
5+
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
6+
return parent[propertyName];
7+
}
8+
return undefined
9+
};
10+
return " <div>Some known helper</div>\n"
11+
+ ((stack1 = lookupProperty(helpers,"anotherHelper").call(depth0 != null ? depth0 : (container.nullContext || {}),true,{"name":"anotherHelper","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data,"loc":{"start":{"line":3,"column":4},"end":{"line":5,"column":22}}})) != null ? stack1 : "");
12+
},"2":function(container,depth0,helpers,partials,data) {
13+
return " <div>Another known helper</div>\n";
14+
},"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
15+
var stack1, lookupProperty = container.lookupProperty || function(parent, propertyName) {
16+
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
17+
return parent[propertyName];
18+
}
19+
return undefined
20+
};
21+
return ((stack1 = lookupProperty(helpers,"someHelper").call(depth0 != null ? depth0 : (container.nullContext || {}),true,{"name":"someHelper","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":15}}})) != null ? stack1 : "");
22+
},"useData":true});
23+
});
24+

spec/expected/version.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.7.3

spec/precompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ describe('precompiler', function() {
317317
Precompiler.loadTemplates(
318318
{ files: [__dirname + '/artifacts'], extension: 'handlebars' },
319319
function(err, opts) {
320-
equal(opts.templates.length, 3);
320+
equal(opts.templates.length, 4);
321321
equal(opts.templates[0].name, 'bom');
322322
equal(opts.templates[1].name, 'empty');
323323
equal(opts.templates[2].name, 'example_1');

tasks/test-bin.js

Lines changed: 97 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,108 @@ const chai = require('chai');
77
chai.use(require('chai-diff'));
88
const expect = chai.expect;
99

10-
module.exports = function(grunt) {
11-
grunt.registerTask('test:bin', function() {
12-
const stdout = executeBinHandlebars(
10+
const testCases = [
11+
{
12+
binInputParameters: ['-a', 'spec/artifacts/empty.handlebars'],
13+
outputLocation: 'stdout',
14+
expectedOutputSpec: './spec/expected/empty.amd.js'
15+
},
16+
{
17+
binInputParameters: [
1318
'-a',
19+
'-f',
20+
'TEST_OUTPUT',
1421
'spec/artifacts/empty.handlebars'
15-
);
22+
],
23+
outputLocation: 'TEST_OUTPUT',
24+
expectedOutputSpec: './spec/expected/empty.amd.js'
25+
},
26+
{
27+
binInputParameters: [
28+
'-a',
29+
'-n',
30+
'CustomNamespace.templates',
31+
'spec/artifacts/empty.handlebars'
32+
],
33+
outputLocation: 'stdout',
34+
expectedOutputSpec: './spec/expected/empty.amd.namespace.js'
35+
},
36+
{
37+
binInputParameters: [
38+
'-a',
39+
'--namespace',
40+
'CustomNamespace.templates',
41+
'spec/artifacts/empty.handlebars'
42+
],
43+
outputLocation: 'stdout',
44+
expectedOutputSpec: './spec/expected/empty.amd.namespace.js'
45+
},
46+
{
47+
binInputParameters: ['-a', '-s', 'spec/artifacts/empty.handlebars'],
48+
outputLocation: 'stdout',
49+
expectedOutputSpec: './spec/expected/empty.amd.simple.js'
50+
},
51+
{
52+
binInputParameters: ['-a', '-m', 'spec/artifacts/empty.handlebars'],
53+
outputLocation: 'stdout',
54+
expectedOutputSpec: './spec/expected/empty.amd.min.js'
55+
},
56+
{
57+
binInputParameters: [
58+
'spec/artifacts/known.helpers.handlebars',
59+
'-a',
60+
'-k',
61+
'someHelper',
62+
'-k',
63+
'anotherHelper',
64+
'-o'
65+
],
66+
outputLocation: 'stdout',
67+
expectedOutputSpec: './spec/expected/non.empty.amd.known.helper.js'
68+
},
69+
{
70+
binInputParameters: ['--help'],
71+
outputLocation: 'stdout',
72+
expectedOutputSpec: './spec/expected/help.menu.txt'
73+
},
74+
{
75+
binInputParameters: ['-v'],
76+
outputLocation: 'stdout',
77+
expectedOutputSpec: './spec/expected/version.txt'
78+
},
79+
{
80+
binInputParameters: ['-i', '<div>Test String</div>'],
81+
outputLocation: 'stdout',
82+
expectedOutputSpec: './spec/expected/compiled.string.txt'
83+
}
84+
];
1685

17-
const expectedOutput = fs.readFileSync(
18-
'./spec/expected/empty.amd.js',
19-
'utf-8'
20-
);
86+
module.exports = function(grunt) {
87+
grunt.registerTask('test:bin', function() {
88+
testCases.forEach(
89+
({ binInputParameters, outputLocation, expectedOutputSpec }) => {
90+
const stdout = executeBinHandlebars(...binInputParameters);
2191

22-
const normalizedOutput = normalizeCrlf(stdout);
23-
const normalizedExpectedOutput = normalizeCrlf(expectedOutput);
92+
const expectedOutput = fs.readFileSync(expectedOutputSpec, 'utf-8');
2493

25-
expect(normalizedOutput).not.to.be.differentFrom(normalizedExpectedOutput);
94+
const useStdout = outputLocation === 'stdout';
95+
const normalizedOutput = normalizeCrlf(
96+
useStdout ? stdout : fs.readFileSync(outputLocation, 'utf-8')
97+
);
98+
const normalizedExpectedOutput = normalizeCrlf(expectedOutput);
99+
100+
if (!useStdout) {
101+
fs.unlinkSync(outputLocation);
102+
}
103+
104+
expect(normalizedOutput).not.to.be.differentFrom(
105+
normalizedExpectedOutput,
106+
{
107+
relaxedSpace: true
108+
}
109+
);
110+
}
111+
);
26112
});
27113
};
28114

0 commit comments

Comments
 (0)