Skip to content

Commit c9a63c3

Browse files
authored
Merge pull request #314 - bugfix for incorrect comparison from AreDeeplyEqual() on Enum values
#241 Bugfix for incorrect comparison from AreDeeplyEqual() on Enum values
2 parents 5bd7e8c + 5c16043 commit c9a63c3

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Reflection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ private static bool AreDeeplyEqual(object expected, object actual, ConditionalWe
446446
return false;
447447
}
448448

449-
if (expectedType.GetTypeInfo().IsPrimitive && actualType.GetTypeInfo().IsPrimitive)
449+
if (expectedType.GetTypeInfo().IsPrimitive || expectedType.GetTypeInfo().IsEnum)
450450
{
451451
return expected.ToString() == actual.ToString();
452452
}

test/MyTested.AspNetCore.Mvc.Abstractions.Test/UtilitiesTests/ReflectionTests.cs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Authorization;
77
using Microsoft.AspNetCore.Mvc;
88
using Microsoft.Extensions.DependencyInjection;
9+
using Setups.Common;
910
using Setups.Controllers;
1011
using Setups.Models;
1112
using Setups.Services;
@@ -449,6 +450,24 @@ public void AreDeeplyEqualShouldWorkCorrectlyWithPrimitiveAndStructTypes()
449450
Assert.False(Reflection.AreDeeplyEqual(new DateTime(2015, 10, 19), new DateTime(2015, 10, 20)));
450451
}
451452

453+
[Fact]
454+
public void AreDeeplyEqualShouldWorkCorrectlyWithEnumerations()
455+
{
456+
// Enum with default values.
457+
Assert.True(Reflection.AreDeeplyEqual(DateTimeKind.Unspecified, DateTimeKind.Unspecified));
458+
Assert.False(Reflection.AreDeeplyEqual(DateTimeKind.Local, DateTimeKind.Utc));
459+
460+
//Enum with overridden values.
461+
Assert.True(Reflection.AreDeeplyEqual(AttributeTargets.Delegate, AttributeTargets.Delegate));
462+
Assert.False(Reflection.AreDeeplyEqual(AttributeTargets.Assembly, AttributeTargets.All));
463+
Assert.False(Reflection.AreDeeplyEqual(AttributeTargets.Assembly, AttributeTargets.Module));
464+
465+
//Enum with default and overriden values.
466+
Assert.True(Reflection.AreDeeplyEqual(CustomEnum.DefaultConstant, CustomEnum.DefaultConstant));
467+
Assert.False(Reflection.AreDeeplyEqual(CustomEnum.DefaultConstant, CustomEnum.ConstantWithCustomValue));
468+
Assert.False(Reflection.AreDeeplyEqual(CustomEnum.DefaultConstant, CustomEnum.CombinedConstant));
469+
}
470+
452471
[Fact]
453472
public void AreDeeplyEqualsShouldWorkCorrectlyWithNormalObjects()
454473
{
@@ -463,6 +482,10 @@ public void AreDeeplyEqualsShouldWorkCorrectlyWithNormalObjects()
463482
Assert.True(Reflection.AreDeeplyEqual(new EqualsModel { Integer = 1, String = "test" }, new EqualsModel { Integer = 1, String = "another" }));
464483
Assert.True(Reflection.AreDeeplyEqual(new EqualityOperatorModel { Integer = 1, String = "test" }, new EqualityOperatorModel { Integer = 1, String = "another" }));
465484
Assert.False(Reflection.AreDeeplyEqual(new object(), "test"));
485+
Assert.False(Reflection.AreDeeplyEqual(new object(), AttributeTargets.All));
486+
Assert.False(Reflection.AreDeeplyEqual(AttributeTargets.All, new object()));
487+
Assert.True(Reflection.AreDeeplyEqual(AttributeTargets.All, (object)AttributeTargets.All));
488+
Assert.True(Reflection.AreDeeplyEqual((object)AttributeTargets.All, AttributeTargets.All));
466489
Assert.False(Reflection.AreDeeplyEqual(DateTime.Now, "test"));
467490
Assert.False(Reflection.AreDeeplyEqual("test", DateTime.Now));
468491
Assert.False(Reflection.AreDeeplyEqual(true, new object()));
@@ -488,12 +511,14 @@ public void AreDeeplyEqualsShouldWorkCorrectlyWithNestedObjects()
488511
{
489512
Integer = 1,
490513
String = "test1",
514+
Enum = CustomEnum.ConstantWithCustomValue,
491515
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
492516
},
493517
new NestedModel
494518
{
495519
Integer = 1,
496520
String = "test1",
521+
Enum = CustomEnum.ConstantWithCustomValue,
497522
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
498523
}));
499524

@@ -502,12 +527,14 @@ public void AreDeeplyEqualsShouldWorkCorrectlyWithNestedObjects()
502527
{
503528
Integer = 1,
504529
String = "test",
530+
Enum = CustomEnum.ConstantWithCustomValue,
505531
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
506532
},
507533
new NestedModel
508534
{
509535
Integer = 1,
510536
String = "test",
537+
Enum = CustomEnum.ConstantWithCustomValue,
511538
Nested = new NestedModel { Integer = 2, String = "test1", Nested = new NestedModel { Integer = 3, String = "test3" } }
512539
}));
513540

@@ -516,12 +543,14 @@ public void AreDeeplyEqualsShouldWorkCorrectlyWithNestedObjects()
516543
{
517544
Integer = 1,
518545
String = "test1",
546+
Enum = CustomEnum.ConstantWithCustomValue,
519547
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test2" } }
520548
},
521549
new NestedModel
522550
{
523551
Integer = 1,
524552
String = "test1",
553+
Enum = CustomEnum.ConstantWithCustomValue,
525554
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
526555
}));
527556
}
@@ -535,27 +564,27 @@ public void AreDeeplyEqualShouldWorkCorrectlyWithCollections()
535564
new NestedModel
536565
{
537566
Integer = 1, String = "test1",
538-
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
567+
Nested = new NestedModel { Integer = 2, String = "test2", Enum = CustomEnum.CombinedConstant, Nested = new NestedModel { Integer = 3, String = "test3" } }
539568
},
540569
new NestedModel
541570
{
542571
Integer = 1,
543572
String = "test1",
544-
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
573+
Nested = new NestedModel { Integer = 2, String = "test2", Enum = CustomEnum.CombinedConstant, Nested = new NestedModel { Integer = 3, String = "test3" } }
545574
}
546575
},
547576
new List<NestedModel>
548577
{
549578
new NestedModel
550579
{
551580
Integer = 1, String = "test1",
552-
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
581+
Nested = new NestedModel { Integer = 2, String = "test2", Enum = CustomEnum.CombinedConstant, Nested = new NestedModel { Integer = 3, String = "test3" } }
553582
},
554583
new NestedModel
555584
{
556585
Integer = 1,
557586
String = "test1",
558-
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
587+
Nested = new NestedModel { Integer = 2, String = "test2", Enum = CustomEnum.CombinedConstant, Nested = new NestedModel { Integer = 3, String = "test3" } }
559588
}
560589
}));
561590

@@ -565,23 +594,27 @@ public void AreDeeplyEqualShouldWorkCorrectlyWithCollections()
565594
{
566595
Integer = 1,
567596
String = "test1",
597+
Enum = CustomEnum.ConstantWithCustomValue,
568598
Nested =
569599
new NestedModel
570600
{
571601
Integer = 2,
572602
String = "test2",
603+
Enum = CustomEnum.ConstantWithCustomValue,
573604
Nested = new NestedModel { Integer = 3, String = "test3" }
574605
}
575606
},
576607
new NestedModel
577608
{
578609
Integer = 1,
579610
String = "test1",
611+
Enum = CustomEnum.ConstantWithCustomValue,
580612
Nested =
581613
new NestedModel
582614
{
583615
Integer = 2,
584616
String = "test2",
617+
Enum = CustomEnum.ConstantWithCustomValue,
585618
Nested = new NestedModel { Integer = 3, String = "test3" }
586619
}
587620
}
@@ -593,23 +626,27 @@ public void AreDeeplyEqualShouldWorkCorrectlyWithCollections()
593626
{
594627
Integer = 1,
595628
String = "test1",
629+
Enum = CustomEnum.ConstantWithCustomValue,
596630
Nested =
597631
new NestedModel
598632
{
599633
Integer = 2,
600634
String = "test2",
635+
Enum = CustomEnum.ConstantWithCustomValue,
601636
Nested = new NestedModel { Integer = 3, String = "test3" }
602637
}
603638
},
604639
new NestedModel
605640
{
606641
Integer = 1,
607642
String = "test1",
643+
Enum = CustomEnum.ConstantWithCustomValue,
608644
Nested =
609645
new NestedModel
610646
{
611647
Integer = 2,
612648
String = "test2",
649+
Enum = CustomEnum.ConstantWithCustomValue,
613650
Nested = new NestedModel { Integer = 3, String = "test3" }
614651
}
615652
}
@@ -894,27 +931,27 @@ public void AreDeeplyEqualShouldWorkCorrectlyWithDictionaries()
894931

895932
var firstDictionaryWithObject = new Dictionary<string, NestedModel>
896933
{
897-
{ "Key", new NestedModel { Integer = 1, String = "Text" } },
934+
{ "Key", new NestedModel { Integer = 1, String = "Text", Enum = CustomEnum.ConstantWithCustomValue} },
898935
{ "AnotherKey", new NestedModel { Integer = 2, String = "AnotherText" } }
899936
};
900937

901938
var secondDictionaryWithObject = new Dictionary<string, NestedModel>
902939
{
903-
{ "Key", new NestedModel { Integer = 1, String = "Text" } },
940+
{ "Key", new NestedModel { Integer = 1, String = "Text", Enum = CustomEnum.ConstantWithCustomValue } },
904941
{ "AnotherKey", new NestedModel { Integer = 2, String = "AnotherText" } }
905942
};
906943

907944
Assert.True(Reflection.AreDeeplyEqual(firstDictionaryWithObject, secondDictionaryWithObject));
908945

909946
firstDictionaryWithObject = new Dictionary<string, NestedModel>
910947
{
911-
{ "Key", new NestedModel { Integer = 1, String = "Text" } },
948+
{ "Key", new NestedModel { Integer = 1, String = "Text", Enum = CustomEnum.ConstantWithCustomValue } },
912949
{ "AnotherKey", new NestedModel { Integer = 2, String = "Text" } }
913950
};
914951

915952
secondDictionaryWithObject = new Dictionary<string, NestedModel>
916953
{
917-
{ "Key", new NestedModel { Integer = 1, String = "Text" } },
954+
{ "Key", new NestedModel { Integer = 1, String = "Text", } },
918955
{ "AnotherKey", new NestedModel { Integer = 2, String = "AnotherText" } }
919956
};
920957

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace MyTested.AspNetCore.Mvc.Test.Setups.Common
2+
{
3+
public enum CustomEnum
4+
{
5+
DefaultConstant,
6+
ConstantWithCustomValue = 128,
7+
CombinedConstant = DefaultConstant | ConstantWithCustomValue
8+
}
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
namespace MyTested.AspNetCore.Mvc.Test.Setups.Models
22
{
3+
using Common;
4+
35
public class NestedModel
46
{
57
public int Integer { get; set; }
68

79
public string String { get; set; }
810

11+
public CustomEnum Enum { get; set; }
12+
913
public NestedModel Nested { get; set; }
1014
}
1115
}

0 commit comments

Comments
 (0)