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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
# File 'lib/pretentious/generator.rb', line 12
def self.impostor_for(module_space, klass)
newStandInKlass = Class.new()
name = klass.name
return klass if (klass.respond_to?(:test_class))
module_space.const_set "#{name.split('::').last}Impostor", newStandInKlass
newStandInKlass.class_eval("
def setup_instance(*args, &block)
@_instance = #{klass.name}_ddt.new(*args, &block)
end
")
newStandInKlass.class_eval("
class << self
def _get_standin_class
#{newStandInKlass}
end
def test_class
#{klass.name}
end
def _current_old_class
#{klass.name}_ddt
end
end
")
newStandInKlass.class_exec do
def initialize(*args, &block)
@_instance_init = {object_id: self.object_id, params: [], block: nil}
self.class.replace_procs_with_recorders(args)
@_instance_init[:params] = args
recordedProc = if (block)
RecordedProc.new(block, true)
else
nil
end
@_instance_init[:block] = recordedProc
setup_instance(*args, &recordedProc)
param_types = @_instance.method(:initialize).parameters
@_instance_init[:params_types] = param_types
@_method_calls = []
@_method_calls_by_method = {}
@_methods_for_test = []
@_let_variables = {}
@_init_let_variables = {}
caller_context = binding.of_caller(2)
v_locals = caller_context.eval('local_variables')
v_locals.each { |v|
variable_value = caller_context.eval("#{v.to_s}")
@_init_let_variables[variable_value.object_id] = v
}
args.each_with_index { |a, index|
@_init_let_variables[a.object_id] = param_types[index][1].to_s if param_types.size == 2
}
self.class._add_instances(self)
end
def _init_arguments
@_instance_init
end
def test_class
@_instance.class
end
def include_for_tests(method_list = [])
@_methods_for_test = @_methods_for_test + method_list
end
def let_variables
@_let_variables
end
def init_let_variables
@_init_let_variables
end
def method_calls_by_method
@_method_calls_by_method
end
def method_calls
@_method_calls
end
def to_s
@_instance.to_s
end
def ==(other)
@_instance==other
end
def kind_of?(klass)
@_instance.kind_of? klass
end
def methods
@_instance.methods + [:method_calls]
end
def freeze
@_instance.freeze
end
def hash
@instance.hash
end
def inspect
@_instance.inspect
end
def is_a?(something)
@_instance.is_a? something
end
def instance_variable_get(sym)
@_instance.instance_variable_get(sym)
end
def instance_variable_set(sym, val)
@_instance.instance_variable_get(sym, val)
end
class << self
def replace_procs_with_recorders(args)
(0..args.size).each do |index|
if (args[index].kind_of? Proc)
args[index] = Pretentious::RecordedProc.new(args[index]) {}
end
end
end
def _set_is_stub
@_is_stub = true
end
def _is_stub?
@_is_stub = @_is_stub || false
@_is_stub
end
def _add_instances(instance)
@_instances = @_instances || []
@_instances << instance unless @_instances.include? instance
end
def let_variables
@_let_variables
end
def method_calls_by_method
@_method_calls_by_method
end
def method_calls
@_method_calls
end
def _add_instances(instance)
@_instances = @_instances || []
@_instances << instance unless @_instances.include? instance
end
def _instances
@_instances
end
def instance_methods
methods = super
test_class.instance_methods + methods
end
def _call_method(target, method_sym, *arguments, &block)
klass = _get_standin_class
caller_context = binding.of_caller(2)
is_stub = _is_stub?
target.instance_exec do
@_method_calls = @_method_calls || []
@_method_calls_by_method = @_method_calls_by_method || {}
@_methods_for_test = @_methods_for_test || []
@_let_variables = @_let_variables || {}
v_locals = caller_context.eval('local_variables')
v_locals.each { |v|
variable_value = caller_context.eval("#{v.to_s}")
@_let_variables[variable_value.object_id] = v
}
klass.replace_procs_with_recorders(arguments)
info_block = {}
info_block[:method] = method_sym
info_block[:params] = arguments
recordedProc = if (block)
RecordedProc.new(block, true)
else
nil
end
info_block[:block] = recordedProc
info_block[:names] = @_instance.method(method_sym).parameters
begin
unless is_stub
current_context = { calls: [] }
info_block[:context] = current_context
Thread.current._push_context(current_context)
end
if (@_instance.methods.include? method_sym)
result = @_instance.send(method_sym, *arguments, &recordedProc)
else
result = @_instance.send(:method_missing, method_sym, *arguments, &recordedProc)
end
Thread.current._pop_context unless is_stub
if method_sym.to_s.end_with? '='
info_block[:result] = arguments[0]
else
info_block[:result] = result
end
rescue Exception=>e
info_block[:result] = e
rescue StandardError=>e
info_block[:result] = e
end
if is_stub
info_block[:class] = test_class
Thread.current._all_context.each { |mock_context|
mock_context[:calls] << info_block if mock_context
}
end
@_method_calls << info_block
if (@_method_calls_by_method[method_sym].nil?)
@_method_calls_by_method[method_sym] = []
end
@_method_calls_by_method[method_sym] << info_block
raise e if (e.kind_of? Exception)
result
end
end
end
end
newStandInKlass.class_exec do
def method_missing(method_sym, *arguments, &block)
self.class._call_method(self, method_sym, *arguments, &block)
end
class << self
def method_missing(method_sym, *arguments, &block)
_add_instances(self)
@_instance = _current_old_class
_call_method(self, method_sym, *arguments, &block)
end
end
end
newStandInKlass
end
|