YingboMa / SafeTestsets.jl

Other
79 stars 6 forks source link

Bug in @safetestset macro, confusing object name and symbol? #14

Open jonalm opened 9 months ago

jonalm commented 9 months ago

This might be a StructTypes issue, but I believe it belongs here. Consider the following module:

module Foo

using StructTypes
using JSON3

export Bar, Baz

struct Baz
    bom::Float64
end

StructTypes.StructType(::Type{Baz}) = StructTypes.Struct()

struct Bar{T}
    type::Symbol
    data::T
end
Bar(x) = Bar{typeof(x)}(Symbol(typeof(x)), x)

StructTypes.StructType(::Type{Bar}) = StructTypes.AbstractType()
StructTypes.StructType(::Type{<:Bar}) = StructTypes.Struct()
StructTypes.subtypes(::Type{Bar}) = (;
    Int64 = Bar{Int64}, 
    Baz = Bar{Baz} # this nt causes problems with the @safetest macro; lookup tries to find Foo.Baz not the symbol :Baz
)
Bar(s::String) = JSON3.read(s, Bar)

end # module Foo

with the corresponding test file

# test.jl
using Test
using Foo
using JSON3

x = Bar(123)
y = Bar(Baz(1.23))
@test x == Bar(JSON3.write(x))
@test y == Bar(JSON3.write(y)) # this line fails when running inside @safetestset macro

This file runs as expected, but running inside @safetestset like below, gives error

using SafeTestsets
@safetestset "test" begin
    include("test.jl")
end

yields

...
test: Error During Test at /Users/jonalmeriksen/dev/Foo/test/test.jl:8
  Test threw exception
  Expression: y == Bar(JSON3.write(y))
  type NamedTuple has no field Foo.Baz
  Stacktrace:
...
Test Summary: | Pass  Error  Total  Time
test          |    1      1      2  1.9s
ERROR: LoadError: Some tests did not pass: 1 passed, 0 failed, 1 errored, 0 broken.
in expression starting at /Users/jonalmeriksen/dev/Foo/test/runtests.jl:3
ERROR: Package Foo errored during testing