summaryrefslogtreecommitdiff
path: root/test/minitest
diff options
context:
space:
mode:
authorryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-12-01 05:33:32 +0000
committerryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-12-01 05:33:32 +0000
commitd264d3cc38696d8b66d7261ae75a96fb8bdf2729 (patch)
treec4db29fe9f04a410c985174f39bcb494adf9204d /test/minitest
parented22b0ecee668cbd58d469c05b10e74cd6f604ab (diff)
Imported minitest 2.0.0 r5952. Fixed test/unit.rb to work with changes
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29990 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/minitest')
-rw-r--r--test/minitest/test_minitest_benchmark.rb104
-rw-r--r--test/minitest/test_minitest_mock.rb (renamed from test/minitest/test_mini_mock.rb)49
-rw-r--r--test/minitest/test_minitest_spec.rb (renamed from test/minitest/test_mini_spec.rb)14
-rw-r--r--test/minitest/test_minitest_unit.rb (renamed from test/minitest/test_mini_test.rb)124
4 files changed, 206 insertions, 85 deletions
diff --git a/test/minitest/test_minitest_benchmark.rb b/test/minitest/test_minitest_benchmark.rb
new file mode 100644
index 0000000000..089d0b3ce0
--- /dev/null
+++ b/test/minitest/test_minitest_benchmark.rb
@@ -0,0 +1,104 @@
+############################################################
+# This file is imported from a different project.
+# DO NOT make modifications in this repo.
+# File a patch instead and assign it to Ryan Davis
+############################################################
+
+require 'minitest/autorun'
+require 'minitest/benchmark'
+
+##
+# Used to verify data:
+# http://www.wolframalpha.com/examples/RegressionAnalysis.html
+
+class TestMiniTestBenchmark < MiniTest::Unit::TestCase
+ def test_cls_bench_exp
+ assert_equal [2, 4, 8, 16, 32], self.class.bench_exp(2, 32, 2)
+ end
+
+ def test_cls_bench_linear
+ assert_equal [2, 4, 6, 8, 10], self.class.bench_linear(2, 10, 2)
+ end
+
+ def test_cls_benchmark_methods
+ assert_equal [], self.class.benchmark_methods
+
+ c = Class.new(MiniTest::Unit::TestCase) do
+ def bench_blah
+ end
+ end
+
+ assert_equal ["bench_blah"], c.benchmark_methods
+ end
+
+ def test_cls_bench_range
+ assert_equal [1, 10, 100, 1_000, 10_000], self.class.bench_range
+ end
+
+ def test_fit_exponential_clean
+ x = [1.0, 2.0, 3.0, 4.0, 5.0]
+ y = x.map { |n| 1.1 * Math.exp(2.1 * n) }
+
+ assert_fit :exponential, x, y, 1.0, 1.1, 2.1
+ end
+
+ def test_fit_exponential_noisy
+ x = [1.0, 1.9, 2.6, 3.4, 5.0]
+ y = [12, 10, 8.2, 6.9, 5.9]
+
+ # verified with Numbers and R
+ assert_fit :exponential, x, y, 0.95, 13.81148, -0.1820
+ end
+
+ def test_fit_linear_clean
+ # y = m * x + b where m = 2.2, b = 3.1
+ x = (1..5).to_a
+ y = x.map { |n| 2.2 * n + 3.1 }
+
+ assert_fit :linear, x, y, 1.0, 3.1, 2.2
+ end
+
+ def test_fit_linear_noisy
+ x = [ 60, 61, 62, 63, 65]
+ y = [3.1, 3.6, 3.8, 4.0, 4.1]
+
+ # verified in numbers and R
+ assert_fit :linear, x, y, 0.8315, -7.9635, 0.1878
+ end
+
+ def test_fit_power_clean
+ # y = A x ** B, where B = b and A = e ** a
+ # if, A = 1, B = 2, then
+
+ x = [1.0, 2.0, 3.0, 4.0, 5.0]
+ y = [1.0, 4.0, 9.0, 16.0, 25.0]
+
+ assert_fit :power, x, y, 1.0, 1.0, 2.0
+ end
+
+ def test_fit_power_noisy
+ # from www.engr.uidaho.edu/thompson/courses/ME330/lecture/least_squares.html
+ x = [10, 12, 15, 17, 20, 22, 25, 27, 30, 32, 35]
+ y = [95, 105, 125, 141, 173, 200, 253, 298, 385, 459, 602]
+
+ # verified in numbers
+ assert_fit :power, x, y, 0.90, 2.6217, 1.4556
+
+ # income to % of households below income amount
+ # http://library.wolfram.com/infocenter/Conferences/6461/PowerLaws.nb
+ x = [15000, 25000, 35000, 50000, 75000, 100000]
+ y = [0.154, 0.283, 0.402, 0.55, 0.733, 0.843]
+
+ # verified in numbers
+ assert_fit :power, x, y, 0.96, 3.119e-5, 0.8959
+ end
+
+ def assert_fit msg, x, y, fit, exp_a, exp_b
+ a, b, rr = send "fit_#{msg}", x, y
+
+ assert_operator rr, :>=, fit
+ assert_in_delta exp_a, a
+ assert_in_delta exp_b, b
+ end
+end
+
diff --git a/test/minitest/test_mini_mock.rb b/test/minitest/test_minitest_mock.rb
index 0f16ae930e..bb4279c7d1 100644
--- a/test/minitest/test_mini_mock.rb
+++ b/test/minitest/test_minitest_mock.rb
@@ -9,39 +9,39 @@ require 'minitest/unit'
MiniTest::Unit.autorun
-class TestMiniMock < MiniTest::Unit::TestCase
+class TestMiniTestMock < MiniTest::Unit::TestCase
def setup
@mock = MiniTest::Mock.new.expect(:foo, nil)
@mock.expect(:meaning_of_life, 42)
end
- def test_should_create_stub_method
+ def test_create_stub_method
assert_nil @mock.foo
end
- def test_should_allow_return_value_specification
+ def test_allow_return_value_specification
assert_equal 42, @mock.meaning_of_life
end
- def test_should_blow_up_if_not_called
+ def test_blow_up_if_not_called
@mock.foo
util_verify_bad
end
- def test_should_not_blow_up_if_everything_called
+ def test_not_blow_up_if_everything_called
@mock.foo
@mock.meaning_of_life
assert @mock.verify
end
- def test_should_allow_expectations_to_be_added_after_creation
+ def test_allow_expectations_to_be_added_after_creation
@mock.expect(:bar, true)
assert @mock.bar
end
- def test_should_not_verify_if_new_expected_method_is_not_called
+ def test_not_verify_if_new_expected_method_is_not_called
@mock.foo
@mock.meaning_of_life
@mock.expect(:bar, true)
@@ -49,13 +49,13 @@ class TestMiniMock < MiniTest::Unit::TestCase
util_verify_bad
end
- def test_should_not_verify_if_unexpected_method_is_called
+ def test_not_verify_if_unexpected_method_is_called
assert_raises NoMethodError do
@mock.unexpected
end
end
- def test_should_blow_up_on_wrong_number_of_arguments
+ def test_blow_up_on_wrong_number_of_arguments
@mock.foo
@mock.meaning_of_life
@mock.expect(:sum, 3, [1, 2])
@@ -65,7 +65,7 @@ class TestMiniMock < MiniTest::Unit::TestCase
end
end
- def test_should_blow_up_on_wrong_arguments
+ def test_blow_up_on_wrong_arguments
@mock.foo
@mock.meaning_of_life
@mock.expect(:sum, 3, [1, 2])
@@ -75,6 +75,35 @@ class TestMiniMock < MiniTest::Unit::TestCase
util_verify_bad
end
+ def test_respond_appropriately
+ assert @mock.respond_to?(:foo)
+ assert !@mock.respond_to?(:bar)
+ end
+
+ def test_no_method_error_on_unexpected_methods
+ assert_raises NoMethodError do
+ @mock.bar
+ end
+ end
+
+ def test_assign_per_mock_return_values
+ a = MiniTest::Mock.new
+ b = MiniTest::Mock.new
+
+ a.expect(:foo, :a)
+ b.expect(:foo, :b)
+
+ assert_equal :a, a.foo
+ assert_equal :b, b.foo
+ end
+
+ def test_do_not_create_stub_method_on_new_mocks
+ a = MiniTest::Mock.new
+ a.expect(:foo, :a)
+
+ assert !MiniTest::Mock.new.respond_to?(:foo)
+ end
+
def util_verify_bad
assert_raises MockExpectationError do
@mock.verify
diff --git a/test/minitest/test_mini_spec.rb b/test/minitest/test_minitest_spec.rb
index f2e30c66d0..733ce0f5c7 100644
--- a/test/minitest/test_mini_spec.rb
+++ b/test/minitest/test_minitest_spec.rb
@@ -203,7 +203,7 @@ end
class TestMeta < MiniTest::Unit::TestCase
def test_structure
- x = y = nil
+ x = y = z = nil
x = describe "top-level thingy" do
before {}
after {}
@@ -213,13 +213,23 @@ class TestMeta < MiniTest::Unit::TestCase
y = describe "inner thingy" do
before {}
it "inner-it" do end
+
+ z = describe "very inner thingy" do
+ before {}
+ it "inner-it" do end
+ end
end
end
+ assert_equal "top-level thingy", x.to_s
+ assert_equal "top-level thingy::inner thingy", y.to_s
+ assert_equal "top-level thingy::inner thingy::very inner thingy", z.to_s
+
top_methods = %w(setup teardown test_0001_top_level_it)
inner_methods = %w(setup test_0001_inner_it)
- assert_equal top_methods, x.instance_methods(false).sort.map {|o| o.to_s }
+ assert_equal top_methods, x.instance_methods(false).sort.map {|o| o.to_s }
assert_equal inner_methods, y.instance_methods(false).sort.map {|o| o.to_s }
+ assert_equal inner_methods, z.instance_methods(false).sort.map {|o| o.to_s }
end
end
diff --git a/test/minitest/test_mini_test.rb b/test/minitest/test_minitest_unit.rb
index 176f6ec26f..a8187f2275 100644
--- a/test/minitest/test_mini_test.rb
+++ b/test/minitest/test_minitest_unit.rb
@@ -10,35 +10,31 @@ require 'minitest/unit'
MiniTest::Unit.autorun
-module M; end
-class E < StandardError; include M; end
+module MyModule; end
+class AnError < StandardError; include MyModule; end
-class TestMiniTest < MiniTest::Unit::TestCase
+class TestMiniTestUnit < MiniTest::Unit::TestCase
pwd = Pathname.new(File.expand_path(Dir.pwd))
basedir = Pathname.new(File.expand_path(MiniTest::MINI_DIR)) + 'mini'
basedir = basedir.relative_path_from(pwd).to_s
MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}"
- BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:165:in `run_test_suites'",
- "#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
- "#{MINITEST_BASE_DIR}/test.rb:161:in `run_test_suites'",
+ BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
"#{MINITEST_BASE_DIR}/test.rb:158:in `each'",
- "#{MINITEST_BASE_DIR}/test.rb:158:in `run_test_suites'",
"#{MINITEST_BASE_DIR}/test.rb:139:in `run'",
"#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]
def assert_report expected = nil
- expected ||= "Test run options: --seed 42
+ expected ||= "Run options: --seed 42
+
+# Running tests:
-Loaded suite blah
-Started
.
-Finished in 0.00
-1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
+Finished tests in 0.00
-Test run options: --seed 42
+1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
"
- output = @output.string.sub(/Finished in .*/, "Finished in 0.00")
+ output = @output.string.sub(/Finished tests in .*/, "Finished tests in 0.00")
output.sub!(/Loaded suite .*/, 'Loaded suite blah')
output.sub!(/^(\s+)(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+:/o, '\1FILE:LINE:')
output.sub!(/\[(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+\]/o, '[FILE:LINE]')
@@ -51,7 +47,6 @@ Test run options: --seed 42
@tu = MiniTest::Unit.new
@output = StringIO.new("")
MiniTest::Unit.output = @output
- assert_equal [0, 0], @tu.run_test_suites
end
def teardown
@@ -151,18 +146,6 @@ Test run options: --seed 42
assert_match(/^Exception.*Oh no again!/m, @tu.report.first)
end
- def test_class_run_test_suites
- tc = Class.new(MiniTest::Unit::TestCase) do
- def test_something
- assert true
- end
- end
-
- Object.const_set(:ATestCase, tc)
-
- assert_equal [1, 1], @tu.run_test_suites
- end
-
def test_filter_backtrace
# this is a semi-lame mix of relative paths.
# I cheated by making the autotest parts not have ./
@@ -218,14 +201,15 @@ Test run options: --seed 42
Object.const_set(:ATestCase, tc)
- @tu.run %w[-s 42]
+ @tu.run %w[--seed 42]
+
+ expected = "Run options: --seed 42
- expected = "Test run options: --seed 42
+# Running tests:
-Loaded suite blah
-Started
E.
-Finished in 0.00
+
+Finished tests in 0.00
1) Error:
test_error(ATestCase):
@@ -233,8 +217,6 @@ RuntimeError: unhandled exception
FILE:LINE:in `test_error'
2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
-
-Test run options: --seed 42
"
assert_report expected
end
@@ -252,14 +234,15 @@ Test run options: --seed 42
Object.const_set(:ATestCase, tc)
- @tu.run %w[-s 42]
+ @tu.run %w[--seed 42]
- expected = "Test run options: --seed 42
+ expected = "Run options: --seed 42
+
+# Running tests:
-Loaded suite blah
-Started
E
-Finished in 0.00
+
+Finished tests in 0.00
1) Error:
test_something(ATestCase):
@@ -267,8 +250,6 @@ RuntimeError: unhandled exception
FILE:LINE:in `teardown'
1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
-
-Test run options: --seed 42
"
assert_report expected
end
@@ -286,22 +267,21 @@ Test run options: --seed 42
Object.const_set(:ATestCase, tc)
- @tu.run %w[-s 42]
+ @tu.run %w[--seed 42]
+
+ expected = "Run options: --seed 42
- expected = "Test run options: --seed 42
+# Running tests:
-Loaded suite blah
-Started
F.
-Finished in 0.00
+
+Finished tests in 0.00
1) Failure:
test_failure(ATestCase) [FILE:LINE]:
Failed assertion, no message given.
2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
-
-Test run options: --seed 42
"
assert_report expected
end
@@ -319,18 +299,17 @@ Test run options: --seed 42
Object.const_set(:ATestCase, tc)
- @tu.run %w[-n /something/ -s 42]
+ @tu.run %w[--name /some|thing/ --seed 42]
+
+ expected = "Run options: --name \"/some|thing/\" --seed 42
- expected = "Test run options: --seed 42 --name \"/something/\"
+# Running tests:
-Loaded suite blah
-Started
.
-Finished in 0.00
-1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
+Finished tests in 0.00
-Test run options: --seed 42 --name \"/something/\"
+1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
"
assert_report expected
end
@@ -344,7 +323,7 @@ Test run options: --seed 42 --name \"/something/\"
Object.const_set(:ATestCase, tc)
- @tu.run %w[-s 42]
+ @tu.run %w[--seed 42]
assert_report
end
@@ -362,22 +341,21 @@ Test run options: --seed 42 --name \"/something/\"
Object.const_set(:ATestCase, tc)
- @tu.run %w[-s 42]
+ @tu.run %w[--seed 42]
- expected = "Test run options: --seed 42
+ expected = "Run options: --seed 42
+
+# Running tests:
-Loaded suite blah
-Started
S.
-Finished in 0.00
+
+Finished tests in 0.00
1) Skipped:
test_skip(ATestCase) [FILE:LINE]:
not yet
2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
-
-Test run options: --seed 42
"
assert_report expected
end
@@ -391,7 +369,7 @@ Test run options: --seed 42
end
end
-class TestMiniTestTestCase < MiniTest::Unit::TestCase
+class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
def setup
MiniTest::Unit::TestCase.reset
@@ -548,9 +526,9 @@ class TestMiniTestTestCase < MiniTest::Unit::TestCase
pattern = Object.new
def pattern.=~(other) false end
- def pattern.inspect; "<<Object>>" end
+ def pattern.inspect; "[Object]" end
- util_assert_triggered 'Expected <<Object>> to match 5.' do
+ util_assert_triggered 'Expected [Object] to match 5.' do
@tc.assert_match pattern, 5
end
end
@@ -663,8 +641,8 @@ class TestMiniTestTestCase < MiniTest::Unit::TestCase
end
def test_assert_raises_module
- @tc.assert_raises M do
- raise E
+ @tc.assert_raises MyModule do
+ raise AnError
end
end
@@ -736,13 +714,13 @@ FILE:LINE:in `test_assert_raises_triggered_different_msg'
def test_assert_raises_triggered_subclass
e = assert_raises MiniTest::Assertion do
@tc.assert_raises StandardError do
- raise E
+ raise AnError
end
end
expected = "[StandardError] exception expected, not
-Class: <E>
-Message: <\"E\">
+Class: <AnError>
+Message: <\"AnError\">
---Backtrace---
FILE:LINE:in `test_assert_raises_triggered_subclass'
---------------"
@@ -1023,9 +1001,9 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
pattern = Object.new
def pattern.=~(other) true end
- def pattern.inspect; "<<Object>>" end
+ def pattern.inspect; "[Object]" end
- util_assert_triggered 'Expected <<Object>> to not match 5.' do
+ util_assert_triggered 'Expected [Object] to not match 5.' do
@tc.refute_match pattern, 5
end
end