jashkenas--coffeescript/test/test_if.coffee

121 lines
1.5 KiB
CoffeeScript
Raw Normal View History

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
ok result
2010-07-25 05:23:37 +00:00
first = if false then false else second = if false then false else true
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
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
# 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')
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
# Testing empty or commented if statements ... should compile:
2010-07-25 05:23:37 +00:00
result = if false
else if false
else
ok result is undefined
2010-07-25 05:23:37 +00:00
result = if false
# comment
else if true
# comment
else
ok result is undefined
# Return an if with no else.
2010-07-25 05:23:37 +00:00
func = ->
return if false then callback()
ok func() is null
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
# If-to-ternary with instanceof requires parentheses (no comment).
if {} instanceof Object
ok yes
else
ok no
2010-07-26 11:40:18 +00:00
try
{} + {}
ok yes
catch e
ok no
# 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
# If/else indented within an assignment.
func = ->
a =
if false
3
else
5
101
a
ok func() is 5
# Unmatched 'then' should catch implicit calls.
i = 1
isTrue = (x) -> x is true
if isTrue yes then i += 1
ok i is 2