From e3c3c540e6164336906c932671c9804dfcc89d63 Mon Sep 17 00:00:00 2001 From: Hashim Khan <64767361+Hashim1999164@users.noreply.github.com> Date: Thu, 13 Aug 2026 05:26:29 +0500 Subject: [PATCH] Allow Utils.inspect and Utils.to_s with no arguments These methods overrode Module#inspect and Module#to_s and required an object, so zero argument calls from tooling like SimpleCov raised ArgumentError. Fall back to super when no object is passed, and keep the existing object rendering behavior. Fixes #2107 --- lib/liquid/utils.rb | 12 ++++++++++-- test/unit/utils_unit_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/unit/utils_unit_test.rb diff --git a/lib/liquid/utils.rb b/lib/liquid/utils.rb index 084739a21..b4bc37f67 100644 --- a/lib/liquid/utils.rb +++ b/lib/liquid/utils.rb @@ -93,7 +93,12 @@ def self.to_liquid_value(obj) obj end - def self.to_s(obj, seen = {}) + # Optional first argument so Module#to_s / #inspect keep working when + # tooling (for example SimpleCov) calls these with no arguments. + def self.to_s(obj = (no_object = true + nil), seen = {}) + return super() if no_object + case obj when BigDecimal obj.to_s("F") @@ -113,7 +118,10 @@ def self.to_s(obj, seen = {}) end end - def self.inspect(obj, seen = {}) + def self.inspect(obj = (no_object = true + nil), seen = {}) + return super() if no_object + case obj when Hash # If the custom hash implementation overrides `#inspect`, use their diff --git a/test/unit/utils_unit_test.rb b/test/unit/utils_unit_test.rb new file mode 100644 index 000000000..1d0dc6ee8 --- /dev/null +++ b/test/unit/utils_unit_test.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'test_helper' + +class UtilsUnitTest < Minitest::Test + def test_inspect_with_no_arguments_uses_module_inspect + assert_equal("Liquid::Utils", Liquid::Utils.inspect) + end + + def test_to_s_with_no_arguments_uses_module_to_s + assert_equal("Liquid::Utils", Liquid::Utils.to_s) + end + + def test_inspect_still_renders_objects + assert_equal("nil", Liquid::Utils.inspect(nil)) + assert_equal("{\"a\"=>1}", Liquid::Utils.inspect({ "a" => 1 })) + end + + def test_to_s_still_renders_objects + assert_equal("", Liquid::Utils.to_s(nil)) + assert_equal("{\"a\"=>1}", Liquid::Utils.to_s({ "a" => 1 })) + end +end