much-stub 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/much-stub/version.rb +1 -1
- data/lib/much-stub.rb +30 -21
- data/test/unit/much-stub_tests.rb +38 -25
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc032b1684e0734a374eb6b4a5df50d2980cc9a7cbf7e1149af37e1a4937a5f9
|
4
|
+
data.tar.gz: 35b88afbbc8c997dacc497e71b5469a4a5062e59259385533b63387a2865180e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 615eaeb52480305c353204c4a1a27653a84d4f855f323356bcd09f55baf421998ccd35ad8b9c80ef1d431dd700db754be51ed05f7734644e452b2ebe16d3e828
|
7
|
+
data.tar.gz: 4f7f4fc3666baca6bed52dec567fccca5fa6248946182c84d1a3325f2e7a8eaab88ffad4287b7c7a8f242e6b523c63f0ac60605c42ed16ae6ea8a331a19be09d
|
data/lib/much-stub/version.rb
CHANGED
data/lib/much-stub.rb
CHANGED
@@ -50,34 +50,34 @@ module MuchStub
|
|
50
50
|
stubs.keys.each{ |key| stubs.delete(key).teardown }
|
51
51
|
end
|
52
52
|
|
53
|
-
def self.stub_send(obj, meth, *
|
53
|
+
def self.stub_send(obj, meth, *pargs, **kargs, &block)
|
54
54
|
orig_caller = caller_locations
|
55
55
|
stub =
|
56
56
|
stubs.fetch(MuchStub::Stub.key(obj, meth)) do
|
57
57
|
raise NotStubbedError, "`#{meth}` not stubbed.", orig_caller.map(&:to_s)
|
58
58
|
end
|
59
|
-
stub.call_method(
|
59
|
+
stub.call_method(*pargs, **kargs, &block)
|
60
60
|
end
|
61
61
|
|
62
62
|
def self.tap(obj, meth, &tap_block)
|
63
|
-
stub(obj, meth) do |*
|
64
|
-
stub_send(obj, meth, *
|
65
|
-
tap_block&.call(value, *
|
63
|
+
stub(obj, meth) do |*pargs, **kargs, &block|
|
64
|
+
stub_send(obj, meth, *pargs, **kargs, &block).tap do |value|
|
65
|
+
tap_block&.call(value, *pargs, **kargs, &block)
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
70
|
def self.tap_on_call(obj, meth, &on_call_block)
|
71
|
-
tap(obj, meth) do |value, *
|
72
|
-
on_call_block&.call(value, MuchStub::Call.new(*
|
71
|
+
tap(obj, meth) do |value, *pargs, **kargs, &block|
|
72
|
+
on_call_block&.call(value, MuchStub::Call.new(*pargs, **kargs, &block))
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
76
|
def self.spy(obj, *meths, **return_values)
|
77
77
|
MuchStub::CallSpy.new(**return_values).call_spy_tap do |spy|
|
78
78
|
meths.each do |meth|
|
79
|
-
stub(obj, meth) do |*
|
80
|
-
spy.__send__(meth, *
|
79
|
+
stub(obj, meth) do |*pargs, **kargs, &block|
|
80
|
+
spy.__send__(meth, *pargs, **kargs, &block)
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
@@ -108,12 +108,13 @@ module MuchStub
|
|
108
108
|
@do = block || @do
|
109
109
|
end
|
110
110
|
|
111
|
-
def call_method(
|
112
|
-
@method.call(*
|
111
|
+
def call_method(*pargs, **kargs, &block)
|
112
|
+
@method.call(*pargs, **kargs, &block)
|
113
113
|
end
|
114
114
|
|
115
|
-
def call(
|
115
|
+
def call(*pargs, orig_caller: nil, **kargs, &block)
|
116
116
|
orig_caller ||= caller_locations
|
117
|
+
args = combined_args(pargs, kargs)
|
117
118
|
unless MuchStub.arity_matches?(@method, args)
|
118
119
|
raise(
|
119
120
|
StubArityError.new(
|
@@ -124,14 +125,15 @@ module MuchStub
|
|
124
125
|
),
|
125
126
|
)
|
126
127
|
end
|
127
|
-
lookup(
|
128
|
+
lookup(pargs, kargs, orig_caller).call(*pargs, **kargs, &block)
|
128
129
|
rescue NotStubbedError
|
129
130
|
@lookup.rehash
|
130
|
-
lookup(
|
131
|
+
lookup(pargs, kargs, orig_caller).call(*pargs, **kargs, &block)
|
131
132
|
end
|
132
133
|
|
133
|
-
def with(*
|
134
|
+
def with(*pargs, **kargs, &block)
|
134
135
|
orig_caller = caller_locations
|
136
|
+
args = combined_args(pargs, kargs)
|
135
137
|
unless MuchStub.arity_matches?(@method, args)
|
136
138
|
raise(
|
137
139
|
StubArityError.new(
|
@@ -148,8 +150,8 @@ module MuchStub
|
|
148
150
|
|
149
151
|
def on_call(&on_call_block)
|
150
152
|
stub_block =
|
151
|
-
->(*
|
152
|
-
on_call_block&.call(MuchStub::Call.new(*
|
153
|
+
->(*pargs, **kargs, &block){
|
154
|
+
on_call_block&.call(MuchStub::Call.new(*pargs, **kargs, &block))
|
153
155
|
}
|
154
156
|
if @lookup.empty?
|
155
157
|
@do = stub_block
|
@@ -198,13 +200,16 @@ module MuchStub
|
|
198
200
|
|
199
201
|
MuchStub.instance_variable_set(@ivar_name, self)
|
200
202
|
@metaclass.class_eval <<-stub_method
|
201
|
-
def #{@method_name}(*
|
202
|
-
MuchStub
|
203
|
+
def #{@method_name}(*pargs, **kargs, &block)
|
204
|
+
MuchStub
|
205
|
+
.instance_variable_get("#{@ivar_name}")
|
206
|
+
.call(*pargs, orig_caller: caller_locations, **kargs, &block)
|
203
207
|
end
|
204
208
|
stub_method
|
205
209
|
end
|
206
210
|
|
207
|
-
def lookup(
|
211
|
+
def lookup(pargs, kargs, orig_caller)
|
212
|
+
args = combined_args(pargs, kargs)
|
208
213
|
@lookup.fetch(args) do
|
209
214
|
self.do ||
|
210
215
|
begin
|
@@ -229,6 +234,10 @@ module MuchStub
|
|
229
234
|
def inspect_call(args)
|
230
235
|
"`#{@method_name}(#{args.map(&:inspect).join(",")})`"
|
231
236
|
end
|
237
|
+
|
238
|
+
def combined_args(pargs, kargs)
|
239
|
+
[*pargs, (kargs.empty? ? nil : kargs)].compact
|
240
|
+
end
|
232
241
|
end
|
233
242
|
|
234
243
|
StubError = Class.new(ArgumentError)
|
@@ -268,7 +277,7 @@ module MuchStub
|
|
268
277
|
def self.new(object, method_name)
|
269
278
|
arity = get_arity(object, method_name)
|
270
279
|
params = build_params_from_arity(arity)
|
271
|
-
params << "*
|
280
|
+
params << "*pargs, **kargs" if arity < 0
|
272
281
|
params << "&block"
|
273
282
|
params.join(", ")
|
274
283
|
end
|
@@ -200,6 +200,10 @@ module MuchStub
|
|
200
200
|
[val1, val2, args]
|
201
201
|
end
|
202
202
|
|
203
|
+
def mykargs(val1:, **kargs)
|
204
|
+
[val1, kargs]
|
205
|
+
end
|
206
|
+
|
203
207
|
def myblk(&block)
|
204
208
|
block.call
|
205
209
|
end
|
@@ -422,46 +426,54 @@ module MuchStub
|
|
422
426
|
|
423
427
|
# no args
|
424
428
|
stub = MuchStub::Stub.new(@myobj, :mymeth){ "mymeth" }
|
425
|
-
assert_equal "mymeth", stub.call
|
426
|
-
assert_equal "meth", stub.call_method
|
429
|
+
assert_equal "mymeth", stub.call
|
430
|
+
assert_equal "meth", stub.call_method
|
427
431
|
|
428
432
|
# static args
|
429
433
|
stub = MuchStub::Stub.new(@myobj, :myval, &:to_s)
|
430
|
-
assert_equal "1", stub.call(
|
431
|
-
assert_equal 1, stub.call_method(
|
432
|
-
assert_equal "2", stub.call(
|
433
|
-
assert_equal 2, stub.call_method(
|
434
|
+
assert_equal "1", stub.call(1)
|
435
|
+
assert_equal 1, stub.call_method(1)
|
436
|
+
assert_equal "2", stub.call(2)
|
437
|
+
assert_equal 2, stub.call_method(2)
|
434
438
|
stub.with(2){ "two" }
|
435
|
-
assert_equal "two", stub.call(
|
436
|
-
assert_equal 2, stub.call_method(
|
439
|
+
assert_equal "two", stub.call(2)
|
440
|
+
assert_equal 2, stub.call_method(2)
|
437
441
|
|
438
442
|
# dynamic args
|
439
443
|
stub = MuchStub::Stub.new(@myobj, :myargs){ |*args| args.join(",") }
|
440
|
-
assert_equal "1,2", stub.call(
|
441
|
-
assert_equal [1, 2], stub.call_method(
|
442
|
-
assert_equal "3,4,5", stub.call(
|
443
|
-
assert_equal [3, 4, 5], stub.call_method(
|
444
|
+
assert_equal "1,2", stub.call(1, 2)
|
445
|
+
assert_equal [1, 2], stub.call_method(1, 2)
|
446
|
+
assert_equal "3,4,5", stub.call(3, 4, 5)
|
447
|
+
assert_equal [3, 4, 5], stub.call_method(3, 4, 5)
|
444
448
|
stub.with(3, 4, 5){ "three-four-five" }
|
445
|
-
assert_equal "three-four-five", stub.call(
|
446
|
-
assert_equal [3, 4, 5],
|
449
|
+
assert_equal "three-four-five", stub.call(3, 4, 5)
|
450
|
+
assert_equal [3, 4, 5], stub.call_method(3, 4, 5)
|
447
451
|
|
448
452
|
# mixed static/dynamic args
|
449
453
|
stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(",") }
|
450
|
-
assert_equal "1,2,3", stub.call(
|
451
|
-
assert_equal [1, 2, [3]], stub.call_method(
|
452
|
-
assert_equal "3,4,5", stub.call(
|
453
|
-
assert_equal [3, 4, [5]], stub.call_method(
|
454
|
+
assert_equal "1,2,3", stub.call(1, 2, 3)
|
455
|
+
assert_equal [1, 2, [3]], stub.call_method(1, 2, 3)
|
456
|
+
assert_equal "3,4,5", stub.call(3, 4, 5)
|
457
|
+
assert_equal [3, 4, [5]], stub.call_method(3, 4, 5)
|
454
458
|
stub.with(3, 4, 5){ "three-four-five" }
|
455
|
-
assert_equal "three-four-five", stub.call(
|
456
|
-
assert_equal [3, 4, [5]], stub.call_method(
|
459
|
+
assert_equal "three-four-five", stub.call(3, 4, 5)
|
460
|
+
assert_equal [3, 4, [5]], stub.call_method(3, 4, 5)
|
461
|
+
|
462
|
+
# keyword args
|
463
|
+
stub = MuchStub::Stub.new(@myobj, :mykargs){ |**kargs| kargs.inspect }
|
464
|
+
assert_equal "{:val1=>1, :val2=>2}", stub.call(val1: 1, val2: 2)
|
465
|
+
assert_equal [1, { val2: 2 }], stub.call_method(val1: 1, val2: 2)
|
466
|
+
stub.with(val1: 3, val2: 4){ "three-four" }
|
467
|
+
assert_equal "three-four", stub.call(val1: 3, val2: 4)
|
468
|
+
assert_equal [3, { val2: 4 }], stub.call_method(val1: 3, val2: 4)
|
457
469
|
|
458
470
|
# blocks
|
459
471
|
blkcalled = false
|
460
472
|
blk = proc{ blkcalled = true }
|
461
473
|
stub = MuchStub::Stub.new(@myobj, :myblk){ blkcalled = "true" }
|
462
|
-
stub.call(
|
474
|
+
stub.call(&blk)
|
463
475
|
assert_equal "true", blkcalled
|
464
|
-
stub.call_method(
|
476
|
+
stub.call_method(&blk)
|
465
477
|
assert_equal true, blkcalled
|
466
478
|
end
|
467
479
|
|
@@ -500,7 +512,7 @@ module MuchStub
|
|
500
512
|
subject{ @stub }
|
501
513
|
|
502
514
|
should "not raise a stub error when called" do
|
503
|
-
assert_nothing_raised{ @stub.call(
|
515
|
+
assert_nothing_raised{ @stub.call(@arg) }
|
504
516
|
end
|
505
517
|
end
|
506
518
|
|
@@ -564,7 +576,7 @@ module MuchStub
|
|
564
576
|
end
|
565
577
|
|
566
578
|
should "build a parameter list for a method that takes any args" do
|
567
|
-
assert_equal "*
|
579
|
+
assert_equal "*pargs, **kargs, &block", subject.new(@object, "anyargs")
|
568
580
|
end
|
569
581
|
|
570
582
|
should "build a parameter list for a method that takes many args" do
|
@@ -574,7 +586,8 @@ module MuchStub
|
|
574
586
|
|
575
587
|
should "build a parameter list for a method that takes a minimum number "\
|
576
588
|
"of args" do
|
577
|
-
expected =
|
589
|
+
expected =
|
590
|
+
"#{ParameterList::LETTERS.join(", ")}, aa, *pargs, **kargs, &block"
|
578
591
|
assert_equal expected, subject.new(@object, "minargs")
|
579
592
|
end
|
580
593
|
end
|