Skip to content

Commit

Permalink
fix serialization of errors in failing tests; add QUnit test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed May 28, 2012
1 parent f45144f commit f37b9ac
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 20 deletions.
7 changes: 7 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ MyApp.utils.bar = function() {
- [jQuery][2]
- [Underscore][3]

### Running the test suite

npm install express
node test/server

Visit [localhost:3000](http://localhost:3000/).


[1]: http://docs.python.org/library/doctest.html
[2]: http://jquery.com/
Expand Down
17 changes: 9 additions & 8 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.0'
doctest.version = '0.2.1'

doctest.queue = []

Expand All @@ -32,13 +32,9 @@ doctest.run = ->
input = fn
continue

actual = try input() catch error then error.constructor
expected = fn()
results.push try
actual = input()
[_.isEqual(actual, expected), q(expected), q(actual), num]
catch error
actual = error.constructor
[actual is expected, expected?.name or q(expected), error.name, num]
results.push [_.isEqual(actual, expected), q(expected), q(actual), num]
input = null

@complete results
Expand Down Expand Up @@ -81,7 +77,12 @@ rewrite = (text) ->


q = (object) ->
if typeof object is 'string' then "\"#{object}\"" else object
switch typeof object
when 'string' then return "\"#{object}\""
when 'function'
try throw object()
catch error then return object.name if error instanceof Error
object


window.doctest = doctest
31 changes: 19 additions & 12 deletions doctest.js

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

1 change: 1 addition & 0 deletions test/doctest.js
7 changes: 7 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var app = require('express').createServer();

app.get(/^\/((doc)?test[.]js)?$/, function (req, res) {
res.sendfile(__dirname + '/' + (req.route.params[0] || 'test.html'));
});

app.listen(3000);
88 changes: 88 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>doctest test suite</title>
<script src="/doctest.js"></script>
<script src="https://raw.github.com/documentcloud/underscore/master/underscore.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://code.jquery.com/qunit/git/qunit.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" />
<script>
jQuery(function($) {

doctest.complete = function(results) {
var idx = 0
var t = function(message, expected) {
if (expected[0]) expected[2] = expected[1]
deepEqual(results[idx++], expected, message)
}

t('global variable accessible in outer scope',
[true, '"global"', , 3])

t('global variable accessible in inner scope',
[true, '"global"', , 10])

t('local variable referenced, not shadowed global',
[true, '"shadowed"', , 14])

t('local variable accessible before declaratation',
[true, 2, , 20])

t('assignment is an expression',
[true, 3, , 25])

t('variable declared in doctest remains accessible',
[true, [1, 2, 3], , 28])

t('arithmetic error reported',
[false, 5, 4, 31])

t('TypeError captured and reported',
[true, 'TypeError', , 35])

t('TypeError expected but not reported',
[false, 'TypeError', 0, 38])

t('function accessible before declaration',
[true, 12, , 42])

t('NaN can be used as expected result',
[true, NaN, , 45])

t('function accessible after declaration',
[true, 4, , 53])

t('multiline input',
[true, [1, 2, 3, 4, 5, 6, 7, 8, 9], , 64])

t('multiline assignment',
[true, '"input may span many lines"', , 70])

t('spaces following "//" and ">" are optional',
[true, '"no spaces"', , 73])

t('indented doctest',
[true, '"Docco-compatible whitespace"', , 76])

t('">" in doctest',
[true, true, true, 79])

start()
}
asyncTest('all tests', function() {
doctest('./test.js')
})

})
</script>
</head>
<body>
<h1 id="qunit-header">doctest test suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>
81 changes: 81 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
0.
// > global
// "global"
global = 'global'

!function() {

1.
// > global
// "global"
!function() {
2.
// > global
// "shadowed"
var global = 'shadowed'
}()

3.
// > one * two
// 2
var one = 1
var two = 2
4.
// > three = one + two
// 3
5.
// > [one, two, three]
// [1, 2, 3]
6.
// > two + two
// 5

7.
// > null.length
// TypeError
8.
// > [].length
// TypeError

9.
// > double(6)
// 12
10.
// > double()
// NaN
var double = function(n) {
// doctests should only be included in contexts where they'll be
// invoked immediately (i.e. at the top level or within an IIFE)
return 2 * n
}
11.
// > double.call(null, 2)
// 4

var triple = function(n) {
// > this.doctest.should.never.be.executed
// ( blow.up.if.for.some.reason.it.is )
return 3 * n
}

// > [1,2,3,
// . 4,5,6,
// . 7,8,9]
// [1,2,3,4,5,6,7,8,9]
//
// > text = "input " +
// . "may span many " +
// . "lines"
// > text
// "input may span many lines"

//>"no spaces"
//"no spaces"
//
// > "Docco-compatible whitespace"
// "Docco-compatible whitespace"
//
// > 2 > 1
// true

}()

0 comments on commit f37b9ac

Please sign in to comment.