-
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.
Merge pull request #37 from johnnychen94/default
break `handlers.jl` into default_rendermode, default_equality and _convert
- Loading branch information
Showing
8 changed files
with
225 additions
and
203 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 |
---|---|---|
@@ -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" | ||
|
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 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
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,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 |
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,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() |
Oops, something went wrong.
46d650a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
46d650a
There was a problem hiding this comment.
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: