Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 38 additions & 26 deletions lib/liquid/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions test/unit/utils_unit_test.rb
Original file line number Diff line number Diff line change
@@ -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
Loading