我想检查两个列表是否具有相同的成员,而不管顺序如何:
let memCount items = items |> Seq.countBy id |> Map.ofSeq let memberEquals items1 items2 = memCount items1 = memCount items2目前,我在测试中使用它如下:
memberEquals expected actual |> should be True但是,这对于错误报告来说并不是那么好。
我可以扩展FsUnit以添加类似于equals或contains的memberEquals吗? 或者,我可以在比较它们之前总是对列表进行排序。 这里最好的方法是什么?
(我正在使用带有nUnit的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 items2Currently, I use this in a test as follows:
memberEquals expected actual |> should be TrueHowever, 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.)
最满意答案
您的memberEquals函数不会测试列表是否具有相同的成员(只有相同数量的项目)。 最简单的方法是:
let memberEquals items1 items2 = (set items1 = set items2)如果列表可能包含重复项,则需要对它们进行排序并将它们作为列表进行比较。
†您可以在MSDN上阅读有关set功能的信息。
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.
更多推荐
发布评论