1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
# frozen_string_literal: true
require 'pp'
require 'delegate'
require 'test/unit'
require 'ruby2_keywords'
module PPTestModule
class PPTest < Test::Unit::TestCase
def test_list0123_12
assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], ''.dup, 12))
end
def test_list0123_11
assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], ''.dup, 11))
end
OverriddenStruct = Struct.new("OverriddenStruct", :members, :class)
def test_struct_override_members # [ruby-core:7865]
a = OverriddenStruct.new(1,2)
assert_equal("#<struct Struct::OverriddenStruct members=1, class=2>\n", PP.pp(a, ''.dup))
end
def test_redefined_method
o = "".dup
def o.method
end
assert_equal(%(""\n), PP.pp(o, "".dup))
end
def test_range
assert_equal("0..1\n", PP.pp(0..1, "".dup))
assert_equal("0...1\n", PP.pp(0...1, "".dup))
assert_equal("0...\n", PP.pp(0..., "".dup))
assert_equal("...1\n", PP.pp(...1, "".dup))
assert_equal("..false\n", PP.pp(..false, "".dup))
assert_equal("false..\n", PP.pp(false.., "".dup))
assert_equal("false..false\n", PP.pp(false..false, "".dup))
assert_equal("nil..nil\n", PP.pp(nil..nil, "".dup))
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)
assert_equal("<inspect:1>\n", PP.pp(a, ''.dup))
end
def test_hasprettyprint
a = HasPrettyPrint.new(1)
assert_equal("<pretty_print:1>\n", PP.pp(a, ''.dup))
end
def test_hasboth
a = HasBoth.new(1)
assert_equal("<pretty_print:1>\n", PP.pp(a, ''.dup))
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}
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
end
def test_to_s_with_iv
a = Object.new
def a.to_s() "aaa" end
a.instance_eval { @a = nil }
result = PP.pp(a, ''.dup)
assert_equal("#{a.inspect}\n", result)
end
def test_to_s_without_iv
a = Object.new
def a.to_s() "aaa" end
result = PP.pp(a, ''.dup)
assert_equal("#{a.inspect}\n", result)
end
def test_basic_object
a = BasicObject.new
assert_match(/\A#<BasicObject:0x[\da-f]+>\n\z/, PP.pp(a, ''.dup))
end
end
class PPCycleTest < Test::Unit::TestCase
def test_array
a = []
a << a
assert_equal("[[...]]\n", PP.pp(a, ''.dup))
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
end
def test_hash
a = {}
a[0] = a
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
end
S = Struct.new("S", :a, :b)
def test_struct
a = S.new(1,2)
a.b = a
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)) unless RUBY_ENGINE == "truffleruby"
end
if defined?(Data.define)
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))
b = Data.define(:a).new(42)
assert_equal("#{b.inspect}\n", PP.pp(b, ''.dup))
end
D2 = Data.define(:aaa, :bbb) do
private :aaa
end
def test_data_private_member
a = D2.new("aaa", "bbb")
assert_equal("#<data PPTestModule::PPCycleTest::D2\n aaa=\"aaa\",\n bbb=\"bbb\">\n", PP.pp(a, ''.dup, 20))
end
D3 = Data.define(:aaa, :bbb) do
remove_method :aaa
end
def test_data_removed_member
a = D3.new("aaa", "bbb")
assert_equal("#<data PPTestModule::PPCycleTest::D3\n bbb=\"bbb\">\n", PP.pp(a, ''.dup, 20))
end
end
def test_object
a = Object.new
a.instance_eval {@a = a}
assert_equal(a.inspect + "\n", PP.pp(a, ''.dup))
end
def test_anonymous
a = Class.new.new
assert_equal(a.inspect + "\n", PP.pp(a, ''.dup))
end
def test_withinspect
omit if RUBY_ENGINE == "jruby" or RUBY_ENGINE == "truffleruby"
a = []
a << HasInspect.new(a)
assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''.dup))
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
end
def test_share_nil
begin
PP.sharing_detection = true
a = [nil, nil]
assert_equal("[nil, nil]\n", PP.pp(a, ''.dup))
ensure
PP.sharing_detection = false
end
end
end
class PPSingleLineTest < Test::Unit::TestCase
def test_hash
assert_equal({1 => 1}.inspect, PP.singleline_pp({1 => 1}, ''.dup)) # [ruby-core:02699]
assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''.dup))
end
def test_hash_symbol_colon_key
omit if RUBY_VERSION < "3.4."
no_quote = "{a: 1, a!: 1, a?: 1}"
unicode_quote = "{\u{3042}: 1}"
quote0 = '{"": 1}'
quote1 = '{"0": 1, "!": 1, "%": 1, "&": 1, "*": 1, "+": 1, "-": 1, "/": 1, "<": 1, ">": 1, "^": 1, "`": 1, "|": 1, "~": 1}'
quote2 = '{"@a": 1, "$a": 1, "+@": 1, "a=": 1, "[]": 1}'
quote3 = '{"a\"b": 1, "@@a": 1, "<=>": 1, "===": 1, "[]=": 1}'
assert_equal(no_quote, PP.singleline_pp(eval(no_quote), ''.dup))
assert_equal({ "\u3042": 1 }.inspect, PP.singleline_pp(eval(unicode_quote), ''.dup))
assert_equal(quote0, PP.singleline_pp(eval(quote0), ''.dup))
assert_equal(quote1, PP.singleline_pp(eval(quote1), ''.dup))
assert_equal(quote2, PP.singleline_pp(eval(quote2), ''.dup))
assert_equal(quote3, PP.singleline_pp(eval(quote3), ''.dup))
end
def test_hash_in_array
omit if RUBY_ENGINE == "jruby"
assert_equal("[{}]", PP.singleline_pp([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup))
assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup))
end
def test_direct_pp
buffer = String.new
a = []
a << a
# Isolate the test from any existing Thread.current[:__recursive_key__][:inspect].
Thread.new do
q = PP::SingleLine.new(buffer)
q.pp(a)
q.flush
end.join
assert_equal("[[...]]", buffer)
end
end
class PPDelegateTest < Test::Unit::TestCase
class A < DelegateClass(Array); end
def test_delegate
assert_equal("[]\n", A.new([]).pretty_inspect, "[ruby-core:25804]")
end
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
end
class PPFileStatTest < Test::Unit::TestCase
def test_nothing_raised
assert_nothing_raised do
File.stat(__FILE__).pretty_inspect
end
end
end
if defined?(RubyVM)
class PPAbstractSyntaxTree < Test::Unit::TestCase
AST = RubyVM::AbstractSyntaxTree
def test_lasgn_literal
ast = AST.parse("_=1")
integer = RUBY_VERSION >= "3.4." ? "INTEGER" : "LIT"
expected = "(SCOPE@1:0-1:3 tbl: [:_] args: nil body: (LASGN@1:0-1:3 :_ (#{integer}@1:2-1:3 1)))"
assert_equal(expected, PP.singleline_pp(ast, ''.dup), ast)
end
end
end
class PPInheritedTest < Test::Unit::TestCase
class PPSymbolHash < PP
def pp_hash_pair(k, v)
case k
when Symbol
text k.inspect.delete_prefix(":").tr('"', "'")
text ":"
group(1) {
breakable
pp v
}
else
super
end
end
end
def test_hash_override
obj = {k: 1, "": :null, "0": :zero, 100 => :ten}
sep = RUBY_VERSION >= "3.4." ? " => " : "=>"
assert_equal <<~EXPECT, PPSymbolHash.pp(obj, "".dup)
{k: 1, '': :null, '0': :zero, 100#{sep}:ten}
EXPECT
end
end
end
|