diff --git a/Testing/.idea/.idea.Testing/.idea/.gitignore b/Testing/.idea/.idea.Testing/.idea/.gitignore new file mode 100644 index 0000000..2fe203b --- /dev/null +++ b/Testing/.idea/.idea.Testing/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/projectSettingsUpdater.xml +/contentModel.xml +/modules.xml +/.idea.Testing.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Testing/.idea/.idea.Testing/.idea/indexLayout.xml b/Testing/.idea/.idea.Testing/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/Testing/.idea/.idea.Testing/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Testing/.idea/.idea.Testing/.idea/vcs.xml b/Testing/.idea/.idea.Testing/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Testing/.idea/.idea.Testing/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ 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..410c656 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; @@ -10,20 +11,16 @@ public class ObjectComparison 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); + var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, 99999999, + new Person("Vasili III of Russia", 28, 170, 60, 99999999, null)); + + actualTsar.Should() + .BeEquivalentTo(expectedTsar, + config => config + .Excluding(person => person.Id) + .Excluding(person => person.Parent.Id)); + // Я специально для теста добавил одно поле, а сам тест остался неизменен. По сути, его нужно изменять только в случае, если нужно добавить какое-то поле в игнор + // типа того, как я добавил айдишники людей, т. е. там скорее всего окажутся только поля, которые должны быть уникальными для каждого объекта, а таких вряд ли будет прям много } [Test] @@ -31,10 +28,11 @@ public void CheckCurrentTsar() public void CheckCurrentTsar_WithCustomEquality() { 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)); + var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, 99999999, + new Person("Vasili III of Russia", 28, 170, 60, 99999999, null)); // Какие недостатки у такого подхода? + // Этот код более читаем, чем был изначальный вариант в первом тесте, но его необходимо будет изменять каждый раз, когда мы будем менять класс, который тестируем ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); } diff --git a/Testing/Basic/Homework/1. ObjectComparison/Person.cs b/Testing/Basic/Homework/1. ObjectComparison/Person.cs index 4846867..3ddabf6 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/Person.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/Person.cs @@ -5,11 +5,12 @@ public class Person { public static int IdCounter = 0; public int Age, Height, Weight; + public uint Wealth; public string Name; public Person Parent; public int Id; - public Person(string name, int age, int height, int weight, Person parent) + public Person(string name, int age, int height, int weight, uint wealth, Person parent) { Id = IdCounter++; Name = name; @@ -17,5 +18,6 @@ public Person(string name, int age, int height, int weight, Person parent) Height = height; Weight = weight; Parent = parent; + Wealth = wealth; } } \ No newline at end of file diff --git a/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs b/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs index f852e90..31f5f84 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs @@ -5,7 +5,7 @@ public class TsarRegistry public static Person GetCurrentTsar() { return new Person( - "Ivan IV The Terrible", 54, 170, 70, - new Person("Vasili III of Russia", 28, 170, 60, null)); + "Ivan IV The Terrible", 54, 170, 70, 99999999, + new Person("Vasili III of Russia", 28, 170, 60, 99999999, null)); } } \ 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..50abcdf 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,92 @@ namespace HomeExercise.Tasks.NumberValidator; [TestFixture] public class NumberValidatorTests { - [Test] - public void Test() + [Test, TestCaseSource(nameof(GenerateValidatorTestsWithExceptions))] + public void Tests_NumberValidator_WithException(int precision, int scale, bool onlyPositive) { - 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 validatorAction = () => new NumberValidator(precision, scale, onlyPositive); + validatorAction.Should().Throw(); + } + + public static IEnumerable GenerateValidatorTestsWithExceptions + { + get + { + var tests = new[] + { + new { precision = -1, scale = 2, onlyPositive = true}, + new { precision = -1, scale = 2, onlyPositive = false}, + new { precision = 1, scale = -1, onlyPositive = false}, + new { precision = 1, scale = 10, onlyPositive = false}, + }; + foreach (var test in tests) + { + yield return new TestCaseData(test.precision, test.scale, test.onlyPositive) + .SetName($"Exception should be thrown with {test.precision} precision, {test.scale} scale, {test.onlyPositive} onlyPositive"); + } + } + } + + [Test, TestCaseSource(nameof(GenerateValidatorTestsWithoutExceptions))] + public void Tests_NumberValidator_WithoutException(int precision, int scale, bool onlyPositive) + { + var validatorAction = () => new NumberValidator(precision, scale, onlyPositive); + validatorAction.Should().NotThrow(); + } - 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")); + public static IEnumerable GenerateValidatorTestsWithoutExceptions + { + get + { + var tests = new[] + { + new { precision = 1, scale = 0, onlyPositive = true}, + }; + foreach (var test in tests) + { + yield return new TestCaseData(test.precision, test.scale, test.onlyPositive) + .SetName($"Exception should be thrown with {test.precision} precision, {test.scale} scale, {test.onlyPositive} onlyPositive"); + } + } + } + + [Test, TestCaseSource(nameof(GenerateValidatorTestIsValidNumberMethod))] + public bool ValidatorTest_IsValidNumber_Method(int precision, int scale, bool onlyPositive, string validatingNumber) + { + var numberValidator = new NumberValidator(precision, scale, onlyPositive); + return numberValidator.IsValidNumber(validatingNumber); + } + + public static IEnumerable GenerateValidatorTestIsValidNumberMethod + { + get + { + var tests = new[] + { + new { precision = 17, scale = 2, onlyPositive = true, validatingNumber = "0.0", expectingResult = true}, + new { precision = 17, scale = 2, onlyPositive = true, validatingNumber = "0", expectingResult = true}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "00.00", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "-0.00", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "+0.00", expectingResult = false}, + new { precision = 4, scale = 2, onlyPositive = true, validatingNumber = "1.23", expectingResult = true}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "+1.23", expectingResult = false}, + new { precision = 17, scale = 2, onlyPositive = true, validatingNumber = "0.000", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "-1.23", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "a.sd", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "", expectingResult = false}, + new { precision = 3, scale = 2, onlyPositive = true, validatingNumber = "0,0", expectingResult = true}, + new { precision = 17, scale = 2, onlyPositive = false, validatingNumber = "+-1.23", expectingResult = false}, + new { precision = 17, scale = 2, onlyPositive = false, validatingNumber = "-1.23", expectingResult = true}, + new { precision = 17, scale = 2, onlyPositive = true, validatingNumber = ".23", expectingResult = false}, + new { precision = 17, scale = 2, onlyPositive = true, validatingNumber = "23.", expectingResult = false}, + }; + foreach (var test in tests) + { + yield return new TestCaseData(test.precision, test.scale, test.onlyPositive, test.validatingNumber) + .Returns(test.expectingResult) + .SetName($"Test with precision: {test.precision}, scale: {test.scale}, onlyPositive: {test.onlyPositive}," + + $"validatingNumber: {test.validatingNumber} should return {test.expectingResult}, but returned {!test.expectingResult}"); + } + } } } \ No newline at end of file diff --git a/Testing/Testing.sln.DotSettings.user b/Testing/Testing.sln.DotSettings.user new file mode 100644 index 0000000..fb8a275 --- /dev/null +++ b/Testing/Testing.sln.DotSettings.user @@ -0,0 +1,8 @@ + + ForceIncluded + ForceIncluded + <SessionState ContinuousTestingMode="0" IsActive="True" Name="NumberValidatorTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>NUnit3x::6ED454CB-A772-43E5-B72D-3FE9DA27337F::net8.0::HomeExercise.Tasks.NumberValidator.NumberValidatorTests</TestId> + </TestAncestor> +</SessionState> \ No newline at end of file