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

Калентьев Илья tg: @m1nus0ne #248

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
81 changes: 63 additions & 18 deletions cs/HomeExercises/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,77 @@
using System;
using System.Text.RegularExpressions;
using FluentAssertions;
using FluentAssertions.Primitives;
using NUnit.Framework;

namespace HomeExercises
{
public class NumberValidatorTests
{
[Test]
public void Test()
[TestCase(2, 3, true)]
[TestCase(-1, 2, false)]
public void Constructor_IncorrectArguments_ShouldThrowArgumentException(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));
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();
}

Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0"));
Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0"));
Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0"));
Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("00.00"));
Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-0.00"));
Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0"));
Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+0.00"));
Assert.IsTrue(new NumberValidator(4, 2, true).IsValidNumber("+1.23"));
Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+1.23"));
Assert.IsFalse(new NumberValidator(17, 2, true).IsValidNumber("0.000"));
Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-1.23"));
Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("a.sd"));
[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, " ")]
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();
}
}

Expand Down
19 changes: 9 additions & 10 deletions cs/HomeExercises/ObjectComparison.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using FluentAssertions;
using System;
using System.Linq;
using System.Runtime.Serialization.Formatters;
using FluentAssertions;
using NUnit.Framework;

namespace HomeExercises
Expand All @@ -16,15 +19,10 @@ public void CheckCurrentTsar()
new Person("Vasili III of Russia", 28, 170, 60, null));

// Перепишите код на использование Fluent Assertions.
Assert.AreEqual(actualTsar.Name, expectedTsar.Name);
Assert.AreEqual(actualTsar.Age, expectedTsar.Age);
Assert.AreEqual(actualTsar.Height, expectedTsar.Height);
Assert.AreEqual(actualTsar.Weight, expectedTsar.Weight);

Assert.AreEqual(expectedTsar.Parent!.Name, actualTsar.Parent!.Name);
Assert.AreEqual(expectedTsar.Parent.Age, actualTsar.Parent.Age);
Assert.AreEqual(expectedTsar.Parent.Height, actualTsar.Parent.Height);
Assert.AreEqual(expectedTsar.Parent.Parent, actualTsar.Parent.Parent);
actualTsar.Should().BeEquivalentTo(expectedTsar, options => options
.Excluding(predicate: info =>
info.SelectedMemberInfo.Name == nameof(Person.Id)
));
}

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

// Какие недостатки у такого подхода?
//Проблема с читаемостью, необходимость обновлять метод AreEqual при изменении сигнатуры Person

Choose a reason for hiding this comment

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

А можешь в этом треде подробнее раскрыть, что ты имеешь в виду под читаемостью и под изменением сигнатуры?

Copy link
Author

Choose a reason for hiding this comment

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

При изменении/добавлении полей возникает необходимость переписывание метода AreEqual, сравнение ннго количества полей менне читаемое решение, чем сравнивание всех полей исключая поле Id ()

Assert.True(AreEqual(actualTsar, expectedTsar));
}

Expand Down