minitest 5.18.0 → 5.25.5

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.
data/lib/minitest/mock.rb CHANGED
@@ -8,9 +8,9 @@ module Minitest # :nodoc:
8
8
  # All mock objects are an instance of Mock
9
9
 
10
10
  class Mock
11
- alias :__respond_to? :respond_to?
11
+ alias __respond_to? respond_to?
12
12
 
13
- overridden_methods = %w[
13
+ overridden_methods = %i[
14
14
  ===
15
15
  class
16
16
  inspect
@@ -23,11 +23,14 @@ module Minitest # :nodoc:
23
23
  to_s
24
24
  ]
25
25
 
26
+ overridden_methods << :singleton_method_added if defined?(::DEBUGGER__)
27
+
26
28
  instance_methods.each do |m|
27
- undef_method m unless overridden_methods.include?(m.to_s) || m =~ /^__/
29
+ undef_method m unless overridden_methods.include?(m) || m =~ /^__/
28
30
  end
29
31
 
30
32
  overridden_methods.map(&:to_sym).each do |method_id|
33
+ old_w, $-w = $-w, nil
31
34
  define_method method_id do |*args, **kwargs, &b|
32
35
  if @expected_calls.key? method_id then
33
36
  if kwargs.empty? then # FIX: drop this after 2.7 dead
@@ -43,6 +46,8 @@ module Minitest # :nodoc:
43
46
  end
44
47
  end
45
48
  end
49
+ ensure
50
+ $-w = old_w
46
51
  end
47
52
 
48
53
  def initialize delegator = nil # :nodoc:
@@ -91,7 +96,7 @@ module Minitest # :nodoc:
91
96
  def expect name, retval, args = [], **kwargs, &blk
92
97
  name = name.to_sym
93
98
 
94
- if block_given?
99
+ if blk then
95
100
  raise ArgumentError, "args ignored when block given" unless args.empty?
96
101
  raise ArgumentError, "kwargs ignored when block given" unless kwargs.empty?
97
102
  @expected_calls[name] << { :retval => retval, :block => blk }
@@ -104,7 +109,7 @@ module Minitest # :nodoc:
104
109
  kwargs = args.pop
105
110
  else
106
111
  unless @@KW_WARNED then
107
- from = caller.first
112
+ from = caller(1..1).first
108
113
  warn "Using MT_KWARGS_HAC\K yet passing kwargs. From #{from}"
109
114
  @@KW_WARNED = true
110
115
  end
@@ -139,16 +144,16 @@ module Minitest # :nodoc:
139
144
 
140
145
  def verify
141
146
  @expected_calls.each do |name, expected|
142
- actual = @actual_calls.fetch(name, nil)
143
- raise MockExpectationError, "expected #{__call name, expected[0]}" unless actual
144
- raise MockExpectationError, "expected #{__call name, expected[actual.size]}, got [#{__call name, actual}]" if
147
+ actual = @actual_calls.fetch name, nil # defaults to []
148
+ raise MockExpectationError, "Expected #{__call name, expected[0]}" unless actual
149
+ raise MockExpectationError, "Expected #{__call name, expected[actual.size]}, got [#{__call name, actual}]" if
145
150
  actual.size < expected.size
146
151
  end
147
152
  true
148
153
  end
149
154
 
150
155
  def method_missing sym, *args, **kwargs, &block # :nodoc:
151
- unless @expected_calls.key?(sym) then
156
+ unless @expected_calls.key? sym then
152
157
  if @delegator && @delegator.respond_to?(sym)
153
158
  if kwargs.empty? then # FIX: drop this after 2.7 dead
154
159
  return @delegator.public_send(sym, *args, &block)
@@ -170,9 +175,9 @@ module Minitest # :nodoc:
170
175
  end
171
176
 
172
177
  expected_args, expected_kwargs, retval, val_block =
173
- expected_call.values_at(:args, :kwargs, :retval, :block)
178
+ expected_call.values_at :args, :kwargs, :retval, :block
174
179
 
175
- expected_kwargs = kwargs.map { |ak, av| [ak, Object] }.to_h if
180
+ expected_kwargs = kwargs.to_h { |ak, av| [ak, Object] } if
176
181
  Hash == expected_kwargs
177
182
 
178
183
  if val_block then
@@ -195,7 +200,7 @@ module Minitest # :nodoc:
195
200
  [sym, expected_kwargs.size, kwargs]
196
201
  end
197
202
 
198
- zipped_args = expected_args.zip(args)
203
+ zipped_args = expected_args.zip args
199
204
  fully_matched = zipped_args.all? { |mod, a|
200
205
  mod === a or mod == a
201
206
  }
@@ -210,10 +215,10 @@ module Minitest # :nodoc:
210
215
  raise MockExpectationError, fmt % [sym, expected_kwargs.keys, kwargs.keys]
211
216
  end
212
217
 
213
- zipped_kwargs = expected_kwargs.map { |ek, ev|
218
+ zipped_kwargs = expected_kwargs.to_h { |ek, ev|
214
219
  av = kwargs[ek]
215
220
  [ek, [ev, av]]
216
- }.to_h
221
+ }
217
222
 
218
223
  fully_matched = zipped_kwargs.all? { |ek, (ev, av)|
219
224
  ev === av or ev == av
@@ -226,8 +231,8 @@ module Minitest # :nodoc:
226
231
 
227
232
  @actual_calls[sym] << {
228
233
  :retval => retval,
229
- :args => zipped_args.map { |e, a| e === a ? e : a },
230
- :kwargs => zipped_kwargs.map { |k, (e, a)| [k, e === a ? e : a] }.to_h,
234
+ :args => zipped_args.map { |e, a| e === a ? e : a },
235
+ :kwargs => zipped_kwargs.to_h { |k, (e, a)| [k, e === a ? e : a] },
231
236
  }
232
237
 
233
238
  retval
@@ -236,20 +241,35 @@ module Minitest # :nodoc:
236
241
  def respond_to? sym, include_private = false # :nodoc:
237
242
  return true if @expected_calls.key? sym.to_sym
238
243
  return true if @delegator && @delegator.respond_to?(sym, include_private)
239
- __respond_to?(sym, include_private)
244
+ __respond_to? sym, include_private
240
245
  end
241
246
  end
242
247
  end
243
248
 
244
249
  module Minitest::Assertions
245
250
  ##
246
- # Assert that the mock verifies correctly.
251
+ # Assert that the mock verifies correctly and fail if not.
247
252
 
248
- def assert_mock mock
253
+ def assert_mock mock, msg = nil
249
254
  assert mock.verify
255
+ rescue MockExpectationError => e
256
+ msg = message(msg) { e.message }
257
+ flunk msg
250
258
  end
251
259
  end
252
260
 
261
+ module Minitest::Expectations
262
+ ##
263
+ # See Minitest::Assertions#assert_mock.
264
+ #
265
+ # _(collection).must_verify
266
+ #
267
+ # :method: must_verify
268
+
269
+ infect_an_assertion :assert_mock, :must_verify, :unary if
270
+ defined?(infect_an_assertion)
271
+ end
272
+
253
273
  ##
254
274
  # Object extensions for Minitest::Mock.
255
275
 
@@ -1,5 +1,5 @@
1
1
  module Minitest
2
- module Parallel #:nodoc:
2
+ module Parallel # :nodoc:
3
3
 
4
4
  ##
5
5
  # The engine used to run multiple tests in parallel.
@@ -16,7 +16,7 @@ module Minitest
16
16
 
17
17
  def initialize size
18
18
  @size = size
19
- @queue = Queue.new
19
+ @queue = Thread::Queue.new
20
20
  @pool = nil
21
21
  end
22
22
 
@@ -24,10 +24,10 @@ module Minitest
24
24
  # Start the executor
25
25
 
26
26
  def start
27
- @pool = size.times.map {
28
- Thread.new(@queue) do |queue|
27
+ @pool = Array.new(size) {
28
+ Thread.new @queue do |queue|
29
29
  Thread.current.abort_on_exception = true
30
- while (job = queue.pop)
30
+ while job = queue.pop do
31
31
  klass, method, reporter = job
32
32
  reporter.synchronize { reporter.prerecord klass, method }
33
33
  result = Minitest.run_one_method klass, method
@@ -8,13 +8,13 @@ module Minitest
8
8
  end
9
9
 
10
10
  def self.plugin_pride_init options # :nodoc:
11
- if PrideIO.pride? then
12
- klass = ENV["TERM"] =~ /^xterm|-256color$/ ? PrideLOL : PrideIO
13
- io = klass.new options[:io]
11
+ return unless PrideIO.pride?
14
12
 
15
- self.reporter.reporters.grep(Minitest::Reporter).each do |rep|
16
- rep.io = io if rep.io.tty?
17
- end
13
+ klass = ENV["TERM"] =~ /^xterm|-(?:256color|direct)$/ ? PrideLOL : PrideIO
14
+ io = klass.new options[:io]
15
+
16
+ self.reporter.reporters.grep(Minitest::Reporter).each do |rep|
17
+ rep.io = io if rep.io.tty?
18
18
  end
19
19
  end
20
20
 
@@ -59,12 +59,10 @@ module Minitest
59
59
 
60
60
  def print o
61
61
  case o
62
- when "." then
62
+ when ".", "S" then
63
63
  io.print pride o
64
64
  when "E", "F" then
65
65
  io.print "#{ESC}41m#{ESC}37m#{o}#{NND}"
66
- when "S" then
67
- io.print pride o
68
66
  else
69
67
  io.print o
70
68
  end
@@ -72,11 +70,9 @@ module Minitest
72
70
 
73
71
  def puts *o # :nodoc:
74
72
  o.map! { |s|
75
- s.to_s.sub(/Finished/) {
73
+ s.to_s.sub("Finished") {
76
74
  @index = 0
77
- "Fabulous run".split(//).map { |c|
78
- pride(c)
79
- }.join
75
+ "Fabulous run".chars.map { |c| pride(c) }.join
80
76
  }
81
77
  }
82
78
 
@@ -113,19 +109,16 @@ module Minitest
113
109
  #
114
110
  # plot (3*sin(x)+3), (3*sin(x+2*pi/3)+3), (3*sin(x+4*pi/3)+3)
115
111
 
116
- # 6 has wide pretty gradients. 3 == lolcat, about half the width
117
- @colors = (0...(6 * 7)).map { |n|
118
- n *= 1.0 / 6
112
+ @colors = Array.new(6 * 7) { |n|
113
+ n *= 1.0 / 3
119
114
  r = (3 * Math.sin(n ) + 3).to_i
120
- g = (3 * Math.sin(n + 2 * PI_3) + 3).to_i
121
- b = (3 * Math.sin(n + 4 * PI_3) + 3).to_i
122
-
123
- # Then we take rgb and encode them in a single number using base 6.
124
- # For some mysterious reason, we add 16... to clear the bottom 4 bits?
125
- # Yes... they're ugly.
115
+ g = (3 * Math.sin(n + 4 * PI_3) + 3).to_i
116
+ b = (3 * Math.sin(n + 2 * PI_3) + 3).to_i
126
117
 
118
+ # Then we take rgb and encode them in a single number using
119
+ # base 6, shifted by 16 for the base 16 ansi colors.
127
120
  36 * r + 6 * g + b + 16
128
- }
121
+ }.rotate(4) # puts "red" first
129
122
 
130
123
  super
131
124
  end
data/lib/minitest/spec.rb CHANGED
@@ -4,11 +4,11 @@ class Module # :nodoc:
4
4
  def infect_an_assertion meth, new_name, dont_flip = false # :nodoc:
5
5
  block = dont_flip == :block
6
6
  dont_flip = false if block
7
- target_obj = block ? '_{obj.method}' : '_(obj)'
7
+ target_obj = block ? "_{obj.method}" : "_(obj)"
8
8
 
9
9
  # https://eregon.me/blog/2021/02/13/correct-delegation-in-ruby-2-27-3.html
10
10
  # Drop this when we can drop ruby 2.6 (aka after rails 6.1 EOL, ~2024-06)
11
- kw_extra = "ruby2_keywords %p" % [new_name] if respond_to?(:ruby2_keywords, true)
11
+ kw_extra = "ruby2_keywords %p" % [new_name] if respond_to? :ruby2_keywords, true
12
12
 
13
13
  # warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
14
14
  self.class_eval <<-EOM, __FILE__, __LINE__ + 1
@@ -81,14 +81,15 @@ module Kernel
81
81
 
82
82
  def describe desc, *additional_desc, &block # :doc:
83
83
  stack = Minitest::Spec.describe_stack
84
- name = [stack.last, desc, *additional_desc].compact.join("::")
85
- sclas = stack.last || if Class === self && kind_of?(Minitest::Spec::DSL) then
86
- self
87
- else
88
- Minitest::Spec.spec_type desc, *additional_desc
89
- end
84
+ is_spec_class = Class === self && kind_of?(Minitest::Spec::DSL)
85
+ name = [stack.last, desc, *additional_desc]
86
+ name.prepend self if stack.empty? && is_spec_class
87
+ sclas =
88
+ stack.last \
89
+ || (is_spec_class && self) \
90
+ || Minitest::Spec.spec_type(desc, *additional_desc)
90
91
 
91
- cls = sclas.create name, desc
92
+ cls = sclas.create name.compact.join("::"), desc
92
93
 
93
94
  stack.push cls
94
95
  cls.class_eval(&block)
@@ -249,7 +250,7 @@ class Minitest::Spec < Minitest::Test
249
250
  pre, post = "let '#{name}' cannot ", ". Please use another name."
250
251
  methods = Minitest::Spec.instance_methods.map(&:to_s) - %w[subject]
251
252
  raise ArgumentError, "#{pre}begin with 'test'#{post}" if
252
- name =~ /\Atest/
253
+ name.start_with? "test"
253
254
  raise ArgumentError, "#{pre}override a method in Minitest::Spec#{post}" if
254
255
  methods.include? name
255
256
 
@@ -268,7 +269,7 @@ class Minitest::Spec < Minitest::Test
268
269
  end
269
270
 
270
271
  def create name, desc # :nodoc:
271
- cls = Class.new(self) do
272
+ cls = Class.new self do
272
273
  @name = name
273
274
  @desc = desc
274
275
 
@@ -289,7 +290,7 @@ class Minitest::Spec < Minitest::Test
289
290
  end
290
291
 
291
292
  attr_reader :desc # :nodoc:
292
- alias :specify :it
293
+ alias specify it
293
294
 
294
295
  ##
295
296
  # Rdoc... why are you so dumb?
data/lib/minitest/test.rb CHANGED
@@ -9,8 +9,8 @@ module Minitest
9
9
 
10
10
  class Test < Runnable
11
11
  require "minitest/assertions"
12
- include Minitest::Assertions
13
12
  include Minitest::Reportable
13
+ include Minitest::Assertions
14
14
 
15
15
  def class_name # :nodoc:
16
16
  self.class.name # for Minitest::Reportable
@@ -52,9 +52,10 @@ module Minitest
52
52
  end
53
53
 
54
54
  ##
55
- # Call this at the top of your tests when you want to run your
56
- # tests in parallel. In doing so, you're admitting that you rule
57
- # and your tests are awesome.
55
+ # Call this at the top of your tests (inside the +Minitest::Test+
56
+ # subclass or +describe+ block) when you want to run your tests in
57
+ # parallel. In doing so, you're admitting that you rule and your
58
+ # tests are awesome.
58
59
 
59
60
  def self.parallelize_me!
60
61
  include Minitest::Parallel::Test
@@ -80,32 +81,22 @@ module Minitest
80
81
  end
81
82
  end
82
83
 
83
- ##
84
- # Defines the order to run tests (:random by default). Override
85
- # this or use a convenience method to change it for your tests.
86
-
87
- def self.test_order
88
- :random
89
- end
90
-
91
84
  ##
92
85
  # Runs a single test with setup/teardown hooks.
93
86
 
94
87
  def run
95
- with_info_handler do
96
- time_it do
97
- capture_exceptions do
98
- SETUP_METHODS.each do |hook|
99
- self.send hook
100
- end
101
-
102
- self.send self.name
88
+ time_it do
89
+ capture_exceptions do
90
+ SETUP_METHODS.each do |hook|
91
+ self.send hook
103
92
  end
104
93
 
105
- TEARDOWN_METHODS.each do |hook|
106
- capture_exceptions do
107
- self.send hook
108
- end
94
+ self.send self.name
95
+ end
96
+
97
+ TEARDOWN_METHODS.each do |hook|
98
+ capture_exceptions do
99
+ self.send hook
109
100
  end
110
101
  end
111
102
  end
@@ -149,7 +140,7 @@ module Minitest
149
140
  # end
150
141
  # end
151
142
  #
152
- # class MiniTest::Test
143
+ # class Minitest::Test
153
144
  # include MyMinitestPlugin
154
145
  # end
155
146
 
@@ -237,20 +228,10 @@ module Minitest
237
228
  ne
238
229
  end
239
230
 
240
- def with_info_handler &block # :nodoc:
241
- t0 = Minitest.clock_time
242
-
243
- handler = lambda do
244
- warn "\nCurrent: %s#%s %.2fs" % [self.class, self.name, Minitest.clock_time - t0]
245
- end
246
-
247
- self.class.on_signal ::Minitest.info_signal, handler, &block
248
- end
249
-
250
231
  include LifecycleHooks
251
232
  include Guard
252
233
  extend Guard
253
234
  end # Test
254
235
  end
255
236
 
256
- require "minitest/unit" unless defined?(MiniTest) # compatibility layer only
237
+ require "minitest/unit" if ENV["MT_COMPAT"] # compatibility layer only
@@ -1,6 +1,12 @@
1
1
  require "shellwords"
2
2
  require "rbconfig"
3
- require "rake/tasklib"
3
+
4
+ begin
5
+ require "rake/tasklib"
6
+ rescue LoadError => e
7
+ warn e.message
8
+ return
9
+ end
4
10
 
5
11
  module Minitest # :nodoc:
6
12
 
@@ -29,6 +35,10 @@ module Minitest # :nodoc:
29
35
  # end
30
36
  #
31
37
  # Customize the name and only run unit tests.
38
+ #
39
+ # NOTE: To hook this task up to the default, make it a dependency:
40
+ #
41
+ # task default: :unit
32
42
 
33
43
  class TestTask < Rake::TaskLib
34
44
  WINDOWS = RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ # :nodoc:
@@ -110,7 +120,7 @@ module Minitest # :nodoc:
110
120
  self.test_globs = ["test/**/test_*.rb",
111
121
  "test/**/*_test.rb"]
112
122
  self.test_prelude = nil
113
- self.verbose = Rake.application.options.trace
123
+ self.verbose = Rake.application.options.trace || Rake.verbose == true
114
124
  self.warning = true
115
125
  end
116
126
 
@@ -140,7 +150,7 @@ module Minitest # :nodoc:
140
150
  ENV["N"] && ENV["N"].to_i > 0
141
151
 
142
152
  lib_extras = (ENV["MT_LIB_EXTRAS"] || "").split File::PATH_SEPARATOR
143
- self.libs[0,0] = lib_extras
153
+ self.libs[0, 0] = lib_extras
144
154
 
145
155
  extra_args << "-n" << ENV["N"] if ENV["N"]
146
156
  extra_args << "-e" << ENV["X"] if ENV["X"]
@@ -157,11 +167,9 @@ module Minitest # :nodoc:
157
167
  end
158
168
 
159
169
  def define # :nodoc:
160
- default_tasks = []
161
-
162
170
  desc "Run the test suite. Use N, X, A, and TESTOPTS to add flags/args."
163
171
  task name do
164
- ruby make_test_cmd, verbose:verbose
172
+ ruby make_test_cmd, verbose: verbose
165
173
  end
166
174
 
167
175
  desc "Print out the test command. Good for profiling and other tools."
@@ -175,7 +183,7 @@ module Minitest # :nodoc:
175
183
 
176
184
  # 3 seems to be the magic number... (tho not by that much)
177
185
  bad, good, n = {}, [], (ENV.delete("K") || 3).to_i
178
- file = ENV.delete("F")
186
+ file = ENV.delete "F"
179
187
  times = {}
180
188
 
181
189
  tt0 = Time.now
@@ -236,18 +244,13 @@ module Minitest # :nodoc:
236
244
 
237
245
  task "#{name}:deps" => "#{name}:isolated" # now just an alias
238
246
 
239
- desc "Show bottom 25 tests wrt time."
247
+ desc "Run the test suite and report the slowest 25 tests."
240
248
  task "#{name}:slow" do
241
249
  sh ["rake #{name} A=-v",
242
250
  "egrep '#test_.* s = .'",
243
251
  "sort -n -k2 -t=",
244
252
  "tail -25"].join " | "
245
253
  end
246
-
247
- default_tasks << name
248
-
249
- desc "Run the default task(s)."
250
- task :default => default_tasks
251
254
  end
252
255
 
253
256
  ##
@@ -265,11 +268,11 @@ module Minitest # :nodoc:
265
268
  runner = runner.join "; "
266
269
 
267
270
  args = []
268
- args << "-I#{libs.join(File::PATH_SEPARATOR)}" unless libs.empty?
271
+ args << "-I#{libs.join File::PATH_SEPARATOR}" unless libs.empty?
269
272
  args << "-w" if warning
270
- args << '-e'
273
+ args << "-e"
271
274
  args << "'#{runner}'"
272
- args << '--'
275
+ args << "--"
273
276
  args << extra_args.map(&:shellescape)
274
277
 
275
278
  args.join " "
@@ -290,11 +293,10 @@ class Work < Queue # :nodoc:
290
293
  end
291
294
 
292
295
  class Integer # :nodoc:
293
- def threads_do(jobs) # :nodoc:
294
- require "thread"
296
+ def threads_do jobs # :nodoc:
295
297
  q = Work.new jobs
296
298
 
297
- self.times.map {
299
+ Array.new(self) {
298
300
  Thread.new do
299
301
  while job = q.pop # go until quit value
300
302
  yield job