File: locale_file_spec.rb

package info (click to toggle)
ruby-i18n-spec 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 224 kB
  • sloc: ruby: 478; makefile: 3
file content (173 lines) | stat: -rw-r--r-- 5,314 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require 'spec_helper'

module LocaleFileHelper
  def locale_file_with_content(content)
    locale_file = I18nSpec::LocaleFile.new('test.yml')
    expect(locale_file).to receive(:content).and_return(content)
    locale_file
  end
end

describe I18nSpec::LocaleFile do
  include LocaleFileHelper

  describe "#locale_code" do
    it "returns the first key of the file" do
      locale_file = locale_file_with_content("pt-BR:\n  hello: world")
      expect(locale_file.locale_code).to eq('pt-BR')
    end
  end

  describe "#locale" do
    it "returns an ISO::Tag based on the locale_code" do
      locale_file = locale_file_with_content("pt-BR:\n  hello: world")
      expect(locale_file.locale).to be_a(ISO::Tag)
      expect(locale_file.locale.language.code).to eq('pt')
      expect(locale_file.locale.region.code).to   eq('BR')
    end
  end

  describe "#is_parseable?" do
    context "when YAML is parseable" do
      let(:locale_file) { locale_file_with_content("en:\n  hello: world") }

      it "returns true" do
        expect(locale_file.is_parseable?).to eq(true)
      end
    end

    context "when YAML is NOT parseable" do
      let(:locale_file) { locale_file_with_content("<This isn't YAML>: foo: bar:") }

      it "returns false" do
        expect(locale_file.is_parseable?).to eq(false)
      end

      it "adds an :unparseable error" do
        locale_file.is_parseable?
        expect(locale_file.errors[:unparseable]).not_to be_nil
      end
    end
  end

  describe "#pluralizations" do
    let(:content) {
      "en:
        cats:
          one: one
          two: two
          three: three
        dogs:
          one: one
          some: some
        birds:
          penguins: penguins
          doves: doves"}

    it "returns the leaf where one of the keys is a pluralization key" do
      locale_file = locale_file_with_content(content)
      expect(locale_file.pluralizations).to eq({
        'en.cats' => {'one' => 'one', 'two' => 'two', 'three' => 'three'},
        'en.dogs' => {'one' => 'one', 'some' => 'some'},
      })
    end
  end

  describe "#invalid_pluralization_keys" do
    let(:content) {
      "en:
        cats:
          one: one
          two: two
          tommy: tommy
          tabby: tabby
        dogs:
          one: one
          some: some
        birds:
          zero: zero
          other: other"}
      let(:locale_file) { locale_file_with_content(content) }

    it "returns the parent that contains invalid pluralizations" do
      expect(locale_file.invalid_pluralization_keys.size).to eq(2)
      expect(locale_file.invalid_pluralization_keys).to be_include 'en.cats'
      expect(locale_file.invalid_pluralization_keys).to be_include 'en.dogs'
      expect(locale_file.invalid_pluralization_keys).not_to be_include 'en.birds'
    end

    it "adds a :invalid_pluralization_keys error with each invalid key" do
      locale_file.invalid_pluralization_keys
      expect(locale_file.invalid_pluralization_keys.size).to eq(2)
      expect(locale_file.invalid_pluralization_keys).to be_include 'en.cats'
      expect(locale_file.invalid_pluralization_keys).to be_include 'en.dogs'
      expect(locale_file.invalid_pluralization_keys).not_to be_include 'en.birds'
    end
  end

  describe "#missing_pluralization_keys" do
    it "returns the parents that containts missing pluralizations in with the english rules" do
      content = "en:
        cats:
          one: one
        dogs:
          other: other
        birds:
          one: one
          other: other"
      locale_file = locale_file_with_content(content)
      expect(locale_file.missing_pluralization_keys).to eq({
        'en.cats' => %w(other),
        'en.dogs' => %w(one)
      })
      expect(locale_file.errors[:missing_pluralization_keys]).not_to be_nil
    end

    it "returns the parents that containts missing pluralizations in with the russian rules" do
      content = "ru:
        cats:
          one: one
          few: few
          many: many
          other: other
        dogs:
          one: one
          other: some
        birds:
          zero: zero
          one: one
          few: few
          other: other"
      locale_file = locale_file_with_content(content)
      expect(locale_file.missing_pluralization_keys).to eq({
        'ru.dogs'  => %w(few many),
        'ru.birds' => %w(many)
      })
      expect(locale_file.errors[:missing_pluralization_keys]).not_to be_nil
    end

    it "returns the parents that containts missing pluralizations in with the japanese rules" do
      content = "ja:
        cats:
          one: one
        dogs:
          other: some
        birds: not really a pluralization"
      locale_file = locale_file_with_content(content)
      expect(locale_file.missing_pluralization_keys).to eq({ 'ja.cats' => %w(other) })
      expect(locale_file.errors[:missing_pluralization_keys]).not_to be_nil
    end

    it "returns an empty hash when all pluralizations are complete" do
      content = "ja:
        cats:
          other: one
        dogs:
          other: some
        birds: not really a pluralization"
      locale_file = locale_file_with_content(content)
      expect(locale_file.missing_pluralization_keys).to eq({})
      expect(locale_file.errors[:missing_pluralization_keys]).to be_nil
    end
  end
end