mardi 29 octobre 2019

How do I test both sides of a `if @generated`?

Consider I might have some generated functions I want to test as below. Following recommendation of the manual I made them optionally generated.

# 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

Now, it turns out I made a "typo", in each. I mistakenly included error(...) in both of them. In my_merge_fail1 I made the mistike in the @generated branch, and in my_merge_fail2 in the nongenerated branch.

My tests seem to only catch it in one branch:

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

How can I improve my tests?

Aucun commentaire:

Enregistrer un commentaire