diff options
Diffstat (limited to 'activesupport/test')
20 files changed, 155 insertions, 119 deletions
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 65de48a7f6..1dfa3833f0 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -15,7 +15,6 @@ silence_warnings do end require 'active_support/testing/autorun' -require 'empty_bool' ENV['NO_RELOAD'] = '1' require 'active_support' @@ -25,6 +24,9 @@ Thread.abort_on_exception = true # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true +# Disable available locale checks to avoid warnings running the test suite. +I18n.enforce_available_locales = false + # Skips the current run on Rubinius using Minitest::Assertions#skip def rubinius_skip(message = '') skip message if RUBY_ENGINE == 'rbx' diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb index f8e2ce22fa..32c2dfdfc0 100644 --- a/activesupport/test/callbacks_test.rb +++ b/activesupport/test/callbacks_test.rb @@ -1,27 +1,6 @@ require 'abstract_unit' module CallbacksTest - class Phone - include ActiveSupport::Callbacks - define_callbacks :save - - set_callback :save, :before, :before_save1 - set_callback :save, :after, :after_save1 - - def before_save1; self.history << :before; end - def after_save1; self.history << :after; end - - def save - run_callbacks :save do - raise 'boom' - end - end - - def history - @history ||= [] - end - end - class Record include ActiveSupport::Callbacks diff --git a/activesupport/test/configurable_test.rb b/activesupport/test/configurable_test.rb index d00273a028..ef847fc557 100644 --- a/activesupport/test/configurable_test.rb +++ b/activesupport/test/configurable_test.rb @@ -95,6 +95,20 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase config_accessor "invalid attribute name" end end + + assert_raises NameError do + Class.new do + include ActiveSupport::Configurable + config_accessor "invalid\nattribute" + end + end + + assert_raises NameError do + Class.new do + include ActiveSupport::Configurable + config_accessor "invalid\n" + end + end end def assert_method_defined(object, method) diff --git a/activesupport/test/core_ext/blank_test.rb b/activesupport/test/core_ext/blank_test.rb index a68c074777..3e3176f993 100644 --- a/activesupport/test/core_ext/blank_test.rb +++ b/activesupport/test/core_ext/blank_test.rb @@ -4,6 +4,14 @@ require 'abstract_unit' require 'active_support/core_ext/object/blank' class BlankTest < ActiveSupport::TestCase + class EmptyTrue + def empty?() true; end + end + + class EmptyFalse + def empty?() false; end + end + BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", ' ', [], {} ] NOT = [ EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], { nil => 0 } ] diff --git a/activesupport/test/core_ext/class/delegating_attributes_test.rb b/activesupport/test/core_ext/class/delegating_attributes_test.rb index 148f82946c..0e0742d147 100644 --- a/activesupport/test/core_ext/class/delegating_attributes_test.rb +++ b/activesupport/test/core_ext/class/delegating_attributes_test.rb @@ -39,10 +39,13 @@ class DelegatingAttributesTest < ActiveSupport::TestCase end def test_simple_accessor_declaration_with_instance_reader_false + _instance_methods = single_class.public_instance_methods single_class.superclass_delegating_accessor :no_instance_reader, :instance_reader => false assert_respond_to single_class, :no_instance_reader assert_respond_to single_class, :no_instance_reader= - assert !single_class.public_instance_methods.map(&:to_s).include?("no_instance_reader") + assert !_instance_methods.include?(:no_instance_reader) + assert !_instance_methods.include?(:no_instance_reader?) + assert !_instance_methods.include?(:_no_instance_reader) end def test_working_with_simple_attributes diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 8eae8c832c..28ba33331e 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -53,12 +53,10 @@ class DurationTest < ActiveSupport::TestCase end def test_argument_error - 1.second.ago('') - flunk("no exception was raised") - rescue ArgumentError => e + e = assert_raise ArgumentError do + 1.second.ago('') + end assert_equal 'expected a time or date, got ""', e.message, "ensure ArgumentError is not being raised by dependencies.rb" - rescue Exception => e - flunk("ArgumentError should be raised, but we got #{e.class} instead") end def test_fractional_weeks diff --git a/activesupport/test/core_ext/module/concerning_test.rb b/activesupport/test/core_ext/module/concerning_test.rb new file mode 100644 index 0000000000..c6863b24a4 --- /dev/null +++ b/activesupport/test/core_ext/module/concerning_test.rb @@ -0,0 +1,35 @@ +require 'abstract_unit' +require 'active_support/core_ext/module/concerning' + +class ConcerningTest < ActiveSupport::TestCase + def test_concern_shortcut_creates_a_module_but_doesnt_include_it + mod = Module.new { concern(:Foo) { } } + assert_kind_of Module, mod::Foo + assert mod::Foo.respond_to?(:included) + assert !mod.ancestors.include?(mod::Foo), mod.ancestors.inspect + end + + def test_concern_creates_a_module_extended_with_active_support_concern + klass = Class.new do + concern :Foo do + included { @foo = 1 } + def should_be_public; end + end + end + + # Declares a concern but doesn't include it + assert_kind_of Module, klass::Foo + assert !klass.ancestors.include?(klass::Foo), klass.ancestors.inspect + + # Public method visibility by default + assert klass::Foo.public_instance_methods.map(&:to_s).include?('should_be_public') + + # Calls included hook + assert_equal 1, Class.new { include klass::Foo }.instance_variable_get('@foo') + end + + def test_concerning_declares_a_concern_and_includes_it_immediately + klass = Class.new { concerning(:Foo) { } } + assert klass.ancestors.include?(klass::Foo), klass.ancestors.inspect + end +end diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 283b13ff8b..5b99fae411 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -12,12 +12,6 @@ class Ab Constant3 = "Goodbye World" end -module Xy - class Bc - include One - end -end - module Yz module Zy class Cd diff --git a/activesupport/test/core_ext/name_error_test.rb b/activesupport/test/core_ext/name_error_test.rb index 03ce09f22a..7525f80cf0 100644 --- a/activesupport/test/core_ext/name_error_test.rb +++ b/activesupport/test/core_ext/name_error_test.rb @@ -3,18 +3,18 @@ require 'active_support/core_ext/name_error' class NameErrorTest < ActiveSupport::TestCase def test_name_error_should_set_missing_name - SomeNameThatNobodyWillUse____Really ? 1 : 0 - flunk "?!?!" - rescue NameError => exc + exc = assert_raise NameError do + SomeNameThatNobodyWillUse____Really ? 1 : 0 + end assert_equal "NameErrorTest::SomeNameThatNobodyWillUse____Really", exc.missing_name assert exc.missing_name?(:SomeNameThatNobodyWillUse____Really) assert exc.missing_name?("NameErrorTest::SomeNameThatNobodyWillUse____Really") end def test_missing_method_should_ignore_missing_name - some_method_that_does_not_exist - flunk "?!?!" - rescue NameError => exc + exc = assert_raise NameError do + some_method_that_does_not_exist + end assert !exc.missing_name?(:Foo) assert_nil exc.missing_name end diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index 8d748791e3..0f454fdd95 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -3,31 +3,6 @@ require 'active_support/time' require 'active_support/core_ext/object' require 'active_support/core_ext/class/subclasses' -class ClassA; end -class ClassB < ClassA; end -class ClassC < ClassB; end -class ClassD < ClassA; end - -class ClassI; end -class ClassJ < ClassI; end - -class ClassK -end -module Nested - class << self - def on_const_missing(&callback) - @on_const_missing = callback - end - private - def const_missing(mod_id) - @on_const_missing[mod_id] if @on_const_missing - super - end - end - class ClassL < ClassK - end -end - class ObjectTests < ActiveSupport::TestCase class DuckTime def acts_like_time? diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index e56bab6d4c..00bec5bd9d 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -73,12 +73,11 @@ class DependenciesTest < ActiveSupport::TestCase $raises_exception_load_count = 0 5.times do |count| - begin + e = assert_raise Exception, 'should have loaded dependencies/raises_exception which raises an exception' do require_dependency filename - flunk 'should have loaded dependencies/raises_exception which raises an exception' - rescue Exception => e - assert_equal 'Loading me failed, so do not add to loaded or history.', e.message end + + assert_equal 'Loading me failed, so do not add to loaded or history.', e.message assert_equal count + 1, $raises_exception_load_count assert !ActiveSupport::Dependencies.loaded.include?(filename) @@ -366,26 +365,19 @@ class DependenciesTest < ActiveSupport::TestCase def test_non_existing_const_raises_name_error_with_fully_qualified_name with_autoloading_fixtures do - begin - A::DoesNotExist.nil? - flunk "No raise!!" - rescue NameError => e - assert_equal "uninitialized constant A::DoesNotExist", e.message - end - begin - A::B::DoesNotExist.nil? - flunk "No raise!!" - rescue NameError => e - assert_equal "uninitialized constant A::B::DoesNotExist", e.message - end + e = assert_raise(NameError) { A::DoesNotExist.nil? } + assert_equal "uninitialized constant A::DoesNotExist", e.message + + e = assert_raise(NameError) { A::B::DoesNotExist.nil? } + assert_equal "uninitialized constant A::B::DoesNotExist", e.message end end def test_smart_name_error_strings - Object.module_eval "ImaginaryObject" - flunk "No raise!!" - rescue NameError => e - assert e.message.include?("uninitialized constant ImaginaryObject") + e = assert_raise NameError do + Object.module_eval "ImaginaryObject" + end + assert_includes "uninitialized constant ImaginaryObject", e.message end def test_loadable_constants_for_path_should_handle_empty_autoloads @@ -543,8 +535,8 @@ class DependenciesTest < ActiveSupport::TestCase require_dependency 'e' mod = Module.new - msg = 'E cannot be autoloaded from an anonymous class or module' - assert_raise(NameError, msg) { mod::E } + e = assert_raise(NameError) { mod::E } + assert_equal 'E cannot be autoloaded from an anonymous class or module', e.message end end @@ -553,12 +545,10 @@ class DependenciesTest < ActiveSupport::TestCase c = ServiceOne ActiveSupport::Dependencies.clear assert ! defined?(ServiceOne) - begin + e = assert_raise ArgumentError do ActiveSupport::Dependencies.load_missing_constant(c, :FakeMissing) - flunk "Expected exception" - rescue ArgumentError => e - assert_match %r{ServiceOne has been removed from the module tree}i, e.message end + assert_match %r{ServiceOne has been removed from the module tree}i, e.message end end @@ -897,12 +887,10 @@ class DependenciesTest < ActiveSupport::TestCase with_autoloading_fixtures do Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError) 2.times do - begin + e = assert_raise NameError do ::RaisesNameError::FooBarBaz.object_id - flunk 'should have raised NameError when autoloaded file referenced FooBarBaz' - rescue NameError => e - assert_equal 'uninitialized constant RaisesNameError::FooBarBaz', e.message end + assert_equal 'uninitialized constant RaisesNameError::FooBarBaz', e.message assert !defined?(::RaisesNameError), "::RaisesNameError is defined but it should have failed!" end diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index 9674851b9d..ee1c69502e 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -104,14 +104,11 @@ class DeprecationTest < ActiveSupport::TestCase message = 'Revise this deprecated stuff now!' callstack = %w(foo bar baz) - begin + e = assert_raise ActiveSupport::DeprecationException do ActiveSupport::Deprecation.behavior.first.call(message, callstack) - rescue ActiveSupport::DeprecationException => e - assert_equal message, e.message - assert_equal callstack, e.backtrace - else - flunk 'the :raise deprecation behaviour should raise the expected exception' end + assert_equal message, e.message + assert_equal callstack, e.backtrace end def test_default_stderr_behavior @@ -174,7 +171,7 @@ class DeprecationTest < ActiveSupport::TestCase ActiveSupport::Deprecation.warn 'abc' ActiveSupport::Deprecation.warn 'def' end - rescue MiniTest::Assertion + rescue Minitest::Assertion flunk 'assert_deprecated should match any warning in block, not just the last one' end diff --git a/activesupport/test/empty_bool.rb b/activesupport/test/empty_bool.rb deleted file mode 100644 index 005b3523ef..0000000000 --- a/activesupport/test/empty_bool.rb +++ /dev/null @@ -1,7 +0,0 @@ -class EmptyTrue - def empty?() true; end -end - -class EmptyFalse - def empty?() false; end -end diff --git a/activesupport/test/fixtures/custom.rb b/activesupport/test/fixtures/custom.rb deleted file mode 100644 index 0eefce0c25..0000000000 --- a/activesupport/test/fixtures/custom.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Custom -end
\ No newline at end of file diff --git a/activesupport/test/message_verifier_test.rb b/activesupport/test/message_verifier_test.rb index f208814468..a5748d28ba 100644 --- a/activesupport/test/message_verifier_test.rb +++ b/activesupport/test/message_verifier_test.rb @@ -55,6 +55,20 @@ class MessageVerifierTest < ActiveSupport::TestCase ActiveSupport.use_standard_json_time_format = prev end + def test_raise_error_when_argument_class_is_not_loaded + # To generate the valid message below: + # + # AutoloadClass = Struct.new(:foo) + # valid_message = @verifier.generate(foo: AutoloadClass.new('foo')) + # + valid_message = "BAh7BjoIZm9vbzonTWVzc2FnZVZlcmlmaWVyVGVzdDo6QXV0b2xvYWRDbGFzcwY6CUBmb29JIghmb28GOgZFVA==--f3ef39a5241c365083770566dc7a9eb5d6ace914" + exception = assert_raise(ArgumentError, NameError) do + @verifier.verify(valid_message) + end + assert_includes ["uninitialized constant MessageVerifierTest::AutoloadClass", + "undefined class/module MessageVerifierTest::AutoloadClass"], exception.message + end + def assert_not_verified(message) assert_raise(ActiveSupport::MessageVerifier::InvalidSignature) do @verifier.verify(message) diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index 2bf73291a2..659fceb852 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -221,9 +221,9 @@ class MultibyteCharsUTF8BehaviourTest < ActiveSupport::TestCase end def test_include_raises_when_nil_is_passed - @chars.include?(nil) - flunk "Expected chars.include?(nil) to raise TypeError or NoMethodError" - rescue Exception + assert_raises TypeError, NoMethodError, "Expected chars.include?(nil) to raise TypeError or NoMethodError" do + @chars.include?(nil) + end end def test_index_should_return_character_offset diff --git a/activesupport/test/number_helper_test.rb b/activesupport/test/number_helper_test.rb index 111db59b2b..6d8d835de7 100644 --- a/activesupport/test/number_helper_test.rb +++ b/activesupport/test/number_helper_test.rb @@ -123,6 +123,12 @@ module ActiveSupport assert_equal("10.00", number_helper.number_to_rounded(9.995, :precision => 2)) assert_equal("11.00", number_helper.number_to_rounded(10.995, :precision => 2)) assert_equal("0.00", number_helper.number_to_rounded(-0.001, :precision => 2)) + + assert_equal("111.23460000000000000000", number_helper.number_to_rounded(111.2346, :precision => 20)) + assert_equal("111.23460000000000000000", number_helper.number_to_rounded(Rational(1112346, 10000), :precision => 20)) + assert_equal("111.23460000000000000000", number_helper.number_to_rounded('111.2346', :precision => 20)) + assert_equal("111.23460000000000000000", number_helper.number_to_rounded(BigDecimal(111.2346, Float::DIG), :precision => 20)) + assert_equal("111.2346" + "0"*96, number_helper.number_to_rounded('111.2346', :precision => 100)) end end @@ -155,6 +161,14 @@ module ActiveSupport assert_equal "10.0", number_helper.number_to_rounded(9.995, :precision => 3, :significant => true) assert_equal "9.99", number_helper.number_to_rounded(9.994, :precision => 3, :significant => true) assert_equal "11.0", number_helper.number_to_rounded(10.995, :precision => 3, :significant => true) + + assert_equal "9775.0000000000000000", number_helper.number_to_rounded(9775, :precision => 20, :significant => true ) + assert_equal "9775.0000000000000000", number_helper.number_to_rounded(9775.0, :precision => 20, :significant => true ) + assert_equal "9775.0000000000000000", number_helper.number_to_rounded(Rational(9775, 1), :precision => 20, :significant => true ) + assert_equal "97.750000000000000000", number_helper.number_to_rounded(Rational(9775, 100), :precision => 20, :significant => true ) + assert_equal "9775.0000000000000000", number_helper.number_to_rounded(BigDecimal(9775), :precision => 20, :significant => true ) + assert_equal "9775.0000000000000000", number_helper.number_to_rounded("9775", :precision => 20, :significant => true ) + assert_equal "9775." + "0"*96, number_helper.number_to_rounded("9775", :precision => 100, :significant => true ) end end diff --git a/activesupport/test/safe_buffer_test.rb b/activesupport/test/safe_buffer_test.rb index 047b89be2a..efa9d5e61f 100644 --- a/activesupport/test/safe_buffer_test.rb +++ b/activesupport/test/safe_buffer_test.rb @@ -140,4 +140,29 @@ class SafeBufferTest < ActiveSupport::TestCase # should still be unsafe assert !y.html_safe?, "should not be safe" end + + test 'Should work with interpolation (array argument)' do + x = 'foo %s bar'.html_safe % ['qux'] + assert_equal 'foo qux bar', x + end + + test 'Should work with interpolation (hash argument)' do + x = 'foo %{x} bar'.html_safe % { x: 'qux' } + assert_equal 'foo qux bar', x + end + + test 'Should escape unsafe interpolated args' do + x = 'foo %{x} bar'.html_safe % { x: '<br/>' } + assert_equal 'foo <br/> bar', x + end + + test 'Should not escape safe interpolated args' do + x = 'foo %{x} bar'.html_safe % { x: '<br/>'.html_safe } + assert_equal 'foo <br/> bar', x + end + + test 'Should interpolate to a safe string' do + x = 'foo %{x} bar'.html_safe % { x: 'qux' } + assert x.html_safe?, 'should be safe' + end end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 5ed2da7e8b..8a71ef4324 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -21,10 +21,10 @@ class AssertDifferenceTest < ActiveSupport::TestCase assert_equal true, assert_not(nil) assert_equal true, assert_not(false) - e = assert_raises(MiniTest::Assertion) { assert_not true } + e = assert_raises(Minitest::Assertion) { assert_not true } assert_equal 'Expected true to be nil or false', e.message - e = assert_raises(MiniTest::Assertion) { assert_not true, 'custom' } + e = assert_raises(Minitest::Assertion) { assert_not true, 'custom' } assert_equal 'custom', e.message end @@ -73,7 +73,7 @@ class AssertDifferenceTest < ActiveSupport::TestCase end def test_array_of_expressions_identify_failure - assert_raises(MiniTest::Assertion) do + assert_raises(Minitest::Assertion) do assert_difference ['@object.num', '1 + 1'] do @object.increment end @@ -81,7 +81,7 @@ class AssertDifferenceTest < ActiveSupport::TestCase end def test_array_of_expressions_identify_failure_when_message_provided - assert_raises(MiniTest::Assertion) do + assert_raises(Minitest::Assertion) do assert_difference ['@object.num', '1 + 1'], 1, 'something went wrong' do @object.increment end diff --git a/activesupport/test/testing/constant_lookup_test.rb b/activesupport/test/testing/constant_lookup_test.rb index aca2951450..71a9561189 100644 --- a/activesupport/test/testing/constant_lookup_test.rb +++ b/activesupport/test/testing/constant_lookup_test.rb @@ -6,7 +6,6 @@ class Bar < Foo def index; end def self.index; end end -class Baz < Bar; end module FooBar; end class ConstantLookupTest < ActiveSupport::TestCase |