Skip to content

Commit

Permalink
NumberValidator great test case merge
Browse files Browse the repository at this point in the history
  • Loading branch information
batyadmx committed Nov 30, 2023
1 parent 71fdae7 commit 6fc28bc
Showing 1 changed file with 41 additions and 57 deletions.
98 changes: 41 additions & 57 deletions cs/HomeExercises/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;

namespace HomeExercises
Expand All @@ -19,71 +21,53 @@ public void NumberValidator_InvalidParameters_ThrowsArgumentException(int precis
Assert.Throws<ArgumentException>(() => 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);
}
}
}
}

0 comments on commit 6fc28bc

Please sign in to comment.