From 5b8dcf7d61750b4e60586ef9c537c9465afc3244 Mon Sep 17 00:00:00 2001 From: Arman Mukanov Date: Mon, 27 Nov 2023 18:02:47 +0500 Subject: [PATCH 1/4] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20Fluent?= =?UTF-8?q?Assertions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cs/HomeExercises/ObjectComparison.cs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index 44d9aed4..fa838119 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -10,21 +10,18 @@ public class ObjectComparison [Category("ToRefactor")] public void CheckCurrentTsar() { + // Это решение более читаемо за счет FluentAPI. + // При неудачном тестировании в отличии от CheckCurrentTsar_WithCustomEquality предоставляет подробную информацию. + // При изменении Person потребуется только немного изменить expectedTsar. + + 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. - 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); + expectedTsar.Should().BeEquivalentTo(actualTsar, + options => options.Excluding(p => p.SelectedMemberInfo.Name == nameof(Person.Id))); } [Test] @@ -35,7 +32,10 @@ public void CheckCurrentTsar_WithCustomEquality() var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, new Person("Vasili III of Russia", 28, 170, 60, null)); - // Какие недостатки у такого подхода? + // Какие недостатки у такого подхода? + // Плохо читаем + // Не предоставляет детальную информацию при неудачном тестировании + // При изменении Person потребует изменений в методе AreEqual Assert.True(AreEqual(actualTsar, expectedTsar)); } From 47a660e623733840dd42c180accef5cb5c2a2c4a Mon Sep 17 00:00:00 2001 From: Arman Mukanov Date: Mon, 27 Nov 2023 18:04:24 +0500 Subject: [PATCH 2/4] =?UTF-8?q?=D0=9E=D1=82=D1=80=D0=B5=D1=84=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D0=BB=20=D1=82=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cs/HomeExercises/NumberValidatorTests.cs | 77 ++++++++++++++++++------ 1 file changed, 59 insertions(+), 18 deletions(-) diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index a2878113..31621a67 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -5,28 +5,69 @@ namespace HomeExercises { + [TestFixture] public class NumberValidatorTests { [Test] - public void Test() + public void Constructor_ShouldThrow_WhenPrecisionIsNotPositive() { - 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)); - - 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")); + Action act = () => new NumberValidator(0); + act.Should() + .Throw() + .WithMessage("precision must be a positive number"); + } + + [Test] + public void Constructor_ShouldThrow_WhenScaleIsNegative() + { + Action act = () => new NumberValidator(3, -1); + act.Should() + .Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + + [Test] + public void Constructor_ShouldThrow_WhenScaleIsGreaterOrEqualPrecision() + { + Action act = () => new NumberValidator(3, 4); + act.Should() + .Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + + [Test] + public void Constructor_ShouldNotThrow_WhenScaleIsZero() + { + Action act = () => new NumberValidator(3, 0); + act.Should().NotThrow(); + } + + [TestCase("0")] + [TestCase("00.0")] + [TestCase("+2.0")] + [TestCase("1111")] + public void IsValidNumber_ShouldBeTrue_WhenNumberIsValid(string value) + { + var validator = new NumberValidator(4, 2, true); + + var result = validator.IsValidNumber(value); + + result.Should().BeTrue(); + } + + [TestCase("")] + [TestCase(" _")] + [TestCase("4.sd")] + [TestCase("-0.0")] + [TestCase("0.000")] + [TestCase("+-0")] + public void IsValidNumber_ShouldBeFalse_WhenNumberIsNotValid(string value) + { + var validator = new NumberValidator(4, 2, true); + + var result = validator.IsValidNumber(value); + + result.Should().BeFalse(); } } From 37c930036fba891dd4ab39732e1a85868310df39 Mon Sep 17 00:00:00 2001 From: Arman Mukanov Date: Thu, 30 Nov 2023 09:59:19 +0500 Subject: [PATCH 3/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cs/HomeExercises/ObjectComparison.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index fa838119..47b4bf41 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -34,6 +34,7 @@ public void CheckCurrentTsar_WithCustomEquality() // Какие недостатки у такого подхода? // Плохо читаем + // Из-за рекурсивного вызова в AreEqual возможно переполнение стека // Не предоставляет детальную информацию при неудачном тестировании // При изменении Person потребует изменений в методе AreEqual Assert.True(AreEqual(actualTsar, expectedTsar)); From 2f18a9565482e661e1edf024535805c6d951af88 Mon Sep 17 00:00:00 2001 From: Arman Mukanov Date: Thu, 30 Nov 2023 10:02:09 +0500 Subject: [PATCH 4/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cs/HomeExercises/NumberValidatorTests.cs | 105 ++++++++++++++++++++--- 1 file changed, 91 insertions(+), 14 deletions(-) diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index 31621a67..b437661e 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -9,7 +9,16 @@ namespace HomeExercises public class NumberValidatorTests { [Test] - public void Constructor_ShouldThrow_WhenPrecisionIsNotPositive() + public void Constructor_ShouldThrow_WhenPrecisionNegative() + { + Action act = () => new NumberValidator(-1); + act.Should() + .Throw() + .WithMessage("precision must be a positive number"); + } + + [Test] + public void Constructor_ShouldThrow_WhenPrecisionIsZero() { Action act = () => new NumberValidator(0); act.Should() @@ -18,7 +27,7 @@ public void Constructor_ShouldThrow_WhenPrecisionIsNotPositive() } [Test] - public void Constructor_ShouldThrow_WhenScaleIsNegative() + public void Constructor_ShouldThrow_WhenScaleNegative() { Action act = () => new NumberValidator(3, -1); act.Should() @@ -27,7 +36,16 @@ public void Constructor_ShouldThrow_WhenScaleIsNegative() } [Test] - public void Constructor_ShouldThrow_WhenScaleIsGreaterOrEqualPrecision() + public void Constructor_ShouldThrow_WhenScaleEqualPrecision() + { + Action act = () => new NumberValidator(3, 3); + act.Should() + .Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + + [Test] + public void Constructor_ShouldThrow_WhenScaleGreaterPrecision() { Action act = () => new NumberValidator(3, 4); act.Should() @@ -42,28 +60,87 @@ public void Constructor_ShouldNotThrow_WhenScaleIsZero() act.Should().NotThrow(); } - [TestCase("0")] - [TestCase("00.0")] - [TestCase("+2.0")] - [TestCase("1111")] - public void IsValidNumber_ShouldBeTrue_WhenNumberIsValid(string value) + + [TestCase("1.2")] + [TestCase("1,2")] + public void IsValidNumber_ShouldBeTrue_WhenValueWithDifferentSeparators(string value) + { + var validator = new NumberValidator(4, 2, true); + + var result = validator.IsValidNumber(value); + + result.Should().BeTrue(); + } + + [TestCase("1`4")] + [TestCase("1'4")] + [TestCase("1_4")] + [TestCase("1 000")] + public void IsValidNumber_ShouldBeFalse_WhenValueWithUnsupportedSeparators(string value) + { + var validator = new NumberValidator(10, 2, true); + + var result = validator.IsValidNumber(value); + + result.Should().BeFalse(); + } + + [TestCase("-0")] + [TestCase("-1")] + public void IsValidNumber_ShouldBeFalse_WhenOnlyPositive(string value) { var validator = new NumberValidator(4, 2, true); var result = validator.IsValidNumber(value); + result.Should().BeFalse(); + } + + [TestCase("-0")] + [TestCase("-1")] + public void IsValidNumber_ShouldBeTrue_WhenNumberIsNegative(string value) + { + var validator = new NumberValidator(4, 2); + + var result = validator.IsValidNumber(value); + result.Should().BeTrue(); } + [TestCase(null)] [TestCase("")] - [TestCase(" _")] - [TestCase("4.sd")] - [TestCase("-0.0")] - [TestCase("0.000")] + public void IsValidNumber_ShouldBeFalse_WhenValueIsNullOrEmpty(string value) + { + var validator = new NumberValidator(10, 2, true); + + var result = validator.IsValidNumber(value); + + result.Should().BeFalse(); + } + + [TestCase("1.")] + [TestCase(".1")] [TestCase("+-0")] - public void IsValidNumber_ShouldBeFalse_WhenNumberIsNotValid(string value) + [TestCase("@#$1")] + [TestCase("\n1")] + [TestCase("abc")] + [TestCase("a4")] + public void IsValidNumber_ShouldBeFalse_WhenInvalidFormat(string value) { - var validator = new NumberValidator(4, 2, true); + var validator = new NumberValidator(10, 2); + + var result = validator.IsValidNumber(value); + + result.Should().BeFalse(); + } + + [TestCase("1111")] + [TestCase("-111")] + [TestCase("-10.0")] + [TestCase("0.00")] + public void IsValidNumber_ShouldBeFalse_WhenValueTooLong(string value) + { + var validator = new NumberValidator(3, 1); var result = validator.IsValidNumber(value);