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

441 lines
7.4 KiB
CoffeeScript
Raw Normal View History

2010-12-29 00:48:54 -05:00
# Control Flow
# ------------
2010-12-29 14:06:57 -05:00
# * Conditionals
# * Loops
# * For
# * While
# * Until
# * Loop
# * Switch
# * Throw
2010-12-29 14:06:57 -05:00
# TODO: make sure postfix forms and expression coercion are properly tested
2010-12-29 00:48:54 -05:00
# shared identity function
id = (_) -> if arguments.length is 1 then _ else Array::slice.call(arguments)
2011-03-11 21:41:12 -05:00
# Conditionals
2010-12-29 00:48:54 -05:00
test "basic conditionals", ->
if false
ok false
else if false
ok false
else
ok true
if true
ok true
else if true
ok false
else
ok true
unless true
ok false
else unless true
ok false
else
ok true
unless false
ok true
else unless false
ok false
else
ok true
test "single-line conditional", ->
if false then ok false else ok true
unless false then ok true else ok false
test "nested conditionals", ->
nonce = {}
eq nonce, (if true
unless false
if false then false else
if true
nonce)
test "nested single-line conditionals", ->
nonce = {}
a = if false then undefined else b = if 0 then undefined else nonce
eq nonce, a
eq nonce, b
c = if false then undefined else (if 0 then undefined else nonce)
eq nonce, c
d = if true then id(if false then undefined else nonce)
eq nonce, d
test "empty conditional bodies", ->
eq undefined, (if false
else if false
else)
test "conditional bodies containing only comments", ->
eq undefined, (if true
###
block comment
###
else
# comment
)
eq undefined, (if false
# comment
else if true
###
block comment
###
else)
test "return value of if-else is from the proper body", ->
nonce = {}
eq nonce, if false then undefined else nonce
test "return value of unless-else is from the proper body", ->
nonce = {}
eq nonce, unless true then undefined else nonce
test "assign inside the condition of a conditional statement", ->
nonce = {}
if a = nonce then 1
eq nonce, a
1 if b = nonce
eq nonce, b
# Interactions With Functions
test "single-line function definition with single-line conditional", ->
fn = -> if 1 < 0.5 then 1 else -1
ok fn() is -1
test "function resturns conditional value with no `else`", ->
fn = ->
return if false then true
eq undefined, fn()
test "function returns a conditional value", ->
a = {}
fnA = ->
return if false then undefined else a
eq a, fnA()
b = {}
fnB = ->
return unless false then b else undefined
eq b, fnB()
test "passing a conditional value to a function", ->
nonce = {}
eq nonce, id if false then undefined else nonce
test "unmatched `then` should catch implicit calls", ->
a = 0
trueFn = -> true
2011-01-03 04:17:00 -05:00
if trueFn undefined then a++
2010-12-29 00:48:54 -05:00
eq 1, a
# if-to-ternary
test "if-to-ternary with instanceof requires parentheses", ->
nonce = {}
eq nonce, (if {} instanceof Object
nonce
else
undefined)
test "if-to-ternary as part of a larger operation requires parentheses", ->
ok 2, 1 + if false then 0 else 1
# Odd Formatting
test "if-else indented within an assignment", ->
nonce = {}
result =
if false
undefined
else
nonce
eq nonce, result
test "suppressed indentation via assignment", ->
nonce = {}
result =
if false then undefined
else if no then undefined
else if 0 then undefined
else if 1 < 0 then undefined
else id(
if false then undefined
else nonce
)
eq nonce, result
test "tight formatting with leading `then`", ->
nonce = {}
eq nonce,
if true
then nonce
else undefined
test "#738", ->
nonce = {}
fn = if true then -> nonce
eq nonce, fn()
test "#748: trailing reserved identifiers", ->
nonce = {}
obj = delete: true
result = if obj.delete
nonce
eq nonce, result
2011-03-11 22:09:33 -05:00
test "basic `while` loops", ->
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
i = 5
list = while i -= 1
i * 2
ok list.join(' ') is "8 6 4 2"
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
i = 5
list = (i * 3 while i -= 1)
ok list.join(' ') is "12 9 6 3"
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
i = 5
func = (num) -> i -= num
assert = -> ok i < 5 > 0
results = while func 1
assert()
i
ok results.join(' ') is '4 3 2 1'
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
i = 10
results = while i -= 1 when i % 2 is 0
i * 2
ok results.join(' ') is '16 12 8 4'
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
test "Issue 759: `if` within `while` condition", ->
2 while if 1 then 0
2010-12-29 00:48:54 -05:00
test "assignment inside the condition of a `while` loop", ->
2011-03-11 22:09:33 -05:00
2010-12-29 00:48:54 -05:00
nonce = {}
count = 1
a = nonce while count--
eq nonce, a
count = 1
while count--
b = nonce
eq nonce, b
2011-03-11 22:09:33 -05:00
test "While over break.", ->
i = 0
result = while i < 10
i++
break
arrayEq result, []
test "While over continue.", ->
i = 0
result = while i < 10
i++
continue
arrayEq result, []
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
test "Basic `until`", ->
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
value = false
i = 0
results = until value
value = true if i is 5
i++
ok i is 6
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
test "Basic `loop`", ->
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
i = 5
list = []
loop
i -= 1
break if i is 0
list.push i * 2
ok list.join(' ') is '8 6 4 2'
2010-12-29 00:48:54 -05:00
test "break at the top level", ->
for i in [1,2,3]
result = i
if i == 2
break
eq 2, result
test "break *not* at the top level", ->
2011-03-11 22:09:33 -05:00
someFunc = ->
2010-12-29 00:48:54 -05:00
i = 0
while ++i < 3
result = i
break if i > 1
result
eq 2, someFunc()
2011-03-11 22:09:33 -05:00
test "basic `switch`", ->
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
num = 10
result = switch num
when 5 then false
when 'a'
true
true
false
when 10 then true
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
# Mid-switch comment with whitespace
# and multi line
when 11 then false
else false
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
ok result
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
func = (num) ->
switch num
when 2, 4, 6
true
when 1, 3, 5
false
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
ok func(2)
ok func(6)
ok !func(3)
eq func(8), undefined
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
test "Ensure that trailing switch elses don't get rewritten.", ->
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
result = false
switch "word"
when "one thing"
doSomething()
else
result = true unless false
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
ok result
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
result = false
switch "word"
when "one thing"
doSomething()
when "other thing"
doSomething()
else
result = true unless false
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
ok result
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
test "Should be able to handle switches sans-condition.", ->
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05: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
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
eq result, ok
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
test "Should be able to use `@properties` within the switch clause.", ->
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
obj = {
num: 101
func: ->
switch @num
when 101 then '101!'
else 'other'
}
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
ok obj.func() is '101!'
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
test "Should be able to use `@properties` within the switch cases.", ->
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
obj = {
num: 101
func: (yesOrNo) ->
result = switch yesOrNo
when yes then @num
else 'other'
result
}
2010-12-29 00:48:54 -05:00
2011-03-11 22:09:33 -05:00
ok obj.func(yes) is 101
2011-03-11 22:09:33 -05:00
test "Switch with break as the return value of a loop.", ->
i = 10
results = while i > 0
i--
switch i % 2
when 1 then i
when 0 then break
eq results.join(', '), '9, 7, 5, 3, 1'
2011-03-11 22:09:33 -05:00
test "Issue #997. Switch doesn't fallthrough.", ->
val = 1
switch true
when true
if false
return 5
else
val = 2
2011-03-11 22:09:33 -05:00
eq val, 1
test "Throw should be usable as an expression.", ->
try
false or throw 'up'
throw new Error 'failed'
catch e
2011-11-24 09:58:04 -05:00
ok e is 'up'
2013-03-17 07:27:16 -04:00
test "#2555, strange function if bodies", ->
success = -> ok true
failure = -> ok false
success() if do ->
yes
failure() if try
false