From f8dbb03c5e5ad6a9a327fb9ab5cb434941c26a89 Mon Sep 17 00:00:00 2001 From: qreaqtor Date: Tue, 5 Nov 2024 05:32:49 +0500 Subject: [PATCH 1/5] hw-1 --- .../1. ObjectComparison/ObjectComparison.cs | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index d544c47..7cb96a9 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,19 +15,18 @@ 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(person => person.Id). + Excluding(person => person.Parent.Id)); } + /* + * Преимущества подхода: + * 1) Хорошая информативность: при непрохождении теста ясно показывается, какие поля не совпали. + * 2) Хорошая расширяемость: при добавлении или удалении полей в классе, нужно внести минимум изменений в тесте. + * 3) Хорошая читаемость: из-за меньшего объема кода и понятного названия методов улучшается читаемость кода. + */ - [Test] + [Test] [Description("Альтернативное решение. Какие у него недостатки?")] public void CheckCurrentTsar_WithCustomEquality() { @@ -37,8 +37,15 @@ public void CheckCurrentTsar_WithCustomEquality() // Какие недостатки у такого подхода? ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); } + /* + * Недостатки: + * 1) При измнении класса Person необходимо будет изменять метод AreEqual. + * 2) ClassicAssert.True() принимает на вход булевое выражение, за счет чего сравнение происходит с True и если тест не проходит, + * то в сообщении будет написано что результат не совпал, без дополнительной информации что именно пошло не так. + * 3) Функция сравнения классов не должна определятся внутри класса с тестами. + */ - private bool AreEqual(Person? actual, Person? expected) + private bool AreEqual(Person? actual, Person? expected) { if (actual == expected) return true; if (actual == null || expected == null) return false; From 5e65d7cd0ca828c2c421638f4aa06e473a4fa54f Mon Sep 17 00:00:00 2001 From: qreaqtor Date: Tue, 5 Nov 2024 05:34:18 +0500 Subject: [PATCH 2/5] hw-2 --- .../NumberValidatorTestCases.cs | 80 +++++++++++++++++++ .../NumberValidatorTests.cs | 59 ++++++++------ 2 files changed, 116 insertions(+), 23 deletions(-) create mode 100644 Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs new file mode 100644 index 0000000..d1436a4 --- /dev/null +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs @@ -0,0 +1,80 @@ +using NUnit.Framework; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HomeExercise.Tasks.NumberValidator +{ + // для каждого теста может быть много тест кейсов, поэтому я вынес их в отдельные коллекции + public partial class NumberValidatorTests + { + public static IEnumerable ValidTestCases + { + get + { + yield return new TestCaseData(17, 1, true, "0.0"); + + yield return new TestCaseData(17, 2, true, "0"); + + yield return new TestCaseData(4, 2, true, "+1.23"); + + yield return new TestCaseData(7, 3, false, "+1.00"); + } + } + + public static IEnumerable NotValidTestCases + { + get + { + yield return new TestCaseData(3, 2, true, "00.00"); + + yield return new TestCaseData(3, 2, true, "-0.0"); + + yield return new TestCaseData(3, 2, true, "+0.00"); + + yield return new TestCaseData(3, 2, true, "+1.23"); + + yield return new TestCaseData(3, 2, true, "-1.23"); + + yield return new TestCaseData(3, 2, true, "a.sd"); + + yield return new TestCaseData(17, 2, true, "0.000"); + + yield return new TestCaseData(3, 2, true, ""); + + yield return new TestCaseData(3, 2, true, null); + + yield return new TestCaseData(4, 2, false, "10*00"); + + yield return new TestCaseData(4, 1, false, "1.0.0"); + } + } + + public static IEnumerable ThrowArgumentExceptionTestCases + { + get + { + yield return new TestCaseData(-1, 2, true); + + yield return new TestCaseData(1, -2, false); + + yield return new TestCaseData(1, 2, true); + + yield return new TestCaseData(1, 1, false); + } + } + + public static IEnumerable NotThrowArgumentExceptionTestCases + { + get + { + yield return new TestCaseData(1, 0, true); + + yield return new TestCaseData(2, 1, true); + } + } + } +} diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 950c9bc..dde5a02 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,31 +1,44 @@  +using FluentAssertions; using NUnit.Framework; -using NUnit.Framework.Legacy; +using System.Collections; namespace HomeExercise.Tasks.NumberValidator; [TestFixture] -public class NumberValidatorTests +public partial 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")); - } + [TestCaseSource(nameof(ValidTestCases))] + public void Should_Be_Valid(int precision, int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + + var result = validator.IsValidNumber(value); + + result.Should().BeTrue(); + } + + [TestCaseSource(nameof(NotValidTestCases))] + public void Should_Be_NotValid(int precision, int scale, bool onlyPositive, string value) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + + var result = validator.IsValidNumber(value); + + result.Should().BeFalse(); + } + + [TestCaseSource(nameof(ThrowArgumentExceptionTestCases))] + public void Should_ThrowArgumentException(int precision, int scale, bool onlyPositive) + { + var action = () => new NumberValidator(precision, scale, onlyPositive); + action.Should().Throw(); + } + + [TestCaseSource(nameof(NotThrowArgumentExceptionTestCases))] + public void Should_NotThrowArgumentException(int precision, int scale, bool onlyPositive) + { + var action = () => new NumberValidator(precision, scale, onlyPositive); + action.Should().NotThrow(); + } } \ No newline at end of file From b6f971d88736abe63365b4daff248c48c7e6e7e9 Mon Sep 17 00:00:00 2001 From: qreaqtor Date: Wed, 6 Nov 2024 21:42:09 +0500 Subject: [PATCH 3/5] refactoring --- Testing/Basic/Basic.sln | 25 ++++++++ .../1. ObjectComparison/ObjectComparison.cs | 15 +++-- .../1. ObjectComparison/TsarRegistry.cs | 9 +-- .../NumberValidatorTestCases.cs | 63 +++++++++++-------- .../NumberValidatorTests.cs | 25 +++++--- 5 files changed, 90 insertions(+), 47 deletions(-) create mode 100644 Testing/Basic/Basic.sln diff --git a/Testing/Basic/Basic.sln b/Testing/Basic/Basic.sln new file mode 100644 index 0000000..b8cf0f0 --- /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.35327.3 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Basic", "Basic.csproj", "{ECC863AA-4C3E-4E5C-A2D0-459A2A1D33A3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {ECC863AA-4C3E-4E5C-A2D0-459A2A1D33A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ECC863AA-4C3E-4E5C-A2D0-459A2A1D33A3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ECC863AA-4C3E-4E5C-A2D0-459A2A1D33A3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ECC863AA-4C3E-4E5C-A2D0-459A2A1D33A3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2B8F6D26-176B-4CA4-8C5B-A15FA83E5325} + EndGlobalSection +EndGlobal diff --git a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs index 7cb96a9..12f76d7 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs @@ -13,12 +13,14 @@ 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)); + new Person("Vasili III of Russia", 28, 170, 60, + new Person("Ivan III", 65, 170, 80, null))); - actualTsar.Should().BeEquivalentTo(expectedTsar, - options => options.Excluding(person => person.Id). - Excluding(person => person.Parent.Id)); - } + + actualTsar.Should().BeEquivalentTo(expectedTsar, + options => options + .Excluding(p => p.Path.EndsWith("Id"))); + } /* * Преимущества подхода: * 1) Хорошая информативность: при непрохождении теста ясно показывается, какие поля не совпали. @@ -32,7 +34,8 @@ 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)); + new Person("Vasili III of Russia", 28, 170, 60, + new Person("Ivan III", 65, 170, 80, null))); // Какие недостатки у такого подхода? ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); diff --git a/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs b/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs index f852e90..4f2016d 100644 --- a/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs +++ b/Testing/Basic/Homework/1. ObjectComparison/TsarRegistry.cs @@ -4,8 +4,9 @@ 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)); - } + return new Person("Ivan IV The Terrible", 54, 170, 70, + new Person("Vasili III of Russia", 28, 170, 60, + new Person("Ivan III", 65, 170, 80, null))); + + } } \ No newline at end of file diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs index d1436a4..a581dee 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs @@ -8,72 +8,81 @@ namespace HomeExercise.Tasks.NumberValidator { + using ValidateTestCaseArgs = (int Precision, int Scale, bool OnlyPositive, string Value); + + using ExceptionTestCaseArgs = (int Precision, int Scale); + // для каждого теста может быть много тест кейсов, поэтому я вынес их в отдельные коллекции public partial class NumberValidatorTests { - public static IEnumerable ValidTestCases + + public static IEnumerable ValidTestCases { get { - yield return new TestCaseData(17, 1, true, "0.0"); + yield return (17, 1, true, "0.0"); + + yield return (17, 1, true, "0,0"); + + yield return (17, 1, true, "+1,0"); - yield return new TestCaseData(17, 2, true, "0"); + yield return (17, 2, true, "0"); - yield return new TestCaseData(4, 2, true, "+1.23"); + yield return (4, 1, true, "1234"); - yield return new TestCaseData(7, 3, false, "+1.00"); + yield return (4, 1, true, "+123"); + + yield return (4, 1, true, "+12.3"); + + yield return (7, 3, false, "+1.00"); } } - public static IEnumerable NotValidTestCases + public static IEnumerable NotValidTestCases { get { - yield return new TestCaseData(3, 2, true, "00.00"); - - yield return new TestCaseData(3, 2, true, "-0.0"); - - yield return new TestCaseData(3, 2, true, "+0.00"); + yield return (3, 2, true, "00.00"); - yield return new TestCaseData(3, 2, true, "+1.23"); + yield return (3, 2, true, "-0.0"); - yield return new TestCaseData(3, 2, true, "-1.23"); + yield return (3, 2, true, "+0.00"); - yield return new TestCaseData(3, 2, true, "a.sd"); + yield return (3, 2, true, "a.sd"); - yield return new TestCaseData(17, 2, true, "0.000"); + yield return (17, 2, true, "0.000"); - yield return new TestCaseData(3, 2, true, ""); + yield return (3, 2, true, ""); - yield return new TestCaseData(3, 2, true, null); + yield return (3, 2, true, null); - yield return new TestCaseData(4, 2, false, "10*00"); + yield return (4, 2, false, "10*00"); - yield return new TestCaseData(4, 1, false, "1.0.0"); + yield return (4, 1, false, "1.0.0"); } } - public static IEnumerable ThrowArgumentExceptionTestCases + public static IEnumerable ThrowArgumentExceptionTestCases { get { - yield return new TestCaseData(-1, 2, true); + yield return (-1, 2); - yield return new TestCaseData(1, -2, false); + yield return (1, -2); - yield return new TestCaseData(1, 2, true); + yield return (1, 2); - yield return new TestCaseData(1, 1, false); + yield return (1, 1); } } - public static IEnumerable NotThrowArgumentExceptionTestCases + public static IEnumerable NotThrowArgumentExceptionTestCases { get { - yield return new TestCaseData(1, 0, true); + yield return (1, 0); - yield return new TestCaseData(2, 1, true); + yield return (2, 1); } } } diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index dde5a02..252b936 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -1,44 +1,49 @@  using FluentAssertions; +using Newtonsoft.Json.Linq; using NUnit.Framework; using System.Collections; namespace HomeExercise.Tasks.NumberValidator; +using ValidateTestCaseArgs = (int Precision, int Scale, bool OnlyPositive, string Value); + +using ExceptionTestCaseArgs = (int Precision, int Scale); + [TestFixture] public partial class NumberValidatorTests { [TestCaseSource(nameof(ValidTestCases))] - public void Should_Be_Valid(int precision, int scale, bool onlyPositive, string value) + public void IsValidNumber_Shoud_BeTrue_WhenArgsCorrect(ValidateTestCaseArgs args) { - var validator = new NumberValidator(precision, scale, onlyPositive); + var validator = new NumberValidator(args.Precision, args.Scale, args.OnlyPositive); - var result = validator.IsValidNumber(value); + var result = validator.IsValidNumber(args.Value); result.Should().BeTrue(); } [TestCaseSource(nameof(NotValidTestCases))] - public void Should_Be_NotValid(int precision, int scale, bool onlyPositive, string value) + public void IsValidNumber_Shoud_BeFalse_WhenArgsBad(ValidateTestCaseArgs args) { - var validator = new NumberValidator(precision, scale, onlyPositive); + var validator = new NumberValidator(args.Precision, args.Scale, args.OnlyPositive); - var result = validator.IsValidNumber(value); + var result = validator.IsValidNumber(args.Value); result.Should().BeFalse(); } [TestCaseSource(nameof(ThrowArgumentExceptionTestCases))] - public void Should_ThrowArgumentException(int precision, int scale, bool onlyPositive) + public void Should_Throw_ArgumentException(ExceptionTestCaseArgs args) { - var action = () => new NumberValidator(precision, scale, onlyPositive); + var action = () => new NumberValidator(args.Precision, args.Scale); action.Should().Throw(); } [TestCaseSource(nameof(NotThrowArgumentExceptionTestCases))] - public void Should_NotThrowArgumentException(int precision, int scale, bool onlyPositive) + public void Should_NotThrow_AnyException(ExceptionTestCaseArgs args) { - var action = () => new NumberValidator(precision, scale, onlyPositive); + var action = () => new NumberValidator(args.Precision, args.Scale); action.Should().NotThrow(); } } \ No newline at end of file From d96f24adef94f5180bfda6fa821c82eed2184c56 Mon Sep 17 00:00:00 2001 From: qreaqtor Date: Thu, 7 Nov 2024 00:53:26 +0500 Subject: [PATCH 4/5] refactoring part 2 --- .../NumberValidatorTestCases.cs | 89 ------------------- .../NumberValidatorTests.cs | 58 ++++++++---- 2 files changed, 42 insertions(+), 105 deletions(-) delete mode 100644 Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs deleted file mode 100644 index a581dee..0000000 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTestCases.cs +++ /dev/null @@ -1,89 +0,0 @@ -using NUnit.Framework; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HomeExercise.Tasks.NumberValidator -{ - using ValidateTestCaseArgs = (int Precision, int Scale, bool OnlyPositive, string Value); - - using ExceptionTestCaseArgs = (int Precision, int Scale); - - // для каждого теста может быть много тест кейсов, поэтому я вынес их в отдельные коллекции - public partial class NumberValidatorTests - { - - public static IEnumerable ValidTestCases - { - get - { - yield return (17, 1, true, "0.0"); - - yield return (17, 1, true, "0,0"); - - yield return (17, 1, true, "+1,0"); - - yield return (17, 2, true, "0"); - - yield return (4, 1, true, "1234"); - - yield return (4, 1, true, "+123"); - - yield return (4, 1, true, "+12.3"); - - yield return (7, 3, false, "+1.00"); - } - } - - public static IEnumerable NotValidTestCases - { - get - { - yield return (3, 2, true, "00.00"); - - yield return (3, 2, true, "-0.0"); - - yield return (3, 2, true, "+0.00"); - - yield return (3, 2, true, "a.sd"); - - yield return (17, 2, true, "0.000"); - - yield return (3, 2, true, ""); - - yield return (3, 2, true, null); - - yield return (4, 2, false, "10*00"); - - yield return (4, 1, false, "1.0.0"); - } - } - - public static IEnumerable ThrowArgumentExceptionTestCases - { - get - { - yield return (-1, 2); - - yield return (1, -2); - - yield return (1, 2); - - yield return (1, 1); - } - } - - public static IEnumerable NotThrowArgumentExceptionTestCases - { - get - { - yield return (1, 0); - - yield return (2, 1); - } - } - } -} diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index 252b936..ad80c3c 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -6,44 +6,70 @@ namespace HomeExercise.Tasks.NumberValidator; -using ValidateTestCaseArgs = (int Precision, int Scale, bool OnlyPositive, string Value); -using ExceptionTestCaseArgs = (int Precision, int Scale); [TestFixture] public partial class NumberValidatorTests { - [TestCaseSource(nameof(ValidTestCases))] - public void IsValidNumber_Shoud_BeTrue_WhenArgsCorrect(ValidateTestCaseArgs args) + + [TestCase(17, 1, true, "0.0", TestName = "Number with point separator")] + [TestCase(17, 1, true, "0,0", TestName = "Number with comma separator")] + [TestCase(17, 2, true, "0", TestName = "Number without fractionl part")] + [TestCase(4, 1, true, "1234", TestName = "Number length equals to precision")] + [TestCase(17, 1, true, "+1,0", TestName = "Positive number with comma")] + [TestCase(4, 1, true, "+123", TestName = "Positive number length with equals to precision")] + [TestCase(4, 1, true, "+12.3", TestName = "Positive number length with fractionl part equals to precision")] + [TestCase(5, 3, true, "+1.000", TestName = "Positive number length with fractional part equals to precision")] + [TestCase(17, 1, false, "-1,0", TestName = "Negative number with comma")] + [TestCase(4, 1, false, "-123", TestName = "Negative number length with plus equals to precision")] + [TestCase(4, 1, false, "-12.3", TestName = "Negative number length with frctionl part equals to precision")] + [TestCase(5, 3, false, "-1.000", TestName = "Negative number length with fractional part and plus equals to precision")] + + public void IsValidNumber_Shoud_BeTrue_WhenArgsCorrect(int precision, int scale, bool onlyPositive, string value) { - var validator = new NumberValidator(args.Precision, args.Scale, args.OnlyPositive); + var validator = new NumberValidator(precision, scale, onlyPositive); - var result = validator.IsValidNumber(args.Value); + var result = validator.IsValidNumber(value); result.Should().BeTrue(); } - [TestCaseSource(nameof(NotValidTestCases))] - public void IsValidNumber_Shoud_BeFalse_WhenArgsBad(ValidateTestCaseArgs args) + + [TestCase(3, 2, true, "00.00", TestName = "Number length with fractionl part more than precision")] + [TestCase(3, 2, true, "-0.0", TestName = "Negative number length with fractionl part more than precision")] + [TestCase(3, 2, true, "+0.00", TestName = "Positive number length with fractionl part more than precision")] + [TestCase(3, 2, true, "a.sd", TestName = "Number consists letters")] + [TestCase(17, 2, true, "0.000", TestName = "Length number's frationl part more thn scale")] + [TestCase(3, 2, true, "", TestName = "Number is empty string")] + [TestCase(3, 2, true, null, TestName = "Number is null")] + [TestCase(4, 2, false, "10*00", TestName = "Number with invalid separator")] + [TestCase(4, 1, false, "1.0.0", TestName = "Number with more than one separator")] + public void IsValidNumber_Shoud_BeFalse_WhenArgsBad(int precision, int scale, bool onlyPositive, string value) { - var validator = new NumberValidator(args.Precision, args.Scale, args.OnlyPositive); + var validator = new NumberValidator(precision, scale, onlyPositive); - var result = validator.IsValidNumber(args.Value); + var result = validator.IsValidNumber(value); result.Should().BeFalse(); } - [TestCaseSource(nameof(ThrowArgumentExceptionTestCases))] - public void Should_Throw_ArgumentException(ExceptionTestCaseArgs args) + + [TestCase(-1, 2, TestName = "Negative precision")] + [TestCase(1, -2, TestName = "Negative scale")] + [TestCase(1, 2, TestName = "Precision less than scale")] + [TestCase(1, 1, TestName = "Precision equals with scale")] + public void Should_Throw_ArgumentException(int precision, int scale) { - var action = () => new NumberValidator(args.Precision, args.Scale); + var action = () => new NumberValidator(precision, scale); action.Should().Throw(); } - [TestCaseSource(nameof(NotThrowArgumentExceptionTestCases))] - public void Should_NotThrow_AnyException(ExceptionTestCaseArgs args) + + [TestCase(1, 0, TestName = "Zero scale")] + [TestCase(2, 1, TestName = "Precision more than scale")] + public void Should_NotThrow_AnyException(int precision, int scale) { - var action = () => new NumberValidator(args.Precision, args.Scale); + var action = () => new NumberValidator(precision, scale); action.Should().NotThrow(); } } \ No newline at end of file From c06e23c2581684838577c32111731c12079557d4 Mon Sep 17 00:00:00 2001 From: qreaqtor Date: Thu, 7 Nov 2024 13:30:06 +0500 Subject: [PATCH 5/5] refactor naming --- .../2. NumberValidator/NumberValidatorTests.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs index ad80c3c..0f7a4f7 100644 --- a/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs +++ b/Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs @@ -35,9 +35,9 @@ public void IsValidNumber_Shoud_BeTrue_WhenArgsCorrect(int precision, int scale, } - [TestCase(3, 2, true, "00.00", TestName = "Number length with fractionl part more than precision")] - [TestCase(3, 2, true, "-0.0", TestName = "Negative number length with fractionl part more than precision")] - [TestCase(3, 2, true, "+0.00", TestName = "Positive number length with fractionl part more than precision")] + [TestCase(3, 2, true, "00.00", TestName = "Number length with fractional part more than precision")] + [TestCase(3, 2, true, "-0.0", TestName = "Number length with fractional part and sign more than precision")] + [TestCase(3, 2, true, "+0.00", TestName = "Number length with sign more than precision and fractional prt equls to scale")] [TestCase(3, 2, true, "a.sd", TestName = "Number consists letters")] [TestCase(17, 2, true, "0.000", TestName = "Length number's frationl part more thn scale")] [TestCase(3, 2, true, "", TestName = "Number is empty string")] @@ -58,7 +58,7 @@ public void IsValidNumber_Shoud_BeFalse_WhenArgsBad(int precision, int scale, bo [TestCase(1, -2, TestName = "Negative scale")] [TestCase(1, 2, TestName = "Precision less than scale")] [TestCase(1, 1, TestName = "Precision equals with scale")] - public void Should_Throw_ArgumentException(int precision, int scale) + public void Constructor_Should_Throw_ArgumentException(int precision, int scale) { var action = () => new NumberValidator(precision, scale); action.Should().Throw(); @@ -67,7 +67,7 @@ public void Should_Throw_ArgumentException(int precision, int scale) [TestCase(1, 0, TestName = "Zero scale")] [TestCase(2, 1, TestName = "Precision more than scale")] - public void Should_NotThrow_AnyException(int precision, int scale) + public void Constructor_Should_NotThrow_AnyException(int precision, int scale) { var action = () => new NumberValidator(precision, scale); action.Should().NotThrow();