Skip to content

Commit 6cc1001

Browse files
authored
Make Test a weak dependency
1 parent 5f6b4da commit 6cc1001

6 files changed

+57
-32
lines changed

Project.toml

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
name = "InverseFunctions"
22
uuid = "3587e190-3f89-42d0-90ee-14403ec27112"
3-
version = "0.1.15"
3+
version = "0.1.16"
44

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

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

1213
[extensions]
13-
DatesExt = "Dates"
14+
InverseFunctionsDatesExt = "Dates"
15+
InverseFunctionsTestExt = "Test"
1416

1517
[compat]
1618
julia = "1"
1719

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

2326
[targets]
24-
test = ["Dates", "Documenter", "Unitful"]
27+
test = ["Dates", "Documenter", "Test", "Unitful"]

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
InverseFunctions.jl defines an interface to invert functions.
1111

12-
`InverseFunctions` is a very lightweight package and has no dependencies
13-
beyond `Base` and `Test`.
12+
`InverseFunctions` is a very lightweight package and has no dependencies.
1413

1514
## Documentation
1615

ext/DatesExt.jl renamed to ext/InverseFunctionsDatesExt.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module DatesExt
1+
module InverseFunctionsDatesExt
22

33
using Dates
44
import InverseFunctions: inverse

ext/InverseFunctionsTestExt.jl

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module InverseFunctionsTestExt
2+
3+
using Test: @test, @testset
4+
using InverseFunctions: InverseFunctions, inverse
5+
6+
function InverseFunctions.test_inverse(f, x; compare=isapprox, kwargs...)
7+
@testset "test_inverse: $f with input $x" begin
8+
y = f(x)
9+
inverse_f = inverse(f)
10+
@test compare(inverse_f(y), x; kwargs...)
11+
inverse_inverse_f = inverse(inverse_f)
12+
@test compare(inverse_inverse_f(x), y; kwargs...)
13+
end
14+
return nothing
15+
end
16+
17+
end

src/InverseFunctions.jl

+32-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,43 @@ Lightweight package that defines an interface to invert functions.
66
"""
77
module InverseFunctions
88

9-
using Test
10-
119
include("functions.jl")
1210
include("inverse.jl")
1311
include("setinverse.jl")
14-
include("test.jl")
12+
13+
"""
14+
InverseFunctions.test_inverse(f, x; compare=isapprox, kwargs...)
15+
16+
Test if [`inverse(f)`](@ref) is implemented correctly.
17+
18+
The function tests (as a `Test.@testset`) if
19+
20+
* `compare(inverse(f)(f(x)), x) == true` and
21+
* `compare(inverse(inverse(f))(x), f(x)) == true`.
22+
23+
`kwargs...` are forwarded to `compare`.
24+
25+
!!! Note
26+
On Julia >= 1.9, you have to load the `Test` standard library to be able to use
27+
this function.
28+
"""
29+
function test_inverse end
1530

1631
@static if !isdefined(Base, :get_extension)
17-
include("../ext/DatesExt.jl")
32+
include("../ext/InverseFunctionsDatesExt.jl")
33+
include("../ext/InverseFunctionsTestExt.jl")
34+
end
35+
36+
# Better error message if users forget to load Test
37+
if isdefined(Base, :get_extension) && isdefined(Base.Experimental, :register_error_hint)
38+
function __init__()
39+
Base.Experimental.register_error_hint(MethodError) do io, exc, _, _
40+
if exc.f === test_inverse &&
41+
(Base.get_extension(InverseFunctions, :InverseFunctionsTest) === nothing)
42+
print(io, "\nDid you forget to load Test?")
43+
end
44+
end
45+
end
1846
end
1947

2048
end # module

src/test.jl

-22
This file was deleted.

0 commit comments

Comments
 (0)