From 96f42c90923f381329c664a837824003ac6bf79e Mon Sep 17 00:00:00 2001 From: Ilya4rh Date: Wed, 6 Nov 2024 14:28:01 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=B7-1=20=D0=A7=D0=B5=D1=80=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=98=D0=BB=D1=8C=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cs/HomeExercises/NumberValidatorTests.cs | 61 +++++++++++++++++------- cs/HomeExercises/ObjectComparison.cs | 20 ++++---- cs/testing.sln.DotSettings | 3 ++ 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index a2878113..3788f4d4 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -8,25 +8,54 @@ namespace HomeExercises public class NumberValidatorTests { [Test] - public void Test() + public void Constructor_ShouldThrowArgumentException_WhenPrecisionIsNegative() + { + Action action = () => new NumberValidator(-1, 2, true); + + action.Should().Throw() + .WithMessage("precision must be a positive number"); + } + + [Test] + public void Constructor_ShouldThrowArgumentException_WhenScaleIsNegative() + { + Action act = () => new NumberValidator(3, -1, true); + + act.Should().Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + + [Test] + public void Constructor_ShouldThrowArgumentException_WhenScaleExceedsThePrecision() + { + Action act = () => new NumberValidator(1, 4, true); + + act.Should().Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + + [Test] + public void Constructor_ShouldNotThrowExceprion_WhenInputValuesAreValid() { - 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)); + } + + [TestCase(4, 3, true, "", false)] + [TestCase(4, 3, true, null, false)] + [TestCase(3, 2, true, "a.sd", false)] + [TestCase(3, 2, true, "00.00", false)] + [TestCase(17, 2, true, "0.000", false)] + [TestCase(7, 3, true, "-1.23", false)] + [TestCase(17, 2, true, "0.0", true)] + [TestCase(17, 2, true, "0", true)] + [TestCase(4, 2, true, "+1.23", true)] + public void IsValidNumberTest(int precision, int scale, bool onlyPositive, string value, bool expectedResult) + { + var numberValidator = new NumberValidator(precision, scale, onlyPositive); + + var isValidNumber = numberValidator.IsValidNumber(value); - Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); - Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0")); - Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("00.00")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-0.00")); - Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+0.00")); - Assert.IsTrue(new NumberValidator(4, 2, true).IsValidNumber("+1.23")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+1.23")); - Assert.IsFalse(new NumberValidator(17, 2, true).IsValidNumber("0.000")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-1.23")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("a.sd")); + isValidNumber.Should().Be(expectedResult); } } diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index 44d9aed4..deda3fd5 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -15,16 +15,11 @@ 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. - Assert.AreEqual(actualTsar.Name, expectedTsar.Name); - Assert.AreEqual(actualTsar.Age, expectedTsar.Age); - Assert.AreEqual(actualTsar.Height, expectedTsar.Height); - Assert.AreEqual(actualTsar.Weight, expectedTsar.Weight); - - Assert.AreEqual(expectedTsar.Parent!.Name, actualTsar.Parent!.Name); - Assert.AreEqual(expectedTsar.Parent.Age, actualTsar.Parent.Age); - Assert.AreEqual(expectedTsar.Parent.Height, actualTsar.Parent.Height); - Assert.AreEqual(expectedTsar.Parent.Parent, actualTsar.Parent.Parent); + actualTsar.Should().BeEquivalentTo(expectedTsar, options => + options.Excluding(person => person.Id) + .Excluding(person => person.Parent.Id) + .AllowingInfiniteRecursion() + ); } [Test] @@ -36,6 +31,11 @@ public void CheckCurrentTsar_WithCustomEquality() new Person("Vasili III of Russia", 28, 170, 60, null)); // Какие недостатки у такого подхода? + // 1) При добавлении нового поля/свойства в класс нужно будет добавлять его проверку в метод AreEqual, + // если этого не сделать, то тест будет выполнять проверку не верно. В моём решении при добавлении поля/свойства + // тест менять не нужно (нужно менять только если мы хотим исключить проверку на это поле/свойство) + // 2) Если поле/свойство будет иметь тип класс, то нужно будет добавлять проверку на этот класс по полям/свойства + // 3) Если тест сломается, то будет неочевидно на каком именно поле/свойстве расхождение Assert.True(AreEqual(actualTsar, expectedTsar)); } diff --git a/cs/testing.sln.DotSettings b/cs/testing.sln.DotSettings index 46ba8b5d..414826a0 100644 --- a/cs/testing.sln.DotSettings +++ b/cs/testing.sln.DotSettings @@ -11,12 +11,15 @@ False <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + <Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></Policy> + <Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Types and namespaces"><ElementKinds><Kind Name="NAMESPACE" /><Kind Name="CLASS" /><Kind Name="STRUCT" /><Kind Name="ENUM" /><Kind Name="DELEGATE" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy> True True True True True True + True True True True \ No newline at end of file