Skip to content

Commit

Permalink
test: add fluent API for testing Handlebars
Browse files Browse the repository at this point in the history
use "expectTemplate(template)....toCompileTo(output)"
  • Loading branch information
nknapp committed Nov 13, 2019
1 parent 7ef8617 commit c2ac79c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions spec/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"shouldCompileTo": true,
"shouldCompileToWithPartials": true,
"shouldThrow": true,
"expectTemplate": true,
"compileWithPartials": true,
"console": true,
"require": true,
Expand Down
86 changes: 83 additions & 3 deletions spec/env/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var global = (function() { return this; }());
var global = (function() {
return this;
}());

var AssertError;
if (Error.captureStackTrace) {
Expand All @@ -16,17 +18,26 @@ if (Error.captureStackTrace) {
AssertError = Error;
}

/**
* @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
*/
global.shouldCompileTo = function(string, hashOrArray, expected, message) {
shouldCompileToWithPartials(string, hashOrArray, false, expected, message);
};

/**
* @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
*/
global.shouldCompileToWithPartials = function shouldCompileToWithPartials(string, hashOrArray, partials, expected, message) {
var result = compileWithPartials(string, hashOrArray, partials);
if (result !== expected) {
throw new AssertError("'" + result + "' should === '" + expected + "': " + message, shouldCompileToWithPartials);
}
};

/**
* @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
*/
global.compileWithPartials = function(string, hashOrArray, partials) {
var template,
ary,
Expand All @@ -36,8 +47,8 @@ global.compileWithPartials = function(string, hashOrArray, partials) {
delete hashOrArray.hash;
} else if (Object.prototype.toString.call(hashOrArray) === '[object Array]') {
ary = [];
ary.push(hashOrArray[0]);
ary.push({ helpers: hashOrArray[1], partials: hashOrArray[2] });
ary.push(hashOrArray[0]); // input
ary.push({helpers: hashOrArray[1], partials: hashOrArray[2]});
options = typeof hashOrArray[3] === 'object' ? hashOrArray[3] : {compat: hashOrArray[3]};
if (hashOrArray[4] != null) {
options.data = !!hashOrArray[4];
Expand Down Expand Up @@ -75,3 +86,72 @@ global.shouldThrow = function(callback, type, msg) {
throw new AssertError('It failed to throw', shouldThrow);
}
};


global.expectTemplate = function(templateAsString) {
return new HandlebarsTestBench(templateAsString);
};


function HandlebarsTestBench(templateAsString) {
this.templateAsString = templateAsString;
this.helpers = {};
this.partials = {};
this.input = {};
this.message = 'Template' + templateAsString + ' does not evaluate to expected output';
this.compileOptions = {};
this.runtimeOptions = {};
}

HandlebarsTestBench.prototype.withInput = function(input) {
this.input = input;
return this;
};

HandlebarsTestBench.prototype.withHelper = function(name, helperFunction) {
this.helpers[name] = helperFunction;
return this;
};

HandlebarsTestBench.prototype.withPartial = function(name, partialAsString) {
this.partials[name] = partialAsString;
return this;
};

HandlebarsTestBench.prototype.withCompileOptions = function(compileOptions) {
this.compileOptions = compileOptions;
return this;
};

HandlebarsTestBench.prototype.withRuntimeOptions = function(runtimeOptions) {
this.runtimeOptions = runtimeOptions;
return this;
};

HandlebarsTestBench.prototype.withMessage = function(message) {
this.message = message;
return this;
};

HandlebarsTestBench.prototype.toCompileTo = function(expectedOutputAsString) {
var self = this;
var compile = Object.keys(this.partials).length > 0
? CompilerContext.compileWithPartial
: CompilerContext.compile;

var combinedRuntimeOptions = {};
Object.keys(this.runtimeOptions).forEach(function(key) {
combinedRuntimeOptions[key] = self.runtimeOptions[key];
});
combinedRuntimeOptions.helpers = this.helpers;
combinedRuntimeOptions.partials = this.partials;

var template = compile(this.templateAsString, this.compileOptions);
var output = template(this.input, combinedRuntimeOptions);

if (output !== expectedOutputAsString) {
// Error message formatted so that IntelliJ-Idea shows "diff"-button
// https://stackoverflow.com/a/10945655/4251384
throw new AssertError(this.message + '\nexpected:' + expectedOutputAsString + 'but was:' + output);
}
};

0 comments on commit c2ac79c

Please sign in to comment.