Skip to content

Commit 7ad5b95

Browse files
authored
Merge pull request #77 from JuliaTesting/ox/plot
Support anything that can save as PNG
2 parents 6d3ed58 + 96a6d45 commit 7ad5b95

File tree

6 files changed

+27
-5
lines changed

6 files changed

+27
-5
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ImageCore = "0.8.1"
2323
ImageInTerminal = "0.3, 0.4"
2424
ImageMagick = "0.7, 1"
2525
ImageTransformations = "0.8"
26+
Plots = "1.4.3"
2627
TestImages = "0.6, 1"
2728
julia = "1"
2829

@@ -31,7 +32,8 @@ CSVFiles = "5d742f6a-9f54-50ce-8119-2520741973ca"
3132
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
3233
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
3334
ImageTransformations = "02fcd773-0e25-5acc-982a-7f6622650795"
35+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
3436
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
3537

3638
[targets]
37-
test = ["CSVFiles", "DataFrames", "ImageMagick", "ImageTransformations", "TestImages"]
39+
test = ["CSVFiles", "DataFrames", "ImageMagick", "ImageTransformations", "Plots", "TestImages"]

src/fileio.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ function _convert(
6464
return join(strs,'\n')
6565
end
6666

67+
# PNG (Including images as arrays of colorants, and things like Plots.jl plots etc)
68+
function _convert(::Type{<:DataFormat{:PNG}}, data)::AbstractArray{<:Colorant}
69+
mktempdir() do dir
70+
filename = File{DataFormat{:PNG}}(joinpath(dir, "inconversion.png"))
71+
savefile(filename, data)
72+
load(filename)
73+
end
74+
end
75+
_convert(::Type{<:DataFormat{:PNG}}, img::AbstractArray{<:Colorant}; kw...) = img
76+
6777
# SHA256
6878
_convert(::Type{DataFormat{:SHA256}}, x; kw...) = bytes2hex(sha256(string(x)))
6979
function _convert(::Type{DataFormat{:SHA256}}, img::AbstractArray{<:Colorant}; kw...)

src/test_reference.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,20 @@ function test_reference(
100100
path = file.filename
101101
dir, filename = splitdir(path)
102102

103+
actual = _convert(F, raw_actual; kw...)
104+
103105
# infer the default rendermode here
104106
# since `nothing` is always passed to this method from
105107
# test_reference(filename::AbstractString, raw_actual; kw...)
106108
if rendermode === nothing
107-
rendermode = default_rendermode(F, raw_actual)
109+
rendermode = default_rendermode(F, actual)
108110
end
109111

110-
actual = _convert(F, raw_actual; kw...)
111112
# preprocessing when reference file doesn't exists
112113
if !isfile(path)
113114
@info("Reference file for \"$filename\" does not exist. It will be created")
114115
# TODO: move encoding out from render
115-
render(rendermode, raw_actual)
116+
render(rendermode, actual)
116117

117118
mkpath(dir)
118119
savefile(file, actual)
@@ -122,7 +123,7 @@ function test_reference(
122123
end
123124

124125
# file exists
125-
reference = loadfile(T, file)
126+
reference = loadfile(typeof(actual), file)
126127

127128
if equiv === nothing
128129
# generally, `reference` and `actual` are of the same type after preprocessing

test/references/heatmap.png

12.6 KB
Loading

test/references/scatter.png

10.8 KB
Loading

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Test
22
using ImageInTerminal, TestImages, ImageCore, ImageTransformations
3+
using Plots
34
using Random
45

56
if isinteractive()
@@ -120,6 +121,14 @@ end
120121
@test_throws Exception @test_reference "references/camera.png" camera # unequal size
121122
end
122123

124+
@testset "Plots as PNG images" begin
125+
# Test disabled on linux because: https://github.com/JuliaPlots/Plots.jl/issues/2127
126+
if !Sys.islinux()
127+
@test_reference "references/heatmap.png" heatmap([1 0; 0 1]) by=psnr_equality(15)
128+
@test_reference "references/scatter.png" scatter([(0,0),(1,0),(0,1),(1,1)], ms=8)
129+
end
130+
end
131+
123132
using DataFrames, CSVFiles
124133
@testset "DataFrame as CSV" begin
125134
@test_reference "references/dataframe.csv" DataFrame(v1=[1,2,3], v2=["a","b","c"])

0 commit comments

Comments
 (0)