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

Кашин Александр #22

Open
wants to merge 5 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
37 changes: 23 additions & 14 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 @@ -10,22 +11,30 @@ public class ObjectComparison
public void CheckCurrentTsar()
{
var actualTsar = TsarRegistry.GetCurrentTsar();
var expectedParentTsar = TestPersonBuilder.Create()
.WithName("Vasili III of Russia")
.WithAge(28)
.WithHeight(170)
.WithWeight(60)
.Build();

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);
var expectedTsar = TestPersonBuilder.Create()
.WithName("Ivan IV The Terrible")
.WithAge(54)
.WithHeight(170)
.WithWeight(70)
.WithParent(expectedParentTsar)
.Build();

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

// 1. Код менее читаемый и менее декларативный.
MrSasha00 marked this conversation as resolved.
Show resolved Hide resolved
// 2. Требуется понимание того, что происходит в AreEqual.
// 3. Потребуется вручную обновлять логику сравнения в AreEqual, что может привести к ошибкам.
// 4. Не информативное сообщение при упавшем тесте.
// 5. При добавлении новых полей в Person потребуется обновлять все места создания Person.
[Test]
[Description("Альтернативное решение. Какие у него недостатки?")]
public void CheckCurrentTsar_WithCustomEquality()
Expand All @@ -49,4 +58,4 @@ private bool AreEqual(Person? actual, Person? expected)
&& actual.Weight == expected.Weight
&& AreEqual(actual.Parent, expected.Parent);
}
}
}
51 changes: 51 additions & 0 deletions Testing/Basic/Homework/1. ObjectComparison/TestPersonBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace HomeExercise.Tasks.ObjectComparison;

public class TestPersonBuilder

Choose a reason for hiding this comment

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

Молодец, что сделал билдер для тестов, код стал читаемее :)

{
private string name = string.Empty;
private int age;
private int height;
private int weight;
private Person? parent;

private TestPersonBuilder() { }

public static Person TestUser() => Create().Build();

public static TestPersonBuilder Create()
{
return new TestPersonBuilder();
}

public Person Build() => new(name, age, height, weight, parent);

public TestPersonBuilder WithName(string newName)
{
name = newName;
return this;
}

public TestPersonBuilder WithAge(int newAge)
{
age = newAge;
return this;
}

public TestPersonBuilder WithHeight(int newHeight)
{
height = newHeight;
return this;
}

public TestPersonBuilder WithWeight(int newWeight)
{
weight = newWeight;
return this;
}

public TestPersonBuilder WithParent(Person newParent)
{
parent = newParent;
return this;
}
}
169 changes: 148 additions & 21 deletions Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,158 @@

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

namespace HomeExercise.Tasks.NumberValidator;

[TestFixture]
public class NumberValidatorTests
{
#region Constructor

private const string PRECISION_VALIDATION_EXCEPTION_MESSAGE = "precision must be a positive number";
private const string SCALE_VALIDATION_EXCEPTION_MESSAGE = "precision must be a non-negative number less or equal than precision";

[Test]
public void Constructor_ThrowArgumentException_PrecisionIsZero()
{
var act = () => new NumberValidator(0, 2);

act.Should()
.Throw<ArgumentException>()
.WithMessage(PRECISION_VALIDATION_EXCEPTION_MESSAGE);
}

[Test]
public void Constructor_ThrowArgumentException_PrecisionIsNegative()
{
var act = () => new NumberValidator(-1, 2);

act.Should()
.Throw<ArgumentException>()
.WithMessage(PRECISION_VALIDATION_EXCEPTION_MESSAGE);
}

[Test]
public void Constructor_ThrowArgumentException_ScaleIsNegative()
{
var act = () => new NumberValidator(1, -1);

act.Should()
.Throw<ArgumentException>()
.WithMessage(SCALE_VALIDATION_EXCEPTION_MESSAGE);
}

[Test]
public void Constructor_ThrowArgumentException_PrecisionIsLessThenScale()
{
var act = () => new NumberValidator(1, 3);

act.Should()
.Throw<ArgumentException>()
.WithMessage(SCALE_VALIDATION_EXCEPTION_MESSAGE);
}

[Test]
public void Constructor_ThrowArgumentException_PrecisionIsEqualToScale()
{
var act = () => new NumberValidator(2, 2);

act.Should()
.Throw<ArgumentException>()
.WithMessage(SCALE_VALIDATION_EXCEPTION_MESSAGE);
}

[Test]
public void Constructor_DoesNoThrow_CorrectParams()
{
var act = () => new NumberValidator(1, 0);

act.Should().NotThrow<ArgumentException>();
act.Invoke().Should().NotBeNull();
}

#endregion

#region IsValidNumber

[TestCase(null)]
[TestCase("")]
public void IsValidNumber_ReturnFalse_ValueIsNullOrEmpty(string value)
{
var validator = new NumberValidator(3, 2);

validator.IsValidNumber(value).Should().BeFalse();
}

[TestCase("a.sd")]
[TestCase("3,3.3")]
[TestCase("3..3")]
public void IsValidNumber_ReturnFalse_RegexNotMatched(string value)
{
var validator = new NumberValidator(3, 2);

validator.IsValidNumber(value).Should().BeFalse();
}

[TestCase("1111")]
[TestCase("1")]
[TestCase("+111")]
public void IsValidNumber_ReturnTrue_PrecisionIsEqualOrLessThenValueLength(string value)
{
var validator = new NumberValidator(4);

validator.IsValidNumber(value).Should().BeTrue();
}

[TestCase("1111")]
[TestCase("+111")]
public void IsValidNumber_ReturnFalse_ValueLengthIsGreaterThenPrecision(string value)
{
var validator = new NumberValidator(3);

validator.IsValidNumber(value).Should().BeFalse();
}

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

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"));
public void IsValidNumber_ReturnFalse_FractionalPartIsGreaterThenScale()
{
var validator = new NumberValidator(7, 2);

validator.IsValidNumber("1.111").Should().BeFalse();
}

[TestCase("1.111")]
[TestCase("1.1")]
[TestCase("1")]
public void IsValidNumber_ReturnTrue_ValueLengthIsEqualOrLessThenScale(string value)
{
var validator = new NumberValidator(4, 3);

validator.IsValidNumber(value).Should().BeTrue();
}

[Test]
public void IsValidNumber_ReturnFalse_OnlyPositiveIsTrueWithNegativeValue()
{
var validator = new NumberValidator(4, 2, true);

validator.IsValidNumber("-1.11").Should().BeFalse();
}

[TestCase("1.11")]
[TestCase("+1.11")]
public void IsValidNumber_ReturnTrue_OnlyPositiveIsTrueWithPositiveValue(string value)
{
var validator = new NumberValidator(4, 2, true);

validator.IsValidNumber(value).Should().BeTrue();
}

[Test] public void IsValidNumber_ReturnTrue_OnlyPositiveIsFalseWithNegativeValue()
{
var validator = new NumberValidator(4, 2, false);

validator.IsValidNumber("-1.11").Should().BeTrue();
}

#endregion
}