Skip to content

Commit 531756e

Browse files
authored
Add the ability to pass a custom comparison function (#4)
The motivating case was a test where the output is path-dependent, so the comparison function needs to compare against the test machine's path.
1 parent 6138068 commit 531756e

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test/expected.out
2+
test/failed.out

Project.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,11 @@ version = "0.1.2"
77
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
88
VT100 = "7774df62-37c0-5c21-b34d-f6d7f98f54bc"
99

10+
[extras]
11+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
12+
1013
[compat]
1114
julia = "1.1"
15+
16+
[targets]
17+
test = ["Test"]

src/TerminalRegressionTests.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ module TerminalRegressionTests
155155
end
156156
end
157157

158-
function automated_test(f, outputpath, inputs; aggressive_yield = false)
158+
function automated_test(f, cmp, outputpath, inputs; aggressive_yield = false)
159159
emuterm = EmulatedTerminal()
160160
emuterm.aggressive_yield = aggressive_yield
161161
emuterm.terminal.warn = true
@@ -178,7 +178,7 @@ module TerminalRegressionTests
178178
decorator = isempty(decorators) ? nothing : popfirst!(decorators)
179179
@assert !eof(emuterm.pty.master)
180180
process_all_buffered(emuterm)
181-
compare(emuterm.terminal, output, decorator)
181+
cmp(emuterm.terminal, output, decorator)
182182
print(emuterm.input_buffer, input); notify(emuterm.filled)
183183
end
184184
Base.notify(c)
@@ -190,6 +190,7 @@ module TerminalRegressionTests
190190
wait(c)
191191
end
192192
end
193+
automated_test(f, outputpath, inputs; kwargs...) = automated_test(f, compare, outputpath, inputs; kwargs...)
193194

194195
function create_automated_test(f, outputpath, inputs; aggressive_yield=false)
195196
emuterm = EmulatedTerminal()

test/TRT2.multiout

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
++++++++++++++++++++++++++++++++++++++++++++++++++
2+
|Hello, wurld!
3+
|
4+
--------------------------------------------------
5+
|AAAAAAAAAAAAA
6+
|

test/runtests.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using TerminalRegressionTests
2+
using Test
23

34
const thisdir = dirname(@__FILE__)
45
TerminalRegressionTests.automated_test(
@@ -10,3 +11,35 @@ TerminalRegressionTests.automated_test(
1011
resp = strip(readline(emuterm))
1112
@assert resp == "Yes!!"
1213
end
14+
15+
mktemp() do _, io
16+
redirect_stderr(io) do
17+
redirect_stdout(io) do
18+
@test_throws ErrorException TerminalRegressionTests.automated_test(
19+
joinpath(thisdir,"TRT2.multiout"),
20+
[""]) do emuterm
21+
println(emuterm, "Hello, world!") # generate with "wurld" rather than "world"
22+
readline(emuterm) # needed to produce output?
23+
end
24+
end
25+
end
26+
end
27+
28+
function compare_replace(em, output; replace=nothing)
29+
buf = IOBuffer()
30+
decoratorbuf = IOBuffer()
31+
TerminalRegressionTests.VT100.dump(buf,decoratorbuf,em)
32+
outbuf = take!(buf)
33+
if replace !== nothing
34+
output = Base.replace(output, replace)
35+
end
36+
TerminalRegressionTests._compare(Vector{UInt8}(codeunits(output)), outbuf) || return false
37+
return true
38+
end
39+
const cmp(a, b, decorator) = compare_replace(a, b; replace="wurld"=>"world")
40+
TerminalRegressionTests.automated_test(cmp,
41+
joinpath(thisdir,"TRT2.multiout"),
42+
[""]) do emuterm
43+
println(emuterm, "Hello, world!")
44+
readline(emuterm)
45+
end

0 commit comments

Comments
 (0)