From 1c4db4d7c34a1062e998bbdee03e992c4c5bff6d Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Wed, 16 Mar 2011 09:43:14 -0400 Subject: Raise on invalid timezone Signed-off-by: Santiago Pastorino --- activesupport/test/core_ext/time_with_zone_test.rb | 31 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index bafa335a09..cebced5d55 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -36,6 +36,12 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal @twz.object_id, @twz.in_time_zone(ActiveSupport::TimeZone['Eastern Time (US & Canada)']).object_id end + def test_in_time_zone_with_bad_argument + assert_raise(ArgumentError) { @twz.in_time_zone('No such timezone exists') } + assert_raise(ArgumentError) { @twz.in_time_zone(-15.hours) } + assert_raise(ArgumentError) { @twz.in_time_zone(Object.new) } + end + def test_localtime assert_equal @twz.localtime, @twz.utc.getlocal end @@ -760,6 +766,15 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase end end + def test_in_time_zone_with_invalid_argument + assert_raise(ArgumentError) { @t.in_time_zone("No such timezone exists") } + assert_raise(ArgumentError) { @dt.in_time_zone("No such timezone exists") } + assert_raise(ArgumentError) { @t.in_time_zone(-15.hours) } + assert_raise(ArgumentError) { @dt.in_time_zone(-15.hours) } + assert_raise(ArgumentError) { @t.in_time_zone(Object.new) } + assert_raise(ArgumentError) { @dt.in_time_zone(Object.new) } + end + def test_in_time_zone_with_time_local_instance with_env_tz 'US/Eastern' do time = Time.local(1999, 12, 31, 19) # == Time.utc(2000) @@ -790,6 +805,14 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone end + def test_use_zone_raises_on_invalid_timezone + Time.zone = 'Alaska' + assert_raise ArgumentError do + Time.use_zone("No such timezone exists") { } + end + assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone + end + def test_time_zone_getter_and_setter Time.zone = ActiveSupport::TimeZone['Alaska'] assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone @@ -843,11 +866,9 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase end def test_time_zone_setter_with_invalid_zone - Time.zone = 'foo' - assert_nil Time.zone - - Time.zone = -15.hours - assert_nil Time.zone + assert_raise(ArgumentError){ Time.zone = "No such timezone exists" } + assert_raise(ArgumentError){ Time.zone = -15.hours } + assert_raise(ArgumentError){ Time.zone = Object.new } end def test_current_returns_time_now_when_zone_not_set -- cgit v1.2.3 From e9020b4b5dbd4a19e288c613a86c78e32010c361 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Tue, 5 Apr 2011 00:33:29 +0200 Subject: added find_zone and find_zone! to AS timezones and changed the AS Railtie to use find_zone! as well as adding Railtie tests Signed-off-by: Santiago Pastorino --- activesupport/test/core_ext/time_with_zone_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index cebced5d55..72b55183ba 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -871,6 +871,24 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase assert_raise(ArgumentError){ Time.zone = Object.new } end + def test_find_zone_without_bang_returns_nil_if_time_zone_can_not_be_found + assert_nil Time.find_zone('No such timezone exists') + assert_nil Time.find_zone(-15.hours) + assert_nil Time.find_zone(Object.new) + end + + def test_find_zone_with_bang_raises_if_time_zone_can_not_be_found + assert_raise(ArgumentError) { Time.find_zone!('No such timezone exists') } + assert_raise(ArgumentError) { Time.find_zone!(-15.hours) } + assert_raise(ArgumentError) { Time.find_zone!(Object.new) } + end + + def test_time_zone_setter_with_find_zone_without_bang + assert_nil Time.zone = Time.find_zone('No such timezone exists') + assert_nil Time.zone = Time.find_zone(-15.hours) + assert_nil Time.zone = Time.find_zone(Object.new) + end + def test_current_returns_time_now_when_zone_not_set with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(2000) -- cgit v1.2.3 From 635d991683c439da56fa72853880e88e6ac291ed Mon Sep 17 00:00:00 2001 From: "Prem Sichanugrist, Brian Morearty, John Reitano" Date: Sun, 10 Apr 2011 20:27:08 +0800 Subject: Add support for Object#in? and Object#either? in Active Support [#6321 state:committed] This will allow you to check if an object is included in another object or the list of objects or not. This patch is derived from patch by Brian Morearty and John Reitano on Lighthouse ticket. I've rewrite it and make sure that we support both 'another object' and 'list of objects' version, as it surely be useful to support both. --- .../test/core_ext/object/inclusion_test.rb | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 activesupport/test/core_ext/object/inclusion_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb new file mode 100644 index 0000000000..b4207c20ea --- /dev/null +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -0,0 +1,52 @@ +require 'abstract_unit' +require 'active_support/core_ext/object/inclusion' + +class InTest < Test::Unit::TestCase + def test_in_array + assert 1.in?([1,2]) + assert !3.in?([1,2]) + end + + def test_in_hash + h = { "a" => 100, "b" => 200 } + assert "a".in?(h) + assert !"z".in?(h) + end + + def test_in_string + assert "lo".in?("hello") + assert !"ol".in?("hello") + assert ?h.in?("hello") + end + + def test_in_range + assert 25.in?(1..50) + assert !75.in?(1..50) + end + + def test_in_set + s = Set.new([1,2]) + assert 1.in?(s) + assert !3.in?(s) + end + + def test_either + assert 1.either?(1,2,3) + assert !5.either?(1,2,3) + assert [1,2,3].either?([1,2,3], 2, [3,4,5]) + end + + module A + end + class B + include A + end + class C < B + end + + def test_in_module + assert A.in?(B) + assert A.in?(C) + assert !A.in?(A) + end +end -- cgit v1.2.3 From a9f3c9da01d721963d05949604ead909aaabbf36 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Mon, 11 Apr 2011 00:52:42 +0800 Subject: Using Object#in? and Object#either? in various places There're a lot of places in Rails source code which make a lot of sense to switching to Object#in? or Object#either? instead of using [].include?. --- activesupport/test/transliterate_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb index b054855d08..09271b759d 100644 --- a/activesupport/test/transliterate_test.rb +++ b/activesupport/test/transliterate_test.rb @@ -1,6 +1,7 @@ # encoding: utf-8 require 'abstract_unit' require 'active_support/inflector/transliterate' +require 'active_support/core_ext/object/inclusion' class TransliterateTest < Test::Unit::TestCase @@ -15,7 +16,7 @@ class TransliterateTest < Test::Unit::TestCase # create string with range of Unicode"s western characters with # diacritics, excluding the division and multiplication signs which for # some reason or other are floating in the middle of all the letters. - string = (0xC0..0x17E).to_a.reject {|c| [0xD7, 0xF7].include? c}.pack("U*") + string = (0xC0..0x17E).to_a.reject {|c| c.either?(0xD7, 0xF7)}.pack("U*") string.each_char do |char| assert_match %r{^[a-zA-Z']*$}, ActiveSupport::Inflector.transliterate(string) end -- cgit v1.2.3 From d1575ae1b9658c91145d6a46ec2a144a5a089207 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 12 Apr 2011 00:23:07 +0200 Subject: Change Object#either? to Object#among? -- thanks to @jamesarosen for the suggestion! --- activesupport/test/core_ext/object/inclusion_test.rb | 6 +++--- activesupport/test/transliterate_test.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb index b4207c20ea..25120deb81 100644 --- a/activesupport/test/core_ext/object/inclusion_test.rb +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -31,9 +31,9 @@ class InTest < Test::Unit::TestCase end def test_either - assert 1.either?(1,2,3) - assert !5.either?(1,2,3) - assert [1,2,3].either?([1,2,3], 2, [3,4,5]) + assert 1.among?(1,2,3) + assert !5.among?(1,2,3) + assert [1,2,3].among?([1,2,3], 2, [3,4,5]) end module A diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb index 09271b759d..90b40b5478 100644 --- a/activesupport/test/transliterate_test.rb +++ b/activesupport/test/transliterate_test.rb @@ -16,7 +16,7 @@ class TransliterateTest < Test::Unit::TestCase # create string with range of Unicode"s western characters with # diacritics, excluding the division and multiplication signs which for # some reason or other are floating in the middle of all the letters. - string = (0xC0..0x17E).to_a.reject {|c| c.either?(0xD7, 0xF7)}.pack("U*") + string = (0xC0..0x17E).to_a.reject {|c| c.among?(0xD7, 0xF7)}.pack("U*") string.each_char do |char| assert_match %r{^[a-zA-Z']*$}, ActiveSupport::Inflector.transliterate(string) end -- cgit v1.2.3 From a616e7a88cc35487b979d5f786092ba53aa47135 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 12 Apr 2011 20:10:50 -0300 Subject: Update test name to the corresponding method name Signed-off-by: Santiago Pastorino --- activesupport/test/core_ext/object/inclusion_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb index 25120deb81..9f2ace0ef7 100644 --- a/activesupport/test/core_ext/object/inclusion_test.rb +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -30,7 +30,7 @@ class InTest < Test::Unit::TestCase assert !3.in?(s) end - def test_either + def test_among assert 1.among?(1,2,3) assert !5.among?(1,2,3) assert [1,2,3].among?([1,2,3], 2, [3,4,5]) -- cgit v1.2.3 From 733bfa63f5d8d3b963202b6d3e9f00b4db070b91 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Wed, 13 Apr 2011 00:04:40 +0800 Subject: Remove `#among?` from Active Support After a long list of discussion about the performance problem from using varargs and the reason that we can't find a great pair for it, it would be best to remove support for it for now. It will come back if we can find a good pair for it. For now, Bon Voyage, `#among?`. --- activesupport/test/core_ext/object/inclusion_test.rb | 6 ------ activesupport/test/transliterate_test.rb | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb index 9f2ace0ef7..382271d42d 100644 --- a/activesupport/test/core_ext/object/inclusion_test.rb +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -30,12 +30,6 @@ class InTest < Test::Unit::TestCase assert !3.in?(s) end - def test_among - assert 1.among?(1,2,3) - assert !5.among?(1,2,3) - assert [1,2,3].among?([1,2,3], 2, [3,4,5]) - end - module A end class B diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb index 90b40b5478..08e11d4f38 100644 --- a/activesupport/test/transliterate_test.rb +++ b/activesupport/test/transliterate_test.rb @@ -16,7 +16,7 @@ class TransliterateTest < Test::Unit::TestCase # create string with range of Unicode"s western characters with # diacritics, excluding the division and multiplication signs which for # some reason or other are floating in the middle of all the letters. - string = (0xC0..0x17E).to_a.reject {|c| c.among?(0xD7, 0xF7)}.pack("U*") + string = (0xC0..0x17E).to_a.reject {|c| c.in?([0xD7, 0xF7])}.pack("U*") string.each_char do |char| assert_match %r{^[a-zA-Z']*$}, ActiveSupport::Inflector.transliterate(string) end -- cgit v1.2.3 From 4db4f8c6244f017def5668d44a62bdad231f4c18 Mon Sep 17 00:00:00 2001 From: Ben Orenstein Date: Fri, 25 Mar 2011 15:48:52 -0400 Subject: Add tests for InheritableOptions. [#6625 state:committed] Signed-off-by: Santiago Pastorino --- activesupport/test/ordered_options_test.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/ordered_options_test.rb b/activesupport/test/ordered_options_test.rb index e48425ca25..b215b60df3 100644 --- a/activesupport/test/ordered_options_test.rb +++ b/activesupport/test/ordered_options_test.rb @@ -50,4 +50,30 @@ class OrderedOptionsTest < Test::Unit::TestCase assert_equal 2, a.size assert_equal 56, a.else_where end + + def test_inheritable_options_continues_lookup_in_parent + parent = ActiveSupport::OrderedOptions.new + parent[:foo] = true + + child = ActiveSupport::InheritableOptions.new(parent) + assert child.foo + end + + def test_inheritable_options_can_override_parent + parent = ActiveSupport::OrderedOptions.new + parent[:foo] = :bar + + child = ActiveSupport::InheritableOptions.new(parent) + child[:foo] = :baz + + assert_equal :baz, child.foo + end + + def test_inheritable_options_inheritable_copy + original = ActiveSupport::InheritableOptions.new + copy = original.inheritable_copy + + assert copy.kind_of?(original.class) + assert_not_equal copy.object_id, original.object_id + end end -- cgit v1.2.3 From 7660e7cb4df69ad127661ad87f495a3b212170b9 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 14 Apr 2011 16:25:08 +0800 Subject: attributes no longer disappear if a tag contains whitespace old: Hash.from_xml("\n") => {"tag"=>"\n"} new: Hash.from_xml("\n") => {"tag"=>{"foo"=>"bar", "__content__"=>"\n"} --- activesupport/test/core_ext/hash_ext_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index a0479d45ac..012b956d7f 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -889,6 +889,15 @@ class HashToXmlTest < Test::Unit::TestCase assert_equal 'application/octet-stream', file.content_type end + def test_tag_with_attrs_and_whitespace + xml = <<-XML + + + XML + hash = Hash.from_xml(xml) + assert_equal "bacon is the best", hash['blog']['name'] + end + def test_xsd_like_types_from_xml bacon_xml = <<-EOT -- cgit v1.2.3 From cd233dd87e6f0f9113a397531d37075cfa7c6526 Mon Sep 17 00:00:00 2001 From: Jared McFarland Date: Fri, 15 Apr 2011 00:04:21 +0800 Subject: Only rescue a thrown NoMethodError, don't preemptively check for #include?; added tests --- activesupport/test/core_ext/object/inclusion_test.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb index 382271d42d..1de857d678 100644 --- a/activesupport/test/core_ext/object/inclusion_test.rb +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -43,4 +43,8 @@ class InTest < Test::Unit::TestCase assert A.in?(C) assert !A.in?(A) end + + def test_no_method_catching + assert_raise(ArgumentError) { 1.in?(1) } + end end -- cgit v1.2.3