-
Notifications
You must be signed in to change notification settings - Fork 32
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
Леонтьев Дмитрий #19
base: master
Are you sure you want to change the base?
Леонтьев Дмитрий #19
Changes from 6 commits
5e91213
321e64c
138ba86
25512ae
a494c2c
c25ecda
383e965
dfebc84
4ab39c2
b71dc00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,27 @@ | ||
using NUnit.Framework; | ||
using NUnit.Framework.Legacy; | ||
using FluentAssertions; | ||
|
||
namespace HomeExercise.Tasks.ObjectComparison; | ||
|
||
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)); | ||
|
||
// Перепишите код на использование 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 | ||
.AllowingInfiniteRecursion() | ||
.Excluding(o => o.Id) | ||
.Excluding(o => o.Parent.Id)); | ||
} | ||
|
||
[Test] | ||
|
@@ -35,6 +33,12 @@ public void CheckCurrentTsar_WithCustomEquality() | |
new Person("Vasili III of Russia", 28, 170, 60, null)); | ||
|
||
// Какие недостатки у такого подхода? | ||
// При изменении класса Person придётся изменять метод AreEqual, | ||
// т.е. если мы добавим поле в Person, то придётся добавить проверку на него в этом методе. | ||
// При провале теста мы не получаем конкретной информации из-за чего тест не прошёл, | ||
// т.к. метод AreEqual возвращает bool и скажет нам только Success или Failed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ага, все так There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resolved |
||
// Невозможно сравнить двух Person без определённых полей, | ||
// придётся создавать различные реализации метода AreEqual. | ||
ClassicAssert.True(AreEqual(actualTsar, expectedTsar)); | ||
} | ||
|
||
|
@@ -49,4 +53,4 @@ private bool AreEqual(Person? actual, Person? expected) | |
&& actual.Weight == expected.Weight | ||
&& AreEqual(actual.Parent, expected.Parent); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,65 @@ | ||
| ||
using NUnit.Framework; | ||
using NUnit.Framework.Legacy; | ||
using NUnit.Framework; | ||
using FluentAssertions; | ||
|
||
namespace HomeExercise.Tasks.NumberValidator; | ||
|
||
[TestFixture] | ||
public class NumberValidatorTests | ||
{ | ||
[Test] | ||
public void Test() | ||
[TestCase(1, TestName = "precision 1, scale 0")] | ||
public void NumberValidator_CorrectParameters_AfterCreatingCorrectValidator(int precision, int scale = 0) | ||
{ | ||
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)); | ||
|
||
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")); | ||
var creation = () => new NumberValidator(precision, scale, true); | ||
|
||
creation.Should() | ||
.NotThrow<ArgumentException>(); | ||
} | ||
|
||
[TestCase(-1, TestName = "negative precision")] | ||
[TestCase(0, TestName = "zero precision")] | ||
public void NumberValidator_IncorrectPrecision_AfterCreatingIncorrectValidator(int precision, int scale = 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У тестов на конструктор имена не очень удались. Лучше что-нибудь типа There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resolved |
||
{ | ||
var creation = () => new NumberValidator(precision, scale); | ||
|
||
creation.Should() | ||
.Throw<ArgumentException>() | ||
.WithMessage("precision must be a positive number"); | ||
} | ||
|
||
[TestCase(1, -1, TestName = "negative scale")] | ||
[TestCase(1, 2, TestName = "scale greater than precision")] | ||
[TestCase(1, 1, TestName = "scale equal to precision")] | ||
public void NumberValidator_IncorrectScale_AfterCreatingIncorrectValidator(int precision, int scale) | ||
{ | ||
var creation = () => new NumberValidator(precision, scale); | ||
|
||
creation.Should() | ||
.Throw<ArgumentException>() | ||
.WithMessage("scale must be a non-negative number less than precision"); | ||
} | ||
|
||
[TestCase(1, 0, true, "0", ExpectedResult = true, TestName = "precision 1, scale 0, only positive, result 0")] | ||
[TestCase(2, 1, true, "0.1", ExpectedResult = true, TestName = "precision 2, scale 1, only positive, result 0.1")] | ||
[TestCase(2, 1, true, "0,1", ExpectedResult = true, TestName = "precision 2, scale 1, only positive, result 0,1")] | ||
[TestCase(2, 0, false, "-1", ExpectedResult = true, TestName = "precision 2, scale 0, not only positive, result -1")] | ||
[TestCase(3, 1, false, "-1.1", ExpectedResult = true, TestName = "precision 3, scale 1, not only positive, result -1.1")] | ||
[TestCase(3, 1, false, "-1,1", ExpectedResult = true, TestName = "precision 3, scale 1, not only positive, result -1,1")] | ||
[TestCase(2, 0, true, "+1", ExpectedResult = true, TestName = "precision 2, scale 0, only positive, result +1")] | ||
[TestCase(3, 1, true, "+1.1", ExpectedResult = true, TestName = "precision 3, scale 1, only positive, result +1.1")] | ||
[TestCase(3, 1, true, "+1,1", ExpectedResult = true, TestName = "precision 3, scale 1, only positive, result +1,1")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я бы предложил сгруппировать тест-кейсы по категориям и отобразить это на уровне методов (разные методы сделать для разных проверок) и на уровне названий (TestName отражает суть проверки, а не перечисляет входные параметры). У тебя так сделано для тестов на конструктор. Так будет более структурировано, и читать такие тесты будет гораздо легче 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resolved |
||
|
||
[TestCase(2, 1, true, "", ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not empty string")] | ||
[TestCase(2, 1, true, null, ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not null")] | ||
[TestCase(2, 1, true, ".0", ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not .0")] | ||
[TestCase(2, 1, true, "0.", ExpectedResult = false, TestName = "precision 2, scale 1, only positive, result not 0.")] | ||
[TestCase(2, 0, true, "-1", ExpectedResult = false, TestName = "precision 2, scale 0, only positive, result not -1")] | ||
[TestCase(1, 0, true, "+1", ExpectedResult = false, TestName = "precision 1, scale 0, only positive, result not +1")] | ||
public bool IsValidNumber_VariousInput_AfterCreatingValidator( | ||
int precision, | ||
int scale, | ||
bool onlyPositive, | ||
string expectedResultValue) | ||
{ | ||
return new NumberValidator(precision, scale, onlyPositive).IsValidNumber(expectedResultValue); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здесь ассерт забыл. Должно быть что-то типа Should().BeTrue() или тому подобное, в зависимости от того, что ты хочешь проверить. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resolved |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ага, а если будет 3 поколения, будет работать?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved