diff --git a/cs/HomeExercises/NumberValidatorTests.cs b/cs/HomeExercises/NumberValidatorTests.cs index a2878113..b437661e 100644 --- a/cs/HomeExercises/NumberValidatorTests.cs +++ b/cs/HomeExercises/NumberValidatorTests.cs @@ -5,28 +5,146 @@ namespace HomeExercises { + [TestFixture] public class NumberValidatorTests { [Test] - public void Test() + public void Constructor_ShouldThrow_WhenPrecisionNegative() { - 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)); - - 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")); + Action act = () => new NumberValidator(-1); + act.Should() + .Throw() + .WithMessage("precision must be a positive number"); + } + + [Test] + public void Constructor_ShouldThrow_WhenPrecisionIsZero() + { + Action act = () => new NumberValidator(0); + act.Should() + .Throw() + .WithMessage("precision must be a positive number"); + } + + [Test] + public void Constructor_ShouldThrow_WhenScaleNegative() + { + Action act = () => new NumberValidator(3, -1); + act.Should() + .Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + + [Test] + public void Constructor_ShouldThrow_WhenScaleEqualPrecision() + { + Action act = () => new NumberValidator(3, 3); + act.Should() + .Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + + [Test] + public void Constructor_ShouldThrow_WhenScaleGreaterPrecision() + { + Action act = () => new NumberValidator(3, 4); + act.Should() + .Throw() + .WithMessage("precision must be a non-negative number less or equal than precision"); + } + + [Test] + public void Constructor_ShouldNotThrow_WhenScaleIsZero() + { + Action act = () => new NumberValidator(3, 0); + act.Should().NotThrow(); + } + + + [TestCase("1.2")] + [TestCase("1,2")] + public void IsValidNumber_ShouldBeTrue_WhenValueWithDifferentSeparators(string value) + { + var validator = new NumberValidator(4, 2, true); + + var result = validator.IsValidNumber(value); + + result.Should().BeTrue(); + } + + [TestCase("1`4")] + [TestCase("1'4")] + [TestCase("1_4")] + [TestCase("1 000")] + public void IsValidNumber_ShouldBeFalse_WhenValueWithUnsupportedSeparators(string value) + { + var validator = new NumberValidator(10, 2, true); + + var result = validator.IsValidNumber(value); + + result.Should().BeFalse(); + } + + [TestCase("-0")] + [TestCase("-1")] + public void IsValidNumber_ShouldBeFalse_WhenOnlyPositive(string value) + { + var validator = new NumberValidator(4, 2, true); + + var result = validator.IsValidNumber(value); + + result.Should().BeFalse(); + } + + [TestCase("-0")] + [TestCase("-1")] + public void IsValidNumber_ShouldBeTrue_WhenNumberIsNegative(string value) + { + var validator = new NumberValidator(4, 2); + + var result = validator.IsValidNumber(value); + + result.Should().BeTrue(); + } + + [TestCase(null)] + [TestCase("")] + public void IsValidNumber_ShouldBeFalse_WhenValueIsNullOrEmpty(string value) + { + var validator = new NumberValidator(10, 2, true); + + var result = validator.IsValidNumber(value); + + result.Should().BeFalse(); + } + + [TestCase("1.")] + [TestCase(".1")] + [TestCase("+-0")] + [TestCase("@#$1")] + [TestCase("\n1")] + [TestCase("abc")] + [TestCase("a4")] + public void IsValidNumber_ShouldBeFalse_WhenInvalidFormat(string value) + { + var validator = new NumberValidator(10, 2); + + var result = validator.IsValidNumber(value); + + result.Should().BeFalse(); + } + + [TestCase("1111")] + [TestCase("-111")] + [TestCase("-10.0")] + [TestCase("0.00")] + public void IsValidNumber_ShouldBeFalse_WhenValueTooLong(string value) + { + var validator = new NumberValidator(3, 1); + + var result = validator.IsValidNumber(value); + + result.Should().BeFalse(); } } diff --git a/cs/HomeExercises/ObjectComparison.cs b/cs/HomeExercises/ObjectComparison.cs index 44d9aed4..47b4bf41 100644 --- a/cs/HomeExercises/ObjectComparison.cs +++ b/cs/HomeExercises/ObjectComparison.cs @@ -10,21 +10,18 @@ public class ObjectComparison [Category("ToRefactor")] public void CheckCurrentTsar() { + // Это решение более читаемо за счет FluentAPI. + // При неудачном тестировании в отличии от CheckCurrentTsar_WithCustomEquality предоставляет подробную информацию. + // При изменении Person потребуется только немного изменить expectedTsar. + + 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. - 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); + expectedTsar.Should().BeEquivalentTo(actualTsar, + options => options.Excluding(p => p.SelectedMemberInfo.Name == nameof(Person.Id))); } [Test] @@ -35,7 +32,11 @@ 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 потребует изменений в методе AreEqual Assert.True(AreEqual(actualTsar, expectedTsar)); }