Added ActiveSupport::Inflector.safe_constantize and String#safe_constantize; refactored common constantize tests into ConstantizeTestCases

This commit is contained in:
Ryan Oblak 2011-09-22 12:17:42 -07:00 committed by José Valim
parent d62fc8e021
commit 310565f537
5 changed files with 106 additions and 21 deletions

View File

@ -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 <tt>:lower</tt> then camelize produces lowerCamelCase.

View File

@ -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.
#

View File

@ -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

View File

@ -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 + "&lt;"
@ -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

View File

@ -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