mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Refactored test_helpers.coffee. Added now-needed deepEqual to browser
test page; warning: was unable to test it, so it might be completely broken!
This commit is contained in:
parent
9dc7d2a081
commit
d9cf34ab8c
2 changed files with 100 additions and 32 deletions
|
@ -43,6 +43,17 @@
|
||||||
|
|
||||||
this.eq = (x, y, msg) -> ok x is y, msg ? x + ' !== ' + y
|
this.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
|
||||||
|
else
|
||||||
|
`a == b`
|
||||||
|
|
||||||
this.throws = (fun, err, msg) ->
|
this.throws = (fun, err, msg) ->
|
||||||
try fun(); throw new String 'No Error'
|
try fun(); throw new String 'No Error'
|
||||||
catch e then eq e, err
|
catch e then eq e, err
|
||||||
|
@ -71,6 +82,7 @@
|
||||||
'break'
|
'break'
|
||||||
'comments'
|
'comments'
|
||||||
'exception_handling'
|
'exception_handling'
|
||||||
|
'helpers'
|
||||||
'operators'
|
'operators'
|
||||||
'regular_expressions'
|
'regular_expressions'
|
||||||
'test_chaining'
|
'test_chaining'
|
||||||
|
@ -79,7 +91,6 @@
|
||||||
'test_comprehensions'
|
'test_comprehensions'
|
||||||
'test_existence'
|
'test_existence'
|
||||||
'test_functions'
|
'test_functions'
|
||||||
'test_helpers'
|
|
||||||
'test_heredocs'
|
'test_heredocs'
|
||||||
'test_if'
|
'test_if'
|
||||||
'test_literals'
|
'test_literals'
|
||||||
|
@ -94,4 +105,4 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,39 +1,96 @@
|
||||||
|
# Helpers
|
||||||
|
# -------
|
||||||
|
|
||||||
|
# pull the helpers from `CoffeeScript.helpers` into local variables
|
||||||
{starts, ends, compact, count, merge, extend, flatten, del, last} = CoffeeScript.helpers
|
{starts, ends, compact, count, merge, extend, flatten, del, last} = CoffeeScript.helpers
|
||||||
|
|
||||||
array = [0, 1, 2, 3, 4]
|
|
||||||
string = array.join ''
|
|
||||||
object = {}
|
|
||||||
|
|
||||||
# Test `starts`
|
#### `starts`
|
||||||
ok starts string, '012'
|
|
||||||
ok starts string, '34', 3
|
|
||||||
ok not starts string, '42'
|
|
||||||
ok not starts string, '42', 6
|
|
||||||
|
|
||||||
# Test `ends`
|
test "the `starts` helper tests if a string starts with another string", ->
|
||||||
ok ends string, '234'
|
ok starts('01234', '012')
|
||||||
ok ends string, '01', 3
|
ok not starts('01234', '123')
|
||||||
ok not ends string, '42'
|
|
||||||
ok not ends string, '42', 6
|
|
||||||
|
|
||||||
# Test `merge`
|
test "the `starts` helper can take an optional offset", ->
|
||||||
merged = merge object, array
|
ok starts('01234', '34', 3)
|
||||||
ok merged isnt object
|
ok not starts('01234', '01', 1)
|
||||||
eq merged[3], 3
|
|
||||||
|
|
||||||
# Test `extend`
|
|
||||||
ok object is extend object, array
|
|
||||||
eq object[3], 3
|
|
||||||
|
|
||||||
# Test `flatten`
|
#### `ends`
|
||||||
ay = yes
|
|
||||||
(ay and= typeof n is 'number') for n in flatten [0, [[1], 2], 3, [4]]
|
|
||||||
ok ay
|
|
||||||
|
|
||||||
# Test `del`
|
test "the `ends` helper tests if a string ends with another string", ->
|
||||||
eq 1, del object, 1
|
ok ends('01234', '234')
|
||||||
ok 1 not of object
|
ok not ends('01234', '012')
|
||||||
|
|
||||||
# Test `last`
|
test "the `ends` helper can take an optional offset", ->
|
||||||
eq 4, last array
|
ok ends('01234', '012', 2)
|
||||||
eq 2, last array, 2
|
ok not ends('01234', '234', 6)
|
||||||
|
|
||||||
|
|
||||||
|
#### `compact`
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
#### `count`
|
||||||
|
|
||||||
|
test "the `count` helper counts the number of occurances of a string in another string", ->
|
||||||
|
eq 1/0, count('abc', '')
|
||||||
|
eq 0, count('abc', 'z')
|
||||||
|
eq 1, count('abc', 'a')
|
||||||
|
eq 1, count('abc', 'b')
|
||||||
|
eq 2, count('abcdc', 'c')
|
||||||
|
eq 2, count('abcdabcd','abc')
|
||||||
|
|
||||||
|
|
||||||
|
#### `merge`
|
||||||
|
|
||||||
|
test "the `merge` helper makes a new object with all properties of the objects given as its arguments", ->
|
||||||
|
ary = [0, 1, 2, 3, 4]
|
||||||
|
obj = {}
|
||||||
|
merged = merge obj, ary
|
||||||
|
ok merged isnt obj
|
||||||
|
ok merged isnt ary
|
||||||
|
for own key, val of ary
|
||||||
|
eq val, merged[key]
|
||||||
|
|
||||||
|
|
||||||
|
#### `extend`
|
||||||
|
|
||||||
|
test "the `extend` helper performs a shallow copy", ->
|
||||||
|
ary = [0, 1, 2, 3]
|
||||||
|
obj = {}
|
||||||
|
# should return the object being extended
|
||||||
|
eq obj, extend(obj, ary)
|
||||||
|
# should copy the other object's properties as well (obviously)
|
||||||
|
eq 2, obj[2]
|
||||||
|
|
||||||
|
|
||||||
|
#### `flatten`
|
||||||
|
|
||||||
|
test "the `flatten` helper flattens an array", ->
|
||||||
|
success = yes
|
||||||
|
(success and= typeof n is 'number') for n in flatten [0, [[[1]], 2], 3, [4]]
|
||||||
|
ok success
|
||||||
|
|
||||||
|
|
||||||
|
#### `del`
|
||||||
|
|
||||||
|
test "the `del` helper deletes a property from an object and returns the deleted value", ->
|
||||||
|
obj = [0, 1, 2]
|
||||||
|
eq 1, del(obj, 1)
|
||||||
|
ok 1 not of obj
|
||||||
|
|
||||||
|
|
||||||
|
#### `last`
|
||||||
|
|
||||||
|
test "the `last` helper returns the last item of an array-like object", ->
|
||||||
|
ary = [0, 1, 2, 3, 4]
|
||||||
|
eq 4, last(ary)
|
||||||
|
|
||||||
|
test "the `last` helper allows one to specify an optional offset", ->
|
||||||
|
ary = [0, 1, 2, 3, 4]
|
||||||
|
eq 2, last(ary, 2)
|
||||||
|
|
Loading…
Reference in a new issue