minitest-stub_any_instance 1.0.2 → 1.0.3

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
- SHA1:
3
- metadata.gz: 3d419f072287bcd82581e413aad81d8bd2fce844
4
- data.tar.gz: cbc5e034772668f9e8d300177ed77cc13da81241
2
+ SHA256:
3
+ metadata.gz: 074f799ebd6b86158091e84929bff1fe48868286d866b8df1a1c40663647ad6b
4
+ data.tar.gz: c203683265aea71324669593243135c96ae4e43a8eee1bca338944868bf28349
5
5
  SHA512:
6
- metadata.gz: b8919405ef56b01544131016a2ea10dccf4eb788265405851e7df1870dfb7096493060d54ca74644bd1580c589e907b82fb285307642475e0d89ad6010a62c5b
7
- data.tar.gz: af7336b7d9c596af39bc2a22f8c80cd323a7e05fbd6c5165e023c7bfba89e3588af54753061ed8c47eb96451aa77cd4062bff0f2ab7afbd350a3842789e6174b
6
+ metadata.gz: bd432d6410b0adb42c3a3a4e74c595002698ac427ba2d38abe9ff43677f550a5b31611e458b9fe42ea0572dc733aa4bc70ade9bd194a1eb47cbb8c26ee43644c
7
+ data.tar.gz: 39c7a9f1a2f59ad09166882b3d1ec63d640d12403d8cf2173f99a924dff23b44c3ef0661ecd27eed41c5b4c3ae1df446207b8c6cda078fabbf4d7e6fb8ff179a
@@ -1,16 +1,26 @@
1
1
  class BasicObject
2
- def self.stub_any_instance name, val_or_callable, &block
2
+ def self.stub_any_instance(name, val_or_callable = nil, &block)
3
3
  new_name = "__minitest_any_instance_stub__#{name}"
4
4
 
5
5
  owns_method = instance_method(name).owner == self
6
6
  class_eval do
7
7
  alias_method new_name, name if owns_method
8
8
 
9
- define_method(name) do |*args|
10
- if val_or_callable.respond_to? :call then
11
- instance_exec(*args, &val_or_callable)
12
- else
13
- val_or_callable
9
+ if ::RUBY_VERSION >= "3"
10
+ define_method(name) do |*args, **kwargs|
11
+ if val_or_callable.respond_to?(:call)
12
+ instance_exec(*args, **kwargs, &val_or_callable)
13
+ else
14
+ val_or_callable
15
+ end
16
+ end
17
+ else
18
+ define_method(name) do |*args|
19
+ if val_or_callable.respond_to?(:call)
20
+ instance_exec(*args, &val_or_callable)
21
+ else
22
+ val_or_callable
23
+ end
14
24
  end
15
25
  end
16
26
  end
@@ -19,7 +19,14 @@ module StubAnyInstanceTest
19
19
  end
20
20
  end
21
21
 
22
- class TestStubAnyInstance < MiniTest::Unit::TestCase
22
+ if Gem.loaded_specs["minitest"].version < Gem::Version.create('5.0')
23
+ MinitestTestClass = MiniTest::Unit::TestCase
24
+ else
25
+ MinitestTestClass = Minitest::Test
26
+ end
27
+
28
+
29
+ class TestStubAnyInstance < MinitestTestClass
23
30
  def setup
24
31
  $stderr = StringIO.new
25
32
  end
@@ -33,6 +40,15 @@ class TestStubAnyInstance < MiniTest::Unit::TestCase
33
40
  assert got_here
34
41
  end
35
42
 
43
+ def test_stubs_a_method_without_passing_any_value
44
+ got_here = false
45
+ String.stub_any_instance(:length) do
46
+ assert_nil "hello".length
47
+ got_here = true
48
+ end
49
+ assert got_here
50
+ end
51
+
36
52
  def test_restores_the_original_method_after_the_block
37
53
  string = "hello world"
38
54
  got_here = false
@@ -67,4 +83,29 @@ class TestStubAnyInstance < MiniTest::Unit::TestCase
67
83
  def test_stubbed_method_does_not_get_restored_to_non_owning_class
68
84
  assert_equal 'bar', ::StubAnyInstanceTest::ChildClass.new.foo
69
85
  end
86
+
87
+ def test_stubbing_method_with_arguments
88
+ original_string = "do not change me"
89
+ changed_string = "i changed"
90
+ gsubbed = original_string.gsub(original_string, changed_string)
91
+ assert_equal gsubbed, changed_string
92
+
93
+ String.stub_any_instance(:gsub, original_string) do
94
+ gsubbed = original_string.gsub(original_string, changed_string)
95
+ assert_equal gsubbed, original_string
96
+ assert gsubbed != changed_string
97
+ end
98
+ end
99
+
100
+ def test_stubbing_method_with_keyword_arguments
101
+ original_string = "do not change me"
102
+ assert_equal original_string.length, 16
103
+
104
+ use_return_arg = ->(return_arg: 0) { return_arg }
105
+ String.stub_any_instance(:length, use_return_arg) do
106
+ result = original_string.length(return_arg: :foo)
107
+ assert_equal result, :foo
108
+ end
109
+ end
110
+
70
111
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-stub_any_instance
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sammy Larbi
8
8
  - Vasiliy Ermolovich
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-01-19 00:00:00.000000000 Z
12
+ date: 2021-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -66,7 +66,7 @@ homepage: https://github.com/codeodor/minitest-stub_any_instance
66
66
  licenses:
67
67
  - MIT
68
68
  metadata: {}
69
- post_install_message:
69
+ post_install_message:
70
70
  rdoc_options: []
71
71
  require_paths:
72
72
  - lib
@@ -74,16 +74,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - ">="
76
76
  - !ruby/object:Gem::Version
77
- version: '0'
77
+ version: '2.0'
78
78
  required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  requirements: []
84
- rubyforge_project:
85
- rubygems_version: 2.6.14
86
- signing_key:
84
+ rubygems_version: 3.0.3
85
+ signing_key:
87
86
  specification_version: 4
88
87
  summary: ''
89
88
  test_files: