Skip to content

Commit

Permalink
chore: add unit tests for cli options.
Browse files Browse the repository at this point in the history
  • Loading branch information
aorinevo committed Mar 29, 2020
1 parent 01ab830 commit 4e14487
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 12 deletions.
6 changes: 6 additions & 0 deletions spec/artifacts/known.helpers.handlebars
@@ -0,0 +1,6 @@
{{#someHelper true}}
<div>Some known helper</div>
{{#anotherHelper true}}
<div>Another known helper</div>
{{/anotherHelper}}
{{/someHelper}}
3 changes: 3 additions & 0 deletions spec/expected/compiled.string.txt
@@ -0,0 +1,3 @@
{"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
return "<div>Test String</div>";
},"useData":true}
1 change: 1 addition & 0 deletions spec/expected/empty.amd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions spec/expected/empty.amd.namespace.js
@@ -0,0 +1,6 @@
define(['handlebars.runtime'], function(Handlebars) {
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = CustomNamespace.templates = CustomNamespace.templates || {};
return templates['empty'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
return "";
},"useData":true});
});
3 changes: 3 additions & 0 deletions spec/expected/empty.amd.simple.js
@@ -0,0 +1,3 @@
{"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
return "";
},"useData":true}
25 changes: 25 additions & 0 deletions spec/expected/help.menu.txt
@@ -0,0 +1,25 @@
Precompile handlebar templates.
Usage: handlebars [template|directory]...

Options:
--help Outputs this message [boolean]
-f, --output Output File [string]
--map Source Map File [string]
-a, --amd Exports amd style (require.js) [boolean]
-c, --commonjs Exports CommonJS style, path to Handlebars module [string] [default: null]
-h, --handlebarPath Path to handlebar.js (only valid for amd-style) [string] [default: ""]
-k, --known Known helpers [string]
-o, --knownOnly Known helpers only [boolean]
-m, --min Minimize output [boolean]
-n, --namespace Template namespace [string] [default: "Handlebars.templates"]
-s, --simple Output template function only. [boolean]
-N, --name Name of passed string templates. Optional if running in a simple mode. Required when operating on
multiple templates. [string]
-i, --string Generates a template from the passed CLI argument.
"-" is treated as a special value and causes stdin to be read for the template value. [string]
-r, --root Template root. Base value that will be stripped from template names. [string]
-p, --partial Compiling a partial template [boolean]
-d, --data Include data when compiling [boolean]
-e, --extension Template extension. [string] [default: "handlebars"]
-b, --bom Removes the BOM (Byte Order Mark) from the beginning of the templates. [boolean]
-v, --version Show version number [boolean]
24 changes: 24 additions & 0 deletions spec/expected/non.empty.amd.known.helper.js
@@ -0,0 +1,24 @@
define(['handlebars.runtime'], function(Handlebars) {
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
return templates['known.helpers'] = template({"1":function(container,depth0,helpers,partials,data) {
var stack1, lookupProperty = container.lookupProperty || function(parent, propertyName) {
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
return parent[propertyName];
}
return undefined
};
return " <div>Some known helper</div>\n"
+ ((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 : "");
},"2":function(container,depth0,helpers,partials,data) {
return " <div>Another known helper</div>\n";
},"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
var stack1, lookupProperty = container.lookupProperty || function(parent, propertyName) {
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
return parent[propertyName];
}
return undefined
};
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 : "");
},"useData":true});
});

1 change: 1 addition & 0 deletions spec/expected/version.txt
@@ -0,0 +1 @@
4.7.3
2 changes: 1 addition & 1 deletion spec/precompiler.js
Expand Up @@ -317,7 +317,7 @@ describe('precompiler', function() {
Precompiler.loadTemplates(
{ files: [__dirname + '/artifacts'], extension: 'handlebars' },
function(err, opts) {
equal(opts.templates.length, 3);
equal(opts.templates.length, 4);
equal(opts.templates[0].name, 'bom');
equal(opts.templates[1].name, 'empty');
equal(opts.templates[2].name, 'example_1');
Expand Down
108 changes: 97 additions & 11 deletions tasks/test-bin.js
Expand Up @@ -7,22 +7,108 @@ const chai = require('chai');
chai.use(require('chai-diff'));
const expect = chai.expect;

module.exports = function(grunt) {
grunt.registerTask('test:bin', function() {
const stdout = executeBinHandlebars(
const testCases = [
{
binInputParameters: ['-a', 'spec/artifacts/empty.handlebars'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/empty.amd.js'
},
{
binInputParameters: [
'-a',
'-f',
'TEST_OUTPUT',
'spec/artifacts/empty.handlebars'
);
],
outputLocation: 'TEST_OUTPUT',
expectedOutputSpec: './spec/expected/empty.amd.js'
},
{
binInputParameters: [
'-a',
'-n',
'CustomNamespace.templates',
'spec/artifacts/empty.handlebars'
],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/empty.amd.namespace.js'
},
{
binInputParameters: [
'-a',
'--namespace',
'CustomNamespace.templates',
'spec/artifacts/empty.handlebars'
],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/empty.amd.namespace.js'
},
{
binInputParameters: ['-a', '-s', 'spec/artifacts/empty.handlebars'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/empty.amd.simple.js'
},
{
binInputParameters: ['-a', '-m', 'spec/artifacts/empty.handlebars'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/empty.amd.min.js'
},
{
binInputParameters: [
'spec/artifacts/known.helpers.handlebars',
'-a',
'-k',
'someHelper',
'-k',
'anotherHelper',
'-o'
],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/non.empty.amd.known.helper.js'
},
{
binInputParameters: ['--help'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/help.menu.txt'
},
{
binInputParameters: ['-v'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/version.txt'
},
{
binInputParameters: ['-i', '<div>Test String</div>'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/compiled.string.txt'
}
];

const expectedOutput = fs.readFileSync(
'./spec/expected/empty.amd.js',
'utf-8'
);
module.exports = function(grunt) {
grunt.registerTask('test:bin', function() {
testCases.forEach(
({ binInputParameters, outputLocation, expectedOutputSpec }) => {
const stdout = executeBinHandlebars(...binInputParameters);

const normalizedOutput = normalizeCrlf(stdout);
const normalizedExpectedOutput = normalizeCrlf(expectedOutput);
const expectedOutput = fs.readFileSync(expectedOutputSpec, 'utf-8');

expect(normalizedOutput).not.to.be.differentFrom(normalizedExpectedOutput);
const useStdout = outputLocation === 'stdout';
const normalizedOutput = normalizeCrlf(
useStdout ? stdout : fs.readFileSync(outputLocation, 'utf-8')
);
const normalizedExpectedOutput = normalizeCrlf(expectedOutput);

if (!useStdout) {
fs.unlinkSync(outputLocation);
}

expect(normalizedOutput).not.to.be.differentFrom(
normalizedExpectedOutput,
{
relaxedSpace: true
}
);
}
);
});
};

Expand Down

0 comments on commit 4e14487

Please sign in to comment.