endlessruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,589 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "simply example case" do
4
+
5
+ it "supplimented its source code with 'end'" do
6
+
7
+ ER2RB(<<DEFINE).should ==
8
+ module HelloEndlessRubyWorld
9
+ def self.hello
10
+ puts "hello endlessruby world"
11
+ DEFINE
12
+ <<DEFINE.chomp!
13
+ module HelloEndlessRubyWorld
14
+ def self.hello
15
+ puts "hello endlessruby world"
16
+ end
17
+ end
18
+ DEFINE
19
+ end
20
+ end
21
+
22
+ MODULE = <<DEFINE.chomp!
23
+ module Example
24
+ DEFINE
25
+
26
+ CLASS = <<DEFINE.chomp!
27
+ class Eample
28
+ DEFINE
29
+
30
+ DEF = <<DEFINE.chomp!
31
+ def example
32
+ DEFINE
33
+
34
+ ERSpecHelper.join_blocks([
35
+ MODULE, [
36
+ DEF, []
37
+ ]
38
+ ])
39
+
40
+ puts ERSpecHelper.join_blocks([
41
+ MODULE, [
42
+ DEF, []
43
+ ]
44
+ ], true)
45
+
46
+ describe "define method case" do
47
+
48
+ it "define method" do
49
+ ER2RB(<<DEFINE).should ==
50
+ def example
51
+ puts "hello world"
52
+ DEFINE
53
+ <<DEFINE.chomp!
54
+ def example
55
+ puts "hello world"
56
+ end
57
+ DEFINE
58
+ end
59
+
60
+ it "define class method" do
61
+ ER2RB(<<DEFINE).should ==
62
+ def self.example
63
+ puts "hello world"
64
+ DEFINE
65
+ <<DEFINE.chomp!
66
+ def self.example
67
+ puts "hello world"
68
+ end
69
+ DEFINE
70
+ end
71
+
72
+ it "define singleton method" do
73
+ ER2RB(<<DEFINE).should ==
74
+ def obj.example
75
+ puts "hello world"
76
+ DEFINE
77
+ <<DEFINE.chomp!
78
+ def obj.example
79
+ puts "hello world"
80
+ end
81
+ DEFINE
82
+ end
83
+
84
+ it "simple rescue raising" do
85
+ ER2RB(<<DEFINE).should ==
86
+ def obj.example
87
+ puts "hello world"
88
+ rescue
89
+ pass
90
+ DEFINE
91
+ <<DEFINE.chomp!
92
+ def obj.example
93
+ puts "hello world"
94
+ rescue
95
+ pass
96
+ end
97
+ DEFINE
98
+ end
99
+
100
+ end
101
+
102
+ describe "define module case" do
103
+
104
+ it "define module" do
105
+ ER2RB(<<DEFINE).should ==
106
+ module Example
107
+ pass
108
+ DEFINE
109
+ <<DEFINE.chomp!
110
+ module Example
111
+ pass
112
+ end
113
+ DEFINE
114
+ end
115
+
116
+
117
+ it "define inner module" do
118
+ ER2RB(<<DEFINE).should ==
119
+ module OuterExample::InnerExample
120
+ pass
121
+ DEFINE
122
+ <<DEFINE.chomp!
123
+ module OuterExample::InnerExample
124
+ pass
125
+ end
126
+ DEFINE
127
+ end
128
+
129
+ it "beggining of the 'module' word method" do
130
+ ER2RB(<<DEFINE).should ==
131
+ module_eval()
132
+ pass
133
+ DEFINE
134
+ <<DEFINE.chomp!
135
+ module_eval()
136
+ pass
137
+ DEFINE
138
+ end
139
+
140
+ end
141
+
142
+ describe "define class case" do
143
+
144
+ it "define class" do
145
+ ER2RB(<<DEFINE).should ==
146
+ class Example
147
+ DEFINE
148
+ <<DEFINE.chomp!
149
+ class Example
150
+ end
151
+ DEFINE
152
+ end
153
+
154
+
155
+ it "define inner module" do
156
+ ER2RB(<<DEFINE).should ==
157
+ class OuterExample::InnerExample
158
+ DEFINE
159
+ <<DEFINE.chomp!
160
+ class OuterExample::InnerExample
161
+ end
162
+ DEFINE
163
+ end
164
+
165
+ it "beggining of the 'class' word method" do
166
+ ER2RB(<<DEFINE).should ==
167
+ class_eval()
168
+ DEFINE
169
+ <<DEFINE.chomp!
170
+ class_eval()
171
+ DEFINE
172
+ end
173
+
174
+ end
175
+
176
+ describe "if expression case" do
177
+
178
+ it "without parenthesis" do
179
+ ER2RB(<<DEFINE).should ==
180
+ if flg
181
+ pass
182
+ DEFINE
183
+ <<DEFINE.chomp!
184
+ if flg
185
+ pass
186
+ end
187
+ DEFINE
188
+ end
189
+
190
+ it "parenthesis" do
191
+ ER2RB(<<DEFINE).should ==
192
+ if(flg)
193
+ pass
194
+ DEFINE
195
+ <<DEFINE.chomp!
196
+ if(flg)
197
+ pass
198
+ end
199
+ DEFINE
200
+ end
201
+
202
+ it "then and without parenthesis" do
203
+ ER2RB(<<DEFINE).should ==
204
+ if flg then
205
+ pass
206
+ DEFINE
207
+ <<DEFINE.chomp!
208
+ if flg then
209
+ pass
210
+ end
211
+ DEFINE
212
+ end
213
+
214
+ it "then and parenthesis" do
215
+ ER2RB(<<DEFINE).should ==
216
+ if(flg) then
217
+ pass
218
+ DEFINE
219
+ <<DEFINE.chomp!
220
+ if(flg) then
221
+ pass
222
+ end
223
+ DEFINE
224
+ end
225
+
226
+ it "else and without parenthesis" do
227
+ ER2RB(<<DEFINE).should ==
228
+ if flg
229
+ pass
230
+ else
231
+ pass
232
+ DEFINE
233
+ <<DEFINE.chomp!
234
+ if flg
235
+ pass
236
+ else
237
+ pass
238
+ end
239
+ DEFINE
240
+ end
241
+
242
+ it "else and parenthesis" do
243
+ ER2RB(<<DEFINE).should ==
244
+ if(flg)
245
+ pass
246
+ else
247
+ pass
248
+ DEFINE
249
+ <<DEFINE.chomp!
250
+ if(flg)
251
+ pass
252
+ else
253
+ pass
254
+ end
255
+ DEFINE
256
+ end
257
+
258
+ it "else and then and without parenthesis" do
259
+ ER2RB(<<DEFINE).should ==
260
+ if flg then
261
+ pass
262
+ else
263
+ pass
264
+ DEFINE
265
+ <<DEFINE.chomp!
266
+ if flg then
267
+ pass
268
+ else
269
+ pass
270
+ end
271
+ DEFINE
272
+ end
273
+
274
+ it "else and then and parenthesis" do
275
+ ER2RB(<<DEFINE).should ==
276
+ if(flg) then
277
+ pass
278
+ else
279
+ pass
280
+ DEFINE
281
+ <<DEFINE.chomp!
282
+ if(flg) then
283
+ pass
284
+ else
285
+ pass
286
+ end
287
+ DEFINE
288
+ end
289
+
290
+ end
291
+
292
+ describe "unless expression case" do
293
+
294
+
295
+ it "without parenthesis" do
296
+ ER2RB(<<DEFINE).should ==
297
+ unless flg
298
+ pass
299
+ DEFINE
300
+ <<DEFINE.chomp!
301
+ unless flg
302
+ pass
303
+ end
304
+ DEFINE
305
+ end
306
+
307
+ it "parenthesis" do
308
+ ER2RB(<<DEFINE).should ==
309
+ unless(flg)
310
+ pass
311
+ DEFINE
312
+ <<DEFINE.chomp!
313
+ unless(flg)
314
+ pass
315
+ end
316
+ DEFINE
317
+ end
318
+
319
+ it "then and without parenthesis" do
320
+ ER2RB(<<DEFINE).should ==
321
+ unless flg then
322
+ pass
323
+ DEFINE
324
+ <<DEFINE.chomp!
325
+ unless flg then
326
+ pass
327
+ end
328
+ DEFINE
329
+ end
330
+
331
+ it "then and parenthesis" do
332
+ ER2RB(<<DEFINE).should ==
333
+ unless(flg) then
334
+ pass
335
+ DEFINE
336
+ <<DEFINE.chomp!
337
+ unless(flg) then
338
+ pass
339
+ end
340
+ DEFINE
341
+ end
342
+
343
+ it "else and without parenthesis" do
344
+ ER2RB(<<DEFINE).should ==
345
+ unless flg
346
+ pass
347
+ else
348
+ pass
349
+ DEFINE
350
+ <<DEFINE.chomp!
351
+ unless flg
352
+ pass
353
+ else
354
+ pass
355
+ end
356
+ DEFINE
357
+ end
358
+
359
+ it "else and parenthesis" do
360
+ ER2RB(<<DEFINE).should ==
361
+ unless(flg)
362
+ pass
363
+ else
364
+ pass
365
+ DEFINE
366
+ <<DEFINE.chomp!
367
+ unless(flg)
368
+ pass
369
+ else
370
+ pass
371
+ end
372
+ DEFINE
373
+ end
374
+
375
+ it "else and then and without parenthesis" do
376
+ ER2RB(<<DEFINE).should ==
377
+ unless flg then
378
+ pass
379
+ else
380
+ pass
381
+ DEFINE
382
+ <<DEFINE.chomp!
383
+ unless flg then
384
+ pass
385
+ else
386
+ pass
387
+ end
388
+ DEFINE
389
+ end
390
+
391
+ it "else and then and parenthesis" do
392
+ ER2RB(<<DEFINE).should ==
393
+ unless(flg) then
394
+ pass
395
+ else
396
+ pass
397
+ DEFINE
398
+ <<DEFINE.chomp!
399
+ unless(flg) then
400
+ pass
401
+ else
402
+ pass
403
+ end
404
+ DEFINE
405
+ end
406
+
407
+ end
408
+
409
+ describe "while expression case" do
410
+
411
+ it "without parenthesis" do
412
+ ER2RB(<<DEFINE).should ==
413
+ while flg
414
+ pass
415
+ DEFINE
416
+ <<DEFINE.chomp!
417
+ while flg
418
+ pass
419
+ end
420
+ DEFINE
421
+ end
422
+
423
+ it "parenthesis" do
424
+ ER2RB(<<DEFINE).should ==
425
+ while(flg)
426
+ pass
427
+ DEFINE
428
+ <<DEFINE.chomp!
429
+ while(flg)
430
+ pass
431
+ end
432
+ DEFINE
433
+ end
434
+ end
435
+
436
+ describe "until expression case" do
437
+
438
+ it "without parenthesis" do
439
+ ER2RB(<<DEFINE).should ==
440
+ until flg
441
+ pass
442
+ DEFINE
443
+ <<DEFINE.chomp!
444
+ until flg
445
+ pass
446
+ end
447
+ DEFINE
448
+ end
449
+
450
+ it "parenthesis" do
451
+ ER2RB(<<DEFINE).should ==
452
+ until(flg)
453
+ pass
454
+ DEFINE
455
+ <<DEFINE.chomp!
456
+ until(flg)
457
+ pass
458
+ end
459
+ DEFINE
460
+ end
461
+
462
+ end
463
+
464
+ describe "exception expression case" do
465
+
466
+ it "try only" do
467
+ ER2RB(<<DEFINE).should ==
468
+ begin
469
+ pass
470
+ DEFINE
471
+ <<DEFINE.chomp!
472
+ begin
473
+ pass
474
+ end
475
+ DEFINE
476
+ end
477
+
478
+
479
+ it "simple rescue" do
480
+ ER2RB(<<DEFINE).should ==
481
+ begin
482
+ pass
483
+ rescue
484
+ pass
485
+ DEFINE
486
+ <<DEFINE.chomp!
487
+ begin
488
+ pass
489
+ rescue
490
+ pass
491
+ end
492
+ DEFINE
493
+ end
494
+
495
+ it "rescue LoadError" do
496
+ ER2RB(<<DEFINE).should ==
497
+ begin
498
+ pass
499
+ rescue LoadError
500
+ pass
501
+ DEFINE
502
+ <<DEFINE.chomp!
503
+ begin
504
+ pass
505
+ rescue LoadError
506
+ pass
507
+ end
508
+ DEFINE
509
+ end
510
+
511
+ it "rescue LoadError to variable" do
512
+ ER2RB(<<DEFINE).should ==
513
+ begin
514
+ pass
515
+ rescue LoadError => e
516
+ pass
517
+ DEFINE
518
+ <<DEFINE.chomp!
519
+ begin
520
+ pass
521
+ rescue LoadError => e
522
+ pass
523
+ end
524
+ DEFINE
525
+ end
526
+
527
+
528
+ it "rescue LoadError and SyntaxError" do
529
+ ER2RB(<<DEFINE).should ==
530
+ begin
531
+ pass
532
+ rescue LoadError
533
+ pass
534
+ rescue SyntaxError
535
+ pass
536
+ DEFINE
537
+ <<DEFINE.chomp!
538
+ begin
539
+ pass
540
+ rescue LoadError
541
+ pass
542
+ rescue SyntaxError
543
+ pass
544
+ end
545
+ DEFINE
546
+ end
547
+
548
+ it "else block" do
549
+ ER2RB(<<DEFINE).should ==
550
+ begin
551
+ pass
552
+ rescue LoadError
553
+ pass
554
+ else
555
+ pass
556
+ DEFINE
557
+ <<DEFINE.chomp!
558
+ begin
559
+ pass
560
+ rescue LoadError
561
+ pass
562
+ else
563
+ pass
564
+ end
565
+ DEFINE
566
+ end
567
+
568
+
569
+ it "ensure block" do
570
+ ER2RB(<<DEFINE).should ==
571
+ begin
572
+ pass
573
+ rescue LoadError
574
+ pass
575
+ ensure
576
+ pass
577
+ DEFINE
578
+ <<DEFINE.chomp!
579
+ begin
580
+ pass
581
+ rescue LoadError
582
+ pass
583
+ ensure
584
+ pass
585
+ end
586
+ DEFINE
587
+ end
588
+
589
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,40 @@
1
+ begin
2
+ require 'rspec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'rspec'
6
+ end
7
+
8
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
9
+ require 'endlessruby'
10
+
11
+ $:.unshift(File.dirname(__FILE__))
12
+
13
+ include EndlessRuby
14
+
15
+ module ERSpecHelper
16
+
17
+ extend self
18
+
19
+ def indent lines, level=1, indent=' '
20
+ lines.split("\n").map! { |l| "#{indent*level}#{l}" }.join "\n"
21
+ end
22
+
23
+ def chomp s
24
+ if s =~ /\A((?:.|\s)*.)\n+\z/
25
+ s = $1
26
+ end
27
+ s
28
+ end
29
+
30
+ def join_blocks blocks, insert_end=false, level=0, indent=' '
31
+ blocks.each_slice(2).map do |b, i|
32
+ [].tap do |a|
33
+ a << indent(b, level, indent)
34
+ a << join_blocks(i, insert_end, level+1, indent) unless i.nil? || i.empty?
35
+ a << indent('end', level, indent) if insert_end
36
+ end.join "\n"
37
+ end.join "\n"
38
+ end
39
+
40
+ end
@@ -0,0 +1,6 @@
1
+ $test_data = "er omission"
2
+
3
+ module TestData
4
+ extend self
5
+ def test_data
6
+ "er omission"
@@ -0,0 +1,6 @@
1
+ $test_data = "file"
2
+
3
+ module TestData
4
+ extend self
5
+ def test_data
6
+ "file"
@@ -0,0 +1,8 @@
1
+ $test_data = "rb omission"
2
+
3
+ module TestData
4
+ extend self
5
+ def test_data
6
+ "rb omission"
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ $test_data = "ruby script"
2
+
3
+ module TestData
4
+ extend self
5
+ def test_data
6
+ "ruby script"
7
+ end
8
+ end
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'rubygems'
5
+
6
+ module Kernel
7
+
8
+ alias endlessruby_original_require require
9
+
10
+ # EndlessRuby によって再定義された require です。
11
+ # たいていのこのrequireはオリジナルなrequireまたはrubygemsのrequireがSyntaxErrorによって失敗した場合のみ機能します。
12
+ # SytanxError によってrequireが失敗した場合、pathを探してpathまたはpath.erの名前のファイルをEndlessRubyの構文として評価します。
13
+ # pathが./または/で以外で始まる場合は$LOAD_PATHと$:をそれぞれ参照してpathを探します。
14
+ # もしpathがそれらで始まる場合はそれぞれ参照しません。(つまり通常のrequireの動作と同じです)
15
+ def require path
16
+ at = caller
17
+ endlessruby_original_require path
18
+ rescue SyntaxError, LoadError
19
+ case path
20
+ when /^\.\/.*?$/, /^\/.*?$/
21
+ unless File.exist? path
22
+ if File.exist? "#{path}.er"
23
+ path = "#{path}.er"
24
+ else
25
+ $@ = at
26
+ raise LoadError, "no such file to load -- #{path}"
27
+
28
+ if File.directory? path
29
+ $@ = at
30
+ raise LoadError, "Is a directory - #{path}"
31
+
32
+ open(path) do |file|
33
+ begin
34
+ EndlessRuby.ereval file.read, TOPLEVEL_BINDING, path
35
+ rescue Exception => e
36
+ $@ = at
37
+ raise e
38
+ return true
39
+ else
40
+ is_that_dir = false
41
+ ($LOAD_PATH | $:).each do |load_path|
42
+ real_path = File.join load_path, path
43
+ unless File.exist? real_path
44
+ if File.exist? "#{real_path}.er"
45
+ real_path = "#{real_path}.er"
46
+ else
47
+ next
48
+
49
+ next is_that_dir = true if File.directory? real_path
50
+ open(real_path) do |file|
51
+ begin
52
+ EndlessRuby.ereval file.read, TOPLEVEL_BINDING, real_path
53
+ rescue Exception => e
54
+ $@ = at
55
+ raise e
56
+ return true
57
+ $@ = at
58
+ if is_that_dir
59
+ raise LoadError, "Is a directory - #{path}"
60
+ else
61
+ raise LoadError, "no such file to load -- #{path}"
62
+