Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Шестопалов Андрей #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NUnit.Framework;
using NUnit.Framework.Legacy;
using FluentAssertions;

namespace HomeExercise.Tasks.ObjectComparison;
public class ObjectComparison
Expand All @@ -14,16 +15,10 @@ 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(x => x.Path.EndsWith("Id"))
.AllowingInfiniteRecursion());
}

[Test]
Expand All @@ -35,6 +30,12 @@ public void CheckCurrentTsar_WithCustomEquality()
new Person("Vasili III of Russia", 28, 170, 60, null));

// Какие недостатки у такого подхода?
/*
* 1) Отсутствие информации о причинах падения теста (В моей проверке, выводятся различающиеся свойства).
*
* 2) При изменении свойств класса Person, необходимо дописывать проверку новых свойств (В моей проверке, сравниваются все public свойства
* и дописывать изменения в тест необходимо надо будет лишь в том случае, если появилось новое свойство, которое разное у всех объектов).
*/
ClassicAssert.True(AreEqual(actualTsar, expectedTsar));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public NumberValidator(int precision, int scale = 0, bool onlyPositive = false)
if (precision <= 0)
throw new ArgumentException("precision must be a positive number");
if (scale < 0 || scale >= precision)
throw new ArgumentException("precision must be a non-negative number less or equal than precision");
throw new ArgumentException("scale must be a non-negative number less or equal than precision");
numberRegex = new Regex(@"^([+-]?)(\d+)([.,](\d+))?$", RegexOptions.IgnoreCase);
}

Expand Down
79 changes: 60 additions & 19 deletions Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,72 @@

using NUnit.Framework;
using NUnit.Framework.Legacy;
using FluentAssertions;

namespace HomeExercise.Tasks.NumberValidator;

[TestFixture]
public class NumberValidatorTests
{
[Test]
public void Test()
[TestCase(0,0, TestName = "PrecisionIsZero")]
[TestCase(-1, 0, TestName = "PrecisionIsNegative")]
[TestCase(1, -1, TestName = "ScaleIsNegative")]
[TestCase(5, 5, TestName = "ScaleEqualsPrecision")]
[TestCase(1, 2, TestName = "ScaleIsGreaterThanPrecision")]
public void Constructor_ThrowException_When(int precision, int scale)
{
Assert.Throws<ArgumentException>(() => new NumberValidator(-1, 2, true));
Assert.DoesNotThrow(() => new NumberValidator(1, 0, true));
Assert.Throws<ArgumentException>(() => new NumberValidator(-1, 2, false));
Assert.DoesNotThrow(() => new NumberValidator(1, 0, true));
var createValidator = () => new NumberValidator(precision, scale);

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"));
createValidator.Should().Throw<ArgumentException>();
}

[TestCase(1, 0, TestName = "PrecisionIsGreaterThanScaleAndScaleIsZero")]
[TestCase(5, 2, TestName = "PrecisionGreaterThanScaleAndScaleGreaterThanZero")]
public void Constructor_NotThrowException_When(int precision, int scale)
{
var createValidator = () => new NumberValidator(precision, scale);

createValidator.Should().NotThrow();
}

[TestCase(1, 0, false, "0", true, TestName = "IntegerWithoutSignAndLengthEqualsPrecision")]
[TestCase(5, 0, false, "40", true, TestName = "IntegerWithoutSignAndLengthLessThanPrecision")]
[TestCase(2, 0, false, "+0", true, TestName = "PositiveZeroWithSign")]
[TestCase(2, 0, false, "-0", true, TestName = "NegativeZeroWithSign")]
[TestCase(3, 0, false, "+21", true, TestName = "IntegerWithSignAndLengthEqualsPrecision")]
[TestCase(5, 0, false, "-40", true, TestName = "IntegerWithSignAndLengthLessThanPrecision")]
[TestCase(3, 1, false, "0", true, TestName = "IntegerWithoutSignAndScaleIsGreaterThanZero")]
[TestCase(5, 2, false, "+21", true, TestName = "IntegerWithSignAndScaleIsGreaterThanZero")]
[TestCase(5, 2, false,"123.45", true, TestName = "DecimalWithoutSignAndFractionalPartLengthEqualsScale")]
[TestCase(7, 4, false, "537.23", true, TestName = "DecimalWithoutSignAndFractionalPartLengthLessThanScale")]
[TestCase(3, 2, false, "0.0", true, TestName = "DecimalZeroWithinPrecisionAndScale")]
[TestCase(6, 2, false, "+237.89", true, TestName = "DecimalWithSignAndFractionalPartLengthEqualsScale")]
[TestCase(7, 4, false, "-445.12", true, TestName = "DecimalWithoutSignAndFractionalPartLengthLessThanScale")]
[TestCase(5, 2, false, "371,47", true, TestName = "DecimalWithoutSignWithCommaSeparator")]
[TestCase(6, 2, false, "+111,11", true, TestName = "DecimalWithSignWithCommaSeparator")]
[TestCase(1, 0, true, "11", false, TestName = "IntegerWithoutSignExceedsPrecision")]
[TestCase(3, 1, true, "112.3", false, TestName = "DecimalWithoutSignExceedsPrecision")]
[TestCase(2, 0, true, "+42", false, TestName = "IntegerWithSignExceedsPrecision")]
[TestCase(4, 2, true, "+42.75", false, TestName = "DecimalWithSignExceedsPrecision")]
[TestCase(2, 0, true, "5.0", false, TestName = "DecimalProvidedAndScaleIsZero")]
[TestCase(5, 0, true, "1.23", false, TestName = "DecimalProvidedAndScaleIsZero")]
[TestCase(3, 1, true, "5.67", false, TestName = "DecimalExceedsScale")]
[TestCase(7, 3, true, "-0", false, TestName = "NegativeZeroAndIsOnlyPositiveIsTrue")]
[TestCase(7, 3, true, "-10", false, TestName = "NegativeIntegerAndIsOnlyPositiveIsTrue")]
[TestCase(7, 3, true, "-1.23", false, TestName = "NegativeDecimalAndIsOnlyPositiveIsTrue")]
[TestCase(5, 2, false, null, false, TestName = "ValueIsNull")]
[TestCase(5, 2, false, "", false, TestName = "ValueIsEmpty")]
[TestCase(5, 2, false, " ", false, TestName = "ValueIsWhitespace")]
[TestCase(5, 2, false, "\t", false, TestName = "ValueIsEscapeSequences")]
[TestCase(5, 2, false, "a.sd", false, TestName = "ValueContainsLetters")]
[TestCase(5, 2, false, "10O", false, TestName = "ValueContainsLetters")]
[TestCase(7, 2, false, "12_34", false, TestName = "ValueContainsIncorrectSeparator")]
[TestCase(5, 0, false, "871.", false, TestName = "DecimalWithoutFractionAndScaleZero")]
[TestCase(5, 2, false, "636.", false, TestName = "DecimalWithoutFractionAndScaleIsPositive")]
public void IsValidNumber_ReturnExpectedResult_When(int precision, int scale, bool onlyPositive, string valueForValidation, bool expectedResult)
{
var validator = new NumberValidator(precision, scale, onlyPositive);
var isValid = validator.IsValidNumber(valueForValidation);

isValid.Should().Be(expectedResult);
}
}