diff --git a/lib/holidays/definition/repository/proc_result_cache.rb b/lib/holidays/definition/repository/proc_result_cache.rb index 8b3b465e..6f35a8db 100644 --- a/lib/holidays/definition/repository/proc_result_cache.rb +++ b/lib/holidays/definition/repository/proc_result_cache.rb @@ -43,7 +43,10 @@ def validate!(function, function_arguments) end def build_proc_key(function, function_arguments) - Digest::MD5.hexdigest("#{function.to_s}_#{function_arguments.join('_')}") + args_string = function_arguments.map { |arg| + arg.is_a?(Date) ? arg.iso8601 : arg.to_s + }.join("_") + Digest::MD5.hexdigest("#{function.to_s}_#{args_string}") end end end diff --git a/test/holidays/definition/repository/test_proc_result_cache.rb b/test/holidays/definition/repository/test_proc_result_cache.rb index 6bedaab3..78f4a9d3 100644 --- a/test/holidays/definition/repository/test_proc_result_cache.rb +++ b/test/holidays/definition/repository/test_proc_result_cache.rb @@ -68,6 +68,21 @@ def test_accepts_mix_of_integers_and_dates_for_multiple_function_arguments assert_equal(Date.civil(2016, 1, 6), @subject.lookup(function, date, modifier)) end + def test_build_proc_key_uses_iso8601_for_date_arguments + # This test ensures Date arguments are formatted using iso8601 rather than + # implicit to_s, which avoids Ruby 3.1+ deprecation warnings about + # Date#to_s without a format argument. + function = lambda { |date| date + 1 } + date = Date.civil(2016, 1, 15) + + # Call lookup twice with the same date - should hit the cache + result1 = @subject.lookup(function, date) + result2 = @subject.lookup(function, date) + + assert_equal(result1, result2) + assert_equal(Date.civil(2016, 1, 16), result1) + end + def test_lookup_raises_error_if_function_argument_is_not_valid function = lambda { |year| Date.civil(year, 2, 1) - 1 } function_argument = "2015"