Module: Localite::Etest

Defined in:
lib/localite.rb

Instance Method Summary collapse

Instance Method Details

#catch_exception(klass, &block) ⇒ Object



201
202
203
204
205
206
# File 'lib/localite.rb', line 201

def catch_exception(klass, &block)
  yield
  nil
rescue klass
  $!
end

#test_base_lookupObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/localite.rb', line 94

def test_base_lookup
  assert !I18n.load_path.empty?
  
  assert_equal("en.t", "t".t)
  Localite.locale("en") { 
    assert_equal("en.t", "t".t )
  }
  
  Localite.locale("de") { 
    assert_equal("de.t", "t".t )
  }
  
  assert_equal("de.t", Localite.locale("de") { "t".t })
end

#test_default_formatObject



281
282
283
284
285
# File 'lib/localite.rb', line 281

def test_default_format
  assert_equal "abc", "param".t(:xxx => "abc")
  assert_equal "a > c", "param".t(:xxx => "a > c")
  assert_equal "abc", "param".t(:xxx => "abc")
end

#test_format_scopeObject



300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/localite.rb', line 300

def test_format_scope
  Localite.format(:html) do
    assert_equal :html, Localite.current_format
    assert_equal("This is hypertext", :title.t)
    assert_equal("This is <> hypertext", :title2.t)
  end
  Localite.format(:text) do
    assert_equal :text, Localite.current_format
    assert_equal("This is hypertext", :title.t)
    assert_equal("This is <> hypertext", :title2.t)
  end
end

#test_html_formatObject



291
292
293
294
295
296
297
298
# File 'lib/localite.rb', line 291

def test_html_format
  Localite.format(:html) { 
    assert_equal(:html, Localite.current_format)
    assert_equal "a &gt; c", "param".t(:xxx => "a > c")
  }
  
  assert_equal "a &gt; c", Localite.format(:html) { "param".t(:xxx => "a > c") }
end

#test_inspectObject



320
321
322
323
324
# File 'lib/localite.rb', line 320

def test_inspect
  assert_nothing_raised {
    Localite.inspect
  }
end

#test_lookup_deObject



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/localite.rb', line 109

def test_lookup_de
  Localite.locale("de") do
    # flat translation
    assert_equal "de.t", "t".t

    Localite.scope(:outer, :inner) do
      assert_equal("de/outer/inner/x1", "x1".t)
    end
      
    # Miss "x1", and don't translate missing entries
    assert_equal("x1", "x1".t)
  end
end

#test_lookup_enObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/localite.rb', line 135

def test_lookup_en
  Localite.locale("en") do

    # flat translation
    assert_equal "en.t", "t".t

    Localite.scope(:outer, :inner) do
      assert_equal("en/outer/inner/x1", :x1.t)
    end

    # Miss "x1", and don't translate missing entries
    assert_equal("x1", "x1".t)
  end
end

#test_lookup_in_baseObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/localite.rb', line 123

def test_lookup_in_base
  Localite.locale("en") do
    # lookup "base" in base translation
    assert_equal "en_only", "base".t
  end

  Localite.locale("de") do
    # lookup "base" in base (i.e. en) translation
    assert_equal "en_only", "base".t
  end
end

#test_lookup_no_specific_langObject



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/localite.rb', line 150

def test_lookup_no_specific_lang
  # flat translation
  assert_equal "en.t", "t".t

  Localite.scope(:outer, :inner, :x1) do
    assert_equal("en/outer/inner/x1", "x1".t)
  end

  # Miss "x1", and don't translate missing entries
  assert_equal("x1", "x1".t)
end

#test_lookup_symbolsObject



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/localite.rb', line 162

def test_lookup_symbols
  assert :base.t?
  
  assert_equal "en_only", :base.t

  Localite.locale("en") do
    assert_equal "en_only", :base.t
  end

  Localite.locale("de") do
    assert_equal "en_only", :base.t
  end
end

#test_missing_lookup_symbolsObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/localite.rb', line 176

def test_missing_lookup_symbols
  assert !:missing.t?
  assert_raise(Localite::Translate::Missing) {
    assert_equal "en_only", :missing.t
  }

  Localite.locale("en") do
    assert_raise(Localite::Translate::Missing) {
      :missing.t
    }
  end

  Localite.locale("de") do
    assert_raise(Localite::Translate::Missing) {
      :missing.t
    }
  end

  begin
    :missing.t
  rescue Localite::Translate::Missing
    assert_kind_of(String, $!.to_s)
  end
end

#test_missing_translation_w_scopeObject



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/localite.rb', line 240

def test_missing_translation_w_scope
  r = catch_exception(Localite::Translate::Missing) do 
    Localite.locale(:de) do
      Localite.scope(:ab, :cd) do
        :yx.t
      end
    end
  end
  
  assert_equal(:de, r.locale)
  assert_equal(:yx, r.string)
  assert_equal("ab:cd", r.scope)
end

#test_missing_translation_wo_scopeObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/localite.rb', line 208

def test_missing_translation_wo_scope
  r = catch_exception(Localite::Translate::Missing) do 
    Localite.scope(:locale => :de, :format => :text) do
      :x.t
    end
  end
  
  assert_equal(:de, r.locale)
  assert_equal(:x, r.string)
  assert_equal("", r.scope)

  r = catch_exception(Localite::Translate::Missing) do 
    Localite.scope(:locale => :de, :format => :html) do
      :x.t
    end
  end
  
  assert_equal(:de, r.locale)
  assert_equal(:x, r.string)
  assert_equal("", r.scope)

  r = catch_exception(Localite::Translate::Missing) do 
    Localite.scope(:locale => :de) do
      :x.t
    end
  end
  
  assert_equal(:de, r.locale)
  assert_equal(:x, r.string)
  assert_equal("", r.scope)
end

#test_reset_scopeObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/localite.rb', line 254

def test_reset_scope
  r = catch_exception(Localite::Translate::Missing) do 
    Localite.scope(:ab) do
      Localite.scope(:cd) do
        :yx.t
      end
    end
  end

  assert_equal("ab:cd", r.scope)

  r = catch_exception(Localite::Translate::Missing) do 
    Localite.scope(:ab) do
      Localite.scope!(:cd) do
        assert_equal([:cd], Localite.current_scope)
      end
      assert_equal([:ab], Localite.current_scope)

      Localite.scope!(:cd) do
        :yx.t
      end
    end
  end

  assert_equal("cd", r.scope)
end

#test_text_formatObject



287
288
289
# File 'lib/localite.rb', line 287

def test_text_format
  assert_equal "a > c", Localite.format(:text) { "param".t(:xxx => "a > c") }
end

#test_tmplObject

make sure .t actually runs the Template engine



89
90
91
92
# File 'lib/localite.rb', line 89

def test_tmpl
  assert_equal "xyz",                   "xyz".t(:xyz => "abc")
  assert_equal "abc",                   "{*xyz*}".t(:xyz => "abc")
end

#test_unavailableObject



313
314
315
316
317
318
# File 'lib/localite.rb', line 313

def test_unavailable
  Localite.locale("unknown") do
    assert_equal(:en, Localite.current_locale)
    assert_equal("This is hypertext", :title.t)
  end
end