-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathBugTests.cs
1038 lines (826 loc) · 37.8 KB
/
BugTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using KellermanSoftware.CompareNetObjects;
using KellermanSoftware.CompareNetObjectsTests.Attributes;
using KellermanSoftware.CompareNetObjectsTests.TestClasses;
using NUnit.Framework;
using System.Drawing;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using KellermanSoftware.CompareNetObjects.Reports;
using KellermanSoftware.CompareNetObjectsTests.TestClasses.HashBug;
using Newtonsoft.Json;
using Point = System.Drawing.Point;
#if !NETSTANDARD
using System.Drawing.Drawing2D;
#endif
namespace KellermanSoftware.CompareNetObjectsTests
{
[TestFixture]
public class BugTests
{
#region Class Variables
private CompareLogic _compare;
#endregion
#region Setup/Teardown
/// <summary>
/// Code that is run before each test
/// </summary>
[SetUp]
public void Initialize()
{
_compare = new CompareLogic();
}
/// <summary>
/// Code that is run after each test
/// </summary>
[TearDown]
public void Cleanup()
{
_compare = null;
}
#endregion
#region Tests
[Test]
public void OverrideNewTypeShouldBeEqual()
{
OverrideNewType a = new OverrideNewType() { MyProperty1 = "1" };
OverrideNewType b = new OverrideNewType() { MyProperty1 = "1" };
CompareLogic compareLogic = new CompareLogic();
ComparisonResult result = compareLogic.Compare(a, b);
Console.WriteLine(result.DifferencesString);
Assert.IsTrue(result.AreEqual);
}
[Test]
public void OverrideEqualsShouldNotCauseStackOverflow()
{
ClassThatOverridesEquals class1 = new ClassThatOverridesEquals() { Id = 1 };
ClassThatOverridesEquals class2 = new ClassThatOverridesEquals() { Id = 2 };
Assert.IsFalse(class1.Equals(class2));
}
[Test]
public void ShouldNotAccessReadOnlyProperty()
{
ClassWithReadOnly object1 = new ClassWithReadOnly { Name = "Greg" };
ClassWithReadOnly object2 = new ClassWithReadOnly { Name = "Greg" };
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config.CompareReadOnly = false;
var result = compareLogic.Compare(object1, object2);
Assert.IsTrue(result.AreEqual);
}
[Test]
public void PositiveInfinityShouldBeTheSame()
{
double positiveInfinity = Double.PositiveInfinity;
double positiveInfinity2 = Double.PositiveInfinity;
CompareLogic compareLogic = new CompareLogic();
var result = compareLogic.Compare(positiveInfinity, positiveInfinity2);
Console.WriteLine(result.DifferencesString);
Assert.IsTrue(result.AreEqual);
}
[Test]
public void PositiveInfinityAndNegativeInfinityShouldBeDifferent()
{
double positiveInfinity = Double.PositiveInfinity;
double negativeInfinity = Double.NegativeInfinity;
CompareLogic compareLogic = new CompareLogic();
var result = compareLogic.Compare(positiveInfinity, negativeInfinity);
Console.WriteLine(result.DifferencesString);
Assert.IsFalse(result.AreEqual);
}
[Test]
public void CompareDoubleNan()
{
CompareLogic compareLogic = new CompareLogic();
var result = compareLogic.Compare(Double.NaN, 1.1);
Console.WriteLine(result.DifferencesString);
Assert.IsFalse(result.AreEqual);
}
[Test]
public void CompareDynamicStructWithinAStruct()
{
var a = (1, new List<(string first, (string, int) second)> { ("foo", ("bar", 2)) });
var b = (1, new List<(string first, (string, int) second)> { ("foo", ("foo", 2)) });
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config.MaxStructDepth = 4;
var result = compareLogic.Compare(a, b);
Console.WriteLine(result.DifferencesString);
Assert.IsFalse(result.AreEqual);
}
#nullable enable
//Failed comparison of objects with complex dictionary key
//https://github.com/GregFinzer/Compare-Net-Objects/issues/222
[Test]
public void ComplexDictionaryShouldCompare()
{
// Arrange.
var originalGraph = ComplexDictionaryModel.Create();
// Act.
var serializedKeys = JsonConvert.SerializeObject(originalGraph.DictOfDicts.Keys.ToArray());
var serializedValues = JsonConvert.SerializeObject(originalGraph.DictOfDicts.Values.ToArray());
var keys = JsonConvert.DeserializeObject<Dictionary<short, int>[]>(serializedKeys);
var values = JsonConvert.DeserializeObject<Dictionary<string, ushort?>[]>(serializedValues);
var dictOfDicts = keys.Zip(values, (x, y) => (x, y)).ToDictionary(pair => pair.x, pair => pair.y, new ComplexDictionaryModel.KeyDictionaryComparer());
var deserializedGraph = new ComplexDictionaryModel(dictOfDicts!);
// Assert.
Console.WriteLine("Manual Compare");
originalGraph.ManualCompare(deserializedGraph);
Console.WriteLine("Compare .NET Objects");
originalGraph.CompareObjects(deserializedGraph);
}
#nullable disable
[Test]
public void IPV6AddressShouldBeDifferent()
{
IPEndPoint a = new IPEndPoint(IPAddress.Parse("2001:4898:e0:5e:444:fcfd:cccc:1111"), 443) ;
IPEndPoint b = new IPEndPoint(IPAddress.Parse("2001:4898:e0:5e:4f4:fcfd:854c:16d9"), 443);
CompareLogic compareLogic = new CompareLogic();
var result = compareLogic.Compare(a, b);
Console.WriteLine(result.DifferencesString);
Assert.IsFalse(result.AreEqual);
}
[Test]
public void ComparingDictionariesReturnTwoDifferencesForSameKey()
{
var oldDoc = new TestDoc()
{
Fields =
{
["a"] = 1,
["b"] = 2,
},
};
var newDoc = new TestDoc()
{
Fields =
{
["a"] = 3,
["c"] = 5
},
};
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config.MaxDifferences = int.MaxValue;
var result = compareLogic.Compare(oldDoc, newDoc);
Console.WriteLine(result.DifferencesString);
Assert.IsTrue(result.Differences.Count == 3);
}
[Test]
public void IgnoreOrderOneListHasADuplicateValue()
{
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config.IgnoreCollectionOrder = true;
List<string> list1 = new List<string>(){ "Item1", "Item2"};
List<string> list2 = new List<string>() { "Item1", "Item2", "Item2" };
var result = compareLogic.Compare(list1, list2);
Console.WriteLine(result.DifferencesString);
Assert.IsFalse(result.AreEqual);
}
[Test]
public void IgnoredMemberGetPropertyAccessed()
{
//This is the comparison class
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config.MembersToIgnore.Add("PersonWithNotImplementedProperty.Name");
//Create a couple objects to compare
PersonWithNotImplementedProperty person1 = new PersonWithNotImplementedProperty();
person1.DateCreated = DateTime.Now;
PersonWithNotImplementedProperty person2 = new PersonWithNotImplementedProperty();
person2.DateCreated = person1.DateCreated;
ComparisonResult result = compareLogic.Compare(person1, person2);
Assert.IsTrue(result.AreEqual);
}
[Test]
public void CheckIgnoreCollectionOrder()
{
Dictionary<Type, IEnumerable<string>> collectionSpec = new Dictionary<Type, IEnumerable<string>>();
collectionSpec.Add(typeof(Foo2), new string[] { "Prop" });
var config = new ComparisonConfig { IgnoreCollectionOrder = true, CollectionMatchingSpec = collectionSpec };
var compareLogic = new CompareLogic(config);
var actual = new Foo2[] { new Foo2(2), new Foo2(1) };
var expected = new Foo2[] { new Foo2(1), new Foo2(2) };
var result = compareLogic.Compare(expected, actual);
Console.WriteLine(result.DifferencesString);
Assert.IsTrue(result.AreEqual);
}
[Test]
public void SimpleArrayTest()
{
var compareLogic = new CompareLogic();
compareLogic.Config.IgnoreCollectionOrder = true;
compareLogic.Config.MaxDifferences = int.MaxValue;
ComparisonResult result = compareLogic.Compare(new[] { "one" }, new[] { "two" });
Console.WriteLine(result.DifferencesString);
}
[Test]
public void HashBugTest()
{
var c = new HashBugC()
{
Text = "I'm C"
};
var b = new HashBugB()
{
Text = "I'm B",
CollectionOfC = new List<HashBugC>()
{
c
}
};
var b2 = new HashBugB()
{
Text = "I'm B",
CollectionOfC = new List<HashBugC>()
{
c
}
};
var comparisonResult = b == b2;
Assert.IsTrue(comparisonResult);
}
[Test]
public void DotsAndTabsShouldFormatCorrectly()
{
List<DotsAndTabs> groundTruth = new List<DotsAndTabs>();
List<DotsAndTabs> newResult = new List<DotsAndTabs>();
groundTruth.Add(new DotsAndTabs { boo = "hello", gg = "hello again" });
groundTruth.Add(new DotsAndTabs { boo = "scorpio \t. .\r\n11-nov", gg = "hello again2" });
newResult.Add(new DotsAndTabs { boo = "hello", gg = "hello again" });
newResult.Add(new DotsAndTabs { boo = " ..... ", gg = "hello again2" });
CompareLogic compareLogicObject = new CompareLogic();
compareLogicObject.Config.MaxDifferences = int.MaxValue;
compareLogicObject.Config.IgnoreCollectionOrder = true;
ComparisonResult assertionResult = compareLogicObject.Compare(groundTruth, newResult);
Console.WriteLine(assertionResult.DifferencesString + "\n\n\n");
UserFriendlyReport friendlyReport = new UserFriendlyReport();
Console.WriteLine(friendlyReport.OutputString(assertionResult.Differences));
}
[Test]
public void ComparingListsOfNullWhileIgnoringCollectionOrderShouldNotThrowObjectReferenceError()
{
//Arrange
ComparisonConfig config = new ComparisonConfig();
config.IgnoreCollectionOrder = true;
config.IgnoreObjectTypes = true;
CompareLogic logic = new CompareLogic(config);
List<object> data1 = new List<object>();
data1.Add(null);
List<object> data2 = new List<object>();
data2.Add(null);
//Act
var result = logic.Compare(data1, data2);
//Assert
Assert.IsTrue(result.AreEqual, result.DifferencesString);
}
[Test]
public void NegativeIntegersShouldBeNegativeOnUserFriendlyReport()
{
List<object> groundTruth = new List<object>();
List<object> newResult = new List<object>();
groundTruth.Add(new { boo = 1, gg = 7 });
groundTruth.Add(new { boo = -5, gg = 9 });
newResult.Add(new { boo = -6, gg = 4 });
newResult.Add(new { boo = 5, gg = 23 });
CompareLogic compareLogicObject = new CompareLogic();
compareLogicObject.Config.MaxDifferences = int.MaxValue;
compareLogicObject.Config.IgnoreCollectionOrder = true;
ComparisonResult assertionResult = compareLogicObject.Compare(groundTruth, newResult);
Console.WriteLine("DifferencesString");
Console.WriteLine(assertionResult.DifferencesString);
Console.WriteLine();
Console.WriteLine("UserFriendlyReport");
UserFriendlyReport friendlyReport = new UserFriendlyReport();
string result = friendlyReport.OutputString(assertionResult.Differences);
Console.WriteLine(result);
Assert.IsTrue(result.Contains("[{ boo = -5, gg = 9 }]"));
}
[Test]
public void ObjectWithSameHashCodeAndDifferentPropertiesShouldBeDifferent()
{
ClassWithOverriddenHashCode person1 = new ClassWithOverriddenHashCode();
person1.Name = "Weird Al Yankovic";
person1.MyCircularReference = person1;
ClassWithOverriddenHashCode person2 = new ClassWithOverriddenHashCode();
person2.Name = "Robin Williams";
person2.MyCircularReference = person2;
CompareLogic compareLogic = new CompareLogic();
ComparisonResult result = compareLogic.Compare(person1, person2);
Console.WriteLine(result.DifferencesString);
Assert.IsFalse(result.AreEqual);
}
[Test]
public void RefStructProperty()
{
var compareLogic = new CompareLogic(new ComparisonConfig
{
MembersToIgnore =
{
"Item"
}
});
var differences = compareLogic.Compare(new RefStructClass(), new RefStructClass());
Assert.IsTrue(differences.AreEqual);
}
[Test]
public void When_CompareDateTimeOffsetWithOffsets_Is_False_Do_Not_Compare_Offsets()
{
DateTime now = DateTime.Now;
DateTimeOffset date1 = new DateTimeOffset(
now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, new TimeSpan(0, 0, 0));
DateTimeOffset date2 = new DateTimeOffset(
now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, new TimeSpan(3, 0, 0));
_compare.Config.CompareDateTimeOffsetWithOffsets = false;
ComparisonResult result = _compare.Compare(date1, date2);
if (!result.AreEqual)
throw new Exception(result.DifferencesString);
}
[Test]
public void When_CompareDateTimeOffsetWithOffsets_Is_True_Compare_Offsets()
{
DateTime now = DateTime.Now;
DateTimeOffset date1 = new DateTimeOffset(
now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, new TimeSpan(0, 0, 0));
DateTimeOffset date2 = new DateTimeOffset(
now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, new TimeSpan(3, 0, 0));
_compare.Config.CompareDateTimeOffsetWithOffsets = true;
ComparisonResult result = _compare.Compare(date1, date2);
Assert.IsFalse(result.AreEqual);
}
[Test]
public void TimespanShouldCompareWhenCompareChildrenIsFalse()
{
//Arrange
var object1 = new ClassWithTimespan() { MyTimeSpan = new TimeSpan(6, 0, 0) };
var object2 = new ClassWithTimespan { MyTimeSpan = new TimeSpan(7, 0, 0) };
var comparerConfig = new ComparisonConfig();
comparerConfig.CompareChildren = false;
var comparer = new CompareLogic(comparerConfig);
//Act
var result = comparer.Compare(object1, object2);
//Assert
Assert.IsFalse(result.AreEqual);
}
[Test]
public void GetPrivatePropertiesNetStandard()
{
//Arrange
Type type = typeof(ClassWithPrivateProperties);
//Act
var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
//Assert
Assert.IsTrue(props.Length > 0);
}
/// <summary>
/// https://github.com/GregFinzer/Compare-Net-Objects/issues/77
/// </summary>
[Test]
public void ComparisonsOfTypesWithPrivateFieldsAreAccurate()
{
var compareLogic = new CompareLogic(new ComparisonConfig { ComparePrivateFields = true });
var result = compareLogic.Compare(new SomethingWithPrivateField(123), new SomethingWithPrivateField(456));
Assert.IsFalse(result.AreEqual);
}
private class SomethingWithPrivateField
{
private readonly int _key;
public SomethingWithPrivateField(int key) { _key = key; }
}
/// <summary>
/// https://github.com/GregFinzer/Compare-Net-Objects/issues/77
/// </summary>
[Test]
public void ComparisonsOfTypesWithPrivatePropertiesAreAccurate()
{
var compareLogic = new CompareLogic(new ComparisonConfig { ComparePrivateProperties = true });
var result = compareLogic.Compare(new SomethingWithPrivateProperty(123), new SomethingWithPrivateProperty(456));
Assert.IsFalse(result.AreEqual);
}
private class SomethingWithPrivateProperty
{
public SomethingWithPrivateProperty(int key) { Key = key; }
private int Key { get; }
}
/// <summary>
/// https://github.com/GregFinzer/Compare-Net-Objects/issues/110
/// </summary>
[Test]
public void CsvReportWithCommaTest()
{
// set up data
Person person1 = new Person();
person1.Name = "Greg";
person1.LastName = "Miller";
person1.Age = 42;
Person person2 = new Person();
person2.Name = "Greg";
person2.LastName = "Miller";
person2.Age = 17;
// compare
var left = new List<Person> { person1 };
var right = new List<Person> { person2 };
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config.IgnoreCollectionOrder = true;
compareLogic.Config.CollectionMatchingSpec.Add(
typeof(Person),
new string[] { "Name", "LastName" }); // specify two indexes
ComparisonResult result = compareLogic.Compare(left, right);
// write to csv
var csv = new CsvReport();
string output = csv.OutputString(result.Differences);
Console.WriteLine(output);
Assert.IsTrue(output.Contains("\"[Name:Greg,LastName:Miller].Age\""));
}
[Test]
public void DifferentNullableDecimalFieldsShouldNotBeEqualWhenCompareChildrenIsFalse()
{
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config.CompareChildren = false;
PrimitiveFieldsNullable object1 = new PrimitiveFieldsNullable();
object1.DecimalField = 0;
PrimitiveFieldsNullable object2 = new PrimitiveFieldsNullable();
object2.DecimalField = 3.0M;
Assert.IsFalse(compareLogic.Compare(object1, object2).AreEqual);
}
[Test]
public void DifferentDecimalFieldsShouldNotBeEqualWhenCompareChildrenIsFalse()
{
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config.CompareChildren = false;
PrimitiveFields object1 = new PrimitiveFields();
object1.DecimalField = 0;
PrimitiveFields object2 = new PrimitiveFields();
object2.DecimalField = 3.0M;
Assert.IsFalse(compareLogic.Compare(object1, object2).AreEqual);
}
[Test]
public void DifferentIntegersShouldNotBeEqualInsideAnAnonymousType()
{
CompareLogic compareLogic = new CompareLogic();
// try with integers
var int1 = new { MyNumber = (int)0 };
var int2 = new { MyNumber = (int)3 };
// test with CompareChildren = false
compareLogic.Config.CompareChildren = false;
ComparisonResult test3 = compareLogic.Compare(int1, int2);
Assert.IsFalse(test3.AreEqual, "int Test - CompareChildren = false");
// test with CompareChildren = true
compareLogic.Config.CompareChildren = true;
ComparisonResult test4 = compareLogic.Compare(int1, int2);
Assert.AreEqual(1, test4.Differences.Count);
Assert.IsFalse(test4.AreEqual, "int Test - CompareChildren = true");
}
[Test]
public void DifferentDecimalsShouldNotBeEqualInsideAnAnonymousType()
{
CompareLogic compareLogic = new CompareLogic();
// try with decimals
var dec1 = new { MyNumber = (decimal)0 };
var dec2 = new { MyNumber = (decimal)3.0 };
// test with CompareChildren = false
compareLogic.Config.CompareChildren = false;
ComparisonResult test1 = compareLogic.Compare(dec1, dec2);
Assert.IsFalse(test1.AreEqual, "Decimal Test - CompareChildren = false");
// test with CompareChildren = true
compareLogic.Config.CompareChildren = true;
ComparisonResult test2 = compareLogic.Compare(dec1, dec2);
Assert.IsFalse(test2.AreEqual, "Decimal Test - CompareChildren = true");
}
[Test]
public void NullableDecimalWithCompareChildrenFalseShouldSendADifferenceCallback()
{
List<Difference> differences = new List<Difference>();
SpecialFields specialFields1 = new SpecialFields();
specialFields1.NullableDecimalProperty = 1000;
SpecialFields specialFields2 = new SpecialFields();
specialFields2.NullableDecimalProperty = 2000;
_compare.Config = new ComparisonConfig()
{
CompareChildren = false,
DifferenceCallback = difference => { differences.Add(difference); }
};
_compare.Compare(specialFields1, specialFields2);
Assert.That(differences.FirstOrDefault(), Is.Not.Null);
}
[Test]
public void PropertyNameShouldNotHaveAPeriodInFrontOfIt()
{
Person person1 = new Person() { Name = "Luke Skywalker", DateCreated = DateTime.Today, ID = 1 };
Person person2 = new Person() { Name = "Leia Skywalker", DateCreated = DateTime.Today, ID = 1 };
var result = _compare.Compare(person1, person2);
Assert.IsFalse(result.AreEqual, "Expected to be different");
Assert.IsFalse(result.Differences[0].PropertyName.StartsWith("."), "Expected not to start with period");
}
[Test]
public void ShowBreadCrumbTest()
{
var people1 = new List<Person>() { new Person() { Name = "Joe" } };
var people2 = new List<Person>() { new Person() { Name = "Joe" } };
var group1 = new KeyValuePair<string, List<Person>>("People", people1);
var group2 = new KeyValuePair<string, List<Person>>("People", people2);
_compare.Config.ShowBreadcrumb = true;
var result = _compare.Compare(group1, group2);
Assert.IsTrue(result.AreEqual);
}
[Test]
public void ListOfDictionariesWithIgnoreOrder()
{
var bar1 = new List<Dictionary<string, string>>
{
new Dictionary<string, string>
{
{"a", "b"},
{"c", "d"},
{"e", "f"},
{"g", "h"},
}
};
var bar2 = new List<Dictionary<string, string>>
{
new Dictionary<string, string>
{
{"e", "f"},
{"g", "h"},
{"c", "d"},
{"a", "b"},
}
};
var comparer = new CompareLogic { Config = { IgnoreCollectionOrder = true } };
var res = comparer.Compare(bar1, bar2);
Assert.IsTrue(res.AreEqual, res.DifferencesString);
}
[Test]
public void DbNullTest()
{
CompareLogic compareLogic = new CompareLogic();
ComparisonResult result = compareLogic.Compare(DBNull.Value, DBNull.Value);
if (!result.AreEqual)
Console.WriteLine(result.DifferencesString);
}
[Test]
public void PropertyComparerFailsWithObjectNullException()
{
//This is the comparison class
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config.SkipInvalidIndexers = true;
//Create a couple objects to compare
Person2 person1 = new Person2();
person1.DateCreated = DateTime.Now;
person1.Name = "Greg";
Person2 person2 = new Person2();
person2.Name = "John";
person2.DateCreated = person1.DateCreated;
//These will be different, write out the differences
ComparisonResult result = compareLogic.Compare(person1, person2);
if (!result.AreEqual)
Console.WriteLine(result.DifferencesString);
}
[Test]
public void GermanUmlautsAndAccents()
{
string string1 = "straße";
string string2 = "strasse";
ComparisonResult result = _compare.Compare(string1, string2);
Console.WriteLine(result.DifferencesString);
Assert.IsFalse(result.AreEqual);
}
[Test]
public void CircularReferencesEnumerable()
{
var o1 = new ClassWithOverriddenHashCode();
o1.Name = "1";
var o2 = new ClassWithOverriddenHashCode();
o2.Name = "1";
IEnumerable<ClassWithOverriddenHashCode> e1 =(new [] {o1}).Where(_ => true);
o1.MyCircularReference = e1;
IEnumerable<ClassWithOverriddenHashCode> e2 =(new [] {o2}).Where(_ => true);
o2.MyCircularReference = e2;
CompareLogic logic = new CompareLogic();
ComparisonResult result = logic.Compare(e1, e2);
Assert.IsTrue(result.AreEqual, result.DifferencesString);
}
public class Foo
{
}
public class Bar
{
}
[Test]
public void GenericEnumerable()
{
List<Foo> fooList = new List<Foo>();
IEnumerable<Bar> barEnumerable = fooList.Select(f => new Bar());
List<Bar> barList = new List<Bar>();
CompareLogic logic = new CompareLogic();
logic.Config.IgnoreObjectTypes = true;
ComparisonResult result = logic.Compare(barEnumerable, barList);
Assert.IsTrue(result.AreEqual, result.DifferencesString);
}
[Test]
public void ObjectTypeObjectTest()
{
ObjectTypeClass objectClass1 = new ObjectTypeClass();
objectClass1.FieldObject = new object();
objectClass1.PropertyObject = new object();
ObjectTypeClass.StaticObject = new object();
ObjectTypeClass objectClass2 = new ObjectTypeClass();
objectClass2.FieldObject = new object();
objectClass2.PropertyObject = new object();
ComparisonResult result = _compare.Compare(objectClass1, objectClass2);
if (!result.AreEqual)
Assert.Fail(result.DifferencesString);
}
[Test]
public void IgnoreTypesTest()
{
ExampleDto1 dto1 = new ExampleDto1();
dto1.Name = "Greg";
ExampleDto2 dto2 = new ExampleDto2();
dto2.Name = "Greg";
ComparisonResult result = _compare.Compare(dto1, dto2);
//These will be different because the types are different
Assert.IsFalse(result.AreEqual);
Console.WriteLine(result.DifferencesString);
_compare.Config.IgnoreObjectTypes = true;
result = _compare.Compare(dto1, dto2);
//Ignore types so they will be equal
Assert.IsTrue(result.AreEqual);
_compare.Config.Reset();
}
[Test]
public void TankElementsToIncludeTest()
{
_compare.Config.MaxDifferences = 10;
_compare.Config.MembersToInclude.Add("TankPerson");
_compare.Config.MembersToInclude.Add("TankName");
_compare.Config.MembersToInclude.Add("Name");
_compare.Config.MembersToInclude.Add("FamilyName");
_compare.Config.MembersToInclude.Add("GivenName");
//Create a couple objects to compare
TankPerson person1 = new TankPerson()
{
Id = 1,
DateCreated = DateTime.Now,
Name = new TankName { FamilyName = "Huston", GivenName = "Greg" },
Address = "Address1"
};
TankPerson person2 = new TankPerson()
{
Id = 2,
Name = new TankName { FamilyName = "McClane", GivenName = "John" },
DateCreated = DateTime.UtcNow,
Address = "Address2"
};
ComparisonResult result = _compare.Compare(person1, person2);
Assert.IsFalse(result.AreEqual);
//These will be different, write out the differences
if (!result.AreEqual)
{
Console.WriteLine("------");
Console.WriteLine("###################");
result.Differences.ForEach(d => Console.WriteLine(d.ToString()));
}
_compare.Config.MembersToInclude.Clear();
_compare.Config.MaxDifferences = 1;
}
private Shipment CreateShipment()
{
return new Shipment { Customer = "ADEG", IdentCode = 12934871928374, InsertDate = new DateTime(2012, 06, 12) };
}
[Test]
public void IgnoreByAttribute_test_should_fail_difference_should_be_customer()
{
// Arrange
Shipment shipment1 = CreateShipment();
Shipment shipment2 = CreateShipment();
shipment2.InsertDate = DateTime.Now; // InsertDate has the CompareIgnoreAttribute on it
shipment2.Customer = "Andritz";
_compare.Config.AttributesToIgnore.Add(typeof(CompareIgnoreAttribute));
_compare.Config.MaxDifferences = int.MaxValue;
// Act
var result = _compare.Compare(shipment1, shipment2);
// Assert
Assert.IsFalse(result.AreEqual);
Assert.AreEqual(1, result.Differences.Count);
Console.WriteLine(result.DifferencesString);
Assert.AreEqual("ADEG", result.Differences[0].Object1Value);
Assert.AreEqual("Andritz", result.Differences[0].Object2Value);
_compare.Config.AttributesToIgnore.Clear();
}
[Test]
public void IgnoreByLackOfAttribute_test_should_fail_difference_should_be_customer()
{
// Arrange
Shipment shipment1 = CreateShipment();
Shipment shipment2 = CreateShipment();
shipment2.InsertDate = DateTime.Now;
shipment2.Customer = "Andritz"; // Only Customer has the CompareAttribute on it
_compare.Config.RequiredAttributesToCompare.Add(typeof(CompareAttribute));
_compare.Config.MaxDifferences = int.MaxValue;
// Act
var result = _compare.Compare(shipment1, shipment2);
// Assert
Assert.IsFalse(result.AreEqual);
Assert.AreEqual(1, result.Differences.Count);
Console.WriteLine(result.DifferencesString);
Assert.AreEqual("ADEG", result.Differences[0].Object1Value);
Assert.AreEqual("Andritz", result.Differences[0].Object2Value);
_compare.Config.RequiredAttributesToCompare.Clear();
}
[Test]
public void WilliamCWarnerTest()
{
ILabTest labTest = new LabTest();
labTest.AlternateContainerDescription = "Test 1";
labTest.TestName = "Test The Audit";
ILabTest origLabLest = new LabTest();//this would be in session
origLabLest.TestName = "Original Test Name";
origLabLest.AlternateContainerDescription = "Test 2";
_compare.Config.MaxDifferences = 500;
var result = _compare.Compare(labTest, origLabLest);
Assert.IsFalse(result.AreEqual);
Assert.IsTrue(result.Differences.Count > 0);
Console.WriteLine(result.DifferencesString);
}
[Test]
public void IgnoreByAttribute_IgnoreCollectionOrder_ShouldPass()
{
// Arrange
Shipment shipment1 = new Shipment() { Customer = "Name1" };
shipment1.InsertDate = DateTime.Now; // InsertDate has the CompareIgnoreAttribute on it
Shipment shipment2 = new Shipment() { Customer = "Name2" };
_compare.Config.AttributesToIgnore.Add(typeof(CompareIgnoreAttribute));
_compare.Config.IgnoreCollectionOrder = true;
_compare.Config.MaxDifferences = int.MaxValue;
List<Shipment> shipments1 = new List<Shipment>();
shipments1.Add(shipment1);
shipments1.Add(shipment2);
List<Shipment> shipments2 = new List<Shipment>();
Shipment shipment3 = new Shipment() { Customer = "Name2" };
Shipment shipment4 = new Shipment() { Customer = "Name1" };
shipment4.InsertDate = DateTime.Now; // InsertDate has the CompareIgnoreAttribute on it
//add in different order
shipments2.Add(shipment3);
shipments2.Add(shipment4);
//shipment1 & shipment3 are same, shipment2 and shipment4 are also same but their InsertDate is different.
//Also the order of items are diiferent in both list.
// Act
var result = _compare.Compare(shipments1, shipments2);
// Assert
Assert.IsTrue(result.AreEqual);
Assert.AreEqual(0, result.Differences.Count);
Console.WriteLine(result.DifferencesString);
_compare.Config.AttributesToIgnore.Clear();
}
[Test]
public void IgnoreByAttribute_IgnoreCollectionOrder_ShouldFail()
{
// Arrange
Shipment shipment1 = new Shipment() { Customer = "Name1" };
shipment1.InsertDate = DateTime.Now; // InsertDate has the CompareIgnoreAttribute on it
Shipment shipment2 = new Shipment() { Customer = "Name2" };
//No ignored attributes added so test should fail for property InsertDate
_compare.Config.IgnoreCollectionOrder = true;
_compare.Config.MaxDifferences = int.MaxValue;
List<Shipment> shipments1 = new List<Shipment>();
shipments1.Add(shipment1);
shipments1.Add(shipment2);
List<Shipment> shipments2 = new List<Shipment>();
Shipment shipment3 = new Shipment() { Customer = "Name2" };
Shipment shipment4 = new Shipment() { Customer = "Name1" };
shipment4.InsertDate = DateTime.Now.AddDays(1); // Set different value for InsertDate
//add in different order
shipments2.Add(shipment3);
shipments2.Add(shipment4);
//shipment1 & shipment3 are same, shipment2 and shipment4 are also same but their InsertDate is different.
//Also the order of items are diiferent in both list.