@@ -25,6 +25,7 @@ function filecheck_exe(; adjust_PATH::Bool=true, adjust_LIBPATH::Bool=true)
2525end
2626
2727function filecheck (f, input;
28+ throws:: Union{Nothing, Type{<:Exception}} = nothing ,
2829 match_full_lines:: Bool = false ,
2930 strict_whitespace:: Bool = false ,
3031 ignore_case:: Bool = false ,
@@ -50,14 +51,33 @@ function filecheck(f, input;
5051 read (pipe, String)
5152 end
5253 result = nothing
54+ thrown = false
5355 io = IOContext (pipe)
54- stats = redirect_stdio (; stdout = io, stderr = io) do
56+ redirect_stdio (; stdout = io, stderr = io) do
5557 put! (pipe_initialized, nothing )
56- result = f (input)
58+ if throws != = nothing
59+ try
60+ result = f (input)
61+ catch e
62+ e isa throws || rethrow ()
63+ thrown = true
64+ Base. display_error (io, e, catch_backtrace ())
65+ end
66+ else
67+ result = f (input)
68+ end
5769 end
58- if result != = nothing
59- println (io)
60- print (io, result)
70+ if throws != = nothing
71+ if ! thrown
72+ close (pipe. in)
73+ fetch (reader)
74+ error (" Expected $throws to be thrown, but no exception was thrown" )
75+ end
76+ else
77+ if result != = nothing
78+ println (io)
79+ print (io, result)
80+ end
6181 end
6282 close (pipe. in)
6383 output = fetch (reader)
@@ -228,6 +248,10 @@ These are forwarded to LLVM's FileCheck as CLI flags:
228248- `check_prefixes::Vector{String}`: Use custom check prefixes (`--check-prefixes`).
229249- `defines::Dict{String,String}`: Define FileCheck variables (`-Dkey=value`).
230250- `allow_empty::Bool`: Allow empty check files (`--allow-empty`).
251+ - `throws::Type{<:Exception}`: Expect the expression to throw an exception of this type.
252+ The error and backtrace are printed in standard Julia format and can be verified with
253+ `@check*` directives. Raises an error if no exception is thrown; re-throws if the wrong
254+ type is caught.
231255
232256# Examples
233257
248272 @check literal=true "foo {{bar}}"
249273 print("foo {{bar}}")
250274end
275+
276+ @test @filecheck throws=ArgumentError begin
277+ @check "ArgumentError: bad"
278+ throw(ArgumentError("bad"))
279+ end
251280```
252281"""
253282macro filecheck (args... )
0 commit comments