diff --git a/src/test_reference.jl b/src/test_reference.jl index 8b323cd..7854fe1 100644 --- a/src/test_reference.jl +++ b/src/test_reference.jl @@ -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, @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 2ee3ff5..543b42d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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") @@ -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