minitest 5.11.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ module Minitest
2
+ module Parallel #:nodoc:
3
+
4
+ ##
5
+ # The engine used to run multiple tests in parallel.
6
+
7
+ class Executor
8
+
9
+ ##
10
+ # The size of the pool of workers.
11
+
12
+ attr_reader :size
13
+
14
+ ##
15
+ # Create a parallel test executor of with +size+ workers.
16
+
17
+ def initialize size
18
+ @size = size
19
+ @queue = Queue.new
20
+ @pool = nil
21
+ end
22
+
23
+ ##
24
+ # Start the executor
25
+
26
+ def start
27
+ @pool = size.times.map {
28
+ Thread.new(@queue) do |queue|
29
+ Thread.current.abort_on_exception = true
30
+ while (job = queue.pop)
31
+ klass, method, reporter = job
32
+ reporter.synchronize { reporter.prerecord klass, method }
33
+ result = Minitest.run_one_method klass, method
34
+ reporter.synchronize { reporter.record result }
35
+ end
36
+ end
37
+ }
38
+ end
39
+
40
+ ##
41
+ # Add a job to the queue
42
+
43
+ def << work; @queue << work; end
44
+
45
+ ##
46
+ # Shuts down the pool of workers by signalling them to quit and
47
+ # waiting for them all to finish what they're currently working
48
+ # on.
49
+
50
+ def shutdown
51
+ size.times { @queue << nil }
52
+ @pool.each(&:join)
53
+ end
54
+ end
55
+
56
+ module Test # :nodoc:
57
+ def _synchronize; Minitest::Test.io_lock.synchronize { yield }; end # :nodoc:
58
+
59
+ module ClassMethods # :nodoc:
60
+ def run_one_method klass, method_name, reporter
61
+ Minitest.parallel_executor << [klass, method_name, reporter]
62
+ end
63
+
64
+ def test_order
65
+ :parallel
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,4 @@
1
+ require "minitest"
2
+
3
+ Minitest.load_plugins
4
+ Minitest::PrideIO.pride!
@@ -0,0 +1,142 @@
1
+ require "minitest"
2
+
3
+ module Minitest
4
+ def self.plugin_pride_options opts, _options # :nodoc:
5
+ opts.on "-p", "--pride", "Pride. Show your testing pride!" do
6
+ PrideIO.pride!
7
+ end
8
+ end
9
+
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]
14
+
15
+ self.reporter.reporters.grep(Minitest::Reporter).each do |rep|
16
+ rep.io = io if rep.io.tty?
17
+ end
18
+ end
19
+ end
20
+
21
+ ##
22
+ # Show your testing pride!
23
+
24
+ class PrideIO
25
+ ##
26
+ # Activate the pride plugin. Called from both -p option and minitest/pride
27
+
28
+ def self.pride!
29
+ @pride = true
30
+ end
31
+
32
+ ##
33
+ # Are we showing our testing pride?
34
+
35
+ def self.pride?
36
+ @pride ||= false
37
+ end
38
+
39
+ # Start an escape sequence
40
+ ESC = "\e["
41
+
42
+ # End the escape sequence
43
+ NND = "#{ESC}0m"
44
+
45
+ # The IO we're going to pipe through.
46
+ attr_reader :io
47
+
48
+ def initialize io # :nodoc:
49
+ @io = io
50
+ # stolen from /System/Library/Perl/5.10.0/Term/ANSIColor.pm
51
+ # also reference http://en.wikipedia.org/wiki/ANSI_escape_code
52
+ @colors ||= (31..36).to_a
53
+ @size = @colors.size
54
+ @index = 0
55
+ end
56
+
57
+ ##
58
+ # Wrap print to colorize the output.
59
+
60
+ def print o
61
+ case o
62
+ when "." then
63
+ io.print pride o
64
+ when "E", "F" then
65
+ io.print "#{ESC}41m#{ESC}37m#{o}#{NND}"
66
+ when "S" then
67
+ io.print pride o
68
+ else
69
+ io.print o
70
+ end
71
+ end
72
+
73
+ def puts *o # :nodoc:
74
+ o.map! { |s|
75
+ s.to_s.sub(/Finished/) {
76
+ @index = 0
77
+ "Fabulous run".split(//).map { |c|
78
+ pride(c)
79
+ }.join
80
+ }
81
+ }
82
+
83
+ io.puts(*o)
84
+ end
85
+
86
+ ##
87
+ # Color a string.
88
+
89
+ def pride string
90
+ string = "*" if string == "."
91
+ c = @colors[@index % @size]
92
+ @index += 1
93
+ "#{ESC}#{c}m#{string}#{NND}"
94
+ end
95
+
96
+ def method_missing msg, *args # :nodoc:
97
+ io.send(msg, *args)
98
+ end
99
+ end
100
+
101
+ ##
102
+ # If you thought the PrideIO was colorful...
103
+ #
104
+ # (Inspired by lolcat, but with clean math)
105
+
106
+ class PrideLOL < PrideIO
107
+ PI_3 = Math::PI / 3 # :nodoc:
108
+
109
+ def initialize io # :nodoc:
110
+ # walk red, green, and blue around a circle separated by equal thirds.
111
+ #
112
+ # To visualize, type this into wolfram-alpha:
113
+ #
114
+ # plot (3*sin(x)+3), (3*sin(x+2*pi/3)+3), (3*sin(x+4*pi/3)+3)
115
+
116
+ # 6 has wide pretty gradients. 3 == lolcat, about half the width
117
+ @colors = (0...(6 * 7)).map { |n|
118
+ n *= 1.0 / 6
119
+ 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.
126
+
127
+ 36 * r + 6 * g + b + 16
128
+ }
129
+
130
+ super
131
+ end
132
+
133
+ ##
134
+ # Make the string even more colorful. Damnit.
135
+
136
+ def pride string
137
+ c = @colors[@index % @size]
138
+ @index += 1
139
+ "#{ESC}38;5;#{c}m#{string}#{NND}"
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,331 @@
1
+ require "minitest/test"
2
+
3
+ class Module # :nodoc:
4
+ def infect_an_assertion meth, new_name, dont_flip = false # :nodoc:
5
+ block = dont_flip == :block
6
+ dont_flip = false if block
7
+
8
+ # warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
9
+ self.class_eval <<-EOM, __FILE__, __LINE__ + 1
10
+ def #{new_name} *args
11
+ Minitest::Expectation.new(self, Minitest::Spec.current).#{new_name}(*args)
12
+ end
13
+ EOM
14
+
15
+ Minitest::Expectation.class_eval <<-EOM, __FILE__, __LINE__ + 1
16
+ def #{new_name} *args
17
+ case
18
+ when #{!!dont_flip} then
19
+ ctx.#{meth}(target, *args)
20
+ when #{block} && Proc === target then
21
+ ctx.#{meth}(*args, &target)
22
+ else
23
+ ctx.#{meth}(args.first, target, *args[1..-1])
24
+ end
25
+ end
26
+ EOM
27
+ end
28
+ end
29
+
30
+ Minitest::Expectation = Struct.new :target, :ctx # :nodoc:
31
+
32
+ ##
33
+ # Kernel extensions for minitest
34
+
35
+ module Kernel
36
+ ##
37
+ # Describe a series of expectations for a given target +desc+.
38
+ #
39
+ # Defines a test class subclassing from either Minitest::Spec or
40
+ # from the surrounding describe's class. The surrounding class may
41
+ # subclass Minitest::Spec manually in order to easily share code:
42
+ #
43
+ # class MySpec < Minitest::Spec
44
+ # # ... shared code ...
45
+ # end
46
+ #
47
+ # class TestStuff < MySpec
48
+ # it "does stuff" do
49
+ # # shared code available here
50
+ # end
51
+ # describe "inner stuff" do
52
+ # it "still does stuff" do
53
+ # # ...and here
54
+ # end
55
+ # end
56
+ # end
57
+ #
58
+ # For more information on getting started with writing specs, see:
59
+ #
60
+ # http://www.rubyinside.com/a-minitestspec-tutorial-elegant-spec-style-testing-that-comes-with-ruby-5354.html
61
+ #
62
+ # For some suggestions on how to improve your specs, try:
63
+ #
64
+ # http://betterspecs.org
65
+ #
66
+ # but do note that several items there are debatable or specific to
67
+ # rspec.
68
+ #
69
+ # For more information about expectations, see Minitest::Expectations.
70
+
71
+ def describe desc, *additional_desc, &block # :doc:
72
+ stack = Minitest::Spec.describe_stack
73
+ name = [stack.last, desc, *additional_desc].compact.join("::")
74
+ sclas = stack.last || if Class === self && kind_of?(Minitest::Spec::DSL) then
75
+ self
76
+ else
77
+ Minitest::Spec.spec_type desc, *additional_desc
78
+ end
79
+
80
+ cls = sclas.create name, desc
81
+
82
+ stack.push cls
83
+ cls.class_eval(&block)
84
+ stack.pop
85
+ cls
86
+ end
87
+ private :describe
88
+ end
89
+
90
+ ##
91
+ # Minitest::Spec -- The faster, better, less-magical spec framework!
92
+ #
93
+ # For a list of expectations, see Minitest::Expectations.
94
+
95
+ class Minitest::Spec < Minitest::Test
96
+
97
+ def self.current # :nodoc:
98
+ Thread.current[:current_spec]
99
+ end
100
+
101
+ def initialize name # :nodoc:
102
+ super
103
+ Thread.current[:current_spec] = self
104
+ end
105
+
106
+ ##
107
+ # Oh look! A Minitest::Spec::DSL module! Eat your heart out DHH.
108
+
109
+ module DSL
110
+ ##
111
+ # Contains pairs of matchers and Spec classes to be used to
112
+ # calculate the superclass of a top-level describe. This allows for
113
+ # automatically customizable spec types.
114
+ #
115
+ # See: register_spec_type and spec_type
116
+
117
+ TYPES = [[//, Minitest::Spec]]
118
+
119
+ ##
120
+ # Register a new type of spec that matches the spec's description.
121
+ # This method can take either a Regexp and a spec class or a spec
122
+ # class and a block that takes the description and returns true if
123
+ # it matches.
124
+ #
125
+ # Eg:
126
+ #
127
+ # register_spec_type(/Controller$/, Minitest::Spec::Rails)
128
+ #
129
+ # or:
130
+ #
131
+ # register_spec_type(Minitest::Spec::RailsModel) do |desc|
132
+ # desc.superclass == ActiveRecord::Base
133
+ # end
134
+
135
+ def register_spec_type *args, &block
136
+ if block then
137
+ matcher, klass = block, args.first
138
+ else
139
+ matcher, klass = *args
140
+ end
141
+ TYPES.unshift [matcher, klass]
142
+ end
143
+
144
+ ##
145
+ # Figure out the spec class to use based on a spec's description. Eg:
146
+ #
147
+ # spec_type("BlahController") # => Minitest::Spec::Rails
148
+
149
+ def spec_type desc, *additional
150
+ TYPES.find { |matcher, _klass|
151
+ if matcher.respond_to? :call then
152
+ matcher.call desc, *additional
153
+ else
154
+ matcher === desc.to_s
155
+ end
156
+ }.last
157
+ end
158
+
159
+ def describe_stack # :nodoc:
160
+ Thread.current[:describe_stack] ||= []
161
+ end
162
+
163
+ def children # :nodoc:
164
+ @children ||= []
165
+ end
166
+
167
+ def nuke_test_methods! # :nodoc:
168
+ self.public_instance_methods.grep(/^test_/).each do |name|
169
+ self.send :undef_method, name
170
+ end
171
+ end
172
+
173
+ ##
174
+ # Define a 'before' action. Inherits the way normal methods should.
175
+ #
176
+ # NOTE: +type+ is ignored and is only there to make porting easier.
177
+ #
178
+ # Equivalent to Minitest::Test#setup.
179
+
180
+ def before _type = nil, &block
181
+ define_method :setup do
182
+ super()
183
+ self.instance_eval(&block)
184
+ end
185
+ end
186
+
187
+ ##
188
+ # Define an 'after' action. Inherits the way normal methods should.
189
+ #
190
+ # NOTE: +type+ is ignored and is only there to make porting easier.
191
+ #
192
+ # Equivalent to Minitest::Test#teardown.
193
+
194
+ def after _type = nil, &block
195
+ define_method :teardown do
196
+ self.instance_eval(&block)
197
+ super()
198
+ end
199
+ end
200
+
201
+ ##
202
+ # Define an expectation with name +desc+. Name gets morphed to a
203
+ # proper test method name. For some freakish reason, people who
204
+ # write specs don't like class inheritance, so this goes way out of
205
+ # its way to make sure that expectations aren't inherited.
206
+ #
207
+ # This is also aliased to #specify and doesn't require a +desc+ arg.
208
+ #
209
+ # Hint: If you _do_ want inheritance, use minitest/test. You can mix
210
+ # and match between assertions and expectations as much as you want.
211
+
212
+ def it desc = "anonymous", &block
213
+ block ||= proc { skip "(no tests defined)" }
214
+
215
+ @specs ||= 0
216
+ @specs += 1
217
+
218
+ name = "test_%04d_%s" % [ @specs, desc ]
219
+
220
+ undef_klasses = self.children.reject { |c| c.public_method_defined? name }
221
+
222
+ define_method name, &block
223
+
224
+ undef_klasses.each do |undef_klass|
225
+ undef_klass.send :undef_method, name
226
+ end
227
+
228
+ name
229
+ end
230
+
231
+ ##
232
+ # Essentially, define an accessor for +name+ with +block+.
233
+ #
234
+ # Why use let instead of def? I honestly don't know.
235
+
236
+ def let name, &block
237
+ name = name.to_s
238
+ pre, post = "let '#{name}' cannot ", ". Please use another name."
239
+ methods = Minitest::Spec.instance_methods.map(&:to_s) - %w[subject]
240
+ raise ArgumentError, "#{pre}begin with 'test'#{post}" if
241
+ name =~ /\Atest/
242
+ raise ArgumentError, "#{pre}override a method in Minitest::Spec#{post}" if
243
+ methods.include? name
244
+
245
+ define_method name do
246
+ @_memoized ||= {}
247
+ @_memoized.fetch(name) { |k| @_memoized[k] = instance_eval(&block) }
248
+ end
249
+ end
250
+
251
+ ##
252
+ # Another lazy man's accessor generator. Made even more lazy by
253
+ # setting the name for you to +subject+.
254
+
255
+ def subject &block
256
+ let :subject, &block
257
+ end
258
+
259
+ def create name, desc # :nodoc:
260
+ cls = Class.new(self) do
261
+ @name = name
262
+ @desc = desc
263
+
264
+ nuke_test_methods!
265
+ end
266
+
267
+ children << cls
268
+
269
+ cls
270
+ end
271
+
272
+ def name # :nodoc:
273
+ defined?(@name) ? @name : super
274
+ end
275
+
276
+ def to_s # :nodoc:
277
+ name # Can't alias due to 1.8.7, not sure why
278
+ end
279
+
280
+ attr_reader :desc # :nodoc:
281
+ alias :specify :it
282
+
283
+ ##
284
+ # Rdoc... why are you so dumb?
285
+
286
+ module InstanceMethods
287
+ ##
288
+ # Returns a value monad that has all of Expectations methods
289
+ # available to it.
290
+ #
291
+ # Also aliased to #value and #expect for your aesthetic pleasure:
292
+ #
293
+ # _(1 + 1).must_equal 2
294
+ # value(1 + 1).must_equal 2
295
+ # expect(1 + 1).must_equal 2
296
+ #
297
+ # This method of expectation-based testing is preferable to
298
+ # straight-expectation methods (on Object) because it stores its
299
+ # test context, bypassing our hacky use of thread-local variables.
300
+ #
301
+ # At some point, the methods on Object will be deprecated and then
302
+ # removed.
303
+
304
+ def _ value = nil, &block
305
+ Minitest::Expectation.new block || value, self
306
+ end
307
+
308
+ alias value _
309
+ alias expect _
310
+
311
+ def before_setup # :nodoc:
312
+ super
313
+ Thread.current[:current_spec] = self
314
+ end
315
+ end
316
+
317
+ def self.extended obj # :nodoc:
318
+ obj.send :include, InstanceMethods
319
+ end
320
+ end
321
+
322
+ extend DSL
323
+
324
+ TYPES = DSL::TYPES # :nodoc:
325
+ end
326
+
327
+ require "minitest/expectations"
328
+
329
+ class Object # :nodoc:
330
+ include Minitest::Expectations unless ENV["MT_NO_EXPECTATIONS"]
331
+ end