Skip to content

Commit

Permalink
Resolve test_approx ambiguities (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzgubic authored Jul 19, 2022
1 parent 4050989 commit 5b9ec32
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRulesTestUtils"
uuid = "cdddcdb0-9152-4a09-a978-84456f9df70a"
version = "1.9.0"
version = "1.9.1"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
16 changes: 12 additions & 4 deletions src/check_result.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ function test_approx(
@test_msg msg isapprox(actual, expected; kwargs...)
end

for (T1, T2) in ((AbstractThunk, Any), (AbstractThunk, AbstractThunk), (Any, AbstractThunk))
for (T1, T2) in
(
(AbstractThunk, Any),
(AbstractThunk, AbstractThunk),
(Any, AbstractThunk),
(Tangent, AbstractThunk),
(AbstractThunk, Tangent),
(AbstractZero, AbstractThunk),
(AbstractThunk, AbstractZero),
)
@eval function test_approx(actual::$T1, expected::$T2, msg=""; kwargs...)
return test_approx(unthunk(actual), unthunk(expected), msg; kwargs...)
end
Expand Down Expand Up @@ -123,9 +132,8 @@ function test_approx(actual::Tangent{P,T}, expected, msg=""; kwargs...) where {T
end
test_approx(x, y::Tangent, msg=""; kwargs...) = test_approx(y, x, msg; kwargs...)

function test_approx(actual::Tangent, expected::AbstractThunk, msg=""; kwargs...)
return test_approx(actual, unthunk(expected), msg; kwargs...)
end
test_approx(z::NoTangent, t::Tangent, msg=""; kwargs...) = all(==(NoTangent()), t)
test_approx(t::Tangent, z::NoTangent, msg=""; kwargs...) = all(==(NoTangent()), t)

# This catches comparisons of Tangents and Tuples/NamedTuple
# and gives an error message complaining about that. the `@test` will definitely fail
Expand Down
14 changes: 14 additions & 0 deletions test/check_result.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ end
Tangent{Tuple{Float64,Float64}}(1.0, 2.0),
@thunk(Tangent{Tuple{Float64,Float64}}(1.0, 2.0)),
)
test_approx(
@thunk(Tangent{Tuple{Float64,Float64}}(1.0, 2.0)),
Tangent{Tuple{Float64,Float64}}(1.0, 2.0),
)
test_approx(@thunk(ZeroTangent()), ZeroTangent())
test_approx(ZeroTangent(), @thunk(ZeroTangent()))
test_approx(
Tangent{Tuple{Float64,Float64}}(NoTangent(), NoTangent()),
NoTangent(),
)
test_approx(
NoTangent(),
Tangent{Tuple{Float64,Float64}}(NoTangent(), NoTangent()),
)
end
@testset "negative case" begin
@test fails(() -> test_approx(1.0, 2.0))
Expand Down

2 comments on commit 5b9ec32

@mzgubic
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/64561

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.9.1 -m "<description of version>" 5b9ec32a49a26984112edc6102488538d5efe2ae
git push origin v1.9.1

Please sign in to comment.