Is there a way to force a wrong dispatch for test purposes?

Hi,

Suppose I have two classes and a function as follows:

abstract struct A end
struct B<:A end

and then the function

f(x::A) = true
f(x::B) = true

The function is supposed to return the same thing in both cases, the method f(x::B) being only a performance improvement w.r.t. f(x::A). To ensure that the functions are returning the same things, is there a way i could force, in my testing, the dispatch on x = B() to forget about the second method and use the first one ?

I believe you’re looking for invoke.

  invoke(f, argtypes::Type, args...; kwargs...)

  Invoke a method for the given generic function f matching the specified types argtypes on the specified
  arguments args and passing the keyword arguments kwargs. The arguments args must conform with the
  specified types in argtypes, i.e. conversion is not automatically performed. This method allows invoking a
  method other than the most specific matching method, which is useful when the behavior of a more general
  definition is explicitly needed (often as part of the implementation of a more specific method of the same
  function).
4 Likes

Exactly what i wanted. Thanks ! So here’s the test I wanted:

x = B()
@test f(x) == invoke(f, Tuple{A}, x)

There’s also a macro form that you may or may not prefer:

x = B()
@test f(x) == @invoke f(x::A)
2 Likes