From 7958cf9bbb322f9baf9f8bdaacd64e2851da2abb Mon Sep 17 00:00:00 2001 From: Shivaansh Dhingra Date: Thu, 10 Sep 2026 14:00:46 -0400 Subject: [PATCH] Fix Liquid::Utils.inspect and to_s to allow zero arguments --- lib/liquid/utils.rb | 64 +++++++++++++++++++++--------------- test/unit/utils_unit_test.rb | 28 ++++++++++++++++ 2 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 test/unit/utils_unit_test.rb diff --git a/lib/liquid/utils.rb b/lib/liquid/utils.rb index 084739a21..bc99e7073 100644 --- a/lib/liquid/utils.rb +++ b/lib/liquid/utils.rb @@ -93,41 +93,53 @@ def self.to_liquid_value(obj) obj end - def self.to_s(obj, seen = {}) - case obj - when BigDecimal - obj.to_s("F") - when Hash - # If the custom hash implementation overrides `#to_s`, use their - # custom implementation. Otherwise we use Liquid's default - # implementation. - if obj.class.instance_method(:to_s) == HASH_TO_S_METHOD - hash_inspect(obj, seen) + def self.to_s(*args) + if args.empty? + super + else + obj = args[0] + seen = args[1] || {} + case obj + when BigDecimal + obj.to_s("F") + when Hash + # If the custom hash implementation overrides `#to_s`, use their + # custom implementation. Otherwise we use Liquid's default + # implementation. + if obj.class.instance_method(:to_s) == HASH_TO_S_METHOD + hash_inspect(obj, seen) + else + obj.to_s + end + when Array + array_inspect(obj, seen) else obj.to_s end - when Array - array_inspect(obj, seen) - else - obj.to_s end end - def self.inspect(obj, seen = {}) - case obj - when Hash - # If the custom hash implementation overrides `#inspect`, use their - # custom implementation. Otherwise we use Liquid's default - # implementation. - if obj.class.instance_method(:inspect) == HASH_INSPECT_METHOD - hash_inspect(obj, seen) + def self.inspect(*args) + if args.empty? + super + else + obj = args[0] + seen = args[1] || {} + case obj + when Hash + # If the custom hash implementation overrides `#inspect`, use their + # custom implementation. Otherwise we use Liquid's default + # implementation. + if obj.class.instance_method(:inspect) == HASH_INSPECT_METHOD + hash_inspect(obj, seen) + else + obj.inspect + end + when Array + array_inspect(obj, seen) else obj.inspect end - when Array - array_inspect(obj, seen) - else - obj.inspect end end diff --git a/test/unit/utils_unit_test.rb b/test/unit/utils_unit_test.rb new file mode 100644 index 000000000..8f9f79d7b --- /dev/null +++ b/test/unit/utils_unit_test.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'test_helper' + +class UtilsUnitTest < Minitest::Test + def test_inspect_without_arguments_calls_super + assert_equal("Liquid::Utils", Liquid::Utils.inspect) + end + + def test_inspect_with_object + assert_equal('"hello"', Liquid::Utils.inspect("hello")) + assert_equal('123', Liquid::Utils.inspect(123)) + assert_equal('[1, 2]', Liquid::Utils.inspect([1, 2])) + assert_equal('{"a"=>1}', Liquid::Utils.inspect({ "a" => 1 })) + assert_equal('nil', Liquid::Utils.inspect(nil)) + end + + def test_to_s_without_arguments_calls_super + assert_equal("Liquid::Utils", Liquid::Utils.to_s) + end + + def test_to_s_with_object + assert_equal("hello", Liquid::Utils.to_s("hello")) + assert_equal("123", Liquid::Utils.to_s(123)) + assert_equal("[1, 2]", Liquid::Utils.to_s([1, 2])) + assert_equal('{"a"=>1}', Liquid::Utils.to_s({ "a" => 1 })) + end +end