From 36a2f1f3c085dd81171cf7d8220770d4ff921ab3 Mon Sep 17 00:00:00 2001 From: hukl Date: Sat, 7 Feb 2009 15:50:40 +0100 Subject: added globalize2 plugin as well as some modifications which use the new translation facilities. backend mostly. --- .../globalize2/test/backends/chained_test.rb | 174 +++++++++++++++++++++ .../globalize2/test/backends/pluralizing_test.rb | 63 ++++++++ .../globalize2/test/backends/static_test.rb | 143 +++++++++++++++++ 3 files changed, 380 insertions(+) create mode 100644 vendor/plugins/globalize2/test/backends/chained_test.rb create mode 100644 vendor/plugins/globalize2/test/backends/pluralizing_test.rb create mode 100644 vendor/plugins/globalize2/test/backends/static_test.rb (limited to 'vendor/plugins/globalize2/test/backends') 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 @@ +require File.join( File.dirname(__FILE__), '..', 'test_helper' ) +require 'globalize/backend/chain' + +module Globalize + module Backend + class Dummy + def translate(locale, key, options = {}) + end + end + end +end + +class ChainedTest < ActiveSupport::TestCase + + test "instantiates a chained backend and sets test as backend" do + assert_nothing_raised { I18n.chain_backends } + assert_instance_of Globalize::Backend::Chain, I18n.backend + end + + test "passes all given arguments to the chained backends #initialize method" do + Globalize::Backend::Chain.expects(:new).with(:spec, :simple) + I18n.chain_backends :spec, :simple + end + + test "passes all given arguments to #add assuming that they are backends" do + # no idea how to spec that + end +end + +class AddChainedTest < ActiveSupport::TestCase + def setup + I18n.backend = Globalize::Backend::Chain.new + end + + test "accepts an instance of a backend" do + assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy.new } + assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first + end + + test "accepts a class and instantiates the backend" do + assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy } + assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first + end + + test "accepts a symbol, constantizes test as a backend class and instantiates the backend" do + assert_nothing_raised { I18n.backend.add :dummy } + assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first + end + + test "accepts any number of backend instances, classes or symbols" do + assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy.new, Globalize::Backend::Dummy, :dummy } + assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first + assert_equal [ Globalize::Backend::Dummy, Globalize::Backend::Dummy, Globalize::Backend::Dummy ], + I18n.backend.send(:backends).map{|backend| backend.class } + end + +end + +class TranslateChainedTest < ActiveSupport::TestCase + def setup + I18n.backend = Globalize::Backend::Chain.new + @first_backend = I18n::Backend::Simple.new + @last_backend = I18n::Backend::Simple.new + I18n.backend.add @first_backend + I18n.backend.add @last_backend + end + + test "delegates #translate to all backends in the order they were added" do + @first_backend.expects(:translate).with(:en, :foo, {}) + @last_backend.expects(:translate).with(:en, :foo, {}) + I18n.translate :foo + end + + test "returns the result from #translate from the first backend if test's not nil" do + @first_backend.store_translations :en, {:foo => 'foo from first backend'} + @last_backend.store_translations :en, {:foo => 'foo from last backend'} + result = I18n.translate :foo + assert_equal 'foo from first backend', result + end + + test "returns the result from #translate from the second backend if the first one returned nil" do + @first_backend.store_translations :en, {} + @last_backend.store_translations :en, {:foo => 'foo from last backend'} + result = I18n.translate :foo + assert_equal 'foo from last backend', result + end + + test "looks up a namespace from all backends and merges them (if a result is a hash and no count option is present)" do + @first_backend.store_translations :en, {:foo => {:bar => 'bar from first backend'}} + @last_backend.store_translations :en, {:foo => {:baz => 'baz from last backend'}} + result = I18n.translate :foo + assert_equal( {:bar => 'bar from first backend', :baz => 'baz from last backend'}, result ) + end + + test "raises a MissingTranslationData exception if no translation was found" do + assert_raise( I18n::MissingTranslationData ) { I18n.translate :not_here, :raise => true } + end + + test "raises an InvalidLocale exception if the locale is nil" do + assert_raise( I18n::InvalidLocale ) { Globalize::Backend::Chain.new.translate nil, :foo } + end + + test "bulk translates a number of keys from different backends" do + @first_backend.store_translations :en, {:foo => 'foo from first backend'} + @last_backend.store_translations :en, {:bar => 'bar from last backend'} + result = I18n.translate [:foo, :bar] + assert_equal( ['foo from first backend', 'bar from last backend'], result ) + end + + test "still calls #translate on all the backends" do + @last_backend.expects :translate + I18n.translate :not_here, :default => 'default' + end + + test "returns a given default string when no backend returns a translation" do + result = I18n.translate :not_here, :default => 'default' + assert_equal 'default', result + end + +end + +class CustomLocalizeBackend < I18n::Backend::Simple + def localize(locale, object, format = :default) + "result from custom localize backend" if locale == 'custom' + end +end + +class LocalizeChainedTest < ActiveSupport::TestCase + def setup + I18n.locale = :en + I18n.backend = Globalize::Backend::Chain.new + @first_backend = CustomLocalizeBackend.new + @last_backend = I18n::Backend::Simple.new + I18n.backend.add @first_backend + I18n.backend.add @last_backend + @time = Time.now + end + + test "delegates #localize to all backends in the order they were added" do + @first_backend.expects(:localize).with(:en, @time, :default) + @last_backend.expects(:localize).with(:en, @time, :default) + I18n.localize @time + end + + test "returns the result from #localize from the first backend if test's not nil" do + @last_backend.expects(:localize).never + result = I18n.localize @time, :locale => 'custom' + assert_equal 'result from custom localize backend', result + end + + test "returns the result from #localize from the second backend if the first one returned nil" do + @last_backend.expects(:localize).returns "value from last backend" + result = I18n.localize @time + assert_equal 'value from last backend', result + end +end + +class NamespaceChainedTest < ActiveSupport::TestCase + def setup + @backend = Globalize::Backend::Chain.new + end + + test "returns false if the given result is not a Hash" do + assert !@backend.send(:namespace_lookup?, 'foo', {}) + end + + test "returns false if a count option is present" do + assert !@backend.send(:namespace_lookup?, {:foo => 'foo'}, {:count => 1}) + end + + test "returns true if the given result is a Hash AND no count option is present" do + assert @backend.send(:namespace_lookup?, {:foo => 'foo'}, {}) + end +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 00000000..7445e3ff --- /dev/null +++ b/vendor/plugins/globalize2/test/backends/pluralizing_test.rb @@ -0,0 +1,63 @@ +require File.join( File.dirname(__FILE__), '..', 'test_helper' ) +require 'globalize/backend/pluralizing' + +class PluralizingTest < ActiveSupport::TestCase + def setup + @backend = Globalize::Backend::Pluralizing.new + @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other } + end + + test "#pluralizer returns the pluralizer for a given locale if defined" do + assert_instance_of Proc, @backend.pluralizer(:en) + end + + test "#pluralizer returns the default pluralizer if no pluralizer is defined for the given locale" do + assert_equal @backend.pluralizer(:en), @backend.pluralizer(:de) + end + + test "#add_pluralizer allows to store a pluralizer per locale" do + assert_nothing_raised { @backend.add_pluralizer(:cz, @cz_pluralizer) } + assert_equal @cz_pluralizer, @backend.pluralizer(:cz) + end + +end + +class PluralizePluralizingTest < ActiveSupport::TestCase + def setup + @backend = Globalize::Backend::Pluralizing.new + @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other } + @backend.store_translations :en, :foo => {:one => 'one en foo', :other => 'many en foos'} + @backend.store_translations :cz, :foo => {:one => 'one cz foo', :few => 'few cz foos', :other => 'many cz foos'} + end + + test "looks up the :one translation when count is 1" do + assert_equal 'one en foo', @backend.translate(:en, :foo, :count => 1) + end + + test "looks up the :other translation when count is 2" do + assert_equal 'many en foos', @backend.translate(:en, :foo, :count => 2) + end +end + +class CzPluralizingTest < ActiveSupport::TestCase + def setup + @backend = Globalize::Backend::Pluralizing.new + @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other } + @backend.store_translations :en, :foo => {:one => 'one en foo', :other => 'many en foos'} + @backend.store_translations :cz, :foo => {:one => 'one cz foo', :few => 'few cz foos', :other => 'many cz foos'} + @backend.add_pluralizer(:cz, @cz_pluralizer) + end + + test "looks up the :one translation when count is 1 (:cz)" do + assert_equal 'one cz foo', @backend.translate(:cz, :foo, :count => 1) + end + + test "looks up the :few translation when count is 2 (:cz)" do + assert_equal 'few cz foos', @backend.translate(:cz, :foo, :count => 2) + end + + test "looks up the :other translation when count is 5 (:cz)" do + assert_equal 'many cz foos', @backend.translate(:cz, :foo, :count => 5) + end + +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 00000000..133e5477 --- /dev/null +++ b/vendor/plugins/globalize2/test/backends/static_test.rb @@ -0,0 +1,143 @@ +require File.join( File.dirname(__FILE__), '..', 'test_helper' ) +require 'globalize/backend/static' +require 'globalize/translation' +require 'action_view' +include ActionView::Helpers::NumberHelper + +I18n.locale = :'en-US' # Need to set this, since I18n defaults to 'en' + +class StaticTest < ActiveSupport::TestCase + def setup + I18n.backend = Globalize::Backend::Static.new + translations = {:"en-US" => {:foo => "foo in en-US", :boz => 'boz', :buz => {:bum => 'bum'}}, + :"en" => {:bar => "bar in en"}, + :"de-DE" => {:baz => "baz in de-DE"}, + :"de" => {:boo => "boo in de", :number => { :currency => { :format => { :unit => '€', :format => '%n %u'}}}}} + translations.each do |locale, data| + I18n.backend.store_translations locale, data + end + I18n.fallbacks.map :"de-DE" => :"en-US", :he => :en + end + + test "returns an instance of Translation:Static" do + translation = I18n.translate :foo + assert_instance_of Globalize::Translation::Static, translation + end + + test "returns the translation in en-US if present" do + assert_equal "foo in en-US", I18n.translate(:foo, :locale => :"en-US") + end + + test "returns the translation in en if en-US is not present" do + assert_equal "bar in en", I18n.translate(:bar, :locale => :"en-US") + end + + test "returns the translation in de-DE if present" do + assert_equal "baz in de-DE", I18n.translate(:baz, :locale => :"de-DE") + end + + test "returns the translation in de if de-DE is not present" do + assert_equal "boo in de", I18n.translate(:boo, :locale => :"de-DE") + end + + test "returns the translation in en-US if none of de-DE and de are present" do + assert_equal "foo in en-US", I18n.translate(:foo, :locale => :"de-DE") + end + + test "returns the translation in en if none of de-DE, de and en-US are present" do + assert_equal "bar in en", I18n.translate(:bar, :locale => :"de-DE") + end + + test "returns the translation in en if none in he is present" do + assert_equal "bar in en", I18n.translate(:bar, :locale => :he) + end + + test "returns the given default String when the key is not present for any locale" do + assert_equal "default", I18n.translate(:missing, :default => "default") + end + + test "returns the fallback translation for the key if present for a fallback locale" do + I18n.backend.store_translations :de, :non_default => "non_default in de" + assert_equal "non_default in de", I18n.translate(:non_default, :default => "default", :locale => :"de-DE") + end + + test "returns an array of translations" do + assert_instance_of Array, I18n.translate([:foo, :boz]) + end + + test "returns an array of instances of Translation::Static" do + assert_equal [Globalize::Translation::Static], I18n.translate([:foo, :boz]).map(&:class).uniq + end + + test "returns a hash of translations" do + assert_instance_of Hash, I18n.translate(:"buz") + end + + test "returns an array of translations 2" do + assert_equal [Globalize::Translation::Static], I18n.translate(:"buz").values.map(&:class) + end + + test "returns currency properly formated" do + currency = number_to_currency(10) + assert_equal "$10.00", currency + end + + test "returns currency properly formated for locale" do + currency = number_to_currency(10, :locale => :'de') + assert_equal "10.000 €", currency + end + + test "returns currency properly formated from parameters" do + currency = number_to_currency(10, :format => "%n %u", :unit => '€') + assert_equal "10.00 €", currency + end + + test "makes sure interpolation does not break even with False as string" do + assert_equal "translation missing: en, support, array, skip_last_comma", I18n.translate(:"support.array.skip_last_comma") + end +end + +class TranslationStaticTest < ActiveSupport::TestCase + def setup + I18n.backend = Globalize::Backend::Static.new + translations = { + :greeting => "Hi {{name}}", + :messages => { :one => "You have one message.", :other => "You have {{count}} messages."} + } + I18n.backend.store_translations :"en", translations + end + + def greeting + I18n.translate :greeting, :locale => :"en-US", :name => "Joshua" + end + + test "stores the actual locale" do + assert_equal :en, greeting.locale + end + + test "stores the requested locale" do + assert_equal :'en-US', greeting.requested_locale + end + + test "stores the requested key" do + assert_equal :greeting, greeting.key + end + + test "stores the options given to #translate" do + assert_equal( {:name => "Joshua"}, greeting.options ) + end + + test "stores the original translation before test was interpolated" do + assert_equal "Hi {{name}}", greeting.original + end + + test "stores the plural_key :one if pluralized as such" do + message = I18n.translate :messages, :locale => :"en-US", :count => 1 + assert_equal :one, message.plural_key + end + + test "stores the plural_key :other if pluralized as such" do + messages = I18n.translate :messages, :locale => :"en-US", :count => 2 + assert_equal :other, messages.plural_key + end +end -- cgit v1.3