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

Холстинин Егор #229

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
99 changes: 81 additions & 18 deletions cs/HomeExercises/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,88 @@ namespace HomeExercises
public class NumberValidatorTests
{
[Test]
public void Test()
public void CreatesWithNoExceptions([Values] 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));

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"));
var creationOfPositiveValidator = new Action(() => { new NumberValidator(2, 1, onlyPositive); });
creationOfPositiveValidator.Should().NotThrow();
}

[TestCase(-1, 2, false, TestName = "Precision < 0")]
[TestCase(3, -1, false, TestName = "Scale < 0")]
[TestCase(3, 4, false, TestName = "Precision < Scale")]
public void ShouldThrowException_WhenInitWithIncorrectData(int precision, int scale, bool onlyPositive)
{
var creationOfValidatorOnlyPositive = new Action(() => { new NumberValidator(precision, scale, onlyPositive); });
creationOfValidatorOnlyPositive.Should().Throw<ArgumentException>();
}

[TestCase(null!, TestName = "Number is null")]
[TestCase("", TestName = "Number is \"\"")]
[TestCase(" ", TestName = "Number is \" \"")]
[TestCase("\n", TestName = "Number is \"\\n\"")]
public void ShouldNotValidate_NonCorrectData(string number)
{
var validator = new NumberValidator(5, 2);
validator.IsValidNumber(number).Should().BeFalse($"{number} is not correct data");
}

[TestCase("abc")]
[TestCase("a.bc")]
[TestCase(".2")]
[TestCase("1.2,3")]
[TestCase(",")]
[TestCase("-.2")]
[TestCase("-1;2")]
public void ShouldNotValidate_NonNumeric(string notNumber)
{
var validator = new NumberValidator(5, 2);
validator.IsValidNumber(notNumber).Should().BeFalse($"{notNumber} is not a number");
}

[TestCase("12.34", true)]
[TestCase("12", true)]
[TestCase("1.000", false)]
[TestCase("1234.00", false)]
public void ShouldValidate_NumbersWithoutSign(string inputValue, bool expected)
{
var validator = new NumberValidator(5, 2, true);
validator.IsValidNumber(inputValue).Should().Be(expected, $"number {inputValue} is {(expected ? "correct" : "incorrect")}");
}

[TestCase("+1.23", true)]
[TestCase("+1", true)]
[TestCase("+0.5", true)]
[TestCase("+0.567", false)]
[TestCase("+123.45", false)]
public void ShouldValidate_NumbersWithPositiveSign(string inputValue, bool expected)
{
var validator = new NumberValidator(5, 2, true);
validator.IsValidNumber(inputValue).Should().Be(expected, $"number {inputValue} is {(expected ? "correct" : "incorrect")}");
}

[TestCase("-1.23", true)]
[TestCase("-1", true)]
[TestCase("-0.5", true)]
[TestCase("-0.567", false)]
[TestCase("-123.45", false)]
public void ShouldValidate_NumbersWithNegativeSign(string inputValue, bool expected)
{
var positiveValidator = new NumberValidator(5, 2, true);
positiveValidator.IsValidNumber(inputValue).Should().Be(false, "only positive validator should not validate negatives");

var validator = new NumberValidator(5, 2);
validator.IsValidNumber(inputValue).Should().Be(expected, $"number {inputValue} is {(expected ? "correct" : "incorrect")}");
}

[TestCase("1,23", true)]
[TestCase("+1,23", true)]
[TestCase("-1,23", true)]
[TestCase("-123,45", false)]
[TestCase("0,456", false)]
public void ShouldValidate_DifferentDelimiter(string inputValue, bool expected)
{
var validator = new NumberValidator(5, 2);
validator.IsValidNumber(inputValue).Should().Be(expected, $"{inputValue} should validate comma");
}
}

Expand Down
25 changes: 14 additions & 11 deletions cs/HomeExercises/ObjectComparison.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using FluentAssertions.Equivalency;
using NUnit.Framework;

namespace HomeExercises
Expand All @@ -7,24 +8,17 @@ public class ObjectComparison
{
[Test]
[Description("Проверка текущего царя")]
[Category("ToRefactor")]
public void CheckCurrentTsar()
{
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.
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
.IncludingFields()
.Excluding(info => info.SelectedMemberInfo.Name == nameof(Person.Id)
&& info.SelectedMemberInfo.DeclaringType == typeof(Person)));
}

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

// Какие недостатки у такого подхода?
/*
* Недостатками такого подхода являются:
* 1. Если тест падает то единственное, что можно из этого понять, что цари не одинаковые
* Никакой информации об их различиях он не выводит
* 2. Из названия теста тоже никак не получится понять что именно тестируется
* 3. При любом расширении класса Person придется дописывать/переписывать компаратор
* 4. Еще ужаснее будет если нововведение в Person будет необязательным и нам придется
* В компараторе поддерживать это опциональное сравнение, а учитывая еще и первый пункт...
*/
Assert.True(AreEqual(actualTsar, expectedTsar));
}

Expand Down