From f0e2cf4a6bddd20c608e335ab49b299f076ab666 Mon Sep 17 00:00:00 2001 From: David Chambers Date: Wed, 19 Nov 2014 20:48:19 -0800 Subject: [PATCH] 0.7.1 --- bower.json | 2 +- lib/command.js | 2 +- lib/doctest.js | 148 ++++++++++++++++++++++----------------------- package.json | 2 +- src/doctest.coffee | 2 +- 5 files changed, 78 insertions(+), 78 deletions(-) diff --git a/bower.json b/bower.json index 52548c77..923dff90 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "doctest", - "version": "0.7.0", + "version": "0.7.1", "main": "./lib/doctest", "ignore": [ "/Makefile", diff --git a/lib/command.js b/lib/command.js index f5272a70..89a93e2f 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.7.1 +// Generated by CoffeeScript 1.8.0 (function() { var doctest, keys, options, program, validators, _; diff --git a/lib/doctest.js b/lib/doctest.js index d9508b9d..2a866269 100644 --- a/lib/doctest.js +++ b/lib/doctest.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.7.1 +// Generated by CoffeeScript 1.8.0 /* >>> @@ -12,8 +12,7 @@ */ (function() { - var CoffeeScript, R, commonjsEval, doctest, esprima, fetch, fs, functionEval, log, noop, pathlib, repr, rewrite, run, substring, toModule, transformComments, wrap, _, - __slice = [].slice; + var CoffeeScript, R, commonjsEval, doctest, esprima, fetch, fs, functionEval, log, noop, pathlib, reduce, repr, rewrite, run, substring, toModule, toPairs, transformComments, wrap, _; doctest = function(path, options, callback) { var type, validateOption, _ref; @@ -32,7 +31,7 @@ validateOption('type', ['coffee', 'js']); type = (_ref = options.type) != null ? _ref : (function() { var match; - match = /[.](coffee|js)$/.exec(path); + match = R.match(/[.](coffee|js)$/, path); if (match === null) { throw new Error('Cannot infer type from extension'); } @@ -43,7 +42,7 @@ source = toModule(rewrite(text, type), options.module); if (options.print) { if (!options.silent) { - console.log(source.replace(/\n$/, '')); + console.log(R.replace(/\n$/, '', source)); } callback(source); return source; @@ -58,10 +57,10 @@ }); }; - doctest.version = '0.7.0'; + doctest.version = '0.7.1'; if (typeof window !== 'undefined') { - _ = window._, CoffeeScript = window.CoffeeScript, esprima = window.esprima, R = window.ramda; + _ = window._, CoffeeScript = window.CoffeeScript, esprima = window.esprima, R = window.R; window.doctest = doctest; } else { fs = require('fs'); @@ -73,6 +72,12 @@ module.exports = doctest; } + noop = function() {}; + + reduce = R.flip(R.reduce); + + toPairs = R.converge(R.zip, R.pipe(R.length, R.range(0)), R.identity); + fetch = function(path, options, callback) { var silent, wrapper; silent = options.silent || options.print; @@ -112,27 +117,29 @@ } }; - transformComments = R.pipe(R.reduce.idx(function(_arg, comment, commentIndex) { - var accum, state; + transformComments = R.pipe(toPairs, reduce(['default', []], function(_arg, _arg1) { + var accum, comment, commentIndex, state; state = _arg[0], accum = _arg[1]; - return R.reduce.idx(function(_arg1, line, idx) { - var accum, end, normalizedLine, prefix, start, state, value, _i, _ref, _ref1; - state = _arg1[0], accum = _arg1[1]; + commentIndex = _arg1[0], comment = _arg1[1]; + return R.pipe(R.prop('value'), R.split('\n'), toPairs, reduce([state, accum], function(_arg2, _arg3) { + var accum, end, idx, line, normalizedLine, prefix, start, state, value, _ref, _ref1; + state = _arg2[0], accum = _arg2[1]; + idx = _arg3[0], line = _arg3[1]; switch (comment.type) { case 'Block': - normalizedLine = line.replace(/^\s*[*]?\s*/, ''); + normalizedLine = R.replace(/^\s*[*]?\s*/, '', line); start = end = { line: comment.loc.start.line + idx }; break; case 'Line': - normalizedLine = line.replace(/^\s*/, ''); + normalizedLine = R.replace(/^\s*/, '', line); _ref = comment.loc, start = _ref.start, end = _ref.end; } - _ref1 = /^(>|[.]*)[ ]?(.*)$/.exec(normalizedLine), _i = _ref1.length - 2, prefix = _ref1[_i++], value = _ref1[_i++]; + _ref1 = R.tail(R.match(/^(>|[.]*)[ ]?(.*)$/, normalizedLine)), prefix = _ref1[0], value = _ref1[1]; if (prefix === '>') { return [ - 'input', accum.concat({ + 'input', R.appendTo(accum, { commentIndex: commentIndex, input: { loc: { @@ -148,7 +155,7 @@ } else if (state === 'input') { if (prefix) { return [ - 'input', R.slice(0, -1, accum).concat({ + 'input', R.appendTo(R.slice(0, -1, accum), { commentIndex: commentIndex, input: { loc: { @@ -161,7 +168,7 @@ ]; } else { return [ - 'output', R.slice(0, -1, accum).concat({ + 'output', R.appendTo(R.slice(0, -1, accum), { commentIndex: commentIndex, input: R.last(accum).input, output: { @@ -177,7 +184,7 @@ } else if (state === 'output') { if (prefix) { return [ - 'output', R.slice(0, -1, accum).concat({ + 'output', R.appendTo(R.slice(0, -1, accum), { commentIndex: commentIndex, input: R.last(accum).input, output: { @@ -193,42 +200,38 @@ return ['default', accum]; } } - }, [state, accum], comment.value.split('\n')); - }, ['default', []]), R.last); + }))(comment); + }), R.last); substring = function(input, start, end) { - var combine; if (start.line === end.line && start.column === end.column) { return ''; } - combine = function(a, b) { - return ["" + a[0] + b[0], b[1]]; - }; - return R.pipe(R.split(/^/m), R.reduce.idx(function(accum, line, idx) { - var isEndLine, isStartLine; + return R.pipe(R.split(/^/m), toPairs, reduce(['', false], function(accum, _arg) { + var idx, isEndLine, isStartLine, line; + idx = _arg[0], line = _arg[1]; isStartLine = idx + 1 === start.line; isEndLine = idx + 1 === end.line; - return combine(accum, R.reduce.idx(function(_arg, chr, column) { - var chrs, inComment; - chrs = _arg[0], inComment = _arg[1]; + return R.pipe(R.split(''), toPairs, reduce(['', R.last(accum)], function(_arg1, _arg2) { + var chr, chrs, column, inComment; + chrs = _arg1[0], inComment = _arg1[1]; + column = _arg2[0], chr = _arg2[1]; if ((isStartLine && column === start.column) || inComment && !(isEndLine && column === end.column)) { - return ["" + chrs + chr, true]; + return [R.concat(chrs, chr), true]; } else { - return ["" + chrs, false]; + return [chrs, false]; } - }, ['', R.last(accum)], line)); - }, ['', false]), R.first)(input); + }), R.converge(R.prepend, R.pipe(R.head, R.concat(R.head(accum))), R.tail))(line); + }), R.head)(input); }; - wrap = function(type, test) { - return R.pipe(R.filter(function(dir) { - return Object.prototype.hasOwnProperty.call(test, dir); - }), R.map(function(dir) { + wrap = R.curry(function(type, test) { + return R.pipe(R.filter(R.rPartial(R.has, test)), R.map(function(dir) { return wrap[type][dir](test); }), R.join('\n'))(['input', 'output']); - }; + }); - wrap.js = R.lPartial(wrap, 'js'); + wrap.js = wrap('js'); wrap.js.input = function(test) { return "__doctest.input(function() {\n return " + test.input.value + ";\n});"; @@ -238,14 +241,14 @@ return "__doctest.output(" + test.output.loc.start.line + ", function() {\n return " + test.output.value + ";\n});"; }; - wrap.coffee = R.lPartial(wrap, 'coffee'); + wrap.coffee = wrap('coffee'); wrap.coffee.input = function(test) { - return "__doctest.input ->\n" + (test.input.value.replace(/^/gm, ' ')); + return "__doctest.input ->\n" + (R.replace(/^/gm, ' ', test.input.value)); }; wrap.coffee.output = function(test) { - return "__doctest.output " + test.output.loc.start.line + ", ->\n" + (test.output.value.replace(/^/gm, ' ')); + return "__doctest.output " + test.output.loc.start.line + ", ->\n" + (R.replace(/^/gm, ' ', test.output.value)); }; rewrite.js = function(input) { @@ -263,39 +266,38 @@ comment: true, loc: true }), R.prop('comments')); - _ref = R.pipe(getComments, R.partition(R.pipe(R.prop('type'), R.eq('Block'))), R.map(transformComments))(input), blockTests = _ref[0], lineTests = _ref[1]; - source = R.pipe(R.concat, R.reduce(function(_arg, test) { - var chunks, start, _ref1; - chunks = _arg[0], start = _arg[1]; - return [__slice.call(chunks).concat([substring(input, start, test.input.loc.start)]), ((_ref1 = test.output) != null ? _ref1 : test.input).loc.end]; - }, [ + _ref = R.pipe(getComments, R.partition(R.propEq('type', 'Block')), R.map(transformComments))(input), blockTests = _ref[0], lineTests = _ref[1]; + source = R.pipe(R.append({ + input: bookend + }), reduce([ [], { line: 1, column: 0 } - ]), R.first, R.rPartial(R.zip, R.concat(R.map(wrap.js, lineTests), [''])), R.flatten, R.join(''))(lineTests, [ - { - input: bookend - } - ]); - return R.pipe(getComments, R.filter(R.pipe(R.prop('type'), R.eq('Block'))), R.rPartial(R.concat, [bookend]), R.reduce.idx(function(_arg, comment, idx) { - var chunks, s, start; + ], function(_arg, test) { + var chunks, start, _ref1; chunks = _arg[0], start = _arg[1]; - s = R.pipe(R.filter(R.pipe(R.prop('commentIndex'), R.eq(idx))), R.map(wrap.js), R.join('\n'))(blockTests); - return [__slice.call(chunks).concat([substring(source, start, comment.loc.start)], [s]), comment.loc.end]; - }, [ + 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); + return R.pipe(getComments, R.filter(R.propEq('type', 'Block')), R.append(bookend), toPairs, reduce([ [], { line: 1, column: 0 } - ]), R.first, R.join(''))(source); + ], function(_arg, _arg1) { + var chunks, comment, idx, start; + chunks = _arg[0], start = _arg[1]; + idx = _arg1[0], comment = _arg1[1]; + return R.pipe(R.filter(R.propEq('commentIndex', idx)), R.map(wrap.js), R.join('\n'), R.appendTo(R.append(substring(source, start, comment.loc.start), chunks)), R.of, R.append(comment.loc.end))(blockTests); + }), R.head, R.join(''))(source); }; rewrite.coffee = function(input) { - var commentChunks, literalChunks, testChunks, _ref; - _ref = R.pipe(R.match(/.*\n/gm), R.reduce.idx(function(_arg, line, idx) { - var commentChunks, current, inCommentChunk, isComment, literalChunks; + var commentChunks, literalChunks, matchLine, testChunks, _ref; + _ref = R.pipe(R.match(/.*\n/g), toPairs, R.reduce(function(_arg, _arg1) { + var commentChunks, current, idx, inCommentChunk, isComment, line, literalChunks; literalChunks = _arg[0], commentChunks = _arg[1], inCommentChunk = _arg[2]; + idx = _arg1[0], line = _arg1[1]; isComment = /^[ \t]*#(?!##)/.test(line); current = isComment ? commentChunks : literalChunks; if (isComment === inCommentChunk) { @@ -323,11 +325,13 @@ } ], [], false ]))(input), literalChunks = _ref[0], commentChunks = _ref[1]; + matchLine = R.match(/^([ \t]*)#[ \t]*(>|[.]*)(.*\n)/); testChunks = R.map(R.pipe(function(commentChunk) { - return R.reduce.idx(function(_arg, line, idx) { - var indent, loc, prefix, state, tests, value, _i, _ref1; + return R.pipe(R.prop('value'), R.match(/.*\n/g), toPairs, reduce(['default', []], function(_arg, _arg1) { + var idx, indent, line, loc, prefix, state, tests, value, _ref1; state = _arg[0], tests = _arg[1]; - _ref1 = /^([ \t]*)#[ \t]*(>|[.]*)(.*\n)/.exec(line), _i = _ref1.length - 3, indent = _ref1[_i++], prefix = _ref1[_i++], value = _ref1[_i++]; + idx = _arg1[0], line = _arg1[1]; + _ref1 = R.tail(matchLine(line)), indent = _ref1[0], prefix = _ref1[1], value = _ref1[2]; if (prefix === '>') { tests[tests.length] = { indent: indent, @@ -353,11 +357,9 @@ } else { return ['default', tests]; } - }, ['default', []], commentChunk.value.match(/.*\n/gm)); - }, R.last, R.map(function(test) { - return wrap.coffee(test).replace(/^/gm, test.indent); - }), R.join('\n')), commentChunks); - return R.pipe(R.zip, R.flatten, R.join('\n'), CoffeeScript.compile)(R.pluck('value', literalChunks), R.concat(testChunks, [''])); + }))(commentChunk); + }, R.last, R.map(R.converge(R.replace(/^/gm), R.prop('indent'), wrap.coffee)), R.join('\n')), commentChunks); + return R.pipe(R.zip, R.flatten, R.join('\n'), CoffeeScript.compile)(R.pluck('value', literalChunks), R.append('', testChunks)); }; functionEval = function(source) { @@ -378,7 +380,7 @@ commonjsEval = function(source, path) { var abspath, queue; abspath = pathlib.resolve(path).replace(/[.][^.]+$/, "-" + (_.now()) + ".js"); - fs.writeFileSync(abspath, "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" + source + "\n(module.exports || exports).__doctest = __doctest;"); + fs.writeFileSync(abspath, source); try { queue = require(abspath).__doctest.queue; } finally { @@ -434,8 +436,6 @@ } }; - noop = function() {}; - repr = function(val) { switch (Object.prototype.toString.call(val)) { case '[object String]': diff --git a/package.json b/package.json index e452ac36..289c882c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "doctest", - "version": "0.7.0", + "version": "0.7.1", "description": "Doctests for JavaScript and CoffeeScript", "contributors": [ "David Chambers ", diff --git a/src/doctest.coffee b/src/doctest.coffee index 22cb3fdc..874b9a53 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.0' +doctest.version = '0.7.1' if typeof window isnt 'undefined'