mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
refactored test_if.coffee, renamed to conditionals.coffee
This commit is contained in:
parent
dd11528160
commit
b38cc75f17
3 changed files with 182 additions and 159 deletions
|
@ -199,13 +199,3 @@ test "block comments inside class bodies", ->
|
||||||
b: ->
|
b: ->
|
||||||
|
|
||||||
ok B.prototype.a instanceof Function
|
ok B.prototype.a instanceof Function
|
||||||
|
|
||||||
test "#934, block comments inside conditional bodies", ->
|
|
||||||
|
|
||||||
eq undefined, (if true
|
|
||||||
###
|
|
||||||
block comment
|
|
||||||
###
|
|
||||||
else
|
|
||||||
# comment
|
|
||||||
)
|
|
||||||
|
|
182
test/conditionals.coffee
Normal file
182
test/conditionals.coffee
Normal file
|
@ -0,0 +1,182 @@
|
||||||
|
# Conditionals
|
||||||
|
# ------------
|
||||||
|
|
||||||
|
# shared "identity" function
|
||||||
|
id = (_) -> _
|
||||||
|
|
||||||
|
|
||||||
|
#### Basic Conditionals
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
#### 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
|
||||||
|
if trueFn undefined then a += 1
|
||||||
|
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
|
|
@ -1,149 +0,0 @@
|
||||||
a = b = d = true
|
|
||||||
c = false
|
|
||||||
|
|
||||||
result = if a
|
|
||||||
if b
|
|
||||||
if c then false else
|
|
||||||
if d
|
|
||||||
true
|
|
||||||
|
|
||||||
ok result
|
|
||||||
|
|
||||||
|
|
||||||
first = if false then false else second = if false then false else true
|
|
||||||
|
|
||||||
ok first
|
|
||||||
ok second
|
|
||||||
|
|
||||||
|
|
||||||
result = if false
|
|
||||||
false
|
|
||||||
else if NaN
|
|
||||||
false
|
|
||||||
else
|
|
||||||
true
|
|
||||||
|
|
||||||
ok result
|
|
||||||
|
|
||||||
|
|
||||||
# Testing unless.
|
|
||||||
result = unless true
|
|
||||||
10
|
|
||||||
else
|
|
||||||
11
|
|
||||||
|
|
||||||
ok result is 11
|
|
||||||
|
|
||||||
|
|
||||||
# Nested inline if statements.
|
|
||||||
echo = (x) -> x
|
|
||||||
result = if true then echo((if false then 'xxx' else 'y') + 'a')
|
|
||||||
ok result is 'ya'
|
|
||||||
|
|
||||||
|
|
||||||
# Testing inline funcs with inline if-elses.
|
|
||||||
func = -> if 1 < 0.5 then 1 else -1
|
|
||||||
ok func() is -1
|
|
||||||
|
|
||||||
|
|
||||||
# Testing empty or commented if statements ... should compile:
|
|
||||||
result = if false
|
|
||||||
else if false
|
|
||||||
else
|
|
||||||
|
|
||||||
ok result is undefined
|
|
||||||
|
|
||||||
result = if false
|
|
||||||
# comment
|
|
||||||
else if true
|
|
||||||
# comment
|
|
||||||
else
|
|
||||||
|
|
||||||
ok result is undefined
|
|
||||||
|
|
||||||
|
|
||||||
# Return an if with no else.
|
|
||||||
func = ->
|
|
||||||
return if false then callback()
|
|
||||||
|
|
||||||
ok func() is undefined
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
# 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
|
|
||||||
else 50 +
|
|
||||||
if false then 10
|
|
||||||
else 20
|
|
||||||
|
|
||||||
ok result is 70
|
|
||||||
|
|
||||||
if 'tight formatting with leading `then` is allowed'
|
|
||||||
then ok 'yay'
|
|
||||||
else throw 'boo'
|
|
||||||
|
|
||||||
# Issue #738
|
|
||||||
func = if true then -> 1
|
|
||||||
eq func(), 1
|
|
||||||
|
|
||||||
|
|
||||||
# Issue #748. Trailing reserved identifiers.
|
|
||||||
obj = delete: true
|
|
||||||
|
|
||||||
result = if obj.delete
|
|
||||||
101
|
|
||||||
|
|
||||||
ok result is 101
|
|
Loading…
Add table
Reference in a new issue