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-08-14 20:33:20 +00:00
|
|
|
ok (non ? existent ? variables ? 1) is 1
|
|
|
|
|
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-07-25 05:23:37 +00:00
|
|
|
obj = {
|
2010-01-24 04:30:55 +00:00
|
|
|
prop: "hello"
|
|
|
|
}
|
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok obj?.prop is "hello"
|
2010-01-24 04:30:55 +00:00
|
|
|
|
2010-02-24 05:06:01 +00:00
|
|
|
ok obj?['prop'] is "hello"
|
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok obj.prop?.length is 5
|
2010-02-02 15:39:44 +00:00
|
|
|
|
2010-02-24 05:06:01 +00:00
|
|
|
ok obj?['prop']?['length'] is 5
|
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok obj?.prop?.non?.existent?.property is undefined
|
2010-01-24 17:52:15 +00:00
|
|
|
|
2010-02-24 05:06:01 +00:00
|
|
|
ok obj?['non']?['existent'].property is undefined
|
|
|
|
|
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-02-17 00:45:25 +00:00
|
|
|
ok arr.pop()?.length is 4
|
|
|
|
ok arr.pop()?.length is 2
|
|
|
|
ok arr.pop()?.length is undefined
|
|
|
|
ok arr[0]?.length is undefined
|
|
|
|
ok arr.pop()?.length?.non?.existent()?.property is undefined
|
2010-02-21 01:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Soaks method calls safely.
|
2010-07-25 05:23:37 +00:00
|
|
|
value = undefined
|
|
|
|
result = value?.toString().toLowerCase()
|
2010-02-21 01:00:07 +00:00
|
|
|
|
|
|
|
ok result is undefined
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
value = 10
|
|
|
|
result = value?.toString().toLowerCase()
|
2010-02-21 01:00:07 +00:00
|
|
|
|
|
|
|
ok result is '10'
|
|
|
|
|
2010-09-11 12:39:14 +00:00
|
|
|
ok(process.exit.nothing?.property() or 101)
|
|
|
|
|
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
|
|
|
# Soaks inner values.
|
|
|
|
ident = (obj) -> obj
|
|
|
|
ok ident(non?.existent().method()) is undefined
|
|
|
|
|
|
|
|
|
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
|
|
|
|
constructor: -> a += 1
|
|
|
|
bar: "bat"
|
|
|
|
|
|
|
|
ok (new Foo())?.bar is 'bat'
|
|
|
|
ok a is 1
|
|
|
|
|
|
|
|
|
2010-02-23 03:11:47 +00:00
|
|
|
# Safely existence test on soaks.
|
2010-07-25 05:23:37 +00:00
|
|
|
result = not value?.property?
|
2010-02-23 03:11:47 +00:00
|
|
|
ok result
|
|
|
|
|
2010-05-05 09:58:48 +00:00
|
|
|
|
|
|
|
# Safely calls values off of non-existent variables.
|
2010-07-25 05:23:37 +00:00
|
|
|
result = nothing?.value
|
2010-05-05 09:58:48 +00:00
|
|
|
ok result is undefined
|
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
|
|
|
|
|
|
|
ok plus1?(41) is 42
|
|
|
|
ok (plus1? 41) is 42
|
2010-08-28 13:00:04 +00:00
|
|
|
ok plus2?(41) is undefined
|
|
|
|
ok (plus2? 41) is undefined
|
2010-09-18 10:27:14 +00:00
|
|
|
ok obj.returnThis?() is obj
|
2010-09-18 14:36:48 +00:00
|
|
|
ok obj.counter().counter().returnThis?() is obj
|
|
|
|
ok count is 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
|
|
|
|
|
|
|
ok maybe_close(plus1, 41)?() is 42
|
|
|
|
ok (maybe_close plus1, 41)?() is 42
|
2010-08-28 13:00:04 +00:00
|
|
|
ok (maybe_close 'string', 41)?() is undefined
|
2010-08-25 22:54:42 +00:00
|
|
|
|
2010-08-28 13:00:04 +00:00
|
|
|
ok 2?(3) is undefined
|