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

299 lines
6.5 KiB
CoffeeScript
Raw Normal View History

2010-12-29 00:48:54 -05:00
# Operators
# ---------
2010-12-29 14:06:57 -05:00
# * Operators
# * Existential Operator (Binary)
# * Existential Operator (Unary)
# * Aliased Operators
# * [not] in/of
# * Chained Comparison
2010-12-29 00:48:54 -05:00
test "binary (2-ary) math operators do not require spaces", ->
a = 1
b = -1
eq +1, a*-b
eq -1, a*+b
eq +1, a/-b
eq -1, a/+b
test "operators should respect new lines as spaced", ->
a = 123 +
456
eq 579, a
b = "1#{2}3" +
"456"
eq '123456', b
test "multiple operators should space themselves", ->
eq (+ +1), (- -1)
test "compound operators on successive lines", ->
a = 1
a +=
1
eq a, 2
2010-12-29 00:48:54 -05:00
test "bitwise operators", ->
eq 2, (10 & 3)
eq 11, (10 | 3)
eq 9, (10 ^ 3)
eq 80, (10 << 3)
eq 1, (10 >> 3)
eq 1, (10 >>> 3)
num = 10; eq 2, (num &= 3)
num = 10; eq 11, (num |= 3)
num = 10; eq 9, (num ^= 3)
num = 10; eq 80, (num <<= 3)
num = 10; eq 1, (num >>= 3)
num = 10; eq 1, (num >>>= 3)
test "`instanceof`", ->
ok new String instanceof String
ok new Boolean instanceof Boolean
# `instanceof` supports negation by prefixing the operator with `not`
ok new Number not instanceof String
ok new Array not instanceof Boolean
test "use `::` operator on keywords `this` and `@`", ->
nonce = {}
obj =
withAt: -> @::prop
withThis: -> this::prop
obj.prototype = prop: nonce
eq nonce, obj.withAt()
eq nonce, obj.withThis()
2010-12-29 00:48:54 -05:00
2011-03-11 21:41:12 -05:00
# Existential Operator (Binary)
2010-12-29 00:48:54 -05:00
2011-01-03 04:17:00 -05:00
test "binary existential operator", ->
2010-12-29 00:48:54 -05:00
nonce = {}
2011-01-03 04:17:00 -05:00
b = a ? nonce
eq nonce, b
2010-12-29 00:48:54 -05:00
2011-01-03 04:17:00 -05:00
a = null
b = undefined
b = a ? nonce
eq nonce, b
2010-12-29 00:48:54 -05:00
2011-01-03 04:17:00 -05:00
a = false
b = a ? nonce
eq false, b
2010-12-29 00:48:54 -05:00
2011-01-03 04:17:00 -05:00
a = 0
b = a ? nonce
eq 0, b
2010-12-29 00:48:54 -05:00
2011-01-03 04:17:00 -05:00
test "binary existential operator conditionally evaluates second operand", ->
i = 1
func = -> i -= 1
result = func() ? func()
eq result, 0
2010-12-29 00:48:54 -05:00
2011-01-03 04:17:00 -05:00
test "binary existential operator with negative number", ->
a = null ? - 1
eq -1, a
2010-12-29 00:48:54 -05:00
2011-03-11 21:41:12 -05:00
# Existential Operator (Unary)
2010-12-29 00:48:54 -05:00
2011-01-03 04:17:00 -05:00
test "postfix existential operator", ->
ok (if nonexistent? then false else true)
defined = true
ok defined?
defined = false
ok defined?
2010-12-29 00:48:54 -05:00
2011-01-03 04:17:00 -05:00
test "postfix existential operator only evaluates its operand once", ->
semaphore = 0
fn = ->
ok false if semaphore
++semaphore
ok(if fn()? then true else false)
2010-12-29 00:48:54 -05:00
2011-01-03 04:17:00 -05:00
test "negated postfix existential operator", ->
ok !nothing?.value
2010-12-29 00:48:54 -05:00
2011-01-03 04:17:00 -05:00
test "postfix existential operator on expressions", ->
eq true, (1 or 0)?, true
2010-12-29 00:48:54 -05:00
2011-03-11 21:41:12 -05:00
# `is`,`isnt`,`==`,`!=`
2010-12-29 00:48:54 -05:00
test "`==` and `is` should be interchangeable", ->
a = b = 1
ok a is 1 and b == 1
ok a == b
ok a is b
test "`!=` and `isnt` should be interchangeable", ->
a = 0
b = 1
ok a isnt 1 and b != 0
ok a != b
ok a isnt b
2011-03-11 21:41:12 -05:00
# [not] in/of
2010-12-29 00:48:54 -05:00
# - `in` should check if an array contains a value using `indexOf`
# - `of` should check if a property is defined on an object using `in`
test "in, of", ->
arr = [1]
ok 0 of arr
ok 1 in arr
# prefixing `not` to `in and `of` should negate them
ok 1 not of arr
ok 0 not in arr
test "`in` should be able to operate on an array literal", ->
ok 2 in [0, 1, 2, 3]
ok 4 not in [0, 1, 2, 3]
arr = [0, 1, 2, 3]
ok 2 in arr
ok 4 not in arr
# should cache the value used to test the array
arr = [0]
val = 0
ok val++ in arr
ok val++ not in arr
val = 0
ok val++ of arr
ok val++ not of arr
test "`of` and `in` should be able to operate on instance variables", ->
obj = {
list: [2,3]
in_list: (value) -> value in @list
not_in_list: (value) -> value not in @list
of_list: (value) -> value of @list
not_of_list: (value) -> value not of @list
}
ok obj.in_list 3
ok obj.not_in_list 1
ok obj.of_list 0
ok obj.not_of_list 2
test "#???: `in` with cache and `__indexOf` should work in argument lists", ->
eq 1, [Object() in Array()].length
test "#737: `in` should have higher precedence than logical operators", ->
eq 1, 1 in [1] and 1
test "#768: `in` should preserve evaluation order", ->
share = 0
a = -> share++ if share is 0
b = -> share++ if share is 1
c = -> share++ if share is 2
ok a() not in [b(),c()]
eq 3, share
2011-03-27 15:35:29 -04:00
test "#1099: empty array after `in` should compile to `false`", ->
2011-03-27 23:42:49 -04:00
eq 1, [5 in []].length
eq false, do -> return 0 in []
test "#1354: optimized `in` checks should not happen when splats are present", ->
a = [6, 9]
eq 9 in [3, a...], true
2011-03-27 15:35:29 -04:00
test "#1100: precedence in or-test compilation of `in`", ->
ok 0 in [1 and 0]
ok 0 in [1, 1 and 0]
ok not (0 in [1, 0 or 1])
test "#1630: `in` should check `hasOwnProperty`", ->
ok undefined not in length: 1
test "#1714: lexer bug with raw range `for` followed by `in`", ->
2011-09-21 00:21:46 -04:00
0 for [1..2]
ok not ('a' in ['b'])
2011-09-21 00:21:46 -04:00
0 for [1..2]; ok not ('a' in ['b'])
2011-09-21 00:21:46 -04:00
0 for [1..10] # comment ending
ok not ('a' in ['b'])
test "#1099: statically determined `not in []` reporting incorrect result", ->
ok 0 not in []
2010-12-29 00:48:54 -05:00
2011-03-11 21:41:12 -05:00
# Chained Comparison
2010-12-29 00:48:54 -05:00
test "chainable operators", ->
ok 100 > 10 > 1 > 0 > -1
ok -1 < 0 < 1 < 10 < 100
test "`is` and `isnt` may be chained", ->
ok true is not false is true is not false
ok 0 is 0 isnt 1 is 1
test "different comparison operators (`>`,`<`,`is`,etc.) may be combined", ->
ok 1 < 2 > 1
ok 10 < 20 > 2+3 is 5
test "some chainable operators can be negated by `unless`", ->
ok (true unless 0==10!=100)
test "operator precedence: `|` lower than `<`", ->
eq 1, 1 | 2 < 3 < 4
test "preserve references", ->
a = b = c = 1
# `a == b <= c` should become `a === b && b <= c`
# (this test does not seem to test for this)
ok a == b <= c
test "chained operations should evaluate each value only once", ->
a = 0
ok 1 > a++ < 1
test "#891: incorrect inversion of chained comparisons", ->
ok (true unless 0 > 1 > 2)
ok (true unless (NaN = 0/0) < 0/0 < NaN)
2011-08-10 22:00:32 -04:00
test "#1234: Applying a splat to :: applies the splat to the wrong object", ->
nonce = {}
class C
method: -> @nonce
nonce: nonce
2011-08-10 22:00:32 -04:00
arr = []
eq nonce, C::method arr... # should be applied to `C::`
test "#1102: String literal prevents line continuation", ->
eq "': '", '' +
"': '"
2011-09-17 22:21:47 -04:00
test "#1703, ---x is invalid JS", ->
x = 2
eq (- --x), -1
2011-12-24 07:04:34 -05:00
2011-12-18 13:21:00 -05:00
test "Regression with implicit calls against an indented assignment", ->
2011-12-24 07:04:34 -05:00
eq 1, a =
2011-12-14 11:32:40 -05:00
1
2011-12-24 07:04:34 -05:00
2011-12-14 11:32:40 -05:00
eq a, 1
test "#2155 ... conditional assignment to a closure", ->
x = null
func = -> x ?= (-> if true then 'hi')
func()
eq x(), 'hi'
test "#2197: Existential existential double trouble", ->
counter = 0
func = -> counter++
func()? ? 100
eq counter, 1
test "#2567: Optimization of negated existential produces correct result", ->
a = 1
ok !(!a?)
ok !b?
test "#2508: Existential access of the prototype", ->
eq NonExistent?::nothing, undefined
ok Object?::toString