Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Test a weak dependency #52

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
name = "InverseFunctions"
uuid = "3587e190-3f89-42d0-90ee-14403ec27112"
version = "0.1.15"
version = "0.1.16"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[weakdeps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[extensions]
DatesExt = "Dates"
InverseFunctionsDatesExt = "Dates"
InverseFunctionsTestExt = "Test"

[compat]
julia = "1"

[extras]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["Dates", "Documenter", "Unitful"]
test = ["Dates", "Documenter", "Test", "Unitful"]
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

InverseFunctions.jl defines an interface to invert functions.

`InverseFunctions` is a very lightweight package and has no dependencies
beyond `Base` and `Test`.
`InverseFunctions` is a very lightweight package and has no dependencies.

## Documentation

Expand Down
2 changes: 1 addition & 1 deletion ext/DatesExt.jl → ext/InverseFunctionsDatesExt.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module DatesExt
module InverseFunctionsDatesExt

using Dates
import InverseFunctions: inverse
Expand Down
17 changes: 17 additions & 0 deletions ext/InverseFunctionsTestExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module InverseFunctionsTestExt

using Test: @test, @testset
using InverseFunctions: InverseFunctions, inverse

function InverseFunctions.test_inverse(f, x; compare=isapprox, kwargs...)
@testset "test_inverse: $f with input $x" begin
y = f(x)
inverse_f = inverse(f)
@test compare(inverse_f(y), x; kwargs...)
inverse_inverse_f = inverse(inverse_f)
@test compare(inverse_inverse_f(x), y; kwargs...)
end
return nothing
end

end
36 changes: 32 additions & 4 deletions src/InverseFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,43 @@ Lightweight package that defines an interface to invert functions.
"""
module InverseFunctions

using Test

include("functions.jl")
include("inverse.jl")
include("setinverse.jl")
include("test.jl")

"""
InverseFunctions.test_inverse(f, x; compare=isapprox, kwargs...)

Test if [`inverse(f)`](@ref) is implemented correctly.

The function tests (as a `Test.@testset`) if

* `compare(inverse(f)(f(x)), x) == true` and
* `compare(inverse(inverse(f))(x), f(x)) == true`.

`kwargs...` are forwarded to `compare`.

!!! Note
On Julia >= 1.9, you have to load the `Test` standard library to be able to use
this function.
"""
function test_inverse end

@static if !isdefined(Base, :get_extension)
include("../ext/DatesExt.jl")
include("../ext/InverseFunctionsDatesExt.jl")
include("../ext/InverseFunctionsTestExt.jl")
end

# Better error message if users forget to load Test
if isdefined(Base, :get_extension) && isdefined(Base.Experimental, :register_error_hint)
function __init__()
Base.Experimental.register_error_hint(MethodError) do io, exc, _, _
if exc.f === test_inverse &&
(Base.get_extension(InverseFunctions, :InverseFunctionsTest) === nothing)
print(io, "\nDid you forget to load Test?")
end
end
end
end

end # module
22 changes: 0 additions & 22 deletions src/test.jl

This file was deleted.

Loading