diff options
| author | hukl <hukl@eight.local> | 2009-02-07 15:50:40 +0100 |
|---|---|---|
| committer | hukl <hukl@eight.local> | 2009-02-07 15:50:40 +0100 |
| commit | 36a2f1f3c085dd81171cf7d8220770d4ff921ab3 (patch) | |
| tree | 5ded15331b192bf428af4c94d46d1abb28ef0690 /vendor/plugins/globalize2/test/backends/chained_test.rb | |
| parent | ce9645d0092d42c7bf8781378181ffbd4ff3d088 (diff) | |
added globalize2 plugin as well as some modifications
which use the new translation facilities.
backend mostly.
Diffstat (limited to 'vendor/plugins/globalize2/test/backends/chained_test.rb')
| -rw-r--r-- | vendor/plugins/globalize2/test/backends/chained_test.rb | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/vendor/plugins/globalize2/test/backends/chained_test.rb b/vendor/plugins/globalize2/test/backends/chained_test.rb new file mode 100644 index 00000000..bc451791 --- /dev/null +++ b/vendor/plugins/globalize2/test/backends/chained_test.rb | |||
| @@ -0,0 +1,174 @@ | |||
| 1 | require File.join( File.dirname(__FILE__), '..', 'test_helper' ) | ||
| 2 | require 'globalize/backend/chain' | ||
| 3 | |||
| 4 | module Globalize | ||
| 5 | module Backend | ||
| 6 | class Dummy | ||
| 7 | def translate(locale, key, options = {}) | ||
| 8 | end | ||
| 9 | end | ||
| 10 | end | ||
| 11 | end | ||
| 12 | |||
| 13 | class ChainedTest < ActiveSupport::TestCase | ||
| 14 | |||
| 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 | ||
| 18 | end | ||
| 19 | |||
| 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 | ||
| 23 | end | ||
| 24 | |||
| 25 | test "passes all given arguments to #add assuming that they are backends" do | ||
| 26 | # no idea how to spec that | ||
| 27 | end | ||
| 28 | end | ||
| 29 | |||
| 30 | class AddChainedTest < ActiveSupport::TestCase | ||
| 31 | def setup | ||
| 32 | I18n.backend = Globalize::Backend::Chain.new | ||
| 33 | end | ||
| 34 | |||
| 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 | ||
| 38 | end | ||
| 39 | |||
| 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 | ||
| 43 | end | ||
| 44 | |||
| 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 | ||
| 48 | end | ||
| 49 | |||
| 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 } | ||
| 55 | end | ||
| 56 | |||
| 57 | end | ||
| 58 | |||
| 59 | class TranslateChainedTest < ActiveSupport::TestCase | ||
| 60 | def setup | ||
| 61 | I18n.backend = Globalize::Backend::Chain.new | ||
| 62 | @first_backend = I18n::Backend::Simple.new | ||
| 63 | @last_backend = I18n::Backend::Simple.new | ||
| 64 | I18n.backend.add @first_backend | ||
| 65 | I18n.backend.add @last_backend | ||
| 66 | end | ||
| 67 | |||
| 68 | test "delegates #translate to all backends in the order they were added" do | ||
| 69 | @first_backend.expects(:translate).with(:en, :foo, {}) | ||
| 70 | @last_backend.expects(:translate).with(:en, :foo, {}) | ||
| 71 | I18n.translate :foo | ||
| 72 | end | ||
| 73 | |||
| 74 | test "returns the result from #translate from the first backend if test's not nil" do | ||
| 75 | @first_backend.store_translations :en, {:foo => 'foo from first backend'} | ||
| 76 | @last_backend.store_translations :en, {:foo => 'foo from last backend'} | ||
| 77 | result = I18n.translate :foo | ||
| 78 | assert_equal 'foo from first backend', result | ||
| 79 | end | ||
| 80 | |||
| 81 | test "returns the result from #translate from the second backend if the first one returned nil" do | ||
| 82 | @first_backend.store_translations :en, {} | ||
| 83 | @last_backend.store_translations :en, {:foo => 'foo from last backend'} | ||
| 84 | result = I18n.translate :foo | ||
| 85 | assert_equal 'foo from last backend', result | ||
| 86 | end | ||
| 87 | |||
| 88 | test "looks up a namespace from all backends and merges them (if a result is a hash and no count option is present)" do | ||
| 89 | @first_backend.store_translations :en, {:foo => {:bar => 'bar from first backend'}} | ||
| 90 | @last_backend.store_translations :en, {:foo => {:baz => 'baz from last backend'}} | ||
| 91 | result = I18n.translate :foo | ||
| 92 | assert_equal( {:bar => 'bar from first backend', :baz => 'baz from last backend'}, result ) | ||
| 93 | end | ||
| 94 | |||
| 95 | test "raises a MissingTranslationData exception if no translation was found" do | ||
| 96 | assert_raise( I18n::MissingTranslationData ) { I18n.translate :not_here, :raise => true } | ||
| 97 | end | ||
| 98 | |||
| 99 | test "raises an InvalidLocale exception if the locale is nil" do | ||
| 100 | assert_raise( I18n::InvalidLocale ) { Globalize::Backend::Chain.new.translate nil, :foo } | ||
| 101 | end | ||
| 102 | |||
| 103 | test "bulk translates a number of keys from different backends" do | ||
| 104 | @first_backend.store_translations :en, {:foo => 'foo from first backend'} | ||
| 105 | @last_backend.store_translations :en, {:bar => 'bar from last backend'} | ||
| 106 | result = I18n.translate [:foo, :bar] | ||
| 107 | assert_equal( ['foo from first backend', 'bar from last backend'], result ) | ||
| 108 | end | ||
| 109 | |||
| 110 | test "still calls #translate on all the backends" do | ||
| 111 | @last_backend.expects :translate | ||
| 112 | I18n.translate :not_here, :default => 'default' | ||
| 113 | end | ||
| 114 | |||
| 115 | test "returns a given default string when no backend returns a translation" do | ||
| 116 | result = I18n.translate :not_here, :default => 'default' | ||
| 117 | assert_equal 'default', result | ||
| 118 | end | ||
| 119 | |||
| 120 | end | ||
| 121 | |||
| 122 | class CustomLocalizeBackend < I18n::Backend::Simple | ||
| 123 | def localize(locale, object, format = :default) | ||
| 124 | "result from custom localize backend" if locale == 'custom' | ||
| 125 | end | ||
| 126 | end | ||
| 127 | |||
| 128 | class LocalizeChainedTest < ActiveSupport::TestCase | ||
| 129 | def setup | ||
| 130 | I18n.locale = :en | ||
| 131 | I18n.backend = Globalize::Backend::Chain.new | ||
| 132 | @first_backend = CustomLocalizeBackend.new | ||
| 133 | @last_backend = I18n::Backend::Simple.new | ||
| 134 | I18n.backend.add @first_backend | ||
| 135 | I18n.backend.add @last_backend | ||
| 136 | @time = Time.now | ||
| 137 | end | ||
| 138 | |||
| 139 | test "delegates #localize to all backends in the order they were added" do | ||
| 140 | @first_backend.expects(:localize).with(:en, @time, :default) | ||
| 141 | @last_backend.expects(:localize).with(:en, @time, :default) | ||
| 142 | I18n.localize @time | ||
| 143 | end | ||
| 144 | |||
| 145 | test "returns the result from #localize from the first backend if test's not nil" do | ||
| 146 | @last_backend.expects(:localize).never | ||
| 147 | result = I18n.localize @time, :locale => 'custom' | ||
| 148 | assert_equal 'result from custom localize backend', result | ||
| 149 | end | ||
| 150 | |||
| 151 | test "returns the result from #localize from the second backend if the first one returned nil" do | ||
| 152 | @last_backend.expects(:localize).returns "value from last backend" | ||
| 153 | result = I18n.localize @time | ||
| 154 | assert_equal 'value from last backend', result | ||
| 155 | end | ||
| 156 | end | ||
| 157 | |||
| 158 | class NamespaceChainedTest < ActiveSupport::TestCase | ||
| 159 | def setup | ||
| 160 | @backend = Globalize::Backend::Chain.new | ||
| 161 | end | ||
| 162 | |||
| 163 | test "returns false if the given result is not a Hash" do | ||
| 164 | assert !@backend.send(:namespace_lookup?, 'foo', {}) | ||
| 165 | end | ||
| 166 | |||
| 167 | test "returns false if a count option is present" do | ||
| 168 | assert !@backend.send(:namespace_lookup?, {:foo => 'foo'}, {:count => 1}) | ||
| 169 | end | ||
| 170 | |||
| 171 | test "returns true if the given result is a Hash AND no count option is present" do | ||
| 172 | assert @backend.send(:namespace_lookup?, {:foo => 'foo'}, {}) | ||
| 173 | end | ||
| 174 | end | ||
