1

I want to check that two lists have the same members, irrespective of order:

let memCount items = items |> Seq.countBy id |> Map.ofSeq
let memberEquals items1 items2 = memCount items1 = memCount items2

Currently, I use this in a test as follows:

memberEquals expected actual |> should be True

However, this is not quite as nice for error reporting.

Can I extend FsUnit to add a memberEquals similar to equals or contains? Alternatively, I could just always sort the lists before comparing them. What is the best approach here?

(I am using FsUnit with nUnit, for what it's worth.)

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
  • It's these sorts of non-trivial scenarios where you'll see Unquote really begin to shine, simply because F# itself so excels at expressing these types of assertions. See https://gist.github.com/2919920. – Stephen Swensen Jun 12 '12 at 20:27

1 Answers1

5

Your memberEquals function doesn't test if the lists have the same members (only the same number of items). The easiest way to do that is:

let memberEquals items1 items2 = (set items1 = set items2)

If the lists may contain duplicates you'll need to sort and compare them as lists instead.

You can read about the set function on MSDN.

Daniel
  • 47,404
  • 11
  • 101
  • 179