diff options
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/caching_test.rb | 8 | ||||
-rw-r--r-- | activesupport/test/core_ext/array_ext_test.rb | 10 | ||||
-rw-r--r-- | activesupport/test/core_ext/io_test.rb | 23 | ||||
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 56 | ||||
-rw-r--r-- | activesupport/test/core_ext/object/public_send_test.rb | 117 | ||||
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 7 | ||||
-rw-r--r-- | activesupport/test/notifications_test.rb | 1 |
7 files changed, 219 insertions, 3 deletions
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index dff3d6ef0d..6bb13ec9b8 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -199,6 +199,14 @@ module CacheStoreBehavior @cache.write('fud', 'biz') assert_equal({"foo" => "bar", "fu" => "baz"}, @cache.read_multi('foo', 'fu')) end + + def test_read_multi_with_expires + @cache.write('foo', 'bar', :expires_in => 0.001) + @cache.write('fu', 'baz') + @cache.write('fud', 'biz') + sleep(0.002) + assert_equal({"fu" => "baz"}, @cache.read_multi('foo', 'fu')) + end def test_read_and_write_compressed_small_data @cache.write('foo', 'bar', :compress => true) diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index e532010b18..52231aaeb0 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -465,3 +465,13 @@ class ArrayWrapperTests < Test::Unit::TestCase assert_equal DoubtfulToAry.new.to_ary, Array.wrap(DoubtfulToAry.new) end end + +class ArrayPrependAppendTest < Test::Unit::TestCase + def test_append + assert_equal [1, 2], [1].append(2) + end + + def test_prepend + assert_equal [2, 1], [1].prepend(2) + end +end diff --git a/activesupport/test/core_ext/io_test.rb b/activesupport/test/core_ext/io_test.rb new file mode 100644 index 0000000000..b9abf685da --- /dev/null +++ b/activesupport/test/core_ext/io_test.rb @@ -0,0 +1,23 @@ +require 'abstract_unit' + +require 'active_support/core_ext/io' + +class IOTest < Test::Unit::TestCase + def test_binread_one_arg + assert_equal File.read(__FILE__), IO.binread(__FILE__) + end + + def test_binread_two_args + assert_equal File.read(__FILE__).bytes.first(10).pack('C*'), + IO.binread(__FILE__, 10) + end + + def test_binread_three_args + actual = IO.binread(__FILE__, 5, 10) + expected = File.open(__FILE__, 'rb') { |f| + f.seek 10 + f.read 5 + } + assert_equal expected, actual + end +end diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index a95cf1591f..d4ce81fdfa 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -26,11 +26,30 @@ module Yz end end -Somewhere = Struct.new(:street, :city) +Somewhere = Struct.new(:street, :city) do + attr_accessor :name -Someone = Struct.new(:name, :place) do - delegate :street, :city, :to_f, :to => :place + protected + + def protected_method + end + + private + + def private_method + end +end + +class Someone < Struct.new(:name, :place) + delegate :street, :city, :to_f, :protected_method, :private_method, :to => :place + delegate :name=, :to => :place, :prefix => true delegate :upcase, :to => "place.city" + + FAILED_DELEGATE_LINE = __LINE__ + 1 + delegate :foo, :to => :place + + FAILED_DELEGATE_LINE_2 = __LINE__ + 1 + delegate :bar, :to => :place, :allow_nil => true end Invoice = Struct.new(:client) do @@ -69,6 +88,19 @@ class ModuleTest < Test::Unit::TestCase assert_equal "Chicago", @david.city end + def test_delegation_to_assignment_method + @david.place_name = "Fred" + assert_equal "Fred", @david.place.name + end + + def test_delegation_to_protected_method + assert_raise(NoMethodError) { @david.protected_method } + end + + def test_delegation_to_private_method + assert_raise(NoMethodError) { @david.private_method } + end + def test_delegation_down_hierarchy assert_equal "CHICAGO", @david.upcase end @@ -164,6 +196,24 @@ class ModuleTest < Test::Unit::TestCase end end + def test_delegation_exception_backtrace + someone = Someone.new("foo", "bar") + someone.foo + rescue NoMethodError => e + file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE}" + assert e.backtrace.first.include?(file_and_line), + "[#{e.backtrace.first}] did not include [#{file_and_line}]" + end + + def test_delegation_exception_backtrace_with_allow_nil + someone = Someone.new("foo", "bar") + someone.bar + rescue NoMethodError => e + file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE_2}" + assert e.backtrace.first.include?(file_and_line), + "[#{e.backtrace.first}] did not include [#{file_and_line}]" + end + def test_parent assert_equal Yz::Zy, Yz::Zy::Cd.parent assert_equal Yz, Yz::Zy.parent diff --git a/activesupport/test/core_ext/object/public_send_test.rb b/activesupport/test/core_ext/object/public_send_test.rb new file mode 100644 index 0000000000..7dc542e51c --- /dev/null +++ b/activesupport/test/core_ext/object/public_send_test.rb @@ -0,0 +1,117 @@ +require 'abstract_unit' +require 'active_support/core_ext/object/public_send' + +module PublicSendReceiver + def receive_public_method(*args) + return args + [yield] + end + + protected + + def receive_protected_method(*args) + return args + [yield] + end + + private + + def receive_private_method(*args) + return args + [yield] + end +end + +# Note, running this on 1.9 will be testing the Ruby core implementation, but it is good to +# do this to ensure that our backport functions the same as Ruby core in 1.9 +class PublicSendTest < Test::Unit::TestCase + def instance + @instance ||= begin + klass = Class.new do + include PublicSendReceiver + end + klass.new + end + end + + def singleton_instance + @singleton_instance ||= begin + object = Object.new + object.singleton_class.send(:include, PublicSendReceiver) + object + end + end + + def test_should_receive_public_method + assert_equal( + [:foo, :bar, :baz], + instance.public_send(:receive_public_method, :foo, :bar) { :baz } + ) + end + + def test_should_receive_public_singleton_method + assert_equal( + [:foo, :bar, :baz], + singleton_instance.public_send(:receive_public_method, :foo, :bar) { :baz } + ) + end + + def test_should_raise_on_protected_method + assert_raises(NoMethodError) do + instance.public_send(:receive_protected_method, :foo, :bar) { :baz } + end + end + + def test_should_raise_on_protected_singleton_method + assert_raises(NoMethodError) do + singleton_instance.public_send(:receive_protected_method, :foo, :bar) { :baz } + end + end + + def test_should_raise_on_private_method + assert_raises(NoMethodError) do + instance.public_send(:receive_private_method, :foo, :bar) { :baz } + end + end + + def test_should_raise_on_singleton_private_method + assert_raises(NoMethodError) do + singleton_instance.public_send(:receive_private_method, :foo, :bar) { :baz } + end + end + + def test_should_raise_on_undefined_method + assert_raises(NoMethodError) do + instance.public_send(:receive_undefined_method, :foo, :bar) { :baz } + end + end + + def test_protected_method_message + instance.public_send(:receive_protected_method, :foo, :bar) { :baz } + rescue NoMethodError => exception + assert_equal( + "protected method `receive_protected_method' called for #{instance.inspect}", + exception.message + ) + end + + def test_private_method_message + instance.public_send(:receive_private_method, :foo, :bar) { :baz } + rescue NoMethodError => exception + assert_equal( + "private method `receive_private_method' called for #{instance.inspect}", + exception.message + ) + end + + def test_undefined_method_message + instance.public_send(:receive_undefined_method, :foo, :bar) { :baz } + rescue NoMethodError => exception + assert_equal( + "undefined method `receive_undefined_method' for #{instance.inspect}", + exception.message + ) + end + + def test_receiver_with_no_singleton + assert_equal "5", 5.public_send(:to_s) + assert_equal "foo", :foo.public_send(:to_s) + end +end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index a4bba056df..81a284dded 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -7,10 +7,17 @@ require 'active_support/inflector' require 'active_support/core_ext/string' require 'active_support/time' require 'active_support/core_ext/string/strip' +require 'active_support/core_ext/string/output_safety' class StringInflectionsTest < Test::Unit::TestCase include InflectorTestCases + def test_erb_escape + string = [192, 60].pack('CC') + expected = 192.chr + "<" + assert_equal expected, ERB::Util.html_escape(string) + end + def test_strip_heredoc_on_an_empty_string assert_equal '', ''.strip_heredoc end diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index cc0dc564f7..884ee61547 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -1,4 +1,5 @@ require 'abstract_unit' +require 'active_support/core_ext/module/delegation' module Notifications class TestCase < ActiveSupport::TestCase |