Skip to content

Commit 001c0b5

Browse files
committed
simplify tests
1 parent 044a163 commit 001c0b5

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

src/ImageInTerminal.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,11 @@ include("display.jl")
9898
"""
9999
imshow([stream], img, [maxsize])
100100
101-
Displays the given image `img` using unicode characters and
102-
terminal colors (defaults to 256 colors).
101+
Displays the given image `img` using unicode characters and terminal colors (defaults to 256 colors).
103102
`img` has to be an array of `Colorant`.
104103
105-
If working in the REPL, the function tries to choose the encoding
106-
based on the current display size. The image will also be
107-
downsampled to fit into the display.
104+
If working in the REPL, the function tries to choose the encoding based on the current display size.
105+
The image will also be downsampled to fit into the display.
108106
109107
Supported encoding:
110108
- sixel (`Sixel` backend)

src/display.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Base.displayable(::TerminalGraphicDisplay, ::MIME"image/png") = true
88

99
function Base.display(d::TerminalGraphicDisplay, ::MIME"image/png", bytes::Vector{UInt8})
1010
# In this case, assume it to be png byte sequences, use FileIO to find a decoder for it.
11-
img = FileIO.load(FileIO.Stream{format"PNG"}(IOBuffer(bytes)))
11+
img = FileIO.load(FileIO.Stream{format"PNG"}(PipeBuffer(bytes)))
1212
display(d, MIME("image/png"), img)
1313
end
1414

test/tst_baseshow.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ for mode in (24, 8)
5555
end
5656
end
5757

58-
set_colormode(8) # paranoid
58+
set_colormode(8) # reset to default state

test/tst_display.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
@testset "display" begin
22
# `latex_fraction.png` was generated using:
33
# $ julia -e 'using Laxtexify; render(latexify(:(x / y)), MIME("image/png"), name=abspath("latex_fraction"), callshow=false)'
4-
img = FileIO.load("latex_fraction.png")
5-
dsp = ImageInTerminal.TerminalGraphicDisplay(stdout)
4+
fn = joinpath(@__DIR__, "latex_fraction.png")
5+
6+
img = FileIO.load(fn)
7+
@test prod(size(img)) > 1_000
8+
9+
io = PipeBuffer()
10+
dsp = ImageInTerminal.TerminalGraphicDisplay(io)
611
display(dsp, MIME("image/png"), img)
12+
@test length(read(io, String)) > 5_000
13+
14+
bytes = read(fn)
15+
dsp = ImageInTerminal.TerminalGraphicDisplay(io)
16+
display(dsp, MIME("image/png"), bytes)
17+
@test length(read(io, String)) > 5_000
718
end

test/tst_imshow.jl

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,39 @@ import OffsetArrays: OffsetArray
22
import SparseArrays: sprand
33
import Rotations: RotMatrix
44

5+
# workaround for github.com/JuliaLang/julia/issues/12711
6+
function _redirect_stdout(f::Function, io::IO)
7+
old_stdout = stdout
8+
rd, = redirect_stdout()
9+
task = @async write(io, rd)
10+
try
11+
ret = f()
12+
Libc.flush_cstdio()
13+
flush(stdout)
14+
return ret
15+
finally
16+
close(rd)
17+
redirect_stdout(old_stdout)
18+
wait(task)
19+
end
20+
end
21+
522
@testset "STDOUT" begin
623
# make sure it compiles and executes
724
for mode in (8, 24)
825
set_colormode(mode)
9-
# 2D - Matrix
10-
imshow(colorview(RGB, rand(3, 2, 3)))
11-
println()
12-
imshow(colorview(RGB, rand(3, 2, 3)), (2, 3))
13-
println()
14-
# 3D
15-
imshow(colorview(RGB, rand(3, 3, 4, 2)))
16-
println()
26+
io = PipeBuffer()
27+
_redirect_stdout(io) do
28+
# 2D - Matrix
29+
@ensurecolor imshow(colorview(RGB, rand(3, 2, 3)))
30+
println()
31+
@ensurecolor imshow(colorview(RGB, rand(3, 2, 3)), (2, 3))
32+
println()
33+
# 3D
34+
@ensurecolor imshow(colorview(RGB, rand(3, 3, 4, 2)))
35+
println()
36+
end
37+
@test length(read(io, String)) > 500
1738
end
1839
end
1940

@@ -71,4 +92,4 @@ end
7192
end
7293
end
7394

74-
set_colormode(8)
95+
set_colormode(8) # reset to default state

0 commit comments

Comments
 (0)