1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #20563 from repinel/fix-inflector-test

Fix inflector test by using dup inflections when it needs to be changed
This commit is contained in:
Matthew Draper 2015-06-16 00:00:50 +09:30
commit ce882b17f4

View file

@ -8,6 +8,20 @@ class InflectorTest < ActiveSupport::TestCase
include InflectorTestCases
include ConstantizeTestCases
def setup
# Dups the singleton before each test, restoring the original inflections later.
#
# 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.
@original_inflections = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__)[:en]
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: @original_inflections.dup)
end
def teardown
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: @original_inflections)
end
def test_pluralize_plurals
assert_equal "plurals", ActiveSupport::Inflector.pluralize("plurals")
assert_equal "Plurals", ActiveSupport::Inflector.pluralize("Plurals")
@ -26,7 +40,6 @@ class InflectorTest < ActiveSupport::TestCase
end
def test_uncountable_word_is_not_greedy
with_dup do
uncountable_word = "ors"
countable_word = "sponsor"
@ -40,7 +53,6 @@ class InflectorTest < ActiveSupport::TestCase
assert_equal "sponsors", ActiveSupport::Inflector.pluralize(countable_word)
assert_equal "sponsor", ActiveSupport::Inflector.singularize(ActiveSupport::Inflector.pluralize(countable_word))
end
end
SingularToPlural.each do |singular, plural|
define_method "test_pluralize_singular_#{singular}" do
@ -70,12 +82,10 @@ class InflectorTest < ActiveSupport::TestCase
def test_overwrite_previous_inflectors
with_dup do
assert_equal("series", ActiveSupport::Inflector.singularize("series"))
ActiveSupport::Inflector.inflections.singular "series", "serie"
assert_equal("serie", ActiveSupport::Inflector.singularize("series"))
end
end
MixtureToTitleCase.each_with_index do |(before, titleized), index|
define_method "test_titleize_mixture_to_title_case_#{index}" do
@ -367,11 +377,9 @@ class InflectorTest < ActiveSupport::TestCase
%w{plurals singulars uncountables humans}.each do |inflection_type|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def test_clear_#{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}\"
end
end
RUBY
end
@ -405,7 +413,6 @@ class InflectorTest < ActiveSupport::TestCase
end
def test_clear_all
with_dup do
ActiveSupport::Inflector.inflections do |inflect|
# ensure any data is present
inflect.plural(/(quiz)$/i, '\1zes')
@ -421,10 +428,8 @@ class InflectorTest < ActiveSupport::TestCase
assert inflect.humans.empty?
end
end
end
def test_clear_with_default
with_dup do
ActiveSupport::Inflector.inflections do |inflect|
# ensure any data is present
inflect.plural(/(quiz)$/i, '\1zes')
@ -440,11 +445,9 @@ class InflectorTest < ActiveSupport::TestCase
assert inflect.humans.empty?
end
end
end
Irregularities.each do |singular, plural|
define_method("test_irregularity_between_#{singular}_and_#{plural}") do
with_dup do
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular(singular, plural)
assert_equal singular, ActiveSupport::Inflector.singularize(plural)
@ -452,29 +455,24 @@ class InflectorTest < ActiveSupport::TestCase
end
end
end
end
Irregularities.each do |singular, plural|
define_method("test_pluralize_of_irregularity_#{plural}_should_be_the_same") do
with_dup do
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular(singular, plural)
assert_equal plural, ActiveSupport::Inflector.pluralize(plural)
end
end
end
end
Irregularities.each do |singular, plural|
define_method("test_singularize_of_irregularity_#{singular}_should_be_the_same") do
with_dup do
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular(singular, plural)
assert_equal singular, ActiveSupport::Inflector.singularize(singular)
end
end
end
end
[ :all, [] ].each do |scope|
ActiveSupport::Inflector.inflections do |inflect|
@ -503,7 +501,6 @@ class InflectorTest < ActiveSupport::TestCase
%w(plurals singulars uncountables humans acronyms).each do |scope|
define_method("test_clear_inflections_with_#{scope}") do
with_dup do
# clear the inflections
ActiveSupport::Inflector.inflections do |inflect|
inflect.clear(scope)
@ -511,7 +508,6 @@ class InflectorTest < ActiveSupport::TestCase
end
end
end
end
def test_inflections_with_uncountable_words
ActiveSupport::Inflector.inflections do |inflect|
@ -520,18 +516,4 @@ class InflectorTest < ActiveSupport::TestCase
assert_equal "HTTP", ActiveSupport::Inflector.pluralize("HTTP")
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.instance_variable_get(:@__instance__)[:en]
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original.dup)
yield
ensure
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original)
end
end