1 # frozen_string_literal: true
6 require 'ruby2_keywords'
10 class PPTest < Test::Unit::TestCase
12 assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], ''.dup, 12))
16 assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], ''.dup, 11))
19 OverriddenStruct = Struct.new("OverriddenStruct", :members, :class)
20 def test_struct_override_members # [ruby-core:7865]
21 a = OverriddenStruct.new(1,2)
22 assert_equal("#<struct Struct::OverriddenStruct members=1, class=2>\n", PP.pp(a, ''.dup))
25 def test_redefined_method
29 assert_equal(%(""\n), PP.pp(o, "".dup))
33 assert_equal("0..1\n", PP.pp(0..1, "".dup))
34 assert_equal("0...1\n", PP.pp(0...1, "".dup))
35 assert_equal("0...\n", PP.pp(0..., "".dup))
36 assert_equal("...1\n", PP.pp(...1, "".dup))
46 return "<inspect:#{@a.inspect}>"
56 q.text "<pretty_print:"
68 return "<inspect:#{@a.inspect}>"
72 q.text "<pretty_print:"
78 class PrettyPrintInspect < HasPrettyPrint
79 alias inspect pretty_print_inspect
82 class PrettyPrintInspectWithoutPrettyPrint
83 alias inspect pretty_print_inspect
86 class PPInspectTest < Test::Unit::TestCase
89 assert_equal("<inspect:1>\n", PP.pp(a, ''.dup))
92 def test_hasprettyprint
93 a = HasPrettyPrint.new(1)
94 assert_equal("<pretty_print:1>\n", PP.pp(a, ''.dup))
99 assert_equal("<pretty_print:1>\n", PP.pp(a, ''.dup))
102 def test_pretty_print_inspect
103 a = PrettyPrintInspect.new(1)
104 assert_equal("<pretty_print:1>", a.inspect)
105 a = PrettyPrintInspectWithoutPrettyPrint.new
106 assert_raise(RuntimeError) { a.inspect }
111 assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
114 def test_to_s_with_iv
116 def a.to_s() "aaa" end
117 a.instance_eval { @a = nil }
118 result = PP.pp(a, ''.dup)
119 assert_equal("#{a.inspect}\n", result)
122 def test_to_s_without_iv
124 def a.to_s() "aaa" end
125 result = PP.pp(a, ''.dup)
126 assert_equal("#{a.inspect}\n", result)
130 class PPCycleTest < Test::Unit::TestCase
134 assert_equal("[[...]]\n", PP.pp(a, ''.dup))
135 assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
141 assert_equal("{0=>{...}}\n", PP.pp(a, ''.dup))
142 assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
145 S = Struct.new("S", :a, :b)
149 assert_equal("#<struct Struct::S a=1, b=#<struct Struct::S:...>>\n", PP.pp(a, ''.dup))
150 assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) unless RUBY_ENGINE == "truffleruby"
153 if defined?(Data.define)
154 D = Data.define(:aaa, :bbb)
156 a = D.new("aaa", "bbb")
157 assert_equal("#<data PPTestModule::PPCycleTest::D\n aaa=\"aaa\",\n bbb=\"bbb\">\n", PP.pp(a, ''.dup, 20))
158 assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
160 b = Data.define(:a).new(42)
161 assert_equal("#{b.inspect}\n", PP.pp(b, ''.dup))
167 a.instance_eval {@a = a}
168 assert_equal(a.inspect + "\n", PP.pp(a, ''.dup))
173 assert_equal(a.inspect + "\n", PP.pp(a, ''.dup))
177 omit if RUBY_ENGINE == "jruby" or RUBY_ENGINE == "truffleruby"
179 a << HasInspect.new(a)
180 assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''.dup))
181 assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
186 PP.sharing_detection = true
188 assert_equal("[nil, nil]\n", PP.pp(a, ''.dup))
190 PP.sharing_detection = false
195 class PPSingleLineTest < Test::Unit::TestCase
197 assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, ''.dup)) # [ruby-core:02699]
198 assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''.dup))
201 def test_hash_in_array
202 omit if RUBY_ENGINE == "jruby"
203 assert_equal("[{}]", PP.singleline_pp([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup))
204 assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup))
208 class PPDelegateTest < Test::Unit::TestCase
209 class A < DelegateClass(Array); end
212 assert_equal("[]\n", A.new([]).pretty_inspect, "[ruby-core:25804]")
215 def test_delegate_cycle
216 a = HasPrettyPrint.new nil
218 a.instance_eval {@a = a}
219 cycle_pretty_inspect = a.pretty_inspect
221 a.instance_eval {@a = SimpleDelegator.new(a)}
222 delegator_cycle_pretty_inspect = a.pretty_inspect
224 assert_equal(cycle_pretty_inspect, delegator_cycle_pretty_inspect)
228 class PPFileStatTest < Test::Unit::TestCase
229 def test_nothing_raised
230 assert_nothing_raised do
231 File.stat(__FILE__).pretty_inspect
237 class PPAbstractSyntaxTree < Test::Unit::TestCase
238 AST = RubyVM::AbstractSyntaxTree
239 def test_lasgn_literal
240 ast = AST.parse("_=1")
241 integer = RUBY_VERSION >= "3.4." ? "INTEGER" : "LIT"
242 expected = "(SCOPE@1:0-1:3 tbl: [:_] args: nil body: (LASGN@1:0-1:3 :_ (#{integer}@1:2-1:3 1)))"
243 assert_equal(expected, PP.singleline_pp(ast, ''.dup), ast)
248 class PPInheritedTest < Test::Unit::TestCase
249 class PPSymbolHash < PP
250 def pp_hash_pair(k, v)
253 text k.inspect.delete_prefix(":")
265 def test_hash_override
266 obj = {k: 1, "": :null, "0": :zero, 100 => :ten}
267 assert_equal <<~EXPECT, PPSymbolHash.pp(obj, "".dup)
268 {k: 1, "": :null, "0": :zero, 100=>:ten}