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 @@ + - +