much-stub 0.1.1 → 0.1.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.
@@ -1,16 +1,15 @@
1
- require 'assert'
2
- require 'much-stub'
1
+ require "assert"
2
+ require "much-stub"
3
3
 
4
- require 'test/support/factory'
4
+ require "test/support/factory"
5
5
 
6
6
  module MuchStub
7
-
8
7
  class UnitTests < Assert::Context
9
8
  desc "MuchStub"
10
9
  end
11
10
 
12
- class ApiTests < UnitTests
13
- desc "api"
11
+ class APITests < UnitTests
12
+ desc "API"
14
13
  setup do
15
14
  @orig_value = Factory.string
16
15
  @stub_value = Factory.string
@@ -69,8 +68,8 @@ module MuchStub
69
68
 
70
69
  should "be able to call a stub's original method" do
71
70
  err = assert_raises(NotStubbedError){ MuchStub.stub_send(@myobj, :mymeth) }
72
- assert_includes 'not stubbed.', err.message
73
- assert_includes 'test/unit/much-stub_tests.rb', err.backtrace.first
71
+ assert_includes "not stubbed.", err.message
72
+ assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
74
73
 
75
74
  MuchStub.(@myobj, :mymeth){ @stub_value }
76
75
 
@@ -78,13 +77,22 @@ module MuchStub
78
77
  assert_equal @orig_value, MuchStub.stub_send(@myobj, :mymeth)
79
78
  end
80
79
 
80
+ should "be able to add a stub tap" do
81
+ my_meth_called_with = nil
82
+ MuchStub.tap(@myobj, :mymeth){ |value, *args, &block|
83
+ my_meth_called_with = args
84
+ }
85
+
86
+ assert_equal @orig_value, @myobj.mymeth
87
+ assert_equal [], my_meth_called_with
88
+ end
81
89
  end
82
90
 
83
91
  class StubTests < UnitTests
84
92
  desc "Stub"
85
93
  setup do
86
94
  @myclass = Class.new do
87
- def mymeth; 'meth'; end
95
+ def mymeth; "meth"; end
88
96
  def myval(val); val; end
89
97
  def myargs(*args); args; end
90
98
  def myvalargs(val1, val2, *args); [val1, val2, args]; end
@@ -108,7 +116,7 @@ module MuchStub
108
116
  end
109
117
 
110
118
  should "know its names" do
111
- assert_equal 'mymeth', subject.method_name
119
+ assert_equal "mymeth", subject.method_name
112
120
  expected = "__muchstub_stub__#{@myobj.object_id}_#{subject.method_name}"
113
121
  assert_equal expected, subject.name
114
122
  expected = "@__muchstub_stub_#{@myobj.object_id}_" \
@@ -118,23 +126,23 @@ module MuchStub
118
126
 
119
127
  should "complain when called if no do block was given" do
120
128
  err = assert_raises(MuchStub::NotStubbedError){ @myobj.mymeth }
121
- assert_includes 'not stubbed.', err.message
122
- assert_includes 'test/unit/much-stub_tests.rb', err.backtrace.first
129
+ assert_includes "not stubbed.", err.message
130
+ assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
123
131
 
124
- subject.do = proc{ 'mymeth' }
132
+ subject.do = proc{ "mymeth" }
125
133
  assert_nothing_raised do
126
134
  @myobj.mymeth
127
135
  end
128
136
 
129
137
  assert_nothing_raised do
130
- MuchStub::Stub.new(@myobj, :mymeth){ 'mymeth' }
138
+ MuchStub::Stub.new(@myobj, :mymeth){ "mymeth" }
131
139
  end
132
140
  end
133
141
 
134
142
  should "complain if stubbing a method that the object doesn't respond to" do
135
143
  err = assert_raises(MuchStub::StubError){ MuchStub::Stub.new(@myobj, :some_other_meth) }
136
- assert_includes 'does not respond to', err.message
137
- assert_includes 'test/unit/much-stub_tests.rb', err.backtrace.first
144
+ assert_includes "does not respond to", err.message
145
+ assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
138
146
  end
139
147
 
140
148
  should "complain if stubbed and called with no `do` proc given" do
@@ -142,20 +150,20 @@ module MuchStub
142
150
  end
143
151
 
144
152
  should "complain if stubbed and called with mismatched arity" do
145
- MuchStub::Stub.new(@myobj, :myval){ 'myval' }
153
+ MuchStub::Stub.new(@myobj, :myval){ "myval" }
146
154
  err = assert_raises(MuchStub::StubArityError){ @myobj.myval }
147
- assert_includes 'arity mismatch on', err.message
148
- assert_includes 'test/unit/much-stub_tests.rb', err.backtrace.first
155
+ assert_includes "arity mismatch on", err.message
156
+ assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
149
157
 
150
158
  assert_nothing_raised { @myobj.myval(1) }
151
159
  assert_raises(MuchStub::StubArityError){ @myobj.myval(1,2) }
152
160
 
153
- MuchStub::Stub.new(@myobj, :myargs){ 'myargs' }
161
+ MuchStub::Stub.new(@myobj, :myargs){ "myargs" }
154
162
  assert_nothing_raised { @myobj.myargs }
155
163
  assert_nothing_raised { @myobj.myargs(1) }
156
164
  assert_nothing_raised { @myobj.myargs(1,2) }
157
165
 
158
- MuchStub::Stub.new(@myobj, :myvalargs){ 'myvalargs' }
166
+ MuchStub::Stub.new(@myobj, :myvalargs){ "myvalargs" }
159
167
  assert_raises(MuchStub::StubArityError){ @myobj.myvalargs }
160
168
  assert_raises(MuchStub::StubArityError){ @myobj.myvalargs(1) }
161
169
  assert_nothing_raised { @myobj.myvalargs(1,2) }
@@ -164,75 +172,75 @@ module MuchStub
164
172
 
165
173
  should "complain if stubbed with mismatched arity" do
166
174
  err = assert_raises(MuchStub::StubArityError) do
167
- MuchStub::Stub.new(@myobj, :myval).with(){ 'myval' }
175
+ MuchStub::Stub.new(@myobj, :myval).with(){ "myval" }
168
176
  end
169
- assert_includes 'arity mismatch on', err.message
170
- assert_includes 'test/unit/much-stub_tests.rb', err.backtrace.first
177
+ assert_includes "arity mismatch on", err.message
178
+ assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
171
179
 
172
180
  assert_raises(MuchStub::StubArityError) do
173
- MuchStub::Stub.new(@myobj, :myval).with(1,2){ 'myval' }
181
+ MuchStub::Stub.new(@myobj, :myval).with(1,2){ "myval" }
174
182
  end
175
183
  assert_nothing_raised do
176
- MuchStub::Stub.new(@myobj, :myval).with(1){ 'myval' }
184
+ MuchStub::Stub.new(@myobj, :myval).with(1){ "myval" }
177
185
  end
178
186
 
179
187
  assert_nothing_raised do
180
- MuchStub::Stub.new(@myobj, :myargs).with(){ 'myargs' }
188
+ MuchStub::Stub.new(@myobj, :myargs).with(){ "myargs" }
181
189
  end
182
190
  assert_nothing_raised do
183
- MuchStub::Stub.new(@myobj, :myargs).with(1,2){ 'myargs' }
191
+ MuchStub::Stub.new(@myobj, :myargs).with(1,2){ "myargs" }
184
192
  end
185
193
  assert_nothing_raised do
186
- MuchStub::Stub.new(@myobj, :myargs).with(1){ 'myargs' }
194
+ MuchStub::Stub.new(@myobj, :myargs).with(1){ "myargs" }
187
195
  end
188
196
 
189
197
  assert_raises(MuchStub::StubArityError) do
190
- MuchStub::Stub.new(@myobj, :myvalargs).with(){ 'myvalargs' }
198
+ MuchStub::Stub.new(@myobj, :myvalargs).with(){ "myvalargs" }
191
199
  end
192
200
  assert_raises(MuchStub::StubArityError) do
193
- MuchStub::Stub.new(@myobj, :myvalargs).with(1){ 'myvalargs' }
201
+ MuchStub::Stub.new(@myobj, :myvalargs).with(1){ "myvalargs" }
194
202
  end
195
203
  assert_nothing_raised do
196
- MuchStub::Stub.new(@myobj, :myvalargs).with(1,2){ 'myvalargs' }
204
+ MuchStub::Stub.new(@myobj, :myvalargs).with(1,2){ "myvalargs" }
197
205
  end
198
206
  assert_nothing_raised do
199
- MuchStub::Stub.new(@myobj, :myvalargs).with(1,2,3){ 'myvalargs' }
207
+ MuchStub::Stub.new(@myobj, :myvalargs).with(1,2,3){ "myvalargs" }
200
208
  end
201
209
  end
202
210
 
203
211
  should "stub methods with no args" do
204
212
  subject.teardown
205
213
 
206
- assert_equal 'meth', @myobj.mymeth
207
- MuchStub::Stub.new(@myobj, :mymeth){ 'mymeth' }
208
- assert_equal 'mymeth', @myobj.mymeth
214
+ assert_equal "meth", @myobj.mymeth
215
+ MuchStub::Stub.new(@myobj, :mymeth){ "mymeth" }
216
+ assert_equal "mymeth", @myobj.mymeth
209
217
  end
210
218
 
211
219
  should "stub methods with required arg" do
212
220
  assert_equal 1, @myobj.myval(1)
213
221
  stub = MuchStub::Stub.new(@myobj, :myval){ |val| val.to_s }
214
- assert_equal '1', @myobj.myval(1)
215
- assert_equal '2', @myobj.myval(2)
216
- stub.with(2){ 'two' }
217
- assert_equal 'two', @myobj.myval(2)
222
+ assert_equal "1", @myobj.myval(1)
223
+ assert_equal "2", @myobj.myval(2)
224
+ stub.with(2){ "two" }
225
+ assert_equal "two", @myobj.myval(2)
218
226
  end
219
227
 
220
228
  should "stub methods with variable args" do
221
229
  assert_equal [1,2], @myobj.myargs(1,2)
222
- stub = MuchStub::Stub.new(@myobj, :myargs){ |*args| args.join(',') }
223
- assert_equal '1,2', @myobj.myargs(1,2)
224
- assert_equal '3,4,5', @myobj.myargs(3,4,5)
225
- stub.with(3,4,5){ 'three-four-five' }
226
- assert_equal 'three-four-five', @myobj.myargs(3,4,5)
230
+ stub = MuchStub::Stub.new(@myobj, :myargs){ |*args| args.join(",") }
231
+ assert_equal "1,2", @myobj.myargs(1,2)
232
+ assert_equal "3,4,5", @myobj.myargs(3,4,5)
233
+ stub.with(3,4,5){ "three-four-five" }
234
+ assert_equal "three-four-five", @myobj.myargs(3,4,5)
227
235
  end
228
236
 
229
237
  should "stub methods with required args and variable args" do
230
238
  assert_equal [1,2, [3]], @myobj.myvalargs(1,2,3)
231
- stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(',') }
232
- assert_equal '1,2,3', @myobj.myvalargs(1,2,3)
233
- assert_equal '3,4,5', @myobj.myvalargs(3,4,5)
234
- stub.with(3,4,5){ 'three-four-five' }
235
- assert_equal 'three-four-five', @myobj.myvalargs(3,4,5)
239
+ stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(",") }
240
+ assert_equal "1,2,3", @myobj.myvalargs(1,2,3)
241
+ assert_equal "3,4,5", @myobj.myvalargs(3,4,5)
242
+ stub.with(3,4,5){ "three-four-five" }
243
+ assert_equal "three-four-five", @myobj.myvalargs(3,4,5)
236
244
  end
237
245
 
238
246
  should "stub methods that yield blocks" do
@@ -242,9 +250,9 @@ module MuchStub
242
250
  assert_equal true, blkcalled
243
251
 
244
252
  blkcalled = false
245
- MuchStub::Stub.new(@myobj, :myblk){ blkcalled = 'true' }
253
+ MuchStub::Stub.new(@myobj, :myblk){ blkcalled = "true" }
246
254
  @myobj.myblk(&blk)
247
- assert_equal 'true', blkcalled
255
+ assert_equal "true", blkcalled
248
256
  end
249
257
 
250
258
  should "stub methods even if they are not local to the object" do
@@ -263,56 +271,56 @@ module MuchStub
263
271
 
264
272
  assert_equal [1,2,[3]], mydelegator.myvalargs(1,2,3)
265
273
  stub = MuchStub::Stub.new(mydelegator, :myvalargs){ |*args| args.inspect }
266
- assert_equal '[1, 2, 3]', mydelegator.myvalargs(1,2,3)
267
- assert_equal '[4, 5, 6]', mydelegator.myvalargs(4,5,6)
268
- stub.with(4,5,6){ 'four-five-six' }
269
- assert_equal 'four-five-six', mydelegator.myvalargs(4,5,6)
274
+ assert_equal "[1, 2, 3]", mydelegator.myvalargs(1,2,3)
275
+ assert_equal "[4, 5, 6]", mydelegator.myvalargs(4,5,6)
276
+ stub.with(4,5,6){ "four-five-six" }
277
+ assert_equal "four-five-six", mydelegator.myvalargs(4,5,6)
270
278
  end
271
279
 
272
280
  should "call to the original method" do
273
281
  subject.teardown
274
282
 
275
283
  # no args
276
- stub = MuchStub::Stub.new(@myobj, :mymeth){ 'mymeth' }
277
- assert_equal 'mymeth', stub.call([])
278
- assert_equal 'meth', stub.call_method([])
284
+ stub = MuchStub::Stub.new(@myobj, :mymeth){ "mymeth" }
285
+ assert_equal "mymeth", stub.call([])
286
+ assert_equal "meth", stub.call_method([])
279
287
 
280
288
  # static args
281
289
  stub = MuchStub::Stub.new(@myobj, :myval){ |val| val.to_s }
282
- assert_equal '1', stub.call([1])
290
+ assert_equal "1", stub.call([1])
283
291
  assert_equal 1, stub.call_method([1])
284
- assert_equal '2', stub.call([2])
292
+ assert_equal "2", stub.call([2])
285
293
  assert_equal 2, stub.call_method([2])
286
- stub.with(2){ 'two' }
287
- assert_equal 'two', stub.call([2])
294
+ stub.with(2){ "two" }
295
+ assert_equal "two", stub.call([2])
288
296
  assert_equal 2, stub.call_method([2])
289
297
 
290
298
  # dynamic args
291
- stub = MuchStub::Stub.new(@myobj, :myargs){ |*args| args.join(',') }
292
- assert_equal '1,2', stub.call([1,2])
299
+ stub = MuchStub::Stub.new(@myobj, :myargs){ |*args| args.join(",") }
300
+ assert_equal "1,2", stub.call([1,2])
293
301
  assert_equal [1,2], stub.call_method([1,2])
294
- assert_equal '3,4,5', stub.call([3,4,5])
302
+ assert_equal "3,4,5", stub.call([3,4,5])
295
303
  assert_equal [3,4,5], stub.call_method([3,4,5])
296
- stub.with(3,4,5){ 'three-four-five' }
297
- assert_equal 'three-four-five', stub.call([3,4,5])
304
+ stub.with(3,4,5){ "three-four-five" }
305
+ assert_equal "three-four-five", stub.call([3,4,5])
298
306
  assert_equal [3,4,5], stub.call_method([3,4,5])
299
307
 
300
308
  # mixed static/dynamic args
301
- stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(',') }
302
- assert_equal '1,2,3', stub.call([1,2,3])
309
+ stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(",") }
310
+ assert_equal "1,2,3", stub.call([1,2,3])
303
311
  assert_equal [1,2, [3]], stub.call_method([1,2,3])
304
- assert_equal '3,4,5', stub.call([3,4,5])
312
+ assert_equal "3,4,5", stub.call([3,4,5])
305
313
  assert_equal [3,4,[5]], stub.call_method([3,4,5])
306
- stub.with(3,4,5){ 'three-four-five' }
307
- assert_equal 'three-four-five', stub.call([3,4,5])
314
+ stub.with(3,4,5){ "three-four-five" }
315
+ assert_equal "three-four-five", stub.call([3,4,5])
308
316
  assert_equal [3,4,[5]], stub.call_method([3,4,5])
309
317
 
310
318
  # blocks
311
319
  blkcalled = false
312
320
  blk = proc{ blkcalled = true }
313
- stub = MuchStub::Stub.new(@myobj, :myblk){ blkcalled = 'true' }
321
+ stub = MuchStub::Stub.new(@myobj, :myblk){ blkcalled = "true" }
314
322
  stub.call([], &blk)
315
- assert_equal 'true', blkcalled
323
+ assert_equal "true", blkcalled
316
324
  stub.call_method([], &blk)
317
325
  assert_equal true, blkcalled
318
326
  end
@@ -326,17 +334,16 @@ module MuchStub
326
334
  should "be removable" do
327
335
  assert_equal 1, @myobj.myval(1)
328
336
  stub = MuchStub::Stub.new(@myobj, :myval){ |val| val.to_s }
329
- assert_equal '1', @myobj.myval(1)
337
+ assert_equal "1", @myobj.myval(1)
330
338
  stub.teardown
331
339
  assert_equal 1, @myobj.myval(1)
332
340
  end
333
341
 
334
342
  should "have a readable inspect" do
335
- expected = "#<#{subject.class}:#{'0x0%x' % (subject.object_id << 1)}" \
343
+ expected = "#<#{subject.class}:#{"0x0%x" % (subject.object_id << 1)}" \
336
344
  " @method_name=#{subject.method_name.inspect}>"
337
345
  assert_equal expected, subject.inspect
338
346
  end
339
-
340
347
  end
341
348
 
342
349
  class MutatingArgsStubTests < StubTests
@@ -354,7 +361,6 @@ module MuchStub
354
361
  should "not raise a stub error when called" do
355
362
  assert_nothing_raised{ @stub.call([@arg]) }
356
363
  end
357
-
358
364
  end
359
365
 
360
366
  class StubInheritedMethodAfterStubbedOnParentTests < StubTests
@@ -369,11 +375,11 @@ module MuchStub
369
375
 
370
376
  should "allow stubbing them independently of each other" do
371
377
  assert_nothing_raised do
372
- @parent_stub.with(1, 2){ 'parent' }
373
- @child_stub.with(1, 2){ 'child' }
378
+ @parent_stub.with(1, 2){ "parent" }
379
+ @child_stub.with(1, 2){ "child" }
374
380
  end
375
- assert_equal 'parent', @parent_class.new(1, 2)
376
- assert_equal 'child', @child_class.new(1, 2)
381
+ assert_equal "parent", @parent_class.new(1, 2)
382
+ assert_equal "child", @child_class.new(1, 2)
377
383
  end
378
384
 
379
385
  should "not raise any errors when tearing down the parent before the child" do
@@ -382,7 +388,6 @@ module MuchStub
382
388
  @child_stub.teardown
383
389
  end
384
390
  end
385
-
386
391
  end
387
392
 
388
393
  class NullStubTests < UnitTests
@@ -393,7 +398,6 @@ module MuchStub
393
398
  subject{ @ns }
394
399
 
395
400
  should have_imeths :teardown
396
-
397
401
  end
398
402
 
399
403
  class ParameterListTests < UnitTests
@@ -401,7 +405,7 @@ module MuchStub
401
405
  setup do
402
406
  many_args = (0..ParameterList::LETTERS.size).map do
403
407
  Assert::Factory.string
404
- end.join(', ')
408
+ end.join(", ")
405
409
  @object = Class.new
406
410
  # use `class_eval` with string to easily define methods with many params
407
411
  @object.class_eval <<-methods
@@ -414,23 +418,22 @@ module MuchStub
414
418
  subject{ ParameterList }
415
419
 
416
420
  should "build a parameter list for a method that takes no args" do
417
- assert_equal '&block', subject.new(@object, 'noargs')
421
+ assert_equal "&block", subject.new(@object, "noargs")
418
422
  end
419
423
 
420
424
  should "build a parameter list for a method that takes any args" do
421
- assert_equal '*args, &block', subject.new(@object, 'anyargs')
425
+ assert_equal "*args, &block", subject.new(@object, "anyargs")
422
426
  end
423
427
 
424
428
  should "build a parameter list for a method that takes many args" do
425
- expected = "#{ParameterList::LETTERS.join(', ')}, aa, &block"
426
- assert_equal expected, subject.new(@object, 'manyargs')
429
+ expected = "#{ParameterList::LETTERS.join(", ")}, aa, &block"
430
+ assert_equal expected, subject.new(@object, "manyargs")
427
431
  end
428
432
 
429
433
  should "build a parameter list for a method that takes a minimum number of args" do
430
- expected = "#{ParameterList::LETTERS.join(', ')}, aa, *args, &block"
431
- assert_equal expected, subject.new(@object, 'minargs')
434
+ expected = "#{ParameterList::LETTERS.join(", ")}, aa, *args, &block"
435
+ assert_equal expected, subject.new(@object, "minargs")
432
436
  end
433
-
434
437
  end
435
438
 
436
439
  class ChangeHashObject
@@ -446,5 +449,4 @@ module MuchStub
446
449
  @value = 1
447
450
  end
448
451
  end
449
-
450
452
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: much-stub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Redding
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-05-06 00:00:00.000000000 Z
12
+ date: 2020-01-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: assert
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: 2.17.0
20
+ version: 2.18.0
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: 2.17.0
27
+ version: 2.18.0
28
28
  description: Stubbing API for replacing method calls on objects in test runs.
29
29
  email:
30
30
  - kelly@kellyredding.com
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  version: '0'
67
67
  requirements: []
68
68
  rubyforge_project:
69
- rubygems_version: 2.7.7
69
+ rubygems_version: 2.7.6.2
70
70
  signing_key:
71
71
  specification_version: 4
72
72
  summary: Stubbing API for replacing method calls on objects in test runs.