提交 31ceb5e6 编写于 作者: X Xavier Noria

decouples the implementation of the inflector from its test suite

Trying alternative implementations of the inflections
is hard because the suite is coupled with the current
one, setting ivars by hand etc. This commit relies on
initialize_dup, as long as you maintain that one you
can tweak the implementation.
上级 004326ec
......@@ -28,6 +28,13 @@ def initialize
@plurals, @singulars, @uncountables, @humans, @acronyms, @acronym_regex = [], [], [], [], {}, /(?=a)b/
end
# Private, for the test suite.
def initialize_dup(orig)
%w(plurals singulars uncountables humans acronyms acronym_regex).each do |scope|
instance_variable_set("@#{scope}", orig.send(scope).dup)
end
end
# Specifies a new acronym. An acronym must be specified as it will appear in a camelized string. An underscore
# string that contains the acronym will retain the acronym when passed to `camelize`, `humanize`, or `titleize`.
# A camelized string that contains the acronym will maintain the acronym when titleized or humanized, and will
......
......@@ -26,11 +26,10 @@ def test_pluralize_empty_string
end
def test_uncountable_word_is_not_greedy
with_dup do
uncountable_word = "ors"
countable_word = "sponsor"
cached_uncountables = ActiveSupport::Inflector.inflections.uncountables
ActiveSupport::Inflector.inflections.uncountable << uncountable_word
assert_equal uncountable_word, ActiveSupport::Inflector.singularize(uncountable_word)
......@@ -40,9 +39,7 @@ def test_uncountable_word_is_not_greedy
assert_equal "sponsor", ActiveSupport::Inflector.singularize(countable_word)
assert_equal "sponsors", ActiveSupport::Inflector.pluralize(countable_word)
assert_equal "sponsor", ActiveSupport::Inflector.singularize(ActiveSupport::Inflector.pluralize(countable_word))
ensure
ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_uncountables
end
end
SingularToPlural.each do |singular, plural|
......@@ -349,16 +346,16 @@ def test_symbol_to_lower_camel
%w{plurals singulars uncountables humans}.each do |inflection_type|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def test_clear_#{inflection_type}
cached_values = ActiveSupport::Inflector.inflections.#{inflection_type}
with_dup do
ActiveSupport::Inflector.inflections.clear :#{inflection_type}
assert ActiveSupport::Inflector.inflections.#{inflection_type}.empty?, \"#{inflection_type} inflections should be empty after clear :#{inflection_type}\"
ActiveSupport::Inflector.inflections.instance_variable_set :@#{inflection_type}, cached_values
end
end
RUBY
end
def test_clear_all
cached_values = ActiveSupport::Inflector.inflections.plurals.dup, ActiveSupport::Inflector.inflections.singulars.dup, ActiveSupport::Inflector.inflections.uncountables.dup, ActiveSupport::Inflector.inflections.humans.dup
with_dup do
ActiveSupport::Inflector.inflections do |inflect|
# ensure any data is present
inflect.plural(/(quiz)$/i, '\1zes')
......@@ -373,14 +370,11 @@ def test_clear_all
assert inflect.uncountables.empty?
assert inflect.humans.empty?
end
ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
end
end
def test_clear_with_default
cached_values = ActiveSupport::Inflector.inflections.plurals.dup, ActiveSupport::Inflector.inflections.singulars.dup, ActiveSupport::Inflector.inflections.uncountables.dup, ActiveSupport::Inflector.inflections.humans.dup
with_dup do
ActiveSupport::Inflector.inflections do |inflect|
# ensure any data is present
inflect.plural(/(quiz)$/i, '\1zes')
......@@ -395,10 +389,7 @@ def test_clear_with_default
assert inflect.uncountables.empty?
assert inflect.humans.empty?
end
ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0]
ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1]
ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2]
ActiveSupport::Inflector.inflections.instance_variable_set :@humans, cached_values[3]
end
end
Irregularities.each do |irregularity|
......@@ -447,26 +438,28 @@ def test_clear_with_default
end
end
{ :singulars => :singular, :plurals => :plural, :uncountables => :uncountable, :humans => :human }.each do |scope, method|
%w(plurals singulars uncountables humans acronyms).each do |scope|
ActiveSupport::Inflector.inflections do |inflect|
define_method("test_clear_inflections_with_#{scope}") do
# save the inflections
values = inflect.send(scope)
with_dup do
# clear the inflections
inflect.clear(scope)
assert_equal [], inflect.send(scope)
# restore the inflections
if scope == :uncountables
inflect.send(method, values)
else
values.reverse.each { |value| inflect.send(method, *value) }
end
assert_equal values, inflect.send(scope)
end
end
end
# Dups the singleton and yields, restoring the original inflections later.
# Use this in tests what modify the state of the singleton.
#
# This helper is implemented by setting @__instance__ because in some tests
# there are module functions that access ActiveSupport::Inflector.inflections,
# so we need to replace the singleton itself.
def with_dup
original = ActiveSupport::Inflector.inflections
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, original.dup)
ensure
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, original)
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册