diff --git a/Project.toml b/Project.toml index dd3a245..493b242 100644 --- a/Project.toml +++ b/Project.toml @@ -14,28 +14,31 @@ SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] +BSON = "0.3" CSVFiles = "1" DataFrames = "0.21, 0.22" DeepDiffs = "1.1" Distances = "0.7, 0.8, 0.9, 0.10" FileIO = "1" +GR = "= 0.50.1" ImageCore = "0.8.1, 0.9" ImageInTerminal = "0.3, 0.4" ImageMagick = "0.7, 1" ImageTransformations = "0.8" -GR = "= 0.50.1" Plots = "= 1.4.3" TestImages = "0.6, 1" julia = "1" [extras] +BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0" CSVFiles = "5d742f6a-9f54-50ce-8119-2520741973ca" DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71" +ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19" 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", "GR", "Plots", "TestImages"] +test = ["CSVFiles", "DataFrames", "ImageMagick", "ImageTransformations", "GR", "Plots", "TestImages", "ImageIO", "BSON"] diff --git a/README.md b/README.md index 8d8e8ee..d60099e 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ to the value of interest. ```julia using ReferenceTests -@test_reference "stringtest1.txt" string(collect(1:20)) +@test_reference "stringtest1.txt" collect(1:20) ``` If you put the above code into your `test/runtests.jl` and diff --git a/src/test_reference.jl b/src/test_reference.jl index 059c459..a581ae9 100644 --- a/src/test_reference.jl +++ b/src/test_reference.jl @@ -7,7 +7,7 @@ equality test strategy `by`. The pipeline of `test_reference` is: 1. preprocess `expr` -2. read and preprocess `filename` +2. read and preprocess `filename` via FileIO.jl 3. compare the results using `by` 4. if test fails in an interactive session (e.g, `include(test/runtests.jl)`), an interactive dialog will be trigered. @@ -26,8 +26,15 @@ compared. The contents is treated as: * Images when `expr` is an image type, i.e., `AbstractArray{<:Colorant}`; * SHA256 when `filename` endswith `*.sha256`; +* Any file-type which FileIO.jl handles and with the proper backend loaded; * Text as a fallback. +## Any FileIO.jl handled filetype + +Any file-types which can be read by [FileIO.jl](https://github.com/JuliaIO/FileIO.jl) can be used. +Note that most Julia values can be stored using packages such as, e.g., +[BSON.jl](https://github.com/JuliaIO/BSON.jl) or [JLD](https://github.com/JuliaIO/JLD.jl) and can thus be used in reference-tests. + ## Images Images are compared _approximately_ using a different `by` to ignore most encoding @@ -50,13 +57,27 @@ The hash of the `expr` and content of `filename` are compared. ## Fallback -Simply test the equality of `expr` and the contents of `filename` without any -preprocessing. +Simply test the equality of `expr` and the contents of `filename` without any preprocessing. +Note that reading `filename` will return a `String`. # Examples ```julia +# compare text-file against string representation of a value +@test_reference "stringtest1.txt" collect(1:20) + +# test number with absolute tolerance 10 +@test_reference "references/string3.txt" 1338 by=(ref, x)->isapprox(ref, x; atol=10) + +# store a floating point array ar in BSON-file and compare it back. +# Note that a Dict is needed and the custom comparison function. +using BSON +comp(d1, d2) = keys(d1)==keys(d2) && + all([ v1≈v2 for (v1,v2) in zip(values(d1), values(d2))]) +@test_reference "reftest-files/X.bson" Dict(:ar=>[1, pi, 4.5]) by=comp + # store as string using ImageInTerminal with encoding size (5,10) +using TestImages @test_reference "camera.txt" testimage("cameraman") size=(5,10) # using folders in the relative path is allowed @@ -66,11 +87,8 @@ preprocessing. # can only check for equality (no tolerance possible) @test_reference "references/camera.sha256" testimage("cameraman") -# test images with custom psnr threshold +# test images with custom Peak Signal-to-Noise Ratio (psnr) threshold @test_reference "references/camera.png" testimage("cameraman") by=psnr_equality(20) - -# test number with absolute tolerance 10 -@test_reference "references/string3.txt" 1338 by=(ref, x)->isapprox(ref, x; atol=10) ``` """ macro test_reference(reference, actual, kws...) diff --git a/test/references/array_any.bson b/test/references/array_any.bson new file mode 100644 index 0000000..4a63950 Binary files /dev/null and b/test/references/array_any.bson differ diff --git a/test/references/array_float.bson b/test/references/array_float.bson new file mode 100644 index 0000000..dace284 Binary files /dev/null and b/test/references/array_float.bson differ diff --git a/test/runtests.jl b/test/runtests.jl index 207589c..2ee3ff5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ using Test using ImageInTerminal, TestImages, ImageCore, ImageTransformations +using BSON:BSON using FileIO using Plots using Random @@ -167,4 +168,16 @@ end @test_reference file raw_contents format="TXT" end +@testset "BSON-files" begin + file = "references/array_any.bson" + arr_any = [1, "asdf", 3//4] + @test_reference file Dict(:ar=>arr_any) + + file = "references/array_float.bson" + comp = (d1, d2) -> keys(d1)==keys(d2) && + all([ isequal(v1,v2) for (v1,v2) in zip(values(d1), values(d2))]) + arr_float = [pi, pi/2, 1.0] + @test_reference file Dict(:ar=>arr_float) by=comp +end + end # top level testset