在FsUnit中列出成员相等性(List member equality in FsUnit)

编程入门 行业动态 更新时间:2024-10-17 11:29:32
在FsUnit中列出成员相等性(List member equality in FsUnit)

我想检查两个列表是否具有相同的成员,而不管顺序如何:

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

最满意答案

您的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.

更多推荐

本文发布于:2023-08-03 09:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1387034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:成员   FsUnit   List   equality   member

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!