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

Калентьев Илья tg: @m1nus0ne #248

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
61 changes: 43 additions & 18 deletions cs/HomeExercises/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,52 @@ namespace HomeExercises
{
public class NumberValidatorTests
{
[Test]
public void Test()
[TestCaseSource(nameof(ValidationExceptionCases))]
public void ValidationExceptionTest(int precision, int scale, bool onlyPositive, bool shouldThrow)
{
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));
// Action action = () => new NumberValidator(precision, scale, onlyPositive);
// action.Should().Throw<ArgumentException>().When(shouldThrow); When не доступно в старой версии либы(
Action action = () => new NumberValidator(precision, scale, onlyPositive);
if (shouldThrow)
m1nus0ne marked this conversation as resolved.
Show resolved Hide resolved
{
action.Should().Throw<ArgumentException>();
}
else
{
action.Should().NotThrow();
}

}
public static object[] ValidationExceptionCases =
m1nus0ne marked this conversation as resolved.
Show resolved Hide resolved
{
new object[] { 1, 0, true, false },
new object[] { -1, 2, false, true },
new object[] { 2, 3, true, 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"));
[TestCaseSource(nameof(CorrectValidationCases))]
public void CorrectValidationTest(int precision, int scale, bool onlyPositive, string inputCase, bool expected)
m1nus0ne marked this conversation as resolved.
Show resolved Hide resolved
m1nus0ne marked this conversation as resolved.
Show resolved Hide resolved
{
var validator = new NumberValidator(precision, scale, onlyPositive);
var isValid = validator.IsValidNumber(inputCase);
isValid.Should().Be(expected);
}

public static object[] CorrectValidationCases =
{
new object[] { 17, 2, true, "0", true },
new object[] { 3, 2, true, "00.00", false },
new object[] { 3, 2, true, "-0.00", false },
new object[] { 17, 2, true, "0.0", true },
new object[] { 3, 2, true, "+0.00", false },
new object[] { 4, 2, true, "+1.23", true },
new object[] { 3, 2, true, "+1.23", false },
new object[] { 17, 2, true, "0.000", false },
new object[] { 3, 2, true, "-1.23", false },
new object[] { 3, 2, true, "a.sd", false },
new object[] { 1, 0, true, "0", true },
new object[] { 1, 0, false, "-0", false },
};
}

public class NumberValidator
Expand Down
12 changes: 3 additions & 9 deletions cs/HomeExercises/ObjectComparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,8 @@ public void CheckCurrentTsar()
new Person("Vasili III of Russia", 28, 170, 60, null));

// Перепишите код на использование Fluent Assertions.
Assert.AreEqual(actualTsar.Name, expectedTsar.Name);
Assert.AreEqual(actualTsar.Age, expectedTsar.Age);
Assert.AreEqual(actualTsar.Height, expectedTsar.Height);
Assert.AreEqual(actualTsar.Weight, expectedTsar.Weight);

Assert.AreEqual(expectedTsar.Parent!.Name, actualTsar.Parent!.Name);
Assert.AreEqual(expectedTsar.Parent.Age, actualTsar.Parent.Age);
Assert.AreEqual(expectedTsar.Parent.Height, actualTsar.Parent.Height);
Assert.AreEqual(expectedTsar.Parent.Parent, actualTsar.Parent.Parent);
actualTsar.Should().BeEquivalentTo(expectedTsar,
options => options.Excluding(person => person.Id).Excluding(person => person.Parent!.Id));
m1nus0ne marked this conversation as resolved.
Show resolved Hide resolved
}

[Test]
Expand All @@ -36,6 +29,7 @@ public void CheckCurrentTsar_WithCustomEquality()
new Person("Vasili III of Russia", 28, 170, 60, null));

// Какие недостатки у такого подхода?
//Проблема с читаемостью, необходимость обновлять метод AreEqual при изменении сигнатуры Person

Choose a reason for hiding this comment

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

А можешь в этом треде подробнее раскрыть, что ты имеешь в виду под читаемостью и под изменением сигнатуры?

Copy link
Author

Choose a reason for hiding this comment

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

При изменении/добавлении полей возникает необходимость переписывание метода AreEqual, сравнение ннго количества полей менне читаемое решение, чем сравнивание всех полей исключая поле Id ()

Assert.True(AreEqual(actualTsar, expectedTsar));
}

Expand Down