Skip to content

Commit 58c0b47

Browse files
authored
Fix PrettyPrinting corner case, add more tests (#1415)
Add some examples from the OSCAR dev docs to make sure they match reality. Also fix a bug where in some situations the pretty printing feature caused a trailing 0 to be printed (e.g. when ending printing with a `Dedent()`), as in this example: julia> show(stdout, MIME"text/plain"(), F) Number field with defining polynomial x^2 + 3 over rational field0 I tried to add a test case for this, but the issue only happens with `stdout`, not with an `IOBuffer`
1 parent c95f17e commit 58c0b47

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

src/PrettyPrinting.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1533,13 +1533,13 @@ unlock(io::IOCustom) = unlock(io.io)
15331533

15341534
Base.displaysize(io::IOCustom) = displaysize(io.io)
15351535

1536-
write(io::IO, ::Indent) = (_unwrap(io).indent_level += 1; 0)
1536+
write(io::IO, ::Indent) = (_unwrap(io).indent_level += 1; nothing)
15371537
print(io::IO, ::Indent) = write(io, Indent())
1538-
write(io::IO, ::Dedent) = (_unwrap(io).indent_level = max(0, io.indent_level - 1); 0)
1538+
write(io::IO, ::Dedent) = (_unwrap(io).indent_level = max(0, io.indent_level - 1); nothing)
15391539
print(io::IO, ::Dedent) = write(io, Dedent())
1540-
write(io::IO, ::Lowercase) = (_unwrap(io).lowercasefirst = true; 0)
1540+
write(io::IO, ::Lowercase) = (_unwrap(io).lowercasefirst = true; nothing)
15411541
print(io::IO, ::Lowercase) = write(io, Lowercase())
1542-
write(io::IO, ::LowercaseOff) = (_unwrap(io).lowercasefirst = false; 0)
1542+
write(io::IO, ::LowercaseOff) = (_unwrap(io).lowercasefirst = false; nothing)
15431543
print(io::IO, ::LowercaseOff) = write(io, LowercaseOff())
15441544

15451545
write_indent(io::IO) = write(_unwrap(io).io, io.indent_str^io.indent_level)

test/PrettyPrinting-test.jl

+104
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,110 @@
310310
@test AbstractAlgebra.obj_to_string_wrt_times(x + y) == "(x + y)"
311311
end
312312

313+
# Test various examples from the Oscar manual
314+
@testset "PrettyPrinting examples" begin
315+
316+
function detailed(x)
317+
io = IOBuffer()
318+
show(io, MIME"text/plain"(), x)
319+
return String(take!(io))
320+
end
321+
322+
function oneline(x)
323+
io = IOBuffer()
324+
print(io, x)
325+
return String(take!(io))
326+
end
327+
328+
function supercompact(x)
329+
io = IOBuffer()
330+
print(IOContext(io, :supercompact => true), x)
331+
return String(take!(io))
332+
end
333+
334+
#
335+
#
336+
#
337+
struct NewRing
338+
base_ring
339+
end
340+
341+
base_ring(R::NewRing) = R.base_ring
342+
343+
function Base.show(io::IO, ::MIME"text/plain", R::NewRing)
344+
println(io, "I am a new ring") # at least one new line is needed
345+
println(io, "I print with newlines")
346+
print(io, base_ring(R)) # the last print statement must not add a new line
347+
end
348+
349+
function Base.show(io::IO, R::NewRing)
350+
if get(io, :supercompact, false)
351+
# no nested printing
352+
print(io, "supercompact printing of newring ")
353+
else
354+
# nested printing allowed, preferably supercompact
355+
print(io, "one line printing of newring with ")
356+
print(IOContext(io, :supercompact => true), "supercompact ", base_ring(R))
357+
end
358+
end
359+
360+
R = NewRing(QQ)
361+
@test detailed(R) ==
362+
"""I am a new ring
363+
I print with newlines
364+
Rationals"""
365+
@test oneline(R) == "one line printing of newring with supercompact Rationals"
366+
@test supercompact(R) == "supercompact printing of newring "
367+
368+
#
369+
#
370+
#
371+
struct A{T}
372+
x::T
373+
end
374+
375+
function Base.show(io::IO, a::A)
376+
io = AbstractAlgebra.pretty(io)
377+
println(io, "Something of type A")
378+
print(io, AbstractAlgebra.Indent(), "over ", AbstractAlgebra.Lowercase(), a.x)
379+
print(io, AbstractAlgebra.Dedent()) # don't forget to undo the indentation!
380+
end
381+
382+
struct B
383+
end
384+
385+
function Base.show(io::IO, b::B)
386+
io = AbstractAlgebra.pretty(io)
387+
print(io, AbstractAlgebra.LowercaseOff(), "Hilbert thing")
388+
end
389+
390+
x = A(2)
391+
y = """
392+
Something of type A
393+
over 2"""
394+
@test detailed(x) == y
395+
@test oneline(x) == y
396+
@test supercompact(x) == y
397+
398+
x = A(A(2))
399+
y = """
400+
Something of type A
401+
over something of type A
402+
over 2"""
403+
@test detailed(x) == y
404+
@test oneline(x) == y
405+
@test supercompact(x) == y
406+
407+
x = A(B())
408+
y = """
409+
Something of type A
410+
over Hilbert thing"""
411+
@test detailed(x) == y
412+
@test oneline(x) == y
413+
@test supercompact(x) == y
414+
415+
end
416+
313417
let
314418
io = IOBuffer()
315419
io = AbstractAlgebra.pretty(io, force_newlines = true)

0 commit comments

Comments
 (0)