From d32d4db52f007c0017e616ab8822349a4cf95c21 Mon Sep 17 00:00:00 2001 From: m1nus0ne Date: Tue, 5 Nov 2024 19:10:53 +0500 Subject: [PATCH 1/2] 1st attempt --- cs/HomeExercises/NumberValidatorTests.cs | 61 +++++++++++++++++------- cs/HomeExercises/ObjectComparison.cs | 12 ++--- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index a2878113..d352792d 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -7,27 +7,52 @@ namespace HomeExercises { public class NumberValidatorTests { - [Test] - public void Test() + [TestCaseSource(nameof(ValidationExceptionCases))] + public void ValidationExceptionTest(int precision, int scale, bool onlyPositive, bool shouldThrow) { - 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 action = () => new NumberValidator(precision, scale, onlyPositive); + // action.Should().Throw().When(shouldThrow); When не доступно в старой версии либы( + Action action = () => new NumberValidator(precision, scale, onlyPositive); + if (shouldThrow) + { + action.Should().Throw(); + } + else + { + action.Should().NotThrow(); + } + + } + public static object[] ValidationExceptionCases = + { + new object[] { 1, 0, true, false }, + new object[] { -1, 2, false, true }, + new object[] { 2, 3, true, true } + }; - 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")); + [TestCaseSource(nameof(CorrectValidationCases))] + public void CorrectValidationTest(int precision, int scale, bool onlyPositive, string inputCase, bool expected) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + var isValid = validator.IsValidNumber(inputCase); + isValid.Should().Be(expected); } + + public static object[] CorrectValidationCases = + { + new object[] { 17, 2, true, "0", true }, + new object[] { 3, 2, true, "00.00", false }, + new object[] { 3, 2, true, "-0.00", false }, + new object[] { 17, 2, true, "0.0", true }, + new object[] { 3, 2, true, "+0.00", false }, + new object[] { 4, 2, true, "+1.23", true }, + new object[] { 3, 2, true, "+1.23", false }, + new object[] { 17, 2, true, "0.000", false }, + new object[] { 3, 2, true, "-1.23", false }, + new object[] { 3, 2, true, "a.sd", false }, + new object[] { 1, 0, true, "0", true }, + new object[] { 1, 0, false, "-0", false }, + }; } public class NumberValidator diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index 44d9aed4..918ed2fb 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -16,15 +16,8 @@ public void CheckCurrentTsar() 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(person => person.Id).Excluding(person => person.Parent!.Id)); } [Test] @@ -36,6 +29,7 @@ public void CheckCurrentTsar_WithCustomEquality() new Person("Vasili III of Russia", 28, 170, 60, null)); // Какие недостатки у такого подхода? + //Проблема с читаемостью, необходимость обновлять метод AreEqual при изменении сигнатуры Person Assert.True(AreEqual(actualTsar, expectedTsar)); } From 724c925bf0ab1d2de8e820422d76c3939e031297 Mon Sep 17 00:00:00 2001 From: m1nus0ne Date: Wed, 6 Nov 2024 11:59:41 +0500 Subject: [PATCH 2/2] 2nd attempt --- cs/HomeExercises/NumberValidatorTests.cs | 94 ++++++++++++++---------- cs/HomeExercises/ObjectComparison.cs | 11 ++- 2 files changed, 65 insertions(+), 40 deletions(-) diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index d352792d..12f6cf53 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -1,58 +1,78 @@ using System; using System.Text.RegularExpressions; using FluentAssertions; +using FluentAssertions.Primitives; using NUnit.Framework; namespace HomeExercises { public class NumberValidatorTests { - [TestCaseSource(nameof(ValidationExceptionCases))] - public void ValidationExceptionTest(int precision, int scale, bool onlyPositive, bool shouldThrow) + [TestCase(2, 3, true)] + [TestCase(-1, 2, false)] + public void Constructor_IncorrectArguments_ShouldThrowArgumentException(int precision, int scale, + bool onlyPositive) { - // Action action = () => new NumberValidator(precision, scale, onlyPositive); - // action.Should().Throw().When(shouldThrow); When не доступно в старой версии либы( Action action = () => new NumberValidator(precision, scale, onlyPositive); - if (shouldThrow) - { - action.Should().Throw(); - } - else - { - action.Should().NotThrow(); - } - + action.Should().Throw(); } - public static object[] ValidationExceptionCases = + + [TestCase(1, 0, true)] + public void Constructor_CorrectArguments_ShouldNotThrowArgumentException(int precision, int scale, + bool onlyPositive) { - new object[] { 1, 0, true, false }, - new object[] { -1, 2, false, true }, - new object[] { 2, 3, true, true } - }; + Action action = () => new NumberValidator(precision, scale, onlyPositive); + action.Should().NotThrow(); + } - [TestCaseSource(nameof(CorrectValidationCases))] - public void CorrectValidationTest(int precision, int scale, bool onlyPositive, string inputCase, bool expected) + [TestCase(3, 2, true, "a.sd")] + [TestCase(3, 2, true, "0+1.0.1")] + public void IsValidNumber_WrongInputType_ShouldReturnFalse(int precision, int scale, bool onlyPositive, + string input) { var validator = new NumberValidator(precision, scale, onlyPositive); - var isValid = validator.IsValidNumber(inputCase); - isValid.Should().Be(expected); + validator.IsValidNumber(input).Should().BeFalse(); } - - public static object[] CorrectValidationCases = + + [TestCase(3, 2, true, "00.00")] + public void IsValidNumber_WrongPrecision_ShouldReturnFalse(int precision, int scale, bool onlyPositive, + string input) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + validator.IsValidNumber(input).Should().BeFalse(); + } + + [TestCase(17, 1, true, "00.00")] + public void IsValidNumber_WrongScale_ShouldReturnFalse(int precision, int scale, bool onlyPositive, + string input) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + validator.IsValidNumber(input).Should().BeFalse(); + } + + [TestCase(4, 3, true, " ")] + public void IsValidNumber_NullOrWhitespaces_ShouldReturnFalse(int precision, int scale, bool onlyPositive, + string input) { - new object[] { 17, 2, true, "0", true }, - new object[] { 3, 2, true, "00.00", false }, - new object[] { 3, 2, true, "-0.00", false }, - new object[] { 17, 2, true, "0.0", true }, - new object[] { 3, 2, true, "+0.00", false }, - new object[] { 4, 2, true, "+1.23", true }, - new object[] { 3, 2, true, "+1.23", false }, - new object[] { 17, 2, true, "0.000", false }, - new object[] { 3, 2, true, "-1.23", false }, - new object[] { 3, 2, true, "a.sd", false }, - new object[] { 1, 0, true, "0", true }, - new object[] { 1, 0, false, "-0", false }, - }; + var validator = new NumberValidator(precision, scale, onlyPositive); + validator.IsValidNumber(input).Should().BeFalse(); + } + + [TestCase(1, 0, false, "-0")] + public void IsValidNumber_NegativeNumberWhenNotAllowed_ShouldReturnFalse(int precision, int scale, bool onlyPositive, + string input) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + validator.IsValidNumber(input).Should().BeFalse(); + } + + [TestCase(17, 2, true, "0")] + public void IsValidNumber_CorrectInput_ShouldReturnTrue(int precision, int scale, bool onlyPositive, + string input) + { + var validator = new NumberValidator(precision, scale, onlyPositive); + validator.IsValidNumber(input).Should().BeTrue(); + } } public class NumberValidator diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index 918ed2fb..307637c0 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -1,4 +1,7 @@ -using FluentAssertions; +using System; +using System.Linq; +using System.Runtime.Serialization.Formatters; +using FluentAssertions; using NUnit.Framework; namespace HomeExercises @@ -16,8 +19,10 @@ public void CheckCurrentTsar() new Person("Vasili III of Russia", 28, 170, 60, null)); // Перепишите код на использование Fluent Assertions. - actualTsar.Should().BeEquivalentTo(expectedTsar, - options => options.Excluding(person => person.Id).Excluding(person => person.Parent!.Id)); + actualTsar.Should().BeEquivalentTo(expectedTsar, options => options + .Excluding(predicate: info => + info.SelectedMemberInfo.Name == nameof(Person.Id) + )); } [Test]