Skip to content

Commit

Permalink
Test: Added locale files test
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Nov 11, 2023
1 parent dea652a commit c148ef5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/locales/cs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# :czech pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)
# :west_slavic pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)

cs:
pagy:
Expand Down
2 changes: 1 addition & 1 deletion lib/locales/sw.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# :one_other pluralization
# :one_other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)

sw:
pagy:
Expand Down
2 changes: 1 addition & 1 deletion lib/locales/vi.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)
# :other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb)

vi:
pagy:
Expand Down
49 changes: 49 additions & 0 deletions test/pagy/i18n_locales_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require_relative '../test_helper'

describe 'pagy/locales' do
let(:rules) { Pagy::I18n::P11n::RULE.keys }
let(:counts) do
{
arabic: %w[zero one two few many other],
east_slavic: %w[one few many other],
one_other: %w[one other],
one_two_other: %w[one two other],
one_upto_two_other: %w[one other],
other: %w[other],
polish: %w[one few many other],
west_slavic: %w[one few other]
}
end

# locale files loop
Pagy.root.join('locales').each_child do |f|
next unless f.extname == '.yml'

message = "locale file #{f}"
locale = f.basename.to_s[0..-5]
comment = f.readlines.first.to_s.strip
rule = comment.to_s.split[1][1..-1].to_s.to_sym

it 'includes a comment with the pluralization rule and the i18n.rb reference' do
_(rules).must_include rule, message
_(comment).must_match 'https://github.com/ddnexus/pagy/blob/master/lib/pagy/i18n.rb', message
end
it 'defines and matches the locale pluralization rule' do
_(Pagy::I18n::P11n::LOCALE[locale]).must_equal Pagy::I18n::P11n::RULE[rule], message
end
it 'pluralizes item_name according to the rule' do
hash = YAML.safe_load(f.read)
item_name = hash[locale]['pagy']['item_name']
case item_name
when String
_(rule).must_equal :other
when Hash
_(item_name.keys - counts[rule]).must_be_empty
else
raise StandardError, "item_name must be Hash or String"
end
end
end
end

0 comments on commit c148ef5

Please sign in to comment.