Skip to content

Commit

Permalink
allow customizable RenderMode for txt
Browse files Browse the repository at this point in the history
This commit enables a user to provide a `mode` keyword
to `@test_reference` for "txt" files, which overwrites
the default `RenderMode`.

This is useful if the text contains ANSII colors and
should not be displayed as a `Diff`, but rather as
a `BeforeAfter` (e.g. for testing UnicodePlots).
  • Loading branch information
Evizero committed Oct 31, 2018
1 parent 6fbf5cb commit a9c405c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function _test_reference(equiv, rendermode, file::File, actual::T) where T
if isinteractive()
if input_bool("Replace reference with actual result (path: $path)?")
savefile(file, actual)
warn("Please run the tests again for any changes to take effect")
@warn("Please run the tests again for any changes to take effect")
else
@test false
end
Expand All @@ -108,7 +108,7 @@ function _test_reference(equiv, rendermode, file::File, actual::T) where T
if input_bool("Create reference file with above content (path: $path)?")
mkpath(dir)
savefile(file, actual)
warn("Please run the tests again for any changes to take effect")
@warn("Please run the tests again for any changes to take effect")
else
@test false
end
Expand Down
12 changes: 6 additions & 6 deletions src/handlers.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# --------------------------------------------------------------------
# plain TXT

function test_reference(file::File{format"TXT"}, actual)
_test_reference(Diff(), file, string(actual))
function test_reference(file::File{format"TXT"}, actual; mode = Diff())
_test_reference(mode, file, string(actual))
end

function test_reference(file::File{format"TXT"}, actual::AbstractArray{<:AbstractString})
function test_reference(file::File{format"TXT"}, actual::AbstractArray{<:AbstractString}; mode = Diff())
str = join(actual, '\n')
_test_reference(Diff(), file, str)
_test_reference(mode, file, str)
end

# ---------------------------------
Expand All @@ -29,10 +29,10 @@ function test_reference(file::File, actual::AbstractArray{<:Colorant}; sigma=one
end

# Image as txt using ImageInTerminal
function test_reference(file::File{format"TXT"}, actual::AbstractArray{<:Colorant}; size = (20,40))
function test_reference(file::File{format"TXT"}, actual::AbstractArray{<:Colorant}; size = (20,40), mode = BeforeAfterFull())
strs = @withcolor ImageInTerminal.encodeimg(ImageInTerminal.SmallBlocks(), ImageInTerminal.TermColor256(), actual, size...)[1]
str = join(strs,'\n')
_test_reference(BeforeAfterFull(), file, str)
_test_reference(mode, file, str)
end

# --------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end
io2str_impl(arg) = :(throw(ArgumentError("Invalid use of `@io2str` macro: The given argument `$($(string(arg)))` is not an expression.")))

function io2str_impl(expr::Expr)
nvar = Symbol("#io#", randstring(4))
nvar = gensym("io")
if replace_expr!(expr, :(::IO), nvar)
esc(quote
$nvar = Base.IOBuffer()
Expand Down
1 change: 1 addition & 0 deletions test/references/ansii.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this should be blue
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ end
@test_reference "references/lena.txt" lena
end

@testset "plain ansi string" begin
@test_reference(
"references/ansii.txt",
@io2str(printstyled(IOContext(::IO, :color=>true), "this should be blue", color=:blue)),
mode = ReferenceTests.BeforeAfterFull()
)
@test_throws ErrorException @test_reference(
"references/ansii.txt",
@io2str(printstyled(IOContext(::IO, :color=>true), "this should be red", color=:red)),
mode = ReferenceTests.BeforeAfterFull()
)
end

@testset "string as SHA" begin
@test_reference "references/number1.sha256" 1337
foo = "foo"
Expand Down

0 comments on commit a9c405c

Please sign in to comment.