From 5e912133e85bf2c372e590f1ac205c6110981fae Mon Sep 17 00:00:00 2001 From: Dary Date: Mon, 4 Nov 2024 17:07:05 +0500 Subject: [PATCH 1/5] Refactored the CheckCurrentTsar method --- .../WordsStatisticsTests.cs | 74 ++++++++++++++++++- Testing/Basic/Classwork/2. TDD/Game.cs | 3 +- .../1. ObjectComparison/ObjectComparison.cs | 28 ++++--- 3 files changed, 90 insertions(+), 15 deletions(-) diff --git a/Testing/Basic/Classwork/1. WordsStatistics/WordsStatisticsTests.cs b/Testing/Basic/Classwork/1. WordsStatistics/WordsStatisticsTests.cs index 6109b10..1d10eca 100644 --- a/Testing/Basic/Classwork/1. WordsStatistics/WordsStatisticsTests.cs +++ b/Testing/Basic/Classwork/1. WordsStatistics/WordsStatisticsTests.cs @@ -9,13 +9,11 @@ namespace Basic.Task.WordsStatistics; [TestFixture] public class WordsStatisticsTests { - private IWordsStatistics wordsStatistics; [SetUp] public void SetUp() { - wordsStatistics = CreateStatistics(); } @@ -46,4 +44,76 @@ public void GetStatistics_ContainsManyItems_AfterAdditionOfDifferentWords() wordsStatistics.AddWord("def"); wordsStatistics.GetStatistics().Should().HaveCount(2); } + + [Test] + public void GetStatistics_ThrowArgumentNullException_AfterAdditionNull() + { + wordsStatistics + .Invoking(word => word.AddWord(null)) + .Should() + .Throw() + .WithMessage("Value cannot be null. (Parameter 'word')"); + } + + [Test] + public void GetStatistics_IsEmpty_AfterAdditionWhiteSpaceString() + { + wordsStatistics.AddWord(" "); + wordsStatistics.GetStatistics().Should().BeEmpty(); + } + + [Test] + public void GetStatistics_LengthWord_AfterAdditionStringLongerThenTen() + { + wordsStatistics.AddWord("very long string"); + wordsStatistics.GetStatistics().Should().Equal(new WordCount("very long ", 1)); + } + + [Test] + public void GetStatistics_Count_AfterAdditionSameWord() + { + for (var i = 0; i < 3; i++) + { + wordsStatistics.AddWord("word"); + } + + wordsStatistics.GetStatistics().First().Count.Should().Be(3); + } + + [Test] + public void GetStatistics_TypeOfWordCount_AfterAdditionWord() + { + wordsStatistics.AddWord("word"); + + wordsStatistics.GetStatistics().First().GetType().Should().Be(typeof(WordCount)); + } + + [Test] + public void GetStatistics_IsLowerWord_AfterAdditionWord() + { + wordsStatistics.AddWord("WORD"); + + wordsStatistics.GetStatistics().First().Word.Should().Be("word"); + } + + [Test] + public void GetStatistics_OrderByDescending_AfterAdditionDifferentWords() + { + wordsStatistics.AddWord("word3"); + + for (var i = 0; i < 2; i++) + { + wordsStatistics.AddWord("word2"); + } + + for (var i = 0; i < 3; i++) + { + wordsStatistics.AddWord("word1"); + } + + wordsStatistics + .GetStatistics() + .Should() + .Equal(new List{new ("word1", 3), new ("word2", 2), new ("word3", 1)}); + } } \ No newline at end of file diff --git a/Testing/Basic/Classwork/2. TDD/Game.cs b/Testing/Basic/Classwork/2. TDD/Game.cs index fbcbc6f..3798ca0 100644 --- a/Testing/Basic/Classwork/2. TDD/Game.cs +++ b/Testing/Basic/Classwork/2. TDD/Game.cs @@ -1,6 +1,7 @@ namespace TDD.Task; public class Game { + private int score = 0; public void Roll(int pins) { throw new NotImplementedException(); @@ -8,7 +9,7 @@ public void Roll(int pins) public int GetScore() { - throw new NotImplementedException(); + return score; } } diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..275797c 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -1,12 +1,13 @@ using NUnit.Framework; using NUnit.Framework.Legacy; +using FluentAssertions; namespace HomeExercise.Tasks.ObjectComparison; + public class ObjectComparison { [Test] [Description("Проверка текущего царя")] - [Category("ToRefactor")] public void CheckCurrentTsar() { var actualTsar = TsarRegistry.GetCurrentTsar(); @@ -14,16 +15,13 @@ public void CheckCurrentTsar() var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, new Person("Vasili III of Russia", 28, 170, 60, null)); - // Перепишите код на использование Fluent Assertions. - ClassicAssert.AreEqual(actualTsar.Name, expectedTsar.Name); - ClassicAssert.AreEqual(actualTsar.Age, expectedTsar.Age); - ClassicAssert.AreEqual(actualTsar.Height, expectedTsar.Height); - ClassicAssert.AreEqual(actualTsar.Weight, expectedTsar.Weight); - - ClassicAssert.AreEqual(expectedTsar.Parent!.Name, actualTsar.Parent!.Name); - ClassicAssert.AreEqual(expectedTsar.Parent.Age, actualTsar.Parent.Age); - ClassicAssert.AreEqual(expectedTsar.Parent.Height, actualTsar.Parent.Height); - ClassicAssert.AreEqual(expectedTsar.Parent.Parent, actualTsar.Parent.Parent); + actualTsar + .Should() + .BeEquivalentTo(expectedTsar, options => + options + .AllowingInfiniteRecursion() + .Excluding(o => o.Id) + .Excluding(o => o.Parent.Id)); } [Test] @@ -35,6 +33,12 @@ public void CheckCurrentTsar_WithCustomEquality() new Person("Vasili III of Russia", 28, 170, 60, null)); // Какие недостатки у такого подхода? + // При изменении класса Person придётся изменять метод AreEqual, + // т.е. если мы добавим поле в Person, то придётся добавить проверку на него в этом методе. + // При провале теста мы не получаем конкретной информации из-за чего тест не прошёл, + // т.к. метод AreEqual возвращает bool и скажет нам только Success или Failed. + // Невозможно сравнить двух Person без определённых полей, + // придётся создавать различные реализации метода AreEqual. ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); } @@ -49,4 +53,4 @@ private bool AreEqual(Person? actual, Person? expected) && actual.Weight == expected.Weight && AreEqual(actual.Parent, expected.Parent); } -} +} \ No newline at end of file From 25512ae0c405d17609f1021a43c48d72f9a22c47 Mon Sep 17 00:00:00 2001 From: Dary Date: Mon, 4 Nov 2024 18:50:35 +0500 Subject: [PATCH 2/5] Add tests for NumberValidator creation --- .../NumberValidatorTests.cs | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 950c9bc..b0090e3 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,6 +1,6 @@ - -using NUnit.Framework; +using NUnit.Framework; using NUnit.Framework.Legacy; +using FluentAssertions; namespace HomeExercise.Tasks.NumberValidator; @@ -10,17 +10,11 @@ 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")); @@ -28,4 +22,39 @@ public void Test() ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-1.23")); ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("a.sd")); } + + [Test] + public void NumberValidator_CorrectParameters_AfterCreating() + { + var creation = () => new NumberValidator(1, 0, true); + + creation.Should().NotThrow(); + } + + [Test] + public void NumberValidator_IncorrectPrecision_AfterCreating() + { + var creation = () => new NumberValidator(-1, 2); + + creation.Should() + .Throw() + .WithMessage("precision must be a positive number"); + } + + [Test] + public void NumberValidator_IncorrectScale_AfterCreating() + { + var creation = () => new NumberValidator(1, -1); + + creation.Should() + .Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + + var secondCreation = () => new NumberValidator(2, 1); + + secondCreation.Should() + .Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + } \ No newline at end of file From a494c2c0b2bcdf8c20143402aff8468f6c5c2cc2 Mon Sep 17 00:00:00 2001 From: Dary Date: Mon, 4 Nov 2024 22:31:02 +0500 Subject: [PATCH 3/5] implemented all tests and add testCase for all tests --- .../2. NumberValidator/NumberValidator.cs | 2 +- .../NumberValidatorTests.cs | 77 ++++++++++--------- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidator.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidator.cs index 327ce9c..9ec611c 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidator.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidator.cs @@ -17,7 +17,7 @@ public NumberValidator(int precision, int scale = 0, bool onlyPositive = false) if (precision <= 0) throw new ArgumentException("precision must be a positive number"); if (scale < 0 || scale >= precision) - throw new ArgumentException("precision must be a non-negative number less or equal than precision"); + throw new ArgumentException("scale must be a non-negative number less than precision"); numberRegex = new Regex(@"^([+-]?)(\d+)([.,](\d+))?$", RegexOptions.IgnoreCase); } diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index b0090e3..9f3525b 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,5 +1,4 @@ using NUnit.Framework; -using NUnit.Framework.Legacy; using FluentAssertions; namespace HomeExercise.Tasks.NumberValidator; @@ -7,54 +6,60 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [Test] - public void Test() + [TestCase(1, TestName = "precision 1, scale 0")] + public void NumberValidator_CorrectParameters_AfterCreatingCorrectValidator(int precision, int scale = 0) { - ClassicAssert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); - - ClassicAssert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("00.00")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-0.00")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+0.00")); - ClassicAssert.IsTrue(new NumberValidator(4, 2, true).IsValidNumber("+1.23")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+1.23")); - ClassicAssert.IsFalse(new NumberValidator(17, 2, true).IsValidNumber("0.000")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-1.23")); - ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("a.sd")); - } - - [Test] - public void NumberValidator_CorrectParameters_AfterCreating() - { - var creation = () => new NumberValidator(1, 0, true); + var creation = () => new NumberValidator(precision, scale, true); - creation.Should().NotThrow(); + creation.Should() + .NotThrow(); } - - [Test] - public void NumberValidator_IncorrectPrecision_AfterCreating() + + [TestCase(-1, TestName = "negative precision")] + [TestCase(0, TestName = "zero precision")] + public void NumberValidator_IncorrectPrecision_AfterCreatingIncorrectValidator(int precision, int scale = 0) { - var creation = () => new NumberValidator(-1, 2); + var creation = () => new NumberValidator(precision, scale); creation.Should() .Throw() .WithMessage("precision must be a positive number"); } - - [Test] - public void NumberValidator_IncorrectScale_AfterCreating() + + [TestCase(1, -1, TestName = "negative scale")] + [TestCase(1, 2, TestName = "scale greater than precision")] + [TestCase(1, 1, TestName = "scale equal to precision")] + public void NumberValidator_IncorrectScale_AfterCreatingIncorrectValidator(int precision, int scale) { - var creation = () => new NumberValidator(1, -1); + var creation = () => new NumberValidator(precision, scale); creation.Should() .Throw() - .WithMessage("precision must be a non-negative number less or equal than precision"); - - var secondCreation = () => new NumberValidator(2, 1); - - secondCreation.Should() - .Throw() - .WithMessage("precision must be a non-negative number less or equal than precision"); + .WithMessage("scale must be a non-negative number less than precision"); } + [TestCase(1, 0, true, "0", ExpectedResult = true, TestName = "precision 1, scale 0, only positive, result 0")] + [TestCase(2, 1, true, "0.1", ExpectedResult = true, TestName = "precision 2, scale 1, only positive, result 0.1")] + [TestCase(2, 1, true, "0,1", ExpectedResult = true, TestName = "precision 2, scale 1, only positive, result 0,1")] + [TestCase(2, 0, false, "-1", ExpectedResult = true, TestName = "precision 2, scale 0, not only positive, result -1")] + [TestCase(3, 1, false, "-1.1", ExpectedResult = true, TestName = "precision 3, scale 1, not only positive, result -1.1")] + [TestCase(3, 1, false, "-1,1", ExpectedResult = true, TestName = "precision 3, scale 1, not only positive, result -1,1")] + [TestCase(2, 0, true, "+1", ExpectedResult = true, TestName = "precision 2, scale 0, only positive, result +1")] + [TestCase(3, 1, true, "+1.1", ExpectedResult = true, TestName = "precision 3, scale 1, only positive, result +1.1")] + [TestCase(3, 1, true, "+1,1", ExpectedResult = true, TestName = "precision 3, scale 1, only positive, result +1,1")] + + [TestCase(2, 1, true, "", ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not empty string")] + [TestCase(2, 1, true, null, ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not null")] + [TestCase(2, 1, true, ".0", ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not .0")] + [TestCase(2, 1, true, "0.", ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not 0.")] + [TestCase(2, 0, true, "-1", ExpectedResult = false, TestName = "precision 2, scale 0, only positive, result not -1")] + [TestCase(1, 0, true, "+1", ExpectedResult = false, TestName = "precision 1, scale 0, only positive, result not +1")] + public bool IsValidNumber_VariousInput_AfterCreatingValidator( + int precision, + int scale, + bool onlyPositive, + string expectedResultValue) + { + return new NumberValidator(precision, scale, onlyPositive).IsValidNumber(expectedResultValue); + } } \ No newline at end of file From 383e96520e75d2c552b0694455d328aa092ae4c8 Mon Sep 17 00:00:00 2001 From: Dary Date: Wed, 6 Nov 2024 00:34:02 +0500 Subject: [PATCH 4/5] all tasks corrected --- .../1. ObjectComparison/ObjectComparison.cs | 9 +- .../NumberValidatorTests.cs | 86 ++++++++++++++----- 2 files changed, 69 insertions(+), 26 deletions(-) diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index 275797c..e13f8af 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -20,8 +20,7 @@ public void CheckCurrentTsar() .BeEquivalentTo(expectedTsar, options => options .AllowingInfiniteRecursion() - .Excluding(o => o.Id) - .Excluding(o => o.Parent.Id)); + .Excluding(o => o.DeclaringType == typeof(Person) && o.Name == nameof(Person.Id))); } [Test] @@ -44,8 +43,10 @@ public void CheckCurrentTsar_WithCustomEquality() private bool AreEqual(Person? actual, Person? expected) { - if (actual == expected) return true; - if (actual == null || expected == null) return false; + if (actual == expected) + return true; + if (actual == null || expected == null) + return false; return actual.Name == expected.Name && actual.Age == expected.Age diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 9f3525b..33ace18 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -6,8 +6,10 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [TestCase(1, TestName = "precision 1, scale 0")] - public void NumberValidator_CorrectParameters_AfterCreatingCorrectValidator(int precision, int scale = 0) + private static NumberValidator GetCorrectValidator(bool onlyPositive = false) => new(2, 1, onlyPositive); + + [TestCase(1, TestName = "Correct parameters")] + public void Constructor_WithCorrectParameters_NotThrows(int precision, int scale = 0) { var creation = () => new NumberValidator(precision, scale, true); @@ -17,7 +19,7 @@ public void NumberValidator_CorrectParameters_AfterCreatingCorrectValidator(int [TestCase(-1, TestName = "negative precision")] [TestCase(0, TestName = "zero precision")] - public void NumberValidator_IncorrectPrecision_AfterCreatingIncorrectValidator(int precision, int scale = 0) + public void Constructor_WithIncorrectPrecision_Throws(int precision, int scale = 0) { var creation = () => new NumberValidator(precision, scale); @@ -29,7 +31,7 @@ public void NumberValidator_IncorrectPrecision_AfterCreatingIncorrectValidator(i [TestCase(1, -1, TestName = "negative scale")] [TestCase(1, 2, TestName = "scale greater than precision")] [TestCase(1, 1, TestName = "scale equal to precision")] - public void NumberValidator_IncorrectScale_AfterCreatingIncorrectValidator(int precision, int scale) + public void Constructor_WithIncorrectScale_Throws(int precision, int scale) { var creation = () => new NumberValidator(precision, scale); @@ -38,28 +40,68 @@ public void NumberValidator_IncorrectScale_AfterCreatingIncorrectValidator(int p .WithMessage("scale must be a non-negative number less than precision"); } - [TestCase(1, 0, true, "0", ExpectedResult = true, TestName = "precision 1, scale 0, only positive, result 0")] - [TestCase(2, 1, true, "0.1", ExpectedResult = true, TestName = "precision 2, scale 1, only positive, result 0.1")] - [TestCase(2, 1, true, "0,1", ExpectedResult = true, TestName = "precision 2, scale 1, only positive, result 0,1")] - [TestCase(2, 0, false, "-1", ExpectedResult = true, TestName = "precision 2, scale 0, not only positive, result -1")] - [TestCase(3, 1, false, "-1.1", ExpectedResult = true, TestName = "precision 3, scale 1, not only positive, result -1.1")] - [TestCase(3, 1, false, "-1,1", ExpectedResult = true, TestName = "precision 3, scale 1, not only positive, result -1,1")] - [TestCase(2, 0, true, "+1", ExpectedResult = true, TestName = "precision 2, scale 0, only positive, result +1")] - [TestCase(3, 1, true, "+1.1", ExpectedResult = true, TestName = "precision 3, scale 1, only positive, result +1.1")] - [TestCase(3, 1, true, "+1,1", ExpectedResult = true, TestName = "precision 3, scale 1, only positive, result +1,1")] - - [TestCase(2, 1, true, "", ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not empty string")] - [TestCase(2, 1, true, null, ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not null")] - [TestCase(2, 1, true, ".0", ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not .0")] - [TestCase(2, 1, true, "0.", ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not 0.")] - [TestCase(2, 0, true, "-1", ExpectedResult = false, TestName = "precision 2, scale 0, only positive, result not -1")] - [TestCase(1, 0, true, "+1", ExpectedResult = false, TestName = "precision 1, scale 0, only positive, result not +1")] - public bool IsValidNumber_VariousInput_AfterCreatingValidator( + [TestCase(2, 0, true, "+1", TestName = "number with a plus")] + public void IsValidNumber_CorrectPlus_ReturnsTrue( + int precision, + int scale, + bool onlyPositive, + string expectedResultValue) + { + new NumberValidator(precision, scale, onlyPositive).IsValidNumber(expectedResultValue).Should().BeTrue(); + } + + [TestCase(2, 0, false, "-1", TestName = "number with a minus")] + public void IsValidNumber_CorrectMinus_ReturnsTrue( + int precision, + int scale, + bool onlyPositive, + string expectedResultValue) + { + new NumberValidator(precision, scale, onlyPositive).IsValidNumber(expectedResultValue).Should().BeTrue(); + } + + [TestCase(1, 0, true, "0", TestName = "one digit")] + [TestCase(3, 0, true, "123", TestName = "three digits")] + public void IsValidNumber_CorrectNumberOfDigits_ReturnsTrue( int precision, int scale, bool onlyPositive, string expectedResultValue) { - return new NumberValidator(precision, scale, onlyPositive).IsValidNumber(expectedResultValue); + new NumberValidator(precision, scale, onlyPositive).IsValidNumber(expectedResultValue).Should().BeTrue(); + } + + [TestCase("0.1", TestName = "separator dot")] + [TestCase("0,1", TestName = "separator comma")] + public void IsValidNumber_CorrectSeparator_ReturnsTrue(string expectedResultValue) + { + GetCorrectValidator().IsValidNumber(expectedResultValue).Should().BeTrue(); + } + + [TestCase("", TestName = "empty string")] + [TestCase(null, TestName = "null")] + public void IsValidNumber_IsNullOrEmpty_ReturnsFalse(string expectedResultValue) + { + GetCorrectValidator().IsValidNumber(expectedResultValue).Should().BeFalse(); + } + + [TestCase(".0", TestName = "intPart is missing")] + [TestCase("0.", TestName = "fracPart part is missing")] + public void IsValidNumber_MatchIsNotSuccess_ReturnsFalse(string expectedResultValue) + { + GetCorrectValidator().IsValidNumber(expectedResultValue).Should().BeFalse(); + } + + [TestCase("+11", TestName = "more numbers than precision (only intPart)")] + [TestCase("-1.1", TestName = "more numbers than precision (intPart and fracPart)")] + public void IsValidNumber_IncorrectPrecision_ReturnsFalse(string expectedResultValue) + { + GetCorrectValidator().IsValidNumber(expectedResultValue).Should().BeFalse(); + } + + [TestCase("0.11", TestName = "more digits in fracPart than scale")] + public void IsValidNumber_IncorrectScale_ReturnsFalse(string expectedResultValue) + { + GetCorrectValidator().IsValidNumber(expectedResultValue).Should().BeFalse(); } } \ No newline at end of file From 4ab39c204276d3bb6bc5d9400948d543d09603e4 Mon Sep 17 00:00:00 2001 From: Dary Date: Wed, 6 Nov 2024 16:24:05 +0500 Subject: [PATCH 5/5] New tests added --- .../NumberValidatorTests.cs | 111 ++++++++++++++---- 1 file changed, 86 insertions(+), 25 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 33ace18..2989579 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -6,8 +6,6 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - private static NumberValidator GetCorrectValidator(bool onlyPositive = false) => new(2, 1, onlyPositive); - [TestCase(1, TestName = "Correct parameters")] public void Constructor_WithCorrectParameters_NotThrows(int precision, int scale = 0) { @@ -40,8 +38,36 @@ public void Constructor_WithIncorrectScale_Throws(int precision, int scale) .WithMessage("scale must be a non-negative number less than precision"); } + [TestCase("+11", TestName = "less numbers than precision (intPart with plus)")] + [TestCase("-1.1", TestName = "less numbers than precision (intPart and fracPart with minus)")] + [TestCase("11", TestName = "less numbers than precision (only intPart)")] + [TestCase("1.1", TestName = "less numbers than precision (intPart and fracPart)")] + [TestCase("123", TestName = "numbers equals precision (only intPart)")] + [TestCase("1.23", TestName = "numbers equals precision (intPart and fracPart)")] + public void IsValidNumber_WithCorrectPrecision_ReturnsTrue(string expectedResultValue) + { + new NumberValidator(3, 2).IsValidNumber(expectedResultValue).Should().BeTrue(); + } + + [TestCase("+1", TestName = "number with plus")] + [TestCase("1", TestName = "number without sign")] + public void IsValidNumber_WithOnlyPositive_ReturnsTrue(string expectedResultValue) + { + new NumberValidator(2, 1, true).IsValidNumber(expectedResultValue).Should().BeTrue(); + } + + [TestCase("-1", TestName = "number with minus")] + [TestCase("+1", TestName = "number with plus")] + [TestCase("1", TestName = "number without sign")] + public void IsValidNumber_WithNotOnlyPositive_ReturnsTrue(string expectedResultValue) + { + new NumberValidator(2, 1).IsValidNumber(expectedResultValue).Should().BeTrue(); + } + [TestCase(2, 0, true, "+1", TestName = "number with a plus")] - public void IsValidNumber_CorrectPlus_ReturnsTrue( + [TestCase(3, 1, true, "+1.2", TestName = "number with a plus and separator dot")] + [TestCase(3, 1, true, "+1,2", TestName = "number with a plus and separator comma")] + public void IsValidNumber_WithCorrectPlus_ReturnsTrue( int precision, int scale, bool onlyPositive, @@ -50,58 +76,93 @@ public void IsValidNumber_CorrectPlus_ReturnsTrue( new NumberValidator(precision, scale, onlyPositive).IsValidNumber(expectedResultValue).Should().BeTrue(); } - [TestCase(2, 0, false, "-1", TestName = "number with a minus")] - public void IsValidNumber_CorrectMinus_ReturnsTrue( + [TestCase(2, 0, "-1", TestName = "number with a minus")] + [TestCase(3, 1, "-1.2", TestName = "number with a minus and separator dot")] + [TestCase(3, 1, "-1,2", TestName = "number with a minus and separator comma")] + public void IsValidNumber_WithCorrectMinus_ReturnsTrue( int precision, int scale, - bool onlyPositive, string expectedResultValue) { - new NumberValidator(precision, scale, onlyPositive).IsValidNumber(expectedResultValue).Should().BeTrue(); + new NumberValidator(precision, scale).IsValidNumber(expectedResultValue).Should().BeTrue(); } - [TestCase(1, 0, true, "0", TestName = "one digit")] - [TestCase(3, 0, true, "123", TestName = "three digits")] - public void IsValidNumber_CorrectNumberOfDigits_ReturnsTrue( + [TestCase(1, 0, "0", TestName = "one digit")] + [TestCase(3, 0, "123", TestName = "three digits")] + [TestCase(3, 1, "12.3", TestName = "three digits with one number after the dot")] + [TestCase(3, 2, "1.23", TestName = "three digits with two number after the dot")] + public void IsValidNumber_WithCorrectNumberOfDigits_ReturnsTrue( int precision, int scale, - bool onlyPositive, string expectedResultValue) { - new NumberValidator(precision, scale, onlyPositive).IsValidNumber(expectedResultValue).Should().BeTrue(); + new NumberValidator(precision, scale).IsValidNumber(expectedResultValue).Should().BeTrue(); } [TestCase("0.1", TestName = "separator dot")] [TestCase("0,1", TestName = "separator comma")] - public void IsValidNumber_CorrectSeparator_ReturnsTrue(string expectedResultValue) + public void IsValidNumber_WithCorrectSeparator_ReturnsTrue(string expectedResultValue) { - GetCorrectValidator().IsValidNumber(expectedResultValue).Should().BeTrue(); + new NumberValidator(2, 1).IsValidNumber(expectedResultValue).Should().BeTrue(); + } + + [TestCase("0.1", TestName = "separator dot")] + [TestCase("0,1", TestName = "separator comma")] + [TestCase("1", TestName = "number")] + [TestCase("+1", TestName = "number with plus")] + [TestCase("-1", TestName = "number with minus")] + public void IsValidNumber_WithStringCorrectFormat_ReturnsTrue(string expectedResultValue) + { + new NumberValidator(2, 1).IsValidNumber(expectedResultValue).Should().BeTrue(); } [TestCase("", TestName = "empty string")] [TestCase(null, TestName = "null")] - public void IsValidNumber_IsNullOrEmpty_ReturnsFalse(string expectedResultValue) + public void IsValidNumber_WithNullOrEmpty_ReturnsFalse(string expectedResultValue) { - GetCorrectValidator().IsValidNumber(expectedResultValue).Should().BeFalse(); + new NumberValidator(2, 1).IsValidNumber(expectedResultValue).Should().BeFalse(); } [TestCase(".0", TestName = "intPart is missing")] [TestCase("0.", TestName = "fracPart part is missing")] - public void IsValidNumber_MatchIsNotSuccess_ReturnsFalse(string expectedResultValue) + [TestCase("0.0.0", TestName = "more than one separator")] + [TestCase(" 1 ", TestName = "spaces on the sides")] + [TestCase("1 . 1", TestName = "spaces on the sides of the separator")] + [TestCase("1 1", TestName = "spaces between numbers")] + [TestCase("1'1", TestName = "foreign separator (apostrophe)")] + [TestCase("1`1", TestName = "foreign separator (backtick)")] + [TestCase("1~1", TestName = "foreign separator (tilde)")] + [TestCase("1/1", TestName = "foreign separator (slash)")] + [TestCase("a", TestName = "not a number")] + [TestCase("a.a", TestName = "not a number with separator dot")] + [TestCase("a,a", TestName = "not a number with separator comma")] + public void IsValidNumber_WithStringIncorrectFormat_ReturnsFalse(string expectedResultValue) + { + new NumberValidator(2, 1).IsValidNumber(expectedResultValue).Should().BeFalse(); + } + + [TestCase("+11", TestName = "more numbers than precision (intPart with plus)")] + [TestCase("-1.1", TestName = "more numbers than precision (intPart and fracPart with minus)")] + [TestCase("123", TestName = "more numbers than precision (only intPart)")] + [TestCase("1.23", TestName = "more numbers than precision (intPart and fracPart)")] + public void IsValidNumber_WithIncorrectPrecision_ReturnsFalse(string expectedResultValue) { - GetCorrectValidator().IsValidNumber(expectedResultValue).Should().BeFalse(); + new NumberValidator(2).IsValidNumber(expectedResultValue).Should().BeFalse(); } - [TestCase("+11", TestName = "more numbers than precision (only intPart)")] - [TestCase("-1.1", TestName = "more numbers than precision (intPart and fracPart)")] - public void IsValidNumber_IncorrectPrecision_ReturnsFalse(string expectedResultValue) + [TestCase("0.12", TestName = "more digits in fracPart than scale")] + [TestCase("+0.12", TestName = "more digits in fracPart than scale with plus")] + [TestCase("-0.12", TestName = "more digits in fracPart than scale with minus")] + public void IsValidNumber_WithIncorrectScale_ReturnsFalse(string expectedResultValue) { - GetCorrectValidator().IsValidNumber(expectedResultValue).Should().BeFalse(); + new NumberValidator(2, 1).IsValidNumber(expectedResultValue).Should().BeFalse(); } - [TestCase("0.11", TestName = "more digits in fracPart than scale")] - public void IsValidNumber_IncorrectScale_ReturnsFalse(string expectedResultValue) + [TestCase("-1", TestName = "number with minus")] + [TestCase("-1.1", TestName = "number with minus and separator dot")] + [TestCase("-1,1", TestName = "number with minus and separator comma")] + public void IsValidNumber_WithOnlyPositive_ReturnsFalse(string expectedResultValue) { - GetCorrectValidator().IsValidNumber(expectedResultValue).Should().BeFalse(); + new NumberValidator(2, 1, true).IsValidNumber(expectedResultValue).Should().BeFalse(); } } \ No newline at end of file