Skip to content
Open
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion ext/InverseFunctionsTestExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 9 additions & 13 deletions src/InverseFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions test/test_inverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading