minitest 5.5.1 → 5.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
- require 'minitest/unit'
2
- require 'minitest/spec'
1
+ require "minitest/unit"
2
+ require "minitest/spec"
3
3
 
4
4
  module Minitest
5
5
  ##
@@ -221,8 +221,8 @@ module Minitest
221
221
  # See: http://en.wikipedia.org/wiki/Coefficient_of_determination
222
222
 
223
223
  def fit_error xys
224
- y_bar = sigma(xys) { |x, y| y } / xys.size.to_f
225
- ss_tot = sigma(xys) { |x, y| (y - y_bar) ** 2 }
224
+ y_bar = sigma(xys) { |_, y| y } / xys.size.to_f
225
+ ss_tot = sigma(xys) { |_, y| (y - y_bar) ** 2 }
226
226
  ss_err = sigma(xys) { |x, y| (yield(x) - y) ** 2 }
227
227
 
228
228
  1 - (ss_err / ss_tot)
@@ -238,9 +238,9 @@ module Minitest
238
238
  def fit_exponential xs, ys
239
239
  n = xs.size
240
240
  xys = xs.zip(ys)
241
- sxlny = sigma(xys) { |x,y| x * Math.log(y) }
242
- slny = sigma(xys) { |x,y| Math.log(y) }
243
- sx2 = sigma(xys) { |x,y| x * x }
241
+ sxlny = sigma(xys) { |x, y| x * Math.log(y) }
242
+ slny = sigma(xys) { |_, y| Math.log(y) }
243
+ sx2 = sigma(xys) { |x, _| x * x }
244
244
  sx = sigma xs
245
245
 
246
246
  c = n * sx2 - sx ** 2
@@ -260,10 +260,10 @@ module Minitest
260
260
  def fit_logarithmic xs, ys
261
261
  n = xs.size
262
262
  xys = xs.zip(ys)
263
- slnx2 = sigma(xys) { |x,y| Math.log(x) ** 2 }
264
- slnx = sigma(xys) { |x,y| Math.log(x) }
265
- sylnx = sigma(xys) { |x,y| y * Math.log(x) }
266
- sy = sigma(xys) { |x,y| y }
263
+ slnx2 = sigma(xys) { |x, _| Math.log(x) ** 2 }
264
+ slnx = sigma(xys) { |x, _| Math.log(x) }
265
+ sylnx = sigma(xys) { |x, y| y * Math.log(x) }
266
+ sy = sigma(xys) { |_, y| y }
267
267
 
268
268
  c = n * slnx2 - slnx ** 2
269
269
  b = ( n * sylnx - sy * slnx ) / c
@@ -272,7 +272,6 @@ module Minitest
272
272
  return a, b, fit_error(xys) { |x| a + b * Math.log(x) }
273
273
  end
274
274
 
275
-
276
275
  ##
277
276
  # Fits the functional form: a + bx.
278
277
  #
@@ -286,7 +285,7 @@ module Minitest
286
285
  sx = sigma xs
287
286
  sy = sigma ys
288
287
  sx2 = sigma(xs) { |x| x ** 2 }
289
- sxy = sigma(xys) { |x,y| x * y }
288
+ sxy = sigma(xys) { |x, y| x * y }
290
289
 
291
290
  c = n * sx2 - sx**2
292
291
  a = (sy * sx2 - sx * sxy) / c
@@ -310,7 +309,7 @@ module Minitest
310
309
  slny = sigma(ys) { | y| Math.log(y) }
311
310
  slnx2 = sigma(xs) { |x | Math.log(x) ** 2 }
312
311
 
313
- b = (n * slnxlny - slnx * slny) / (n * slnx2 - slnx ** 2);
312
+ b = (n * slnxlny - slnx * slny) / (n * slnx2 - slnx ** 2)
314
313
  a = (slny - b * slnx) / n
315
314
 
316
315
  return Math.exp(a), b, fit_error(xys) { |x| (Math.exp(a) * (x ** b)) }
@@ -357,7 +356,7 @@ module Minitest
357
356
  # See ::bench_performance_linear for an example of how to use this.
358
357
 
359
358
  def self.bench name, &block
360
- define_method "bench_#{name.gsub(/\W+/, '_')}", &block
359
+ define_method "bench_#{name.gsub(/\W+/, "_")}", &block
361
360
  end
362
361
 
363
362
  ##
@@ -4,17 +4,44 @@
4
4
  # Please note, because of the way that expectations are implemented,
5
5
  # all expectations (eg must_equal) are dependent upon a thread local
6
6
  # variable +:current_spec+. If your specs rely on mixing threads into
7
- # the specs themselves, you're better off using assertions. For
8
- # example:
7
+ # the specs themselves, you're better off using assertions or the new
8
+ # _(value) wrapper. For example:
9
9
  #
10
10
  # it "should still work in threads" do
11
11
  # my_threaded_thingy do
12
- # (1+1).must_equal 2 # bad
13
- # assert_equal 2, 1+1 # good
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
14
16
  # end
15
17
  # end
16
18
 
17
19
  module Minitest::Expectations
20
+
21
+ ##
22
+ # Returns a value monad that has all of Expectations methods
23
+ # available to it.
24
+ #
25
+ # Also aliased to #value and #expect for your aesthetic pleasure:
26
+ #
27
+ # _(1 + 1).must_equal 2
28
+ # value(1 + 1).must_equal 2
29
+ # expect(1 + 1).must_equal 2
30
+ #
31
+ # This method of expectation-based testing is preferable to
32
+ # straight-expectation methods (on Object) because it stores its
33
+ # test context, bypassing our hacky use of thread-local variables.
34
+ #
35
+ # At some point, the methods on Object will be deprecated and then
36
+ # removed.
37
+
38
+ def _ value = nil, &block
39
+ Minitest::Expectation.new block || value, self
40
+ end
41
+
42
+ alias value _
43
+ alias expect _
44
+
18
45
  ##
19
46
  # See Minitest::Assertions#assert_empty.
20
47
  #
@@ -10,15 +10,17 @@ module Minitest # :nodoc:
10
10
  class Mock
11
11
  alias :__respond_to? :respond_to?
12
12
 
13
- overridden_methods = %w(
13
+ overridden_methods = %w[
14
14
  ===
15
15
  inspect
16
+ instance_eval
17
+ instance_variables
16
18
  object_id
17
19
  public_send
18
20
  respond_to_missing?
19
21
  send
20
22
  to_s
21
- )
23
+ ]
22
24
 
23
25
  instance_methods.each do |m|
24
26
  undef_method m unless overridden_methods.include?(m.to_s) || m =~ /^__/
@@ -26,7 +28,7 @@ module Minitest # :nodoc:
26
28
 
27
29
  overridden_methods.map(&:to_sym).each do |method_id|
28
30
  define_method method_id do |*args, &b|
29
- if @expected_calls.has_key? method_id then
31
+ if @expected_calls.key? method_id then
30
32
  method_missing(method_id, *args, &b)
31
33
  else
32
34
  super(*args, &b)
@@ -64,7 +66,7 @@ module Minitest # :nodoc:
64
66
  # @mock.uses_one_string("bar") # => true
65
67
  # @mock.verify # => raises MockExpectationError
66
68
 
67
- def expect(name, retval, args=[], &blk)
69
+ def expect(name, retval, args = [], &blk)
68
70
  name = name.to_sym
69
71
 
70
72
  if block_given?
@@ -98,11 +100,11 @@ module Minitest # :nodoc:
98
100
  msg2 = "#{msg1}, got [#{__call name, @actual_calls[name]}]"
99
101
 
100
102
  raise MockExpectationError, msg2 if
101
- @actual_calls.has_key?(name) and
103
+ @actual_calls.key?(name) and
102
104
  not @actual_calls[name].include?(expected)
103
105
 
104
106
  raise MockExpectationError, msg1 unless
105
- @actual_calls.has_key?(name) and
107
+ @actual_calls.key?(name) and
106
108
  @actual_calls[name].include?(expected)
107
109
  end
108
110
  end
@@ -110,7 +112,7 @@ module Minitest # :nodoc:
110
112
  end
111
113
 
112
114
  def method_missing(sym, *args, &block) # :nodoc:
113
- unless @expected_calls.has_key?(sym) then
115
+ unless @expected_calls.key?(sym) then
114
116
  raise NoMethodError, "unmocked method %p, expected one of %p" %
115
117
  [sym, @expected_calls.keys.sort_by(&:to_s)]
116
118
  end
@@ -127,11 +129,12 @@ module Minitest # :nodoc:
127
129
  expected_call.values_at(:args, :retval, :block)
128
130
 
129
131
  if val_block then
132
+ # keep "verify" happy
133
+ @actual_calls[sym] << expected_call
134
+
130
135
  raise MockExpectationError, "mocked method %p failed block w/ %p" %
131
136
  [sym, args] unless val_block.call(*args, &block)
132
137
 
133
- # keep "verify" happy
134
- @actual_calls[sym] << expected_call
135
138
  return retval
136
139
  end
137
140
 
@@ -151,20 +154,23 @@ module Minitest # :nodoc:
151
154
 
152
155
  @actual_calls[sym] << {
153
156
  :retval => retval,
154
- :args => expected_args.zip(args).map { |mod, a| mod === a ? mod : a }
157
+ :args => expected_args.zip(args).map { |mod, a| mod === a ? mod : a },
155
158
  }
156
159
 
157
160
  retval
158
161
  end
159
162
 
160
163
  def respond_to?(sym, include_private = false) # :nodoc:
161
- return true if @expected_calls.has_key? sym.to_sym
162
- return __respond_to?(sym, include_private)
164
+ return true if @expected_calls.key? sym.to_sym
165
+ __respond_to?(sym, include_private)
163
166
  end
164
167
  end
165
168
  end
166
169
 
167
- class Object # :nodoc:
170
+ ##
171
+ # Object extensions for Minitest::Mock.
172
+
173
+ class Object
168
174
 
169
175
  ##
170
176
  # Add a temporary stubbed method replacing +name+ for the duration
@@ -184,7 +190,7 @@ class Object # :nodoc:
184
190
  # end
185
191
  #
186
192
 
187
- def stub name, val_or_callable, *block_args, &block
193
+ def stub name, val_or_callable, *block_args
188
194
  new_name = "__minitest_stub__#{name}"
189
195
 
190
196
  metaclass = class << self; self; end
@@ -198,12 +204,11 @@ class Object # :nodoc:
198
204
  metaclass.send :alias_method, new_name, name
199
205
 
200
206
  metaclass.send :define_method, name do |*args, &blk|
201
-
202
207
  ret = if val_or_callable.respond_to? :call then
203
- val_or_callable.call(*args)
204
- else
205
- val_or_callable
206
- end
208
+ val_or_callable.call(*args)
209
+ else
210
+ val_or_callable
211
+ end
207
212
 
208
213
  blk.call(*block_args) if blk
209
214
 
@@ -216,5 +221,4 @@ class Object # :nodoc:
216
221
  metaclass.send :alias_method, name, new_name
217
222
  metaclass.send :undef_method, new_name
218
223
  end
219
-
220
224
  end
@@ -17,10 +17,17 @@ module Minitest
17
17
  def initialize size
18
18
  @size = size
19
19
  @queue = Queue.new
20
+ @pool = nil
21
+ end
22
+
23
+ ##
24
+ # Start the executor
25
+
26
+ def start
20
27
  @pool = size.times.map {
21
28
  Thread.new(@queue) do |queue|
22
29
  Thread.current.abort_on_exception = true
23
- while job = queue.pop
30
+ while (job = queue.pop)
24
31
  klass, method, reporter = job
25
32
  result = Minitest.run_one_method klass, method
26
33
  reporter.synchronize { reporter.record result }
@@ -48,11 +55,14 @@ module Minitest
48
55
  module Test
49
56
  def _synchronize; Test.io_lock.synchronize { yield }; end # :nodoc:
50
57
 
51
- module ClassMethods
52
- def run_one_method klass, method_name, reporter # :nodoc:
58
+ module ClassMethods # :nodoc:
59
+ def run_one_method klass, method_name, reporter
53
60
  Minitest.parallel_executor << [klass, method_name, reporter]
54
61
  end
55
- def test_order; :parallel; end # :nodoc:
62
+
63
+ def test_order
64
+ :parallel
65
+ end
56
66
  end
57
67
  end
58
68
  end
@@ -1,7 +1,7 @@
1
1
  require "minitest"
2
2
 
3
3
  module Minitest
4
- def self.plugin_pride_options opts, options # :nodoc:
4
+ def self.plugin_pride_options opts, _options # :nodoc:
5
5
  opts.on "-p", "--pride", "Pride. Show your testing pride!" do
6
6
  PrideIO.pride!
7
7
  end
@@ -74,7 +74,7 @@ module Minitest
74
74
  o.map! { |s|
75
75
  s.to_s.sub(/Finished/) {
76
76
  @index = 0
77
- 'Fabulous run'.split(//).map { |c|
77
+ "Fabulous run".split(//).map { |c|
78
78
  pride(c)
79
79
  }.join
80
80
  }
@@ -4,21 +4,32 @@ class Module # :nodoc:
4
4
  def infect_an_assertion meth, new_name, dont_flip = false # :nodoc:
5
5
  # warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
6
6
  self.class_eval <<-EOM
7
+ def #{new_name} *args
8
+ Minitest::Expectation.new(self, Minitest::Spec.current).#{new_name}(*args)
9
+ end
10
+ EOM
11
+
12
+ Minitest::Expectation.class_eval <<-EOM, __FILE__, __LINE__ + 1
7
13
  def #{new_name} *args
8
14
  case
9
15
  when #{!!dont_flip} then
10
- Minitest::Spec.current.#{meth}(self, *args)
11
- when Proc === self then
12
- Minitest::Spec.current.#{meth}(*args, &self)
16
+ ctx.#{meth}(target, *args)
17
+ when Proc === target then
18
+ ctx.#{meth}(*args, &target)
13
19
  else
14
- Minitest::Spec.current.#{meth}(args.first, self, *args[1..-1])
20
+ ctx.#{meth}(args.first, target, *args[1..-1])
15
21
  end
16
22
  end
17
23
  EOM
18
24
  end
19
25
  end
20
26
 
21
- module Kernel # :nodoc:
27
+ Minitest::Expectation = Struct.new :target, :ctx # :nodoc:
28
+
29
+ ##
30
+ # Kernel extensions for minitest
31
+
32
+ module Kernel
22
33
  ##
23
34
  # Describe a series of expectations for a given target +desc+.
24
35
  #
@@ -57,7 +68,7 @@ module Kernel # :nodoc:
57
68
  def describe desc, *additional_desc, &block # :doc:
58
69
  stack = Minitest::Spec.describe_stack
59
70
  name = [stack.last, desc, *additional_desc].compact.join("::")
60
- sclas = stack.last || if Class === self && is_a?(Minitest::Spec::DSL) then
71
+ sclas = stack.last || if Class === self && kind_of?(Minitest::Spec::DSL) then
61
72
  self
62
73
  else
63
74
  Minitest::Spec.spec_type desc, *additional_desc
@@ -133,7 +144,7 @@ class Minitest::Spec < Minitest::Test
133
144
  # spec_type("BlahController") # => Minitest::Spec::Rails
134
145
 
135
146
  def spec_type desc, *additional
136
- TYPES.find { |matcher, klass|
147
+ TYPES.find { |matcher, _klass|
137
148
  if matcher.respond_to? :call then
138
149
  matcher.call desc, *additional
139
150
  else
@@ -146,10 +157,7 @@ class Minitest::Spec < Minitest::Test
146
157
  Thread.current[:describe_stack] ||= []
147
158
  end
148
159
 
149
- ##
150
- # Returns the children of this spec.
151
-
152
- def children
160
+ def children # :nodoc:
153
161
  @children ||= []
154
162
  end
155
163
 
@@ -166,7 +174,7 @@ class Minitest::Spec < Minitest::Test
166
174
  #
167
175
  # Equivalent to Minitest::Test#setup.
168
176
 
169
- def before type = nil, &block
177
+ def before _type = nil, &block
170
178
  define_method :setup do
171
179
  super()
172
180
  self.instance_eval(&block)
@@ -180,7 +188,7 @@ class Minitest::Spec < Minitest::Test
180
188
  #
181
189
  # Equivalent to Minitest::Test#teardown.
182
190
 
183
- def after type = nil, &block
191
+ def after _type = nil, &block
184
192
  define_method :teardown do
185
193
  self.instance_eval(&block)
186
194
  super()
@@ -266,22 +274,19 @@ class Minitest::Spec < Minitest::Test
266
274
  name # Can't alias due to 1.8.7, not sure why
267
275
  end
268
276
 
269
- # :stopdoc:
270
- attr_reader :desc
277
+ attr_reader :desc # :nodoc:
271
278
  alias :specify :it
272
279
 
273
- module InstanceMethods
274
- def before_setup
280
+ module InstanceMethods # :nodoc:
281
+ def before_setup # :nodoc:
275
282
  super
276
283
  Thread.current[:current_spec] = self
277
284
  end
278
285
  end
279
286
 
280
- def self.extended obj
287
+ def self.extended obj # :nodoc:
281
288
  obj.send :include, InstanceMethods
282
289
  end
283
-
284
- # :startdoc:
285
290
  end
286
291
 
287
292
  extend DSL
@@ -94,7 +94,7 @@ module Minitest
94
94
  super
95
95
  end
96
96
 
97
- TEARDOWN_METHODS = %w{ before_teardown teardown after_teardown } # :nodoc:
97
+ TEARDOWN_METHODS = %w[ before_teardown teardown after_teardown ] # :nodoc:
98
98
 
99
99
  ##
100
100
  # Runs a single test with setup/teardown hooks.
@@ -202,15 +202,13 @@ module Minitest
202
202
  end # LifecycleHooks
203
203
 
204
204
  def capture_exceptions # :nodoc:
205
- begin
206
- yield
207
- rescue *PASSTHROUGH_EXCEPTIONS
208
- raise
209
- rescue Assertion => e
210
- self.failures << e
211
- rescue Exception => e
212
- self.failures << UnexpectedError.new(e)
213
- end
205
+ yield
206
+ rescue *PASSTHROUGH_EXCEPTIONS
207
+ raise
208
+ rescue Assertion => e
209
+ self.failures << e
210
+ rescue Exception => e
211
+ self.failures << UnexpectedError.new(e)
214
212
  end
215
213
 
216
214
  ##