mirror of https://github.com/rails/rails
Add #to_s and pretty print for ActiveSupport::InheritableOptions
This commit is contained in:
parent
ea46a00a9f
commit
3d60490a9d
|
@ -116,6 +116,14 @@ module ActiveSupport
|
||||||
"#<#{self.class.name} #{to_h.inspect}>"
|
"#<#{self.class.name} #{to_h.inspect}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
to_h.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def pretty_print(pp)
|
||||||
|
pp.pp_hash(to_h)
|
||||||
|
end
|
||||||
|
|
||||||
alias_method :own_key?, :key?
|
alias_method :own_key?, :key?
|
||||||
private :own_key?
|
private :own_key?
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "pp"
|
||||||
require_relative "abstract_unit"
|
require_relative "abstract_unit"
|
||||||
require "active_support/ordered_options"
|
require "active_support/ordered_options"
|
||||||
|
|
||||||
|
@ -278,4 +279,46 @@ class OrderedOptionsTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
assert_equal 3, object.count
|
assert_equal 3, object.count
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue