Skip to content

Commit

Permalink
allow comments in input/output, and don't rely on ASI
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed May 30, 2012
1 parent 1843b8c commit 093ec4e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
16 changes: 12 additions & 4 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
9 changes: 5 additions & 4 deletions doctest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

doctest = (urls...) -> _.each urls, fetch

doctest.version = '0.2.1'
doctest.version = '0.2.2'

doctest.queue = []

Expand Down Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions doctest.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}()

0 comments on commit 093ec4e

Please sign in to comment.