@@ -7,23 +7,18 @@ test "generator as argument", ->
7
7
ok -> yield 0
8
8
9
9
test " generator definition" , ->
10
- x = ->
10
+ x = do ->
11
11
yield 0
12
12
yield 1
13
13
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
27
22
28
23
test " bound generator" , ->
29
24
obj =
@@ -42,3 +37,45 @@ test "bound generator", ->
42
37
eq obj, obj .bound ().next ().value
43
38
ok obj isnt obj .unbound ().next ().value
44
39
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