diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..cf31a8b 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -1,7 +1,9 @@ -using NUnit.Framework; +using FluentAssertions; +using NUnit.Framework; using NUnit.Framework.Legacy; namespace HomeExercise.Tasks.ObjectComparison; + public class ObjectComparison { [Test] @@ -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] @@ -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)); } @@ -49,4 +47,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..464ff67 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,4 +1,4 @@ - +using FluentAssertions; using NUnit.Framework; using NUnit.Framework.Legacy; @@ -7,25 +7,72 @@ 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(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(); + } + + [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(); } } \ No newline at end of file