Skip to content

Commit

Permalink
support tests which expect a particular kind of error to be thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed May 11, 2012
1 parent e155027 commit d7b2591
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
9 changes: 9 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ This can easily be done from a browser console:

Oops. Looks like we have a bug.

### Errors

It's easy to indicate that an error (of a particular kind) is expected:

// > (var x = 5)
// SyntaxError
// > null.length
// TypeError

### Scoping

All expressions are eval'd in the global scope, so the following will leave
Expand Down
16 changes: 10 additions & 6 deletions doctest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ window.doctest = (url) ->
jQuery.get url, (text) ->
results = test text
console.log "running doctests in #{url}..."
console.log ((if pass then '.' else 'x') for {pass} in results).join ''
for {pass, expected, actual, num} in (r for r in results when not r.pass)
console.warn "expected #{q expected} on line #{num} (got #{q actual})"
console.log ((if pass then '.' else 'x') for [pass] in results).join ''
for [pass, expected, actual, num] in (r for r in results when not r[0])
console.warn "expected #{expected} on line #{num} (got #{actual})"

window.doctest.version = '0.1.0'
window.doctest.version = '0.1.1'

commented_lines = (text) ->
lines = []
Expand All @@ -30,9 +30,13 @@ test = (text) ->
else if match = /^[.](.*)/.exec line
expr += '\n' + match[1]
else if expr
actual = eval expr
expected = eval line
results.push {actual, expected, num, pass: _.isEqual expected, actual}
results.push try
actual = eval expr
[_.isEqual(actual, expected), q(expected), q(actual), num]
catch error
actual = error.constructor
[actual is expected, expected?.name or q(expected), error.name, num]
expr = ''
results

Expand Down
26 changes: 14 additions & 12 deletions doctest.js

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

0 comments on commit d7b2591

Please sign in to comment.