2015-11-10 14:48:14 +03:00
|
|
|
# frozen_string_literal: true
|
2015-10-05 16:27:48 +03:00
|
|
|
|
2009-02-15 02:36:45 +03:00
|
|
|
require 'pp'
|
2009-09-27 08:08:31 +04:00
|
|
|
require 'delegate'
|
2009-02-15 02:36:45 +03:00
|
|
|
require 'test/unit'
|
|
|
|
|
2009-09-27 08:08:31 +04:00
|
|
|
module PPTestModule
|
|
|
|
|
2009-02-15 02:36:45 +03:00
|
|
|
class PPTest < Test::Unit::TestCase
|
|
|
|
def test_list0123_12
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], ''.dup, 12))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_list0123_11
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], ''.dup, 11))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
OverriddenStruct = Struct.new("OverriddenStruct", :members, :class)
|
|
|
|
def test_struct_override_members # [ruby-core:7865]
|
|
|
|
a = OverriddenStruct.new(1,2)
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("#<struct Struct::OverriddenStruct members=1, class=2>\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_redefined_method
|
2015-10-05 16:27:48 +03:00
|
|
|
o = "".dup
|
2009-02-15 02:36:45 +03:00
|
|
|
def o.method
|
|
|
|
end
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal(%(""\n), PP.pp(o, "".dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class HasInspect
|
|
|
|
def initialize(a)
|
|
|
|
@a = a
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
return "<inspect:#{@a.inspect}>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class HasPrettyPrint
|
|
|
|
def initialize(a)
|
|
|
|
@a = a
|
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_print(q)
|
|
|
|
q.text "<pretty_print:"
|
|
|
|
q.pp @a
|
|
|
|
q.text ">"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class HasBoth
|
|
|
|
def initialize(a)
|
|
|
|
@a = a
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
return "<inspect:#{@a.inspect}>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_print(q)
|
|
|
|
q.text "<pretty_print:"
|
|
|
|
q.pp @a
|
|
|
|
q.text ">"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class PrettyPrintInspect < HasPrettyPrint
|
|
|
|
alias inspect pretty_print_inspect
|
|
|
|
end
|
|
|
|
|
|
|
|
class PrettyPrintInspectWithoutPrettyPrint
|
|
|
|
alias inspect pretty_print_inspect
|
|
|
|
end
|
|
|
|
|
|
|
|
class PPInspectTest < Test::Unit::TestCase
|
|
|
|
def test_hasinspect
|
|
|
|
a = HasInspect.new(1)
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("<inspect:1>\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hasprettyprint
|
|
|
|
a = HasPrettyPrint.new(1)
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("<pretty_print:1>\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hasboth
|
|
|
|
a = HasBoth.new(1)
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("<pretty_print:1>\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pretty_print_inspect
|
|
|
|
a = PrettyPrintInspect.new(1)
|
|
|
|
assert_equal("<pretty_print:1>", a.inspect)
|
|
|
|
a = PrettyPrintInspectWithoutPrettyPrint.new
|
|
|
|
assert_raise(RuntimeError) { a.inspect }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proc
|
|
|
|
a = proc {1}
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s_with_iv
|
|
|
|
a = Object.new
|
|
|
|
def a.to_s() "aaa" end
|
|
|
|
a.instance_eval { @a = nil }
|
2015-10-05 16:27:48 +03:00
|
|
|
result = PP.pp(a, ''.dup)
|
2009-02-15 02:36:45 +03:00
|
|
|
assert_equal("#{a.inspect}\n", result)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s_without_iv
|
|
|
|
a = Object.new
|
|
|
|
def a.to_s() "aaa" end
|
2015-10-05 16:27:48 +03:00
|
|
|
result = PP.pp(a, ''.dup)
|
2009-02-15 02:36:45 +03:00
|
|
|
assert_equal("#{a.inspect}\n", result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class PPCycleTest < Test::Unit::TestCase
|
|
|
|
def test_array
|
|
|
|
a = []
|
|
|
|
a << a
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("[[...]]\n", PP.pp(a, ''.dup))
|
|
|
|
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash
|
|
|
|
a = {}
|
|
|
|
a[0] = a
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("{0=>{...}}\n", PP.pp(a, ''.dup))
|
|
|
|
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
S = Struct.new("S", :a, :b)
|
|
|
|
def test_struct
|
|
|
|
a = S.new(1,2)
|
|
|
|
a.b = a
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("#<struct Struct::S a=1, b=#<struct Struct::S:...>>\n", PP.pp(a, ''.dup))
|
|
|
|
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_object
|
|
|
|
a = Object.new
|
|
|
|
a.instance_eval {@a = a}
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal(a.inspect + "\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_anonymous
|
|
|
|
a = Class.new.new
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal(a.inspect + "\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_withinspect
|
|
|
|
a = []
|
|
|
|
a << HasInspect.new(a)
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''.dup))
|
|
|
|
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_share_nil
|
|
|
|
begin
|
|
|
|
PP.sharing_detection = true
|
|
|
|
a = [nil, nil]
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("[nil, nil]\n", PP.pp(a, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
ensure
|
|
|
|
PP.sharing_detection = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class PPSingleLineTest < Test::Unit::TestCase
|
|
|
|
def test_hash
|
2015-10-05 16:27:48 +03:00
|
|
|
assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, ''.dup)) # [ruby-core:02699]
|
|
|
|
assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''.dup))
|
2009-02-15 02:36:45 +03:00
|
|
|
end
|
|
|
|
end
|
2009-09-27 08:08:31 +04:00
|
|
|
|
|
|
|
class PPDelegateTest < Test::Unit::TestCase
|
|
|
|
class A < DelegateClass(Array); end
|
|
|
|
|
|
|
|
def test_delegate
|
|
|
|
assert_equal("[]\n", A.new([]).pretty_inspect, "[ruby-core:25804]")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-05 22:48:15 +03:00
|
|
|
class PPFileStatTest < Test::Unit::TestCase
|
|
|
|
def test_nothing_raised
|
|
|
|
assert_nothing_raised do
|
|
|
|
File.stat(__FILE__).pretty_inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-27 08:08:31 +04:00
|
|
|
end
|