diff --git a/Project.toml b/Project.toml index b6acb6f..bd50f06 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "InverseFunctions" uuid = "3587e190-3f89-42d0-90ee-14403ec27112" -version = "0.1.17" +version = "0.1.18" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/ext/InverseFunctionsTestExt.jl b/ext/InverseFunctionsTestExt.jl index c130074..f6b4062 100644 --- a/ext/InverseFunctionsTestExt.jl +++ b/ext/InverseFunctionsTestExt.jl @@ -3,7 +3,7 @@ module InverseFunctionsTestExt using Test: @test, @testset using InverseFunctions: InverseFunctions, inverse -function InverseFunctions.test_inverse(f, x; compare=isapprox, kwargs...) +function InverseFunctions._test_inverse(f, x; compare=isapprox, kwargs...) @testset "test_inverse: $f with input $x" begin y = f(x) inverse_f = inverse(f) diff --git a/src/InverseFunctions.jl b/src/InverseFunctions.jl index 4cffde1..e84895a 100644 --- a/src/InverseFunctions.jl +++ b/src/InverseFunctions.jl @@ -26,23 +26,19 @@ The function tests (as a `Test.@testset`) if On Julia >= 1.9, you have to load the `Test` standard library to be able to use this function. """ -function test_inverse end +function test_inverse(f, x; kwargs...) + hasmethod(_test_inverse, Tuple{Any,Any}) || throw(ArgumentError( + "InverseFunctions.test_inverse requires the Test standard library: `using Test`." + )) + return _test_inverse(f, x; kwargs...) +end + +# implemented by InverseFunctionsTestExt +function _test_inverse end @static if !isdefined(Base, :get_extension) 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/test/test_inverse.jl b/test/test_inverse.jl index 79cf725..3a5b232 100644 --- a/test/test_inverse.jl +++ b/test/test_inverse.jl @@ -43,6 +43,27 @@ end InverseFunctions.test_inverse(inverse, log, compare = ===) end +@testset "test_inverse" begin + @test !isdefined(InverseFunctions, :__init__) + + err = try + InverseFunctions.test_inverse(identity) + catch e + e + end + @test err isa MethodError + @test !occursin("requires the Test standard library", sprint(showerror, err)) + + # Test can't be unloaded, so the missing-Test error is checked in a subprocess + if isdefined(Base, :get_extension) + script = "using InverseFunctions; InverseFunctions.test_inverse(identity, 1)" + cmd = `$(Base.julia_cmd()) --startup-file=no --project=$(Base.active_project()) -e $script` + output = IOBuffer() + run(pipeline(ignorestatus(cmd); stdout=output, stderr=output)) + @test occursin("ArgumentError: InverseFunctions.test_inverse requires the Test standard library", String(take!(output))) + end +end + @testset "maths" begin InverseFunctions.test_inverse(!, false)