Skip to content

Commit 567dbd9

Browse files
committed
split out default_render and default_equality
1 parent d87fd0b commit 567dbd9

File tree

5 files changed

+88
-75
lines changed

5 files changed

+88
-75
lines changed

src/ReferenceTests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export
1818
include("utils.jl")
1919
include("test_reference.jl")
2020
include("core.jl")
21-
include("handlers.jl")
2221
include("equality_metrics.jl")
2322

2423
end # module

src/core.jl

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ function render(mode::RenderMode, actual)
9292
println("-------------------------------")
9393
end
9494

95+
96+
default_rendermode(::TestMode, ::File, actual) = BeforeAfterFull()
97+
98+
# plain TXT
99+
default_rendermode(::TestMode, ::File{format"TXT"}, actual) = BeforeAfterLimited()
100+
default_rendermode(::TestMode, ::File{format"TXT"}, actual::AbstractString) = Diff()
101+
default_rendermode(::TestMode, ::File{format"TXT"}, actual::Number) = BeforeAfterFull()
102+
103+
# SHA256
104+
default_rendermode(::TestMode, ::File{format"SHA256"}, actual) = BeforeAfterFull()
105+
106+
# images
107+
function default_rendermode(::TestMode, ::File, actual::AbstractArray{<:Colorant})
108+
return BeforeAfterImage()
109+
end
110+
function default_rendermode(::TestMode, ::File{format"TXT"}, actual::AbstractArray{<:Colorant})
111+
return BeforeAfterFull()
112+
end
113+
95114
#######################################
96115
# IO
97116
# Right now this basically just extends FileIO to support some things as text files
@@ -118,17 +137,69 @@ function savefile(file::TextFile, content)
118137
write(file.filename, content)
119138
end
120139

140+
#######################################
141+
# Preprocess
142+
143+
preprocess(::File, actual; kw...) = actual
144+
145+
# plain TXT
146+
preprocess(::File{format"TXT"}, actual; kw...) = string(actual)
147+
preprocess(::File{format"TXT"}, actual::Number; kw...) = actual
148+
function preprocess(::File{format"TXT"}, actual::AbstractArray{<:AbstractString}; kw...)
149+
return join(actual, '\n')
150+
end
151+
152+
# SHA256
153+
preprocess(::File{format"SHA256"}, actual; kw...) = bytes2hex(sha256(string(actual)))
154+
155+
# images
156+
function preprocess(
157+
::File{format"TXT"}, actual::AbstractArray{<:Colorant};
158+
size = (20,40), kw...)
159+
160+
strs = @withcolor ImageInTerminal.encodeimg(
161+
ImageInTerminal.SmallBlocks(),
162+
ImageInTerminal.TermColor256(),
163+
actual,
164+
size...)[1]
165+
str = join(strs,'\n')
166+
end
167+
168+
function preprocess(::File{format"SHA256"}, actual::AbstractArray{<:Colorant}; kw...)
169+
size_str = bytes2hex(sha256(reinterpret(UInt8,[map(Int64,size(actual))...])))
170+
img_str = bytes2hex(sha256(reinterpret(UInt8,vec(rawview(channelview(actual))))))
171+
172+
return size_str * img_str
173+
end
174+
175+
121176
##########################################
122177

123178
# Final function
124179
# all other functions should hit one of this eventually
125180
# Which handles the actual testing and user prompting
126181

127-
function _test_reference(equiv, rendermode, file::File, actual::T) where T
128-
path = file.filename
129-
dir, filename = splitdir(path)
182+
function test_reference(
183+
filename::AbstractString, actual;
184+
by = nothing, render = nothing, kw...)
185+
186+
test_reference(query_extended(filename), actual, render, by; kw...)
187+
end
188+
189+
function test_reference(
190+
file::File,
191+
actual::T,
192+
rendermode::Union{RenderMode, Nothing}=nothing,
193+
equiv::Union{Function, Nothing}=nothing;
194+
kw...) where T
195+
130196
testmode = TESTMODE()
197+
if isnothing(rendermode)
198+
rendermode = default_rendermode(testmode, file, actual)
199+
end
131200

201+
path = file.filename
202+
dir, filename = splitdir(path)
132203
if !isfile(path)
133204
println("Reference file for \"$filename\" does not exist.")
134205
render(rendermode, actual)
@@ -141,8 +212,14 @@ function _test_reference(equiv, rendermode, file::File, actual::T) where T
141212
end
142213

143214
# file exists
215+
actual = preprocess(file, actual; kw...)
144216
reference = loadfile(T, file)
145217

218+
if isnothing(equiv)
219+
# generally, `reference` and `actual` are of the same type after preprocessing
220+
equiv = default_equality(typeof(reference), typeof(actual))
221+
end
222+
146223
if equiv(reference, actual)
147224
@test true
148225
else

src/equality_metrics.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
default_equality(reference, actual) = isequal
2+
function default_equality(
3+
reference::AbstractArray{<:Colorant},
4+
actual::AbstractArray{<:Colorant})
5+
6+
psnr_equality()(reference, actual)
7+
end
8+
19
# ---------------------------------
210
# Image
311

4-
default_image_equality(reference, actual) = psnr_equality()(reference, actual)
5-
612
"""
713
psnr_equality(threshold=25) -> f
814

src/handlers.jl

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/test_reference.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ macro test_reference(reference, actual, kws...)
8383
expr
8484
end
8585

86-
function test_reference(filename::AbstractString, actual; kw...)
87-
test_reference(query_extended(filename), actual; kw...)
88-
end
89-
9086
function query_extended(filename)
9187
file, ext = splitext(filename)
9288
# TODO: make this less hacky

0 commit comments

Comments
 (0)