-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* restrucure so as to be able to add deepdiffs * add multiline failure test
- Loading branch information
Showing
8 changed files
with
202 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ Images 0.6 | |
FileIO 0.4 | ||
ImageInTerminal 0.2 | ||
ColorTypes 0.4 | ||
DeepDiffs | ||
SHA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
################################################# | ||
# Rendering | ||
# This controls how failures are displayed | ||
abstract type RenderMode end | ||
struct Diff <: RenderMode end | ||
|
||
abstract type BeforeAfter <: RenderMode end | ||
struct BeforeAfterLimited <: BeforeAfter end | ||
struct BeforeAfterFull <: BeforeAfter end | ||
struct BeforeAfterImage <: BeforeAfter end | ||
|
||
render_item(::RenderMode, item) = println(item) | ||
function render_item(::BeforeAfterLimited, item) | ||
show(IOContext(STDOUT, limit=true, displaysize=(20,80)), "text/plain", item) | ||
println() | ||
end | ||
function render_item(::BeforeAfterImage, item) | ||
str_item = @withcolor ImageInTerminal.encodeimg(ImageInTerminal.SmallBlocks(), ImageInTerminal.TermColor256(), item, 20, 40)[1] | ||
println("eltype: ", eltype(item)) | ||
println("size: ", map(length, indices(item))) | ||
println("thumbnail:") | ||
println.(str_item) | ||
end | ||
|
||
## 2 arg form render for comparing | ||
function render(mode::BeforeAfter, reference, actual) | ||
println("- REFERENCE -------------------") | ||
render_item(mode, reference) | ||
println("-------------------------------") | ||
println("- ACTUAL ----------------------") | ||
render_item(mode, actual) | ||
println("-------------------------------") | ||
end | ||
function render(::Diff, reference, actual) | ||
println("- DIFF ------------------------") | ||
@withcolor println(deepdiff(reference, actual)) | ||
println("-------------------------------") | ||
end | ||
|
||
## 1 arg form render for new content | ||
function render(mode::RenderMode, actual) | ||
println("- NEW CONTENT -----------------") | ||
render_item(mode, actual) | ||
println("-------------------------------") | ||
end | ||
|
||
####################################### | ||
# IO | ||
# Right now this basically just extends FileIO to support some things as text files | ||
|
||
const TextFile = Union{File{format"TXT"}, File{format"SHA256"}} | ||
|
||
function loadfile(T, file::File) | ||
T(load(file)) # Fallback to FileIO | ||
end | ||
|
||
function loadfile(T, file::TextFile) | ||
readstring(file.filename) | ||
end | ||
|
||
function savefile(file::File, content) | ||
save(file, content) # Fallback to FileIO | ||
end | ||
|
||
function savefile(file::TextFile, content) | ||
write(file.filename, content) | ||
end | ||
|
||
########################################## | ||
|
||
# Final function | ||
# all other functions should hit one of this eventually | ||
# Which handles the actual testing and user prompting | ||
|
||
function _test_reference(rendermode, file::File, actual) | ||
_test_reference(isequal, rendermode, file, actual) | ||
end | ||
|
||
function _test_reference(equiv, rendermode, file::File, actual::T) where T | ||
path = file.filename | ||
dir, filename = splitdir(path) | ||
try | ||
reference = loadfile(T, file) | ||
if equiv(reference, actual) | ||
@test true # to increase test counter if reached | ||
else # test failed | ||
println("Test for \"$filename\" failed.") | ||
render(rendermode, reference, actual) | ||
if isinteractive() | ||
print("Replace reference with actual result (path: $path)? [y/n] ") | ||
answer = first(readline()) | ||
if answer == 'y' | ||
savefile(file, actual) | ||
warn("Please run the tests again for any changes to take effect") | ||
else | ||
@test false | ||
end | ||
else | ||
error("You need to run the tests interactively with 'include(\"test/runtests.jl\")' to update reference images") | ||
end | ||
end | ||
catch ex | ||
if ex isa SystemError || # File doesn't exist | ||
(is_apple() && ex isa MethodError) || # MethodError is for OSX for some reason | ||
(ex isa ErrorException && startswith(ex.msg, "unable to open")) | ||
|
||
println("Reference file for \"$filename\" does not exist.") | ||
render(rendermode, actual) | ||
if isinteractive() | ||
print("Create reference file with above content (path: $path)? [y/n] ") | ||
answer = first(readline()) | ||
if answer == 'y' | ||
mkpath(dir) | ||
savefile(file, actual) | ||
warn("Please run the tests again for any changes to take effect") | ||
else | ||
@test false | ||
end | ||
else | ||
error("You need to run the tests interactively with 'include(\"test/runtests.jl\")' to create new reference images") | ||
end | ||
else | ||
rethrow(ex) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -------------------------------------------------------------------- | ||
# plain TXT | ||
|
||
function test_reference(file::File{format"TXT"}, actual) | ||
_test_reference(Diff(), file, string(actual)) | ||
end | ||
|
||
function test_reference(file::File{format"TXT"}, actual::AbstractArray{<:AbstractString}) | ||
str = join(actual, '\n') | ||
_test_reference(Diff(), file, str) | ||
end | ||
|
||
# --------------------------------- | ||
# Image | ||
|
||
function test_reference(file::File, actual::AbstractArray{<:Colorant}; sigma=ones(length(indices(actual))), eps=0.01) | ||
_test_reference(BeforeAfterImage(), file, actual) do reference, actual | ||
try | ||
Images.@test_approx_eq_sigma_eps(reference, actual, sigma, eps) | ||
return true | ||
catch err | ||
if err isa ErrorException | ||
return false | ||
else | ||
rethrow() | ||
end | ||
end | ||
end | ||
end | ||
|
||
# Image as txt using ImageInTerminal | ||
function test_reference(file::File{format"TXT"}, actual::AbstractArray{<:Colorant}; size = (20,40)) | ||
strs = @withcolor ImageInTerminal.encodeimg(ImageInTerminal.SmallBlocks(), ImageInTerminal.TermColor256(), actual, size...)[1] | ||
str = join(strs,'\n') | ||
_test_reference(BeforeAfterFull(), file, str) | ||
end | ||
|
||
# -------------------------------------------------------------------- | ||
# SHA as string | ||
|
||
function test_reference(file::File{format"SHA256"}, actual) | ||
test_reference(file, string(actual)) | ||
end | ||
|
||
function test_reference(file::File{format"SHA256"}, actual::Union{AbstractString,Vector{UInt8}}) | ||
str = bytes2hex(sha256(actual)) | ||
_test_reference(BeforeAfterFull(), file, str) | ||
end | ||
|
||
function test_reference(file::File{format"SHA256"}, actual::AbstractArray{<:Colorant}) | ||
size_str = bytes2hex(sha256(reinterpret(UInt8,[map(Int64,size(actual))...]))) | ||
img_str = bytes2hex(sha256(reinterpret(UInt8,vec(rawview(channelview(actual)))))) | ||
_test_reference(BeforeAfterFull(), file, size_str * img_str) | ||
end | ||
|
||
# -------------------------------------------------------------------- | ||
|
||
# Fallback | ||
function test_reference(file::File, actual) | ||
if actual isa AbstractString | ||
# we don't use dispatch for this as it is very ambiguous | ||
# specialization will remove this conditional regardless | ||
|
||
_test_reference(Diff(), file, actual) | ||
else | ||
_test_reference(BeforeAfterLimited(), file, actual) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.