Skip to content

Commit 015400e

Browse files
committed
split out default_render and default_equality
1 parent d87fd0b commit 015400e

File tree

5 files changed

+84
-73
lines changed

5 files changed

+84
-73
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: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ 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+
# images
104+
function default_rendermode(::TestMode, ::File, actual::AbstractArray{<:Colorant})
105+
return BeforeAfterImage()
106+
end
107+
function default_rendermode(::TestMode, ::File{format"TXT"}, actual::AbstractArray{<:Colorant})
108+
return BeforeAfterFull()
109+
end
110+
95111
#######################################
96112
# IO
97113
# Right now this basically just extends FileIO to support some things as text files
@@ -118,17 +134,70 @@ function savefile(file::TextFile, content)
118134
write(file.filename, content)
119135
end
120136

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

123175
# Final function
124176
# all other functions should hit one of this eventually
125177
# Which handles the actual testing and user prompting
126178

127-
function _test_reference(equiv, rendermode, file::File, actual::T) where T
179+
function test_reference(
180+
filename::AbstractString, actual;
181+
by = nothing, render = nothing, kw...)
182+
183+
test_reference(query_extended(filename), actual, render, by; kw...)
184+
end
185+
186+
function test_reference(
187+
file::File,
188+
actual::T,
189+
rendermode::Union{RenderMode, Nothing}=nothing,
190+
equiv::Union{Function, Nothing}=nothing;
191+
kw...) where T
192+
128193
path = file.filename
129194
dir, filename = splitdir(path)
130195
testmode = TESTMODE()
131196

197+
if rendermode === nothing
198+
rendermode = default_rendermode(testmode, file, actual)
199+
end
200+
132201
if !isfile(path)
133202
println("Reference file for \"$filename\" does not exist.")
134203
render(rendermode, actual)
@@ -141,8 +210,14 @@ function _test_reference(equiv, rendermode, file::File, actual::T) where T
141210
end
142211

143212
# file exists
213+
actual = preprocess(file, actual; kw...)
144214
reference = loadfile(T, file)
145215

216+
if equiv === nothing
217+
# generally, `reference` and `actual` are of the same type after preprocessing
218+
equiv = default_equality(reference, actual)
219+
end
220+
146221
if equiv(reference, actual)
147222
@test true
148223
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+
return psnr_equality()
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)