Skip to content

Commit

Permalink
Merge pull request #709 from voxik/ruby-3.4
Browse files Browse the repository at this point in the history
Ruby 3.4 `Hash#inspect` compatibility.
  • Loading branch information
radar authored Dec 12, 2024
2 parents 3b65f65 + 2af0b5b commit 09e26d0
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 18 deletions.
19 changes: 16 additions & 3 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,35 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby_version: [3.3, 3.2, 3.1, '3.0', jruby]
ruby_version: [head, 3.3, 3.2, 3.1, '3.0', jruby]
gemfile:
- Gemfile
- gemfiles/Gemfile.rails-6.0.x
- gemfiles/Gemfile.rails-6.1.x
- gemfiles/Gemfile.rails-7.0.x
- gemfiles/Gemfile.rails-7.1.x
- gemfiles/Gemfile.rails-7.2.x
- gemfiles/Gemfile.rails-8.0.x
- gemfiles/Gemfile.rails-main
exclude:
# Ruby 3.0 is not supported by Rails main (requires at least Ruby 3.1)
# Rails 8+ requires at least Ruby 3.2
- ruby_version: '3.1'
gemfile: gemfiles/Gemfile.rails-main
- ruby_version: '3.0'
gemfile: gemfiles/Gemfile.rails-main
- ruby_version: '3.1'
gemfile: gemfiles/Gemfile.rails-8.0.x
- ruby_version: '3.0'
gemfile: gemfiles/Gemfile.rails-8.0.x
# Rails 7.2.x requires at least Ruby 3.1
- ruby_version: '3.0'
gemfile: gemfiles/Gemfile.rails-7.2.x
# JRuby is not supported by Rails 7.0.x
- ruby_version: jruby
gemfile: gemfiles/Gemfile.rails-7.0.x
# JRuby is not supported by Rails main
# JRuby is not supported by Rails 8+
- ruby_version: jruby
gemfile: gemfiles/Gemfile.rails-8.0.x
- ruby_version: jruby
gemfile: gemfiles/Gemfile.rails-main

Expand Down
2 changes: 2 additions & 0 deletions gemfiles/Gemfile.rails-6.1.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ gem 'test_declarative', '0.0.6'
gem 'rake'
gem 'minitest', '~> 5.14'
gem 'racc'
gem 'base64'
gem 'mutex_m'

platforms :mri do
gem 'oj'
Expand Down
14 changes: 14 additions & 0 deletions gemfiles/Gemfile.rails-7.2.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source 'https://rubygems.org'

gemspec :path => '..'

gem 'activesupport', '~> 7.2'
gem 'mocha', '~> 2'
gem 'test_declarative', '0.0.6'
gem 'rake'
gem 'minitest', '~> 5.1'
gem 'racc'

platforms :mri do
gem 'oj'
end
14 changes: 14 additions & 0 deletions gemfiles/Gemfile.rails-8.0.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source 'https://rubygems.org'

gemspec :path => '..'

gem 'activesupport', '~> 8.0'
gem 'mocha', '~> 2'
gem 'test_declarative', '0.0.6'
gem 'rake'
gem 'minitest', '~> 5.1'
gem 'racc'

platforms :mri do
gem 'oj'
end
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 09e26d0

Please sign in to comment.