File: lookup.rb

package info (click to toggle)
libi18n-ruby 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 576 kB
  • ctags: 619
  • sloc: ruby: 4,655; makefile: 5
file content (70 lines) | stat: -rw-r--r-- 2,666 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
# encoding: utf-8

module Tests
  module Api
    module Lookup
      def setup
        super
        store_translations(:foo => { :bar => 'bar', :baz => 'baz' }, :bla => false,
          :string => "a", :array => %w(a b c), :hash => { "a" => "b" })
      end

      define_method "test lookup: it returns a string" do
        assert_equal("a", I18n.t(:string))
      end

      define_method "test lookup: it returns hash" do
        assert_equal({ :a => "b" }, I18n.t(:hash))
      end

      define_method "test lookup: it returns a array" do
        assert_equal(%w(a b c), I18n.t(:array))
      end

      define_method "test lookup: it returns a native false" do
        assert_equal false, I18n.t(:bla)
      end

      define_method "test lookup: given a missing key, no default and no raise option it returns an error message" do
        assert_equal "translation missing: en, missing", I18n.t(:missing)
      end

      define_method "test lookup: given a missing key, no default and the raise option it raises MissingTranslationData" do
        assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
      end

      define_method "test lookup: does not raise an exception if no translation data is present for the given locale" do
        assert_nothing_raised { I18n.t(:foo, :locale => :xx) }
      end

      define_method "test lookup: given an array of keys it translates all of them" do
        assert_equal %w(bar baz), I18n.t([:bar, :baz], :scope => [:foo])
      end

      define_method "test lookup: using a custom scope separator" do
        # data must have been stored using the custom separator when using the ActiveRecord backend
        I18n.backend.store_translations(:en, { :foo => { :bar => 'bar' } }, { :separator => '|' })
        assert_equal 'bar', I18n.t('foo|bar', :separator => '|')
      end

      # In fact it probably *should* fail but Rails currently relies on using the default locale instead.
      # So we'll stick to this for now until we get it fixed in Rails.
      define_method "test lookup: given nil as a locale it does not raise but use the default locale" do
        # assert_raise(I18n::InvalidLocale) { I18n.t(:bar, :locale => nil) }
        assert_nothing_raised { I18n.t(:bar, :locale => nil) }
      end

      define_method "test lookup: a resulting String is not frozen" do
        assert !I18n.t(:string).frozen?
      end

      define_method "test lookup: a resulting Array is not frozen" do
        assert !I18n.t(:array).frozen?
      end

      define_method "test lookup: a resulting Hash is not frozen" do
        assert !I18n.t(:hash).frozen?
      end
    end
  end
end