2013-11-29 23:58:26 -05:00
|
|
|
# Generators
|
|
|
|
# -----------------
|
2013-12-19 17:21:14 -05:00
|
|
|
#
|
2013-11-29 23:58:26 -05:00
|
|
|
# * Generator Definition
|
|
|
|
|
2013-12-19 21:08:25 -05:00
|
|
|
test "generator as argument", ->
|
2014-09-06 06:55:27 -04:00
|
|
|
ok -> yield 0
|
2013-11-29 23:58:26 -05:00
|
|
|
|
2013-12-19 21:08:25 -05:00
|
|
|
test "generator definition", ->
|
2014-09-06 11:25:44 -04:00
|
|
|
x = do ->
|
2013-11-30 15:26:32 -05:00
|
|
|
yield 0
|
|
|
|
yield 1
|
|
|
|
yield 2
|
2014-09-06 11:25:44 -04:00
|
|
|
y = x.next()
|
|
|
|
ok y.value is 0 and y.done is false
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is 1 and y.done is false
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is 2 and y.done is false
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is undefined and y.done is true
|
2013-12-19 17:21:14 -05:00
|
|
|
|
|
|
|
test "bound generator", ->
|
|
|
|
obj =
|
|
|
|
bound: ->
|
2014-09-06 06:55:27 -04:00
|
|
|
do =>
|
|
|
|
yield this
|
2013-12-19 17:21:14 -05:00
|
|
|
unbound: ->
|
2014-09-06 06:55:27 -04:00
|
|
|
do ->
|
|
|
|
yield this
|
2013-12-19 17:21:14 -05:00
|
|
|
nested: ->
|
2014-09-06 06:55:27 -04:00
|
|
|
do =>
|
|
|
|
yield do =>
|
|
|
|
yield do =>
|
|
|
|
yield this
|
2013-12-19 17:21:14 -05:00
|
|
|
|
2013-12-19 21:08:25 -05:00
|
|
|
eq obj, obj.bound().next().value
|
|
|
|
ok obj isnt obj.unbound().next().value
|
|
|
|
eq obj, obj.nested().next().value.next().value.next().value
|
2014-09-06 11:25:44 -04:00
|
|
|
|
|
|
|
test "error if `yield` occurs outside of a function", ->
|
|
|
|
throws -> CoffeeScript.compile 'yield 1'
|
|
|
|
|
|
|
|
test "error if `yield from` occurs outside of a function", ->
|
|
|
|
throws -> CoffeeScript.compile 'yield from 1'
|
|
|
|
|
|
|
|
test "`yield from` support", ->
|
|
|
|
x = do ->
|
|
|
|
yield from do ->
|
|
|
|
yield i for i in [3..4]
|
|
|
|
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is 3 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 1
|
|
|
|
ok y.value is 4 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 2
|
|
|
|
arrayEq y.value, [1, 2]
|
|
|
|
ok y.done is true
|
|
|
|
|
|
|
|
test "empty generator", ->
|
|
|
|
x = do -> yield return
|
|
|
|
|
|
|
|
y = x.next()
|
2014-10-12 21:32:02 -04:00
|
|
|
ok y.value is undefined and y.done is true
|
2014-09-06 11:25:44 -04:00
|
|
|
|
|
|
|
test "`yield` by itself not at the end of a function errors", ->
|
|
|
|
throws -> CoffeeScript.compile 'x = -> yield; return'
|
|
|
|
|
|
|
|
test "`yield from` at the end of a function errors", ->
|
|
|
|
throws -> CoffeeScript.compile 'x = -> x = 1; yield from'
|
|
|
|
|
|
|
|
test "yield in if statements", ->
|
|
|
|
x = do -> if 1 is yield 2 then 3 else 4
|
|
|
|
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is 2 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 1
|
|
|
|
ok y.value is 3 and y.done is true
|
2014-10-12 13:08:28 -04:00
|
|
|
|
|
|
|
test "symbolic operators has precedence over the `yield`", ->
|
2014-10-12 15:32:02 -04:00
|
|
|
symbolic = '+ - * / << >> & | || && ** ^ // or and'.split ' '
|
2014-10-12 13:08:28 -04:00
|
|
|
compound = ("#{op}=" for op in symbolic)
|
|
|
|
relations = '< > == != <= >= is isnt'.split ' '
|
|
|
|
|
2014-10-12 15:32:02 -04:00
|
|
|
operators = [symbolic..., '=', compound..., relations...]
|
2014-10-12 13:08:28 -04:00
|
|
|
|
2014-10-12 15:32:02 -04:00
|
|
|
collect = (gen) -> ref.value until (ref = gen.next()).done
|
2014-10-12 13:08:28 -04:00
|
|
|
|
|
|
|
values = [0, 1, 2, 3]
|
|
|
|
for op in operators
|
2014-10-12 15:32:02 -04:00
|
|
|
expression = "i #{op} 2"
|
|
|
|
|
2014-11-21 17:14:53 -05:00
|
|
|
yielded = CoffeeScript.eval "(arr) -> yield #{expression} for i in arr"
|
|
|
|
mapped = CoffeeScript.eval "(arr) -> (#{expression} for i in arr)"
|
2014-10-12 15:32:02 -04:00
|
|
|
|
2014-11-21 17:14:53 -05:00
|
|
|
arrayEq mapped(values), collect yielded values
|
2014-10-12 15:32:02 -04:00
|
|
|
|
2014-11-21 17:14:53 -05:00
|
|
|
test "for loop expressions", ->
|
|
|
|
x = do ->
|
|
|
|
y = for i in [1..3]
|
|
|
|
yield i * 2
|
|
|
|
|
|
|
|
z = x.next()
|
|
|
|
ok z.value is 2 and z.done is false
|
|
|
|
z = x.next 10
|
|
|
|
ok z.value is 4 and z.done is false
|
|
|
|
z = x.next 20
|
|
|
|
ok z.value is 6 and z.done is false
|
|
|
|
z = x.next 30
|
|
|
|
arrayEq z.value, [10, 20, 30]
|
|
|
|
ok z.done is true
|
|
|
|
|
|
|
|
test "switch expressions", ->
|
|
|
|
x = do ->
|
|
|
|
y = switch yield 1
|
|
|
|
when 2 then yield 1337
|
|
|
|
|
|
|
|
z = x.next()
|
|
|
|
ok z.value is 1 and z.done is false
|
|
|
|
z = x.next 2
|
|
|
|
ok z.value is 1337 and z.done is false
|
|
|
|
z = x.next 3
|
|
|
|
ok z.value is 3 and z.done is true
|