mauro3 / SimpleTraits.jl

Simple Traits for Julia
MIT License
159 stars 14 forks source link

How to get code coverage for @traitfn-wrapped functions? #28

Closed sbromberger closed 7 years ago

sbromberger commented 7 years ago

I'm using @traitfn like this:

"""docstring"""
foo(x...) = error("Not implemented")            #1
@traitfn foo{X<:XType; IsMyTrait{X}}(x::X) = ...      #2
@traitfn foo{X<:XType; !IsMyTrait{X}}(x::X) = ...     #3

I can get code coverage for #1, but even when I do real tests on foo (#2 and #3), codecov complains that the method isn't tested.

Is there a recommended way to do this? (Also: is there a better way to associate a docstring other than creating a dummy non-traited function?)

mauro3 commented 7 years ago

This would need something like https://github.com/bramtayl/NumberedLines.jl. I hope to do this at some point but it's not high on my priority list.

However, @bramtayl seems keen to see his package used: https://github.com/bramtayl/NumberedLines.jl/issues/1 . So maybe he'd work with you on a PR...

bramtayl commented 7 years ago

Hmm. I'm not sure I'm seeing what the issue is? It looks to me like the line number info is in the function body (fbody), which gets passed through untouched?

mauro3 commented 7 years ago

Sorry, I got confused with my packages: this package has line number information (thanks to @timholy, see here) but Parameters.jl is lacking it. So, I'm not sure why coverage is not reported correctly but it seems a Julia-0.5 issue, as below tests show. @sbromberger can you check whether it works on 0.6?

Running:

# julia --code-coverage coverage-test.jl
using SimpleTraits, Base.Test
@traitdef Tr{X}
@traitimpl Tr{Int}

foo() = 1
@traitfn foo(x::::Tr) = 2
@traitfn foo(x::::(!Tr)) = 3

@test foo()==1
@test foo(1)==2
@test foo(1.0)==3

Julia 0.5 produces no .cov file at all. Conversely, running with 0.6 a .cov file is produced:

        - # julia --code-coverage coverage-test.jl
        - using SimpleTraits, Base.Test
        - @traitdef Tr{X}
        - @traitimpl Tr{Int}
        -
        1 foo() = 1
        2 @traitfn foo(x::::Tr) = 2
        2 @traitfn foo(x::::(!Tr)) = 3
        -
        - @test foo()==1
        - @test foo(1)==2
        - @test foo(1.0)==3

Not 100% right as it over-reports at least the second line. In fact, running a slightly different script shows the problem a bit better:

        - # julia --code-coverage coverage-test.jl
        - using SimpleTraits, Base.Test
        - @traitdef Tr{X}
        - @traitimpl Tr{Int}
        -
        1 foo() = 1
        2 @traitfn foo(x::::Tr) = 2
        - @traitfn function foo(x::::(!Tr))
        4     println("here") # over-reporting
        2     println("here") # over-reporting
        2     println("here") # correct
        2     if x>0
        1         return 3
        -     else
        1         return -3
        -     end
        - end
        -
        -
        - @test foo()==1
        - @test foo(1)==2
        - @test foo(1.0)==3
        - @test foo(-1.0)==-3
mauro3 commented 7 years ago

@sbromberger did you check whether Julia-0.6 gives better coverage results?

sbromberger commented 7 years ago

@mauro3 I won't be able to do that for a while since we haven't migrated this branch to have compatibility with 0.6.

mauro3 commented 7 years ago

Ok. I don't think there is anything for me to do here (and I certainly wouldn't know what). So, I'll close this. But if you figure something out, a PR is welcome.