1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

refactored test_assignment.coffee

This commit is contained in:
Michael Ficarra 2010-12-03 18:21:09 -05:00
parent fb7498a8ec
commit cf45da33f6
2 changed files with 111 additions and 72 deletions

111
test/assignment.coffee Normal file
View file

@ -0,0 +1,111 @@
#############
## Assignment
#############
# context property assignment (using @)
(->
nonce = {}
addMethod = ->
@method = -> nonce
this
eq addMethod.call({}).method(), nonce
)()
# unassignable values
(->
nonce = {}
for nonref in ['', '""', '0', 'f()'].concat CoffeeScript.RESERVED
eq nonce, (try CoffeeScript.compile "#{nonref} = v" catch e then nonce)
)()
# compound assignments should not declare
# TODO: make description more clear
eq Math, (-> Math or= 0)()
#### Statements as Expressions
# assign the result of a try/catch block
(->
# multiline
result = try
nonexistent * missing
catch error
true
eq result, true
# single line
result = try nonexistent * missing catch error then true
eq result, true
)()
# conditionals
(->
# assign inside the condition of a conditional statement
nonce = {}
if a = nonce then 1
eq a, nonce
1 if b = nonce
eq b, nonce
# assign the result of a conditional statement
c = if true then nonce
eq c, nonce
)()
# assign inside the condition of a `while` loop
(->
nonce = {}
count = 1
a = nonce while count--
eq a, nonce
count = 1
while count--
b = nonce
eq b, nonce
)()
#### Compound Assignment
# compound assignment (math operators)
(->
num = 10
num -= 5
eq num, 5
num *= 10
eq num, 50
num /= 10
eq num, 5
num %= 3
eq num, 2
)()
# more compound assignment
(->
a = {}
val = undefined
val ||= a
val ||= true
eq val, a
b = {}
val &&= true
eq val, true
val &&= b
eq val, b
c = {}
val = null
val ?= c
val ?= true
eq val, c
)()
#### Destructuring Assignment
# NO TESTS?!

View file

@ -1,72 +0,0 @@
# Can assign the result of a try/catch block.
result = try
nonexistent * missing
catch error
true
result2 = try nonexistent * missing catch error then true
ok result is true and result2 is true
# Can assign a conditional statement.
getX = -> 10
if x = getX() then 100
ok x is 10
x = if getX() then 100
ok x is 100
# This-assignment.
tester = ->
@example = -> 'example function'
this
ok tester().example() is 'example function'
try throw CoffeeScript.tokens 'in = 1'
catch e then eq e.message, 'Reserved word "in" on line 1 can\'t be assigned'
num = 10
num -= 5
eq num, 5
num *= 10
eq num, 50
num /= 10
eq num, 5
num %= 3
eq num, 2
val = false
val ||= 'value'
val ||= 'eulav'
eq val, 'value'
val &&= 'rehto'
val &&= 'other'
eq val, 'other'
val = null
val ?= 'value'
val ?= 'eulav'
eq val, 'value'
for nonref in ['""', '0', 'f()']
try
ok not CoffeeScript.compile "{k: #{nonref}} = v"
catch e
eq e.message, "\"#{nonref}\" cannot be assigned."
# Compound assignments should not declare.
eq Math, (-> Math or= 0)()