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

2nd attempt #35

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

namespace HomeExercise.Tasks.ObjectComparison;

public class ObjectComparison
{
[Test]
Expand All @@ -15,15 +17,9 @@ public void CheckCurrentTsar()
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(predicate: info => info.Name == nameof(Person.Id)
));
}

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

// Какие недостатки у такого подхода?
//Проблема с читаемостью( сравнение ннго количества полей менее читаемое решение,
//чем сравнивание всех полей исключая поле Id), необходимость обновлять метод AreEqual при изменении сигнатуры Person (добавлении или изменении полей)
ClassicAssert.True(AreEqual(actualTsar, expectedTsar));
}

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

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

Expand All @@ -7,25 +7,72 @@ namespace HomeExercise.Tasks.NumberValidator;
[TestFixture]
public class NumberValidatorTests
{
[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"));
[TestCase(2, 3, true)]
[TestCase(2, -1, false)]
[TestCase(0, 2, false)]
public void Constructor_IncorrectArguments_ShouldThrowArgumentException(int precision, int scale,
bool onlyPositive)
{
Action action = () => new NumberValidator(precision, scale, onlyPositive);
action.Should().Throw<ArgumentException>();
}

[TestCase(1, 0, true)]
public void Constructor_CorrectArguments_ShouldNotThrowArgumentException(int precision, int scale,
bool onlyPositive)
{
Action action = () => new NumberValidator(precision, scale, onlyPositive);
action.Should().NotThrow();
}

[TestCase(3, 2, true, "a.sd")]
[TestCase(3, 2, true, "0+1.0.1")]
public void IsValidNumber_WrongInputType_ShouldReturnFalse(int precision, int scale, bool onlyPositive,
string input)
{
var validator = new NumberValidator(precision, scale, onlyPositive);
validator.IsValidNumber(input).Should().BeFalse();
}

[TestCase(3, 2, true, "00.00")]
public void IsValidNumber_WrongPrecision_ShouldReturnFalse(int precision, int scale, bool onlyPositive,
string input)
{
var validator = new NumberValidator(precision, scale, onlyPositive);
validator.IsValidNumber(input).Should().BeFalse();
}

[TestCase(17, 1, true, "00.00")]
public void IsValidNumber_WrongScale_ShouldReturnFalse(int precision, int scale, bool onlyPositive,
string input)
{
var validator = new NumberValidator(precision, scale, onlyPositive);
validator.IsValidNumber(input).Should().BeFalse();
}

[TestCase(4, 3, true, " ")]
[TestCase(4, 3, true, null)]
public void IsValidNumber_NullOrWhitespaces_ShouldReturnFalse(int precision, int scale, bool onlyPositive,
string input)
{
var validator = new NumberValidator(precision, scale, onlyPositive);
validator.IsValidNumber(input).Should().BeFalse();
}

[TestCase(1, 0, false, "-0")]
public void IsValidNumber_NegativeNumberWhenNotAllowed_ShouldReturnFalse(int precision, int scale,
bool onlyPositive,
string input)
{
var validator = new NumberValidator(precision, scale, onlyPositive);
validator.IsValidNumber(input).Should().BeFalse();
}

[TestCase(17, 2, true, "0")]
public void IsValidNumber_CorrectInput_ShouldReturnTrue(int precision, int scale, bool onlyPositive,
string input)
{
var validator = new NumberValidator(precision, scale, onlyPositive);
validator.IsValidNumber(input).Should().BeTrue();
}
}