Add tests to ensure correct pluralization

This commit is contained in:
Gerhard Schlager 2015-09-25 00:43:23 +02:00
parent 43614439e6
commit 5f9b7c67cd

View file

@ -58,7 +58,7 @@ describe "i18n integrity checks" do
end
end
describe 'keys in English locale files' do
describe 'English locale file' do
locale_files = ['config/locales', 'plugins/**/locales']
.product(['server.en.yml', 'client.en.yml'])
.collect { |dir, filename| Dir["#{Rails.root}/#{dir}/#{filename}"] }
@ -85,12 +85,42 @@ describe "i18n integrity checks" do
end
end
module Pluralizations
def self.load(path)
whitelist = Regexp.union([/messages.restrict_dependent_destroy/])
yaml = YAML.load_file("#{Rails.root}/#{path}")
pluralizations = find_pluralizations(yaml['en'])
pluralizations.reject! { |key| key.match(whitelist) }
pluralizations
end
def self.find_pluralizations(hash, parent_key = '', pluralizations = Hash.new)
hash.each do |key, value|
if value.is_a? Hash
current_key = parent_key.blank? ? key : "#{parent_key}.#{key}"
find_pluralizations(value, current_key, pluralizations)
elsif key == 'one' || key == 'other'
pluralizations[parent_key] = hash
end
end
pluralizations
end
end
locale_files.each do |path|
context path do
it 'has no duplicate keys' do
duplicates = DuplicateKeyFinder.new.find_duplicates("#{Rails.root}/#{path}")
expect(duplicates).to be_empty
end
Pluralizations.load(path).each do |key, values|
it "key '#{key}' has valid pluralizations" do
expect(values.keys).to contain_exactly('one', 'other')
end
end
end
end
end