minitest 5.12.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,284 @@
1
+ ##
2
+ # It's where you hide your "assertions".
3
+ #
4
+ # Please note, because of the way that expectations are implemented,
5
+ # all expectations (eg must_equal) are dependent upon a thread local
6
+ # variable +:current_spec+. If your specs rely on mixing threads into
7
+ # the specs themselves, you're better off using assertions or the new
8
+ # _(value) wrapper. For example:
9
+ #
10
+ # it "should still work in threads" do
11
+ # my_threaded_thingy do
12
+ # (1+1).must_equal 2 # bad
13
+ # assert_equal 2, 1+1 # good
14
+ # _(1 + 1).must_equal 2 # good
15
+ # value(1 + 1).must_equal 2 # good, also #expect
16
+ # end
17
+ # end
18
+
19
+ module Minitest::Expectations
20
+
21
+ ##
22
+ # See Minitest::Assertions#assert_empty.
23
+ #
24
+ # collection.must_be_empty
25
+ #
26
+ # :method: must_be_empty
27
+
28
+ infect_an_assertion :assert_empty, :must_be_empty, :unary
29
+
30
+ ##
31
+ # See Minitest::Assertions#assert_equal
32
+ #
33
+ # a.must_equal b
34
+ #
35
+ # :method: must_equal
36
+
37
+ infect_an_assertion :assert_equal, :must_equal
38
+
39
+ ##
40
+ # See Minitest::Assertions#assert_in_delta
41
+ #
42
+ # n.must_be_close_to m [, delta]
43
+ #
44
+ # :method: must_be_close_to
45
+
46
+ infect_an_assertion :assert_in_delta, :must_be_close_to
47
+
48
+ alias :must_be_within_delta :must_be_close_to # :nodoc:
49
+
50
+ ##
51
+ # See Minitest::Assertions#assert_in_epsilon
52
+ #
53
+ # n.must_be_within_epsilon m [, epsilon]
54
+ #
55
+ # :method: must_be_within_epsilon
56
+
57
+ infect_an_assertion :assert_in_epsilon, :must_be_within_epsilon
58
+
59
+ ##
60
+ # See Minitest::Assertions#assert_includes
61
+ #
62
+ # collection.must_include obj
63
+ #
64
+ # :method: must_include
65
+
66
+ infect_an_assertion :assert_includes, :must_include, :reverse
67
+
68
+ ##
69
+ # See Minitest::Assertions#assert_instance_of
70
+ #
71
+ # obj.must_be_instance_of klass
72
+ #
73
+ # :method: must_be_instance_of
74
+
75
+ infect_an_assertion :assert_instance_of, :must_be_instance_of
76
+
77
+ ##
78
+ # See Minitest::Assertions#assert_kind_of
79
+ #
80
+ # obj.must_be_kind_of mod
81
+ #
82
+ # :method: must_be_kind_of
83
+
84
+ infect_an_assertion :assert_kind_of, :must_be_kind_of
85
+
86
+ ##
87
+ # See Minitest::Assertions#assert_match
88
+ #
89
+ # a.must_match b
90
+ #
91
+ # :method: must_match
92
+
93
+ infect_an_assertion :assert_match, :must_match
94
+
95
+ ##
96
+ # See Minitest::Assertions#assert_nil
97
+ #
98
+ # obj.must_be_nil
99
+ #
100
+ # :method: must_be_nil
101
+
102
+ infect_an_assertion :assert_nil, :must_be_nil, :unary
103
+
104
+ ##
105
+ # See Minitest::Assertions#assert_operator
106
+ #
107
+ # n.must_be :<=, 42
108
+ #
109
+ # This can also do predicates:
110
+ #
111
+ # str.must_be :empty?
112
+ #
113
+ # :method: must_be
114
+
115
+ infect_an_assertion :assert_operator, :must_be, :reverse
116
+
117
+ ##
118
+ # See Minitest::Assertions#assert_output
119
+ #
120
+ # proc { ... }.must_output out_or_nil [, err]
121
+ #
122
+ # :method: must_output
123
+
124
+ infect_an_assertion :assert_output, :must_output, :block
125
+
126
+ ##
127
+ # See Minitest::Assertions#assert_raises
128
+ #
129
+ # proc { ... }.must_raise exception
130
+ #
131
+ # :method: must_raise
132
+
133
+ infect_an_assertion :assert_raises, :must_raise, :block
134
+
135
+ ##
136
+ # See Minitest::Assertions#assert_respond_to
137
+ #
138
+ # obj.must_respond_to msg
139
+ #
140
+ # :method: must_respond_to
141
+
142
+ infect_an_assertion :assert_respond_to, :must_respond_to, :reverse
143
+
144
+ ##
145
+ # See Minitest::Assertions#assert_same
146
+ #
147
+ # a.must_be_same_as b
148
+ #
149
+ # :method: must_be_same_as
150
+
151
+ infect_an_assertion :assert_same, :must_be_same_as
152
+
153
+ ##
154
+ # See Minitest::Assertions#assert_silent
155
+ #
156
+ # proc { ... }.must_be_silent
157
+ #
158
+ # :method: must_be_silent
159
+
160
+ infect_an_assertion :assert_silent, :must_be_silent, :block
161
+
162
+ ##
163
+ # See Minitest::Assertions#assert_throws
164
+ #
165
+ # proc { ... }.must_throw sym
166
+ #
167
+ # :method: must_throw
168
+
169
+ infect_an_assertion :assert_throws, :must_throw, :block
170
+
171
+ ##
172
+ # See Minitest::Assertions#refute_empty
173
+ #
174
+ # collection.wont_be_empty
175
+ #
176
+ # :method: wont_be_empty
177
+
178
+ infect_an_assertion :refute_empty, :wont_be_empty, :unary
179
+
180
+ ##
181
+ # See Minitest::Assertions#refute_equal
182
+ #
183
+ # a.wont_equal b
184
+ #
185
+ # :method: wont_equal
186
+
187
+ infect_an_assertion :refute_equal, :wont_equal
188
+
189
+ ##
190
+ # See Minitest::Assertions#refute_in_delta
191
+ #
192
+ # n.wont_be_close_to m [, delta]
193
+ #
194
+ # :method: wont_be_close_to
195
+
196
+ infect_an_assertion :refute_in_delta, :wont_be_close_to
197
+
198
+ alias :wont_be_within_delta :wont_be_close_to # :nodoc:
199
+
200
+ ##
201
+ # See Minitest::Assertions#refute_in_epsilon
202
+ #
203
+ # n.wont_be_within_epsilon m [, epsilon]
204
+ #
205
+ # :method: wont_be_within_epsilon
206
+
207
+ infect_an_assertion :refute_in_epsilon, :wont_be_within_epsilon
208
+
209
+ ##
210
+ # See Minitest::Assertions#refute_includes
211
+ #
212
+ # collection.wont_include obj
213
+ #
214
+ # :method: wont_include
215
+
216
+ infect_an_assertion :refute_includes, :wont_include, :reverse
217
+
218
+ ##
219
+ # See Minitest::Assertions#refute_instance_of
220
+ #
221
+ # obj.wont_be_instance_of klass
222
+ #
223
+ # :method: wont_be_instance_of
224
+
225
+ infect_an_assertion :refute_instance_of, :wont_be_instance_of
226
+
227
+ ##
228
+ # See Minitest::Assertions#refute_kind_of
229
+ #
230
+ # obj.wont_be_kind_of mod
231
+ #
232
+ # :method: wont_be_kind_of
233
+
234
+ infect_an_assertion :refute_kind_of, :wont_be_kind_of
235
+
236
+ ##
237
+ # See Minitest::Assertions#refute_match
238
+ #
239
+ # a.wont_match b
240
+ #
241
+ # :method: wont_match
242
+
243
+ infect_an_assertion :refute_match, :wont_match
244
+
245
+ ##
246
+ # See Minitest::Assertions#refute_nil
247
+ #
248
+ # obj.wont_be_nil
249
+ #
250
+ # :method: wont_be_nil
251
+
252
+ infect_an_assertion :refute_nil, :wont_be_nil, :unary
253
+
254
+ ##
255
+ # See Minitest::Assertions#refute_operator
256
+ #
257
+ # n.wont_be :<=, 42
258
+ #
259
+ # This can also do predicates:
260
+ #
261
+ # str.wont_be :empty?
262
+ #
263
+ # :method: wont_be
264
+
265
+ infect_an_assertion :refute_operator, :wont_be, :reverse
266
+
267
+ ##
268
+ # See Minitest::Assertions#refute_respond_to
269
+ #
270
+ # obj.wont_respond_to msg
271
+ #
272
+ # :method: wont_respond_to
273
+
274
+ infect_an_assertion :refute_respond_to, :wont_respond_to, :reverse
275
+
276
+ ##
277
+ # See Minitest::Assertions#refute_same
278
+ #
279
+ # a.wont_be_same_as b
280
+ #
281
+ # :method: wont_be_same_as
282
+
283
+ infect_an_assertion :refute_same, :wont_be_same_as
284
+ end
@@ -0,0 +1,11 @@
1
+ require "minitest/parallel"
2
+
3
+ class Minitest::Test
4
+ parallelize_me!
5
+ end
6
+
7
+ begin
8
+ require "minitest/proveit"
9
+ rescue LoadError
10
+ warn "NOTE: `gem install minitest-proveit` for even more hellish tests"
11
+ end
@@ -0,0 +1,240 @@
1
+ class MockExpectationError < StandardError; end # :nodoc:
2
+
3
+ module Minitest # :nodoc:
4
+
5
+ ##
6
+ # A simple and clean mock object framework.
7
+ #
8
+ # All mock objects are an instance of Mock
9
+
10
+ class Mock
11
+ alias :__respond_to? :respond_to?
12
+
13
+ overridden_methods = %w[
14
+ ===
15
+ class
16
+ inspect
17
+ instance_eval
18
+ instance_variables
19
+ object_id
20
+ public_send
21
+ respond_to_missing?
22
+ send
23
+ to_s
24
+ ]
25
+
26
+ instance_methods.each do |m|
27
+ undef_method m unless overridden_methods.include?(m.to_s) || m =~ /^__/
28
+ end
29
+
30
+ overridden_methods.map(&:to_sym).each do |method_id|
31
+ define_method method_id do |*args, &b|
32
+ if @expected_calls.key? method_id then
33
+ method_missing(method_id, *args, &b)
34
+ else
35
+ super(*args, &b)
36
+ end
37
+ end
38
+ end
39
+
40
+ def initialize delegator = nil # :nodoc:
41
+ @delegator = delegator
42
+ @expected_calls = Hash.new { |calls, name| calls[name] = [] }
43
+ @actual_calls = Hash.new { |calls, name| calls[name] = [] }
44
+ end
45
+
46
+ ##
47
+ # Expect that method +name+ is called, optionally with +args+ or a
48
+ # +blk+, and returns +retval+.
49
+ #
50
+ # @mock.expect(:meaning_of_life, 42)
51
+ # @mock.meaning_of_life # => 42
52
+ #
53
+ # @mock.expect(:do_something_with, true, [some_obj, true])
54
+ # @mock.do_something_with(some_obj, true) # => true
55
+ #
56
+ # @mock.expect(:do_something_else, true) do |a1, a2|
57
+ # a1 == "buggs" && a2 == :bunny
58
+ # end
59
+ #
60
+ # +args+ is compared to the expected args using case equality (ie, the
61
+ # '===' operator), allowing for less specific expectations.
62
+ #
63
+ # @mock.expect(:uses_any_string, true, [String])
64
+ # @mock.uses_any_string("foo") # => true
65
+ # @mock.verify # => true
66
+ #
67
+ # @mock.expect(:uses_one_string, true, ["foo"])
68
+ # @mock.uses_one_string("bar") # => raises MockExpectationError
69
+ #
70
+ # If a method will be called multiple times, specify a new expect for each one.
71
+ # They will be used in the order you define them.
72
+ #
73
+ # @mock.expect(:ordinal_increment, 'first')
74
+ # @mock.expect(:ordinal_increment, 'second')
75
+ #
76
+ # @mock.ordinal_increment # => 'first'
77
+ # @mock.ordinal_increment # => 'second'
78
+ # @mock.ordinal_increment # => raises MockExpectationError "No more expects available for :ordinal_increment"
79
+ #
80
+
81
+ def expect name, retval, args = [], &blk
82
+ name = name.to_sym
83
+
84
+ if block_given?
85
+ raise ArgumentError, "args ignored when block given" unless args.empty?
86
+ @expected_calls[name] << { :retval => retval, :block => blk }
87
+ else
88
+ raise ArgumentError, "args must be an array" unless Array === args
89
+ @expected_calls[name] << { :retval => retval, :args => args }
90
+ end
91
+ self
92
+ end
93
+
94
+ def __call name, data # :nodoc:
95
+ case data
96
+ when Hash then
97
+ "#{name}(#{data[:args].inspect[1..-2]}) => #{data[:retval].inspect}"
98
+ else
99
+ data.map { |d| __call name, d }.join ", "
100
+ end
101
+ end
102
+
103
+ ##
104
+ # Verify that all methods were called as expected. Raises
105
+ # +MockExpectationError+ if the mock object was not called as
106
+ # expected.
107
+
108
+ def verify
109
+ @expected_calls.each do |name, expected|
110
+ actual = @actual_calls.fetch(name, nil)
111
+ raise MockExpectationError, "expected #{__call name, expected[0]}" unless actual
112
+ raise MockExpectationError, "expected #{__call name, expected[actual.size]}, got [#{__call name, actual}]" if
113
+ actual.size < expected.size
114
+ end
115
+ true
116
+ end
117
+
118
+ def method_missing sym, *args, &block # :nodoc:
119
+ unless @expected_calls.key?(sym) then
120
+ if @delegator && @delegator.respond_to?(sym)
121
+ return @delegator.public_send(sym, *args, &block)
122
+ else
123
+ raise NoMethodError, "unmocked method %p, expected one of %p" %
124
+ [sym, @expected_calls.keys.sort_by(&:to_s)]
125
+ end
126
+ end
127
+
128
+ index = @actual_calls[sym].length
129
+ expected_call = @expected_calls[sym][index]
130
+
131
+ unless expected_call then
132
+ raise MockExpectationError, "No more expects available for %p: %p" %
133
+ [sym, args]
134
+ end
135
+
136
+ expected_args, retval, val_block =
137
+ expected_call.values_at(:args, :retval, :block)
138
+
139
+ if val_block then
140
+ # keep "verify" happy
141
+ @actual_calls[sym] << expected_call
142
+
143
+ raise MockExpectationError, "mocked method %p failed block w/ %p" %
144
+ [sym, args] unless val_block.call(*args, &block)
145
+
146
+ return retval
147
+ end
148
+
149
+ if expected_args.size != args.size then
150
+ raise ArgumentError, "mocked method %p expects %d arguments, got %d" %
151
+ [sym, expected_args.size, args.size]
152
+ end
153
+
154
+ zipped_args = expected_args.zip(args)
155
+ fully_matched = zipped_args.all? { |mod, a|
156
+ mod === a or mod == a
157
+ }
158
+
159
+ unless fully_matched then
160
+ raise MockExpectationError, "mocked method %p called with unexpected arguments %p" %
161
+ [sym, args]
162
+ end
163
+
164
+ @actual_calls[sym] << {
165
+ :retval => retval,
166
+ :args => zipped_args.map! { |mod, a| mod === a ? mod : a },
167
+ }
168
+
169
+ retval
170
+ end
171
+
172
+ def respond_to? sym, include_private = false # :nodoc:
173
+ return true if @expected_calls.key? sym.to_sym
174
+ return true if @delegator && @delegator.respond_to?(sym, include_private)
175
+ __respond_to?(sym, include_private)
176
+ end
177
+ end
178
+ end
179
+
180
+ module Minitest::Assertions
181
+ ##
182
+ # Assert that the mock verifies correctly.
183
+
184
+ def assert_mock mock
185
+ assert mock.verify
186
+ end
187
+ end
188
+
189
+ ##
190
+ # Object extensions for Minitest::Mock.
191
+
192
+ class Object
193
+
194
+ ##
195
+ # Add a temporary stubbed method replacing +name+ for the duration
196
+ # of the +block+. If +val_or_callable+ responds to #call, then it
197
+ # returns the result of calling it, otherwise returns the value
198
+ # as-is. If stubbed method yields a block, +block_args+ will be
199
+ # passed along. Cleans up the stub at the end of the +block+. The
200
+ # method +name+ must exist before stubbing.
201
+ #
202
+ # def test_stale_eh
203
+ # obj_under_test = Something.new
204
+ # refute obj_under_test.stale?
205
+ #
206
+ # Time.stub :now, Time.at(0) do
207
+ # assert obj_under_test.stale?
208
+ # end
209
+ # end
210
+ #
211
+
212
+ def stub name, val_or_callable, *block_args
213
+ new_name = "__minitest_stub__#{name}"
214
+
215
+ metaclass = class << self; self; end
216
+
217
+ if respond_to? name and not methods.map(&:to_s).include? name.to_s then
218
+ metaclass.send :define_method, name do |*args|
219
+ super(*args)
220
+ end
221
+ end
222
+
223
+ metaclass.send :alias_method, new_name, name
224
+
225
+ metaclass.send :define_method, name do |*args, &blk|
226
+ if val_or_callable.respond_to? :call then
227
+ val_or_callable.call(*args, &blk)
228
+ else
229
+ blk.call(*block_args) if blk
230
+ val_or_callable
231
+ end
232
+ end
233
+
234
+ yield self
235
+ ensure
236
+ metaclass.send :undef_method, name
237
+ metaclass.send :alias_method, name, new_name
238
+ metaclass.send :undef_method, new_name
239
+ end
240
+ end