Skip to content

Commit

Permalink
Restructure to add Diffs (#7)
Browse files Browse the repository at this point in the history
* restrucure so as to be able to add deepdiffs

* add multiline failure test
  • Loading branch information
oxinabox authored and Evizero committed Jun 30, 2018
1 parent 9449621 commit a082fab
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 213 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Images 0.6
FileIO 0.4
ImageInTerminal 0.2
ColorTypes 0.4
DeepDiffs
SHA
5 changes: 3 additions & 2 deletions src/ReferenceTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using FileIO
using ImageInTerminal
using ColorTypes
using SHA
using DeepDiffs

export

Expand All @@ -15,7 +16,7 @@ export

include("utils.jl")
include("test_reference.jl")
include("string.jl")
include("fallback.jl")
include("core.jl")
include("handlers.jl")

end # module
126 changes: 126 additions & 0 deletions src/core.jl
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
53 changes: 0 additions & 53 deletions src/fallback.jl

This file was deleted.

68 changes: 68 additions & 0 deletions src/handlers.jl
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
92 changes: 0 additions & 92 deletions src/string.jl

This file was deleted.

Loading

0 comments on commit a082fab

Please sign in to comment.