From 6cc1001fb718bd45fc59468555d0292eace0819c Mon Sep 17 00:00:00 2001 From: David Widmann Date: Sat, 10 Aug 2024 23:24:14 +0200 Subject: [PATCH] Make Test a weak dependency --- Project.toml | 9 +++-- README.md | 3 +- ...atesExt.jl => InverseFunctionsDatesExt.jl} | 2 +- ext/InverseFunctionsTestExt.jl | 17 +++++++++ src/InverseFunctions.jl | 36 ++++++++++++++++--- src/test.jl | 22 ------------ 6 files changed, 57 insertions(+), 32 deletions(-) rename ext/{DatesExt.jl => InverseFunctionsDatesExt.jl} (94%) create mode 100644 ext/InverseFunctionsTestExt.jl delete mode 100644 src/test.jl diff --git a/Project.toml b/Project.toml index 0313697..23a09fe 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "InverseFunctions" uuid = "3587e190-3f89-42d0-90ee-14403ec27112" -version = "0.1.15" +version = "0.1.16" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" @@ -8,9 +8,11 @@ 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" @@ -18,7 +20,8 @@ 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"] diff --git a/README.md b/README.md index c9e9695..c292c12 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/ext/DatesExt.jl b/ext/InverseFunctionsDatesExt.jl similarity index 94% rename from ext/DatesExt.jl rename to ext/InverseFunctionsDatesExt.jl index 38a153f..c999184 100644 --- a/ext/DatesExt.jl +++ b/ext/InverseFunctionsDatesExt.jl @@ -1,4 +1,4 @@ -module DatesExt +module InverseFunctionsDatesExt using Dates import InverseFunctions: inverse diff --git a/ext/InverseFunctionsTestExt.jl b/ext/InverseFunctionsTestExt.jl new file mode 100644 index 0000000..c130074 --- /dev/null +++ b/ext/InverseFunctionsTestExt.jl @@ -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 diff --git a/src/InverseFunctions.jl b/src/InverseFunctions.jl index 3710fbe..51496ac 100644 --- a/src/InverseFunctions.jl +++ b/src/InverseFunctions.jl @@ -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 diff --git a/src/test.jl b/src/test.jl deleted file mode 100644 index 684d5bf..0000000 --- a/src/test.jl +++ /dev/null @@ -1,22 +0,0 @@ -""" - 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`. -""" -function 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