Skip to content

Commit

Permalink
once again fixing DateTime.inspect assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Nov 17, 2010
1 parent ad3629a commit a63fb7f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 23 deletions.
3 changes: 3 additions & 0 deletions ci/Gemfile.all.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ DEPENDENCIES
rufus-tokyo
sqlite3-ruby
test_declarative

METADATA
version: 1.0.6
21 changes: 21 additions & 0 deletions ci/Gemfile.rails-2.3.x.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
GEM
remote: http://rubygems.org/
specs:
activesupport (2.3.10)
mocha (0.9.9)
rake
rake (0.8.7)
sqlite3-ruby (1.3.2)
test_declarative (0.0.4)

PLATFORMS
ruby

DEPENDENCIES
activesupport (~> 2.3)
mocha
sqlite3-ruby
test_declarative

METADATA
version: 1.0.6
3 changes: 3 additions & 0 deletions ci/Gemfile.rails-3.x.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ DEPENDENCIES
mocha
sqlite3-ruby
test_declarative

METADATA
version: 1.0.6
2 changes: 1 addition & 1 deletion lib/i18n/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def available_locales

# Sets the available locales.
def available_locales=(locales)
@@available_locales = Array(locales).map {|locale| locale.to_sym}
@@available_locales = Array(locales).map { |locale| locale.to_sym }
@@available_locales = nil if @@available_locales.empty?
end

Expand Down
41 changes: 21 additions & 20 deletions lib/i18n/tests/localization/procs.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
# encoding: utf-8

class DateTime
def inspect
strftime('%a, %d %b %Y %H:%M:%S %Z')
end
end

class Date
def inspect
strftime('%a, %d %b %Y')
end
end

module I18n
module Tests
module Localization
Expand Down Expand Up @@ -52,44 +40,57 @@ module Procs
test "localize DateTime: given a format that resolves to a Proc it calls the Proc with the object" do
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)
assert_equal '[Sat, 01 Mar 2008 06:00:00 +00:00, {}]', I18n.l(datetime, :format => :proc, :locale => :ru)
end

test "localize DateTime: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
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)
assert_equal '[Sat, 01 Mar 2008 06:00:00 +00:00, {:foo=>"foo"}]', I18n.l(datetime, :format => :proc, :foo => 'foo', :locale => :ru)
end

test "localize Time: given a format that resolves to a Proc it calls the Proc with the object" do
setup_time_proc_translations
time = ::Time.utc(2008, 3, 1, 6, 0)
assert_equal [time, {}].inspect, I18n.l(time, :format => :proc, :locale => :ru)
assert_equal inspect_args([time, {}]), I18n.l(time, :format => :proc, :locale => :ru)
end

test "localize Time: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
setup_time_proc_translations
time = ::Time.utc(2008, 3, 1, 6, 0)
options = { :foo => 'foo' }
assert_equal [time, options].inspect, I18n.l(time, options.merge(:format => :proc, :locale => :ru))
assert_equal inspect_args([time, options]), I18n.l(time, options.merge(:format => :proc, :locale => :ru))
end

protected

def filter_args(*args)
args.map { |arg| arg.delete(:fallback) if arg.is_a?(Hash) ; arg }.inspect
def inspect_args(args)
args = args.map do |arg|
case arg
when ::Time, ::DateTime
arg.strftime('%a, %d %b %Y %H:%M:%S %Z').sub('+0000', '+00:00')
when ::Date
arg.strftime('%a, %d %b %Y')
when Hash
arg.delete(:fallback)
arg.inspect
else
arg.inspect
end
end
"[#{args.join(', ')}]"
end

def setup_time_proc_translations
I18n.backend.store_translations :ru, {
:time => {
:formats => {
:proc => lambda { |*args| filter_args(*args) }
:proc => lambda { |*args| inspect_args(args) }
}
},
:date => {
:formats => {
:proc => lambda { |*args| filter_args(*args) }
:proc => lambda { |*args| inspect_args(args) }
},
:'day_names' => lambda { |key, options|
(options[:format] =~ /^%A/) ?
Expand Down
3 changes: 1 addition & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
# to load I18n using the gem method. Instead, we want to test the local library of course.
alias :gem_for_ruby_19 :gem # for 1.9. gives a super ugly seg fault otherwise
def gem(gem_name, *version_requirements)
puts("skipping loading the i18n gem ...") && return if gem_name =='i18n'
super(gem_name, *version_requirements)
gem_name =='i18n' ? puts("skipping loading the i18n gem ...") : super
end

require 'bundler/setup'
Expand Down

0 comments on commit a63fb7f

Please sign in to comment.