minitest 5.11.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,220 @@
1
+ require "minitest" unless defined? Minitest::Runnable
2
+
3
+ module Minitest
4
+ ##
5
+ # Subclass Test to create your own tests. Typically you'll want a
6
+ # Test subclass per implementation class.
7
+ #
8
+ # See Minitest::Assertions
9
+
10
+ class Test < Runnable
11
+ require "minitest/assertions"
12
+ include Minitest::Assertions
13
+ include Minitest::Reportable
14
+
15
+ def class_name # :nodoc:
16
+ self.class.name # for Minitest::Reportable
17
+ end
18
+
19
+ PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, SystemExit] # :nodoc:
20
+
21
+ # :stopdoc:
22
+ class << self; attr_accessor :io_lock; end
23
+ self.io_lock = Mutex.new
24
+ # :startdoc:
25
+
26
+ ##
27
+ # Call this at the top of your tests when you absolutely
28
+ # positively need to have ordered tests. In doing so, you're
29
+ # admitting that you suck and your tests are weak.
30
+
31
+ def self.i_suck_and_my_tests_are_order_dependent!
32
+ class << self
33
+ undef_method :test_order if method_defined? :test_order
34
+ define_method :test_order do :alpha end
35
+ end
36
+ end
37
+
38
+ ##
39
+ # Make diffs for this Test use #pretty_inspect so that diff
40
+ # in assert_equal can have more details. NOTE: this is much slower
41
+ # than the regular inspect but much more usable for complex
42
+ # objects.
43
+
44
+ def self.make_my_diffs_pretty!
45
+ require "pp"
46
+
47
+ define_method :mu_pp, &:pretty_inspect
48
+ end
49
+
50
+ ##
51
+ # Call this at the top of your tests when you want to run your
52
+ # tests in parallel. In doing so, you're admitting that you rule
53
+ # and your tests are awesome.
54
+
55
+ def self.parallelize_me!
56
+ include Minitest::Parallel::Test
57
+ extend Minitest::Parallel::Test::ClassMethods
58
+ end
59
+
60
+ ##
61
+ # Returns all instance methods starting with "test_". Based on
62
+ # #test_order, the methods are either sorted, randomized
63
+ # (default), or run in parallel.
64
+
65
+ def self.runnable_methods
66
+ methods = methods_matching(/^test_/)
67
+
68
+ case self.test_order
69
+ when :random, :parallel then
70
+ max = methods.size
71
+ methods.sort.sort_by { rand max }
72
+ when :alpha, :sorted then
73
+ methods.sort
74
+ else
75
+ raise "Unknown test_order: #{self.test_order.inspect}"
76
+ end
77
+ end
78
+
79
+ ##
80
+ # Defines the order to run tests (:random by default). Override
81
+ # this or use a convenience method to change it for your tests.
82
+
83
+ def self.test_order
84
+ :random
85
+ end
86
+
87
+ TEARDOWN_METHODS = %w[ before_teardown teardown after_teardown ] # :nodoc:
88
+
89
+ ##
90
+ # Runs a single test with setup/teardown hooks.
91
+
92
+ def run
93
+ with_info_handler do
94
+ time_it do
95
+ capture_exceptions do
96
+ before_setup; setup; after_setup
97
+
98
+ self.send self.name
99
+ end
100
+
101
+ TEARDOWN_METHODS.each do |hook|
102
+ capture_exceptions do
103
+ self.send hook
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ Result.from self # per contract
110
+ end
111
+
112
+ ##
113
+ # Provides before/after hooks for setup and teardown. These are
114
+ # meant for library writers, NOT for regular test authors. See
115
+ # #before_setup for an example.
116
+
117
+ module LifecycleHooks
118
+
119
+ ##
120
+ # Runs before every test, before setup. This hook is meant for
121
+ # libraries to extend minitest. It is not meant to be used by
122
+ # test developers.
123
+ #
124
+ # As a simplistic example:
125
+ #
126
+ # module MyMinitestPlugin
127
+ # def before_setup
128
+ # super
129
+ # # ... stuff to do before setup is run
130
+ # end
131
+ #
132
+ # def after_setup
133
+ # # ... stuff to do after setup is run
134
+ # super
135
+ # end
136
+ #
137
+ # def before_teardown
138
+ # super
139
+ # # ... stuff to do before teardown is run
140
+ # end
141
+ #
142
+ # def after_teardown
143
+ # # ... stuff to do after teardown is run
144
+ # super
145
+ # end
146
+ # end
147
+ #
148
+ # class MiniTest::Test
149
+ # include MyMinitestPlugin
150
+ # end
151
+
152
+ def before_setup; end
153
+
154
+ ##
155
+ # Runs before every test. Use this to set up before each test
156
+ # run.
157
+
158
+ def setup; end
159
+
160
+ ##
161
+ # Runs before every test, after setup. This hook is meant for
162
+ # libraries to extend minitest. It is not meant to be used by
163
+ # test developers.
164
+ #
165
+ # See #before_setup for an example.
166
+
167
+ def after_setup; end
168
+
169
+ ##
170
+ # Runs after every test, before teardown. This hook is meant for
171
+ # libraries to extend minitest. It is not meant to be used by
172
+ # test developers.
173
+ #
174
+ # See #before_setup for an example.
175
+
176
+ def before_teardown; end
177
+
178
+ ##
179
+ # Runs after every test. Use this to clean up after each test
180
+ # run.
181
+
182
+ def teardown; end
183
+
184
+ ##
185
+ # Runs after every test, after teardown. This hook is meant for
186
+ # libraries to extend minitest. It is not meant to be used by
187
+ # test developers.
188
+ #
189
+ # See #before_setup for an example.
190
+
191
+ def after_teardown; end
192
+ end # LifecycleHooks
193
+
194
+ def capture_exceptions # :nodoc:
195
+ yield
196
+ rescue *PASSTHROUGH_EXCEPTIONS
197
+ raise
198
+ rescue Assertion => e
199
+ self.failures << e
200
+ rescue Exception => e
201
+ self.failures << UnexpectedError.new(e)
202
+ end
203
+
204
+ def with_info_handler &block # :nodoc:
205
+ t0 = Minitest.clock_time
206
+
207
+ handler = lambda do
208
+ warn "\nCurrent: %s#%s %.2fs" % [self.class, self.name, Minitest.clock_time - t0]
209
+ end
210
+
211
+ self.class.on_signal ::Minitest.info_signal, handler, &block
212
+ end
213
+
214
+ include LifecycleHooks
215
+ include Guard
216
+ extend Guard
217
+ end # Test
218
+ end
219
+
220
+ require "minitest/unit" unless defined?(MiniTest) # compatibility layer only
@@ -0,0 +1,45 @@
1
+ # :stopdoc:
2
+
3
+ unless defined?(Minitest) then
4
+ # all of this crap is just to avoid circular requires and is only
5
+ # needed if a user requires "minitest/unit" directly instead of
6
+ # "minitest/autorun", so we also warn
7
+
8
+ from = caller.reject { |s| s =~ /rubygems/ }.join("\n ")
9
+ warn "Warning: you should require 'minitest/autorun' instead."
10
+ warn %(Warning: or add 'gem "minitest"' before 'require "minitest/autorun"')
11
+ warn "From:\n #{from}"
12
+
13
+ module Minitest; end
14
+ MiniTest = Minitest # prevents minitest.rb from requiring back to us
15
+ require "minitest"
16
+ end
17
+
18
+ MiniTest = Minitest unless defined?(MiniTest)
19
+
20
+ module Minitest
21
+ class Unit
22
+ VERSION = Minitest::VERSION
23
+ class TestCase < Minitest::Test
24
+ def self.inherited klass # :nodoc:
25
+ from = caller.first
26
+ warn "MiniTest::Unit::TestCase is now Minitest::Test. From #{from}"
27
+ super
28
+ end
29
+ end
30
+
31
+ def self.autorun # :nodoc:
32
+ from = caller.first
33
+ warn "MiniTest::Unit.autorun is now Minitest.autorun. From #{from}"
34
+ Minitest.autorun
35
+ end
36
+
37
+ def self.after_tests &b # :nodoc:
38
+ from = caller.first
39
+ warn "MiniTest::Unit.after_tests is now Minitest.after_run. From #{from}"
40
+ Minitest.after_run(&b)
41
+ end
42
+ end
43
+ end
44
+
45
+ # :startdoc:
@@ -0,0 +1,102 @@
1
+ require "tempfile"
2
+ require "stringio"
3
+ require "minitest/autorun"
4
+
5
+ class Minitest::Test
6
+ def clean s
7
+ s.gsub(/^ {6}/, "")
8
+ end
9
+ end
10
+
11
+ class FakeNamedTest < Minitest::Test
12
+ @@count = 0
13
+
14
+ def self.name
15
+ @fake_name ||= begin
16
+ @@count += 1
17
+ "FakeNamedTest%02d" % @@count
18
+ end
19
+ end
20
+ end
21
+
22
+ class MetaMetaMetaTestCase < Minitest::Test
23
+ attr_accessor :reporter, :output, :tu
24
+
25
+ def run_tu_with_fresh_reporter flags = %w[--seed 42]
26
+ options = Minitest.process_args flags
27
+
28
+ @output = StringIO.new("".encode('UTF-8'))
29
+
30
+ self.reporter = Minitest::CompositeReporter.new
31
+ reporter << Minitest::SummaryReporter.new(@output, options)
32
+ reporter << Minitest::ProgressReporter.new(@output, options)
33
+
34
+ reporter.start
35
+
36
+ yield(reporter) if block_given?
37
+
38
+ @tus ||= [@tu]
39
+ @tus.each do |tu|
40
+ Minitest::Runnable.runnables.delete tu
41
+
42
+ tu.run reporter, options
43
+ end
44
+
45
+ reporter.report
46
+ end
47
+
48
+ def first_reporter
49
+ reporter.reporters.first
50
+ end
51
+
52
+ def assert_report expected, flags = %w[--seed 42], &block
53
+ header = clean <<-EOM
54
+ Run options: #{flags.map { |s| s =~ /\|/ ? s.inspect : s }.join " "}
55
+
56
+ # Running:
57
+
58
+ EOM
59
+
60
+ run_tu_with_fresh_reporter flags, &block
61
+
62
+ output = normalize_output @output.string.dup
63
+
64
+ assert_equal header + expected, output
65
+ end
66
+
67
+ def normalize_output output
68
+ output.sub!(/Finished in .*/, "Finished in 0.00")
69
+ output.sub!(/Loaded suite .*/, "Loaded suite blah")
70
+
71
+ output.gsub!(/FakeNamedTest\d+/, "FakeNamedTestXX")
72
+ output.gsub!(/ = \d+.\d\d s = /, " = 0.00 s = ")
73
+ output.gsub!(/0x[A-Fa-f0-9]+/, "0xXXX")
74
+ output.gsub!(/ +$/, "")
75
+
76
+ if windows? then
77
+ output.gsub!(/\[(?:[A-Za-z]:)?[^\]:]+:\d+\]/, "[FILE:LINE]")
78
+ output.gsub!(/^(\s+)(?:[A-Za-z]:)?[^:]+:\d+:in/, '\1FILE:LINE:in')
79
+ else
80
+ output.gsub!(/\[[^\]:]+:\d+\]/, "[FILE:LINE]")
81
+ output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in')
82
+ end
83
+
84
+ output
85
+ end
86
+
87
+ def restore_env
88
+ old_value = ENV["MT_NO_SKIP_MSG"]
89
+ ENV.delete "MT_NO_SKIP_MSG"
90
+
91
+ yield
92
+ ensure
93
+ ENV["MT_NO_SKIP_MSG"] = old_value
94
+ end
95
+
96
+ def setup
97
+ super
98
+ srand 42
99
+ Minitest::Test.reset
100
+ @tu = nil
101
+ end
102
+ end
@@ -0,0 +1,137 @@
1
+ require "minitest/autorun"
2
+ require "minitest/benchmark"
3
+
4
+ ##
5
+ # Used to verify data:
6
+ # http://www.wolframalpha.com/examples/RegressionAnalysis.html
7
+
8
+ class TestMinitestBenchmark < Minitest::Test
9
+ def test_cls_bench_exp
10
+ assert_equal [2, 4, 8, 16, 32], Minitest::Benchmark.bench_exp(2, 32, 2)
11
+ end
12
+
13
+ def test_cls_bench_linear
14
+ assert_equal [2, 4, 6, 8, 10], Minitest::Benchmark.bench_linear(2, 10, 2)
15
+ end
16
+
17
+ def test_cls_runnable_methods
18
+ assert_equal [], Minitest::Benchmark.runnable_methods
19
+
20
+ c = Class.new(Minitest::Benchmark) do
21
+ def bench_blah
22
+ end
23
+ end
24
+
25
+ assert_equal ["bench_blah"], c.runnable_methods
26
+ end
27
+
28
+ def test_cls_bench_range
29
+ assert_equal [1, 10, 100, 1_000, 10_000], Minitest::Benchmark.bench_range
30
+ end
31
+
32
+ def test_fit_exponential_clean
33
+ x = [1.0, 2.0, 3.0, 4.0, 5.0]
34
+ y = x.map { |n| 1.1 * Math.exp(2.1 * n) }
35
+
36
+ assert_fit :exponential, x, y, 1.0, 1.1, 2.1
37
+ end
38
+
39
+ def test_fit_exponential_noisy
40
+ x = [1.0, 1.9, 2.6, 3.4, 5.0]
41
+ y = [12, 10, 8.2, 6.9, 5.9]
42
+
43
+ # verified with Numbers and R
44
+ assert_fit :exponential, x, y, 0.95, 13.81148, -0.1820
45
+ end
46
+
47
+ def test_fit_logarithmic_clean
48
+ x = [1.0, 2.0, 3.0, 4.0, 5.0]
49
+ y = x.map { |n| 1.1 + 2.1 * Math.log(n) }
50
+
51
+ assert_fit :logarithmic, x, y, 1.0, 1.1, 2.1
52
+ end
53
+
54
+ def test_fit_logarithmic_noisy
55
+ x = [1.0, 2.0, 3.0, 4.0, 5.0]
56
+ # Generated with
57
+ # y = x.map { |n| jitter = 0.999 + 0.002 * rand; (Math.log(n) ) * jitter }
58
+ y = [0.0, 0.6935, 1.0995, 1.3873, 1.6097]
59
+
60
+ assert_fit :logarithmic, x, y, 0.95, 0, 1
61
+ end
62
+
63
+ def test_fit_constant_clean
64
+ x = (1..5).to_a
65
+ y = [5.0, 5.0, 5.0, 5.0, 5.0]
66
+
67
+ assert_fit :linear, x, y, nil, 5.0, 0
68
+ end
69
+
70
+ def test_fit_constant_noisy
71
+ x = (1..5).to_a
72
+ y = [1.0, 1.2, 1.0, 0.8, 1.0]
73
+
74
+ # verified in numbers and R
75
+ assert_fit :linear, x, y, nil, 1.12, -0.04
76
+ end
77
+
78
+ def test_fit_linear_clean
79
+ # y = m * x + b where m = 2.2, b = 3.1
80
+ x = (1..5).to_a
81
+ y = x.map { |n| 2.2 * n + 3.1 }
82
+
83
+ assert_fit :linear, x, y, 1.0, 3.1, 2.2
84
+ end
85
+
86
+ def test_fit_linear_noisy
87
+ x = [ 60, 61, 62, 63, 65]
88
+ y = [3.1, 3.6, 3.8, 4.0, 4.1]
89
+
90
+ # verified in numbers and R
91
+ assert_fit :linear, x, y, 0.8315, -7.9635, 0.1878
92
+ end
93
+
94
+ def test_fit_power_clean
95
+ # y = A x ** B, where B = b and A = e ** a
96
+ # if, A = 1, B = 2, then
97
+
98
+ x = [1.0, 2.0, 3.0, 4.0, 5.0]
99
+ y = [1.0, 4.0, 9.0, 16.0, 25.0]
100
+
101
+ assert_fit :power, x, y, 1.0, 1.0, 2.0
102
+ end
103
+
104
+ def test_fit_power_noisy
105
+ # from www.engr.uidaho.edu/thompson/courses/ME330/lecture/least_squares.html
106
+ x = [10, 12, 15, 17, 20, 22, 25, 27, 30, 32, 35]
107
+ y = [95, 105, 125, 141, 173, 200, 253, 298, 385, 459, 602]
108
+
109
+ # verified in numbers
110
+ assert_fit :power, x, y, 0.90, 2.6217, 1.4556
111
+
112
+ # income to % of households below income amount
113
+ # http://library.wolfram.com/infocenter/Conferences/6461/PowerLaws.nb
114
+ x = [15_000, 25_000, 35_000, 50_000, 75_000, 100_000]
115
+ y = [0.154, 0.283, 0.402, 0.55, 0.733, 0.843]
116
+
117
+ # verified in numbers
118
+ assert_fit :power, x, y, 0.96, 3.119e-5, 0.8959
119
+ end
120
+
121
+ def assert_fit msg, x, y, fit, exp_a, exp_b
122
+ bench = Minitest::Benchmark.new :blah
123
+
124
+ a, b, rr = bench.send "fit_#{msg}", x, y
125
+
126
+ assert_operator rr, :>=, fit if fit
127
+ assert_in_delta exp_a, a
128
+ assert_in_delta exp_b, b
129
+ end
130
+ end
131
+
132
+ describe "my class Bench" do
133
+ klass = self
134
+ it "should provide bench methods" do
135
+ klass.must_respond_to :bench
136
+ end
137
+ end