|
310 | 310 | @test AbstractAlgebra.obj_to_string_wrt_times(x + y) == "(x + y)"
|
311 | 311 | end
|
312 | 312 |
|
| 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 | + |
313 | 417 | let
|
314 | 418 | io = IOBuffer()
|
315 | 419 | io = AbstractAlgebra.pretty(io, force_newlines = true)
|
|
0 commit comments