spy 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,258 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestSpy < MiniTest::Unit::TestCase
4
- class Pen
5
- attr_reader :written, :color
6
-
7
- def initialize(color = :black)
8
- @color = color
9
- @written = []
10
- end
11
-
12
- def write(string)
13
- @written << string
14
- string
15
- end
16
-
17
- def write_block(&block)
18
- string = yield
19
- @written << string
20
- string
21
- end
22
-
23
- def write_hello
24
- write("hello")
25
- end
26
-
27
- def write_array(*args)
28
- args.each do |arg|
29
- write(arg)
30
- end
31
- end
32
-
33
- def greet(hello = "hello", name)
34
- write("#{hello} #{name}")
35
- end
36
-
37
- def public_method
38
- end
39
-
40
- protected
41
- def protected_method
42
- end
43
-
44
- private
45
- def private_method
46
- end
47
-
48
- class << self
49
- def another
50
- "another"
51
- end
52
- end
53
- end
54
-
55
- def setup
56
- Spy.teardown
57
- @pen = Pen.new
58
- end
59
-
60
- def test_spy_on_hook_and_saves_spy
61
- pen_write_spy = Spy.on(@pen, :write).and_return("hello")
62
- assert_equal "hello", @pen.write(nil)
63
- assert_kind_of Spy, pen_write_spy
64
- assert_equal [pen_write_spy], Spy.all
65
- assert pen_write_spy.has_been_called?
66
- end
67
-
68
- def test_spy_on_hooks_and_saves_spy_with_array
69
- pen_write_spy, pen_write_hello_spy = Spy.on(@pen, :write, :write_hello)
70
- assert_nil @pen.write(nil)
71
- assert_nil @pen.write_hello
72
-
73
- assert_kind_of Spy, pen_write_spy
74
- assert_kind_of Spy, pen_write_hello_spy
75
- assert_equal [pen_write_spy, pen_write_hello_spy], Spy.all
76
- assert pen_write_spy.has_been_called?
77
- assert pen_write_hello_spy.has_been_called?
78
- end
79
-
80
- def test_spy_on_hooks_and_saves_spy_with_array
81
- pen_write_spy, pen_write_hello_spy = Spy.on(@pen, write: "hello", write_hello: "world")
82
- assert_equal "hello", @pen.write(nil)
83
- assert_equal "world", @pen.write_hello
84
-
85
- assert_kind_of Spy, pen_write_spy
86
- assert_kind_of Spy, pen_write_hello_spy
87
- assert_equal [pen_write_spy, pen_write_hello_spy], Spy.all
88
- assert pen_write_spy.has_been_called?
89
- assert pen_write_hello_spy.has_been_called?
90
- end
91
-
92
- def test_spy_can_hook_and_record_a_method_call
93
- pen_write_spy = Spy.new(@pen, :write)
94
- pen_write_spy.hook
95
- refute pen_write_spy.has_been_called?
96
- @pen.write("hello")
97
- assert pen_write_spy.has_been_called?
98
- assert_empty @pen.written
99
- end
100
-
101
- def test_spy_can_hook_and_record_a_method_call_on_a_constant
102
- another_spy = Spy.new(Pen, :another)
103
- another_spy.hook
104
- refute another_spy.has_been_called?
105
- assert_nil Pen.another
106
- assert another_spy.has_been_called?
107
- another_spy.unhook
108
- assert_equal "another", Pen.another
109
- end
110
-
111
- def test_spy_can_unhook_a_method
112
- pen_write_spy = Spy.new(@pen, :write)
113
- pen_write_spy.hook
114
- pen_write_spy.unhook
115
- @pen.write("hello")
116
- refute pen_write_spy.has_been_called?
117
- end
118
-
119
- def test_spy_cannot_hook_a_non_existent_method
120
- spy = Spy.new(@pen, :no_method)
121
- assert_raises NameError do
122
- spy.hook
123
- end
124
- end
125
-
126
- def test_spy_can_hook_a_non_existent_method_if_param_set
127
- spy = Spy.new(@pen, :no_method).and_return(:yep)
128
- spy.hook(force: true)
129
- assert_equal :yep, @pen.no_method
130
- end
131
-
132
- def test_spy_and_return_returns_the_set_value
133
- result = "hello world"
134
-
135
- Spy.on(@pen, :write).and_return(result)
136
-
137
- assert_equal result, @pen.write(nil)
138
- end
139
-
140
- def test_spy_and_return_can_call_a_block
141
- result = "hello world"
142
-
143
- Spy.on(@pen, :write).and_return do |string|
144
- string.reverse
145
- end
146
-
147
- assert_equal result.reverse, @pen.write(result)
148
- assert_empty @pen.written
149
- end
150
-
151
- def test_spy_and_return_can_call_a_block_that_recieves_a_block
152
- string = "hello world"
153
-
154
- Spy.on(@pen, :write_block).and_return do |&block|
155
- block.call
156
- end
157
-
158
- result = @pen.write_block do
159
- string
160
- end
161
- assert_equal string, result
162
- end
163
-
164
- def test_spy_hook_records_number_of_calls
165
- pen_write_spy = Spy.on(@pen, :write)
166
- assert_equal 0, pen_write_spy.calls.size
167
- 5.times do |i|
168
- @pen.write("hello world")
169
- assert_equal i + 1, pen_write_spy.calls.size
170
- end
171
- end
172
-
173
- def test_has_been_called_with?
174
- pen_write_spy = Spy.on(@pen, :write)
175
- refute pen_write_spy.has_been_called_with?("hello")
176
- @pen.write("hello")
177
- assert pen_write_spy.has_been_called_with?("hello")
178
- @pen.write("world")
179
- assert pen_write_spy.has_been_called_with?("hello")
180
- @pen.write("hello world")
181
- assert pen_write_spy.has_been_called_with?("hello")
182
- end
183
-
184
- def test_spy_hook_records_number_of_calls
185
- args = ["hello world"]
186
- block = Proc.new {}
187
- pen_write_spy = Spy.on(@pen, :write)
188
- @pen.write(*args, &block)
189
- call_log = pen_write_spy.calls.first
190
- assert_equal @pen, call_log.object
191
- assert_equal args, call_log.args
192
- assert_equal block, call_log.block
193
- end
194
-
195
- def test_that_method_spy_keeps_arity
196
- Spy.on(@pen, :write)
197
- @pen.write("hello world")
198
- assert_raises ArgumentError do
199
- @pen.write("hello", "world")
200
- end
201
-
202
- Spy.on(@pen, :write_hello)
203
- @pen.write_hello
204
- assert_raises ArgumentError do
205
- @pen.write_hello("hello")
206
- end
207
-
208
- Spy.on(@pen, :write_array)
209
- @pen.write_hello
210
- assert_raises ArgumentError do
211
- @pen.write_hello("hello")
212
- end
213
-
214
- Spy.on(@pen, :greet)
215
- @pen.greet("bob")
216
- assert_raises ArgumentError do
217
- @pen.greet
218
- end
219
- assert_raises ArgumentError do
220
- @pen.greet("hello", "bob", "error")
221
- end
222
- end
223
-
224
- def test_hook_mimics_public_visibility
225
- Spy.on(@pen, :public_method)
226
- assert @pen.singleton_class.public_method_defined? :public_method
227
- refute @pen.singleton_class.protected_method_defined? :public_method
228
- refute @pen.singleton_class.private_method_defined? :public_method
229
- end
230
-
231
- def test_hook_mimics_protected_visibility
232
- Spy.on(@pen, :protected_method)
233
- refute @pen.singleton_class.public_method_defined? :protected_method
234
- assert @pen.singleton_class.protected_method_defined? :protected_method
235
- refute @pen.singleton_class.private_method_defined? :protected_method
236
- end
237
-
238
- def test_hook_mimics_private_visibility
239
- Spy.on(@pen, :private_method)
240
- refute @pen.singleton_class.public_method_defined? :private_method
241
- refute @pen.singleton_class.protected_method_defined? :private_method
242
- assert @pen.singleton_class.private_method_defined? :private_method
243
- end
244
-
245
- def test_spy_off_unhooks_a_method
246
- pen_write_spy = Spy.on(@pen, :write)
247
- Spy.off(@pen,:write)
248
- assert_equal "hello world", @pen.write("hello world")
249
- refute pen_write_spy.has_been_called?
250
- end
251
-
252
- def test_spy_get_can_retrieve_a_spy
253
- pen_write_spy = Spy.on(@pen, :write).and_return(:hello)
254
- assert_equal :hello, @pen.write(:world)
255
- assert_equal pen_write_spy, Spy.get(@pen, :write)
256
- assert Spy.get(@pen, :write).has_been_called?
257
- end
258
- end