diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-02-14 19:28:05 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-02-14 19:28:05 +0000 |
commit | 6f3c5f67870a625b8be4de6e34e8d0d2f5d2b5e3 (patch) | |
tree | 3da8d75101aabe3c1d90d0582505ec1480c9d885 /activesupport/test | |
parent | 77bf78b3b78a41d4f2f6e733f5c9c00608c0adba (diff) | |
parent | a1b60696e2b13cbe94d748444cc0da37b190fbb8 (diff) | |
download | rails-6f3c5f67870a625b8be4de6e34e8d0d2f5d2b5e3.tar.gz rails-6f3c5f67870a625b8be4de6e34e8d0d2f5d2b5e3.tar.bz2 rails-6f3c5f67870a625b8be4de6e34e8d0d2f5d2b5e3.zip |
Merge remote branch 'mainstream/master'
Conflicts:
railties/README
railties/guides/source/active_support_core_extensions.textile
railties/guides/source/getting_started.textile
railties/lib/generators/rails/app/templates/README
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/core_ext/class_test.rb | 29 | ||||
-rw-r--r-- | activesupport/test/core_ext/module/anonymous_test.rb | 14 | ||||
-rw-r--r-- | activesupport/test/core_ext/module/reachable_test.rb | 41 | ||||
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 12 | ||||
-rw-r--r-- | activesupport/test/core_ext/object_and_class_ext_test.rb | 50 | ||||
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 37 | ||||
-rw-r--r-- | activesupport/test/dependencies_test.rb | 27 | ||||
-rw-r--r-- | activesupport/test/json/decoding_test.rb | 3 | ||||
-rw-r--r-- | activesupport/test/json/encoding_test.rb | 10 | ||||
-rw-r--r-- | activesupport/test/rescuable_test.rb | 6 |
10 files changed, 174 insertions, 55 deletions
diff --git a/activesupport/test/core_ext/class_test.rb b/activesupport/test/core_ext/class_test.rb new file mode 100644 index 0000000000..b7f3dd9930 --- /dev/null +++ b/activesupport/test/core_ext/class_test.rb @@ -0,0 +1,29 @@ +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_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/module/anonymous_test.rb b/activesupport/test/core_ext/module/anonymous_test.rb new file mode 100644 index 0000000000..7a78a3b012 --- /dev/null +++ b/activesupport/test/core_ext/module/anonymous_test.rb @@ -0,0 +1,14 @@ +require 'abstract_unit' +require 'active_support/core_ext/module/anonymous' + +class AnonymousTest < ActiveSupport::TestCase + test "an anonymous class or module are anonymous" do + assert Module.new.anonymous? + assert Class.new.anonymous? + end + + test "a named class or module are not anonymous" do + assert !Kernel.anonymous? + assert !Object.anonymous? + end +end
\ No newline at end of file diff --git a/activesupport/test/core_ext/module/reachable_test.rb b/activesupport/test/core_ext/module/reachable_test.rb new file mode 100644 index 0000000000..72892b77d5 --- /dev/null +++ b/activesupport/test/core_ext/module/reachable_test.rb @@ -0,0 +1,41 @@ +require 'abstract_unit' +require 'active_support/core_ext/module/reachable' + +class AnonymousTest < ActiveSupport::TestCase + test "an anonymous class or module is not reachable" do + assert !Module.new.reachable? + assert !Class.new.reachable? + end + + test "ordinary named classes or modules are reachable" do + assert Kernel.reachable? + assert Object.reachable? + end + + test "a named class or module whose constant has gone is not reachable" do + c = eval "class C; end; C" + m = eval "module M; end; M" + + self.class.send(:remove_const, :C) + self.class.send(:remove_const, :M) + + assert !c.reachable? + assert !m.reachable? + end + + test "a named class or module whose constants store different objects are not reachable" do + c = eval "class C; end; C" + m = eval "module M; end; M" + + self.class.send(:remove_const, :C) + self.class.send(:remove_const, :M) + + eval "class C; end" + eval "module M; end" + + assert C.reachable? + assert M.reachable? + assert !c.reachable? + assert !m.reachable? + end +end
\ No newline at end of file diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 87f056ea85..1fe75d5930 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -72,13 +72,6 @@ class ModuleTest < Test::Unit::TestCase @david = Someone.new("David", Somewhere.new("Paulina", "Chicago")) end - def test_included_in_classes - assert One.included_in_classes.include?(Ab) - assert One.included_in_classes.include?(Xy::Bc) - assert One.included_in_classes.include?(Yz::Zy::Cd) - assert !One.included_in_classes.include?(De) - end - def test_delegation_to_methods assert_equal "Paulina", @david.street assert_equal "Chicago", @david.city @@ -170,11 +163,6 @@ class ModuleTest < Test::Unit::TestCase def test_local_constants assert_equal %w(Constant1 Constant3), Ab.local_constants.sort.map(&:to_s) end - - def test_as_load_path - assert_equal 'yz/zy', Yz::Zy.as_load_path - assert_equal 'yz', Yz.as_load_path - end end module BarMethodAliaser 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 0b2a9c418e..f31e7774e9 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -1,6 +1,7 @@ require 'abstract_unit' require 'active_support/time' require 'active_support/core_ext/object' +require 'active_support/core_ext/class/subclasses' class ClassA; end class ClassB < ClassA; end @@ -39,6 +40,55 @@ class Foo include Bar end +class ClassExtTest < Test::Unit::TestCase + def test_subclasses_of_should_find_nested_classes + assert Class.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 = Class.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 = Class.subclasses_of ClassK + assert !const_missing + assert_equal [ Nested::ClassL ], subclasses + + removed = Nested.class_eval { remove_const :ClassL } # keep it in memory + subclasses = Class.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 = Class.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 [], Class.subclasses_of(Foo) + bar = Class.new(Foo) + assert_nothing_raised do + assert_equal [bar], Class.subclasses_of(Foo) + end + end +end + class ObjectTests < Test::Unit::TestCase class DuckTime def acts_like_time? diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index ca26f91e8c..d8145d467b 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -335,6 +335,11 @@ end class OutputSafetyTest < ActiveSupport::TestCase def setup @string = "hello" + @object = Class.new(Object) do + def to_s + "other" + end + end.new end test "A string is unsafe by default" do @@ -355,17 +360,15 @@ class OutputSafetyTest < ActiveSupport::TestCase end test "An object is unsafe by default" do - klass = Class.new(Object) do - def to_str - "other" - end - end + assert !@object.html_safe? + end - @string.html_safe - @string << klass.new + test "Adding an object to a safe string returns a safe string" do + string = @string.html_safe + string << @object - assert_equal "helloother", @string - assert !@string.html_safe? + assert_equal "helloother", string + assert string.html_safe? end test "Adding a safe string to another safe string returns a safe string" do @@ -391,9 +394,9 @@ class OutputSafetyTest < ActiveSupport::TestCase test "Concatting safe onto unsafe yields unsafe" do @other_string = "other" - @string.html_safe - @other_string.concat(@string) + string = @string.html_safe + @other_string.concat(string) assert !@other_string.html_safe? end @@ -406,17 +409,17 @@ class OutputSafetyTest < ActiveSupport::TestCase test "Concatting safe onto safe yields safe" do @other_string = "other".html_safe - @string.html_safe + string = @string.html_safe - @other_string.concat(@string) + @other_string.concat(string) assert @other_string.html_safe? end test "Concatting safe onto unsafe with << yields unsafe" do @other_string = "other" - @string.html_safe + string = @string.html_safe - @other_string << @string + @other_string << string assert !@other_string.html_safe? end @@ -429,9 +432,9 @@ class OutputSafetyTest < ActiveSupport::TestCase test "Concatting safe onto safe with << yields safe" do @other_string = "other".html_safe - @string.html_safe + string = @string.html_safe - @other_string << @string + @other_string << string assert @other_string.html_safe? end diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 0fcf1eaf00..6ff6dfb607 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -1,7 +1,6 @@ require 'abstract_unit' require 'pp' require 'active_support/dependencies' -require 'active_support/core_ext/module/loading' require 'active_support/core_ext/kernel/reporting' module ModuleWithMissing @@ -43,12 +42,17 @@ class DependenciesTest < Test::Unit::TestCase require_dependency 'dependencies/service_one' require_dependency 'dependencies/service_two' assert_equal 2, ActiveSupport::Dependencies.loaded.size + ensure + Object.send(:remove_const, :ServiceOne) if Object.const_defined?(:ServiceOne) + Object.send(:remove_const, :ServiceTwo) if Object.const_defined?(:ServiceTwo) end def test_tracking_identical_loaded_files require_dependency 'dependencies/service_one' require_dependency 'dependencies/service_one' assert_equal 1, ActiveSupport::Dependencies.loaded.size + ensure + Object.send(:remove_const, :ServiceOne) if Object.const_defined?(:ServiceOne) end def test_missing_dependency_raises_missing_source_file @@ -130,10 +134,6 @@ class DependenciesTest < Test::Unit::TestCase end end - def test_as_load_path - assert_equal '', DependenciesTest.as_load_path - end - def test_module_loading with_autoloading_fixtures do assert_kind_of Module, A @@ -291,7 +291,7 @@ class DependenciesTest < Test::Unit::TestCase assert ActiveSupport::Dependencies.qualified_const_defined?("::Test::Unit::TestCase") end - def test_qualified_const_defined_should_not_call_method_missing + def test_qualified_const_defined_should_not_call_const_missing ModuleWithMissing.missing_count = 0 assert ! ActiveSupport::Dependencies.qualified_const_defined?("ModuleWithMissing::A") assert_equal 0, ModuleWithMissing.missing_count @@ -299,6 +299,10 @@ class DependenciesTest < Test::Unit::TestCase assert_equal 0, ModuleWithMissing.missing_count end + def test_qualified_const_defined_explodes_with_invalid_const_name + assert_raises(NameError) { ActiveSupport::Dependencies.qualified_const_defined?("invalid") } + end + def test_autoloaded? with_autoloading_fixtures do assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder") @@ -333,7 +337,6 @@ class DependenciesTest < Test::Unit::TestCase assert_equal "A", ActiveSupport::Dependencies.qualified_name_for(:Object, :A) assert_equal "A", ActiveSupport::Dependencies.qualified_name_for("Object", :A) assert_equal "A", ActiveSupport::Dependencies.qualified_name_for("::Object", :A) - assert_equal "A", ActiveSupport::Dependencies.qualified_name_for("::Kernel", :A) assert_equal "ActiveSupport::Dependencies::A", ActiveSupport::Dependencies.qualified_name_for(:'ActiveSupport::Dependencies', :A) assert_equal "ActiveSupport::Dependencies::A", ActiveSupport::Dependencies.qualified_name_for(ActiveSupport::Dependencies, :A) @@ -460,14 +463,6 @@ class DependenciesTest < Test::Unit::TestCase end end - def test_const_missing_on_kernel_should_fallback_to_object - with_autoloading_fixtures do - kls = Kernel::E - assert_equal "E", kls.name - assert_equal kls.object_id, Kernel::E.object_id - end - end - def test_preexisting_constants_are_not_marked_as_autoloaded with_autoloading_fixtures do require_dependency 'e' @@ -711,7 +706,7 @@ class DependenciesTest < Test::Unit::TestCase def test_autoload_doesnt_shadow_name_error with_autoloading_fixtures do Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError) - 2.times do + 2.times do |i| begin ::RaisesNameError::FooBarBaz.object_id flunk 'should have raised NameError when autoloaded file referenced FooBarBaz' diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb index 8fcb16abfb..d2e3efaa6b 100644 --- a/activesupport/test/json/decoding_test.rb +++ b/activesupport/test/json/decoding_test.rb @@ -45,10 +45,11 @@ class TestJSONDecoding < ActiveSupport::TestCase } # load the default JSON backend - ActiveSupport::JSON.backend + ActiveSupport::JSON.backend = 'Yaml' backends = %w(Yaml) backends << "JSONGem" if defined?(::JSON) + backends << "Yajl" if defined?(::Yajl) backends.each do |backend| TESTS.each do |json, expected| diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index cf9a635b5f..188b799f3f 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -50,13 +50,18 @@ class TestJSONEncoding < Test::Unit::TestCase StandardDateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10+00:00") ]] StandardStringTests = [[ 'this is the <string>', %("this is the <string>")]] + def sorted_json(json) + return json unless json =~ /^\{.*\}$/ + '{' + json[1..-2].split(',').sort.join(',') + '}' + end + constants.grep(/Tests$/).each do |class_tests| define_method("test_#{class_tests[0..-6].underscore}") do begin ActiveSupport.escape_html_entities_in_json = class_tests !~ /^Standard/ ActiveSupport.use_standard_json_time_format = class_tests =~ /^Standard/ self.class.const_get(class_tests).each do |pair| - assert_equal pair.last, ActiveSupport::JSON.encode(pair.first) + assert_equal pair.last, sorted_json(ActiveSupport::JSON.encode(pair.first)) end ensure ActiveSupport.escape_html_entities_in_json = false @@ -71,8 +76,7 @@ class TestJSONEncoding < Test::Unit::TestCase assert_equal %({\"a\":[1,2]}), ActiveSupport::JSON.encode('a' => [1,2]) assert_equal %({"1":2}), ActiveSupport::JSON.encode(1 => 2) - sorted_json = '{' + ActiveSupport::JSON.encode(:a => :b, :c => :d)[1..-2].split(',').sort.join(',') + '}' - assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json + assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json(ActiveSupport::JSON.encode(:a => :b, :c => :d)) end def test_utf8_string_encoded_properly_when_kcode_is_utf8 diff --git a/activesupport/test/rescuable_test.rb b/activesupport/test/rescuable_test.rb index 9f2b783b2e..ff77e16edd 100644 --- a/activesupport/test/rescuable_test.rb +++ b/activesupport/test/rescuable_test.rb @@ -7,12 +7,6 @@ class NuclearExplosion < StandardError end class MadRonon < StandardError - attr_accessor :message - - def initialize(message) - @message = message - super() - end end class Stargate |