From 73f6e23a90001f1e54ef99d34cf9c58b4343f0d8 Mon Sep 17 00:00:00 2001 From: Mauro Date: Thu, 28 Oct 2021 16:02:04 +0200 Subject: [PATCH] Added example on how to use BSON-file (#98) --- Project.toml | 7 +++++-- README.md | 2 +- src/test_reference.jl | 32 ++++++++++++++++++++++++------- test/references/array_any.bson | Bin 0 -> 262 bytes test/references/array_float.bson | Bin 0 -> 166 bytes test/runtests.jl | 13 +++++++++++++ 6 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 test/references/array_any.bson create mode 100644 test/references/array_float.bson 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 0000000000000000000000000000000000000000..4a639500bc79cefad7a93f18a67051433e403b6f GIT binary patch literal 262 zcmaKnK?=e!5JkUDthnt~@CHI#!~^I?7lL?$!B#1>X-UR~*YRN9L~te|-8qZo<>yc4 zC!GM6Jl+7Z9CC`46qpK7sID+2Ucw2}4Ksnj{(fN?ehf99zni&0MNCn(19|EKP9mBd z`@bd~1na1)u~cIoNAG%HLt>;N9UO1gqiZ#~j?_4H=^I-L(osT_oQa#>Q7dRQ6&6DB NKO>t~R=|EKt{(uOD$M`@ literal 0 HcmV?d00001 diff --git a/test/references/array_float.bson b/test/references/array_float.bson new file mode 100644 index 0000000000000000000000000000000000000000..dace28426aa06c1d06b5edaf78ed0060fc883fad GIT binary patch literal 166 zcmZ3+z`($qSi~?FNHdirrZcbs8Hq(jiIoh@C6xuK3|=q^P9P&Cu_O^J%2JS6l$cx0 zzzP&(V986&O=VC3vX~6OBF_0msSHep3>-iqx19XM5;GH!vf|9DR0aVcOUQtM8AyQv cE67X+2@qRC*Cph)BBuj{{%H?U@Bv5z0Earr_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