Nick Heiner
Nick Heiner

Reputation: 122450

List member equality in FsUnit

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.)

Upvotes: 1

Views: 560

Answers (1)

Daniel
Daniel

Reputation: 47904

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.

Upvotes: 5

Related Questions