1 require File.join( File.dirname(__FILE__), '..', 'test_helper' )
2 require 'globalize/backend/chain'
7 def translate(locale, key, options = {})
13 class ChainedTest < ActiveSupport::TestCase
15 test "instantiates a chained backend and sets test as backend" do
16 assert_nothing_raised { I18n.chain_backends }
17 assert_instance_of Globalize::Backend::Chain, I18n.backend
20 test "passes all given arguments to the chained backends #initialize method" do
21 Globalize::Backend::Chain.expects(:new).with(:spec, :simple)
22 I18n.chain_backends :spec, :simple
25 test "passes all given arguments to #add assuming that they are backends" do
26 # no idea how to spec that
30 class AddChainedTest < ActiveSupport::TestCase
32 I18n.backend = Globalize::Backend::Chain.new
35 test "accepts an instance of a backend" do
36 assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy.new }
37 assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first
40 test "accepts a class and instantiates the backend" do
41 assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy }
42 assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first
45 test "accepts a symbol, constantizes test as a backend class and instantiates the backend" do
46 assert_nothing_raised { I18n.backend.add :dummy }
47 assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first
50 test "accepts any number of backend instances, classes or symbols" do
51 assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy.new, Globalize::Backend::Dummy, :dummy }
52 assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first
53 assert_equal [ Globalize::Backend::Dummy, Globalize::Backend::Dummy, Globalize::Backend::Dummy ],
54 I18n.backend.send(:backends).map{|backend| backend.class }
59 class TranslateChainedTest < ActiveSupport::TestCase
62 I18n.backend = Globalize::Backend::Chain.new
63 @first_backend = I18n::Backend::Simple.new
64 @last_backend = I18n::Backend::Simple.new
65 I18n.backend.add @first_backend
66 I18n.backend.add @last_backend
69 test "delegates #translate to all backends in the order they were added" do
70 @first_backend.expects(:translate).with(:en, :foo, {})
71 @last_backend.expects(:translate).with(:en, :foo, {})
75 test "returns the result from #translate from the first backend if test's not nil" do
76 @first_backend.store_translations :en, {:foo => 'foo from first backend'}
77 @last_backend.store_translations :en, {:foo => 'foo from last backend'}
78 result = I18n.translate :foo
79 assert_equal 'foo from first backend', result
82 test "returns the result from #translate from the second backend if the first one returned nil" do
83 @first_backend.store_translations :en, {}
84 @last_backend.store_translations :en, {:foo => 'foo from last backend'}
85 result = I18n.translate :foo
86 assert_equal 'foo from last backend', result
89 test "looks up a namespace from all backends and merges them (if a result is a hash and no count option is present)" do
90 @first_backend.store_translations :en, {:foo => {:bar => 'bar from first backend'}}
91 @last_backend.store_translations :en, {:foo => {:baz => 'baz from last backend'}}
92 result = I18n.translate :foo
93 assert_equal( {:bar => 'bar from first backend', :baz => 'baz from last backend'}, result )
96 test "raises a MissingTranslationData exception if no translation was found" do
97 assert_raise( I18n::MissingTranslationData ) { I18n.translate :not_here, :raise => true }
100 test "raises an InvalidLocale exception if the locale is nil" do
101 assert_raise( I18n::InvalidLocale ) { Globalize::Backend::Chain.new.translate nil, :foo }
104 test "bulk translates a number of keys from different backends" do
105 @first_backend.store_translations :en, {:foo => 'foo from first backend'}
106 @last_backend.store_translations :en, {:bar => 'bar from last backend'}
107 result = I18n.translate [:foo, :bar]
108 assert_equal( ['foo from first backend', 'bar from last backend'], result )
111 test "still calls #translate on all the backends" do
112 @last_backend.expects :translate
113 I18n.translate :not_here, :default => 'default'
116 test "returns a given default string when no backend returns a translation" do
117 result = I18n.translate :not_here, :default => 'default'
118 assert_equal 'default', result
123 class CustomLocalizeBackend < I18n::Backend::Simple
124 def localize(locale, object, format = :default)
125 "result from custom localize backend" if locale == 'custom'
129 class LocalizeChainedTest < ActiveSupport::TestCase
132 I18n.backend = Globalize::Backend::Chain.new
133 @first_backend = CustomLocalizeBackend.new
134 @last_backend = I18n::Backend::Simple.new
135 I18n.backend.add @first_backend
136 I18n.backend.add @last_backend
140 test "delegates #localize to all backends in the order they were added" do
141 @first_backend.expects(:localize).with(:en, @time, :default)
142 @last_backend.expects(:localize).with(:en, @time, :default)
146 test "returns the result from #localize from the first backend if test's not nil" do
147 @last_backend.expects(:localize).never
148 result = I18n.localize @time, :locale => 'custom'
149 assert_equal 'result from custom localize backend', result
152 test "returns the result from #localize from the second backend if the first one returned nil" do
153 @last_backend.expects(:localize).returns "value from last backend"
154 result = I18n.localize @time
155 assert_equal 'value from last backend', result
159 class NamespaceChainedTest < ActiveSupport::TestCase
161 @backend = Globalize::Backend::Chain.new
164 test "returns false if the given result is not a Hash" do
165 assert !@backend.send(:namespace_lookup?, 'foo', {})
168 test "returns false if a count option is present" do
169 assert !@backend.send(:namespace_lookup?, {:foo => 'foo'}, {:count => 1})
172 test "returns true if the given result is a Hash AND no count option is present" do
173 assert @backend.send(:namespace_lookup?, {:foo => 'foo'}, {})