fancy 0.3.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +14 -14
- data/Rakefile +16 -4
- data/bin/fancy +3 -0
- data/bin/fspec +20 -0
- data/bin/fyi +27 -11
- data/bin/ifancy +1 -1
- data/boot/fancy_ext.rb +1 -0
- data/boot/fancy_ext/block_env.rb +1 -9
- data/boot/fancy_ext/delegator.rb +64 -0
- data/boot/fancy_ext/object.rb +4 -0
- data/boot/fancy_ext/thread.rb +4 -0
- data/boot/load.rb +5 -1
- data/boot/rbx-compiler/compiler/ast.rb +0 -1
- data/boot/rbx-compiler/compiler/ast/class_def.rb +2 -0
- data/boot/rbx-compiler/compiler/ast/method_def.rb +2 -0
- data/boot/rbx-compiler/compiler/ast/node.rb +10 -0
- data/boot/rbx-compiler/compiler/ast/ruby_args.rb +12 -0
- data/boot/rbx-compiler/compiler/ast/singleton_method_def.rb +2 -0
- data/boot/rbx-compiler/parser/fancy_parser.bundle +0 -0
- data/boot/rbx-compiler/parser/lexer.lex +5 -11
- data/boot/rbx-compiler/parser/parser.rb +16 -5
- data/boot/rbx-compiler/parser/parser.y +39 -24
- data/doc/api/fancy.css +1 -1
- data/doc/api/fancy.jsonp +1 -1
- data/doc/api/fdoc.js +22 -4
- data/doc/api/index.html +5 -6
- data/doc/api/jquery-ui.min.js +401 -0
- data/doc/api/jquery.tools.min.js +192 -0
- data/doc/api/themeswitchertool.js +250 -0
- data/doc/features.md +17 -0
- data/examples/actor_bunnies.fy +32 -0
- data/examples/actors.fy +26 -0
- data/examples/actors_primitive.fy +27 -0
- data/examples/actors_ring.fy +37 -0
- data/examples/armstrong_numbers.fy +1 -1
- data/examples/array.fy +7 -9
- data/examples/async_send.fy +1 -2
- data/examples/blocks.fy +4 -4
- data/examples/call_with_receiver.fy +1 -1
- data/examples/class.fy +1 -1
- data/examples/default_args.fy +4 -1
- data/examples/define_methods.fy +2 -2
- data/examples/echo.fy +1 -1
- data/examples/factorial.fy +1 -1
- data/examples/future_composition.fy +2 -2
- data/examples/futures.fy +0 -5
- data/examples/game_of_life.fy +1 -1
- data/examples/person.fy +1 -1
- data/lib/argv.fy +7 -2
- data/lib/array.fy +109 -42
- data/lib/block.fy +39 -14
- data/lib/boot.fy +2 -0
- data/lib/class.fy +2 -0
- data/lib/compiler/ast.fy +2 -1
- data/lib/compiler/ast/assign.fy +2 -3
- data/lib/compiler/ast/async_send.fy +1 -15
- data/lib/compiler/ast/class_def.fy +2 -1
- data/lib/compiler/ast/expression_list.fy +4 -5
- data/lib/compiler/ast/future_send.fy +1 -10
- data/lib/compiler/ast/goto.fy +46 -0
- data/lib/compiler/ast/identifier.fy +9 -7
- data/lib/compiler/ast/literals.fy +8 -1
- data/lib/compiler/ast/match.fy +14 -4
- data/lib/compiler/ast/message_send.fy +34 -6
- data/lib/compiler/ast/method_def.fy +6 -6
- data/lib/compiler/ast/node.fy +3 -3
- data/lib/compiler/ast/range.fy +1 -0
- data/lib/compiler/ast/script.fy +0 -2
- data/lib/compiler/ast/singleton_method_def.fy +2 -4
- data/lib/compiler/ast/string_interpolation.fy +17 -0
- data/lib/compiler/ast/super.fy +5 -4
- data/lib/compiler/ast/try_catch.fy +8 -6
- data/lib/compiler/ast/tuple_literal.fy +3 -2
- data/lib/compiler/command.fy +0 -1
- data/lib/compiler/compiler.fy +1 -5
- data/lib/compiler/stages.fy +6 -14
- data/lib/documentation.fy +57 -46
- data/lib/enumerable.fy +257 -23
- data/lib/enumerator.fy +122 -15
- data/lib/false_class.fy +10 -1
- data/lib/fancy_spec.fy +263 -61
- data/lib/fdoc.fy +11 -25
- data/lib/fiber.fy +11 -0
- data/lib/file.fy +8 -11
- data/lib/future.fy +84 -5
- data/lib/hash.fy +65 -14
- data/lib/integer.fy +35 -0
- data/lib/iteration.fy +54 -29
- data/lib/message.fy +6 -0
- data/lib/method.fy +0 -16
- data/lib/nil_class.fy +58 -8
- data/lib/number.fy +49 -22
- data/lib/object.fy +371 -65
- data/lib/package.fy +24 -1
- data/lib/package/installer.fy +5 -9
- data/lib/package/specification.fy +2 -2
- data/lib/parser/ext/lexer.lex +15 -11
- data/lib/parser/ext/parser.y +70 -23
- data/lib/parser/methods.fy +33 -14
- data/lib/proxy.fy +33 -3
- data/lib/range.fy +28 -0
- data/lib/rbx.fy +3 -1
- data/lib/rbx/actor.fy +53 -0
- data/lib/rbx/alpha.fy +31 -0
- data/lib/rbx/array.fy +21 -12
- data/lib/rbx/bignum.fy +6 -2
- data/lib/rbx/block.fy +23 -26
- data/lib/rbx/class.fy +54 -2
- data/lib/rbx/code_loader.fy +8 -4
- data/lib/rbx/date.fy +9 -0
- data/lib/rbx/directory.fy +18 -0
- data/lib/rbx/environment_variables.fy +1 -0
- data/lib/rbx/exception.fy +9 -2
- data/lib/rbx/fiber.fy +22 -4
- data/lib/rbx/file.fy +5 -5
- data/lib/rbx/fixnum.fy +5 -0
- data/lib/rbx/float.fy +11 -3
- data/lib/rbx/hash.fy +31 -16
- data/lib/rbx/integer.fy +1 -0
- data/lib/rbx/io.fy +17 -7
- data/lib/rbx/match_data.fy +15 -4
- data/lib/rbx/method.fy +40 -7
- data/lib/rbx/name_error.fy +4 -0
- data/lib/rbx/object.fy +92 -24
- data/lib/rbx/range.fy +20 -6
- data/lib/rbx/regexp.fy +11 -3
- data/lib/rbx/string.fy +51 -1
- data/lib/rbx/stringio.fy +17 -0
- data/lib/rbx/symbol.fy +15 -1
- data/lib/rbx/system.fy +20 -2
- data/lib/rbx/tcp_server.fy +4 -1
- data/lib/rbx/tcp_socket.fy +11 -0
- data/lib/rbx/time.fy +6 -0
- data/lib/rbx/tuple.fy +14 -5
- data/lib/set.fy +144 -29
- data/lib/stack.fy +42 -11
- data/lib/string.fy +118 -8
- data/lib/struct.fy +13 -3
- data/lib/symbol.fy +21 -2
- data/lib/thread_pool.fy +2 -1
- data/lib/true_class.fy +45 -7
- data/lib/tuple.fy +27 -9
- data/lib/version.fy +2 -2
- data/ruby_lib/fancy +43 -0
- data/ruby_lib/fdoc +23 -0
- data/ruby_lib/fspec +3 -0
- data/ruby_lib/fyi +3 -0
- data/ruby_lib/ifancy +3 -0
- data/tests/argv.fy +5 -9
- data/tests/array.fy +323 -196
- data/tests/assignment.fy +29 -29
- data/tests/block.fy +72 -59
- data/tests/class.fy +227 -138
- data/tests/control_flow.fy +83 -51
- data/tests/documentation.fy +8 -8
- data/tests/enumerable.fy +8 -0
- data/tests/enumerator.fy +47 -29
- data/tests/exception.fy +49 -32
- data/tests/file.fy +28 -28
- data/tests/fixnum.fy +170 -0
- data/tests/future.fy +24 -7
- data/tests/hash.fy +55 -38
- data/tests/method.fy +50 -43
- data/tests/nil_class.fy +37 -37
- data/tests/object.fy +152 -70
- data/tests/pattern_matching.fy +67 -31
- data/tests/range.fy +6 -6
- data/tests/set.fy +101 -4
- data/tests/stack.fy +14 -5
- data/tests/string.fy +115 -61
- data/tests/stringio.fy +18 -0
- data/tests/struct.fy +27 -0
- data/tests/symbol.fy +19 -6
- data/tests/true_class.fy +34 -34
- data/tests/tuple.fy +30 -12
- metadata +103 -81
- data/boot/rbx-compiler/compiler/ast/require.rb +0 -20
- data/examples/actor.fy +0 -37
- data/examples/curl_async.fy +0 -37
- data/lib/compiler/ast/require.fy +0 -15
- data/tests/number.fy +0 -135
- data/tests/parsing/sexp.fy +0 -50
data/tests/control_flow.fy
CHANGED
|
@@ -1,202 +1,234 @@
|
|
|
1
1
|
FancySpec describe: "Control Flow" with: {
|
|
2
2
|
|
|
3
|
-
it: "
|
|
4
|
-
'foo if_nil: { 'is_nil } .
|
|
5
|
-
"hello, world" if_nil: { 'is_nil } .
|
|
3
|
+
it: "does NOT call the block if not nil" with: 'if_nil: when: {
|
|
4
|
+
'foo if_nil: { 'is_nil } . is: nil
|
|
5
|
+
"hello, world" if_nil: { 'is_nil } . is: nil
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
it: "
|
|
8
|
+
it: "works like if_true:" with: 'if:then: when: {
|
|
9
9
|
if: (4 < 5) then: {
|
|
10
|
-
4 < 5
|
|
10
|
+
4 < 5 is: true
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
it: "
|
|
14
|
+
it: "works like if_true:else: " with: 'if:then:else: when: {
|
|
15
15
|
if: (4 < 5) then: {
|
|
16
|
-
4 < 5
|
|
16
|
+
4 < 5 is: true
|
|
17
17
|
} else: {
|
|
18
|
-
4 < 5
|
|
18
|
+
4 < 5 is: nil
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
it: "
|
|
22
|
+
it: "works like while_true:" with: 'while:do: when: {
|
|
23
23
|
x = 0
|
|
24
24
|
while: { x < 10 } do: {
|
|
25
|
-
x < 10
|
|
25
|
+
x < 10 is: true
|
|
26
26
|
x = x + 1
|
|
27
27
|
}
|
|
28
|
-
x == 10
|
|
28
|
+
x == 10 is: true
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
it: "
|
|
31
|
+
it: "works like while_false: " with: 'until:do: when: {
|
|
32
32
|
x = 0
|
|
33
33
|
until: { x == 10 } do: {
|
|
34
|
-
x < 10
|
|
34
|
+
x < 10 is: true
|
|
35
35
|
x = x + 1
|
|
36
36
|
}
|
|
37
|
-
x == 10
|
|
37
|
+
x == 10 is: true
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
it: "
|
|
40
|
+
it: "calls a block while another one is true, but call it at least once" with: 'do:while: when: {
|
|
41
|
+
x = 0
|
|
42
|
+
arr = []
|
|
43
|
+
do: {
|
|
44
|
+
x < 10 is: true
|
|
45
|
+
arr << x
|
|
46
|
+
x = x + 1
|
|
47
|
+
} while: { x < 10 }
|
|
48
|
+
arr is: [0,1,2,3,4,5,6,7,8,9]
|
|
49
|
+
|
|
50
|
+
times_called = 0
|
|
51
|
+
do: {
|
|
52
|
+
times_called = times_called + 1
|
|
53
|
+
} while: { false }
|
|
54
|
+
times_called is: 1
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
it: "calls a block until another one is true, but call it at least once" with: 'do:until: when: {
|
|
58
|
+
x = 0
|
|
59
|
+
do: {
|
|
60
|
+
x < 10 is: true
|
|
61
|
+
x = x + 1
|
|
62
|
+
} until: { x == 10 }
|
|
63
|
+
x is: 10
|
|
64
|
+
|
|
65
|
+
times_called = 0
|
|
66
|
+
do: {
|
|
67
|
+
times_called = times_called + 1
|
|
68
|
+
} until: { true }
|
|
69
|
+
times_called is: 1
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
it: "works like if_false:: " with: 'unless:do: when: {
|
|
41
73
|
unless: (4 > 5) do: {
|
|
42
|
-
5 > 4
|
|
74
|
+
5 > 4 is: true
|
|
43
75
|
}
|
|
44
76
|
}
|
|
45
77
|
|
|
46
|
-
it: "
|
|
78
|
+
it: "only calls the block if it's a true-ish value" with: 'if_true: when: {
|
|
47
79
|
1 if_true: |num| {
|
|
48
80
|
num * 10
|
|
49
|
-
} .
|
|
81
|
+
} . is: 10
|
|
50
82
|
|
|
51
83
|
nil if_true: {
|
|
52
84
|
"nope"
|
|
53
|
-
} .
|
|
85
|
+
} . is: nil
|
|
54
86
|
|
|
55
87
|
false if_true: {
|
|
56
88
|
"nope again"
|
|
57
|
-
} .
|
|
89
|
+
} . is: nil
|
|
58
90
|
}
|
|
59
91
|
|
|
60
|
-
it: "
|
|
92
|
+
it: "calls the then_block if it's a true-ish value and call the else_block otherwise" with: 'if_true:else: when: {
|
|
61
93
|
1 if_true: |num| {
|
|
62
94
|
num * 10
|
|
63
95
|
} else: {
|
|
64
96
|
nil
|
|
65
|
-
} .
|
|
97
|
+
} . is: 10
|
|
66
98
|
|
|
67
99
|
nil if_true: {
|
|
68
100
|
"nope"
|
|
69
101
|
} else: {
|
|
70
102
|
"yup"
|
|
71
|
-
} .
|
|
103
|
+
} . is: "yup"
|
|
72
104
|
|
|
73
105
|
false if_true: {
|
|
74
106
|
"nope again"
|
|
75
107
|
} else: {
|
|
76
108
|
"yup again"
|
|
77
|
-
} .
|
|
109
|
+
} . is: "yup again"
|
|
78
110
|
}
|
|
79
111
|
|
|
80
|
-
it: "
|
|
81
|
-
class
|
|
112
|
+
it: "is possible to override the if_true:else: method and work accordingly in conditionals" when: {
|
|
113
|
+
class AClassThatIsLikeFalse {
|
|
82
114
|
def if_true: block else: another_block {
|
|
83
115
|
another_block call
|
|
84
116
|
}
|
|
85
117
|
}
|
|
86
118
|
|
|
87
|
-
obj =
|
|
119
|
+
obj = AClassThatIsLikeFalse new
|
|
88
120
|
if: obj then: {
|
|
89
121
|
'fail
|
|
90
122
|
} else: {
|
|
91
123
|
'success
|
|
92
|
-
} .
|
|
124
|
+
} . is: 'success
|
|
93
125
|
|
|
94
126
|
# let's get rid of this custom if_true:else: method
|
|
95
|
-
|
|
127
|
+
AClassThatIsLikeFalse undefine_method: 'if_true:else:
|
|
96
128
|
|
|
97
129
|
if: obj then: {
|
|
98
130
|
'now_this_is_success
|
|
99
131
|
} else: {
|
|
100
132
|
'fail
|
|
101
|
-
} .
|
|
133
|
+
} . is: 'now_this_is_success
|
|
102
134
|
}
|
|
103
135
|
|
|
104
|
-
it: "
|
|
136
|
+
it: "breaks from an iteration" with: 'break when: {
|
|
105
137
|
x = 0
|
|
106
138
|
until: { x == 10 } do: {
|
|
107
139
|
x = x + 1
|
|
108
140
|
{ break } if: (x == 5)
|
|
109
141
|
}
|
|
110
|
-
x == 5
|
|
142
|
+
x == 5 is: true
|
|
111
143
|
}
|
|
112
144
|
|
|
113
|
-
it: "
|
|
145
|
+
it: "breaks from an iteration with return value" with: 'break: when: {
|
|
114
146
|
x = 0
|
|
115
147
|
y = until: { x == 10 } do: {
|
|
116
148
|
x = x + 1
|
|
117
149
|
{ break: 42 } if: (x == 5)
|
|
118
150
|
}
|
|
119
151
|
|
|
120
|
-
x
|
|
121
|
-
y
|
|
152
|
+
x is: 5
|
|
153
|
+
y is: 42
|
|
122
154
|
}
|
|
123
155
|
|
|
124
|
-
it: "
|
|
156
|
+
it: "skips an iteration over a Range" with: 'next when: {
|
|
125
157
|
total = 0
|
|
126
158
|
(1..10) each: |i| {
|
|
127
159
|
{ next } if: (i == 5)
|
|
128
160
|
total = total + i
|
|
129
161
|
}
|
|
130
|
-
total
|
|
162
|
+
total is: 50
|
|
131
163
|
}
|
|
132
164
|
|
|
133
|
-
it: "
|
|
165
|
+
it: "skips an iteration over an Array" with: 'next when: {
|
|
134
166
|
total = 0
|
|
135
167
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] each: |i| {
|
|
136
168
|
{ next } if: (i == 5)
|
|
137
169
|
total = total + i
|
|
138
170
|
}
|
|
139
|
-
total
|
|
171
|
+
total is: 50
|
|
140
172
|
}
|
|
141
173
|
|
|
142
|
-
it: "
|
|
174
|
+
it: "skips an iteration over an Hash" with: 'next when: {
|
|
143
175
|
total = 0
|
|
144
176
|
<['a => 1, 'b => 2, 'c => 3, 'd => 4, 'e => 5, 'f => 6]> each: |k v| {
|
|
145
177
|
{ next } if: (k == 'd)
|
|
146
178
|
total = total + v
|
|
147
179
|
}
|
|
148
|
-
total
|
|
180
|
+
total is: 17
|
|
149
181
|
}
|
|
150
182
|
|
|
151
|
-
it: "stops any loop type at the correct spot"
|
|
183
|
+
it: "stops any loop type at the correct spot" with: 'break when: {
|
|
152
184
|
i = 0
|
|
153
185
|
loop: {
|
|
154
186
|
{ break } if: (i == 3)
|
|
155
187
|
i = i + 1
|
|
156
188
|
}
|
|
157
|
-
i
|
|
189
|
+
i is: 3
|
|
158
190
|
|
|
159
191
|
i = 0
|
|
160
192
|
while: { i < 5 } do: {
|
|
161
193
|
{ break } if: (i == 3)
|
|
162
194
|
i = i + 1
|
|
163
195
|
}
|
|
164
|
-
i
|
|
196
|
+
i is: 3
|
|
165
197
|
|
|
166
198
|
i = 0
|
|
167
199
|
0 upto: 5 do: |n| {
|
|
168
200
|
i = n
|
|
169
201
|
{ break } if: (n == 3)
|
|
170
202
|
}
|
|
171
|
-
i
|
|
203
|
+
i is: 3
|
|
172
204
|
}
|
|
173
205
|
|
|
174
|
-
it: "stops any loop type at the correct spot"
|
|
206
|
+
it: "stops any loop type at the correct spot" with: 'break: when: {
|
|
175
207
|
i = 0
|
|
176
208
|
loop: {
|
|
177
209
|
{ break: i } if: (i == 2)
|
|
178
210
|
i = i + 1
|
|
179
|
-
} .
|
|
211
|
+
} . is: 2
|
|
180
212
|
|
|
181
213
|
i = 0
|
|
182
214
|
while: { i < 5 } do: {
|
|
183
215
|
{ break: i } if: (i == 2)
|
|
184
216
|
i = i + 1
|
|
185
|
-
} .
|
|
217
|
+
} . is: 2
|
|
186
218
|
|
|
187
219
|
i = 0
|
|
188
220
|
0 upto: 5 do: |n| {
|
|
189
221
|
i = n
|
|
190
222
|
{ break: n } if: (n == 2)
|
|
191
223
|
}
|
|
192
|
-
i
|
|
224
|
+
i is: 2
|
|
193
225
|
}
|
|
194
226
|
|
|
195
|
-
it: "
|
|
227
|
+
it: "allows empty try blocks" when: {
|
|
196
228
|
x = "foo"
|
|
197
229
|
try {
|
|
198
230
|
} finally {
|
|
199
|
-
x
|
|
231
|
+
x is: "foo"
|
|
200
232
|
}
|
|
201
233
|
}
|
|
202
234
|
}
|
data/tests/documentation.fy
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
FancySpec describe: "Documentations" with: {
|
|
2
|
-
it: "
|
|
2
|
+
it: "displays the documentation for a method" when: {
|
|
3
3
|
documentation = "Array#each: iterates over its elements, calling a given block with each element."
|
|
4
4
|
Array new method: "each:" . documentation: documentation
|
|
5
|
-
Array new method: "each:" . documentation . docs first
|
|
5
|
+
Array new method: "each:" . documentation . docs first is: documentation
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
it: "
|
|
8
|
+
it: "defines a documenation string for a class and method" when: {
|
|
9
9
|
class ClassWithDoc {
|
|
10
10
|
"This class has a documentation! Yay!"
|
|
11
11
|
def foo {
|
|
@@ -13,12 +13,12 @@ FancySpec describe: "Documentations" with: {
|
|
|
13
13
|
nil
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
-
ClassWithDoc documentation
|
|
17
|
-
ClassWithDoc documentation
|
|
18
|
-
ClassWithDoc new method: 'foo . documentation docs
|
|
16
|
+
ClassWithDoc documentation to_s is_not: ""
|
|
17
|
+
ClassWithDoc documentation to_s is: "This class has a documentation! Yay!"
|
|
18
|
+
ClassWithDoc new method: 'foo . documentation docs is: ["bar!"]
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
it: "
|
|
22
|
-
Array new method: "first" . documentation
|
|
21
|
+
it: "has a documentation string for a method" when: {
|
|
22
|
+
Array new method: "first" . documentation is_not be: 'nil?
|
|
23
23
|
}
|
|
24
24
|
}
|
data/tests/enumerable.fy
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
FancySpec describe: FancyEnumerable with: {
|
|
2
|
+
it: "selects the right element based on a comparison Block and an optional selection Block" with: 'superior_by:taking: when: {
|
|
3
|
+
[1,2,3,4,5] superior_by: '> . is: 5
|
|
4
|
+
[1,2,3,4,5] superior_by: '< . is: 1
|
|
5
|
+
[[1,2], [2,3,4], [], [1]] superior_by: '> taking: 'size . is: [2,3,4]
|
|
6
|
+
[[1,2], [2,3,4], [-1]] superior_by: '< taking: 'first . is: [-1]
|
|
7
|
+
}
|
|
8
|
+
}
|
data/tests/enumerator.fy
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
1
|
FancySpec describe: FancyEnumerator with: {
|
|
2
|
-
it: "iterates with 'next"
|
|
2
|
+
it: "iterates with 'next" with: 'new: when: {
|
|
3
3
|
enum = FancyEnumerator new: (42..50)
|
|
4
|
-
enum next
|
|
5
|
-
enum next
|
|
4
|
+
enum next is: 42
|
|
5
|
+
enum next is: 43
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
it: "peeks to find next element"
|
|
8
|
+
it: "peeks to find next element" with: 'peek when: {
|
|
9
9
|
enum = FancyEnumerator new: (42..50)
|
|
10
|
-
enum peek
|
|
11
|
-
enum peek
|
|
12
|
-
enum next
|
|
10
|
+
enum peek is: 42
|
|
11
|
+
enum peek is: 42
|
|
12
|
+
enum next is: 42
|
|
13
13
|
|
|
14
|
-
enum peek
|
|
15
|
-
enum peek
|
|
16
|
-
enum next
|
|
14
|
+
enum peek is: 43
|
|
15
|
+
enum peek is: 43
|
|
16
|
+
enum next is: 43
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
it: "turns an object with 'each: into an FancyEnumerator"
|
|
19
|
+
it: "turns an object with 'each: into an FancyEnumerator" with: 'to_enum when: {
|
|
20
20
|
enum = (42..45) to_enum
|
|
21
|
-
enum next
|
|
21
|
+
enum next is: 42
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
it: "turns an object with given method into an FancyEnumerator"
|
|
24
|
+
it: "turns an object with given method into an FancyEnumerator" with: 'to_enum: when: {
|
|
25
25
|
o = {}
|
|
26
26
|
def o iter: block {
|
|
27
27
|
1 upto: 10 do: block
|
|
28
28
|
}
|
|
29
29
|
enum = o to_enum: 'iter:
|
|
30
|
-
enum next
|
|
30
|
+
enum next is: 1
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
it: "rewinds to the beginning of the iteration"
|
|
33
|
+
it: "rewinds to the beginning of the iteration" with: 'rewind when: {
|
|
34
34
|
enum = (42..45) to_enum
|
|
35
35
|
check = {
|
|
36
|
-
enum peek
|
|
37
|
-
enum next
|
|
38
|
-
enum peek
|
|
39
|
-
enum next
|
|
36
|
+
enum peek is: 42
|
|
37
|
+
enum next is: 42
|
|
38
|
+
enum peek is: 43
|
|
39
|
+
enum next is: 43
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
check call
|
|
@@ -44,21 +44,21 @@ FancySpec describe: FancyEnumerator with: {
|
|
|
44
44
|
check call
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
it: "raises Fancy StopIteration when out of values"
|
|
47
|
+
it: "raises Fancy StopIteration when out of values" with: 'next when: {
|
|
48
48
|
o = Object new
|
|
49
49
|
def o each: block { block call: [1] }
|
|
50
50
|
e = o to_enum
|
|
51
51
|
|
|
52
|
-
e next
|
|
52
|
+
e next is: 1
|
|
53
53
|
try {
|
|
54
54
|
e next
|
|
55
|
-
"We
|
|
55
|
+
"We is not reach this line" is: true
|
|
56
56
|
} catch (Fancy StopIteration) => ex {
|
|
57
|
-
ex result
|
|
57
|
+
ex result is: nil
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
it: "sets result on Fancy StopIteration when out of values"
|
|
61
|
+
it: "sets result on Fancy StopIteration when out of values" with: 'next when: {
|
|
62
62
|
o = Object new
|
|
63
63
|
def o each: block {
|
|
64
64
|
block call: [1]
|
|
@@ -66,19 +66,37 @@ FancySpec describe: FancyEnumerator with: {
|
|
|
66
66
|
}
|
|
67
67
|
e = o to_enum
|
|
68
68
|
|
|
69
|
-
e next
|
|
69
|
+
e next is: 1
|
|
70
70
|
try {
|
|
71
71
|
e next
|
|
72
|
-
"We
|
|
72
|
+
"We is not reach this line" is: true
|
|
73
73
|
} catch (Fancy StopIteration) => ex {
|
|
74
|
-
ex result
|
|
74
|
+
ex result is: 42
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
it: "iterates with an object"
|
|
78
|
+
it: "iterates with an object" with: 'with:each: when: {
|
|
79
79
|
enum = (42..45) to_enum
|
|
80
80
|
result = enum with: [] each: |val, obj| { obj << val }
|
|
81
|
-
result
|
|
81
|
+
result is: [42, 43, 44, 45]
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
it: "chunks up into enums" with: 'chunk: when: {
|
|
85
|
+
enum = (1..42) to_enum
|
|
86
|
+
chunked = enum chunk: |n| { n % 3 == 0 }
|
|
87
|
+
chunked next is: [false, [1,2]]
|
|
88
|
+
chunked next is: [true, [3]]
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
it: "converts to an Array" with: 'to_a when: {
|
|
92
|
+
enum = (1..10) to_enum
|
|
93
|
+
enum to_a is: [1,2,3,4,5,6,7,8,9,10]
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
it: "has ended (no more values left)" with: 'ended? when: {
|
|
97
|
+
enum = (1..9) to_enum
|
|
98
|
+
10 times: { enum ended? is: false; enum next } # move forward
|
|
99
|
+
enum ended? is: true
|
|
82
100
|
}
|
|
83
101
|
}
|
|
84
102
|
|