File: exceptions_test.rb

package info (click to toggle)
ruby-i18n 0.6.0-3%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 592 kB
  • sloc: ruby: 4,306; makefile: 3
file content (120 lines) | stat: -rw-r--r-- 4,547 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
require 'test_helper'

class I18nExceptionsTest < Test::Unit::TestCase
  def test_invalid_locale_stores_locale
    force_invalid_locale
  rescue I18n::ArgumentError => e
    assert_nil exception.locale
  end

  test "passing an invalid locale raises an InvalidLocale exception" do
    force_invalid_locale do |exception|
      assert_equal 'nil is not a valid locale', exception.message
    end
  end

  test "MissingTranslationData exception stores locale, key and options" do
    force_missing_translation_data do |exception|
      assert_equal 'de', exception.locale
      assert_equal :foo, exception.key
      assert_equal({:scope => :bar}, exception.options)
    end
  end

  test "MissingTranslationData message contains the locale and scoped key" do
    force_missing_translation_data do |exception|
      assert_equal 'translation missing: de.bar.foo', exception.message
    end
  end

  test "MissingTranslationData html_message is a span with the titlelized last key token" do
    exception = I18n::MissingTranslationData.new(:de, :foo, :scope => :bar)
    assert_equal '<span class="translation_missing" title="translation missing: de.bar.foo">Foo</span>', exception.html_message
  end

  test "MissingTranslationData html_message html escapes key names" do
    exception = I18n::MissingTranslationData.new(:de, '<script>Evil</script>', :scope => '<iframe src="example.com" />')
    assert_equal '<span class="translation_missing" title="translation missing: de.&lt;iframe src=&quot;example.com&quot; /&gt;.&lt;script&gt;Evil&lt;/script&gt;">&lt;Script&gt;Evil&lt;/Script&gt;</span>', exception.html_message
  end

  test "ExceptionHandler returns the html_message if :rescue_format => :html was given" do
    message = force_missing_translation_data(:rescue_format => :html)
    assert_equal '<span class="translation_missing" title="translation missing: de.bar.foo">Foo</span>', message
  end

  test "InvalidPluralizationData stores entry and count" do
    force_invalid_pluralization_data do |exception|
      assert_equal [:bar], exception.entry
      assert_equal 1, exception.count
    end
  end

  test "InvalidPluralizationData message contains count and data" do
    force_invalid_pluralization_data do |exception|
      assert_equal 'translation data [:bar] can not be used with :count => 1', exception.message
    end
  end

  test "MissingInterpolationArgument stores key and string" do
    assert_raise(I18n::MissingInterpolationArgument) { force_missing_interpolation_argument }
    force_missing_interpolation_argument do |exception|
      # assert_equal :bar, exception.key
      assert_equal "%{bar}", exception.string
    end
  end

  test "MissingInterpolationArgument message contains the missing and given arguments" do
    force_missing_interpolation_argument do |exception|
      assert_equal 'missing interpolation argument in "%{bar}" ({:baz=>"baz"} given)', exception.message
    end
  end

  test "ReservedInterpolationKey stores key and string" do
    force_reserved_interpolation_key do |exception|
      assert_equal :scope, exception.key
      assert_equal "%{scope}", exception.string
    end
  end

  test "ReservedInterpolationKey message contains the reserved key" do
    force_reserved_interpolation_key do |exception|
      assert_equal 'reserved key :scope used in "%{scope}"', exception.message
    end
  end

  private

    def force_invalid_locale
      I18n.translate(:foo, :locale => nil)
    rescue I18n::ArgumentError => e
      block_given? ? yield(e) : raise(e)
    end

    def force_missing_translation_data(options = {})
      I18n.backend.store_translations('de', :bar => nil)
      I18n.translate(:foo, options.merge(:scope => :bar, :locale => :de))
    rescue I18n::ArgumentError => e
      block_given? ? yield(e) : raise(e)
    end

    def force_invalid_pluralization_data
      I18n.backend.store_translations('de', :foo => [:bar])
      I18n.translate(:foo, :count => 1, :locale => :de)
    rescue I18n::ArgumentError => e
      block_given? ? yield(e) : raise(e)
    end

    def force_missing_interpolation_argument
      I18n.backend.store_translations('de', :foo => "%{bar}")
      I18n.translate(:foo, :baz => 'baz', :locale => :de)
    rescue I18n::ArgumentError => e
      block_given? ? yield(e) : raise(e)
    end

    def force_reserved_interpolation_key
      I18n.backend.store_translations('de', :foo => "%{scope}")
      I18n.translate(:foo, :baz => 'baz', :locale => :de)
    rescue I18n::ArgumentError => e
      block_given? ? yield(e) : raise(e)
    end
end