Skip to content

Commit

Permalink
Added example on how to use BSON-file (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro3 authored Oct 28, 2021
1 parent c2ad98c commit 73f6e23
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 25 additions & 7 deletions src/test_reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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...)
Expand Down
Binary file added test/references/array_any.bson
Binary file not shown.
Binary file added test/references/array_float.bson
Binary file not shown.
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test
using ImageInTerminal, TestImages, ImageCore, ImageTransformations
using BSON:BSON
using FileIO
using Plots
using Random
Expand Down Expand Up @@ -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

0 comments on commit 73f6e23

Please sign in to comment.