pax_global_header00006660000000000000000000000064127251465470014527gustar00rootroot0000000000000052 comment=7cad069255989318fc5638228fd2fa54fe35992f jstransformer-1.0.0/000077500000000000000000000000001272514654700144245ustar00rootroot00000000000000jstransformer-1.0.0/.gitignore000066400000000000000000000001511272514654700164110ustar00rootroot00000000000000lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz pids logs results npm-debug.log node_modules /coverage jstransformer-1.0.0/.travis.yml000066400000000000000000000001601272514654700165320ustar00rootroot00000000000000language: node_js sudo: false node_js: - "0.10" - "0.12" - "4" - "5" after_success: npm run coveralls jstransformer-1.0.0/CHANGELOG.md000066400000000000000000000021121272514654700162310ustar00rootroot00000000000000# Change Log All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). ## [1.0.0] - 2016-06-06 ### Changed - Browser support is fixed - Marked as stable ## [0.0.4] - 2015-11-23 ### Changed - Switched to [Keep a Changelog](http://keepachangelog.com/) spec for CHANGELOG.md - Creates an `options.filename` for `*File()` functions, if one is not provided ## [0.0.3] - 2015-07-13 ### Added - Added `.render()` fallbacks for `.compile()` ### Changed - Prefers locals over options when passing a single argument to `.render()` - Updated dependencies ## [0.0.2] - 2015-04-11 ### Added - Now have a logo - Added tests ### Changed - Updated documentation ## 0.0.1 - 2015-02-09 ### Added - Initial release [1.0.0]: https://github.com/jstransformers/jstransformer/compare/0.0.4...1.0.0 [0.0.4]: https://github.com/jstransformers/jstransformer/compare/0.0.3...0.0.4 [0.0.3]: https://github.com/jstransformers/jstransformer/compare/0.0.2...0.0.3 [0.0.2]: https://github.com/jstransformers/jstransformer/compare/0.0.1...0.0.2 jstransformer-1.0.0/LICENSE.md000066400000000000000000000021341272514654700160300ustar00rootroot00000000000000# The MIT License (MIT) Copyright (c) 2015 Forbes Lindesay > Permission is hereby granted, free of charge, to any person obtaining a copy > of this software and associated documentation files (the "Software"), to deal > in the Software without restriction, including without limitation the rights > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell > copies of the Software, and to permit persons to whom the Software is > furnished to do so, subject to the following conditions: > > The above copyright notice and this permission notice shall be included in > all copies or substantial portions of the Software. > > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE > SOFTWARE. jstransformer-1.0.0/README.md000066400000000000000000000077301272514654700157120ustar00rootroot00000000000000

JSTransformer

Normalize the API of any jstransformer

Build Status Dependency Status Developers' Dependency Status Coverage Status NPM version

## Installation npm install jstransformer ## Usage ```js var transformer = require('jstransformer'); var marked = transformer(require('jstransformer-marked')); var options = {}; var res = marked.render('Some **markdown**', options); // => {body: 'Some markdown', dependencies: []} ``` This gives the same API regardless of the jstransformer passed in. ## API A transformer, once normalised using this module, will implement the following methods. Note that if the underlying transformer cannot be used to implement the functionality, it may ultimately just throw an error. ### Returned object from `.render*` ```js {body: String, dependencies: Array.} ``` - `body` represents the result as a string - `dependencies` is an array of files that were read in as part of the render process (or an empty array if there were no dependencies) ### `.render` ```js transformer.render(str, options, locals); => {body: String, dependencies: Array.} ``` _requires the underlying transform to implement `.render` or `.compile`_ Transform a string and return an object. ### `.renderAsync` ```js transformer.renderAsync(str[, options], locals, callback); ``` ```js transformer.renderAsync(str[, options], locals); => Promise({body: String, dependencies: Array.}) ``` _requires the underlying transform to implement `.renderAsync` or `.render`_ Transform a string asynchronously. If a callback is provided, it is called as `callback(err, data)`, otherwise a Promise is returned. ### `.renderFile` ```js transformer.renderFile(filename, options, locals) => {body: String, dependencies: Array.} ``` _requires the underlying transform to implement `.renderFile`, `.render`, `.compileFile`, or `.compile`_ Transform a file and return an object. ### `.renderFileAsync` ```js transformer.renderFileAsync(filename[, options], locals, callback); ``` ```js transformer.renderFileAsync(filename[, options], locals); => Promise({body: String, dependencies: Array.}) ``` _requires the underlying transform to implement `.renderFileAsync`, `.renderFile`, `.renderAsync`, `.render`, `.compileFileAsync`, `.compileFile`, `.compileAsync`, or `.compileFile`_ Transform a file asynchronously. If a callback is provided, it is called as `callback(err, data)`, otherwise a Promise is returned. ### `.inputFormats` ```js var formats = transformer.inputFormats; => ['md', 'markdown'] ``` Returns an array of strings representing potential input formats for the transform. If not provided directly by the transform, results in an array containing the name of the transform. ### `.outputFormat` ```js var md = require('jstransformer')(require('jstransformer-markdown')) var outputFormat = md.outputFormat => 'html' ``` Returns a string representing the default output format the transform would be expected to return when calling `.render()`. ## License MIT jstransformer-1.0.0/index.js000066400000000000000000000336421272514654700161010ustar00rootroot00000000000000'use strict'; var fs = require('fs'); var assert = require('assert'); var Promise = require('promise'); var isPromise = require('is-promise'); 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; if (fs.readFile) { tr.readFile = Promise.denodeify(fs.readFile); tr.readFileSync = fs.readFileSync; } else { tr.readFile = function () { throw new Error('fs.readFile unsupported'); }; tr.readFileSync = function () { throw new Error('fs.readFileSync unsupported'); }; } 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: []}; } else if (result && typeof result === 'object' && typeof result.body === 'string') { 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 normalizeAsync(result, 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([ '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; this.inputFormats = this._tr.inputFormats || [this.name]; } var fallbacks = { compile: ['compile', 'render'], compileAsync: ['compileAsync', 'compile', 'render'], compileFile: ['compileFile', 'compile', 'renderFile', 'render'], compileFileAsync: [ 'compileFileAsync', 'compileFile', 'compileAsync', 'compile', 'renderFile', 'render' ], compileClient: ['compileClient'], compileClientAsync: ['compileClientAsync', 'compileClient'], compileFileClient: ['compileFileClient', 'compileClient'], compileFileClientAsync: [ 'compileFileClientAsync', 'compileFileClient', 'compileClientAsync', 'compileClient' ], render: ['render', 'compile'], renderAsync: ['renderAsync', 'render', 'compileAsync', 'compile'], renderFile: ['renderFile', 'render', 'compileFile', 'compile'], renderFileAsync: [ 'renderFileAsync', 'renderFile', 'renderAsync', 'render', 'compileFileAsync', 'compileFile', 'compileAsync', 'compile' ] }; Transformer.prototype._hasMethod = function (method) { return typeof this._tr[method] === 'function'; }; Transformer.prototype.can = function (method) { return fallbacks[method].some(function (method) { return this._hasMethod(method); }.bind(this)); }; /* COMPILE */ Transformer.prototype.compile = function (str, options) { if (!this._hasMethod('compile')) { if (this.can('render')) { var _this = this; return { fn: function (locals) { return tr.normalize(_this._tr.render(str, options, locals)).body; }, dependencies: [] }; } if (this.can('compileAsync')) { throw new Error('The Transform "' + this.name + '" does not support synchronous compilation'); } else if (this.can('compileFileAsync')) { throw new Error('The Transform "' + this.name + '" does not support compiling plain strings'); } else { throw new Error('The Transform "' + this.name + '" does not support compilation'); } } return tr.normalizeFn(this._tr.compile(str, options)); }; Transformer.prototype.compileAsync = function (str, options, cb) { if (!this.can('compileAsync')) { // compileFile* || renderFile* || renderAsync || compile*Client* return Promise.reject(new Error('The Transform "' + this.name + '" does not support compiling plain strings')).nodeify(cb); } if (this._hasMethod('compileAsync')) { return tr.normalizeFnAsync(this._tr.compileAsync(str, options), cb); } else { // render || compile return tr.normalizeFnAsync(this.compile(str, options), cb); } }; Transformer.prototype.compileFile = function (filename, options) { if (!this.can('compileFile')) { // compile*Client* || compile*Async || render*Async throw new Error('The Transform "' + this.name + '" does not support synchronous compilation'); } if (this._hasMethod('compileFile')) { return tr.normalizeFn(this._tr.compileFile(filename, options)); } else if (this._hasMethod('renderFile')) { return tr.normalizeFn(function (locals) { return tr.normalize(this._tr.renderFile(filename, options, locals)).body; }.bind(this)); } else { // render || compile if (!options) options = {}; if (options.filename === undefined) options.filename = filename; return this.compile(tr.readFileSync(filename, 'utf8'), options); } }; Transformer.prototype.compileFileAsync = function (filename, options, cb) { if (!this.can('compileFileAsync')) { return Promise.reject(new Error('The Transform "' + this.name + '" does not support compilation')); } if (this._hasMethod('compileFileAsync')) { return tr.normalizeFnAsync(this._tr.compileFileAsync(filename, options), cb); } else if (this._hasMethod('compileFile') || this._hasMethod('renderFile')) { return tr.normalizeFnAsync(this.compileFile(filename, options), cb); } else { // compileAsync || compile || render if (!options) options = {}; if (options.filename === undefined) options.filename = filename; return tr.normalizeFnAsync(tr.readFile(filename, 'utf8').then(function (str) { if (this._hasMethod('compileAsync')) { return this._tr.compileAsync(str, options); } else { // compile || render return this.compile(str, options); } }.bind(this)), cb); } }; /* COMPILE CLIENT */ Transformer.prototype.compileClient = function (str, options) { if (!this.can('compileClient')) { if (this.can('compileClientAsync')) { throw new Error('The Transform "' + this.name + '" does not support compiling for the client synchronously.'); } else if (this.can('compileFileClientAsync')) { throw new Error('The Transform "' + this.name + '" does not support compiling for the client from a string.'); } else { throw new Error('The Transform "' + this.name + '" does not support compiling for the client'); } } return tr.normalize(this._tr.compileClient(str, options)); }; Transformer.prototype.compileClientAsync = function (str, options, cb) { if (!this.can('compileClientAsync')) { if (this.can('compileFileClientAsync')) { return Promise.reject(new Error('The Transform "' + this.name + '" does not support compiling for the client from a string.')).nodeify(cb); } else { return Promise.reject(new Error('The Transform "' + this.name + '" does not support compiling for the client')).nodeify(cb); } } if (this._hasMethod('compileClientAsync')) { return tr.normalizeAsync(this._tr.compileClientAsync(str, options), cb); } else { return tr.normalizeAsync(this._tr.compileClient(str, options), cb); } }; Transformer.prototype.compileFileClient = function (filename, options) { if (!this.can('compileFileClient')) { if (this.can('compileFileClientAsync')) { throw new Error('The Transform "' + this.name + '" does not support compiling for the client synchronously.'); } else { throw new Error('The Transform "' + this.name + '" does not support compiling for the client'); } } if (this._hasMethod('compileFileClient')) { return tr.normalize(this._tr.compileFileClient(filename, options)); } else { if (!options) options = {}; if (options.filename === undefined) options.filename = filename; return tr.normalize(this._tr.compileClient(tr.readFileSync(filename, 'utf8'), options)); } }; Transformer.prototype.compileFileClientAsync = function (filename, options, cb) { if (!this.can('compileFileClientAsync')) { return Promise.reject(new Error('The Transform "' + this.name + '" does not support compiling for the client')).nodeify(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 { if (!options) options = {}; if (options.filename === undefined) options.filename = filename; return tr.normalizeAsync(tr.readFile(filename, 'utf8').then(function (str) { if (this._hasMethod('compileClientAsync')) { return this._tr.compileClientAsync(str, options); } else { return this._tr.compileClient(str, options); } }.bind(this)), cb); } }; /* RENDER */ Transformer.prototype.render = function (str, options, locals) { if (!this.can('render')) { if (this.can('renderAsync')) { throw new Error('The Transform "' + this.name + '" does not support rendering synchronously.'); } else if (this.can('renderFileAsync')) { throw new Error('The Transform "' + this.name + '" does not support rendering from a string.'); } else { throw new Error('The Transform "' + this.name + '" does not support rendering'); } } if (this._hasMethod('render')) { return tr.normalize(this._tr.render(str, options, locals)); } else { var compiled = tr.normalizeFn(this._tr.compile(str, options)); var body = compiled.fn(locals || options); if (typeof body !== 'string') { throw new Error('The Transform "' + this.name + '" does not support rendering synchronously.'); } return tr.normalize({body: body, dependencies: compiled.dependencies}); } }; Transformer.prototype.renderAsync = function (str, options, locals, cb) { if (typeof locals === 'function') { cb = locals; locals = options; } if (!this.can('renderAsync')) { if (this.can('renderFileAsync')) { return Promise.reject(new Error('The Transform "' + this.name + '" does not support rendering from a string.')).nodeify(cb); } else { return Promise.reject(new Error('The Transform "' + this.name + '" does not support rendering')).nodeify(cb); } } 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 { return tr.normalizeAsync(this.compileAsync(str, options).then(function (compiled) { return {body: compiled.fn(locals || options), dependencies: compiled.dependencies}; }), cb); } }; Transformer.prototype.renderFile = function (filename, options, locals) { if (!this.can('renderFile')) { // *Async, *Client throw new Error('The Transform "' + this.name + '" does not support rendering synchronously.'); } if (this._hasMethod('renderFile')) { return tr.normalize(this._tr.renderFile(filename, options, locals)); } else if (this._hasMethod('render')) { if (!options) options = {}; if (options.filename === undefined) options.filename = filename; return tr.normalize(this._tr.render(tr.readFileSync(filename, 'utf8'), options, locals)); } else { // compile || compileFile var compiled = this.compileFile(filename, options); return tr.normalize({body: compiled.fn(locals || options), dependencies: compiled.dependencies}); } }; Transformer.prototype.renderFileAsync = function (filename, options, locals, cb) { if (!this.can('renderFileAsync')) { // *Client throw new Error('The Transform "' + this.name + '" does not support rendering.'); } if (typeof locals === 'function') { cb = locals; locals = options; } if (this._hasMethod('renderFileAsync')) { return tr.normalizeAsync(this._tr.renderFileAsync(filename, options, locals), cb); } else if (this._hasMethod('renderFile')) { 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(locals || options), dependencies: compiled.dependencies}; }), cb); } else { // render || renderAsync if (!options) options = {}; if (options.filename === undefined) options.filename = filename; return tr.normalizeAsync(tr.readFile(filename, 'utf8').then(function (str) { return this.renderAsync(str, options, locals); }.bind(this)), cb); } }; jstransformer-1.0.0/logo.svg000066400000000000000000000042101272514654700161020ustar00rootroot00000000000000 jstransformer-1.0.0/package.json000066400000000000000000000012541272514654700167140ustar00rootroot00000000000000{ "name": "jstransformer", "version": "1.0.0", "description": "Normalize the API of any jstransformer", "keywords": [ "jstransformer" ], "dependencies": { "is-promise": "^2.0.0", "promise": "^7.0.1" }, "devDependencies": { "coveralls": "^2.11.2", "istanbul": "^0.4.0", "testit": "^2.0.2" }, "scripts": { "test": "node test", "coverage": "istanbul cover test", "coveralls": "npm run coverage && cat ./coverage/lcov.info | coveralls" }, "files": [ "index.js" ], "repository": { "type": "git", "url": "https://github.com/jstransformers/jstransformer.git" }, "author": "ForbesLindesay", "license": "MIT" } jstransformer-1.0.0/test/000077500000000000000000000000001272514654700154035ustar00rootroot00000000000000jstransformer-1.0.0/test/compile-async.js000066400000000000000000000043121272514654700205040ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var Promise = require('promise'); var test = require('./test'); var createTransformer = require('../'); test('compileAsync', function () { test('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('with tr.compile(str, options) => fn', function () { var sentinel = {}; var fnSentinel = function (locals) {}; var tr = createTransformer({ name: 'test', outputFormat: 'html', compile: function (str, options) { assert(str === 'example input'); assert(options === sentinel); return fnSentinel; } }); return tr.compileAsync('example input', sentinel).then(function (out) { assert(out.fn === fnSentinel); }); }); test('with tr.render(str, options, locals) => output', function () { var sentinel = {}; var localsSentinel = {}; 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.compileAsync('example input', sentinel).then(function (out) { assert(out.fn(localsSentinel) === 'example output'); }); }); test('without any of the above', function () { var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFile: function (filename, options) { } }); return tr.compileAsync('example input', {}).then(function () { throw new Error('Expected error'); }, function (err) { if (!(/does not support compiling plain strings/.test(err.message))) throw err; }); }); }); jstransformer-1.0.0/test/compile-client-async.js000066400000000000000000000045061272514654700217650ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var Promise = require('promise'); var test = require('./test'); var createTransformer = require('../'); test('compileClientAsync', function () { test('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('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('without any of the above', function () { var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFileClientAsync : function () { } }); var a = tr.compileClientAsync('example input', {}).then(function () { throw new Error('expected to have an error'); }, function (err) { if (!/does not support compiling for the client from a string/.test(err.message)) { throw err; } }); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFile: function () { } }); var b = tr.compileClientAsync('example input', {}).then(function () { throw new Error('expected to have an error'); }, function (err) { if (!/does not support compiling for the client/.test(err.message)) { throw err; } }); return Promise.all([a, b]); }); }); jstransformer-1.0.0/test/compile-client.js000066400000000000000000000030521272514654700206450ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var test = require('./test'); var createTransformer = require('../'); test('compileClient', function () { test('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('without any of the above', function () { var tr = createTransformer({ name: 'test', outputFormat: 'html', compileClientAsync: function () { } }); assert.throws(function () { tr.compileClient('example input', {}); }, /does not support compiling for the client synchronously/); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFileClient: function () { } }); assert.throws(function () { tr.compileClient('example input', {}); }, /does not support compiling for the client from a string/); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFile: function () { } }); assert.throws(function () { tr.compileClient('example input', {}); }, /does not support compiling for the client/); }); }); jstransformer-1.0.0/test/compile-file-async.js000066400000000000000000000112071272514654700214220ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var Promise = require('promise'); var test = require('./test'); var createTransformer = require('../'); test('compileFileAsync', function () { test('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('with tr.compileFile(src, options) => fn', function () { var sentinel = {}; var fnSentinel = function (locals) {}; var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFile: function (file, options) { assert(file === 'example-input.txt'); assert(options === sentinel); return fnSentinel; } }); return tr.compileFileAsync('example-input.txt', sentinel).then(function (out) { assert(out.fn === fnSentinel); }); }); test('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('with tr.compile(src, options) => fn', function (override) { var sentinel = {}; 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'); }}; }); var tr = createTransformer({ name: 'test', outputFormat: 'html', compile: function (str, options) { assert(str === 'example input'); assert(options === sentinel); return fnSentinel; } }); return tr.compileFileAsync('example-input.txt', sentinel).then(function (out) { assert(out.fn === fnSentinel); }); }); test('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('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('without any of the above', function (override) { override('readFile', function (filename) { assert(filename === 'example-input.txt'); return Promise.resolve('example input'); }); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileClient: 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; }); }); }); jstransformer-1.0.0/test/compile-file-client-async.js000066400000000000000000000067561272514654700227130ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var Promise = require('promise'); var test = require('./test'); var createTransformer = require('../'); test('compileFileClientAsync', function () { test('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('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('without any of the above', function (override) { 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; }); }); }); jstransformer-1.0.0/test/compile-file-client.js000066400000000000000000000041671272514654700215720ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var Promise = require('promise'); var test = require('./test'); var createTransformer = require('../'); test('compileFileClient', function () { test('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('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('without any of the above', function () { var tr = createTransformer({ name: 'test', outputFormat: 'html', compileClientAsync: function () { } }); assert.throws(function () { tr.compileFileClient('example-input.txt', {}); }, /does not support compiling for the client synchronously/); var tr = createTransformer({ name: 'test', outputFormat: 'html', render: function () { } }); assert.throws(function () { tr.compileFileClient('example-input.txt', {}); }, /does not support compiling for the client/); }); }); jstransformer-1.0.0/test/compile-file.js000066400000000000000000000054521272514654700203140ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var test = require('./test'); var createTransformer = require('../'); test('compileFile', function () { test('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('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('with tr.renderFile(src, options, locals) => output', function (override) { 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'; } }); assert(tr.compileFile('example-input.txt', sentinel).fn(localsSentinel) === 'example output'); }); test('with tr.render(src, options, locals) => output', function (override) { var sentinel = {}; var localsSentinel = {}; override('readFileSync', function (filename) { assert(filename === 'example-input.txt'); return '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'; } }); assert(tr.compileFile('example-input.txt', sentinel).fn(localsSentinel) === 'example output'); }); test('without any of the above', function () { var tr = createTransformer({ name: 'test', outputFormat: 'html', compileAsync: function () { } }); assert.throws(function () { tr.compileFile('example input', {}); }, /does not support synchronous compilation/); }); }); jstransformer-1.0.0/test/compile.js000066400000000000000000000034061272514654700173740ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var test = require('./test'); var createTransformer = require('../'); test('compile', function () { test('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('with tr.render(src, options, locals) => output', function () { var tr = createTransformer({ name: 'test', outputFormat: 'html', render: function (str, options, locals) { assert(str === 'example input'); return locals.name; } }); assert.equal(tr.compile('example input').fn({name: 'hola'}), 'hola'); }); test('without any of the above', function () { var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFile: function () { } }); assert.throws(function () { tr.compile('example input', {}); }, /does not support compiling plain strings/); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileAsync: function () { } }); assert.throws(function () { tr.compile('example input', {}); }, /does not support synchronous compilation/); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileClient: function () { } }); assert.throws(function () { tr.compile('example input', {}); }, /does not support compilation/); }); }); jstransformer-1.0.0/test/index.js000066400000000000000000000024621272514654700170540ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var test = require('testit'); var createTransformer = require('../'); test('constructor', function () { test('throws if `tr` is not an object', function () { assert.throws(function () { createTransformer(false); }, /Transformer must be an object/); }); test('throws if `tr` does not have a name', function () { assert.throws(function () { createTransformer({}); }, /Transformer must have a name/); }); test('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('passes for a well formed transformer', function () { createTransformer({name: 'test', outputFormat: 'html', render: function () { return '
'; }}); }); }); 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'); require('./render-async'); require('./render-file'); require('./render-file-async'); require('./input-formats'); require('./output-format'); require('./normalize'); require('./normalize-async'); jstransformer-1.0.0/test/input-formats.js000066400000000000000000000013101272514654700205440ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var test = require('./test'); var createTransformer = require('../'); test('inputFormats', function () { test('with tr.inputFormats', function (override) { var tr = createTransformer({ name: 'test', outputFormat: 'html', inputFormats: [ 'html', 'htm' ], render: function (str, options) { return str; } }); assert.deepEqual(tr.inputFormats, ['html', 'htm']); }); test('without any of the above', function (override) { var tr = createTransformer({ name: 'test', outputFormat: 'html', render: function (str, options) { return str; } }); assert.deepEqual(tr.inputFormats, ['test']); }); }); jstransformer-1.0.0/test/normalize-async.js000066400000000000000000000202241272514654700210540ustar00rootroot00000000000000'use strict'; var Promise = require('promise'); var assert = require('assert'); var test = require('./test'); var tr = require('../'); function testAsync (funcName, bodyVar, specs) { var func = tr[funcName]; var bodyType = specs.good[0], body = specs.good[1], notBodyType = specs.bad[0], notBody = specs.bad[1]; test(funcName, function () { test('Using callbacks', function (done) { var expected = { dependencies: [] }; expected[bodyVar] = body; func(body, function (err, normalized) { if (err) return done(err); try { assert.deepEqual(normalized, expected); } catch (err) { return done(err); } func(notBody, function (err, normalized) { if (!err) return done(new Error('expecting error')); done(); }); }); }); test(bodyType + ' => Promise({ ' + bodyVar + ', dependencies })', function () { var expected = { dependencies: [] }; expected[bodyVar] = body; return func(body).then(function (normalized) { assert.deepEqual(normalized, expected); }); }); test('{ ' + bodyVar + ': ' + bodyType + ' } => Promise({ ' + bodyVar + ', dependencies })', function () { var input = {}; input[bodyVar] = body; return func(input).then(function (normalized) { input.dependencies = []; assert.deepEqual(normalized, input); }); }); test('{ ' + bodyVar + ': ' + bodyType + ', dependencies: [] } => Promise({ ' + bodyVar + ', dependencies })', function () { var input = { dependencies: [] }; input[bodyVar] = body; var promise1 = func(input).then(function (normalized) { input.dependencies = []; assert.deepEqual(normalized, input); }); input.dependencies = [ '/tmp/a.txt' ]; var promise2 = func(input).then(function (normalized) { input.dependencies = [ '/tmp/a.txt' ]; assert.deepEqual(normalized, input); }); return Promise.all([promise1, promise2]); }); test('{ ' + bodyVar + ': Promise(' + bodyType + ') } => Promise({ ' + bodyVar + ', dependencies })', function () { var input = {}; input[bodyVar] = Promise.resolve(body); return func(input).then(function (normalized) { input.dependencies = []; input[bodyVar] = body; assert.deepEqual(normalized, input); }); }); test('{ ' + bodyVar + ': Promise(' + bodyType + '), dependencies: [] } => Promise({ ' + bodyVar + ', dependencies })', function () { var input = { dependencies: [] }; input[bodyVar] = Promise.resolve(body); var promise1 = func(input).then(function (normalized) { input.dependencies = []; input[bodyVar] = body; assert.deepEqual(normalized, input); }); input.dependencies = [ '/tmp/a.txt' ]; var promise2 = func(input).then(function (normalized) { input.dependencies = [ '/tmp/a.txt' ]; input[bodyVar] = body; assert.deepEqual(normalized, input); }); return Promise.all([promise1, promise2]); }); test('Promise(' + bodyType + ') => Promise({ ' + bodyVar + ', dependencies })', function () { var expected = { dependencies: [] }; expected[bodyVar] = body; return func(Promise.resolve(body)).then(function (normalized) { assert.deepEqual(normalized, expected); }); }); test('Promise({ ' + bodyVar + ': ' + bodyType + ' }) => Promise({ ' + bodyVar + ', dependencies })', function () { var input = {}; input[bodyVar] = body; return func(Promise.resolve(input)).then(function (normalized) { input.dependencies = []; assert.deepEqual(normalized, input); }); }); test('Promise({ ' + bodyVar + ': ' + bodyType + ', dependencies: [] }) => Promise({ ' + bodyVar + ', dependencies })', function () { var input = { dependencies: [] }; input[bodyVar] = body; var promise1 = func(Promise.resolve(input)).then(function (normalized) { input.dependencies = []; assert.deepEqual(normalized, input); }); input.dependencies = [ '/tmp/a.txt' ]; var promise2 = func(Promise.resolve(input)).then(function (normalized) { input.dependencies = [ '/tmp/a.txt' ]; assert.deepEqual(normalized, input); }); return Promise.all([promise1, promise2]); }); test('Promise({ ' + bodyVar + ': Promise(' + bodyType + '), dependencies: [] }) => Promise({ ' + bodyVar + ', dependencies })', function () { var input = { dependencies: [] }; input[bodyVar] = Promise.resolve(body); var promise1 = func(Promise.resolve(input)).then(function (normalized) { input[bodyVar] = body; input.dependencies = []; assert.deepEqual(normalized, input); }); // Use another promise input[bodyVar] = Promise.resolve(body); input.dependencies = [ '/tmp/a.txt' ]; var promise2 = func(Promise.resolve(input)).then(function (normalized) { input[bodyVar] = body; input.dependencies = [ '/tmp/a.txt' ]; assert.deepEqual(normalized, input); }); return Promise.all([promise1, promise2]); }); test(notBodyType + ' => Promise(throw)', function () { return assertPromiseError(func(notBody), /Invalid result object/); }); test('{ ' + bodyVar + ': ' + notBodyType + ', dependencies: [] } => Promise(throw)', function () { var input = { dependencies: [] }; input[bodyVar] = notBody; return assertPromiseError(func(input), /Invalid result object/); }); test('{ ' + bodyVar + ': ' + bodyType + ', dependencies: Number } => Promise(throw)', function () { var input = { dependencies: 1 }; input[bodyVar] = body; return assertPromiseError(func(input), /dependencies .* array/); }); test('Promise(' + notBodyType + ') => Promise(throw)', function () { return assertPromiseError(func(Promise.resolve(notBody)), /Invalid result object/); }); test('Promise({ ' + bodyVar + ': ' + notBodyType + ', dependencies: [] }) => Promise(throw)', function () { var input = { dependencies: [] }; input[bodyVar] = notBody; return assertPromiseError(func(Promise.resolve(input)), /Invalid result object/); }); test('Promise({ ' + bodyVar + ': ' + bodyType + ', dependencies: Number }) => Promise(throw)', function () { var input = { dependencies: 1 }; input[bodyVar] = body; return assertPromiseError(func(Promise.resolve(input)), /dependencies .* array/); }); test('{ ' + bodyVar + ': Promise(' + notBodyType + '), dependencies: [] } => Promise(throw)', function () { var input = { dependencies: [] }; input[bodyVar] = Promise.resolve(notBody); return assertPromiseError(func(input), /Invalid result object/); }); test('{ ' + bodyVar + ': Promise(' + bodyType + '), dependencies: Number } => Promise(throw)', function () { var input = { dependencies: 1 }; input[bodyVar] = Promise.resolve(body); return assertPromiseError(func(input), /dependencies .* array/); }); test('Promise({ ' + bodyVar + ': Promise(' + notBodyType + '), dependencies: [] }) => Promise(throw)', function () { var input = { dependencies: [] }; input[bodyVar] = Promise.resolve(notBody); return assertPromiseError(func(Promise.resolve(input)), /Invalid result object/); }); test('Promise({ ' + bodyVar + ': Promise(' + bodyType + '), dependencies: Number }) => Promise(throw)', function () { var input = { dependencies: 1 }; input[bodyVar] = Promise.resolve(body); return assertPromiseError(func(Promise.resolve(input)), /dependencies .* array/); }); }); } testAsync('normalizeAsync', 'body', { good: [ 'String', '' ], bad: [ 'Function', function () {} ] }); testAsync('normalizeFnAsync', 'fn', { good: [ 'Function', function () {} ], bad: [ 'String', '' ] }); function assertPromiseError (promise, regex) { return promise.then(function () { throw new Error('expected error'); }, function (err) { if (!regex || regex.test(err.message)) return; throw new Error('not the expected error message: ' + err.message); }); } jstransformer-1.0.0/test/normalize.js000066400000000000000000000044621272514654700177470ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var test = require('./test'); var tr = require('../'); function testSync (funcName, bodyVar, specs) { var func = tr[funcName]; var bodyType = specs.good[0], body = specs.good[1], notBodyType = specs.bad[0], notBody = specs.bad[1]; test(funcName, function () { test(bodyType + ' => { ' + bodyVar + ', dependencies }', function () { var normalized = func(body); var expected = { dependencies: [] }; expected[bodyVar] = body; assert.deepEqual(normalized, expected); }); test('{ ' + bodyVar + ': ' + bodyType + ' } => { ' + bodyVar + ', dependencies }', function () { var input = {}; input[bodyVar] = body; var normalized = func(input); input.dependencies = []; assert.deepEqual(normalized, input); }); test('{ ' + bodyVar + ': ' + bodyType + ', dependencies: [] } => { ' + bodyVar + ', dependencies }', function () { var input = { dependencies: [] }; input[bodyVar] = body; var normalized = func(input); assert.deepEqual(normalized, input); input.dependencies = [ '/tmp/a.txt' ]; normalized = func(input); assert.deepEqual(normalized, input); }); test(notBodyType + ' => throw', function () { assert.throws(function () { func(notBody); }, /Invalid result object/); }); test('{ blah: ' + bodyType + ' } => throw', function () { var input = { blah: body }; assert.throws(function () { func(input); }, /Invalid result object/); }); test('{ ' + bodyVar + ': ' + notBodyType + ', dependencies: [] } => throw', function () { var input = { dependencies: [] }; input[bodyVar] = notBody; assert.throws(function () { func(input); }, /Invalid result object/); }); test('{ ' + bodyVar + ': ' + bodyType + ', dependencies: Number } => throw', function () { var input = { dependencies: 1 }; input[bodyVar] = body; assert.throws(function () { func(input); }, /dependencies .* array/); }); }); } testSync('normalize', 'body', { good: [ 'String', '' ], bad: [ 'Function', function () {} ] }); testSync('normalizeFn', 'fn', { good: [ 'Function', function () {} ], bad: [ 'String', '' ] }); jstransformer-1.0.0/test/output-format.js000066400000000000000000000012101272514654700205610ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var test = require('./test'); var createTransformer = require('../'); test('outputFormat', function () { test('preserves tr.outputFormat', function () { var tr = createTransformer({ name: 'test', outputFormat: 'css', render: function (str, options) { return str; } }); assert.equal(tr.outputFormat, 'css'); }); test('throws without tr.outputFormat', function () { assert.throws(function () { createTransformer({ name: 'test', render: function (str, options) { return str; } }); }, /Transformer must have an output format/); }); }); jstransformer-1.0.0/test/render-async.js000066400000000000000000000112021272514654700203270ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var Promise = require('promise'); var test = require('./test'); var createTransformer = require('../'); test('renderAsync', function () { test('with tr.renderAsync(src, options) => str', function (override) { var sentinel = {}; var localSentinel = {}; var fnSentinel = {}; var cbSentinel = function () {}; var normalizedSentinel = {}; override('normalizeAsync', function (body, cb) { assert(body === fnSentinel); assert(cb === cbSentinel); return normalizedSentinel; }); var tr = createTransformer({ name: 'test', outputFormat: 'html', renderAsync: function (str, options) { assert(str === 'example input'); assert(options === sentinel); return fnSentinel; } }); assert(tr.renderAsync('example input', sentinel, cbSentinel) === normalizedSentinel); }); test('with tr.render(src, options) => str', function (override) { var sentinel = {}; var localSentinel = {}; var fnSentinel = {}; var cbSentinel = function () {}; var normalizedSentinel = {}; override('normalizeAsync', function (body, cb) { assert(body === fnSentinel); assert(cb === cbSentinel); return normalizedSentinel; }); var tr = createTransformer({ name: 'test', outputFormat: 'html', render: function (str, options) { assert(str === 'example input'); assert(options === sentinel); return fnSentinel; } }); assert(tr.renderAsync('example input', sentinel, cbSentinel) === normalizedSentinel); }); test('with tr.compileAsync(src, options) => fn', function (override) { var sentinel = {}; var fnSentinel = {}; var normalizedSentinel = {}; var cbSentinel = function () {}; override('normalizeAsync', function (result) { assert.deepEqual(result, { body: '
', dependencies: ['example.js'] }); return normalizedSentinel; }); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileAsync: function () { throw new Error('Did not expect this to be called'); } }); tr.compileAsync = function (str, options) { assert(str === 'example input'); assert(options === sentinel); return {then: function (fn) { return fn({fn: function (locals) { assert(locals === sentinel); return '
'; }, dependencies: ['example.js']}); }}; }; assert(tr.renderAsync('example input', sentinel, cbSentinel) === normalizedSentinel); }); test('with tr.compile(src, options) => fn', function (override) { var sentinel = {}; var fnSentinel = {}; var normalizedSentinel = {}; var cbSentinel = function () {}; override('normalizeAsync', function (result) { assert.deepEqual(result, { body: '
', dependencies: ['example.js'] }); return normalizedSentinel; }); var tr = createTransformer({ name: 'test', outputFormat: 'html', compile: function () { throw new Error('Did not expect this to be called'); } }); tr.compileAsync = function (str, options) { assert(str === 'example input'); assert(options === sentinel); return {then: function (fn) { return fn({fn: function (locals) { assert(locals === sentinel); return '
'; }, dependencies: ['example.js']}); }}; }; assert(tr.renderAsync('example input', sentinel, cbSentinel) === normalizedSentinel); }); test('renderAsync(src, options, locals) - with tr.compile(src, options) => fn', function (override) { var nameSentinel = 'jstransformer'; override('normalizeAsync', function (result) { return result; }); var tr = createTransformer({ name: 'test', outputFormat: 'html', compile: function (str, options) { return function (locals) { return String(locals.name); }; } }); tr.renderAsync('example input', { blah: true }, { name: nameSentinel }) .then(function (res) { assert.equal( res.body, nameSentinel) }) }); test('without any of the above', function () { var tr = createTransformer({ name: 'test', outputFormat: 'html', renderFileAsync : function () { } }); var a = tr.renderAsync('example input', {}).then(function () { throw new Error('expected to have an error'); }, function (err) { if (!/does not support rendering from a string/.test(err.message)) { throw err; } }); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFileClient: function () { } }); var b = tr.renderAsync('example input', {}).then(function () { throw new Error('expected to have an error'); }, function (err) { if (!/does not support rendering/.test(err.message)) { throw err; } }); return Promise.all([a, b]); }); }); jstransformer-1.0.0/test/render-file-async.js000066400000000000000000000134551272514654700212600ustar00rootroot00000000000000'use strict'; var Promise = require('promise'); var assert = require('assert'); var test = require('./test'); var createTransformer = require('../'); test('renderFileAsync', function () { test('with tr.renderFileAsync(file, options, locals) => Promise(str)', function (override) { var sentinel = {}; var localSentinel = {}; var tr = createTransformer({ name: 'test', outputFormat: 'html', renderFileAsync: function (file, options, locals) { assert(file === 'example-input.txt'); assert(options === sentinel); assert(locals === localSentinel); return Promise.resolve('example output'); } }); return tr.renderFileAsync('example-input.txt', sentinel, localSentinel).then(function (rendered) { assert(rendered.body === 'example output'); }); }); test('with tr.renderFile(file, options, locals) => str', function (override) { var sentinel = {}; var localSentinel = {}; var tr = createTransformer({ name: 'test', outputFormat: 'html', renderFile: function (file, options, locals) { assert(file === 'example-input.txt'); assert(options === sentinel); assert(locals === localSentinel); return 'example output'; } }); return tr.renderFileAsync('example-input.txt', sentinel, localSentinel).then(function (rendered) { assert(rendered.body === 'example output'); }); }); test('with tr.renderAsync(src, options, locals) => Promise(str)', function (override) { var sentinel = {}; var localSentinel = {}; override('readFile', function (filename) { assert(filename === 'example-input.txt'); return Promise.resolve('example input'); }); var tr = createTransformer({ name: 'test', outputFormat: 'html', renderAsync: function (str, options, locals) { assert(str === 'example input'); assert(options === sentinel); assert(locals === localSentinel); return Promise.resolve('example output'); } }); return tr.renderFileAsync('example-input.txt', sentinel, localSentinel).then(function (rendered) { assert(rendered.body === 'example output'); }); }); test('with tr.render(src, options, locals) => str', function (override) { var sentinel = {}; var localSentinel = {}; override('readFile', function (filename) { assert(filename === 'example-input.txt'); return Promise.resolve('example input'); }); var tr = createTransformer({ name: 'test', outputFormat: 'html', render: function (str, options, locals) { assert(str === 'example input'); assert(options === sentinel); assert(locals === localSentinel); return 'example output'; } }); return tr.renderFileAsync('example-input.txt', sentinel, localSentinel).then(function (rendered) { assert(rendered.body === 'example output'); }); }); test('with tr.compileFileAsync(file, options) => Promise(fn => Promise(str))', function (override) { var sentinel = {}; var localSentinel = {}; var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFileAsync: function (file, options) { assert(file === 'example-input.txt'); assert(options === sentinel); return Promise.resolve(function (locals) { assert(locals === localSentinel); return Promise.resolve('example output'); }); } }); return tr.renderFileAsync('example-input.txt', sentinel, localSentinel).then(function (rendered) { assert(rendered.body === 'example output'); }); }); test('with tr.compileFile(file, options) => fn', function (override) { var sentinel = {}; var localSentinel = {}; var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFile: function (file, options) { assert(file === 'example-input.txt'); assert(options === sentinel); return function (locals) { assert(locals === localSentinel); return 'example output'; }; } }); return tr.renderFileAsync('example-input.txt', sentinel, localSentinel).then(function (rendered) { assert(rendered.body === 'example output'); }); }); test('with tr.compileAsync(str, options) => Promise(fn => Promise(str))', function (override) { var sentinel = {}; var localSentinel = {}; override('readFile', function (filename) { assert(filename === 'example-input.txt'); return Promise.resolve('example input'); }); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileAsync: function (str, options) { assert(str === 'example input'); assert(options === sentinel); return Promise.resolve(function (locals) { assert(locals === localSentinel); return Promise.resolve('example output'); }); } }); return tr.renderFileAsync('example-input.txt', sentinel, localSentinel).then(function (rendered) { assert(rendered.body === 'example output'); }); }); test('with tr.compile(str, options) => fn', function (override) { var sentinel = {}; var localSentinel = {}; override('readFile', function (filename) { assert(filename === 'example-input.txt'); return Promise.resolve('example input'); }); var tr = createTransformer({ name: 'test', outputFormat: 'html', compile: function (str, options) { assert(str === 'example input'); assert(options === sentinel); return function (locals) { assert(locals === localSentinel); return 'example output'; }; } }); return tr.renderFileAsync('example-input.txt', sentinel, localSentinel).then(function (rendered) { assert(rendered.body === 'example output'); }); }); test('without any of the above', function (override) { var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFileClientAsync: function () { } }); assert.throws(function () { tr.renderFileAsync('example input', {}, {}); }, /does not support rendering/); }); }); jstransformer-1.0.0/test/render-file.js000066400000000000000000000061571272514654700201460ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var test = require('./test'); var createTransformer = require('../'); test('renderFile', function () { test('with tr.renderFile(file, options, locals) => str', function (override) { var sentinel = {}; var localSentinel = {}; override('readFileSync', function (filename) { assert(filename === 'example-input.txt'); return 'example input'; }); var tr = createTransformer({ name: 'test', outputFormat: 'html', renderFile: function (file, options, locals) { assert(file === 'example-input.txt'); assert(options === sentinel); assert(locals === localSentinel); return 'example output'; } }); assert(tr.renderFile('example-input.txt', sentinel, localSentinel).body === 'example output'); }); test('with tr.render(src, options, locals) => str', function (override) { var sentinel = {}; var localSentinel = {}; override('readFileSync', function (filename) { assert(filename === 'example-input.txt'); return 'example input'; }); var tr = createTransformer({ name: 'test', outputFormat: 'html', render: function (str, options, locals) { assert(str === 'example input'); assert(options === sentinel); assert(locals === localSentinel); return 'example output'; } }); assert(tr.renderFile('example-input.txt', sentinel, localSentinel).body === 'example output'); }); test('with tr.compileFile(file, options) => fn', function (override) { var sentinel = {}; var localSentinel = {}; var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFile: function (file, options) { assert(file === 'example-input.txt'); assert(options === sentinel); return function (locals) { assert(locals === localSentinel); return 'example output'; }; } }); assert(tr.renderFile('example-input.txt', sentinel, localSentinel).body === 'example output'); }); test('with tr.compile(str, options) => fn', function (override) { var sentinel = {}; var localSentinel = {}; override('readFileSync', function (filename) { assert(filename === 'example-input.txt'); return 'example input'; }); var tr = createTransformer({ name: 'test', outputFormat: 'html', compile: function (str, options) { assert(str === 'example input'); assert(options === sentinel); return function (locals) { assert(locals === localSentinel); return 'example output'; }; } }); assert(tr.renderFile('example-input.txt', sentinel, localSentinel).body === 'example output'); }); test('without any of the above', function (override) { var tr = createTransformer({ name: 'test', outputFormat: 'html', compileAsync: function () { } }); assert.throws(function () { tr.renderFile('example input', {}, {}); }, /does not support rendering synchronously/); tr = createTransformer({ name: 'test', outputFormat: 'html', compileClient: function () { } }); assert.throws(function () { tr.renderFile('example input', {}, {}); }, /does not support rendering synchronously/); }); }); jstransformer-1.0.0/test/render.js000066400000000000000000000062501272514654700172230ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var Promise = require('promise'); var test = require('./test'); var createTransformer = require('../'); test('render', function () { test('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('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 '
'; }, dependencies: ['example.js'] }; }); override('normalize', function (result) { assert.deepEqual(result, { body: '
', 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(src, options, locals) - with tr.compile(src, options) => fn', function (override) { var nameSentinel = 'jstransformer'; var tr = createTransformer({ name: 'test', outputFormat: 'html', compile: function (str, options) { return function (locals) { return String(locals.name); }; } }); assert.equal( tr.render( 'example input' , { blah: true } , { name: nameSentinel }).body , nameSentinel); }); test('without any of the above', function () { var tr = createTransformer({ name: 'test', outputFormat: 'html', compileAsync: function () { } }); assert.throws(function () { tr.render('example input', {}); }, /does not support rendering synchronously/); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileFileAsync: function () { } }); assert.throws(function () { tr.render('example input', {}); }, /does not support rendering from a string/); var tr = createTransformer({ name: 'test', outputFormat: 'html', compileClient: function () { } }); assert.throws(function () { tr.render('example input', {}); }, /does not support rendering/); assert.throws(function () { tr.render('example input', {}); }, /does not support rendering/); var tr = createTransformer({ name: 'test', outputFormat: 'html', compile: function () { return function () { return Promise.resolve('foo'); }; } }); assert.throws(function () { tr.render('example input', {}); }, /does not support rendering synchronously/); }); }); jstransformer-1.0.0/test/test.js000066400000000000000000000023011272514654700167140ustar00rootroot00000000000000'use strict'; var testit = require('testit'); var tr = require('../'); module.exports = test; function test(name, fn) { function testBody (done) { var originals = {}, result; try { if (done) { result = fn(function (name, overrider) { originals[name] = tr[name]; tr[name] = overrider; }, function (err) { restore(); return done(err); }); } else { result = fn(function (name, overrider) { originals[name] = tr[name]; tr[name] = overrider; }); } } catch (ex) { restore(); throw ex; } restore(); if (Object.keys(originals).length === 0) { return result; } else if (result) { return result.then(function () { restore(); }, function (err) { restore(); throw err; }); } else { restore(); } function restore () { Object.keys(originals).forEach(function (key) { tr[key] = originals[key]; }); } } if (fn.length < 2) { testit(name, function () { return testBody(); }); } else { testit(name, function (done) { return testBody(done); }); } }