From 0c0a48d83e747abf3b22b245fd77e15bbfb65ac5 Mon Sep 17 00:00:00 2001 From: David Chambers Date: Sun, 2 Dec 2012 19:12:38 -0800 Subject: [PATCH] expose expected test output to headless test runner --- Makefile | 3 ++ README.markdown | 2 +- test/browser.coffee | 12 +++++++ test/cli.coffee | 27 +++++++++++++++ test/open | 11 ++++++ test/server.coffee | 6 ++++ test/test.html | 83 ++------------------------------------------- test/tests.coffee | 67 ++++++++++++++++++++++++++++++++++++ 8 files changed, 129 insertions(+), 82 deletions(-) create mode 100644 test/browser.coffee create mode 100644 test/cli.coffee create mode 100755 test/open create mode 100644 test/tests.coffee diff --git a/Makefile b/Makefile index 93131490..3e138322 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ .PHONY: compile clean setup test +PORT := 3000 coffee = node_modules/.bin/coffee compile: @@ -13,4 +14,6 @@ setup: @npm install test: + @$(coffee) test/cli + @sleep 0.1 && test/open http://localhost:$(PORT) & @$(coffee) test/server diff --git a/README.markdown b/README.markdown index 37647b32..5c76bf76 100644 --- a/README.markdown +++ b/README.markdown @@ -172,7 +172,7 @@ MyApp.utils.bar = function() { make setup make test -Visit [localhost:3000](http://localhost:3000/). +This runs doctest's test suite: first headless, then in a browser. [1]: http://docs.python.org/library/doctest.html diff --git a/test/browser.coffee b/test/browser.coffee new file mode 100644 index 00000000..2417d2db --- /dev/null +++ b/test/browser.coffee @@ -0,0 +1,12 @@ +jQuery ($) -> + + doctest.complete = (results) -> + for message, expected of tests + deepEqual results.shift(), expected, message + start() + + asyncTest 'JavaScript doctests', -> + doctest './test.js' + + asyncTest 'CoffeeScript doctests', -> + doctest './test.coffee' diff --git a/test/cli.coffee b/test/cli.coffee new file mode 100644 index 00000000..353d3faa --- /dev/null +++ b/test/cli.coffee @@ -0,0 +1,27 @@ +{isEqual} = require 'underscore' + +doctest = require '../src/doctest' +tests = require './tests' + + +gray = green = red = reset = '' +unless process.env.NODE_DISABLE_COLORS or process.platform is 'win32' + gray = '\x1B[0;37m' + green = '\x1B[0;32m' + red = '\x1B[0;31m' + reset = '\x1B[0m' + +queue = ['test/test.js', 'test/test.coffee'] +next = -> doctest queue.shift() if queue.length + +doctest.complete = (results) -> + for message, expected of tests + actual = results.shift() + if isEqual actual, expected + console.log "#{green} \u2714 #{gray} #{message}#{reset}" + else + console.warn "#{red} \u2718 #{gray} #{message}#{reset}" + console.log "#{gray} expected: #{green}#{expected}#{reset}" + console.log "#{gray} received: #{red}#{actual}#{reset}" + next() +next() diff --git a/test/open b/test/open new file mode 100755 index 00000000..c4659bf5 --- /dev/null +++ b/test/open @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +for program in open xdg-open ; do + command -v $program >/dev/null 2>&1 + if [[ $? == 0 ]] ; then + $program "$@" + exit 0 + fi +done + +exit 1 diff --git a/test/server.coffee b/test/server.coffee index 90c947a3..353f8ef1 100644 --- a/test/server.coffee +++ b/test/server.coffee @@ -21,5 +21,11 @@ app.get '/doctest.js', (req, res) -> res.contentType 'js' res.send CoffeeScript.compile text +['browser', 'tests'].forEach (name) -> + app.get "/#{name}.js", (req, res) -> + fs.readFile path.resolve('test', "#{name}.coffee"), 'utf8', (err, text) -> + res.contentType 'js' + res.send CoffeeScript.compile text + port = process.env.PORT ? 3000 app.listen port, -> console.log "listening on port #{port}" diff --git a/test/test.html b/test/test.html index 5d6abe8a..c83fd8a7 100644 --- a/test/test.html +++ b/test/test.html @@ -6,90 +6,11 @@ + - +

doctest test suite

diff --git a/test/tests.coffee b/test/tests.coffee new file mode 100644 index 00000000..24133a9d --- /dev/null +++ b/test/tests.coffee @@ -0,0 +1,67 @@ +tests = + +'global variable accessible in outer scope': + [yes, '"global"', '"global"', 3] + +'global variable accessible in inner scope': + [yes, '"global"', '"global"', 10] + +'local variable referenced, not shadowed global': + [yes, '"shadowed"', '"shadowed"', 14] + +'local variable accessible before declaration': + [yes, 2, 2, 20] + +'assignment is an expression': + [yes, 3, 3, 25] + +'variable declared in doctest remains accessible': + [yes, [1, 2, 3], [1, 2, 3], 28] + +'arithmetic error reported': + [no, 5, 4, 31] + +'TypeError captured and reported': + [yes, 'TypeError', 'TypeError', 35] + +'TypeError expected but not reported': + [no, 'TypeError', 0, 38] + +'function accessible before declaration': + [yes, 12, 12, 42] + +'NaN can be used as expected result': + [yes, NaN, NaN, 45] + +'function accessible after declaration': + [yes, 4, 4, 53] + +'multiline input': + [yes, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 65] + +'multiline assignment': + [yes, '"input may span many lines"', '"input may span many lines"', 71] + +'spaces following "//" and ">" are optional': + [yes, '"no spaces"', '"no spaces"', 75] + +'indented doctest': + [yes, '"Docco-compatible whitespace"', '"Docco-compatible whitespace"', 78] + +'">" in doctest': + [yes, yes, yes, 81] + +'comment on input line': + [yes, '"foobar"', '"foobar"', 85] + +'comment on output line': + [yes, 25, 25, 88] + +'variable in creation context is not accessible': + [yes, '"undefined"', '"undefined"', 92] + + +if typeof window isnt 'undefined' + window.tests = tests +else + module.exports = tests