I often want to test my things are resturning the type I want.
The sensible way to do so would be:
@test result isa FooType.
But that does not give a good error message on failure.
So instead I write:
@test result |> typeof <: FooType,
which will, on a failure output the type that it actually was
Sometimes I could just be using @inferred for this.
This is simialr to
https://github.com/JuliaLang/julia/issues/25486
https://github.com/JuliaLang/julia/issues/25487
You can make your own:
macro cooltest(ex)
if ex.head === :call && ex.args[1] === :isa
a, b = ex.args[2:3]
return :(@test typeof($a) <: $b)
else
return :(@test $ex)
end
end
which produces output like
julia> @cooltest 1 isa String
Test Failed at REPL[2]:4
Expression: typeof(1) <: String
Evaluated: Int64 <: String
ERROR: There was an error during testing
Personally I don't think we should do any syntactic modifications in @test itself to make sure that the test that's run faithfully represents what was written.
We do a lot of that already:
isa:julia> using Test
julia> result = Int
Int64
julia> @test result isa AbstractString
Test Failed at REPL[3]:1
Expression: result isa AbstractString
Evaluated: Int64 isa AbstractString
ERROR: There was an error during testing
Most helpful comment
We do a lot of that already:
https://github.com/JuliaLang/julia/blob/de705f3b6961a288c0b79d2f88010597e846faa8/stdlib/Test/src/Test.jl#L358-L411