Skip to content

Add @test_reference_plot #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
ImageInTerminal = "d8c32880-2388-543b-8c61-d9f865259254"
PNGFiles = "f57f5aa1-a3ce-4bc8-8ab9-96f992907883"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand All @@ -23,6 +24,7 @@ ImageCore = "0.8.1"
ImageInTerminal = "0.3, 0.4"
ImageMagick = "0.7, 1"
ImageTransformations = "0.8"
PNGFiles = "0.3"
TestImages = "0.6, 1"
julia = "1"

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

[targets]
test = ["CSVFiles", "DataFrames", "ImageMagick", "ImageTransformations", "TestImages"]
test = ["CSVFiles", "DataFrames", "ImageMagick", "ImageTransformations", "Plots", "TestImages"]
3 changes: 3 additions & 0 deletions src/ReferenceTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using Test
using ImageCore
using Distances
using FileIO
using PNGFiles
using ImageInTerminal
using SHA
using DeepDiffs
Expand All @@ -13,10 +14,12 @@ export
@withcolor,
@io2str,
@test_reference,
@test_reference_plot,
psnr_equality

include("utils.jl")
include("test_reference.jl")
include("test_reference_plot.jl")
include("fileio.jl")
include("equality_metrics.jl")
include("render.jl")
Expand Down
23 changes: 23 additions & 0 deletions src/test_reference_plot.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
@test_reference_plot filename plot [by] [kw...]

Tests that the `plot` with reference `filename` using
equality test strategy `by`. The macro simply converts
the plot object to a PNG image and forwards it to the
[`@test_reference`](@ref) macro.
"""
macro test_reference_plot(reference, actual, kws...)
ref = esc(reference)
plt = esc(actual)
opt = esc(kws)
:(@test_reference $ref asimage($plt))
end

# helper function to convert a Plots.jl plot
# into an image for visual comparison
function asimage(plt)
io = IOBuffer()
show(io, "image/png", plt)
seekstart(io)
PNGFiles.load(io)
end
Binary file added test/references/heatmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/references/scatter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 14 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using Test
using ImageInTerminal, TestImages, ImageCore, ImageTransformations
using Random
using Test, Random
using TestImages
using ImageCore
using ImageInTerminal
using ImageTransformations
using Plots

# workaround GR warnings
ENV["GKSwstype"] = "100"

if isinteractive()
@info ("In interactive use, one should respond \"n\" when the program"
Expand Down Expand Up @@ -150,4 +156,9 @@ end
rm(newfilename, force=true)
end

@testset "Plots as PNG images" begin
@test_reference_plot "references/heatmap.png" heatmap([1 0; 0 1])
@test_reference_plot "references/scatter.png" scatter([(0,0),(1,0),(0,1),(1,1)],ms=8)
end

end # top level testset