2010-07-25 05:23:37 +00:00
|
|
|
a = b = d = true
|
|
|
|
c = false
|
2009-12-24 08:12:07 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
result = if a
|
2009-12-24 08:12:07 +00:00
|
|
|
if b
|
2009-12-29 04:08:02 +00:00
|
|
|
if c then false else
|
2009-12-24 08:12:07 +00:00
|
|
|
if d
|
2009-12-29 04:08:02 +00:00
|
|
|
true
|
2009-12-24 08:12:07 +00:00
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok result
|
2010-01-26 01:52:33 +00:00
|
|
|
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
first = if false then false else second = if false then false else true
|
2010-01-26 01:52:33 +00:00
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok first
|
|
|
|
ok second
|
2010-02-13 07:00:39 +00:00
|
|
|
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
result = if false
|
2010-02-13 07:00:39 +00:00
|
|
|
false
|
|
|
|
else if NaN
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
|
2010-03-10 21:18:17 +00:00
|
|
|
ok result
|
|
|
|
|
|
|
|
|
2010-04-27 23:35:15 +00:00
|
|
|
# Testing unless.
|
2010-07-25 05:23:37 +00:00
|
|
|
result = unless true
|
2010-04-27 23:35:15 +00:00
|
|
|
10
|
|
|
|
else
|
|
|
|
11
|
|
|
|
|
|
|
|
ok result is 11
|
2010-06-12 23:38:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Nested inline if statements.
|
2010-07-25 05:23:37 +00:00
|
|
|
echo = (x) -> x
|
|
|
|
result = if true then echo((if false then 'xxx' else 'y') + 'a')
|
2010-06-12 23:38:14 +00:00
|
|
|
ok result is 'ya'
|
2010-06-26 21:21:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Testing inline funcs with inline if-elses.
|
2010-07-25 05:23:37 +00:00
|
|
|
func = -> if 1 < 0.5 then 1 else -1
|
2010-06-26 21:21:30 +00:00
|
|
|
ok func() is -1
|
2010-06-27 17:19:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Testing empty or commented if statements ... should compile:
|
2010-07-25 05:23:37 +00:00
|
|
|
result = if false
|
2010-06-27 17:19:23 +00:00
|
|
|
else if false
|
|
|
|
else
|
|
|
|
|
|
|
|
ok result is undefined
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
result = if false
|
2010-06-27 17:19:23 +00:00
|
|
|
# comment
|
|
|
|
else if true
|
|
|
|
# comment
|
|
|
|
else
|
|
|
|
|
|
|
|
ok result is undefined
|
2010-07-07 03:04:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Return an if with no else.
|
2010-07-25 05:23:37 +00:00
|
|
|
func = ->
|
2010-08-12 01:28:22 +00:00
|
|
|
return if false then callback()
|
2010-07-07 03:04:35 +00:00
|
|
|
|
|
|
|
ok func() is null
|
2010-07-24 03:44:56 +00:00
|
|
|
|
2010-08-12 01:28:22 +00:00
|
|
|
func = ->
|
|
|
|
return unless false then 100 else -100
|
|
|
|
|
|
|
|
ok func() is 100
|
|
|
|
|
|
|
|
ident = (x) -> x
|
|
|
|
result = ident if false then 300 else 100
|
|
|
|
|
|
|
|
ok result is 100
|
|
|
|
|
2010-07-24 03:44:56 +00:00
|
|
|
|
|
|
|
# If-to-ternary with instanceof requires parentheses (no comment).
|
|
|
|
if {} instanceof Object
|
|
|
|
ok yes
|
|
|
|
else
|
|
|
|
ok no
|
2010-07-26 03:59:28 +00:00
|
|
|
|
2010-07-26 11:40:18 +00:00
|
|
|
try
|
|
|
|
{} + {}
|
|
|
|
ok yes
|
|
|
|
catch e
|
|
|
|
ok no
|
2010-08-17 00:18:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
# If-to-ternary as part of a larger operation requires parens.
|
|
|
|
x = 1
|
|
|
|
result = x + if false then 10 else 1
|
|
|
|
ok result is 2
|
2010-08-21 16:13:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# If/else indented within an assignment.
|
|
|
|
func = ->
|
|
|
|
a =
|
|
|
|
if false
|
|
|
|
3
|
|
|
|
else
|
|
|
|
5
|
|
|
|
101
|
|
|
|
a
|
|
|
|
|
|
|
|
ok func() is 5
|
2010-08-31 02:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Unmatched 'then' should catch implicit calls.
|
|
|
|
i = 1
|
|
|
|
isTrue = (x) -> x is true
|
|
|
|
|
|
|
|
if isTrue yes then i += 1
|
|
|
|
|
|
|
|
ok i is 2
|
2010-09-09 02:46:13 +00:00
|
|
|
|
|
|
|
# If/else with a suppressed indentation via assignment.
|
|
|
|
result =
|
|
|
|
if false then 10
|
|
|
|
else if no then 20
|
|
|
|
else if 0 then 30
|
|
|
|
else if NaN then 40
|
2010-09-09 02:48:28 +00:00
|
|
|
else 50 +
|
|
|
|
if false then 10
|
|
|
|
else 20
|
2010-09-09 02:46:13 +00:00
|
|
|
|
2010-09-09 02:48:28 +00:00
|
|
|
ok result is 70
|