diff options
Diffstat (limited to 'activesupport/test/test_case_test.rb')
-rw-r--r-- | activesupport/test/test_case_test.rb | 180 |
1 files changed, 158 insertions, 22 deletions
diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb index 18228a2ac5..79b75a001a 100644 --- a/activesupport/test/test_case_test.rb +++ b/activesupport/test/test_case_test.rb @@ -1,4 +1,6 @@ -require 'abstract_unit' +# frozen_string_literal: true + +require "abstract_unit" class AssertDifferenceTest < ActiveSupport::TestCase def setup @@ -20,21 +22,21 @@ class AssertDifferenceTest < ActiveSupport::TestCase assert_equal true, assert_not(false) e = assert_raises(Minitest::Assertion) { assert_not true } - assert_equal 'Expected true to be nil or false', e.message + assert_equal "Expected true to be nil or false", e.message - e = assert_raises(Minitest::Assertion) { assert_not true, 'custom' } - assert_equal 'custom', e.message + e = assert_raises(Minitest::Assertion) { assert_not true, "custom" } + assert_equal "custom", e.message end def test_assert_no_difference_pass - assert_no_difference '@object.num' do + assert_no_difference "@object.num" do # ... end end def test_assert_no_difference_fail error = assert_raises(Minitest::Assertion) do - assert_no_difference '@object.num' do + assert_no_difference "@object.num" do @object.increment end end @@ -43,7 +45,7 @@ class AssertDifferenceTest < ActiveSupport::TestCase def test_assert_no_difference_with_message_fail error = assert_raises(Minitest::Assertion) do - assert_no_difference '@object.num', 'Object Changed' do + assert_no_difference "@object.num", "Object Changed" do @object.increment end end @@ -51,13 +53,13 @@ class AssertDifferenceTest < ActiveSupport::TestCase end def test_assert_difference - assert_difference '@object.num', +1 do + assert_difference "@object.num", +1 do @object.increment end end def test_assert_difference_retval - incremented = assert_difference '@object.num', +1 do + incremented = assert_difference "@object.num", +1 do @object.increment end @@ -65,40 +67,41 @@ class AssertDifferenceTest < ActiveSupport::TestCase end def test_assert_difference_with_implicit_difference - assert_difference '@object.num' do + assert_difference "@object.num" do @object.increment end end def test_arbitrary_expression - assert_difference '@object.num + 1', +2 do + assert_difference "@object.num + 1", +2 do @object.increment @object.increment end end def test_negative_differences - assert_difference '@object.num', -1 do + assert_difference "@object.num", -1 do @object.decrement end end def test_expression_is_evaluated_in_the_appropriate_scope silence_warnings do - local_scope = local_scope = 'foo' - assert_difference('local_scope; @object.num') { @object.increment } + local_scope = "foo" + local_scope = local_scope # to suppress unused variable warning + assert_difference("local_scope; @object.num") { @object.increment } end end def test_array_of_expressions - assert_difference [ '@object.num', '@object.num + 1' ], +1 do + assert_difference [ "@object.num", "@object.num + 1" ], +1 do @object.increment end end def test_array_of_expressions_identify_failure assert_raises(Minitest::Assertion) do - assert_difference ['@object.num', '1 + 1'] do + assert_difference ["@object.num", "1 + 1"] do @object.increment end end @@ -106,14 +109,147 @@ class AssertDifferenceTest < ActiveSupport::TestCase def test_array_of_expressions_identify_failure_when_message_provided assert_raises(Minitest::Assertion) do - assert_difference ['@object.num', '1 + 1'], 1, 'something went wrong' do + assert_difference ["@object.num", "1 + 1"], 1, "something went wrong" do + @object.increment + end + end + end + + def test_assert_changes_pass + assert_changes "@object.num" do + @object.increment + end + end + + def test_assert_changes_pass_with_lambda + assert_changes -> { @object.num } do + @object.increment + end + end + + def test_assert_changes_with_from_option + assert_changes "@object.num", from: 0 do + @object.increment + end + end + + def test_assert_changes_with_from_option_with_wrong_value + assert_raises Minitest::Assertion do + assert_changes "@object.num", from: -1 do + @object.increment + end + end + end + + def test_assert_changes_with_from_option_with_nil + error = assert_raises Minitest::Assertion do + assert_changes "@object.num", from: nil do + @object.increment + end + end + assert_equal "\"@object.num\" isn't nil", error.message + end + + def test_assert_changes_with_to_option + assert_changes "@object.num", to: 1 do + @object.increment + end + end + + def test_assert_changes_with_to_option_but_no_change_has_special_message + error = assert_raises Minitest::Assertion do + assert_changes "@object.num", to: 0 do + # no changes + end + end + + assert_equal "\"@object.num\" didn't change. It was already 0", error.message + end + + def test_assert_changes_with_wrong_to_option + assert_raises Minitest::Assertion do + assert_changes "@object.num", to: 2 do @object.increment end end end -end -class AlsoDoingNothingTest < ActiveSupport::TestCase + def test_assert_changes_with_from_option_and_to_option + assert_changes "@object.num", from: 0, to: 1 do + @object.increment + end + end + + def test_assert_changes_with_from_and_to_options_and_wrong_to_value + assert_raises Minitest::Assertion do + assert_changes "@object.num", from: 0, to: 2 do + @object.increment + end + end + end + + def test_assert_changes_works_with_any_object + # Silences: instance variable @new_object not initialized. + retval = silence_warnings do + assert_changes :@new_object, from: nil, to: 42 do + @new_object = 42 + end + end + + assert_equal 42, retval + end + + def test_assert_changes_works_with_nil + oldval = @object + + retval = assert_changes :@object, from: oldval, to: nil do + @object = nil + end + + assert_nil retval + end + + def test_assert_changes_with_to_and_case_operator + token = nil + + assert_changes -> { token }, to: /\w{32}/ do + token = SecureRandom.hex + end + end + + def test_assert_changes_with_to_and_from_and_case_operator + token = SecureRandom.hex + + assert_changes -> { token }, from: /\w{32}/, to: /\w{32}/ do + token = SecureRandom.hex + end + end + + def test_assert_changes_with_message + error = assert_raises Minitest::Assertion do + assert_changes "@object.num", "@object.num should 1", to: 1 do + @object.decrement + end + end + + assert_equal "@object.num should 1.\n\"@object.num\" didn't change to 1", error.message + end + + def test_assert_no_changes_pass + assert_no_changes "@object.num" do + # ... + end + end + + def test_assert_no_changes_with_message + error = assert_raises Minitest::Assertion do + assert_no_changes "@object.num", "@object.num should not change" do + @object.increment + end + end + + assert_equal "@object.num should not change.\n\"@object.num\" did change to 1", error.message + end end # Setup and teardown callbacks. @@ -133,7 +269,7 @@ class SetupAndTeardownTest < ActiveSupport::TestCase def teardown end - protected + private def reset_callback_record @called_back = [] @@ -158,7 +294,7 @@ class SubclassSetupAndTeardownTest < SetupAndTeardownTest assert_equal [:foo, :sentinel, :bar], self.class._teardown_callbacks.map(&:raw_filter) end - protected + private def bar @called_back << :bar end @@ -170,7 +306,7 @@ end class TestCaseTaggedLoggingTest < ActiveSupport::TestCase def before_setup - require 'stringio' + require "stringio" @out = StringIO.new self.tagged_logger = ActiveSupport::TaggedLogging.new(Logger.new(@out)) super |