Skip to content

Commit

Permalink
Matcher for be a subset
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrish committed Dec 15, 2011
1 parent fec7b7e commit cc57671
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -31,6 +31,12 @@ Even better, you can run all of these tests for every file in a directory like s

## Testing the validity of your translation data

To test that your locale file is a subset of the default locale file

describe "config/locales/fr.yml" do
it { should be_a_subset_of 'config/locales/en.yml' }
end

If you need to test that all translations have been completed :

describe "config/locales/fr.yml" do
Expand Down
14 changes: 14 additions & 0 deletions lib/i18n-spec/matchers/be_a_subset_of_matcher.rb
@@ -0,0 +1,14 @@
RSpec::Matchers.define :be_a_subset_of do |expected|
match do |actual|
@locale_file = I18nSpec::LocaleFile.new(actual)
@default_locale = I18nSpec::LocaleFile.new(expected)
@superset = @locale_file.flattened_translations.keys.reject do |key|
@default_locale.flattened_translations.keys.include?(key)
end
@superset.empty?
end

failure_message_for_should do |player|
"expected #{@locale_file.filepath} to not include :\n- " << @superset.join("\n- ")#{@superset.join"
end
end
14 changes: 10 additions & 4 deletions lib/i18n-spec/models/locale_file.rb
Expand Up @@ -2,10 +2,20 @@ module I18nSpec
class LocaleFile
PLURALIZATION_KEYS = %w{zero one two few many other}

attr_accessor :filepath

def initialize(filepath)
@filepath = filepath
end

def translations
@translations ||= Psych.load_file(@filepath)
end

def flattened_translations
@flattened_translations ||= flatten_tree(translations.values.first)
end

def is_parseable?
begin
Psych.load_file(@filepath)
Expand Down Expand Up @@ -39,10 +49,6 @@ def is_a_complete_tranlsation_of?(default_locale_filepath)

protected

def translations
@translations ||= Psych.load_file(@filepath)
end

def flatten_tree(data, prefix = '', result = {})
data.each do |key, value|
current_prefix = prefix.empty? ? key.to_s : "#{prefix}.#{key}"
Expand Down
9 changes: 9 additions & 0 deletions spec/fixtures/not_subset.yml
@@ -0,0 +1,9 @@
en:
dogs:
zero: 'no dogs'
one: 'one dog'
other: '%{count} dogs'
itteh:
bitteh:
ceiling:
doggeh: 'is asking for trouble'
3 changes: 3 additions & 0 deletions spec/lib/i18n-spec/matchers_spec.rb
Expand Up @@ -9,14 +9,17 @@
it { 'spec/fixtures/invalid_pluralization_keys.yml'.should_not have_valid_pluralization_keys }
it { 'spec/fixtures/multiple_top_levels.yml'.should_not have_one_top_level_namespace }
it { 'spec/fixtures/multiple_top_levels.yml'.should_not be_named_like_top_level_namespace }
it { 'spec/fixtures/not_subset.yml'.should_not be_a_subset_of 'spec/fixtures/en.yml' }
end

describe "Translated files" do
describe 'spec/fixtures/fr.yml' do
it { should be_a_subset_of 'spec/fixtures/en.yml' }
it { should be_a_complete_translation_of 'spec/fixtures/en.yml' }
end

describe 'spec/fixtures/es.yml' do
it { should be_a_subset_of 'spec/fixtures/en.yml' }
it { should_not be_a_complete_translation_of 'spec/fixtures/en.yml'}
end
end

0 comments on commit cc57671

Please sign in to comment.