codemodels-ruby 0.1.5-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +3 -0
  3. data/README.md +6 -0
  4. data/Rakefile +13 -0
  5. data/codemodels-ruby.gemspec +33 -0
  6. data/lib/codemodels/ruby.rb +12 -0
  7. data/lib/codemodels/ruby/code.rb +14 -0
  8. data/lib/codemodels/ruby/info_extraction.rb +100 -0
  9. data/lib/codemodels/ruby/language.rb +17 -0
  10. data/lib/codemodels/ruby/metamodel.rb +481 -0
  11. data/lib/codemodels/ruby/model_building.rb +36 -0
  12. data/lib/codemodels/ruby/parser.rb +809 -0
  13. data/lib/codemodels/ruby/query.rb +25 -0
  14. data/lib/jars/com.google.collect_1.0.0.v201105210816.jar +0 -0
  15. data/lib/jars/com.google.inject_2.0.0.v201105231817.jar +0 -0
  16. data/lib/jars/com.ibm.icu_4.4.2.v20110208.jar +0 -0
  17. data/lib/jars/org.apache.commons.lang_2.4.0.v201005080502.jar +0 -0
  18. data/lib/jars/org.apache.commons.logging_1.1.1.jar +0 -0
  19. data/lib/jars/org.apache.log4j_1.2.16.jar +0 -0
  20. data/lib/jars/org.eclipse.core.runtime.compatibility_3.2.100.v20100505.jar +0 -0
  21. data/lib/jars/org.eclipse.core.runtime_3.7.0.v20110110.jar +0 -0
  22. data/test/data/012_add_comments_permissions.rb +14 -0
  23. data/test/data/darcs_adapter.rb +242 -0
  24. data/test/data/example_of_complex_class.rb +157 -0
  25. data/test/data/issues_helper_test.rb +271 -0
  26. data/test/data/status_test.rb +14 -0
  27. data/test/data/user_custom_field.rb +23 -0
  28. data/test/helper.rb +87 -0
  29. data/test/test_assignments.rb +14 -0
  30. data/test/test_blocks.rb +102 -0
  31. data/test/test_constants.rb +14 -0
  32. data/test/test_exception_handling.rb +35 -0
  33. data/test/test_info.rb +52 -0
  34. data/test/test_logic.rb +61 -0
  35. data/test/test_metamodel.rb +70 -0
  36. data/test/test_modules_and_classes.rb +81 -0
  37. data/test/test_not_variable_assignements.rb +75 -0
  38. data/test/test_operations.rb +440 -0
  39. data/test/test_parsing_complex.rb +49 -0
  40. data/test/test_parsing_literals.rb +92 -0
  41. data/test/test_statements.rb +169 -0
  42. data/test/test_terms_extraction.rb +240 -0
  43. data/test/test_values_extraction.rb +102 -0
  44. data/test/test_variables.rb +81 -0
  45. metadata +272 -0
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestConstants < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include LightModels
7
+
8
+ def test_const_decl
9
+ root = Ruby.parse("MODULE_NAME = 'test'")
10
+ assert_node root,Ruby::ConstantDecl,
11
+ name:'MODULE_NAME', value: Ruby.string('test')
12
+ end
13
+
14
+ end
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+
3
+ class TestExceptionHandling < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include LightModels
7
+
8
+ def test_rescue_empty
9
+ root = Ruby.parse('begin;rescue;end')
10
+ assert_node root, Ruby::BeginEndBlock,
11
+ body: nil
12
+ assert_equal 1,root.rescue_clauses.count
13
+ assert_node root.rescue_clauses[0], Ruby::RescueClause,
14
+ body: nil
15
+ end
16
+
17
+ def test_rescue_full
18
+ root = Ruby.parse('begin;1;rescue;2;end')
19
+ assert_node root, Ruby::BeginEndBlock,
20
+ body: Ruby.int(1)
21
+ assert_equal 1,root.rescue_clauses.count
22
+ assert_node root.rescue_clauses[0], Ruby::RescueClause,
23
+ body: Ruby.int(2)
24
+ end
25
+
26
+ def test_rescue_def_attached
27
+ root = Ruby.parse('def a;1;rescue;2;end')
28
+ assert_node root, Ruby::Def,
29
+ body: Ruby.int(1)
30
+ assert_equal 1,root.rescue_clauses.count
31
+ assert_node root.rescue_clauses[0], Ruby::RescueClause,
32
+ body: Ruby.int(2)
33
+ end
34
+
35
+ end
@@ -0,0 +1,52 @@
1
+ require 'helper'
2
+
3
+ class TestInfoExtraction < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include LightModels
7
+
8
+ def test_id_to_words_empty
9
+ assert_equal [''],Ruby::InfoExtraction.id_to_words('')
10
+ end
11
+
12
+ def test_id_to_words_one_word
13
+ assert_equal ['ciao'],Ruby::InfoExtraction.id_to_words('ciao')
14
+ end
15
+
16
+ def test_id_to_words_starting_separator
17
+ assert_equal ['ciao'],Ruby::InfoExtraction.id_to_words('_ciao')
18
+ end
19
+
20
+ def test_id_to_words_ending_separator
21
+ assert_equal ['ciao'],Ruby::InfoExtraction.id_to_words('ciao_')
22
+ end
23
+
24
+ def test_id_to_words_many_words
25
+ assert_equal ['ciao','come','stai'],Ruby::InfoExtraction.id_to_words('ciao_come_stai')
26
+ end
27
+
28
+ def test_id_to_words_qmark
29
+ assert_equal ['ciao','come','stai'],Ruby::InfoExtraction.id_to_words('ciao_come_stai?')
30
+ end
31
+
32
+ def test_id_to_words_bang
33
+ assert_equal ['ciao','come','stai'],Ruby::InfoExtraction.id_to_words('ciao_come_stai!')
34
+ end
35
+
36
+ def test_id_to_words_equal
37
+ assert_equal ['ciao','come','stai'],Ruby::InfoExtraction.id_to_words('ciao_come_stai=')
38
+ end
39
+
40
+ def test_assignment_method_name_recognized_as_identifier
41
+ assert Ruby::InfoExtraction.is_id_str'ciao='
42
+ end
43
+
44
+ def test_modifier_method_name_recognized_as_identifier
45
+ assert Ruby::InfoExtraction.is_id_str'ciao!'
46
+ end
47
+
48
+ def test_boolean_method_name_recognized_as_identifier
49
+ assert Ruby::InfoExtraction.is_id_str'ciao?'
50
+ end
51
+
52
+ end
@@ -0,0 +1,61 @@
1
+ require 'helper'
2
+
3
+ class TestLogic < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include LightModels
7
+
8
+ def test_and_symbol
9
+ root = Ruby.parse('1 && 2')
10
+ assert_node root, Ruby::AndOperator,
11
+ #word_form: false,
12
+ left: Ruby.int(1),
13
+ right: Ruby.int(2)
14
+ end
15
+
16
+ def test_or_symbol
17
+ root = Ruby.parse('1 || 2')
18
+ assert_node root, Ruby::OrOperator,
19
+ #word_form: false,
20
+ left: Ruby.int(1),
21
+ right: Ruby.int(2)
22
+ end
23
+
24
+ def test_and_word
25
+ root = Ruby.parse('1 and 2')
26
+ assert_node root, Ruby::AndOperator,
27
+ #word_form: true,
28
+ left: Ruby.int(1),
29
+ right: Ruby.int(2)
30
+ end
31
+
32
+ def test_or_word
33
+ root = Ruby.parse('1 or 2')
34
+ assert_node root, Ruby::OrOperator,
35
+ #word_form: true,
36
+ left: Ruby.int(1),
37
+ right: Ruby.int(2)
38
+ end
39
+
40
+ def test_or_local_assignement
41
+ root = Ruby.parse('a||=1')
42
+ assert_node root, Ruby::OrAssignment,
43
+ assigned: Ruby::LocalVarAccess.build( { name: 'a' } ),
44
+ value: Ruby.int(1)
45
+ end
46
+
47
+ def test_or_global_assignement
48
+ root = Ruby.parse('$a||=1')
49
+ assert_node root, Ruby::OrAssignment,
50
+ assigned: Ruby::GlobalVarAccess.build( { name: 'a' } ),
51
+ value: Ruby.int(1)
52
+ end
53
+
54
+ def test_or_instance_assignement
55
+ root = Ruby.parse('@a||=1')
56
+ assert_node root, Ruby::OrAssignment,
57
+ assigned: Ruby::InstanceVarAccess.build( { name: 'a' } ),
58
+ value: Ruby.int(1)
59
+ end
60
+
61
+ end
@@ -0,0 +1,70 @@
1
+ require 'helper'
2
+
3
+ class TestOperations < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include LightModels
7
+
8
+ def test_constant_top_container_single
9
+ a = Ruby.constant('a')
10
+
11
+ assert a.top_container == nil
12
+ end
13
+
14
+ def test_constant_top_container_double
15
+ ab = Ruby.constant('a','b')
16
+
17
+ assert_equal 'a',ab.top_container.name
18
+ end
19
+
20
+ def test_constant_top_container_triple
21
+ abc = Ruby.constant('a','b','c')
22
+
23
+ assert_equal 'a',abc.top_container.name
24
+ end
25
+
26
+ def test_constant_single
27
+ a = Ruby.constant('a')
28
+
29
+ assert_right_class a, Ruby::Constant
30
+ assert_equal 'a',a.name
31
+ assert a.container==nil
32
+ end
33
+
34
+ def test_constant_double
35
+ ab = Ruby.constant('a','b')
36
+
37
+ assert_right_class ab, Ruby::Constant
38
+ assert_equal 'b',ab.name
39
+
40
+ a = ab.container
41
+ assert_right_class a, Ruby::Constant
42
+ assert_equal 'a',a.name
43
+ assert a.container==nil
44
+ end
45
+
46
+ def test_constant_triple
47
+ abc = Ruby.constant('a','b','c')
48
+
49
+ assert_right_class abc, Ruby::Constant
50
+ assert_equal 'c',abc.name
51
+
52
+ ab = abc.container
53
+ assert_right_class ab, Ruby::Constant
54
+ assert_equal 'b',ab.name
55
+
56
+ a = ab.container
57
+ assert_right_class a, Ruby::Constant
58
+ assert_equal 'a',a.name
59
+ assert a.container==nil
60
+ end
61
+
62
+ def test_element_assignment
63
+ assert Ruby.const_defined? :ElementAssignment
64
+ c = Ruby.const_get :ElementAssignment
65
+
66
+ assert_all_attrs [], c
67
+ assert_all_refs ['container','element','value'], c
68
+ end
69
+
70
+ end
@@ -0,0 +1,81 @@
1
+ require 'helper'
2
+
3
+ class TestOperations < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include LightModels
7
+
8
+ def test_class_decl_ext_class_in_module
9
+ root = Ruby.parse("class TestOperations < Test::Unit::TestCase\nend")
10
+
11
+ assert_right_class root, Ruby::ClassDecl
12
+ assert_right_class root.super_class,Ruby::Constant
13
+ assert_equal 'TestCase', root.super_class.name
14
+ assert_right_class root.super_class.container,Ruby::Constant
15
+ assert_equal 'Unit', root.super_class.container.name
16
+ assert_right_class root.super_class.container.container,Ruby::Constant
17
+ assert_equal 'Test', root.super_class.container.container.name
18
+ assert_equal nil, root.super_class.container.container.container
19
+ end
20
+
21
+ def test_class_decl_ext_class_simple
22
+ root = Ruby.parse("class Literal < Value\nend")
23
+
24
+ assert_right_class root, Ruby::ClassDecl
25
+ assert_equal Ruby.constant('Literal'),root.defname
26
+ assert_equal 'Value', root.super_class.name
27
+ assert_equal nil,root.super_class.container
28
+ end
29
+
30
+ def test_class_decl_no_ext
31
+ root = Ruby.parse("class Literal\nend")
32
+
33
+ assert_right_class root, Ruby::ClassDecl
34
+ assert_equal nil,root.super_class
35
+ end
36
+
37
+ def test_class_with_nil_content
38
+ root = Ruby.parse("class Literal\nnil\nend")
39
+
40
+ assert_right_class root, Ruby::ClassDecl
41
+ assert_equal 1,root.contents.count
42
+ assert_right_class root.contents[0],Ruby::NilLiteral
43
+ end
44
+
45
+ def test_class_with_content
46
+ root = Ruby.parse("class AClass\nattr_accessor :name\nend")
47
+
48
+ assert_right_class root, Ruby::ClassDecl
49
+ assert_equal nil,root.super_class
50
+ assert_simple_const root.defname,'AClass'
51
+ assert_equal 1,root.contents.count
52
+ assert_right_class root.contents[0], Ruby::Call
53
+ end
54
+
55
+ def test_module
56
+ root = Ruby.parse('module MyModule;end')
57
+
58
+ assert_right_class root, Ruby::ModuleDecl
59
+ assert_equal 0,root.contents.count
60
+ end
61
+
62
+ def test_self
63
+ root = Ruby.parse('self')
64
+
65
+ assert_node root, Ruby::Self
66
+ end
67
+
68
+ def test_singleton_class
69
+ r = Ruby.parse('class << self; end')
70
+
71
+ assert_node r, Ruby::SingletonClassDecl, contents: [], object: Ruby::Self.new
72
+ end
73
+
74
+ def test_class_extending_struct
75
+ r = Ruby.parse 'class Notifiable < Struct.new(:name, :parent); end'
76
+
77
+ assert_node r, Ruby::ClassDecl
78
+ assert_node r.super_class, Ruby::Call, name: 'new'
79
+ end
80
+
81
+ end
@@ -0,0 +1,75 @@
1
+ require 'helper'
2
+
3
+ class TestNotVariableAssignment < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include LightModels
7
+
8
+ def test_element_assignment
9
+ root = Ruby.parse('models[1] = 2')
10
+ assert_node root,Ruby::ElementAssignment,
11
+ container: Ruby::ExplicitReceiverCall.build(name:'models'),
12
+ element: Ruby.int(1),
13
+ value: Ruby.int(2)
14
+ end
15
+
16
+ def test_element_plus_assignment
17
+ root = Ruby.parse('models[1] += 2')
18
+ assert_node root,Ruby::ElementOperationAssignment,
19
+ container: Ruby::ExplicitReceiverCall.build(name:'models'),
20
+ element: Ruby.int(1),
21
+ value: Ruby.int(2),
22
+ operator:'+'
23
+ end
24
+
25
+ def test_element_minus_assignment
26
+ root = Ruby.parse('models[1] -= 2')
27
+ assert_node root,Ruby::ElementOperationAssignment,
28
+ container: Ruby::ExplicitReceiverCall.build(name:'models'),
29
+ element: Ruby.int(1),
30
+ value: Ruby.int(2),
31
+ operator:'-'
32
+ end
33
+
34
+ def test_element_mul_assignment
35
+ root = Ruby.parse('models[1] *= 2')
36
+ assert_node root,Ruby::ElementOperationAssignment,
37
+ container: Ruby::ExplicitReceiverCall.build(name:'models'),
38
+ element: Ruby.int(1),
39
+ value: Ruby.int(2),
40
+ operator:'*'
41
+ end
42
+
43
+ def test_element_div_assignment
44
+ root = Ruby.parse('models[1] /= 2')
45
+ assert_node root,Ruby::ElementOperationAssignment,
46
+ container: Ruby::ExplicitReceiverCall.build(name:'models'),
47
+ element: Ruby.int(1),
48
+ value: Ruby.int(2),
49
+ operator:'/'
50
+ end
51
+
52
+ def test_multiple_assignment_to_values
53
+ root = Ruby.parse('a,@b,c = 1,2,3')
54
+ assert_node root,Ruby::MultipleAssignment
55
+ assert_equal 3,root.assignments.count
56
+ assert_node root.assignments[0], Ruby::LocalVarAssignment,
57
+ name_assigned:'a',
58
+ value: nil
59
+ assert_node root.assignments[1], Ruby::InstanceVarAssignment,
60
+ name_assigned:'b',
61
+ value: nil
62
+ assert_node root.assignments[2], Ruby::LocalVarAssignment,
63
+ name_assigned:'c',
64
+ value: nil
65
+ assert_equal [Ruby.int(1),Ruby.int(2),Ruby.int(3)],root.values
66
+ end
67
+
68
+ def test_multiple_assignment_to_call
69
+ root = Ruby.parse('@auth_source_pages, @auth_sources = paginate AuthSource, :per_page => 25')
70
+ assert_node root,Ruby::MultipleAssignment
71
+ assert_equal 1,root.values.count
72
+ assert_node root.values[0], Ruby::Call, name: 'paginate'
73
+ end
74
+
75
+ end
@@ -0,0 +1,440 @@
1
+ require 'helper'
2
+
3
+ class TestOperations < Test::Unit::TestCase
4
+
5
+ include TestHelper
6
+ include LightModels
7
+ include LightModels::Ruby
8
+
9
+ def test_sum
10
+ root = Ruby.parse('3+40')
11
+
12
+ assert_right_class root, Ruby::ExplicitReceiverCall
13
+ assert_equal '+', root.name
14
+ assert_is_int root.receiver, 3
15
+ assert_equal 1, root.args.count
16
+ assert_is_int root.args[0], 40
17
+ end
18
+
19
+ def test_def_with_some_statements
20
+ root = Ruby.parse("def somefunc \n 1\n 2\n 3\n end")
21
+
22
+ assert_right_class root, Ruby::Def
23
+ assert_equal 'somefunc', root.name
24
+ assert root.body.is_a? Ruby::Block
25
+ assert_equal 3,root.body.contents.count
26
+ assert_is_int root.body.contents[0], 1
27
+ assert_is_int root.body.contents[1], 2
28
+ assert_is_int root.body.contents[2], 3
29
+ end
30
+
31
+ def test_def_with_one_statements
32
+ root = Ruby.parse("def somefunc \n 10\n end")
33
+
34
+ assert_right_class root, Ruby::Def
35
+ assert_equal 'somefunc', root.name
36
+ assert_is_int root.body, 10
37
+ end
38
+
39
+ def test_require
40
+ root = Ruby.parse("require 'something'")
41
+
42
+ assert_right_class root, Ruby::ImplicitReceiverCall
43
+ assert_equal 'require', root.name
44
+ assert_equal 1, root.args.count
45
+ assert_is_str root.args[0],'something'
46
+ end
47
+
48
+ def test_empty_hash
49
+ root = Ruby.parse('{}')
50
+
51
+ assert_right_class root, Ruby::HashLiteral
52
+ assert_equal 0, root.pairs.count
53
+ end
54
+
55
+ def test_hash_with_pairs
56
+ root = Ruby.parse('{ "a"=>1, "b"=>2}')
57
+
58
+ assert_right_class root, Ruby::HashLiteral
59
+ assert_equal 2, root.pairs.count
60
+ assert_node root.pairs[0], Ruby::HashPair,
61
+ key: Ruby.string('a'),
62
+ value: Ruby.int(1)
63
+ assert_node root.pairs[1], Ruby::HashPair,
64
+ key: Ruby.string('b'),
65
+ value: Ruby.int(2)
66
+ end
67
+
68
+ def test_empty_array
69
+ root = Ruby.parse('[]')
70
+
71
+ assert_right_class root, Ruby::ArrayLiteral
72
+ assert_equal 0, root.values.count
73
+ end
74
+
75
+ def test_array_with_values
76
+ root = Ruby.parse('[1,2]')
77
+
78
+ assert_right_class root, Ruby::ArrayLiteral
79
+ assert_equal 2, root.values.count
80
+ assert_is_int root.values[0], 1
81
+ assert_is_int root.values[1], 2
82
+ end
83
+
84
+ def test_self_def
85
+ root = Ruby.parse('class A;def self.verbose;true;end;end')
86
+
87
+ assert_right_class root,Ruby::ClassDecl
88
+ assert_node root.contents[0], Ruby::SelfDef,
89
+ name: 'verbose',
90
+ body: Ruby::BooleanLiteral.build(true)
91
+ end
92
+
93
+ def test_return_empty
94
+ root = Ruby.parse('return')
95
+ assert_node root,Ruby::Return,
96
+ value:nil
97
+ end
98
+
99
+ def test_return_value
100
+ root = Ruby.parse('return 1')
101
+ assert_node root,Ruby::Return,
102
+ value: Ruby.int(1)
103
+ end
104
+
105
+ def test_true_eq_true
106
+ assert_equal true,Ruby.bool(true)==Ruby.bool(true)
107
+ end
108
+
109
+ def test_false_eq_false
110
+ assert_equal true,Ruby.bool(false)==Ruby.bool(false)
111
+ end
112
+
113
+ def test_true_neq_false
114
+ assert_equal false,Ruby.bool(true)==Ruby.bool(false)
115
+ end
116
+
117
+ # this and the following are more testing emf_jruby than this package...
118
+ def test_eql_constants
119
+ c1 = Ruby.constant 'a','b','c'
120
+ c2 = Ruby.constant 'a','b','c'
121
+
122
+ assert c1.eql?(c2)
123
+ assert c2.eql?(c1)
124
+ assert c1==c2
125
+ assert c2==c1
126
+ end
127
+
128
+ def test_not_eql_constants
129
+ c1 = Ruby.constant 'a','b','c'
130
+ c2 = Ruby.constant 'a','d','c'
131
+
132
+ assert (not (c1.eql?(c2)))
133
+ assert (not (c2.eql?(c1)))
134
+ assert (not (c1==c2))
135
+ assert (not (c2==c1))
136
+ end
137
+
138
+ def test_call_to_super_with_no_params
139
+ r = Ruby.parse('def mymethod;super;end')
140
+
141
+ call_to_super = r.body
142
+ assert_node call_to_super,Ruby::CallToSuper
143
+ assert_equal 0,call_to_super.args.count
144
+ end
145
+
146
+ def test_global_scope_ref
147
+ r = Ruby.parse('::FooBar')
148
+
149
+ assert_node r, Ruby::GlobalScopeReference,
150
+ name: 'FooBar'
151
+ end
152
+
153
+ def test_constant_through_global_scope_ref
154
+ r = Ruby.parse('::ActionView::MissingTemplate')
155
+
156
+ assert_node r, Ruby::Constant,
157
+ name: 'MissingTemplate'
158
+ assert_node r.container, Ruby::GlobalScopeReference,
159
+ name: 'ActionView'
160
+ end
161
+
162
+ def test_splat
163
+ r = Ruby.parse("a = *args").value
164
+ assert_node r,Ruby::Splat
165
+ assert_node r.splatted, Ruby::Call, name:'args'
166
+ end
167
+
168
+ def test_unary_minus
169
+ r = Ruby.parse('-a')
170
+ assert_node r, Ruby::UnaryOperation, operator_name: '-'
171
+ assert_node r.value, Ruby::Call, name:'a'
172
+ end
173
+
174
+ def test_retry
175
+ r = Ruby.parse('retry')
176
+ assert_node r, Ruby::RetryStatement
177
+ end
178
+
179
+ def test_regex_matcher
180
+ r = Ruby.parse('k =~ /^extra_/')
181
+
182
+ assert_node r, Ruby::RegexMatcher, regex: Ruby::StaticRegExpLiteral.build('^extra_')
183
+ assert_node r.checked_value, Ruby::Call, name: 'k'
184
+ end
185
+
186
+ def test_regex_tryer
187
+ r = Ruby.parse('/^extra_/ =~ k')
188
+
189
+ assert_node r, Ruby::RegexTryer, regex: Ruby::StaticRegExpLiteral.build('^extra_')
190
+ assert_node r.checked_value, Ruby::Call, name: 'k'
191
+ end
192
+
193
+ def test_range
194
+ r = Ruby.parse('1..2')
195
+ assert_node r, Ruby::Range, lower: Ruby.int(1), upper: Ruby.int(2)
196
+ end
197
+
198
+ def test_yield
199
+ r = Ruby.parse('yield')
200
+ assert_node r, Ruby::YieldStatement
201
+ end
202
+
203
+ def test_def_with_ensure
204
+ r = Ruby.parse('def a;2;ensure;1;end')
205
+ assert_node r, Ruby::Def, body: Ruby.int(2), ensure_body: Ruby.int(1)
206
+ end
207
+
208
+ def test_def_on_self_with_block_and_ensure
209
+ code = "def self.with_deliveries(enabled = true, &block)
210
+ was_enabled = ActionMailer::Base.perform_deliveries
211
+ ActionMailer::Base.perform_deliveries = !!enabled
212
+ yield
213
+ ensure
214
+ ActionMailer::Base.perform_deliveries = was_enabled
215
+ end"
216
+ r = Ruby.parse(code)
217
+ assert_node r, Ruby::Def, name:'with_deliveries'
218
+ assert_not_nil r.ensure_body
219
+ assert_node r.ensure_body, Ruby::Assignment
220
+ end
221
+
222
+ def test_begin_ensure_block
223
+ r = Ruby.parse('begin;1;ensure;2;end')
224
+ assert_node r,Ruby::BeginEndBlock, body: Ruby.int(1), ensure_body: Ruby.int(2)
225
+ end
226
+
227
+ def test_next
228
+ r = Ruby.parse('next')
229
+ assert_node r, Ruby::NextStatement
230
+ end
231
+
232
+ def test_nth_group_ref
233
+ r = Ruby.parse('$1')
234
+ assert_node r, Ruby::NthGroupReference, n: 1
235
+ end
236
+
237
+ def test_back_ref
238
+ r = Ruby.parse('$&')
239
+ assert_node r, Ruby::BackReference
240
+ end
241
+
242
+ def test_super_call
243
+ r = Ruby.parse('super(1,2)')
244
+ assert_node r, Ruby::SuperCall, args: [Ruby.int(1),Ruby.int(2)]
245
+ end
246
+
247
+ def test_argscat_at_the_end_after_three
248
+ r = Ruby.parse('link_to(name, options, html_options, *parameters_for_method_reference)')
249
+
250
+ assert_equal 4,r.args.count
251
+ assert_node r.args[0], Ruby::Call, name: 'name'
252
+ assert_node r.args[1], Ruby::Call, name: 'options'
253
+ assert_node r.args[2], Ruby::Call, name: 'html_options'
254
+ assert_node r.args[3], Ruby::Splat
255
+ assert_node r.args[3].splatted, Ruby::Call, name: 'parameters_for_method_reference'
256
+ end
257
+
258
+ def test_argspush_at_the_beginning_before_three
259
+ r = Ruby.parse('link_to(*parameters_for_method_reference, name, options, html_options)')
260
+
261
+ assert_equal 4,r.args.count
262
+ assert_node r.args[1], Ruby::Call, name: 'name'
263
+ assert_node r.args[2], Ruby::Call, name: 'options'
264
+ assert_node r.args[3], Ruby::Call, name: 'html_options'
265
+ assert_node r.args[0], Ruby::Splat
266
+ assert_node r.args[0].splatted, Ruby::Call, name: 'parameters_for_method_reference'
267
+ end
268
+
269
+ def test_argscat_on_empty_array_at_the_end_after_three
270
+ r = Ruby.parse('link_to(name, options, html_options, *[])')
271
+
272
+ assert_equal 4,r.args.count
273
+ assert_node r.args[0], Ruby::Call, name: 'name'
274
+ assert_node r.args[1], Ruby::Call, name: 'options'
275
+ assert_node r.args[2], Ruby::Call, name: 'html_options'
276
+ assert_node r.args[3], Ruby::Splat
277
+ assert_node r.args[3].splatted, Ruby::ArrayLiteral
278
+ end
279
+
280
+ def test_argspush_on_empty_array_at_the_beginning_before_three
281
+ r = Ruby.parse('link_to(*[], name, options, html_options)')
282
+
283
+ assert_equal 4,r.args.count
284
+ assert_node r.args[1], Ruby::Call, name: 'name'
285
+ assert_node r.args[2], Ruby::Call, name: 'options'
286
+ assert_node r.args[3], Ruby::Call, name: 'html_options'
287
+ assert_node r.args[0], Ruby::Splat
288
+ assert_node r.args[0].splatted, Ruby::ArrayLiteral
289
+ end
290
+
291
+ def test_five_before_and_after_splat_arg
292
+ r = Ruby.parse('link_to(0,1,2,3,4,*5,6,7,8,9,10)')
293
+ assert_equal 11,r.args.count
294
+ assert_equal r.args[0], Ruby.int(0)
295
+ assert_equal r.args[1], Ruby.int(1)
296
+ assert_equal r.args[2], Ruby.int(2)
297
+ assert_equal r.args[3], Ruby.int(3)
298
+ assert_equal r.args[4], Ruby.int(4)
299
+ assert_equal r.args[5].splatted, Ruby.int(5)
300
+ assert_equal r.args[6], Ruby.int(6)
301
+ assert_equal r.args[7], Ruby.int(7)
302
+ assert_equal r.args[8], Ruby.int(8)
303
+ assert_equal r.args[9], Ruby.int(9)
304
+ assert_equal r.args[10], Ruby.int(10)
305
+ end
306
+
307
+ def test_five_before_and_after_with_array_as_splat_arg
308
+ r = Ruby.parse('link_to(0,1,2,3,4,*[],6,7,8,9,10)')
309
+ assert_equal 11,r.args.count
310
+ assert_equal r.args[0], Ruby.int(0)
311
+ assert_equal r.args[1], Ruby.int(1)
312
+ assert_equal r.args[2], Ruby.int(2)
313
+ assert_equal r.args[3], Ruby.int(3)
314
+ assert_equal r.args[4], Ruby.int(4)
315
+ assert_equal r.args[5].splatted, Ruby::ArrayLiteral.new
316
+ assert_equal r.args[6], Ruby.int(6)
317
+ assert_equal r.args[7], Ruby.int(7)
318
+ assert_equal r.args[8], Ruby.int(8)
319
+ assert_equal r.args[9], Ruby.int(9)
320
+ assert_equal r.args[10], Ruby.int(10)
321
+ end
322
+
323
+ def test_five_before_and_after_with_array_as_splat_arg_as_some_of_other_args
324
+ r = Ruby.parse('link_to([],1,2,3,4,*[],6,7,8,[],10)')
325
+ assert_equal 11,r.args.count
326
+ assert_equal r.args[0], Ruby::ArrayLiteral.new
327
+ assert_equal r.args[1], Ruby.int(1)
328
+ assert_equal r.args[2], Ruby.int(2)
329
+ assert_equal r.args[3], Ruby.int(3)
330
+ assert_equal r.args[4], Ruby.int(4)
331
+ assert_equal r.args[5].splatted, Ruby::ArrayLiteral.new
332
+ assert_equal r.args[6], Ruby.int(6)
333
+ assert_equal r.args[7], Ruby.int(7)
334
+ assert_equal r.args[8], Ruby.int(8)
335
+ assert_equal r.args[9], Ruby::ArrayLiteral.new
336
+ assert_equal r.args[10], Ruby.int(10)
337
+ end
338
+
339
+ def test_parsing_array_containing_splat_at_end
340
+ r = Ruby.parse("[1, *2]")
341
+
342
+ assert_node r, Ruby::ArrayLiteral, values: [Ruby.int(1), Ruby.splat(Ruby.int(2))]
343
+ end
344
+
345
+ def test_parsing_array_containing_splat_at_start
346
+ r = Ruby.parse("[*1, 2]")
347
+
348
+ assert_node r, Ruby::ArrayLiteral, values: [Ruby.splat(Ruby.int(1)), Ruby.int(2)]
349
+ end
350
+
351
+ def test_parsing_array_containing_splat_in_the_middle
352
+ r = Ruby.parse("[1, *2, 3]")
353
+
354
+ assert_node r, Ruby::ArrayLiteral, values: [Ruby.int(1), Ruby.splat(Ruby.int(2)), Ruby.int(3)]
355
+ end
356
+
357
+ def test_multiple_assignment_in_block_args
358
+ r = Ruby.parse('m() { |params, (key, value)| 1}')
359
+
360
+ assert_node r.block_arg, Ruby::CodeBlock
361
+ assert_equal 2, r.block_arg.args.count
362
+ assert_node r.block_arg.args[0], Ruby::Argument, name:'params'
363
+ assert_node r.block_arg.args[1], Ruby::SplittedArgument, names:['key','value']
364
+ end
365
+
366
+ def splatted_array
367
+ splatted = Ruby::Splat.new
368
+ splatted.splatted = Ruby::ArrayLiteral.new
369
+ splatted
370
+ end
371
+
372
+ def test_args_cat_one
373
+ r = Ruby.parse('m(1,*[])')
374
+ assert_equal [Ruby.int(1),splatted_array], r.args
375
+ end
376
+
377
+ def test_args_cat_two
378
+ r = Ruby.parse('m(1,2,*[])')
379
+ assert_equal [Ruby.int(1),Ruby.int(2),splatted_array], r.args
380
+ end
381
+
382
+ def test_args_push_one
383
+ r = Ruby.parse('m(*[],1)')
384
+ assert_equal [splatted_array,Ruby.int(1)], r.args
385
+ end
386
+
387
+ def test_args_push_two
388
+ r = Ruby.parse('m(*[],1,2)')
389
+ assert_equal [splatted_array,Ruby.int(1),Ruby.int(2)], r.args
390
+ end
391
+
392
+ def test_args_cat_or_push_zero
393
+ r = Ruby.parse('m(*[])')
394
+ assert_equal [splatted_array], r.args
395
+ end
396
+
397
+ def test_args_cat_and_push_two
398
+ r = Ruby.parse('m(1,2,*[],3,4)')
399
+ assert_equal [Ruby.int(1),Ruby.int(2),splatted_array,Ruby.int(3),Ruby.int(4)],r.args
400
+ end
401
+
402
+ def test_def_formal_args_with_default_value
403
+ code = %q{
404
+ def mymethod(a=1, b=nil, c={})
405
+ end
406
+ }
407
+ r = Ruby.parse_code(code)
408
+ assert_node r,InstanceDef,name:'mymethod'
409
+ assert_equal 3,r.formal_args.count
410
+ assert_node r.formal_args[0],FormalArgument,name:'a',default_value:Ruby.int(1)
411
+ assert_node r.formal_args[1],FormalArgument,name:'b',default_value:NilLiteral.new
412
+ assert_node r.formal_args[2],FormalArgument,name:'c',default_value:HashLiteral.new
413
+ end
414
+
415
+ def test_def_formal_args_without_default_value
416
+ code = %q{
417
+ def mymethod(a, b, c)
418
+ end
419
+ }
420
+ r = Ruby.parse_code(code)
421
+ assert_node r,InstanceDef,name:'mymethod'
422
+ assert_equal 3,r.formal_args.count
423
+ assert_node r.formal_args[0],FormalArgument,name:'a',default_value:nil
424
+ assert_node r.formal_args[1],FormalArgument,name:'b',default_value:nil
425
+ assert_node r.formal_args[2],FormalArgument,name:'c',default_value:nil
426
+ end
427
+
428
+ def test_def_formal_args_with_splat_at_end
429
+ code = %q{
430
+ def mymethod(a, *b)
431
+ end
432
+ }
433
+ r = Ruby.parse_code(code)
434
+ assert_node r,InstanceDef,name:'mymethod'
435
+ assert_equal 2,r.formal_args.count
436
+ assert_node r.formal_args[0],FormalArgument,name:'a',default_value:nil
437
+ assert_node r.formal_args[1],FormalArgument,name:'b',default_value:nil
438
+ end
439
+
440
+ end