Abstract the `eq` and `arrayEq` functions, shared by Cakefile and test.html, into one file that can be included into both
This commit is contained in:
parent
faab9330e9
commit
729fec2947
23
Cakefile
23
Cakefile
|
@ -83,7 +83,7 @@ task 'build:full', 'rebuild the source twice, and run the tests', ->
|
||||||
|
|
||||||
|
|
||||||
task 'build:parser', 'rebuild the Jison parser (run build first)', ->
|
task 'build:parser', 'rebuild the Jison parser (run build first)', ->
|
||||||
helpers.extend global, require('util')
|
helpers.extend global, require 'util'
|
||||||
require 'jison'
|
require 'jison'
|
||||||
parser = require('./lib/coffee-script/grammar').parser
|
parser = require('./lib/coffee-script/grammar').parser
|
||||||
fs.writeFileSync 'lib/coffee-script/parser.js', parser.generate()
|
fs.writeFileSync 'lib/coffee-script/parser.js', parser.generate()
|
||||||
|
@ -183,6 +183,8 @@ task 'doc:site', 'watch and continually rebuild the documentation for the websit
|
||||||
</b>
|
</b>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
testHelpers = fs.readFileSync('test/support/helpers.coffee', 'utf-8').replace /exports\./g, '@'
|
||||||
|
|
||||||
testsInScriptBlocks = ->
|
testsInScriptBlocks = ->
|
||||||
output = ''
|
output = ''
|
||||||
for filename in fs.readdirSync 'test'
|
for filename in fs.readdirSync 'test'
|
||||||
|
@ -223,6 +225,7 @@ task 'doc:site', 'watch and continually rebuild the documentation for the websit
|
||||||
do renderTest = ->
|
do renderTest = ->
|
||||||
render = _.template fs.readFileSync(testFile, 'utf-8')
|
render = _.template fs.readFileSync(testFile, 'utf-8')
|
||||||
output = render
|
output = render
|
||||||
|
testHelpers: testHelpers
|
||||||
tests: testsInScriptBlocks()
|
tests: testsInScriptBlocks()
|
||||||
majorVersion: majorVersion
|
majorVersion: majorVersion
|
||||||
fs.writeFileSync "docs/v#{majorVersion}/test.html", output
|
fs.writeFileSync "docs/v#{majorVersion}/test.html", output
|
||||||
|
@ -289,23 +292,7 @@ runTests = (CoffeeScript) ->
|
||||||
description: description if description?
|
description: description if description?
|
||||||
source: fn.toString() if fn.toString?
|
source: fn.toString() if fn.toString?
|
||||||
|
|
||||||
# See http://wiki.ecmascript.org/doku.php?id=harmony:egal
|
helpers.extend global, require './test/support/helpers'
|
||||||
egal = (a, b) ->
|
|
||||||
if a is b
|
|
||||||
a isnt 0 or 1/a is 1/b
|
|
||||||
else
|
|
||||||
a isnt a and b isnt b
|
|
||||||
|
|
||||||
# A recursive functional equivalence helper; uses egal for testing equivalence.
|
|
||||||
arrayEgal = (a, b) ->
|
|
||||||
if egal a, b then yes
|
|
||||||
else if a instanceof Array and b instanceof Array
|
|
||||||
return no unless a.length is b.length
|
|
||||||
return no for el, idx in a when not arrayEgal el, b[idx]
|
|
||||||
yes
|
|
||||||
|
|
||||||
global.eq = (a, b, msg) -> ok egal(a, b), msg ? "Expected #{a} to equal #{b}"
|
|
||||||
global.arrayEq = (a, b, msg) -> ok arrayEgal(a,b), msg ? "Expected #{a} to deep equal #{b}"
|
|
||||||
|
|
||||||
# When all the tests have run, collect and print errors.
|
# When all the tests have run, collect and print errors.
|
||||||
# If a stacktrace is available, output the compiled function source.
|
# If a stacktrace is available, output the compiled function source.
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
<pre id="stdout"></pre>
|
<pre id="stdout"></pre>
|
||||||
|
|
||||||
<script type="text/coffeescript">
|
<script type="text/coffeescript">
|
||||||
|
@global = window
|
||||||
stdout = document.getElementById 'stdout'
|
stdout = document.getElementById 'stdout'
|
||||||
desc = ''
|
desc = ''
|
||||||
start = new Date
|
start = new Date
|
||||||
|
@ -57,26 +58,12 @@ say = (msg, emphasis) ->
|
||||||
say "#{desc}:", no
|
say "#{desc}:", no
|
||||||
throw Error say msg, no
|
throw Error say msg, no
|
||||||
|
|
||||||
@eq = (x, y, msg) -> ok x is y, msg ? x + ' isnt ' + y
|
<%= testHelpers %>
|
||||||
|
|
||||||
arrayEqual = (a, b) ->
|
|
||||||
if a is b
|
|
||||||
# 0 isnt -0
|
|
||||||
a isnt 0 or 1/a is 1/b
|
|
||||||
else if a instanceof Array and b instanceof Array
|
|
||||||
return no unless a.length is b.length
|
|
||||||
return no for el, idx in a when not arrayEq el, b[idx]
|
|
||||||
yes
|
|
||||||
else
|
|
||||||
# NaN is NaN
|
|
||||||
a isnt a and b isnt b
|
|
||||||
|
|
||||||
@doesNotThrow = (fn) ->
|
@doesNotThrow = (fn) ->
|
||||||
fn()
|
fn()
|
||||||
ok true
|
ok true
|
||||||
|
|
||||||
@arrayEq = (a, b, msg) -> ok arrayEqual(a,b), msg
|
|
||||||
|
|
||||||
@throws = (fun, err, msg) ->
|
@throws = (fun, err, msg) ->
|
||||||
try
|
try
|
||||||
fun()
|
fun()
|
||||||
|
@ -88,7 +75,6 @@ arrayEqual = (a, b) ->
|
||||||
return
|
return
|
||||||
ok no
|
ok no
|
||||||
|
|
||||||
|
|
||||||
# Run the tests
|
# Run the tests
|
||||||
for test in document.getElementsByClassName 'test'
|
for test in document.getElementsByClassName 'test'
|
||||||
say '\u2714 ' + test.id
|
say '\u2714 ' + test.id
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
# See http://wiki.ecmascript.org/doku.php?id=harmony:egal
|
||||||
|
egal = (a, b) ->
|
||||||
|
if a is b
|
||||||
|
a isnt 0 or 1/a is 1/b
|
||||||
|
else
|
||||||
|
a isnt a and b isnt b
|
||||||
|
|
||||||
|
# A recursive functional equivalence helper; uses egal for testing equivalence.
|
||||||
|
arrayEgal = (a, b) ->
|
||||||
|
if egal a, b then yes
|
||||||
|
else if a instanceof Array and b instanceof Array
|
||||||
|
return no unless a.length is b.length
|
||||||
|
return no for el, idx in a when not arrayEgal el, b[idx]
|
||||||
|
yes
|
||||||
|
|
||||||
|
exports.eq = (a, b, msg) -> ok egal(a, b), msg or "Expected #{a} to equal #{b}"
|
||||||
|
exports.arrayEq = (a, b, msg) -> ok arrayEgal(a,b), msg or "Expected #{a} to deep equal #{b}"
|
Loading…
Reference in New Issue