spy 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc1c3954f26350a18af51b75fd2400d06600c859ba9e4675a2ddfb5b983f8a8d
4
- data.tar.gz: e535c67bdf68ba225c6cfb98f3182c53004de5452e27eb3d3476deaa16369956
3
+ metadata.gz: aa89505674a7286f055ee019988cf29debbdd5cb7e525a022864f551d20ae842
4
+ data.tar.gz: fa61fa5e78f5aea6fab75bcc471bdcc8032182d8e9c7528e9370b91022ef7ae0
5
5
  SHA512:
6
- metadata.gz: 6d6fb00c5cdc3e0f435f24ffdff53921fa8578f69a004c0b995fda335b69a694a4ea06fe61541d66fb0d40c0a52303d197ceec5b429bec102c09828da19082a4
7
- data.tar.gz: 16de7875661311fa5d6ca327ff4510751e88d743e672e937492a8d4abcbd11599f12df9667965c2e5434a5bd6b5725c12caf4c781672fac033ce6c51b5746dbe
6
+ metadata.gz: abb91b0bcef139e03b90cb59f2f1b5d9f0c52c8137beafab301f56a96d44dbcffc203bcf0c69cd7e5376bed68549e0bb9bf407b4f85e07fc91d875d401ce39d0
7
+ data.tar.gz: 58d355373220b031ba5740b0cfdc2a6d9bb94a7c39166fafae23896342eec3c3b907994f7224e80abc7f2d1ae289048d98cd56a05750569befa62c98f6330a22
@@ -0,0 +1 @@
1
+ ruby 2.7.0
@@ -1,7 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.0
4
- - 2.5.1
3
+ - '2.1'
4
+ - '2.5'
5
+ - '2.7'
5
6
  - jruby
6
7
  - ruby-head
7
8
  - rbx-3
@@ -9,3 +10,4 @@ matrix:
9
10
  allow_failures:
10
11
  - rvm: ruby-head
11
12
  - rvm: rbx-3
13
+ - rvm: jruby
@@ -1,7 +1,14 @@
1
+ ## Spy 1.0.1 (August 20th, 2020) ##
2
+
3
+ * Fix call_through w/ instance methods (@lreeves)
4
+ * Replace implicit Proc.new by explicit given block reference (@Hugo-Hache)
5
+ * Remove 2.7 warnings (@byroot)
6
+
1
7
  ## Spy 1.0.0 (October 10, 2018) ##
2
8
 
3
9
  * drop support for ruby 1.9.3, 2.0. Only support 2.1+ (@dylanahsmith)
4
10
  * support named arguments (@dylanahsmith)
11
+ * Fix readme (@ignat-z)
5
12
 
6
13
  ## Spy 0.4.3 (April 14, 2016) ##
7
14
 
data/lib/spy.rb CHANGED
@@ -34,8 +34,6 @@ module Spy
34
34
  spy = Subroutine.get(base_object, method_name)
35
35
  if spy
36
36
  spy.unhook
37
- else
38
- raise NoSpyError, "#{method_name} was not hooked on #{base_object.inspect}."
39
37
  end
40
38
  end
41
39
 
@@ -36,8 +36,8 @@ module Spy
36
36
  end
37
37
  end
38
38
 
39
- def with(*args)
40
- @with = block_given? ? Proc.new : args
39
+ def with(*args, &block)
40
+ @with = block_given? ? block : args
41
41
  self
42
42
  end
43
43
 
@@ -52,8 +52,21 @@ module Spy
52
52
  @original_method = current_method
53
53
  end
54
54
 
55
- define_method_with = singleton_method ? :define_singleton_method : :define_method
56
- base_object.send(define_method_with, method_name, override_method)
55
+ if original_method && original_method.owner == base_object
56
+ original_method.owner.send(:remove_method, method_name)
57
+ end
58
+
59
+ if singleton_method
60
+ if base_object.singleton_class.method_defined?(method_name) || base_object.singleton_class.private_method_defined?(method_name)
61
+ base_object.singleton_class.alias_method(method_name, method_name)
62
+ end
63
+ base_object.define_singleton_method(method_name, override_method)
64
+ else
65
+ if base_object.method_defined?(method_name) || base_object.private_method_defined?(method_name)
66
+ base_object.alias_method(method_name, method_name)
67
+ end
68
+ base_object.define_method(method_name, override_method)
69
+ end
57
70
 
58
71
  if [:public, :protected, :private].include? hook_opts[:visibility]
59
72
  method_owner.send(hook_opts[:visibility], method_name)
@@ -69,11 +82,10 @@ module Spy
69
82
  def unhook
70
83
  raise NeverHookedError, "'#{method_name}' method has not been hooked" unless hooked?
71
84
 
85
+ method_owner.send(:remove_method, method_name)
72
86
  if original_method && method_owner == original_method.owner
73
- method_owner.send(:define_method, method_name, original_method)
74
- method_owner.send(original_method_visibility, method_name) if original_method_visibility
75
- else
76
- method_owner.send(:remove_method, method_name)
87
+ original_method.owner.send(:define_method, method_name, original_method)
88
+ original_method.owner.send(original_method_visibility, method_name) if original_method_visibility
77
89
  end
78
90
 
79
91
  clear_method!
@@ -105,7 +117,7 @@ module Spy
105
117
  # spy.and_return(force: true) { |invalid_arity| true }
106
118
  #
107
119
  # @return [self]
108
- def and_return(value = nil)
120
+ def and_return(value = nil, &block)
109
121
  @do_not_check_plan_arity = false
110
122
 
111
123
  if block_given?
@@ -115,7 +127,7 @@ module Spy
115
127
  raise ArgumentError, "value and block conflict. Choose one"
116
128
  end
117
129
 
118
- @plan = Proc.new
130
+ @plan = block
119
131
  check_for_too_many_arguments!(@plan)
120
132
  else
121
133
  @plan = Proc.new { value }
@@ -136,13 +148,29 @@ module Spy
136
148
  # tells the spy to call the original method
137
149
  # @return [self]
138
150
  def and_call_through
139
- @plan = Proc.new do |*args, &block|
140
- if original_method
141
- original_method.call(*args, &block)
142
- else
143
- base_object.send(:method_missing, method_name, *args, &block)
151
+ if @base_object.is_a? Class
152
+ @plan = Proc.new do |object, *args, &block|
153
+ if original_method
154
+ if original_method.is_a? UnboundMethod
155
+ bound_method = original_method.bind(object)
156
+ bound_method.call(*args, &block)
157
+ else
158
+ original_method.call(*args, &block)
159
+ end
160
+ else
161
+ base_object.send(:method_missing, method_name, *args, &block)
162
+ end
163
+ end
164
+ else
165
+ @plan = Proc.new do |*args, &block|
166
+ if original_method
167
+ original_method.call(*args, &block)
168
+ else
169
+ base_object.send(:method_missing, method_name, *args, &block)
170
+ end
144
171
  end
145
172
  end
173
+
146
174
  self
147
175
  end
148
176
 
@@ -193,9 +221,9 @@ module Spy
193
221
  # check if the method was called with the exact arguments
194
222
  # @param args Arguments that should have been sent to the method
195
223
  # @return [Boolean]
196
- def has_been_called_with?(*args)
224
+ def has_been_called_with?(*args, &block)
197
225
  raise NeverHookedError unless @was_hooked
198
- match = block_given? ? Proc.new : proc { |call| call.args == args }
226
+ match = block_given? ? block : proc { |call| call.args == args }
199
227
  calls.any?(&match)
200
228
  end
201
229
 
@@ -203,10 +231,18 @@ module Spy
203
231
  # method.
204
232
  def invoke(object, args, block, called_from)
205
233
  check_arity!(args.size)
206
- result = if @plan
207
- check_for_too_many_arguments!(@plan)
208
- @plan.call(*args, &block)
209
- end
234
+
235
+ if base_object.is_a? Class
236
+ result = if @plan
237
+ check_for_too_many_arguments!(@plan)
238
+ @plan.call(object, *args, &block)
239
+ end
240
+ else
241
+ result = if @plan
242
+ check_for_too_many_arguments!(@plan)
243
+ @plan.call(*args, &block)
244
+ end
245
+ end
210
246
  ensure
211
247
  calls << CallLog.new(object, called_from, args, block, result)
212
248
  end
@@ -274,6 +310,8 @@ module Spy
274
310
  min_arity = block.arity
275
311
  min_arity = min_arity.abs - 1 if min_arity < 0
276
312
 
313
+ min_arity -=1 if base_object.is_a? Class # Instance-method procs take an extra param for receiving object
314
+
277
315
  if min_arity > arity_range.max
278
316
  raise ArgumentError.new("block requires #{min_arity} arguments while original_method require a maximum of #{arity_range.max}")
279
317
  end
@@ -1,3 +1,3 @@
1
1
  module Spy
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -19,8 +19,7 @@ Gem::Specification.new do |gem|
19
19
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
20
  gem.require_paths = ["lib"]
21
21
  gem.add_development_dependency('pry')
22
- gem.add_development_dependency('pry-nav')
23
- gem.add_development_dependency('pry-stack_explorer')
22
+ gem.add_development_dependency('pry-byebug')
24
23
  gem.add_development_dependency('minitest', '>= 4.5.0')
25
24
  gem.add_development_dependency('minitest-reporters')
26
25
  gem.add_development_dependency('rspec-core')
@@ -30,7 +30,7 @@ class TestConstantSpying < Minitest::Test
30
30
  assert_equal "hello world", Foo.hello
31
31
 
32
32
  spy = Spy.on_const(Foo, :HELLO)
33
- assert_equal nil, Foo.hello
33
+ assert_nil Foo.hello
34
34
  spy.and_return("awesome")
35
35
  assert_equal "awesome", Foo.hello
36
36
 
@@ -39,7 +39,7 @@ class TestConstantSpying < Minitest::Test
39
39
 
40
40
  assert_equal "hello world", Foo::Bar.hello
41
41
  spy = Spy.on_const(Foo, :HELLO)
42
- assert_equal nil, Foo::Bar.hello
42
+ assert_nil Foo::Bar.hello
43
43
  spy.and_return("awesome")
44
44
  assert_equal "awesome", Foo::Bar.hello
45
45
 
@@ -48,7 +48,7 @@ class TestConstantSpying < Minitest::Test
48
48
 
49
49
  assert_equal "hello world", ChildFoo.hello
50
50
  spy = Spy.on_const(Foo, :HELLO)
51
- assert_equal nil, ChildFoo.hello
51
+ assert_nil ChildFoo.hello
52
52
  spy.and_return("awesome")
53
53
  assert_equal "awesome", ChildFoo.hello
54
54
 
@@ -17,6 +17,12 @@ class TestAnyInstanceOf < Minitest::Test
17
17
  Spy::Agency.instance.dissolve!
18
18
  end
19
19
 
20
+ def test_call_through_with_instance_method
21
+ Spy.on_instance_method(Foo, :bar).and_call_through
22
+ assert_equal "foobar", Foo.new.bar
23
+ Spy.off_instance_method(Foo, :bar)
24
+ end
25
+
20
26
  def test_it_overides_all_methods
21
27
  assert_equal Foo.new.bar, "foobar"
22
28
  spy = Spy.on_instance_method(Foo, bar: "timshel")
@@ -16,12 +16,12 @@ class TestSpy < Minitest::Test
16
16
 
17
17
  assert_kind_of Spy::Subroutine, pen_write_spy
18
18
  assert_kind_of Spy::Subroutine, pen_write_hello_spy
19
- assert_equal [pen_write_spy, pen_write_hello_spy], Spy::Agency.instance.subroutines
19
+ assert_equal [pen_write_spy, pen_write_hello_spy], Spy::Agency.instance.spies
20
20
  assert pen_write_spy.has_been_called?
21
21
  assert pen_write_hello_spy.has_been_called?
22
22
  end
23
23
 
24
- def test_spy_on_hooks_and_saves_spy_with_array
24
+ def test_spy_on_hooks_and_saves_spy_with_hash
25
25
  pen_write_spy, pen_write_hello_spy = Spy.on(@pen, write: "hello", write_hello: "world")
26
26
  assert_equal "hello", @pen.write(nil)
27
27
  assert_equal "world", @pen.write_hello
@@ -68,7 +68,7 @@ module Spy
68
68
  end
69
69
  end
70
70
 
71
- BUGGY_METHODS = %i(tap pretty_print_inspect trust untrust untrusted?)
71
+ BUGGY_METHODS = %i(tap pretty_print_inspect trust untrust untrusted? debugger byebug taint untaint tainted?)
72
72
  def test_mocked_methods
73
73
  pen_methods = Pen.public_instance_methods(false) +
74
74
  Pen.protected_instance_methods(false) +
@@ -157,7 +157,7 @@ module Spy
157
157
  assert pen_write_spy.has_been_called_with?("hello")
158
158
  end
159
159
 
160
- def test_spy_hook_records_number_of_calls
160
+ def test_spy_hook_records_number_of_calls2
161
161
  args = ["hello world"]
162
162
  block = Proc.new {}
163
163
  pen_write_spy = spy_on(@pen, :write)
@@ -1,8 +1,8 @@
1
1
  require 'bundler/setup'
2
+ require 'pry'
3
+ require 'pry-byebug'
2
4
  require 'minitest/autorun'
3
5
  require "minitest/reporters"
4
- require 'pry'
5
- require 'pry-nav'
6
6
  require 'coveralls'
7
7
  Coveralls.wear!
8
8
  Minitest::Reporters.use!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Ong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-10 00:00:00.000000000 Z
11
+ date: 2020-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -25,21 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: pry-nav
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: pry-stack_explorer
28
+ name: pry-byebug
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - ">="
@@ -132,6 +118,7 @@ extensions: []
132
118
  extra_rdoc_files: []
133
119
  files:
134
120
  - ".gitignore"
121
+ - ".tool-versions"
135
122
  - ".travis.yml"
136
123
  - ".yardopts"
137
124
  - CHANGELOG.md
@@ -197,8 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
184
  - !ruby/object:Gem::Version
198
185
  version: '0'
199
186
  requirements: []
200
- rubyforge_project:
201
- rubygems_version: 2.7.6
187
+ rubygems_version: 3.1.2
202
188
  signing_key:
203
189
  specification_version: 4
204
190
  summary: A simple modern mocking library that uses the spy pattern and checks method's