changed occurrences of deepEqual to new, self-defined arrayEqual for

recursively walking arrays and testing if their values are equal
This commit is contained in:
Michael Ficarra
2010-12-16 00:12:11 -05:00
parent 912d6f442a
commit dd11528160
4 changed files with 36 additions and 18 deletions

View File

@@ -169,6 +169,21 @@ runTests = (CoffeeScript) ->
e.source = fn.toString() if fn.toString?
failures.push file: currentFile, error: e
# A recursive equality helper
arrayEq = (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
global.arrayEqual = (a, b, msg) -> ok arrayEq(a,b), msg
# When all the tests have run, collect and print errors.
# If a stacktrace is available, output the compiled function source.
process.on 'exit', ->

View File

@@ -43,10 +43,10 @@ test "reference `arguments` inside of functions", ->
#### Parameter List Features
test "splats", ->
deepEqual [0, 1, 2], (((splat...) -> splat) 0, 1, 2)
deepEqual [2, 3], (((_, _, splat...) -> splat) 0, 1, 2, 3)
deepEqual [0, 1], (((splat..., _, _) -> splat) 0, 1, 2, 3)
deepEqual [2], (((_, _, splat..., _) -> splat) 0, 1, 2, 3)
arrayEqual [0, 1, 2], (((splat...) -> splat) 0, 1, 2)
arrayEqual [2, 3], (((_, _, splat...) -> splat) 0, 1, 2, 3)
arrayEqual [0, 1], (((splat..., _, _) -> splat) 0, 1, 2, 3)
arrayEqual [2], (((_, _, splat..., _) -> splat) 0, 1, 2, 3)
test "@-parameters: automatically assign an argument's value to a property of the context", ->
nonce = {}

View File

@@ -32,7 +32,7 @@ test "the `ends` helper can take an optional offset", ->
test "the `compact` helper removes falsey values from an array, preserves truthy ones", ->
allValues = [1, 0, false, obj={}, [], '', ' ', -1, null, undefined, true]
truthyValues = [1, obj, [], ' ', -1, true]
deepEqual truthyValues, compact(allValues)
arrayEqual truthyValues, compact(allValues)
#### `count`

View File

@@ -34,27 +34,30 @@
stdout.appendChild div
msg
this.test = (desc, fn) ->
@test = (desc, fn) ->
fn()
this.ok = (good, msg) ->
@ok = (good, msg) ->
++total
if good then ++success else throw Error say msg
this.eq = (x, y, msg) -> ok x is y, msg ? x + ' !== ' + y
@eq = (x, y, msg) -> ok x is y, msg ? x + ' !== ' + y
this.deepEqual = (a, b, msg) -> ok deepEq(a,b), msg
deepEq = (a, b) ->
if a instanceof Array and b instanceof Array
success = yes
success and= deepEq(a[prop],b[prop]) for prop in a
success and= deepEq(a[prop],b[prop]) for prop in b when prop not in a
success
arrayEq = (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
`a == b`
# NaN is NaN
a isnt a and b isnt b
this.throws = (fun, err, msg) ->
@arrayEqual = (a, b, msg) -> ok arrayEq(a,b), msg
@throws = (fun, err, msg) ->
try fun(); throw new String 'No Error'
catch e then eq e, err