-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathshow.jl
2817 lines (2489 loc) · 106 KB
/
show.jl
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
using LinearAlgebra
# For curmod_*
include("testenv.jl")
replstr(x, kv::Pair...) = sprint((io,x) -> show(IOContext(io, :limit => true, :displaysize => (24, 80), kv...), MIME("text/plain"), x), x)
showstr(x, kv::Pair...) = sprint((io,x) -> show(IOContext(io, :limit => true, :displaysize => (24, 80), kv...), x), x)
@testset "IOContext" begin
io = IOBuffer()
ioc = IOContext(io)
@test ioc.io == io
@test ioc.dict == Base.ImmutableDict{Symbol, Any}()
ioc = IOContext(io, :x => 1)
@test ioc.io == io
@test ioc.dict == Base.ImmutableDict{Symbol, Any}(:x, 1)
ioc = IOContext(io, :x => 1, :y => 2)
@test ioc.io == io
@test ioc.dict == Base.ImmutableDict(Base.ImmutableDict{Symbol, Any}(:x, 1),
:y => 2)
@test Base.ImmutableDict((key => ioc[key] for key in keys(ioc))...) == ioc.dict
@test keys(IOBuffer()) isa Base.KeySet
@test length(keys(IOBuffer())) == 0
end
@test replstr(Array{Any}(undef, 2)) == "2-element Vector{Any}:\n #undef\n #undef"
@test replstr(Array{Any}(undef, 2,2)) == "2×2 Matrix{Any}:\n #undef #undef\n #undef #undef"
@test replstr(Array{Any}(undef, 2,2,2)) == "2×2×2 Array{Any, 3}:\n[:, :, 1] =\n #undef #undef\n #undef #undef\n\n[:, :, 2] =\n #undef #undef\n #undef #undef"
@test replstr([1f10]) == "1-element Vector{Float32}:\n 1.0f10"
struct T5589
names::Vector{String}
end
@test replstr(T5589(Vector{String}(undef, 100))) == "$(curmod_prefix)T5589([#undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef … #undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef, #undef])"
@test replstr(Meta.parse("mutable struct X end")) == ":(mutable struct X\n #= none:1 =#\n end)"
@test replstr(Meta.parse("struct X end")) == ":(struct X\n #= none:1 =#\n end)"
let s = "ccall(:f, Int, (Ptr{Cvoid},), &x)"
@test replstr(Meta.parse(s)) == ":($s)"
end
# recursive array printing
# issue #10353
let a = Any[]
push!(a,a)
show(IOBuffer(), a)
push!(a,a)
show(IOBuffer(), a)
end
# expression printing
macro test_repr(x)
# this is a macro instead of function so we can avoid getting useful backtraces :)
return :(test_repr($(esc(x))))
end
macro weak_test_repr(x)
# this is a macro instead of function so we can avoid getting useful backtraces :)
return :(test_repr($(esc(x)), true))
end
function test_repr(x::String, remove_linenums::Bool = false)
# Note: We can't just compare x1 and x2 because interpolated
# strings get converted to string Exprs by the first show().
# This could produce a few false positives, but until string
# interpolation works we don't really have a choice.
#
# Rectification: comparing x1 and x2 seems to be working
x1 = Meta.parse(x)
x2 = eval(Meta.parse(repr(x1)))
x3 = eval(Meta.parse(repr(x2)))
if !remove_linenums
if ! (x1 == x2 == x3)
error(string(
"\nrepr test (Rule 2) failed:",
"\noriginal: ", x,
"\n\npreparsed: ", x1, "\n", sprint(dump, x1),
"\n\nparsed: ", x2, "\n", sprint(dump, x2),
"\n\nreparsed: ", x3, "\n", sprint(dump, x3),
"\n\n"))
end
@test x1 == x2 == x3
end
x4 = Base.remove_linenums!(Meta.parse(x))
x5 = eval(Base.remove_linenums!(Meta.parse(repr(x4))))
x6 = eval(Base.remove_linenums!(Meta.parse(repr(x5))))
if ! (x4 == x5 == x6)
error(string(
"\nrepr test (Rule 2) without line numbers failed:",
"\noriginal: ", x,
"\n\npreparsed: ", x4, "\n", sprint(dump, x4),
"\n\nparsed: ", x5, "\n", sprint(dump, x5),
"\n\nreparsed: ", x6, "\n", sprint(dump, x6),
"\n\n"))
end
@test x4 == x5 == x6
@test Base.remove_linenums!(x1) ==
Base.remove_linenums!(x2) ==
Base.remove_linenums!(x3) ==
x4 == x5 == x6
if isa(x1, Expr) && remove_linenums
if Base.remove_linenums!(Meta.parse(string(x1))) != x1
error(string(
"\nstring test (Rule 1) failed:",
"\noriginal: ", x,
"\n\npreparsed: ", x1, "\n", sprint(dump, x4),
"\n\nstring(preparsed): ", string(x1),
"\n\nBase.remove_linenums!(Meta.parse(string(preparsed))): ",
Base.remove_linenums!(Meta.parse(string(x1))), "\n",
sprint(dump, Base.remove_linenums!(Meta.parse(string(x1)))),
"\n\n"))
end
@test Base.remove_linenums!(Meta.parse(string(x1))) == x1
elseif isa(x1, Expr)
if Meta.parse(string(x1)) != x1
error(string(
"\nstring test (Rule 1) failed:",
"\noriginal: ", x,
"\n\npreparsed: ", x1, "\n", sprint(dump, x4),
"\n\nstring(preparsed): ", string(x1),
"\n\nMeta.parse(string(preparsed)): ",
Meta.parse(string(x1)), "\n",
sprint(dump, Meta.parse(string(x1))),
"\n\n"))
end
@test Meta.parse(string(x1)) == x1
end
end
# primitive types
@test_repr "x"
@test_repr "123"
@test_repr "\"123\""
@test_repr ":()"
@test_repr ":(x, y)"
# basic expressions
@test_repr "x + y"
@test_repr "2e"
@test_repr "2*e1"
@test_repr "2*E1"
@test_repr "2*f1"
@test_repr "0x00*a"
@test_repr "!x"
@test_repr "f(1, 2, 3)"
@test_repr "x = ~y"
@test_repr ":(:x, :y)"
@test_repr ":(:(:(x)))"
@test_repr "-\"\""
@test_repr "-(<=)"
@test_repr "\$x"
@test_repr "\$(\"x\")"
# order of operations
@test_repr "x + y * z"
@test_repr "x * y + z"
@test_repr "x * (y + z)"
@test_repr "!x^y"
@test_repr "!x^(y+z)"
@test_repr "!(x^y+z)"
@test_repr "x^-y"
@test_repr "x^-(y+z)"
@test_repr "x^-f(y+z)"
@test_repr "+(w-x)^-f(y+z)"
@test_repr "w = ((x = y) = z)" # parens aren't necessary, but not wrong
@test_repr "w = ((x, y) = z)" # parens aren't necessary, but not wrong
@test_repr "a & b && c"
@test_repr "a & (b && c)"
@test_repr "(a => b) in c"
@test_repr "a => b in c"
@test_repr "*(a..., b)"
@test_repr "+(a, b, c...)"
@test_repr "f((x...)...)"
# precedence tie resolution
@test_repr "(a * b) * (c * d)"
@test_repr "(a / b) / (c / d / e)"
@test_repr "(a == b == c) != (c == d < e)"
# Exponentiation (>= operator_precedence(:^)) and unary operators
@test_repr "(-1)^a"
@test_repr "(-2.1)^-1"
@test_repr "(-x)^a"
@test_repr "(-a)^-1"
@test_repr "(!x)↑!a"
@test_repr "(!x).a"
@test_repr "(!x)::a"
# invalid UTF-8 strings
@test_repr "\"\\ud800\""
@test_repr "\"\\udfff\""
@test_repr "\"\\xc0\\xb0\""
@test_repr "\"\\xe0\\xb0\\xb0\""
@test_repr "\"\\xf0\\xb0\\xb0\\xb0\""
# import statements
@test_repr "using A"
@test_repr "using A, B.C, D"
@test_repr "using A: b"
@test_repr "using A: a, x, y.z"
@test_repr "using A.B.C: a, x, y.z"
@test_repr "using ..A: a, x, y.z"
@test_repr "import A"
@test_repr "import A, B.C, D"
@test_repr "import A: b"
@test_repr "import A: a, x, y.z"
@test_repr "import A.B.C: a, x, y.z"
@test_repr "import ..A: a, x, y.z"
@test_repr "import A.B, C.D"
@test_repr "import A as B"
@test_repr "import A.x as y"
@test_repr "import A: x as y"
@test_repr "import A.B: x, y as z"
@test_repr "import A.B: x, y as z, a.b as c, xx"
# keyword args (issue #34023 and #32775)
@test_repr "f(a, b=c)"
@test_repr "f(a, b! = c)"
@test_repr "T{x=1}"
@test_repr "[a=1]"
@test_repr "a[x=1]"
@test_repr "f(; a=1)"
@test_repr "f(b=2; a=1)"
@test_repr "@f(1, y=3)"
@test_repr "n + (x=1)"
@test_repr "(;x=1)"
@test_repr "(x,;x=1)"
@test_repr "(a=1,;x=1)"
@test_repr "(a=1,b=2;x=1,y,:z=>2)"
@test repr(:((a,;b))) == ":((a,; b))"
@test repr(:((a=1,;x=2))) == ":((a = 1,; x = 2))"
@test repr(:((a=1,3;x=2))) == ":((a = 1, 3; x = 2))"
@test repr(:(g(a,; b))) == ":(g(a; b))"
@test repr(:(;)) == ":((;))"
@test repr(:(-(;x))) == ":(-(; x))"
@test repr(:(+(1, 2;x))) == ":(+(1, 2; x))"
@test repr(:(1:2...)) == ":(1:2...)"
@test repr(:(1 := 2)) == ":(1 := 2)"
@test repr(:(1 ≔ 2)) == ":(1 ≔ 2)"
@test repr(:(1 ⩴ 2)) == ":(1 ⩴ 2)"
@test repr(:(1 ≕ 2)) == ":(1 ≕ 2)"
@test repr(:(∓ 1)) == ":(∓1)"
@test repr(:(± 1)) == ":(±1)"
for ex in [Expr(:call, :f, Expr(:(=), :x, 1)),
Expr(:ref, :f, Expr(:(=), :x, 1)),
Expr(:vect, 1, 2, Expr(:kw, :x, 1)),
Expr(:kw, :a, :b),
Expr(:curly, :T, Expr(:kw, :x, 1)),
Expr(:call, :+, :n, Expr(:kw, :x, 1)),
:((a=1,; $(Expr(:(=), :x, 2)))),
:(($(Expr(:(=), :a, 1)),; x = 2)),
Expr(:tuple, Expr(:parameters)),
Expr(:call, :*, 0, :x01),
Expr(:call, :*, 0, :b01),
Expr(:call, :*, 0, :o01)]
@test eval(Meta.parse(repr(ex))) == ex
end
@test repr(Expr(:using, :Foo)) == ":(\$(Expr(:using, :Foo)))"
@test repr(Expr(:using, Expr(:(.), ))) == ":(\$(Expr(:using, :(\$(Expr(:.))))))"
@test repr(Expr(:import, :Foo)) == ":(\$(Expr(:import, :Foo)))"
@test repr(Expr(:import, Expr(:(.), ))) == ":(\$(Expr(:import, :(\$(Expr(:.))))))"
@test repr(Expr(:using, Expr(:(.), :A))) == ":(using A)"
@test repr(Expr(:using, Expr(:(.), :A),
Expr(:(.), :B))) == ":(using A, B)"
@test repr(Expr(:using, Expr(:(.), :A),
Expr(:(.), :B, :C),
Expr(:(.), :D))) == ":(using A, B.C, D)"
@test repr(Expr(:using, Expr(:(.), :A, :B),
Expr(:(.), :C, :D))) == ":(using A.B, C.D)"
@test repr(Expr(:import, Expr(:(.), :A))) == ":(import A)"
@test repr(Expr(:import, Expr(:(.), :A),
Expr(:(.), :B))) == ":(import A, B)"
@test repr(Expr(:import, Expr(:(.), :A),
Expr(:(.), :B, :(C)),
Expr(:(.), :D))) == ":(import A, B.C, D)"
@test repr(Expr(:import, Expr(:(.), :A, :B),
Expr(:(.), :C, :D))) == ":(import A.B, C.D)"
# https://github.com/JuliaLang/julia/issues/49168
@test repr(:(using A: (..))) == ":(using A: (..))"
@test repr(:(using A: (..) as twodots)) == ":(using A: (..) as twodots)"
# range syntax
@test_repr "1:2"
@test_repr "3:4:5"
let ex4 = Expr(:call, :(:), 1, 2, 3, 4),
ex1 = Expr(:call, :(:), 1)
@test eval(Meta.parse(repr(ex4))) == ex4
@test eval(Meta.parse(repr(ex1))) == ex1
end
# Complex
# Meta.parse(repr(:(...))) returns a double-quoted block, so we need to eval twice to unquote it
@test iszero(eval(eval(Meta.parse(repr(:($(1 + 2im) - $(1 + 2im)))))))
# control structures (shamelessly stolen from base/bitarray.jl)
@weak_test_repr """mutable struct BitArray{N} <: AbstractArray{Bool, N}
# line meta
chunks::Vector{UInt64}
# line meta
len::Int
# line meta
dims::NTuple{N,Int}
# line meta
function BitArray(undef, dims::Int...)
# line meta
if length(dims) != N
# line meta
error(\"number of dimensions must be \$N (got \$(length(dims)))\")
end
# line meta
n = 1
# line meta
for d in dims
# line meta
if d < 0
# line meta
error(\"dimension size must be non-negative (got \$d)\")
end
# line meta
n *= d
end
# line meta
nc = num_bit_chunks(n)
# line meta
chunks = Vector{UInt64}(undef, nc)
# line meta
if nc > 0
# line meta
chunks[end] = UInt64(0)
end
# line meta
b = new(chunks, n)
# line meta
if N != 1
# line meta
b.dims = dims
end
# line meta
return b
end
end"""
@weak_test_repr """function copy_chunks(dest::Vector{UInt64}, pos_d::Integer, src::Vector{UInt64}, pos_s::Integer, numbits::Integer)
# line meta
if numbits == 0
# line meta
return
end
# line meta
if dest === src && pos_d > pos_s
# line meta
return copy_chunks_rtol(dest, pos_d, pos_s, numbits)
end
# line meta
kd0, ld0 = get_chunks_id(pos_d)
# line meta
kd1, ld1 = get_chunks_id(pos_d + numbits - 1)
# line meta
ks0, ls0 = get_chunks_id(pos_s)
# line meta
ks1, ls1 = get_chunks_id(pos_s + numbits - 1)
# line meta
delta_kd = kd1 - kd0
# line meta
delta_ks = ks1 - ks0
# line meta
u = _msk64
# line meta
if delta_kd == 0
# line meta
msk_d0 = ~(u << ld0) | (u << ld1 << 1)
else
# line meta
msk_d0 = ~(u << ld0)
# line meta
msk_d1 = (u << ld1 << 1)
end
# line meta
if delta_ks == 0
# line meta
msk_s0 = (u << ls0) & ~(u << ls1 << 1)
else
# line meta
msk_s0 = (u << ls0)
end
# line meta
chunk_s0 = glue_src_bitchunks(src, ks0, ks1, msk_s0, ls0)
# line meta
dest[kd0] = (dest[kd0] & msk_d0) | ((chunk_s0 << ld0) & ~msk_d0)
# line meta
if delta_kd == 0
# line meta
return
end
# line meta
for i = 1 : kd1 - kd0 - 1
# line meta
chunk_s1 = glue_src_bitchunks(src, ks0 + i, ks1, msk_s0, ls0)
# line meta
chunk_s = (chunk_s0 >>> (63 - ld0) >>> 1) | (chunk_s1 << ld0)
# line meta
dest[kd0 + i] = chunk_s
# line meta
chunk_s0 = chunk_s1
end
# line meta
if ks1 >= ks0 + delta_kd
# line meta
chunk_s1 = glue_src_bitchunks(src, ks0 + delta_kd, ks1, msk_s0, ls0)
else
# line meta
chunk_s1 = UInt64(0)
end
# line meta
chunk_s = (chunk_s0 >>> (63 - ld0) >>> 1) | (chunk_s1 << ld0)
# line meta
dest[kd1] = (dest[kd1] & msk_d1) | (chunk_s & ~msk_d1)
# line meta
return
end"""
@weak_test_repr """if a
# line meta
b
end
"""
@weak_test_repr """if a
# line meta
b
elseif c
# line meta
d
end
"""
@weak_test_repr """if a
# line meta
b
elseif c
# line meta
d
else
# line meta
e
end
"""
@weak_test_repr """if a
# line meta
b
elseif c
# line meta
d
elseif e
# line meta
f
end
"""
@weak_test_repr """f(x, y) do z, w
# line meta
a
# line meta
b
end
"""
@weak_test_repr """f(x, y) do z
# line meta
a
# line meta
b
end
"""
# issue #7188
@test sprint(show, :foo) == ":foo"
@test sprint(show, Symbol("foo bar")) == "Symbol(\"foo bar\")"
@test sprint(show, Symbol("foo \"bar")) == "Symbol(\"foo \\\"bar\")"
@test sprint(show, :+) == ":+"
@test sprint(show, :end) == ":end"
# make sure :var"'" prints correctly
@test sprint(show, Symbol("'")) == "Symbol(\"'\")"
@test_repr "var\"'\" = 5"
# isidentifier
@test Meta.isidentifier("x")
@test Meta.isidentifier("x1")
@test !Meta.isidentifier("x.1")
@test !Meta.isidentifier("1x")
@test Meta.isidentifier(Symbol("x"))
@test Meta.isidentifier(Symbol("x1"))
@test !Meta.isidentifier(Symbol("x.1"))
@test !Meta.isidentifier(Symbol("1x"))
# issue #32408: Printing of names which are invalid identifiers
# Invalid identifiers which need `var` quoting:
@test sprint(show, Expr(:call, :foo, Symbol("##"))) == ":(foo(var\"##\"))"
@test sprint(show, Expr(:call, :foo, Symbol("a-b"))) == ":(foo(var\"a-b\"))"
@test sprint(show, :(export var"#")) == ":(export var\"#\")"
@test sprint(show, :(import A: var"#")) == ":(import A: var\"#\")"
@test sprint(show, :(macro var"#" end)) == ":(macro var\"#\" end)"
@test sprint(show, :"x$(var"#")y") == ":(\"x\$(var\"#\")y\")"
# Macro-like names outside macro calls
@test sprint(show, Expr(:call, :foo, Symbol("@bar"))) == ":(foo(var\"@bar\"))"
@test sprint(show, :(export @foo)) == ":(export @foo)"
@test sprint(show, :(import A.B: c.@d)) == ":(import A.B: c.@d)"
@test sprint(show, :(using A.@foo)) == ":(using A.@foo)"
# Hidden macro names
@test sprint(show, Expr(:macrocall, Symbol("@#"), nothing, :a)) == ":(@var\"#\" a)"
# Test that public expressions are rendered nicely
# though they are hard to create with quotes because public is not a context dependant keyword
@test sprint(show, Expr(:public, Symbol("@foo"))) == ":(public @foo)"
@test sprint(show, Expr(:public, :f,:o,:o)) == ":(public f, o, o)"
s = sprint(show, :(module A; public x; end))
@test match(r"^:\(module A\n #= .* =#\n #= .* =#\n public x\n end\)$", s) !== nothing
# PR #38418
module M1 var"#foo#"() = 2 end
@test occursin("M1.var\"#foo#\"", sprint(show, M1.var"#foo#", context = :module=>@__MODULE__))
# PR #43932
module var"#43932#" end
@test endswith(sprint(show, var"#43932#"), ".var\"#43932#\"")
# issue #12477
@test sprint(show, Union{Int64, Int32, Int16, Int8, Float64}) == "Union{Float64, Int16, Int32, Int64, Int8}"
# Function and array reference precedence
@test_repr "([2] + 3)[1]"
@test_repr "foo.bar[1]"
@test_repr "foo.bar()"
@test_repr "(foo + bar)()"
# issue #7921
@test replace(sprint(show, Expr(:function, :(==(a, b)), Expr(:block,:(return a == b)))),
r"\s+" => " ") == ":(function ==(a, b) return a == b end)"
# unicode operator printing
@test sprint(show, :(1 ⊕ (2 ⊗ 3))) == ":(1 ⊕ 2 ⊗ 3)"
@test sprint(show, :((1 ⊕ 2) ⊗ 3)) == ":((1 ⊕ 2) ⊗ 3)"
# issue #8155
@test_repr "foo(x,y; z=bar)"
@test_repr "foo(x,y,z=bar)"
@test_repr "Int[i for i=1:10]"
@test_repr "Int[(i, j) for (i, j) in zip(1:10,1:0)]"
@test_repr "[1 2 3; 4 5 6; 7 8 9]'"
@weak_test_repr "baremodule X
# line meta
# line meta
import ...B.c
# line meta
import D
# line meta
import B.C.D.E.F.g
end"
@weak_test_repr "baremodule Y
# line meta
# line meta
export A, B, C
# line meta
export D, E, F
end"
# issue #19840
@test_repr "Array{Int}(undef, 0)"
@test_repr "Array{Int}(undef, 0,0)"
@test_repr "Array{Int}(undef, 0,0,0)"
@test_repr "Array{Int}(undef, 0,1)"
@test_repr "Array{Int}(undef, 0,0,1)"
# issue #8994
@test_repr "get! => 2"
@test_repr "(<) : 2"
@test_repr "(<) :: T"
@test_repr "S{(<) <: T}"
@test_repr "+ + +"
# issue #9474
for s in ("(1::Int64 == 1::Int64)::Bool", "(1:2:3) + 4", "x = 1:2:3")
local s
@test sprint(show, Meta.parse(s)) == ":("*s*")"
end
# parametric type instantiation printing
struct TParametricPrint{a}; end
@test sprint(show, :(TParametricPrint{false}())) == ":(TParametricPrint{false}())"
# issue #9797
let q1 = Meta.parse(repr(:("$(a)b"))),
q2 = Meta.parse(repr(:("$ab")))
@test isa(q1, Expr)
@test q1.args[1].head === :string
@test q1.args[1].args == [:a, "b"]
@test isa(q2, Expr)
@test q2.args[1].head === :string
@test q2.args[1].args == [:ab,]
end
x8d003 = 2
let a = Expr(:quote,Expr(:$,:x8d003))
@test eval(Meta.parse(repr(a))) == a
@test eval(eval(Meta.parse(repr(a)))) == 2
end
# issue #11413
@test string(:(*{1, 2})) == "*{1, 2}"
@test string(:(*{1, x})) == "*{1, x}"
@test string(:(-{x})) == "-{x}"
# issue #11393
@test_repr "@m(x, y) + z"
@test_repr "(@m(x, y), z)"
@test_repr "[@m(x, y), z]"
@test_repr "A[@m(x, y), z]"
@test_repr "T{@m(x, y), z}"
@test_repr "@m x @n(y) z"
@test_repr "f(@m(x, y); z=@n(a))"
@test_repr "@m(x, y).z"
@test_repr "::@m(x, y) + z"
@test_repr "[@m(x) y z]"
@test_repr "[@m(x) y; z]"
test_repr("let @m(x), y=z; end", true)
@test repr(:(@m x y)) == ":(#= $(@__FILE__):$(@__LINE__) =# @m x y)"
@test string(:(@m x y)) == "#= $(@__FILE__):$(@__LINE__) =# @m x y"
@test string(:(@m x y;)) == "begin\n #= $(@__FILE__):$(@__LINE__) =# @m x y\nend"
# issue #11436
@test_repr "1 => 2 => 3"
@test_repr "1 => (2 => 3)"
@test_repr "(1 => 2) => 3"
# pr 12008
@test_repr "primitive type A B end"
@test_repr "primitive type B 100 end"
@test repr(:(primitive type A B end)) == ":(primitive type A B end)"
@test repr(:(primitive type B 100 end)) == ":(primitive type B 100 end)"
# `where` syntax
@test_repr "A where T<:B"
@test_repr "A where T<:(Array{T} where T<:Real)"
@test_repr "Array{T} where {S<:Real, T<:Array{S}}"
@test_repr "x::Array{T} where T"
@test_repr "(a::b) where T"
@test_repr "a::b where T"
@test_repr "X where (T=1)"
@test_repr "X where T = 1"
@test_repr "Array{<:Real}"
@test_repr "Array{>:Real}"
@test repr(Base.typename(Array)) == "typename(Array)"
let oldout = stdout, olderr = stderr
local rdout, wrout, rderr, wrerr, out, err, rd, wr, io
try
# pr 16917
rdout, wrout = redirect_stdout()
@test wrout === stdout
out = @async read(rdout, String)
rderr, wrerr = redirect_stderr()
@test wrerr === stderr
err = @async read(rderr, String)
@test dump(Int64) === nothing
if !Sys.iswindows()
close(wrout)
close(wrerr)
end
for io in (Core.stdout, Core.stderr)
Core.println(io, "TESTA")
println(io, "TESTB")
print(io, 'Α', 1)
Core.print(io, 'Β', 2)
Core.show(io, "A")
println(io)
end
Core.println("A")
Core.print("1", 2, 3.0)
Core.show("C")
Core.println()
redirect_stdout(oldout)
redirect_stderr(olderr)
close(wrout)
close(wrerr)
@test fetch(out) == "primitive type Int64 <: Signed\nTESTA\nTESTB\nΑ1Β2\"A\"\nA\n123\"C\"\n"
@test fetch(err) == "TESTA\nTESTB\nΑ1Β2\"A\"\n"
finally
redirect_stdout(oldout)
redirect_stderr(olderr)
end
end
let filename = tempname()
ret = open(filename, "w") do f
redirect_stdout(f) do
println("hello")
[1,3]
end
end
@test ret == [1,3]
@test chomp(read(filename, String)) == "hello"
ret = open(filename, "w") do f
redirect_stderr(f) do
println(stderr, "WARNING: hello")
[2]
end
end
@test ret == [2]
# stdin is unavailable on the workers. Run test on master.
@test occursin("WARNING: hello", read(filename, String))
ret = Core.eval(Main, quote
remotecall_fetch(1, $filename) do fname
open(fname) do f
redirect_stdin(f) do
readline()
end
end
end
end)
@test occursin("WARNING: hello", ret)
rm(filename)
end
# issue #13127
function f13127()
buf = IOBuffer()
f() = 1
show(buf, f)
String(take!(buf))
end
@test startswith(f13127(), "$(@__MODULE__).var\"#f")
@test startswith(sprint(show, typeof(x->x), context = :module=>@__MODULE__), "var\"")
# PR 53719
module M53719
f = x -> x + 1
function foo(x)
function bar(y)
function baz(z)
return x + y + z
end
return baz
end
return bar
end
function foo2(x)
function bar2(y)
return z -> x + y + z
end
return bar2
end
lambda1 = (x)->begin
function foo(y)
return x + y
end
return foo
end
lambda2 = (x)->begin
y -> x + y
end
end
@testset "PR 53719 function names" begin
# M53719.f should be printed as var"#[0-9]+"
@test occursin(r"var\"#[0-9]+", sprint(show, M53719.f, context = :module=>M53719))
# M53719.foo(1) should be printed as var"#bar"
@test occursin(r"var\"#bar", sprint(show, M53719.foo(1), context = :module=>M53719))
# M53719.foo(1)(2) should be printed as var"#baz"
@test occursin(r"var\"#baz", sprint(show, M53719.foo(1)(2), context = :module=>M53719))
# M53719.foo2(1) should be printed as var"#bar2"
@test occursin(r"var\"#bar2", sprint(show, M53719.foo2(1), context = :module=>M53719))
# M53719.foo2(1)(2) should be printed as var"#foo2##[0-9]+"
@test occursin(r"var\"#foo2##[0-9]+", sprint(show, M53719.foo2(1)(2), context = :module=>M53719))
# M53719.lambda1(1) should be printed as var"#foo"
@test occursin(r"var\"#foo", sprint(show, M53719.lambda1(1), context = :module=>M53719))
# M53719.lambda2(1) should be printed as var"#[0-9]+"
@test occursin(r"var\"#[0-9]+", sprint(show, M53719.lambda2(1), context = :module=>M53719))
end
@testset "PR 53719 function types" begin
# typeof(M53719.f) should be printed as var"#[0-9]+#[0-9]+"
@test occursin(r"var\"#[0-9]+#[0-9]+", sprint(show, typeof(M53719.f), context = :module=>M53719))
#typeof(M53719.foo(1)) should be printed as var"#bar#foo##[0-9]+"
@test occursin(r"var\"#bar#foo##[0-9]+", sprint(show, typeof(M53719.foo(1)), context = :module=>M53719))
#typeof(M53719.foo(1)(2)) should be printed as var"#baz#foo##[0-9]+"
@test occursin(r"var\"#baz#foo##[0-9]+", sprint(show, typeof(M53719.foo(1)(2)), context = :module=>M53719))
#typeof(M53719.foo2(1)) should be printed as var"#bar2#foo2##[0-9]+"
@test occursin(r"var\"#bar2#foo2##[0-9]+", sprint(show, typeof(M53719.foo2(1)), context = :module=>M53719))
#typeof(M53719.foo2(1)(2)) should be printed as var"#foo2##[0-9]+#foo2##[0-9]+"
@test occursin(r"var\"#foo2##[0-9]+#foo2##[0-9]+", sprint(show, typeof(M53719.foo2(1)(2)), context = :module=>M53719))
#typeof(M53719.lambda1(1)) should be printed as var"#foo#[0-9]+"
@test occursin(r"var\"#foo#[0-9]+", sprint(show, typeof(M53719.lambda1(1)), context = :module=>M53719))
#typeof(M53719.lambda2(1)) should be printed as var"#[0-9]+#[0-9]+"
@test occursin(r"var\"#[0-9]+#[0-9]+", sprint(show, typeof(M53719.lambda2(1)), context = :module=>M53719))
end
#test methodshow.jl functions
@test Base.inbase(Base)
@test !Base.inbase(LinearAlgebra)
@test !Base.inbase(Core)
let repr = sprint(show, "text/plain", methods(Base.inbase))
@test occursin("inbase(m::Module)", repr)
end
let repr = sprint(show, "text/html", methods(Base.inbase))
@test occursin("inbase(m::<b>Module</b>)", repr)
end
f5971(x, y...; z=1, w...) = nothing
let repr = sprint(show, "text/plain", methods(f5971))
@test occursin("f5971(x, y...; z, w...)", repr)
end
let repr = sprint(show, "text/html", methods(f5971))
@test occursin("f5971(x, y...; <i>z, w...</i>)", repr)
end
f16580(x, y...; z=1, w=y+x, q...) = nothing
let repr = sprint(show, "text/html", methods(f16580))
@test occursin("f16580(x, y...; <i>z, w, q...</i>)", repr)
end
# Just check it doesn't error
f46594(::Vararg{T, 2}) where T = 1
let repr = sprint(show, "text/html", first(methods(f46594)))
@test occursin("f46594(::Vararg{T, 2}) where T", replace(repr, r"</?[A-Za-z]>"=>""))
end
function triangular_methodshow(x::T1, y::T2) where {T2<:Integer, T1<:T2}
end
let repr = sprint(show, "text/plain", methods(triangular_methodshow))
@test occursin("where {T2<:Integer, T1<:T2}", repr)
end
struct S45879{P} end
let ms = methods(S45879)
@test ms isa Base.MethodList
@test length(ms) == 0
@test sprint(show, Base.MethodList(Method[], typeof(S45879).name.mt)) isa String
end
function f49475(a=12.0; b) end
let ms = methods(f49475)
@test length(ms) == 2
repr1 = sprint(show, "text/plain", ms[1])
repr2 = sprint(show, "text/plain", ms[2])
@test occursin("f49475(; ...)", repr1) || occursin("f49475(; ...)", repr2)
end
if isempty(Base.GIT_VERSION_INFO.commit)
@test occursin("https://github.com/JuliaLang/julia/tree/v$VERSION/base/special/trig.jl#L", Base.url(which(sin, (Float64,))))
else
@test occursin("https://github.com/JuliaLang/julia/tree/$(Base.GIT_VERSION_INFO.commit)/base/special/trig.jl#L", Base.url(which(sin, (Float64,))))
end
# Method location correction (Revise integration)
dummyloc(m::Method) = :nofile, Int32(123456789)
Base.methodloc_callback[] = dummyloc
let repr = sprint(show, "text/plain", methods(Base.inbase))
@test occursin("nofile:123456789", repr)
end
let repr = sprint(show, "text/html", methods(Base.inbase))
@test occursin("nofile:123456789", repr)
end
Base.methodloc_callback[] = nothing
@testset "matrix printing" begin
# print_matrix should be able to handle small and large objects easily, test by
# calling show. This also indirectly tests print_matrix_row, which
# is used repeatedly by print_matrix.
# This fits on screen:
@test replstr(Matrix(1.0I, 10, 10)) == "10×10 Matrix{Float64}:\n 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0\n 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0\n 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0\n 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0\n 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0\n 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0\n 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0"
# an array too long vertically to fit on screen, and too long horizontally:
@test replstr(Vector(1.:100.)) == "100-element Vector{Float64}:\n 1.0\n 2.0\n 3.0\n 4.0\n 5.0\n 6.0\n 7.0\n 8.0\n 9.0\n 10.0\n ⋮\n 92.0\n 93.0\n 94.0\n 95.0\n 96.0\n 97.0\n 98.0\n 99.0\n 100.0"
@test occursin(r"1×100 adjoint\(::Vector{Float64}\) with eltype Float64:\n 1.0 2.0 3.0 4.0 5.0 6.0 7.0 … 95.0 96.0 97.0 98.0 99.0 100.0", replstr(Vector(1.:100.)'))
# too big in both directions to fit on screen:
@test replstr((1.:100.)*(1:100)') == "100×100 Matrix{Float64}:\n 1.0 2.0 3.0 4.0 5.0 6.0 … 97.0 98.0 99.0 100.0\n 2.0 4.0 6.0 8.0 10.0 12.0 194.0 196.0 198.0 200.0\n 3.0 6.0 9.0 12.0 15.0 18.0 291.0 294.0 297.0 300.0\n 4.0 8.0 12.0 16.0 20.0 24.0 388.0 392.0 396.0 400.0\n 5.0 10.0 15.0 20.0 25.0 30.0 485.0 490.0 495.0 500.0\n 6.0 12.0 18.0 24.0 30.0 36.0 … 582.0 588.0 594.0 600.0\n 7.0 14.0 21.0 28.0 35.0 42.0 679.0 686.0 693.0 700.0\n 8.0 16.0 24.0 32.0 40.0 48.0 776.0 784.0 792.0 800.0\n 9.0 18.0 27.0 36.0 45.0 54.0 873.0 882.0 891.0 900.0\n 10.0 20.0 30.0 40.0 50.0 60.0 970.0 980.0 990.0 1000.0\n ⋮ ⋮ ⋱ \n 92.0 184.0 276.0 368.0 460.0 552.0 8924.0 9016.0 9108.0 9200.0\n 93.0 186.0 279.0 372.0 465.0 558.0 9021.0 9114.0 9207.0 9300.0\n 94.0 188.0 282.0 376.0 470.0 564.0 9118.0 9212.0 9306.0 9400.0\n 95.0 190.0 285.0 380.0 475.0 570.0 9215.0 9310.0 9405.0 9500.0\n 96.0 192.0 288.0 384.0 480.0 576.0 … 9312.0 9408.0 9504.0 9600.0\n 97.0 194.0 291.0 388.0 485.0 582.0 9409.0 9506.0 9603.0 9700.0\n 98.0 196.0 294.0 392.0 490.0 588.0 9506.0 9604.0 9702.0 9800.0\n 99.0 198.0 297.0 396.0 495.0 594.0 9603.0 9702.0 9801.0 9900.0\n 100.0 200.0 300.0 400.0 500.0 600.0 9700.0 9800.0 9900.0 10000.0"
# test that no spurious visual lines are added when one element spans multiple lines
v = fill!(Array{Any}(undef, 9), 0)
v[1] = "look I'm wide! --- " ^ 9
r = replstr(v)
@test startswith(r, "9-element Vector{Any}:\n \"look I'm wide! ---")
@test endswith(r, "look I'm wide! --- \"\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0")
# test vertical/diagonal ellipsis
v = fill!(Array{Any}(undef, 50), 0)
v[1] = "look I'm wide! --- " ^ 9
r = replstr(v)
@test startswith(r, "50-element Vector{Any}:\n \"look I'm wide! ---")
@test endswith(r, "look I'm wide! --- \"\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n ⋮\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0")
r = replstr([fill(0, 50) v])
@test startswith(r, "50×2 Matrix{Any}:\n 0 … \"look I'm wide! ---")
@test endswith(r, "look I'm wide! --- \"\n 0 0\n 0 0\n 0 0\n 0 0\n 0 … 0\n 0 0\n 0 0\n 0 0\n 0 0\n ⋮ ⋱ \n 0 0\n 0 0\n 0 0\n 0 0\n 0 … 0\n 0 0\n 0 0\n 0 0\n 0 0")
# issue #34659
@test replstr(Int32[]) == "Int32[]"
@test replstr([Int32[]]) == "1-element Vector{Vector{Int32}}:\n []"
@test replstr(permutedims([Int32[],Int32[]])) == "1×2 Matrix{Vector{Int32}}:\n [] []"
@test replstr(permutedims([Dict(),Dict()])) == "1×2 Matrix{Dict{Any, Any}}:\n Dict() Dict()"
@test replstr(permutedims([undef,undef])) == "1×2 Matrix{UndefInitializer}:\n UndefInitializer() UndefInitializer()"
@test replstr([zeros(3,0),zeros(2,0)]) == "2-element Vector{Matrix{Float64}}:\n 3×0 Matrix{Float64}\n 2×0 Matrix{Float64}"
end
# string show with elision
@testset "string show with elision" begin
@testset "elision logic" begin
strs = ["A", "∀", "∀A", "A∀", "😃", "x̂"]
for limit = 0:100, len = 0:100, str in strs
str = str^len
str = str[1:nextind(str, 0, len)]
out = sprint() do io
show(io, MIME"text/plain"(), str; limit)
end
lower = textwidth("\"\" ⋯ $(ncodeunits(str)) bytes ⋯ \"\"")
limit = max(limit, lower)
if textwidth(str) + 2 ≤ limit+1 && !contains(out, '⋯')
@test eval(Meta.parse(out)) == str
else
@test limit-2 <= textwidth(out) <= limit
re = r"(\"[^\"]*\") ⋯ (\d+) bytes ⋯ (\"[^\"]*\")"
m = match(re, out)
head = eval(Meta.parse(m.captures[1]))
tail = eval(Meta.parse(m.captures[3]))
skip = parse(Int, m.captures[2])
@test startswith(str, head)
@test endswith(str, tail)
@test ncodeunits(str) ==
ncodeunits(head) + skip + ncodeunits(tail)
end
end
end
@testset "default elision limit" begin
r = replstr("x"^1000)
@test length(r) == 7*80-1
@test r == repr("x"^270) * " ⋯ 460 bytes ⋯ " * repr("x"^270)
r = replstr(["x"^1000])
@test length(r) < 120
@test r == "1-element Vector{String}:\n " * repr("x"^30) * " ⋯ 940 bytes ⋯ " * repr("x"^30)
end
end
# Issue 14121
@test_repr "(A'x)'"
# issue #14481
@test_repr "in(1,2,3)"
@test_repr "<(1,2,3)"
@test_repr "+(1,2,3)"
@test_repr "-(1,2,3)"
@test_repr "*(1,2,3)"
# issue #15309
let ex,
l1 = Expr(:line, 42),
l2 = Expr(:line, 42, :myfile),
l2n = LineNumberNode(42)
@test string(l2n) == "#= line 42 =#"
@test string(l2) == "#= myfile:42 =#"
@test string(l1) == string(l2n)
ex = Expr(:block, l1, :x, l2, :y, l2n, :z)
@test replace(string(ex)," " => "") == replace("""
begin
#= line 42 =#
x
#= myfile:42 =#
y
#= line 42 =#
z
end""", " " => "")
end
# Test the printing of whatever form of line number representation
# that is used in the arguments to a macro looks the same as for
# regular quoting