aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/core_ext/array_ext_test.rb40
-rw-r--r--activesupport/test/core_ext/class_test.rb47
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb93
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb4
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb5
-rw-r--r--activesupport/test/ordered_hash_test.rb24
6 files changed, 54 insertions, 159 deletions
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb
index f5f91ddd80..d4cd5ddbde 100644
--- a/activesupport/test/core_ext/array_ext_test.rb
+++ b/activesupport/test/core_ext/array_ext_test.rb
@@ -52,8 +52,6 @@ class ArrayExtToParamTests < Test::Unit::TestCase
end
class ArrayExtToSentenceTests < Test::Unit::TestCase
- include ActiveSupport::Testing::Deprecation
-
def test_plain_array_to_sentence
assert_equal "", [].to_sentence
assert_equal "one", ['one'].to_sentence
@@ -62,28 +60,12 @@ class ArrayExtToSentenceTests < Test::Unit::TestCase
end
def test_to_sentence_with_words_connector
- assert_deprecated(":connector has been deprecated. Use :words_connector instead") do
- assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '')
- end
-
- assert_deprecated(":connector has been deprecated. Use :words_connector instead") do
- assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ')
- end
-
assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ')
assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ')
assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil)
end
def test_to_sentence_with_last_word_connector
- assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do
- assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => true)
- end
-
- assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do
- assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false)
- end
-
assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ')
assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil)
assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ')
@@ -320,6 +302,28 @@ class ArrayExtractOptionsTests < Test::Unit::TestCase
end
end
+class ArrayUniqByTests < Test::Unit::TestCase
+ def test_uniq_by
+ assert_equal [1,2], [1,2,3,4].uniq_by { |i| i.odd? }
+ assert_equal [1,2], [1,2,3,4].uniq_by(&:even?)
+ assert_equal (-5..0).to_a, (-5..5).to_a.uniq_by{ |i| i**2 }
+ end
+
+ def test_uniq_by!
+ a = [1,2,3,4]
+ a.uniq_by! { |i| i.odd? }
+ assert_equal [1,2], a
+
+ a = [1,2,3,4]
+ a.uniq_by! { |i| i.even? }
+ assert_equal [1,2], a
+
+ a = (-5..5).to_a
+ a.uniq_by! { |i| i**2 }
+ assert_equal (-5..0).to_a, a
+ end
+end
+
class ArrayExtRandomTests < Test::Unit::TestCase
def test_random_element_from_array
assert_nil [].rand
diff --git a/activesupport/test/core_ext/class_test.rb b/activesupport/test/core_ext/class_test.rb
deleted file mode 100644
index bb4eb3c7d5..0000000000
--- a/activesupport/test/core_ext/class_test.rb
+++ /dev/null
@@ -1,47 +0,0 @@
-require 'abstract_unit'
-require 'active_support/core_ext/class'
-
-class A
-end
-
-module X
- class B
- end
-end
-
-module Y
- module Z
- class C
- end
- end
-end
-
-class ClassTest < Test::Unit::TestCase
- def test_removing_class_in_root_namespace
- assert A.is_a?(Class)
- Class.remove_class(A)
- assert_raise(NameError) { A.is_a?(Class) }
- end
-
- def test_removing_class_in_one_level_namespace
- assert X::B.is_a?(Class)
- Class.remove_class(X::B)
- assert_raise(NameError) { X::B.is_a?(Class) }
- end
-
- def test_removing_class_in_two_level_namespace
- assert Y::Z::C.is_a?(Class)
- Class.remove_class(Y::Z::C)
- assert_raise(NameError) { Y::Z::C.is_a?(Class) }
- end
-
- def test_retrieving_subclasses
- @parent = eval("class D; end; D")
- @sub = eval("class E < D; end; E")
- @subofsub = eval("class F < E; end; F")
- assert_equal 2, @parent.subclasses.size
- assert_equal [@subofsub.to_s], @sub.subclasses
- assert_equal [], @subofsub.subclasses
- assert_equal [@sub.to_s, @subofsub.to_s].sort, @parent.subclasses.sort
- end
-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 e6fbdb637b..0b2a9c418e 100644
--- a/activesupport/test/core_ext/object_and_class_ext_test.rb
+++ b/activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -1,7 +1,6 @@
require 'abstract_unit'
require 'active_support/time'
require 'active_support/core_ext/object'
-require 'active_support/core_ext/class/removal'
class ClassA; end
class ClassB < ClassA; end
@@ -40,99 +39,7 @@ class Foo
include Bar
end
-class ClassExtTest < Test::Unit::TestCase
- def test_methods
- assert defined?(ClassB)
- assert defined?(ClassC)
- assert defined?(ClassD)
-
- ClassA.remove_subclasses
-
- assert !defined?(ClassB)
- assert !defined?(ClassC)
- assert !defined?(ClassD)
- end
-
- def test_subclasses_of
- cj = ClassJ
- assert_equal [ClassJ], Object.subclasses_of(ClassI)
- ClassI.remove_subclasses
- assert_equal [], Object.subclasses_of(ClassI)
- ensure
- Object.const_set :ClassJ, cj
- end
-
- def test_subclasses_of_should_find_nested_classes
- assert Object.subclasses_of(ClassK).include?(Nested::ClassL)
- end
-
- def test_subclasses_of_should_not_return_removed_classes
- # First create the removed class
- old_class = Nested.class_eval { remove_const :ClassL }
- new_class = Class.new(ClassK)
- Nested.const_set :ClassL, new_class
- assert_equal "Nested::ClassL", new_class.name # Sanity check
-
- subclasses = Object.subclasses_of(ClassK)
- assert subclasses.include?(new_class)
- assert ! subclasses.include?(old_class)
- ensure
- Nested.const_set :ClassL, old_class unless defined?(Nested::ClassL)
- end
-
- def test_subclasses_of_should_not_trigger_const_missing
- const_missing = false
- Nested.on_const_missing { const_missing = true }
-
- subclasses = Object.subclasses_of ClassK
- assert !const_missing
- assert_equal [ Nested::ClassL ], subclasses
-
- removed = Nested.class_eval { remove_const :ClassL } # keep it in memory
- subclasses = Object.subclasses_of ClassK
- assert !const_missing
- assert subclasses.empty?
- ensure
- Nested.const_set :ClassL, removed unless defined?(Nested::ClassL)
- end
-
- def test_subclasses_of_with_multiple_roots
- classes = Object.subclasses_of(ClassI, ClassK)
- assert_equal %w(ClassJ Nested::ClassL), classes.collect(&:to_s).sort
- end
-
- def test_subclasses_of_doesnt_find_anonymous_classes
- assert_equal [], Object.subclasses_of(Foo)
- bar = Class.new(Foo)
- assert_nothing_raised do
- assert_equal [bar], Object.subclasses_of(Foo)
- end
- end
-end
-
class ObjectTests < Test::Unit::TestCase
- def test_extended_by
- foo = Foo.new
- assert foo.extended_by.include?(Bar)
- foo.extend(Baz)
- assert(([Bar, Baz] - foo.extended_by).empty?, "Expected Bar, Baz in #{foo.extended_by.inspect}")
- end
-
- def test_extend_with_included_modules_from
- foo, object = Foo.new, Object.new
- assert !object.respond_to?(:bar)
- assert !object.respond_to?(:baz)
-
- object.extend_with_included_modules_from(foo)
- assert object.respond_to?(:bar)
- assert !object.respond_to?(:baz)
-
- foo.extend(Baz)
- object.extend_with_included_modules_from(foo)
- assert object.respond_to?(:bar)
- assert object.respond_to?(:baz)
- end
-
class DuckTime
def acts_like_time?
true
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index f6003bc083..08c079e113 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -722,6 +722,10 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
def test_minus_with_time_with_zone
assert_equal 86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['UTC'] )
end
+
+ def test_minus_with_datetime
+ assert_equal 86_400.0, Time.utc(2000, 1, 2) - DateTime.civil(2000, 1, 1)
+ end
def test_time_created_with_local_constructor_cannot_represent_times_during_hour_skipped_by_dst
with_env_tz 'US/Eastern' do
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index 3a12100e86..d88f79ae4f 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -56,8 +56,11 @@ class TimeWithZoneTest < Test::Unit::TestCase
assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst
end
- def test_to_json
+ def test_to_json_with_use_standard_json_time_format_config_set_to_false
+ old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, false
assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(@twz)
+ ensure
+ ActiveSupport.use_standard_json_time_format = old
end
def test_to_json_with_use_standard_json_time_format_config_set_to_true
diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb
index 1521279437..d070206d44 100644
--- a/activesupport/test/ordered_hash_test.rb
+++ b/activesupport/test/ordered_hash_test.rb
@@ -198,4 +198,28 @@ class OrderedHashTest < Test::Unit::TestCase
assert_same original, @ordered_hash
assert_equal @other_ordered_hash.keys, @ordered_hash.keys
end
+
+ def test_each_after_yaml_serialization
+ values = []
+ @deserialized_ordered_hash = YAML::load(YAML::dump(@ordered_hash))
+
+ @deserialized_ordered_hash.each {|key, value| values << value}
+ assert_equal @values, values
+ end
+
+ def test_order_after_yaml_serialization
+ @deserialized_ordered_hash = YAML::load(YAML::dump(@ordered_hash))
+
+ assert_equal @keys, @deserialized_ordered_hash.keys
+ assert_equal @values, @deserialized_ordered_hash.values
+ end
+
+ def test_order_after_yaml_serialization_with_nested_arrays
+ @ordered_hash[:array] = %w(a b c)
+
+ @deserialized_ordered_hash = YAML::load(YAML::dump(@ordered_hash))
+
+ assert_equal @ordered_hash.keys, @deserialized_ordered_hash.keys
+ assert_equal @ordered_hash.values, @deserialized_ordered_hash.values
+ end
end