From 8a9c996605bdc8c869fdc639d485268b2922d885 Mon Sep 17 00:00:00 2001 From: Virtical <138823554+Virtical@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:27:32 +0500 Subject: [PATCH 1/4] ObjectComparison task has been solved --- .../1. ObjectComparison/ObjectComparison.cs | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..844b512 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -1,5 +1,6 @@ using NUnit.Framework; using NUnit.Framework.Legacy; +using FluentAssertions; namespace HomeExercise.Tasks.ObjectComparison; public class ObjectComparison @@ -14,16 +15,10 @@ 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 + .Excluding(x => x.Path.EndsWith("Id")) + .AllowingInfiniteRecursion()); } [Test] @@ -35,6 +30,12 @@ public void CheckCurrentTsar_WithCustomEquality() new Person("Vasili III of Russia", 28, 170, 60, null)); // Какие недостатки у такого подхода? + /* + * 1) Отсутствие информации о причинах падения теста (В моей проверке, выводятся различающиеся свойства). + * + * 2) При изменении свойств класса Person, необходимо дописывать проверку новых свойств (В моей проверке, сравниваются все public свойства + * и дописывать изменения в тест необходимо надо будет лишь в том случае, если появилось новое свойство, которое разное у всех объектов). + */ ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); } From f62673c31dd2fbc418c0b5e4f236c785f1e4b9be Mon Sep 17 00:00:00 2001 From: Virtical <138823554+Virtical@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:33:43 +0500 Subject: [PATCH 2/4] NumberValidator task has been solved --- .../NumberValidatorTests.cs | 281 ++++++++++++++++-- 1 file changed, 261 insertions(+), 20 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 950c9bc..0ab12c8 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -7,25 +7,266 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] 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")); + #region InitializationTests + [TestCase(0)] + [TestCase(-1)] + [TestCase(-100)] + [TestCase(-9999)] + [Category("InitializationTests")] + public void Constructor_PrecisionIsNotPositive_ThrowArgumentException(int precision) + { + TestDelegate action = () => new NumberValidator(precision); + + var ex = Assert.Throws(action, + $"Failed: Expected ArgumentException for precision = {precision}"); + + Assert.That(ex.Message, Is.EqualTo("precision must be a positive number"), + "Failed: Expected specific message for precision is not positive"); + } + + [TestCase(-1)] + [TestCase(-100)] + [TestCase(-9999)] + [Category("InitializationTests")] + public void Constructor_ScaleIsNegative_ThrowArgumentException(int scale) + { + TestDelegate action = () => new NumberValidator(1, scale); + + var ex = Assert.Throws(action, + $"Failed: Expected ArgumentException for scale = {scale}"); + + Assert.That(ex.Message, Is.EqualTo("precision must be a non-negative number less or equal than precision"), + "Failed: Expected specific message for scale is negative"); + } + + [TestCase(5, 5)] + [TestCase(1, 2)] + [TestCase(5, 10)] + [Category("InitializationTests")] + public void Constructor_ScaleNotLessThanPrecision_ThrowArgumentException(int precision, int scale) + { + TestDelegate action = () => new NumberValidator(precision, scale); + + var ex = Assert.Throws(action, + $"Failed: Expected ArgumentException for scale >= precision (precision = {precision}, scale = {scale})"); + + Assert.That(ex.Message, Is.EqualTo("precision must be a non-negative number less or equal than precision"), + "Failed: Expected specific message for scale >= precision"); + } + + [TestCase(1, 0)] + [TestCase(2, 1)] + [TestCase(10, 5)] + [Category("InitializationTests")] + public void Constructor_PrecisionAndScaleAreValid_NotThrowException(int precision, int scale) + { + TestDelegate action = () => new NumberValidator(precision, scale); + + Assert.DoesNotThrow(action, + $"Failed: Unexpected ArgumentException for precision = {precision}, scale = {scale}"); + } + #endregion + + #region IntegersTests + [TestCase(1, "0")] + [TestCase(2, "21")] + [TestCase(5, "40")] + [Category("IntegersTests")] + public void IsValidNumber_IntegerWithoutSignWithinPrecision_ReturnTrue(int precision, string numberToValidator) + { + var validator = new NumberValidator(precision, 0, true); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.True, + $"Failed: Expected valid for '{numberToValidator}', precision = {precision}, scale=0, onlyPositive=true"); + } + + [TestCase(2, "+0")] + [TestCase(2, "-0")] + [TestCase(3, "+21")] + [TestCase(5, "-40")] + [Category("IntegersTests")] + public void IsValidNumber_IntegerWithSignWithinPrecision_ReturnTrue(int precision, string numberToValidator) + { + var validator = new NumberValidator(precision, 0, false); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.True, + $"Failed: Expected valid for '{numberToValidator}', precision = {precision}, scale=0, onlyPositive=false"); + } + + [TestCase(2, 1, "0")] + [TestCase(3, 1, "5")] + [TestCase(5, 2, "-1")] + [TestCase(3, 2, "+21")] + [Category("IntegersTests")] + public void IsValidNumber_IntegerProvidedAndScaleIsGreaterThanZero_ReturnTrue(int precision, int scale, string numberToValidator) + { + var validator = new NumberValidator(precision, scale, false); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.True, + $"Failed: Expected valid for '{numberToValidator}', precision = {precision}, scale={scale}, onlyPositive=false"); + } + #endregion + + #region DecimalsTests + [TestCase(5, 2, "123.45")] + [TestCase(5, 2, "123.45")] + [TestCase(7, 2, "123.45")] + [TestCase(5, 4, "123.45")] + [TestCase(3, 2, "0.0")] + [Category("DecimalsTests")] + public void IsValidNumber_DecimalsWithoutSignWithinPrecisionAndScale_ReturnsTrue(int precision, int scale, string numberToValidator) + { + var validator = new NumberValidator(precision, scale, true); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.True, + $"Failed: Expected valid for '{numberToValidator}', precision = {precision}, scale={scale}, onlyPositive=true"); + } + + [TestCase(6, 2, "+123.45")] + [TestCase(7, 2, "-123.45")] + [TestCase(3, 2, "+0.0")] + [TestCase(3, 2, "-0.0")] + [Category("DecimalsTests")] + public void IsValidNumber_DecimalsWithSignWithinPrecisionAndScale_ReturnsTrue(int precision, int scale, string numberToValidator) + { + var validator = new NumberValidator(precision, scale, false); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.True, + $"Failed: Expected valid for '{numberToValidator}', precision = {precision}, scale={scale}, onlyPositive=false"); + } + + [TestCase("123.45")] + [TestCase("123,45")] + [TestCase("+123,45")] + [TestCase("-123,45")] + [TestCase("+123.45")] + [TestCase("-123.45")] + [Category("DecimalsTests")] + public void IsValidNumber_DecimalsWithCommaOrDotSeparator_ReturnTrue(string numberToValidator) + { + var validator = new NumberValidator(6, 3, false); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.True, + $"Failed: Expected valid for '{numberToValidator}', precision = 6, scale=3, onlyPositive=false"); + } + #endregion + + #region InvalidConfigurationTests + [TestCase(1, 0, "11")] + [TestCase(3, 1, "112.3")] + [TestCase(2, 0, "+42")] + [TestCase(4, 2, "+42.75")] + [Category("InvalidConfigurationTests")] + public void IsValidNumber_NumberExceedsPrecision_ReturnFalse(int precision, int scale, string numberToValidator) + { + var validator = new NumberValidator(precision, scale, false); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.False, + $"Failed: Expected invalid for '{numberToValidator}', precision = {precision}, scale={scale}, onlyPositive=false"); + } + + [TestCase(2, "0.0")] + [TestCase(3, "1.23")] + [TestCase(5, "-12.34")] + [TestCase(7, "+123.45")] + [Category("InvalidConfigurationTests")] + public void IsValidNumber_DecimalsProvidedAndScaleIsZero_ReturnFalse(int precision, string numberToValidator) + { + var validator = new NumberValidator(precision, 0, false); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.False, + $"Failed: Expected invalid for '{numberToValidator}', precision={precision}, scale=0, onlyPositive=false"); + } + + [TestCase(3, 1, "1.23")] + [TestCase(4, 1, "+1.32")] + [TestCase(7, 2, "+1.321")] + [Category("InvalidConfigurationTests")] + public void IsValidNumber_DecimalExceedsScale_ReturnFalse(int precision, int scale, string numberToValidator) + { + var validator = new NumberValidator(precision, scale, false); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.False, + $"Failed: Expected invalid for '{numberToValidator}', precision={precision}, scale={scale}, onlyPositive=false"); + } + + [TestCase("-0")] + [TestCase("-10")] + [TestCase("-1.23")] + [Category("InvalidConfigurationTests")] + public void IsValidNumber_NegativeNumberIsOnlyPositive_ReturnFalse(string numberToValidator) + { + var validator = new NumberValidator(7, 3, true); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.False, + $"Failed: Expected invalid for '{numberToValidator}', precision=7, scale=3, onlyPositive=false"); + } + #endregion + + #region InvalidInputsTests + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + [TestCase("\t")] + [Category("InvalidInputsTests")] + public void IsValidNumber_NumberNullOrEmpty_ReturnFalse(string numberToValidator) + { + var validator = new NumberValidator(5); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.False, + $"Failed: Expected invalid for '{numberToValidator}', precision = 5, scale=0, onlyPositive=false"); + } + + [TestCase("a.sd")] + [TestCase("10O")] + [TestCase("12.34#")] + [TestCase("$12.34")] + [Category("InvalidInputsTests")] + public void IsValidNumber_InputContainsLetters_ReturnFalse(string numberToValidator) + { + var validator = new NumberValidator(5, 3); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.False, + $"Failed: Expected invalid for '{numberToValidator}', precision = 5, scale=3, onlyPositive=false"); + } + + [TestCase("12_34")] + [TestCase("12/34")] + [TestCase("$12;34")] + [Category("InvalidInputsTests")] + public void IsValidNumber_DecimalsContainsIncorrectSeparator_ReturnFalse(string numberToValidator) + { + var validator = new NumberValidator(7, 3); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.False, + $"Failed: Expected invalid for '{numberToValidator}', precision = 5, scale=3, onlyPositive=false"); + } + + [TestCase(0, "123.")] + [TestCase(1, "123.")] + [TestCase(0, "12,")] + [TestCase(2, "123,")] + [Category("InvalidInputsTests")] + public void IsValidNumber_DecimalsMissingFractionalPart_ReturnFalse(int scale, string numberToValidator) + { + var validator = new NumberValidator(5, scale); + var isValid = validator.IsValidNumber(numberToValidator); + + Assert.That(isValid, Is.False, + $"Failed: Expected invalid for '{numberToValidator}', precision = 5, scale={scale}, onlyPositive=false"); } + #endregion } \ No newline at end of file From 47755b42e6f3f0ebe73e6ddc76217e2fce533b21 Mon Sep 17 00:00:00 2001 From: Virtical <138823554+Virtical@users.noreply.github.com> Date: Wed, 6 Nov 2024 04:24:45 +0500 Subject: [PATCH 3/4] Corrections after mentor review --- .../2. NumberValidator/NumberValidator.cs | 2 +- .../NumberValidatorTests.cs | 340 ++++++------------ 2 files changed, 120 insertions(+), 222 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidator.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidator.cs index 327ce9c..55c3c61 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 or equal 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 0ab12c8..b21ffc8 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,272 +1,170 @@  using NUnit.Framework; -using NUnit.Framework.Legacy; +using FluentAssertions; namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - #region InitializationTests - [TestCase(0)] - [TestCase(-1)] - [TestCase(-100)] - [TestCase(-9999)] + #region ConstructorTests + [TestCase(0,0, "precision must be a positive number", + TestName = "Constructor_PrecisionIsZero_ThrowArgumentException (precision = 0, scale = 0)")] + [TestCase(-1, 0, "precision must be a positive number", + TestName = "Constructor_PrecisionIsNegative_ThrowArgumentException (precision = -1, scale = 0)")] + + [TestCase(1, -1, "scale must be a non-negative number less or equal than precision", + TestName = "Constructor_ScaleIsNegative_ThrowArgumentException (precision = 1, scale = -1)")] + [TestCase(5, 5, "scale must be a non-negative number less or equal than precision", + TestName = "Constructor_PrecisionEqualsScale_ThrowArgumentException (precision = 5, scale = 5)")] + [TestCase(1, 2, "scale must be a non-negative number less or equal than precision", + TestName = "Constructor_PrecisionIsGreaterThanPrecision_ThrowArgumentException (precision = 1, scale = 2)")] [Category("InitializationTests")] - public void Constructor_PrecisionIsNotPositive_ThrowArgumentException(int precision) + public void Constructor_InvalidArguments_ThrowArgumentException(int precision, int scale, string expectedMessage) { - TestDelegate action = () => new NumberValidator(precision); + Action action = () => new NumberValidator(precision, scale); - var ex = Assert.Throws(action, - $"Failed: Expected ArgumentException for precision = {precision}"); - - Assert.That(ex.Message, Is.EqualTo("precision must be a positive number"), - "Failed: Expected specific message for precision is not positive"); + action.Should().Throw().WithMessage(expectedMessage); } - [TestCase(-1)] - [TestCase(-100)] - [TestCase(-9999)] + [TestCase(1, 0, + TestName = "Constructor_PrecisionIsGreaterThanScaleAndScaleIsZero_DoesNotThrowException (precision = 1, scale = 0)")] + [TestCase(5, 2, + TestName = "Constructor_PrecisionGreaterThanScaleAndScaleGreaterThanZero_DoesNotThrowException (precision = 5, scale = 2)")] [Category("InitializationTests")] - public void Constructor_ScaleIsNegative_ThrowArgumentException(int scale) + public void Constructor_ValidArguments_NotThrowException(int precision, int scale) { - TestDelegate action = () => new NumberValidator(1, scale); - - var ex = Assert.Throws(action, - $"Failed: Expected ArgumentException for scale = {scale}"); + Action action = () => new NumberValidator(precision, scale); - Assert.That(ex.Message, Is.EqualTo("precision must be a non-negative number less or equal than precision"), - "Failed: Expected specific message for scale is negative"); - } - - [TestCase(5, 5)] - [TestCase(1, 2)] - [TestCase(5, 10)] - [Category("InitializationTests")] - public void Constructor_ScaleNotLessThanPrecision_ThrowArgumentException(int precision, int scale) - { - TestDelegate action = () => new NumberValidator(precision, scale); - - var ex = Assert.Throws(action, - $"Failed: Expected ArgumentException for scale >= precision (precision = {precision}, scale = {scale})"); - - Assert.That(ex.Message, Is.EqualTo("precision must be a non-negative number less or equal than precision"), - "Failed: Expected specific message for scale >= precision"); - } - - [TestCase(1, 0)] - [TestCase(2, 1)] - [TestCase(10, 5)] - [Category("InitializationTests")] - public void Constructor_PrecisionAndScaleAreValid_NotThrowException(int precision, int scale) - { - TestDelegate action = () => new NumberValidator(precision, scale); - - Assert.DoesNotThrow(action, - $"Failed: Unexpected ArgumentException for precision = {precision}, scale = {scale}"); + action.Should().NotThrow(); } #endregion #region IntegersTests - [TestCase(1, "0")] - [TestCase(2, "21")] - [TestCase(5, "40")] + [TestCase(1, 0, "0", + TestName = "IsValidNumber_IntegerWithoutSign_LengthEqualsPrecision_ReturnsTrue (precision = 1, scale = 0, onlyPositive = false, valueForValidation = \"0\")")] + [TestCase(5, 0, "40", + TestName = "IsValidNumber_IntegerWithoutSign_LengthLessThanPrecision_ReturnsTrue (precision = 5, scale = 0, onlyPositive = false, valueForValidation = \"40\")")] + [TestCase(2, 0, "+0", + TestName = "IsValidNumber_PositiveZeroWithSign_ReturnsTrue (precision = 2, scale = 0, onlyPositive = false, valueForValidation = \"+0\")")] + + [TestCase(2, 0, "-0", + TestName = "IsValidNumber_NegativeZeroWithSign_ReturnsTrue (precision = 2, scale = 0, onlyPositive = false, valueForValidation = \"-0\")")] + [TestCase(3, 0, "+21", + TestName = "IsValidNumber_IntegerWithSign_LengthEqualsPrecision_ReturnsTrue (precision = 3, scale = 0, onlyPositive = false, valueForValidation = \"+21\")")] + [TestCase(5, 0, "-40", + TestName = "IsValidNumber_IntegerWithSign_LengthLessThanPrecision_ReturnsTrue (precision = 5, scale = 0, onlyPositive = false, valueForValidation = \"-40\")")] + + [TestCase(3, 1, "0", + TestName = "IsValidNumber_IntegerWithoutSign_ScaleIsGreaterThanZero_ReturnsTrue (precision = 3, scale = 1, onlyPositive = false, valueForValidation = \"6\")")] + [TestCase(5, 2, "+21", + TestName = "IsValidNumber_IntegerWithSign_ScaleIsGreaterThanZero_ReturnsTrue (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"+37\")")] [Category("IntegersTests")] - public void IsValidNumber_IntegerWithoutSignWithinPrecision_ReturnTrue(int precision, string numberToValidator) - { - var validator = new NumberValidator(precision, 0, true); - var isValid = validator.IsValidNumber(numberToValidator); - - Assert.That(isValid, Is.True, - $"Failed: Expected valid for '{numberToValidator}', precision = {precision}, scale=0, onlyPositive=true"); - } - - [TestCase(2, "+0")] - [TestCase(2, "-0")] - [TestCase(3, "+21")] - [TestCase(5, "-40")] - [Category("IntegersTests")] - public void IsValidNumber_IntegerWithSignWithinPrecision_ReturnTrue(int precision, string numberToValidator) - { - var validator = new NumberValidator(precision, 0, false); - var isValid = validator.IsValidNumber(numberToValidator); - - Assert.That(isValid, Is.True, - $"Failed: Expected valid for '{numberToValidator}', precision = {precision}, scale=0, onlyPositive=false"); - } - - [TestCase(2, 1, "0")] - [TestCase(3, 1, "5")] - [TestCase(5, 2, "-1")] - [TestCase(3, 2, "+21")] - [Category("IntegersTests")] - public void IsValidNumber_IntegerProvidedAndScaleIsGreaterThanZero_ReturnTrue(int precision, int scale, string numberToValidator) + public void IsValidNumber_IntegerWithinPrecisionAndScale_ReturnTrue(int precision, int scale, string valueForValidation) { var validator = new NumberValidator(precision, scale, false); - var isValid = validator.IsValidNumber(numberToValidator); + var isValid = validator.IsValidNumber(valueForValidation); - Assert.That(isValid, Is.True, - $"Failed: Expected valid for '{numberToValidator}', precision = {precision}, scale={scale}, onlyPositive=false"); + isValid.Should().BeTrue(); } #endregion #region DecimalsTests - [TestCase(5, 2, "123.45")] - [TestCase(5, 2, "123.45")] - [TestCase(7, 2, "123.45")] - [TestCase(5, 4, "123.45")] - [TestCase(3, 2, "0.0")] - [Category("DecimalsTests")] - public void IsValidNumber_DecimalsWithoutSignWithinPrecisionAndScale_ReturnsTrue(int precision, int scale, string numberToValidator) - { - var validator = new NumberValidator(precision, scale, true); - var isValid = validator.IsValidNumber(numberToValidator); - - Assert.That(isValid, Is.True, - $"Failed: Expected valid for '{numberToValidator}', precision = {precision}, scale={scale}, onlyPositive=true"); - } - - [TestCase(6, 2, "+123.45")] - [TestCase(7, 2, "-123.45")] - [TestCase(3, 2, "+0.0")] - [TestCase(3, 2, "-0.0")] + [TestCase(5, 2, "123.45", + TestName = "IsValidNumber_DecimalWithoutSign_FractionalPartLengthEqualsScale_ReturnsTrue (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"123.45\")")] + [TestCase(7, 4, "537.23", + TestName = "IsValidNumber_DecimalWithoutSign_FractionalPartLengthLessThanScale_ReturnsTrue (precision = 7, scale = 4, onlyPositive = false, valueForValidation = \"537.23\")")] + + [TestCase(3, 2, "0.0", + TestName = "IsValidNumber_DecimalZeroWithinPrecisionAndScale_ReturnsTrue (precision = 3, scale = 2, valueForValidation = \"0.0\")")] + + [TestCase(6, 2, "+237.89", + TestName = "IsValidNumber_DecimalWithSign_FractionalPartLengthEqualsScale_ReturnsTrue (precision = 6, scale = 2, onlyPositive = false, valueForValidation = \"+237.89\")")] + [TestCase(7, 4, "-445.12", + TestName = "IsValidNumber_DecimalWithoutSign_FractionalPartLengthLessThanScale_ReturnsTrue (precision = 7, scale = 4, onlyPositive = false, valueForValidation = \"-445.12\")")] + + [TestCase(5, 2, "371,47", + TestName = "IsValidNumber_DecimalWithoutSign_WithCommaSeparator_ReturnsTrue (precision = 5, scale = 2, valueForValidation = \"371,47\")")] + [TestCase(6, 2, "+111,11", + TestName = "IsValidNumber_DecimalWithSign_WithCommaSeparator_ReturnsTrue (precision = 6, scale = 2, valueForValidation = \"111,11\")")] + [TestCase(6, 2, "-984,75", + TestName = "IsValidNumber_DecimalWithSign_WithCommaSeparator_ReturnsTrue (precision = 6, scale = 2, valueForValidation = \"-984,75\")")] [Category("DecimalsTests")] - public void IsValidNumber_DecimalsWithSignWithinPrecisionAndScale_ReturnsTrue(int precision, int scale, string numberToValidator) + public void IsValidNumber_DecimalWithinPrecisionAndScale_ReturnsTrue(int precision, int scale, string valueForValidation) { var validator = new NumberValidator(precision, scale, false); - var isValid = validator.IsValidNumber(numberToValidator); + var isValid = validator.IsValidNumber(valueForValidation); - Assert.That(isValid, Is.True, - $"Failed: Expected valid for '{numberToValidator}', precision = {precision}, scale={scale}, onlyPositive=false"); - } - - [TestCase("123.45")] - [TestCase("123,45")] - [TestCase("+123,45")] - [TestCase("-123,45")] - [TestCase("+123.45")] - [TestCase("-123.45")] - [Category("DecimalsTests")] - public void IsValidNumber_DecimalsWithCommaOrDotSeparator_ReturnTrue(string numberToValidator) - { - var validator = new NumberValidator(6, 3, false); - var isValid = validator.IsValidNumber(numberToValidator); - - Assert.That(isValid, Is.True, - $"Failed: Expected valid for '{numberToValidator}', precision = 6, scale=3, onlyPositive=false"); + isValid.Should().BeTrue(); } #endregion #region InvalidConfigurationTests - [TestCase(1, 0, "11")] - [TestCase(3, 1, "112.3")] - [TestCase(2, 0, "+42")] - [TestCase(4, 2, "+42.75")] - [Category("InvalidConfigurationTests")] - public void IsValidNumber_NumberExceedsPrecision_ReturnFalse(int precision, int scale, string numberToValidator) - { - var validator = new NumberValidator(precision, scale, false); - var isValid = validator.IsValidNumber(numberToValidator); - - Assert.That(isValid, Is.False, - $"Failed: Expected invalid for '{numberToValidator}', precision = {precision}, scale={scale}, onlyPositive=false"); - } - - [TestCase(2, "0.0")] - [TestCase(3, "1.23")] - [TestCase(5, "-12.34")] - [TestCase(7, "+123.45")] - [Category("InvalidConfigurationTests")] - public void IsValidNumber_DecimalsProvidedAndScaleIsZero_ReturnFalse(int precision, string numberToValidator) - { - var validator = new NumberValidator(precision, 0, false); - var isValid = validator.IsValidNumber(numberToValidator); - - Assert.That(isValid, Is.False, - $"Failed: Expected invalid for '{numberToValidator}', precision={precision}, scale=0, onlyPositive=false"); - } - - [TestCase(3, 1, "1.23")] - [TestCase(4, 1, "+1.32")] - [TestCase(7, 2, "+1.321")] - [Category("InvalidConfigurationTests")] - public void IsValidNumber_DecimalExceedsScale_ReturnFalse(int precision, int scale, string numberToValidator) - { - var validator = new NumberValidator(precision, scale, false); - var isValid = validator.IsValidNumber(numberToValidator); - - Assert.That(isValid, Is.False, - $"Failed: Expected invalid for '{numberToValidator}', precision={precision}, scale={scale}, onlyPositive=false"); - } - [TestCase("-0")] - [TestCase("-10")] - [TestCase("-1.23")] + [TestCase(1, 0, "11", + TestName = "IsValidNumber_IntegerWithoutSign_ExceedsPrecision_ReturnsFalse (precision = 1, scale = 0, onlyPositive = true, valueForValidation = \"11\")")] + [TestCase(3, 1, "112.3", + TestName = "IsValidNumber_DecimalWithoutSign_ExceedsPrecision_ReturnsFalse (precision = 3, scale = 1, onlyPositive = true, valueForValidation = \"112.3\")")] + + [TestCase(2, 0, "+42", + TestName = "IsValidNumber_IntegerWithSign_ExceedsPrecision_ReturnFalse (precision = 2, scale = 0, onlyPositive = true, valueForValidation = \"+42\")")] + [TestCase(4, 2, "+42.75", + TestName = "IsValidNumber_DecimalWithSign_ExceedsPrecision_ReturnFalse (precision = 4, scale = 2, onlyPositive = true, valueForValidation = \"+42.75\")")] + + [TestCase(2, 0,"5.0", + TestName = "IsValidNumber_DecimalProvided_ScaleIsZero_ReturnFalse (precision = 2, scale = 0, onlyPositive = true, valueForValidation = \"5.0\")")] + [TestCase(5, 0, "1.23", + TestName = "IsValidNumber_DecimalProvided_ScaleIsZero_ReturnFalse (precision = 5, scale = 0, onlyPositive = true, valueForValidation = \"1.23\")")] + + [TestCase(3, 1, "5.67", + TestName = "IsValidNumber_DecimalExceedsScale_ReturnFalse (precision = 3, scale = 1, onlyPositive = true, valueForValidation = \"5.67\")")] + + [TestCase(7, 3, "-0", + TestName = "IsValidNumber_NegativeZero_IsOnlyPositive_ReturnFalse (precision = 7, scale = 3, onlyPositive = true, valueForValidation = \"-0\")")] + [TestCase(7, 3, "-10", + TestName = "IsValidNumber_NegativeInteger_IsOnlyPositive_ReturnFalse (precision = 7, scale = 3, onlyPositive = true, valueForValidation = \"-10\")")] + [TestCase(7, 3, "-1.23", + TestName = "IsValidNumber_NegativeDecimal_IsOnlyPositive_ReturnFalse (precision = 7, scale = 3, onlyPositive = true, valueForValidation = \"-1.23\")")] [Category("InvalidConfigurationTests")] - public void IsValidNumber_NegativeNumberIsOnlyPositive_ReturnFalse(string numberToValidator) + public void IsValidNumber_InvalidConfigurations_ReturnFalse(int precision, int scale, string valueForValidation) { - var validator = new NumberValidator(7, 3, true); - var isValid = validator.IsValidNumber(numberToValidator); + var validator = new NumberValidator(precision, scale, true); + var isValid = validator.IsValidNumber(valueForValidation); - Assert.That(isValid, Is.False, - $"Failed: Expected invalid for '{numberToValidator}', precision=7, scale=3, onlyPositive=false"); + isValid.Should().BeFalse(); } #endregion #region InvalidInputsTests - [TestCase(null)] - [TestCase("")] - [TestCase(" ")] - [TestCase("\t")] - [Category("InvalidInputsTests")] - public void IsValidNumber_NumberNullOrEmpty_ReturnFalse(string numberToValidator) - { - var validator = new NumberValidator(5); - var isValid = validator.IsValidNumber(numberToValidator); - - Assert.That(isValid, Is.False, - $"Failed: Expected invalid for '{numberToValidator}', precision = 5, scale=0, onlyPositive=false"); - } - - [TestCase("a.sd")] - [TestCase("10O")] - [TestCase("12.34#")] - [TestCase("$12.34")] - [Category("InvalidInputsTests")] - public void IsValidNumber_InputContainsLetters_ReturnFalse(string numberToValidator) - { - var validator = new NumberValidator(5, 3); - var isValid = validator.IsValidNumber(numberToValidator); - - Assert.That(isValid, Is.False, - $"Failed: Expected invalid for '{numberToValidator}', precision = 5, scale=3, onlyPositive=false"); - } - - [TestCase("12_34")] - [TestCase("12/34")] - [TestCase("$12;34")] - [Category("InvalidInputsTests")] - public void IsValidNumber_DecimalsContainsIncorrectSeparator_ReturnFalse(string numberToValidator) - { - var validator = new NumberValidator(7, 3); - var isValid = validator.IsValidNumber(numberToValidator); - - Assert.That(isValid, Is.False, - $"Failed: Expected invalid for '{numberToValidator}', precision = 5, scale=3, onlyPositive=false"); - } + [TestCase(5, 2, null, + TestName = "IsValidNumber_InputIsNull_ReturnFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = null)")] + [TestCase(5, 2, "", + TestName = "IsValidNumber_InputIsEmpty_ReturnFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"\")")] + [TestCase(5, 2, " ", + TestName = "IsValidNumber_InputIsWhitespace_ReturnFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \" \")")] + [TestCase(5, 2, "\t", + TestName = "IsValidNumber_InputIsEscapeSequences_ReturnFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"\\t\")")] + + [TestCase(5,2, "a.sd", + TestName = "IsValidNumber_InputContainsLetters_ReturnsFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"a.sd\")")] + [TestCase(5, 2, "10O", + TestName = "IsValidNumber_InputContainsLetters_ReturnsFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"10O\")")] + + [TestCase(7, 2, "12_34", + TestName = "IsValidNumber_InputContainsIncorrectSeparator_ReturnsFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"12_34\")")] + + [TestCase(5, 0, "871.", + TestName = "IsValidNumber_DecimalWithoutFraction_ScaleZero_ReturnsFalse (precision = 5, scale = 0, onlyPositive = false, valueForValidation = \"871.\")")] + [TestCase(5, 2, "636.", + TestName = "IsValidNumber_DecimalWithoutFraction_ScaleIsPositive_ReturnsFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"636.\")")] - [TestCase(0, "123.")] - [TestCase(1, "123.")] - [TestCase(0, "12,")] - [TestCase(2, "123,")] [Category("InvalidInputsTests")] - public void IsValidNumber_DecimalsMissingFractionalPart_ReturnFalse(int scale, string numberToValidator) + public void IsValidNumber_InvalidInputs_ReturnFalse(int precision, int scale, string valueToValidator) { - var validator = new NumberValidator(5, scale); - var isValid = validator.IsValidNumber(numberToValidator); + var validator = new NumberValidator(precision, scale); + var isValid = validator.IsValidNumber(valueToValidator); - Assert.That(isValid, Is.False, - $"Failed: Expected invalid for '{numberToValidator}', precision = 5, scale={scale}, onlyPositive=false"); + isValid.Should().BeFalse(); } #endregion } \ No newline at end of file From 7877e6ddb695bad86aefe823e576955c0c122ad5 Mon Sep 17 00:00:00 2001 From: Virtical <138823554+Virtical@users.noreply.github.com> Date: Wed, 6 Nov 2024 15:17:26 +0500 Subject: [PATCH 4/4] =?UTF-8?q?Corrections=20after=20mentor=20review=20?= =?UTF-8?q?=E2=84=962?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NumberValidatorTests.cs | 198 +++++------------- 1 file changed, 50 insertions(+), 148 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index b21ffc8..567289c 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -7,164 +7,66 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - #region ConstructorTests - [TestCase(0,0, "precision must be a positive number", - TestName = "Constructor_PrecisionIsZero_ThrowArgumentException (precision = 0, scale = 0)")] - [TestCase(-1, 0, "precision must be a positive number", - TestName = "Constructor_PrecisionIsNegative_ThrowArgumentException (precision = -1, scale = 0)")] - - [TestCase(1, -1, "scale must be a non-negative number less or equal than precision", - TestName = "Constructor_ScaleIsNegative_ThrowArgumentException (precision = 1, scale = -1)")] - [TestCase(5, 5, "scale must be a non-negative number less or equal than precision", - TestName = "Constructor_PrecisionEqualsScale_ThrowArgumentException (precision = 5, scale = 5)")] - [TestCase(1, 2, "scale must be a non-negative number less or equal than precision", - TestName = "Constructor_PrecisionIsGreaterThanPrecision_ThrowArgumentException (precision = 1, scale = 2)")] - [Category("InitializationTests")] - public void Constructor_InvalidArguments_ThrowArgumentException(int precision, int scale, string expectedMessage) + [TestCase(0,0, TestName = "PrecisionIsZero")] + [TestCase(-1, 0, TestName = "PrecisionIsNegative")] + [TestCase(1, -1, TestName = "ScaleIsNegative")] + [TestCase(5, 5, TestName = "ScaleEqualsPrecision")] + [TestCase(1, 2, TestName = "ScaleIsGreaterThanPrecision")] + public void Constructor_ThrowException_When(int precision, int scale) { - Action action = () => new NumberValidator(precision, scale); +var createValidator = () => new NumberValidator(precision, scale); - action.Should().Throw().WithMessage(expectedMessage); +createValidator.Should().Throw(); } - [TestCase(1, 0, - TestName = "Constructor_PrecisionIsGreaterThanScaleAndScaleIsZero_DoesNotThrowException (precision = 1, scale = 0)")] - [TestCase(5, 2, - TestName = "Constructor_PrecisionGreaterThanScaleAndScaleGreaterThanZero_DoesNotThrowException (precision = 5, scale = 2)")] - [Category("InitializationTests")] - public void Constructor_ValidArguments_NotThrowException(int precision, int scale) + [TestCase(1, 0, TestName = "PrecisionIsGreaterThanScaleAndScaleIsZero")] + [TestCase(5, 2, TestName = "PrecisionGreaterThanScaleAndScaleGreaterThanZero")] + public void Constructor_NotThrowException_When(int precision, int scale) { - Action action = () => new NumberValidator(precision, scale); + var createValidator = () => new NumberValidator(precision, scale); - action.Should().NotThrow(); + createValidator.Should().NotThrow(); } - #endregion - - #region IntegersTests - [TestCase(1, 0, "0", - TestName = "IsValidNumber_IntegerWithoutSign_LengthEqualsPrecision_ReturnsTrue (precision = 1, scale = 0, onlyPositive = false, valueForValidation = \"0\")")] - [TestCase(5, 0, "40", - TestName = "IsValidNumber_IntegerWithoutSign_LengthLessThanPrecision_ReturnsTrue (precision = 5, scale = 0, onlyPositive = false, valueForValidation = \"40\")")] - [TestCase(2, 0, "+0", - TestName = "IsValidNumber_PositiveZeroWithSign_ReturnsTrue (precision = 2, scale = 0, onlyPositive = false, valueForValidation = \"+0\")")] - [TestCase(2, 0, "-0", - TestName = "IsValidNumber_NegativeZeroWithSign_ReturnsTrue (precision = 2, scale = 0, onlyPositive = false, valueForValidation = \"-0\")")] - [TestCase(3, 0, "+21", - TestName = "IsValidNumber_IntegerWithSign_LengthEqualsPrecision_ReturnsTrue (precision = 3, scale = 0, onlyPositive = false, valueForValidation = \"+21\")")] - [TestCase(5, 0, "-40", - TestName = "IsValidNumber_IntegerWithSign_LengthLessThanPrecision_ReturnsTrue (precision = 5, scale = 0, onlyPositive = false, valueForValidation = \"-40\")")] - - [TestCase(3, 1, "0", - TestName = "IsValidNumber_IntegerWithoutSign_ScaleIsGreaterThanZero_ReturnsTrue (precision = 3, scale = 1, onlyPositive = false, valueForValidation = \"6\")")] - [TestCase(5, 2, "+21", - TestName = "IsValidNumber_IntegerWithSign_ScaleIsGreaterThanZero_ReturnsTrue (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"+37\")")] - [Category("IntegersTests")] - public void IsValidNumber_IntegerWithinPrecisionAndScale_ReturnTrue(int precision, int scale, string valueForValidation) + [TestCase(1, 0, false, "0", true, TestName = "IntegerWithoutSignAndLengthEqualsPrecision")] + [TestCase(5, 0, false, "40", true, TestName = "IntegerWithoutSignAndLengthLessThanPrecision")] + [TestCase(2, 0, false, "+0", true, TestName = "PositiveZeroWithSign")] + [TestCase(2, 0, false, "-0", true, TestName = "NegativeZeroWithSign")] + [TestCase(3, 0, false, "+21", true, TestName = "IntegerWithSignAndLengthEqualsPrecision")] + [TestCase(5, 0, false, "-40", true, TestName = "IntegerWithSignAndLengthLessThanPrecision")] + [TestCase(3, 1, false, "0", true, TestName = "IntegerWithoutSignAndScaleIsGreaterThanZero")] + [TestCase(5, 2, false, "+21", true, TestName = "IntegerWithSignAndScaleIsGreaterThanZero")] + [TestCase(5, 2, false,"123.45", true, TestName = "DecimalWithoutSignAndFractionalPartLengthEqualsScale")] + [TestCase(7, 4, false, "537.23", true, TestName = "DecimalWithoutSignAndFractionalPartLengthLessThanScale")] + [TestCase(3, 2, false, "0.0", true, TestName = "DecimalZeroWithinPrecisionAndScale")] + [TestCase(6, 2, false, "+237.89", true, TestName = "DecimalWithSignAndFractionalPartLengthEqualsScale")] + [TestCase(7, 4, false, "-445.12", true, TestName = "DecimalWithoutSignAndFractionalPartLengthLessThanScale")] + [TestCase(5, 2, false, "371,47", true, TestName = "DecimalWithoutSignWithCommaSeparator")] + [TestCase(6, 2, false, "+111,11", true, TestName = "DecimalWithSignWithCommaSeparator")] + [TestCase(1, 0, true, "11", false, TestName = "IntegerWithoutSignExceedsPrecision")] + [TestCase(3, 1, true, "112.3", false, TestName = "DecimalWithoutSignExceedsPrecision")] + [TestCase(2, 0, true, "+42", false, TestName = "IntegerWithSignExceedsPrecision")] + [TestCase(4, 2, true, "+42.75", false, TestName = "DecimalWithSignExceedsPrecision")] + [TestCase(2, 0, true, "5.0", false, TestName = "DecimalProvidedAndScaleIsZero")] + [TestCase(5, 0, true, "1.23", false, TestName = "DecimalProvidedAndScaleIsZero")] + [TestCase(3, 1, true, "5.67", false, TestName = "DecimalExceedsScale")] + [TestCase(7, 3, true, "-0", false, TestName = "NegativeZeroAndIsOnlyPositiveIsTrue")] + [TestCase(7, 3, true, "-10", false, TestName = "NegativeIntegerAndIsOnlyPositiveIsTrue")] + [TestCase(7, 3, true, "-1.23", false, TestName = "NegativeDecimalAndIsOnlyPositiveIsTrue")] + [TestCase(5, 2, false, null, false, TestName = "ValueIsNull")] + [TestCase(5, 2, false, "", false, TestName = "ValueIsEmpty")] + [TestCase(5, 2, false, " ", false, TestName = "ValueIsWhitespace")] + [TestCase(5, 2, false, "\t", false, TestName = "ValueIsEscapeSequences")] + [TestCase(5, 2, false, "a.sd", false, TestName = "ValueContainsLetters")] + [TestCase(5, 2, false, "10O", false, TestName = "ValueContainsLetters")] + [TestCase(7, 2, false, "12_34", false, TestName = "ValueContainsIncorrectSeparator")] + [TestCase(5, 0, false, "871.", false, TestName = "DecimalWithoutFractionAndScaleZero")] + [TestCase(5, 2, false, "636.", false, TestName = "DecimalWithoutFractionAndScaleIsPositive")] + public void IsValidNumber_ReturnExpectedResult_When(int precision, int scale, bool onlyPositive, string valueForValidation, bool expectedResult) { - var validator = new NumberValidator(precision, scale, false); + var validator = new NumberValidator(precision, scale, onlyPositive); var isValid = validator.IsValidNumber(valueForValidation); - isValid.Should().BeTrue(); - } - #endregion - - #region DecimalsTests - [TestCase(5, 2, "123.45", - TestName = "IsValidNumber_DecimalWithoutSign_FractionalPartLengthEqualsScale_ReturnsTrue (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"123.45\")")] - [TestCase(7, 4, "537.23", - TestName = "IsValidNumber_DecimalWithoutSign_FractionalPartLengthLessThanScale_ReturnsTrue (precision = 7, scale = 4, onlyPositive = false, valueForValidation = \"537.23\")")] - - [TestCase(3, 2, "0.0", - TestName = "IsValidNumber_DecimalZeroWithinPrecisionAndScale_ReturnsTrue (precision = 3, scale = 2, valueForValidation = \"0.0\")")] - - [TestCase(6, 2, "+237.89", - TestName = "IsValidNumber_DecimalWithSign_FractionalPartLengthEqualsScale_ReturnsTrue (precision = 6, scale = 2, onlyPositive = false, valueForValidation = \"+237.89\")")] - [TestCase(7, 4, "-445.12", - TestName = "IsValidNumber_DecimalWithoutSign_FractionalPartLengthLessThanScale_ReturnsTrue (precision = 7, scale = 4, onlyPositive = false, valueForValidation = \"-445.12\")")] - - [TestCase(5, 2, "371,47", - TestName = "IsValidNumber_DecimalWithoutSign_WithCommaSeparator_ReturnsTrue (precision = 5, scale = 2, valueForValidation = \"371,47\")")] - [TestCase(6, 2, "+111,11", - TestName = "IsValidNumber_DecimalWithSign_WithCommaSeparator_ReturnsTrue (precision = 6, scale = 2, valueForValidation = \"111,11\")")] - [TestCase(6, 2, "-984,75", - TestName = "IsValidNumber_DecimalWithSign_WithCommaSeparator_ReturnsTrue (precision = 6, scale = 2, valueForValidation = \"-984,75\")")] - [Category("DecimalsTests")] - public void IsValidNumber_DecimalWithinPrecisionAndScale_ReturnsTrue(int precision, int scale, string valueForValidation) - { - var validator = new NumberValidator(precision, scale, false); - var isValid = validator.IsValidNumber(valueForValidation); - - isValid.Should().BeTrue(); - } - #endregion - - #region InvalidConfigurationTests - - [TestCase(1, 0, "11", - TestName = "IsValidNumber_IntegerWithoutSign_ExceedsPrecision_ReturnsFalse (precision = 1, scale = 0, onlyPositive = true, valueForValidation = \"11\")")] - [TestCase(3, 1, "112.3", - TestName = "IsValidNumber_DecimalWithoutSign_ExceedsPrecision_ReturnsFalse (precision = 3, scale = 1, onlyPositive = true, valueForValidation = \"112.3\")")] - - [TestCase(2, 0, "+42", - TestName = "IsValidNumber_IntegerWithSign_ExceedsPrecision_ReturnFalse (precision = 2, scale = 0, onlyPositive = true, valueForValidation = \"+42\")")] - [TestCase(4, 2, "+42.75", - TestName = "IsValidNumber_DecimalWithSign_ExceedsPrecision_ReturnFalse (precision = 4, scale = 2, onlyPositive = true, valueForValidation = \"+42.75\")")] - - [TestCase(2, 0,"5.0", - TestName = "IsValidNumber_DecimalProvided_ScaleIsZero_ReturnFalse (precision = 2, scale = 0, onlyPositive = true, valueForValidation = \"5.0\")")] - [TestCase(5, 0, "1.23", - TestName = "IsValidNumber_DecimalProvided_ScaleIsZero_ReturnFalse (precision = 5, scale = 0, onlyPositive = true, valueForValidation = \"1.23\")")] - - [TestCase(3, 1, "5.67", - TestName = "IsValidNumber_DecimalExceedsScale_ReturnFalse (precision = 3, scale = 1, onlyPositive = true, valueForValidation = \"5.67\")")] - - [TestCase(7, 3, "-0", - TestName = "IsValidNumber_NegativeZero_IsOnlyPositive_ReturnFalse (precision = 7, scale = 3, onlyPositive = true, valueForValidation = \"-0\")")] - [TestCase(7, 3, "-10", - TestName = "IsValidNumber_NegativeInteger_IsOnlyPositive_ReturnFalse (precision = 7, scale = 3, onlyPositive = true, valueForValidation = \"-10\")")] - [TestCase(7, 3, "-1.23", - TestName = "IsValidNumber_NegativeDecimal_IsOnlyPositive_ReturnFalse (precision = 7, scale = 3, onlyPositive = true, valueForValidation = \"-1.23\")")] - [Category("InvalidConfigurationTests")] - public void IsValidNumber_InvalidConfigurations_ReturnFalse(int precision, int scale, string valueForValidation) - { - var validator = new NumberValidator(precision, scale, true); - var isValid = validator.IsValidNumber(valueForValidation); - - isValid.Should().BeFalse(); - } - #endregion - - #region InvalidInputsTests - [TestCase(5, 2, null, - TestName = "IsValidNumber_InputIsNull_ReturnFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = null)")] - [TestCase(5, 2, "", - TestName = "IsValidNumber_InputIsEmpty_ReturnFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"\")")] - [TestCase(5, 2, " ", - TestName = "IsValidNumber_InputIsWhitespace_ReturnFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \" \")")] - [TestCase(5, 2, "\t", - TestName = "IsValidNumber_InputIsEscapeSequences_ReturnFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"\\t\")")] - - [TestCase(5,2, "a.sd", - TestName = "IsValidNumber_InputContainsLetters_ReturnsFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"a.sd\")")] - [TestCase(5, 2, "10O", - TestName = "IsValidNumber_InputContainsLetters_ReturnsFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"10O\")")] - - [TestCase(7, 2, "12_34", - TestName = "IsValidNumber_InputContainsIncorrectSeparator_ReturnsFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"12_34\")")] - - [TestCase(5, 0, "871.", - TestName = "IsValidNumber_DecimalWithoutFraction_ScaleZero_ReturnsFalse (precision = 5, scale = 0, onlyPositive = false, valueForValidation = \"871.\")")] - [TestCase(5, 2, "636.", - TestName = "IsValidNumber_DecimalWithoutFraction_ScaleIsPositive_ReturnsFalse (precision = 5, scale = 2, onlyPositive = false, valueForValidation = \"636.\")")] - - [Category("InvalidInputsTests")] - public void IsValidNumber_InvalidInputs_ReturnFalse(int precision, int scale, string valueToValidator) - { - var validator = new NumberValidator(precision, scale); - var isValid = validator.IsValidNumber(valueToValidator); - - isValid.Should().BeFalse(); + isValid.Should().Be(expectedResult); } - #endregion } \ No newline at end of file