diff --git a/Testing/Basic/Basic.sln b/Testing/Basic/Basic.sln new file mode 100644 index 0000000..44911a3 --- /dev/null +++ b/Testing/Basic/Basic.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35303.130 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Basic", "Basic.csproj", "{61D6F679-81DC-4D99-89CE-C3CEE18F9D83}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {61D6F679-81DC-4D99-89CE-C3CEE18F9D83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {61D6F679-81DC-4D99-89CE-C3CEE18F9D83}.Debug|Any CPU.Build.0 = Debug|Any CPU + {61D6F679-81DC-4D99-89CE-C3CEE18F9D83}.Release|Any CPU.ActiveCfg = Release|Any CPU + {61D6F679-81DC-4D99-89CE-C3CEE18F9D83}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C6BF16E1-4A74-40D0-9D06-39C39FFA1D6F} + EndGlobalSection +EndGlobal diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..2c324dd 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; @@ -14,16 +15,17 @@ 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); + actualTsar.Should() + .BeEquivalentTo(expectedTsar, + options => options + .Excluding(x => x.Id) + .Excluding(x => x.Parent!.Id)); - 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); + //1. ипользование метода CheckCurrentTsar_WithCustomEquality сложнее в понимании + //использование FluentAssertions повышает простоту и читаемость кода; + //2. после создания каждого нового поля, нужно добавлять его в метод + //в моем методе добавлять нужно только поля, которые нужно исключить из сравнения + //таких полей намного меньше, поэтому количество изменений метода меньше; } [Test] diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 950c9bc..1e1b8c7 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,4 +1,5 @@  +using FluentAssertions; using NUnit.Framework; using NUnit.Framework.Legacy; @@ -7,25 +8,48 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [Test] - public void Test() + [TestCase(17, 2, true, "0.0")] + [TestCase(17, 2, true, "0")] + [TestCase(4, 2, true, "+1.23")] + [TestCase(4, 2, false, "-1.23")] + public void IsValidNumber_True_When_CorrectValues(int precision, int scale, bool onlyPositive, string value) { - 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 validator = new NumberValidator(precision, scale, onlyPositive); + validator.IsValidNumber(value).Should().BeTrue(); + } + + [TestCase(3, 2, true, "00.00")] + [TestCase(3, 2, true, "-0.00")] + [TestCase(3, 2, true, "+0.00")] + [TestCase(3, 2, true, "+1.23")] + [TestCase(17, 2, true, "0.000")] + [TestCase(3, 2, true, "-1.23")] + [TestCase(3, 2, true, "a.sd")] + [TestCase(3, 2, true, "")] + [TestCase(3, 2, true, null)] + public void IsValidNumber_False_When_InCorrectValues(int precision, int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + validator.IsValidNumber(value).Should().BeFalse(); + } + - 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(-1, 2, true)] + [TestCase(1, 2, false)] + [TestCase(5, -2, false)] + public void NumberValidator_ThrowException_When_InCorrectValue(int precision, int scale, bool onlyPositive) + { + var action = () => new NumberValidator(precision, scale, onlyPositive); + action.Should().Throw(); + } + + [TestCase(1, 0, true)] + [TestCase(3, 2, true)] + [TestCase(10, 9, false)] + [TestCase(3, 2, false)] + public void NumberValidator_NotThrowException_When_CorrectValue(int precision, int scale, bool onlyPositive) + { + var action = () => new NumberValidator(precision, scale, onlyPositive); + action.Should().NotThrow(); } } \ No newline at end of file