Skip to content

Commit

Permalink
changed occurrences of deepEqual to new, self-defined arrayEqual for
Browse files Browse the repository at this point in the history
recursively walking arrays and testing if their values are equal
  • Loading branch information
michaelficarra committed Dec 16, 2010
1 parent 912d6f4 commit dd11528
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
15 changes: 15 additions & 0 deletions Cakefile
Expand Up @@ -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', ->
Expand Down
8 changes: 4 additions & 4 deletions test/arguments.coffee
Expand Up @@ -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 = {}
Expand Down
2 changes: 1 addition & 1 deletion test/helpers.coffee
Expand Up @@ -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`
Expand Down
29 changes: 16 additions & 13 deletions test/test.html
Expand Up @@ -34,27 +34,30 @@ <h1>CoffeeScript Test Suite</h1>
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

@arrayEqual = (a, b, msg) -> ok arrayEq(a,b), msg

this.throws = (fun, err, msg) ->
@throws = (fun, err, msg) ->
try fun(); throw new String 'No Error'
catch e then eq e, err

Expand Down

0 comments on commit dd11528

Please sign in to comment.