Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert.Equal failing for value-slot collections of different concrete types #2850

Closed
FlorianHockmann opened this issue Dec 20, 2023 · 4 comments

Comments

@FlorianHockmann
Copy link

One of our tests starts failing in version 2.6.2 and 2.6.3, but it still worked in 2.6.1 (and versions before that).
The test compares two collections which contain a Dictionary that contains collections as its values. One of these nested collections is an object[] whereas the other is a List<object>.

This is a minimal example to reproduce the problem:

var expected = new List<object> { new Dictionary<object, object> { { 1, new List<object> { 2 } } } };
var actual = new object[] { new Dictionary<object, object> { { 1, new object[] { 2 } } } };

Assert.Equal(expected, actual);

It fails with:

Assert.Equal() Failure: Exception thrown during comparison
                        ↓ (pos 0)
Expected: List<object> [[[1] = [2]]]
Actual:   object[]     [[[1] = [2]]]
                        ↑ (pos 0)

System.InvalidCastException
Unable to cast object of type 'System.Object[]' to type 'System.Collections.Generic.List`1[System.Object]'.

The InvalidCastException comes from the nested collection in the Dictionary, not the outer collection.

@bradwilson
Copy link
Member

bradwilson commented Dec 20, 2023

Bummer. That means the fix for #2821 broke this. Not sure what the fix is yet.

Edit: I've determined what this is, fix is incoming.

@bradwilson bradwilson changed the title Assert.Equal failing for nested object[] and List<object> since 2.6.2 Assert.Equal failing for value-slot collections of different concrete types Dec 20, 2023
@bradwilson
Copy link
Member

This also affects both keys and values for comparing KeyValuePair<K,V>.

bradwilson added a commit to xunit/assert.xunit that referenced this issue Dec 20, 2023
…erent concrete types (also fixed for KeyValuePair keys and values)
bradwilson added a commit that referenced this issue Dec 20, 2023
bradwilson added a commit that referenced this issue Dec 20, 2023
@bradwilson
Copy link
Member

Available in v2: 2.6.4-pre.5
Available in v3: 0.1.1-pre.334

https://xunit.net/docs/using-ci-builds

@FlorianHockmann
Copy link
Author

Thanks a lot for the really fast response and fix! 😃

ViktorHofer added a commit to dotnet/arcade that referenced this issue Jan 19, 2024
…a6..141681779

141681779 Missed #nullable enable in AsyncCollectionAsserts
22c89b0ea xunit/xunit#2367: Add IAsyncEnumerable<> overloads (.NET Core 3.0+)
d5c32630a While formatting type names in Assert.Equal/NotEqual, convert generated type names to '<generated>'
d7b807179 Add platform conditionals to support .NET 6 Roslyn Analyzers
6d9024665 xunit/xunit#2850: Assert.Equal failing value-slot collections of different concrete types (also fixed for KeyValuePair keys and values)
1f66b837a xunit/xunit#2811: Add SortedSet and ImmutableSortedSet overloads for Assert.Contains/DoesNotContain
1dab747d3 Update FuncEqualityComparer to throw if GetHashCode is called, and update EqualException/NotEqualException to process it
6e0a7cd70 xunit/xunit#2828: Prefer IEquatable<> over custom collection equality
c35ef46d5 xunit/xunit#2824: Assert.Equal fails with null values in dictionary
455865ac8 xunit/xunit#2821: Assert.Equal for collections of IEquatable objects don't call custom Equals
9af2c9c12 Clarify names for range comparer
2e6d9b267 Updates for .NET 8 SDK

git-subtree-dir: src/Microsoft.DotNet.XUnitAssert/src
git-subtree-split: 141681779e7638887a2ba711ad5407c67b6efe32
ViktorHofer added a commit to dotnet/arcade that referenced this issue Mar 28, 2024
…a6..574aebac4

574aebac4 xunit/xunit#2872: Expand special handling for sets in Assert.Contains/DoesNotContain
3b8edcbf1 xunit/xunit#2880: Update XML documentation for string-based Assert.Equal
d9f8361d2 Consolidate string and span-based Assert.Equal primary implementation
9ad71163e Move span-of-char assertions to StringAsserts and update docs/param names to indicate they're treated like strings
d70b34621 xunit/xunit#2871: Inner exception stack trace is missing from Assert.Collection failure
141681779 Missed #nullable enable in AsyncCollectionAsserts
22c89b0ea xunit/xunit#2367: Add IAsyncEnumerable<> overloads (.NET Core 3.0+)
d5c32630a While formatting type names in Assert.Equal/NotEqual, convert generated type names to '<generated>'
d7b807179 Add platform conditionals to support .NET 6 Roslyn Analyzers
6d9024665 xunit/xunit#2850: Assert.Equal failing value-slot collections of different concrete types (also fixed for KeyValuePair keys and values)
1f66b837a xunit/xunit#2811: Add SortedSet and ImmutableSortedSet overloads for Assert.Contains/DoesNotContain
1dab747d3 Update FuncEqualityComparer to throw if GetHashCode is called, and update EqualException/NotEqualException to process it
6e0a7cd70 xunit/xunit#2828: Prefer IEquatable<> over custom collection equality
c35ef46d5 xunit/xunit#2824: Assert.Equal fails with null values in dictionary
455865ac8 xunit/xunit#2821: Assert.Equal for collections of IEquatable objects don't call custom Equals
9af2c9c12 Clarify names for range comparer
2e6d9b267 Updates for .NET 8 SDK

git-subtree-dir: src/Microsoft.DotNet.XUnitAssert/src
git-subtree-split: 574aebac41dbbbf9e5e98bb9c65c6c5fab9b47f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants