Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify can store procs logic.

  • Loading branch information...
commit 9ec2fe907771bd4b6509e70becbee43dfed4e1fe 1 parent 712aee8
@josevalim josevalim authored
View
80 lib/i18n/tests/localization/procs.rb
@@ -5,79 +5,61 @@ module Tests
module Localization
module Procs
define_method "test localize: using day names from lambdas" do
- if can_store_procs?
- setup_time_proc_translations
- time = ::Time.parse('2008-03-01 6:00 UTC')
- assert_match /Суббота/, I18n.l(time, :format => "%A, %d %B", :locale => :ru)
- assert_match /суббота/, I18n.l(time, :format => "%d %B (%A)", :locale => :ru)
- end
+ setup_time_proc_translations
+ time = ::Time.parse('2008-03-01 6:00 UTC')
+ assert_match /Суббота/, I18n.l(time, :format => "%A, %d %B", :locale => :ru)
+ assert_match /суббота/, I18n.l(time, :format => "%d %B (%A)", :locale => :ru)
end
define_method "test localize: using month names from lambdas" do
- if can_store_procs?
- setup_time_proc_translations
- time = ::Time.parse('2008-03-01 6:00 UTC')
- assert_match /марта/, I18n.l(time, :format => "%d %B %Y", :locale => :ru)
- assert_match /Март /, I18n.l(time, :format => "%B %Y", :locale => :ru)
- end
+ setup_time_proc_translations
+ time = ::Time.parse('2008-03-01 6:00 UTC')
+ assert_match /марта/, I18n.l(time, :format => "%d %B %Y", :locale => :ru)
+ assert_match /Март /, I18n.l(time, :format => "%B %Y", :locale => :ru)
end
define_method "test localize: using abbreviated day names from lambdas" do
- if can_store_procs?
- setup_time_proc_translations
- time = ::Time.parse('2008-03-01 6:00 UTC')
- assert_match /марта/, I18n.l(time, :format => "%d %b %Y", :locale => :ru)
- assert_match /март /, I18n.l(time, :format => "%b %Y", :locale => :ru)
- end
+ setup_time_proc_translations
+ time = ::Time.parse('2008-03-01 6:00 UTC')
+ assert_match /марта/, I18n.l(time, :format => "%d %b %Y", :locale => :ru)
+ assert_match /март /, I18n.l(time, :format => "%b %Y", :locale => :ru)
end
define_method "test localize Date: given a format that resolves to a Proc it calls the Proc with the object" do
- if can_store_procs?
- setup_time_proc_translations
- date = ::Date.new(2008, 3, 1, 6)
- assert_equal '[Sat, 01 Mar 2008, {}]', I18n.l(date, :format => :proc, :locale => :ru)
- end
+ setup_time_proc_translations
+ date = ::Date.new(2008, 3, 1, 6)
+ assert_equal '[Sat, 01 Mar 2008, {}]', I18n.l(date, :format => :proc, :locale => :ru)
end
define_method "test localize Date: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
- if can_store_procs?
- setup_time_proc_translations
- date = ::Date.new(2008, 3, 1, 6)
- assert_equal '[Sat, 01 Mar 2008, {:foo=>"foo"}]', I18n.l(date, :format => :proc, :foo => 'foo', :locale => :ru)
- end
+ setup_time_proc_translations
+ date = ::Date.new(2008, 3, 1, 6)
+ assert_equal '[Sat, 01 Mar 2008, {:foo=>"foo"}]', I18n.l(date, :format => :proc, :foo => 'foo', :locale => :ru)
end
define_method "test localize DateTime: given a format that resolves to a Proc it calls the Proc with the object" do
- if can_store_procs?
- setup_time_proc_translations
- datetime = ::DateTime.new(2008, 3, 1, 6)
- assert_equal '[Sat, 01 Mar 2008 06:00:00 +0000, {}]', I18n.l(datetime, :format => :proc, :locale => :ru)
- end
+ setup_time_proc_translations
+ datetime = ::DateTime.new(2008, 3, 1, 6)
+ assert_equal '[Sat, 01 Mar 2008 06:00:00 +0000, {}]', I18n.l(datetime, :format => :proc, :locale => :ru)
end
define_method "test localize DateTime: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
- if can_store_procs?
- setup_time_proc_translations
- datetime = ::DateTime.new(2008, 3, 1, 6)
- assert_equal '[Sat, 01 Mar 2008 06:00:00 +0000, {:foo=>"foo"}]', I18n.l(datetime, :format => :proc, :foo => 'foo', :locale => :ru)
- end
+ setup_time_proc_translations
+ datetime = ::DateTime.new(2008, 3, 1, 6)
+ assert_equal '[Sat, 01 Mar 2008 06:00:00 +0000, {:foo=>"foo"}]', I18n.l(datetime, :format => :proc, :foo => 'foo', :locale => :ru)
end
define_method "test localize Time: given a format that resolves to a Proc it calls the Proc with the object" do
- if can_store_procs?
- setup_time_proc_translations
- time = ::Time.parse('2008-03-01 6:00 UTC')
- assert_equal [time, {}].inspect, I18n.l(time, :format => :proc, :locale => :ru)
- end
+ setup_time_proc_translations
+ time = ::Time.parse('2008-03-01 6:00 UTC')
+ assert_equal [time, {}].inspect, I18n.l(time, :format => :proc, :locale => :ru)
end
define_method "test localize Time: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
- if can_store_procs?
- setup_time_proc_translations
- time = ::Time.parse('2008-03-01 6:00 UTC')
- options = { :foo => 'foo' }
- assert_equal [time, options].inspect, I18n.l(time, options.merge(:format => :proc, :locale => :ru))
- end
+ setup_time_proc_translations
+ time = ::Time.parse('2008-03-01 6:00 UTC')
+ options = { :foo => 'foo' }
+ assert_equal [time, options].inspect, I18n.l(time, options.merge(:format => :proc, :locale => :ru))
end
protected
View
30 lib/i18n/tests/procs.rb
@@ -3,15 +3,9 @@
module I18n
module Tests
module Procs
- def filter_args(*args)
- args.map {|arg| arg.delete(:fallback) if arg.is_a?(Hash) ; arg }.inspect
- end
-
define_method "test lookup: given a translation is a proc it calls the proc with the key and interpolation values" do
- if can_store_procs?
- I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
- assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(:a_lambda, :foo => 'foo')
- end
+ I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
+ assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(:a_lambda, :foo => 'foo')
end
define_method "test defaults: given a default is a Proc it calls it with the key and interpolation values" do
@@ -20,11 +14,9 @@ def filter_args(*args)
end
define_method "test defaults: given a default is a key that resolves to a Proc it calls it with the key and interpolation values" do
- if can_store_procs?
- I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
- assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(nil, :default => :a_lambda, :foo => 'foo')
- assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(nil, :default => [nil, :a_lambda], :foo => 'foo')
- end
+ I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
+ assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(nil, :default => :a_lambda, :foo => 'foo')
+ assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(nil, :default => [nil, :a_lambda], :foo => 'foo')
end
define_method "test interpolation: given an interpolation value is a lambda it calls it with key and values before interpolating it" do
@@ -45,15 +37,19 @@ def filter_args(*args)
end
define_method "test lookup: given the option :resolve => false was passed it does not resolve proc translations" do
- if can_store_procs?
- I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
- assert_equal Proc, I18n.t(:a_lambda, :resolve => false).class
- end
+ I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
+ assert_equal Proc, I18n.t(:a_lambda, :resolve => false).class
end
define_method "test lookup: given the option :resolve => false was passed it does not resolve proc default" do
assert_equal Proc, I18n.t(nil, :default => lambda { |*args| filter_args(*args) }, :resolve => false).class
end
+
+ protected
+
+ def filter_args(*args)
+ args.map {|arg| arg.delete(:fallback) if arg.is_a?(Hash) ; arg }.inspect
+ end
end
end
end
View
8 test/api/active_record_test.rb
@@ -10,18 +10,22 @@ def setup
super
end
+ def self.can_store_procs?
+ I18n::Backend::ActiveRecord.included_modules.include?(I18n::Backend::ActiveRecord::StoreProcs)
+ end
+
include I18n::Tests::Basics
include I18n::Tests::Defaults
include I18n::Tests::Interpolation
include I18n::Tests::Link
include I18n::Tests::Lookup
include I18n::Tests::Pluralization
- include I18n::Tests::Procs # unless RUBY_VERSION >= '1.9.1'
+ include I18n::Tests::Procs if can_store_procs?
include I18n::Tests::Localization::Date
include I18n::Tests::Localization::DateTime
include I18n::Tests::Localization::Time
- include I18n::Tests::Localization::Procs # unless RUBY_VERSION >= '1.9.1'
+ include I18n::Tests::Localization::Procs if can_store_procs?
test "make sure we use an ActiveRecord backend" do
assert_equal I18n::Backend::ActiveRecord, I18n.backend.class
View
13 test/test_helper.rb
@@ -49,19 +49,6 @@ def store_translations(*args)
def locales_dir
File.dirname(__FILE__) + '/test_data/locales'
end
-
- def can_store_procs?
- active_record_available? && non_active_record_backend?
- end
-
- def active_record_available?
- defined?(ActiveRecord)
- end
-
- def non_active_record_backend?
- I18n.backend.class != I18n::Backend::ActiveRecord or
- I18n::Backend::ActiveRecord.included_modules.include?(I18n::Backend::ActiveRecord::StoreProcs)
- end
end
Object.class_eval do
Please sign in to comment.
Something went wrong with that request. Please try again.