1- using FluentAssertions ;
1+ using System . Runtime . CompilerServices ;
2+ using FluentAssertions ;
23using NUnit . Framework ;
34using 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 прежде чем дать результат,
0 commit comments