much-stub 0.1.3 → 0.1.4
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.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/lib/much-stub.rb +14 -6
- data/lib/much-stub/call_spy.rb +56 -23
- data/lib/much-stub/version.rb +1 -1
- data/much-stub.gemspec +1 -2
- data/test/unit/call_spy_tests.rb +2 -2
- data/test/unit/much-stub_tests.rb +26 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21883570fda48199a217f7489b6c45f6d3c2e1d1cb5aa872847e154033754771
|
4
|
+
data.tar.gz: bf63e826137aa1d315d48823c7a11787a09be559b16abc600a168edc19ff9068
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f6c8c294b7f7d006b3c88a3890d24e3ff913110caa11b70642d7ba246449dd3dcb20de0df46e939d90a9b917f0c509144c260fdbfc689f6a89be1f9fb5b942f
|
7
|
+
data.tar.gz: 6a7c16b6b19625ad5b84df6d5413119a921f81425278f5f3b8c0b579895c9a22e8de58da349e56684412d93a74f450ba6e7ed2b53f06ad9185f9cf8ee31ec1a5
|
data/README.md
CHANGED
@@ -116,6 +116,12 @@ MuchStub.(my_object, :basic_method) { |*args|
|
|
116
116
|
MuchStub.(my_object, :basic_method).on_call { |call|
|
117
117
|
basic_method_called_with = call
|
118
118
|
}
|
119
|
+
# OR
|
120
|
+
MuchStub.on_call(my_object, :basic_method) { |call|
|
121
|
+
# MucStub.on_call(...) { ... } is equivalent to
|
122
|
+
# MuchStub.(...).on_call { ... }
|
123
|
+
basic_method_called_with = call
|
124
|
+
}
|
119
125
|
|
120
126
|
my_object.basic_method(123)
|
121
127
|
basic_method_called_with.args
|
data/lib/much-stub.rb
CHANGED
@@ -18,16 +18,24 @@ module MuchStub
|
|
18
18
|
return false
|
19
19
|
end
|
20
20
|
|
21
|
-
def self.call(*args, &block)
|
22
|
-
self.stub(*args, &block)
|
23
|
-
end
|
24
|
-
|
25
21
|
def self.stub(obj, meth, &block)
|
26
22
|
key = self.stub_key(obj, meth)
|
27
23
|
self.stubs[key] ||= MuchStub::Stub.new(obj, meth, caller_locations)
|
28
24
|
self.stubs[key].tap{ |s| s.do = block }
|
29
25
|
end
|
30
26
|
|
27
|
+
def self.call(*args, &block)
|
28
|
+
self.stub(*args, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.stub_on_call(*args, &on_call_block)
|
32
|
+
self.stub(*args).on_call(&on_call_block)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.on_call(*args, &on_call_block)
|
36
|
+
self.stub_on_call(*args, &on_call_block)
|
37
|
+
end
|
38
|
+
|
31
39
|
def self.unstub(obj, meth)
|
32
40
|
key = self.stub_key(obj, meth)
|
33
41
|
(self.stubs.delete(key) || MuchStub::NullStub.new).teardown
|
@@ -60,10 +68,10 @@ module MuchStub
|
|
60
68
|
end
|
61
69
|
|
62
70
|
def self.spy(obj, *meths, **return_values)
|
63
|
-
MuchStub::CallSpy.new(**return_values).
|
71
|
+
MuchStub::CallSpy.new(**return_values).call_spy_tap do |spy|
|
64
72
|
meths.each do |meth|
|
65
73
|
self.stub(obj, meth) { |*args, &block|
|
66
|
-
spy.
|
74
|
+
spy.__send__(meth, *args, &block)
|
67
75
|
}
|
68
76
|
end
|
69
77
|
end
|
data/lib/much-stub/call_spy.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "much-stub/call"
|
2
2
|
|
3
3
|
module MuchStub
|
4
|
-
class CallSpy
|
4
|
+
class CallSpy < ::BasicObject
|
5
5
|
METHOD_NAME_REPLACEMENTS = {
|
6
6
|
"!" => "_bang",
|
7
7
|
"?" => "_predicate"
|
@@ -9,10 +9,42 @@ module MuchStub
|
|
9
9
|
|
10
10
|
def initialize(**return_values)
|
11
11
|
@call_spy_return_values = return_values.transform_keys{ |key| key.to_s }
|
12
|
-
|
13
|
-
@call_spy_method_calls = Hash.new { |hash, key| hash[key] = [] }
|
12
|
+
@call_spy_method_calls = ::Hash.new { |hash, key| hash[key] = [] }
|
14
13
|
@call_spy_method_return_values =
|
15
|
-
Hash.new { |hash, key| hash[key] = call_spy_return_value_proc(key) }
|
14
|
+
::Hash.new { |hash, key| hash[key] = call_spy_return_value_proc(key) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def call_spy_tap
|
18
|
+
yield self
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def ==(other)
|
23
|
+
self.equal?(other)
|
24
|
+
end
|
25
|
+
|
26
|
+
def ===(other)
|
27
|
+
self.equal?(other)
|
28
|
+
end
|
29
|
+
|
30
|
+
def eql?(other)
|
31
|
+
self.equal?(other)
|
32
|
+
end
|
33
|
+
|
34
|
+
def equal?(other)
|
35
|
+
self.__id__ == other.__id__
|
36
|
+
end
|
37
|
+
|
38
|
+
def hash
|
39
|
+
self.__id__
|
40
|
+
end
|
41
|
+
|
42
|
+
def respond_to?(*)
|
43
|
+
true
|
44
|
+
end
|
45
|
+
|
46
|
+
def inspect
|
47
|
+
"#<MuchStub::CallSpy:#{"0x0%x" % (self.__id__ << 1)}>"
|
16
48
|
end
|
17
49
|
|
18
50
|
private
|
@@ -25,7 +57,7 @@ module MuchStub
|
|
25
57
|
value = @call_spy_return_values[method_name]
|
26
58
|
return value if value.respond_to?(:call)
|
27
59
|
|
28
|
-
|
60
|
+
::Proc.new { value.nil? ? self : value }
|
29
61
|
end
|
30
62
|
|
31
63
|
def call_spy_normalize_method_name(name)
|
@@ -36,8 +68,8 @@ module MuchStub
|
|
36
68
|
|
37
69
|
def call_spy_define_spied_method(name)
|
38
70
|
method_name = call_spy_normalize_method_name(name)
|
39
|
-
|
40
|
-
call = MuchStub::Call.new(*args, &block)
|
71
|
+
call_spy_define_metaclass_method(name) do |*args, &block|
|
72
|
+
call = ::MuchStub::Call.new(*args, &block)
|
41
73
|
@call_spy_method_calls[method_name] << call
|
42
74
|
call_spy_method_return_value(name, call)
|
43
75
|
end
|
@@ -47,45 +79,46 @@ module MuchStub
|
|
47
79
|
spied_method_name = query_method_match[1]
|
48
80
|
query_method_suffix = query_method_match[2]
|
49
81
|
method_name = call_spy_normalize_method_name(spied_method_name)
|
50
|
-
|
51
|
-
yield(method_name) if block_given?
|
82
|
+
call_spy_define_metaclass_method("#{method_name}#{query_method_suffix}") do
|
83
|
+
yield(method_name) if ::Kernel.block_given?
|
52
84
|
end
|
53
85
|
end
|
54
86
|
|
87
|
+
def call_spy_define_metaclass_method(name, &block)
|
88
|
+
metaclass = class << self; self; end
|
89
|
+
metaclass.define_method(name, &block)
|
90
|
+
end
|
91
|
+
|
55
92
|
def method_missing(name, *args, &block)
|
56
93
|
if (match = name.match(/(\w+)(_calls)\z/))
|
57
94
|
call_spy_define_query_method(match) do |method_name|
|
58
95
|
@call_spy_method_calls[method_name]
|
59
96
|
end
|
60
|
-
self.
|
97
|
+
self.__send__(name, *args, &block)
|
61
98
|
elsif (match = name.match(/(\w+)(_last_called_with)\z/))
|
62
99
|
call_spy_define_query_method(match) do |method_name|
|
63
|
-
self.
|
100
|
+
self.__send__("#{method_name}_calls").last
|
64
101
|
end
|
65
|
-
self.
|
102
|
+
self.__send__(name, *args, &block)
|
66
103
|
elsif (match = name.match(/(\w+)(_called_with)\z/))
|
67
104
|
call_spy_define_query_method(match) do |method_name|
|
68
|
-
self.
|
105
|
+
self.__send__("#{method_name}_last_called_with")
|
69
106
|
end
|
70
|
-
self.
|
107
|
+
self.__send__(name, *args, &block)
|
71
108
|
elsif (match = name.match(/(\w+)(_call_count)\z/))
|
72
109
|
call_spy_define_query_method(match) do |method_name|
|
73
|
-
self.
|
110
|
+
self.__send__("#{method_name}_calls").size
|
74
111
|
end
|
75
|
-
self.
|
112
|
+
self.__send__(name, *args, &block)
|
76
113
|
elsif (match = name.match(/(\w+)(_called\?)\z/))
|
77
114
|
call_spy_define_query_method(match) do |method_name|
|
78
|
-
self.
|
115
|
+
self.__send__("#{method_name}_call_count") > 0
|
79
116
|
end
|
80
|
-
self.
|
117
|
+
self.__send__(name, *args, &block)
|
81
118
|
else
|
82
119
|
call_spy_define_spied_method(name)
|
83
|
-
self.
|
120
|
+
self.__send__(name, *args, &block)
|
84
121
|
end
|
85
122
|
end
|
86
|
-
|
87
|
-
def respond_to_missing?(*args)
|
88
|
-
super
|
89
|
-
end
|
90
123
|
end
|
91
124
|
end
|
data/lib/much-stub/version.rb
CHANGED
data/much-stub.gemspec
CHANGED
data/test/unit/call_spy_tests.rb
CHANGED
@@ -4,7 +4,7 @@ require "much-stub/call_spy"
|
|
4
4
|
require "test/support/factory"
|
5
5
|
|
6
6
|
class MuchStub::CallSpy
|
7
|
-
class UnitTests < Assert::Context
|
7
|
+
class UnitTests < ::Assert::Context
|
8
8
|
desc "MuchStub::CallSpy"
|
9
9
|
setup do
|
10
10
|
@unit_class = MuchStub::CallSpy
|
@@ -19,7 +19,7 @@ class MuchStub::CallSpy
|
|
19
19
|
subject{ @spy }
|
20
20
|
|
21
21
|
should "spy on method calls and return itself" do
|
22
|
-
|
22
|
+
assert_true subject.respond_to?(:get)
|
23
23
|
|
24
24
|
assert_equal [], subject.get_calls
|
25
25
|
assert_nil subject.get_last_called_with
|
@@ -25,10 +25,32 @@ module MuchStub
|
|
25
25
|
stub1 = MuchStub.(@myobj, :mymeth)
|
26
26
|
assert_kind_of MuchStub::Stub, stub1
|
27
27
|
|
28
|
-
stub2 = MuchStub.
|
28
|
+
stub2 = MuchStub.stub(@myobj, :mymeth)
|
29
29
|
assert_kind_of MuchStub::Stub, stub2
|
30
30
|
end
|
31
31
|
|
32
|
+
should "build a stub with an on_call block" do
|
33
|
+
my_meth_called_with = nil
|
34
|
+
stub1 =
|
35
|
+
MuchStub.on_call(@myobj, :mymeth) { |call|
|
36
|
+
my_meth_called_with = call
|
37
|
+
}
|
38
|
+
|
39
|
+
@myobj.mymeth
|
40
|
+
assert_kind_of MuchStub::Stub, stub1
|
41
|
+
assert_equal [], my_meth_called_with.args
|
42
|
+
|
43
|
+
my_meth_called_with = nil
|
44
|
+
stub2 =
|
45
|
+
MuchStub.stub_on_call(@myobj, :mymeth) { |call|
|
46
|
+
my_meth_called_with = call
|
47
|
+
}
|
48
|
+
|
49
|
+
@myobj.mymeth
|
50
|
+
assert_kind_of MuchStub::Stub, stub2
|
51
|
+
assert_equal [], my_meth_called_with.args
|
52
|
+
end
|
53
|
+
|
32
54
|
should "lookup stubs that have been called before" do
|
33
55
|
stub1 = MuchStub.(@myobj, :mymeth)
|
34
56
|
stub2 = MuchStub.(@myobj, :mymeth)
|
@@ -112,11 +134,13 @@ module MuchStub
|
|
112
134
|
:one,
|
113
135
|
:two,
|
114
136
|
:three,
|
137
|
+
:to_s,
|
115
138
|
ready?: true)
|
116
139
|
|
117
140
|
assert_equal spy, myobj.one
|
118
141
|
assert_equal spy, myobj.two("a")
|
119
142
|
assert_equal spy, myobj.three
|
143
|
+
assert_equal spy, myobj.to_s
|
120
144
|
|
121
145
|
assert_true myobj.one.two("b").three.ready?
|
122
146
|
|
@@ -124,6 +148,7 @@ module MuchStub
|
|
124
148
|
assert_equal 2, spy.one_call_count
|
125
149
|
assert_equal 2, spy.two_call_count
|
126
150
|
assert_equal 2, spy.three_call_count
|
151
|
+
assert_equal 1, spy.to_s_call_count
|
127
152
|
assert_equal 1, spy.ready_predicate_call_count
|
128
153
|
assert_equal ["b"], spy.two_last_called_with.args
|
129
154
|
assert_true spy.ready_predicate_called?
|
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.
|
4
|
+
version: 0.1.4
|
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: 2020-
|
12
|
+
date: 2020-09-01 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.18.
|
20
|
+
version: 2.18.2
|
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.18.
|
27
|
+
version: 2.18.2
|
28
28
|
description: Stubbing API for replacing method calls on objects in test runs.
|
29
29
|
email:
|
30
30
|
- kelly@kellyredding.com
|