diff --git a/Testing/Basic/.idea/.idea.Basic.dir/.idea/.gitignore b/Testing/Basic/.idea/.idea.Basic.dir/.idea/.gitignore new file mode 100644 index 0000000..4d98aed --- /dev/null +++ b/Testing/Basic/.idea/.idea.Basic.dir/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/.idea.Basic.iml +/modules.xml +/contentModel.xml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Testing/Basic/.idea/.idea.Basic.dir/.idea/encodings.xml b/Testing/Basic/.idea/.idea.Basic.dir/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/Testing/Basic/.idea/.idea.Basic.dir/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..0e720ea 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -1,4 +1,6 @@ -using NUnit.Framework; +using FluentAssertions; +using FluentAssertions.Equivalency; +using NUnit.Framework; using NUnit.Framework.Legacy; namespace HomeExercise.Tasks.ObjectComparison; @@ -14,16 +16,10 @@ 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.Should().BeEquivalentTo(expectedTsar, options => options + .Excluding((IMemberInfo memberInfo) => memberInfo.Name == "Id") + .AllowingInfiniteRecursion() + ); } [Test] @@ -35,6 +31,11 @@ public void CheckCurrentTsar_WithCustomEquality() new Person("Vasili III of Russia", 28, 170, 60, null)); // Какие недостатки у такого подхода? + // 1) При добавлении нового поля/свойства в класс нужно будет добавлять его проверку в метод AreEqual, + // если этого не сделать, то тест будет выполнять проверку не верно. В моём решении при добавлении поля/свойства + // тест менять не нужно (нужно менять только если мы хотим исключить проверку на это поле/свойство) + // 2) Если поле/свойство будет иметь тип класс, то нужно будет добавлять проверку на этот класс по полям/свойства + // 3) Если тест сломается, то будет неочевидно на каком именно поле/свойстве расхождение ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); } diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 950c9bc..91574f2 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,6 +1,5 @@ - +using FluentAssertions; using NUnit.Framework; -using NUnit.Framework.Legacy; namespace HomeExercise.Tasks.NumberValidator; @@ -8,24 +7,119 @@ namespace HomeExercise.Tasks.NumberValidator; public class NumberValidatorTests { [Test] - public void Test() + public void Constructor_ShouldThrowArgumentException_WhenPrecisionIsNegative() + { + Action action = () => new NumberValidator(-1, 2, true); + + action.Should().Throw() + .WithMessage("precision must be a positive number"); + } + + [Test] + public void Constructor_ShouldThrowArgumentException_WhenScaleIsNegative() + { + Action act = () => new NumberValidator(3, -1, true); + + act.Should().Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + + [Test] + public void Constructor_ShouldThrowArgumentException_WhenScaleExceedsThePrecision() + { + Action act = () => new NumberValidator(1, 4, true); + + act.Should().Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + + [Test] + public void Constructor_ShouldNotThrowException_WhenInputValuesAreValid() { - 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)); + } + + [TestCase(4, 3, true, "")] + [TestCase(4, 3, true, null)] + [TestCase(4, 3, true, "a")] + [TestCase(4, 3, true, "a.sd")] + [TestCase(4, 3, true, "a.0")] + [TestCase(10, 7, true, "1.23a4")] + [TestCase(10, 7, true, "123.")] + public void IsValidNumber_ReturnFalse_WhenInputValueIsInvalidFormat(int precision, int scale, bool onlyPositive, string value) + { + var numberValidator = new NumberValidator(precision, scale, onlyPositive); + + var isValidNumber = numberValidator.IsValidNumber(value); + + isValidNumber.Should().Be(false); + } + + [TestCase(5, 2, "-123")] + [TestCase(5, 4, "-1.23")] + public void IsValidNumber_ReturnFalse_WhenValueIsNegativeAndOnlyPositiveIsTrue(int precision, int scale, + string value) + { + var numberValidator = new NumberValidator(precision, scale, true); + + var isValidNumber = numberValidator.IsValidNumber(value); + + isValidNumber.Should().Be(false); + } + + [TestCase(2, 0, "12", true)] + [TestCase(2, 1, "123", false)] + public void IsValidNumber_ReturnExpectedResult_WhenInputValueIsPositiveInteger(int precision, int scale, + string value, bool expectedResult) + { + var numberValidator = new NumberValidator(precision, scale, true); + + var isValidNumber = numberValidator.IsValidNumber(value); + + isValidNumber.Should().Be(expectedResult); + } + + [TestCase(3, 0, "-12", true)] + [TestCase(3, 1, "-123", false)] + public void IsValidNumber_ReturnExpectedResult_WhenInputValueIsNegativeInteger(int precision, int scale, string value, + bool expectedResult) + { + var numberValidator = new NumberValidator(precision, scale); + + var isValidNumber = numberValidator.IsValidNumber(value); + + isValidNumber.Should().Be(expectedResult); + } + + [TestCase(8, 4, "12.34", true)] + [TestCase(5, 4, "11.234", true)] + [TestCase(7, 3, "1.234", true)] + [TestCase(8, 4, "+123,4567", true)] + [TestCase(4, 2, "+12.34", false)] + [TestCase(6, 2, "1.234", false)] + public void IsValidNumber_ReturnExpectedResult_WhenInputValueIsPositiveDouble(int precision, int scale, string value, + bool expectedResult) + { + var numberValidator = new NumberValidator(precision, scale, true); + + var isValidNumber = numberValidator.IsValidNumber(value); + + isValidNumber.Should().Be(expectedResult); + } + + [TestCase(5, 4, "-12.3", true)] + [TestCase(8, 4, "-1234.567", true)] + [TestCase(12, 4, "-123.4567", true)] + [TestCase(6, 3, "-12,345", true)] + [TestCase(4, 1, "-123,4", false)] + [TestCase(10, 2, "-123.456", false)] + public void IsValidNumber_ReturnExpectedResult_WhenInputValueIsNegativeDouble(int precision, int scale, + string value, bool expectedResult) + { + var numberValidator = new NumberValidator(precision, scale); + + var isValidNumber = numberValidator.IsValidNumber(value); - 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")); + isValidNumber.Should().Be(expectedResult); } } \ No newline at end of file