mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-01-25 14:48:18 -05:00
using nonces more where applicable, also added tests for default
arguments
This commit is contained in:
@@ -57,29 +57,33 @@ eq (((_, _, splat..., _) -> splat) 0,1,2,3)[0], 2
|
||||
|
||||
# @-parameters: automatically assign an argument's value to a property of the context
|
||||
(->
|
||||
((@prop) ->).call context = {}, 1
|
||||
eq context.prop, 1
|
||||
nonce = {}
|
||||
|
||||
((@prop) ->).call context = {}, nonce
|
||||
eq context.prop, nonce
|
||||
|
||||
# allow splats along side the special argument
|
||||
((splat..., @prop) ->).apply context = {}, [1, 2, 3]
|
||||
eq context.prop, 3
|
||||
((splat..., @prop) ->).apply context = {}, [0, 0, nonce]
|
||||
eq context.prop, nonce
|
||||
|
||||
# allow the argument itself to be a splat
|
||||
((@prop...) ->).call context = {}, 1, 2, 3
|
||||
eq context.prop.join(' '), '1 2 3'
|
||||
((@prop...) ->).call context = {}, 0, nonce, 0
|
||||
eq context.prop[1], nonce
|
||||
|
||||
# the argument should still be able to be referenced normally
|
||||
eq (((@prop) -> prop).call {}, 1), 1
|
||||
eq (((@prop) -> prop).call {}, nonce), nonce
|
||||
)()
|
||||
|
||||
# @-parameters and splats with constructors
|
||||
(->
|
||||
a = {}
|
||||
b = {}
|
||||
class Klass
|
||||
constructor: (@first, splat..., @last) ->
|
||||
|
||||
obj = new Klass 0, 1, 2
|
||||
eq obj.first, 0
|
||||
eq obj.last, 2
|
||||
obj = new Klass a, 0, 0, b
|
||||
eq obj.first, a
|
||||
eq obj.last, b
|
||||
)()
|
||||
|
||||
# destructuring in function definition
|
||||
@@ -90,9 +94,41 @@ eq (((_, _, splat..., _) -> splat) 0,1,2,3)[0], 2
|
||||
|
||||
# default values
|
||||
(->
|
||||
obj = f: (q = 123, @p = 456) -> q
|
||||
eq obj.f(), 123
|
||||
eq obj.p , 456
|
||||
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
|
||||
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
|
||||
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
|
||||
)()
|
||||
|
||||
# default values with @-parameters
|
||||
(->
|
||||
a = {}
|
||||
b = {}
|
||||
obj = f: (q = a, @p = b) -> q
|
||||
eq obj.f(), a
|
||||
eq obj.p , b
|
||||
)()
|
||||
|
||||
# default values with splatted arguments
|
||||
@@ -104,10 +140,3 @@ eq (((_, _, splat..., _) -> splat) 0,1,2,3)[0], 2
|
||||
eq 1, withSplats 1, 1, 1
|
||||
eq 2, withSplats 1, 1, 1, 1
|
||||
)()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@ eq (+ +1), (- -1)
|
||||
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
|
||||
@@ -53,25 +52,27 @@ eq (+ +1), (- -1)
|
||||
)()
|
||||
|
||||
|
||||
#### compound assignment operators
|
||||
#### Compound Assignment Operators
|
||||
|
||||
# boolean operators
|
||||
(->
|
||||
nonce = {}
|
||||
|
||||
a = 0
|
||||
a or= 2
|
||||
eq a, 2
|
||||
a or= nonce
|
||||
eq a, nonce
|
||||
|
||||
b = 1
|
||||
b or= 2
|
||||
b or= nonce
|
||||
eq b, 1
|
||||
|
||||
c = 0
|
||||
c and= 2
|
||||
c and= nonce
|
||||
eq c, 0
|
||||
|
||||
d = 1
|
||||
d and= 2
|
||||
eq d, 2
|
||||
d and= nonce
|
||||
eq d, nonce
|
||||
|
||||
# ensure that RHS is treated as a group
|
||||
e = f = false
|
||||
@@ -89,6 +90,7 @@ eq (+ +1), (- -1)
|
||||
)()
|
||||
|
||||
# compound assignment should be careful about caching variables
|
||||
# *note: this test could still use refactoring*
|
||||
(->
|
||||
count = 0
|
||||
list = []
|
||||
@@ -216,7 +218,7 @@ eq 1, 1 in [1] and 1
|
||||
)()
|
||||
|
||||
|
||||
#### chainable operators
|
||||
#### Chainable Operators
|
||||
|
||||
ok 100 > 10 > 1 > 0 > -1
|
||||
ok -1 < 0 < 1 < 10 < 100
|
||||
@@ -239,6 +241,7 @@ eq 1, 1 | 2 < 3 < 4
|
||||
(->
|
||||
a = b = c = 1
|
||||
# `a == b <= c` should become `a === b && b <= c`
|
||||
# (this test does not seem to test for this)
|
||||
ok a == b <= c
|
||||
)()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user