-
Notifications
You must be signed in to change notification settings - Fork 1
/
busted_test.rb
321 lines (258 loc) · 9.31 KB
/
busted_test.rb
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
require "test_helper"
class BustedTest < Minitest::Test
def test_invalid_profiler_exception
error = assert_raises ArgumentError do
Busted.run profiler: :pizza
end
assert_equal "profiler `pizza' does not exist", error.message
end
def test_invalid_profiler_action_exception
error = assert_raises ArgumentError do
Busted.start profiler: :sandwich, action: :pizza
end
assert_equal "profiler requires start or finish action", error.message
end
def test_cache_invalidations_requires_block
assert_raises LocalJumpError do
Busted.run
end
end
def test_method_cache_invalidations_requires_block
assert_raises LocalJumpError do
Busted.method_cache_invalidations
end
end
def test_constant_cache_invalidations_requires_block
assert_raises LocalJumpError do
Busted.constant_cache_invalidations
end
end
def test_cache_invalidations_with_empty_block
report = Busted.run { }
assert_equal 0, report[:invalidations][:method]
assert_equal 0, report[:invalidations][:constant]
end
def test_method_cache_invalidations_with_empty_block
assert_equal 0, Busted.method_cache_invalidations { }
end
def test_constant_cache_invalidations_with_empty_block
assert_equal 0, Busted.constant_cache_invalidations { }
end
def test_cache_invalidations_with_addition
report = Busted.run { 1 + 1 }
assert_equal 0, report[:invalidations][:method]
assert_equal 0, report[:invalidations][:constant]
end
def test_method_cache_invalidations_with_addition
assert_equal 0, Busted.method_cache_invalidations { 1 + 1 }
end
def test_constant_cache_invalidations_with_addition
assert_equal 0, Busted.constant_cache_invalidations { 1 + 1 }
end
def test_start_finish_with_addition
Busted.start
1 + 1
report = Busted.finish
assert_equal 0, report[:invalidations][:method]
assert_equal 0, report[:invalidations][:constant]
end
def test_cache_invalidations_with_new_constant
report = Busted.run { self.class.const_set :"CHEESE", "cheese" }
assert_equal 0, report[:invalidations][:method]
assert_equal 1, report[:invalidations][:constant]
end
def test_method_cache_invalidations_with_new_constant
invalidations = Busted.method_cache_invalidations do
self.class.const_set :"HAWAIIAN", "hawaiian"
end
assert_equal 0, invalidations
end
def test_constant_cache_invalidations_with_new_constant
invalidations = Busted.constant_cache_invalidations do
self.class.const_set :"VEGETABLE", "vegetable"
end
assert_equal 1, invalidations
end
def test_start_finish_with_new_constant
Busted.start
self.class.const_set :"PEPPERONI", "pepperoni"
report = Busted.finish
assert_equal 0, report[:invalidations][:method]
assert_equal 1, report[:invalidations][:constant]
end
def test_cache_invalidations_with_new_method
report = Busted.run { Object.class_exec { def cheese; end } }
assert_equal 1, report[:invalidations][:method]
assert_equal 0, report[:invalidations][:constant]
end
def test_method_cache_invalidations_with_new_method
invalidations = Busted.method_cache_invalidations do
Object.class_exec { def hawaiian; end }
end
assert_equal 1, invalidations
end
def test_constant_cache_invalidations_with_new_method
invalidations = Busted.constant_cache_invalidations do
Object.class_exec { def vegetable; end }
end
assert_equal 0, invalidations
end
def test_start_finish_with_new_method
Busted.start
Object.class_exec { def pepperoni; end }
report = Busted.finish
assert_equal 1, report[:invalidations][:method]
assert_equal 0, report[:invalidations][:constant]
end
def test_cache_invalidations_with_new_class
report = Busted.run { Object.class_eval "class ThreeCheese; end" }
assert_equal 0, report[:invalidations][:method]
assert_equal 1, report[:invalidations][:constant]
end
def test_method_cache_invalidations_with_new_class
invalidations = Busted.method_cache_invalidations do
Object.class_eval "class SweetHawaiian; end"
end
assert_equal 0, invalidations
end
def test_constant_cache_invalidations_with_new_class
invalidations = Busted.constant_cache_invalidations do
Object.class_eval "class Veggie; end"
end
assert_equal 1, invalidations
end
def test_start_finish_with_new_class
Busted.start
Object.class_eval "class Pepperoni; end"
report = Busted.finish
assert_equal 0, report[:invalidations][:method]
assert_equal 1, report[:invalidations][:constant]
end
def test_cache_predicate_requires_block
assert_raises LocalJumpError do
Busted.cache?
end
end
def test_method_cache_predicate_requires_block
assert_raises LocalJumpError do
Busted.method_cache?
end
end
def test_constant_cache_predicate_requires_block
assert_raises LocalJumpError do
Busted.constant_cache?
end
end
def test_cache_predicate_with_empty_block
refute Busted.cache? { }
end
def test_method_cache_predicate_with_empty_block
refute Busted.method_cache? { }
end
def test_constant_cache_predicate_with_empty_block
refute Busted.constant_cache? { }
end
def test_cache_predicate_with_addition
refute Busted.cache? { 1 + 1 }
end
def test_method_cache_predicate_with_addition
refute Busted.method_cache? { 1 + 1 }
end
def test_constant_cache_predicate_with_addition
refute Busted.constant_cache? { 1 + 1 }
end
def test_cache_predicate_with_new_constant
assert Busted.cache? { self.class.const_set :"PORTER", "porter" }
end
def test_method_cache_predicate_with_new_constant
refute Busted.method_cache? { self.class.const_set :"SCHWARZBIER", "schwarzbier" }
end
def test_constant_cache_predicate_with_new_constant
assert Busted.constant_cache? { self.class.const_set :"STOUT", "stout" }
end
def test_cache_predicate_with_new_method
assert Busted.cache? { Object.class_exec { def porter; end } }
end
def test_method_cache_predicate_with_new_method
assert Busted.method_cache? { Object.class_exec { def schwarzbier; end } }
end
def test_constant_cache_predicate_with_new_method
refute Busted.constant_cache? { Object.class_exec { def stout; end } }
end
def test_cache_predicate_with_new_class
assert Busted.cache? { Object.class_eval "class PierRatPorter; end" }
end
def test_method_cache_predicate_with_new_class
refute Busted.method_cache? { Object.class_eval "class MidnightExpression; end" }
end
def test_constant_cache_predicate_with_new_class
assert Busted.constant_cache? { Object.class_eval "class SantasLittleHelper; end" }
end
def test_nesting_run
outer_report = {}
inner_report = {}
outer_report = Busted.run do
Object.class_eval "class CarlsbadChronic; end"
inner_report = Busted.run do
Object.class_eval "class SharkBite; end"
end
end
assert_equal({invalidations:{method:0,constant:1}}, inner_report)
assert_equal({invalidations:{method:0,constant:2}}, outer_report)
end
def test_nesting_start_finish
outer_report = {}
inner_report = {}
Busted.start
Object.class_eval "class ExtraCheese; end"
Busted.start
Object.class_eval "class ExtraExtraCheese; end"
inner_report = Busted.finish
Object.class_eval "class TooMuchCheese; end"
outer_report = Busted.finish
assert_equal({invalidations:{method:0,constant:1}}, inner_report)
assert_equal({invalidations:{method:0,constant:3}}, outer_report)
end
def test_start_return_value
assert_equal true, Busted.start
Busted.finish
end
if Busted::Tracer.exists? && Busted::CurrentProcess.privileged?
def test_cache_invalidations_and_traces_with_new_method
report = Busted.run(trace: true) { Object.class_exec { def cookie; end } }
assert_equal 1, report[:invalidations][:method]
assert_equal 0, report[:invalidations][:constant]
assert_equal "global", report[:traces][:method][0][:class]
assert_match /test\/busted_test.rb\z/, report[:traces][:method][0][:sourcefile]
assert_equal "#{__LINE__ - 5}", report[:traces][:method][0][:lineno]
end
def test_start_finish_and_traces_with_new_method
Busted.start trace: true
Object.class_exec { def candy; end }
report = Busted.finish
assert_equal 1, report[:invalidations][:method]
assert_equal 0, report[:invalidations][:constant]
assert_equal "global", report[:traces][:method][0][:class]
assert_match /test\/busted_test.rb\z/, report[:traces][:method][0][:sourcefile]
assert_equal "#{__LINE__ - 6}", report[:traces][:method][0][:lineno]
end
end
def test_trace_without_root_privileges
Busted::Tracer.stub :exists?, true do
Busted::CurrentProcess.stub :privileged?, false do
error = assert_raises Errno::EPERM do
Busted.run(trace: true) { Object.class_exec { def ice_cream; end } }
end
assert_equal "Operation not permitted - dtrace requires root privileges", error.message
end
end
end
def test_trace_without_dtrace_installed
Busted::Tracer.stub :exists?, false do
error = assert_raises Busted::Tracer::MissingCommandError do
Busted.run(trace: true) { Object.class_exec { def pie; end } }
end
assert_equal "tracer requires dtrace", error.message
end
end
end