fastruby 0.0.3 → 0.0.4

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.
@@ -0,0 +1,440 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby, "fastruby" do
4
+ it "should allow basic exception control" do
5
+ fastruby "
6
+ class ::L1
7
+ def foo
8
+ begin
9
+ rescue
10
+ end
11
+
12
+ 0
13
+ end
14
+ end
15
+ "
16
+ ::L1.new.foo.should be == 0
17
+ end
18
+
19
+ it "should allow basic exception control and catch exception" do
20
+ fastruby "
21
+ class ::L2
22
+ def foo
23
+ begin
24
+ raise RuntimeError
25
+ rescue RuntimeError
26
+ return 1
27
+ end
28
+
29
+ 0
30
+ end
31
+ end
32
+ "
33
+
34
+ lambda {
35
+ ::L2.new.foo.should be == 1
36
+ }.should_not raise_exception
37
+ end
38
+
39
+ it "should allow basic exception control and ensure" do
40
+ fastruby "
41
+ class ::L3
42
+ def foo
43
+
44
+ a = 0
45
+
46
+ begin
47
+ raise RuntimeError
48
+ rescue RuntimeError
49
+ ensure
50
+ a = 2
51
+ end
52
+
53
+ a
54
+ end
55
+ end
56
+ "
57
+
58
+ lambda {
59
+ ::L3.new.foo.should be == 2
60
+ }.should_not raise_exception
61
+ end
62
+
63
+ it "should allow basic exception control and ensure without rescue" do
64
+ class ::L4
65
+ attr_reader :a
66
+
67
+ fastruby "
68
+ def foo
69
+ begin
70
+ raise RuntimeError
71
+ ensure
72
+ @a = 2
73
+ end
74
+ end
75
+ "
76
+ end
77
+
78
+ l4 = ::L4.new
79
+
80
+ lambda {
81
+ l4.foo
82
+ }.should raise_exception(Exception)
83
+
84
+ l4.a.should be == 2
85
+ end
86
+
87
+ class BahException < Exception
88
+ end
89
+
90
+ def self.basic_unhandled_exception(*exception_names)
91
+
92
+ exception_names.each do |exception_name|
93
+ it "should raise basic exception #{exception_name}" do
94
+
95
+ random_name = "::L5_" + rand(10000).to_s
96
+
97
+ fastruby "
98
+ class #{random_name}
99
+ def foo
100
+ raise #{exception_name}
101
+ end
102
+ end
103
+ "
104
+
105
+ l = eval(random_name).new
106
+
107
+ lambda {
108
+ l.foo
109
+ }.should raise_exception(eval(exception_name))
110
+ end
111
+
112
+ it "should not raise basic exception #{exception_name} if rescued" do
113
+
114
+ random_name = "::L6_" + rand(10000).to_s
115
+
116
+ fastruby "
117
+ class #{random_name}
118
+ def foo
119
+ begin
120
+ raise #{exception_name}
121
+ rescue #{exception_name}
122
+ end
123
+ end
124
+ end
125
+ "
126
+
127
+ l = eval(random_name).new
128
+
129
+ lambda {
130
+ l.foo
131
+ }.should_not raise_exception
132
+ end
133
+
134
+ it "should raise basic exception #{exception_name} even if rescued when the rescue is for another exception" do
135
+
136
+ random_name = "::L7_" + rand(10000).to_s
137
+
138
+ fastruby "
139
+ class #{random_name}
140
+ def foo
141
+ begin
142
+ raise #{exception_name}
143
+ rescue BahException
144
+ end
145
+ end
146
+ end
147
+ "
148
+
149
+ l = eval(random_name).new
150
+
151
+ lambda {
152
+ l.foo
153
+ }.should raise_exception(eval(exception_name))
154
+ end
155
+
156
+ it "should rescue basic exception #{exception_name} when raised from rubycode called from fastruby code" do
157
+
158
+ random_name = "::L8_" + rand(10000).to_s
159
+ random_name_2 = "::L8_" + rand(10000).to_s
160
+
161
+ eval "
162
+ class #{random_name_2}
163
+ def bar
164
+ raise #{exception_name}
165
+ end
166
+ end
167
+ "
168
+
169
+ fastruby "
170
+ class #{random_name}
171
+ def foo(x)
172
+ x.bar
173
+ end
174
+ end
175
+ "
176
+
177
+ l1 = eval(random_name_2).new
178
+ l2 = eval(random_name).new
179
+ lambda {
180
+ l2.foo(l1)
181
+ }.should raise_exception(eval(exception_name))
182
+ end
183
+
184
+ it "should rescue basic exception #{exception_name} from fastruby code when raised from rubycode" do
185
+
186
+ random_name = "::L9_" + rand(10000).to_s
187
+ random_name_2 = "::L9_" + rand(10000).to_s
188
+
189
+ eval "
190
+ class #{random_name_2}
191
+ def bar
192
+ raise #{exception_name}
193
+ end
194
+ end
195
+ "
196
+
197
+ fastruby "
198
+ class #{random_name}
199
+ def foo(x)
200
+ begin
201
+ x.bar
202
+ rescue #{exception_name}
203
+ end
204
+ end
205
+ end
206
+ "
207
+
208
+ l1 = eval(random_name_2).new
209
+ l2 = eval(random_name).new
210
+ lambda {
211
+ l2.foo(l1)
212
+ }.should_not raise_exception
213
+ end
214
+
215
+
216
+
217
+
218
+ it "should raise basic exception #{exception_name} from singleton method" do
219
+
220
+ random_name = "::L5_" + rand(10000).to_s
221
+
222
+ fastruby "
223
+ class #{random_name}
224
+ def foo(x)
225
+ def x.foo
226
+ raise #{exception_name}
227
+ end
228
+
229
+ x
230
+ end
231
+ end
232
+ "
233
+
234
+ l = eval(random_name).new
235
+
236
+ lambda {
237
+ l.foo("").foo
238
+ }.should raise_exception(eval(exception_name))
239
+ end
240
+
241
+ it "should not raise basic exception #{exception_name} if rescued from singleton method" do
242
+
243
+ random_name = "::L6_" + rand(10000).to_s
244
+
245
+ fastruby "
246
+ class #{random_name}
247
+ def foo(x)
248
+ def x.foo
249
+ begin
250
+ raise #{exception_name}
251
+ rescue #{exception_name}
252
+ end
253
+ end
254
+
255
+ x
256
+ end
257
+ end
258
+ "
259
+
260
+ l = eval(random_name).new
261
+
262
+ lambda {
263
+ l.foo("").foo
264
+ }.should_not raise_exception
265
+ end
266
+
267
+ it "should raise basic exception #{exception_name} even if rescued when the rescue is for another exception from singleton method" do
268
+
269
+ random_name = "::L7_" + rand(10000).to_s
270
+
271
+ fastruby "
272
+ class #{random_name}
273
+ def foo(x)
274
+ def x.foo
275
+ begin
276
+ raise #{exception_name}
277
+ rescue BahException
278
+ end
279
+ end
280
+
281
+ x
282
+ end
283
+ end
284
+ "
285
+
286
+ l = eval(random_name).new
287
+
288
+ lambda {
289
+ l.foo("").foo
290
+ }.should raise_exception(eval(exception_name))
291
+ end
292
+
293
+ it "should rescue basic exception #{exception_name} when raised from rubycode called from fastruby code from singleton method" do
294
+
295
+ random_name = "::L8_" + rand(10000).to_s
296
+ random_name_2 = "::L8_" + rand(10000).to_s
297
+
298
+ eval "
299
+ class #{random_name_2}
300
+ def bar
301
+ raise #{exception_name}
302
+ end
303
+ end
304
+ "
305
+
306
+ fastruby "
307
+ class #{random_name}
308
+ def foo(x)
309
+ def x.foo(y)
310
+ y.bar
311
+ end
312
+
313
+ x
314
+ end
315
+ end
316
+ "
317
+
318
+ l1 = eval(random_name_2).new
319
+ l2 = eval(random_name).new
320
+ lambda {
321
+ l2.foo("").foo(l1)
322
+ }.should raise_exception(eval(exception_name))
323
+ end
324
+
325
+ it "should rescue basic exception #{exception_name} from fastruby code when raised from rubycode from singleton methods" do
326
+
327
+ random_name = "::L9_" + rand(10000).to_s
328
+ random_name_2 = "::L9_" + rand(10000).to_s
329
+
330
+ eval "
331
+ class #{random_name_2}
332
+ def bar
333
+ raise #{exception_name}
334
+ end
335
+ end
336
+ "
337
+
338
+ fastruby "
339
+ class #{random_name}
340
+ def foo(x)
341
+ def x.foo(y)
342
+ begin
343
+ y.bar
344
+ rescue #{exception_name}
345
+ end
346
+ end
347
+
348
+ x
349
+ end
350
+ end
351
+ "
352
+
353
+ l1 = eval(random_name_2).new
354
+ l2 = eval(random_name).new
355
+ lambda {
356
+ l2.foo("").foo(l1)
357
+ }.should_not raise_exception
358
+ end
359
+
360
+
361
+
362
+
363
+
364
+
365
+
366
+
367
+
368
+
369
+
370
+ end
371
+ end
372
+
373
+ basic_unhandled_exception("Exception")
374
+ basic_unhandled_exception("RuntimeError")
375
+ basic_unhandled_exception("StandardError")
376
+ basic_unhandled_exception("Errno::ENOENT")
377
+
378
+ it "should accept else with rescue" do
379
+
380
+ random_name = "::L11_" + rand(10000).to_s
381
+ fastruby "
382
+ class #{random_name}
383
+ def foo
384
+ begin
385
+ raise Exception
386
+ rescue Exception
387
+ return 111
388
+ else
389
+ return 222
390
+ end
391
+ end
392
+ end
393
+ "
394
+
395
+ l = eval(random_name).new
396
+ l.foo.should be == 111
397
+ end
398
+
399
+ it "should accept else with rescue, when no exception is raised" do
400
+
401
+ random_name = "::L12_" + rand(10000).to_s
402
+ fastruby "
403
+ class #{random_name}
404
+ def foo
405
+ begin
406
+ rescue Exception
407
+ return 111
408
+ else
409
+ return 222
410
+ end
411
+ end
412
+ end
413
+ "
414
+
415
+ l = eval(random_name).new
416
+ l.foo.should be == 222
417
+ end
418
+
419
+ it "should accept else with rescue, when no exception is raised and begin has body" do
420
+
421
+ random_name = "::L13_" + rand(10000).to_s
422
+ fastruby "
423
+ class #{random_name}
424
+ def foo
425
+ begin
426
+ a = 77
427
+ rescue Exception
428
+ return 111
429
+ else
430
+ return 222
431
+ end
432
+ end
433
+ end
434
+ "
435
+
436
+ l = eval(random_name).new
437
+ l.foo.should be == 222
438
+ end
439
+
440
+ end
@@ -0,0 +1,36 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby, "fastruby" do
4
+ it "should alow definition of modules" do
5
+ fastruby "
6
+ module H1
7
+ end
8
+ "
9
+ end
10
+
11
+ it "should alow definition of modules with other code" do
12
+ fastruby '
13
+ print "defining module\n"
14
+
15
+ module H2
16
+ end
17
+ '
18
+ end
19
+
20
+ it "should alow definition of method in modules" do
21
+ fastruby '
22
+ module H2
23
+ def foo
24
+ 77
25
+ end
26
+ end
27
+ '
28
+
29
+ class H2C
30
+ include H2
31
+ end
32
+
33
+ H2C.new.foo.should be == 77
34
+ end
35
+
36
+ end
@@ -0,0 +1,99 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby, "fastruby" do
4
+ it "should allow basic return" do
5
+ fastruby "
6
+ class ::P1
7
+ def foo
8
+ return 1
9
+ end
10
+ end
11
+ "
12
+ ::P1.new.foo.should be == 1
13
+ end
14
+
15
+ it "should allow return from inside a block" do
16
+ fastruby "
17
+ class ::P2
18
+ def bar
19
+ yield
20
+ end
21
+
22
+ def foo
23
+ bar do
24
+ return 8
25
+ end
26
+
27
+ return 0
28
+ end
29
+ end
30
+ "
31
+ ::P2.new.foo.should be == 8
32
+ end
33
+
34
+ it "should allow basic return on singleton method" do
35
+ fastruby "
36
+ class ::P3
37
+ end
38
+
39
+ class ::P31
40
+ def bar(x)
41
+ def x.foo
42
+ return 1
43
+ end
44
+ end
45
+ end
46
+ "
47
+
48
+ p3 = ::P3.new
49
+ ::P31.new.bar(p3)
50
+ p3.foo.should be == 1
51
+ end
52
+
53
+ it "should allow return from inside a block on a singleton method" do
54
+ fastruby "
55
+ class ::P4
56
+ def bar
57
+ yield
58
+ end
59
+ end
60
+
61
+ class ::P41
62
+ def bar(x)
63
+ def x.foo
64
+ bar do
65
+ return 8
66
+ end
67
+ return 0
68
+ end
69
+ end
70
+ end
71
+ "
72
+ p4 = ::P4.new
73
+ ::P41.new.bar(p4)
74
+ p4.foo.should be == 8
75
+ end
76
+
77
+ it "should execute ensure when ensure impl has a return" do
78
+ fastruby "
79
+ class ::P5
80
+ def a
81
+ @a
82
+ end
83
+
84
+ def foo
85
+ begin
86
+ return 16
87
+ ensure
88
+ @a = 9
89
+ end
90
+
91
+ return 32
92
+ end
93
+ end
94
+ "
95
+ p5 = ::P5.new
96
+ p5.foo.should be == 16
97
+ p5.a.should be ==9
98
+ end
99
+ end
@@ -0,0 +1,76 @@
1
+ require "fastruby"
2
+
3
+ describe FastRuby, "fastruby" do
4
+
5
+ it "should allow defining class methods" do
6
+ fastruby '
7
+ class X
8
+ def self.foo
9
+ 9
10
+ end
11
+ end
12
+ '
13
+ X.foo.should be == 9
14
+ end
15
+
16
+ it "should allow defining class methods with blocks" do
17
+ fastruby '
18
+ class R2
19
+ def self.bar
20
+ yield
21
+ end
22
+ end
23
+ '
24
+ R2.bar{67}.should be == 67
25
+ end
26
+
27
+ it "should call singleton methods from outside" do
28
+ class R3
29
+ def bar
30
+ 12
31
+ end
32
+ end
33
+
34
+ fastruby '
35
+ class R4
36
+ def foo(x)
37
+ def x.bar
38
+ 24
39
+ end
40
+ end
41
+ end
42
+ '
43
+
44
+ r3 = R3.new
45
+ R4.new.foo(r3)
46
+ r3.bar.should be == 24
47
+ end
48
+
49
+ it "should call singleton methods from inside" do
50
+ fastruby '
51
+ class R5
52
+ def bar
53
+ 12
54
+ end
55
+ end
56
+ class R6
57
+ def foo(x)
58
+ def x.bar
59
+ 24
60
+ end
61
+ end
62
+ end
63
+ class R7
64
+ def foo(x)
65
+ x.bar+1
66
+ end
67
+ end
68
+ '
69
+
70
+ r5 = R5.new
71
+ R7.new.foo(r5).should be == 13
72
+ R6.new.foo(r5)
73
+ R7.new.foo(r5).should be == 25
74
+ end
75
+
76
+ end
data/spec/sugar_spec.rb CHANGED
@@ -57,7 +57,7 @@ describe FastRuby, "fastruby" do
57
57
  it "should compile standalone code and execute it inmediatly" do
58
58
  $e4 = ::E4
59
59
  fastruby "
60
- require 'fastruby'
60
+ print 'fastruby'
61
61
  $e4.new.foo
62
62
  "
63
63
  end
@@ -65,7 +65,7 @@ describe FastRuby, "fastruby" do
65
65
  it "should compile standalone code togheter with classes and execute it inmediatly" do
66
66
  $e4 = ::E4
67
67
  fastruby "
68
- require 'fastruby'
68
+ print 'fastruby'
69
69
  $e4.new.foo
70
70
 
71
71
  class ::E5