sourcify 0.5.0 → 0.6.0.rc1

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.
Files changed (130) hide show
  1. data/.gitignore +1 -0
  2. data/HISTORY.txt +5 -0
  3. data/README.rdoc +86 -3
  4. data/Rakefile +47 -15
  5. data/lib/sourcify.rb +13 -0
  6. data/lib/sourcify/common/parser/converter.rb +29 -0
  7. data/lib/sourcify/common/parser/raw_scanner/comment.rb +23 -0
  8. data/lib/sourcify/common/parser/raw_scanner/counter.rb +43 -0
  9. data/lib/sourcify/common/parser/raw_scanner/dstring.rb +60 -0
  10. data/lib/sourcify/common/parser/raw_scanner/extensions.rb +121 -0
  11. data/lib/sourcify/common/parser/raw_scanner/heredoc.rb +26 -0
  12. data/lib/sourcify/common/parser/source_code.rb +33 -0
  13. data/lib/sourcify/common/ragel/common.rl +5 -0
  14. data/lib/sourcify/common/ragel/expressions.rl +38 -0
  15. data/lib/sourcify/{proc/scanner.rl → common/ragel/machines.rl} +10 -178
  16. data/lib/sourcify/errors.rb +4 -0
  17. data/lib/sourcify/method.rb +134 -0
  18. data/lib/sourcify/method/methods.rb +3 -0
  19. data/lib/sourcify/method/methods/to_raw_source.rb +30 -0
  20. data/lib/sourcify/method/methods/to_sexp.rb +30 -0
  21. data/lib/sourcify/method/methods/to_source.rb +30 -0
  22. data/lib/sourcify/method/parser.rb +104 -0
  23. data/lib/sourcify/method/parser/converter.rb +8 -0
  24. data/lib/sourcify/method/parser/raw_scanner.rb +2494 -0
  25. data/lib/sourcify/method/parser/raw_scanner.rl +144 -0
  26. data/lib/sourcify/method/parser/raw_scanner_extensions.rb +64 -0
  27. data/lib/sourcify/method/parser/scanner.rb +48 -0
  28. data/lib/sourcify/method/parser/source_code.rb +8 -0
  29. data/lib/sourcify/proc.rb +0 -2
  30. data/lib/sourcify/proc/parser.rb +2 -5
  31. data/lib/sourcify/proc/parser/converter.rb +2 -23
  32. data/lib/sourcify/proc/{scanner.rb → parser/raw_scanner.rb} +808 -806
  33. data/lib/sourcify/proc/parser/raw_scanner.rl +149 -0
  34. data/lib/sourcify/proc/parser/raw_scanner_extensions.rb +72 -0
  35. data/lib/sourcify/proc/parser/{code_scanner.rb → scanner.rb} +9 -5
  36. data/lib/sourcify/proc/parser/source_code.rb +2 -27
  37. data/lib/sourcify/version.rb +1 -1
  38. data/spec/method/others_from_def_end_block_spec.rb +49 -0
  39. data/spec/method/others_from_define_method_spec.rb +62 -0
  40. data/spec/method/raw_scanner/block_comment_spec.rb +8 -0
  41. data/spec/method/raw_scanner/double_colons_spec.rb +8 -0
  42. data/spec/method/raw_scanner/double_quote_str_w_interpolation_spec.rb +8 -0
  43. data/spec/method/raw_scanner/double_quote_str_wo_interpolation_spec.rb +8 -0
  44. data/spec/method/raw_scanner/heredoc_w_indent_spec.rb +8 -0
  45. data/spec/method/raw_scanner/heredoc_wo_indent_spec.rb +8 -0
  46. data/spec/method/raw_scanner/kw_block_start_alias1_spec.rb +20 -0
  47. data/spec/method/raw_scanner/kw_block_start_alias2_spec.rb +20 -0
  48. data/spec/method/raw_scanner/per_line_comment_spec.rb +8 -0
  49. data/spec/method/raw_scanner/single_quote_str_spec.rb +8 -0
  50. data/spec/method/raw_scanner/slash_operator_spec.rb +8 -0
  51. data/spec/method/raw_scanner/spec_helper.rb +80 -0
  52. data/spec/method/spec_helper.rb +1 -0
  53. data/spec/method/to_raw_source_spec.rb +31 -0
  54. data/spec/method/to_raw_source_w_specified_strip_enclosure_spec.rb +148 -0
  55. data/spec/method/to_sexp_from_def_end_block_w_variables_spec.rb +46 -0
  56. data/spec/method/to_sexp_from_def_end_block_within_irb_spec.rb +38 -0
  57. data/spec/method/to_sexp_from_define_method_w_multi_blocks_and_specified_attached_to_spec.rb +56 -0
  58. data/spec/method/to_sexp_from_define_method_w_variables_spec.rb +52 -0
  59. data/spec/method/to_sexp_from_define_method_within_irb_spec.rb +42 -0
  60. data/spec/method/to_sexp_w_specified_strip_enclosure_spec.rb +74 -0
  61. data/spec/method/to_source_from_def_end_block_w_19_extras_spec.rb +23 -0
  62. data/spec/method/to_source_from_def_end_block_w_nested_begin_spec.rb +35 -0
  63. data/spec/method/to_source_from_def_end_block_w_nested_case_spec.rb +35 -0
  64. data/spec/method/to_source_from_def_end_block_w_nested_class_spec.rb +51 -0
  65. data/spec/method/to_source_from_def_end_block_w_nested_do_end_block_spec.rb +33 -0
  66. data/spec/method/to_source_from_def_end_block_w_nested_for_spec.rb +126 -0
  67. data/spec/method/to_source_from_def_end_block_w_nested_if_spec.rb +82 -0
  68. data/spec/method/to_source_from_def_end_block_w_nested_literal_keyword_spec.rb +141 -0
  69. data/spec/method/to_source_from_def_end_block_w_nested_method_spec.rb +33 -0
  70. data/spec/method/to_source_from_def_end_block_w_nested_module_spec.rb +59 -0
  71. data/spec/method/to_source_from_def_end_block_w_nested_unless_spec.rb +82 -0
  72. data/spec/method/to_source_from_def_end_block_w_nested_until_spec.rb +179 -0
  73. data/spec/method/to_source_from_def_end_block_w_nested_while_spec.rb +179 -0
  74. data/spec/method/to_source_from_def_end_block_w_singleton_method_spec.rb +19 -0
  75. data/spec/method/to_source_from_def_end_block_within_irb_spec.rb +30 -0
  76. data/spec/method/to_source_from_def_end_w_multi_blocks_and_many_matches_spec.rb +30 -0
  77. data/spec/method/to_source_from_def_end_w_multi_blocks_and_single_match_spec.rb +36 -0
  78. data/spec/method/to_source_from_define_method_w_braced_block_spec.rb +113 -0
  79. data/spec/method/to_source_from_define_method_w_do_end_block_spec.rb +145 -0
  80. data/spec/method/to_source_from_define_method_w_multi_blocks_and_many_matches_spec.rb +56 -0
  81. data/spec/method/to_source_from_define_method_w_multi_blocks_and_single_match_spec.rb +73 -0
  82. data/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_and_many_matches_spec.rb +36 -0
  83. data/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_and_no_match_spec.rb +36 -0
  84. data/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_and_single_match_spec.rb +28 -0
  85. data/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_spec.rb +103 -0
  86. data/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_body_matcher_and_many_matches_spec.rb +36 -0
  87. data/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_body_matcher_and_no_match_spec.rb +36 -0
  88. data/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_body_matcher_and_single_match_spec.rb +28 -0
  89. data/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_ignore_nested_spec.rb +36 -0
  90. data/spec/method/to_source_from_define_method_within_irb_spec.rb +32 -0
  91. data/spec/method/to_source_magic_file_var_spec.rb +176 -0
  92. data/spec/method/to_source_magic_line_var_spec.rb +298 -0
  93. data/spec/method/to_source_w_specified_strip_enclosure_spec.rb +39 -0
  94. data/spec/no_method/unsupported_platform_spec.rb +26 -0
  95. data/spec/proc/raw_scanner/block_comment_spec.rb +8 -0
  96. data/spec/proc/raw_scanner/double_colons_spec.rb +8 -0
  97. data/spec/proc/raw_scanner/double_quote_str_w_interpolation_spec.rb +8 -0
  98. data/spec/proc/raw_scanner/double_quote_str_wo_interpolation_spec.rb +8 -0
  99. data/spec/proc/raw_scanner/heredoc_w_indent_spec.rb +8 -0
  100. data/spec/proc/raw_scanner/heredoc_wo_indent_spec.rb +8 -0
  101. data/spec/proc/raw_scanner/kw_block_start_alias1_spec.rb +20 -0
  102. data/spec/proc/raw_scanner/kw_block_start_alias2_spec.rb +20 -0
  103. data/spec/proc/raw_scanner/per_line_comment_spec.rb +8 -0
  104. data/spec/proc/raw_scanner/single_quote_str_spec.rb +8 -0
  105. data/spec/proc/raw_scanner/slash_operator_spec.rb +8 -0
  106. data/spec/proc/raw_scanner/spec_helper.rb +63 -0
  107. data/spec/{proc_scanner/block_comment_spec.rb → raw_scanner/block_comment_shared_spec.rb} +1 -5
  108. data/spec/{proc_scanner/double_colons_spec.rb → raw_scanner/double_colons_shared_spec.rb} +1 -5
  109. data/spec/{proc_scanner/double_quote_str_w_interpolation_spec.rb → raw_scanner/double_quote_str_w_interpolation_shared_spec.rb} +1 -5
  110. data/spec/{proc_scanner/double_quote_str_wo_interpolation_spec.rb → raw_scanner/double_quote_str_wo_interpolation_shared_spec.rb} +1 -5
  111. data/spec/raw_scanner/heredoc_w_indent_shared_spec.rb +69 -0
  112. data/spec/raw_scanner/heredoc_wo_indent_shared_spec.rb +70 -0
  113. data/spec/{proc_scanner/kw_do_alias1_spec.rb → raw_scanner/kw_block_start_alias1_shared_spec.rb} +10 -26
  114. data/spec/{proc_scanner/kw_do_alias2_spec.rb → raw_scanner/kw_block_start_alias2_shared_spec.rb} +10 -25
  115. data/spec/{proc_scanner/per_line_comment_spec.rb → raw_scanner/per_line_comment_shared_spec.rb} +1 -5
  116. data/spec/raw_scanner/shared_specs.rb +3 -0
  117. data/spec/{proc_scanner/single_quote_str_spec.rb → raw_scanner/single_quote_str_shared_spec.rb} +1 -5
  118. data/spec/{proc_scanner/slash_operator_spec.rb → raw_scanner/slash_operator_shared_spec.rb} +1 -5
  119. data/spec/run_spec.sh +7 -1
  120. data/spec/spec_helper.rb +8 -25
  121. metadata +204 -47
  122. data/.rvmrc +0 -1
  123. data/VERSION +0 -1
  124. data/lib/sourcify/proc/scanner/comment.rb +0 -21
  125. data/lib/sourcify/proc/scanner/counter.rb +0 -44
  126. data/lib/sourcify/proc/scanner/dstring.rb +0 -59
  127. data/lib/sourcify/proc/scanner/extensions.rb +0 -171
  128. data/lib/sourcify/proc/scanner/heredoc.rb +0 -24
  129. data/spec/proc_scanner/heredoc_spec.rb +0 -144
  130. data/spec/proc_scanner/spec_helper.rb +0 -33
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ unless has_parsetree?
4
+ describe "Method's raw scanner > heredoc (w indent)" do
5
+ extend Sourcify::Method::Parser::RawScanner::Spec::GenericSupport
6
+ behaves_like "Heredoc (w indent)"
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ unless has_parsetree?
4
+ describe "Method's raw scanner > heredoc (wo indent)" do
5
+ extend Sourcify::Method::Parser::RawScanner::Spec::GenericSupport
6
+ behaves_like "Heredoc (wo indent)"
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ unless has_parsetree?
4
+ describe "Method's raw scanner > keyword block start alias #1 (incrementing counter by 1)" do
5
+
6
+ extend Sourcify::Method::Parser::RawScanner::Spec::KwBlockStartSupport
7
+ behaves_like "Keyword block start alias #1 (incrementing counter by 1)"
8
+
9
+ kw_block_start_alias1.each do |kw|
10
+ should "increment counter with ... def #{kw} ..." do
11
+ kw_block_start_counter(<<EOL
12
+ aa def #{kw} bb ...
13
+ cc
14
+ EOL
15
+ ).should.equal([2,2])
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ unless has_parsetree?
4
+ describe "Method's raw scanner > keyword block start alias #2 (incrementing counter by 0..1)" do
5
+
6
+ extend Sourcify::Method::Parser::RawScanner::Spec::KwBlockStartSupport
7
+ behaves_like "Keyword block start alias #2 (incrementing counter by 0..1)"
8
+
9
+ kw_block_start_alias2.each do |kw|
10
+ should "increment counter with ... def #{kw} ..." do
11
+ kw_block_start_counter(<<EOL
12
+ aa def #{kw} bb ...
13
+ cc
14
+ EOL
15
+ ).should.equal([1,2])
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ unless has_parsetree?
4
+ describe "Method's raw scanner > per line comment (# ...)" do
5
+ extend Sourcify::Method::Parser::RawScanner::Spec::GenericSupport
6
+ behaves_like "Per line comment (# ...)"
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ unless has_parsetree?
4
+ describe "Method's raw scanner > single quote strings (\', %q & %w)" do
5
+ extend Sourcify::Method::Parser::RawScanner::Spec::GenericSupport
6
+ behaves_like 'Single quote strings (\', %q & %w)'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ unless has_parsetree?
4
+ describe "Method's raw scanner > slash operator" do
5
+ extend Sourcify::Method::Parser::RawScanner::Spec::GenericSupport
6
+ behaves_like 'Slash operator'
7
+ end
8
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../../../raw_scanner/shared_specs', __FILE__)
3
+
4
+ module Sourcify::Method::Parser::RawScanner
5
+
6
+ SCANNER = self
7
+
8
+ class << self
9
+ attr_reader :tokens, :counter
10
+ end
11
+
12
+ module Spec
13
+
14
+ module GenericSupport
15
+ def self.extended(base)
16
+ base.instance_eval do
17
+
18
+ before do
19
+ SCANNER.instance_eval do
20
+ class << self
21
+ alias_method :orig_stop_if_probably_defined_by_proc,
22
+ :stop_if_probably_defined_by_proc
23
+ def stop_if_probably_defined_by_proc; end
24
+ end
25
+ end
26
+ end
27
+
28
+ after do
29
+ SCANNER.instance_eval do
30
+ class << self
31
+ alias_method :stop_if_probably_defined_by_proc,
32
+ :orig_stop_if_probably_defined_by_proc
33
+ end
34
+ end
35
+ end
36
+
37
+ def process(data)
38
+ SCANNER.process(data)
39
+ SCANNER.tokens
40
+ end
41
+
42
+ end
43
+ end
44
+ end
45
+
46
+ module KwBlockStartSupport
47
+ def self.extended(base)
48
+ base.instance_eval do
49
+
50
+ before do
51
+ Extensions::Counter.class_eval do
52
+ alias_method :orig_started?, :started?
53
+ def started?; true; end
54
+ end
55
+ end
56
+
57
+ after do
58
+ Extensions::Counter.class_eval do
59
+ alias_method :started?, :orig_started?
60
+ end
61
+ end
62
+
63
+ def kw_block_start_counter(data)
64
+ SCANNER.process(data)
65
+ SCANNER.counter.counts
66
+ end
67
+
68
+ def kw_block_start_alias1
69
+ %w{class do module begin case module if unless}
70
+ end
71
+
72
+ def kw_block_start_alias2
73
+ %w{while until for}
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'spec_helper')
@@ -0,0 +1,31 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'spec_helper')
2
+
3
+ describe "Method#to_raw_source" do
4
+
5
+ should 'retain comments' do
6
+ def m1
7
+ a = 1
8
+ # i should stay !!
9
+ end
10
+ method(:m1).should.be having_raw_source(%(
11
+ def m1
12
+ a = 1
13
+ # i should stay !!
14
+ end
15
+ ))
16
+ end
17
+
18
+ should 'retain last nil statement' do
19
+ def m2
20
+ a = 1
21
+ nil
22
+ end
23
+ method(:m2).should.be having_raw_source(%(
24
+ def m2
25
+ a = 1
26
+ nil
27
+ end
28
+ ))
29
+ end
30
+
31
+ end
@@ -0,0 +1,148 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'spec_helper')
2
+
3
+ describe 'Method#to_raw_source' do
4
+ describe 'w specified {:strip_enclosure => ...}' do
5
+
6
+ describe '>> w true' do
7
+
8
+ options = {:strip_enclosure => true}
9
+
10
+ should 'strip enclosing def..end wo arg' do
11
+ def m
12
+ # i should stay !!
13
+ end
14
+ method(:m).should.be having_raw_source('# i should stay !!', options)
15
+ end
16
+
17
+ should 'strip enclosing def..end w 1 required arg' do
18
+ def m(aa)
19
+ # i should stay !!
20
+ end
21
+ method(:m).should.be having_raw_source('# i should stay !!', options)
22
+ end
23
+
24
+ should 'strip enclosing def..end w 1 optional arg' do
25
+ def m(aa = nil)
26
+ # i should stay !!
27
+ end
28
+ method(:m).should.be having_raw_source('# i should stay !!', options)
29
+ end
30
+
31
+ should 'strip enclosing def..end w 1 splat arg' do
32
+ def m(*aa)
33
+ # i should stay !!
34
+ end
35
+ method(:m).should.be having_raw_source('# i should stay !!', options)
36
+ end
37
+
38
+ should 'strip enclosing def..end w 1 bracketed arg' do
39
+ def m((aa, bb))
40
+ # i should stay !!
41
+ end
42
+ method(:m).should.be having_raw_source('# i should stay !!', options)
43
+ end
44
+
45
+ should 'strip enclosing def..end w 1 required & 1 optional' do
46
+ def m(aa, bb = nil)
47
+ # i should stay !!
48
+ end
49
+ method(:m).should.be having_raw_source('# i should stay !!', options)
50
+ end
51
+
52
+ should 'strip enclosing def..end w 1 required & 1 splat' do
53
+ def m(aa, *bb)
54
+ # i should stay !!
55
+ end
56
+ method(:m).should.be having_raw_source('# i should stay !!', options)
57
+ end
58
+
59
+ should 'strip enclosing def..end w 1 required & 1 bracketed' do
60
+ def m(aa, (bb, cc))
61
+ # i should stay !!
62
+ end
63
+ method(:m).should.be having_raw_source('# i should stay !!', options)
64
+ end
65
+
66
+ should 'strip enclosing def..end even wo parenthesis' do
67
+ def m aa
68
+ # i should stay !!
69
+ end
70
+ method(:m).should.be having_raw_source('# i should stay !!', options)
71
+ end
72
+
73
+ end
74
+
75
+ describe '>> w false' do
76
+
77
+ options = {:strip_enclosure => false}
78
+
79
+ should 'not strip enclosing def..end wo arg' do
80
+ def m
81
+ # i should stay !!
82
+ end
83
+ method(:m).should.be having_raw_source(%(
84
+ def m
85
+ # i should stay !!
86
+ end
87
+ ), options)
88
+ end
89
+
90
+ should 'not strip enclosing def..end w 1 required arg' do
91
+ def m(aa)
92
+ # i should stay !!
93
+ end
94
+ method(:m).should.be having_raw_source(%(
95
+ def m(aa)
96
+ # i should stay !!
97
+ end
98
+ ), options)
99
+ end
100
+
101
+ should 'not strip enclosing def..end w 1 optional arg' do
102
+ def m(aa = nil)
103
+ # i should stay !!
104
+ end
105
+ method(:m).should.be having_raw_source(%(
106
+ def m(aa = nil)
107
+ # i should stay !!
108
+ end
109
+ ), options)
110
+ end
111
+
112
+ should 'not strip enclosing def..end w 1 splat arg' do
113
+ def m(*aa)
114
+ # i should stay !!
115
+ end
116
+ method(:m).should.be having_raw_source(%(
117
+ def m(*aa)
118
+ # i should stay !!
119
+ end
120
+ ), options)
121
+ end
122
+
123
+ should 'not strip enclosing def..end w 1 bracketed arg' do
124
+ def m((aa, bb))
125
+ # i should stay !!
126
+ end
127
+ method(:m).should.be having_raw_source(%(
128
+ def m((aa, bb))
129
+ # i should stay !!
130
+ end
131
+ ), options)
132
+ end
133
+
134
+ should 'not strip enclosing def..end even wo parenthesis' do
135
+ def m aa
136
+ # i should stay !!
137
+ end
138
+ method(:m).should.be having_raw_source(%(
139
+ def m aa
140
+ # i should stay !!
141
+ end
142
+ ), options)
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+ end
@@ -0,0 +1,46 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'spec_helper')
2
+
3
+ describe 'Method#to_sexp (from def..end block)' do
4
+ describe 'variables' do
5
+
6
+ should 'handle non var' do
7
+ def m1; x; end
8
+ method(:m1).should.be having_sexp(
9
+ s(:defn, :m1, s(:args), s(:scope, s(:block, s(:call, nil, :x, s(:arglist)))))
10
+ )
11
+ end
12
+
13
+ should 'not handle local var' do
14
+ x = 'lx';
15
+ def m2; x; end
16
+ method(:m2).should.be having_sexp(
17
+ s(:defn, :m2, s(:args), s(:scope, s(:block, s(:call, nil, :x, s(:arglist)))))
18
+ )
19
+ end
20
+
21
+ should 'handle instance var' do
22
+ @x = 'ix'
23
+ def m3; @x; end
24
+ method(:m3).should.be having_sexp(
25
+ s(:defn, :m3, s(:args), s(:scope, s(:block, s(:ivar, :@x))))
26
+ )
27
+ end
28
+
29
+ should 'handle class var' do
30
+ @@x = 'cx'
31
+ def m4; @@x; end
32
+ method(:m4).should.be having_sexp(
33
+ s(:defn, :m4, s(:args), s(:scope, s(:block, s(:cvar, :@@x))))
34
+ )
35
+ end
36
+
37
+ should 'handle global var' do
38
+ $x = 'gx'
39
+ def m5; $x; end
40
+ method(:m5).should.be having_sexp(
41
+ s(:defn, :m5, s(:args), s(:scope, s(:block, s(:gvar, :$x))))
42
+ )
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,38 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'spec_helper')
2
+
3
+ describe 'Method#to_sexp (from def..end block)' do
4
+ describe 'within IRB' do
5
+
6
+ class << self
7
+
8
+ def irb_eval(string)
9
+ irb_exec(string)[-1]
10
+ end
11
+
12
+ def equal_to(expected)
13
+ lambda {|found| found == expected }
14
+ end
15
+
16
+ end
17
+
18
+ should 'handle non var' do
19
+ irb_eval(%(
20
+ def m1; x; end
21
+ method(:m1).to_sexp
22
+ )).should.be equal_to(
23
+ s(:defn, :m1, s(:args), s(:scope, s(:block, s(:call, nil, :x, s(:arglist)))))
24
+ )
25
+ end
26
+
27
+ should 'not handle local var' do
28
+ irb_eval(%(
29
+ x = :lx
30
+ def m2; x; end
31
+ method(:m2).to_sexp
32
+ )).should.be equal_to(
33
+ s(:defn, :m2, s(:args), s(:scope, s(:block, s(:call, nil, :x, s(:arglist)))))
34
+ )
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,56 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'spec_helper')
2
+
3
+ describe 'Method#to_sexp (from define_method)' do
4
+ describe 'w specified {:attached_to => ...}' do
5
+
6
+ # NOTE: We just want enough specs to show that handling of :attached_to
7
+ # works for Method#to_sexp as well.
8
+
9
+ before { @thing = Object.new }
10
+ options = {:attached_to => /^.*?(\W|)watever(\W)/}
11
+
12
+ should 'handle no nesting on same line' do
13
+ b1 = lambda {|a| @x1+1 }; b2 = watever { @x1+2 }; b3 = lambda { @x1+3 }
14
+ @thing.class.send(:define_method, :m1, &b2)
15
+ @thing.method(:m1).should.be having_sexp(
16
+ s(:defn,
17
+ :m1,
18
+ s(:args),
19
+ s(:scope, s(:block, s(:call, s(:ivar, :@x1), :+, s(:arglist, s(:lit, 2)))))),
20
+ options
21
+ )
22
+ end
23
+
24
+ should 'handle single level nesting on same line' do
25
+ b1 = lambda {|a| @x2+1 }; b2 = watever { lambda { @x2+2 } }
26
+ @thing.class.send(:define_method, :m2, &b2)
27
+ @thing.method(:m2).should.be having_sexp(
28
+ s(:defn,
29
+ :m2,
30
+ s(:args),
31
+ s(:scope,
32
+ s(:block,
33
+ s(:iter,
34
+ s(:call, nil, :lambda, s(:arglist)),
35
+ nil,
36
+ s(:call, s(:ivar, :@x2), :+, s(:arglist, s(:lit, 2))))))),
37
+ options
38
+ )
39
+ end
40
+
41
+ should 'handle multi level nesting on same line' do
42
+ b2 = (lambda {|a| watever { lambda { @x3 } } }).call(1)
43
+ @thing.class.send(:define_method, :m3, &b2)
44
+ @thing.method(:m3).should.be having_sexp(
45
+ s(:defn,
46
+ :m3,
47
+ s(:args),
48
+ s(:scope,
49
+ s(:block,
50
+ s(:iter, s(:call, nil, :lambda, s(:arglist)), nil, s(:ivar, :@x3))))),
51
+ options
52
+ )
53
+ end
54
+
55
+ end
56
+ end