diff --git a/README.markdown b/README.markdown index 7fee5015..6d5ec3bb 100644 --- a/README.markdown +++ b/README.markdown @@ -94,10 +94,18 @@ In the first step, the code example above would be rewritten as: ```javascript !function() { - doctest.input(function() { return toUsername("Jesper Nøhr") }) - doctest.output(4, function() { return "jespernhr" }) - doctest.input(function() { return 15 * 15 }) - doctest.output(6, function() { return "225" }) + doctest.input(function() { + return toUsername("Jesper Nøhr") + }); + doctest.output(4, function() { + return "jespernhr" + }); + doctest.input(function() { + return 15 * 15 + }); + doctest.output(6, function() { + return "225" + }); var toUsername = function(text) { return ('' + text).replace(/\W/g, '').toLowerCase() } diff --git a/doctest.coffee b/doctest.coffee index 48a2806c..9ca7ed4f 100644 --- a/doctest.coffee +++ b/doctest.coffee @@ -12,7 +12,7 @@ doctest = (urls...) -> _.each urls, fetch -doctest.version = '0.2.1' +doctest.version = '0.2.2' doctest.queue = [] @@ -58,18 +58,19 @@ fetch = (url) -> rewrite = (text) -> + f = (expr) -> "function() {\n return #{expr}\n}" lines = []; expr = '' for line, idx in text.split /\r?\n|\r/ if match = /^[ \t]*\/\/[ \t]*(.+)/.exec line comment = match[1] if match = /^>(.*)/.exec comment - lines.push "doctest.input(function(){return #{expr}})" if expr + lines.push "doctest.input(#{f expr});" if expr expr = match[1] else if match = /^[.](.*)/.exec comment expr += '\n' + match[1] else if expr - lines.push "doctest.input(function(){return #{expr}})" - lines.push "doctest.output(#{idx + 1},function(){return #{comment}})" + lines.push "doctest.input(#{f expr});" + lines.push "doctest.output(#{idx + 1}, #{f comment});" expr = '' else lines.push line diff --git a/doctest.js b/doctest.js index f3f34f11..c8273adb 100644 --- a/doctest.js +++ b/doctest.js @@ -22,7 +22,7 @@ return _.each(urls, fetch); }; - doctest.version = '0.2.1'; + doctest.version = '0.2.2'; doctest.queue = []; @@ -108,7 +108,10 @@ }; rewrite = function(text) { - var comment, expr, idx, line, lines, match, _i, _len, _ref; + var comment, expr, f, idx, line, lines, match, _i, _len, _ref; + f = function(expr) { + return "function() {\n return " + expr + "\n}"; + }; lines = []; expr = ''; _ref = text.split(/\r?\n|\r/); @@ -118,14 +121,14 @@ comment = match[1]; if (match = /^>(.*)/.exec(comment)) { if (expr) { - lines.push("doctest.input(function(){return " + expr + "})"); + lines.push("doctest.input(" + (f(expr)) + ");"); } expr = match[1]; } else if (match = /^[.](.*)/.exec(comment)) { expr += '\n' + match[1]; } else if (expr) { - lines.push("doctest.input(function(){return " + expr + "})"); - lines.push("doctest.output(" + (idx + 1) + ",function(){return " + comment + "})"); + lines.push("doctest.input(" + (f(expr)) + ");"); + lines.push("doctest.output(" + (idx + 1) + ", " + (f(comment)) + ");"); expr = ''; } } else { diff --git a/test/test.html b/test/test.html index 451f3ba6..78de3e0d 100644 --- a/test/test.html +++ b/test/test.html @@ -67,7 +67,13 @@ [true, '"Docco-compatible whitespace"', , 78]) t('">" in doctest', - [true, true, true, 81]) + [true, true, , 81]) + + t('comment on input line', + [true, '"foobar"', , 85]) + + t('comment on output line', + [true, 25, , 88]) start() } diff --git a/test/test.js b/test/test.js index ca7ddaaf..3cf42095 100644 --- a/test/test.js +++ b/test/test.js @@ -80,4 +80,16 @@ global = 'global' // > 2 > 1 // true + 17. + // > "foo" + "bar" // comment + // "foobar" + 18. + // > 5 * 5 + // 25 // comment + + 19. + // > "the rewriter should not rely" + // "on automatic semicolon insertion" + (4 + 4) + }()