otaku 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,78 +0,0 @@
1
- module Otaku
2
- class Handler
3
- class Processor #:nodoc:
4
-
5
- extend Forwardable
6
- %w{file line code eval!}.each do |meth|
7
- def_delegator :@magic_proc, meth.to_sym
8
- end
9
-
10
- def initialize(block)
11
- @magic_proc = MagicProc.new(block)
12
- end
13
-
14
- def marshal_dump
15
- [@magic_proc]
16
- end
17
-
18
- def marshal_load(data)
19
- @magic_proc, _ = data
20
- end
21
-
22
- private
23
-
24
- class MagicProc < Handler::MagicProc
25
-
26
- def code_fragments
27
- ignore, start_marker, arg =
28
- [:ignore, :start_marker, :arg].map{|key| code_match_args[key] }
29
- [
30
- "proc #{start_marker} |#{arg}| ",
31
- source_code.sub(ignore, '')
32
- ]
33
- end
34
-
35
- def sexp_regexp
36
- @cache[:sexp_regexp] ||= (
37
- Regexp.new([
38
- Regexp.quote("s(:iter, s(:call, nil, :"),
39
- "(proc|lambda)",
40
- Regexp.quote(", s(:arglist)), s(:lasgn, :#{code_match_args[:arg]}), s("),
41
- ].join)
42
- )
43
- end
44
-
45
- def code_match_args
46
- @cache[:code_match_args] ||= (
47
- arg_idx, marker_idx = 5, 3
48
- args = source_code.match(code_regexp)
49
- while args && prob_regexp =~ args[1]
50
- arg_idx, marker_idx = 4, 2
51
- args = source_code.match(revised_regexp(args[1]))
52
- end
53
- {
54
- :ignore => args[1],
55
- :start_marker => args[marker_idx],
56
- :arg => args[arg_idx]
57
- }
58
- )
59
- end
60
-
61
- def code_regexp
62
- /^(.*?(Otaku\.start.*?|lambda|proc|Proc\.new)\s*(do|\{)\s*(\|(\w+)\|\s*))/m
63
- end
64
-
65
- def revised_regexp(e)
66
- /^(.*?#{Regexp.quote(e)}.*?\s*(\{|do)\s*(\|(\w+)\|)\s*)/m
67
- end
68
-
69
- def prob_regexp
70
- /Otaku\.start.*?(lambda|proc|Proc\.new)\s*(\{|do)\s*\|\w+\|\s*$/m
71
- end
72
-
73
- end
74
-
75
- end
76
- end
77
- end
78
-
@@ -1,1128 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper')
2
-
3
- Otaku::Handler.class_eval do
4
- attr_reader :context, :proc
5
- end
6
-
7
- Otaku.instance_eval do
8
- def start(context = {}, &block)
9
- Otaku::Handler.new(context, block)
10
- end
11
- end
12
-
13
- class Otaku::Handler::Context
14
- alias_method :orig_magic_proc, :magic_proc
15
- attr_reader :magic_procs
16
- def magic_proc(val)
17
- (@magic_procs ||= []) << orig_magic_proc(val)
18
- @magic_procs.last
19
- end
20
- end
21
-
22
- describe "Otaku Service Handler" do
23
-
24
- describe '>> initializing @context' do
25
-
26
- should 'assign to empty anoynmous class instance code when given {}' do
27
- Otaku::Handler.new({}, lambda{}).context.code.should.equal('Class.new{ }.new')
28
- end
29
-
30
- should 'assign to non-empty anoynmous class instance code when given {:m1 => v1, :m2 => v2, ...}' do
31
- encode = lambda{|val| Otaku::Encoder.encode(val).gsub('|','\|') }
32
- Otaku::Handler.new({:m1 => 'v|1', :m2 => 'v|2'}, lambda{}).context.code.should.equal(
33
- 'Class.new{ %s; %s }.new' % [
34
- "def m1; Encoder.decode(%|#{encode['v|1']}|); end",
35
- "def m2; Encoder.decode(%|#{encode['v|2']}|); end"
36
- ])
37
- end
38
-
39
- describe '>>> handling serializing of proc variable' do
40
-
41
- class << self
42
-
43
- def new_otaku_handler(*args, &block)
44
- Otaku::Handler.new({:processor => block}, lambda{})
45
- end
46
-
47
- def should_have_expected_context(context, code, line)
48
- context.magic_procs[0].code.should.equal(code)
49
- context.magic_procs[0].file.should.equal(File.expand_path(__FILE__))
50
- context.magic_procs[0].line.should.equal(line)
51
- end
52
-
53
- end
54
-
55
- no_arg_expected = "lambda { [\"a\", \"b\"].map { |x| puts(x) } }"
56
- single_arg_expected = "lambda { |arg| [\"a\", \"b\"].map { |x| puts(x) } }"
57
- multiple_args_expected = "lambda { |arg1, arg2| [\"a\", \"b\"].map { |x| puts(x) } }"
58
- unlimited_args_expected = "lambda { |*args| [\"a\", \"b\"].map { |x| puts(x) } }"
59
-
60
- {
61
- # ////////////////////////////////////////////////////////////////////////
62
- # >> Always newlinling (single arg)
63
- # ////////////////////////////////////////////////////////////////////////
64
- __LINE__ => [
65
- lambda do |arg|
66
- %w{a b}.map do |x|
67
- puts x
68
- end
69
- end,
70
- single_arg_expected
71
- ],
72
- __LINE__ => [
73
- lambda { |arg|
74
- %w{a b}.map{|x|
75
- puts x
76
- }
77
- },
78
- single_arg_expected
79
- ],
80
- __LINE__ => [
81
- proc do |arg|
82
- %w{a b}.map do |x|
83
- puts x
84
- end
85
- end,
86
- single_arg_expected
87
- ],
88
- __LINE__ => [
89
- lambda { |arg|
90
- %w{a b}.map{|x|
91
- puts x
92
- }
93
- },
94
- single_arg_expected
95
- ],
96
- __LINE__ => [
97
- Proc.new do |arg|
98
- %w{a b}.map do |x|
99
- puts x
100
- end
101
- end,
102
- single_arg_expected
103
- ],
104
- __LINE__ => [
105
- Proc.new { |arg|
106
- %w{a b}.map{|x|
107
- puts x
108
- }
109
- },
110
- single_arg_expected
111
- ],
112
- # ////////////////////////////////////////////////////////////////////////
113
- # >> Always newlinling (multiple args)
114
- # ////////////////////////////////////////////////////////////////////////
115
- __LINE__ => [
116
- lambda do |arg1, arg2|
117
- %w{a b}.map do |x|
118
- puts x
119
- end
120
- end,
121
- multiple_args_expected
122
- ],
123
- __LINE__ => [
124
- lambda { |arg1, arg2|
125
- %w{a b}.map{|x|
126
- puts x
127
- }
128
- },
129
- multiple_args_expected
130
- ],
131
- __LINE__ => [
132
- proc do |arg1, arg2|
133
- %w{a b}.map do |x|
134
- puts x
135
- end
136
- end,
137
- multiple_args_expected
138
- ],
139
- __LINE__ => [
140
- lambda { |arg1, arg2|
141
- %w{a b}.map{|x|
142
- puts x
143
- }
144
- },
145
- multiple_args_expected
146
- ],
147
- __LINE__ => [
148
- Proc.new do |arg1, arg2|
149
- %w{a b}.map do |x|
150
- puts x
151
- end
152
- end,
153
- multiple_args_expected
154
- ],
155
- __LINE__ => [
156
- Proc.new { |arg1, arg2|
157
- %w{a b}.map{|x|
158
- puts x
159
- }
160
- },
161
- multiple_args_expected
162
- ],
163
- # ////////////////////////////////////////////////////////////////////////
164
- # >> Always newlinling (unlimited args)
165
- # ////////////////////////////////////////////////////////////////////////
166
- __LINE__ => [
167
- lambda do |*args|
168
- %w{a b}.map do |x|
169
- puts x
170
- end
171
- end,
172
- unlimited_args_expected
173
- ],
174
- __LINE__ => [
175
- lambda { |*args|
176
- %w{a b}.map{|x|
177
- puts x
178
- }
179
- },
180
- unlimited_args_expected
181
- ],
182
- __LINE__ => [
183
- proc do |*args|
184
- %w{a b}.map do |x|
185
- puts x
186
- end
187
- end,
188
- unlimited_args_expected
189
- ],
190
- __LINE__ => [
191
- lambda { |*args|
192
- %w{a b}.map{|x|
193
- puts x
194
- }
195
- },
196
- unlimited_args_expected
197
- ],
198
- __LINE__ => [
199
- Proc.new do |*args|
200
- %w{a b}.map do |x|
201
- puts x
202
- end
203
- end,
204
- unlimited_args_expected
205
- ],
206
- __LINE__ => [
207
- Proc.new { |*args|
208
- %w{a b}.map{|x|
209
- puts x
210
- }
211
- },
212
- unlimited_args_expected
213
- ],
214
- # ////////////////////////////////////////////////////////////////////////
215
- # >> Always newlinling (no arg)
216
- # ////////////////////////////////////////////////////////////////////////
217
- __LINE__ => [
218
- lambda do
219
- %w{a b}.map do |x|
220
- puts x
221
- end
222
- end,
223
- no_arg_expected
224
- ],
225
- __LINE__ => [
226
- lambda {
227
- %w{a b}.map{|x|
228
- puts x
229
- }
230
- },
231
- no_arg_expected
232
- ],
233
- __LINE__ => [
234
- proc do
235
- %w{a b}.map do |x|
236
- puts x
237
- end
238
- end,
239
- no_arg_expected
240
- ],
241
- __LINE__ => [
242
- lambda {
243
- %w{a b}.map{|x|
244
- puts x
245
- }
246
- },
247
- no_arg_expected
248
- ],
249
- __LINE__ => [
250
- Proc.new do
251
- %w{a b}.map do |x|
252
- puts x
253
- end
254
- end,
255
- no_arg_expected
256
- ],
257
- __LINE__ => [
258
- Proc.new {
259
- %w{a b}.map{|x|
260
- puts x
261
- }
262
- },
263
- no_arg_expected
264
- ],
265
- # ////////////////////////////////////////////////////////////////////////
266
- # >> Partial newlining (single arg)
267
- # ////////////////////////////////////////////////////////////////////////
268
- __LINE__ => [
269
- lambda do |arg|
270
- %w{a b}.map do |x| puts x end
271
- end,
272
- single_arg_expected
273
- ],
274
- __LINE__ => [
275
- lambda { |arg|
276
- %w{a b}.map{|x| puts x }
277
- },
278
- single_arg_expected
279
- ],
280
- __LINE__ => [
281
- proc do |arg|
282
- %w{a b}.map do |x| puts x end
283
- end,
284
- single_arg_expected
285
- ],
286
- __LINE__ => [
287
- lambda { |arg|
288
- %w{a b}.map{|x| puts x }
289
- },
290
- single_arg_expected
291
- ],
292
- __LINE__ => [
293
- Proc.new do |arg|
294
- %w{a b}.map do |x| puts x end
295
- end,
296
- single_arg_expected
297
- ],
298
- __LINE__ => [
299
- Proc.new { |arg|
300
- %w{a b}.map{|x| puts x }
301
- },
302
- single_arg_expected
303
- ],
304
- # ////////////////////////////////////////////////////////////////////////
305
- # >> Partial newlining (multiple args)
306
- # ////////////////////////////////////////////////////////////////////////
307
- __LINE__ => [
308
- lambda do |arg1, arg2|
309
- %w{a b}.map do |x| puts x end
310
- end,
311
- multiple_args_expected
312
- ],
313
- __LINE__ => [
314
- lambda { |arg1, arg2|
315
- %w{a b}.map{|x| puts x }
316
- },
317
- multiple_args_expected
318
- ],
319
- __LINE__ => [
320
- proc do |arg1, arg2|
321
- %w{a b}.map do |x| puts x end
322
- end,
323
- multiple_args_expected
324
- ],
325
- __LINE__ => [
326
- lambda { |arg1, arg2|
327
- %w{a b}.map{|x| puts x }
328
- },
329
- multiple_args_expected
330
- ],
331
- __LINE__ => [
332
- Proc.new do |arg1, arg2|
333
- %w{a b}.map do |x| puts x end
334
- end,
335
- multiple_args_expected
336
- ],
337
- __LINE__ => [
338
- Proc.new { |arg1, arg2|
339
- %w{a b}.map{|x| puts x }
340
- },
341
- multiple_args_expected
342
- ],
343
- # ////////////////////////////////////////////////////////////////////////
344
- # >> Partial newlining (unlimited args)
345
- # ////////////////////////////////////////////////////////////////////////
346
- __LINE__ => [
347
- lambda do |*args|
348
- %w{a b}.map do |x| puts x end
349
- end,
350
- unlimited_args_expected
351
- ],
352
- __LINE__ => [
353
- lambda { |*args|
354
- %w{a b}.map{|x| puts x }
355
- },
356
- unlimited_args_expected
357
- ],
358
- __LINE__ => [
359
- proc do |*args|
360
- %w{a b}.map do |x| puts x end
361
- end,
362
- unlimited_args_expected
363
- ],
364
- __LINE__ => [
365
- lambda { |*args|
366
- %w{a b}.map{|x| puts x }
367
- },
368
- unlimited_args_expected
369
- ],
370
- __LINE__ => [
371
- Proc.new do |*args|
372
- %w{a b}.map do |x| puts x end
373
- end,
374
- unlimited_args_expected
375
- ],
376
- __LINE__ => [
377
- Proc.new { |*args|
378
- %w{a b}.map{|x| puts x }
379
- },
380
- unlimited_args_expected
381
- ],
382
- # ////////////////////////////////////////////////////////////////////////
383
- # >> Partial newlining (no args)
384
- # ////////////////////////////////////////////////////////////////////////
385
- __LINE__ => [
386
- lambda do
387
- %w{a b}.map do |x| puts x end
388
- end,
389
- no_arg_expected
390
- ],
391
- __LINE__ => [
392
- lambda {
393
- %w{a b}.map{|x| puts x }
394
- },
395
- no_arg_expected
396
- ],
397
- __LINE__ => [
398
- proc do
399
- %w{a b}.map do |x| puts x end
400
- end,
401
- no_arg_expected
402
- ],
403
- __LINE__ => [
404
- lambda {
405
- %w{a b}.map{|x| puts x }
406
- },
407
- no_arg_expected
408
- ],
409
- __LINE__ => [
410
- Proc.new do
411
- %w{a b}.map do |x| puts x end
412
- end,
413
- no_arg_expected
414
- ],
415
- __LINE__ => [
416
- Proc.new {
417
- %w{a b}.map{|x| puts x }
418
- },
419
- no_arg_expected
420
- ],
421
- # ////////////////////////////////////////////////////////////////////////
422
- # >> No newlining (single arg)
423
- # ////////////////////////////////////////////////////////////////////////
424
- __LINE__ => [
425
- lambda do |arg| %w{a b}.map do |x| puts x end end,
426
- single_arg_expected
427
- ],
428
- __LINE__ => [
429
- lambda { |arg| %w{a b}.map{|x| puts x } },
430
- single_arg_expected
431
- ],
432
- __LINE__ => [
433
- proc do |arg| %w{a b}.map do |x| puts x end end,
434
- single_arg_expected
435
- ],
436
- __LINE__ => [
437
- lambda { |arg| %w{a b}.map{|x| puts x } },
438
- single_arg_expected
439
- ],
440
- __LINE__ => [
441
- Proc.new do |arg| %w{a b}.map do |x| puts x end end,
442
- single_arg_expected
443
- ],
444
- __LINE__ => [
445
- Proc.new { |arg| %w{a b}.map{|x| puts x } },
446
- single_arg_expected
447
- ],
448
- # ////////////////////////////////////////////////////////////////////////
449
- # >> No newlining (multiple args)
450
- # ////////////////////////////////////////////////////////////////////////
451
- __LINE__ => [
452
- lambda do |arg1, arg2| %w{a b}.map do |x| puts x end end,
453
- multiple_args_expected
454
- ],
455
- __LINE__ => [
456
- lambda { |arg1, arg2| %w{a b}.map{|x| puts x } },
457
- multiple_args_expected
458
- ],
459
- __LINE__ => [
460
- proc do |arg1, arg2| %w{a b}.map do |x| puts x end end,
461
- multiple_args_expected
462
- ],
463
- __LINE__ => [
464
- lambda { |arg1, arg2| %w{a b}.map{|x| puts x } },
465
- multiple_args_expected
466
- ],
467
- __LINE__ => [
468
- Proc.new do |arg1, arg2| %w{a b}.map do |x| puts x end end,
469
- multiple_args_expected
470
- ],
471
- __LINE__ => [
472
- Proc.new { |arg1, arg2| %w{a b}.map{|x| puts x } },
473
- multiple_args_expected
474
- ],
475
- # ////////////////////////////////////////////////////////////////////////
476
- # >> No newlining (unlimited args)
477
- # ////////////////////////////////////////////////////////////////////////
478
- __LINE__ => [
479
- lambda do |*args| %w{a b}.map do |x| puts x end end,
480
- unlimited_args_expected
481
- ],
482
- __LINE__ => [
483
- lambda { |*args| %w{a b}.map{|x| puts x } },
484
- unlimited_args_expected
485
- ],
486
- __LINE__ => [
487
- proc do |*args| %w{a b}.map do |x| puts x end end,
488
- unlimited_args_expected
489
- ],
490
- __LINE__ => [
491
- lambda { |*args| %w{a b}.map{|x| puts x } },
492
- unlimited_args_expected
493
- ],
494
- __LINE__ => [
495
- Proc.new do |*args| %w{a b}.map do |x| puts x end end,
496
- unlimited_args_expected
497
- ],
498
- __LINE__ => [
499
- Proc.new { |*args| %w{a b}.map{|x| puts x } },
500
- unlimited_args_expected
501
- ],
502
- # ////////////////////////////////////////////////////////////////////////
503
- # >> No newlining (no args)
504
- # ////////////////////////////////////////////////////////////////////////
505
- __LINE__ => [
506
- lambda do %w{a b}.map do |x| puts x end end,
507
- no_arg_expected
508
- ],
509
- __LINE__ => [
510
- lambda { %w{a b}.map{|x| puts x } },
511
- no_arg_expected
512
- ],
513
- __LINE__ => [
514
- proc do %w{a b}.map do |x| puts x end end,
515
- no_arg_expected
516
- ],
517
- __LINE__ => [
518
- lambda { %w{a b}.map{|x| puts x } },
519
- no_arg_expected
520
- ],
521
- __LINE__ => [
522
- Proc.new do %w{a b}.map do |x| puts x end end,
523
- no_arg_expected
524
- ],
525
- __LINE__ => [
526
- Proc.new { %w{a b}.map{|x| puts x } },
527
- no_arg_expected
528
- ],
529
- }.each do |debug, (block, expected)|
530
- should "handle proc variable [##{debug}]" do
531
- magic_proc = Otaku::Handler::MagicProc.new(block)
532
- encoded = Otaku::Encoder.encode(magic_proc).gsub('|','\|')
533
- context = Otaku::Handler.new({:processor => block}, lambda{}).context
534
- context.code.should.equal \
535
- 'Class.new{ %s }.new' % "def processor; Encoder.decode(%|#{encoded}|).eval!; end"
536
- should_have_expected_context(context, expected, debug.succ)
537
- end
538
- end
539
-
540
- # No args
541
-
542
- should "handle block using do ... end [##{__LINE__}]" do
543
- should_have_expected_context((
544
- new_otaku_handler(1, 2) do
545
- %w{a b}.map{|x| puts x }
546
- end
547
- ).context, no_arg_expected, __LINE__ - 3)
548
- end
549
-
550
- should "handle block using do ... end [##{__LINE__}]" do
551
- should_have_expected_context((
552
- new_otaku_handler do
553
- %w{a b}.map{|x| puts x }
554
- end
555
- ).context, no_arg_expected, __LINE__ - 3)
556
- end
557
-
558
- should "handle block using do ... end [##{__LINE__}]" do
559
- should_have_expected_context((
560
- new_otaku_handler 1, 2 do
561
- %w{a b}.map{|x| puts x }
562
- end
563
- ).context, no_arg_expected, __LINE__ - 3)
564
- end
565
-
566
- should "handle block using do ... end [##{__LINE__}]" do
567
- should_have_expected_context((
568
- new_otaku_handler(1, 2) do %w{a b}.map{|x| puts x } end
569
- ).context, no_arg_expected, __LINE__ - 1)
570
- end
571
-
572
- should "handle block using do ... end [##{__LINE__}]" do
573
- should_have_expected_context((
574
- new_otaku_handler do %w{a b}.map{|x| puts x } end
575
- ).context, no_arg_expected, __LINE__ - 1)
576
- end
577
-
578
- should "handle block using do ... end [##{__LINE__}]" do
579
- should_have_expected_context((
580
- new_otaku_handler 1, 2 do %w{a b}.map{|x| puts x } end
581
- ).context, no_arg_expected, __LINE__ - 1)
582
- end
583
-
584
- should "handle block using { ... } [##{__LINE__}]" do
585
- should_have_expected_context((
586
- new_otaku_handler(1, 2) {
587
- %w{a b}.map{|x| puts x }
588
- }
589
- ).context, no_arg_expected, __LINE__ - 3)
590
- end
591
-
592
- should "handle block using { ... } [##{__LINE__}]" do
593
- should_have_expected_context((
594
- new_otaku_handler {
595
- %w{a b}.map{|x| puts x }
596
- }
597
- ).context, no_arg_expected, __LINE__ - 3)
598
- end
599
-
600
- should "handle block using { ... } [##{__LINE__}]" do
601
- should_have_expected_context((
602
- new_otaku_handler(1, 2) { %w{a b}.map{|x| puts x } }
603
- ).context, no_arg_expected, __LINE__ - 1)
604
- end
605
-
606
- should "handle block using { ... } [##{__LINE__}]" do
607
- should_have_expected_context((
608
- new_otaku_handler { %w{a b}.map{|x| puts x } }
609
- ).context, no_arg_expected, __LINE__ - 1)
610
- end
611
-
612
- # Single arg
613
-
614
- should "handle block using do ... end [##{__LINE__}]" do
615
- should_have_expected_context((
616
- new_otaku_handler(1, 2) do |arg|
617
- %w{a b}.map{|x| puts x }
618
- end
619
- ).context, single_arg_expected, __LINE__ - 3)
620
- end
621
-
622
- should "handle block using do ... end [##{__LINE__}]" do
623
- should_have_expected_context((
624
- new_otaku_handler do |arg|
625
- %w{a b}.map{|x| puts x }
626
- end
627
- ).context, single_arg_expected, __LINE__ - 3)
628
- end
629
-
630
- should "handle block using do ... end [##{__LINE__}]" do
631
- should_have_expected_context((
632
- new_otaku_handler 1, 2 do |arg|
633
- %w{a b}.map{|x| puts x }
634
- end
635
- ).context, single_arg_expected, __LINE__ - 3)
636
- end
637
-
638
- should "handle block using do ... end [##{__LINE__}]" do
639
- should_have_expected_context((
640
- new_otaku_handler(1, 2) do |arg| %w{a b}.map{|x| puts x } end
641
- ).context, single_arg_expected, __LINE__ - 1)
642
- end
643
-
644
- should "handle block using do ... end [##{__LINE__}]" do
645
- should_have_expected_context((
646
- new_otaku_handler do |arg| %w{a b}.map{|x| puts x } end
647
- ).context, single_arg_expected, __LINE__ - 1)
648
- end
649
-
650
- should "handle block using do ... end [##{__LINE__}]" do
651
- should_have_expected_context((
652
- new_otaku_handler 1, 2 do |arg| %w{a b}.map{|x| puts x } end
653
- ).context, single_arg_expected, __LINE__ - 1)
654
- end
655
-
656
- should "handle block using { ... } [##{__LINE__}]" do
657
- should_have_expected_context((
658
- new_otaku_handler(1, 2) { |arg|
659
- %w{a b}.map{|x| puts x }
660
- }
661
- ).context, single_arg_expected, __LINE__ - 3)
662
- end
663
-
664
- should "handle block using { ... } [##{__LINE__}]" do
665
- should_have_expected_context((
666
- new_otaku_handler { |arg|
667
- %w{a b}.map{|x| puts x }
668
- }
669
- ).context, single_arg_expected, __LINE__ - 3)
670
- end
671
-
672
- should "handle block using { ... } [##{__LINE__}]" do
673
- should_have_expected_context((
674
- new_otaku_handler(1, 2) { |arg| %w{a b}.map{|x| puts x } }
675
- ).context, single_arg_expected, __LINE__ - 1)
676
- end
677
-
678
- should "handle block using { ... } [##{__LINE__}]" do
679
- should_have_expected_context((
680
- new_otaku_handler { |arg| %w{a b}.map{|x| puts x } }
681
- ).context, single_arg_expected, __LINE__ - 1)
682
- end
683
-
684
- # Multiple args
685
-
686
- should "handle block using do ... end [##{__LINE__}]" do
687
- should_have_expected_context((
688
- new_otaku_handler(1, 2) do |arg1, arg2|
689
- %w{a b}.map{|x| puts x }
690
- end
691
- ).context, multiple_args_expected, __LINE__ - 3)
692
- end
693
-
694
- should "handle block using do ... end [##{__LINE__}]" do
695
- should_have_expected_context((
696
- new_otaku_handler do |arg1, arg2|
697
- %w{a b}.map{|x| puts x }
698
- end
699
- ).context, multiple_args_expected, __LINE__ - 3)
700
- end
701
-
702
- should "handle block using do ... end [##{__LINE__}]" do
703
- should_have_expected_context((
704
- new_otaku_handler 1, 2 do |arg1, arg2|
705
- %w{a b}.map{|x| puts x }
706
- end
707
- ).context, multiple_args_expected, __LINE__ - 3)
708
- end
709
-
710
- should "handle block using do ... end [##{__LINE__}]" do
711
- should_have_expected_context((
712
- new_otaku_handler(1, 2) do |arg1, arg2| %w{a b}.map{|x| puts x } end
713
- ).context, multiple_args_expected, __LINE__ - 1)
714
- end
715
-
716
- should "handle block using do ... end [##{__LINE__}]" do
717
- should_have_expected_context((
718
- new_otaku_handler do |arg1, arg2| %w{a b}.map{|x| puts x } end
719
- ).context, multiple_args_expected, __LINE__ - 1)
720
- end
721
-
722
- should "handle block using do ... end [##{__LINE__}]" do
723
- should_have_expected_context((
724
- new_otaku_handler 1, 2 do |arg1, arg2| %w{a b}.map{|x| puts x } end
725
- ).context, multiple_args_expected, __LINE__ - 1)
726
- end
727
-
728
- should "handle block using { ... } [##{__LINE__}]" do
729
- should_have_expected_context((
730
- new_otaku_handler(1, 2) { |arg1, arg2|
731
- %w{a b}.map{|x| puts x }
732
- }
733
- ).context, multiple_args_expected, __LINE__ - 3)
734
- end
735
-
736
- should "handle block using { ... } [##{__LINE__}]" do
737
- should_have_expected_context((
738
- new_otaku_handler { |arg1, arg2|
739
- %w{a b}.map{|x| puts x }
740
- }
741
- ).context, multiple_args_expected, __LINE__ - 3)
742
- end
743
-
744
- should "handle block using { ... } [##{__LINE__}]" do
745
- should_have_expected_context((
746
- new_otaku_handler(1, 2) { |arg1, arg2| %w{a b}.map{|x| puts x } }
747
- ).context, multiple_args_expected, __LINE__ - 1)
748
- end
749
-
750
- should "handle block using { ... } [##{__LINE__}]" do
751
- should_have_expected_context((
752
- new_otaku_handler { |arg1, arg2| %w{a b}.map{|x| puts x } }
753
- ).context, multiple_args_expected, __LINE__ - 1)
754
- end
755
-
756
- # Unlimited args
757
-
758
- should "handle block using do ... end [##{__LINE__}]" do
759
- should_have_expected_context((
760
- new_otaku_handler(1, 2) do |*args|
761
- %w{a b}.map{|x| puts x }
762
- end
763
- ).context, unlimited_args_expected, __LINE__ - 3)
764
- end
765
-
766
- should "handle block using do ... end [##{__LINE__}]" do
767
- should_have_expected_context((
768
- new_otaku_handler do |*args|
769
- %w{a b}.map{|x| puts x }
770
- end
771
- ).context, unlimited_args_expected, __LINE__ - 3)
772
- end
773
-
774
- should "handle block using do ... end [##{__LINE__}]" do
775
- should_have_expected_context((
776
- new_otaku_handler 1, 2 do |*args|
777
- %w{a b}.map{|x| puts x }
778
- end
779
- ).context, unlimited_args_expected, __LINE__ - 3)
780
- end
781
-
782
- should "handle block using do ... end [##{__LINE__}]" do
783
- should_have_expected_context((
784
- new_otaku_handler(1, 2) do |*args| %w{a b}.map{|x| puts x } end
785
- ).context, unlimited_args_expected, __LINE__ - 1)
786
- end
787
-
788
- should "handle block using do ... end [##{__LINE__}]" do
789
- should_have_expected_context((
790
- new_otaku_handler do |*args| %w{a b}.map{|x| puts x } end
791
- ).context, unlimited_args_expected, __LINE__ - 1)
792
- end
793
-
794
- should "handle block using do ... end [##{__LINE__}]" do
795
- should_have_expected_context((
796
- new_otaku_handler 1, 2 do |*args| %w{a b}.map{|x| puts x } end
797
- ).context, unlimited_args_expected, __LINE__ - 1)
798
- end
799
-
800
- should "handle block using { ... } [##{__LINE__}]" do
801
- should_have_expected_context((
802
- new_otaku_handler(1, 2) { |*args|
803
- %w{a b}.map{|x| puts x }
804
- }
805
- ).context, unlimited_args_expected, __LINE__ - 3)
806
- end
807
-
808
- should "handle block using { ... } [##{__LINE__}]" do
809
- should_have_expected_context((
810
- new_otaku_handler { |*args|
811
- %w{a b}.map{|x| puts x }
812
- }
813
- ).context, unlimited_args_expected, __LINE__ - 3)
814
- end
815
-
816
- should "handle block using { ... } [##{__LINE__}]" do
817
- should_have_expected_context((
818
- new_otaku_handler(1, 2) { |*args| %w{a b}.map{|x| puts x } }
819
- ).context, unlimited_args_expected, __LINE__ - 1)
820
- end
821
-
822
- should "handle block using { ... } [##{__LINE__}]" do
823
- should_have_expected_context((
824
- new_otaku_handler { |*args| %w{a b}.map{|x| puts x } }
825
- ).context, unlimited_args_expected, __LINE__ - 1)
826
- end
827
-
828
- end
829
-
830
- end
831
-
832
- describe '>> initializing @processor' do
833
-
834
- expected = "lambda { |watever| [\"a\", \"b\"].map { |x| puts(x) } }"
835
-
836
- {
837
- # ////////////////////////////////////////////////////////////////////////
838
- # >> Always newlinling
839
- # ////////////////////////////////////////////////////////////////////////
840
- __LINE__ => (
841
- lambda do |watever|
842
- %w{a b}.map do |x|
843
- puts x
844
- end
845
- end
846
- ),
847
- __LINE__ => (
848
- lambda { |watever|
849
- %w{a b}.map{|x|
850
- puts x
851
- }
852
- }
853
- ),
854
- __LINE__ => (
855
- proc do |watever|
856
- %w{a b}.map do |x|
857
- puts x
858
- end
859
- end
860
- ),
861
- __LINE__ => (
862
- lambda { |watever|
863
- %w{a b}.map{|x|
864
- puts x
865
- }
866
- }
867
- ),
868
- __LINE__ => (
869
- Proc.new do |watever|
870
- %w{a b}.map do |x|
871
- puts x
872
- end
873
- end
874
- ),
875
- __LINE__ => (
876
- Proc.new { |watever|
877
- %w{a b}.map{|x|
878
- puts x
879
- }
880
- }
881
- ),
882
- # ////////////////////////////////////////////////////////////////////////
883
- # >> Partial newlining
884
- # ////////////////////////////////////////////////////////////////////////
885
- __LINE__ => (
886
- lambda do |watever|
887
- %w{a b}.map do |x| puts x end
888
- end
889
- ),
890
- __LINE__ => (
891
- lambda { |watever|
892
- %w{a b}.map{|x| puts x }
893
- }
894
- ),
895
- __LINE__ => (
896
- proc do |watever|
897
- %w{a b}.map do |x| puts x end
898
- end
899
- ),
900
- __LINE__ => (
901
- lambda { |watever|
902
- %w{a b}.map{|x| puts x }
903
- }
904
- ),
905
- __LINE__ => (
906
- Proc.new do |watever|
907
- %w{a b}.map do |x| puts x end
908
- end
909
- ),
910
- __LINE__ => (
911
- Proc.new { |watever|
912
- %w{a b}.map{|x| puts x }
913
- }
914
- ),
915
- # ////////////////////////////////////////////////////////////////////////
916
- # >> No newlining
917
- # ////////////////////////////////////////////////////////////////////////
918
- __LINE__ => (
919
- lambda do |watever| %w{a b}.map do |x| puts x end end
920
- ),
921
- __LINE__ => (
922
- lambda { |watever| %w{a b}.map{|x| puts x } }
923
- ),
924
- __LINE__ => (
925
- proc do |watever| %w{a b}.map do |x| puts x end end
926
- ),
927
- __LINE__ => (
928
- lambda { |watever| %w{a b}.map{|x| puts x } }
929
- ),
930
- __LINE__ => (
931
- Proc.new do |watever| %w{a b}.map do |x| puts x end end
932
- ),
933
- __LINE__ => (
934
- Proc.new { |watever| %w{a b}.map{|x| puts x } }
935
- ),
936
- }.each do |debug, block|
937
- should "handle proc as variable [##{debug}]" do
938
- Otaku::Handler.new({}, block).processor.code.should.equal(expected)
939
- end
940
- end
941
-
942
- should "handle block using do ... end [##{__LINE__}]" do
943
- Otaku.start({}) do |watever|
944
- %w{a b}.map do |x|
945
- puts x
946
- end
947
- end.processor.code.should.equal(expected)
948
- end
949
-
950
- should "handle block using do ... end [##{__LINE__}]" do
951
- Otaku.start do |watever|
952
- %w{a b}.map do |x|
953
- puts x
954
- end
955
- end.processor.code.should.equal(expected)
956
- end
957
-
958
- should "handle block using do ... end [##{__LINE__}]" do
959
- Otaku.start({}) do |watever|
960
- %w{a b}.map do |x| puts x end
961
- end.processor.code.should.equal(expected)
962
- end
963
-
964
- should "handle block using do ... end [##{__LINE__}]" do
965
- Otaku.start do |watever|
966
- %w{a b}.map do |x| puts x end
967
- end.processor.code.should.equal(expected)
968
- end
969
-
970
- should "handle block using do ... end [##{__LINE__}]" do
971
- Otaku.start({}) do |watever| %w{a b}.map do |x| puts x end end.
972
- processor.code.should.equal(expected)
973
- end
974
-
975
- should "handle block using do ... end [##{__LINE__}]" do
976
- Otaku.start do |watever| %w{a b}.map do |x| puts x end end.
977
- processor.code.should.equal(expected)
978
- end
979
-
980
- should "handle block using { ... } [##{__LINE__}]" do
981
- Otaku.start({}) { |watever|
982
- %w{a b}.map do |x|
983
- puts x
984
- end
985
- }.processor.code.should.equal(expected)
986
- end
987
-
988
- should "handle block using { ... } [##{__LINE__}]" do
989
- Otaku.start { |watever|
990
- %w{a b}.map do |x|
991
- puts x
992
- end
993
- }.processor.code.should.equal(expected)
994
- end
995
-
996
- should "handle block using { ... } [##{__LINE__}]" do
997
- Otaku.start({}) { |watever|
998
- %w{a b}.map { |x| puts x }
999
- }.processor.code.should.equal(expected)
1000
- end
1001
-
1002
- should "handle block using { ... } [##{__LINE__}]" do
1003
- Otaku.start { |watever|
1004
- %w{a b}.map { |x| puts x }
1005
- }.processor.code.should.equal(expected)
1006
- end
1007
-
1008
- should "handle block using { ... } [##{__LINE__}]" do
1009
- Otaku.start({}) { |watever| %w{a b}.map { |x| puts x } }.processor.code.should.equal(expected)
1010
- end
1011
-
1012
- should "handle block using { ... } [##{__LINE__}]" do
1013
- Otaku.start { |watever| %w{a b}.map { |x| puts x } }.processor.code.should.equal(expected)
1014
- end
1015
-
1016
- should "leave __FILE__ as __FILE__ [##{__LINE__}]" do
1017
- Otaku.start { |watever| __FILE__ }.processor.code.should.
1018
- equal("lambda { |watever| __FILE__ }" % File.expand_path('spec/handler_spec.rb'))
1019
- end
1020
-
1021
- should "leave __LINE__ as __LINE__ [##{__LINE__}]" do
1022
- Otaku.start { |watever| __LINE__ }.processor.code.should.
1023
- equal("lambda { |watever| __LINE__ }")
1024
- end
1025
-
1026
- should "handle block with context variable as proc [##{__LINE__}]" do
1027
- Otaku.start(:processor => lambda {'dummy'}) { |watever|
1028
- %w{a b}.map do |x|
1029
- puts x
1030
- end
1031
- }.processor.code.should.equal(expected)
1032
- end
1033
-
1034
- should "handle block with context variable as proc [##{__LINE__}]" do
1035
- Otaku.start(:processor => proc {'dummy'}) { |watever|
1036
- %w{a b}.map do |x|
1037
- puts x
1038
- end
1039
- }.processor.code.should.equal(expected)
1040
- end
1041
-
1042
- should "handle block with context variable as proc [##{__LINE__}]" do
1043
- Otaku.start(:processor => Proc.new {'dummy'}) { |watever|
1044
- %w{a b}.map do |x|
1045
- puts x
1046
- end
1047
- }.processor.code.should.equal(expected)
1048
- end
1049
-
1050
- should "handle block with context variable as proc [##{__LINE__}]" do
1051
- Otaku.start(:processor => lambda {'dummy'}) do |watever|
1052
- %w{a b}.map do |x|
1053
- puts x
1054
- end
1055
- end.processor.code.should.equal(expected)
1056
- end
1057
-
1058
- should "handle block with context variable as proc [##{__LINE__}]" do
1059
- Otaku.start(:processor => proc {'dummy'}) do |watever|
1060
- %w{a b}.map do |x|
1061
- puts x
1062
- end
1063
- end.processor.code.should.equal(expected)
1064
- end
1065
-
1066
- should "handle block with context variable as proc [##{__LINE__}]" do
1067
- Otaku.start(:processor => proc {|arg| 'dummy'}) do |watever|
1068
- %w{a b}.map do |x|
1069
- puts x
1070
- end
1071
- end.processor.code.should.equal(expected)
1072
- end
1073
-
1074
- should "handle block with context variable as proc [##{__LINE__}]" do
1075
- Otaku.start(:processor => proc {|arg1, arg2| 'dummy'}) do |watever|
1076
- %w{a b}.map do |x|
1077
- puts x
1078
- end
1079
- end.processor.code.should.equal(expected)
1080
- end
1081
-
1082
- should "handle block with context variable as proc [##{__LINE__}]" do
1083
- Otaku.start(:processor => proc {|*args| 'dummy'}) do |watever|
1084
- %w{a b}.map do |x|
1085
- puts x
1086
- end
1087
- end.processor.code.should.equal(expected)
1088
- end
1089
-
1090
- should "handle block with context variable as proc [##{__LINE__}]" do
1091
- Otaku.start(:processor => Proc.new {'dummy'}) do |watever|
1092
- %w{a b}.map do |x|
1093
- puts x
1094
- end
1095
- end.processor.code.should.equal(expected)
1096
- end
1097
-
1098
- end
1099
-
1100
- describe '>> fetching root' do
1101
- should 'return directory of current file' do
1102
- Otaku.start { |watever| __LINE__ }.root.should.equal(File.expand_path(File.dirname(__FILE__)))
1103
- end
1104
- end
1105
-
1106
- describe '>> processing magic variables' do
1107
-
1108
- should "reflect __FILE__ captured when the proc was 1st defined [##{__LINE__}]" do
1109
- Otaku.start(:processor => lambda { __FILE__ }) { |watever| processor.call }.
1110
- process(:fake_data).should.equal(File.expand_path(__FILE__))
1111
- end
1112
-
1113
- should "reflect __FILE__ captured when the proc was 1st defined [##{__LINE__}]" do
1114
- Otaku.start{ |watever| __FILE__ }.process(:fake_data).should.equal(File.expand_path(__FILE__))
1115
- end
1116
-
1117
- should "reflect __LINE__ captured when the proc was 1st defined [##{__LINE__}]" do
1118
- Otaku.start(:processor => lambda { __LINE__ }) { |watever| processor.call }.
1119
- process(:fake_data).should.equal(__LINE__.pred)
1120
- end
1121
-
1122
- should "reflect __LINE__ captured when the proc was 1st defined [##{__LINE__}]" do
1123
- Otaku.start{ |watever| __LINE__ }.process(:fake_data).should.equal(__LINE__)
1124
- end
1125
-
1126
- end
1127
-
1128
- end