This repository was archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest.rb
More file actions
executable file
·292 lines (254 loc) · 7.57 KB
/
Copy pathtest.rb
File metadata and controls
executable file
·292 lines (254 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env ruby
# Test solutions to New Relic's metaprogramming counter challenge.
#
# ./tmp/program.rb is auto generated by the test cases.
require 'test/unit'
require 'fileutils'
SOLUTION_FILE = ARGV[0]
unless SOLUTION_FILE
warn "usage #$0 <SOLUTION_FILE>"
exit 1
end
class MetaCounterTest < Test::Unit::TestCase
def workspace
File.expand_path(File.dirname(__FILE__) + '/tmp')
end
def program_path
workspace + '/program.rb'
end
def solution_path
SOLUTION_FILE
end
def create_program(source)
FileUtils.mkdir_p(workspace)
File.open(program_path, 'w') do |f|
f.print source
end
end
def assert_sig_and_count(output, method_signature, count)
# assert there's a line of output like "String#to_s called 10 times"
regexp = /#{Regexp.escape(method_signature)} .*\b#{Regexp.escape(count.to_s)}\b/
assert output.match(regexp),
"Expected output to include '#{method_signature} called #{count} times'\nGot:\n#{output}"
end
def execute_program(count_calls_to, source)
create_program(source)
`COUNT_CALLS_TO='#{count_calls_to}' ruby -r #{solution_path} #{program_path} 2>&1`
end
def test_standard_lib_class_and_method
output = execute_program('String#to_s', <<-RB)
10.times{ "foo".to_s }
RB
assert_sig_and_count(output, 'String#to_s', 10)
end
def test_method_defined_in_class
output = execute_program('A#b', <<-RB)
class A; def b; end; end
10.times{ A.new.b }
RB
assert_sig_and_count(output, 'A#b', 10)
end
def test_nested_constant_lookup
output = execute_program('A::B::C::D::E#b', <<-RB)
class A; class B; class C; class D; class E; def b; end; end; end; end; end; end
10.times{ A::B::C::D::E.new.b }
RB
assert_sig_and_count(output, 'A::B::C::D::E#b', 10)
end
def test_method_inherited_by_class
output = execute_program('B#b', <<-RB)
class A; def b; end; end
class B < A; end
10.times{ B.new.b }
RB
assert_sig_and_count(output, 'B#b', 10)
end
def test_method_inherited_by_class_and_counted_on_ancestor
output = execute_program('A#b', <<-RB)
class A; def b; end; end
class B < A; end
10.times{ B.new.b }
RB
assert_sig_and_count(output, 'A#b', 10)
end
def test_method_inherited_by_class_then_overridden_and_counted_on_ancestor
output = execute_program('A#b', <<-RB)
class A; def b; end; end
class B < A; end
10.times{ B.new.b }
class B; def b; end; end
10.times{ B.new.b }
RB
assert_sig_and_count(output, 'A#b', 10)
end
def test_method_inherited_by_class_then_overridden
output = execute_program('B#b', <<-RB)
class A; def b; end; end
class B < A; end
10.times{ B.new.b }
class B; def b; end; end
10.times{ B.new.b }
RB
assert_sig_and_count(output, 'B#b', 20)
end
def test_method_included_via_module
output = execute_program('B#b', <<-RB)
module A; def b; end; end
class B; include A; end
10.times{ B.new.b }
RB
assert_sig_and_count(output, 'B#b', 10)
end
def test_method_included_via_module_and_counted_through_module
output = execute_program('A#b', <<-RB)
module A; def b; end; end
class B; include A; end
10.times{ B.new.b }
RB
assert_sig_and_count(output, 'A#b', 10)
end
def test_method_extended_into_object
output = execute_program('B#b', <<-RB)
module A; def b; end; end
class B; include A; end
b = B.new
b.extend A
10.times{ b.b }
RB
assert_sig_and_count(output, 'B#b', 10)
end
def test_method_extended_into_object_and_counted_through_module
output = execute_program('A#b', <<-RB)
module A; def b; end; end
class B; include A; end
b = B.new
b.extend A
10.times{ b.b }
RB
assert_sig_and_count(output, 'A#b', 10)
end
# This one is tricky because it's a weird symbol (which makes
# alias_method_chaining hard) and it's often used in the instrumentation
# itself to increment a counter (leading to stack overflow).
def test_integer_addition
output = execute_program('Fixnum#+', <<-RB)
10.times{ |i| i + i}
RB
assert_sig_and_count(output, 'Fixnum#+', 10)
end
def test_subtraction
output = execute_program('Fixnum#-', <<-RB)
10.times{ |i| i - i}
RB
assert_sig_and_count(output, 'Fixnum#-', 10)
end
def test_division
output = execute_program('Fixnum#/', <<-RB)
10.times{ |i| i / 2}
RB
assert_sig_and_count(output, 'Fixnum#/', 10)
end
def test_multiplication
output = execute_program('Fixnum#*', <<-RB)
10.times{ |i| i * 2}
RB
assert_sig_and_count(output, 'Fixnum#*', 10)
end
def test_question_method
output = execute_program('Fixnum#odd?', <<-RB)
10.times{ |i| i.odd? }
RB
assert_sig_and_count(output, 'Fixnum#odd?', 10)
end
def test_bang_method
output = execute_program('String#sub!', <<-RB)
10.times{ |i| i.to_s.sub! '1', '2'}
RB
assert_sig_and_count(output, 'String#sub!', 10)
end
def test_bracket_method
output = execute_program('Fixnum#[]', <<-RB)
10.times{ |i| i[0]}
RB
assert_sig_and_count(output, 'Fixnum#[]', 10)
end
def test_class_method
output = execute_program('A.b', <<-RB)
class A; def self.b; end; end
10.times{ A.b}
RB
assert_sig_and_count(output, 'A.b', 10)
end
def test_class_method_inherited
output = execute_program('B.b', <<-RB)
class A; def self.b; end; end
class B < A; end
10.times{ B.b}
RB
assert_sig_and_count(output, 'B.b', 10)
end
def test_class_method_extended
output = execute_program('B.b', <<-RB)
module A; def b; end; end
class B; extend A end
10.times{ B.b}
RB
assert_sig_and_count(output, 'B.b', 10)
end
def test_module_extends_self_then_defines_method
output = execute_program('A.b', <<-RB)
module A; extend self; def b; end; end
10.times{ A.b}
RB
assert_sig_and_count(output, 'A.b', 10)
end
def test_module_extends_self_then_defines_method_counted_at_instance_level
output = execute_program('A#b', <<-RB)
module A; extend self; def b; end; end
10.times{ A.b}
RB
assert_sig_and_count(output, 'A#b', 10)
end
def test_module_defines_method_then_extends_self
output = execute_program('A.b', <<-RB)
module A; def b; end; extend self; end
10.times{ A.b}
RB
assert_sig_and_count(output, 'A.b', 10)
end
def test_module_method
output = execute_program('A.b', <<-RB)
module A; def self.b; end; end
10.times{ A.b}
RB
assert_sig_and_count(output, 'A.b', 10)
end
def test_works_with_single_argument_methods
output = execute_program('A#b', <<-RB)
class A; def b(a); puts "I got " + a.inspect ; end; end
A.new.b 'a sandwich!'
RB
assert output.include?('I got "a sandwich!"'), "Got:\n #{output}"
end
def test_works_with_multiple_argument_methods
output = execute_program('A#b', <<-RB)
class A; def b(a, b, c); puts "I got " + [a, b, c].join(' ') ; end; end
A.new.b 'a', 'ham', 'sandwich!'
RB
assert output.include?('I got a ham sandwich!'), "Got:\n #{output}"
end
def test_works_with_var_argument_methods
output = execute_program('A#b', <<-RB)
class A; def b(*a); puts "I got " + a.join(' ') ; end; end
A.new.b 'a', 'ham', 'sandwich!'
RB
assert output.include?('I got a ham sandwich!'), "Got:\n #{output}"
end
def test_works_with_block_argument_methods
output = execute_program('A#b', <<-RB)
class A; def b(*a); yield a ; end; end
A.new.b('a', 'ham', 'sandwich!'){|*a| puts "I got " + a.join(' ')}
RB
assert output.include?('I got a ham sandwich!'), "Got:\n #{output}"
end
end