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

73 lines
1 KiB
CoffeeScript
Raw Normal View History

# Can assign the result of a try/catch block.
2010-07-25 01:23:37 -04:00
result = try
2010-01-17 10:40:59 -05:00
nonexistent * missing
catch error
true
2010-07-25 01:23:37 -04:00
result2 = try nonexistent * missing catch error then true
2010-01-17 10:40:59 -05:00
ok result is true and result2 is true
2010-01-17 10:40:59 -05:00
# Can assign a conditional statement.
2010-07-25 01:23:37 -04:00
getX = -> 10
2010-01-17 10:40:59 -05:00
2010-07-25 01:23:37 -04:00
if x = getX() then 100
2010-01-17 10:40:59 -05:00
ok x is 10
2010-01-17 10:40:59 -05:00
2010-07-25 01:23:37 -04:00
x = if getX() then 100
2010-01-17 10:40:59 -05:00
ok x is 100
# This-assignment.
2010-07-25 01:23:37 -04:00
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()']
2010-11-20 18:21:31 -05:00
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)()