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.Equivalent failed when there's a Tuple member of the same class type #2758

Closed
vkairy opened this issue Jul 20, 2023 · 1 comment
Closed

Comments

@vkairy
Copy link

vkairy commented Jul 20, 2023

Assert.Equivalent seems to not handle properly when using a Tuple of the same class type.
Below you can see a minimum sample reproducing the problem.

Environment:

  • C# Console App
  • .Net 7
  • xUnit 2.5.0
using System;
using Xunit;
					
public class Program
{
	class Person {
		public Guid Id { get; set; }
		public string Name { get; set; }
		public int Age { get; set; }
		public (Person Mother, Person Father) Parents { get; set; }
	}
	
	public static void Main() {
		Person p1 = new() {
			Id = Guid.NewGuid(),
			Name = "Person #1",
			Age = 30,
			Parents = (new() {
				Id = Guid.NewGuid(),
				Name = "Parent #1",
				Age = 60,
			}, new() {
				Id = Guid.NewGuid(),
				Name = "Parent #2",
				Age = 63,
			})
		};

		Person p2 = new() {
			Id = p1.Id,
			Name = "Person #1",
			Age = 30,
			Parents = (new() {
				Id = p1.Parents.Mother.Id,
				Name = "Parent #1",
				Age = 60,
			}, new() {
				Id = p1.Parents.Father.Id,
				Name = "Parent #2",
				Age = 63,
			})
		};

		Assert.Equivalent(new Person(), new Person()); // Assertion succeeded
		Assert.Equivalent(p1, p2);                     // Assertion failed
	}
}

Failure message:

Unhandled exception. Xunit.Sdk.EquivalentException: Assert.Equivalent() Failure: Mismatched value on member 'Parents'
Expected: Tuple (Person { Age = 60, Id = 8771e6b9-5235-4486-8866-d07622504464, Name = "Parent #1", Parents = Tuple (null, null) }, Person { Age = 63, Id = e597d774-01fd-42a9-8ecc-21bc489d761b, Name = "Parent #2", Parents = Tuple (null, null) })
Actual: Tuple (Person { Age = 60, Id = 8771e6b9-5235-4486-8866-d07622504464, Name = "Parent #1", Parents = Tuple (null, null) }, Person { Age = 63, Id = e597d774-01fd-42a9-8ecc-21bc489d761b, Name = "Parent #2", Parents = Tuple (null, null) })
at Xunit.Assert.Equivalent(Object expected, Object actual, Boolean strict) in /_/src/xunit.assert/Asserts/EquivalenceAsserts.cs:line 41
at Program.Main()
Command terminated by signal 6

Notice: The result is the same if using strict:true.

@bradwilson
Copy link
Member

In v2: 2.5.1-pre.10
In v3: 0.1.1-pre.265

agocke added a commit to agocke/arcade that referenced this issue Oct 11, 2023
…a..46dfaa92

46dfaa92 xunit/xunit#2773: Add Assert.RaisesAny and Assert.RaisesAnyAsync non-generic for EventArgs
c0485bdd Add additional NETSTANDARD excludeions for System.AppDomain usage
b6cb339c xunit/xunit#2767: Verify types match when comparing FileSystemInfo values
03b07513 Use AppDomain.CurrentDomain.GetAssemblies() to find System.IO.FileSystemInfo type
482be8e0 xunit/xunit#2767: Special case FileSystemInfo objects by just comparing the FullName in Assert.Equivalent
78d70dec xunit/xunit#2767: Prevent stack overflow in Assert.Equivalent with a max traversal depth of 50
d0a234a5 Delay calling Dispose() on CollectionTracker's inner enumerator, for xunit/xunit#2762
96236a4c Add disable for CS8607 in AssertEqualityComparerAdapter because of nullability inconsistency
6193f9ee Move to explicit DateTime(Offset) handling in Assert.Equivalent (related: xunit/xunit#2758)
20e76223 xunit/xunit#2743: Assert.Equal with HashSet and custom comparer ignores comparer
247c1016 Mark BitArray as safe to multi-enumerate
8926c0fc Wrap AssertEqualityComparer collection logic in !XUNIT_FRAMEWORK for v2 xunit.execution.*
90d59772 xunit/xunit#2755: Assert.Equal regression for dictionaries with collection values
17c7b611 Add nullable annotation to Assert.NotNull<T>(T?) (dotnet#64)
1886f126 xunit/xunit#2741: Ensure all exception factory methods are public

git-subtree-dir: src/Microsoft.DotNet.XUnitAssert/src
git-subtree-split: 46dfaa9248b7aa4c8c88e5cf6d4a6c84671a93f5
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