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_switch.coffee

93 lines
1.5 KiB
CoffeeScript
Raw Normal View History

2010-07-25 01:23:37 -04:00
num = 10
2010-07-25 01:23:37 -04:00
result = switch num
2009-12-28 23:08:02 -05:00
when 5 then false
when 'a'
true
true
false
when 10 then true
# Mid-switch comment with whitespace
# and multi line
2009-12-28 23:08:02 -05:00
when 11 then false
else false
ok result
2010-02-12 16:23:52 -05:00
2010-07-25 01:23:37 -04:00
func = (num) ->
switch num
when 2, 4, 6
true
when 1, 3, 5
false
ok func(2)
ok func(6)
ok !func(3)
2010-10-20 23:45:50 -04:00
eq func(8), undefined
# Ensure that trailing switch elses don't get rewritten.
2010-07-25 01:23:37 -04:00
result = false
switch "word"
when "one thing"
doSomething()
else
2010-07-25 01:23:37 -04:00
result = true unless false
ok result
2010-07-25 01:23:37 -04:00
result = false
switch "word"
when "one thing"
doSomething()
when "other thing"
doSomething()
else
2010-07-25 01:23:37 -04:00
result = true unless false
ok result
2010-05-10 22:57:51 -04:00
# Should be able to handle switches sans-condition.
2010-07-25 01:23:37 -04:00
result = switch
when null then 0
when !1 then 1
when '' not of {''} then 2
when [] not instanceof Array then 3
when true is false then 4
when 'x' < 'y' > 'z' then 5
when 'a' in ['b', 'c'] then 6
when 'd' in (['e', 'f']) then 7
else ok
eq result, ok
# Should be able to use "@properties" within the switch clause.
2010-07-25 01:23:37 -04:00
obj = {
num: 101
func: ->
switch @num
when 101 then '101!'
else 'other'
}
ok obj.func() is '101!'
# Should be able to use "@properties" within the switch cases.
obj = {
num: 101
func: (yesOrNo) ->
result = switch yesOrNo
when yes then @num
else 'other'
result
}
ok obj.func(yes) is 101