Skip to content

Commit

Permalink
Make Test a weak dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Aug 10, 2024
1 parent 5f6b4da commit 6cc1001
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 32 deletions.
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.

2 comments on commit 6cc1001

@ChrisRackauckas
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@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/112881

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v0.1.16 -m "<description of version>" 6cc1001fb718bd45fc59468555d0292eace0819c
git push origin v0.1.16

Please sign in to comment.