diff options
| author | hukl <contact@smyck.org> | 2010-01-14 22:21:43 +0100 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2010-01-14 22:21:43 +0100 |
| commit | 57d1382013c85a7b11ac8ce5e683f6006b12b328 (patch) | |
| tree | 19646c4fa5952fa1cf0a2c874952affa346373f1 /vendor/plugins/globalize2/test/backends/chained_test.rb | |
| parent | 1b86bf5f2e35d1820bfa32d979bcc2d9ff9a9a8e (diff) | |
Updated globalize2 to latest version. Modified existing code accoringly
Diffstat (limited to 'vendor/plugins/globalize2/test/backends/chained_test.rb')
| -rw-r--r-- | vendor/plugins/globalize2/test/backends/chained_test.rb | 175 |
1 files changed, 0 insertions, 175 deletions
diff --git a/vendor/plugins/globalize2/test/backends/chained_test.rb b/vendor/plugins/globalize2/test/backends/chained_test.rb deleted file mode 100644 index bb0b3e05..00000000 --- a/vendor/plugins/globalize2/test/backends/chained_test.rb +++ /dev/null | |||
| @@ -1,175 +0,0 @@ | |||
| 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.locale = :en | ||
| 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 | ||
| 67 | end | ||
| 68 | |||
| 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, {}) | ||
| 72 | I18n.translate :foo | ||
| 73 | end | ||
| 74 | |||
| 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 | ||
| 80 | end | ||
| 81 | |||
| 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 | ||
| 87 | end | ||
| 88 | |||
| 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 ) | ||
| 94 | end | ||
| 95 | |||
| 96 | test "raises a MissingTranslationData exception if no translation was found" do | ||
| 97 | assert_raise( I18n::MissingTranslationData ) { I18n.translate :not_here, :raise => true } | ||
| 98 | end | ||
| 99 | |||
| 100 | test "raises an InvalidLocale exception if the locale is nil" do | ||
| 101 | assert_raise( I18n::InvalidLocale ) { Globalize::Backend::Chain.new.translate nil, :foo } | ||
| 102 | end | ||
| 103 | |||
| 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 ) | ||
| 109 | end | ||
| 110 | |||
| 111 | test "still calls #translate on all the backends" do | ||
| 112 | @last_backend.expects :translate | ||
| 113 | I18n.translate :not_here, :default => 'default' | ||
| 114 | end | ||
| 115 | |||
| 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 | ||
| 119 | end | ||
| 120 | |||
| 121 | end | ||
| 122 | |||
| 123 | class CustomLocalizeBackend < I18n::Backend::Simple | ||
| 124 | def localize(locale, object, format = :default) | ||
| 125 | "result from custom localize backend" if locale == 'custom' | ||
| 126 | end | ||
| 127 | end | ||
| 128 | |||
| 129 | class LocalizeChainedTest < ActiveSupport::TestCase | ||
| 130 | def setup | ||
| 131 | I18n.locale = :en | ||
| 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 | ||
| 137 | @time = Time.now | ||
| 138 | end | ||
| 139 | |||
| 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) | ||
| 143 | I18n.localize @time | ||
| 144 | end | ||
| 145 | |||
| 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 | ||
| 150 | end | ||
| 151 | |||
| 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 | ||
| 156 | end | ||
| 157 | end | ||
| 158 | |||
| 159 | class NamespaceChainedTest < ActiveSupport::TestCase | ||
| 160 | def setup | ||
| 161 | @backend = Globalize::Backend::Chain.new | ||
| 162 | end | ||
| 163 | |||
| 164 | test "returns false if the given result is not a Hash" do | ||
| 165 | assert !@backend.send(:namespace_lookup?, 'foo', {}) | ||
| 166 | end | ||
| 167 | |||
| 168 | test "returns false if a count option is present" do | ||
| 169 | assert !@backend.send(:namespace_lookup?, {:foo => 'foo'}, {:count => 1}) | ||
| 170 | end | ||
| 171 | |||
| 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'}, {}) | ||
| 174 | end | ||
| 175 | end | ||
