diff --git a/bower.json b/bower.json index 288b4428..6a285371 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "doctest", - "version": "0.7.1", + "version": "0.8.0", "main": "./lib/doctest", "ignore": [ "/Makefile", diff --git a/lib/command.js b/lib/command.js index 89a93e2f..8ebd60cd 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1,34 +1,34 @@ // Generated by CoffeeScript 1.8.0 (function() { - var doctest, keys, options, program, validators, _; + var R, doctest, failures, keys, options, program, validators; program = require('commander'); - _ = require('underscore'); + R = require('ramda'); doctest = require('../lib/doctest'); - program.version(doctest.version).usage('[options] path/to/js/or/coffee/module').option('-m, --module ', 'specify module system ("amd" or "commonjs")').option('-p, --print', 'output the rewritten source without running tests').option('-s, --silent', 'suppress output').option('-t, --type ', 'specify file type ("coffee" or "js")').parse(process.argv); + program.version(doctest.version).usage('[options] path/to/js/or/coffee/module').option('-m, --module ', 'specify module system ("amd" or "commonjs")').option(' --nodejs', 'pass options directly to the "node" binary').option('-p, --print', 'output the rewritten source without running tests').option('-s, --silent', 'suppress output').option('-t, --type ', 'specify file type ("coffee" or "js")').parse(process.argv); validators = { - module: _.partial(_.contains, [void 0, 'amd', 'commonjs']), - print: _.constant(true), - silent: _.constant(true), - type: _.partial(_.contains, [void 0, 'coffee', 'js']) + module: R.contains(R.__, [void 0, 'amd', 'commonjs']), + print: R.always(true), + silent: R.always(true), + type: R.contains(R.__, [void 0, 'coffee', 'js']) }; - keys = _.keys(validators).sort(); + keys = R.keys(validators).sort(); - options = _.pick(program, keys); + options = R.pick(keys, program); - _.each(keys, function(key) { + keys.forEach(function(key) { if (!validators[key](options[key])) { process.stderr.write("\n error: invalid " + key + " `" + options[key] + "'\n\n"); return process.exit(1); } }); - process.exit(_.reduce(program.args, function(failures, path) { + failures = R.reduce(function(failures, path) { var err, results; try { results = doctest(path, options); @@ -37,7 +37,9 @@ process.stderr.write("\n error: " + (err.message[0].toLowerCase()) + err.message.slice(1) + "\n\n"); process.exit(1); } - return failures + _.reject(_.map(results, _.first), _.identity).length; - }, 0)); + return failures + R.length(R.reject(R.identity, R.map(R.head, results))); + }, 0, program.args); + + process.exit(failures === 0 ? 0 : 1); }).call(this); diff --git a/lib/doctest.js b/lib/doctest.js index 2a866269..6d1bc53b 100644 --- a/lib/doctest.js +++ b/lib/doctest.js @@ -12,7 +12,7 @@ */ (function() { - var CoffeeScript, R, commonjsEval, doctest, esprima, fetch, fs, functionEval, log, noop, pathlib, reduce, repr, rewrite, run, substring, toModule, toPairs, transformComments, wrap, _; + var CoffeeScript, R, commonjsEval, doctest, esprima, fetch, fs, functionEval, indent2, indentN, log, noop, pathlib, reduce, repr, rewrite, run, substring, toModule, toPairs, transformComments, wrap; doctest = function(path, options, callback) { var type, validateOption, _ref; @@ -57,21 +57,24 @@ }); }; - doctest.version = '0.7.1'; + doctest.version = '0.8.0'; if (typeof window !== 'undefined') { - _ = window._, CoffeeScript = window.CoffeeScript, esprima = window.esprima, R = window.R; + CoffeeScript = window.CoffeeScript, esprima = window.esprima, R = window.R; window.doctest = doctest; } else { fs = require('fs'); pathlib = require('path'); - _ = require('underscore'); CoffeeScript = require('coffee-script'); - esprima = require('esprima'); + esprima = require('esprima-fb'); R = require('ramda'); module.exports = doctest; } + indentN = R.pipe(R.times(R.always(' ')), R.join(''), R.replace(/^(?!$)/gm)); + + indent2 = indentN(2); + noop = function() {}; reduce = R.flip(R.reduce); @@ -107,11 +110,15 @@ }; toModule = function(source, moduleType) { + var iifeWrap; switch (moduleType) { case 'amd': - return "" + source + "\nfunction define() {\n for (var idx = 0; idx < arguments.length; idx += 1) {\n if (typeof arguments[idx] == 'function') {\n arguments[idx]();\n break;\n }\n }\n}"; + return "" + source + "\nfunction define() {\n for (var idx = 0; idx < arguments.length; idx += 1) {\n if (typeof arguments[idx] == 'function') {\n arguments[idx]();\n break;\n }\n }\n}\n"; case 'commonjs': - return "var __doctest = {\n queue: [],\n input: function(fn) {\n __doctest.queue.push([fn]);\n },\n output: function(num, fn) {\n __doctest.queue.push([fn, num]);\n }\n};\n\n" + source + "\n(module.exports || exports).__doctest = __doctest;"; + iifeWrap = function(s) { + return "void function() {\n" + (indent2(s)) + "}.call(this);"; + }; + return iifeWrap("var __doctest = {\n queue: [],\n input: function(fn) {\n __doctest.queue.push([fn]);\n },\n output: function(num, fn) {\n __doctest.queue.push([fn, num]);\n }\n};\n\n" + (iifeWrap(source)) + "\n\n(module.exports || exports).__doctest = __doctest;\n"); default: return source; } @@ -155,7 +162,7 @@ } else if (state === 'input') { if (prefix) { return [ - 'input', R.appendTo(R.slice(0, -1, accum), { + 'input', R.appendTo(R.init(accum), { commentIndex: commentIndex, input: { loc: { @@ -168,7 +175,7 @@ ]; } else { return [ - 'output', R.appendTo(R.slice(0, -1, accum), { + 'output', R.appendTo(R.init(accum), { commentIndex: commentIndex, input: R.last(accum).input, output: { @@ -184,7 +191,7 @@ } else if (state === 'output') { if (prefix) { return [ - 'output', R.appendTo(R.slice(0, -1, accum), { + 'output', R.appendTo(R.init(accum), { commentIndex: commentIndex, input: R.last(accum).input, output: { @@ -226,7 +233,7 @@ }; wrap = R.curry(function(type, test) { - return R.pipe(R.filter(R.rPartial(R.has, test)), R.map(function(dir) { + return R.pipe(R.filter(R.partialRight(R.has, test)), R.map(function(dir) { return wrap[type][dir](test); }), R.join('\n'))(['input', 'output']); }); @@ -244,11 +251,11 @@ wrap.coffee = wrap('coffee'); wrap.coffee.input = function(test) { - return "__doctest.input ->\n" + (R.replace(/^/gm, ' ', test.input.value)); + return "__doctest.input ->\n" + (indent2(test.input.value)); }; wrap.coffee.output = function(test) { - return "__doctest.output " + test.output.loc.start.line + ", ->\n" + (R.replace(/^/gm, ' ', test.output.value)); + return "__doctest.output " + test.output.loc.start.line + ", ->\n" + (indent2(test.output.value)); }; rewrite.js = function(input) { @@ -262,7 +269,7 @@ } } }; - getComments = R.pipe(R.rPartial(esprima.parse, { + getComments = R.pipe(R.partialRight(esprima.parse, { comment: true, loc: true }), R.prop('comments')); @@ -278,7 +285,7 @@ var chunks, start, _ref1; chunks = _arg[0], start = _arg[1]; return [R.appendTo(chunks, substring(input, start, test.input.loc.start)), ((_ref1 = test.output) != null ? _ref1 : test.input).loc.end]; - }), R.head, R.rPartial(R.zip, R.append('', R.map(wrap.js, lineTests))), R.flatten, R.join(''))(lineTests); + }), R.head, R.partialRight(R.zip, R.append('', R.map(wrap.js, lineTests))), R.flatten, R.join(''))(lineTests); return R.pipe(getComments, R.filter(R.propEq('type', 'Block')), R.append(bookend), toPairs, reduce([ [], { line: 1, @@ -358,7 +365,7 @@ return ['default', tests]; } }))(commentChunk); - }, R.last, R.map(R.converge(R.replace(/^/gm), R.prop('indent'), wrap.coffee)), R.join('\n')), commentChunks); + }, R.last, R.map(R.converge(R.apply, R.pipe(R.prop('indent'), R.length, indentN), R.pipe(wrap.coffee, R.of))), R.join('\n')), commentChunks); return R.pipe(R.zip, R.flatten, R.join('\n'), CoffeeScript.compile)(R.pluck('value', literalChunks), R.append('', testChunks)); }; @@ -379,7 +386,7 @@ commonjsEval = function(source, path) { var abspath, queue; - abspath = pathlib.resolve(path).replace(/[.][^.]+$/, "-" + (_.now()) + ".js"); + abspath = pathlib.resolve(path).replace(/[.][^.]+$/, "-" + (Date.now()) + ".js"); fs.writeFileSync(abspath, source); try { queue = require(abspath).__doctest.queue; @@ -410,7 +417,7 @@ } })(); expected = arr[0](); - results.push([_.isEqual(actual, expected), repr(expected), repr(actual), arr[1]]); + results.push([R.eqDeep(actual, expected), repr(expected), repr(actual), arr[1]]); input = noop; } } diff --git a/package.json b/package.json index 8fd506b4..e0e8a6ed 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "doctest", - "version": "0.7.1", + "version": "0.8.0", "description": "Doctests for JavaScript and CoffeeScript", "contributors": [ "David Chambers ", diff --git a/src/doctest.coffee b/src/doctest.coffee index a7294087..53a53046 100644 --- a/src/doctest.coffee +++ b/src/doctest.coffee @@ -40,7 +40,7 @@ doctest = (path, options = {}, callback = noop) -> callback results results -doctest.version = '0.7.1' +doctest.version = '0.8.0' if typeof window isnt 'undefined'