[Pkg-javascript-commits] [node-jade] 38/72: Allow render and renderFile as fallbacks for compileFileAsync
Jelmer Vernooij
jelmer at moszumanska.debian.org
Sun Jul 3 18:03:27 UTC 2016
This is an automated email from the git hooks/post-receive script.
jelmer pushed a commit to annotated tag upstream/1.0.0
in repository node-jade.
commit e9466d1bdf55dd1d2f50ac4699ea636ac623a627
Author: Timothy Gu <timothygu99 at gmail.com>
Date: Fri Jul 3 01:07:29 2015 -0700
Allow render and renderFile as fallbacks for compileFileAsync
---
index.js | 15 ++++++----
test/compile-file-async.js | 75 ++++++++++++++++++++++++++++++++--------------
2 files changed, 61 insertions(+), 29 deletions(-)
diff --git a/index.js b/index.js
index af4d5c4..52ff501 100644
--- a/index.js
+++ b/index.js
@@ -101,7 +101,10 @@ var fallbacks = {
compile: ['compile', 'render'],
compileAsync: ['compileAsync', 'compile', 'render'],
compileFile: ['compileFile', 'compile', 'renderFile', 'render'],
- compileFileAsync: ['compileFileAsync', 'compileFile', 'compileAsync', 'compile'],
+ compileFileAsync: [
+ 'compileFileAsync', 'compileFile', 'compileAsync', 'compile',
+ 'renderFile', 'render'
+ ],
compileClient: ['compileClient'],
compileClientAsync: ['compileClientAsync', 'compileClient'],
compileFileClient: ['compileFileClient', 'compileClient'],
@@ -179,14 +182,14 @@ Transformer.prototype.compileFileAsync = function (filename, options, cb) {
}
if (this._hasMethod('compileFileAsync')) {
return tr.normalizeFnAsync(this._tr.compileFileAsync(filename, options), cb);
- } else if (this._hasMethod('compileFile')) {
- return tr.normalizeFnAsync(this._tr.compileFile(filename, options), cb);
- } else {
+ } else if (this._hasMethod('compileFile') || this._hasMethod('renderFile')) {
+ return tr.normalizeFnAsync(this.compileFile(filename, options), cb);
+ } else { // compileAsync || compile || render
return tr.normalizeFnAsync(tr.readFile(filename, 'utf8').then(function (str) {
if (this._hasMethod('compileAsync')) {
return this._tr.compileAsync(str, options);
- } else {
- return this._tr.compile(str, options);
+ } else { // compile || render
+ return this.compile(str, options);
}
}.bind(this)), cb);
}
diff --git a/test/compile-file-async.js b/test/compile-file-async.js
index 304c7d7..12c563f 100644
--- a/test/compile-file-async.js
+++ b/test/compile-file-async.js
@@ -26,26 +26,21 @@ test('compileFileAsync - with tr.compileFileAsync(src, options) => Promise(fn)',
});
assert(tr.compileFileAsync('example input', sentinel, cbSentinel) === normalizedSentinel);
});
-test('compileFileAsync - with tr.compileFile(src, options) => fn', function (override) {
+test('compileFileAsync - with tr.compileFile(src, options) => fn', function () {
var sentinel = {};
- var fnSentinel = {};
- var cbSentinel = {};
- var normalizedSentinel = {};
- override('normalizeFnAsync', function (fn, cb) {
- assert(fn === fnSentinel);
- assert(cb === cbSentinel);
- return normalizedSentinel;
- });
+ var fnSentinel = function (locals) {};
var tr = createTransformer({
name: 'test',
outputFormat: 'html',
- compileFile: function (str, options) {
- assert(str === 'example input');
+ compileFile: function (file, options) {
+ assert(file === 'example-input.txt');
assert(options === sentinel);
return fnSentinel;
}
});
- assert(tr.compileFileAsync('example input', sentinel, cbSentinel) === normalizedSentinel);
+ return tr.compileFileAsync('example-input.txt', sentinel).then(function (out) {
+ assert(out.fn === fnSentinel);
+ });
});
test('compileFileAsync - with tr.compileAsync(src, options) => Promise(fn)', function (override) {
var sentinel = {};
@@ -75,19 +70,12 @@ test('compileFileAsync - with tr.compileAsync(src, options) => Promise(fn)', fun
});
test('compileFileAsync - with tr.compile(src, options) => fn', function (override) {
var sentinel = {};
- var fnSentinel = {};
- var cbSentinel = {};
- var normalizedSentinel = {};
+ var fnSentinel = function (locals) {};
override('readFile', function (filename, encoding) {
assert(filename === 'example-input.txt');
assert(encoding === 'utf8');
return {then: function (fn) { return fn('example input'); }};
});
- override('normalizeFnAsync', function (fn, cb) {
- assert(fn === fnSentinel);
- assert(cb === cbSentinel);
- return normalizedSentinel;
- });
var tr = createTransformer({
name: 'test',
outputFormat: 'html',
@@ -97,9 +85,50 @@ test('compileFileAsync - with tr.compile(src, options) => fn', function (overrid
return fnSentinel;
}
});
- assert(tr.compileFileAsync('example-input.txt', sentinel, cbSentinel) === normalizedSentinel);
+ return tr.compileFileAsync('example-input.txt', sentinel).then(function (out) {
+ assert(out.fn === fnSentinel);
+ });
+});
+test('compileFileAsync - with tr.renderFile(file, options, locals) => output', function () {
+ var sentinel = {};
+ var localsSentinel = {};
+ var tr = createTransformer({
+ name: 'test',
+ outputFormat: 'html',
+ renderFile: function (file, options, locals) {
+ assert(file === 'example-input.txt');
+ assert(options === sentinel);
+ assert(locals === localsSentinel);
+ return 'example output';
+ }
+ });
+ return tr.compileFileAsync('example-input.txt', sentinel).then(function (out) {
+ assert(out.fn(localsSentinel) === 'example output');
+ });
+});
+test('compileFileAsync - with tr.render(src, options, locals) => output', function (override) {
+ var sentinel = {};
+ var localsSentinel = {};
+ override('readFile', function (filename, encoding) {
+ assert(filename === 'example-input.txt');
+ assert(encoding === 'utf8');
+ return {then: function (fn) { return fn('example input'); }};
+ });
+ var tr = createTransformer({
+ name: 'test',
+ outputFormat: 'html',
+ render: function (str, options, locals) {
+ assert(str === 'example input');
+ assert(options === sentinel);
+ assert(locals === localsSentinel);
+ return 'example output';
+ }
+ });
+ return tr.compileFileAsync('example-input.txt', sentinel).then(function (out) {
+ assert(out.fn(localsSentinel) === 'example output');
+ });
});
-test('compileFileAsync - without tr.compile, tr.compileAsync, tr.compileFile or tr.compileFileAsync', function (override) {
+test('compileFileAsync - without tr.compile, tr.compileAsync, tr.compileFile, tr.compileFileAsync, tr.render or tr.renderFile', function (override) {
override('readFile', function (filename) {
assert(filename === 'example-input.txt');
return Promise.resolve('example input');
@@ -107,7 +136,7 @@ test('compileFileAsync - without tr.compile, tr.compileAsync, tr.compileFile or
var tr = createTransformer({
name: 'test',
outputFormat: 'html',
- render: function () {
+ compileClient: function () {
}
});
return tr.compileFileAsync('example-input.txt', {}).then(function () {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-jade.git
More information about the Pkg-javascript-commits
mailing list