Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Волков Кирилл #236

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactored tests for NumberValidator.cs
caimanchik committed Nov 27, 2023
commit 995aded4cba7a1d97debc2eca184207d70f85fa7
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
using System;
using System.Text.RegularExpressions;
using FluentAssertions;
using NUnit.Framework;

namespace HomeExercises
{
public class NumberValidatorTests
{
[Test]
public void Test()
{
Assert.Throws<ArgumentException>(() => new NumberValidator(-1, 2, true));
Assert.DoesNotThrow(() => new NumberValidator(1, 0, true));
Assert.Throws<ArgumentException>(() => new NumberValidator(-1, 2, false));
Assert.DoesNotThrow(() => new NumberValidator(1, 0, true));

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"));
}
}
// тесты в Tests/NumberValues_Should

public class NumberValidator
{
130 changes: 130 additions & 0 deletions cs/HomeExercises/Tests/NumberValidator_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.Collections;
using FluentAssertions;
using NUnit.Framework;

namespace HomeExercises.Tests
{
public class NumberValidator_Should
{
#region ConstructorTestsSources

private static IEnumerable IncorrectConstructorParamsTests()
{
yield return new TestCaseData(-1, 2, true)
.SetName("Constructor_ThrowsArgumentExceptionOnNegativePrecision");
yield return new TestCaseData(1, 2, true)
.SetName("Constructor_ThrowsArgumentExceptionOnPrecisionLessThanScale");
yield return new TestCaseData(1, 1, true)
.SetName("Constructor_ThrowsArgumentExceptionSamePrecisionAndScale");
yield return new TestCaseData(1, -1, true)
.SetName("Constructor_ThrowsArgumentExceptionOnNegativeScale");
}

private static IEnumerable CorrectConstructorParamsTests()
{
yield return new TestCaseData(2, 1, true)
.SetName("Constructor_WorksWhenPrecisionIsNonNegativeAndGreaterThanScale");
yield return new TestCaseData(2, 1, false)
.SetName("Constructor_WorksWhenPrecisionIsNonNegativeAndGreaterThanScale");
}

#endregion

#region IsValidNumberTestsSources

private static IEnumerable IsValidNumberPrecisionTests()
{
yield return new TestCaseData(3, 2, true, "00.00")
.SetName("IsValidNumber_ReturnsFalse_WhenSumOfIntPartAndFracPartIsGreaterThanPrecision")
.Returns(false);
yield return new TestCaseData(3, 2, true, "-0.00")
.SetName("IsValidNumber_ReturnsFalse_WhenSignWithFracAndIntPartsIsGreaterThanPrecision")
.Returns(false);
yield return new TestCaseData(3, 2, true, "+0.00")
.SetName("IsValidNumber_ReturnsFalse_WhenSignWithFracAndIntPartsIsGreaterThanPrecision")
.Returns(false);

yield return new TestCaseData(17, 2, true, "0.0")
.SetName("IsValidNumber_ReturnsTrue_WhenSumOfIntPartAndFracPartIsNotGreaterThanPrecision")
.Returns(true);
yield return new TestCaseData(17, 2, true, "0")
.SetName("IsValidNumber_ReturnsTrue_WhenIntPartIsNotGreaterThanPrecision")
.Returns(true);
yield return new TestCaseData(17, 2, true, "+0.0")
.SetName("IsValidNumber_ReturnsTrue_WhenSumOfIntPartAndFracPartIsNotGreaterThanPrecision")
.Returns(true);
}

private static IEnumerable IsValidNumberScaleTests()
{
yield return new TestCaseData(17, 2, true, "0.111")
.SetName("IsValidNumber_ReturnsFalse_WhenFracPartIsGreaterThanScale")
.Returns(false);

yield return new TestCaseData(17, 2, true, "0.11")
.SetName("IsValidNumber_ReturnsTrue_WhenFracPartIsNotGreaterThanScale")
.Returns(true);
}

private static IEnumerable IsValidNumberSignTests()
{
yield return new TestCaseData(17, 2, true, "-0.11")
.SetName("IsValidNumber_ReturnsFalse_WhenSignIsNegativeWithOnlyPositive")
.Returns(false);

yield return new TestCaseData(17, 2, true, "+0.11")
.SetName("IsValidNumber_ReturnsTrue_WhenSignIsPositiveWithOnlyPositive")
.Returns(true);
yield return new TestCaseData(17, 2, false, "+0.11")
.SetName("IsValidNumber_ReturnsTrue_WhenSignIsPositiveWithoutOnlyPositive")
.Returns(true);
yield return new TestCaseData(17, 2, false, "-0.11")
.SetName("IsValidNumber_ReturnsTrue_WhenSignIsNegativeWithoutOnlyPositive")
.Returns(true);
}

private static IEnumerable IsValidNumberValueTests()
{
yield return new TestCaseData(17, 2, true, null)
.SetName("IsValidNumber_ReturnsFalse_WhenNullIsGiven")
.Returns(false);
yield return new TestCaseData(17, 2, true, "")
.SetName("IsValidNumber_ReturnsFalse_WhenEmptyStringIsGiven")
.Returns(false);
yield return new TestCaseData(17, 2, true, "a.a")
.SetName("IsValidNumber_ReturnsFalse_WhenNonDigitStringIsGiven")
.Returns(false);
}

#endregion

[TestCaseSource(nameof(IncorrectConstructorParamsTests))]
public void FailsWithIncorrectConstructorArguments(int precision, int scale, bool onlyPositive)
{
new Func<NumberValidator>(() => new NumberValidator(precision, scale, onlyPositive))
.Should()
.ThrowExactly<ArgumentException>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Бывают случаи когда тебе важна не факт ошибки в тесте, но и текст этой ошибки, допустим когда этот текст выводиться клиенту

}

[TestCaseSource(nameof(CorrectConstructorParamsTests))]
public void InitsWithCorrectConstructorArguments(int precision, int scale, bool onlyPositive)
{
new Func<NumberValidator>(() => new NumberValidator(precision, scale, onlyPositive))
.Should()
.NotThrow();
}

[
TestCaseSource(nameof(IsValidNumberPrecisionTests)),
TestCaseSource(nameof(IsValidNumberScaleTests)),
TestCaseSource(nameof(IsValidNumberSignTests)),
TestCaseSource(nameof(IsValidNumberValueTests)),
]
public bool IsValidNumber(int precision, int scale, bool onlyPositive, string value)
{
return new Func<NumberValidator>(() => new NumberValidator(precision, scale, onlyPositive))()
.IsValidNumber(value);
}
}
}
1 change: 1 addition & 0 deletions cs/HomeExercises/Tests/ObjectComparsion_Should.cs
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ public void CheckCurrentTsar_WithCustomEquality()
// 3. Не показывается информация, на каком именно объекте тест упал, что усложняет разработку
// 4. При расширении класса Person нужно будет дописывать AreEqual, а в FluentAssertions достаточно указать IncludingFields в конфигурации и все поля автоматически будут сравниваться
// 5. Повышается читабельность и тесты проще писать
// 7. Не инкапсулирована функциональность сравнения внутри класса

// Улучшайзинги
// 1. Также я вынес инициализацию объектов в метод SetUp, помеченный соответствующим атрибутом