Skip to content

Commit fb23681

Browse files
authored
Merge pull request #1 from JuliaLLVM/tb/throws
Add throws keyword to support validating exception messages.
1 parent ebadff4 commit fb23681

3 files changed

Lines changed: 76 additions & 6 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "FileCheck"
22
uuid = "4e644321-382b-4b05-b0b6-5d23c3d944fb"
33
authors = ["Tim Besard <tim.besard@gmail.com>"]
4-
version = "1.0"
4+
version = "1.1"
55

66
[deps]
77
LLVM_jll = "86de99a1-58d6-5da7-8064-bd56ce2e322c"

src/FileCheck.jl

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function filecheck_exe(; adjust_PATH::Bool=true, adjust_LIBPATH::Bool=true)
2525
end
2626

2727
function 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
@@ -248,6 +272,11 @@ end
248272
@check literal=true "foo {{bar}}"
249273
print("foo {{bar}}")
250274
end
275+
276+
@test @filecheck throws=ArgumentError begin
277+
@check "ArgumentError: bad"
278+
throw(ArgumentError("bad"))
279+
end
251280
```
252281
"""
253282
macro filecheck(args...)

test/runtests.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,47 @@ end
162162
end
163163
end
164164

165+
@testset "throws" begin
166+
# Basic: catch expected exception, verify error message
167+
@test @filecheck throws=ErrorException begin
168+
@check "ERROR: TestError"
169+
error("TestError")
170+
end
171+
172+
# Verify backtrace is included
173+
@test @filecheck throws=ErrorException begin
174+
@check "ERROR: TestError"
175+
@check "Stacktrace:"
176+
error("TestError")
177+
end
178+
179+
# ArgumentError type
180+
@test @filecheck throws=ArgumentError begin
181+
@check "ArgumentError: bad arg"
182+
throw(ArgumentError("bad arg"))
183+
end
184+
185+
# Output before exception is also captured
186+
@test @filecheck throws=ErrorException begin
187+
@check "before"
188+
@check "ERROR: boom"
189+
println("before")
190+
error("boom")
191+
end
192+
193+
# No exception thrown when expected → error
194+
@test_throws "no exception was thrown" @filecheck throws=ErrorException begin
195+
@check "hello"
196+
"hello"
197+
end
198+
199+
# Wrong exception type → rethrown
200+
@test_throws ArgumentError @filecheck throws=ErrorException begin
201+
@check "hello"
202+
throw(ArgumentError("wrong type"))
203+
end
204+
end
205+
165206
@testset "conditional checks" begin
166207
# cond=true: check is included
167208
@test @filecheck begin

0 commit comments

Comments
 (0)