Skip to content

Commit

Permalink
Ruby 3.4 Hash#inspect compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
voxik committed Dec 9, 2024
1 parent ce4b850 commit a6b16dc
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/i18n/tests/localization/procs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module Procs
test "localize Date: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
setup_time_proc_translations
date = ::Date.new(2008, 3, 1)
assert_equal '[Sat, 01 Mar 2008, {:foo=>"foo"}]', I18n.l(date, :format => :proc, :foo => 'foo', :locale => :ru)
assert_equal %|[Sat, 01 Mar 2008, #{{:foo=>"foo"}}]|, I18n.l(date, :format => :proc, :foo => 'foo', :locale => :ru)
end

test "localize DateTime: given a format that resolves to a Proc it calls the Proc with the object" do
Expand All @@ -46,7 +46,7 @@ module Procs
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 +00:00, {: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
Expand Down
18 changes: 11 additions & 7 deletions lib/i18n/tests/procs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,38 @@ module Tests
module Procs
test "lookup: given a translation is a proc it calls the proc with the key and interpolation values" do
I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| I18n::Tests::Procs.filter_args(*args) })
assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(:a_lambda, :foo => 'foo')
assert_equal %|[:a_lambda, #{{:foo=>"foo"}}]|, I18n.t(:a_lambda, :foo => 'foo')
end

test "lookup: given a translation is a proc it passes the interpolation values as keyword arguments" do
I18n.backend.store_translations(:en, :a_lambda => lambda { |key, foo:, **| I18n::Tests::Procs.filter_args(key, foo: foo) })
assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(:a_lambda, :foo => 'foo')
assert_equal %|[:a_lambda, #{{:foo=>"foo"}}]|, I18n.t(:a_lambda, :foo => 'foo')
end

test "defaults: given a default is a Proc it calls it with the key and interpolation values" do
proc = lambda { |*args| I18n::Tests::Procs.filter_args(*args) }
assert_equal '[nil, {:foo=>"foo"}]', I18n.t(nil, :default => proc, :foo => 'foo')
assert_equal %|[nil, #{{:foo=>"foo"}}]|, I18n.t(nil, :default => proc, :foo => 'foo')
end

test "defaults: given a default is a key that resolves to a Proc it calls it with the key and interpolation values" do
the_lambda = lambda { |*args| I18n::Tests::Procs.filter_args(*args) }
I18n.backend.store_translations(:en, :a_lambda => the_lambda)
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')
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

test "interpolation: given an interpolation value is a lambda it calls it with key and values before interpolating it" do
proc = lambda { |*args| I18n::Tests::Procs.filter_args(*args) }
assert_match %r(\[\{:foo=>#<Proc.*>\}\]), I18n.t(nil, :default => '%{foo}', :foo => proc)
if RUBY_VERSION < "3.4"
assert_match %r(\[\{:foo=>#<Proc.*>\}\]), I18n.t(nil, :default => '%{foo}', :foo => proc)
else
assert_match %r(\[\{foo: #<Proc.*>\}\]), I18n.t(nil, :default => '%{foo}', :foo => proc)
end
end

test "interpolation: given a key resolves to a Proc that returns a string then interpolation still works" do
proc = lambda { |*args| "%{foo}: " + I18n::Tests::Procs.filter_args(*args) }
assert_equal 'foo: [nil, {:foo=>"foo"}]', I18n.t(nil, :default => proc, :foo => 'foo')
assert_equal %|foo: [nil, #{{:foo=>"foo"}}]|, I18n.t(nil, :default => proc, :foo => 'foo')
end

test "pluralization: given a key resolves to a Proc that returns valid data then pluralization still works" do
Expand Down
2 changes: 1 addition & 1 deletion test/backend/exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ def setup

test "exceptions: MissingInterpolationArgument message includes missing key, provided keys and full string" do
exception = I18n::MissingInterpolationArgument.new('key', {:this => 'was given'}, 'string')
assert_equal 'missing interpolation argument "key" in "string" ({:this=>"was given"} given)', exception.message
assert_equal %|missing interpolation argument "key" in "string" (#{{:this=>"was given"}} given)|, exception.message
end
end
2 changes: 1 addition & 1 deletion test/backend/interpolation_compiler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_custom_missing_interpolation_argument_handler
I18n.config.missing_interpolation_argument_handler = lambda do |key, values, string|
"missing key is #{key}, values are #{values.inspect}, given string is '#{string}'"
end
assert_equal %|first missing key is last, values are {:first=>"first"}, given string is '%{first} %{last}'|,
assert_equal %|first missing key is last, values are #{{:first=>"first"}.to_s}, given string is '%{first} %{last}'|,
compile_and_interpolate('%{first} %{last}', :first => 'first')
ensure
I18n.config.missing_interpolation_argument_handler = old_handler
Expand Down
4 changes: 2 additions & 2 deletions test/i18n/exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_invalid_locale_stores_locale
test "InvalidPluralizationData message contains count, data and missing key" do
force_invalid_pluralization_data do |exception|
assert_match '1', exception.message
assert_match '{:other=>"bar"}', exception.message
assert_match %|#{{:other=>"bar"}}|, exception.message
assert_match 'one', exception.message
end
end
Expand All @@ -58,7 +58,7 @@ def test_invalid_locale_stores_locale

test "MissingInterpolationArgument message contains the missing and given arguments" do
force_missing_interpolation_argument do |exception|
assert_equal 'missing interpolation argument :bar in "%{bar}" ({:baz=>"baz"} given)', exception.message
assert_equal %|missing interpolation argument :bar in "%{bar}" (#{{:baz=>"baz"}.to_s} given)|, exception.message
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/i18n/interpolate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def teardown
end

test "String interpolation can use custom missing interpolation handler" do
assert_equal %|Masao missing key is last, values are {:first=>"Masao"}, given string is '%{first} %{last}'|,
assert_equal %|Masao missing key is last, values are #{{:first=>"Masao"}.to_s}, given string is '%{first} %{last}'|,
I18n.interpolate("%{first} %{last}", :first => 'Masao')
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/locale/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,6 @@ def setup
end

test "#inspect" do
assert_equal('#<I18n::Locale::Fallbacks @map={:"de-AT"=>[:"de-DE"]} @defaults=[:"en-US", :en]>', @fallbacks.inspect)
assert_equal(%|#<I18n::Locale::Fallbacks @map=#{{:"de-AT"=>[:"de-DE"]}} @defaults=[:"en-US", :en]>|, @fallbacks.inspect)
end
end

0 comments on commit a6b16dc

Please sign in to comment.