2010-06-12 23:05:13 +00:00
|
|
|
ok(if mySpecialVariable? then false else true)
|
2010-01-01 17:41:55 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
mySpecialVariable = false
|
2010-01-01 17:41:55 +00:00
|
|
|
|
2010-06-12 23:05:13 +00:00
|
|
|
ok(if mySpecialVariable? then true else false)
|
2010-01-17 04:03:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Existential assignment.
|
2010-07-25 05:23:37 +00:00
|
|
|
a = 5
|
|
|
|
a = null
|
2010-01-17 04:03:54 +00:00
|
|
|
a ?= 10
|
|
|
|
b ?= 10
|
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok a is 10 and b is 10
|
2010-01-17 04:03:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
# The existential operator.
|
2010-07-25 05:23:37 +00:00
|
|
|
z = null
|
|
|
|
x = z ? "EX"
|
2010-02-17 00:45:25 +00:00
|
|
|
ok z is null and x is "EX"
|
2010-01-17 19:23:41 +00:00
|
|
|
|
2010-08-14 20:24:05 +00:00
|
|
|
i = 9
|
|
|
|
func = -> i += 1
|
|
|
|
result = func() ? 101
|
|
|
|
ok result is 10
|
|
|
|
|
2010-01-17 19:23:41 +00:00
|
|
|
# Only evaluate once.
|
2010-07-25 05:23:37 +00:00
|
|
|
counter = 0
|
|
|
|
getNextNode = ->
|
2010-01-17 19:23:41 +00:00
|
|
|
throw "up" if counter
|
|
|
|
counter++
|
|
|
|
|
2010-06-12 23:05:13 +00:00
|
|
|
ok(if getNextNode()? then true else false)
|
2010-01-24 04:30:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Existence chains, soaking up undefined properties:
|
2010-10-01 22:17:35 +00:00
|
|
|
obj =
|
2010-01-24 04:30:55 +00:00
|
|
|
prop: "hello"
|
2010-02-02 15:39:44 +00:00
|
|
|
|
2010-10-01 22:17:35 +00:00
|
|
|
eq obj?.prop, "hello"
|
|
|
|
eq obj?['prop'], "hello"
|
|
|
|
eq obj.prop?.length, 5
|
|
|
|
eq obj?.prop?['length'], 5
|
2010-10-02 00:52:23 +00:00
|
|
|
eq obj?.prop?.non?.existent?.property, undefined
|
2010-02-24 05:06:01 +00:00
|
|
|
|
2010-01-24 17:52:15 +00:00
|
|
|
|
|
|
|
# Soaks and caches method calls as well.
|
2010-07-25 05:23:37 +00:00
|
|
|
arr = ["--", "----"]
|
2010-01-24 17:52:15 +00:00
|
|
|
|
2010-10-01 22:17:35 +00:00
|
|
|
eq arr.pop()?.length, 4
|
|
|
|
eq arr.pop()?.length, 2
|
2010-10-02 00:52:23 +00:00
|
|
|
eq arr.pop()?.length, undefined
|
|
|
|
eq arr.pop()?.length?.non?.existent()?.property, undefined
|
2010-02-21 01:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Soaks method calls safely.
|
2010-10-01 22:17:35 +00:00
|
|
|
value = null
|
2010-10-02 00:52:23 +00:00
|
|
|
eq value?.toString().toLowerCase(), undefined
|
2010-02-21 01:00:07 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
value = 10
|
2010-10-01 22:17:35 +00:00
|
|
|
eq value?.toString().toLowerCase(), '10'
|
2010-02-21 01:00:07 +00:00
|
|
|
|
2010-10-11 17:04:51 +00:00
|
|
|
eq 0.nothing?.property() or 101, 101
|
2010-09-11 12:39:14 +00:00
|
|
|
|
2010-09-12 20:18:05 +00:00
|
|
|
counter = 0
|
|
|
|
func = ->
|
|
|
|
counter += 1
|
|
|
|
'prop'
|
|
|
|
obj =
|
|
|
|
prop: -> this
|
|
|
|
value: 25
|
|
|
|
|
|
|
|
ok obj[func()]()[func()]()[func()]()?.value is 25
|
|
|
|
ok counter is 3
|
|
|
|
|
2010-02-23 03:11:47 +00:00
|
|
|
|
2010-08-14 15:29:10 +00:00
|
|
|
ident = (obj) -> obj
|
2010-10-02 00:52:23 +00:00
|
|
|
eq ident(non?.existent().method()), undefined, 'soaks inner values'
|
2010-08-14 15:29:10 +00:00
|
|
|
|
|
|
|
|
2010-07-15 13:08:51 +00:00
|
|
|
# Soaks constructor invocations.
|
2010-07-25 05:23:37 +00:00
|
|
|
a = 0
|
2010-07-15 13:08:51 +00:00
|
|
|
class Foo
|
2010-11-14 19:21:55 +00:00
|
|
|
constructor: -> a += 1
|
2010-07-15 13:08:51 +00:00
|
|
|
bar: "bat"
|
|
|
|
|
|
|
|
ok (new Foo())?.bar is 'bat'
|
|
|
|
ok a is 1
|
|
|
|
|
|
|
|
|
2010-10-01 22:17:35 +00:00
|
|
|
ok not value?.property?, 'safely checks existence on soaks'
|
2010-02-23 03:11:47 +00:00
|
|
|
|
2010-05-05 09:58:48 +00:00
|
|
|
|
2010-10-02 00:52:23 +00:00
|
|
|
eq nothing?.value, undefined, 'safely calls values off of non-existent variables'
|
2010-10-09 22:10:20 +00:00
|
|
|
eq !nothing?.value and 1, 1, 'corresponding operators work as expected'
|
2010-07-10 15:27:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Assign to the result of an exsitential operation with a minus.
|
2010-07-25 05:23:37 +00:00
|
|
|
x = null ? - 1
|
2010-07-10 15:27:43 +00:00
|
|
|
ok x is - 1
|
2010-07-16 01:38:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Things that compile to ternaries should force parentheses, like operators do.
|
2010-07-25 05:23:37 +00:00
|
|
|
duration = if options?.animated then 150 else 0
|
2010-07-16 01:38:35 +00:00
|
|
|
ok duration is 0
|
2010-08-25 22:54:42 +00:00
|
|
|
|
|
|
|
|
2010-09-18 10:27:14 +00:00
|
|
|
# Function soaks.
|
2010-08-25 22:54:42 +00:00
|
|
|
plus1 = (x) -> x + 1
|
2010-09-18 14:36:48 +00:00
|
|
|
count = 0
|
2010-09-18 10:27:14 +00:00
|
|
|
obj = {
|
2010-09-18 14:36:48 +00:00
|
|
|
counter: -> count += 1; this
|
2010-09-18 10:27:14 +00:00
|
|
|
returnThis: -> this
|
|
|
|
}
|
2010-08-25 22:54:42 +00:00
|
|
|
|
2010-10-17 04:19:51 +00:00
|
|
|
eq plus1?(41), 42
|
|
|
|
eq (plus1? 41), 42
|
|
|
|
eq plus2?(41), undefined
|
|
|
|
eq (plus2? 41), undefined
|
|
|
|
eq obj.returnThis?(), obj
|
|
|
|
eq obj.returnSelf?(), undefined
|
|
|
|
eq obj.returnThis?().flag = on, on
|
|
|
|
eq obj.returnSelf?().flag = on, undefined
|
|
|
|
eq obj.counter().counter().returnThis?(), obj
|
|
|
|
eq count, 2
|
2010-08-28 13:00:04 +00:00
|
|
|
|
|
|
|
maybe_close = (f, arg) -> if typeof f is 'function' then () -> f(arg) else -1
|
2010-08-25 22:54:42 +00:00
|
|
|
|
2010-10-17 04:19:51 +00:00
|
|
|
eq maybe_close(plus1, 41)?(), 42
|
|
|
|
eq (maybe_close plus1, 41)?(), 42
|
|
|
|
eq (maybe_close 'string', 41)?(), undefined
|
2010-08-25 22:54:42 +00:00
|
|
|
|
2010-10-01 22:17:35 +00:00
|
|
|
eq 2?(3), undefined
|
2010-10-17 04:19:51 +00:00
|
|
|
eq new Number?(42) | 0, 42
|
|
|
|
eq new Bumper?(42) | 0, 0
|
2010-10-01 22:17:35 +00:00
|
|
|
|
2010-10-20 00:42:12 +00:00
|
|
|
|
2010-10-01 22:17:35 +00:00
|
|
|
#726
|
2010-10-02 00:52:23 +00:00
|
|
|
eq calendar?[Date()], undefined
|
2010-09-28 03:23:08 +00:00
|
|
|
|
2010-10-20 00:42:12 +00:00
|
|
|
|
2010-10-01 22:17:35 +00:00
|
|
|
#733
|
|
|
|
a = b: {c: null}
|
|
|
|
eq a.b?.c?(), undefined
|
2010-10-20 00:42:12 +00:00
|
|
|
|
2010-10-01 22:17:35 +00:00
|
|
|
a.b?.c or= (it) -> it
|
|
|
|
eq a.b?.c?(1), 1
|
|
|
|
eq a.b?.c?([2, 3]...), 2
|
2010-10-20 00:42:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
#756
|
|
|
|
a = null
|
|
|
|
ok isNaN a?.b.c + 1
|
|
|
|
eq undefined, a?.b.c += 1
|
|
|
|
eq undefined, ++a?.b.c
|
|
|
|
eq undefined, delete a?.b.c
|
|
|
|
|
|
|
|
a = b: {c: 0}
|
|
|
|
eq 1, a?.b.c + 1
|
|
|
|
eq 1, a?.b.c += 1
|
|
|
|
eq 2, ++a?.b.c
|
|
|
|
eq yes, delete a?.b.c
|
2010-11-09 07:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
eq (1 or 0)?, true, 'postfix `?` should unwrap correctly'
|