diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..0a985ef 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -1,29 +1,48 @@ -using NUnit.Framework; +using FluentAssertions; +using FluentAssertions.Equivalency; +using NUnit.Framework; using NUnit.Framework.Legacy; +using System; +using System.Linq.Expressions; namespace HomeExercise.Tasks.ObjectComparison; + +public static class EquivalencyAssertionOptionsExtensions +{ + public static EquivalencyAssertionOptions ExcludeRecursively + (this EquivalencyAssertionOptions congiguration, + Expression> expression) + { + var memberExpression = (MemberExpression)expression.Body; + var propertyName = memberExpression.Member.Name; + return congiguration + .Excluding(member => + member.DeclaringType == typeof(TDeclaringType) + && member.Name.Equals(propertyName)); + } +} + public class ObjectComparison { + private Person actualTsar; + + [SetUp] + [Description("Получение экземпляра текущего царя")] + public void SetUp() + { + actualTsar = TsarRegistry.GetCurrentTsar(); + } + [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. - 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, + c => c.ExcludeRecursively(x => x.Id)); } [Test] @@ -34,7 +53,15 @@ public void CheckCurrentTsar_WithCustomEquality() var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, new Person("Vasili III of Russia", 28, 170, 60, null)); - // Какие недостатки у такого подхода? + /* Какие недостатки у такого подхода? + + Основным недостатком данного подхода я считаю необходимость + изменения метода AreEqual при добавлении новых полей класса Person. + Например, если мы добавим в Person поле Gender(пол), + то нам нужно будет в AreEqual так же ручками добавить + проверку совпадения полов: + actual.Gender == expected.Gender. + */ ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); } @@ -49,4 +76,4 @@ private bool AreEqual(Person? actual, Person? expected) && actual.Weight == expected.Weight && AreEqual(actual.Parent, expected.Parent); } -} +} \ No newline at end of file diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 950c9bc..d51912a 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,31 +1,111 @@ - -using NUnit.Framework; -using NUnit.Framework.Legacy; +using NUnit.Framework; namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [Test] - public void Test() - { - Assert.Throws(() => new NumberValidator(-1, 2, true)); - Assert.DoesNotThrow(() => new NumberValidator(1, 0, true)); - Assert.Throws(() => 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(0)] + [TestCase(-1)] + public void NumberValidator_ThrowsArgumentException_WhenPrecisionLessOrEqualThanZero(int precision) + { + Assert.Throws(() => new NumberValidator(precision)); + } + + [TestCase(1, -1)] + public void NumberValidator_ThrowsArgumentException_WhenScaleLessThanZero(int precision, + int scale) + { + Assert.Throws(() => new NumberValidator(precision, scale)); + } + + [TestCase(1, 1)] + [TestCase(1, 2)] + public void NumberValidator_ThrowsArgumentException_WhenScaleMoreOrEqualThanPrecision(int precision, + int scale) + { + Assert.Throws(() => new NumberValidator(precision, scale)); + } + + [TestCase(17, 2, true, null, ExpectedResult = false)] + [TestCase(17, 2, true, "", ExpectedResult = false)] + [TestCase(17, 2, true, " ", ExpectedResult = false)] + public bool IsValidNumber_ReturnFalse_WhenGivenNullOrEmptyString(int precision, + int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + return validator.IsValidNumber(value); + } + + [TestCase(17, 2, true, "0.0", ExpectedResult = true)] + [TestCase(17, 2, true, "0", ExpectedResult = true)] + [TestCase(4, 2, true, "+1.23", ExpectedResult = true)] + public bool IsValidNumber_ReturnTrue_WhenGivenCorrectValues(int precision, + int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + return validator.IsValidNumber(value); + } + + [TestCase(17, 2, true, "0,0", ExpectedResult = true)] + public bool IsValidNumber_ReturnTrue_WhenGivenCorrectValuesWithComma(int precision, + int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + return validator.IsValidNumber(value); + } + + [TestCase(17, 2, true, "a.sd", ExpectedResult = false)] + [TestCase(17, 2, true, "1..2", ExpectedResult = false)] + [TestCase(17, 2, true, "1.2.3", ExpectedResult = false)] + [TestCase(17, 2, true, "1.a", ExpectedResult = false)] + [TestCase(17, 2, true, "a.1", ExpectedResult = false)] + [TestCase(17, 2, true, "0.,0", ExpectedResult = false)] + [TestCase(17, 2, true, ".0", ExpectedResult = false)] + [TestCase(17, 2, false, "+-0.0", ExpectedResult = false)] + [TestCase(17, 2, false, "0.", ExpectedResult = false)] + [TestCase(17, 2, true, "1.1\n1", ExpectedResult = false)] + public bool IsValidNumber_ReturnFalse_WhenGivenNotANumberValues(int precision, + int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + return validator.IsValidNumber(value); + } + + [TestCase(3, 2, true, "00.00", ExpectedResult = false)] + [TestCase(3, 0, true, "0.000", ExpectedResult = false)] + [TestCase(3, 2, false, "-1.23", ExpectedResult = false)] + public bool IsValidNumber_ReturnFalse_WhenPrecisionIsExceeded(int precision, + int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + return validator.IsValidNumber(value); + } + + [TestCase(17, 2, true, "0.000", ExpectedResult = false)] + public bool IsValidNumber_ReturnFalse_WhenScaleIsExceeded(int precision, + int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + return validator.IsValidNumber(value); + } + + [TestCase(3, 2, true, "-0.00", ExpectedResult = false)] + public bool IsValidNumber_ReturnFalse_WhenInvalidNumberSign(int precision, + int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + return validator.IsValidNumber(value); + } + + [TestCase(3, 2, true, "+1.23", ExpectedResult = false)] + [TestCase(4, 2, true, "+1.23", ExpectedResult = true)] + [TestCase(3, 2, false, "-1.23", ExpectedResult = false)] + [TestCase(4, 2, false, "-1.23", ExpectedResult = true)] + public bool IsValidNumber_InvolvesSignInLengthCalculation(int precision, + int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + return validator.IsValidNumber(value); } } \ No newline at end of file