[Pkg-javascript-commits] [node-jade] 02/72: Begin adding tests and add support for compile/compileClient etc.

Jelmer Vernooij jelmer at moszumanska.debian.org
Sun Jul 3 18:03:24 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 c9e437cba3e56def57887a7ca8adb73bbea7040c
Author: Forbes Lindesay <forbes at lindesay.co.uk>
Date:   Mon Feb 9 22:28:07 2015 +0000

    Begin adding tests and add support for compile/compileClient etc.
---
 .gitignore                        |   1 +
 index.js                          | 233 +++++++++++++++++++++++++++++++++-----
 package.json                      |  12 +-
 test/compile-async.js             |  62 ++++++++++
 test/compile-client-async.js      |  63 +++++++++++
 test/compile-client.js            |  37 ++++++
 test/compile-file-async.js        | 118 +++++++++++++++++++
 test/compile-file-client-async.js | 113 ++++++++++++++++++
 test/compile-file-client.js       |  63 +++++++++++
 test/compile-file.js              |  59 ++++++++++
 test/compile.js                   |  36 ++++++
 test/index.js                     |  39 +++++++
 test/render.js                    |  68 +++++++++++
 test/test.js                      |  44 +++++++
 14 files changed, 918 insertions(+), 30 deletions(-)

diff --git a/.gitignore b/.gitignore
index b83202d..34de02e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,3 +11,4 @@ logs
 results
 npm-debug.log
 node_modules
+/coverage
diff --git a/index.js b/index.js
index 3edddb6..780aba0 100644
--- a/index.js
+++ b/index.js
@@ -3,12 +3,48 @@
 var fs = require('fs');
 var assert = require('assert');
 var Promise = require('promise');
-var readFile = Promise.denodeify(fs.readFile);
 
-module.exports = function (transformer) {
+var tr = (module.exports = function (transformer) {
   return new Transformer(transformer);
-};
+});
+tr.Transformer = Transformer;
+tr.normalizeFn = normalizeFn;
+tr.normalizeFnAsync = normalizeFnAsync;
+tr.normalize = normalize;
+tr.normalizeAsync = normalizeAsync;
+tr.readFile = Promise.denodeify(fs.readFile);
+tr.readFileSync = fs.readFileSync;
 
+function isPromise(value) {
+  return value && (typeof value === 'object' || typeof value === 'function') && typeof value.then === 'function';
+}
+function normalizeFn(result) {
+  if (typeof result === 'function') {
+    return {fn: result, dependencies: []};
+  } else if (result && typeof result === 'object' && typeof result.fn === 'function') {
+    if ('dependencies' in result) {
+      if (!Array.isArray(result.dependencies)) {
+        throw new Error('Result should have a dependencies property that is an array');
+      }
+    } else {
+      result.dependencies = [];
+    }
+    return result;
+  } else {
+    throw new Error('Invalid result object from transform.');
+  }
+}
+function normalizeFnAsync(result, cb) {
+  return Promise.resolve(result).then(function (result) {
+    if (result && isPromise(result.fn)) {
+      return result.fn.then(function (fn) {
+        result.fn = fn;
+        return result;
+      });
+    }
+    return result;
+  }).then(tr.normalizeFn).nodeify(cb);
+}
 function normalize(result) {
   if (typeof result === 'string') {
     return {body: result, dependencies: []};
@@ -26,56 +62,199 @@ function normalize(result) {
   }
 }
 function normalizeAsync(result, cb) {
-  return Promise.resolve(result).then(normalize).nodeify(cb);
+  return Promise.resolve(result).then(function (result) {
+    if (result && isPromise(result.body)) {
+      return result.body.then(function (body) {
+        result.body = body;
+        return result;
+      });
+    }
+    return result;
+  }).then(tr.normalize).nodeify(cb);
 }
 
 function Transformer(tr) {
   assert(tr, 'Transformer must be an object');
   assert(typeof tr.name === 'string', 'Transformer must have a name');
   assert(typeof tr.outputFormat === 'string', 'Transformer must have an output format');
-  assert(['render', 'renderAsync', 'renderFile', 'renderFileAsync'].some(function (method) {
+  assert([
+    'compile',
+    'compileAsync',
+    'compileFile',
+    'compileFileAsync',
+    'compileClient',
+    'compileClientAsync',
+    'compileFileClient',
+    'compileFileClientAsync',
+    'render',
+    'renderAsync',
+    'renderFile',
+    'renderFileAsync'
+  ].some(function (method) {
     return typeof tr[method] === 'function';
   }), 'Transformer must implement at least one of the potential methods.');
   this._tr = tr;
   this.name = this._tr.name;
   this.outputFormat = this._tr.outputFormat;
 }
-Transformer.prototype.render = function (str, options) {
-  if (typeof this._tr.render === 'function') {
-    return normalize(this._tr.render(str, options));
-  } else if (typeof this._tr.renderAsync === 'function') {
-    throw new Error('This transform does not support synchronous rendering');
+
+Transformer.prototype._hasMethod = function (method) {
+  return typeof this._tr[method] === 'function';
+};
+
+/* COMPILE */
+
+Transformer.prototype.compile = function (str, options) {
+  if (this._hasMethod('compile')) {
+    return tr.normalizeFn(this._tr.compile(str, options));
   } else {
-    throw new Error('This transform does not support rendering plain strings');
+    throw new Error('This transform does not support synchronous compiling of plain strings');
   }
 };
-Transformer.prototype.renderAsync = function (str, options, cb) {
-  if (typeof this._tr.renderAsync === 'function') {
-    return normalizeAsync(this._tr.renderAsync(str, options), cb);
-  } else if (typeof this._tr.render === 'function') {
-    return normalizeAsync(this._tr.render(str, options), cb);
+Transformer.prototype.compileAsync = function (str, options, cb) {
+  if (this._hasMethod('compileAsync')) {
+    return tr.normalizeFnAsync(this._tr.compileAsync(str, options), cb);
+  } else if (this._hasMethod('compile')) {
+    return tr.normalizeFnAsync(this._tr.compile(str, options), cb);
   } else {
-    throw new Error('This transform does not support rendering of plain strings');
+    return Promise.reject(new Error('This transform does not support compiling plain strings')).nodeify(cb);
   }
 };
+Transformer.prototype.compileFile = function (filename, options) {
+  if (this._hasMethod('compileFile')) {
+    return tr.normalizeFn(this._tr.compileFile(filename, options));
+  } else if (this._hasMethod('compile')) {
+    return tr.normalizeFn(this._tr.compile(tr.readFileSync(filename, 'utf8'), options));
+  } else {
+    throw new Error('This transform does not support synchronous compiling');
+  }
+};
+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 {
+    return tr.normalizeFnAsync(tr.readFile(filename, 'utf8').then(function (str) {
+      if (this._hasMethod('compileAsync')) {
+        return this._tr.compileAsync(str, options);
+      } else if (this._hasMethod('compile')) {
+        return this._tr.compile(str, options);
+      } else {
+        throw new Error('Transform does not support compiling');
+      }
+    }.bind(this)), cb);
+  }
+};
+
+/* COMPILE CLIENT */
+
 
-Transformer.prototype.renderFile = function (filename, options) {
+Transformer.prototype.compileClient = function (str, options) {
+  if (this._hasMethod('compileClient')) {
+    return tr.normalize(this._tr.compileClient(str, options));
+  } else {
+    throw new Error('This transform does not support compile client plain strings');
+  }
+};
+Transformer.prototype.compileClientAsync = function (str, options, cb) {
+  if (this._hasMethod('compileClientAsync')) {
+    return tr.normalizeAsync(this._tr.compileClientAsync(str, options), cb);
+  } else if (this._hasMethod('compileClient')) {
+    return tr.normalizeAsync(this._tr.compileClient(str, options), cb);
+  } else {
+    return Promise.reject(new Error('This transform does not support compile client'));
+  }
+};
+Transformer.prototype.compileFileClient = function (filename, options) {
+  if (this._hasMethod('compileFileClient')) {
+    return tr.normalize(this._tr.compileFileClient(filename, options));
+  } else if (this._hasMethod('compileClient')) {
+    return tr.normalize(this._tr.compileClient(tr.readFileSync(filename, 'utf8'), options));
+  } else {
+    throw new Error('This transform does not support synchronous compiling to client');
+  }
+};
+Transformer.prototype.compileFileClientAsync = function (filename, options, cb) {
+  if (this._hasMethod('compileFileClientAsync')) {
+    return tr.normalizeAsync(this._tr.compileFileClientAsync(filename, options), cb);
+  } else if (this._hasMethod('compileFileClient')) {
+    return tr.normalizeAsync(this._tr.compileFileClient(filename, options), cb);
+  } else {
+    return tr.normalizeAsync(tr.readFile(filename, 'utf8').then(function (str) {
+      if (this._hasMethod('compileClientAsync')) {
+        return this._tr.compileClientAsync(str, options);
+      } else if (this._hasMethod('compileClient')) {
+        return this._tr.compileClient(str, options);
+      } else {
+        throw new Error('Transform does not support compileFileClientAsync');
+      }
+    }.bind(this)), cb);
+  }
+};
+
+/* RENDER */
+
+Transformer.prototype.render = function (str, options, locals) {
+  if (this._hasMethod('render')) {
+    return tr.normalize(this._tr.render(str, options, locals));
+  } else if (this._hasMethod('compile')) {
+    var compiled = tr.normalizeFn(this._tr.compile(str, options));
+    var body = compiled.fn(options || locals);
+    if (typeof body !== 'string') {
+      throw new Error('This transform does not support synchronous rendering');
+    }
+    return tr.normalize({body: body, dependencies: compiled.dependencies});
+  } else {
+    throw new Error('This transform does not support rendering plain strings');
+  }
+};
+Transformer.prototype.renderAsync = function (str, options, locals, cb) {
+  if (typeof locals === 'function') {
+    cb = locals;
+    locals = options;
+  }
+  if (this._hasMethod('renderAsync')) {
+    return tr.normalizeAsync(this._tr.renderAsync(str, options, locals), cb);
+  } else if (this._hasMethod('render')) {
+    return tr.normalizeAsync(this._tr.render(str, options, locals), cb);
+  } else if (this._hasMethod('compile') || this._hasMethod('compileAsync')) {
+    return tr.normalizeAsync(this.compileAsync(str, options).then(function (compiled) {
+      return {body: compiled.fn(options || locals), dependencies: compiled.dependencies};
+    }), cb);
+  } else {
+    return Promise.reject(new Error('This transform does not support rendering of plain strings'));
+  }
+};
+Transformer.prototype.renderFile = function (filename, options, locals) {
   if (typeof this._tr.renderFile === 'function') {
-    return normalize(this._tr.renderFile(filename, options));
+    return tr.normalize(this._tr.renderFile(filename, options, locals));
   } else if (typeof this._tr.render === 'function') {
-    return normalize(this._tr.render(fs.readFileSync(filename, 'utf8'), options));
+    return tr.normalize(this._tr.render(tr.readFileSync(filename, 'utf8'), options, locals));
+  } else if (this._hasMethod('compile') || this._hasMethod('compileFile')) {
+    var compiled = this.compileFile(filename, options);
+    return tr.normalize({body: compiled.fn(options || locals), dependencies: compiled.dependencies});
   } else {
-    throw new Error('This transform does not support synchronous rendering');
+    return Promise.reject(new Error('This transform does not support synchronous rendering'));
   }
 };
-Transformer.prototype.renderFileAsync = function (filename, options, cb) {
+Transformer.prototype.renderFileAsync = function (filename, options, locals, cb) {
+  if (typeof locals === 'function') {
+    cb = locals;
+    locals = options;
+  }
   if (typeof this._tr.renderFileAsync === 'function') {
-    return normalizeAsync(this._tr.renderFileAsync(filename, options), cb);
+    return tr.normalizeAsync(this._tr.renderFileAsync(filename, options, locals), cb);
   } else if (typeof this._tr.renderFile === 'function') {
-    return normalizeAsync(this._tr.renderFile(filename, options), cb);
+    return tr.normalizeAsync(this._tr.renderFile(filename, options, locals), cb);
+  } else if (this._hasMethod('compile') || this._hasMethod('compileAsync')
+             || this._hasMethod('compileFile') || this._hasMethod('compileFileAsync')) {
+    return tr.normalizeAsync(this.compileFileAsync(filename, options).then(function (compiled) {
+      return {body: compiled.fn(options || locals), dependencies: compiled.dependencies};
+    }), cb);
   } else {
-    return readFile(filename, 'utf8').then(function (str) {
-      return this.renderAsync(str, options);
-    }.bind(this)).then(normalize).nodeify(cb);
+    return tr.normalizeAsync(tr.readFile(filename, 'utf8').then(function (str) {
+      return this.renderAsync(str, options, locals);
+    }.bind(this)), cb);
   }
 };
diff --git a/package.json b/package.json
index 920dca6..d331870 100644
--- a/package.json
+++ b/package.json
@@ -2,13 +2,19 @@
   "name": "jstransformer",
   "version": "0.0.0",
   "description": "Normalize the API of any jstransformer",
-  "keywords": [],
+  "keywords": [
+    "jstransformer"
+  ],
   "dependencies": {
     "promise": "^6.0.1"
   },
-  "devDependencies": {},
+  "devDependencies": {
+    "istanbul": "^0.3.5",
+    "testit": "^1.2.0"
+  },
   "scripts": {
-    "test": "node test"
+    "test": "node test",
+    "coverage": "istanbul cover test/index.js"
   },
   "repository": {
     "type": "git",
diff --git a/test/compile-async.js b/test/compile-async.js
new file mode 100644
index 0000000..9495435
--- /dev/null
+++ b/test/compile-async.js
@@ -0,0 +1,62 @@
+'use strict';
+
+var assert = require('assert');
+var Promise = require('promise');
+var test = require('./test');
+var createTransformer = require('../');
+
+test('compileAsync - with tr.compileAsync(str, options) => Promise(fn)', function (override) {
+  var sentinel = {};
+  var fnSentinel = {};
+  var cbSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeFnAsync', function (fn, cb) {
+    assert(fn === fnSentinel);
+    assert(cb === cbSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileAsync: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileAsync('example input', sentinel, cbSentinel) === normalizedSentinel);
+});
+test('compileAsync - with tr.compile(str, options) => fn', function (override) {
+  var sentinel = {};
+  var fnSentinel = {};
+  var cbSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeFnAsync', function (fn, cb) {
+    assert(fn === fnSentinel);
+    assert(cb === cbSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compile: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel
+    }
+  });
+  assert(tr.compileAsync('example input', sentinel, cbSentinel) === normalizedSentinel);
+});
+test('compileAsync - without tr.compile or tr.compileAsync', function () {
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    render: function (str, options) {
+    }
+  });
+  return tr.compileAsync('example input', {}).then(function () {
+    throw new Error('Expected error');
+  }, function (err) {
+    if (!(/does not support/.test(err.message))) throw err;
+  });
+});
diff --git a/test/compile-client-async.js b/test/compile-client-async.js
new file mode 100644
index 0000000..2eabe42
--- /dev/null
+++ b/test/compile-client-async.js
@@ -0,0 +1,63 @@
+'use strict';
+
+var assert = require('assert');
+var test = require('./test');
+var createTransformer = require('../');
+
+test('compileClientAsync - with tr.compileClientAsync(src, options) => Promise(str)', function (override) {
+  var sentinel = {};
+  var bodySentinel = {};
+  var cbSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeAsync', function (body, cb) {
+    assert(body === bodySentinel);
+    assert(cb === cbSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileClientAsync: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return bodySentinel;
+    }
+  });
+  assert(tr.compileClientAsync('example input', sentinel, cbSentinel) === normalizedSentinel);
+});
+test('compileClientAsync - with tr.compileClient(src, options) => fn', function (override) {
+  var sentinel = {};
+  var bodySentinel = {};
+  var cbSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeAsync', function (body, cb) {
+    assert(body === bodySentinel);
+    assert(cb === cbSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileClient: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return bodySentinel;
+    }
+  });
+  assert(tr.compileClientAsync('example input', sentinel, cbSentinel) === normalizedSentinel);
+});
+test('compile - without tr.compileClient', function () {
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileFile: function () {
+    }
+  });
+  return tr.compileClientAsync('example input', {}).then(function () {
+    throw new Error('expected to have an error');
+  }, function (err) {
+    if (!/does not support/.test(err.message)) {
+      throw err;
+    }
+  });
+});
diff --git a/test/compile-client.js b/test/compile-client.js
new file mode 100644
index 0000000..59e4ca7
--- /dev/null
+++ b/test/compile-client.js
@@ -0,0 +1,37 @@
+'use strict';
+
+var assert = require('assert');
+var test = require('./test');
+var createTransformer = require('../');
+
+test('compileClient - with tr.compileClient(src, options) => str', function (override) {
+  var sentinel = {};
+  var localSentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('normalize', function (body) {
+    assert(body === fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileClient: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileClient('example input', sentinel) === normalizedSentinel);
+});
+test('compileClient - without tr.compileClient', function () {
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileFile: function () {
+    }
+  });
+  assert.throws(function () {
+    tr.compileClient('example input', {});
+  }, /does not support/);
+});
diff --git a/test/compile-file-async.js b/test/compile-file-async.js
new file mode 100644
index 0000000..304c7d7
--- /dev/null
+++ b/test/compile-file-async.js
@@ -0,0 +1,118 @@
+'use strict';
+
+var assert = require('assert');
+var Promise = require('promise');
+var test = require('./test');
+var createTransformer = require('../');
+
+test('compileFileAsync - with tr.compileFileAsync(src, options) => Promise(fn)', function (override) {
+  var sentinel = {};
+  var fnSentinel = {};
+  var cbSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeFnAsync', function (fn, cb) {
+    assert(fn === fnSentinel);
+    assert(cb === cbSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileFileAsync: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFileAsync('example input', sentinel, cbSentinel) === normalizedSentinel);
+});
+test('compileFileAsync - with tr.compileFile(src, options) => fn', function (override) {
+  var sentinel = {};
+  var fnSentinel = {};
+  var cbSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeFnAsync', function (fn, cb) {
+    assert(fn === fnSentinel);
+    assert(cb === cbSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileFile: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFileAsync('example input', sentinel, cbSentinel) === normalizedSentinel);
+});
+test('compileFileAsync - with tr.compileAsync(src, options) => Promise(fn)', function (override) {
+  var sentinel = {};
+  var fnSentinel = {};
+  var cbSentinel = {};
+  var normalizedSentinel = {};
+  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',
+    compileAsync: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFileAsync('example-input.txt', sentinel, cbSentinel) === normalizedSentinel);
+});
+test('compileFileAsync - with tr.compile(src, options) => fn', function (override) {
+  var sentinel = {};
+  var fnSentinel = {};
+  var cbSentinel = {};
+  var normalizedSentinel = {};
+  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',
+    compile: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFileAsync('example-input.txt', sentinel, cbSentinel) === normalizedSentinel);
+});
+test('compileFileAsync - without tr.compile, tr.compileAsync, tr.compileFile or tr.compileFileAsync', function (override) {
+  override('readFile', function (filename) {
+    assert(filename === 'example-input.txt');
+    return Promise.resolve('example input');
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    render: function () {
+    }
+  });
+  return tr.compileFileAsync('example-input.txt', {}).then(function () {
+    throw new Error('Expected error');
+  }, function (err) {
+    if (!(/does not support/.test(err.message))) throw err;
+  });
+});
diff --git a/test/compile-file-client-async.js b/test/compile-file-client-async.js
new file mode 100644
index 0000000..06cb007
--- /dev/null
+++ b/test/compile-file-client-async.js
@@ -0,0 +1,113 @@
+'use strict';
+
+var assert = require('assert');
+var Promise = require('promise');
+var test = require('./test');
+var createTransformer = require('../');
+
+test('compileFileClientAsync - with tr.compileFileClientAsync(filename, options) => Promise(fn)', function (override) {
+  var optionsSentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeAsync', function (fn) {
+    assert(fn === fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileFileClientAsync: function (filename, options) {
+      assert(filename === 'example-input.txt');
+      assert(options === optionsSentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFileClientAsync('example-input.txt', optionsSentinel) === normalizedSentinel);
+});
+test('compileFileClientAsync - with tr.compileFileClient(filename, options) => fn', function (override) {
+  var optionsSentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeAsync', function (fn) {
+    assert(fn === fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileFileClient: function (filename, options) {
+      assert(filename === 'example-input.txt');
+      assert(options === optionsSentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFileClientAsync('example-input.txt', optionsSentinel) === normalizedSentinel);
+});
+
+test('compileFileClient - with tr.compileClientAsync(filename, options) => fn', function (override) {
+  var optionsSentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('readFile', function (filename, encoding) {
+    assert(filename === 'example-input.txt');
+    assert(encoding === 'utf8');
+    return {then: function (fn) { return fn('example input'); }};
+  });
+  override('normalizeAsync', function (fn) {
+    assert(fn === fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileClientAsync: function (str, options) {
+      assert(str === 'example input');
+      assert(options === optionsSentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFileClientAsync('example-input.txt', optionsSentinel) === normalizedSentinel);
+});
+test('compileFileClient - with tr.compileClient(filename, options) => fn', function (override) {
+  var optionsSentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('readFile', function (filename, encoding) {
+    assert(filename === 'example-input.txt');
+    assert(encoding === 'utf8');
+    return {then: function (fn) { return fn('example input'); }};
+  });
+  override('normalizeAsync', function (fn) {
+    assert(fn === fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileClient: function (str, options) {
+      assert(str === 'example input');
+      assert(options === optionsSentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFileClientAsync('example-input.txt', optionsSentinel) === normalizedSentinel);
+});
+
+test('compileFileClientAsync - without tr.compileClient, tr.compileClientAsync, tr.compileFileClient or tr.compileFileClientAsync', function (override) {
+  override('readFile', function (filename, encoding) {
+    assert(filename === 'example-input.txt');
+    assert(encoding === 'utf8');
+    return Promise.resolve('expected text');
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    render: function () {
+    }
+  });
+  return tr.compileFileClientAsync('example-input.txt', {}).then(function () {
+    throw new Error('Missing expected error');
+  }, function (err) {
+    if (!/does not support/.test(err.message)) throw err;
+  });
+});
diff --git a/test/compile-file-client.js b/test/compile-file-client.js
new file mode 100644
index 0000000..989da45
--- /dev/null
+++ b/test/compile-file-client.js
@@ -0,0 +1,63 @@
+'use strict';
+
+var assert = require('assert');
+var Promise = require('promise');
+var test = require('./test');
+var createTransformer = require('../');
+
+test('compileFileClient - with tr.compileFileClient(filename, options) => fn', function (override) {
+  var optionsSentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('normalize', function (fn) {
+    assert(fn === fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileFileClient: function (filename, options) {
+      assert(filename === 'example-input.txt');
+      assert(options === optionsSentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFileClient('example-input.txt', optionsSentinel) === normalizedSentinel);
+});
+
+test('compileFileClient - with tr.compileClient(filename, options) => fn', function (override) {
+  var optionsSentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('readFileSync', function (filename, encoding) {
+    assert(filename === 'example-input.txt');
+    assert(encoding === 'utf8');
+    return 'example input';
+  });
+  override('normalize', function (fn) {
+    assert(fn === fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileClient: function (str, options) {
+      assert(str === 'example input');
+      assert(options === optionsSentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFileClient('example-input.txt', optionsSentinel) === normalizedSentinel);
+});
+
+test('compileFileClient - without tr.compileClient or tr.compileFileClient', function () {
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    render: function () {
+    }
+  });
+  assert.throws(function () {
+    tr.compileFileClient('example-input.txt', {});
+  }, /does not support/);
+});
diff --git a/test/compile-file.js b/test/compile-file.js
new file mode 100644
index 0000000..3812acb
--- /dev/null
+++ b/test/compile-file.js
@@ -0,0 +1,59 @@
+'use strict';
+
+var assert = require('assert');
+var test = require('./test');
+var createTransformer = require('../');
+
+test('compileFile - with tr.compileFile(src, options) => fn', function (override) {
+  var sentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeFn', function (fn) {
+    assert(fn === fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileFile: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFile('example input', sentinel) === normalizedSentinel);
+});
+test('compileFile - with tr.compile(src, options) => fn', function (override) {
+  var sentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('readFileSync', function (filename) {
+    assert(filename === 'example-input.txt');
+    return 'example input';
+  });
+  override('normalizeFn', function (fn) {
+    assert(fn == fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compile: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compileFile('example-input.txt', sentinel) === normalizedSentinel);
+});
+test('compileFile - without tr.compile or tr.compileFile', function () {
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    render: function () {
+    }
+  });
+  assert.throws(function () {
+    tr.compileFile('example input', {});
+  }, /does not support/);
+});
diff --git a/test/compile.js b/test/compile.js
new file mode 100644
index 0000000..346b73d
--- /dev/null
+++ b/test/compile.js
@@ -0,0 +1,36 @@
+'use strict';
+
+var assert = require('assert');
+var test = require('./test');
+var createTransformer = require('../');
+
+test('compile - with tr.compile(src, options) => fn', function (override) {
+  var sentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeFn', function (fn) {
+    assert(fn === fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compile: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.compile('example input', sentinel) === normalizedSentinel);
+});
+test('compile - without tr.compile', function () {
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileFile: function () {
+    }
+  });
+  assert.throws(function () {
+    tr.compile('example input', {});
+  }, /does not support/);
+});
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..b01165a
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,39 @@
+'use strict';
+
+var assert = require('assert');
+var test = require('testit');
+var createTransformer = require('../');
+
+test('constructor - throws if `tr` is not an object', function () {
+  assert.throws(function () {
+    createTransformer(false);
+  }, /Transformer must be an object/);
+});
+test('constructor - throws if `tr` does not have a name', function () {
+  assert.throws(function () {
+    createTransformer({});
+  }, /Transformer must have a name/);
+});
+test('constructor - throws if `tr` does not have an output format', function () {
+  assert.throws(function () {
+    createTransformer({name: 'test'});
+  }, /Transformer must have an output format/);
+});
+test('constructor - throws if `tr` does not have any methods', function () {
+  assert.throws(function () {
+    createTransformer({name: 'test', outputFormat: 'html'});
+  }, /Transformer must implement at least one of the potential methods/);
+});
+test('constructor - passes for a well formed transformer', function () {
+  createTransformer({name: 'test', outputFormat: 'html', render: function () { return '<br/>'; }});
+});
+
+require('./compile');
+require('./compile-async');
+require('./compile-file');
+require('./compile-file-async');
+require('./compile-client');
+require('./compile-client-async');
+require('./compile-file-client');
+require('./compile-file-client-async');
+require('./render');
diff --git a/test/render.js b/test/render.js
new file mode 100644
index 0000000..84615b1
--- /dev/null
+++ b/test/render.js
@@ -0,0 +1,68 @@
+'use strict';
+
+var assert = require('assert');
+var test = require('./test');
+var createTransformer = require('../');
+
+test('render - with tr.render(src, options) => str', function (override) {
+  var sentinel = {};
+  var localSentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('normalize', function (body) {
+    assert(body === fnSentinel);
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    render: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.render('example input', sentinel) === normalizedSentinel);
+});
+test('render - with tr.compile(src, options) => fn', function (override) {
+  var sentinel = {};
+  var fnSentinel = {};
+  var normalizedSentinel = {};
+  override('normalizeFn', function (body) {
+    assert(body === fnSentinel);
+    return {
+      fn: function (locals) {
+        return '<br />';
+      },
+      dependencies: ['example.js']
+    };
+  });
+  override('normalize', function (result) {
+    assert.deepEqual(result, {
+      body: '<br />',
+      dependencies: ['example.js']
+    });
+    return normalizedSentinel;
+  });
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compile: function (str, options) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      return fnSentinel;
+    }
+  });
+  assert(tr.render('example input', sentinel) === normalizedSentinel);
+});
+test('render - without tr.render', function () {
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileClient: function () {
+    }
+  });
+  assert.throws(function () {
+    tr.render('example input', {});
+  }, /does not support/);
+});
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..86e624b
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,44 @@
+'use strict';
+
+var testit = require('testit');
+var tr = require('../');
+
+module.exports = test;
+function test(name, fn) {
+  testit(name, function () {
+    var originals = {}, result;
+    try {
+      result = fn(function (name, overrider) {
+        originals[name] = tr[name];
+        tr[name] = overrider;
+      });
+    } catch (ex) {
+      Object.keys(originals).forEach(function (key) {
+        tr[key] = originals[key];
+      });
+      throw ex;
+    }
+    Object.keys(originals).forEach(function (key) {
+      tr[key] = originals[key];
+    });
+    if (Object.keys(originals).length === 0) {
+      return result;
+    } else if (result) {
+      return result.then(function () {
+        Object.keys(originals).forEach(function (key) {
+          tr[key] = originals[key];
+        });
+      }, function (err) {
+        Object.keys(originals).forEach(function (key) {
+          tr[key] = originals[key];
+        });
+        throw err;
+      });
+      throw new Error('Asynchronous tests cannot use mocks');
+    } else {
+      Object.keys(originals).forEach(function (key) {
+        tr[key] = originals[key];
+      });
+    }
+  });
+}

-- 
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