-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathruntests.jl
More file actions
209 lines (179 loc) · 7.67 KB
/
Copy pathruntests.jl
File metadata and controls
209 lines (179 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Test
using FileIO
using BSON: BSON
using Random
using ReferenceTests
using Logging
# option to minimize messages
const verbose = tryparse(Bool, get(ENV, "VERBOSE", "true")) === true
const test_logger = verbose ? current_logger() : NullLogger()
if isinteractive()
@info (
"In interactive use, one should respond \"n\" when the program " *
"offers to create or replace files associated with some tests."
)
else
verbose && @info (
"Ten tests should correctly report failure in the transcript " *
"(but not the test summary)."
)
end
ambs = detect_ambiguities(ReferenceTests, Base, Core)
@test isempty(ambs)
# to properly test world age issues, the full test dependencies must be loaded later
include("test_no_world_age_issues.jl")
using XTermColors, TestImages, ImageCore, ImageTransformations
using Plots
strip_summary(content::String) = join(split(content, "\n")[2:end], "\n")
@testset "ReferenceTests" begin
# load/create some example images
monarch = testimage("monarch_color_256")
camera = testimage("cameraman")
cameras = similar(camera, size(camera)..., 2)
copyto!(view(cameras,:,:,1), camera)
copyto!(view(cameras,:,:,2), camera)
square = Gray{N0f8}[0.1 0.2 0.3; 0.4 0.5 0.6; 0.7 0.6 0.9]
rgb_rect = rand(RGB{N0f8}, 2, 3)
@testset "io2str" begin
@test_throws Exception eval(@macroexpand @io2str(::IO))
@test_throws ArgumentError @io2str(2)
@test_throws ArgumentError @io2str(string(2))
@test @io2str(print(::IO, "foo")) == "foo"
@test @io2str(println(::IO, "foo")) == "foo\n"
@test @io2str(show(::IO, "foo")) == "\"foo\""
A = ones(30,30)
@test @io2str(show(IOContext(::IO, :limit => true, :displaysize => (5,5)), A)) == "[1.0 1.0 … 1.0 1.0; 1.0 1.0 … 1.0 1.0; … ; 1.0 1.0 … 1.0 1.0; 1.0 1.0 … 1.0 1.0]"
end
@testset "withcolor" begin
@test_throws ArgumentError @withcolor throw(ArgumentError("foo"))
@test @withcolor Base.have_color == true
@test @withcolor @io2str(printstyled(IOContext(::IO, :color => true), "test", color=:green)) == "\e[32mtest\e[39m"
end
@testset "string as txt" begin
foo = "foo"
@test_reference "references/string1.txt" foo * "bar"
@test_reference "references/string1.txt" [foo * "bar"]
A = ones(30,30)
@test_reference "references/string2.txt" @io2str show(IOContext(::IO, :limit=>true, :displaysize=>(5,5)), A)
@test_reference "references/string3.txt" 1337
@test_reference "references/string3.txt" 1338 by=(ref, x)->isapprox(ref, x; atol=10)
@test_reference "references/string4.txt" strip_summary(@io2str show(::IO, MIME"text/plain"(), Int64.(collect(1:5))))
# ignore CRLF/LF differences
@test_reference "references/string5.txt" """
This is a\r
multiline string that does not end with a new line."""
@test_reference "references/string5.txt" """
This is a
multiline string that does not end with a new line."""
@test_reference "references/string6.txt" """
This on the other hand is a
multiline string that does indeed end with a new line.
"""
with_logger(test_logger) do
@test_throws ErrorException @test_reference "references/string1.txt" "intentionally wrong to check that this message prints"
@test_throws ErrorException @test_reference "references/string5.txt" """
This is an incorrect
multiline string that does not end with a new line."""
end
end
@testset "string as unknown file type" begin
@test_reference "references/string1.nottxt" "This is not a .txt file, but it should be treated as such.\n"
end
@testset "images as txt using XTermColors" begin
# @test_throws MethodError @test_reference "references/fail.txt" rand(2,2)
@test_reference "references/camera.txt" camera size=(5,10)
@test_reference "references/moarch.txt" monarch
end
@testset "plain ansi string" begin
@test_reference(
"references/ansii.txt",
@io2str(printstyled(IOContext(::IO, :color=>true), "this should be blue", color=:blue)),
render = ReferenceTests.BeforeAfterFull()
)
with_logger(test_logger) do
@test_throws ErrorException @test_reference(
"references/ansii.txt",
@io2str(printstyled(IOContext(::IO, :color=>true), "this should be red", color=:red)),
render = ReferenceTests.BeforeAfterFull()
)
end
end
@testset "string as SHA" begin
@test_reference "references/number1.sha256" 1337
foo = "foo"
@test_reference "references/string1.sha256" foo * "bar"
A = ones(30,30)
@test_reference "references/string2.sha256" @io2str show(IOContext(::IO, :limit=>true, :displaysize=>(5,5)), A)
end
@testset "images as SHA" begin
@test_reference "references/camera.sha256" camera
@test_reference "references/monarch.sha256" convert(Matrix{RGB{Float64}}, monarch)
end
@testset "images as PNG" begin
@test_reference "references/camera.png" imresize(camera, (64,64))
@test_reference "references/camera.png" imresize(camera, (64,64)) by=psnr_equality(25)
with_logger(test_logger) do
@test_throws ErrorException @test_reference "references/camera.png" imresize(monarch, (64,64))
@test_throws Exception @test_reference "references/camera.png" camera # unequal size
end
end
# Test disabled on linux because: https://github.com/JuliaPlots/Plots.jl/issues/2127
Sys.islinux() || @testset "Plots as PNG images" begin
@test_reference "references/heatmap.png" heatmap([1 0; 0 1]) by=psnr_equality(15)
@test_reference "references/scatter.png" scatter([(0,0),(1,0),(0,1),(1,1)], ms=8)
end
using DataFrames, CSVFiles
@testset "DataFrame as CSV" begin
@test_reference "references/dataframe.csv" DataFrame(v1=[1,2,3], v2=["a","b","c"])
with_logger(test_logger) do
@test_throws ErrorException @test_reference "references/dataframe.csv" DataFrame(v1=[1,2,3], v2=["c","b","c"])
end
end
@testset "Create new .$ext" for (ext, val) in (
("csv", DataFrame(v1=[1,2,3], v2=["c","b","c"])),
("png", imresize(camera, (64,64))),
("txt", "Lorem ipsum dolor sit amet, labore et dolore magna aliqua."),
)
mktempdir() do path
newfilename = joinpath(path, "newfilename.$ext")
@assert !isfile(newfilename)
withenv("JULIA_REFERENCETESTS_UPDATE"=>true) do
with_logger(test_logger) do
@test_reference newfilename val # this should create it
end
end
@test isfile(newfilename) # Was created
@test_reference newfilename val # Matches expected content
end
end
@testset "Create new image as txt" begin
# This is a separate testset as need to use the `size` argument to ``@test_reference`
mktempdir() do path
newfilename = joinpath(path, "new_camera.txt")
@assert !isfile(newfilename)
with_logger(test_logger) do
withenv("JULIA_REFERENCETESTS_UPDATE"=>true) do
@test_reference newfilename camera size=(5,10) # this should create it
end
end
@test isfile(newfilename) # Was created
@test_reference newfilename camera size=(5,10) # Matches expected content
end
end
@testset "format keyword" begin
file = "references/camera.sha256"
raw_contents = read(file, String)
@test_reference file raw_contents format=format"TXT"
@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