55ff9529c649d94c62ea10432776bfeaa8a92585
[packages/precise/mcollective.git] / lib / mcollective / vendor / i18n / lib / i18n / tests / procs.rb
1 # encoding: utf-8
2
3 module I18n
4   module Tests
5     module Procs
6       test "lookup: given a translation is a proc it calls the proc with the key and interpolation values" do
7         I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
8         assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(:a_lambda, :foo => 'foo')
9       end
10
11       test "defaults: given a default is a Proc it calls it with the key and interpolation values" do
12         proc = lambda { |*args| filter_args(*args) }
13         assert_equal '[nil, {:foo=>"foo"}]', I18n.t(nil, :default => proc, :foo => 'foo')
14       end
15
16       test "defaults: given a default is a key that resolves to a Proc it calls it with the key and interpolation values" do
17         I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
18         assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(nil, :default => :a_lambda, :foo => 'foo')
19         assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(nil, :default => [nil, :a_lambda], :foo => 'foo')
20       end
21
22       test "interpolation: given an interpolation value is a lambda it calls it with key and values before interpolating it" do
23         proc = lambda { |*args| filter_args(*args) }
24         assert_match %r(\[\{:foo=>#<Proc.*>\}\]), I18n.t(nil, :default => '%{foo}', :foo => proc)
25       end
26
27       test "interpolation: given a key resolves to a Proc that returns a string then interpolation still works" do
28         proc = lambda { |*args| "%{foo}: " + filter_args(*args) }
29         assert_equal 'foo: [nil, {:foo=>"foo"}]', I18n.t(nil, :default => proc, :foo => 'foo')
30       end
31
32       test "pluralization: given a key resolves to a Proc that returns valid data then pluralization still works" do
33         proc = lambda { |*args| { :zero => 'zero', :one => 'one', :other => 'other' } }
34         assert_equal 'zero',  I18n.t(:default => proc, :count => 0)
35         assert_equal 'one',   I18n.t(:default => proc, :count => 1)
36         assert_equal 'other', I18n.t(:default => proc, :count => 2)
37       end
38
39       test "lookup: given the option :resolve => false was passed it does not resolve proc translations" do
40         I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
41         assert_equal Proc, I18n.t(:a_lambda, :resolve => false).class
42       end
43
44       test "lookup: given the option :resolve => false was passed it does not resolve proc default" do
45         assert_equal Proc, I18n.t(nil, :default => lambda { |*args| filter_args(*args) }, :resolve => false).class
46       end
47
48       protected
49
50       def filter_args(*args)
51         args.map {|arg| arg.delete(:fallback) if arg.is_a?(Hash) ; arg }.inspect
52       end
53     end
54   end
55 end