[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / test / test_pp.rb
blob2fdd5df1148a83e9ae74bdd4700872d6626d49eb
1 # frozen_string_literal: true
3 require 'pp'
4 require 'delegate'
5 require 'test/unit'
6 require 'ruby2_keywords'
8 module PPTestModule
10 class PPTest < Test::Unit::TestCase
11   def test_list0123_12
12     assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], ''.dup, 12))
13   end
15   def test_list0123_11
16     assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], ''.dup, 11))
17   end
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))
23   end
25   def test_redefined_method
26     o = "".dup
27     def o.method
28     end
29     assert_equal(%(""\n), PP.pp(o, "".dup))
30   end
32   def test_range
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))
37   end
38 end
40 class HasInspect
41   def initialize(a)
42     @a = a
43   end
45   def inspect
46     return "<inspect:#{@a.inspect}>"
47   end
48 end
50 class HasPrettyPrint
51   def initialize(a)
52     @a = a
53   end
55   def pretty_print(q)
56     q.text "<pretty_print:"
57     q.pp @a
58     q.text ">"
59   end
60 end
62 class HasBoth
63   def initialize(a)
64     @a = a
65   end
67   def inspect
68     return "<inspect:#{@a.inspect}>"
69   end
71   def pretty_print(q)
72     q.text "<pretty_print:"
73     q.pp @a
74     q.text ">"
75   end
76 end
78 class PrettyPrintInspect < HasPrettyPrint
79   alias inspect pretty_print_inspect
80 end
82 class PrettyPrintInspectWithoutPrettyPrint
83   alias inspect pretty_print_inspect
84 end
86 class PPInspectTest < Test::Unit::TestCase
87   def test_hasinspect
88     a = HasInspect.new(1)
89     assert_equal("<inspect:1>\n", PP.pp(a, ''.dup))
90   end
92   def test_hasprettyprint
93     a = HasPrettyPrint.new(1)
94     assert_equal("<pretty_print:1>\n", PP.pp(a, ''.dup))
95   end
97   def test_hasboth
98     a = HasBoth.new(1)
99     assert_equal("<pretty_print:1>\n", PP.pp(a, ''.dup))
100   end
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 }
107   end
109   def test_proc
110     a = proc {1}
111     assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
112   end
114   def test_to_s_with_iv
115     a = Object.new
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)
120   end
122   def test_to_s_without_iv
123     a = Object.new
124     def a.to_s() "aaa" end
125     result = PP.pp(a, ''.dup)
126     assert_equal("#{a.inspect}\n", result)
127   end
130 class PPCycleTest < Test::Unit::TestCase
131   def test_array
132     a = []
133     a << a
134     assert_equal("[[...]]\n", PP.pp(a, ''.dup))
135     assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
136   end
138   def test_hash
139     a = {}
140     a[0] = a
141     assert_equal("{0=>{...}}\n", PP.pp(a, ''.dup))
142     assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
143   end
145   S = Struct.new("S", :a, :b)
146   def test_struct
147     a = S.new(1,2)
148     a.b = a
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"
151   end
153   if defined?(Data.define)
154     D = Data.define(:aaa, :bbb)
155     def test_data
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))
162     end
163   end
165   def test_object
166     a = Object.new
167     a.instance_eval {@a = a}
168     assert_equal(a.inspect + "\n", PP.pp(a, ''.dup))
169   end
171   def test_anonymous
172     a = Class.new.new
173     assert_equal(a.inspect + "\n", PP.pp(a, ''.dup))
174   end
176   def test_withinspect
177     omit if RUBY_ENGINE == "jruby" or RUBY_ENGINE == "truffleruby"
178     a = []
179     a << HasInspect.new(a)
180     assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''.dup))
181     assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
182   end
184   def test_share_nil
185     begin
186       PP.sharing_detection = true
187       a = [nil, nil]
188       assert_equal("[nil, nil]\n", PP.pp(a, ''.dup))
189     ensure
190       PP.sharing_detection = false
191     end
192   end
195 class PPSingleLineTest < Test::Unit::TestCase
196   def test_hash
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))
199   end
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))
205   end
208 class PPDelegateTest < Test::Unit::TestCase
209   class A < DelegateClass(Array); end
211   def test_delegate
212     assert_equal("[]\n", A.new([]).pretty_inspect, "[ruby-core:25804]")
213   end
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)
225   end
228 class PPFileStatTest < Test::Unit::TestCase
229   def test_nothing_raised
230     assert_nothing_raised do
231       File.stat(__FILE__).pretty_inspect
232     end
233   end
236 if defined?(RubyVM)
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)
244     end
245   end
248 class PPInheritedTest < Test::Unit::TestCase
249   class PPSymbolHash < PP
250     def pp_hash_pair(k, v)
251       case k
252       when Symbol
253         text k.inspect.delete_prefix(":")
254         text ":"
255         group(1) {
256           breakable
257           pp v
258         }
259       else
260         super
261       end
262     end
263   end
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}
269     EXPECT
270   end