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:
Geoffrey Booth 2016-11-29 21:19:11 -08:00
parent faab9330e9
commit 729fec2947
3 changed files with 24 additions and 34 deletions

View File

@ -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)', ->
helpers.extend global, require('util')
helpers.extend global, require 'util'
require 'jison'
parser = require('./lib/coffee-script/grammar').parser
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>
"""
testHelpers = fs.readFileSync('test/support/helpers.coffee', 'utf-8').replace /exports\./g, '@'
testsInScriptBlocks = ->
output = ''
for filename in fs.readdirSync 'test'
@ -223,6 +225,7 @@ task 'doc:site', 'watch and continually rebuild the documentation for the websit
do renderTest = ->
render = _.template fs.readFileSync(testFile, 'utf-8')
output = render
testHelpers: testHelpers
tests: testsInScriptBlocks()
majorVersion: majorVersion
fs.writeFileSync "docs/v#{majorVersion}/test.html", output
@ -289,23 +292,7 @@ runTests = (CoffeeScript) ->
description: description if description?
source: fn.toString() if fn.toString?
# 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
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}"
helpers.extend global, require './test/support/helpers'
# When all the tests have run, collect and print errors.
# If a stacktrace is available, output the compiled function source.

View File

@ -32,6 +32,7 @@
<pre id="stdout"></pre>
<script type="text/coffeescript">
@global = window
stdout = document.getElementById 'stdout'
desc = ''
start = new Date
@ -57,26 +58,12 @@ say = (msg, emphasis) ->
say "#{desc}:", no
throw Error say msg, no
@eq = (x, y, msg) -> ok x is y, msg ? x + ' isnt ' + y
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
<%= testHelpers %>
@doesNotThrow = (fn) ->
fn()
ok true
@arrayEq = (a, b, msg) -> ok arrayEqual(a,b), msg
@throws = (fun, err, msg) ->
try
fun()
@ -88,7 +75,6 @@ arrayEqual = (a, b) ->
return
ok no
# Run the tests
for test in document.getElementsByClassName 'test'
say '\u2714 ' + test.id

View File

@ -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}"