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 | |
| 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')
3 files changed, 380 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 0000000..bc45179 --- /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 | ||
diff --git a/vendor/plugins/globalize2/test/backends/pluralizing_test.rb b/vendor/plugins/globalize2/test/backends/pluralizing_test.rb new file mode 100644 index 0000000..7445e3f --- /dev/null +++ b/vendor/plugins/globalize2/test/backends/pluralizing_test.rb | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | require File.join( File.dirname(__FILE__), '..', 'test_helper' ) | ||
| 2 | require 'globalize/backend/pluralizing' | ||
| 3 | |||
| 4 | class PluralizingTest < ActiveSupport::TestCase | ||
| 5 | def setup | ||
| 6 | @backend = Globalize::Backend::Pluralizing.new | ||
| 7 | @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other } | ||
| 8 | end | ||
| 9 | |||
| 10 | test "#pluralizer returns the pluralizer for a given locale if defined" do | ||
| 11 | assert_instance_of Proc, @backend.pluralizer(:en) | ||
| 12 | end | ||
| 13 | |||
| 14 | test "#pluralizer returns the default pluralizer if no pluralizer is defined for the given locale" do | ||
| 15 | assert_equal @backend.pluralizer(:en), @backend.pluralizer(:de) | ||
| 16 | end | ||
| 17 | |||
| 18 | test "#add_pluralizer allows to store a pluralizer per locale" do | ||
| 19 | assert_nothing_raised { @backend.add_pluralizer(:cz, @cz_pluralizer) } | ||
| 20 | assert_equal @cz_pluralizer, @backend.pluralizer(:cz) | ||
| 21 | end | ||
| 22 | |||
| 23 | end | ||
| 24 | |||
| 25 | class PluralizePluralizingTest < ActiveSupport::TestCase | ||
| 26 | def setup | ||
| 27 | @backend = Globalize::Backend::Pluralizing.new | ||
| 28 | @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other } | ||
| 29 | @backend.store_translations :en, :foo => {:one => 'one en foo', :other => 'many en foos'} | ||
| 30 | @backend.store_translations :cz, :foo => {:one => 'one cz foo', :few => 'few cz foos', :other => 'many cz foos'} | ||
| 31 | end | ||
| 32 | |||
| 33 | test "looks up the :one translation when count is 1" do | ||
| 34 | assert_equal 'one en foo', @backend.translate(:en, :foo, :count => 1) | ||
| 35 | end | ||
| 36 | |||
| 37 | test "looks up the :other translation when count is 2" do | ||
| 38 | assert_equal 'many en foos', @backend.translate(:en, :foo, :count => 2) | ||
| 39 | end | ||
| 40 | end | ||
| 41 | |||
| 42 | class CzPluralizingTest < ActiveSupport::TestCase | ||
| 43 | def setup | ||
| 44 | @backend = Globalize::Backend::Pluralizing.new | ||
| 45 | @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other } | ||
| 46 | @backend.store_translations :en, :foo => {:one => 'one en foo', :other => 'many en foos'} | ||
| 47 | @backend.store_translations :cz, :foo => {:one => 'one cz foo', :few => 'few cz foos', :other => 'many cz foos'} | ||
| 48 | @backend.add_pluralizer(:cz, @cz_pluralizer) | ||
| 49 | end | ||
| 50 | |||
| 51 | test "looks up the :one translation when count is 1 (:cz)" do | ||
| 52 | assert_equal 'one cz foo', @backend.translate(:cz, :foo, :count => 1) | ||
| 53 | end | ||
| 54 | |||
| 55 | test "looks up the :few translation when count is 2 (:cz)" do | ||
| 56 | assert_equal 'few cz foos', @backend.translate(:cz, :foo, :count => 2) | ||
| 57 | end | ||
| 58 | |||
| 59 | test "looks up the :other translation when count is 5 (:cz)" do | ||
| 60 | assert_equal 'many cz foos', @backend.translate(:cz, :foo, :count => 5) | ||
| 61 | end | ||
| 62 | |||
| 63 | end | ||
diff --git a/vendor/plugins/globalize2/test/backends/static_test.rb b/vendor/plugins/globalize2/test/backends/static_test.rb new file mode 100644 index 0000000..133e547 --- /dev/null +++ b/vendor/plugins/globalize2/test/backends/static_test.rb | |||
| @@ -0,0 +1,143 @@ | |||
| 1 | require File.join( File.dirname(__FILE__), '..', 'test_helper' ) | ||
| 2 | require 'globalize/backend/static' | ||
| 3 | require 'globalize/translation' | ||
| 4 | require 'action_view' | ||
| 5 | include ActionView::Helpers::NumberHelper | ||
| 6 | |||
| 7 | I18n.locale = :'en-US' # Need to set this, since I18n defaults to 'en' | ||
| 8 | |||
| 9 | class StaticTest < ActiveSupport::TestCase | ||
| 10 | def setup | ||
| 11 | I18n.backend = Globalize::Backend::Static.new | ||
| 12 | translations = {:"en-US" => {:foo => "foo in en-US", :boz => 'boz', :buz => {:bum => 'bum'}}, | ||
| 13 | :"en" => {:bar => "bar in en"}, | ||
| 14 | :"de-DE" => {:baz => "baz in de-DE"}, | ||
| 15 | :"de" => {:boo => "boo in de", :number => { :currency => { :format => { :unit => '€', :format => '%n %u'}}}}} | ||
| 16 | translations.each do |locale, data| | ||
| 17 | I18n.backend.store_translations locale, data | ||
| 18 | end | ||
| 19 | I18n.fallbacks.map :"de-DE" => :"en-US", :he => :en | ||
| 20 | end | ||
| 21 | |||
| 22 | test "returns an instance of Translation:Static" do | ||
| 23 | translation = I18n.translate :foo | ||
| 24 | assert_instance_of Globalize::Translation::Static, translation | ||
| 25 | end | ||
| 26 | |||
| 27 | test "returns the translation in en-US if present" do | ||
| 28 | assert_equal "foo in en-US", I18n.translate(:foo, :locale => :"en-US") | ||
| 29 | end | ||
| 30 | |||
| 31 | test "returns the translation in en if en-US is not present" do | ||
| 32 | assert_equal "bar in en", I18n.translate(:bar, :locale => :"en-US") | ||
| 33 | end | ||
| 34 | |||
| 35 | test "returns the translation in de-DE if present" do | ||
| 36 | assert_equal "baz in de-DE", I18n.translate(:baz, :locale => :"de-DE") | ||
| 37 | end | ||
| 38 | |||
| 39 | test "returns the translation in de if de-DE is not present" do | ||
| 40 | assert_equal "boo in de", I18n.translate(:boo, :locale => :"de-DE") | ||
| 41 | end | ||
| 42 | |||
| 43 | test "returns the translation in en-US if none of de-DE and de are present" do | ||
| 44 | assert_equal "foo in en-US", I18n.translate(:foo, :locale => :"de-DE") | ||
| 45 | end | ||
| 46 | |||
| 47 | test "returns the translation in en if none of de-DE, de and en-US are present" do | ||
| 48 | assert_equal "bar in en", I18n.translate(:bar, :locale => :"de-DE") | ||
| 49 | end | ||
| 50 | |||
| 51 | test "returns the translation in en if none in he is present" do | ||
| 52 | assert_equal "bar in en", I18n.translate(:bar, :locale => :he) | ||
| 53 | end | ||
| 54 | |||
| 55 | test "returns the given default String when the key is not present for any locale" do | ||
| 56 | assert_equal "default", I18n.translate(:missing, :default => "default") | ||
| 57 | end | ||
| 58 | |||
| 59 | test "returns the fallback translation for the key if present for a fallback locale" do | ||
| 60 | I18n.backend.store_translations :de, :non_default => "non_default in de" | ||
| 61 | assert_equal "non_default in de", I18n.translate(:non_default, :default => "default", :locale => :"de-DE") | ||
| 62 | end | ||
| 63 | |||
| 64 | test "returns an array of translations" do | ||
| 65 | assert_instance_of Array, I18n.translate([:foo, :boz]) | ||
| 66 | end | ||
| 67 | |||
| 68 | test "returns an array of instances of Translation::Static" do | ||
| 69 | assert_equal [Globalize::Translation::Static], I18n.translate([:foo, :boz]).map(&:class).uniq | ||
| 70 | end | ||
| 71 | |||
| 72 | test "returns a hash of translations" do | ||
| 73 | assert_instance_of Hash, I18n.translate(:"buz") | ||
| 74 | end | ||
| 75 | |||
| 76 | test "returns an array of translations 2" do | ||
| 77 | assert_equal [Globalize::Translation::Static], I18n.translate(:"buz").values.map(&:class) | ||
| 78 | end | ||
| 79 | |||
| 80 | test "returns currency properly formated" do | ||
| 81 | currency = number_to_currency(10) | ||
| 82 | assert_equal "$10.00", currency | ||
| 83 | end | ||
| 84 | |||
| 85 | test "returns currency properly formated for locale" do | ||
| 86 | currency = number_to_currency(10, :locale => :'de') | ||
| 87 | assert_equal "10.000 €", currency | ||
| 88 | end | ||
| 89 | |||
| 90 | test "returns currency properly formated from parameters" do | ||
| 91 | currency = number_to_currency(10, :format => "%n %u", :unit => '€') | ||
| 92 | assert_equal "10.00 €", currency | ||
| 93 | end | ||
| 94 | |||
| 95 | test "makes sure interpolation does not break even with False as string" do | ||
| 96 | assert_equal "translation missing: en, support, array, skip_last_comma", I18n.translate(:"support.array.skip_last_comma") | ||
| 97 | end | ||
| 98 | end | ||
| 99 | |||
| 100 | class TranslationStaticTest < ActiveSupport::TestCase | ||
| 101 | def setup | ||
| 102 | I18n.backend = Globalize::Backend::Static.new | ||
| 103 | translations = { | ||
| 104 | :greeting => "Hi {{name}}", | ||
| 105 | :messages => { :one => "You have one message.", :other => "You have {{count}} messages."} | ||
| 106 | } | ||
| 107 | I18n.backend.store_translations :"en", translations | ||
| 108 | end | ||
| 109 | |||
| 110 | def greeting | ||
| 111 | I18n.translate :greeting, :locale => :"en-US", :name => "Joshua" | ||
| 112 | end | ||
| 113 | |||
| 114 | test "stores the actual locale" do | ||
| 115 | assert_equal :en, greeting.locale | ||
| 116 | end | ||
| 117 | |||
| 118 | test "stores the requested locale" do | ||
| 119 | assert_equal :'en-US', greeting.requested_locale | ||
| 120 | end | ||
| 121 | |||
| 122 | test "stores the requested key" do | ||
| 123 | assert_equal :greeting, greeting.key | ||
| 124 | end | ||
| 125 | |||
| 126 | test "stores the options given to #translate" do | ||
| 127 | assert_equal( {:name => "Joshua"}, greeting.options ) | ||
| 128 | end | ||
| 129 | |||
| 130 | test "stores the original translation before test was interpolated" do | ||
| 131 | assert_equal "Hi {{name}}", greeting.original | ||
| 132 | end | ||
| 133 | |||
| 134 | test "stores the plural_key :one if pluralized as such" do | ||
| 135 | message = I18n.translate :messages, :locale => :"en-US", :count => 1 | ||
| 136 | assert_equal :one, message.plural_key | ||
| 137 | end | ||
| 138 | |||
| 139 | test "stores the plural_key :other if pluralized as such" do | ||
| 140 | messages = I18n.translate :messages, :locale => :"en-US", :count => 2 | ||
| 141 | assert_equal :other, messages.plural_key | ||
| 142 | end | ||
| 143 | end | ||
