Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add the match_reference function, and then refactor test_reference to call match_reference under the hood #112

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
74 changes: 66 additions & 8 deletions src/test_reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,17 @@ macro test_reference(reference, actual, kws...)
expr
end

function test_reference(
filename::AbstractString, raw_actual;
by = nothing, render = nothing, format = nothing, kw...)
format isa AbstractString && (format = FileIO.DataFormat{Symbol(format)})
reference_file = format === nothing ? query_extended(filename) : File{format}(filename)
test_reference(reference_file, raw_actual, by, render; kw...)
for f in [:test_reference, :match_reference, :_do_reference_matching]
@eval function $f(
filename::AbstractString, raw_actual;
by = nothing, render = nothing, format = nothing, kw...)
format isa AbstractString && (format = FileIO.DataFormat{Symbol(format)})
reference_file = format === nothing ? query_extended(filename) : File{format}(filename)
$f(reference_file, raw_actual, by, render; kw...)
end
end

function test_reference(
function _do_reference_matching(
reference_file::File{F},
raw_actual::T,
equiv=nothing,
Expand Down Expand Up @@ -152,7 +154,63 @@ function test_reference(
equiv = default_equality(reference, actual)
end

if equiv(reference, actual)
match_result = equiv(reference, actual)

all_info = (;
actual = actual,
match_result = match_result,
reference = reference,
reference_path = reference_path,
reference_dir = reference_dir,
reference_filename = reference_filename,
rendermode = rendermode,
)
return all_info
end

function match_reference(
reference_file::File{F},
raw_actual::T,
equiv=nothing,
rendermode=nothing;
kw...) where {F <: DataFormat, T}
all_info = _do_reference_matching(
reference_file,
raw_actual::T,
equiv,
rendermode;
kw...,
)
(all_info === nothing) && return nothing
match_result = all_info.match_result
return match_result
end

function test_reference(
reference_file::File{F},
raw_actual::T,
equiv=nothing,
rendermode=nothing;
kw...) where {F <: DataFormat, T}

all_info = _do_reference_matching(
reference_file,
raw_actual,
equiv,
rendermode;
kw...,
)
(all_info === nothing) && return nothing

actual = all_info.actual
match_result = all_info.match_result
reference = all_info.reference
reference_path = all_info.reference_path
reference_dir = all_info.reference_dir
reference_filename = all_info.reference_filename
rendermode = all_info.rendermode

if match_result
@test true # to increase test counter if reached
else # When test fails
# Saving actual file so user can look at it
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ end
refambs = detect_ambiguities(ImageInTerminal, Base, Core)

using ReferenceTests
const match_reference = ReferenceTests.match_reference
ambs = detect_ambiguities(ReferenceTests, ImageInTerminal, Base, Core)

strip_summary(content::String) = join(split(content, "\n")[2:end], "\n")
Expand Down Expand Up @@ -81,6 +82,7 @@ end

@testset "string as unknown file type" begin
@test_reference "references/string1.nottxt" "This is not a .txt file, but it should be treated as such.\n"
@test match_reference("references/string1.nottxt", "This is not a .txt file, but it should be treated as such.\n")
end

@testset "images as txt using ImageInTerminal" begin
Expand Down