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

Черных Илья правки ревью #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions Testing/Basic/.idea/.idea.Basic.dir/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Testing/Basic/.idea/.idea.Basic.dir/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NUnit.Framework;
using FluentAssertions;
using FluentAssertions.Equivalency;
using NUnit.Framework;
using NUnit.Framework.Legacy;

namespace HomeExercise.Tasks.ObjectComparison;
Expand All @@ -14,16 +16,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((IMemberInfo memberInfo) => memberInfo.Name == "Id")

Choose a reason for hiding this comment

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

Можно было немного попроще - через nameof, но так тоже норм)

.AllowingInfiniteRecursion()
);
}

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

// Какие недостатки у такого подхода?
// 1) При добавлении нового поля/свойства в класс нужно будет добавлять его проверку в метод AreEqual,
// если этого не сделать, то тест будет выполнять проверку не верно. В моём решении при добавлении поля/свойства
// тест менять не нужно (нужно менять только если мы хотим исключить проверку на это поле/свойство)
// 2) Если поле/свойство будет иметь тип класс, то нужно будет добавлять проверку на этот класс по полям/свойства
// 3) Если тест сломается, то будет неочевидно на каком именно поле/свойстве расхождение
ClassicAssert.True(AreEqual(actualTsar, expectedTsar));
}

Expand Down
130 changes: 112 additions & 18 deletions Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,125 @@

using FluentAssertions;
using NUnit.Framework;
using NUnit.Framework.Legacy;

namespace HomeExercise.Tasks.NumberValidator;

[TestFixture]
public class NumberValidatorTests
{
[Test]
public void Test()
public void Constructor_ShouldThrowArgumentException_WhenPrecisionIsNegative()
{
Action action = () => new NumberValidator(-1, 2, true);

action.Should().Throw<ArgumentException>()
.WithMessage("precision must be a positive number");
}

[Test]
public void Constructor_ShouldThrowArgumentException_WhenScaleIsNegative()
{
Action act = () => new NumberValidator(3, -1, true);

act.Should().Throw<ArgumentException>()
.WithMessage("precision must be a non-negative number less or equal than precision");
}

[Test]
public void Constructor_ShouldThrowArgumentException_WhenScaleExceedsThePrecision()
{
Action act = () => new NumberValidator(1, 4, true);

act.Should().Throw<ArgumentException>()
.WithMessage("precision must be a non-negative number less or equal than precision");
}

[Test]
public void Constructor_ShouldNotThrowException_WhenInputValuesAreValid()
{
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));
}

[TestCase(4, 3, true, "")]
[TestCase(4, 3, true, null)]
[TestCase(4, 3, true, "a")]
[TestCase(4, 3, true, "a.sd")]
[TestCase(4, 3, true, "a.0")]
[TestCase(10, 7, true, "1.23a4")]
[TestCase(10, 7, true, "123.")]
public void IsValidNumber_ReturnFalse_WhenInputValueIsInvalidFormat(int precision, int scale, bool onlyPositive, string value)
{
var numberValidator = new NumberValidator(precision, scale, onlyPositive);

var isValidNumber = numberValidator.IsValidNumber(value);

isValidNumber.Should().Be(false);
}

[TestCase(5, 2, "-123")]
[TestCase(5, 4, "-1.23")]
public void IsValidNumber_ReturnFalse_WhenValueIsNegativeAndOnlyPositiveIsTrue(int precision, int scale,
string value)
{
var numberValidator = new NumberValidator(precision, scale, true);

var isValidNumber = numberValidator.IsValidNumber(value);

isValidNumber.Should().Be(false);
}

[TestCase(2, 0, "12", true)]
[TestCase(2, 1, "123", false)]
public void IsValidNumber_ReturnExpectedResult_WhenInputValueIsPositiveInteger(int precision, int scale,
string value, bool expectedResult)
{
var numberValidator = new NumberValidator(precision, scale, true);

var isValidNumber = numberValidator.IsValidNumber(value);

isValidNumber.Should().Be(expectedResult);
}

[TestCase(3, 0, "-12", true)]
[TestCase(3, 1, "-123", false)]
public void IsValidNumber_ReturnExpectedResult_WhenInputValueIsNegativeInteger(int precision, int scale, string value,
bool expectedResult)
{
var numberValidator = new NumberValidator(precision, scale);

var isValidNumber = numberValidator.IsValidNumber(value);

isValidNumber.Should().Be(expectedResult);
}

[TestCase(8, 4, "12.34", true)]
[TestCase(5, 4, "11.234", true)]
[TestCase(7, 3, "1.234", true)]
[TestCase(8, 4, "+123,4567", true)]
[TestCase(4, 2, "+12.34", false)]
[TestCase(6, 2, "1.234", false)]
public void IsValidNumber_ReturnExpectedResult_WhenInputValueIsPositiveDouble(int precision, int scale, string value,
bool expectedResult)
{
var numberValidator = new NumberValidator(precision, scale, true);

var isValidNumber = numberValidator.IsValidNumber(value);

isValidNumber.Should().Be(expectedResult);
}

[TestCase(5, 4, "-12.3", true)]
[TestCase(8, 4, "-1234.567", true)]
[TestCase(12, 4, "-123.4567", true)]
[TestCase(6, 3, "-12,345", true)]
[TestCase(4, 1, "-123,4", false)]
[TestCase(10, 2, "-123.456", false)]
public void IsValidNumber_ReturnExpectedResult_WhenInputValueIsNegativeDouble(int precision, int scale,
string value, bool expectedResult)
{
var numberValidator = new NumberValidator(precision, scale);

var isValidNumber = numberValidator.IsValidNumber(value);

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"));
isValidNumber.Should().Be(expectedResult);
}
}