相交扩展方法,区分大小写不起作用

发布于 2024-08-18 05:51:12 字数 1354 浏览 5 评论 0 原文

我正在尝试使用 MSTEST 在 .NET 的单元测试中比较 2 个集合的内容。为了让事情变得简单,我发现了新的、非常酷的 .Intersect 扩展方法,而不必使用 .Sort() 然后循环遍历集合并一次比较一个项目。

通过这样做似乎效果很好:

         Assert.AreEqual(expected.Count, actual.Intersect(expected).Count)

但是,现在我有一个需要区分大小写的测试,它崩溃了。我尝试发送 Intersect 的第二个参数 StringComparer.Ordinal、StringComparer.InvariantCulture 和 StringComparer.CurrentCulture...不走运..

以前有人遇到过这种情况吗?

谢谢!

编辑:这是数据:

 Actual:
    (0) "FOO"   String
    (1) "foo"   String
    (2) "Foo"   String
    (3) "fOo"   String
    (4) "foO"   String
    (5) "BAR"   String
    (6) "BAR"   String
    (7) "BAZ"   String
    (8) "baz"   String
    (9) "foo"   String

 Expected:

    (0) "FOO"   String
    (1) "foo"   String
    (2) "Foo"   String
    (3) "fOo"   String
    (4) "foO"   String
    (5) "BAR"   String
    (6) "BAR"   String
    (7) "BAZ"   String
    (8) "baz"   String
    (9) "foo"   String

 actual.Intersect(expected, StringComparer.CurrentCulture)

    (0) "FOO"   String
    (1) "foo"   String
    (2) "Foo"   String
    (3) "fOo"   String
    (4) "foO"   String
    (5) "BAR"   String
    (6) "BAZ"   String
    (7) "baz"   String

它似乎正在删除匹配的重复“foo”和匹配的重复“BAZ”。也许有更好的方法来断言集合匹配?

_EDIT2:我认为 Intersect() 会删除重复项,这就是它被破坏的原因。我找到了 CollectionAssert 类。这正是我所需要的!谢谢! _

I am trying to compare the contents of 2 collections in a unit test in .NET using MSTEST. To make things simple, instead of having to .Sort() and then loop through the collection and compare items one at a time, I've found the new and very cool .Intersect Extension method.

It seems to work great by doing:

         Assert.AreEqual(expected.Count, actual.Intersect(expected).Count)

However, now that I have a test which needs to be case-sensitive, it breaks. I've tried sending Intersect's second parameter StringComparer.Ordinal,StringComparer.InvariantCulture, and StringComparer.CurrentCulture... no luck..

Anyone experience this before?

thanks!

EDIT: here is the data:

 Actual:
    (0) "FOO"   String
    (1) "foo"   String
    (2) "Foo"   String
    (3) "fOo"   String
    (4) "foO"   String
    (5) "BAR"   String
    (6) "BAR"   String
    (7) "BAZ"   String
    (8) "baz"   String
    (9) "foo"   String

 Expected:

    (0) "FOO"   String
    (1) "foo"   String
    (2) "Foo"   String
    (3) "fOo"   String
    (4) "foO"   String
    (5) "BAR"   String
    (6) "BAR"   String
    (7) "BAZ"   String
    (8) "baz"   String
    (9) "foo"   String

 actual.Intersect(expected, StringComparer.CurrentCulture)

    (0) "FOO"   String
    (1) "foo"   String
    (2) "Foo"   String
    (3) "fOo"   String
    (4) "foO"   String
    (5) "BAR"   String
    (6) "BAZ"   String
    (7) "baz"   String

It seems to be removing a matching duplicate 'foo', and a matching duplicate 'BAZ'. Perhaps there is a better way to assert collections are matching?

_EDIT2: I think Intersect() removes duplicates, which is why this is breaking. I Found the CollectionAssert Class. This is exactly what I needed! Thanks! _

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2024-08-25 05:51:12

出于测试目的,请使用 CollectionAssert.AreEqual(actual, Expected)

您正在寻找 序列相等,而不是相交。 Intersect 返回两个序列的交集,即。他们之间共享的项目。对 { 1, 2, 3 } 和 { 3, 4, 5 } 使用 Intersect 将返回 3。SequenceEqual 将返回 false。

如果您没有找到 CollectionAssert 您可以使用:

Assert.IsTrue(actual.SequenceEqual(expected))

For testing purposes use CollectionAssert.AreEqual(actual, expected)

You were looking for SequenceEqual, not Intersect. Intersect returns the intersection of two sequences, ie. the items shared between them. Using Intersect on { 1, 2, 3 } and { 3, 4, 5 } would return 3. SequenceEqual would return false.

Had you not found CollectionAssert you could've used:

Assert.IsTrue(actual.SequenceEqual(expected))
小女人ら 2024-08-25 05:51:12

像这样实现 IEqualityComparer:


类 StringCaseInsensitiveComparer
   实现 IEqualityComparer(Of String)
   
公共函数 Equals(ByVal s1 As String, ByVal s2 As String) As Boolean
       返回 s1.ToLower() = s2.ToLower()
   结束函数
   
公共函数 GetHashCode(ByVal s As String) As Integer
       返回 s.GetHashCode()
   结束函数
结束课程

然后,调用 Intersect 的重载:

Dim first As IEnumerable(Of TSource)
昏暗的第二个作为 IEnumerable(Of TSource)
暗淡比较器作为 IEqualityComparer(Of TSource)
Dim returnValue As IEnumerable(Of TSource)

向其传递您刚刚创建的比较器的实例。

Implement an IEqualityComparer like so:


Class StringCaseInsensitiveComparer
    Implements IEqualityComparer(Of String)

Public Function Equals(ByVal s1 As String, ByVal s2 As String) As Boolean
        Return s1.ToLower() = s2.ToLower()
    End Function

Public Function GetHashCode(ByVal s As String) As Integer
        Return s.GetHashCode()
    End Function
End Class

Then, call this overload of Intersect:

Dim first As IEnumerable(Of TSource)
Dim second As IEnumerable(Of TSource)
Dim comparer As IEqualityComparer(Of TSource)
Dim returnValue As IEnumerable(Of TSource)

passing it an instance of the comparer that you just made.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文