test: add fluent API for testing Handlebars
use "expectTemplate(template)....toCompileTo(output)"
This commit is contained in:
committed by
Nils Knappmeier
parent
7ef86173ab
commit
c2ac79c970
@@ -13,6 +13,7 @@
|
|||||||
"shouldCompileTo": true,
|
"shouldCompileTo": true,
|
||||||
"shouldCompileToWithPartials": true,
|
"shouldCompileToWithPartials": true,
|
||||||
"shouldThrow": true,
|
"shouldThrow": true,
|
||||||
|
"expectTemplate": true,
|
||||||
"compileWithPartials": true,
|
"compileWithPartials": true,
|
||||||
"console": true,
|
"console": true,
|
||||||
"require": true,
|
"require": true,
|
||||||
|
|||||||
Vendored
+83
-3
@@ -1,4 +1,6 @@
|
|||||||
var global = (function() { return this; }());
|
var global = (function() {
|
||||||
|
return this;
|
||||||
|
}());
|
||||||
|
|
||||||
var AssertError;
|
var AssertError;
|
||||||
if (Error.captureStackTrace) {
|
if (Error.captureStackTrace) {
|
||||||
@@ -16,10 +18,16 @@ if (Error.captureStackTrace) {
|
|||||||
AssertError = Error;
|
AssertError = Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
|
||||||
|
*/
|
||||||
global.shouldCompileTo = function(string, hashOrArray, expected, message) {
|
global.shouldCompileTo = function(string, hashOrArray, expected, message) {
|
||||||
shouldCompileToWithPartials(string, hashOrArray, false, expected, message);
|
shouldCompileToWithPartials(string, hashOrArray, false, expected, message);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
|
||||||
|
*/
|
||||||
global.shouldCompileToWithPartials = function shouldCompileToWithPartials(string, hashOrArray, partials, expected, message) {
|
global.shouldCompileToWithPartials = function shouldCompileToWithPartials(string, hashOrArray, partials, expected, message) {
|
||||||
var result = compileWithPartials(string, hashOrArray, partials);
|
var result = compileWithPartials(string, hashOrArray, partials);
|
||||||
if (result !== expected) {
|
if (result !== expected) {
|
||||||
@@ -27,6 +35,9 @@ global.shouldCompileToWithPartials = function shouldCompileToWithPartials(string
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
|
||||||
|
*/
|
||||||
global.compileWithPartials = function(string, hashOrArray, partials) {
|
global.compileWithPartials = function(string, hashOrArray, partials) {
|
||||||
var template,
|
var template,
|
||||||
ary,
|
ary,
|
||||||
@@ -36,8 +47,8 @@ global.compileWithPartials = function(string, hashOrArray, partials) {
|
|||||||
delete hashOrArray.hash;
|
delete hashOrArray.hash;
|
||||||
} else if (Object.prototype.toString.call(hashOrArray) === '[object Array]') {
|
} else if (Object.prototype.toString.call(hashOrArray) === '[object Array]') {
|
||||||
ary = [];
|
ary = [];
|
||||||
ary.push(hashOrArray[0]);
|
ary.push(hashOrArray[0]); // input
|
||||||
ary.push({ helpers: hashOrArray[1], partials: hashOrArray[2] });
|
ary.push({helpers: hashOrArray[1], partials: hashOrArray[2]});
|
||||||
options = typeof hashOrArray[3] === 'object' ? hashOrArray[3] : {compat: hashOrArray[3]};
|
options = typeof hashOrArray[3] === 'object' ? hashOrArray[3] : {compat: hashOrArray[3]};
|
||||||
if (hashOrArray[4] != null) {
|
if (hashOrArray[4] != null) {
|
||||||
options.data = !!hashOrArray[4];
|
options.data = !!hashOrArray[4];
|
||||||
@@ -75,3 +86,72 @@ global.shouldThrow = function(callback, type, msg) {
|
|||||||
throw new AssertError('It failed to throw', shouldThrow);
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user