Skip to content

Commit

Permalink
Merge pull request #37 from johnnychen94/default
Browse files Browse the repository at this point in the history
break `handlers.jl` into default_rendermode, default_equality and _convert
  • Loading branch information
johnnychen94 authored Oct 11, 2019
2 parents dcba05a + ffa165a commit 46d650a
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 203 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ReferenceTests"
uuid = "324d217c-45ce-50fc-942e-d289b448e8cf"
authors = ["Christof Stocker <[email protected]>", "Lyndon White <[email protected]>"]
version = "0.8.1"
version = "0.8.2"

[deps]
DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6"
Expand Down
4 changes: 2 additions & 2 deletions src/ReferenceTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export

include("utils.jl")
include("test_reference.jl")
include("core.jl")
include("handlers.jl")
include("fileio.jl")
include("equality_metrics.jl")
include("render.jl")

end # module
122 changes: 0 additions & 122 deletions src/core.jl

This file was deleted.

18 changes: 16 additions & 2 deletions src/equality_metrics.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
"""
default_equality(reference, actual) -> f
Infer a suitable equality comparison method `f` according to input types.
`f` is a function that satisfies signature `f(reference, actual)::Bool`. If `f` outputs
`true`, it indicates that `reference` and `actual` are "equal" in the sense of `f`.
"""
default_equality(reference, actual) = isequal
function default_equality(
reference::AbstractArray{<:Colorant},
actual::AbstractArray{<:Colorant})

return psnr_equality()
end

# ---------------------------------
# Image

default_image_equality(reference, actual) = psnr_equality()(reference, actual)

"""
psnr_equality(threshold=25) -> f
Expand Down
75 changes: 75 additions & 0 deletions src/fileio.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#######################################
# 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)
read(file.filename, String)
end

function loadfile(::Type{<:Number}, file::File{format"TXT"})
parse(Float64, loadfile(String, file))
end

function savefile(file::File, content)
save(file, content) # Fallback to FileIO
end

function savefile(file::TextFile, content)
write(file.filename, content)
end

function query_extended(filename)
file, ext = splitext(filename)
# TODO: make this less hacky
if uppercase(ext) == ".SHA256"
res = File{format"SHA256"}(filename)
else
res = query(filename)
if res isa File{DataFormat{:UNKNOWN}}
res = File{format"TXT"}(filename)
end
end
res
end

"""
_convert(T::Type{<:DataFormat}, x; kw...) -> out
Convert `x` to a validate content for file data format `T`.
"""
_convert(::Type{<:DataFormat}, x; kw...) = x

# plain TXT
_convert(::Type{DataFormat{:TXT}}, x; kw...) = string(x)
_convert(::Type{DataFormat{:TXT}}, x::Number; kw...) = x
function _convert(::Type{DataFormat{:TXT}}, x::AbstractArray{<:AbstractString}; kw...)
return join(x, '\n')
end
function _convert(
::Type{DataFormat{:TXT}}, img::AbstractArray{<:Colorant};
size = (20,40), kw...)

# encode image into string
strs = @withcolor ImageInTerminal.encodeimg(
ImageInTerminal.SmallBlocks(),
ImageInTerminal.TermColor256(),
img,
size...)[1]
return join(strs,'\n')
end

# SHA256
_convert(::Type{DataFormat{:SHA256}}, x; kw...) = bytes2hex(sha256(string(x)))
function _convert(::Type{DataFormat{:SHA256}}, img::AbstractArray{<:Colorant}; kw...)
# encode image into SHA256
size_str = bytes2hex(sha256(reinterpret(UInt8,[map(Int64,size(img))...])))
img_str = bytes2hex(sha256(reinterpret(UInt8,vec(rawview(channelview(img))))))

return size_str * img_str
end
65 changes: 0 additions & 65 deletions src/handlers.jl

This file was deleted.

65 changes: 65 additions & 0 deletions src/render.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#################################################
# 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, axes(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

"""
default_rendermode(::DataFormat, actual)
Infer the most appropriate render mode according to type of reference file and `actual`.
"""
default_rendermode(::Type{<:DataFormat}, ::Any) = BeforeAfterLimited()
default_rendermode(::Type{<:DataFormat}, ::AbstractString) = Diff()
default_rendermode(::Type{<:DataFormat}, ::AbstractArray{<:Colorant}) = BeforeAfterImage()

# plain TXTs
default_rendermode(::Type{DataFormat{:TXT}}, ::Any) = Diff()
default_rendermode(::Type{DataFormat{:TXT}}, ::AbstractString) = Diff()
default_rendermode(::Type{DataFormat{:TXT}}, ::Number) = BeforeAfterFull()
default_rendermode(::Type{DataFormat{:TXT}}, ::AbstractArray{<:Colorant}) = BeforeAfterFull()

# SHA256
default_rendermode(::Type{DataFormat{:SHA256}}, ::Any) = BeforeAfterFull()
default_rendermode(::Type{DataFormat{:SHA256}}, ::AbstractString) = BeforeAfterFull()
default_rendermode(::Type{DataFormat{:SHA256}}, ::AbstractArray{<:Colorant}) = BeforeAfterFull()
Loading

2 comments on commit 46d650a

@johnnychen94
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/4295

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.2 -m "<description of version>" 46d650a2efc73177bc9df7408a579c78488c6849
git push origin v0.8.2

Please sign in to comment.