diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..a0fca21 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using FluentAssertions; +using NUnit.Framework; using NUnit.Framework.Legacy; namespace HomeExercise.Tasks.ObjectComparison; @@ -13,17 +14,8 @@ public void CheckCurrentTsar() 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.IsEqual(expectedTsar); } [Test] @@ -35,6 +27,14 @@ public void CheckCurrentTsar_WithCustomEquality() new Person("Vasili III of Russia", 28, 170, 60, null)); // Какие недостатки у такого подхода? + /* + * 1) Если у класса изменять поля (удалять или добавлять), то также придется изменять + * логику проверки на соответствие сущностей. В случае с удалением поля программа вообще не скомпилируется + * 2) Возможна бесконечная рекурсия + * 3) При падении теста не поймем в чем была причина. Ожидался True, а получили False, вот и вся инфа( + * 4) Использование FluentAssertion ускоряет понимание логики секции проверки, за счет использования + * method chaining. В данном тесте вникнуть в логику проверки теста сложнее, чем в первом. + */ ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); } @@ -50,3 +50,16 @@ private bool AreEqual(Person? actual, Person? expected) && AreEqual(actual.Parent, expected.Parent); } } + +internal static class ObjectComparisonAssertExtensions +{ + public static void IsEqual(this Person actualTsar, Person expectedTsar) + { + actualTsar.Should().BeEquivalentTo( + expectedTsar, + options => options + .Excluding(x => x.Name == nameof(Person.Id) && x.DeclaringType == typeof(Person)) + .AllowingInfiniteRecursion() + .IgnoringCyclicReferences()); + } +} \ 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..05b9af5 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,31 +1,42 @@ - +using FluentAssertions; using NUnit.Framework; -using NUnit.Framework.Legacy; namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [Test] - public void Test() + [TestCase(-1, 5, TestName = "IfPrecisionIsNegative")] + [TestCase(5, -1, TestName = "IfScaleIsNegative")] + [TestCase(2, 5, TestName = "IfScaleGreaterPrecision")] + [TestCase(2, 2, TestName = "IfScaleEqualPrecision")] + public void ShouldThrowArgumentException(int precision, int scale) { - 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)); + var numberValidatorConstructor = () => new NumberValidator(precision, scale); + + numberValidatorConstructor.Should().Throw(); + } - 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(null, ExpectedResult = false, TestName = "IfValueIsNull")] + [TestCase(" ", ExpectedResult = false, TestName = "IfValueIsEmpty")] + [TestCase("2.a1", ExpectedResult = false, TestName = "IfValueContainsLetter")] + [TestCase("0.", ExpectedResult = false, TestName = "IfValueContainsSeparatorWithoutFracPart")] + [TestCase(",0", ExpectedResult = false, TestName = "IfValueWithoutIntPart")] + [TestCase("0*0", ExpectedResult = false, TestName = "IfValueSeparatorIncorrect")] + [TestCase("!0,0", ExpectedResult = false, TestName = "IfValueSignIncorrect")] + [TestCase("1,234", ExpectedResult = false, TestName = "IfValueFracPartMoreThanScale")] + [TestCase("11111", ExpectedResult = false, TestName = "IfValueIntPartMoreThanPrecision")] + [TestCase("225,32", ExpectedResult = false, TestName = "IfValueWithFracPartMoreThanPrecision")] + [TestCase("+14,23", ExpectedResult = false, TestName = "IfValueWithSignMoreThanPrecision")] + [TestCase("-0", ExpectedResult = false, TestName = "IfOnlyPositiveTrueAndValueContainsMinus")] + [TestCase("-1,4", false, ExpectedResult = true, TestName = "IfOnlyPositiveFalseAndValueContainsMinus")] + [TestCase("1", ExpectedResult = true, TestName = "IfValueIntPart")] + [TestCase("+1,44", ExpectedResult = true, TestName = "IfValueContainsFracPartAndSign")] + [TestCase("15,44", ExpectedResult = true, TestName = "IfValueContainsFracPart")] + public bool IsValidNumber_ShouldReturnExpectedResult(string value, bool onlyPositive = true) + { + var numberValidator = new NumberValidator(4, 2, onlyPositive); + + return numberValidator.IsValidNumber(value); } } \ No newline at end of file