From 1c75d103029fe8caaad02868aa5af26c5f3bc7a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D0=B2=D0=B5=D0=BB=D0=B8=D0=B9?= Date: Sun, 3 Nov 2024 23:46:41 +0500 Subject: [PATCH 1/6] did NumberValidator --- .../NumberValidatorTests.cs | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 950c9bc..6d016c8 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,31 +1,48 @@ - -using NUnit.Framework; -using NUnit.Framework.Legacy; +using NUnit.Framework; namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [Test] - public void Test() + [TestCase(-1,2,true,ExpectedResult = true,TestName = "Negative precision")] + [TestCase(1,0,true,ExpectedResult = false,TestName = "Not catch Exception")] + [TestCase(2,5,true,ExpectedResult = true,TestName = "Precision less then scale")] + [TestCase(2,-3,true,ExpectedResult = true,TestName = "Negative scale")] + public bool NumberValidator_Exceptions_Test(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)); + try + { + new NumberValidator(precision, scale, onlyPositive); + } + catch (ArgumentException) + { + return true; + } + + return false; + } - 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")); + [Test, TestCaseSource(nameof(NumberValidatorTestCases))] + public bool IsValidNumber(NumberValidator numberValidator, String number) + { + return numberValidator.IsValidNumber(number); + } + + public static IEnumerable NumberValidatorTestCases + { + get + { + yield return new TestCaseData(new NumberValidator(8, 2, true), "0.0").Returns(true); + yield return new TestCaseData(new NumberValidator(3, 0, true), "0").Returns(true); + yield return new TestCaseData(new NumberValidator(4, 2, true), "-1.23").Returns(false); + yield return new TestCaseData(new NumberValidator(4, 2, false), "-1.24").Returns(true); + yield return new TestCaseData(new NumberValidator(5, 2, true), "1.253").Returns(false); + yield return new TestCaseData(new NumberValidator(3, 2, false), "a.ds").Returns(false); + yield return new TestCaseData(new NumberValidator(4, 2, true), "").Returns(false); + yield return new TestCaseData(new NumberValidator(5, 2, true), "+-1.25").Returns(false); + yield return new TestCaseData(new NumberValidator(5, 2, true), null).Returns(false); + yield return new TestCaseData(new NumberValidator(3, 0, true), "1234").Returns(false); + } } } \ No newline at end of file From a266c8c6388ad5e00118cb878771200c8a3cf84e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D0=B2=D0=B5=D0=BB=D0=B8=D0=B9?= Date: Mon, 4 Nov 2024 00:10:06 +0500 Subject: [PATCH 2/6] did ObjectComparison --- .../1. ObjectComparison/ObjectComparison.cs | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..313b2b7 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; @@ -13,17 +14,13 @@ 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().BeEquivalentTo(expectedTsar, options => options. + Excluding(x => x.Id). + Excluding(x=>x.Parent)); + + expectedTsar.Parent.Should().BeEquivalentTo(actualTsar.Parent, options => options. + Excluding(x =>x.Id)); } [Test] @@ -36,6 +33,17 @@ public void CheckCurrentTsar_WithCustomEquality() // Какие недостатки у такого подхода? ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); + /* + * Недостатки: + * 1. Такая реализация теста не даёт никакой конкретики при обвале теста, + * он скажет только Success или Failed, но не причину, а точнее мы не узнаем + * какое конкретно поле класса Person показало несовпадение, в отличие от моего решения. + * 2. Масштабируемость. Метод AreEqual работает только для текущей реализации Person, + * и при его изменении (добавление или удаление полей), нужно будет добавлять/убирать строки + * и в AreEqual. Также можно и вовсе обойтись без него, что заметно повысит читаемость кода. + * 3. Ликвидность. Если нам потребуется сравнить каких-нибудь двух Person без определённого поля, + * тогда в текущем классе потребуется несколько реализаций AreEqual, что очень плохо. + */ } private bool AreEqual(Person? actual, Person? expected) From ef5b983ec47708f2f24d34ddc730b6e277200dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D0=B2=D0=B5=D0=BB=D0=B8=D0=B9?= Date: Fri, 8 Nov 2024 16:27:58 +0500 Subject: [PATCH 3/6] Refactor ObjectComparison.cs --- .../1. ObjectComparison/ObjectComparison.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index 313b2b7..32daf1a 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -3,24 +3,24 @@ using NUnit.Framework.Legacy; namespace HomeExercise.Tasks.ObjectComparison; + public class ObjectComparison { [Test] [Description("Проверка текущего царя")] [Category("ToRefactor")] - public void CheckCurrentTsar() + public void TsarRegistry_ShouldBeEqual_WithCurrentTsar() { 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)); - - actualTsar.Should().BeEquivalentTo(expectedTsar, options => options. - Excluding(x => x.Id). - Excluding(x=>x.Parent)); - - expectedTsar.Parent.Should().BeEquivalentTo(actualTsar.Parent, options => options. - Excluding(x =>x.Id)); + + actualTsar.Should().BeEquivalentTo(expectedTsar, + options => options + .Excluding(x => x.Path.EndsWith("Id")) + .AllowingInfiniteRecursion() + .IncludingProperties()); } [Test] @@ -57,4 +57,4 @@ private bool AreEqual(Person? actual, Person? expected) && actual.Weight == expected.Weight && AreEqual(actual.Parent, expected.Parent); } -} +} \ No newline at end of file From 4e7e771e1f4bb42e38eaeda08587ff9a4d80ffdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D0=B2=D0=B5=D0=BB=D0=B8=D0=B9?= Date: Fri, 8 Nov 2024 16:27:58 +0500 Subject: [PATCH 4/6] Refactor ObjectComparison.cs --- .../1. ObjectComparison/ObjectComparison.cs | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index 313b2b7..9e1dafa 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -3,24 +3,24 @@ using NUnit.Framework.Legacy; namespace HomeExercise.Tasks.ObjectComparison; + public class ObjectComparison { [Test] [Description("Проверка текущего царя")] [Category("ToRefactor")] - public void CheckCurrentTsar() + public void TsarRegistry_ShouldBeEqual_WithCurrentTsar() { 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)); - - actualTsar.Should().BeEquivalentTo(expectedTsar, options => options. - Excluding(x => x.Id). - Excluding(x=>x.Parent)); - - expectedTsar.Parent.Should().BeEquivalentTo(actualTsar.Parent, options => options. - Excluding(x =>x.Id)); + + actualTsar.Should().BeEquivalentTo(expectedTsar, + options => options + .Excluding(x => x.Path.EndsWith("Id")) + .AllowingInfiniteRecursion() + .IncludingProperties()); } [Test] @@ -38,10 +38,7 @@ public void CheckCurrentTsar_WithCustomEquality() * 1. Такая реализация теста не даёт никакой конкретики при обвале теста, * он скажет только Success или Failed, но не причину, а точнее мы не узнаем * какое конкретно поле класса Person показало несовпадение, в отличие от моего решения. - * 2. Масштабируемость. Метод AreEqual работает только для текущей реализации Person, - * и при его изменении (добавление или удаление полей), нужно будет добавлять/убирать строки - * и в AreEqual. Также можно и вовсе обойтись без него, что заметно повысит читаемость кода. - * 3. Ликвидность. Если нам потребуется сравнить каких-нибудь двух Person без определённого поля, + * 2. Ликвидность. Если нам потребуется сравнить каких-нибудь двух Person без определённого поля, * тогда в текущем классе потребуется несколько реализаций AreEqual, что очень плохо. */ } @@ -57,4 +54,4 @@ private bool AreEqual(Person? actual, Person? expected) && actual.Weight == expected.Weight && AreEqual(actual.Parent, expected.Parent); } -} +} \ No newline at end of file From 49844bf953a48220ea00cef13a3aaeecece92457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D0=B2=D0=B5=D0=BB=D0=B8=D0=B9?= Date: Fri, 8 Nov 2024 17:22:04 +0500 Subject: [PATCH 5/6] Refactor NumberValidatorTests.cs --- .../NumberValidatorTests.cs | 71 ++++++++++--------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 6d016c8..c945e1c 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,48 +1,55 @@ -using NUnit.Framework; +using FluentAssertions; +using NUnit.Framework; namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [TestCase(-1,2,true,ExpectedResult = true,TestName = "Negative precision")] - [TestCase(1,0,true,ExpectedResult = false,TestName = "Not catch Exception")] - [TestCase(2,5,true,ExpectedResult = true,TestName = "Precision less then scale")] - [TestCase(2,-3,true,ExpectedResult = true,TestName = "Negative scale")] - public bool NumberValidator_Exceptions_Test(int precision, int scale, bool onlyPositive) + [TestCase(-1, 2, true, TestName = "Negative precision")] + [TestCase(2, 5, true, TestName = "Precision less then scale")] + [TestCase(2, -3, true, TestName = "Negative scale")] + [TestCase(-5, -3, true, TestName = "Negative scale and Precision less then scale ")] + [TestCase(null, null, null, TestName = "Null input data")] + public void NumberValidator_ShouldThrowArgumentException_WithIncorrectData(int precision, int scale, + bool onlyPositive) { - try - { - new NumberValidator(precision, scale, onlyPositive); - } - catch (ArgumentException) - { - return true; - } - - return false; + Action act = () => new NumberValidator(precision, scale, onlyPositive); + act.Should().Throw(); } - [Test, TestCaseSource(nameof(NumberValidatorTestCases))] - public bool IsValidNumber(NumberValidator numberValidator, String number) + [Test] + public void NumberValidator_ShouldNotThrowArgumentException_WithCorrectData() { + Action act = () => new NumberValidator(1, 0, true); + act.Should().NotThrow(); + } + + [TestCase(8, 2, true, "0.0", ExpectedResult = true)] + [TestCase(3, 0, true, "0", ExpectedResult = true)] + [TestCase(4, 2, false, "-1.24", ExpectedResult = true)] + [TestCase(4, 2, false, "-1.24", ExpectedResult = true)] + public bool NumberValidator_ShouldReturnTrue_WithCorrectData(int precision, int scale, bool onlyPositive, + String number) + { + NumberValidator numberValidator = new NumberValidator(precision, scale, onlyPositive); return numberValidator.IsValidNumber(number); } - public static IEnumerable NumberValidatorTestCases + [TestCase(4, 2, true, "-1.23", ExpectedResult = false)] + [TestCase(5, 2, true, "1.253", ExpectedResult = false)] + [TestCase(3, 0, false, "a.ds", ExpectedResult = false)] + [TestCase(4, 2, false, "", ExpectedResult = false)] + [TestCase(5, 2, true, "+-1.25", ExpectedResult = false)] + [TestCase(5, 2, true, null, ExpectedResult = false)] + [TestCase(3, 0, true, "1234", ExpectedResult = false)] + [TestCase(5, 2, true, "\r", ExpectedResult = false)] + [TestCase(3, 0, true, "1 23", ExpectedResult = false)] + [TestCase(3, 0, true, " 2.3", ExpectedResult = false)] + public bool NumberValidator_ShouldReturnFalse_WithInCorrectData(int precision, int scale, bool onlyPositive, + String number) { - get - { - yield return new TestCaseData(new NumberValidator(8, 2, true), "0.0").Returns(true); - yield return new TestCaseData(new NumberValidator(3, 0, true), "0").Returns(true); - yield return new TestCaseData(new NumberValidator(4, 2, true), "-1.23").Returns(false); - yield return new TestCaseData(new NumberValidator(4, 2, false), "-1.24").Returns(true); - yield return new TestCaseData(new NumberValidator(5, 2, true), "1.253").Returns(false); - yield return new TestCaseData(new NumberValidator(3, 2, false), "a.ds").Returns(false); - yield return new TestCaseData(new NumberValidator(4, 2, true), "").Returns(false); - yield return new TestCaseData(new NumberValidator(5, 2, true), "+-1.25").Returns(false); - yield return new TestCaseData(new NumberValidator(5, 2, true), null).Returns(false); - yield return new TestCaseData(new NumberValidator(3, 0, true), "1234").Returns(false); - } + NumberValidator numberValidator = new NumberValidator(precision, scale, onlyPositive); + return numberValidator.IsValidNumber(number); } } \ No newline at end of file From 467e3e3af9dd6e9620b370f36883385e0f42760e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D0=B2=D0=B5=D0=BB=D0=B8=D0=B9?= Date: Mon, 11 Nov 2024 17:49:55 +0500 Subject: [PATCH 6/6] Refactor NumberValidatorTests.cs --- .../2. NumberValidator/NumberValidatorTests.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index c945e1c..58d8564 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -3,16 +3,14 @@ namespace HomeExercise.Tasks.NumberValidator; -[TestFixture] public class NumberValidatorTests { [TestCase(-1, 2, true, TestName = "Negative precision")] [TestCase(2, 5, true, TestName = "Precision less then scale")] [TestCase(2, -3, true, TestName = "Negative scale")] [TestCase(-5, -3, true, TestName = "Negative scale and Precision less then scale ")] - [TestCase(null, null, null, TestName = "Null input data")] - public void NumberValidator_ShouldThrowArgumentException_WithIncorrectData(int precision, int scale, - bool onlyPositive) + public void NumberValidator_ShouldThrowArgumentException_WithIncorrectData( + int precision, int scale, bool onlyPositive) { Action act = () => new NumberValidator(precision, scale, onlyPositive); act.Should().Throw(); @@ -29,8 +27,8 @@ public void NumberValidator_ShouldNotThrowArgumentException_WithCorrectData() [TestCase(3, 0, true, "0", ExpectedResult = true)] [TestCase(4, 2, false, "-1.24", ExpectedResult = true)] [TestCase(4, 2, false, "-1.24", ExpectedResult = true)] - public bool NumberValidator_ShouldReturnTrue_WithCorrectData(int precision, int scale, bool onlyPositive, - String number) + public bool NumberValidator_ShouldReturnTrue_WithCorrectData( + int precision, int scale, bool onlyPositive, string number) { NumberValidator numberValidator = new NumberValidator(precision, scale, onlyPositive); return numberValidator.IsValidNumber(number); @@ -46,8 +44,8 @@ public bool NumberValidator_ShouldReturnTrue_WithCorrectData(int precision, int [TestCase(5, 2, true, "\r", ExpectedResult = false)] [TestCase(3, 0, true, "1 23", ExpectedResult = false)] [TestCase(3, 0, true, " 2.3", ExpectedResult = false)] - public bool NumberValidator_ShouldReturnFalse_WithInCorrectData(int precision, int scale, bool onlyPositive, - String number) + public bool NumberValidator_ShouldReturnFalse_WithInCorrectData( + int precision, int scale, bool onlyPositive, string number) { NumberValidator numberValidator = new NumberValidator(precision, scale, onlyPositive); return numberValidator.IsValidNumber(number);