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

Савенко Дмитрий #15

Open
wants to merge 2 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
31 changes: 18 additions & 13 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 @@ -7,23 +8,17 @@ 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);

This comment was marked as outdated.

Copy link
Author

Choose a reason for hiding this comment

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

До конца не понял, что именно прибрать надо там, максимум что я понял, то название у "Проверка текущего царя"

Choose a reason for hiding this comment

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

Все правильно понял)

actualTsar.Should().BeEquivalentTo(expectedTsar, options => options
.Excluding(x => x.Name == nameof(Person.Id) &&
x.DeclaringType == typeof(Person))
.IgnoringCyclicReferences());
}

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

// Какие недостатки у такого подхода?
/* 1. Ограниченная расширяемость:

This comment was marked as outdated.

* Когда мы добавим что-то в класс Person, придется метод AreEqual обновлять вручную.
* Тем самым будет усложняться поддержка.
*2. Недостаточная читаемость и ясность:
* Загроможденный код: AreEqual содержит явные проверки каждого свойства,
* тем самым делая код громоздким и менее понятным.
* Также логика сравнения скрыта внутри метода, тем самым затрудняя понимания сравнения объектов.
* 3. Отсутствие подробных сообщений об ошибках:
* True или False не дает никакой детальной информации об ошибке, что усложнит исправления кода.
*/
ClassicAssert.True(AreEqual(actualTsar, expectedTsar));
}

Expand Down
74 changes: 52 additions & 22 deletions Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@

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

namespace HomeExercise.Tasks.NumberValidator;

[TestFixture]
public class NumberValidatorTests
{
[Test]
public void Test()
private const string ConstructorTestNamePrefix = "Constructor_Should";
private const string IsValidNumberTestNamePrefix = "IsValidNumber_ShouldReturn";

[TestCase(-1, 2, true, TestName = $"{ConstructorTestNamePrefix}Throw_WhenPrecisionIsNegative")]
[TestCase(1, -2, true, TestName = $"{ConstructorTestNamePrefix}Throw_WhenScaleIsNegative")]
[TestCase(1, 2, true, TestName = $"{ConstructorTestNamePrefix}Throw_WhenScaleExceedsPrecision")]
[TestCase(1, 1, true, TestName = $"{ConstructorTestNamePrefix}Throw_WhenScaleEqualsPrecision")]
[TestCase(0, 0, false, TestName = $"{ConstructorTestNamePrefix}Throw_WhenPrecisionIsZero")]
public void Constructor_ShouldThrowArgumentException_WhenInvalidArguments(int precision, int scale, bool onlyPositive)
{
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));
var act = () => new NumberValidator(precision, scale, onlyPositive);
act.Should().Throw<ArgumentException>();
}

[TestCase(1, 0, true, TestName = $"{ConstructorTestNamePrefix}NotThrow_WhenValidArguments")]
public void Constructor_ShouldNotThrow_WhenValidArguments(int precision, int scale, bool onlyPositive)
{
var act = () => new NumberValidator(precision, scale, onlyPositive);
act.Should().NotThrow();
}

[TestCase("0", 1, 0, true, TestName = $"{IsValidNumberTestNamePrefix}True_WhenSingleDigit")]
[TestCase("0.1", 2, 1, true, TestName = $"{IsValidNumberTestNamePrefix}True_WhenValidNumberWithDecimalPoint")]
[TestCase("0,1", 2, 1, true, TestName = $"{IsValidNumberTestNamePrefix}True_WhenValidNumberWithComma")]
[TestCase("-1", 2, 0, false, TestName = $"{IsValidNumberTestNamePrefix}True_WhenNegativeIntegerAllowed")]
[TestCase("-1.1", 3, 1, false, TestName = $"{IsValidNumberTestNamePrefix}True_WhenNegativeDecimalAllowed")]
[TestCase("-1,1", 3, 1, false, TestName = $"{IsValidNumberTestNamePrefix}True_WhenNegativeDecimalWithCommaAllowed")]
[TestCase("+1", 2, 0, true, TestName = $"{IsValidNumberTestNamePrefix}True_WhenPositiveIntegerWithSign")]
[TestCase("+1.1", 3, 1, true, TestName = $"{IsValidNumberTestNamePrefix}True_WhenPositiveDecimalWithSign")]
[TestCase("+1,1", 3, 1, true, TestName = $"{IsValidNumberTestNamePrefix}True_WhenPositiveDecimalWithComma")]
public void IsValidNumber_ShouldReturnTrue(string value, int precision, int scale, bool onlyPositive)
{
var validator = new NumberValidator(precision, scale, onlyPositive);
var result = validator.IsValidNumber(value);
result.Should().Be(true);
}

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("", 2, 1, true, TestName = $"{IsValidNumberTestNamePrefix}False_WhenEmptyString")]
[TestCase(".0", 2, 1, true, TestName = $"{IsValidNumberTestNamePrefix}False_WhenMissingIntegerPart")]
[TestCase("0.", 2, 1, true, TestName = $"{IsValidNumberTestNamePrefix}False_WhenMissingFractionalPart")]
[TestCase("-1", 2, 0, true, TestName = $"{IsValidNumberTestNamePrefix}False_WhenNegativeAndOnlyPositive")]
[TestCase("a.a", 2, 1, true, TestName = $"{IsValidNumberTestNamePrefix}False_WhenNonNumericString")]
[TestCase("1.1.1", 3, 2, true, TestName = $"{IsValidNumberTestNamePrefix}False_WhenMultipleDecimalPoints")]
[TestCase("23,1", 2, 1, true, TestName = $"{IsValidNumberTestNamePrefix}False_WhenExceedsPrecisionWithComma")]
[TestCase("23,1", 3, 0, true, TestName = $"{IsValidNumberTestNamePrefix}False_WhenExceedsScaleWithComma")]
[TestCase("+1", 1, 0, true, TestName = $"{IsValidNumberTestNamePrefix}False_WhenSignExceedsPrecision")]
public void IsValidNumber_ShouldReturnFalse(string value, int precision, int scale, bool onlyPositive)
{
var validator = new NumberValidator(precision, scale, onlyPositive);
var result = validator.IsValidNumber(value);
result.Should().Be(false);
}
}