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 2 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
32 changes: 20 additions & 12 deletions Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using FluentAssertions;
using NUnit.Framework;
using NUnit.Framework.Legacy;

namespace HomeExercise.Tasks.ObjectComparison;
Expand All @@ -13,17 +14,13 @@ 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);

This comment was marked as outdated.

actualTsar.Should().BeEquivalentTo(expectedTsar, options => options.
Excluding(x => x.Id).
Excluding(x=>x.Parent));

This comment was marked as outdated.


expectedTsar.Parent.Should().BeEquivalentTo(actualTsar.Parent, options => options.
Excluding(x =>x.Id));

This comment was marked as outdated.

}

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

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

This comment was marked as outdated.

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

private bool AreEqual(Person? actual, Person? expected)
Expand Down
59 changes: 38 additions & 21 deletions Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@

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

namespace HomeExercise.Tasks.NumberValidator;

[TestFixture]

This comment was marked as outdated.

public class NumberValidatorTests
{
[Test]
public void Test()
[TestCase(-1,2,true,ExpectedResult = true,TestName = "Negative precision")]
[TestCase(1,0,true,ExpectedResult = false,TestName = "Not catch Exception")]

This comment was marked as outdated.

[TestCase(2,5,true,ExpectedResult = true,TestName = "Precision less then scale")]
[TestCase(2,-3,true,ExpectedResult = true,TestName = "Negative scale")]
public bool NumberValidator_Exceptions_Test(int precision, int scale, bool onlyPositive)

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));
try

This comment was marked as outdated.

{
new NumberValidator(precision, scale, onlyPositive);
}
catch (ArgumentException)
{
return true;
}

return false;
}

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"));
[Test, TestCaseSource(nameof(NumberValidatorTestCases))]

This comment was marked as outdated.

public bool IsValidNumber(NumberValidator numberValidator, String number)
{
return numberValidator.IsValidNumber(number);
}

public static IEnumerable<TestCaseData> NumberValidatorTestCases
{
get
{
yield return new TestCaseData(new NumberValidator(8, 2, true), "0.0").Returns(true);
yield return new TestCaseData(new NumberValidator(3, 0, true), "0").Returns(true);
yield return new TestCaseData(new NumberValidator(4, 2, true), "-1.23").Returns(false);
yield return new TestCaseData(new NumberValidator(4, 2, false), "-1.24").Returns(true);
yield return new TestCaseData(new NumberValidator(5, 2, true), "1.253").Returns(false);
yield return new TestCaseData(new NumberValidator(3, 2, false), "a.ds").Returns(false);
yield return new TestCaseData(new NumberValidator(4, 2, true), "").Returns(false);
yield return new TestCaseData(new NumberValidator(5, 2, true), "+-1.25").Returns(false);
yield return new TestCaseData(new NumberValidator(5, 2, true), null).Returns(false);
yield return new TestCaseData(new NumberValidator(3, 0, true), "1234").Returns(false);

This comment was marked as outdated.

}
}
}