From a4b4e097197b03f7e3206d242129c84e7f4e881a Mon Sep 17 00:00:00 2001 From: Foma Medvedev Date: Mon, 27 Oct 2025 17:50:14 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=82=D0=B5=D1=81=D1=82=20=D0=B2=20ObjectComparison,=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=B2=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83=20=D1=87=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B7=20FluentAssertions.BeEquivalentTo()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Переделал тест в NumberValidatorTests, сделав его через генераторы и имена для тестов, добавил своих --- Testing/.idea/.idea.Testing/.idea/.gitignore | 13 +++ .../.idea/.idea.Testing/.idea/indexLayout.xml | 8 ++ Testing/.idea/.idea.Testing/.idea/vcs.xml | 6 + .../1. ObjectComparison/ObjectComparison.cs | 28 ++--- .../Homework/1. ObjectComparison/Person.cs | 4 +- .../1. ObjectComparison/TsarRegistry.cs | 4 +- .../NumberValidatorTests.cs | 104 +++++++++++++++--- Testing/Testing.sln.DotSettings.user | 8 ++ 8 files changed, 137 insertions(+), 38 deletions(-) create mode 100644 Testing/.idea/.idea.Testing/.idea/.gitignore create mode 100644 Testing/.idea/.idea.Testing/.idea/indexLayout.xml create mode 100644 Testing/.idea/.idea.Testing/.idea/vcs.xml create mode 100644 Testing/Testing.sln.DotSettings.user diff --git a/Testing/.idea/.idea.Testing/.idea/.gitignore b/Testing/.idea/.idea.Testing/.idea/.gitignore new file mode 100644 index 0000000..2fe203b --- /dev/null +++ b/Testing/.idea/.idea.Testing/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/projectSettingsUpdater.xml +/contentModel.xml +/modules.xml +/.idea.Testing.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Testing/.idea/.idea.Testing/.idea/indexLayout.xml b/Testing/.idea/.idea.Testing/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/Testing/.idea/.idea.Testing/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Testing/.idea/.idea.Testing/.idea/vcs.xml b/Testing/.idea/.idea.Testing/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Testing/.idea/.idea.Testing/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..ff4c9c6 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; @@ -10,20 +11,12 @@ public class ObjectComparison public void CheckCurrentTsar() { var actualTsar = TsarRegistry.GetCurrentTsar(); - - 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); + var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, 99999999, + new Person("Vasili III of Russia", 28, 170, 60, 99999999, null)); + + actualTsar.Should().BeEquivalentTo(expectedTsar, config => config.Excluding(x => x.Id).Excluding(x => x.Parent.Id)); + // Я специально для теста добавил одно поле, а сам тест остался неизменен. По сути, его нужно изменять только в случае, если нужно добавить какое-то поле в игнор + // типа того, как я добавил айдишники людей, т. е. там скорее всего окажутся только поля, которые должны быть уникальными для каждого объекта, а таких вряд ли будет прям много } [Test] @@ -31,10 +24,11 @@ public void CheckCurrentTsar() public void CheckCurrentTsar_WithCustomEquality() { var actualTsar = TsarRegistry.GetCurrentTsar(); - var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, - new Person("Vasili III of Russia", 28, 170, 60, null)); + var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, 99999999, + new Person("Vasili III of Russia", 28, 170, 60, 99999999, null)); // Какие недостатки у такого подхода? + // Этот код более читаем, чем был изначальный вариант в первом тесте, но его необходимо будет изменять каждый раз, когда мы будем менять класс, который тестируем ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); } diff --git a/Testing/Basic/Homework/1. ObjectComparison/Person.cs b/Testing/Basic/Homework/1. ObjectComparison/Person.cs index 4846867..3ddabf6 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/Person.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/Person.cs @@ -5,11 +5,12 @@ public class Person { public static int IdCounter = 0; public int Age, Height, Weight; + public uint Wealth; public string Name; public Person Parent; public int Id; - public Person(string name, int age, int height, int weight, Person parent) + public Person(string name, int age, int height, int weight, uint wealth, Person parent) { Id = IdCounter++; Name = name; @@ -17,5 +18,6 @@ public Person(string name, int age, int height, int weight, Person parent) Height = height; Weight = weight; Parent = parent; + Wealth = wealth; } } \ No newline at end of file diff --git a/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs b/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs index f852e90..31f5f84 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs @@ -5,7 +5,7 @@ public class TsarRegistry public static Person GetCurrentTsar() { return new Person( - "Ivan IV The Terrible", 54, 170, 70, - new Person("Vasili III of Russia", 28, 170, 60, null)); + "Ivan IV The Terrible", 54, 170, 70, 99999999, + new Person("Vasili III of Russia", 28, 170, 60, 99999999, null)); } } \ No newline at end of file diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 950c9bc..50abcdf 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,4 +1,5 @@  +using FluentAssertions; using NUnit.Framework; using NUnit.Framework.Legacy; @@ -7,25 +8,92 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [Test] - public void Test() + [Test, TestCaseSource(nameof(GenerateValidatorTestsWithExceptions))] + public void Tests_NumberValidator_WithException(int precision, int scale, bool onlyPositive) { - Assert.Throws(() => new NumberValidator(-1, 2, true)); - Assert.DoesNotThrow(() => new NumberValidator(1, 0, true)); - Assert.Throws(() => new NumberValidator(-1, 2, false)); - Assert.DoesNotThrow(() => new NumberValidator(1, 0, true)); + var validatorAction = () => new NumberValidator(precision, scale, onlyPositive); + validatorAction.Should().Throw(); + } + + public static IEnumerable GenerateValidatorTestsWithExceptions + { + get + { + var tests = new[] + { + new { precision = -1, scale = 2, onlyPositive = true}, + new { precision = -1, scale = 2, onlyPositive = false}, + new { precision = 1, scale = -1, onlyPositive = false}, + new { precision = 1, scale = 10, onlyPositive = false}, + }; + foreach (var test in tests) + { + yield return new TestCaseData(test.precision, test.scale, test.onlyPositive) + .SetName($"Exception should be thrown with {test.precision} precision, {test.scale} scale, {test.onlyPositive} onlyPositive"); + } + } + } + + [Test, TestCaseSource(nameof(GenerateValidatorTestsWithoutExceptions))] + public void Tests_NumberValidator_WithoutException(int precision, int scale, bool onlyPositive) + { + var validatorAction = () => new NumberValidator(precision, scale, onlyPositive); + validatorAction.Should().NotThrow(); + } - ClassicAssert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); - ClassicAssert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0")); - ClassicAssert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("00.00")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-0.00")); - ClassicAssert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+0.00")); - ClassicAssert.IsTrue(new NumberValidator(4, 2, true).IsValidNumber("+1.23")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+1.23")); - ClassicAssert.IsFalse(new NumberValidator(17, 2, true).IsValidNumber("0.000")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-1.23")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("a.sd")); + public static IEnumerable GenerateValidatorTestsWithoutExceptions + { + get + { + var tests = new[] + { + new { precision = 1, scale = 0, onlyPositive = true}, + }; + foreach (var test in tests) + { + yield return new TestCaseData(test.precision, test.scale, test.onlyPositive) + .SetName($"Exception should be thrown with {test.precision} precision, {test.scale} scale, {test.onlyPositive} onlyPositive"); + } + } + } + + [Test, TestCaseSource(nameof(GenerateValidatorTestIsValidNumberMethod))] + public bool ValidatorTest_IsValidNumber_Method(int precision, int scale, bool onlyPositive, string validatingNumber) + { + var numberValidator = new NumberValidator(precision, scale, onlyPositive); + return numberValidator.IsValidNumber(validatingNumber); + } + + public static IEnumerable GenerateValidatorTestIsValidNumberMethod + { + get + { + var tests = new[] + { + new { precision = 17, scale = 2, onlyPositive = true, validatingNumber = "0.0", expectingResult = true}, + new { precision = 17, scale = 2, onlyPositive = true, validatingNumber = "0", expectingResult = true}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "00.00", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "-0.00", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "+0.00", expectingResult = false}, + new { precision = 4, scale = 2, onlyPositive = true, validatingNumber = "1.23", expectingResult = true}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "+1.23", expectingResult = false}, + new { precision = 17, scale = 2, onlyPositive = true, validatingNumber = "0.000", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "-1.23", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "a.sd", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "0,0", expectingResult = true}, + new { precision = 17, scale = 2, onlyPositive = false, validatingNumber = "+-1.23", expectingResult = false}, + new { precision = 17, scale = 2, onlyPositive = false, validatingNumber = "-1.23", expectingResult = true}, + new { precision = 17, scale = 2, onlyPositive = true, validatingNumber = ".23", expectingResult = false}, + new { precision = 17, scale = 2, onlyPositive = true, validatingNumber = "23.", expectingResult = false}, + }; + foreach (var test in tests) + { + yield return new TestCaseData(test.precision, test.scale, test.onlyPositive, test.validatingNumber) + .Returns(test.expectingResult) + .SetName($"Test with precision: {test.precision}, scale: {test.scale}, onlyPositive: {test.onlyPositive}," + + $"validatingNumber: {test.validatingNumber} should return {test.expectingResult}, but returned {!test.expectingResult}"); + } + } } } \ No newline at end of file diff --git a/Testing/Testing.sln.DotSettings.user b/Testing/Testing.sln.DotSettings.user new file mode 100644 index 0000000..fb8a275 --- /dev/null +++ b/Testing/Testing.sln.DotSettings.user @@ -0,0 +1,8 @@ + + ForceIncluded + ForceIncluded + <SessionState ContinuousTestingMode="0" IsActive="True" Name="NumberValidatorTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>NUnit3x::6ED454CB-A772-43E5-B72D-3FE9DA27337F::net8.0::HomeExercise.Tasks.NumberValidator.NumberValidatorTests</TestId> + </TestAncestor> +</SessionState> \ No newline at end of file From f3ab5be637f0dbc42e0db38f8e57e0763932016a Mon Sep 17 00:00:00 2001 From: Foma Medvedev Date: Thu, 30 Oct 2025 12:35:11 +0500 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9E=D1=82=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=20=D1=81=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B2=20ObjectCompariso?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Basic/Homework/1. ObjectComparison/ObjectComparison.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index ff4c9c6..410c656 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -14,7 +14,11 @@ public void CheckCurrentTsar() var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, 99999999, new Person("Vasili III of Russia", 28, 170, 60, 99999999, null)); - actualTsar.Should().BeEquivalentTo(expectedTsar, config => config.Excluding(x => x.Id).Excluding(x => x.Parent.Id)); + actualTsar.Should() + .BeEquivalentTo(expectedTsar, + config => config + .Excluding(person => person.Id) + .Excluding(person => person.Parent.Id)); // Я специально для теста добавил одно поле, а сам тест остался неизменен. По сути, его нужно изменять только в случае, если нужно добавить какое-то поле в игнор // типа того, как я добавил айдишники людей, т. е. там скорее всего окажутся только поля, которые должны быть уникальными для каждого объекта, а таких вряд ли будет прям много }