Skip to content

Commit dea98a0

Browse files
committed
Use @testinferred
1 parent 77bb1c1 commit dea98a0

File tree

3 files changed

+10
-68
lines changed

3 files changed

+10
-68
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Random = "1"
1919
Reexport = "1"
2020
TensorOperations = "5"
2121
Test = "1"
22-
TestExtras = "0.3"
22+
TestExtras = "0.3.2"
2323
WignerSymbols = "1,2"
2424

2525
[extras]

test/runtests.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ foreach(SectorTestSuite.test_sector, sectorlist)
4848
a = first(smallset(I1))
4949
b = first(smallset(I2))
5050

51-
@constinferred a b
52-
@constinferred a b a
53-
@constinferred a b a b
54-
@constinferred I1 I2
51+
@testinferred a b
52+
@testinferred a b a
53+
@testinferred a b a b
54+
@testinferred I1 I2
5555
@test typeof(a b) == I1 I2
5656

57-
@test @constinferred(length(allunits(I1 I2))) == 1
58-
@test @constinferred(unit(I1 I2)) == leftunit(a b) == rightunit(a b)
57+
@test @testinferred(length(allunits(I1 I2))) == 1
58+
@test @testinferred(unit(I1 I2)) == leftunit(a b) == rightunit(a b)
5959
end
60-
@test @constinferred(Tuple(SU2Irrep(1) U1Irrep(0))) == (SU2Irrep(1), U1Irrep(0))
61-
@test @constinferred(length(FermionParity(1) SU2Irrep(1 // 2) U1Irrep(1))) == 3
60+
@test @testinferred(Tuple(SU2Irrep(1) U1Irrep(0))) == (SU2Irrep(1), U1Irrep(0))
61+
@test @testinferred(length(FermionParity(1) SU2Irrep(1 // 2) U1Irrep(1))) == 3
6262
end
6363

6464
@testset "Issue that came up in #11" begin

test/testsuite.jl

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ macro testsuite(name, ex)
4848
fn = Symbol("test_$(safe_name)")
4949
return quote
5050
$(esc(fn))(I) = $(esc(ex))(I)
51-
@assert !haskey(tests, $name)
51+
@assert !haskey(tests, $name) "duplicate testsuite name: $name"
5252
tests[$name] = $fn
5353
end
5454
end
@@ -69,64 +69,6 @@ function test_sector(I::Type)
6969
end
7070
end
7171

72-
"""
73-
@testinferred [AllowedTypes] ex
74-
75-
Like `Test.@inferred`, but registers failures through a test, rather than an error.
76-
"""
77-
macro testinferred(ex)
78-
return _inferred(ex, __module__)
79-
end
80-
macro testinferred(ex, allow)
81-
return _inferred(ex, __module__, allow)
82-
end
83-
84-
# Implementation copied from Test._inferred:
85-
function _inferred(ex, mod, allow = :(Union{}))
86-
if Meta.isexpr(ex, :ref)
87-
ex = Expr(:call, :getindex, ex.args...)
88-
end
89-
Meta.isexpr(ex, :call)|| error("@testinferred requires a call expression")
90-
farg = ex.args[1]
91-
if isa(farg, Symbol) && farg !== :.. && first(string(farg)) == '.'
92-
farg = Symbol(string(farg)[2:end])
93-
ex = Expr(
94-
:call, GlobalRef(Test, :_materialize_broadcasted),
95-
farg, ex.args[2:end]...
96-
)
97-
end
98-
result = let ex = ex
99-
quote
100-
let allow = $(esc(allow))
101-
allow isa Type || throw(ArgumentError("@inferred requires a type as second argument"))
102-
$(
103-
if any(@nospecialize(a) -> (Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex.args)
104-
# Has keywords
105-
# Create the call expression with escaped user expressions
106-
call_expr = :($(esc(ex.args[1]))(args...; kwargs...))
107-
quote
108-
args, kwargs, result = $(esc(Expr(:call, _args_and_call, ex.args[2:end]..., ex.args[1])))
109-
# wrap in dummy hygienic-scope to work around scoping issues with `call_expr` already having `esc` on the necessary parts
110-
inftype = $(Expr(:var"hygienic-scope", Base.gen_call_with_extracted_types(mod, Base.infer_return_type, call_expr; is_source_reflection = false), Test))
111-
end
112-
else
113-
# No keywords
114-
quote
115-
args = ($([esc(ex.args[i]) for i in 2:length(ex.args)]...),)
116-
result = $(esc(ex.args[1]))(args...)
117-
inftype = Base.infer_return_type($(esc(ex.args[1])), Base.typesof(args...))
118-
end
119-
end
120-
)
121-
rettype = result isa Type ? Type{result} : typeof(result)
122-
@test rettype <: allow || rettype == Base.typesplit(inftype, allow)
123-
result
124-
end
125-
end
126-
end
127-
return Base.remove_linenums!(result)
128-
end
129-
13072
smallset(::Type{I}) where {I <: Sector} = take(values(I), 5)
13173
function smallset(::Type{ProductSector{Tuple{I1, I2}}}) where {I1, I2}
13274
iter = product(smallset(I1), smallset(I2))

0 commit comments

Comments
 (0)