much-stub 0.1.2 → 0.1.7
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/.l.yml +8 -0
- data/.rubocop.yml +3 -0
- data/.ruby-version +1 -0
- data/.t.yml +6 -0
- data/Gemfile +5 -1
- data/README.md +78 -12
- data/lib/much-stub.rb +124 -50
- data/lib/much-stub/call.rb +17 -0
- data/lib/much-stub/call_spy.rb +125 -0
- data/lib/much-stub/version.rb +3 -1
- data/much-stub.gemspec +20 -11
- data/test/helper.rb +3 -1
- data/test/support/factory.rb +2 -0
- data/test/system/much-stub_tests.rb +115 -77
- data/test/unit/call_spy_tests.rb +111 -0
- data/test/unit/call_tests.rb +70 -0
- data/test/unit/much-stub_tests.rb +220 -82
- metadata +31 -8
@@ -0,0 +1,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "assert"
|
4
|
+
require "much-stub/call_spy"
|
5
|
+
|
6
|
+
require "test/support/factory"
|
7
|
+
|
8
|
+
class MuchStub::CallSpy
|
9
|
+
class UnitTests < ::Assert::Context
|
10
|
+
desc "MuchStub::CallSpy"
|
11
|
+
setup do
|
12
|
+
@unit_class = MuchStub::CallSpy
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class InitTests < UnitTests
|
17
|
+
desc "when init"
|
18
|
+
before do
|
19
|
+
@spy = @unit_class.new
|
20
|
+
end
|
21
|
+
subject{ @spy }
|
22
|
+
|
23
|
+
should "spy on method calls and return itself" do
|
24
|
+
assert_true subject.respond_to?(:get)
|
25
|
+
|
26
|
+
assert_equal [], subject.get_calls
|
27
|
+
assert_nil subject.get_last_called_with
|
28
|
+
assert_nil subject.get_called_with
|
29
|
+
assert_equal 0, subject.get_call_count
|
30
|
+
assert_false subject.get_called?
|
31
|
+
|
32
|
+
assert_same subject, subject.get
|
33
|
+
|
34
|
+
assert_true subject.respond_to?(:get)
|
35
|
+
|
36
|
+
assert_kind_of Array, subject.get_calls
|
37
|
+
assert_kind_of MuchStub::Call, subject.get_last_called_with
|
38
|
+
assert_equal [], subject.get_last_called_with.args
|
39
|
+
assert_equal subject.get_last_called_with, subject.get_called_with
|
40
|
+
assert_equal 1, subject.get_call_count
|
41
|
+
assert_true subject.get_called?
|
42
|
+
|
43
|
+
assert_same subject, subject.set!("value1")
|
44
|
+
assert_same subject, subject.set!("value2")
|
45
|
+
|
46
|
+
assert_kind_of Array, subject.set_bang_calls
|
47
|
+
assert_kind_of MuchStub::Call, subject.set_bang_last_called_with
|
48
|
+
assert_equal ["value2"], subject.set_bang_last_called_with.args
|
49
|
+
assert_equal 2, subject.set_bang_call_count
|
50
|
+
assert_true subject.set_bang_called?
|
51
|
+
end
|
52
|
+
|
53
|
+
should "normalize method names in call query methods" do
|
54
|
+
assert_same subject, subject.set!("value1")
|
55
|
+
assert_kind_of Array, subject.set_bang_calls
|
56
|
+
assert_kind_of MuchStub::Call, subject.set_bang_last_called_with
|
57
|
+
assert_equal ["value1"], subject.set_bang_last_called_with.args
|
58
|
+
assert_equal ["value1"], subject.set_bang_called_with.args
|
59
|
+
assert_equal 1, subject.set_bang_call_count
|
60
|
+
assert_true subject.set_bang_called?
|
61
|
+
|
62
|
+
assert_same subject, subject.any?
|
63
|
+
assert_kind_of Array, subject.any_predicate_calls
|
64
|
+
assert_kind_of MuchStub::Call, subject.any_predicate_last_called_with
|
65
|
+
assert_kind_of MuchStub::Call, subject.any_predicate_called_with
|
66
|
+
assert_equal 1, subject.any_predicate_call_count
|
67
|
+
assert_true subject.any_predicate_called?
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class InitWithReturnValuesTests < UnitTests
|
72
|
+
desc "when init with return values"
|
73
|
+
setup do
|
74
|
+
@spy = @unit_class.new(any?: false)
|
75
|
+
end
|
76
|
+
subject{ @spy }
|
77
|
+
|
78
|
+
should "return the given values instead of itself if that method is "\
|
79
|
+
"called" do
|
80
|
+
assert_false subject.get.set!("value1").any?
|
81
|
+
assert_true subject.get_called?
|
82
|
+
assert_equal ["value1"], subject.set_bang_called_with.args
|
83
|
+
assert_true subject.any_predicate_called?
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class InitWithReturnValueProcsTests < UnitTests
|
88
|
+
desc "when init with return value procs"
|
89
|
+
setup do
|
90
|
+
@result = Factory.boolean
|
91
|
+
@spy = @unit_class.new(any?: ->(_call){ @result })
|
92
|
+
end
|
93
|
+
subject{ @spy }
|
94
|
+
|
95
|
+
should "return the value of calling the procs instead of itself" do
|
96
|
+
assert_equal @result, subject.get.set!("value1").any?
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class InitWithNilReturnValuesTests < UnitTests
|
101
|
+
desc "when init with nil return values"
|
102
|
+
setup do
|
103
|
+
@spy = @unit_class.new(result: nil)
|
104
|
+
end
|
105
|
+
subject{ @spy }
|
106
|
+
|
107
|
+
should "return nil" do
|
108
|
+
assert_equal "nil", subject.get.set!("value1").result.inspect
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "assert"
|
4
|
+
require "much-stub/call"
|
5
|
+
|
6
|
+
require "test/support/factory"
|
7
|
+
|
8
|
+
class MuchStub::Call
|
9
|
+
class UnitTests < Assert::Context
|
10
|
+
desc "MuchStub::Call"
|
11
|
+
setup do
|
12
|
+
@unit_class = MuchStub::Call
|
13
|
+
|
14
|
+
@pargs = [Factory.string, Factory.integer]
|
15
|
+
@kargs = {
|
16
|
+
one: 1,
|
17
|
+
two: 2,
|
18
|
+
}
|
19
|
+
@block = ->{}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class InitWithNoArgsTests < UnitTests
|
24
|
+
desc "when init with no args"
|
25
|
+
subject{ @unit_class.new }
|
26
|
+
|
27
|
+
should "know its attrs" do
|
28
|
+
assert_nil subject.pargs
|
29
|
+
assert_nil subject.kargs
|
30
|
+
assert_equal [], subject.args
|
31
|
+
assert_nil subject.block
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class InitWithOnlyPositionalArgsTests < UnitTests
|
36
|
+
desc "when init with only positional args"
|
37
|
+
subject{ @unit_class.new(*@pargs) }
|
38
|
+
|
39
|
+
should "know its attrs" do
|
40
|
+
assert_equal @pargs, subject.pargs
|
41
|
+
assert_nil subject.kargs
|
42
|
+
assert_equal [*@pargs], subject.args
|
43
|
+
assert_nil subject.block
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class InitWithOnlyKeywordArgsTests < UnitTests
|
48
|
+
desc "when init with only keyword args"
|
49
|
+
subject{ @unit_class.new(**@kargs) }
|
50
|
+
|
51
|
+
should "know its attrs" do
|
52
|
+
assert_nil subject.pargs
|
53
|
+
assert_equal @kargs, subject.kargs
|
54
|
+
assert_equal [@kargs], subject.args
|
55
|
+
assert_nil subject.block
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class InitWithBothPositionalAndKeywordArgsTests < UnitTests
|
60
|
+
desc "when init with only keyword args"
|
61
|
+
subject{ @unit_class.new(*@pargs, **@kargs, &@block) }
|
62
|
+
|
63
|
+
should "know its attrs" do
|
64
|
+
assert_equal @pargs, subject.pargs
|
65
|
+
assert_equal @kargs, subject.kargs
|
66
|
+
assert_equal [*@pargs, @kargs], subject.args
|
67
|
+
assert_equal @block, subject.block
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "assert"
|
2
4
|
require "much-stub"
|
3
5
|
|
@@ -15,30 +17,57 @@ module MuchStub
|
|
15
17
|
@stub_value = Factory.string
|
16
18
|
|
17
19
|
@myclass = Class.new do
|
18
|
-
def initialize(value)
|
19
|
-
|
20
|
+
def initialize(value)
|
21
|
+
@value = value
|
22
|
+
end
|
23
|
+
|
24
|
+
def mymeth
|
25
|
+
@value
|
26
|
+
end
|
20
27
|
end
|
21
28
|
@myobj = @myclass.new(@orig_value)
|
22
29
|
end
|
23
30
|
|
24
31
|
should "build a stub" do
|
25
|
-
stub1 = MuchStub.(@myobj, :mymeth)
|
32
|
+
stub1 = MuchStub.call(@myobj, :mymeth)
|
26
33
|
assert_kind_of MuchStub::Stub, stub1
|
27
34
|
|
28
|
-
stub2 = MuchStub.
|
35
|
+
stub2 = MuchStub.stub(@myobj, :mymeth)
|
36
|
+
assert_kind_of MuchStub::Stub, stub2
|
37
|
+
end
|
38
|
+
|
39
|
+
should "build a stub with an on_call block" do
|
40
|
+
my_meth_called_with = nil
|
41
|
+
stub1 =
|
42
|
+
MuchStub.on_call(@myobj, :mymeth) do |call|
|
43
|
+
my_meth_called_with = call
|
44
|
+
end
|
45
|
+
|
46
|
+
@myobj.mymeth
|
47
|
+
assert_kind_of MuchStub::Stub, stub1
|
48
|
+
assert_equal [], my_meth_called_with.args
|
49
|
+
|
50
|
+
my_meth_called_with = nil
|
51
|
+
stub2 =
|
52
|
+
MuchStub.stub_on_call(@myobj, :mymeth) do |call|
|
53
|
+
my_meth_called_with = call
|
54
|
+
end
|
55
|
+
|
56
|
+
@myobj.mymeth
|
29
57
|
assert_kind_of MuchStub::Stub, stub2
|
58
|
+
assert_equal [], my_meth_called_with.args
|
30
59
|
end
|
31
60
|
|
32
61
|
should "lookup stubs that have been called before" do
|
33
|
-
stub1 = MuchStub.(@myobj, :mymeth)
|
34
|
-
stub2 = MuchStub.(@myobj, :mymeth)
|
62
|
+
stub1 = MuchStub.call(@myobj, :mymeth)
|
63
|
+
stub2 = MuchStub.call(@myobj, :mymeth)
|
35
64
|
assert_same stub1, stub2
|
36
65
|
end
|
37
66
|
|
38
67
|
should "set the stub's do block if given a block" do
|
39
|
-
MuchStub.(@myobj, :mymeth)
|
68
|
+
MuchStub.call(@myobj, :mymeth)
|
40
69
|
assert_raises(MuchStub::NotStubbedError){ @myobj.mymeth }
|
41
|
-
MuchStub.(@myobj, :mymeth){ @stub_value }
|
70
|
+
MuchStub.call(@myobj, :mymeth){ @stub_value }
|
42
71
|
assert_equal @stub_value, @myobj.mymeth
|
43
72
|
end
|
44
73
|
|
@@ -48,7 +77,7 @@ module MuchStub
|
|
48
77
|
assert_equal @orig_value, @myobj.mymeth
|
49
78
|
|
50
79
|
assert_equal @orig_value, @myobj.mymeth
|
51
|
-
MuchStub.(@myobj, :mymeth){ @stub_value }
|
80
|
+
MuchStub.call(@myobj, :mymeth){ @stub_value }
|
52
81
|
assert_equal @stub_value, @myobj.mymeth
|
53
82
|
MuchStub.unstub(@myobj, :mymeth)
|
54
83
|
assert_equal @orig_value, @myobj.mymeth
|
@@ -57,7 +86,7 @@ module MuchStub
|
|
57
86
|
should "know and teardown all stubs" do
|
58
87
|
assert_equal @orig_value, @myobj.mymeth
|
59
88
|
|
60
|
-
MuchStub.(@myobj, :mymeth){ @stub_value }
|
89
|
+
MuchStub.call(@myobj, :mymeth){ @stub_value }
|
61
90
|
assert_equal @stub_value, @myobj.mymeth
|
62
91
|
assert_equal 1, MuchStub.stubs.size
|
63
92
|
|
@@ -67,11 +96,14 @@ module MuchStub
|
|
67
96
|
end
|
68
97
|
|
69
98
|
should "be able to call a stub's original method" do
|
70
|
-
err =
|
99
|
+
err =
|
100
|
+
assert_raises(NotStubbedError) do
|
101
|
+
MuchStub.stub_send(@myobj, :mymeth)
|
102
|
+
end
|
71
103
|
assert_includes "not stubbed.", err.message
|
72
104
|
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
73
105
|
|
74
|
-
MuchStub.(@myobj, :mymeth){ @stub_value }
|
106
|
+
MuchStub.call(@myobj, :mymeth){ @stub_value }
|
75
107
|
|
76
108
|
assert_equal @stub_value, @myobj.mymeth
|
77
109
|
assert_equal @orig_value, MuchStub.stub_send(@myobj, :mymeth)
|
@@ -79,24 +111,95 @@ module MuchStub
|
|
79
111
|
|
80
112
|
should "be able to add a stub tap" do
|
81
113
|
my_meth_called_with = nil
|
82
|
-
MuchStub.tap(@myobj, :mymeth)
|
114
|
+
MuchStub.tap(@myobj, :mymeth) do |_value, *args|
|
83
115
|
my_meth_called_with = args
|
84
|
-
|
116
|
+
end
|
85
117
|
|
86
118
|
assert_equal @orig_value, @myobj.mymeth
|
87
119
|
assert_equal [], my_meth_called_with
|
88
120
|
end
|
121
|
+
|
122
|
+
should "be able to add a stub tap with an on_call block" do
|
123
|
+
my_meth_called_with = nil
|
124
|
+
MuchStub.tap_on_call(@myobj, :mymeth) do |_value, call|
|
125
|
+
my_meth_called_with = call
|
126
|
+
end
|
127
|
+
|
128
|
+
assert_equal @orig_value, @myobj.mymeth
|
129
|
+
assert_equal [], my_meth_called_with.args
|
130
|
+
end
|
131
|
+
|
132
|
+
should "be able to add a stubbed spy" do
|
133
|
+
myclass = Class.new do
|
134
|
+
def one
|
135
|
+
self
|
136
|
+
end
|
137
|
+
|
138
|
+
def two(_val)
|
139
|
+
self
|
140
|
+
end
|
141
|
+
|
142
|
+
def three
|
143
|
+
self
|
144
|
+
end
|
145
|
+
|
146
|
+
def ready?
|
147
|
+
false
|
148
|
+
end
|
149
|
+
end
|
150
|
+
myobj = myclass.new
|
151
|
+
|
152
|
+
spy =
|
153
|
+
MuchStub.spy(
|
154
|
+
myobj,
|
155
|
+
:one,
|
156
|
+
:two,
|
157
|
+
:three,
|
158
|
+
:to_s,
|
159
|
+
ready?: true,
|
160
|
+
)
|
161
|
+
|
162
|
+
assert_equal spy, myobj.one
|
163
|
+
assert_equal spy, myobj.two("a")
|
164
|
+
assert_equal spy, myobj.three
|
165
|
+
assert_equal spy, myobj.to_s
|
166
|
+
|
167
|
+
assert_true myobj.one.two("b").three.ready?
|
168
|
+
|
169
|
+
assert_kind_of MuchStub::CallSpy, spy
|
170
|
+
assert_equal 2, spy.one_call_count
|
171
|
+
assert_equal 2, spy.two_call_count
|
172
|
+
assert_equal 2, spy.three_call_count
|
173
|
+
assert_equal 1, spy.to_s_call_count
|
174
|
+
assert_equal 1, spy.ready_predicate_call_count
|
175
|
+
assert_equal ["b"], spy.two_last_called_with.args
|
176
|
+
assert_true spy.ready_predicate_called?
|
177
|
+
end
|
89
178
|
end
|
90
179
|
|
91
180
|
class StubTests < UnitTests
|
92
181
|
desc "Stub"
|
93
182
|
setup do
|
94
183
|
@myclass = Class.new do
|
95
|
-
def mymeth
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
def
|
184
|
+
def mymeth
|
185
|
+
"meth"
|
186
|
+
end
|
187
|
+
|
188
|
+
def myval(val)
|
189
|
+
val
|
190
|
+
end
|
191
|
+
|
192
|
+
def myargs(*args)
|
193
|
+
args
|
194
|
+
end
|
195
|
+
|
196
|
+
def myvalargs(val1, val2, *args)
|
197
|
+
[val1, val2, args]
|
198
|
+
end
|
199
|
+
|
200
|
+
def myblk(&block)
|
201
|
+
block.call
|
202
|
+
end
|
100
203
|
end
|
101
204
|
@myobj = @myclass.new
|
102
205
|
|
@@ -112,7 +215,10 @@ module MuchStub
|
|
112
215
|
should "generate a key given an object and method name" do
|
113
216
|
obj = @myobj
|
114
217
|
meth = :mymeth
|
115
|
-
assert_equal
|
218
|
+
assert_equal(
|
219
|
+
"--#{obj.object_id}--#{meth}--",
|
220
|
+
MuchStub::Stub.key(obj, meth),
|
221
|
+
)
|
116
222
|
end
|
117
223
|
|
118
224
|
should "know its names" do
|
@@ -134,77 +240,85 @@ module MuchStub
|
|
134
240
|
@myobj.mymeth
|
135
241
|
end
|
136
242
|
|
243
|
+
MuchStub::Stub.new(@myobj, :mymeth){ "mymeth" }
|
137
244
|
assert_nothing_raised do
|
138
|
-
|
245
|
+
@myobj.mymeth
|
139
246
|
end
|
140
247
|
end
|
141
248
|
|
142
|
-
should "complain
|
143
|
-
|
144
|
-
|
249
|
+
should "complain when called if no lookup block was given" do
|
250
|
+
MuchStub::Stub.new(@myobj, :myval).with(1)
|
251
|
+
|
252
|
+
err = assert_raises(MuchStub::StubError){ @myobj.myval(1) }
|
253
|
+
assert_includes "stubbed with no block.", err.message
|
145
254
|
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
146
255
|
end
|
147
256
|
|
148
|
-
should "complain if
|
149
|
-
|
257
|
+
should "complain if stubbing a method that the object doesn't respond to" do
|
258
|
+
err =
|
259
|
+
assert_raises(MuchStub::StubError) do
|
260
|
+
MuchStub::Stub.new(@myobj, :some_other_meth)
|
261
|
+
end
|
262
|
+
assert_includes "does not respond to", err.message
|
263
|
+
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
150
264
|
end
|
151
265
|
|
152
266
|
should "complain if stubbed and called with mismatched arity" do
|
153
267
|
MuchStub::Stub.new(@myobj, :myval){ "myval" }
|
154
268
|
err = assert_raises(MuchStub::StubArityError){ @myobj.myval }
|
155
|
-
assert_includes "arity mismatch on",
|
269
|
+
assert_includes "arity mismatch on", err.message
|
156
270
|
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
157
271
|
|
158
|
-
assert_nothing_raised
|
159
|
-
assert_raises(MuchStub::StubArityError){ @myobj.myval(1,2) }
|
272
|
+
assert_nothing_raised{ @myobj.myval(1) }
|
273
|
+
assert_raises(MuchStub::StubArityError){ @myobj.myval(1, 2) }
|
160
274
|
|
161
275
|
MuchStub::Stub.new(@myobj, :myargs){ "myargs" }
|
162
|
-
assert_nothing_raised
|
163
|
-
assert_nothing_raised
|
164
|
-
assert_nothing_raised
|
276
|
+
assert_nothing_raised{ @myobj.myargs }
|
277
|
+
assert_nothing_raised{ @myobj.myargs(1) }
|
278
|
+
assert_nothing_raised{ @myobj.myargs(1, 2) }
|
165
279
|
|
166
280
|
MuchStub::Stub.new(@myobj, :myvalargs){ "myvalargs" }
|
167
281
|
assert_raises(MuchStub::StubArityError){ @myobj.myvalargs }
|
168
282
|
assert_raises(MuchStub::StubArityError){ @myobj.myvalargs(1) }
|
169
|
-
assert_nothing_raised
|
170
|
-
assert_nothing_raised
|
283
|
+
assert_nothing_raised{ @myobj.myvalargs(1, 2) }
|
284
|
+
assert_nothing_raised{ @myobj.myvalargs(1, 2, 3) }
|
171
285
|
end
|
172
286
|
|
173
287
|
should "complain if stubbed with mismatched arity" do
|
174
288
|
err = assert_raises(MuchStub::StubArityError) do
|
175
|
-
MuchStub::Stub.new(@myobj, :myval).with
|
289
|
+
MuchStub::Stub.new(@myobj, :myval).with{ "myval" }
|
176
290
|
end
|
177
|
-
assert_includes "arity mismatch on",
|
291
|
+
assert_includes "arity mismatch on", err.message
|
178
292
|
assert_includes "test/unit/much-stub_tests.rb", err.backtrace.first
|
179
293
|
|
180
294
|
assert_raises(MuchStub::StubArityError) do
|
181
|
-
MuchStub::Stub.new(@myobj, :myval).with(1,2){ "myval" }
|
295
|
+
MuchStub::Stub.new(@myobj, :myval).with(1, 2){ "myval" }
|
182
296
|
end
|
183
297
|
assert_nothing_raised do
|
184
298
|
MuchStub::Stub.new(@myobj, :myval).with(1){ "myval" }
|
185
299
|
end
|
186
300
|
|
187
301
|
assert_nothing_raised do
|
188
|
-
MuchStub::Stub.new(@myobj, :myargs).with
|
302
|
+
MuchStub::Stub.new(@myobj, :myargs).with{ "myargs" }
|
189
303
|
end
|
190
304
|
assert_nothing_raised do
|
191
|
-
MuchStub::Stub.new(@myobj, :myargs).with(1,2){ "myargs" }
|
305
|
+
MuchStub::Stub.new(@myobj, :myargs).with(1, 2){ "myargs" }
|
192
306
|
end
|
193
307
|
assert_nothing_raised do
|
194
308
|
MuchStub::Stub.new(@myobj, :myargs).with(1){ "myargs" }
|
195
309
|
end
|
196
310
|
|
197
311
|
assert_raises(MuchStub::StubArityError) do
|
198
|
-
MuchStub::Stub.new(@myobj, :myvalargs).with
|
312
|
+
MuchStub::Stub.new(@myobj, :myvalargs).with{ "myvalargs" }
|
199
313
|
end
|
200
314
|
assert_raises(MuchStub::StubArityError) do
|
201
315
|
MuchStub::Stub.new(@myobj, :myvalargs).with(1){ "myvalargs" }
|
202
316
|
end
|
203
317
|
assert_nothing_raised do
|
204
|
-
MuchStub::Stub.new(@myobj, :myvalargs).with(1,2){ "myvalargs" }
|
318
|
+
MuchStub::Stub.new(@myobj, :myvalargs).with(1, 2){ "myvalargs" }
|
205
319
|
end
|
206
320
|
assert_nothing_raised do
|
207
|
-
MuchStub::Stub.new(@myobj, :myvalargs).with(1,2,3){ "myvalargs" }
|
321
|
+
MuchStub::Stub.new(@myobj, :myvalargs).with(1, 2, 3){ "myvalargs" }
|
208
322
|
end
|
209
323
|
end
|
210
324
|
|
@@ -218,7 +332,7 @@ module MuchStub
|
|
218
332
|
|
219
333
|
should "stub methods with required arg" do
|
220
334
|
assert_equal 1, @myobj.myval(1)
|
221
|
-
stub = MuchStub::Stub.new(@myobj, :myval
|
335
|
+
stub = MuchStub::Stub.new(@myobj, :myval, &:to_s)
|
222
336
|
assert_equal "1", @myobj.myval(1)
|
223
337
|
assert_equal "2", @myobj.myval(2)
|
224
338
|
stub.with(2){ "two" }
|
@@ -226,21 +340,36 @@ module MuchStub
|
|
226
340
|
end
|
227
341
|
|
228
342
|
should "stub methods with variable args" do
|
229
|
-
assert_equal [1,2], @myobj.myargs(1,2)
|
343
|
+
assert_equal [1, 2], @myobj.myargs(1, 2)
|
344
|
+
|
230
345
|
stub = MuchStub::Stub.new(@myobj, :myargs){ |*args| args.join(",") }
|
231
|
-
assert_equal "1,2", @myobj.myargs(1,2)
|
232
|
-
|
233
|
-
|
234
|
-
|
346
|
+
assert_equal "1,2,3", @myobj.myargs(1, 2, 3)
|
347
|
+
stub.with(3, 4, 5){ |*args| args.join(":") }
|
348
|
+
assert_equal "3:4:5", @myobj.myargs(3, 4, 5)
|
349
|
+
|
350
|
+
stub =
|
351
|
+
MuchStub::Stub.new(@myobj, :myargs).on_call do |call|
|
352
|
+
call.args.join(",")
|
353
|
+
end
|
354
|
+
assert_equal "1,2,3", @myobj.myargs(1, 2, 3)
|
355
|
+
stub.with(3, 4, 5).on_call{ |call| call.args.join(":") }
|
356
|
+
assert_equal "3:4:5", @myobj.myargs(3, 4, 5)
|
235
357
|
end
|
236
358
|
|
237
359
|
should "stub methods with required args and variable args" do
|
238
|
-
assert_equal [1,2, [3]], @myobj.myvalargs(1,2,3)
|
360
|
+
assert_equal [1, 2, [3]], @myobj.myvalargs(1, 2, 3)
|
239
361
|
stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(",") }
|
240
|
-
assert_equal "1,2,3", @myobj.myvalargs(1,2,3)
|
241
|
-
|
242
|
-
|
243
|
-
|
362
|
+
assert_equal "1,2,3", @myobj.myvalargs(1, 2, 3)
|
363
|
+
stub.with(3, 4, 5){ |*args| args.join(":") }
|
364
|
+
assert_equal "3:4:5", @myobj.myvalargs(3, 4, 5)
|
365
|
+
|
366
|
+
stub =
|
367
|
+
MuchStub::Stub.new(@myobj, :myvalargs).on_call do |call|
|
368
|
+
call.args.join(",")
|
369
|
+
end
|
370
|
+
assert_equal "1,2,3", @myobj.myvalargs(1, 2, 3)
|
371
|
+
stub.with(3, 4, 5).on_call{ |call| call.args.join(":") }
|
372
|
+
assert_equal "3:4:5", @myobj.myvalargs(3, 4, 5)
|
244
373
|
end
|
245
374
|
|
246
375
|
should "stub methods that yield blocks" do
|
@@ -260,21 +389,27 @@ module MuchStub
|
|
260
389
|
def initialize(delegateclass)
|
261
390
|
@delegate = delegateclass.new
|
262
391
|
end
|
392
|
+
|
263
393
|
def respond_to?(meth)
|
264
394
|
@delegate.respond_to?(meth) || super
|
265
395
|
end
|
396
|
+
|
266
397
|
def method_missing(meth, *args, &block)
|
267
398
|
respond_to?(meth) ? @delegate.send(meth, *args, &block) : super
|
268
399
|
end
|
400
|
+
|
401
|
+
def respond_to_missing?(meth, _)
|
402
|
+
respond_to?(meth) || super
|
403
|
+
end
|
269
404
|
end
|
270
405
|
mydelegator = mydelegatorclass.new(@myclass)
|
271
406
|
|
272
|
-
assert_equal [1,2,[3]], mydelegator.myvalargs(1,2,3)
|
407
|
+
assert_equal [1, 2, [3]], mydelegator.myvalargs(1, 2, 3)
|
273
408
|
stub = MuchStub::Stub.new(mydelegator, :myvalargs){ |*args| args.inspect }
|
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)
|
409
|
+
assert_equal "[1, 2, 3]", mydelegator.myvalargs(1, 2, 3)
|
410
|
+
assert_equal "[4, 5, 6]", mydelegator.myvalargs(4, 5, 6)
|
411
|
+
stub.with(4, 5, 6){ "four-five-six" }
|
412
|
+
assert_equal "four-five-six", mydelegator.myvalargs(4, 5, 6)
|
278
413
|
end
|
279
414
|
|
280
415
|
should "call to the original method" do
|
@@ -286,7 +421,7 @@ module MuchStub
|
|
286
421
|
assert_equal "meth", stub.call_method([])
|
287
422
|
|
288
423
|
# static args
|
289
|
-
stub = MuchStub::Stub.new(@myobj, :myval
|
424
|
+
stub = MuchStub::Stub.new(@myobj, :myval, &:to_s)
|
290
425
|
assert_equal "1", stub.call([1])
|
291
426
|
assert_equal 1, stub.call_method([1])
|
292
427
|
assert_equal "2", stub.call([2])
|
@@ -297,23 +432,23 @@ module MuchStub
|
|
297
432
|
|
298
433
|
# dynamic args
|
299
434
|
stub = MuchStub::Stub.new(@myobj, :myargs){ |*args| args.join(",") }
|
300
|
-
assert_equal "1,2",
|
301
|
-
assert_equal [1,2],
|
302
|
-
assert_equal "3,4,5", stub.call([3,4,5])
|
303
|
-
assert_equal [3,4,5], stub.call_method([3,4,5])
|
304
|
-
stub.with(3,4,5){ "three-four-five" }
|
305
|
-
assert_equal "three-four-five", stub.call([3,4,5])
|
306
|
-
assert_equal [3,4,5], stub.call_method([3,4,5])
|
435
|
+
assert_equal "1,2", stub.call([1, 2])
|
436
|
+
assert_equal [1, 2], stub.call_method([1, 2])
|
437
|
+
assert_equal "3,4,5", stub.call([3, 4, 5])
|
438
|
+
assert_equal [3, 4, 5], stub.call_method([3, 4, 5])
|
439
|
+
stub.with(3, 4, 5){ "three-four-five" }
|
440
|
+
assert_equal "three-four-five", stub.call([3, 4, 5])
|
441
|
+
assert_equal [3, 4, 5], stub.call_method([3, 4, 5])
|
307
442
|
|
308
443
|
# mixed static/dynamic args
|
309
444
|
stub = MuchStub::Stub.new(@myobj, :myvalargs){ |*args| args.join(",") }
|
310
|
-
assert_equal "1,2,3",
|
311
|
-
assert_equal [1,2, [3]], stub.call_method([1,2,3])
|
312
|
-
assert_equal "3,4,5",
|
313
|
-
assert_equal [3,4,[5]],
|
314
|
-
stub.with(3,4,5){ "three-four-five" }
|
315
|
-
assert_equal "three-four-five", stub.call([3,4,5])
|
316
|
-
assert_equal [3,4,[5]],
|
445
|
+
assert_equal "1,2,3", stub.call([1, 2, 3])
|
446
|
+
assert_equal [1, 2, [3]], stub.call_method([1, 2, 3])
|
447
|
+
assert_equal "3,4,5", stub.call([3, 4, 5])
|
448
|
+
assert_equal [3, 4, [5]], stub.call_method([3, 4, 5])
|
449
|
+
stub.with(3, 4, 5){ "three-four-five" }
|
450
|
+
assert_equal "three-four-five", stub.call([3, 4, 5])
|
451
|
+
assert_equal [3, 4, [5]], stub.call_method([3, 4, 5])
|
317
452
|
|
318
453
|
# blocks
|
319
454
|
blkcalled = false
|
@@ -325,7 +460,7 @@ module MuchStub
|
|
325
460
|
assert_equal true, blkcalled
|
326
461
|
end
|
327
462
|
|
328
|
-
should "store and remove itself
|
463
|
+
should "store and remove itself" do
|
329
464
|
assert_equal subject, MuchStub.instance_variable_get(subject.ivar_name)
|
330
465
|
subject.teardown
|
331
466
|
assert_nil MuchStub.instance_variable_get(subject.ivar_name)
|
@@ -333,16 +468,17 @@ module MuchStub
|
|
333
468
|
|
334
469
|
should "be removable" do
|
335
470
|
assert_equal 1, @myobj.myval(1)
|
336
|
-
stub = MuchStub::Stub.new(@myobj, :myval
|
471
|
+
stub = MuchStub::Stub.new(@myobj, :myval, &:to_s)
|
337
472
|
assert_equal "1", @myobj.myval(1)
|
338
473
|
stub.teardown
|
339
474
|
assert_equal 1, @myobj.myval(1)
|
340
475
|
end
|
341
476
|
|
342
477
|
should "have a readable inspect" do
|
343
|
-
|
344
|
-
|
345
|
-
|
478
|
+
exp =
|
479
|
+
"#<#{subject.class}:#{format("0x0%x", (subject.object_id << 1))}" \
|
480
|
+
" @method_name=#{subject.method_name.inspect}>"
|
481
|
+
assert_equal exp, subject.inspect
|
346
482
|
end
|
347
483
|
end
|
348
484
|
|
@@ -382,7 +518,8 @@ module MuchStub
|
|
382
518
|
assert_equal "child", @child_class.new(1, 2)
|
383
519
|
end
|
384
520
|
|
385
|
-
should "not raise any errors when tearing down the parent before the
|
521
|
+
should "not raise any errors when tearing down the parent before the "\
|
522
|
+
"child" do
|
386
523
|
assert_nothing_raised do
|
387
524
|
@parent_stub.teardown
|
388
525
|
@child_stub.teardown
|
@@ -403,9 +540,9 @@ module MuchStub
|
|
403
540
|
class ParameterListTests < UnitTests
|
404
541
|
desc "ParameterList"
|
405
542
|
setup do
|
406
|
-
many_args = (0..ParameterList::LETTERS.size).map
|
543
|
+
many_args = (0..ParameterList::LETTERS.size).map{
|
407
544
|
Assert::Factory.string
|
408
|
-
|
545
|
+
}.join(", ")
|
409
546
|
@object = Class.new
|
410
547
|
# use `class_eval` with string to easily define methods with many params
|
411
548
|
@object.class_eval <<-methods
|
@@ -430,7 +567,8 @@ module MuchStub
|
|
430
567
|
assert_equal expected, subject.new(@object, "manyargs")
|
431
568
|
end
|
432
569
|
|
433
|
-
should "build a parameter list for a method that takes a minimum number
|
570
|
+
should "build a parameter list for a method that takes a minimum number "\
|
571
|
+
"of args" do
|
434
572
|
expected = "#{ParameterList::LETTERS.join(", ")}, aa, *args, &block"
|
435
573
|
assert_equal expected, subject.new(@object, "minargs")
|
436
574
|
end
|