From c141b1fd467d985fc2a568303575824930337f3b Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Mon, 4 Nov 2024 01:55:01 +0500 Subject: [PATCH 01/16] =?UTF-8?q?ObjectComparison.cs.=20=D0=A0=D0=B5=D1=84?= =?UTF-8?q?=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D0=BE=D0=B3=D0=BE=20=D1=82=D0=B5=D1=81=D1=82=D0=B0?= =?UTF-8?q?,=20=D0=BE=D0=B1=D1=8A=D1=8F=D1=81=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BD=D0=B5=D0=B4=D0=BE=D1=81=D1=82=D0=B0=D1=82=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=B2=D1=82=D0=BE=D1=80=D0=BE=D0=B3=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1. ObjectComparison/ObjectComparison.cs | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..a0fca21 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,8 @@ 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.IsEqual(expectedTsar); } [Test] @@ -35,6 +27,14 @@ public void CheckCurrentTsar_WithCustomEquality() new Person("Vasili III of Russia", 28, 170, 60, null)); // Какие недостатки у такого подхода? + /* + * 1) Если у класса изменять поля (удалять или добавлять), то также придется изменять + * логику проверки на соответствие сущностей. В случае с удалением поля программа вообще не скомпилируется + * 2) Возможна бесконечная рекурсия + * 3) При падении теста не поймем в чем была причина. Ожидался True, а получили False, вот и вся инфа( + * 4) Использование FluentAssertion ускоряет понимание логики секции проверки, за счет использования + * method chaining. В данном тесте вникнуть в логику проверки теста сложнее, чем в первом. + */ ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); } @@ -50,3 +50,16 @@ private bool AreEqual(Person? actual, Person? expected) && AreEqual(actual.Parent, expected.Parent); } } + +internal static class ObjectComparisonAssertExtensions +{ + public static void IsEqual(this Person actualTsar, Person expectedTsar) + { + actualTsar.Should().BeEquivalentTo( + expectedTsar, + options => options + .Excluding(x => x.Name == nameof(Person.Id) && x.DeclaringType == typeof(Person)) + .AllowingInfiniteRecursion() + .IgnoringCyclicReferences()); + } +} \ No newline at end of file From 3ff842cffd42bad72b17cd1a4f9fdacca1921877 Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Mon, 4 Nov 2024 12:29:58 +0500 Subject: [PATCH 02/16] =?UTF-8?q?NumberValidatorTests.=20=D0=9D=D0=B0?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20=D0=BD=D0=B5=D0=B4=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=8E=D1=89=D0=B8=D0=B5=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D1=8B,=20=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B7=D0=BD=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=D1=8B=D0=B5=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B8?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=80=D0=B0=D0=B7=D0=BD=D1=8B=D0=B5=20=D1=82?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=D1=8B.=20=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20TestNumberValidatorBuilder=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D1=83=D0=B4=D0=BE=D0=B1=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20NumberValidator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NumberValidatorTests.cs | 163 ++++++++++++++++-- .../TestNumberValidatorBuilder.cs | 34 ++++ 2 files changed, 178 insertions(+), 19 deletions(-) create mode 100644 Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 950c9bc..24bd3dc 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; @@ -8,24 +9,148 @@ namespace HomeExercise.Tasks.NumberValidator; public class NumberValidatorTests { [Test] - public void Test() - { - 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)); - - 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 void Should_Throw_ArgumentException_If_Precision_Is_Negative() + { + var builder = new TestNumberValidatorBuilder().WithPrecision(-1); + + Assert.Throws(() => builder.Build()); + } + + [Test] + public void Should_Throw_ArgumentException_If_Scale_Is_Negative() + { + var builder = new TestNumberValidatorBuilder().WithScale(-1); + + Assert.Throws(() => builder.Build()); + } + + [Test] + public void Should_Throw_ArgumentException_If_Scale_Greater_Precision() + { + var builder = new TestNumberValidatorBuilder().WithScale(5).WithPrecision(2); + + Assert.Throws(() => builder.Build()); + } + + [Test] + public void Should_Throw_ArgumentException_If_Scale_Equal_Precision() + { + var builder = new TestNumberValidatorBuilder().WithScale(2).WithPrecision(2); + + Assert.Throws(() => builder.Build()); + } + + [Test] + public void Should_DoesNotThrow_Exception_If_Scale_And_Precision_Is_Correct() + { + var builder = new TestNumberValidatorBuilder(); + + Assert.DoesNotThrow(() => builder.Build()); + } + + [Test] + public void IsValidNumber_Should_Return_False_If_Value_Is_Null() + { + var numberValidator = new TestNumberValidatorBuilder().Build(); + + var actual = numberValidator.IsValidNumber(null); + + actual.Should().BeFalse(); + } + + [Test] + [TestCase("")] + [TestCase(" ")] + [TestCase(" ")] + public void IsValidNumber_Should_Return_False_If_Value_Is_Empty(string value) + { + var numberValidator = new TestNumberValidatorBuilder().Build(); + + var actual = numberValidator.IsValidNumber(value); + + actual.Should().BeFalse(); + } + + [Test] + [TestCase("a.sd")] + [TestCase("0.")] + [TestCase(",0")] + [TestCase("0*0")] + [TestCase("!0,0")] + public void IsValidNumber_Should_Return_False_If_Match_With_Value_Fail(string value) + { + var numberValidator = new TestNumberValidatorBuilder().Build(); + + var actual = numberValidator.IsValidNumber(value); + + actual.Should().BeFalse(); + } + + [Test] + [TestCase("1,234")] + [TestCase("1111")] + [TestCase("22,32")] + [TestCase("+1,23")] + [TestCase("+0,00")] + [TestCase("-0,00")] + public void IsValidNumber_Should_Return_False_If_Sum_Of_IntPart_And_FracPart_More_Than_Precision(string value) + { + var numberValidator = new TestNumberValidatorBuilder().WithPrecision(3).WithScale(2).Build(); + + var actual = numberValidator.IsValidNumber(value); + + actual.Should().BeFalse(); + } + + [Test] + public void IsValidNumber_Should_Return_False_If_FracPart_More_Than_Scale() + { + var numberValidator = new TestNumberValidatorBuilder().WithScale(2).Build(); + var value = "0,000"; + + var actual = numberValidator.IsValidNumber(value); + + actual.Should().BeFalse(); + } + + [Test] + [TestCase("-0,0")] + [TestCase("-0,000")] + [TestCase("-0")] + public void IsValidNumber_Should_Return_False_If_OnlyPositive_True_And_Value_Contains_Minus(string value) + { + var numberValidator = new TestNumberValidatorBuilder().Build(); + + var actual = numberValidator.IsValidNumber(value); + + actual.Should().BeFalse(); + } + + [Test] + [TestCase("-0,0")] + [TestCase("-0,000")] + [TestCase("-0")] + public void IsValidNumber_Should_Return_True_If_OnlyPositive_False_And_Value_Contains_Minus(string value) + { + var numberValidator = new TestNumberValidatorBuilder().WithOnlyPositive(false).Build(); + + var actual = numberValidator.IsValidNumber(value); + + actual.Should().BeTrue(); + } + + [Test] + [TestCase("0.0")] + [TestCase("0")] + [TestCase("+1.23")] + [TestCase("1.444")] + [TestCase("+1.444")] + public void IsValidNumber_Should_Return_True_If_Value_Correct(string value) + { + var numberValidator = new TestNumberValidatorBuilder().Build(); + + var actual = numberValidator.IsValidNumber(value); + + actual.Should().BeTrue(); } } \ No newline at end of file diff --git a/Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs b/Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs new file mode 100644 index 0000000..fd7454c --- /dev/null +++ b/Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs @@ -0,0 +1,34 @@ +namespace HomeExercise.Tasks.NumberValidator; + +public class TestNumberValidatorBuilder +{ + public const int DEFAULT_PRECISION = 5; + public const int DEFAULT_SCALE = 3; + public const bool DEFAULT_ONLY_POSITIVE = true; + private int precision = DEFAULT_PRECISION; + private int scale = DEFAULT_SCALE; + private bool onlyPositive = DEFAULT_ONLY_POSITIVE; + + public TestNumberValidatorBuilder WithPrecision(int precision) + { + this.precision = precision; + return this; + } + + public TestNumberValidatorBuilder WithScale(int scale) + { + this.scale = scale; + return this; + } + + public TestNumberValidatorBuilder WithOnlyPositive(bool onlyPositive) + { + this.onlyPositive = onlyPositive; + return this; + } + + public NumberValidator Build() + { + return new NumberValidator(precision, scale, onlyPositive); + } +} \ No newline at end of file From 9dcb67f44439938fe0225b630ce7ba90ad4a9f9e Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Mon, 4 Nov 2024 21:06:47 +0500 Subject: [PATCH 03/16] remove needless attribute Test in TestCases --- .../2. NumberValidator/NumberValidatorTests.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 24bd3dc..24acea0 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -57,8 +57,7 @@ public void IsValidNumber_Should_Return_False_If_Value_Is_Null() actual.Should().BeFalse(); } - - [Test] + [TestCase("")] [TestCase(" ")] [TestCase(" ")] @@ -70,8 +69,7 @@ public void IsValidNumber_Should_Return_False_If_Value_Is_Empty(string value) actual.Should().BeFalse(); } - - [Test] + [TestCase("a.sd")] [TestCase("0.")] [TestCase(",0")] @@ -85,8 +83,7 @@ public void IsValidNumber_Should_Return_False_If_Match_With_Value_Fail(string va actual.Should().BeFalse(); } - - [Test] + [TestCase("1,234")] [TestCase("1111")] [TestCase("22,32")] @@ -113,7 +110,6 @@ public void IsValidNumber_Should_Return_False_If_FracPart_More_Than_Scale() actual.Should().BeFalse(); } - [Test] [TestCase("-0,0")] [TestCase("-0,000")] [TestCase("-0")] @@ -126,7 +122,6 @@ public void IsValidNumber_Should_Return_False_If_OnlyPositive_True_And_Value_Con actual.Should().BeFalse(); } - [Test] [TestCase("-0,0")] [TestCase("-0,000")] [TestCase("-0")] @@ -138,8 +133,7 @@ public void IsValidNumber_Should_Return_True_If_OnlyPositive_False_And_Value_Con actual.Should().BeTrue(); } - - [Test] + [TestCase("0.0")] [TestCase("0")] [TestCase("+1.23")] From d948c7176140a836c686fc37b4552198781a525c Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Mon, 4 Nov 2024 21:52:38 +0500 Subject: [PATCH 04/16] take out assert on argumentException in method --- .../NumberValidatorTests.cs | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 24acea0..0effa80 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -9,33 +9,24 @@ namespace HomeExercise.Tasks.NumberValidator; public class NumberValidatorTests { [Test] - public void Should_Throw_ArgumentException_If_Precision_Is_Negative() - { - var builder = new TestNumberValidatorBuilder().WithPrecision(-1); - - Assert.Throws(() => builder.Build()); - } - + public void Should_Throw_ArgumentException_If_Precision_Is_Negative() => + ShouldThrowExceptionWhenCreating(-1, 5); + [Test] - public void Should_Throw_ArgumentException_If_Scale_Is_Negative() - { - var builder = new TestNumberValidatorBuilder().WithScale(-1); - - Assert.Throws(() => builder.Build()); - } - + public void Should_Throw_ArgumentException_If_Scale_Is_Negative() => + ShouldThrowExceptionWhenCreating(5, -1); + [Test] - public void Should_Throw_ArgumentException_If_Scale_Greater_Precision() - { - var builder = new TestNumberValidatorBuilder().WithScale(5).WithPrecision(2); - - Assert.Throws(() => builder.Build()); - } - + public void Should_Throw_ArgumentException_If_Scale_Greater_Precision() => + ShouldThrowExceptionWhenCreating(2, 5); + [Test] - public void Should_Throw_ArgumentException_If_Scale_Equal_Precision() + public void Should_Throw_ArgumentException_If_Scale_Equal_Precision() => + ShouldThrowExceptionWhenCreating(2, 2); + + private void ShouldThrowExceptionWhenCreating(int precision, int scale) { - var builder = new TestNumberValidatorBuilder().WithScale(2).WithPrecision(2); + var builder = new TestNumberValidatorBuilder().WithScale(scale).WithPrecision(precision); Assert.Throws(() => builder.Build()); } From 80437c9a2d8d0984323af893bbff33c0ab527e33 Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Mon, 4 Nov 2024 22:19:53 +0500 Subject: [PATCH 05/16] take out assert on IsValidNumber return false --- .../NumberValidatorTests.cs | 64 ++++++------------- 1 file changed, 20 insertions(+), 44 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 0effa80..f8722da 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -40,40 +40,22 @@ public void Should_DoesNotThrow_Exception_If_Scale_And_Precision_Is_Correct() } [Test] - public void IsValidNumber_Should_Return_False_If_Value_Is_Null() - { - var numberValidator = new TestNumberValidatorBuilder().Build(); - - var actual = numberValidator.IsValidNumber(null); - - actual.Should().BeFalse(); - } + public void IsValidNumber_Should_Return_False_If_Value_Is_Null() => + IsValidNumberShouldReturnFalse(null!, 10, 5); [TestCase("")] [TestCase(" ")] [TestCase(" ")] - public void IsValidNumber_Should_Return_False_If_Value_Is_Empty(string value) - { - var numberValidator = new TestNumberValidatorBuilder().Build(); - - var actual = numberValidator.IsValidNumber(value); - - actual.Should().BeFalse(); - } + public void IsValidNumber_Should_Return_False_If_Value_Is_Empty(string value) => + IsValidNumberShouldReturnFalse(value, 10, 5, true); [TestCase("a.sd")] [TestCase("0.")] [TestCase(",0")] [TestCase("0*0")] [TestCase("!0,0")] - public void IsValidNumber_Should_Return_False_If_Match_With_Value_Fail(string value) - { - var numberValidator = new TestNumberValidatorBuilder().Build(); - - var actual = numberValidator.IsValidNumber(value); - - actual.Should().BeFalse(); - } + public void IsValidNumber_Should_Return_False_If_Match_With_Value_Fail(string value) => + IsValidNumberShouldReturnFalse(value, 10, 5); [TestCase("1,234")] [TestCase("1111")] @@ -81,32 +63,26 @@ public void IsValidNumber_Should_Return_False_If_Match_With_Value_Fail(string va [TestCase("+1,23")] [TestCase("+0,00")] [TestCase("-0,00")] - public void IsValidNumber_Should_Return_False_If_Sum_Of_IntPart_And_FracPart_More_Than_Precision(string value) - { - var numberValidator = new TestNumberValidatorBuilder().WithPrecision(3).WithScale(2).Build(); - - var actual = numberValidator.IsValidNumber(value); - - actual.Should().BeFalse(); - } - + public void IsValidNumber_Should_Return_False_If_Sum_Of_IntPart_And_FracPart_More_Than_Precision(string value) => + IsValidNumberShouldReturnFalse(value, 3, 2); + [Test] - public void IsValidNumber_Should_Return_False_If_FracPart_More_Than_Scale() - { - var numberValidator = new TestNumberValidatorBuilder().WithScale(2).Build(); - var value = "0,000"; - - var actual = numberValidator.IsValidNumber(value); - - actual.Should().BeFalse(); - } + public void IsValidNumber_Should_Return_False_If_FracPart_More_Than_Scale() => + IsValidNumberShouldReturnFalse("0,000", 10, 2); [TestCase("-0,0")] [TestCase("-0,000")] [TestCase("-0")] - public void IsValidNumber_Should_Return_False_If_OnlyPositive_True_And_Value_Contains_Minus(string value) + public void IsValidNumber_Should_Return_False_If_OnlyPositive_True_And_Value_Contains_Minus(string value) => + IsValidNumberShouldReturnFalse(value, 10, 5); + + private void IsValidNumberShouldReturnFalse(string value, int precision, int scale, bool onlyPositive = true) { - var numberValidator = new TestNumberValidatorBuilder().Build(); + var numberValidator = new TestNumberValidatorBuilder() + .WithPrecision(precision) + .WithScale(scale) + .WithOnlyPositive(onlyPositive) + .Build(); var actual = numberValidator.IsValidNumber(value); From 5a5622f6e0adeda465036b5fc0f7a464cd3ef86a Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Mon, 4 Nov 2024 22:29:25 +0500 Subject: [PATCH 06/16] update TestNumberValidatorBuilder.cs --- .../2. NumberValidator/NumberValidatorTests.cs | 7 +++---- .../TestNumberValidatorBuilder.cs | 14 ++++---------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index f8722da..865547b 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -26,7 +26,7 @@ public void Should_Throw_ArgumentException_If_Scale_Equal_Precision() => private void ShouldThrowExceptionWhenCreating(int precision, int scale) { - var builder = new TestNumberValidatorBuilder().WithScale(scale).WithPrecision(precision); + var builder = new TestNumberValidatorBuilder().WithPrecisionAndScale(precision, scale); Assert.Throws(() => builder.Build()); } @@ -47,7 +47,7 @@ public void IsValidNumber_Should_Return_False_If_Value_Is_Null() => [TestCase(" ")] [TestCase(" ")] public void IsValidNumber_Should_Return_False_If_Value_Is_Empty(string value) => - IsValidNumberShouldReturnFalse(value, 10, 5, true); + IsValidNumberShouldReturnFalse(value, 10, 5); [TestCase("a.sd")] [TestCase("0.")] @@ -79,8 +79,7 @@ public void IsValidNumber_Should_Return_False_If_OnlyPositive_True_And_Value_Con private void IsValidNumberShouldReturnFalse(string value, int precision, int scale, bool onlyPositive = true) { var numberValidator = new TestNumberValidatorBuilder() - .WithPrecision(precision) - .WithScale(scale) + .WithPrecisionAndScale(precision, scale) .WithOnlyPositive(onlyPositive) .Build(); diff --git a/Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs b/Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs index fd7454c..4a1f7e6 100644 --- a/Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs +++ b/Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs @@ -9,26 +9,20 @@ public class TestNumberValidatorBuilder private int scale = DEFAULT_SCALE; private bool onlyPositive = DEFAULT_ONLY_POSITIVE; - public TestNumberValidatorBuilder WithPrecision(int precision) + public TestNumberValidatorBuilder WithPrecisionAndScale(int precision, int scale) { this.precision = precision; - return this; - } - - public TestNumberValidatorBuilder WithScale(int scale) - { this.scale = scale; + return this; } public TestNumberValidatorBuilder WithOnlyPositive(bool onlyPositive) { this.onlyPositive = onlyPositive; + return this; } - public NumberValidator Build() - { - return new NumberValidator(precision, scale, onlyPositive); - } + public NumberValidator Build() => new(precision, scale, onlyPositive); } \ No newline at end of file From 589aa083d7cd4d9a43e2bee9a474e2d5eac5059a Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Tue, 5 Nov 2024 21:22:55 +0500 Subject: [PATCH 07/16] merge throw Exception tests into one --- .../NumberValidatorTests.cs | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 865547b..f674c92 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -8,23 +8,11 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [Test] - public void Should_Throw_ArgumentException_If_Precision_Is_Negative() => - ShouldThrowExceptionWhenCreating(-1, 5); - - [Test] - public void Should_Throw_ArgumentException_If_Scale_Is_Negative() => - ShouldThrowExceptionWhenCreating(5, -1); - - [Test] - public void Should_Throw_ArgumentException_If_Scale_Greater_Precision() => - ShouldThrowExceptionWhenCreating(2, 5); - - [Test] - public void Should_Throw_ArgumentException_If_Scale_Equal_Precision() => - ShouldThrowExceptionWhenCreating(2, 2); - - private void ShouldThrowExceptionWhenCreating(int precision, int scale) + [TestCase(-1, 5)] + [TestCase(5, -1)] + [TestCase(2, 5)] + [TestCase(2, 2)] + public void Should_Throw_Exception_With_Incorrect_Parameters(int precision, int scale) { var builder = new TestNumberValidatorBuilder().WithPrecisionAndScale(precision, scale); From ec56affee52b138aa4f294a4177c7fbf12a68301 Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Tue, 5 Nov 2024 22:23:37 +0500 Subject: [PATCH 08/16] merge IsValidNumber Tests in TestCases --- .../NumberValidatorTests.cs | 61 ++++--------------- 1 file changed, 11 insertions(+), 50 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index f674c92..b9c7326 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -27,48 +27,22 @@ public void Should_DoesNotThrow_Exception_If_Scale_And_Precision_Is_Correct() Assert.DoesNotThrow(() => builder.Build()); } - [Test] - public void IsValidNumber_Should_Return_False_If_Value_Is_Null() => - IsValidNumberShouldReturnFalse(null!, 10, 5); - - [TestCase("")] + [TestCase(null)] [TestCase(" ")] - [TestCase(" ")] - public void IsValidNumber_Should_Return_False_If_Value_Is_Empty(string value) => - IsValidNumberShouldReturnFalse(value, 10, 5); - [TestCase("a.sd")] [TestCase("0.")] [TestCase(",0")] [TestCase("0*0")] [TestCase("!0,0")] - public void IsValidNumber_Should_Return_False_If_Match_With_Value_Fail(string value) => - IsValidNumberShouldReturnFalse(value, 10, 5); - [TestCase("1,234")] - [TestCase("1111")] + [TestCase("11111")] [TestCase("22,32")] - [TestCase("+1,23")] - [TestCase("+0,00")] - [TestCase("-0,00")] - public void IsValidNumber_Should_Return_False_If_Sum_Of_IntPart_And_FracPart_More_Than_Precision(string value) => - IsValidNumberShouldReturnFalse(value, 3, 2); - - [Test] - public void IsValidNumber_Should_Return_False_If_FracPart_More_Than_Scale() => - IsValidNumberShouldReturnFalse("0,000", 10, 2); - - [TestCase("-0,0")] - [TestCase("-0,000")] + [TestCase("+14,23")] [TestCase("-0")] - public void IsValidNumber_Should_Return_False_If_OnlyPositive_True_And_Value_Contains_Minus(string value) => - IsValidNumberShouldReturnFalse(value, 10, 5); - - private void IsValidNumberShouldReturnFalse(string value, int precision, int scale, bool onlyPositive = true) + public void IsValidNumber_Should_Return_False(string value) { var numberValidator = new TestNumberValidatorBuilder() - .WithPrecisionAndScale(precision, scale) - .WithOnlyPositive(onlyPositive) + .WithPrecisionAndScale(4, 2) .Build(); var actual = numberValidator.IsValidNumber(value); @@ -76,26 +50,13 @@ private void IsValidNumberShouldReturnFalse(string value, int precision, int sca actual.Should().BeFalse(); } - [TestCase("-0,0")] - [TestCase("-0,000")] - [TestCase("-0")] - public void IsValidNumber_Should_Return_True_If_OnlyPositive_False_And_Value_Contains_Minus(string value) - { - var numberValidator = new TestNumberValidatorBuilder().WithOnlyPositive(false).Build(); - - var actual = numberValidator.IsValidNumber(value); - - actual.Should().BeTrue(); - } - - [TestCase("0.0")] - [TestCase("0")] - [TestCase("+1.23")] - [TestCase("1.444")] - [TestCase("+1.444")] - public void IsValidNumber_Should_Return_True_If_Value_Correct(string value) + [TestCase("-1,4", false)] + [TestCase("1")] + [TestCase("+1,444")] + [TestCase("1,444")] + public void IsValidNumber_Should_Return_True(string value, bool onlyPositive = true) { - var numberValidator = new TestNumberValidatorBuilder().Build(); + var numberValidator = new TestNumberValidatorBuilder().WithOnlyPositive(onlyPositive).Build(); var actual = numberValidator.IsValidNumber(value); From ee134c276918d29e92da33e5368e6cafabfd574f Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Tue, 5 Nov 2024 22:33:28 +0500 Subject: [PATCH 09/16] remove using TestNumberValidatorBuilder --- .../2. NumberValidator/NumberValidatorTests.cs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index b9c7326..9a64cfc 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,7 +1,5 @@ - -using FluentAssertions; +using FluentAssertions; using NUnit.Framework; -using NUnit.Framework.Legacy; namespace HomeExercise.Tasks.NumberValidator; @@ -14,17 +12,17 @@ public class NumberValidatorTests [TestCase(2, 2)] public void Should_Throw_Exception_With_Incorrect_Parameters(int precision, int scale) { - var builder = new TestNumberValidatorBuilder().WithPrecisionAndScale(precision, scale); + var act = () => new NumberValidator(precision, scale); - Assert.Throws(() => builder.Build()); + act.Should().Throw(); } [Test] public void Should_DoesNotThrow_Exception_If_Scale_And_Precision_Is_Correct() { - var builder = new TestNumberValidatorBuilder(); + var act = () => new NumberValidator(10, 5); - Assert.DoesNotThrow(() => builder.Build()); + act.Should().NotThrow(); } [TestCase(null)] @@ -41,9 +39,7 @@ public void Should_DoesNotThrow_Exception_If_Scale_And_Precision_Is_Correct() [TestCase("-0")] public void IsValidNumber_Should_Return_False(string value) { - var numberValidator = new TestNumberValidatorBuilder() - .WithPrecisionAndScale(4, 2) - .Build(); + var numberValidator = new NumberValidator(4, 2, true); var actual = numberValidator.IsValidNumber(value); @@ -56,7 +52,7 @@ public void IsValidNumber_Should_Return_False(string value) [TestCase("1,444")] public void IsValidNumber_Should_Return_True(string value, bool onlyPositive = true) { - var numberValidator = new TestNumberValidatorBuilder().WithOnlyPositive(onlyPositive).Build(); + var numberValidator = new NumberValidator(10, 5, onlyPositive); var actual = numberValidator.IsValidNumber(value); From f0f22f608476608c0e869bdac51687db1376b42b Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Tue, 5 Nov 2024 22:36:18 +0500 Subject: [PATCH 10/16] delete TestNumberValidatorBuilder.cs from project --- .../TestNumberValidatorBuilder.cs | 28 ------------------- 1 file changed, 28 deletions(-) delete mode 100644 Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs diff --git a/Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs b/Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs deleted file mode 100644 index 4a1f7e6..0000000 --- a/Testing/Basic/Homework/2. NumberValidator/TestNumberValidatorBuilder.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace HomeExercise.Tasks.NumberValidator; - -public class TestNumberValidatorBuilder -{ - public const int DEFAULT_PRECISION = 5; - public const int DEFAULT_SCALE = 3; - public const bool DEFAULT_ONLY_POSITIVE = true; - private int precision = DEFAULT_PRECISION; - private int scale = DEFAULT_SCALE; - private bool onlyPositive = DEFAULT_ONLY_POSITIVE; - - public TestNumberValidatorBuilder WithPrecisionAndScale(int precision, int scale) - { - this.precision = precision; - this.scale = scale; - - return this; - } - - public TestNumberValidatorBuilder WithOnlyPositive(bool onlyPositive) - { - this.onlyPositive = onlyPositive; - - return this; - } - - public NumberValidator Build() => new(precision, scale, onlyPositive); -} \ No newline at end of file From 9a1a895ca9aaa7e305e67d48d3132d6e73d90d91 Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Tue, 5 Nov 2024 23:07:49 +0500 Subject: [PATCH 11/16] add TestName in TestCases --- .../NumberValidatorTests.cs | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 9a64cfc..4d26c2b 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -6,11 +6,11 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [TestCase(-1, 5)] - [TestCase(5, -1)] - [TestCase(2, 5)] - [TestCase(2, 2)] - public void Should_Throw_Exception_With_Incorrect_Parameters(int precision, int scale) + [TestCase(-1, 5, TestName = "Should_Throw_ArgumentException_If_Precision_Is_Negative")] + [TestCase(5, -1, TestName = "Should_Throw_ArgumentException_If_Scale_Is_Negative")] + [TestCase(2, 5, TestName = "Should_Throw_ArgumentException_If_Scale_Greater_Precision")] + [TestCase(2, 2, TestName = "Should_Throw_ArgumentException_If_Scale_Equal_Precision")] + public void Should_Throw_Exception(int precision, int scale) { var act = () => new NumberValidator(precision, scale); @@ -25,18 +25,18 @@ public void Should_DoesNotThrow_Exception_If_Scale_And_Precision_Is_Correct() act.Should().NotThrow(); } - [TestCase(null)] - [TestCase(" ")] - [TestCase("a.sd")] - [TestCase("0.")] - [TestCase(",0")] - [TestCase("0*0")] - [TestCase("!0,0")] - [TestCase("1,234")] - [TestCase("11111")] - [TestCase("22,32")] - [TestCase("+14,23")] - [TestCase("-0")] + [TestCase(null, TestName = "IsValidNumber_Should_Return_False_If_Value_Is_Null")] + [TestCase(" ", TestName = "IsValidNumber_Should_Return_False_If_Value_Is_Empty")] + [TestCase("2.a1", TestName = "IsValidNumber_Should_Return_False_If_Value_Contains_Letter")] + [TestCase("0.", TestName = "IsValidNumber_Should_Return_False_If_Value_Contains_Separator_Without_FracPart")] + [TestCase(",0", TestName = "IsValidNumber_Should_Return_False_If_Value_Without_IntPart")] + [TestCase("0*0", TestName = "IsValidNumber_Should_Return_False_If_Value_Separator_Incorrect")] + [TestCase("!0,0", TestName = "IsValidNumber_Should_Return_False_If_Value_Sign_Incorrect")] + [TestCase("1,234", TestName = "IsValidNumber_Should_Return_False_If_Value_FracPart_More_Than_Scale")] + [TestCase("11111", TestName = "IsValidNumber_Should_Return_False_If_Value_IntPart_More_Than_Precision")] + [TestCase("225,32", TestName = "IsValidNumber_Should_Return_False_If_Value_With_FracPart_More_Than_Precision")] + [TestCase("+14,23", TestName = "IsValidNumber_Should_Return_False_If_Value_With_Sign_More_Than_Precision")] + [TestCase("-0", TestName = "IsValidNumber_Should_Return_False_If_OnlyPositive_True_And_Value_Contains_Minus")] public void IsValidNumber_Should_Return_False(string value) { var numberValidator = new NumberValidator(4, 2, true); @@ -46,10 +46,10 @@ public void IsValidNumber_Should_Return_False(string value) actual.Should().BeFalse(); } - [TestCase("-1,4", false)] - [TestCase("1")] - [TestCase("+1,444")] - [TestCase("1,444")] + [TestCase("-1,4", false, TestName = "IsValidNumber_Should_Return_True_If_OnlyPositive_False_And_Value_Contains_Minus")] + [TestCase("1", TestName = "IsValidNumber_Should_Return_True_If_Value_IntPart")] + [TestCase("+1,444", TestName = "IsValidNumber_Should_Return_True_If_Value_Contains_FracPart_And_Sign")] + [TestCase("1,444", TestName = "IsValidNumber_Should_Return_True_If_Value_Contains_FracPart")] public void IsValidNumber_Should_Return_True(string value, bool onlyPositive = true) { var numberValidator = new NumberValidator(10, 5, onlyPositive); From 8aaed3be29a959d1b07e2eea399e5b644ee86a0b Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Wed, 6 Nov 2024 20:30:00 +0500 Subject: [PATCH 12/16] change NumberValidator Constructor tests --- .../2. NumberValidator/NumberValidatorTests.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 4d26c2b..a2c0e08 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -6,23 +6,23 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [TestCase(-1, 5, TestName = "Should_Throw_ArgumentException_If_Precision_Is_Negative")] - [TestCase(5, -1, TestName = "Should_Throw_ArgumentException_If_Scale_Is_Negative")] - [TestCase(2, 5, TestName = "Should_Throw_ArgumentException_If_Scale_Greater_Precision")] - [TestCase(2, 2, TestName = "Should_Throw_ArgumentException_If_Scale_Equal_Precision")] - public void Should_Throw_Exception(int precision, int scale) + [TestCase(-1, 5, TestName = "_IfPrecisionIsNegative")] + [TestCase(5, -1, TestName = "_IfScaleIsNegative")] + [TestCase(2, 5, TestName = "_IfScaleGreaterPrecision")] + [TestCase(2, 2, TestName = "_IfScaleEqualPrecision")] + public void ShouldThrowArgumentException(int precision, int scale) { - var act = () => new NumberValidator(precision, scale); + var numberValidatorConstructor = () => new NumberValidator(precision, scale); - act.Should().Throw(); + numberValidatorConstructor.Should().Throw(); } [Test] public void Should_DoesNotThrow_Exception_If_Scale_And_Precision_Is_Correct() { - var act = () => new NumberValidator(10, 5); + var numberValidatorConstructor = () => new NumberValidator(10, 5); - act.Should().NotThrow(); + numberValidatorConstructor.Should().NotThrow(); } [TestCase(null, TestName = "IsValidNumber_Should_Return_False_If_Value_Is_Null")] From 5f49f1dce0357420000b11fc91625667dd75db57 Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Wed, 6 Nov 2024 20:32:29 +0500 Subject: [PATCH 13/16] delete test on DoesNotThrowException --- .../Homework/2. NumberValidator/NumberValidatorTests.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index a2c0e08..bf228c9 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -17,14 +17,6 @@ public void ShouldThrowArgumentException(int precision, int scale) numberValidatorConstructor.Should().Throw(); } - [Test] - public void Should_DoesNotThrow_Exception_If_Scale_And_Precision_Is_Correct() - { - var numberValidatorConstructor = () => new NumberValidator(10, 5); - - numberValidatorConstructor.Should().NotThrow(); - } - [TestCase(null, TestName = "IsValidNumber_Should_Return_False_If_Value_Is_Null")] [TestCase(" ", TestName = "IsValidNumber_Should_Return_False_If_Value_Is_Empty")] [TestCase("2.a1", TestName = "IsValidNumber_Should_Return_False_If_Value_Contains_Letter")] From c655d532dddeae82135f5439318821414ba03b5b Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Wed, 6 Nov 2024 20:50:24 +0500 Subject: [PATCH 14/16] merge all IsValidNumber tests in one test with testCases --- .../NumberValidatorTests.cs | 47 ++++++++----------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index bf228c9..f09235e 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -17,37 +17,28 @@ public void ShouldThrowArgumentException(int precision, int scale) numberValidatorConstructor.Should().Throw(); } - [TestCase(null, TestName = "IsValidNumber_Should_Return_False_If_Value_Is_Null")] - [TestCase(" ", TestName = "IsValidNumber_Should_Return_False_If_Value_Is_Empty")] - [TestCase("2.a1", TestName = "IsValidNumber_Should_Return_False_If_Value_Contains_Letter")] - [TestCase("0.", TestName = "IsValidNumber_Should_Return_False_If_Value_Contains_Separator_Without_FracPart")] - [TestCase(",0", TestName = "IsValidNumber_Should_Return_False_If_Value_Without_IntPart")] - [TestCase("0*0", TestName = "IsValidNumber_Should_Return_False_If_Value_Separator_Incorrect")] - [TestCase("!0,0", TestName = "IsValidNumber_Should_Return_False_If_Value_Sign_Incorrect")] - [TestCase("1,234", TestName = "IsValidNumber_Should_Return_False_If_Value_FracPart_More_Than_Scale")] - [TestCase("11111", TestName = "IsValidNumber_Should_Return_False_If_Value_IntPart_More_Than_Precision")] - [TestCase("225,32", TestName = "IsValidNumber_Should_Return_False_If_Value_With_FracPart_More_Than_Precision")] - [TestCase("+14,23", TestName = "IsValidNumber_Should_Return_False_If_Value_With_Sign_More_Than_Precision")] - [TestCase("-0", TestName = "IsValidNumber_Should_Return_False_If_OnlyPositive_True_And_Value_Contains_Minus")] - public void IsValidNumber_Should_Return_False(string value) + [TestCase(null, false, TestName = "IfValueIsNull")] + [TestCase(" ", false, TestName = "IfValueIsEmpty")] + [TestCase("2.a1", false, TestName = "IfValueContainsLetter")] + [TestCase("0.", false, TestName = "IfValueContainsSeparatorWithoutFracPart")] + [TestCase(",0", false, TestName = "IfValueWithoutIntPart")] + [TestCase("0*0", false, TestName = "IfValueSeparatorIncorrect")] + [TestCase("!0,0", false, TestName = "IfValueSignIncorrect")] + [TestCase("1,234", false, TestName = "IfValueFracPartMoreThanScale")] + [TestCase("11111", false, TestName = "IfValueIntPartMoreThanPrecision")] + [TestCase("225,32", false, TestName = "IfValueWithFracPartMoreThanPrecision")] + [TestCase("+14,23", false, TestName = "IfValueWithSignMoreThanPrecision")] + [TestCase("-0", false, TestName = "IfOnlyPositiveTrueAndValueContainsMinus")] + [TestCase("-1,4", true, false, TestName = "IfOnlyPositiveFalseAndValueContainsMinus")] + [TestCase("1", true, TestName = "IfValueIntPart")] + [TestCase("+1,44", true, TestName = "IfValueContainsFracPartAndSign")] + [TestCase("15,44", true, TestName = "IfValueContainsFracPart")] + public void IsValidNumber_ShouldReturnExpectedResult(string value, bool expectedResult, bool onlyPositive = true) { - var numberValidator = new NumberValidator(4, 2, true); + var numberValidator = new NumberValidator(4, 2, onlyPositive); var actual = numberValidator.IsValidNumber(value); - actual.Should().BeFalse(); - } - - [TestCase("-1,4", false, TestName = "IsValidNumber_Should_Return_True_If_OnlyPositive_False_And_Value_Contains_Minus")] - [TestCase("1", TestName = "IsValidNumber_Should_Return_True_If_Value_IntPart")] - [TestCase("+1,444", TestName = "IsValidNumber_Should_Return_True_If_Value_Contains_FracPart_And_Sign")] - [TestCase("1,444", TestName = "IsValidNumber_Should_Return_True_If_Value_Contains_FracPart")] - public void IsValidNumber_Should_Return_True(string value, bool onlyPositive = true) - { - var numberValidator = new NumberValidator(10, 5, onlyPositive); - - var actual = numberValidator.IsValidNumber(value); - - actual.Should().BeTrue(); + actual.Should().Be(expectedResult); } } \ No newline at end of file From 4ce8a146f01451596233aac7aaa7042f0078b1f0 Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Wed, 6 Nov 2024 20:53:09 +0500 Subject: [PATCH 15/16] remove underscore from TestNames --- .../Homework/2. NumberValidator/NumberValidatorTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index f09235e..2b640a3 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -6,10 +6,10 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [TestCase(-1, 5, TestName = "_IfPrecisionIsNegative")] - [TestCase(5, -1, TestName = "_IfScaleIsNegative")] - [TestCase(2, 5, TestName = "_IfScaleGreaterPrecision")] - [TestCase(2, 2, TestName = "_IfScaleEqualPrecision")] + [TestCase(-1, 5, TestName = "IfPrecisionIsNegative")] + [TestCase(5, -1, TestName = "IfScaleIsNegative")] + [TestCase(2, 5, TestName = "IfScaleGreaterPrecision")] + [TestCase(2, 2, TestName = "IfScaleEqualPrecision")] public void ShouldThrowArgumentException(int precision, int scale) { var numberValidatorConstructor = () => new NumberValidator(precision, scale); From 5e0a291cccd0afb228d37359c0363e7437f9cbd1 Mon Sep 17 00:00:00 2001 From: S4MPAI Date: Wed, 6 Nov 2024 21:50:13 +0500 Subject: [PATCH 16/16] add NUnit ExpectedResult in TestCases --- .../NumberValidatorTests.cs | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 2b640a3..05b9af5 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -17,28 +17,26 @@ public void ShouldThrowArgumentException(int precision, int scale) numberValidatorConstructor.Should().Throw(); } - [TestCase(null, false, TestName = "IfValueIsNull")] - [TestCase(" ", false, TestName = "IfValueIsEmpty")] - [TestCase("2.a1", false, TestName = "IfValueContainsLetter")] - [TestCase("0.", false, TestName = "IfValueContainsSeparatorWithoutFracPart")] - [TestCase(",0", false, TestName = "IfValueWithoutIntPart")] - [TestCase("0*0", false, TestName = "IfValueSeparatorIncorrect")] - [TestCase("!0,0", false, TestName = "IfValueSignIncorrect")] - [TestCase("1,234", false, TestName = "IfValueFracPartMoreThanScale")] - [TestCase("11111", false, TestName = "IfValueIntPartMoreThanPrecision")] - [TestCase("225,32", false, TestName = "IfValueWithFracPartMoreThanPrecision")] - [TestCase("+14,23", false, TestName = "IfValueWithSignMoreThanPrecision")] - [TestCase("-0", false, TestName = "IfOnlyPositiveTrueAndValueContainsMinus")] - [TestCase("-1,4", true, false, TestName = "IfOnlyPositiveFalseAndValueContainsMinus")] - [TestCase("1", true, TestName = "IfValueIntPart")] - [TestCase("+1,44", true, TestName = "IfValueContainsFracPartAndSign")] - [TestCase("15,44", true, TestName = "IfValueContainsFracPart")] - public void IsValidNumber_ShouldReturnExpectedResult(string value, bool expectedResult, bool onlyPositive = true) + [TestCase(null, ExpectedResult = false, TestName = "IfValueIsNull")] + [TestCase(" ", ExpectedResult = false, TestName = "IfValueIsEmpty")] + [TestCase("2.a1", ExpectedResult = false, TestName = "IfValueContainsLetter")] + [TestCase("0.", ExpectedResult = false, TestName = "IfValueContainsSeparatorWithoutFracPart")] + [TestCase(",0", ExpectedResult = false, TestName = "IfValueWithoutIntPart")] + [TestCase("0*0", ExpectedResult = false, TestName = "IfValueSeparatorIncorrect")] + [TestCase("!0,0", ExpectedResult = false, TestName = "IfValueSignIncorrect")] + [TestCase("1,234", ExpectedResult = false, TestName = "IfValueFracPartMoreThanScale")] + [TestCase("11111", ExpectedResult = false, TestName = "IfValueIntPartMoreThanPrecision")] + [TestCase("225,32", ExpectedResult = false, TestName = "IfValueWithFracPartMoreThanPrecision")] + [TestCase("+14,23", ExpectedResult = false, TestName = "IfValueWithSignMoreThanPrecision")] + [TestCase("-0", ExpectedResult = false, TestName = "IfOnlyPositiveTrueAndValueContainsMinus")] + [TestCase("-1,4", false, ExpectedResult = true, TestName = "IfOnlyPositiveFalseAndValueContainsMinus")] + [TestCase("1", ExpectedResult = true, TestName = "IfValueIntPart")] + [TestCase("+1,44", ExpectedResult = true, TestName = "IfValueContainsFracPartAndSign")] + [TestCase("15,44", ExpectedResult = true, TestName = "IfValueContainsFracPart")] + public bool IsValidNumber_ShouldReturnExpectedResult(string value, bool onlyPositive = true) { var numberValidator = new NumberValidator(4, 2, onlyPositive); - var actual = numberValidator.IsValidNumber(value); - - actual.Should().Be(expectedResult); + return numberValidator.IsValidNumber(value); } } \ No newline at end of file