diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index a5b7a131..df411dbd 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -1,4 +1,6 @@ using System; +using System.Collections; +using System.Collections.Generic; using NUnit.Framework; namespace HomeExercises @@ -19,71 +21,53 @@ public void NumberValidator_InvalidParameters_ThrowsArgumentException(int precis Assert.Throws(() => new NumberValidator(precision, scale, true)); } - [TestCase(17, 2, true, "0.0")] - [TestCase(17, 2, true, "0")] - [TestCase(17, 2, true, "+0.0")] - [TestCase(3, 1, true, "+1.2")] - [TestCase(3, 1, false, "-1.2")] - - public void IsValidNumber_WithProperValue_Success - (int precision, int scale, bool positive, string value) + [TestCaseSource(typeof(NumberValidatorData), nameof(NumberValidatorData.TestCaseData))] + public void IsValidNumber_Tests + (int precision, int scale, bool positive, string value, bool expected) { var validator = new NumberValidator(precision, scale, positive); var actual = validator.IsValidNumber(value); - Assert.True(actual); - } - - [TestCase(" ")] - [TestCase("")] - [TestCase(null)] - [TestCase("+-1")] - [TestCase("abc")] - [TestCase("a.bc")] - [TestCase("1.0.1")] - public void IsValidNumber_WithIncorrectValue_ReturnFalse(string value) - { - var validator = new NumberValidator(10, 5); - - var actual = validator.IsValidNumber(value); - - Assert.False(actual); - } - - - [TestCase("-1.0")] - [TestCase("+1.0")] - [TestCase("10.0")] - [TestCase("1.00")] - public void IsValidNumber_WithValueGreaterThanPrecision_ReturnFalse(string value) - { - var validator = new NumberValidator(2, 1); - - var actual = validator.IsValidNumber(value); - - Assert.False(actual); - } - - [TestCase(true, "1.00")] - [TestCase(false, "-1.00")] - public void IsValidNumber_WithFracPartGreaterThanScale_ReturnFalse(bool positive, string value) - { - var validator = new NumberValidator(10, 1, positive); - - var actual = validator.IsValidNumber(value); - - Assert.False(actual); + Assert.AreEqual(expected, actual); } + } - [Test] - public void IsValidNumber_OnlyPositiveWithNegativeValue_ReturnFalse() + public class NumberValidatorData + { + public static IEnumerable TestCaseData { - var validator = new NumberValidator(10, 5, true); - - var actual = validator.IsValidNumber("-1.0"); - - Assert.False(actual); + get + { + //IsValidNumber_WithProperValue_Success + yield return new TestCaseData(17, 2, true, "0.0", true); + yield return new TestCaseData(17, 2, true, "0", true); + yield return new TestCaseData(17, 2, true, "+0.0", true); + yield return new TestCaseData(3, 1, true, "+1.2", true); + yield return new TestCaseData(3, 1, false, "-1.2", true); + + //IsValidNumber_WithIncorrectValue_ReturnFalse + yield return new TestCaseData(10, 5, false, " ", false); + yield return new TestCaseData(10, 5, false, "", false); + yield return new TestCaseData(10, 5, false, null, false); + yield return new TestCaseData(10, 5, false, "+-1", false); + yield return new TestCaseData(10, 5, false, "abc", false); + yield return new TestCaseData(10, 5, false, "a.bc", false); + yield return new TestCaseData(10, 5, false, "1.0.1", false); + + //IsValidNumber_WithValueGreaterThanPrecision_ReturnFalse + yield return new TestCaseData(2, 1, false, "-1.0", false); + yield return new TestCaseData(2, 1, false, "+1.0", false); + yield return new TestCaseData(2, 1, false, "10.0", false); + yield return new TestCaseData(2, 1, false, "1.00", false); + + //IsValidNumber_WithFracPartGreaterThanScale_ReturnFalse + yield return new TestCaseData(10, 1, true, "1.00", false); + yield return new TestCaseData(10, 1, false, "-1.00", false); + + //IsValidNumber_OnlyPositiveWithNegativeValue_ReturnFalse + yield return new TestCaseData(10, 5, true, "-1.0", false); + } } } } \ No newline at end of file