From 1b9c8daf73127847c3ec29978fb0c190332b020b Mon Sep 17 00:00:00 2001 From: ShpakovDaniel <44341164+Reltig@users.noreply.github.com> Date: Fri, 24 Nov 2023 10:50:31 +0500 Subject: [PATCH 1/6] add two variants of test --- cs/HomeExercises/ObjectComparison.cs | 37 ++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index 44d9aed4..6be6766e 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -1,4 +1,6 @@ -using FluentAssertions; +using System; +using FluentAssertions; +using FluentAssertions.Equivalency; using NUnit.Framework; namespace HomeExercises @@ -16,15 +18,29 @@ public void CheckCurrentTsar() 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); + + // 1 вариант, эквивалентен тесту CheckCurrentTsar + actualTsar.Should().BeEquivalentTo(expectedTsar, options => + options + .Excluding(p => p.Id) + .Excluding(p => p.Parent!.Id) + ); + // 2 вариант, эквивалентен тесту CheckCurrentTsar_WithCustomEquality + actualTsar.Should().BeEquivalentTo(expectedTsar, options => + options + .Excluding((IMemberInfo info) => + info.SelectedMemberInfo.DeclaringType == typeof(Person) + && info.SelectedMemberInfo.Name == nameof(Person.Id)) + ); + // 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); } [Test] @@ -35,6 +51,7 @@ 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)); + // Какие недостатки у такого подхода? Assert.True(AreEqual(actualTsar, expectedTsar)); } From 1e73cecb9da78c8e1eb1ba517e81d0a8fd3c505b Mon Sep 17 00:00:00 2001 From: ShpakovDaniel <44341164+Reltig@users.noreply.github.com> Date: Fri, 24 Nov 2023 20:12:53 +0500 Subject: [PATCH 2/6] choose assertions variant --- cs/HomeExercises/ObjectComparison.cs | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index 6be6766e..55b18310 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -18,29 +18,13 @@ public void CheckCurrentTsar() new Person("Vasili III of Russia", 28, 170, 60, null)); // Перепишите код на использование Fluent Assertions. - - // 1 вариант, эквивалентен тесту CheckCurrentTsar - actualTsar.Should().BeEquivalentTo(expectedTsar, options => - options - .Excluding(p => p.Id) - .Excluding(p => p.Parent!.Id) - ); - // 2 вариант, эквивалентен тесту CheckCurrentTsar_WithCustomEquality actualTsar.Should().BeEquivalentTo(expectedTsar, options => options + .IgnoringCyclicReferences() .Excluding((IMemberInfo info) => info.SelectedMemberInfo.DeclaringType == typeof(Person) && info.SelectedMemberInfo.Name == nameof(Person.Id)) ); - // 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); } [Test] @@ -53,6 +37,11 @@ public void CheckCurrentTsar_WithCustomEquality() // Какие недостатки у такого подхода? + + // 1. Вероятность StackOverflow из-за бесконечной рекурсии + // 2. При каждом изменении свойств или добавлении новых функцию придётся переписывать + // 3. Данный код не выполняет никакой полезной функции кроме сравнения полей двух объектов + // 4. Нарушение SRP, ответственность за сравнение объетов должна быть инкапсулирована в класс Assert.True(AreEqual(actualTsar, expectedTsar)); } From 2327296640140a361359c77fa56b5df335930044 Mon Sep 17 00:00:00 2001 From: ShpakovDaniel <44341164+Reltig@users.noreply.github.com> Date: Fri, 24 Nov 2023 20:14:30 +0500 Subject: [PATCH 3/6] rewrite tests --- cs/HomeExercises/NumberValidatorTests.cs | 56 ++++++++++++++++-------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index a2878113..416cd1f7 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -7,26 +7,46 @@ namespace HomeExercises { public class NumberValidatorTests { - [Test] - public void Test() + [TestCase(1, 0,"0", TestName = "int zero")] + [TestCase(2, 0,"+0", TestName = "positive int zero")] + [TestCase(2, 0,"-0", false , TestName = "negative int zero")] + [TestCase(2, 1,"0.0", TestName = "zero with fractional part")] + [TestCase(3, 2,"1.23", TestName = "positive non zero without sign with fractional part")] + [TestCase(4, 2,"+1.23", TestName = "positive non zero with sign and fractional part")] + [TestCase(4, 2,"-1.23", false, TestName = "negative non zero with sign and fractional part")] + public void IsValid(int precision, int scale, string validatingString, bool onlyPositive = true) { - 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)); + new NumberValidator(precision, scale, onlyPositive) + .IsValidNumber(validatingString) + .Should() + .BeTrue(); + } + + [TestCase(2, 1,"0.00", TestName = "zero intPart + fracPart should be less than precesion")] + [TestCase(3, 2, "+0.00", TestName = "zero intPart + fracPart + \"+\" should be less than precesion")] + [TestCase(3, 2, "-0.00", false, TestName = "zero intPart + fracPart + \"-\" should be less than precesion")] + [TestCase(3, 2,"+1.23", TestName = "positive non zero intPart + fracPart + \"+\" should be less than precesion")] + [TestCase(3, 2,"-1.23", false, TestName = "negative non zero intPart + fracPart + \"+\" should be less than precesion")] + [TestCase(3, 2, "a.sd", TestName = "non digit symbols")] + [TestCase(2, 1, ".0", TestName = "must have digits before point")] + [TestCase(1, 0, "0.", TestName = "must have digits after point (if exist)")] + public void IsNotValid(int precision, int scale, string validatingString, bool onlyPositive = true) + { + new NumberValidator(precision, scale, onlyPositive) + .IsValidNumber(validatingString) + .Should() + .BeFalse(); + } - 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")); + [TestCase(-1, 1, TestName = "negative precision")] + [TestCase(1, -1, TestName = "negative scale")] + [TestCase(-1, -1, TestName = "negative precision and scale")] + [TestCase(1, 1, TestName = "precision equals scale")] + [TestCase(1, 2, TestName = "precision less than scale")] + public void ShouldThrow(int precision, int scale, bool onlyPositive = true) + { + Action act = () => new NumberValidator(precision, scale, onlyPositive); + act.Should().Throw(); } } From 89dae09e3a2692fdab8e05233bfd8604af615eff Mon Sep 17 00:00:00 2001 From: ShpakovDaniel <44341164+Reltig@users.noreply.github.com> Date: Mon, 27 Nov 2023 18:23:01 +0500 Subject: [PATCH 4/6] upd --- cs/HomeExercises/NumberValidatorTests.cs | 120 +++++++++++++++++++---- cs/HomeExercises/ObjectComparison.cs | 14 +-- 2 files changed, 110 insertions(+), 24 deletions(-) diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index 416cd1f7..ac238cfe 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -1,4 +1,6 @@ using System; +using System.Collections; +using System.Linq; using System.Text.RegularExpressions; using FluentAssertions; using NUnit.Framework; @@ -7,26 +9,108 @@ namespace HomeExercises { public class NumberValidatorTests { - [TestCase(1, 0,"0", TestName = "int zero")] - [TestCase(2, 0,"+0", TestName = "positive int zero")] - [TestCase(2, 0,"-0", false , TestName = "negative int zero")] - [TestCase(2, 1,"0.0", TestName = "zero with fractional part")] - [TestCase(3, 2,"1.23", TestName = "positive non zero without sign with fractional part")] - [TestCase(4, 2,"+1.23", TestName = "positive non zero with sign and fractional part")] - [TestCase(4, 2,"-1.23", false, TestName = "negative non zero with sign and fractional part")] - public void IsValid(int precision, int scale, string validatingString, bool onlyPositive = true) + #region TestCases + + public static object[][] numberTestCases = { - new NumberValidator(precision, scale, onlyPositive) - .IsValidNumber(validatingString) - .Should() - .BeTrue(); + new object[] { 1, 0, "0" }, + new object[] { 2, 1, "0.0" }, + new object[] { 1, 0, "1" }, + new object[] { 2, 0, "12" }, + new object[] { 2, 1, "1.2" }, + new object[] { 3, 2, "1.23" }, + new object[] { 3, 1, "12.3" }, + new object[] { 4, 2, "12.34" }, + }; + + public static IEnumerable CorrectSignedNumbersCases() + { + int precision, scale; + string validatingString; + foreach (var testCase in numberTestCases) + { + precision = (int)testCase[0]; + scale = (int)testCase[1]; + validatingString = (string)testCase[2]; + yield return new TestCaseData(precision + 1, scale, $"+{validatingString}").Returns(true); + yield return new TestCaseData(precision + 1, scale, $"-{validatingString}").Returns(true); + } + } + + public static IEnumerable CorrectUnsignedNumbersCases() + { + foreach (var testCase in numberTestCases) + { + yield return new TestCaseData(testCase).Returns(true); + } + } + + public static IEnumerable IncorrectUnsignedNumbersCases() + { + int precision, scale; + string validatingString; + foreach (var testCase in numberTestCases) + { + precision = (int)testCase[0]; + scale = (int)testCase[1]; + validatingString = (string)testCase[2]; + if (precision > 1 && precision - 1 > scale) + yield return new TestCaseData(precision - 1, scale, validatingString).Returns(false); + if (scale > 0) + yield return new TestCaseData(testCase[0], scale - 1, validatingString).Returns(false); + } + } + + public static IEnumerable IncorrectSignedNumbersCases() + { + int precision, scale; + string validatingString; + foreach (var testCase in numberTestCases) + { + precision = (int)testCase[0]; + scale = (int)testCase[1]; + validatingString = (string)testCase[2]; + yield return new TestCaseData(precision, scale, $"+{validatingString}").Returns(false); + yield return new TestCaseData(precision, scale, $"-{validatingString}").Returns(false); + if (scale > 0 && precision > scale) + { + yield return new TestCaseData(precision + 1, scale - 1, $"+{validatingString}") + .Returns(false); + yield return new TestCaseData(precision + 1, scale - 1, $"-{validatingString}") + .Returns(false); + } + } } - - [TestCase(2, 1,"0.00", TestName = "zero intPart + fracPart should be less than precesion")] - [TestCase(3, 2, "+0.00", TestName = "zero intPart + fracPart + \"+\" should be less than precesion")] - [TestCase(3, 2, "-0.00", false, TestName = "zero intPart + fracPart + \"-\" should be less than precesion")] - [TestCase(3, 2,"+1.23", TestName = "positive non zero intPart + fracPart + \"+\" should be less than precesion")] - [TestCase(3, 2,"-1.23", false, TestName = "negative non zero intPart + fracPart + \"+\" should be less than precesion")] + + public static IEnumerable NegativeNumbers() + { + int precision; + string validatingString; + foreach (var testCase in numberTestCases) + { + precision = (int)testCase[0]; + validatingString = (string)testCase[2]; + yield return new TestCaseData(precision + 1, testCase[1], $"-{validatingString}").Returns(false); + } + } + + #endregion + + [TestCaseSource(nameof(CorrectSignedNumbersCases))] + [TestCaseSource(nameof(CorrectUnsignedNumbersCases))] + [TestCaseSource(nameof(IncorrectUnsignedNumbersCases))] + [TestCaseSource(nameof(IncorrectSignedNumbersCases))] + public bool ValidationTests(int precision, int scale, string validatingString) + { + return new NumberValidator(precision, scale).IsValidNumber(validatingString); + } + + [TestCaseSource(nameof(NegativeNumbers))] + public bool ShouldFailWhenNumberNegativeWithOnlyPositive(int precision, int scale, string validatingString) + { + return new NumberValidator(precision, scale, true).IsValidNumber(validatingString); + } + [TestCase(3, 2, "a.sd", TestName = "non digit symbols")] [TestCase(2, 1, ".0", TestName = "must have digits before point")] [TestCase(1, 0, "0.", TestName = "must have digits after point (if exist)")] diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index 55b18310..13949f03 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -18,10 +18,10 @@ public void CheckCurrentTsar() new Person("Vasili III of Russia", 28, 170, 60, null)); // Перепишите код на использование Fluent Assertions. - actualTsar.Should().BeEquivalentTo(expectedTsar, options => + actualTsar.Should().BeEquivalentTo(expectedTsar, options => options .IgnoringCyclicReferences() - .Excluding((IMemberInfo info) => + .Excluding((IMemberInfo info) => info.SelectedMemberInfo.DeclaringType == typeof(Person) && info.SelectedMemberInfo.Name == nameof(Person.Id)) ); @@ -35,13 +35,15 @@ 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)); - + // Какие недостатки у такого подхода? - + // 1. Вероятность StackOverflow из-за бесконечной рекурсии // 2. При каждом изменении свойств или добавлении новых функцию придётся переписывать - // 3. Данный код не выполняет никакой полезной функции кроме сравнения полей двух объектов - // 4. Нарушение SRP, ответственность за сравнение объетов должна быть инкапсулирована в класс + // 3. Нарушение SRP, ответственность за сравнение объетов должна быть инкапсулирована в класс + // 4. Тест не показывает, по каким критериям различаются два объекта + // 5. Название теста не говорит о конкрутных различиях этого теста с предыдущим + Assert.True(AreEqual(actualTsar, expectedTsar)); } From 25ca7c7d123d933169a1cebaa64b9bcfd352051e Mon Sep 17 00:00:00 2001 From: ShpakovDaniel <44341164+Reltig@users.noreply.github.com> Date: Tue, 28 Nov 2023 18:18:35 +0500 Subject: [PATCH 5/6] upd 2 --- cs/HomeExercises/NumberValidatorTests.cs | 145 +++++++++-------------- 1 file changed, 59 insertions(+), 86 deletions(-) diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index ac238cfe..a1d7b802 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -9,112 +9,86 @@ namespace HomeExercises { public class NumberValidatorTests { - #region TestCases - - public static object[][] numberTestCases = + public static TestCaseData[] unsignedPositiveNumberTestCases = { - new object[] { 1, 0, "0" }, - new object[] { 2, 1, "0.0" }, - new object[] { 1, 0, "1" }, - new object[] { 2, 0, "12" }, - new object[] { 2, 1, "1.2" }, - new object[] { 3, 2, "1.23" }, - new object[] { 3, 1, "12.3" }, - new object[] { 4, 2, "12.34" }, + new TestCaseData(1, 0, "0").SetName("unsigned zero"), + new TestCaseData(2, 1, "0.0").SetName("unsigned zero with 1 digit after"), + new TestCaseData(1, 0, "1").SetName("unsigned 1 digit number"), + new TestCaseData(2, 0, "12").SetName("unsigned 2 digit number"), + new TestCaseData(2, 1, "1.2").SetName("unsigned 2 digit number with 1 digit after point"), + new TestCaseData(3, 2, "1.23").SetName("unsigned 3 digit number with 2 digit after point"), + new TestCaseData(3, 1, "12.3").SetName("unsigned 3 digit number with 1 digit after point"), + new TestCaseData(4, 2, "12.34").SetName("unsigned 4 digit number with 2 digit after point"), }; - public static IEnumerable CorrectSignedNumbersCases() - { - int precision, scale; - string validatingString; - foreach (var testCase in numberTestCases) - { - precision = (int)testCase[0]; - scale = (int)testCase[1]; - validatingString = (string)testCase[2]; - yield return new TestCaseData(precision + 1, scale, $"+{validatingString}").Returns(true); - yield return new TestCaseData(precision + 1, scale, $"-{validatingString}").Returns(true); - } - } - - public static IEnumerable CorrectUnsignedNumbersCases() + public static TestCaseData[] signedPositiveNumberTestCases = { - foreach (var testCase in numberTestCases) - { - yield return new TestCaseData(testCase).Returns(true); - } - } + new TestCaseData(2, 0, "+0").SetName("positive zero"), + new TestCaseData(3, 1, "+0.0").SetName("positive zero with 1 digit after"), + new TestCaseData(2, 0, "+1").SetName("positive 1 digit number"), + new TestCaseData(3, 0, "+12").SetName("positive 2 digit number"), + new TestCaseData(3, 1, "+1.2").SetName("positive 2 digit number with 1 digit after point"), + new TestCaseData(4, 2, "+1.23").SetName("positive 3 digit number with 2 digit after point"), + new TestCaseData(4, 1, "+12.3").SetName("positive 3 digit number with 1 digit after point"), + new TestCaseData(5, 2, "+12.34").SetName("positive 4 digit number with 2 digit after point"), + }; - public static IEnumerable IncorrectUnsignedNumbersCases() + public static TestCaseData[] negativeNumberTestCases = { - int precision, scale; - string validatingString; - foreach (var testCase in numberTestCases) - { - precision = (int)testCase[0]; - scale = (int)testCase[1]; - validatingString = (string)testCase[2]; - if (precision > 1 && precision - 1 > scale) - yield return new TestCaseData(precision - 1, scale, validatingString).Returns(false); - if (scale > 0) - yield return new TestCaseData(testCase[0], scale - 1, validatingString).Returns(false); - } - } + new TestCaseData(2, 0, "-0").SetName("negative zero"), + new TestCaseData(3, 1, "-0.0").SetName("negative zero with 1 digit after"), + new TestCaseData(2, 0, "-1").SetName("negative 1 digit number"), + new TestCaseData(3, 0, "-12").SetName("negative 2 digit number"), + new TestCaseData(3, 1, "-1.2").SetName("negative 2 digit number with 1 digit after point"), + new TestCaseData(4, 2, "-1.23").SetName("negative 3 digit number with 2 digit after point"), + new TestCaseData(4, 1, "-12.3").SetName("negative 3 digit number with 1 digit after point"), + new TestCaseData(5, 2, "-12.34").SetName("negative 4 digit number with 2 digit after point"), + }; - public static IEnumerable IncorrectSignedNumbersCases() + public static TestCaseData[] wrongCases = { - int precision, scale; - string validatingString; - foreach (var testCase in numberTestCases) - { - precision = (int)testCase[0]; - scale = (int)testCase[1]; - validatingString = (string)testCase[2]; - yield return new TestCaseData(precision, scale, $"+{validatingString}").Returns(false); - yield return new TestCaseData(precision, scale, $"-{validatingString}").Returns(false); - if (scale > 0 && precision > scale) - { - yield return new TestCaseData(precision + 1, scale - 1, $"+{validatingString}") - .Returns(false); - yield return new TestCaseData(precision + 1, scale - 1, $"-{validatingString}") - .Returns(false); - } - } - } + new TestCaseData(1, 0, "-1") + .SetName("integer number with \"+\" sign must have precision more than digits count").Returns(false), + new TestCaseData(1, 0, "+1") + .SetName("integer number with \"-\" sign must have precision more than digits count").Returns(false), + new TestCaseData(2, 1, "+1.2") + .SetName("number with fractional part with \"+\" sign must have precision more than digits count") + .Returns(false), + new TestCaseData(2, 1, "-1.2") + .SetName("number with fractional part with \"-\" sign must have precision more than digits count") + .Returns(false), + }; - public static IEnumerable NegativeNumbers() + [TestCaseSource(nameof(unsignedPositiveNumberTestCases))] + [TestCaseSource(nameof(signedPositiveNumberTestCases))] + [TestCaseSource(nameof(negativeNumberTestCases))] + public void IsValidNumber(int precision, int scale, string validatingString) { - int precision; - string validatingString; - foreach (var testCase in numberTestCases) - { - precision = (int)testCase[0]; - validatingString = (string)testCase[2]; - yield return new TestCaseData(precision + 1, testCase[1], $"-{validatingString}").Returns(false); - } + new NumberValidator(precision, scale) + .IsValidNumber(validatingString) + .Should() + .BeTrue(); } - #endregion - - [TestCaseSource(nameof(CorrectSignedNumbersCases))] - [TestCaseSource(nameof(CorrectUnsignedNumbersCases))] - [TestCaseSource(nameof(IncorrectUnsignedNumbersCases))] - [TestCaseSource(nameof(IncorrectSignedNumbersCases))] - public bool ValidationTests(int precision, int scale, string validatingString) + [TestCaseSource(nameof(wrongCases))] + public bool IsNotValidNumber(int precision, int scale, string validatingString) { return new NumberValidator(precision, scale).IsValidNumber(validatingString); } - [TestCaseSource(nameof(NegativeNumbers))] - public bool ShouldFailWhenNumberNegativeWithOnlyPositive(int precision, int scale, string validatingString) + [TestCaseSource(nameof(negativeNumberTestCases))] + public void ShouldFailWhenNumberNegativeWithOnlyPositive(int precision, int scale, string validatingString) { - return new NumberValidator(precision, scale, true).IsValidNumber(validatingString); + new NumberValidator(precision, scale, true) + .IsValidNumber(validatingString) + .Should() + .BeFalse(); } [TestCase(3, 2, "a.sd", TestName = "non digit symbols")] [TestCase(2, 1, ".0", TestName = "must have digits before point")] [TestCase(1, 0, "0.", TestName = "must have digits after point (if exist)")] - public void IsNotValid(int precision, int scale, string validatingString, bool onlyPositive = true) + public void WrongFormat(int precision, int scale, string validatingString, bool onlyPositive = true) { new NumberValidator(precision, scale, onlyPositive) .IsValidNumber(validatingString) @@ -129,8 +103,7 @@ public void IsNotValid(int precision, int scale, string validatingString, bool o [TestCase(1, 2, TestName = "precision less than scale")] public void ShouldThrow(int precision, int scale, bool onlyPositive = true) { - Action act = () => new NumberValidator(precision, scale, onlyPositive); - act.Should().Throw(); + Assert.Throws(() => new NumberValidator(precision, scale, onlyPositive)); } } From 86082c25aa0138abfb158744c344d3cc93077986 Mon Sep 17 00:00:00 2001 From: ShpakovDaniel <44341164+Reltig@users.noreply.github.com> Date: Wed, 29 Nov 2023 22:36:07 +0500 Subject: [PATCH 6/6] upd 2.1 --- cs/HomeExercises/NumberValidatorTests.cs | 77 ++++++++++++------------ 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index a1d7b802..2d660268 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -7,55 +7,56 @@ namespace HomeExercises { + [TestFixture] public class NumberValidatorTests { public static TestCaseData[] unsignedPositiveNumberTestCases = { - new TestCaseData(1, 0, "0").SetName("unsigned zero"), - new TestCaseData(2, 1, "0.0").SetName("unsigned zero with 1 digit after"), - new TestCaseData(1, 0, "1").SetName("unsigned 1 digit number"), - new TestCaseData(2, 0, "12").SetName("unsigned 2 digit number"), - new TestCaseData(2, 1, "1.2").SetName("unsigned 2 digit number with 1 digit after point"), - new TestCaseData(3, 2, "1.23").SetName("unsigned 3 digit number with 2 digit after point"), - new TestCaseData(3, 1, "12.3").SetName("unsigned 3 digit number with 1 digit after point"), - new TestCaseData(4, 2, "12.34").SetName("unsigned 4 digit number with 2 digit after point"), + new TestCaseData(1, 0, "0").SetName("When_UnsignedZero"), + new TestCaseData(2, 1, "0.0").SetName("When_UnsignedZero_WithOneDigitAfter"), + new TestCaseData(1, 0, "1").SetName("When_UnsignedOneDigitNumber"), + new TestCaseData(2, 0, "12").SetName("When_UnsignedTwoDigitNumber"), + new TestCaseData(2, 1, "1.2").SetName("When_UnsignedTwoDigitNumber_WithOneDigitAfterPoint"), + new TestCaseData(3, 2, "1.23").SetName("When_UnsignedThreeDigitNumber_WithTwoDigitsAfterPoint"), + new TestCaseData(3, 1, "12.3").SetName("When_UnsignedThreeDigitNumber_WithOneDigitAfterPoint"), + new TestCaseData(4, 2, "12.34").SetName("When_UnsignedFourDigitNumber_WithTwoDigitsAfterPoint"), }; public static TestCaseData[] signedPositiveNumberTestCases = { - new TestCaseData(2, 0, "+0").SetName("positive zero"), - new TestCaseData(3, 1, "+0.0").SetName("positive zero with 1 digit after"), - new TestCaseData(2, 0, "+1").SetName("positive 1 digit number"), - new TestCaseData(3, 0, "+12").SetName("positive 2 digit number"), - new TestCaseData(3, 1, "+1.2").SetName("positive 2 digit number with 1 digit after point"), - new TestCaseData(4, 2, "+1.23").SetName("positive 3 digit number with 2 digit after point"), - new TestCaseData(4, 1, "+12.3").SetName("positive 3 digit number with 1 digit after point"), - new TestCaseData(5, 2, "+12.34").SetName("positive 4 digit number with 2 digit after point"), + new TestCaseData(2, 0, "+0").SetName("When_PositiveZero"), + new TestCaseData(3, 1, "+0.0").SetName("When_PositiveZero_WithOneDigitAfter"), + new TestCaseData(2, 0, "+1").SetName("When_PositiveOneDigitNumber"), + new TestCaseData(3, 0, "+12").SetName("When_PositiveTwoDigitNumber"), + new TestCaseData(3, 1, "+1.2").SetName("When_PositiveTwoDigitNumber_WithOneDigitAfterPoint"), + new TestCaseData(4, 2, "+1.23").SetName("When_PositiveThreeDigitNumber_WithTwoDigitsAfterPoint"), + new TestCaseData(4, 1, "+12.3").SetName("When_PositiveThreeDigitNumber_WithOneDigitAfterPoint"), + new TestCaseData(5, 2, "+12.34").SetName("When_PositiveFourDigitNumber_WithTwoDigitsAfterPoint"), }; public static TestCaseData[] negativeNumberTestCases = { - new TestCaseData(2, 0, "-0").SetName("negative zero"), - new TestCaseData(3, 1, "-0.0").SetName("negative zero with 1 digit after"), - new TestCaseData(2, 0, "-1").SetName("negative 1 digit number"), - new TestCaseData(3, 0, "-12").SetName("negative 2 digit number"), - new TestCaseData(3, 1, "-1.2").SetName("negative 2 digit number with 1 digit after point"), - new TestCaseData(4, 2, "-1.23").SetName("negative 3 digit number with 2 digit after point"), - new TestCaseData(4, 1, "-12.3").SetName("negative 3 digit number with 1 digit after point"), - new TestCaseData(5, 2, "-12.34").SetName("negative 4 digit number with 2 digit after point"), + new TestCaseData(2, 0, "-0").SetName("When_NegativeZero"), + new TestCaseData(3, 1, "-0.0").SetName("When_NegativeZero_WithOneDigitAfter"), + new TestCaseData(2, 0, "-1").SetName("When_NegativeOneDigitNumber"), + new TestCaseData(3, 0, "-12").SetName("When_NegativeTwoDigitNumber"), + new TestCaseData(3, 1, "-1.2").SetName("When_NegativeTwoDigitNumber_WithOneDigitAfterPoint"), + new TestCaseData(4, 2, "-1.23").SetName("When_NegativeThreeDigitNumber_WithTwoDigitsAfterPoint"), + new TestCaseData(4, 1, "-12.3").SetName("When_NegativeThreeDigitNumber_WithOneDigitAfterPoint"), + new TestCaseData(5, 2, "-12.34").SetName("When_NegativeFourDigitNumber_WithTwoDigitsAfterPoint"), }; public static TestCaseData[] wrongCases = { new TestCaseData(1, 0, "-1") - .SetName("integer number with \"+\" sign must have precision more than digits count").Returns(false), + .SetName("When_NegativeIntegerNumber_HavePrecisionLesserOrEqual_ThanDigitsCount").Returns(false), new TestCaseData(1, 0, "+1") - .SetName("integer number with \"-\" sign must have precision more than digits count").Returns(false), + .SetName("When_PositiveIntegerNumber_HavePrecisionLesserOrEqual_ThanDigitsCount").Returns(false), new TestCaseData(2, 1, "+1.2") - .SetName("number with fractional part with \"+\" sign must have precision more than digits count") + .SetName("When_PositiveNumberWithFractionalPart_HavePrecisionLesserOrEqual_ThanDigitsCount") .Returns(false), new TestCaseData(2, 1, "-1.2") - .SetName("number with fractional part with \"-\" sign must have precision more than digits count") + .SetName("When_NegativeNumberWithFractionalPart_HavePrecisionLesserOrEqual_ThanDigitsCount") .Returns(false), }; @@ -77,7 +78,7 @@ public bool IsNotValidNumber(int precision, int scale, string validatingString) } [TestCaseSource(nameof(negativeNumberTestCases))] - public void ShouldFailWhenNumberNegativeWithOnlyPositive(int precision, int scale, string validatingString) + public void IsFailWithOnlyPositiveFlag(int precision, int scale, string validatingString) { new NumberValidator(precision, scale, true) .IsValidNumber(validatingString) @@ -85,10 +86,10 @@ public void ShouldFailWhenNumberNegativeWithOnlyPositive(int precision, int scal .BeFalse(); } - [TestCase(3, 2, "a.sd", TestName = "non digit symbols")] - [TestCase(2, 1, ".0", TestName = "must have digits before point")] - [TestCase(1, 0, "0.", TestName = "must have digits after point (if exist)")] - public void WrongFormat(int precision, int scale, string validatingString, bool onlyPositive = true) + [TestCase(3, 2, "a.sd", TestName = "WhenContain_NonDigitSymbols")] + [TestCase(2, 1, ".0", TestName = "WhenHaveNot_DigitBeforePoint")] + [TestCase(1, 0, "0.", TestName = "WhenHaveNot_DigitAfterPointIfExist")] + public void IsWrongFormat(int precision, int scale, string validatingString, bool onlyPositive = true) { new NumberValidator(precision, scale, onlyPositive) .IsValidNumber(validatingString) @@ -96,11 +97,11 @@ public void WrongFormat(int precision, int scale, string validatingString, bool .BeFalse(); } - [TestCase(-1, 1, TestName = "negative precision")] - [TestCase(1, -1, TestName = "negative scale")] - [TestCase(-1, -1, TestName = "negative precision and scale")] - [TestCase(1, 1, TestName = "precision equals scale")] - [TestCase(1, 2, TestName = "precision less than scale")] + [TestCase(-1, 1, TestName = "When_NegativePrecision")] + [TestCase(1, -1, TestName = "When_NegativeScale")] + [TestCase(-1, -1, TestName = "When_NegativePrecisionAndScale")] + [TestCase(1, 1, TestName = "When_PrecisionEqualsScale")] + [TestCase(1, 2, TestName = "When_PrecisionLessThanScale")] public void ShouldThrow(int precision, int scale, bool onlyPositive = true) { Assert.Throws(() => new NumberValidator(precision, scale, onlyPositive));