spec 5.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.autotest +34 -0
- data/.gitignore +3 -0
- data/History.txt +911 -0
- data/Manifest.txt +26 -0
- data/README.txt +497 -0
- data/Rakefile +214 -0
- data/design_rationale.rb +52 -0
- data/lib/hoe/minitest.rb +26 -0
- data/lib/minitest/assertions.rb +649 -0
- data/lib/minitest/autorun.rb +12 -0
- data/lib/minitest/benchmark.rb +423 -0
- data/lib/minitest/expectations.rb +268 -0
- data/lib/minitest/hell.rb +11 -0
- data/lib/minitest/mock.rb +220 -0
- data/lib/minitest/parallel_each.rb +120 -0
- data/lib/minitest/pride.rb +4 -0
- data/lib/minitest/pride_plugin.rb +143 -0
- data/lib/minitest/spec.rb +292 -0
- data/lib/minitest/test.rb +272 -0
- data/lib/minitest/unit.rb +45 -0
- data/lib/minitest.rb +839 -0
- data/lib/spec.rb +3 -0
- data/readme.md +7 -0
- data/release_notes.md +49 -0
- data/spec.gemspec +36 -0
- data/test/manual/appium.rb +14 -0
- data/test/manual/appium_after_last.rb +24 -0
- data/test/manual/appium_before_first.rb +23 -0
- data/test/manual/assert.rb +61 -0
- data/test/manual/before_first_0.rb +27 -0
- data/test/manual/before_first_1.rb +29 -0
- data/test/manual/debug.rb +37 -0
- data/test/manual/do_end.rb +31 -0
- data/test/manual/raise.rb +61 -0
- data/test/manual/run2.rb +74 -0
- data/test/manual/run3.rb +91 -0
- data/test/manual/setup.rb +13 -0
- data/test/manual/simple.rb +19 -0
- data/test/manual/simple2.rb +20 -0
- data/test/manual/t.rb +11 -0
- data/test/manual/trace.rb +19 -0
- data/test/manual/trace2.rb +15 -0
- data/test/minitest/metametameta.rb +78 -0
- data/test/minitest/test_helper.rb +20 -0
- data/test/minitest/test_minitest_benchmark.rb +131 -0
- data/test/minitest/test_minitest_mock.rb +490 -0
- data/test/minitest/test_minitest_reporter.rb +270 -0
- data/test/minitest/test_minitest_spec.rb +794 -0
- data/test/minitest/test_minitest_unit.rb +1846 -0
- metadata +147 -0
@@ -0,0 +1,78 @@
|
|
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 MetaMetaMetaTestCase < Minitest::Test
|
12
|
+
attr_accessor :reporter, :output, :tu
|
13
|
+
|
14
|
+
def run_tu_with_fresh_reporter flags = %w[--seed 42]
|
15
|
+
options = Minitest.process_args flags
|
16
|
+
|
17
|
+
@output = StringIO.new("")
|
18
|
+
|
19
|
+
self.reporter = Minitest::CompositeReporter.new
|
20
|
+
reporter << Minitest::SummaryReporter.new(@output, options)
|
21
|
+
reporter << Minitest::ProgressReporter.new(@output, options)
|
22
|
+
|
23
|
+
reporter.start
|
24
|
+
|
25
|
+
@tus ||= [@tu]
|
26
|
+
@tus.each do |tu|
|
27
|
+
Minitest::Runnable.runnables.delete tu
|
28
|
+
|
29
|
+
tu.run reporter, options
|
30
|
+
end
|
31
|
+
|
32
|
+
reporter.report
|
33
|
+
end
|
34
|
+
|
35
|
+
def first_reporter
|
36
|
+
reporter.reporters.first
|
37
|
+
end
|
38
|
+
|
39
|
+
def assert_report expected, flags = %w[--seed 42]
|
40
|
+
header = clean <<-EOM
|
41
|
+
Run options: #{flags.map { |s| s =~ /\|/ ? s.inspect : s }.join " "}
|
42
|
+
|
43
|
+
# Running:
|
44
|
+
|
45
|
+
EOM
|
46
|
+
|
47
|
+
run_tu_with_fresh_reporter flags
|
48
|
+
|
49
|
+
output = normalize_output @output.string.dup
|
50
|
+
|
51
|
+
assert_equal header + expected, output
|
52
|
+
end
|
53
|
+
|
54
|
+
def normalize_output output
|
55
|
+
output.sub!(/Finished in .*/, "Finished in 0.00")
|
56
|
+
output.sub!(/Loaded suite .*/, 'Loaded suite blah')
|
57
|
+
|
58
|
+
output.gsub!(/ = \d+.\d\d s = /, ' = 0.00 s = ')
|
59
|
+
output.gsub!(/0x[A-Fa-f0-9]+/, '0xXXX')
|
60
|
+
|
61
|
+
if windows? then
|
62
|
+
output.gsub!(/\[(?:[A-Za-z]:)?[^\]:]+:\d+\]/, '[FILE:LINE]')
|
63
|
+
output.gsub!(/^(\s+)(?:[A-Za-z]:)?[^:]+:\d+:in/, '\1FILE:LINE:in')
|
64
|
+
else
|
65
|
+
output.gsub!(/\[[^\]:]+:\d+\]/, '[FILE:LINE]')
|
66
|
+
output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in')
|
67
|
+
end
|
68
|
+
|
69
|
+
output
|
70
|
+
end
|
71
|
+
|
72
|
+
def setup
|
73
|
+
super
|
74
|
+
srand 42
|
75
|
+
Minitest::Test.reset
|
76
|
+
@tu = nil
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Disable fail on first failure
|
2
|
+
class Minitest::Runnable
|
3
|
+
def self.check_failures result, reporter
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
# Restore summary reporter's output
|
8
|
+
class Minitest::SummaryReporter < Minitest::StatisticsReporter
|
9
|
+
def start # :nodoc:
|
10
|
+
super
|
11
|
+
|
12
|
+
io.puts "Run options: #{options[:args]}"
|
13
|
+
io.puts
|
14
|
+
io.puts "# Running:"
|
15
|
+
io.puts
|
16
|
+
|
17
|
+
self.sync = io.respond_to? :"sync=" # stupid emacs
|
18
|
+
self.old_sync, io.sync = io.sync, true if self.sync
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/benchmark'
|
3
|
+
require 'minitest/test_helper'
|
4
|
+
|
5
|
+
##
|
6
|
+
# Used to verify data:
|
7
|
+
# http://www.wolframalpha.com/examples/RegressionAnalysis.html
|
8
|
+
|
9
|
+
class TestMinitestBenchmark < Minitest::Test
|
10
|
+
def test_cls_bench_exp
|
11
|
+
assert_equal [2, 4, 8, 16, 32], Minitest::Benchmark.bench_exp(2, 32, 2)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_cls_bench_linear
|
15
|
+
assert_equal [2, 4, 6, 8, 10], Minitest::Benchmark.bench_linear(2, 10, 2)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_cls_runnable_methods
|
19
|
+
assert_equal [], Minitest::Benchmark.runnable_methods
|
20
|
+
|
21
|
+
c = Class.new(Minitest::Benchmark) do
|
22
|
+
def bench_blah
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
assert_equal ["bench_blah"], c.runnable_methods
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_cls_bench_range
|
30
|
+
assert_equal [1, 10, 100, 1_000, 10_000], Minitest::Benchmark.bench_range
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_fit_exponential_clean
|
34
|
+
x = [1.0, 2.0, 3.0, 4.0, 5.0]
|
35
|
+
y = x.map { |n| 1.1 * Math.exp(2.1 * n) }
|
36
|
+
|
37
|
+
assert_fit :exponential, x, y, 1.0, 1.1, 2.1
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_fit_exponential_noisy
|
41
|
+
x = [1.0, 1.9, 2.6, 3.4, 5.0]
|
42
|
+
y = [12, 10, 8.2, 6.9, 5.9]
|
43
|
+
|
44
|
+
# verified with Numbers and R
|
45
|
+
assert_fit :exponential, x, y, 0.95, 13.81148, -0.1820
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_fit_logarithmic_clean
|
49
|
+
x = [1.0, 2.0, 3.0, 4.0, 5.0]
|
50
|
+
y = x.map { |n| 1.1 + 2.1 * Math.log(n) }
|
51
|
+
|
52
|
+
assert_fit :logarithmic, x, y, 1.0, 1.1, 2.1
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_fit_logarithmic_noisy
|
56
|
+
x = [1.0, 2.0, 3.0, 4.0, 5.0]
|
57
|
+
# Generated with
|
58
|
+
# y = x.map { |n| jitter = 0.999 + 0.002 * rand; (Math.log(n) ) * jitter }
|
59
|
+
y = [0.0, 0.6935, 1.0995, 1.3873, 1.6097]
|
60
|
+
|
61
|
+
assert_fit :logarithmic, x, y, 0.95, 0, 1
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_fit_constant_clean
|
65
|
+
x = (1..5).to_a
|
66
|
+
y = [5.0, 5.0, 5.0, 5.0, 5.0]
|
67
|
+
|
68
|
+
assert_fit :linear, x, y, nil, 5.0, 0
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_fit_constant_noisy
|
72
|
+
x = (1..5).to_a
|
73
|
+
y = [1.0, 1.2, 1.0, 0.8, 1.0]
|
74
|
+
|
75
|
+
# verified in numbers and R
|
76
|
+
assert_fit :linear, x, y, nil, 1.12, -0.04
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_fit_linear_clean
|
80
|
+
# y = m * x + b where m = 2.2, b = 3.1
|
81
|
+
x = (1..5).to_a
|
82
|
+
y = x.map { |n| 2.2 * n + 3.1 }
|
83
|
+
|
84
|
+
assert_fit :linear, x, y, 1.0, 3.1, 2.2
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_fit_linear_noisy
|
88
|
+
x = [ 60, 61, 62, 63, 65]
|
89
|
+
y = [3.1, 3.6, 3.8, 4.0, 4.1]
|
90
|
+
|
91
|
+
# verified in numbers and R
|
92
|
+
assert_fit :linear, x, y, 0.8315, -7.9635, 0.1878
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_fit_power_clean
|
96
|
+
# y = A x ** B, where B = b and A = e ** a
|
97
|
+
# if, A = 1, B = 2, then
|
98
|
+
|
99
|
+
x = [1.0, 2.0, 3.0, 4.0, 5.0]
|
100
|
+
y = [1.0, 4.0, 9.0, 16.0, 25.0]
|
101
|
+
|
102
|
+
assert_fit :power, x, y, 1.0, 1.0, 2.0
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_fit_power_noisy
|
106
|
+
# from www.engr.uidaho.edu/thompson/courses/ME330/lecture/least_squares.html
|
107
|
+
x = [10, 12, 15, 17, 20, 22, 25, 27, 30, 32, 35]
|
108
|
+
y = [95, 105, 125, 141, 173, 200, 253, 298, 385, 459, 602]
|
109
|
+
|
110
|
+
# verified in numbers
|
111
|
+
assert_fit :power, x, y, 0.90, 2.6217, 1.4556
|
112
|
+
|
113
|
+
# income to % of households below income amount
|
114
|
+
# http://library.wolfram.com/infocenter/Conferences/6461/PowerLaws.nb
|
115
|
+
x = [15000, 25000, 35000, 50000, 75000, 100000]
|
116
|
+
y = [0.154, 0.283, 0.402, 0.55, 0.733, 0.843]
|
117
|
+
|
118
|
+
# verified in numbers
|
119
|
+
assert_fit :power, x, y, 0.96, 3.119e-5, 0.8959
|
120
|
+
end
|
121
|
+
|
122
|
+
def assert_fit msg, x, y, fit, exp_a, exp_b
|
123
|
+
bench = Minitest::Benchmark.new :blah
|
124
|
+
|
125
|
+
a, b, rr = bench.send "fit_#{msg}", x, y
|
126
|
+
|
127
|
+
assert_operator rr, :>=, fit if fit
|
128
|
+
assert_in_delta exp_a, a
|
129
|
+
assert_in_delta exp_b, b
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,490 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/test_helper'
|
3
|
+
|
4
|
+
class TestMinitestMock < Minitest::Test
|
5
|
+
parallelize_me!
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@mock = Minitest::Mock.new.expect(:foo, nil)
|
9
|
+
@mock.expect(:meaning_of_life, 42)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_create_stub_method
|
13
|
+
assert_nil @mock.foo
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_allow_return_value_specification
|
17
|
+
assert_equal 42, @mock.meaning_of_life
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_blow_up_if_not_called
|
21
|
+
@mock.foo
|
22
|
+
|
23
|
+
util_verify_bad "expected meaning_of_life() => 42, got []"
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_not_blow_up_if_everything_called
|
27
|
+
@mock.foo
|
28
|
+
@mock.meaning_of_life
|
29
|
+
|
30
|
+
assert @mock.verify
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_allow_expectations_to_be_added_after_creation
|
34
|
+
@mock.expect(:bar, true)
|
35
|
+
assert @mock.bar
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_not_verify_if_new_expected_method_is_not_called
|
39
|
+
@mock.foo
|
40
|
+
@mock.meaning_of_life
|
41
|
+
@mock.expect(:bar, true)
|
42
|
+
|
43
|
+
util_verify_bad "expected bar() => true, got []"
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_blow_up_on_wrong_number_of_arguments
|
47
|
+
@mock.foo
|
48
|
+
@mock.meaning_of_life
|
49
|
+
@mock.expect(:sum, 3, [1, 2])
|
50
|
+
|
51
|
+
e = assert_raises ArgumentError do
|
52
|
+
@mock.sum
|
53
|
+
end
|
54
|
+
|
55
|
+
assert_equal "mocked method :sum expects 2 arguments, got 0", e.message
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_return_mock_does_not_raise
|
59
|
+
retval = Minitest::Mock.new
|
60
|
+
mock = Minitest::Mock.new
|
61
|
+
mock.expect(:foo, retval)
|
62
|
+
mock.foo
|
63
|
+
|
64
|
+
assert mock.verify
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_mock_args_does_not_raise
|
68
|
+
skip "non-opaque use of ==" if maglev?
|
69
|
+
|
70
|
+
arg = Minitest::Mock.new
|
71
|
+
mock = Minitest::Mock.new
|
72
|
+
mock.expect(:foo, nil, [arg])
|
73
|
+
mock.foo(arg)
|
74
|
+
|
75
|
+
assert mock.verify
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_set_expectation_on_special_methods
|
79
|
+
mock = Minitest::Mock.new
|
80
|
+
|
81
|
+
mock.expect :object_id, "received object_id"
|
82
|
+
assert_equal "received object_id", mock.object_id
|
83
|
+
|
84
|
+
mock.expect :respond_to_missing?, "received respond_to_missing?"
|
85
|
+
assert_equal "received respond_to_missing?", mock.respond_to_missing?
|
86
|
+
|
87
|
+
mock.expect :===, "received ==="
|
88
|
+
assert_equal "received ===", mock.===
|
89
|
+
|
90
|
+
mock.expect :inspect, "received inspect"
|
91
|
+
assert_equal "received inspect", mock.inspect
|
92
|
+
|
93
|
+
mock.expect :to_s, "received to_s"
|
94
|
+
assert_equal "received to_s", mock.to_s
|
95
|
+
|
96
|
+
mock.expect :public_send, "received public_send"
|
97
|
+
assert_equal "received public_send", mock.public_send
|
98
|
+
|
99
|
+
mock.expect :send, "received send"
|
100
|
+
assert_equal "received send", mock.send
|
101
|
+
|
102
|
+
assert mock.verify
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_expectations_can_be_satisfied_via_send
|
106
|
+
@mock.send :foo
|
107
|
+
@mock.send :meaning_of_life
|
108
|
+
|
109
|
+
assert @mock.verify
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_expectations_can_be_satisfied_via_public_send
|
113
|
+
skip if RUBY_VERSION < "1.9"
|
114
|
+
|
115
|
+
@mock.public_send :foo
|
116
|
+
@mock.public_send :meaning_of_life
|
117
|
+
|
118
|
+
assert @mock.verify
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_blow_up_on_wrong_arguments
|
122
|
+
@mock.foo
|
123
|
+
@mock.meaning_of_life
|
124
|
+
@mock.expect(:sum, 3, [1, 2])
|
125
|
+
|
126
|
+
e = assert_raises MockExpectationError do
|
127
|
+
@mock.sum(2, 4)
|
128
|
+
end
|
129
|
+
|
130
|
+
exp = "mocked method :sum called with unexpected arguments [2, 4]"
|
131
|
+
assert_equal exp, e.message
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_expect_with_non_array_args
|
135
|
+
e = assert_raises ArgumentError do
|
136
|
+
@mock.expect :blah, 3, false
|
137
|
+
end
|
138
|
+
|
139
|
+
assert_equal "args must be an array", e.message
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_respond_appropriately
|
143
|
+
assert @mock.respond_to?(:foo)
|
144
|
+
assert @mock.respond_to?(:foo, true)
|
145
|
+
assert @mock.respond_to?('foo')
|
146
|
+
assert !@mock.respond_to?(:bar)
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_no_method_error_on_unexpected_methods
|
150
|
+
e = assert_raises NoMethodError do
|
151
|
+
@mock.bar
|
152
|
+
end
|
153
|
+
|
154
|
+
expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
|
155
|
+
|
156
|
+
assert_equal expected, e.message
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_assign_per_mock_return_values
|
160
|
+
a = Minitest::Mock.new
|
161
|
+
b = Minitest::Mock.new
|
162
|
+
|
163
|
+
a.expect(:foo, :a)
|
164
|
+
b.expect(:foo, :b)
|
165
|
+
|
166
|
+
assert_equal :a, a.foo
|
167
|
+
assert_equal :b, b.foo
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_do_not_create_stub_method_on_new_mocks
|
171
|
+
a = Minitest::Mock.new
|
172
|
+
a.expect(:foo, :a)
|
173
|
+
|
174
|
+
assert !Minitest::Mock.new.respond_to?(:foo)
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_mock_is_a_blank_slate
|
178
|
+
@mock.expect :kind_of?, true, [Fixnum]
|
179
|
+
@mock.expect :==, true, [1]
|
180
|
+
|
181
|
+
assert @mock.kind_of?(Fixnum), "didn't mock :kind_of\?"
|
182
|
+
assert @mock == 1, "didn't mock :=="
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_verify_allows_called_args_to_be_loosely_specified
|
186
|
+
mock = Minitest::Mock.new
|
187
|
+
mock.expect :loose_expectation, true, [Integer]
|
188
|
+
mock.loose_expectation 1
|
189
|
+
|
190
|
+
assert mock.verify
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_verify_raises_with_strict_args
|
194
|
+
mock = Minitest::Mock.new
|
195
|
+
mock.expect :strict_expectation, true, [2]
|
196
|
+
|
197
|
+
e = assert_raises MockExpectationError do
|
198
|
+
mock.strict_expectation 1
|
199
|
+
end
|
200
|
+
|
201
|
+
exp = "mocked method :strict_expectation called with unexpected arguments [1]"
|
202
|
+
assert_equal exp, e.message
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_method_missing_empty
|
206
|
+
mock = Minitest::Mock.new
|
207
|
+
|
208
|
+
mock.expect :a, nil
|
209
|
+
|
210
|
+
mock.a
|
211
|
+
|
212
|
+
e = assert_raises MockExpectationError do
|
213
|
+
mock.a
|
214
|
+
end
|
215
|
+
|
216
|
+
assert_equal "No more expects available for :a: []", e.message
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_same_method_expects_are_verified_when_all_called
|
220
|
+
mock = Minitest::Mock.new
|
221
|
+
mock.expect :foo, nil, [:bar]
|
222
|
+
mock.expect :foo, nil, [:baz]
|
223
|
+
|
224
|
+
mock.foo :bar
|
225
|
+
mock.foo :baz
|
226
|
+
|
227
|
+
assert mock.verify
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_same_method_expects_blow_up_when_not_all_called
|
231
|
+
mock = Minitest::Mock.new
|
232
|
+
mock.expect :foo, nil, [:bar]
|
233
|
+
mock.expect :foo, nil, [:baz]
|
234
|
+
|
235
|
+
mock.foo :bar
|
236
|
+
|
237
|
+
e = assert_raises(MockExpectationError) { mock.verify }
|
238
|
+
|
239
|
+
exp = "expected foo(:baz) => nil, got [foo(:bar) => nil]"
|
240
|
+
|
241
|
+
assert_equal exp, e.message
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_verify_passes_when_mock_block_returns_true
|
245
|
+
mock = Minitest::Mock.new
|
246
|
+
mock.expect :foo, nil do
|
247
|
+
true
|
248
|
+
end
|
249
|
+
|
250
|
+
mock.foo
|
251
|
+
|
252
|
+
assert mock.verify
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_mock_block_is_passed_function_params
|
256
|
+
arg1, arg2, arg3 = :bar, [1,2,3], {:a => 'a'}
|
257
|
+
mock = Minitest::Mock.new
|
258
|
+
mock.expect :foo, nil do |a1, a2, a3|
|
259
|
+
a1 == arg1 &&
|
260
|
+
a2 == arg2 &&
|
261
|
+
a3 == arg3
|
262
|
+
end
|
263
|
+
|
264
|
+
mock.foo arg1, arg2, arg3
|
265
|
+
|
266
|
+
assert mock.verify
|
267
|
+
end
|
268
|
+
|
269
|
+
def test_verify_fails_when_mock_block_returns_false
|
270
|
+
mock = Minitest::Mock.new
|
271
|
+
mock.expect :foo, nil do
|
272
|
+
false
|
273
|
+
end
|
274
|
+
|
275
|
+
e = assert_raises(MockExpectationError) { mock.foo }
|
276
|
+
exp = "mocked method :foo failed block w/ []"
|
277
|
+
|
278
|
+
assert_equal exp, e.message
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_mock_block_throws_if_args_passed
|
282
|
+
mock = Minitest::Mock.new
|
283
|
+
|
284
|
+
e = assert_raises(ArgumentError) do
|
285
|
+
mock.expect :foo, nil, [:a, :b, :c] do
|
286
|
+
true
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
exp = "args ignored when block given"
|
291
|
+
|
292
|
+
assert_equal exp, e.message
|
293
|
+
end
|
294
|
+
|
295
|
+
def test_mock_returns_retval_when_called_with_block
|
296
|
+
mock = Minitest::Mock.new
|
297
|
+
mock.expect(:foo, 32) do
|
298
|
+
true
|
299
|
+
end
|
300
|
+
|
301
|
+
rs = mock.foo
|
302
|
+
|
303
|
+
assert_equal rs, 32
|
304
|
+
end
|
305
|
+
|
306
|
+
def util_verify_bad exp
|
307
|
+
e = assert_raises MockExpectationError do
|
308
|
+
@mock.verify
|
309
|
+
end
|
310
|
+
|
311
|
+
assert_equal exp, e.message
|
312
|
+
end
|
313
|
+
|
314
|
+
def test_mock_called_via_send
|
315
|
+
mock = Minitest::Mock.new
|
316
|
+
mock.expect(:foo, true)
|
317
|
+
|
318
|
+
mock.send :foo
|
319
|
+
mock.verify
|
320
|
+
end
|
321
|
+
|
322
|
+
def test_mock_called_via___send__
|
323
|
+
mock = Minitest::Mock.new
|
324
|
+
mock.expect(:foo, true)
|
325
|
+
|
326
|
+
mock.__send__ :foo
|
327
|
+
mock.verify
|
328
|
+
end
|
329
|
+
|
330
|
+
def test_mock_called_via_send_with_args
|
331
|
+
mock = Minitest::Mock.new
|
332
|
+
mock.expect(:foo, true, [1,2,3])
|
333
|
+
|
334
|
+
mock.send(:foo, 1, 2, 3)
|
335
|
+
mock.verify
|
336
|
+
end
|
337
|
+
|
338
|
+
end
|
339
|
+
|
340
|
+
require "minitest/metametameta"
|
341
|
+
|
342
|
+
class TestMinitestStub < Minitest::Test
|
343
|
+
parallelize_me!
|
344
|
+
|
345
|
+
def setup
|
346
|
+
super
|
347
|
+
Minitest::Test.reset
|
348
|
+
|
349
|
+
@tc = Minitest::Test.new 'fake tc'
|
350
|
+
@assertion_count = 1
|
351
|
+
end
|
352
|
+
|
353
|
+
def teardown
|
354
|
+
super
|
355
|
+
assert_equal @assertion_count, @tc.assertions
|
356
|
+
end
|
357
|
+
|
358
|
+
class Time
|
359
|
+
def self.now
|
360
|
+
24
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
def assert_stub val_or_callable
|
365
|
+
@assertion_count += 1
|
366
|
+
|
367
|
+
t = Time.now.to_i
|
368
|
+
|
369
|
+
Time.stub :now, val_or_callable do
|
370
|
+
@tc.assert_equal 42, Time.now
|
371
|
+
end
|
372
|
+
|
373
|
+
@tc.assert_operator Time.now.to_i, :>=, t
|
374
|
+
end
|
375
|
+
|
376
|
+
def test_stub_private_module_method
|
377
|
+
@assertion_count += 1
|
378
|
+
|
379
|
+
t0 = Time.now
|
380
|
+
|
381
|
+
self.stub :sleep, nil do
|
382
|
+
@tc.assert_nil sleep(10)
|
383
|
+
end
|
384
|
+
|
385
|
+
@tc.assert_operator Time.now - t0, :<=, 1
|
386
|
+
end
|
387
|
+
|
388
|
+
def test_stub_private_module_method_indirect
|
389
|
+
@assertion_count += 1
|
390
|
+
|
391
|
+
fail_clapper = Class.new do
|
392
|
+
def fail_clap
|
393
|
+
fail
|
394
|
+
:clap
|
395
|
+
end
|
396
|
+
end.new
|
397
|
+
|
398
|
+
fail_clapper.stub :fail, nil do |safe_clapper|
|
399
|
+
@tc.assert_equal :clap, safe_clapper.fail_clap # either form works
|
400
|
+
@tc.assert_equal :clap, fail_clapper.fail_clap # yay closures
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
def test_stub_public_module_method
|
405
|
+
Math.stub :log10, :stubbed do
|
406
|
+
@tc.assert_equal :stubbed, Math.log10(1000)
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
def test_stub_value
|
411
|
+
assert_stub 42
|
412
|
+
end
|
413
|
+
|
414
|
+
def test_stub_block
|
415
|
+
assert_stub lambda { 42 }
|
416
|
+
end
|
417
|
+
|
418
|
+
def test_stub_block_args
|
419
|
+
@assertion_count += 1
|
420
|
+
|
421
|
+
t = Time.now.to_i
|
422
|
+
|
423
|
+
Time.stub :now, lambda { |n| n * 2 } do
|
424
|
+
@tc.assert_equal 42, Time.now(21)
|
425
|
+
end
|
426
|
+
|
427
|
+
@tc.assert_operator Time.now.to_i, :>=, t
|
428
|
+
end
|
429
|
+
|
430
|
+
def test_stub_callable
|
431
|
+
obj = Object.new
|
432
|
+
|
433
|
+
def obj.call
|
434
|
+
42
|
435
|
+
end
|
436
|
+
|
437
|
+
assert_stub obj
|
438
|
+
end
|
439
|
+
|
440
|
+
def test_stub_yield_self
|
441
|
+
obj = "foo"
|
442
|
+
|
443
|
+
val = obj.stub :to_s, "bar" do |s|
|
444
|
+
s.to_s
|
445
|
+
end
|
446
|
+
|
447
|
+
@tc.assert_equal "bar", val
|
448
|
+
end
|
449
|
+
|
450
|
+
def test_dynamic_method
|
451
|
+
@assertion_count = 2
|
452
|
+
|
453
|
+
dynamic = Class.new do
|
454
|
+
def self.respond_to?(meth)
|
455
|
+
meth == :found
|
456
|
+
end
|
457
|
+
|
458
|
+
def self.method_missing(meth, *args, &block)
|
459
|
+
if meth == :found
|
460
|
+
false
|
461
|
+
else
|
462
|
+
super
|
463
|
+
end
|
464
|
+
end
|
465
|
+
end
|
466
|
+
|
467
|
+
val = dynamic.stub(:found, true) do |s|
|
468
|
+
s.found
|
469
|
+
end
|
470
|
+
|
471
|
+
@tc.assert_equal true, val
|
472
|
+
@tc.assert_equal false, dynamic.found
|
473
|
+
end
|
474
|
+
|
475
|
+
def test_mock_with_yield
|
476
|
+
mock = Minitest::Mock.new
|
477
|
+
mock.expect(:write, true) do
|
478
|
+
true
|
479
|
+
end
|
480
|
+
rs = nil
|
481
|
+
|
482
|
+
File.stub(:open, true, mock) do
|
483
|
+
File.open("foo.txt", "r") do |f|
|
484
|
+
rs = f.write
|
485
|
+
end
|
486
|
+
end
|
487
|
+
@tc.assert_equal true, rs
|
488
|
+
end
|
489
|
+
|
490
|
+
end
|