Skip to content

Commit

Permalink
test suite cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Nov 5, 2010
1 parent 4cf8570 commit b85510b
Show file tree
Hide file tree
Showing 51 changed files with 357 additions and 533 deletions.
1 change: 1 addition & 0 deletions ci/Gemfile.all
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ gem 'ffi'
gem 'ruby-cldr'
gem 'ruby2ruby'
gem 'ParseTree'
gem 'test_declarative'
37 changes: 0 additions & 37 deletions ci/Gemfile.all.lock

This file was deleted.

1 change: 1 addition & 0 deletions ci/Gemfile.no-rails
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source :rubygems

gem 'mocha'
gem 'test_declarative'

11 changes: 0 additions & 11 deletions ci/Gemfile.no-rails.lock

This file was deleted.

1 change: 1 addition & 0 deletions ci/Gemfile.rails-2.3.x
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ source :rubygems
gem 'activesupport', '~> 2.3'
gem 'sqlite3-ruby'
gem 'mocha'
gem 'test_declarative'

18 changes: 0 additions & 18 deletions ci/Gemfile.rails-2.3.x.lock

This file was deleted.

2 changes: 2 additions & 0 deletions ci/Gemfile.rails-3.x
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ source :rubygems
gem 'activesupport', '~> 3.0.0'
gem 'sqlite3-ruby'
gem 'mocha'
gem 'test_declarative'

16 changes: 0 additions & 16 deletions ci/Gemfile.rails-3.x.lock

This file was deleted.

2 changes: 0 additions & 2 deletions lib/i18n/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: utf-8

class KeyError < IndexError
def initialize(message = nil)
super(message || "key not found")
Expand Down
20 changes: 10 additions & 10 deletions lib/i18n/tests/basics.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
# encoding: utf-8

module I18n
module Tests
module Basics
def teardown
I18n.available_locales = nil
end

def test_available_locales
test "available_locales returns the locales stored to the backend by default" do
I18n.backend.store_translations('de', :foo => 'bar')
I18n.backend.store_translations('en', :foo => 'foo')

assert I18n.available_locales.include?(:de)
assert I18n.available_locales.include?(:en)
end

def test_available_locales_setter
test "available_locales can be set to something else independently from the actual locale data" do
I18n.backend.store_translations('de', :foo => 'bar')
I18n.backend.store_translations('en', :foo => 'foo')

I18n.available_locales = :foo
assert_equal [:foo], I18n.available_locales
Expand All @@ -25,29 +24,30 @@ def test_available_locales_setter
assert_equal [:foo, :bar], I18n.available_locales

I18n.available_locales = nil
assert_equal [:de, :en], I18n.available_locales.sort {|a,b| a.to_s <=> b.to_s}
assert I18n.available_locales.include?(:de)
assert I18n.available_locales.include?(:en)
end

def test_available_locales_memoizes_when_explicitely_set
test "available_locales memoizes when set explicitely" do
I18n.backend.expects(:available_locales).never
I18n.available_locales = [:foo]
I18n.backend.store_translations('de', :bar => 'baz')
I18n.reload!
assert_equal [:foo], I18n.available_locales
end

def test_available_locales_delegates_to_backend_when_not_explicitely_set
test "available_locales delegates to the backend when not set explicitely" do
I18n.backend.expects(:available_locales).twice
assert_equal I18n.available_locales, I18n.available_locales
end

def test_delete_value
test "storing a nil value as a translation removes it from the available locale data" do
I18n.backend.store_translations(:en, :to_be_deleted => 'bar')
assert_equal 'bar', I18n.t('to_be_deleted', :default => 'baz')
assert_equal 'bar', I18n.t(:to_be_deleted, :default => 'baz')

I18n.cache_store.clear if I18n.respond_to?(:cache_store) && I18n.cache_store
I18n.backend.store_translations(:en, :to_be_deleted => nil)
assert_equal 'baz', I18n.t('to_be_deleted', :default => 'baz')
assert_equal 'baz', I18n.t(:to_be_deleted, :default => 'baz')
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/i18n/tests/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ def setup
I18n.backend.store_translations(:en, :foo => { :bar => 'bar', :baz => 'baz' })
end

define_method "test defaults: given nil as a key it returns the given default" do
test "defaults: given nil as a key it returns the given default" do
assert_equal 'default', I18n.t(nil, :default => 'default')
end

define_method "test defaults: given a symbol as a default it translates the symbol" do
test "defaults: given a symbol as a default it translates the symbol" do
assert_equal 'bar', I18n.t(nil, :default => :'foo.bar')
end

define_method "test defaults: given a symbol as a default and a scope it stays inside the scope when looking up the symbol" do
test "defaults: given a symbol as a default and a scope it stays inside the scope when looking up the symbol" do
assert_equal 'bar', I18n.t(:missing, :default => :bar, :scope => :foo)
end

define_method "test defaults: given an array as a default it returns the first match" do
test "defaults: given an array as a default it returns the first match" do
assert_equal 'bar', I18n.t(:does_not_exist, :default => [:does_not_exist_2, :'foo.bar'])
end

define_method "test defaults: given an array of missing keys it raises a MissingTranslationData exception" do
test "defaults: given an array of missing keys it raises a MissingTranslationData exception" do
assert_raise I18n::MissingTranslationData do
I18n.t(:does_not_exist, :default => [:does_not_exist_2, :does_not_exist_3], :raise => true)
end
end

define_method "test defaults: using a custom scope separator" do
test "defaults: using a custom scope separator" do
# data must have been stored using the custom separator when using the ActiveRecord backend
I18n.backend.store_translations(:en, { :foo => { :bar => 'bar' } }, { :separator => '|' })
assert_equal 'bar', I18n.t(nil, :default => :'foo|bar', :separator => '|')
Expand Down
36 changes: 18 additions & 18 deletions lib/i18n/tests/interpolation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,96 +16,96 @@ module Interpolation
# * Security concerns: if I allow users to translate a web site, they can
# insert %{} in messages causing the I18n lookup to fail in every request.
#
define_method "test interpolation: given no values it does not alter the string" do
test "interpolation: given no values it does not alter the string" do
assert_equal 'Hi %{name}!', interpolate(:default => 'Hi %{name}!')
end

define_method "test interpolation: given values it interpolates them into the string" do
test "interpolation: given values it interpolates them into the string" do
assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => 'David')
end

define_method "test interpolation: given a nil value it still interpolates it into the string" do
test "interpolation: given a nil value it still interpolates it into the string" do
assert_equal 'Hi !', interpolate(:default => 'Hi %{name}!', :name => nil)
end

define_method "test interpolation: given a lambda as a value it calls it if the string contains the key" do
test "interpolation: given a lambda as a value it calls it if the string contains the key" do
assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => lambda { |*args| 'David' })
end

define_method "test interpolation: given a lambda as a value it does not call it if the string does not contain the key" do
test "interpolation: given a lambda as a value it does not call it if the string does not contain the key" do
assert_nothing_raised { interpolate(:default => 'Hi!', :name => lambda { |*args| raise 'fail' }) }
end

define_method "test interpolation: given values but missing a key it raises I18n::MissingInterpolationArgument" do
test "interpolation: given values but missing a key it raises I18n::MissingInterpolationArgument" do
assert_raise(I18n::MissingInterpolationArgument) do
interpolate(:default => '%{foo}', :bar => 'bar')
end
end

define_method "test interpolation: it does not raise I18n::MissingInterpolationArgument for escaped variables" do
test "interpolation: it does not raise I18n::MissingInterpolationArgument for escaped variables" do
assert_nothing_raised(I18n::MissingInterpolationArgument) do
assert_equal 'Barr %{foo}', interpolate(:default => '%{bar} %%{foo}', :bar => 'Barr')
end
end

define_method "test interpolation: it does not change the original, stored translation string" do
test "interpolation: it does not change the original, stored translation string" do
I18n.backend.store_translations(:en, :interpolate => 'Hi %{name}!')
assert_equal 'Hi David!', interpolate(:interpolate, :name => 'David')
assert_equal 'Hi Yehuda!', interpolate(:interpolate, :name => 'Yehuda')
end

define_method "test interpolation: works with the deprecated syntax" do
test "interpolation: works with the deprecated syntax" do
deprecation = capture(:stderr) do
assert_equal 'Hi David!', interpolate(:default => 'Hi {{name}}!', :name => 'David')
end
assert_match "The {{key}} interpolation syntax in I18n messages is deprecated", deprecation
end

define_method "test interpolation: given the translation is in utf-8 it still works" do
test "interpolation: given the translation is in utf-8 it still works" do
assert_equal 'Häi David!', interpolate(:default => 'Häi %{name}!', :name => 'David')
end

define_method "test interpolation: given the value is in utf-8 it still works" do
test "interpolation: given the value is in utf-8 it still works" do
assert_equal 'Hi ゆきひろ!', interpolate(:default => 'Hi %{name}!', :name => 'ゆきひろ')
end

define_method "test interpolation: given the translation and the value are in utf-8 it still works" do
test "interpolation: given the translation and the value are in utf-8 it still works" do
assert_equal 'こんにちは、ゆきひろさん!', interpolate(:default => 'こんにちは、%{name}さん!', :name => 'ゆきひろ')
end

if Kernel.const_defined?(:Encoding)
define_method "test interpolation: given a euc-jp translation and a utf-8 value it raises Encoding::CompatibilityError" do
test "interpolation: given a euc-jp translation and a utf-8 value it raises Encoding::CompatibilityError" do
assert_raise(Encoding::CompatibilityError) do
interpolate(:default => euc_jp('こんにちは、%{name}さん!'), :name => 'ゆきひろ')
end
end

define_method "test interpolation: given a utf-8 translation and a euc-jp value it raises Encoding::CompatibilityError" do
test "interpolation: given a utf-8 translation and a euc-jp value it raises Encoding::CompatibilityError" do
assert_raise(Encoding::CompatibilityError) do
interpolate(:default => 'こんにちは、%{name}さん!', :name => euc_jp('ゆきひろ'))
end
end

define_method "test interpolation: ASCII strings in the backend should be encoded to UTF8 if interpolation options are in UTF8" do
test "interpolation: ASCII strings in the backend should be encoded to UTF8 if interpolation options are in UTF8" do
I18n.backend.store_translations 'en', 'encoding' => ('%{who} let me go'.force_encoding("ASCII"))
result = I18n.t 'encoding', :who => "måmmå miå"
assert_equal Encoding::UTF_8, result.encoding
end

define_method "test interpolation: UTF8 strings in the backend are still returned as UTF8 with ASCII interpolation" do
test "interpolation: UTF8 strings in the backend are still returned as UTF8 with ASCII interpolation" do
I18n.backend.store_translations 'en', 'encoding' => 'måmmå miå %{what}'
result = I18n.t 'encoding', :what => 'let me go'.force_encoding("ASCII")
assert_equal Encoding::UTF_8, result.encoding
end

define_method "test interpolation: UTF8 strings in the backend are still returned as UTF8 even with numbers interpolation" do
test "interpolation: UTF8 strings in the backend are still returned as UTF8 even with numbers interpolation" do
I18n.backend.store_translations 'en', 'encoding' => '%{count} times: måmmå miå'
result = I18n.t 'encoding', :count => 3
assert_equal Encoding::UTF_8, result.encoding
end
end

define_method "test interpolation: given a translations containing a reserved key it raises I18n::ReservedInterpolationKey" do
test "interpolation: given a translations containing a reserved key it raises I18n::ReservedInterpolationKey" do
assert_raise(I18n::ReservedInterpolationKey) { interpolate(:default => '%{default}', :foo => :bar) }
assert_raise(I18n::ReservedInterpolationKey) { interpolate(:default => '%{scope}', :foo => :bar) }
assert_raise(I18n::ReservedInterpolationKey) { interpolate(:default => '%{separator}', :foo => :bar) }
Expand Down
12 changes: 6 additions & 6 deletions lib/i18n/tests/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,47 @@
module I18n
module Tests
module Link
define_method "test linked lookup: if a key resolves to a symbol it looks up the symbol" do
test "linked lookup: if a key resolves to a symbol it looks up the symbol" do
I18n.backend.store_translations 'en', {
:link => :linked,
:linked => 'linked'
}
assert_equal 'linked', I18n.backend.translate('en', :link)
end

define_method "test linked lookup: if a key resolves to a dot-separated symbol it looks up the symbol" do
test "linked lookup: if a key resolves to a dot-separated symbol it looks up the symbol" do
I18n.backend.store_translations 'en', {
:link => :"foo.linked",
:foo => { :linked => 'linked' }
}
assert_equal('linked', I18n.backend.translate('en', :link))
end

define_method "test linked lookup: if a dot-separated key resolves to a symbol it looks up the symbol" do
test "linked lookup: if a dot-separated key resolves to a symbol it looks up the symbol" do
I18n.backend.store_translations 'en', {
:foo => { :link => :linked },
:linked => 'linked'
}
assert_equal('linked', I18n.backend.translate('en', :'foo.link'))
end

define_method "test linked lookup: if a dot-separated key resolves to a dot-separated symbol it looks up the symbol" do
test "linked lookup: if a dot-separated key resolves to a dot-separated symbol it looks up the symbol" do
I18n.backend.store_translations 'en', {
:foo => { :link => :"bar.linked" },
:bar => { :linked => 'linked' }
}
assert_equal('linked', I18n.backend.translate('en', :'foo.link'))
end

define_method "test linked lookup: links always refer to the absolute key" do
test "linked lookup: links always refer to the absolute key" do
I18n.backend.store_translations 'en', {
:foo => { :link => :linked, :linked => 'linked in foo' },
:linked => 'linked absolutely'
}
assert_equal 'linked absolutely', I18n.backend.translate('en', :link, :scope => :foo)
end

define_method "test linked lookup: a link can resolve to a namespace in the middle of a dot-separated key" do
test "linked lookup: a link can resolve to a namespace in the middle of a dot-separated key" do
I18n.backend.store_translations 'en', {
:activemodel => { :errors => { :messages => { :blank => "can't be blank" } } },
:activerecord => { :errors => { :messages => :"activemodel.errors.messages" } }
Expand Down
Loading

0 comments on commit b85510b

Please sign in to comment.