Considérons que je pourrais avoir quelques fonctions générées que je veux tester comme ci-dessous. Suivant recommandation du manuel Je les ai fait facultativement généré .
# This code is taken directly from Base.
# https://github.com/JuliaLang/julia/blob/592748adb25301a45bd6edef3ac0a93eed069852/base/namedtuple.jl#L220-L231
# So importing some of the helpers
using Base: merge_names, merge_types, sym_in
function my_merge_fail1(a::NamedTuple{an}, b::NamedTuple{bn}) where {an, bn}
if @generated
names = merge_names(an, bn)
types = merge_types(names, a, b)
vals = Any[ :(getfield($(sym_in(n, bn) ? :b : :a), $(QuoteNode(n)))) for n in names ]
:(error("typo1"); NamedTuple{$names,$types}(($(vals...),)) )
else
names = merge_names(an, bn)
types = merge_types(names, typeof(a), typeof(b))
NamedTuple{names,types}(map(n->getfield(sym_in(n, bn) ? b : a, n), names))
end
end
function my_merge_fail2(a::NamedTuple{an}, b::NamedTuple{bn}) where {an, bn}
if @generated
names = merge_names(an, bn)
types = merge_types(names, a, b)
vals = Any[ :(getfield($(sym_in(n, bn) ? :b : :a), $(QuoteNode(n)))) for n in names ]
:(NamedTuple{$names,$types}(($(vals...),)) )
else
error("typo2")
names = merge_names(an, bn)
types = merge_types(names, typeof(a), typeof(b))
NamedTuple{names,types}(map(n->getfield(sym_in(n, bn) ? b : a, n), names))
end
end
Maintenant, il s'avère que j'ai fait une "coquille", dans chaque. J'ai inclus par erreur error(...)
dans les deux. Sur my_merge_fail1
J'ai fait l'erreur dans le @generated
branche, et dans my_merge_fail2
dans la branche non générée.
Mes tests ne semblent le détecter que dans une seule branche :
julia> using Test
julia> @test my_merge_fail1((a=1,), (b=2,)) == (a=1, b=2)
Error During Test at REPL[12]:1
Test threw exception
Expression: my_merge_fail1((a = 1,), (b = 2,)) == (a = 1, b = 2)
typo1
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] macro expansion at ./REPL[6]:2 [inlined]
[3] my_merge_fail1(::NamedTuple{(:a,),Tuple{Int64}}, ::NamedTuple{(:b,),Tuple{Int64}}) at ./REPL[6]:2
[4] top-level scope at REPL[12]:1
[5] eval(::Module, ::Any) at ./boot.jl:331
[6] eval_user_input(::Any, ::REPL.REPLBackend) at /usr/local/src/julia/julia-master/usr/share/julia/stdlib/v1.4/REPL/src/REPL.jl:86
[7] macro expansion at /usr/local/src/julia/julia-master/usr/share/julia/stdlib/v1.4/REPL/src/REPL.jl:118 [inlined]
[8] (::REPL.var"#26#27"{REPL.REPLBackend})() at ./task.jl:333
ERROR: There was an error during testing
julia> @test my_merge_fail2((a=1,), (b=2,)) == (a=1, b=2)
Test Passed
Comment puis-je améliorer mes tests ?