Updated mcollective.init according to OSCI-658
[packages/precise/mcollective.git] / lib / mcollective / vendor / i18n / lib / i18n / tests / interpolation.rb
1 # encoding: utf-8
2
3 module I18n
4   module Tests
5     module Interpolation
6       # If no interpolation parameter is not given, I18n should not alter the string.
7       # This behavior is due to three reasons:
8       #
9       #   * Checking interpolation keys in all strings hits performance, badly;
10       #
11       #   * This allows us to retrieve untouched values through I18n. For example
12       #     I could have a middleware that returns I18n lookup results in JSON
13       #     to be processed through Javascript. Leaving the keys untouched allows
14       #     the interpolation to happen at the javascript level;
15       #
16       #   * Security concerns: if I allow users to translate a web site, they can
17       #     insert %{} in messages causing the I18n lookup to fail in every request.
18       #
19       test "interpolation: given no values it does not alter the string" do
20         assert_equal 'Hi %{name}!', interpolate(:default => 'Hi %{name}!')
21       end
22
23       test "interpolation: given values it interpolates them into the string" do
24         assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => 'David')
25       end
26
27       test "interpolation: given a nil value it still interpolates it into the string" do
28         assert_equal 'Hi !', interpolate(:default => 'Hi %{name}!', :name => nil)
29       end
30
31       test "interpolation: given a lambda as a value it calls it if the string contains the key" do
32         assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => lambda { |*args| 'David' })
33       end
34
35       test "interpolation: given a lambda as a value it does not call it if the string does not contain the key" do
36         assert_nothing_raised { interpolate(:default => 'Hi!', :name => lambda { |*args| raise 'fail' }) }
37       end
38
39       test "interpolation: given values but missing a key it raises I18n::MissingInterpolationArgument" do
40         assert_raise(I18n::MissingInterpolationArgument) do
41           interpolate(:default => '%{foo}', :bar => 'bar')
42         end
43       end
44
45       test "interpolation: it does not raise I18n::MissingInterpolationArgument for escaped variables" do
46         assert_nothing_raised(I18n::MissingInterpolationArgument) do
47           assert_equal 'Barr %{foo}', interpolate(:default => '%{bar} %%{foo}', :bar => 'Barr')
48         end
49       end
50
51       test "interpolation: it does not change the original, stored translation string" do
52         I18n.backend.store_translations(:en, :interpolate => 'Hi %{name}!')
53         assert_equal 'Hi David!', interpolate(:interpolate, :name => 'David')
54         assert_equal 'Hi Yehuda!', interpolate(:interpolate, :name => 'Yehuda')
55       end
56
57       test "interpolation: given the translation is in utf-8 it still works" do
58         assert_equal 'Häi David!', interpolate(:default => 'Häi %{name}!', :name => 'David')
59       end
60
61       test "interpolation: given the value is in utf-8 it still works" do
62         assert_equal 'Hi ゆきひろ!', interpolate(:default => 'Hi %{name}!', :name => 'ゆきひろ')
63       end
64
65       test "interpolation: given the translation and the value are in utf-8 it still works" do
66         assert_equal 'こんにちは、ゆきひろさん!', interpolate(:default => 'こんにちは、%{name}さん!', :name => 'ゆきひろ')
67       end
68
69       if Kernel.const_defined?(:Encoding)
70         test "interpolation: given a euc-jp translation and a utf-8 value it raises Encoding::CompatibilityError" do
71           assert_raise(Encoding::CompatibilityError) do
72             interpolate(:default => euc_jp('こんにちは、%{name}さん!'), :name => 'ゆきひろ')
73           end
74         end
75
76         test "interpolation: given a utf-8 translation and a euc-jp value it raises Encoding::CompatibilityError" do
77           assert_raise(Encoding::CompatibilityError) do
78             interpolate(:default => 'こんにちは、%{name}さん!', :name => euc_jp('ゆきひろ'))
79           end
80         end
81
82         test "interpolation: ASCII strings in the backend should be encoded to UTF8 if interpolation options are in UTF8" do
83           I18n.backend.store_translations 'en', 'encoding' => ('%{who} let me go'.force_encoding("ASCII"))
84           result = I18n.t 'encoding', :who => "måmmå miå"
85           assert_equal Encoding::UTF_8, result.encoding
86         end
87
88         test "interpolation: UTF8 strings in the backend are still returned as UTF8 with ASCII interpolation" do
89           I18n.backend.store_translations 'en', 'encoding' => 'måmmå miå %{what}'
90           result = I18n.t 'encoding', :what => 'let me go'.force_encoding("ASCII")
91           assert_equal Encoding::UTF_8, result.encoding
92         end
93
94         test "interpolation: UTF8 strings in the backend are still returned as UTF8 even with numbers interpolation" do
95           I18n.backend.store_translations 'en', 'encoding' => '%{count} times: måmmå miå'
96           result = I18n.t 'encoding', :count => 3
97           assert_equal Encoding::UTF_8, result.encoding
98         end
99       end
100
101       test "interpolation: given a translations containing a reserved key it raises I18n::ReservedInterpolationKey" do
102         assert_raise(I18n::ReservedInterpolationKey) { interpolate(:default => '%{default}',   :foo => :bar) }
103         assert_raise(I18n::ReservedInterpolationKey) { interpolate(:default => '%{scope}',     :foo => :bar) }
104         assert_raise(I18n::ReservedInterpolationKey) { interpolate(:default => '%{separator}', :foo => :bar) }
105       end
106
107       protected
108
109       def capture(stream)
110         begin
111           stream = stream.to_s
112           eval "$#{stream} = StringIO.new"
113           yield
114           result = eval("$#{stream}").string
115         ensure
116           eval("$#{stream} = #{stream.upcase}")
117         end
118
119         result
120       end
121
122       def euc_jp(string)
123         string.encode!(Encoding::EUC_JP)
124       end
125
126       def interpolate(*args)
127         options = args.last.is_a?(Hash) ? args.pop : {}
128         key = args.pop
129         I18n.backend.translate('en', key, options)
130       end
131     end
132   end
133 end