assert 2.12.1 → 2.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/assert/stub.rb CHANGED
@@ -32,12 +32,14 @@ module Assert
32
32
  "--#{object.object_id}--#{method_name}--"
33
33
  end
34
34
 
35
- attr_reader :method_name, :name, :do
35
+ attr_reader :method_name, :name, :ivar_name, :do
36
36
 
37
37
  def initialize(object, method_name, &block)
38
38
  @metaclass = class << object; self; end
39
39
  @method_name = method_name.to_s
40
- @name = "__assert_stub__#{@method_name}"
40
+ @name = "__assert_stub__#{object.object_id}_#{@method_name}"
41
+ @ivar_name = "@__assert_stub_#{object.object_id}_" \
42
+ "#{@method_name.to_sym.object_id}"
41
43
 
42
44
  setup(object)
43
45
 
@@ -52,7 +54,12 @@ module Assert
52
54
  end
53
55
 
54
56
  def call(*args, &block)
55
- raise StubArityError, "artiy mismatch" unless arity_matches?(args)
57
+ unless arity_matches?(args)
58
+ message = "arity mismatch on `#{@method_name}`: " \
59
+ "expected #{number_of_args(@method.arity)}, " \
60
+ "called with #{args.size}"
61
+ raise StubArityError, message
62
+ end
56
63
  @lookup[args].call(*args, &block)
57
64
  rescue NotStubbedError => exception
58
65
  @lookup.rehash
@@ -60,7 +67,12 @@ module Assert
60
67
  end
61
68
 
62
69
  def with(*args, &block)
63
- raise StubArityError, "artiy mismatch" unless arity_matches?(args)
70
+ unless arity_matches?(args)
71
+ message = "arity mismatch on `#{@method_name}`: " \
72
+ "expected #{number_of_args(@method.arity)}, " \
73
+ "stubbed with #{args.size}"
74
+ raise StubArityError, message
75
+ end
64
76
  @lookup[args] = block
65
77
  end
66
78
 
@@ -70,33 +82,45 @@ module Assert
70
82
 
71
83
  def teardown
72
84
  @metaclass.send(:undef_method, @method_name)
85
+ Assert.send(:remove_instance_variable, @ivar_name)
73
86
  @metaclass.send(:alias_method, @method_name, @name)
74
87
  @metaclass.send(:undef_method, @name)
75
88
  end
76
89
 
90
+ def inspect
91
+ "#<#{self.class}:#{'0x0%x' % (object_id << 1)}" \
92
+ " @method_name=#{@method_name.inspect}" \
93
+ ">"
94
+ end
95
+
77
96
  protected
78
97
 
79
98
  def setup(object)
80
99
  unless object.respond_to?(@method_name)
81
100
  raise StubError, "#{object.inspect} does not respond to `#{@method_name}`"
82
101
  end
83
- if !object.methods.map(&:to_s).include?(@method_name)
102
+ is_constant = object.kind_of?(Module)
103
+ local_object_methods = object.methods(false).map(&:to_s)
104
+ all_object_methods = object.methods.map(&:to_s)
105
+ if (is_constant && !local_object_methods.include?(@method_name)) ||
106
+ (!is_constant && !all_object_methods.include?(@method_name))
107
+ params_list = ParameterList.new(object, @method_name)
84
108
  @metaclass.class_eval <<-method
85
- def #{@method_name}(*args, &block)
86
- super(*args, &block)
87
- end
109
+ def #{@method_name}(#{params_list}); super; end
88
110
  method
89
111
  end
90
112
 
91
- if !object.respond_to?(@name) # already stubbed
113
+ if !local_object_methods.include?(@name) # already stubbed
92
114
  @metaclass.send(:alias_method, @name, @method_name)
93
115
  end
94
116
  @method = object.method(@name)
95
117
 
96
- stub = self
97
- @metaclass.send(:define_method, @method_name) do |*args, &block|
98
- stub.call(*args, &block)
99
- end
118
+ Assert.instance_variable_set(@ivar_name, self)
119
+ @metaclass.class_eval <<-stub_method
120
+ def #{@method_name}(*args, &block)
121
+ Assert.instance_variable_get("#{@ivar_name}").call(*args, &block)
122
+ end
123
+ stub_method
100
124
  end
101
125
 
102
126
  private
@@ -115,6 +139,45 @@ module Assert
115
139
  "`#{@method_name}(#{args.map(&:inspect).join(',')})`"
116
140
  end
117
141
 
142
+ def number_of_args(arity)
143
+ if arity < 0
144
+ "at least #{(arity + 1).abs}"
145
+ else
146
+ arity
147
+ end
148
+ end
149
+
150
+ module ParameterList
151
+ LETTERS = ('a'..'z').to_a.freeze
152
+
153
+ def self.new(object, method_name)
154
+ arity = get_arity(object, method_name)
155
+ params = build_params_from_arity(arity)
156
+ params << '*args' if arity < 0
157
+ params << '&block'
158
+ params.join(', ')
159
+ end
160
+
161
+ private
162
+
163
+ def self.get_arity(object, method_name)
164
+ object.method(method_name).arity
165
+ rescue NameError
166
+ -1
167
+ end
168
+
169
+ def self.build_params_from_arity(arity)
170
+ number = arity < 0 ? (arity + 1).abs : arity
171
+ (0..(number - 1)).map{ |param_index| get_param_name(param_index) }
172
+ end
173
+
174
+ def self.get_param_name(param_index)
175
+ param_index += LETTERS.size # avoid getting 0 for the number of letters
176
+ number_of_letters, letter_index = param_index.divmod(LETTERS.size)
177
+ LETTERS[letter_index] * number_of_letters
178
+ end
179
+ end
180
+
118
181
  end
119
182
 
120
183
  end
@@ -1,3 +1,3 @@
1
1
  module Assert
2
- VERSION = "2.12.1"
2
+ VERSION = "2.12.2"
3
3
  end
@@ -0,0 +1,754 @@
1
+ require 'assert'
2
+ require 'assert/stub'
3
+
4
+ class Assert::Stub
5
+
6
+ class SystemTests < Assert::Context
7
+ desc "Assert::Stub"
8
+
9
+ end
10
+
11
+ class InstanceTests < SystemTests
12
+ desc "for instance methods"
13
+ setup do
14
+ @instance = TestClass.new
15
+ Assert.stub(@instance, :noargs){ 'default' }
16
+ Assert.stub(@instance, :noargs).with{ 'none' }
17
+
18
+ Assert.stub(@instance, :withargs){ 'default' }
19
+ Assert.stub(@instance, :withargs).with(1){ 'one' }
20
+
21
+ Assert.stub(@instance, :anyargs){ 'default' }
22
+ Assert.stub(@instance, :anyargs).with(1, 2){ 'one-two' }
23
+
24
+ Assert.stub(@instance, :minargs){ 'default' }
25
+ Assert.stub(@instance, :minargs).with(1, 2){ 'one-two' }
26
+ Assert.stub(@instance, :minargs).with(1, 2, 3){ 'one-two-three' }
27
+
28
+ Assert.stub(@instance, :withblock){ 'default' }
29
+ end
30
+ subject{ @instance }
31
+
32
+ should "allow stubbing a method that doesn't take args" do
33
+ assert_equal 'none', subject.noargs
34
+ end
35
+
36
+ should "allow stubbing a method that takes args" do
37
+ assert_equal 'one', subject.withargs(1)
38
+ assert_equal 'default', subject.withargs(2)
39
+ end
40
+
41
+ should "allow stubbing a method that takes any args" do
42
+ assert_equal 'default', subject.anyargs
43
+ assert_equal 'default', subject.anyargs(1)
44
+ assert_equal 'one-two', subject.anyargs(1, 2)
45
+ end
46
+
47
+ should "allow stubbing a method that takes a minimum number of args" do
48
+ assert_equal 'one-two', subject.minargs(1, 2)
49
+ assert_equal 'one-two-three', subject.minargs(1, 2, 3)
50
+ assert_equal 'default', subject.minargs(1, 2, 4)
51
+ assert_equal 'default', subject.minargs(1, 2, 3, 4)
52
+ end
53
+
54
+ should "allow stubbing a method that takes a block" do
55
+ assert_equal 'default', subject.withblock
56
+ assert_equal 'default', subject.withblock{ 'my-block' }
57
+ end
58
+
59
+ should "not allow stubbing methods with invalid arity" do
60
+ assert_raises{ Assert.stub(subject, :noargs).with(1){ } }
61
+
62
+ assert_raises{ Assert.stub(subject, :withargs).with{ } }
63
+ assert_raises{ Assert.stub(subject, :withargs).with(1, 2){ } }
64
+
65
+ assert_raises{ Assert.stub(subject, :minargs).with{ } }
66
+ assert_raises{ Assert.stub(subject, :minargs).with(1){ } }
67
+
68
+ assert_raises{ Assert.stub(subject, :withblock).with(1){ } }
69
+ end
70
+
71
+ should "not allow calling methods with invalid arity" do
72
+ assert_raises{ subject.noargs(1) }
73
+
74
+ assert_raises{ subject.withargs }
75
+ assert_raises{ subject.withargs(1, 2) }
76
+
77
+ assert_raises{ subject.minargs }
78
+ assert_raises{ subject.minargs(1) }
79
+
80
+ assert_raises{ subject.withblock(1) }
81
+ end
82
+
83
+ end
84
+
85
+ class ClassTests < SystemTests
86
+ desc "for singleton methods on a class"
87
+ setup do
88
+ @class = TestClass
89
+ Assert.stub(@class, :noargs){ 'default' }
90
+ Assert.stub(@class, :noargs).with{ 'none' }
91
+
92
+ Assert.stub(@class, :withargs){ 'default' }
93
+ Assert.stub(@class, :withargs).with(1){ 'one' }
94
+
95
+ Assert.stub(@class, :anyargs){ 'default' }
96
+ Assert.stub(@class, :anyargs).with(1, 2){ 'one-two' }
97
+
98
+ Assert.stub(@class, :minargs){ 'default' }
99
+ Assert.stub(@class, :minargs).with(1, 2){ 'one-two' }
100
+ Assert.stub(@class, :minargs).with(1, 2, 3){ 'one-two-three' }
101
+
102
+ Assert.stub(@class, :withblock){ 'default' }
103
+ end
104
+ subject{ @class }
105
+
106
+ should "allow stubbing a method that doesn't take args" do
107
+ assert_equal 'none', subject.noargs
108
+ end
109
+
110
+ should "allow stubbing a method that takes args" do
111
+ assert_equal 'one', subject.withargs(1)
112
+ assert_equal 'default', subject.withargs(2)
113
+ end
114
+
115
+ should "allow stubbing a method that takes any args" do
116
+ assert_equal 'default', subject.anyargs
117
+ assert_equal 'default', subject.anyargs(1)
118
+ assert_equal 'one-two', subject.anyargs(1, 2)
119
+ end
120
+
121
+ should "allow stubbing a method that takes a minimum number of args" do
122
+ assert_equal 'one-two', subject.minargs(1, 2)
123
+ assert_equal 'one-two-three', subject.minargs(1, 2, 3)
124
+ assert_equal 'default', subject.minargs(1, 2, 4)
125
+ assert_equal 'default', subject.minargs(1, 2, 3, 4)
126
+ end
127
+
128
+ should "allow stubbing a method that takes a block" do
129
+ assert_equal 'default', subject.withblock
130
+ assert_equal 'default', subject.withblock{ 'my-block' }
131
+ end
132
+
133
+ should "not allow stubbing methods with invalid arity" do
134
+ assert_raises{ Assert.stub(subject, :noargs).with(1){ } }
135
+
136
+ assert_raises{ Assert.stub(subject, :withargs).with{ } }
137
+ assert_raises{ Assert.stub(subject, :withargs).with(1, 2){ } }
138
+
139
+ assert_raises{ Assert.stub(subject, :minargs).with{ } }
140
+ assert_raises{ Assert.stub(subject, :minargs).with(1){ } }
141
+
142
+ assert_raises{ Assert.stub(subject, :withblock).with(1){ } }
143
+ end
144
+
145
+ should "not allow calling methods with invalid arity" do
146
+ assert_raises{ subject.noargs(1) }
147
+
148
+ assert_raises{ subject.withargs }
149
+ assert_raises{ subject.withargs(1, 2) }
150
+
151
+ assert_raises{ subject.minargs }
152
+ assert_raises{ subject.minargs(1) }
153
+
154
+ assert_raises{ subject.withblock(1) }
155
+ end
156
+
157
+ end
158
+
159
+ class ModuleTests < SystemTests
160
+ desc "for singleton methods on a module"
161
+ setup do
162
+ @module = TestModule
163
+ Assert.stub(@module, :noargs){ 'default' }
164
+ Assert.stub(@module, :noargs).with{ 'none' }
165
+
166
+ Assert.stub(@module, :withargs){ 'default' }
167
+ Assert.stub(@module, :withargs).with(1){ 'one' }
168
+
169
+ Assert.stub(@module, :anyargs){ 'default' }
170
+ Assert.stub(@module, :anyargs).with(1, 2){ 'one-two' }
171
+
172
+ Assert.stub(@module, :minargs){ 'default' }
173
+ Assert.stub(@module, :minargs).with(1, 2){ 'one-two' }
174
+ Assert.stub(@module, :minargs).with(1, 2, 3){ 'one-two-three' }
175
+
176
+ Assert.stub(@module, :withblock){ 'default' }
177
+ end
178
+ subject{ @module }
179
+
180
+ should "allow stubbing a method that doesn't take args" do
181
+ assert_equal 'none', subject.noargs
182
+ end
183
+
184
+ should "allow stubbing a method that takes args" do
185
+ assert_equal 'one', subject.withargs(1)
186
+ assert_equal 'default', subject.withargs(2)
187
+ end
188
+
189
+ should "allow stubbing a method that takes any args" do
190
+ assert_equal 'default', subject.anyargs
191
+ assert_equal 'default', subject.anyargs(1)
192
+ assert_equal 'one-two', subject.anyargs(1, 2)
193
+ end
194
+
195
+ should "allow stubbing a method that takes a minimum number of args" do
196
+ assert_equal 'one-two', subject.minargs(1, 2)
197
+ assert_equal 'one-two-three', subject.minargs(1, 2, 3)
198
+ assert_equal 'default', subject.minargs(1, 2, 4)
199
+ assert_equal 'default', subject.minargs(1, 2, 3, 4)
200
+ end
201
+
202
+ should "allow stubbing a method that takes a block" do
203
+ assert_equal 'default', subject.withblock
204
+ assert_equal 'default', subject.withblock{ 'my-block' }
205
+ end
206
+
207
+ should "not allow stubbing methods with invalid arity" do
208
+ assert_raises{ Assert.stub(subject, :noargs).with(1){ } }
209
+
210
+ assert_raises{ Assert.stub(subject, :withargs).with{ } }
211
+ assert_raises{ Assert.stub(subject, :withargs).with(1, 2){ } }
212
+
213
+ assert_raises{ Assert.stub(subject, :minargs).with{ } }
214
+ assert_raises{ Assert.stub(subject, :minargs).with(1){ } }
215
+
216
+ assert_raises{ Assert.stub(subject, :withblock).with(1){ } }
217
+ end
218
+
219
+ should "not allow calling methods with invalid arity" do
220
+ assert_raises{ subject.noargs(1) }
221
+
222
+ assert_raises{ subject.withargs }
223
+ assert_raises{ subject.withargs(1, 2) }
224
+
225
+ assert_raises{ subject.minargs }
226
+ assert_raises{ subject.minargs(1) }
227
+
228
+ assert_raises{ subject.withblock(1) }
229
+ end
230
+
231
+ end
232
+
233
+ class ExtendedTests < SystemTests
234
+ desc "for extended methods"
235
+ setup do
236
+ @class = Class.new{ extend TestMixin }
237
+ Assert.stub(@class, :noargs){ 'default' }
238
+ Assert.stub(@class, :noargs).with{ 'none' }
239
+
240
+ Assert.stub(@class, :withargs){ 'default' }
241
+ Assert.stub(@class, :withargs).with(1){ 'one' }
242
+
243
+ Assert.stub(@class, :anyargs){ 'default' }
244
+ Assert.stub(@class, :anyargs).with(1, 2){ 'one-two' }
245
+
246
+ Assert.stub(@class, :minargs){ 'default' }
247
+ Assert.stub(@class, :minargs).with(1, 2){ 'one-two' }
248
+ Assert.stub(@class, :minargs).with(1, 2, 3){ 'one-two-three' }
249
+
250
+ Assert.stub(@class, :withblock){ 'default' }
251
+ end
252
+ subject{ @class }
253
+
254
+ should "allow stubbing a method that doesn't take args" do
255
+ assert_equal 'none', subject.noargs
256
+ end
257
+
258
+ should "allow stubbing a method that takes args" do
259
+ assert_equal 'one', subject.withargs(1)
260
+ assert_equal 'default', subject.withargs(2)
261
+ end
262
+
263
+ should "allow stubbing a method that takes any args" do
264
+ assert_equal 'default', subject.anyargs
265
+ assert_equal 'default', subject.anyargs(1)
266
+ assert_equal 'one-two', subject.anyargs(1, 2)
267
+ end
268
+
269
+ should "allow stubbing a method that takes a minimum number of args" do
270
+ assert_equal 'one-two', subject.minargs(1, 2)
271
+ assert_equal 'one-two-three', subject.minargs(1, 2, 3)
272
+ assert_equal 'default', subject.minargs(1, 2, 4)
273
+ assert_equal 'default', subject.minargs(1, 2, 3, 4)
274
+ end
275
+
276
+ should "allow stubbing a method that takes a block" do
277
+ assert_equal 'default', subject.withblock
278
+ assert_equal 'default', subject.withblock{ 'my-block' }
279
+ end
280
+
281
+ should "not allow stubbing methods with invalid arity" do
282
+ assert_raises{ Assert.stub(subject, :noargs).with(1){ } }
283
+
284
+ assert_raises{ Assert.stub(subject, :withargs).with{ } }
285
+ assert_raises{ Assert.stub(subject, :withargs).with(1, 2){ } }
286
+
287
+ assert_raises{ Assert.stub(subject, :minargs).with{ } }
288
+ assert_raises{ Assert.stub(subject, :minargs).with(1){ } }
289
+
290
+ assert_raises{ Assert.stub(subject, :withblock).with(1){ } }
291
+ end
292
+
293
+ should "not allow calling methods with invalid arity" do
294
+ assert_raises{ subject.noargs(1) }
295
+
296
+ assert_raises{ subject.withargs }
297
+ assert_raises{ subject.withargs(1, 2) }
298
+
299
+ assert_raises{ subject.minargs }
300
+ assert_raises{ subject.minargs(1) }
301
+
302
+ assert_raises{ subject.withblock(1) }
303
+ end
304
+
305
+ end
306
+
307
+ class IncludedTests < SystemTests
308
+ desc "for an included method"
309
+ setup do
310
+ @class = Class.new{ include TestMixin }
311
+ @instance = @class.new
312
+ Assert.stub(@instance, :noargs){ 'default' }
313
+ Assert.stub(@instance, :noargs).with{ 'none' }
314
+
315
+ Assert.stub(@instance, :withargs){ 'default' }
316
+ Assert.stub(@instance, :withargs).with(1){ 'one' }
317
+
318
+ Assert.stub(@instance, :anyargs){ 'default' }
319
+ Assert.stub(@instance, :anyargs).with(1, 2){ 'one-two' }
320
+
321
+ Assert.stub(@instance, :minargs){ 'default' }
322
+ Assert.stub(@instance, :minargs).with(1, 2){ 'one-two' }
323
+ Assert.stub(@instance, :minargs).with(1, 2, 3){ 'one-two-three' }
324
+
325
+ Assert.stub(@instance, :withblock){ 'default' }
326
+ end
327
+ subject{ @instance }
328
+
329
+ should "allow stubbing a method that doesn't take args" do
330
+ assert_equal 'none', subject.noargs
331
+ end
332
+
333
+ should "allow stubbing a method that takes args" do
334
+ assert_equal 'one', subject.withargs(1)
335
+ assert_equal 'default', subject.withargs(2)
336
+ end
337
+
338
+ should "allow stubbing a method that takes any args" do
339
+ assert_equal 'default', subject.anyargs
340
+ assert_equal 'default', subject.anyargs(1)
341
+ assert_equal 'one-two', subject.anyargs(1, 2)
342
+ end
343
+
344
+ should "allow stubbing a method that takes a minimum number of args" do
345
+ assert_equal 'one-two', subject.minargs(1, 2)
346
+ assert_equal 'one-two-three', subject.minargs(1, 2, 3)
347
+ assert_equal 'default', subject.minargs(1, 2, 4)
348
+ assert_equal 'default', subject.minargs(1, 2, 3, 4)
349
+ end
350
+
351
+ should "allow stubbing a method that takes a block" do
352
+ assert_equal 'default', subject.withblock
353
+ assert_equal 'default', subject.withblock{ 'my-block' }
354
+ end
355
+
356
+ should "not allow stubbing methods with invalid arity" do
357
+ assert_raises{ Assert.stub(subject, :noargs).with(1){ } }
358
+
359
+ assert_raises{ Assert.stub(subject, :withargs).with{ } }
360
+ assert_raises{ Assert.stub(subject, :withargs).with(1, 2){ } }
361
+
362
+ assert_raises{ Assert.stub(subject, :minargs).with{ } }
363
+ assert_raises{ Assert.stub(subject, :minargs).with(1){ } }
364
+
365
+ assert_raises{ Assert.stub(subject, :withblock).with(1){ } }
366
+ end
367
+
368
+ should "not allow calling methods with invalid arity" do
369
+ assert_raises{ subject.noargs(1) }
370
+
371
+ assert_raises{ subject.withargs }
372
+ assert_raises{ subject.withargs(1, 2) }
373
+
374
+ assert_raises{ subject.minargs }
375
+ assert_raises{ subject.minargs(1) }
376
+
377
+ assert_raises{ subject.withblock(1) }
378
+ end
379
+
380
+ end
381
+
382
+ class InheritedClassTests < SystemTests
383
+ desc "for an inherited class method"
384
+ setup do
385
+ @class = Class.new(TestClass)
386
+ Assert.stub(@class, :noargs){ 'default' }
387
+ Assert.stub(@class, :noargs).with{ 'none' }
388
+
389
+ Assert.stub(@class, :withargs){ 'default' }
390
+ Assert.stub(@class, :withargs).with(1){ 'one' }
391
+
392
+ Assert.stub(@class, :anyargs){ 'default' }
393
+ Assert.stub(@class, :anyargs).with(1, 2){ 'one-two' }
394
+
395
+ Assert.stub(@class, :minargs){ 'default' }
396
+ Assert.stub(@class, :minargs).with(1, 2){ 'one-two' }
397
+ Assert.stub(@class, :minargs).with(1, 2, 3){ 'one-two-three' }
398
+
399
+ Assert.stub(@class, :withblock){ 'default' }
400
+ end
401
+ subject{ @class }
402
+
403
+ should "allow stubbing a method that doesn't take args" do
404
+ assert_equal 'none', subject.noargs
405
+ end
406
+
407
+ should "allow stubbing a method that takes args" do
408
+ assert_equal 'one', subject.withargs(1)
409
+ assert_equal 'default', subject.withargs(2)
410
+ end
411
+
412
+ should "allow stubbing a method that takes any args" do
413
+ assert_equal 'default', subject.anyargs
414
+ assert_equal 'default', subject.anyargs(1)
415
+ assert_equal 'one-two', subject.anyargs(1, 2)
416
+ end
417
+
418
+ should "allow stubbing a method that takes a minimum number of args" do
419
+ assert_equal 'one-two', subject.minargs(1, 2)
420
+ assert_equal 'one-two-three', subject.minargs(1, 2, 3)
421
+ assert_equal 'default', subject.minargs(1, 2, 4)
422
+ assert_equal 'default', subject.minargs(1, 2, 3, 4)
423
+ end
424
+
425
+ should "allow stubbing a method that takes a block" do
426
+ assert_equal 'default', subject.withblock
427
+ assert_equal 'default', subject.withblock{ 'my-block' }
428
+ end
429
+
430
+ should "not allow stubbing methods with invalid arity" do
431
+ assert_raises{ Assert.stub(subject, :noargs).with(1){ } }
432
+
433
+ assert_raises{ Assert.stub(subject, :withargs).with{ } }
434
+ assert_raises{ Assert.stub(subject, :withargs).with(1, 2){ } }
435
+
436
+ assert_raises{ Assert.stub(subject, :minargs).with{ } }
437
+ assert_raises{ Assert.stub(subject, :minargs).with(1){ } }
438
+
439
+ assert_raises{ Assert.stub(subject, :withblock).with(1){ } }
440
+ end
441
+
442
+ should "not allow calling methods with invalid arity" do
443
+ assert_raises{ subject.noargs(1) }
444
+
445
+ assert_raises{ subject.withargs }
446
+ assert_raises{ subject.withargs(1, 2) }
447
+
448
+ assert_raises{ subject.minargs }
449
+ assert_raises{ subject.minargs(1) }
450
+
451
+ assert_raises{ subject.withblock(1) }
452
+ end
453
+
454
+ end
455
+
456
+ class InheritedInstanceTests < SystemTests
457
+ desc "for an inherited instance method"
458
+ setup do
459
+ @class = Class.new(TestClass)
460
+ @instance = @class.new
461
+ Assert.stub(@instance, :noargs){ 'default' }
462
+ Assert.stub(@instance, :noargs).with{ 'none' }
463
+
464
+ Assert.stub(@instance, :withargs){ 'default' }
465
+ Assert.stub(@instance, :withargs).with(1){ 'one' }
466
+
467
+ Assert.stub(@instance, :anyargs){ 'default' }
468
+ Assert.stub(@instance, :anyargs).with(1, 2){ 'one-two' }
469
+
470
+ Assert.stub(@instance, :minargs){ 'default' }
471
+ Assert.stub(@instance, :minargs).with(1, 2){ 'one-two' }
472
+ Assert.stub(@instance, :minargs).with(1, 2, 3){ 'one-two-three' }
473
+
474
+ Assert.stub(@instance, :withblock){ 'default' }
475
+ end
476
+ subject{ @instance }
477
+
478
+ should "allow stubbing a method that doesn't take args" do
479
+ assert_equal 'none', subject.noargs
480
+ end
481
+
482
+ should "allow stubbing a method that takes args" do
483
+ assert_equal 'one', subject.withargs(1)
484
+ assert_equal 'default', subject.withargs(2)
485
+ end
486
+
487
+ should "allow stubbing a method that takes any args" do
488
+ assert_equal 'default', subject.anyargs
489
+ assert_equal 'default', subject.anyargs(1)
490
+ assert_equal 'one-two', subject.anyargs(1, 2)
491
+ end
492
+
493
+ should "allow stubbing a method that takes a minimum number of args" do
494
+ assert_equal 'one-two', subject.minargs(1, 2)
495
+ assert_equal 'one-two-three', subject.minargs(1, 2, 3)
496
+ assert_equal 'default', subject.minargs(1, 2, 4)
497
+ assert_equal 'default', subject.minargs(1, 2, 3, 4)
498
+ end
499
+
500
+ should "allow stubbing a method that takes a block" do
501
+ assert_equal 'default', subject.withblock
502
+ assert_equal 'default', subject.withblock{ 'my-block' }
503
+ end
504
+
505
+ should "not allow stubbing methods with invalid arity" do
506
+ assert_raises{ Assert.stub(subject, :noargs).with(1){ } }
507
+
508
+ assert_raises{ Assert.stub(subject, :withargs).with{ } }
509
+ assert_raises{ Assert.stub(subject, :withargs).with(1, 2){ } }
510
+
511
+ assert_raises{ Assert.stub(subject, :minargs).with{ } }
512
+ assert_raises{ Assert.stub(subject, :minargs).with(1){ } }
513
+
514
+ assert_raises{ Assert.stub(subject, :withblock).with(1){ } }
515
+ end
516
+
517
+ should "not allow calling methods with invalid arity" do
518
+ assert_raises{ subject.noargs(1) }
519
+
520
+ assert_raises{ subject.withargs }
521
+ assert_raises{ subject.withargs(1, 2) }
522
+
523
+ assert_raises{ subject.minargs }
524
+ assert_raises{ subject.minargs(1) }
525
+
526
+ assert_raises{ subject.withblock(1) }
527
+ end
528
+
529
+ end
530
+
531
+ class DelegateClassTests < SystemTests
532
+ desc "a class that delegates another object"
533
+ setup do
534
+ @class = DelegateClass
535
+ Assert.stub(@class, :noargs){ 'default' }
536
+ Assert.stub(@class, :noargs).with{ 'none' }
537
+
538
+ Assert.stub(@class, :withargs){ 'default' }
539
+ Assert.stub(@class, :withargs).with(1){ 'one' }
540
+
541
+ Assert.stub(@class, :anyargs){ 'default' }
542
+ Assert.stub(@class, :anyargs).with(1, 2){ 'one-two' }
543
+
544
+ Assert.stub(@class, :minargs){ 'default' }
545
+ Assert.stub(@class, :minargs).with(1, 2){ 'one-two' }
546
+ Assert.stub(@class, :minargs).with(1, 2, 3){ 'one-two-three' }
547
+
548
+ Assert.stub(@class, :withblock){ 'default' }
549
+ end
550
+ subject{ @class }
551
+
552
+ should "allow stubbing a method that doesn't take args" do
553
+ assert_equal 'none', subject.noargs
554
+ end
555
+
556
+ should "allow stubbing a method that takes args" do
557
+ assert_equal 'one', subject.withargs(1)
558
+ assert_equal 'default', subject.withargs(2)
559
+ end
560
+
561
+ should "allow stubbing a method that takes any args" do
562
+ assert_equal 'default', subject.anyargs
563
+ assert_equal 'default', subject.anyargs(1)
564
+ assert_equal 'one-two', subject.anyargs(1, 2)
565
+ end
566
+
567
+ should "allow stubbing a method that takes a minimum number of args" do
568
+ assert_equal 'one-two', subject.minargs(1, 2)
569
+ assert_equal 'one-two-three', subject.minargs(1, 2, 3)
570
+ assert_equal 'default', subject.minargs(1, 2, 4)
571
+ assert_equal 'default', subject.minargs(1, 2, 3, 4)
572
+ end
573
+
574
+ should "allow stubbing a method that takes a block" do
575
+ assert_equal 'default', subject.withblock
576
+ assert_equal 'default', subject.withblock{ 'my-block' }
577
+ end
578
+
579
+ should "allow stubbing methods with invalid arity" do
580
+ assert_nothing_raised{ Assert.stub(subject, :noargs).with(1){ } }
581
+
582
+ assert_nothing_raised{ Assert.stub(subject, :withargs).with{ } }
583
+ assert_nothing_raised{ Assert.stub(subject, :withargs).with(1, 2){ } }
584
+
585
+ assert_nothing_raised{ Assert.stub(subject, :minargs).with{ } }
586
+ assert_nothing_raised{ Assert.stub(subject, :minargs).with(1){ } }
587
+
588
+ assert_nothing_raised{ Assert.stub(subject, :withblock).with(1){ } }
589
+ end
590
+
591
+ should "allow calling methods with invalid arity" do
592
+ assert_nothing_raised{ subject.noargs(1) }
593
+
594
+ assert_nothing_raised{ subject.withargs }
595
+ assert_nothing_raised{ subject.withargs(1, 2) }
596
+
597
+ assert_nothing_raised{ subject.minargs }
598
+ assert_nothing_raised{ subject.minargs(1) }
599
+
600
+ assert_nothing_raised{ subject.withblock(1) }
601
+ end
602
+
603
+ end
604
+
605
+ class DelegateInstanceTests < SystemTests
606
+ desc "an instance that delegates another object"
607
+ setup do
608
+ @instance = DelegateClass.new
609
+ Assert.stub(@instance, :noargs){ 'default' }
610
+ Assert.stub(@instance, :noargs).with{ 'none' }
611
+
612
+ Assert.stub(@instance, :withargs){ 'default' }
613
+ Assert.stub(@instance, :withargs).with(1){ 'one' }
614
+
615
+ Assert.stub(@instance, :anyargs){ 'default' }
616
+ Assert.stub(@instance, :anyargs).with(1, 2){ 'one-two' }
617
+
618
+ Assert.stub(@instance, :minargs){ 'default' }
619
+ Assert.stub(@instance, :minargs).with(1, 2){ 'one-two' }
620
+ Assert.stub(@instance, :minargs).with(1, 2, 3){ 'one-two-three' }
621
+
622
+ Assert.stub(@instance, :withblock){ 'default' }
623
+ end
624
+ subject{ @instance }
625
+
626
+ should "allow stubbing a method that doesn't take args" do
627
+ assert_equal 'none', subject.noargs
628
+ end
629
+
630
+ should "allow stubbing a method that takes args" do
631
+ assert_equal 'one', subject.withargs(1)
632
+ assert_equal 'default', subject.withargs(2)
633
+ end
634
+
635
+ should "allow stubbing a method that takes any args" do
636
+ assert_equal 'default', subject.anyargs
637
+ assert_equal 'default', subject.anyargs(1)
638
+ assert_equal 'one-two', subject.anyargs(1, 2)
639
+ end
640
+
641
+ should "allow stubbing a method that takes a minimum number of args" do
642
+ assert_equal 'one-two', subject.minargs(1, 2)
643
+ assert_equal 'one-two-three', subject.minargs(1, 2, 3)
644
+ assert_equal 'default', subject.minargs(1, 2, 4)
645
+ assert_equal 'default', subject.minargs(1, 2, 3, 4)
646
+ end
647
+
648
+ should "allow stubbing a method that takes a block" do
649
+ assert_equal 'default', subject.withblock
650
+ assert_equal 'default', subject.withblock{ 'my-block' }
651
+ end
652
+
653
+ should "allow stubbing methods with invalid arity" do
654
+ assert_nothing_raised{ Assert.stub(subject, :noargs).with(1){ } }
655
+
656
+ assert_nothing_raised{ Assert.stub(subject, :withargs).with{ } }
657
+ assert_nothing_raised{ Assert.stub(subject, :withargs).with(1, 2){ } }
658
+
659
+ assert_nothing_raised{ Assert.stub(subject, :minargs).with{ } }
660
+ assert_nothing_raised{ Assert.stub(subject, :minargs).with(1){ } }
661
+
662
+ assert_nothing_raised{ Assert.stub(subject, :withblock).with(1){ } }
663
+ end
664
+
665
+ should "allow calling methods with invalid arity" do
666
+ assert_nothing_raised{ subject.noargs(1) }
667
+
668
+ assert_nothing_raised{ subject.withargs }
669
+ assert_nothing_raised{ subject.withargs(1, 2) }
670
+
671
+ assert_nothing_raised{ subject.minargs }
672
+ assert_nothing_raised{ subject.minargs(1) }
673
+
674
+ assert_nothing_raised{ subject.withblock(1) }
675
+ end
676
+
677
+ end
678
+
679
+ class ParentAndChildClassTests < SystemTests
680
+ desc "for a parent method stubbed on both the parent and child"
681
+ setup do
682
+ @parent_class = Class.new
683
+ @child_class = Class.new(@parent_class)
684
+
685
+ Assert.stub(@parent_class, :new){ 'parent' }
686
+ Assert.stub(@child_class, :new){ 'child' }
687
+ end
688
+
689
+ should "allow stubbing the methods individually" do
690
+ assert_equal 'parent', @parent_class.new
691
+ assert_equal 'child', @child_class.new
692
+ end
693
+
694
+ end
695
+
696
+ class TestClass
697
+
698
+ def self.noargs; end
699
+ def self.withargs(a); end
700
+ def self.anyargs(*args); end
701
+ def self.minargs(a, b, *args); end
702
+ def self.withblock(&block); end
703
+
704
+ def noargs; end
705
+ def withargs(a); end
706
+ def anyargs(*args); end
707
+ def minargs(a, b, *args); end
708
+ def withblock(&block); end
709
+
710
+ end
711
+
712
+ module TestModule
713
+
714
+ def self.noargs; end
715
+ def self.withargs(a); end
716
+ def self.anyargs(*args); end
717
+ def self.minargs(a, b, *args); end
718
+ def self.withblock(&block); end
719
+
720
+ end
721
+
722
+ module TestMixin
723
+
724
+ def noargs; end
725
+ def withargs(a); end
726
+ def anyargs(*args); end
727
+ def minargs(a, b, *args); end
728
+ def withblock(&block); end
729
+
730
+ end
731
+
732
+ class DelegateClass
733
+ def self.respond_to?(*args)
734
+ TestClass.respond_to?(*args) || super
735
+ end
736
+
737
+ def self.method_missing(name, *args, &block)
738
+ TestClass.respond_to?(name) ? TestClass.send(name, *args, &block) : super
739
+ end
740
+
741
+ def initialize
742
+ @delegate = TestClass.new
743
+ end
744
+
745
+ def respond_to?(*args)
746
+ @delegate.respond_to?(*args) || super
747
+ end
748
+
749
+ def method_missing(name, *args, &block)
750
+ @delegate.respond_to?(name) ? @delegate.send(name, *args, &block) : super
751
+ end
752
+ end
753
+
754
+ end
@@ -1,6 +1,8 @@
1
1
  require 'assert'
2
2
  require 'assert/stub'
3
3
 
4
+ require 'assert/factory'
5
+
4
6
  class Assert::Stub
5
7
 
6
8
  class UnitTests < Assert::Context
@@ -19,7 +21,7 @@ class Assert::Stub
19
21
  end
20
22
  subject{ @stub }
21
23
 
22
- should have_readers :method_name, :name, :do
24
+ should have_readers :method_name, :name, :ivar_name, :do
23
25
  should have_writers :do
24
26
  should have_cmeths :key
25
27
 
@@ -31,7 +33,11 @@ class Assert::Stub
31
33
 
32
34
  should "know its names" do
33
35
  assert_equal 'mymeth', subject.method_name
34
- assert_equal "__assert_stub__#{subject.method_name}", subject.name
36
+ expected = "__assert_stub__#{@myobj.object_id}_#{subject.method_name}"
37
+ assert_equal expected, subject.name
38
+ expected = "@__assert_stub_#{@myobj.object_id}_" \
39
+ "#{subject.method_name.to_sym.object_id}"
40
+ assert_equal expected, subject.ivar_name
35
41
  end
36
42
 
37
43
  should "complain when called if no do block was given" do
@@ -181,6 +187,12 @@ class Assert::Stub
181
187
  assert_equal 'four-five-six', mydelegator.myvalargs(4,5,6)
182
188
  end
183
189
 
190
+ should "store and remove itself on asserts main module" do
191
+ assert_equal subject, Assert.instance_variable_get(subject.ivar_name)
192
+ subject.teardown
193
+ assert_nil Assert.instance_variable_get(subject.ivar_name)
194
+ end
195
+
184
196
  should "be removable" do
185
197
  assert_equal 1, @myobj.myval(1)
186
198
  stub = Assert::Stub.new(@myobj, :myval){ |val| val.to_s }
@@ -189,6 +201,12 @@ class Assert::Stub
189
201
  assert_equal 1, @myobj.myval(1)
190
202
  end
191
203
 
204
+ should "have a readable inspect" do
205
+ expected = "#<#{subject.class}:#{'0x0%x' % (subject.object_id << 1)}" \
206
+ " @method_name=#{subject.method_name.inspect}>"
207
+ assert_equal expected, subject.inspect
208
+ end
209
+
192
210
  end
193
211
 
194
212
  class MutatingArgsStubTests < UnitTests
@@ -209,6 +227,34 @@ class Assert::Stub
209
227
 
210
228
  end
211
229
 
230
+ class StubInheritedMethodAfterStubbedOnParentTests < UnitTests
231
+ desc "stubbing an inherited method after its stubbed on the parent class"
232
+ setup do
233
+ @parent_class = Class.new
234
+ @child_class = Class.new(@parent_class)
235
+
236
+ @parent_stub = Assert::Stub.new(@parent_class, :new)
237
+ @child_stub = Assert::Stub.new(@child_class, :new)
238
+ end
239
+
240
+ should "allow stubbing them independently of each other" do
241
+ assert_nothing_raised do
242
+ @parent_stub.with(1, 2){ 'parent' }
243
+ @child_stub.with(1, 2){ 'child' }
244
+ end
245
+ assert_equal 'parent', @parent_class.new(1, 2)
246
+ assert_equal 'child', @child_class.new(1, 2)
247
+ end
248
+
249
+ should "not raise any errors when tearing down the parent before the child" do
250
+ assert_nothing_raised do
251
+ @parent_stub.teardown
252
+ @child_stub.teardown
253
+ end
254
+ end
255
+
256
+ end
257
+
212
258
  class NullStubTests < UnitTests
213
259
  desc "NullStub"
214
260
  setup do
@@ -220,6 +266,43 @@ class Assert::Stub
220
266
 
221
267
  end
222
268
 
269
+ class ParameterListTests < UnitTests
270
+ desc "ParameterList"
271
+ setup do
272
+ many_args = (0..ParameterList::LETTERS.size).map do
273
+ Assert::Factory.string
274
+ end.join(', ')
275
+ @object = Class.new
276
+ # use `class_eval` with string to easily define methods with many params
277
+ @object.class_eval <<-methods
278
+ def self.noargs; end
279
+ def self.anyargs(*args); end
280
+ def self.manyargs(#{many_args}); end
281
+ def self.minargs(#{many_args}, *args); end
282
+ methods
283
+ end
284
+ subject{ ParameterList }
285
+
286
+ should "build a parameter list for a method that takes no args" do
287
+ assert_equal '&block', subject.new(@object, 'noargs')
288
+ end
289
+
290
+ should "build a parameter list for a method that takes any args" do
291
+ assert_equal '*args, &block', subject.new(@object, 'anyargs')
292
+ end
293
+
294
+ should "build a parameter list for a method that takes many args" do
295
+ expected = "#{ParameterList::LETTERS.join(', ')}, aa, &block"
296
+ assert_equal expected, subject.new(@object, 'manyargs')
297
+ end
298
+
299
+ should "build a parameter list for a method that takes a minimum number of args" do
300
+ expected = "#{ParameterList::LETTERS.join(', ')}, aa, *args, &block"
301
+ assert_equal expected, subject.new(@object, 'minargs')
302
+ end
303
+
304
+ end
305
+
223
306
  class ChangeHashObject
224
307
  def initialize
225
308
  @value = nil
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert
3
3
  version: !ruby/object:Gem::Version
4
- hash: 61
4
+ hash: 59
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 12
9
- - 1
10
- version: 2.12.1
9
+ - 2
10
+ version: 2.12.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2014-06-27 00:00:00 Z
19
+ date: 2014-07-08 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -84,6 +84,7 @@ files:
84
84
  - test/support/factory.rb
85
85
  - test/support/inherited_stuff.rb
86
86
  - test/system/running_tests.rb
87
+ - test/system/stub_tests.rb
87
88
  - test/unit/assert_tests.rb
88
89
  - test/unit/assertions/assert_block_tests.rb
89
90
  - test/unit/assertions/assert_empty_tests.rb
@@ -155,6 +156,7 @@ test_files:
155
156
  - test/support/factory.rb
156
157
  - test/support/inherited_stuff.rb
157
158
  - test/system/running_tests.rb
159
+ - test/system/stub_tests.rb
158
160
  - test/unit/assert_tests.rb
159
161
  - test/unit/assertions/assert_block_tests.rb
160
162
  - test/unit/assertions/assert_empty_tests.rb