public void DifferentAddresses() { var a = new Address { Id = 1, Lines = new[] { "123 Fake ST", "Arlington, VA 22222" }, }; var b = new Address { Id = 1, Lines = new[] { "321 Fake ST", "Arlington, VA 22222" }, }; AssertAreNotEqual(a, b); AssertAreEqual(a, b, new ObjectTreeNodeFilter { ExcludedProperties = new[] { ReflectionUtils.Property <Address>(x => x.Lines) }, }); var assertionException = Assert.Throws <AssertionException>(() => ObjectTreeAssert.AreEqual(a, b)); StringAssert.Contains("1 Difference", assertionException.ToString()); StringAssert.Contains("123 Fake ST", assertionException.ToString()); StringAssert.Contains("321 Fake ST", assertionException.ToString()); }
public void DifferentCompanies() { var a = CreateSampleCompany(); var b = CreateSampleCompany(); b.Employees.First().FullName = "Robert Plant"; Assert.AreNotEqual(a, b); // Try a bunch of different ways to filter out the difference so that they are equal. AssertAreEqual(a, b, new ObjectTreeNodeFilter { ExcludedProperties = new[] { ReflectionUtils.Property <Company>(x => x.Employees) } }); AssertAreEqual(a, b, new ObjectTreeNodeFilter { ExcludedProperties = new[] { ReflectionUtils.Property <Employee>(x => x.FullName) } }); AssertAreEqual(a, b, new ObjectTreeNodeFilter { ExcludedProperties = new[] { ReflectionUtils.Property <Person>(x => x.FullName) } }); AssertAreEqual(a, b, new ObjectTreeNodeFilter { ExcludedPropertyNames = new[] { "FullName" } }); AssertAreEqual(a, b, new ObjectTreeNodeFilter { ExcludedPropertyPredicates = new Func <PropertyInfo, bool>[] { (propertyInfo) => propertyInfo.Name.Contains("Name"), } }); AssertAreEqual(a, b, new ObjectTreeNodeFilter { ExcludedNodePredicates = new Func <ObjectTreeNode, bool>[] { (node) => node.ParentNode != null && node.ParentNode.Value is Employee && ((Employee)node.ParentNode.Value).Id == 2 && node.EdgeFromParent != null && node.EdgeFromParent.Member.Name.Contains("Name") } }); AssertAreNotEqual(a, b, new ObjectTreeNodeFilter { ExcludedNodePredicates = new Func <ObjectTreeNode, bool>[] { (node) => node.ParentNode != null && node.ParentNode.Value is Employee && ((Employee)node.ParentNode.Value).Id == 1 && node.EdgeFromParent != null && node.EdgeFromParent.Member.Name.Contains("Name") } }); var assertionException = Assert.Throws <AssertionException>(() => ObjectTreeAssert.AreEqual(a, b)); StringAssert.Contains("1 Difference", assertionException.ToString()); StringAssert.Contains("Robert Paulson", assertionException.ToString()); StringAssert.Contains("Robert Plant", assertionException.ToString()); }
private void AssertAreNotEqual(object expected, object actual, IObjectTreeNodeFilter nodeFilter) { ObjectTreeAssert.AreNotEqual(expected, actual, nodeFilter); Assert.That(actual, IsObjectTree.NotEqualTo(expected).WithFilter(nodeFilter)); Assert.That(actual, Is.Not.ObjectTreeEqualTo(expected).WithFilter(nodeFilter)); Assert.Throws <AssertionException>(() => ObjectTreeAssert.AreEqual(expected, actual, nodeFilter)); Assert.Throws <AssertionException>(() => Assert.That(actual, IsObjectTree.EqualTo(expected).WithFilter(nodeFilter))); }
private void AssertAreNotEqual(object expected, object actual) { ObjectTreeAssert.AreNotEqual(expected, actual); Assert.That(actual, IsObjectTree.NotEqualTo(expected)); Assert.That(actual, Is.Not.ObjectTreeEqualTo(expected)); Assert.Throws <AssertionException>(() => ObjectTreeAssert.AreEqual(expected, actual)); Assert.Throws <AssertionException>(() => Assert.That(actual, IsObjectTree.EqualTo(expected))); }
public void AreEqualFail_DifferentLists() { var expected = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }; var actual = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 }; var expectedException = Assert.Throws <AssertionException>(() => ObjectTreeAssert.AreEqual(expected, actual)); var expectedMessage = " Expected: <LatticeObjectTree.ObjectTree> with root < 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 >" + Environment.NewLine + " But was: <LatticeObjectTree.ObjectTree> with root < 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 >" + Environment.NewLine + " 26 Differences: < <<root>[0]: expected value 0 but was 1.>, <<root>[1]: expected value 1 but was 2.>, <<root>[2]: expected value 2 but was 3.>, <<root>[3]: expected value 3 but was 4.>, <<root>[4]: expected value 4 but was 5.>, <<root>[5]: expected value 5 but was 6.>, <<root>[6]: expected value 6 but was 7.>, <<root>[7]: expected value 7 but was 8.>, <<root>[8]: expected value 8 but was 9.>, <<root>[9]: expected value 9 but was 10.>, <<root>[10]: expected value 10 but was 11.>, <<root>[11]: expected value 11 but was 12.>, <<root>[12]: expected value 12 but was 13.>, <<root>[13]: expected value 13 but was 14.>, <<root>[14]: expected value 14 but was 15.>, <<root>[15]: expected value 15 but was 16.>, <<root>[16]: expected value 16 but was 17.>, <<root>[17]: expected value 17 but was 18.>, <<root>[18]: expected value 18 but was 19.>, <<root>[19]: expected value 19 but was 20.>... >"; Assert.AreEqual(expectedMessage, expectedException.Message); }
public void AreEqualFail_DifferentAnonymousObjects() { var expected = new { id = 1, message = "hello" }; var actual = new { id = 2, message = "world" }; var expectedException = Assert.Throws <AssertionException>(() => ObjectTreeAssert.AreEqual(expected, actual)); var expectedMessage = " Expected: <LatticeObjectTree.ObjectTree> with root <{ id = 1, message = hello }>" + Environment.NewLine + " But was: <LatticeObjectTree.ObjectTree> with root <{ id = 2, message = world }>" + Environment.NewLine + " 2 Differences: < <<root>.id: expected value 1 but was 2.>, <<root>.message: expected value \"hello\" but was \"world\".> >"; Assert.AreEqual(expectedMessage, expectedException.Message); }
public void AreEqualFail_DifferentStrings() { var expected = "hello"; var actual = "world"; var expectedException = Assert.Throws <AssertionException>(() => ObjectTreeAssert.AreEqual(expected, actual)); var expectedMessage = " Expected: <LatticeObjectTree.ObjectTree> with root \"hello\"" + Environment.NewLine + " But was: <LatticeObjectTree.ObjectTree> with root \"world\"" + Environment.NewLine + " 1 Differences: < <<root>: expected value \"hello\" but was \"world\".> >"; Assert.AreEqual(expectedMessage, expectedException.Message); }
public void AreEqualFail_DifferentCustomObjects() { var expected = new Company { Id = 1, Name = "hello" }; var actual = new Company { Id = 2, Name = "world" }; var expectedException = Assert.Throws <AssertionException>(() => ObjectTreeAssert.AreEqual(expected, actual)); var expectedMessage = " Expected: <LatticeObjectTree.ObjectTree> with root <LatticeObjectTree.NUnit.ObjectTreeAssertTest+Company>" + Environment.NewLine + " But was: <LatticeObjectTree.ObjectTree> with root <LatticeObjectTree.NUnit.ObjectTreeAssertTest+Company>" + Environment.NewLine + " 2 Differences: < <<root>.Id: expected value 1 but was 2.>, <<root>.Name: expected value \"hello\" but was \"world\".> >"; Assert.AreEqual(expectedMessage, expectedException.Message); }
public void AreEqual_CompareObjectToItself() { var obj = new object(); ObjectTreeAssert.AreEqual(obj, obj); }