2015-11-10 06:48:14 -05:00
|
|
|
# frozen_string_literal: true
|
2015-10-05 09:27:48 -04:00
|
|
|
|
2009-02-14 18:36:45 -05:00
|
|
|
require 'pp'
|
2009-09-27 00:08:31 -04:00
|
|
|
require 'delegate'
|
2009-02-14 18:36:45 -05:00
|
|
|
require 'test/unit'
|
2021-09-28 10:53:03 -04:00
|
|
|
require 'ruby2_keywords'
|
|
|
|
|
2021-12-07 16:10:16 -05:00
|
|
|
# Define bind_call for Ruby 2.6 and earlier, to allow testing on JRuby 9.3
|
2021-09-28 10:53:03 -04:00
|
|
|
class UnboundMethod
|
|
|
|
unless public_method_defined?(:bind_call)
|
|
|
|
def bind_call(obj, *args, &block)
|
|
|
|
bind(obj).call(*args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-02-14 18:36:45 -05:00
|
|
|
|
2009-09-27 00:08:31 -04:00
|
|
|
module PPTestModule
|
|
|
|
|
2009-02-14 18:36:45 -05:00
|
|
|
class PPTest < Test::Unit::TestCase
|
|
|
|
def test_list0123_12
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], ''.dup, 12))
|
2009-02-14 18:36:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_list0123_11
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], ''.dup, 11))
|
2009-02-14 18:36:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
OverriddenStruct = Struct.new("OverriddenStruct", :members, :class)
|
|
|
|
def test_struct_override_members # [ruby-core:7865]
|
|
|
|
a = OverriddenStruct.new(1,2)
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal("#<struct Struct::OverriddenStruct members=1, class=2>\n", PP.pp(a, ''.dup))
|
2009-02-14 18:36:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_redefined_method
|
2015-10-05 09:27:48 -04:00
|
|
|
o = "".dup
|
2009-02-14 18:36:45 -05:00
|
|
|
def o.method
|
|
|
|
end
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal(%(""\n), PP.pp(o, "".dup))
|
2009-02-14 18:36:45 -05: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 09:27:48 -04:00
|
|
|
assert_equal("<inspect:1>\n", PP.pp(a, ''.dup))
|
2009-02-14 18:36:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hasprettyprint
|
|
|
|
a = HasPrettyPrint.new(1)
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal("<pretty_print:1>\n", PP.pp(a, ''.dup))
|
2009-02-14 18:36:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hasboth
|
|
|
|
a = HasBoth.new(1)
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal("<pretty_print:1>\n", PP.pp(a, ''.dup))
|
2009-02-14 18:36:45 -05: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 09:27:48 -04:00
|
|
|
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
|
2009-02-14 18:36:45 -05: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 09:27:48 -04:00
|
|
|
result = PP.pp(a, ''.dup)
|
2009-02-14 18:36:45 -05: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 09:27:48 -04:00
|
|
|
result = PP.pp(a, ''.dup)
|
2009-02-14 18:36:45 -05:00
|
|
|
assert_equal("#{a.inspect}\n", result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class PPCycleTest < Test::Unit::TestCase
|
|
|
|
def test_array
|
|
|
|
a = []
|
|
|
|
a << a
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal("[[...]]\n", PP.pp(a, ''.dup))
|
|
|
|
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
|
2009-02-14 18:36:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash
|
|
|
|
a = {}
|
|
|
|
a[0] = a
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal("{0=>{...}}\n", PP.pp(a, ''.dup))
|
|
|
|
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
|
2009-02-14 18:36:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
S = Struct.new("S", :a, :b)
|
|
|
|
def test_struct
|
|
|
|
a = S.new(1,2)
|
|
|
|
a.b = a
|
2015-10-05 09:27:48 -04: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-14 18:36:45 -05:00
|
|
|
end
|
|
|
|
|
2022-10-09 23:30:35 -04:00
|
|
|
if "3.2" <= RUBY_VERSION
|
|
|
|
D = Data.define(:aaa, :bbb)
|
|
|
|
def test_data
|
|
|
|
a = D.new("aaa", "bbb")
|
|
|
|
assert_equal("#<data PPTestModule::PPCycleTest::D\n aaa=\"aaa\",\n bbb=\"bbb\">\n", PP.pp(a, ''.dup, 20))
|
|
|
|
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-14 18:36:45 -05:00
|
|
|
def test_object
|
|
|
|
a = Object.new
|
|
|
|
a.instance_eval {@a = a}
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal(a.inspect + "\n", PP.pp(a, ''.dup))
|
2009-02-14 18:36:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_anonymous
|
|
|
|
a = Class.new.new
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal(a.inspect + "\n", PP.pp(a, ''.dup))
|
2009-02-14 18:36:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_withinspect
|
|
|
|
a = []
|
|
|
|
a << HasInspect.new(a)
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''.dup))
|
|
|
|
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
|
2021-12-07 16:10:16 -05:00
|
|
|
end unless RUBY_VERSION < "2.7" # temporary mask to test on JRuby 9.3 (2.6 equivalent)
|
2009-02-14 18:36:45 -05:00
|
|
|
|
|
|
|
def test_share_nil
|
|
|
|
begin
|
|
|
|
PP.sharing_detection = true
|
|
|
|
a = [nil, nil]
|
2015-10-05 09:27:48 -04:00
|
|
|
assert_equal("[nil, nil]\n", PP.pp(a, ''.dup))
|
2009-02-14 18:36:45 -05:00
|
|
|
ensure
|
|
|
|
PP.sharing_detection = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class PPSingleLineTest < Test::Unit::TestCase
|
|
|
|
def test_hash
|
2015-10-05 09:27:48 -04: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-14 18:36:45 -05:00
|
|
|
end
|
2020-01-21 19:14:10 -05:00
|
|
|
|
|
|
|
def test_hash_in_array
|
2020-01-22 21:12:34 -05:00
|
|
|
assert_equal("[{}]", PP.singleline_pp([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup))
|
2020-01-23 04:59:26 -05:00
|
|
|
assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup))
|
2020-01-21 19:14:10 -05:00
|
|
|
end
|
2009-02-14 18:36:45 -05:00
|
|
|
end
|
2009-09-27 00: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
|
2017-01-21 20:50:08 -05:00
|
|
|
|
|
|
|
def test_delegate_cycle
|
|
|
|
a = HasPrettyPrint.new nil
|
|
|
|
|
|
|
|
a.instance_eval {@a = a}
|
|
|
|
cycle_pretty_inspect = a.pretty_inspect
|
|
|
|
|
|
|
|
a.instance_eval {@a = SimpleDelegator.new(a)}
|
|
|
|
delegator_cycle_pretty_inspect = a.pretty_inspect
|
|
|
|
|
|
|
|
assert_equal(cycle_pretty_inspect, delegator_cycle_pretty_inspect)
|
|
|
|
end
|
2009-09-27 00:08:31 -04:00
|
|
|
end
|
|
|
|
|
2014-12-05 14:48:15 -05:00
|
|
|
class PPFileStatTest < Test::Unit::TestCase
|
|
|
|
def test_nothing_raised
|
|
|
|
assert_nothing_raised do
|
|
|
|
File.stat(__FILE__).pretty_inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-05 17:48:52 -05:00
|
|
|
if defined?(RubyVM)
|
|
|
|
class PPAbstractSyntaxTree < Test::Unit::TestCase
|
|
|
|
AST = RubyVM::AbstractSyntaxTree
|
|
|
|
def test_lasgn_literal
|
|
|
|
ast = AST.parse("_=1")
|
|
|
|
expected = "(SCOPE@1:0-1:3 tbl: [:_] args: nil body: (LASGN@1:0-1:3 :_ (LIT@1:2-1:3 1)))"
|
|
|
|
assert_equal(expected, PP.singleline_pp(ast, ''.dup), ast)
|
|
|
|
end
|
2018-12-02 19:24:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-27 00:08:31 -04:00
|
|
|
end
|