From 9fda9638175c05bdf64da0ebdbecd7ce0cccd8ce Mon Sep 17 00:00:00 2001 From: Nikita_Trofimov Date: Tue, 28 Nov 2023 17:41:55 +0500 Subject: [PATCH 1/4] =?UTF-8?q?=D0=9E=D1=82=D1=80=D0=B5=D1=84=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D0=BB=20NumberValidator=20=D0=B8=20Obj?= =?UTF-8?q?ectComparsion,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= =?UTF-8?q?=20=D0=B2=20NumberValidator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cs/HomeExercises/NumberValidatorTests.cs | 71 +++++++++++++++--------- cs/HomeExercises/ObjectComparison.cs | 18 +++--- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index a2878113..94e89710 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -1,32 +1,59 @@ using System; +using System.Diagnostics.SymbolStore; using System.Text.RegularExpressions; using FluentAssertions; using NUnit.Framework; +using NUnit.Framework.Internal; namespace HomeExercises { public class NumberValidatorTests { [Test] - public void Test() + [TestCase(-1, 2, true, TestName = "Creation_ShouldThrowArgumentException_WhenNegativePrecision")] + [TestCase(0,0,true, TestName = "Creation_ShouldThrowArgumentException_WhenPrecisionIsZero")] + [TestCase(3,-1,true, TestName = "Creation_ShouldThrowArgumentException_WhenNegativeScale")] + [TestCase(1,2,true, TestName = "Creation_ShouldThrowArgumentException_WhenScaleIsGreaterThanPrecision")] + public void Creation_ShouldThrowArgumentException(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)); + Action creation = () => new NumberValidator(precision, scale, onlyPositive); + creation.Should().Throw(); + } + + [Test] + [TestCase(1, 0, true, TestName = "Creation_ShouldDoesNotThrow_WhenCorrectValue")] + public void Creation_ShouldDoesNotThrow(int precision, int scale, bool onlyPositive) + { + Action creation = () => new NumberValidator(precision, scale, onlyPositive); + creation.Should().NotThrow(); + } - Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); - Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0")); - Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("00.00")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-0.00")); - Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+0.00")); - Assert.IsTrue(new NumberValidator(4, 2, true).IsValidNumber("+1.23")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+1.23")); - Assert.IsFalse(new NumberValidator(17, 2, true).IsValidNumber("0.000")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-1.23")); - Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("a.sd")); + [Test] + [TestCase(3,2,true, "-+0.0", TestName = "IsValidNumber_ReturnsFalseWhenTwoSigns")] + [TestCase(3,2,true, " -0.0", TestName = "IsValidNumber_ReturnsFalseWhenSpaceFirstDigitInNumber")] + [TestCase(3,2,true, "0,", TestName ="IsValidNumber_ReturnsFalseWhenAren'tDigitsAfterCommas")] + [TestCase(3,2,true, "0.0.0", TestName = "IsValidNumber_ReturnsFalseWhenThreeDigitsInNumberByTwoDots")] + [TestCase(3,2,true, "0,0,0", TestName = "IsValidNumber_ReturnsFalseWhenThreeDigitsInNumberByTwoCommas")] + [TestCase(3, 2, true, "00.00", TestName = "IsValidNumber_ReturnsFalseWhenNumberPrecisionGreaterThanValidatorPrecision")] + [TestCase(3, 2, true, null, TestName = "IsValidNumber_ReturnsFalseWhenNumberNull")] + [TestCase(3, 2, true, "", TestName = "IsValidNumber_ReturnsFalseWhenNumberEmpty")] + [TestCase(3, 2, true, "+1.23", TestName = "IsValidNumber_ReturnsFalseWhenNumberPrecisionGreaterThanValidatorPrecision")] + [TestCase(3, 2, true, "-1.23", TestName = "IsValidNumber_ReturnsFalseWhenNumberNegativeInNumberOnlyPositive")] + [TestCase(17, 2, true, "0.000", TestName = "IsValidNumber_ReturnsFalseWhenNumberScaleGreaterThanValidatorScale")] + [TestCase(17, 2, true, "a.sd", TestName = "IsValidNumber_ReturnsFalseWhenIncorrectNumberFormatBecauseLettersArePresent")] + public void IsValidNumber_False(int precision, int scale, bool onlyPositive, string number) + { + NumberValidator validator = new NumberValidator(precision, scale, onlyPositive); + validator.IsValidNumber(number).Should().BeFalse(); + } + [Test] + [TestCase(17, 2, true, "0.0", TestName = "IsValidNumber_ReturnTrueWhenNumberWithFractionalPart")] + [TestCase(17, 2, true, "0", TestName = "IsValidNumber_ReturnTrueWhenOnlyInteger")] + [TestCase(4, 2, true, "+1.23", TestName = "IsValidNumber_ReturnTrueWhenNumberWithSign")] + public void IsValidNumber_True(int precision, int scale, bool onlyPositive, string number) + { + NumberValidator validator = new NumberValidator(precision, scale, onlyPositive); + validator.IsValidNumber(number).Should().BeTrue(); } } @@ -51,22 +78,14 @@ public NumberValidator(int precision, int scale = 0, bool onlyPositive = false) public bool IsValidNumber(string value) { - // Проверяем соответствие входного значения формату N(m,k), в соответствии с правилом, - // описанным в Формате описи документов, направляемых в налоговый орган в электронном виде по телекоммуникационным каналам связи: - // Формат числового значения указывается в виде N(m.к), где m – максимальное количество знаков в числе, включая знак (для отрицательного числа), - // целую и дробную часть числа без разделяющей десятичной точки, k – максимальное число знаков дробной части числа. - // Если число знаков дробной части числа равно 0 (т.е. число целое), то формат числового значения имеет вид N(m). - if (string.IsNullOrEmpty(value)) return false; var match = numberRegex.Match(value); if (!match.Success) return false; - - // Знак и целая часть + var intPart = match.Groups[1].Value.Length + match.Groups[2].Value.Length; - // Дробная часть var fracPart = match.Groups[4].Value.Length; if (intPart + fracPart > precision || fracPart > scale) diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index 44d9aed4..3d222757 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -15,16 +15,9 @@ 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. - Assert.AreEqual(actualTsar.Name, expectedTsar.Name); - Assert.AreEqual(actualTsar.Age, expectedTsar.Age); - Assert.AreEqual(actualTsar.Height, expectedTsar.Height); - Assert.AreEqual(actualTsar.Weight, expectedTsar.Weight); - - Assert.AreEqual(expectedTsar.Parent!.Name, actualTsar.Parent!.Name); - Assert.AreEqual(expectedTsar.Parent.Age, actualTsar.Parent.Age); - Assert.AreEqual(expectedTsar.Parent.Height, actualTsar.Parent.Height); - Assert.AreEqual(expectedTsar.Parent.Parent, actualTsar.Parent.Parent); + actualTsar.Should().BeEquivalentTo(expectedTsar, options => options + .Excluding(tsar => tsar.Parent) + .Excluding(tsar => tsar.Id)); } [Test] @@ -35,7 +28,10 @@ public void CheckCurrentTsar_WithCustomEquality() var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, new Person("Vasili III of Russia", 28, 170, 60, null)); - // Какие недостатки у такого подхода? + //Какие недостатки у такого подхода? + //Нужно будет переписывать AreEqual в случае изменения полей Person + //Возможна ошибка при написании метода + //Если тест упадет, сообщение об ошибке будет малоинформативным Assert.True(AreEqual(actualTsar, expectedTsar)); } From b1c745d1c6e874fdf3fd9ea8cb4e44c2b45e0852 Mon Sep 17 00:00:00 2001 From: Nikita_Trofimov Date: Thu, 30 Nov 2023 16:32:13 +0500 Subject: [PATCH 2/4] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=82?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=BE=D0=B2=20=D0=9F=D0=B5=D1=80=D0=B5=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=B0=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=20=D1=81=20?= =?UTF-8?q?=D0=BE=D0=B4=D0=BD=D0=B8=D0=BC=20testCase=20=D0=9F=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=BD=D1=91=D1=81=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20?= =?UTF-8?q?=D0=B2=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5?= =?UTF-8?q?=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=94=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=20=D0=B2=20Creati?= =?UTF-8?q?on=5FShouldThrowArgumentException=20=D0=A3=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=20Onl?= =?UTF-8?q?yPositive=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB=20=D1=82?= =?UTF-8?q?=D0=B5=D1=81=D1=82=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D1=86=D0=B0=D1=80=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cs/HomeExercises/NumberValidatorTests.cs | 55 +--------------- cs/HomeExercises/ObjectComparison.cs | 60 +----------------- .../tests/NumberValidator_Should.cs | 57 +++++++++++++++++ .../tests/ObjectComparsion_Should.cs | 62 +++++++++++++++++++ 4 files changed, 121 insertions(+), 113 deletions(-) create mode 100644 cs/HomeExercises/tests/NumberValidator_Should.cs create mode 100644 cs/HomeExercises/tests/ObjectComparsion_Should.cs diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index 94e89710..9d85112d 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -1,62 +1,9 @@ using System; -using System.Diagnostics.SymbolStore; using System.Text.RegularExpressions; -using FluentAssertions; -using NUnit.Framework; -using NUnit.Framework.Internal; + namespace HomeExercises { - public class NumberValidatorTests - { - [Test] - [TestCase(-1, 2, true, TestName = "Creation_ShouldThrowArgumentException_WhenNegativePrecision")] - [TestCase(0,0,true, TestName = "Creation_ShouldThrowArgumentException_WhenPrecisionIsZero")] - [TestCase(3,-1,true, TestName = "Creation_ShouldThrowArgumentException_WhenNegativeScale")] - [TestCase(1,2,true, TestName = "Creation_ShouldThrowArgumentException_WhenScaleIsGreaterThanPrecision")] - public void Creation_ShouldThrowArgumentException(int precision, int scale, bool onlyPositive) - { - Action creation = () => new NumberValidator(precision, scale, onlyPositive); - creation.Should().Throw(); - } - - [Test] - [TestCase(1, 0, true, TestName = "Creation_ShouldDoesNotThrow_WhenCorrectValue")] - public void Creation_ShouldDoesNotThrow(int precision, int scale, bool onlyPositive) - { - Action creation = () => new NumberValidator(precision, scale, onlyPositive); - creation.Should().NotThrow(); - } - - [Test] - [TestCase(3,2,true, "-+0.0", TestName = "IsValidNumber_ReturnsFalseWhenTwoSigns")] - [TestCase(3,2,true, " -0.0", TestName = "IsValidNumber_ReturnsFalseWhenSpaceFirstDigitInNumber")] - [TestCase(3,2,true, "0,", TestName ="IsValidNumber_ReturnsFalseWhenAren'tDigitsAfterCommas")] - [TestCase(3,2,true, "0.0.0", TestName = "IsValidNumber_ReturnsFalseWhenThreeDigitsInNumberByTwoDots")] - [TestCase(3,2,true, "0,0,0", TestName = "IsValidNumber_ReturnsFalseWhenThreeDigitsInNumberByTwoCommas")] - [TestCase(3, 2, true, "00.00", TestName = "IsValidNumber_ReturnsFalseWhenNumberPrecisionGreaterThanValidatorPrecision")] - [TestCase(3, 2, true, null, TestName = "IsValidNumber_ReturnsFalseWhenNumberNull")] - [TestCase(3, 2, true, "", TestName = "IsValidNumber_ReturnsFalseWhenNumberEmpty")] - [TestCase(3, 2, true, "+1.23", TestName = "IsValidNumber_ReturnsFalseWhenNumberPrecisionGreaterThanValidatorPrecision")] - [TestCase(3, 2, true, "-1.23", TestName = "IsValidNumber_ReturnsFalseWhenNumberNegativeInNumberOnlyPositive")] - [TestCase(17, 2, true, "0.000", TestName = "IsValidNumber_ReturnsFalseWhenNumberScaleGreaterThanValidatorScale")] - [TestCase(17, 2, true, "a.sd", TestName = "IsValidNumber_ReturnsFalseWhenIncorrectNumberFormatBecauseLettersArePresent")] - public void IsValidNumber_False(int precision, int scale, bool onlyPositive, string number) - { - NumberValidator validator = new NumberValidator(precision, scale, onlyPositive); - validator.IsValidNumber(number).Should().BeFalse(); - } - [Test] - [TestCase(17, 2, true, "0.0", TestName = "IsValidNumber_ReturnTrueWhenNumberWithFractionalPart")] - [TestCase(17, 2, true, "0", TestName = "IsValidNumber_ReturnTrueWhenOnlyInteger")] - [TestCase(4, 2, true, "+1.23", TestName = "IsValidNumber_ReturnTrueWhenNumberWithSign")] - public void IsValidNumber_True(int precision, int scale, bool onlyPositive, string number) - { - NumberValidator validator = new NumberValidator(precision, scale, onlyPositive); - validator.IsValidNumber(number).Should().BeTrue(); - } - } - public class NumberValidator { private readonly Regex numberRegex; diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index 3d222757..cfa47b35 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -1,63 +1,5 @@ -using FluentAssertions; -using NUnit.Framework; - -namespace HomeExercises +namespace HomeExercises { - public class ObjectComparison - { - [Test] - [Description("Проверка текущего царя")] - [Category("ToRefactor")] - 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)); - - actualTsar.Should().BeEquivalentTo(expectedTsar, options => options - .Excluding(tsar => tsar.Parent) - .Excluding(tsar => tsar.Id)); - } - - [Test] - [Description("Альтернативное решение. Какие у него недостатки?")] - 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)); - - //Какие недостатки у такого подхода? - //Нужно будет переписывать AreEqual в случае изменения полей Person - //Возможна ошибка при написании метода - //Если тест упадет, сообщение об ошибке будет малоинформативным - Assert.True(AreEqual(actualTsar, expectedTsar)); - } - - private bool AreEqual(Person? actual, Person? expected) - { - if (actual == expected) return true; - if (actual == null || expected == null) return false; - return - actual.Name == expected.Name - && actual.Age == expected.Age - && actual.Height == expected.Height - && actual.Weight == expected.Weight - && AreEqual(actual.Parent, expected.Parent); - } - } - - 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)); - } - } - public class Person { public static int IdCounter = 0; diff --git a/cs/HomeExercises/tests/NumberValidator_Should.cs b/cs/HomeExercises/tests/NumberValidator_Should.cs new file mode 100644 index 00000000..95535347 --- /dev/null +++ b/cs/HomeExercises/tests/NumberValidator_Should.cs @@ -0,0 +1,57 @@ +using FluentAssertions; +using NUnit.Framework; +using System; + +namespace HomeExercises.tests +{ + public class NumberValidator_Should + { + [TestCase(-1, 2, TestName = "NegativePrecision")] + [TestCase(0, 0, TestName = "PrecisionIsZero")] + [TestCase(3, -1, TestName = "NegativeScale")] + [TestCase(1, 2, TestName = "ScaleIsGreaterThanPrecision")] + [TestCase(1, 1, TestName = "PrecisionEqualsScale")] + public void Creation_ShouldThrowArgumentException(int precision, int scale) + { + Action creation = () => new NumberValidator(precision, scale); + creation.Should().Throw(); + } + + [Test] + public void Creation_ShouldNotThrow() + { + Action creation = () => new NumberValidator(1, 0); + creation + .Should() + .NotThrow(); + + } + + [TestCase(3, 2,"-+0.0", TestName = "TwoSigns")] + [TestCase(3, 2," -0.0", TestName = "SpaceFirstDigitInNumber")] + [TestCase(3, 2,"0,", TestName ="Aren'tDigitsAfterCommas")] + [TestCase(3, 2,"0.0.0", TestName = "ThreeDigitsInNumberByTwoDots")] + [TestCase(3, 2,"0,0,0", TestName = "ThreeDigitsInNumberByTwoCommas")] + [TestCase(3, 2, "00.00", TestName = "NumberPrecisionGreaterThanValidatorPrecision")] + [TestCase(3, 2, null, TestName = "NumberNull")] + [TestCase(3, 2, "", TestName = "NumberEmpty")] + [TestCase(3, 2, "+1.23", TestName = "NumberPrecisionGreaterThanValidatorPrecision")] + [TestCase(3, 2, "-1.23", TestName = "NumberNegativeInNumberOnlyPositive")] + [TestCase(17, 2, "0.000", TestName = "NumberScaleGreaterThanValidatorScale")] + [TestCase(17, 2, "a.sd", TestName = "IncorrectNumberFormatBecauseLettersArePresent")] + public void IsValidNumber_False(int precision, int scale, string number) + { + var validator = new NumberValidator(precision, scale); + validator.IsValidNumber(number).Should().BeFalse(); + } + + [TestCase(17, 2, "0.0", TestName = "NumberWithFractionalPart")] + [TestCase(17, 2, "0", TestName = "OnlyInteger")] + [TestCase(4, 2, "+1.23", TestName = "NumberWithSign")] + public void IsValidNumber_True(int precision, int scale, string number) + { + var validator = new NumberValidator(precision, scale); + validator.IsValidNumber(number).Should().BeTrue(); + } + } +} diff --git a/cs/HomeExercises/tests/ObjectComparsion_Should.cs b/cs/HomeExercises/tests/ObjectComparsion_Should.cs new file mode 100644 index 00000000..cf816535 --- /dev/null +++ b/cs/HomeExercises/tests/ObjectComparsion_Should.cs @@ -0,0 +1,62 @@ +using FluentAssertions; +using NUnit.Framework; + + +namespace HomeExercises.tests +{ + public class ObjectComparsion_Should + { + [Test] + [Description("Проверка текущего царя")] + [Category("ToRefactor")] + public void CheckValuesOfTheCurrentTsar() + { + 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)); + + actualTsar.Should().BeEquivalentTo(expectedTsar, options => options + .Excluding(tsar => + tsar.SelectedMemberInfo.Name == nameof(Person.Id) + && tsar.SelectedMemberInfo.DeclaringType == typeof(Person))); + } + + [Test] + [Description("Альтернативное решение. Какие у него недостатки?")] + 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)); + + //Какие недостатки у такого подхода? + //Нужно будет переписывать AreEqual в случае изменения полей Person + //Возможна ошибка при написании метода + //Если тест упадет, сообщение об ошибке будет малоинформативным + Assert.True(AreEqual(actualTsar, expectedTsar)); + } + + private bool AreEqual(Person? actual, Person? expected) + { + if (actual == expected) return true; + if (actual == null || expected == null) return false; + return + actual.Name == expected.Name + && actual.Age == expected.Age + && actual.Height == expected.Height + && actual.Weight == expected.Weight + && AreEqual(actual.Parent, expected.Parent); + } + } + + 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)); + } + } +} \ No newline at end of file From 6a3a718c85588bee5032f949b09ae7d1705be75f Mon Sep 17 00:00:00 2001 From: Nikita_Trofimov Date: Thu, 30 Nov 2023 20:01:26 +0500 Subject: [PATCH 3/4] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D1=82=D0=B5=D1=81=D1=82=D1=83=20=D1=81=20onlyPo?= =?UTF-8?q?sitive=20=3D=20true=20=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BB=20=D0=BD=D0=B5=D0=B9=D0=BC=D0=B8=D0=BD=D0=B3?= =?UTF-8?q?=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cs/Challenge/Challenge.csproj | 1 + cs/HomeExercises/HomeExercises.csproj | 3 +- ...erValidatorTests.cs => NumberValidator.cs} | 1 - .../{ObjectComparison.cs => Person.cs} | 0 .../tests/NumberValidator_Should.cs | 53 +++++++++---------- ...tComparsion_Should.cs => Person_Should.cs} | 3 +- cs/Samples/Samples.csproj | 1 - 7 files changed, 30 insertions(+), 32 deletions(-) rename cs/HomeExercises/{NumberValidatorTests.cs => NumberValidator.cs} (99%) rename cs/HomeExercises/{ObjectComparison.cs => Person.cs} (100%) rename cs/HomeExercises/tests/{ObjectComparsion_Should.cs => Person_Should.cs} (98%) diff --git a/cs/Challenge/Challenge.csproj b/cs/Challenge/Challenge.csproj index 0f41e066..a13acb1a 100644 --- a/cs/Challenge/Challenge.csproj +++ b/cs/Challenge/Challenge.csproj @@ -13,6 +13,7 @@ + diff --git a/cs/HomeExercises/HomeExercises.csproj b/cs/HomeExercises/HomeExercises.csproj index ede81aec..26a773ec 100644 --- a/cs/HomeExercises/HomeExercises.csproj +++ b/cs/HomeExercises/HomeExercises.csproj @@ -14,7 +14,8 @@ - + + \ No newline at end of file diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidator.cs similarity index 99% rename from cs/HomeExercises/NumberValidatorTests.cs rename to cs/HomeExercises/NumberValidator.cs index 9d85112d..b357db83 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidator.cs @@ -1,7 +1,6 @@ using System; using System.Text.RegularExpressions; - namespace HomeExercises { public class NumberValidator diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/Person.cs similarity index 100% rename from cs/HomeExercises/ObjectComparison.cs rename to cs/HomeExercises/Person.cs diff --git a/cs/HomeExercises/tests/NumberValidator_Should.cs b/cs/HomeExercises/tests/NumberValidator_Should.cs index 95535347..2a333492 100644 --- a/cs/HomeExercises/tests/NumberValidator_Should.cs +++ b/cs/HomeExercises/tests/NumberValidator_Should.cs @@ -6,17 +6,16 @@ namespace HomeExercises.tests { public class NumberValidator_Should { - [TestCase(-1, 2, TestName = "NegativePrecision")] - [TestCase(0, 0, TestName = "PrecisionIsZero")] - [TestCase(3, -1, TestName = "NegativeScale")] - [TestCase(1, 2, TestName = "ScaleIsGreaterThanPrecision")] - [TestCase(1, 1, TestName = "PrecisionEqualsScale")] - public void Creation_ShouldThrowArgumentException(int precision, int scale) + [TestCase(-1, 2, false, TestName = "NegativePrecision")] + [TestCase(0, 0, false, TestName = "PrecisionIsZero")] + [TestCase(3, -1, false, TestName = "NegativeScale")] + [TestCase(1, 2, false, TestName = "ScaleIsGreaterThanPrecision")] + [TestCase(1, 1, true, TestName = "PrecisionEqualsScale")] + public void Creation_ShouldThrowArgumentException(int precision, int scale, bool onlyPositive) { - Action creation = () => new NumberValidator(precision, scale); + Action creation = () => new NumberValidator(precision, scale, onlyPositive); creation.Should().Throw(); } - [Test] public void Creation_ShouldNotThrow() { @@ -27,28 +26,28 @@ public void Creation_ShouldNotThrow() } - [TestCase(3, 2,"-+0.0", TestName = "TwoSigns")] - [TestCase(3, 2," -0.0", TestName = "SpaceFirstDigitInNumber")] - [TestCase(3, 2,"0,", TestName ="Aren'tDigitsAfterCommas")] - [TestCase(3, 2,"0.0.0", TestName = "ThreeDigitsInNumberByTwoDots")] - [TestCase(3, 2,"0,0,0", TestName = "ThreeDigitsInNumberByTwoCommas")] - [TestCase(3, 2, "00.00", TestName = "NumberPrecisionGreaterThanValidatorPrecision")] - [TestCase(3, 2, null, TestName = "NumberNull")] - [TestCase(3, 2, "", TestName = "NumberEmpty")] - [TestCase(3, 2, "+1.23", TestName = "NumberPrecisionGreaterThanValidatorPrecision")] - [TestCase(3, 2, "-1.23", TestName = "NumberNegativeInNumberOnlyPositive")] - [TestCase(17, 2, "0.000", TestName = "NumberScaleGreaterThanValidatorScale")] - [TestCase(17, 2, "a.sd", TestName = "IncorrectNumberFormatBecauseLettersArePresent")] - public void IsValidNumber_False(int precision, int scale, string number) + [TestCase(3, 2, false, "-+0.0", TestName = "TwoSigns")] + [TestCase(3, 2, false, " -0.0", TestName = "SpaceFirstDigitInNumber")] + [TestCase(3, 2, false, "0,", TestName ="Aren'tDigitsAfterCommas")] + [TestCase(3, 2, false, "0.0.0", TestName = "ThreeDigitsInNumberByTwoDots")] + [TestCase(3, 2, false, "0,0,0", TestName = "ThreeDigitsInNumberByTwoCommas")] + [TestCase(3, 2, false, "00.00", TestName = "NumberPrecisionGreaterThanValidatorPrecision")] + [TestCase(3, 2, false, null, TestName = "NumberNull")] + [TestCase(3, 2, false, "", TestName = "NumberEmpty")] + [TestCase(3, 2, false, "+1.23", TestName = "NumberPrecisionGreaterThanValidatorPrecision")] + [TestCase(3, 2, false, "-1.23", TestName = "NumberNegativeInNumberOnlyPositive")] + [TestCase(17, 2, true, "0.000", TestName = "NumberScaleGreaterThanValidatorScale")] + [TestCase(17, 2, true, "a.sd", TestName = "IncorrectNumberFormatBecauseLettersArePresent")] + public void IsValidNumber_ShouldFalse_When(int precision, int scale, bool onlyPositive, string number) { - var validator = new NumberValidator(precision, scale); + var validator = new NumberValidator(precision, scale, onlyPositive); validator.IsValidNumber(number).Should().BeFalse(); } - - [TestCase(17, 2, "0.0", TestName = "NumberWithFractionalPart")] - [TestCase(17, 2, "0", TestName = "OnlyInteger")] - [TestCase(4, 2, "+1.23", TestName = "NumberWithSign")] - public void IsValidNumber_True(int precision, int scale, string number) + + [TestCase(17, 2, false, "0.0", TestName = "NumberWithFractionalPart")] + [TestCase(17, 2, false, "0", TestName = "OnlyInteger")] + [TestCase(4, 2, true, "+1.23", TestName = "NumberWithSign")] + public void IsValidNumber_ShouldTrue_When(int precision, int scale, bool onlyPositive, string number) { var validator = new NumberValidator(precision, scale); validator.IsValidNumber(number).Should().BeTrue(); diff --git a/cs/HomeExercises/tests/ObjectComparsion_Should.cs b/cs/HomeExercises/tests/Person_Should.cs similarity index 98% rename from cs/HomeExercises/tests/ObjectComparsion_Should.cs rename to cs/HomeExercises/tests/Person_Should.cs index cf816535..a4b8ca31 100644 --- a/cs/HomeExercises/tests/ObjectComparsion_Should.cs +++ b/cs/HomeExercises/tests/Person_Should.cs @@ -1,10 +1,9 @@ using FluentAssertions; using NUnit.Framework; - namespace HomeExercises.tests { - public class ObjectComparsion_Should + public class Person_Should { [Test] [Description("Проверка текущего царя")] diff --git a/cs/Samples/Samples.csproj b/cs/Samples/Samples.csproj index c2c8798a..dc0b5716 100644 --- a/cs/Samples/Samples.csproj +++ b/cs/Samples/Samples.csproj @@ -12,7 +12,6 @@ - From 6cbad54017be8d2f95f2c6ac73520238a1276e5d Mon Sep 17 00:00:00 2001 From: Nikita_Trofimov Date: Thu, 30 Nov 2023 20:08:33 +0500 Subject: [PATCH 4/4] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=B1=D0=B5=D0=B7=20=D0=BE=D0=BF=D0=B5=D1=87?= =?UTF-8?q?=D0=B0=D1=82=D0=BA=D0=B8=20(ObjectComparison)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{Person.cs => ObjectComparison.cs} | 6 ++--- ...n_Should.cs => ObjectComparison_Should.cs} | 24 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) rename cs/HomeExercises/{Person.cs => ObjectComparison.cs} (61%) rename cs/HomeExercises/tests/{Person_Should.cs => ObjectComparison_Should.cs} (64%) diff --git a/cs/HomeExercises/Person.cs b/cs/HomeExercises/ObjectComparison.cs similarity index 61% rename from cs/HomeExercises/Person.cs rename to cs/HomeExercises/ObjectComparison.cs index cfa47b35..804fee89 100644 --- a/cs/HomeExercises/Person.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -1,14 +1,14 @@ namespace HomeExercises { - public class Person + public class ObjectComparison { public static int IdCounter = 0; public int Age, Height, Weight; public string Name; - public Person? Parent; + public ObjectComparison? Parent; public int Id; - public Person(string name, int age, int height, int weight, Person? parent) + public ObjectComparison(string name, int age, int height, int weight, ObjectComparison? parent) { Id = IdCounter++; Name = name; diff --git a/cs/HomeExercises/tests/Person_Should.cs b/cs/HomeExercises/tests/ObjectComparison_Should.cs similarity index 64% rename from cs/HomeExercises/tests/Person_Should.cs rename to cs/HomeExercises/tests/ObjectComparison_Should.cs index a4b8ca31..89509860 100644 --- a/cs/HomeExercises/tests/Person_Should.cs +++ b/cs/HomeExercises/tests/ObjectComparison_Should.cs @@ -3,7 +3,7 @@ namespace HomeExercises.tests { - public class Person_Should + public class ObjectComparison_Should { [Test] [Description("Проверка текущего царя")] @@ -12,13 +12,13 @@ public void CheckValuesOfTheCurrentTsar() { 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 ObjectComparison("Ivan IV The Terrible", 54, 170, 70, + new ObjectComparison("Vasili III of Russia", 28, 170, 60, null)); actualTsar.Should().BeEquivalentTo(expectedTsar, options => options .Excluding(tsar => - tsar.SelectedMemberInfo.Name == nameof(Person.Id) - && tsar.SelectedMemberInfo.DeclaringType == typeof(Person))); + tsar.SelectedMemberInfo.Name == nameof(ObjectComparison.Id) + && tsar.SelectedMemberInfo.DeclaringType == typeof(ObjectComparison))); } [Test] @@ -26,17 +26,17 @@ public void CheckValuesOfTheCurrentTsar() 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 ObjectComparison("Ivan IV The Terrible", 54, 170, 70, + new ObjectComparison("Vasili III of Russia", 28, 170, 60, null)); //Какие недостатки у такого подхода? - //Нужно будет переписывать AreEqual в случае изменения полей Person + //Нужно будет переписывать AreEqual в случае изменения полей ObjectComparison //Возможна ошибка при написании метода //Если тест упадет, сообщение об ошибке будет малоинформативным Assert.True(AreEqual(actualTsar, expectedTsar)); } - private bool AreEqual(Person? actual, Person? expected) + private bool AreEqual(ObjectComparison? actual, ObjectComparison? expected) { if (actual == expected) return true; if (actual == null || expected == null) return false; @@ -51,11 +51,11 @@ private bool AreEqual(Person? actual, Person? expected) public class TsarRegistry { - public static Person GetCurrentTsar() + public static ObjectComparison GetCurrentTsar() { - return new Person( + return new ObjectComparison( "Ivan IV The Terrible", 54, 170, 70, - new Person("Vasili III of Russia", 28, 170, 60, null)); + new ObjectComparison("Vasili III of Russia", 28, 170, 60, null)); } } } \ No newline at end of file