elm-test icon indicating copy to clipboard operation
elm-test copied to clipboard

No easy way to compare lists (or other datasets) in a manner other than exact equals

Open JonathanFraser opened this issue 8 years ago • 4 comments

As the title says, take for example the case where I would like to compare a List Float or similar case. There is no method to reduce: List Expectation -> Expectation or customListEquals: (a -> a -> Expectation) -> List a -> List a -> Expectation

JonathanFraser avatar Sep 07 '17 20:09 JonathanFraser

@JonathanFraser can you show us some code (or pseudocode) of what you'd like to be able to do?

rtfeldman avatar Sep 08 '17 04:09 rtfeldman

Specifically I was implementing some numeric code (DFT), this type of transform operates in a matrix multiplication type fashion. Moreover, it uses complex numbers so I need a custom data type to capture the pair. The type signature of the function in question is: dft: List Complex -> List Complex There is a corresponding inverse (idft) with the same signature and I need to validate the property dft x |> idft == x but in the floating point sense as opposed to the exact equals sense.

I would like to be able to compare the two lists with a custom operator, something like: complexCompare: Complex -> Complex -> Expectation

(\x -> 
  dft x 
   |> idft
   |> customListCompare complexCompare x
)

or something similar that allows for the same properties testing but with a datastructure that won't obey exact equality.

JonathanFraser avatar Sep 08 '17 16:09 JonathanFraser

It sounds to me like you want a functions of the sort existing in this module: https://github.com/NoRedInk/elm-formatted-text/blob/master/tests/EqualCheck.elm

Granted, the functions there's not a EqualCheck a -> EqualCheck (List a) in there, but it would be easy to imagine it can be added.

I've also been in the position where I felt I needed to build a custom equality function to go along with a custom type I created, for writing tests around that type. I'm not a hundred percent sure yet the approach taken in the module linked above is the best, but it might be nice to experiment with it, perhaps build a library for specifically that purpose.

jwoudenberg avatar Sep 09 '17 07:09 jwoudenberg

I have this problem too. I have two lists and want to check that one is a subset of the other (with some slight domain-specific complexity as well). I have a predicate I can use to test the subset part, but I lack the ability to make an Expectation with the two inputs. Right now I'm doing:

  if (Util.containsOrdered expected ops)
  then Expect.pass
  else Expect.equal expected ops
       |> Expect.onFail "not equal: these should be containsOrdered"

pbiggar avatar Oct 23 '17 22:10 pbiggar