consistency: eq(expected,actual), formatting, etc.

This commit is contained in:
Michael Ficarra
2010-12-07 22:04:16 -05:00
parent 6dcdf4ff45
commit 049df99afc
5 changed files with 129 additions and 122 deletions

View File

@@ -1,6 +1,7 @@
############
## Arguments
############
###############
## Arguments ##
###############
id = (_) ->
if arguments.length is 1 then _ else Array::slice.call(arguments)
@@ -10,10 +11,10 @@ id = (_) ->
a = {}
b = {}
c = {}
eq (id 1), 1
eq (id 1, 2)[1], 2
eq (id a), a
eq (id a, b, c)[2], c
eq 1, (id 1)
eq 2, (id 1, 2)[1]
eq a, (id a)
eq c, (id a, b, c)[2]
)()
# passing arguments on separate lines
@@ -26,15 +27,15 @@ id = (_) ->
b
c
)[1] is b)
eq(id(
eq(0, id(
0
10
)[0], 0)
eq(id(
)[0])
eq(a,id(
a
),a)
eq (id b),
b
))
eq b,
(id b)
)()
# reference `arguments` inside of functions
@@ -43,35 +44,35 @@ id = (_) ->
sum = (a,b)-> a + b
Array::reduce.call(arguments,sum,0)
eq sumOfArgs(0, 1, 2, 3, 4), 10
eq 10, sumOfArgs(0, 1, 2, 3, 4)
)()
#### Parameter List Features
# splats
eq (((splat...) -> splat) 0,1,2)[2], 2
eq (((_, _, splat...) -> splat) 0,1,2,3)[1], 3
eq (((splat..., _, _) -> splat) 0,1,2,3)[1], 1
eq (((_, _, splat..., _) -> splat) 0,1,2,3)[0], 2
eq 2, (((splat...) -> splat) 0,1,2)[2]
eq 3, (((_, _, splat...) -> splat) 0,1,2,3)[1]
eq 1, (((splat..., _, _) -> splat) 0,1,2,3)[1]
eq 2, (((_, _, splat..., _) -> splat) 0,1,2,3)[0]
# @-parameters: automatically assign an argument's value to a property of the context
(->
nonce = {}
((@prop) ->).call context = {}, nonce
eq context.prop, nonce
eq nonce, context.prop
# allow splats along side the special argument
((splat..., @prop) ->).apply context = {}, [0, 0, nonce]
eq context.prop, nonce
eq nonce, context.prop
# allow the argument itself to be a splat
((@prop...) ->).call context = {}, 0, nonce, 0
eq context.prop[1], nonce
eq nonce, context.prop[1]
# the argument should still be able to be referenced normally
eq (((@prop) -> prop).call {}, nonce), nonce
eq nonce, (((@prop) -> prop).call {}, nonce)
)()
# @-parameters and splats with constructors
@@ -82,14 +83,14 @@ eq (((_, _, splat..., _) -> splat) 0,1,2,3)[0], 2
constructor: (@first, splat..., @last) ->
obj = new Klass a, 0, 0, b
eq obj.first, a
eq obj.last, b
eq a, obj.first
eq b, obj.last
)()
# destructuring in function definition
(([{a: [b], c}]...) ->
eq b, 1
eq c, 2
eq 1, b
eq 2, c
) {a: [1], c: 2}
# default values
@@ -97,29 +98,29 @@ eq (((_, _, splat..., _) -> splat) 0,1,2,3)[0], 2
nonceA = {}
nonceB = {}
a = (_,_,arg=nonceA) -> arg
eq a(), nonceA
eq a(0), nonceA
eq a(0,0,nonceB), nonceB
eq a(0,0,undefined), nonceA
eq a(0,0,null), nonceA
eq a(0,0,false), false
eq a(undefined,undefined,nonceB,undefined), nonceB
eq nonceA, a()
eq nonceA, a(0)
eq nonceB, a(0,0,nonceB)
eq nonceA, a(0,0,undefined)
eq nonceA, a(0,0,null)
eq false , a(0,0,false)
eq nonceB, a(undefined,undefined,nonceB,undefined)
b = (_,arg=nonceA,_,_) -> arg
eq b(), nonceA
eq b(0), nonceA
eq b(0,nonceB), nonceB
eq b(0,undefined), nonceA
eq b(0,null), nonceA
eq b(0,false), false
eq b(undefined,nonceB,undefined), nonceB
eq nonceA, b()
eq nonceA, b(0)
eq nonceB, b(0,nonceB)
eq nonceA, b(0,undefined)
eq nonceA, b(0,null)
eq false , b(0,false)
eq nonceB, b(undefined,nonceB,undefined)
c = (arg=nonceA,_,_) -> arg
eq c(), nonceA
eq c(0), 0
eq c(nonceB), nonceB
eq c(undefined), nonceA
eq c(null), nonceA
eq c(false), false
eq c(nonceB,undefined,undefined), nonceB
eq nonceA, c()
eq 0, c(0)
eq nonceB, c(nonceB)
eq nonceA, c(undefined)
eq nonceA, c(null)
eq false , c(false)
eq nonceB, c(nonceB,undefined,undefined)
)()
# default values with @-parameters
@@ -127,16 +128,16 @@ eq (((_, _, splat..., _) -> splat) 0,1,2,3)[0], 2
a = {}
b = {}
obj = f: (q = a, @p = b) -> q
eq obj.f(), a
eq obj.p , b
eq a, obj.f()
eq b, obj.p
)()
# default values with splatted arguments
(->
withSplats = (a = 2, b..., c = 3, d = 5) -> a * (b.length + 1) * c * d
eq 30, withSplats()
eq 15, withSplats 1
eq 5, withSplats 1, 1
eq 1, withSplats 1, 1, 1
eq 2, withSplats 1, 1, 1, 1
eq 15, withSplats(1)
eq 5, withSplats(1,1)
eq 1, withSplats(1,1,1)
eq 2, withSplats(1,1,1,1)
)()

View File

@@ -1,6 +1,7 @@
#############
## Assignment
#############
################
## Assignment ##
################
# context property assignment (using @)
(->
@@ -8,7 +9,7 @@
addMethod = ->
@method = -> nonce
this
eq addMethod.call({}).method(), nonce
eq nonce, addMethod.call({}).method()
)()
# unassignable values
@@ -20,6 +21,7 @@
# compound assignments should not declare
# TODO: make description more clear
# TODO: remove reference to Math
eq Math, (-> Math or= 0)()
@@ -32,11 +34,11 @@ eq Math, (-> Math or= 0)()
nonexistent * missing
catch error
true
eq result, true
eq true, result
# single line
result = try nonexistent * missing catch error then true
eq result, true
eq true, result
)()
# conditionals
@@ -44,13 +46,13 @@ eq Math, (-> Math or= 0)()
# assign inside the condition of a conditional statement
nonce = {}
if a = nonce then 1
eq a, nonce
eq nonce, a
1 if b = nonce
eq b, nonce
eq nonce, b
# assign the result of a conditional statement
c = if true then nonce
eq c, nonce
eq nonce, c
)()
# assign inside the condition of a `while` loop
@@ -58,11 +60,11 @@ eq Math, (-> Math or= 0)()
nonce = {}
count = 1
a = nonce while count--
eq a, nonce
eq nonce, a
count = 1
while count--
b = nonce
eq b, nonce
eq nonce, b
)()
@@ -72,16 +74,16 @@ eq Math, (-> Math or= 0)()
(->
num = 10
num -= 5
eq num, 5
eq 5, num
num *= 10
eq num, 50
eq 50, num
num /= 10
eq num, 5
eq 5, num
num %= 3
eq num, 2
eq 2, num
)()
# more compound assignment
@@ -90,22 +92,23 @@ eq Math, (-> Math or= 0)()
val = undefined
val ||= a
val ||= true
eq val, a
eq a, val
b = {}
val &&= true
eq val, true
val &&= b
eq val, b
eq b, val
c = {}
val = null
val ?= c
val ?= true
eq val, c
eq c, val
)()
#### Destructuring Assignment
# NO TESTS?!
# TODO: make tests for destructuring assignment

View File

@@ -1,6 +1,7 @@
########
## Break
########
###########
## Break ##
###########
# break at the top level
(->
@@ -8,7 +9,7 @@
result = i
if i == 2
break
eq result, 2
eq 2, result
)()
# break *not* at the top level
@@ -19,5 +20,5 @@
result = i
break if i > 1
result
eq someFunc(), 2
eq 2, someFunc()
)()

View File

@@ -4,6 +4,7 @@
# note: some tests purposely left in outermost scope
# note: awkward spacing seen in some tests is likely intentional
# comments in objects
obj1 = {
# comment

View File

@@ -1,26 +1,27 @@
############
## Operators
############
###############
## Operators ##
###############
# binary (2-ary) math operators do not require spaces
(->
a = 1
b = -1
eq a*-b, 1
eq a*+b, -1
eq a/-b, 1
eq a/+b, -1
eq +1, a*-b
eq -1, a*+b
eq +1, a/-b
eq -1, a/+b
)()
# operators should respect new lines as spaced
(->
a = 123 +
456
eq a, 579
eq 579, a
b = "1#{2}3" +
"456"
eq b, '123456'
eq '123456', b
)()
# multiple operators should space themselves
@@ -28,18 +29,18 @@ eq (+ +1), (- -1)
# bitwise operators
(->
eq (10 & 3), 2
eq (10 | 3), 11
eq (10 ^ 3), 9
eq (10 << 3), 80
eq (10 >> 3), 1
eq (10 >>> 3), 1
num = 10; eq (num &= 3), 2
num = 10; eq (num |= 3), 11
num = 10; eq (num ^= 3), 9
num = 10; eq (num <<= 3), 80
num = 10; eq (num >>= 3), 1
num = 10; eq (num >>>= 3), 1
eq 2, (10 & 3)
eq 11, (10 | 3)
eq 9, (10 ^ 3)
eq 80, (10 << 3)
eq 1, (10 >> 3)
eq 1, (10 >>> 3)
num = 10; eq 2, (num &= 3)
num = 10; eq 11, (num |= 3)
num = 10; eq 9, (num ^= 3)
num = 10; eq 80, (num <<= 3)
num = 10; eq 1, (num >>= 3)
num = 10; eq 1, (num >>>= 3)
)()
# `instanceof`
@@ -60,33 +61,33 @@ eq (+ +1), (- -1)
a = 0
a or= nonce
eq a, nonce
eq nonce, a
b = 1
b or= nonce
eq b, 1
eq 1, b
c = 0
c and= nonce
eq c, 0
eq 0, c
d = 1
d and= nonce
eq d, nonce
eq nonce, d
# ensure that RHS is treated as a group
e = f = false
e and= f or true
eq e, false
eq false, e
)()
# compound assignment as a sub expression
(->
[a, b, c] = [1, 2, 3]
eq (a + b += c), 6
eq a, 1
eq b, 5
eq c, 3
eq 6, (a + b += c)
eq 1, a
eq 5, b
eq 3, c
)()
# compound assignment should be careful about caching variables
@@ -96,28 +97,28 @@ eq (+ +1), (- -1)
list = []
list[++count] or= 1
eq list[1], 1
eq count, 1
eq 1, list[1]
eq 1, count
list[++count] ?= 2
eq list[2], 2
eq count, 2
eq 2, list[2]
eq 2, count
list[count++] and= 'two'
eq list[2], 'two'
eq count, 3
list[count++] and= 6
eq 6, list[2]
eq 3, count
base = ->
++count
base
base().four or= 4
eq base.four, 4
eq count, 4
eq 4, base.four
eq 4, count
base().five ?= 5
eq base.five, 5
eq count, 5
eq 5, base.five
eq 5, count
)()
# compound assignment with implicit objects
@@ -126,13 +127,13 @@ eq (+ +1), (- -1)
obj ?=
one: 1
eq obj.one, 1
eq 1, obj.one
obj and=
two: 2
eq obj.one, undefined
eq obj.two, 2
eq undefined, obj.one
eq 2, obj.two
)()
@@ -202,7 +203,7 @@ eq (+ +1), (- -1)
)()
#???: `in` with cache and `__indexOf` should work in argument lists
eq [Object() in Array()].length, 1
eq 1, [Object() in Array()].length
#737: `in` should have higher precedence than logical operators.
eq 1, 1 in [1] and 1
@@ -214,7 +215,7 @@ eq 1, 1 in [1] and 1
b = -> share++ if share is 1
c = -> share++ if share is 2
ok a() not in [b(),c()]
eq share, 3
eq 3, share
)()