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

Вильданов Савелий #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
31 changes: 18 additions & 13 deletions Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
using NUnit.Framework;
using FluentAssertions;
using NUnit.Framework;
using NUnit.Framework.Legacy;

namespace HomeExercise.Tasks.ObjectComparison;

public class ObjectComparison
{
[Test]
[Description("Проверка текущего царя")]
[Category("ToRefactor")]
public void CheckCurrentTsar()
public void TsarRegistry_ShouldBeEqual_WithCurrentTsar()
{
var actualTsar = TsarRegistry.GetCurrentTsar();

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(x => x.Path.EndsWith("Id"))
.AllowingInfiniteRecursion()
.IncludingProperties());
}

[Test]
Expand All @@ -36,6 +33,14 @@ public void CheckCurrentTsar_WithCustomEquality()

// Какие недостатки у такого подхода?
ClassicAssert.True(AreEqual(actualTsar, expectedTsar));
/*
* Недостатки:

This comment was marked as outdated.

* 1. Такая реализация теста не даёт никакой конкретики при обвале теста,
* он скажет только Success или Failed, но не причину, а точнее мы не узнаем
* какое конкретно поле класса Person показало несовпадение, в отличие от моего решения.
* 2. Ликвидность. Если нам потребуется сравнить каких-нибудь двух Person без определённого поля,
* тогда в текущем классе потребуется несколько реализаций AreEqual, что очень плохо.
*/
}

private bool AreEqual(Person? actual, Person? expected)
Expand All @@ -49,4 +54,4 @@ private bool AreEqual(Person? actual, Person? expected)
&& actual.Weight == expected.Weight
&& AreEqual(actual.Parent, expected.Parent);
}
}
}
62 changes: 43 additions & 19 deletions Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@

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

namespace HomeExercise.Tasks.NumberValidator;

[TestFixture]

This comment was marked as outdated.

public class NumberValidatorTests
{
[TestCase(-1, 2, true, TestName = "Negative precision")]
[TestCase(2, 5, true, TestName = "Precision less then scale")]
[TestCase(2, -3, true, TestName = "Negative scale")]
[TestCase(-5, -3, true, TestName = "Negative scale and Precision less then scale ")]
[TestCase(null, null, null, TestName = "Null input data")]

This comment was marked as outdated.

public void NumberValidator_ShouldThrowArgumentException_WithIncorrectData(int precision, int scale,
bool onlyPositive)

This comment was marked as outdated.

{
Action act = () => new NumberValidator(precision, scale, onlyPositive);
act.Should().Throw<ArgumentException>();
}

[Test]
public void Test()
public void NumberValidator_ShouldNotThrowArgumentException_WithCorrectData()
{
Action act = () => new NumberValidator(1, 0, true);
act.Should().NotThrow<ArgumentException>();
}

[TestCase(8, 2, true, "0.0", ExpectedResult = true)]
[TestCase(3, 0, true, "0", ExpectedResult = true)]
[TestCase(4, 2, false, "-1.24", ExpectedResult = true)]
[TestCase(4, 2, false, "-1.24", ExpectedResult = true)]
public bool NumberValidator_ShouldReturnTrue_WithCorrectData(int precision, int scale, bool onlyPositive,
String number)

This comment was marked as outdated.

{
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));
NumberValidator numberValidator = new NumberValidator(precision, scale, onlyPositive);
return numberValidator.IsValidNumber(number);
}

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"));
[TestCase(4, 2, true, "-1.23", ExpectedResult = false)]
[TestCase(5, 2, true, "1.253", ExpectedResult = false)]
[TestCase(3, 0, false, "a.ds", ExpectedResult = false)]
[TestCase(4, 2, false, "", ExpectedResult = false)]
[TestCase(5, 2, true, "+-1.25", ExpectedResult = false)]
[TestCase(5, 2, true, null, ExpectedResult = false)]
[TestCase(3, 0, true, "1234", ExpectedResult = false)]
[TestCase(5, 2, true, "\r", ExpectedResult = false)]
[TestCase(3, 0, true, "1 23", ExpectedResult = false)]
[TestCase(3, 0, true, " 2.3", ExpectedResult = false)]
public bool NumberValidator_ShouldReturnFalse_WithInCorrectData(int precision, int scale, bool onlyPositive,
String number)
{
NumberValidator numberValidator = new NumberValidator(precision, scale, onlyPositive);
return numberValidator.IsValidNumber(number);
}
}