From 7930a0ddd5fb3b40df5164cef48aabd7593f1ca4 Mon Sep 17 00:00:00 2001 From: stepan Date: Wed, 6 Nov 2024 13:46:34 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=BE=D0=B5=20=D1=80=D0=B5=D1=88=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BB=D1=83=D1=87=D1=88=D0=B5=20CheckCurr?= =?UTF-8?q?entTsar=5FWithCustomEquality=20=D0=B2=20=D1=81=D0=BB=D0=B5?= =?UTF-8?q?=D0=B4=D1=83=D1=8E=D1=89=D0=B8=D1=85=20=D0=B0=D1=81=D0=BF=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=B0=D1=85:=20=D0=BF=D1=80=D0=B8=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D1=8B=D1=85=20=D0=BF=D0=BE=D0=BB=D0=B5=D0=B9=20=D0=B2=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20Person=20=D0=BD=D0=B5=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D0=B4=D0=B5=D1=82=D1=81=D1=8F=20=D0=B2=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D0=B8=D1=82=D1=8C=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B9=20=D0=B2=20=D0=BA=D0=BE=D0=B4=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B8=D1=80=D1=83=D1=8E=D1=89=D0=B5=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0;=20=D0=BA=D1=80=D0=BE?= =?UTF-8?q?=D0=BC=D0=B5=20=D1=82=D0=BE=D0=B3=D0=BE=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D1=81=D1=80=D0=B0=D0=B2=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BE?= =?UTF-8?q?=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2=20Person=20=D0=B8?= =?UTF-8?q?=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D1=83=D0=B5=D1=82=D1=81?= =?UTF-8?q?=D1=8F=20=D0=B1=D0=BE=D0=BB=D0=B5=D0=B5=20=D1=87=D0=B8=D1=82?= =?UTF-8?q?=D0=B0=D0=B5=D0=BC=D1=8B=D0=B9=20=D1=81=D0=B8=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=D0=BA=D1=81=D0=B8=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1. ObjectComparison/ObjectComparison.cs | 14 +++----------- .../Homework/1. ObjectComparison/Person.cs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..81245d5 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using FluentAssertions; +using NUnit.Framework; using NUnit.Framework.Legacy; namespace HomeExercise.Tasks.ObjectComparison; @@ -14,16 +15,7 @@ public void CheckCurrentTsar() var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, new Person("Vasili III of Russia", 28, 170, 60, null)); - // Перепишите код на использование Fluent Assertions. - ClassicAssert.AreEqual(actualTsar.Name, expectedTsar.Name); - ClassicAssert.AreEqual(actualTsar.Age, expectedTsar.Age); - ClassicAssert.AreEqual(actualTsar.Height, expectedTsar.Height); - ClassicAssert.AreEqual(actualTsar.Weight, expectedTsar.Weight); - - ClassicAssert.AreEqual(expectedTsar.Parent!.Name, actualTsar.Parent!.Name); - ClassicAssert.AreEqual(expectedTsar.Parent.Age, actualTsar.Parent.Age); - ClassicAssert.AreEqual(expectedTsar.Parent.Height, actualTsar.Parent.Height); - ClassicAssert.AreEqual(expectedTsar.Parent.Parent, actualTsar.Parent.Parent); + actualTsar.Should().Be(expectedTsar); } [Test] diff --git a/Testing/Basic/Homework/1. ObjectComparison/Person.cs b/Testing/Basic/Homework/1. ObjectComparison/Person.cs index 4846867..a4e45f1 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/Person.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/Person.cs @@ -18,4 +18,23 @@ public Person(string name, int age, int height, int weight, Person parent) Weight = weight; Parent = parent; } + + public override bool Equals(object obj) + { + if (obj is Person person) return this.GetHashCode() == person.GetHashCode(); + return false; + } + + public int GetHashCode() + { + var result = 0; + var degree = 0; + foreach (var field in GetType().GetFields()) + { + if (field.IsStatic || field.Name == "Id") continue; + result += (int)Math.Pow(field.GetValue(this).GetHashCode(), ++degree); + } + + return result; + } } \ No newline at end of file