2010-07-25 05:23:37 +00:00
|
|
|
num = 10
|
2009-12-24 09:33:59 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
result = switch num
|
2009-12-29 04:08:02 +00:00
|
|
|
when 5 then false
|
|
|
|
when 'a'
|
|
|
|
true
|
|
|
|
true
|
|
|
|
false
|
|
|
|
when 10 then true
|
2010-01-09 18:25:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Mid-switch comment with whitespace
|
|
|
|
# and multi line
|
2009-12-29 04:08:02 +00:00
|
|
|
when 11 then false
|
|
|
|
else false
|
2010-01-09 18:25:44 +00:00
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok result
|
2010-01-14 00:56:35 +00:00
|
|
|
|
2010-02-12 21:23:52 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
func = (num) ->
|
2010-01-14 00:56:35 +00:00
|
|
|
switch num
|
|
|
|
when 2, 4, 6
|
|
|
|
true
|
|
|
|
when 1, 3, 5
|
|
|
|
false
|
|
|
|
else false
|
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok func(2)
|
|
|
|
ok func(6)
|
|
|
|
ok !func(3)
|
|
|
|
ok !func(8)
|
2010-02-18 02:23:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Should cache the switch value, if anything fancier than a literal.
|
2010-07-25 05:23:37 +00:00
|
|
|
num = 5
|
|
|
|
result = switch num += 5
|
2010-02-18 02:23:59 +00:00
|
|
|
when 5 then false
|
|
|
|
when 15 then false
|
|
|
|
when 10 then true
|
|
|
|
else false
|
|
|
|
|
|
|
|
ok result
|
|
|
|
|
2010-02-23 00:22:09 +00:00
|
|
|
|
|
|
|
# Ensure that trailing switch elses don't get rewritten.
|
2010-07-25 05:23:37 +00:00
|
|
|
result = false
|
2010-02-23 00:22:09 +00:00
|
|
|
switch "word"
|
|
|
|
when "one thing"
|
2010-06-12 23:05:13 +00:00
|
|
|
doSomething()
|
2010-02-23 00:22:09 +00:00
|
|
|
else
|
2010-07-25 05:23:37 +00:00
|
|
|
result = true unless false
|
2010-02-23 00:22:09 +00:00
|
|
|
|
|
|
|
ok result
|
2010-02-23 01:17:54 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
result = false
|
2010-02-23 01:17:54 +00:00
|
|
|
switch "word"
|
|
|
|
when "one thing"
|
2010-06-12 23:05:13 +00:00
|
|
|
doSomething()
|
2010-02-23 01:17:54 +00:00
|
|
|
when "other thing"
|
2010-06-12 23:05:13 +00:00
|
|
|
doSomething()
|
2010-02-23 01:17:54 +00:00
|
|
|
else
|
2010-07-25 05:23:37 +00:00
|
|
|
result = true unless false
|
2010-02-23 01:17:54 +00:00
|
|
|
|
|
|
|
ok result
|
2010-05-11 02:57:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Should be able to handle switches sans-condition.
|
2010-07-25 05:23:37 +00:00
|
|
|
result = switch
|
2010-05-11 13:32:16 +00:00
|
|
|
when null then 1
|
|
|
|
when 'truthful string' then 2
|
2010-05-11 02:57:51 +00:00
|
|
|
else 3
|
|
|
|
|
2010-05-11 13:32:16 +00:00
|
|
|
ok result is 2
|
2010-05-31 19:13:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Should be able to use "@properties" within the switch clause.
|
2010-07-25 05:23:37 +00:00
|
|
|
obj = {
|
2010-05-31 19:13:48 +00:00
|
|
|
num: 101
|
|
|
|
func: ->
|
|
|
|
switch @num
|
|
|
|
when 101 then '101!'
|
|
|
|
else 'other'
|
|
|
|
}
|
|
|
|
|
|
|
|
ok obj.func() is '101!'
|