Test: support do-block syntax in @test_throws#61157
Open
christiankadiu wants to merge 1 commit intoJuliaLang:masterfrom
Open
Test: support do-block syntax in @test_throws#61157christiankadiu wants to merge 1 commit intoJuliaLang:masterfrom
christiankadiu wants to merge 1 commit intoJuliaLang:masterfrom
Conversation
Contributor
|
Can you elaborate on the quality-of-life benefits of this new syntax, in comparison to the already working @test_throws ErrorException begin
error("This should pass but it fails")
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I am opening this PR because I have identified the following issue:
Currently, using a do block with the @test_throws macro fails silently or behaves unexpectedly. For example:
Because do blocks are parsed as anonymous functions passed as the first argument to the macro, @test_throws receives the expression as a closure (e.g., () -> begin ... end) and simply evaluates it. Since evaluating a closure definition only returns the function without executing its body, no exception is ever thrown, leading to a test failure.
This PR introduces explicit support for do-block syntax in @test_throws. It inspects the AST to detect if the passed expression is a zero-argument anonymous function (checking for :-> or :function heads with empty argument tuples). If it is, the macro rewrites the AST to wrap the escaped expression in an Expr(:call, ...) so that the closure is actually executed during the test.
This provides a much-needed quality of life improvement for testing longer blocks of code that are expected to throw exceptions.