From 310565f537b5eeb134e9a4bb0801358432f03e04 Mon Sep 17 00:00:00 2001 From: Ryan Oblak Date: Thu, 22 Sep 2011 12:17:42 -0700 Subject: Added ActiveSupport::Inflector.safe_constantize and String#safe_constantize; refactored common constantize tests into ConstantizeTestCases --- .../active_support/core_ext/string/inflections.rb | 19 ++++++++++--- .../lib/active_support/inflector/methods.rb | 27 +++++++++++++++++++ activesupport/test/constantize_test_cases.rb | 31 ++++++++++++++++++++++ activesupport/test/core_ext/string_ext_test.rb | 23 +++++++++++++++- activesupport/test/inflector_test.rb | 27 +++++++------------ 5 files changed, 106 insertions(+), 21 deletions(-) create mode 100644 activesupport/test/constantize_test_cases.rb (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index 002688d6c0..b4218cb42c 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -33,14 +33,27 @@ class String # +constantize+ tries to find a declared constant with the name specified # in the string. It raises a NameError when the name is not in CamelCase - # or is not initialized. + # or is not initialized. See ActiveSupport::Inflector.constantize # # Examples - # "Module".constantize # => Module - # "Class".constantize # => Class + # "Module".constantize # => Module + # "Class".constantize # => Class + # "blargle".safe_constantize # => NameError: wrong constant name blargle def constantize ActiveSupport::Inflector.constantize(self) end + + # +safe_constantize+ tries to find a declared constant with the name specified + # in the string. It returns nil when the name is not in CamelCase + # or is not initialized. See ActiveSupport::Inflector.safe_constantize + # + # Examples + # "Module".safe_constantize # => Module + # "Class".safe_constantize # => Class + # "blargle".safe_constantize # => nil + def safe_constantize + ActiveSupport::Inflector.safe_constantize(self) + end # By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize # is set to :lower then camelize produces lowerCamelCase. diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index 423b5abd20..b22e39c7c2 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -224,6 +224,33 @@ module ActiveSupport end end + # Tries to find a constant with the name specified in the argument string: + # + # "Module".safe_constantize # => Module + # "Test::Unit".safe_constantize # => Test::Unit + # + # The name is assumed to be the one of a top-level constant, no matter whether + # it starts with "::" or not. No lexical context is taken into account: + # + # C = 'outside' + # module M + # C = 'inside' + # C # => 'inside' + # "C".safe_constantize # => 'outside', same as ::C + # end + # + # nil is returned when the name is not in CamelCase or the constant is + # unknown. + # + # "blargle".safe_constantize # => nil + def safe_constantize(camel_cased_word) + begin + camel_cased_word.constantize + rescue NameError + nil + end + end + # Turns a number into an ordinal string used to denote the position in an # ordered sequence such as 1st, 2nd, 3rd, 4th. # diff --git a/activesupport/test/constantize_test_cases.rb b/activesupport/test/constantize_test_cases.rb new file mode 100644 index 0000000000..b8a866b6c9 --- /dev/null +++ b/activesupport/test/constantize_test_cases.rb @@ -0,0 +1,31 @@ +module Ace + module Base + class Case + end + end +end + +module ConstantizeTestCases + def run_constantize_tests_on + assert_nothing_raised { assert_equal Ace::Base::Case, yield("Ace::Base::Case") } + assert_nothing_raised { assert_equal Ace::Base::Case, yield("::Ace::Base::Case") } + assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") } + assert_nothing_raised { assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases") } + assert_raise(NameError) { yield("UnknownClass") } + assert_raise(NameError) { yield("An invalid string") } + assert_raise(NameError) { yield("InvalidClass\n") } + assert_raise(NameError) { yield("Ace::Base::ConstantizeTestCases") } + end + + def run_safe_constantize_tests_on + assert_nothing_raised { assert_equal Ace::Base::Case, yield("Ace::Base::Case") } + assert_nothing_raised { assert_equal Ace::Base::Case, yield("::Ace::Base::Case") } + assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") } + assert_nothing_raised { assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases") } + assert_nothing_raised { assert_equal nil, yield("UnknownClass") } + assert_nothing_raised { assert_equal nil, yield("An invalid string") } + assert_nothing_raised { assert_equal nil, yield("InvalidClass\n") } + assert_nothing_raised { assert_equal nil, yield("blargle") } + assert_nothing_raised { assert_equal nil, yield("Ace::Base::ConstantizeTestCases") } + end +end \ No newline at end of file diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 81a284dded..5c1dddaf96 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -2,6 +2,7 @@ require 'date' require 'abstract_unit' require 'inflector_test_cases' +require 'constantize_test_cases' require 'active_support/inflector' require 'active_support/core_ext/string' @@ -9,9 +10,17 @@ require 'active_support/time' require 'active_support/core_ext/string/strip' require 'active_support/core_ext/string/output_safety' +module Ace + module Base + class Case + end + end +end + class StringInflectionsTest < Test::Unit::TestCase include InflectorTestCases - + include ConstantizeTestCases + def test_erb_escape string = [192, 60].pack('CC') expected = 192.chr + "<" @@ -292,6 +301,18 @@ class StringInflectionsTest < Test::Unit::TestCase "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding('UTF-8').truncate(10) end end + + def test_constantize + run_constantize_tests_on do |string| + string.constantize + end + end + + def test_safe_constantize + run_safe_constantize_tests_on do |string| + string.safe_constantize + end + end end class StringBehaviourTest < Test::Unit::TestCase diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index b9e299af75..5c956e0075 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -2,16 +2,11 @@ require 'abstract_unit' require 'active_support/inflector' require 'inflector_test_cases' - -module Ace - module Base - class Case - end - end -end +require 'constantize_test_cases' class InflectorTest < Test::Unit::TestCase include InflectorTestCases + include ConstantizeTestCases def test_pluralize_plurals assert_equal "plurals", ActiveSupport::Inflector.pluralize("plurals") @@ -282,17 +277,15 @@ class InflectorTest < Test::Unit::TestCase end def test_constantize - assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("Ace::Base::Case") } - assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("::Ace::Base::Case") } - assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("InflectorTest") } - assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("::InflectorTest") } - assert_raise(NameError) { ActiveSupport::Inflector.constantize("UnknownClass") } - assert_raise(NameError) { ActiveSupport::Inflector.constantize("An invalid string") } - assert_raise(NameError) { ActiveSupport::Inflector.constantize("InvalidClass\n") } + run_constantize_tests_on do |string| + ActiveSupport::Inflector.constantize(string) + end end - - def test_constantize_does_lexical_lookup - assert_raise(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") } + + def test_safe_constantize + run_safe_constantize_tests_on do |string| + ActiveSupport::Inflector.safe_constantize(string) + end end def test_ordinal -- cgit v1.2.3