-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from daniel-thom/assert-op
Add assert_op macro
- Loading branch information
Showing
15 changed files
with
88 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" | ||
Throw an `AssertionError` if conditions like `op(exp1, exp2)` are `false`, where `op` is a conditional infix operator. | ||
# Examples | ||
``` | ||
julia> a = 3; b = 4; | ||
julia> @assert_op a == b | ||
ERROR: AssertionError: 3 == 4 | ||
julia> @assert_op a + 3 > b + 4 | ||
ERROR: AssertionError: 6 > 8 | ||
``` | ||
""" | ||
macro assert_op(expr) | ||
assert_op(expr) | ||
end | ||
|
||
function assert_op(expr::Expr) | ||
# Only special case expressions of the form `expr1 == expr2` | ||
if length(expr.args) == 3 && expr.head == :call | ||
return assert_op(expr.args[1], expr.args[2], expr.args[3]) | ||
else | ||
return :(@assert $(expr)) | ||
end | ||
end | ||
|
||
function assert_op(op, exp1, exp2) | ||
return :( | ||
if !$op($(esc(exp1)), $(esc(exp2))) | ||
val1 = $(esc(exp1)) | ||
val2 = $(esc(exp2)) | ||
op_str = $(esc(op)) | ||
throw(AssertionError("$val1 $op_str $val2")) | ||
end | ||
) | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@testset "Test assert_op" begin | ||
a = 2 | ||
b = 2 | ||
IS.@assert_op a == b | ||
IS.@assert_op a + 2 == b + 2 | ||
IS.@assert_op isequal(a + 2, b + 2) | ||
|
||
@test_throws AssertionError IS.@assert_op a + 3 == b + 2 | ||
@test_throws AssertionError IS.@assert_op isequal(a + 3, b + 2) | ||
end |