Unit tests for interfaces

Is there a convenient way to unit test support for interfaces in Base? I am thinking of something like

test_interface_array(my_array_like_object)

that would test that the type is <: AbstractArray, query size and indices, do a bit of consistency checking with eltype etc.

Similarly for iterators and indexing. I thought I would check before writing one. Or not writing one :smiley: I keep repeating code for this in my own package unit tests.

1 Like
1 Like

I had some time to look into it now, and at this stage InterfaceTesting.jl only checks that some methods exist, not what they actually do. The API functions actually take a type, not an object. So while it is a step in the right direction, its usefulness is limited.

Feel Free to raise issues,
or submit PRs.

My main initial goal with InterfaceTesting was to stop errors where iteratoreltypewas declared on a object, not a Type,
Which is why I started from there.
It tests the interface type contract, when you look for the analogy to Java/C# interfaces.
Which basically means asserting that the right methods exist.

I can certainly see it being reasonable to include some sanity checks.
Something which requires that a typical instance of the item being tested be passed in.
and then checks certain invariants hold.
Though working out what they are is nontrivial.

Some ideas:

  • eltype(first(x))<:eltype(x) seems reasonable for all iterators. possibly even doing something like checking the first up to 100.
  • x[first(indices(x))] === first(x)
  • x[last(indices(x))] === last(x)
  • length(x) = *(size(x)....)
  • probably some checks around collect

Anyway, make some issues/PRs.

Will do! Thanks for starting the package.