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'
|
2021-09-28 17:53:03 +03:00
|
|
|
require 'ruby2_keywords'
|
|
|
|
|
2021-12-08 00:10:16 +03:00
|
|
|
# Define bind_call for Ruby 2.6 and earlier, to allow testing on JRuby 9.3
|
2021-09-28 17:53:03 +03: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-15 02:36:45 +03:00
|
|
|
|
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
|
|
|
|
|
2022-10-10 06:30:35 +03: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-15 02:36:45 +03:00
|
|
|
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))
|
2021-12-08 00:10:16 +03:00
|
|
|
end unless RUBY_VERSION < "2.7" # temporary mask to test on JRuby 9.3 (2.6 equivalent)
|
2009-02-15 02:36:45 +03:00
|
|
|
|
|
|
|
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
|
2020-01-22 03:14:10 +03:00
|
|
|
|
|
|
|
def test_hash_in_array
|
2020-01-23 05:12:34 +03:00
|
|
|
assert_equal("[{}]", PP.singleline_pp([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup))
|
2020-01-23 12:59:26 +03:00
|
|
|
assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup))
|
2020-01-22 03:14:10 +03:00
|
|
|
end
|
2009-02-15 02:36:45 +03:00
|
|
|
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
|
2017-01-22 04:50:08 +03: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 08:08:31 +04:00
|
|
|
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
|
|
|
|
|
2020-03-06 01:48:52 +03: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-03 03:24:38 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-27 08:08:31 +04:00
|
|
|
end
|