Skip to content

Commit a12d432

Browse files
committed
Refactor ДЗ Testing
1 parent ad33db9 commit a12d432

File tree

3 files changed

+181
-72
lines changed

3 files changed

+181
-72
lines changed

Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs

Lines changed: 114 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using FluentAssertions;
1+
using System.Runtime.CompilerServices;
2+
using FluentAssertions;
23
using NUnit.Framework;
34
using NUnit.Framework.Legacy;
45

@@ -8,69 +9,140 @@ public class ObjectComparison
89
#region test data
910
private static Person currentTsar = TsarRegistry.GetCurrentTsar();
1011
private static Person tsarCopy = new Person("Ivan IV The Terrible", 54, 170, 70, new Person("Vasili III of Russia", 28, 170, 60, null));
12+
private static Person tsarCopyDifferent = new Person("Ivan IV The Kind", 54, 170, 70, new Person("Vasili III of Russia", 28, 170, 60, null));
13+
1114
private static Person person0Parents = new Person("1", 1, 1, 1, null);
12-
private static Person person0ParentsCopy = new Person("1", 1, 1, 1, null);
15+
private static Person person0ParentsCopy = Person.Copy(person0Parents);
16+
private static Person person0ParentsLimits = new Person(string.Empty, int.MaxValue, int.MinValue, 0, null);
17+
private static Person person0ParentsLimitsCopy = Person.Copy(person0ParentsLimits);
1318
private static Person person1Parents = new Person("1", 1, 1, 1, new Person("2", 2, 2, 2, null));
14-
private static Person person1ParentsСopy = new Person("1", 1, 1, 1, new Person("2", 2, 2, 2, null));
19+
private static Person person1ParentsСopy = Person.Copy(person1Parents);
20+
private static Person person1ParentsDifferent = new Person("1", 1, 1, 1, new Person("3", 3, 3, 3, null));
1521
private static Person person2Parents = new Person("1", 1, 1, 1, new Person("2", 2, 2, 2, new Person("3", 3, 3, 3, null)));
16-
private static Person person2ParentsCopy = new Person("1", 1, 1, 1, new Person("2", 2, 2, 2, new Person("3", 3, 3, 3, null)));
22+
private static Person person2ParentsCopy = Person.Copy(person2Parents);
23+
private static Person person2ParentsDifferent = new Person("1", 1, 1, 1, new Person("2", 2, 2, 2, new Person("5", 5, 5, 5, null)));
1724
#endregion
1825

19-
private static IEnumerable<TestCaseData> TsarCheck_FailCases()
26+
private static IEnumerable<TestCaseData> FieldCompare_SuccessCases()
2027
{
21-
var tsarFailedCopy = new Person("Ivan IV The", 54, 170, 70, new Person("Vasili III of Russia", 28, 170, 60, null));
28+
yield return new TestCaseData(null, null).SetName("Пустые аргументы");
29+
yield return new TestCaseData(currentTsar, tsarCopy).SetName("Проверка царя");
30+
yield return new TestCaseData(person0Parents, person0ParentsCopy).SetName("Нет родителя");
31+
yield return new TestCaseData(person1Parents, person1ParentsСopy).SetName("Есть родитель");
32+
yield return new TestCaseData(person0Parents, person1ParentsСopy).SetName("Разные родители");
33+
yield return new TestCaseData(person2Parents, person2ParentsDifferent).SetName("Разные прародители");
34+
yield return new TestCaseData(person0ParentsLimits, person0ParentsLimitsCopy).SetName("Проверка с граничными значениями полей");
35+
}
2236

23-
yield return new TestCaseData(currentTsar, tsarFailedCopy).SetName("Проверка неправильного царя");
24-
yield return new TestCaseData(person0Parents, person1ParentsСopy).SetName("Разный родитель");
25-
yield return new TestCaseData(person1Parents, person2ParentsCopy).SetName("Разный родитель у родителя");
37+
[Test, TestCaseSource(nameof(FieldCompare_SuccessCases))]
38+
[Description("Проверка Person по полям - поля равны")]
39+
public void AreEqual_NotThrows_OnEqualFields(Person? actual, Person? expected)
40+
{
41+
actual.Should().BeEquivalentTo(expected, options =>
42+
options
43+
.Excluding(t => t.Id)
44+
.Excluding(t => t.Parent)
45+
);
46+
}
47+
48+
private static IEnumerable<TestCaseData> FieldCompare_FailCases()
49+
{
50+
yield return new TestCaseData(currentTsar, null).SetName("Сравнение с null");
51+
yield return new TestCaseData(currentTsar, tsarCopyDifferent).SetName("Проверка неправильного царя");
2652
yield return new TestCaseData(currentTsar, person0Parents).SetName("Полностью разные");
53+
yield return new TestCaseData(person0ParentsLimits, person0Parents).SetName("Просто разные");
54+
}
55+
56+
[Test, TestCaseSource(nameof(FieldCompare_FailCases))]
57+
[Description("Проверка Person по полям - поля разные")]
58+
public void AreEqual_Throws_OnDifferentFields(Person? actual, Person? expected)
59+
{
60+
actual.Should().NotBeEquivalentTo(expected, options =>
61+
options
62+
.Excluding(t => t.Id)
63+
.Excluding(t => t.Parent)
64+
);
2765
}
2866

29-
[Test, TestCaseSource(nameof(TsarCheck_FailCases))]
30-
public void FailCheckCurrentTsar(Person? actualTsar, Person? expectedTsar)
67+
private static IEnumerable<TestCaseData> FullCompare_SuccessCases()
3168
{
32-
Action act = () => AreEqualFuent(actualTsar, expectedTsar);
33-
act.Should().Throw<AssertionException>();
69+
yield return new TestCaseData(null, null).SetName("Пустые аргументы при полном сравнении");
70+
yield return new TestCaseData(currentTsar, tsarCopy).SetName("Проверка царя при полном сравнении");
71+
yield return new TestCaseData(person0Parents, person0ParentsCopy).SetName("Нет родителя при полном сравнении");
72+
yield return new TestCaseData(person1Parents, person1ParentsСopy).SetName("Есть родитель при полном сравнении");
73+
yield return new TestCaseData(person2Parents, person2ParentsCopy).SetName("Родитель у родителя при полном сравнении");
3474
}
35-
36-
private static IEnumerable<TestCaseData> TsarCheck_Cases()
75+
76+
77+
[Test, TestCaseSource(nameof(FullCompare_SuccessCases))]
78+
[Description("Проверка Person по Parent - Parent равны")]
79+
public void AreEqual_NotThrows_OnEqualPersons(Person? actual, Person? expected)
3780
{
38-
yield return new TestCaseData(null, null).SetName("Пустые аргументы");
39-
yield return new TestCaseData(currentTsar, tsarCopy).SetName("Проверка царя");
40-
yield return new TestCaseData(person0Parents, person0ParentsCopy).SetName("Нет родителя");
41-
yield return new TestCaseData(person1Parents, person1ParentsСopy).SetName("Есть родитель");
42-
yield return new TestCaseData(person2Parents, person2ParentsCopy).SetName("Родитель у родителя");
43-
}
81+
actual.Should().BeEquivalentTo(expected, options =>
82+
options
83+
.Excluding(t => t.Id)
84+
.Using<Person>(ctx =>
85+
{
86+
AreEqual_NotThrows_OnEqualFields(ctx.Subject, ctx.Expectation);
87+
if (ctx.Subject != null && ctx.Expectation != null)
88+
{
89+
if (ctx.Subject.Parent != null && ctx.Expectation.Parent != null)
90+
{
91+
AreEqual_NotThrows_OnEqualPersons(ctx.Subject.Parent, ctx.Expectation.Parent);
92+
}
93+
else
94+
{
95+
ctx.Subject.Parent.Should().BeEquivalentTo(ctx.Expectation.Parent, options =>
96+
options
97+
.Excluding(t => t.Id)
98+
.Excluding(t => t.Parent)
99+
);
100+
}
101+
}
102+
}
103+
)
104+
.WhenTypeIs<Person>()
105+
);
106+
}
44107

45-
[Test, TestCaseSource(nameof(TsarCheck_Cases))]
46-
[Description("Проверка текущего царя")]
47-
[Category("ToRefactor")]
48-
public void CheckCurrentTsar(Person? actualTsar, Person? expectedTsar)
108+
private static IEnumerable<TestCaseData> FullCompare_FailCases()
49109
{
50-
// Перепишите код на использование Fluent Assertions.
51-
AreEqualFuent(actualTsar, expectedTsar);
110+
yield return new TestCaseData(currentTsar, null).SetName("Сравнение с null при полном сравнении");
111+
yield return new TestCaseData(currentTsar, tsarCopyDifferent).SetName("Проверка неправильного царя при полном сравнении");
112+
yield return new TestCaseData(currentTsar, person1Parents).SetName("Полностью разные при полном сравнении");
113+
yield return new TestCaseData(person1Parents, person0Parents).SetName("Родитель null при полном сравнении");
114+
yield return new TestCaseData(person1Parents, person1ParentsDifferent).SetName("Разные родители при полном сравнении");
115+
yield return new TestCaseData(person2Parents, person2ParentsDifferent).SetName("Разные прародители при полном сравнении");
52116
}
53117

54-
private void AreEqualFuent(Person? actual, Person? expected)
118+
[Test, TestCaseSource(nameof(FullCompare_FailCases))]
119+
[Description("Проверка Person по Parent - Parent различны")]
120+
public void AreEqual_Throw_OnDifferentPersons(Person? actual, Person? expected)
55121
{
56-
actual.Should().BeEquivalentTo(expected, options =>
122+
actual.Should().NotBeEquivalentTo(expected, options =>
57123
options
58124
.Excluding(t => t.Id)
59-
.Excluding(t => t.Parent) // Игнорировать Parent
125+
.Using<Person>(ctx =>
126+
{
127+
AreEqual_NotThrows_OnEqualFields(ctx.Subject, ctx.Expectation);
128+
if (ctx.Subject != null && ctx.Expectation != null)
129+
{
130+
if (ctx.Subject.Parent != null && ctx.Expectation.Parent != null)
131+
{
132+
AreEqual_NotThrows_OnEqualPersons(ctx.Subject.Parent, ctx.Expectation.Parent);
133+
}
134+
else
135+
{
136+
(ctx.Subject.Parent == null && ctx.Expectation.Parent == null).Should().BeTrue();
137+
}
138+
}
139+
}
140+
)
141+
.WhenTypeIs<Person>()
60142
);
61-
if (actual != null && expected != null)
62-
{
63-
if (actual.Parent != null && expected.Parent != null)
64-
{
65-
AreEqualFuent(actual.Parent, expected.Parent);
66-
}
67-
if (actual.Parent == null || expected.Parent == null)
68-
{
69-
actual.Parent.Should().BeNull();
70-
expected.Parent.Should().BeNull();
71-
}
72-
}
73143
}
144+
145+
//Как будто бы если оставлять обобщенный метод кода становится меньше
74146

75147
// При добавлении новых полей в Person придется расширять условие в return еще больше;
76148
// Тест должен пройти по всем Parent прежде чем дать результат,

Testing/Basic/Homework/1. ObjectComparison/Person.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ public Person(string name, int age, int height, int weight, Person parent)
1818
Weight = weight;
1919
Parent = parent;
2020
}
21+
22+
public static Person? Copy(Person? source)
23+
{
24+
if (source is null)
25+
{
26+
return null;
27+
}
28+
return new Person(source.Name, source.Age, source.Height, source.Weight, Person.Copy(source.Parent));
29+
}
2130
}

Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,87 @@ public class NumberValidatorTests
1111
{
1212
#region Constructor tests
1313

14-
private static IEnumerable<TestCaseData> Constructor_MustThrow()
14+
private static IEnumerable<TestCaseData> Constructor_MustThrow_OnPrecisionExceed()
1515
{
16-
yield return new TestCaseData(-1, 2, true);
17-
yield return new TestCaseData(-1, 2, false);
18-
yield return new TestCaseData(1, -2, true);
19-
yield return new TestCaseData(-1, -2, false);
20-
yield return new TestCaseData(1, 2, true);
21-
yield return new TestCaseData(1, 1, false);
16+
yield return new TestCaseData(0, 0, false).SetName("Некорректная длина");
17+
yield return new TestCaseData(-1, -1, false).SetName("Некорректная длина");
18+
yield return new TestCaseData(-2, 2, true).SetName("Некорректная длина");
19+
yield return new TestCaseData(0, -3, false).SetName("Некорректная длина");
20+
yield return new TestCaseData(-5, -5, false).SetName("Некорректная длина");
2221
}
2322

24-
private static IEnumerable<TestCaseData> Constructor_NotThrow()
23+
private static IEnumerable<TestCaseData> Constructor_MustThrow_OnScaleExceed()
2524
{
26-
yield return new TestCaseData(1, 0, true);
27-
yield return new TestCaseData(int.MaxValue, int.MaxValue-1, true);
25+
yield return new TestCaseData(1, 2, true).SetName("Некорректная точность");
26+
yield return new TestCaseData(1, 1, false).SetName("Некорректная точность");
27+
yield return new TestCaseData(1, -1, false).SetName("Некорректная точность");
2828
}
2929

30-
[Test, TestCaseSource(nameof(Constructor_MustThrow))]
31-
public void Constructor_Throw_OnInvalidArgument(int precision, int scale, bool onlyPositive)
30+
private static IEnumerable<TestCaseData> Constructor_NotThrow_OnValidArguments()
31+
{
32+
yield return new TestCaseData(1, 0, true).SetName("Корректные аргументы");
33+
yield return new TestCaseData(int.MaxValue, int.MaxValue-1, true).SetName("Корректные аргументы");
34+
}
35+
36+
[Test]
37+
[
38+
TestCaseSource(nameof(Constructor_MustThrow_OnPrecisionExceed)),
39+
TestCaseSource(nameof(Constructor_MustThrow_OnScaleExceed))
40+
]
41+
public void Constructor_Throws_OnInvalidArgument(int precision, int scale, bool onlyPositive)
3242
{
3343
Action action = () => new NumberValidator(precision, scale, onlyPositive);
3444
action.Should().Throw();
3545
}
3646

37-
[Test, TestCaseSource(nameof(Constructor_NotThrow))]
38-
public void Constructor_NotThrow_OnValidArgument(int precision, int scale, bool onlyPositive)
47+
[Test, TestCaseSource(nameof(Constructor_NotThrow_OnValidArguments))]
48+
public void Constructor_NotThrows_OnValidArgument(int precision, int scale, bool onlyPositive)
3949
{
4050
Action action = () => new NumberValidator(precision, scale, onlyPositive);
4151
action.Should().NotThrow();
4252
}
43-
53+
4454
#endregion
4555

4656
#region IsValidNumber tests
4757

48-
private static IEnumerable<TestCaseData> CorrectNumbers_Cases()
58+
private static IEnumerable<TestCaseData> IsValidNumbers_ValidNumbersCases()
4959
{
50-
yield return new TestCaseData(17, 2, true, new string[] { "0.0", "0" });
60+
yield return new TestCaseData(17, 2, true, new string[] { "0.0", "0", "012322121.23" }).SetName("Правильные числа");
5161

52-
yield return new TestCaseData(4, 2, false, new string[] { "-1.23", "12.32" });
53-
54-
yield return new TestCaseData(4, 2, true, new string[] {"+1.23", "32"});
62+
yield return new TestCaseData(4, 2, false, new string[] { "-1,23", "12,32" }).SetName("Правильные числа");
5563

56-
yield return new TestCaseData(20, 19, false, new string[] {"1.1234567890123456789", "1123456789012345678.9"});
64+
yield return new TestCaseData(4, 2, true, new string[] { "+1,23", "32" }).SetName("Правильные числа");
65+
66+
yield return new TestCaseData(20, 19, false, new string[] { "1,1234567890123456789", "1123456789012345678.9" }).SetName("Правильные числа");
5767
}
58-
private static IEnumerable<TestCaseData> IncorrectNumbers_Cases()
68+
69+
private static IEnumerable<TestCaseData> IsValidNumbers_TooBigNumbersCases()
5970
{
60-
yield return new TestCaseData(3, 2, true, new string[] { "00.00", "-0.00", "a.sd", "+0.00", "+1.23" });
71+
yield return new TestCaseData(3, 2, false, new string[] { "00.00", "-0.00", "+0.00", "01,24" }).SetName("Слишком большие числа");
72+
73+
yield return new TestCaseData(19, 18, false, new string[] { "1.1234567890123456789", "12345678912345678900.01" }).SetName("Слишком большие числа");
74+
75+
}
76+
private static IEnumerable<TestCaseData> IsValidNumbers_WithWrongSignCases()
77+
{
78+
yield return new TestCaseData(4, 2, true, new string[] { "-0.00", "-1.23" }).SetName("Числа с неправильным знаком");;
6179

62-
yield return new TestCaseData(4, 2, true, new string[] {"-0.00", "-1.23"});
80+
yield return new TestCaseData(4, 3, true, new string[] { ".000", "+.124" }).SetName("Числа без целой части");;
81+
}
82+
83+
private static IEnumerable<TestCaseData> IsValidNumbers_WithLiteralsCases()
84+
{
85+
yield return new TestCaseData(4, 2, true, new string[] { "O.00", "_0.00"}).SetName("Некорректные символы");
6386

64-
yield return new TestCaseData(17, 2, false, new string[] {"0.sd00", "O12322121.23"});
87+
yield return new TestCaseData(17, 2, false, new string[] {"0.sd00", "OIOIIOIO"}).SetName("Некорректные символы");;
6588

66-
yield return new TestCaseData(19, 18, false, new string[] {"1.1234567890123456789", "12345678912345678900.01"});
89+
yield return new TestCaseData(19, 18, false, new string[] {"1,12 0123456789", "1678912345678900 . 01", "#!@$:|><1,2?%^&*()"}).SetName("Некорректные символы");;
6790

6891
}
6992

70-
[Test, TestCaseSource(nameof(CorrectNumbers_Cases))]
71-
public void IsValidNumber_CorrectNumber(int precision, int scale, bool onlyPositive, string[] numbers)
93+
[Test, TestCaseSource(nameof(IsValidNumbers_ValidNumbersCases))]
94+
public void IsValidNumber_IsTrue_OnCorrectNumber(int precision, int scale, bool onlyPositive, string[] numbers)
7295
{
7396
var validator = new NumberValidator(precision, scale, onlyPositive);
7497

@@ -78,8 +101,13 @@ public void IsValidNumber_CorrectNumber(int precision, int scale, bool onlyPosit
78101
}
79102
}
80103

81-
[Test, TestCaseSource(nameof(IncorrectNumbers_Cases))]
82-
public void IsValidNumber_IncorrectNumber(int precision, int scale, bool onlyPositive, string[] numbers)
104+
[Test]
105+
[
106+
TestCaseSource(nameof(IsValidNumbers_TooBigNumbersCases)),
107+
TestCaseSource(nameof(IsValidNumbers_WithLiteralsCases)),
108+
TestCaseSource(nameof(IsValidNumbers_WithWrongSignCases))
109+
]
110+
public void IsValidNumber_IsFalse_OnIncorrectNumber(int precision, int scale, bool onlyPositive, string[] numbers)
83111
{
84112
var validator = new NumberValidator(precision, scale, onlyPositive);
85113

0 commit comments

Comments
 (0)