grift 0.1.0 → 1.0.0
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/.github/CODEOWNERS +1 -0
- data/.github/CONTRIBUTING.md +37 -0
- data/.github/dependabot.yml +11 -0
- data/.github/workflows/ci.yml +46 -0
- data/.gitignore +7 -19
- data/.rubocop.yml +159 -0
- data/.yardopts +1 -0
- data/CHANGELOG.md +26 -0
- data/Gemfile +18 -4
- data/Gemfile.lock +50 -3
- data/README.md +76 -4
- data/bin/console +1 -0
- data/grift.gemspec +2 -0
- data/lib/grift/config/restricted.yml +31 -0
- data/lib/grift/config.rb +27 -0
- data/lib/grift/error.rb +8 -0
- data/lib/grift/minitest_plugin.rb +27 -0
- data/lib/grift/mock_method/mock_executions.rb +102 -0
- data/lib/grift/mock_method.rb +284 -0
- data/lib/grift/mock_store.rb +141 -0
- data/lib/grift/version.rb +5 -1
- data/lib/grift.rb +206 -8
- metadata +15 -2
data/lib/grift.rb
CHANGED
@@ -1,16 +1,214 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'grift/config'
|
4
|
+
require 'grift/error'
|
5
|
+
require 'grift/minitest_plugin'
|
6
|
+
require 'grift/mock_method'
|
7
|
+
require 'grift/mock_method/mock_executions'
|
8
|
+
require 'grift/mock_store'
|
3
9
|
require 'grift/version'
|
4
10
|
|
11
|
+
##
|
12
|
+
# The base of the gem. Nearly all interactions with Grift should occur
|
13
|
+
# through this base module.
|
14
|
+
#
|
5
15
|
module Grift
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
16
|
+
@mock_store = Grift::MockStore.new
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# @example
|
20
|
+
# Grift.mock_store
|
21
|
+
# @return [Grift::MockStore] the current store of mocked methods
|
22
|
+
attr_reader :mock_store
|
23
|
+
|
24
|
+
##
|
25
|
+
# Mocks the given method to return the provided value.
|
26
|
+
# This is syntactical sugar equivalent to calling
|
27
|
+
# `spy_on` and then `mock_return_value`.
|
28
|
+
#
|
29
|
+
# @example
|
30
|
+
# my_mock = Grift.mock(MyClass, :some_method, true)
|
31
|
+
#
|
32
|
+
# @param klass [Class] the class of the method to be mocked
|
33
|
+
# @param method [Symbol] the symbol representing the method to be mocked
|
34
|
+
# @param return_value the value the method should return while mocked
|
35
|
+
#
|
36
|
+
# @raise [Grift::Error] exception if method is already mocked or does not exist
|
37
|
+
#
|
38
|
+
# @return [Grift::MockMethod]
|
39
|
+
#
|
40
|
+
def mock(klass, method, return_value = nil)
|
41
|
+
spy_on(klass, method).mock_return_value(return_value)
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Creates a mock for the given method without mocking
|
46
|
+
# the implementation or return values.
|
47
|
+
#
|
48
|
+
# @example
|
49
|
+
# my_mock = Grift.spy_on(MyClass, :some_method)
|
50
|
+
#
|
51
|
+
# @param klass [Class] the class of the method to be watched
|
52
|
+
# @param method [Symbol] the symbol representing the method to be watched
|
53
|
+
#
|
54
|
+
# @raise [Grift::Error] exception if method is already mocked or does not exist
|
55
|
+
#
|
56
|
+
# @return [Grift::MockMethod]
|
57
|
+
#
|
58
|
+
def spy_on(klass, method)
|
59
|
+
MockMethod.new(klass, method)
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# Checks whether the given method is currently mocked.
|
64
|
+
#
|
65
|
+
# @example
|
66
|
+
# Grift.mock(String, :upcase, 'STRING')
|
67
|
+
# Grift.mock_method?(String, :upcase)
|
68
|
+
# #=> true
|
69
|
+
#
|
70
|
+
# @example
|
71
|
+
# Grift.mock_method?(String, :downcase)
|
72
|
+
# #=> false
|
73
|
+
#
|
74
|
+
# @param klass [Class] the class of the method to be checked
|
75
|
+
# @param method [Symbol] the symbol representing the method to be checked
|
76
|
+
#
|
77
|
+
# @return [Boolean] true if method is currently mocked
|
78
|
+
#
|
79
|
+
def mock_method?(klass, method)
|
80
|
+
hash_key = Grift::MockMethod.hash_key(klass, method)
|
81
|
+
@mock_store.include?(hash_key)
|
82
|
+
end
|
83
|
+
|
84
|
+
##
|
85
|
+
# Checks whether the given method is restricted from being mocked.
|
86
|
+
# For the list of restricted methods see {Grift::Config#restricted_methods}.
|
87
|
+
#
|
88
|
+
# @see Grift::Config#restricted_methods
|
89
|
+
#
|
90
|
+
# @example
|
91
|
+
# Grift.restricted_method?(Grift, :mock)
|
92
|
+
# #=> true
|
93
|
+
#
|
94
|
+
# @example
|
95
|
+
# Grift.restricted_method?(String, :upcase)
|
96
|
+
# #=> false
|
97
|
+
#
|
98
|
+
# @param klass [Class] the class of the method to be checked
|
99
|
+
# @param method [Symbol] the symbol representing the method to be checked
|
100
|
+
#
|
101
|
+
# @return [Boolean] true if method cannot be mocked by Grift
|
102
|
+
#
|
103
|
+
def restricted_method?(klass, method)
|
104
|
+
base_klass = klass.to_s.split('::').first
|
105
|
+
klass_config = Grift::Config.restricted_methods[base_klass]
|
106
|
+
return false unless klass_config
|
107
|
+
|
108
|
+
(klass_config.include?('*') && !klass_config.include?("^#{method}")) || klass_config.include?(method.to_s)
|
109
|
+
end
|
110
|
+
|
111
|
+
##
|
112
|
+
# Clear the mocks of methods for a single class. Returns an array of the
|
113
|
+
# new state of mock executions for the class after clearing.
|
114
|
+
#
|
115
|
+
# @example
|
116
|
+
# Grift.clear_mocks(MyClass)
|
117
|
+
#
|
118
|
+
# @param klass [Class] the class for which to clear mocks
|
119
|
+
#
|
120
|
+
# @return [Array<Grift::MockMethod::MockExecutions>]
|
121
|
+
#
|
122
|
+
def clear_mocks(klass)
|
123
|
+
@mock_store.mocks(klass: klass).each(&:mock_clear)
|
124
|
+
end
|
125
|
+
|
126
|
+
##
|
127
|
+
# Clear the mocks of methods for all classes. Returns an array of the
|
128
|
+
# new state of mock executions for all mocks.
|
129
|
+
#
|
130
|
+
# @example
|
131
|
+
# Grift.clear_all_mocks
|
132
|
+
#
|
133
|
+
# @return [Array<Grift::MockMethod::MockExecutions>]
|
134
|
+
#
|
135
|
+
def clear_all_mocks
|
136
|
+
@mock_store.mocks.each(&:mock_clear)
|
137
|
+
end
|
138
|
+
|
139
|
+
##
|
140
|
+
# Reset the mocks of methods for a single class. Returns an array of the
|
141
|
+
# new state of mock executions for the class after resetting.
|
142
|
+
#
|
143
|
+
# @example
|
144
|
+
# Grift.reset_mocks(MyClass)
|
145
|
+
#
|
146
|
+
# @param klass [Class] the class for which to reset mocks
|
147
|
+
#
|
148
|
+
# @return [Array<Grift::MockMethod::MockExecutions>]
|
149
|
+
#
|
150
|
+
def reset_mocks(klass)
|
151
|
+
@mock_store.mocks(klass: klass).each(&:mock_reset)
|
152
|
+
end
|
153
|
+
|
154
|
+
##
|
155
|
+
# Reset the mocks of methods for all classes. Returns an array of the
|
156
|
+
# new state of mock executions for all mocks.
|
157
|
+
#
|
158
|
+
# @example
|
159
|
+
# Grift.reset_all_mocks
|
160
|
+
#
|
161
|
+
# @return [Array<Grift::MockMethod::MockExecutions>]
|
162
|
+
#
|
163
|
+
def reset_all_mocks
|
164
|
+
@mock_store.mocks.each(&:mock_reset)
|
165
|
+
end
|
166
|
+
|
167
|
+
##
|
168
|
+
# Restore the mocks of methods for a single class. Returns an array of the
|
169
|
+
# new state of mock executions for the class after restoring.
|
170
|
+
#
|
171
|
+
# @example
|
172
|
+
# Grift.restore_mocks(MyClass)
|
173
|
+
# #=> restores mocks and stops watching them
|
174
|
+
# @example
|
175
|
+
# Grift.restore_mocks(MyClass, watch: true)
|
176
|
+
# #=> restores mocks but keeps watching them
|
177
|
+
#
|
178
|
+
# @param klass [Class] the class for which to restore mocks
|
179
|
+
# @param watch [Boolean] if true, keep watching the methods
|
180
|
+
#
|
181
|
+
# @return [Array<Grift::MockMethod::MockExecutions>]
|
182
|
+
#
|
183
|
+
def restore_mocks(klass, watch: false)
|
184
|
+
if watch
|
185
|
+
@mock_store.mocks(klass: klass).each { |m| m.mock_restore(watch: true) }
|
186
|
+
else
|
187
|
+
@mock_store.remove(klass: klass)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
##
|
192
|
+
# Restore the mocks of methods for all classes. Returns an array of the
|
193
|
+
# new state of mock executions for all mocks.
|
194
|
+
#
|
195
|
+
# @example
|
196
|
+
# Grift.restore_mocks
|
197
|
+
# #=> restores all mocks and stops watching them
|
198
|
+
# @example
|
199
|
+
# Grift.restore_mocks(watch: true)
|
200
|
+
# #=> restores all mocks but keeps watching them
|
201
|
+
#
|
202
|
+
# @param watch [Boolean] if true, keep watching the methods
|
203
|
+
#
|
204
|
+
# @return [Array<Grift::MockMethod::MockExecutions>, Grift::MockStore]
|
205
|
+
#
|
206
|
+
def restore_all_mocks(watch: false)
|
207
|
+
if watch
|
208
|
+
@mock_store.mocks.each { |m| m.mock_restore(watch: true) }
|
209
|
+
else
|
210
|
+
@mock_store.remove
|
211
|
+
end
|
14
212
|
end
|
15
213
|
end
|
16
214
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grift
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Clark Brown
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A gem for simple mocking and spying in Ruby's MiniTest framework.
|
14
14
|
email:
|
@@ -17,7 +17,13 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- ".github/CODEOWNERS"
|
21
|
+
- ".github/CONTRIBUTING.md"
|
22
|
+
- ".github/dependabot.yml"
|
23
|
+
- ".github/workflows/ci.yml"
|
20
24
|
- ".gitignore"
|
25
|
+
- ".rubocop.yml"
|
26
|
+
- ".yardopts"
|
21
27
|
- CHANGELOG.md
|
22
28
|
- Gemfile
|
23
29
|
- Gemfile.lock
|
@@ -28,6 +34,13 @@ files:
|
|
28
34
|
- bin/setup
|
29
35
|
- grift.gemspec
|
30
36
|
- lib/grift.rb
|
37
|
+
- lib/grift/config.rb
|
38
|
+
- lib/grift/config/restricted.yml
|
39
|
+
- lib/grift/error.rb
|
40
|
+
- lib/grift/minitest_plugin.rb
|
41
|
+
- lib/grift/mock_method.rb
|
42
|
+
- lib/grift/mock_method/mock_executions.rb
|
43
|
+
- lib/grift/mock_store.rb
|
31
44
|
- lib/grift/version.rb
|
32
45
|
homepage: https://github.com/clarkedb/grift
|
33
46
|
licenses:
|