Add #to_s and pretty print for ActiveSupport::InheritableOptions

This commit is contained in:
Kevin McPhillips 2023-11-28 17:46:42 -05:00
parent ea46a00a9f
commit 3d60490a9d
2 changed files with 51 additions and 0 deletions

View File

@ -116,6 +116,14 @@ module ActiveSupport
"#<#{self.class.name} #{to_h.inspect}>"
end
def to_s
to_h.to_s
end
def pretty_print(pp)
pp.pp_hash(to_h)
end
alias_method :own_key?, :key?
private :own_key?

View File

@ -1,5 +1,6 @@
# frozen_string_literal: true
require "pp"
require_relative "abstract_unit"
require "active_support/ordered_options"
@ -278,4 +279,46 @@ class OrderedOptionsTest < ActiveSupport::TestCase
assert_equal 3, object.count
end
def test_ordered_options_to_s
object = ActiveSupport::OrderedOptions.new
assert_equal "{}", object.to_s
object.one = "first value"
object[:two] = "second value"
object["three"] = "third value"
assert_equal "{:one=>\"first value\", :two=>\"second value\", :three=>\"third value\"}", object.to_s
end
def test_inheritable_options_to_s
object = ActiveSupport::InheritableOptions.new(one: "first value")
assert_equal "{:one=>\"first value\"}", object.to_s
object[:two] = "second value"
object["three"] = "third value"
assert_equal "{:one=>\"first value\", :two=>\"second value\", :three=>\"third value\"}", object.to_s
end
def test_odrered_options_pp
object = ActiveSupport::OrderedOptions.new
object.one = "first value"
object[:two] = "second value"
object["three"] = "third value"
io = StringIO.new
PP.pp(object, io)
assert_equal "{:one=>\"first value\", :two=>\"second value\", :three=>\"third value\"}\n", io.string
end
def test_inheritable_options_pp
object = ActiveSupport::InheritableOptions.new(one: "first value")
object[:two] = "second value"
object["three"] = "third value"
assert_equal "{:one=>\"first value\", :two=>\"second value\", :three=>\"third value\"}", object.to_s
io = StringIO.new
PP.pp(object, io)
assert_equal "{:one=>\"first value\", :two=>\"second value\", :three=>\"third value\"}\n", io.string
end
end