ruby-internal 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,229 +0,0 @@
1
- require 'test/unit'
2
- require 'timeout'
3
-
4
- dir = File.dirname(__FILE__)
5
- $:.unshift(dir) if not $:.include?(dir)
6
- $:.unshift("#{dir}/../lib") if not $:.include?("#{dir}/../lib")
7
- $:.unshift("#{dir}/../ext") if not $:.include?("#{dir}/../ext")
8
-
9
- require 'internal/node/as_expression'
10
- require 'internal/method/as_expression'
11
- require 'internal/proc/as_expression'
12
-
13
- require 'expression_samples'
14
-
15
- $stdout.sync = true
16
- $stderr.sync = true
17
-
18
- class TC_As_Expression < Test::Unit::TestCase
19
- extend Test::Unit::Assertions
20
-
21
- # Some of the samples use this
22
- def foo(*a, &b)
23
- return b.call(1, 2) if b
24
- return a
25
- end
26
-
27
- EXPRESSION_SAMPLES.each do |name, code|
28
- p = proc {
29
- p_orig = eval("proc { #{code} }")
30
- code_new = p_orig.body.as_expression
31
- # p code, code_new
32
- p_new = eval("proc { #{code_new} }")
33
- result_orig = result_new = nil
34
- exc_orig = exc_new = nil
35
- timeout(1) { begin; result_orig = p_orig.call; rescue; exc_orig = $!; end }
36
- timeout(1) { begin; result_new = p_new.call; rescue; exc_new = $!; end }
37
- assert_equal(result_orig, result_new)
38
- assert_equal(exc_orig.class, exc_new.class)
39
- if exc_orig and exc_new then
40
- assert_equal(exc_orig.message, exc_new.message)
41
- end
42
- }
43
- define_method "test_#{name}", p
44
- end
45
-
46
- def initialize(test_method_name)
47
- # TODO: This seems to be the only way to get tests defined with #
48
- # define_method to run on 1.8.1 and earlier.
49
- catch(:invalid_test) { super(test_method_name) }
50
- end
51
-
52
- def method_no_args
53
- 42
54
- end
55
-
56
- def test_method_no_args_as_expression
57
- m = method(:method_no_args)
58
- assert_equal 'def method_no_args(); 42; end', m.as_expression
59
- end
60
-
61
- def method_one_arg(a)
62
- 42
63
- end
64
-
65
- def test_method_one_arg_as_expression
66
- m = method(:method_one_arg)
67
- assert_equal 'def method_one_arg(a); 42; end', m.as_expression
68
- end
69
-
70
- def method_two_args(a, b)
71
- 42
72
- end
73
-
74
- def test_method_two_args_as_expression
75
- m = method(:method_two_args)
76
- assert_equal 'def method_two_args(a, b); 42; end', m.as_expression
77
- end
78
-
79
- def method_rest_arg(*rest)
80
- 42
81
- end
82
-
83
- def test_method_rest_arg_as_expression
84
- m = method(:method_rest_arg)
85
- assert_equal 'def method_rest_arg(*rest); 42; end', m.as_expression
86
- end
87
-
88
- def method_block_arg(&block)
89
- 42
90
- end
91
-
92
- def test_method_block_arg_as_expression
93
- m = method(:method_block_arg)
94
- assert_equal 'def method_block_arg(&block); 42; end', m.as_expression
95
- end
96
-
97
- def method_rest_and_block_arg(*rest, &block)
98
- 42
99
- end
100
-
101
- def test_method_rest_and_block_arg_as_expression
102
- m = method(:method_rest_and_block_arg)
103
- assert_equal 'def method_rest_and_block_arg(*rest, &block); 42; end', m.as_expression
104
- end
105
-
106
- def method_two_args_and_rest_and_block_arg(a, b, *rest, &block)
107
- 42
108
- end
109
-
110
- def test_method_two_args_and_rest_and_block_arg_as_expression
111
- m = method(:method_two_args_and_rest_and_block_arg)
112
- assert_equal 'def method_two_args_and_rest_and_block_arg(a, b, *rest, &block); 42; end', m.as_expression
113
- end
114
-
115
- def method_two_default_args(a = 1, b = 2)
116
- 42
117
- end
118
-
119
- def test_method_two_default_args_as_expression
120
- m = method(:method_two_default_args)
121
- assert_equal('def method_two_default_args(a=1, b=2); 42; end', m.as_expression)
122
- end
123
-
124
- def method_with_body(a, b)
125
- a + b
126
- end
127
-
128
- def test_method_with_body_as_expression
129
- m = method(:method_with_body)
130
- assert_equal 'def method_with_body(a, b); a + b; end', m.as_expression
131
- end
132
-
133
- def test_proc_no_args_as_expression
134
- p = proc { }
135
- assert_equal 'proc { }', p.as_expression
136
- end
137
-
138
- def test_proc_empty_args_as_expression
139
- if not defined?(RubyVM) then
140
- # indistinguishable from proc { } on YARV
141
- p = proc { || }
142
- assert_equal 'proc { || }', p.as_expression
143
- end
144
- end
145
-
146
- def test_proc_one_arg_as_expression
147
- p = proc { |a| }
148
- assert_equal 'proc { |a| }', p.as_expression
149
- end
150
-
151
- def test_proc_one_array_arg_as_expression
152
- p = proc { |a,| }
153
- assert_equal 'proc { |a,| }', p.as_expression
154
- end
155
-
156
- def test_proc_two_args_as_expression
157
- p = proc { |a, b| }
158
- assert_equal 'proc { |a, b| }', p.as_expression
159
- end
160
-
161
- def test_proc_rest_arg_as_expression
162
- p = proc { |*rest| }
163
- assert_equal 'proc { |*rest| }', p.as_expression
164
- end
165
-
166
- def test_proc_two_args_and_rest_arg_as_expression
167
- p = proc { |a, b, *rest| }
168
- assert_equal 'proc { |a, b, *rest| }', p.as_expression
169
- end
170
-
171
- def test_proc_with_body_as_expression
172
- p = proc { |a, b| a + b }
173
- assert_equal 'proc { |a, b| a + b }', p.as_expression
174
- end
175
-
176
- def setup
177
- @foo = 42
178
- end
179
-
180
- # 1.7 and later
181
- def foo(*args)
182
- return args
183
- end
184
-
185
- def remove_foo
186
- self.class.remove_foo
187
- end
188
-
189
- # 1.6
190
- def self.foo(*args)
191
- return args
192
- end
193
-
194
- def self.remove_foo
195
- self.class_eval { remove_const(:FOO) if const_defined?(:FOO) }
196
- end
197
-
198
- @@foo = 10
199
-
200
- FOO = 57
201
-
202
- class ObjectWithFooAccessor
203
- attr_accessor :foo
204
- end
205
-
206
- OWFA = ObjectWithFooAccessor.new
207
- end
208
-
209
- if __FILE__ == $0 then
210
- require 'test/unit/ui/console/testrunner'
211
-
212
- if Test::Unit.const_defined?(:AutoRunner) then
213
- exit Test::Unit::AutoRunner.run
214
- else
215
- if ARGV.empty? then
216
- suite = TC_As_Expression.suite
217
- else
218
- suite = Test::Unit::TestSuite.new('TC_As_Expression')
219
- TC_As_Expression.suite.tests.each do |test|
220
- ARGV.each do |arg|
221
- suite << test if /#{arg}/ =~ test.name
222
- end
223
- end
224
- end
225
- result = Test::Unit::UI::Console::TestRunner.run(suite)
226
- exit(result.error_count + result.failure_count)
227
- end
228
- end
229
-
@@ -1,267 +0,0 @@
1
- require 'test/unit'
2
-
3
- dir = File.dirname(__FILE__)
4
- $:.unshift(dir) if not $:.include?(dir)
5
- $:.unshift("#{dir}/../lib") if not $:.include?("#{dir}/../lib")
6
- $:.unshift("#{dir}/../ext") if not $:.include?("#{dir}/../ext")
7
-
8
- require 'internal/method/signature'
9
-
10
- $stdout.sync = true
11
- $stderr.sync = true
12
-
13
- class TC_Methodsig < Test::Unit::TestCase
14
- def check_args(expected_arg_info, sig)
15
- expected_arg_info.each do |name, value|
16
- assert_equal name, sig.args[name].name
17
- assert_equal value, sig.args[name].to_s
18
- if value =~ /=/ then
19
- default = value.sub(/.*=/, '')
20
- assert_equal default, sig.args[name].default
21
- assert_equal true, sig.args[name].optional?
22
- assert_equal false, sig.args[name].required?
23
- assert_equal false, sig.args[name].rest?
24
- assert_equal false, sig.args[name].block?
25
- elsif ?* == value[0] then
26
- assert_equal nil, sig.args[name].default
27
- assert_equal true, sig.args[name].optional?
28
- assert_equal false, sig.args[name].required?
29
- assert_equal true, sig.args[name].rest?
30
- assert_equal false, sig.args[name].block?
31
- elsif ?& == value[0] then
32
- assert_equal nil, sig.args[name].default
33
- assert_equal true, sig.args[name].optional?
34
- assert_equal false, sig.args[name].required?
35
- assert_equal false, sig.args[name].rest?
36
- assert_equal true, sig.args[name].block?
37
- else
38
- assert_equal nil, sig.args[name].default
39
- assert_equal false, sig.args[name].optional?
40
- assert_equal true, sig.args[name].required?
41
- assert_equal false, sig.args[name].rest?
42
- assert_equal false, sig.args[name].block?
43
- end
44
- end
45
- assert_equal expected_arg_info.size, sig.args.size
46
- end
47
-
48
- def no_args_no_parens
49
- end
50
-
51
- def test_no_args_no_parens
52
- name = :no_args_no_parens
53
- sig = method(name).signature
54
- assert_equal self.class, sig.origin_class
55
- assert_equal name.to_s, sig.name
56
- assert_equal [], sig.arg_names
57
- check_args({}, sig)
58
- assert_equal "#{self.class.name}##{name}()", sig.to_s
59
- end
60
-
61
- def no_args_with_parens()
62
- end
63
-
64
- def test_no_args_with_parens
65
- name = :no_args_with_parens
66
- sig = method(name).signature
67
- assert_equal self.class, sig.origin_class
68
- assert_equal name.to_s, sig.name
69
- assert_equal [], sig.arg_names
70
- check_args({}, sig)
71
- assert_equal "#{self.class.name}##{name}()", sig.to_s
72
- end
73
-
74
- def one_arg(a)
75
- end
76
-
77
- def test_one_arg
78
- name = :one_arg
79
- sig = method(name).signature
80
- assert_equal self.class, sig.origin_class
81
- assert_equal name.to_s, sig.name
82
- assert_equal [:a], sig.arg_names
83
- check_args({:a => "a"}, sig)
84
- assert_equal "#{self.class.name}##{name}(a)", sig.to_s
85
- end
86
-
87
- def two_args(a, b)
88
- end
89
-
90
- def test_two_args
91
- name = :two_args
92
- sig = method(name).signature
93
- assert_equal self.class, sig.origin_class
94
- assert_equal name.to_s, sig.name
95
- assert_equal [:a, :b], sig.arg_names
96
- check_args({:a=>"a", :b=>"b"}, sig)
97
- assert_equal "#{self.class.name}##{name}(a, b)", sig.to_s
98
- end
99
-
100
- def two_args_with_locals(a, b)
101
- x = 1
102
- y = 2
103
- end
104
-
105
- def test_two_args_with_locals
106
- name = :two_args_with_locals
107
- sig = method(name).signature
108
- assert_equal self.class, sig.origin_class
109
- assert_equal name.to_s, sig.name
110
- assert_equal [:a, :b], sig.arg_names
111
- check_args({:a=>"a", :b=>"b"}, sig)
112
- assert_equal "#{self.class.name}##{name}(a, b)", sig.to_s
113
- end
114
-
115
- def three_args(a, b, c)
116
- end
117
-
118
- def test_three_args
119
- name = :three_args
120
- sig = method(name).signature
121
- assert_equal self.class, sig.origin_class
122
- assert_equal name.to_s, sig.name
123
- assert_equal [:a, :b, :c], sig.arg_names
124
- check_args({:a=>"a", :b=>"b", :c=>"c"}, sig)
125
- assert_equal "#{self.class.name}##{name}(a, b, c)", sig.to_s
126
- end
127
-
128
- def block_arg(&b)
129
- end
130
-
131
- def test_block_arg
132
- name = :block_arg
133
- sig = method(name).signature
134
- assert_equal self.class, sig.origin_class
135
- assert_equal name.to_s, sig.name
136
- assert_equal [:b], sig.arg_names
137
- check_args({:b=>"&b"}, sig)
138
- assert_equal "#{self.class.name}##{name}(&b)", sig.to_s
139
- end
140
-
141
- def rest_arg(*r)
142
- end
143
-
144
- def test_rest_arg
145
- name = :rest_arg
146
- sig = method(name).signature
147
- assert_equal self.class, sig.origin_class
148
- assert_equal name.to_s, sig.name
149
- assert_equal [:r], sig.arg_names
150
- check_args({:r=>"*r"}, sig)
151
- assert_equal "#{self.class.name}##{name}(*r)", sig.to_s
152
- end
153
-
154
- def block_and_rest_arg(*r, &b)
155
- end
156
-
157
- def test_block_and_rest_arg
158
- name = :block_and_rest_arg
159
- sig = method(name).signature
160
- assert_equal self.class, sig.origin_class
161
- assert_equal name.to_s, sig.name
162
- assert_equal [:r, :b], sig.arg_names
163
- check_args({:r=>"*r", :b=>"&b"}, sig)
164
- assert_equal "#{self.class.name}##{name}(*r, &b)", sig.to_s
165
- end
166
-
167
- def default_arg(a = 1)
168
- end
169
-
170
- def test_default_arg
171
- name = :default_arg
172
- sig = method(name).signature
173
- assert_equal self.class, sig.origin_class
174
- assert_equal name.to_s, sig.name
175
- assert_equal [:a], sig.arg_names
176
- check_args({:a=>"a=1"}, sig)
177
- assert_equal "#{self.class.name}##{name}(a=1)", sig.to_s
178
- end
179
-
180
- def default_and_nondefault_arg(a, b = 42)
181
- end
182
-
183
- def test_default_and_nondefault_arg
184
- name = :default_and_nondefault_arg
185
- sig = method(name).signature
186
- assert_equal self.class, sig.origin_class
187
- assert_equal name.to_s, sig.name
188
- assert_equal [:a, :b], sig.arg_names
189
- check_args({:a=>"a", :b=>"b=42"}, sig)
190
- assert_equal "#{self.class.name}##{name}(a, b=42)", sig.to_s
191
- end
192
-
193
- def default_and_rest(a = 42, *r)
194
- end
195
-
196
- def test_default_and_rest
197
- name = :default_and_rest
198
- sig = method(name).signature
199
- assert_equal self.class, sig.origin_class
200
- assert_equal name.to_s, sig.name
201
- assert_equal [:a, :r], sig.arg_names
202
- check_args({:a=>"a=42", :r=>"*r"}, sig)
203
- assert_equal "#{self.class.name}##{name}(a=42, *r)", sig.to_s
204
- end
205
-
206
- def default_and_rest_and_block(a = 42, *r, &b)
207
- end
208
-
209
- def test_default_and_rest_and_block
210
- name = :default_and_rest_and_block
211
- sig = method(name).signature
212
- assert_equal self.class, sig.origin_class
213
- assert_equal name.to_s, sig.name
214
- assert_equal [:a, :r, :b], sig.arg_names
215
- check_args({:a=>"a=42", :r=>"*r", :b=>"&b"}, sig)
216
- assert_equal "#{self.class.name}##{name}(a=42, *r, &b)", sig.to_s
217
- end
218
-
219
- def nondefault_and_default_and_rest_and_block(a, b = 42, *r, &block)
220
- end
221
-
222
- def test_nondefault_and_default_and_rest_and_block
223
- name = :nondefault_and_default_and_rest_and_block
224
- sig = method(name).signature
225
- assert_equal self.class, sig.origin_class
226
- assert_equal name.to_s, sig.name
227
- assert_equal [:a, :b, :r, :block], sig.arg_names
228
- check_args({:a=>"a", :b=>"b=42", :r=>"*r", :block=>"&block"}, sig)
229
- assert_equal "#{self.class.name}##{name}(a, b=42, *r, &block)", sig.to_s
230
- end
231
-
232
- def nondefault_and_default_and_rest_and_block_with_locals(a, b = 42, *r, &block)
233
- x = 1
234
- y = 2
235
- end
236
-
237
- def test_nondefault_and_default_and_rest_and_block_with_locals
238
- name = :nondefault_and_default_and_rest_and_block_with_locals
239
- sig = method(name).signature
240
- assert_equal self.class, sig.origin_class
241
- assert_equal name.to_s, sig.name
242
- assert_equal [:a, :b, :r, :block], sig.arg_names
243
- check_args({:a=>"a", :b=>"b=42", :r=>"*r", :block=>"&block"}, sig)
244
- assert_equal "#{self.class.name}##{name}(a, b=42, *r, &block)", sig.to_s
245
- end
246
-
247
- def three_default_and_rest_and_block_with_locals(a = 42, b = 43, c = 44, *r, &block)
248
- x = 1
249
- y = 2
250
- end
251
-
252
- def test_three_default_and_rest_and_block_with_locals
253
- name = :three_default_and_rest_and_block_with_locals
254
- sig = method(name).signature
255
- assert_equal self.class, sig.origin_class
256
- assert_equal name.to_s, sig.name
257
- assert_equal [:a, :b, :c, :r, :block], sig.arg_names
258
- check_args({:a=>"a=42", :b=>"b=43", :c=>"c=44", :r=>"*r", :block=>"&block"}, sig)
259
- assert_equal "#{self.class.name}##{name}(a=42, b=43, c=44, *r, &block)", sig.to_s
260
- end
261
- end
262
-
263
- if __FILE__ == $0 then
264
- require 'test_helpers'
265
- run_all_tests()
266
- end
267
-