Skip to content

Commit efca286

Browse files
committed
added tests for yield, yield from, yield return and yield in if statements
1 parent 781ea22 commit efca286

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

test/generators.coffee

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,18 @@ test "generator as argument", ->
77
ok -> yield 0
88

99
test "generator definition", ->
10-
x = ->
10+
x = do ->
1111
yield 0
1212
yield 1
1313
yield 2
14-
y = x()
15-
z = y.next()
16-
eq z.value, 0
17-
eq z.done, false
18-
z = y.next()
19-
eq z.value, 1
20-
eq z.done, false
21-
z = y.next()
22-
eq z.value, 2
23-
eq z.done, false
24-
z = y.next()
25-
eq z.value, undefined
26-
eq z.done, true
14+
y = x.next()
15+
ok y.value is 0 and y.done is false
16+
y = x.next()
17+
ok y.value is 1 and y.done is false
18+
y = x.next()
19+
ok y.value is 2 and y.done is false
20+
y = x.next()
21+
ok y.value is undefined and y.done is true
2722

2823
test "bound generator", ->
2924
obj =
@@ -42,3 +37,45 @@ test "bound generator", ->
4237
eq obj, obj.bound().next().value
4338
ok obj isnt obj.unbound().next().value
4439
eq obj, obj.nested().next().value.next().value.next().value
40+
41+
test "error if `yield` occurs outside of a function", ->
42+
throws -> CoffeeScript.compile 'yield 1'
43+
44+
test "error if `yield from` occurs outside of a function", ->
45+
throws -> CoffeeScript.compile 'yield from 1'
46+
47+
test "`yield from` support", ->
48+
x = do ->
49+
yield from do ->
50+
yield i for i in [3..4]
51+
52+
y = x.next()
53+
ok y.value is 3 and y.done is false
54+
55+
y = x.next 1
56+
ok y.value is 4 and y.done is false
57+
58+
y = x.next 2
59+
arrayEq y.value, [1, 2]
60+
ok y.done is true
61+
62+
test "empty generator", ->
63+
x = do -> yield return
64+
65+
y = x.next()
66+
ok y.value is undefined and y.done is true
67+
68+
test "`yield` by itself not at the end of a function errors", ->
69+
throws -> CoffeeScript.compile 'x = -> yield; return'
70+
71+
test "`yield from` at the end of a function errors", ->
72+
throws -> CoffeeScript.compile 'x = -> x = 1; yield from'
73+
74+
test "yield in if statements", ->
75+
x = do -> if 1 is yield 2 then 3 else 4
76+
77+
y = x.next()
78+
ok y.value is 2 and y.done is false
79+
80+
y = x.next 1
81+
ok y.value is 3 and y.done is true

0 commit comments

Comments
 (0)