ANN: MethodInSrc.jl ; verify that your method is invoked

Example: Test that sum(m) uses a method defined in the module I am testing, but that prod(m) uses a generic method:

@test @isinsrc sum(m)
@test  ! @isinsrc prod(m) 

Example: Test sum(m) == 1, but throw an error if the method invoked is not in the module being tested.

@test 1 == @insrc sum(m)

I have needed this for a while, so I wrote it yesterday.

10 Likes

Kinda sorry for bumping my own post, but it occurred to me that the motivation might be a bit mysterious. So I added a motivation section in the README. I reproduce it here.

Motivation

Some code provides efficient, specialized methods for particular data types. It’s important that the input, the output, and the function call look exactly as they did before implementing the efficient method. So the test shouldn’t change:

@test sum(A) = n

How then, apart from benchmarking, do you test your new implementation ? This module is a step towards a solution.

The problem becomes more complicated, and more error prone, if you have a file defining binary operations, say multiplication, for combinations of various particular and abstract types. Are you sure dispatch is occurring as you intend ? Furthermore, due to changes in other code, someone may remove a specialized method that has become redundant, or add a new one. When reading this code (even if you changed it yourself last year) and you can’t find method, did it go missing, or was removed intentionally ? “… wait a minute, there’s a test for that missing method in the test suite, and it’s passing!”
You get the idea.

2 Likes