How can I get useful diff messeges when comparing tuples?

Im just spooling up with Julia, and Im having difficulty getting useful diff-msgs on some of the tests I am writing, when using ExtendedTestSet. The problem seems to be related to the type of vectors/tuples i am comparing. Sometimes I can get a diff, othertimes I get nothing!

I think the problem is that there is not a diff function defined for the types I am trying to compare, but I am unsure how to define one.

How can I make my own diff methods, and structure my own output for them?

Here is a simple example:

using Test
using TestSetExtensions

@testset ExtendedTestSet "unit test for inside_ranges" begin

    # not useful diff msg
    @test (2,3) == (3,4)

    # sort of useful diff msg
    @test [2,3] == [3,4]
    
end

gives:

=====================================================

unit test for inside_ranges: **Test Failed**

Expression: (2, 3) == (3, 4)

Diff:

nothing
=====================================================

=====================================================
unit test for inside_ranges: **Test Failed**

Expression: [2, 3] == [3, 4]

Diff:

[2, 3, 4]
=====================================================

If you broadcast the comparison, i. e. (2,3) .== (3,4) you get:

  Expression evaluated to non-Boolean
  Expression: (2, 3) .== (3, 4)
       Value: (false, false)

Or you can get the same error message you get with arrays converting the tuples into arrays:

@test [(2,3)...] == [(3,4)...]
1 Like

Thanks. Thats helpful. I’d still like to code my own diff though. Is it possible to do that?

Probably you can, you should check how to do that in the documentation of TestSetExtensions. What you certainly can is define your own function to compare tuples, like, for instance:

julia> function diff_tuple(x,y)
          diff = Tuple[]
          for i in eachindex(x)
            if x[i] != y[i]
              push!(diff,((i,x[i]=>y[i])))
            end
          end
          if length(diff) == 0
            return true
          end
          return diff
       end
diff_tuple (generic function with 1 method)

julia> @test diff_tuple((2,3),(2,3))
Test Passed

julia> @test diff_tuple((2,3),(3,4))
Error During Test at REPL[65]:1
  Expression evaluated to non-Boolean
  Expression: diff_tuple((2, 3), (3, 4))
       Value: Tuple[(1, 2 => 3), (2, 3 => 4)]

The error message here indicates which elements changed, at which position, and from what to what.

Edit: It didn’t seem to me that the package was written to be really extendable. An inner change was needed. I have added a pull request here in which the tuples are expanded into vectors and the error message is printed as such:

Not very elegant, but better than printing nothing.