Example #1
0
        public void Equal_FalseOnDifferentObjectsWithDifferentValues()
        {
            Guid sharedGuid = Guid.NewGuid();

            Assert.False(MemberComparer.Equal(new { PropertyA = "B", Integer = 23, Guid = sharedGuid },
                                              new { PropertyA = "A", Integer = 23, Guid = sharedGuid }));
        }
Example #2
0
        public void Equal_TrueOnAnonymousObjectsWithNestedObjects()
        {
            var sub1 = new { PropertyB = "b1" };
            var sub2 = new { PropertyB = "b1" };

            Assert.True(MemberComparer.Equal(sub1, sub2));
        }
        /// <summary>
        /// Checks whether two given structural types are equal.
        /// </summary>
        /// <param name="x">The left type.</param>
        /// <param name="y">The right type.</param>
        /// <returns>true if the given types are equal, false otherwise.</returns>
        protected virtual bool EqualsStructural(Type x, Type y)
        {
            if (TypeMap.TryGetValue(x, out Type res))
            {
                return(res.Equals(y));
            }
            else
            {
                TypeMap[x] = y;
            }

            var xMembers = x.GetMembers();
            var yMembers = y.GetMembers();

            if (xMembers.Length != yMembers.Length)
            {
                TypeMap.Remove(x);
                return(false);
            }

            foreach (var xMember in xMembers)
            {
                if (MemberComparer.ResolveMember(y, xMember) == null)
                {
                    TypeMap.Remove(x);
                    return(false);
                }
            }

            return(true);
        }
Example #4
0
        public void Interface()
        {
            const string keys = nameof(IDictionary.Keys), values = nameof(IDictionary.Values);
            var          definition = typeof(IDictionary <,>).GetRuntimeProperty(keys);

            var sut = new MemberComparer(ImplementedTypeComparer.Default);

            var constructed = typeof(IDictionary <string, int>);

            Assert.Equal(definition, constructed.GetRuntimeProperty(keys), sut);
            Assert.NotEqual(definition, constructed.GetRuntimeProperty(values), sut);

            var implementation = typeof(Dictionary <string, int>);

            Assert.Equal(definition, implementation.GetRuntimeProperty(keys), sut);
            Assert.NotEqual(definition, implementation.GetRuntimeProperty(values), sut);

            var not = typeof(NotDictionary <,>);

            Assert.NotEqual(definition, not.GetRuntimeProperty(keys), sut);
            Assert.NotEqual(definition, not.GetRuntimeProperty(values), sut);

            var notConstructed = typeof(NotDictionary <string, int>);

            Assert.NotEqual(definition, notConstructed.GetRuntimeProperty(keys), sut);
            Assert.NotEqual(definition, notConstructed.GetRuntimeProperty(values), sut);
        }
Example #5
0
        public void Explicit()
        {
            const string keys = nameof(IDictionary.Keys), values = nameof(IDictionary.Values);
            var          definition = typeof(Dictionary <string, object>).GetRuntimeProperty(keys);

            var sut = new MemberComparer(TypeIdentityComparer.Default);

            var other = typeof(Dictionary <string, int>);

            Assert.NotEqual(definition, other.GetRuntimeProperty(keys), sut);
            Assert.NotEqual(definition, other.GetRuntimeProperty(values), sut);

            var implementation = typeof(Dictionary <string, object>);

            Assert.Equal(definition, implementation.GetRuntimeProperty(keys), sut);
            Assert.NotEqual(definition, implementation.GetRuntimeProperty(values), sut);

            var not = typeof(NotDictionary <,>);

            Assert.NotEqual(definition, not.GetRuntimeProperty(keys), sut);
            Assert.NotEqual(definition, not.GetRuntimeProperty(values), sut);

            var notConstructed = typeof(NotDictionary <string, int>);

            Assert.NotEqual(definition, notConstructed.GetRuntimeProperty(keys), sut);
            Assert.NotEqual(definition, notConstructed.GetRuntimeProperty(values), sut);
        }
Example #6
0
        public void Equal_TrueOnMatchedDictionaryObjectInstances()
        {
            DictionaryObj dobj1 = new DictionaryObj {
                ObjectDictionary = new Dictionary <int, ClassWithFieldsAndProperties>
                {
                    { 1, new ClassWithFieldsAndProperties()
                      {
                          Foo = "one", Bar = "two"
                      } }, { 2, new ClassWithFieldsAndProperties()
                             {
                                 Foo = "three", Bar = "four"
                             } }
                }
            };
            DictionaryObj dobj2 = new DictionaryObj {
                ObjectDictionary = new Dictionary <int, ClassWithFieldsAndProperties>
                {
                    { 1, new ClassWithFieldsAndProperties()
                      {
                          Foo = "one", Bar = "two"
                      } }, { 2, new ClassWithFieldsAndProperties()
                             {
                                 Foo = "three", Bar = "four"
                             } }
                }
            };

            Assert.True(MemberComparer.Equal(dobj1, dobj2));
        }
Example #7
0
        public void Equal_FalseOnDatesDifferingByLessThanASecond()
        {
            DateTime one = DateTime.Parse("07:27:15.01"),
                     two = DateTime.Parse("07:27:15.49");

            Assert.False(MemberComparer.Equal(one, two));
        }
Example #8
0
        public void Equal_TrueToSecondOnDatesDifferingByLessThanASecondWithCustomComparer()
        {
            DateTime one = DateTime.Parse("07:27:15.01"),
                     two = DateTime.Parse("07:27:15.49");

            Assert.True(MemberComparer.Equal(one, two, new[] { new DateComparer(DateComparisonType.TruncatedToSecond) }));
        }
Example #9
0
        public void Equal_FalseOnAnonymousObjectsWithNestedObjectsWithDifferentValues()
        {
            var sub1 = new { PropertyB = "b1" };
            var sub2 = new { PropertyB = "b2" };

            Assert.False(MemberComparer.Equal(sub1, sub2));
        }
Example #10
0
        public void Equal_TrueOnExceptionsWithinSameScopeOfSameType()
        {
            var exception  = new ArgumentNullException("foo");
            var exception2 = new ArgumentNullException("foo");

            Assert.True(MemberComparer.Equal(exception, exception2));
        }
Example #11
0
        public void Equal_TrueOnObjectsWithNestedObjects()
        {
            var sub1 = new { PropertyB = "b1" };
            var sub2 = new { PropertyB = "b1" };

            Assert.True(MemberComparer.Equal(new { PropertyA = "A", Integer = 23, Sub = sub1 },
                                             new { PropertyA = "A", Integer = 23, Sub = sub2 }));
        }
Example #12
0
        public void Equal_TrueOnDifferentObjectsWithSameValues()
        {
            Guid     sharedGuid = Guid.NewGuid();
            DateTime now        = DateTime.Now;

            Assert.True(MemberComparer.Equal(new { PropertyA = "A", Integer = 23, Guid = sharedGuid, Date = now },
                                             new { PropertyA = "A", Integer = 23, Guid = sharedGuid, Date = now }));
        }
Example #13
0
        public void Equal_FalseOnObjectsWithNestedObjectsWithDifferentValues()
        {
            var sub1 = new { PropertyB = "b1" };
            var sub2 = new { PropertyB = "b2" };

            Assert.False(MemberComparer.Equal(new { PropertyA = "A", Integer = 23, Sub = sub1 },
                                              new { PropertyA = "A", Integer = 23, Sub = sub2 }));
        }
Example #14
0
        public void Equal_TrueOnDoubleNestedObjectsWithSameValues()
        {
            //we've nested two levels deep
            var sub2 = new { PropertyC = "b2" };
            var sub1 = new { PropertyB = "b1", Sub = sub2 };

            Assert.True(MemberComparer.Equal(new { PropertyA = "A", Integer = 23, Sub = sub1 },
                                             new { PropertyA = "A", Integer = 23, Sub = sub1 }));
        }
 public void SutIsMemberComparer()
 {
     // Fixture setup
     var dummyComparer = new DelegatingEqualityComparer();
     var sut = new MemberComparer(dummyComparer);
     // Exercise system and verify outcome
     Assert.IsAssignableFrom<IMemberComparer>(sut);
     // Teardown
 }
Example #16
0
 public void Equal_ScopesComparisonToSpecifiedType()
 {
     Assert.True(MemberComparer.Equal <A>(new B()
     {
         Integer = 4, String = "Foo"
     }, new B()
     {
         Integer = 4, String = "Bar"
     }));
 }
Example #17
0
        public void Equal_TrueToSecondOnNestedDatesDifferingByLessThanASecondWithCustomComparer()
        {
            DateTime one = DateTime.Parse("07:27:15.01"),
                     two = DateTime.Parse("07:27:15.49");

            var a = new { Foo = 5, Bar = new { Now = one } };
            var b = new { Foo = 5, Bar = new { Now = two } };

            Assert.True(MemberComparer.Equal(one, two, new[] { new DateComparer(DateComparisonType.TruncatedToSecond) }));
        }
        public void SutIsMemberComparer()
        {
            // Fixture setup
            var dummyComparer = new DelegatingEqualityComparer();
            var sut           = new MemberComparer(dummyComparer);

            // Exercise system and verify outcome
            Assert.IsAssignableFrom <IMemberComparer>(sut);
            // Teardown
        }
Example #19
0
        private static void _SortBy
        (
            EnumMemberInfo[] members,
            SortBy sortBy
        )
        {
            MemberComparer comparer = new MemberComparer(sortBy);

            Array.Sort(members, comparer);
        }
Example #20
0
 public void Equal_TrueOnInterfaces()
 {
     Assert.True(MemberComparer.Equal <IFoo>(new Foo()
     {
         Integer = 5
     }, new Foo()
     {
         Integer = 5
     }));
 }
Example #21
0
        public void Equal_FalseOnObjectsWithOneNullNestedObjectAndOneNonNullNestedObject()
        {
            var sub1 = new { PropertyB = "b1" };
            var sub2 = new { PropertyB = "b2" };

            sub2 = null;

            Assert.False(MemberComparer.Equal(new { PropertyA = "A", Integer = 23, Sub = sub1 },
                                              new { PropertyA = "A", Integer = 23, Sub = sub2 }));
        }
 public void ComparerIsCorrect()
 {
     // Fixture setup
     var expected = new DelegatingEqualityComparer();
     var sut = new MemberComparer(expected);
     // Exercise system
     var result = sut.Comparer;
     // Verify outcome
     Assert.Equal(expected, result);
     // Teardown
 }
Example #23
0
 public void CompareProperties_SameNameDifferentAccessors_Equal()
 {
     // Arrange
     var type = Substitute.For<ITypeSymbol>();
     var property = new Property("p1", type, true, true);
     var otherProperty = new Property("p1", type, true, false);
     var comparer = new MemberComparer();
     // Act
     var equal = comparer.IsSameAs(property, otherProperty);
     // Assert
     Assert.IsTrue(equal);
 }
Example #24
0
        public void Equal_FalseOnClassWithMismatchFieldValues()
        {
            string Bar = "bar";

            Assert.False(MemberComparer.Equal(new ClassWithFieldsAndProperties()
            {
                Foo = "456", Bar = Bar
            }, new ClassWithFieldsAndProperties()
            {
                Foo = "4567", Bar = Bar
            }));
        }
Example #25
0
        public void Equal_TrueOnClasswithPropertiesAndFields()
        {
            string Bar = "123", Foo = "456";

            Assert.True(MemberComparer.Equal(new ClassWithFieldsAndProperties()
            {
                Bar = Bar, Foo = Foo
            }, new ClassWithFieldsAndProperties()
            {
                Bar = Bar, Foo = Foo
            }));
        }
Example #26
0
        public void Equal_TrueOnNullAnonymousObjects()
        {
            //anonymous types that look the same like these actually share a static type (as constructed by the compiler)
            var sub1 = new { PropertyB = "b1" };

            sub1 = null;
            var sub2 = new { PropertyB = "b1" };

            sub2 = null;

            Assert.True(MemberComparer.Equal(sub1, sub2));
        }
Example #27
0
        public void Equal_TrueOnNestedRefToSameException()
        {
            var exception = new ArgumentNullException("foo");

            Assert.True(MemberComparer.Equal(new ExceptionHolder()
            {
                Exception = exception
            }, new ExceptionHolder()
            {
                Exception = exception
            }));
        }
        public void ComparerIsCorrect()
        {
            // Fixture setup
            var expected = new DelegatingEqualityComparer();
            var sut      = new MemberComparer(expected);
            // Exercise system
            var result = sut.Comparer;

            // Verify outcome
            Assert.Equal(expected, result);
            // Teardown
        }
Example #29
0
        public void Equal_FalseOnObjectsWithNestedCollections()
        {
            var nestedCollection1 = new { Property = "value", NestedProperties = new List <string>()
                                          {
                                              "a", "b"
                                          } };
            var nestedCollection2 = new { Property = "value", NestedProperties = new List <string>()
                                          {
                                              "b", "a"
                                          } };

            Assert.False(MemberComparer.Equal(nestedCollection1, nestedCollection2));
        }
Example #30
0
        public void Equal_TrueOnObjectsWithNullNestedObjects()
        {
            //anonymous types that look the same like these actually share a static type (as constructed by the compiler)
            var sub1 = new { PropertyB = "b1" };

            sub1 = null;
            var sub2 = new { PropertyB = "b1" };

            sub2 = null;

            Assert.True(MemberComparer.Equal(new { PropertyA = "A", Integer = 23, Sub = sub1 },
                                             new { PropertyA = "A", Integer = 23, Sub = sub2 }));
        }
Example #31
0
        public void Equal_IgnoresStatics()
        {
            var a = new ClassWithStatics()
            {
                Value = "Foo"
            };
            var b = new ClassWithStatics()
            {
                Value = "Foo"
            };

            Assert.True(MemberComparer.Equal(a, b));
        }
Example #32
0
        public void Equal_TrueOnClassWithMismatchedPropertiesAndFieldsWithCustomComparer()
        {
            string Bar = "bar";

            Assert.True(MemberComparer.Equal(new ClassWithFieldsAndProperties()
            {
                Foo = "456", Bar = Bar
            }, new ClassWithFieldsAndProperties()
            {
                Foo = "4567", Bar = Bar
            },
                                             new[] { new GenericEqualityComparer <ClassWithFieldsAndProperties>((a, b) => a.Bar == b.Bar) }));
        }
        public void IsSatisfiedByWithDefaultSpecificationForFieldReturnsCorrectResult()
        {
            // Fixture setup
            var field         = typeof(FieldHolder <int>).GetProperty("Field");
            var dummyComparer = new DelegatingEqualityComparer();
            var sut           = new MemberComparer(dummyComparer);
            // Exercise system
            var result = sut.IsSatisfiedBy(field);

            // Verify outcome
            Assert.True(result);
            // Teardown
        }
 public void GetHashCodeForwardsCorrectCallToComparer(
     object obj,
     int expected)
 {
     // Fixture setup
     var comparerStub = new DelegatingEqualityComparer
     {
         OnGetHashCode = x => x == obj ? expected : 0
     };
     var sut = new MemberComparer(comparerStub);
     // Exercise system
     var result = sut.GetHashCode(obj);
     // Verify outcome
     Assert.Equal(expected, result);
     // Teardown
 }
 public void EqualsForwardsCorrectCallToComparer(
     object a,
     object b,
     bool expected)
 {
     // Fixture setup
     var comparerStub = new DelegatingEqualityComparer
     {
         OnEquals = (x, y) => x.Equals(y)
     };
     var sut = new MemberComparer(comparerStub);
     // Exercise system
     var result = sut.Equals(a, b);
     // Verify outcome
     Assert.Equal(expected, result);
     // Teardown
 }
Example #36
0
        public void CompareProperties_SameNameDifferentTypes_Equal()
        {
            // Arrange
            var typeOfProperty = Substitute.For<ITypeSymbol>();
            var typeOfOtherProperty = Substitute.For<ITypeSymbol>();
            var property = new Property("p1", typeOfProperty, true, true);
            var otherProperty = new Property("p1", typeOfOtherProperty, true, true);
            var comparer = new MemberComparer();

            // Act
            var equal = comparer.IsSameAs(property, otherProperty);

            // First ensure that both types are different
            Assert.AreNotEqual(typeOfProperty, typeOfOtherProperty);
            // now ensure that the properties are the same even with different types
            Assert.IsTrue(equal);
        }
        public void PropertySpecificationIsCorrect()
        {
            // Fixture setup
            var dummyComparer = new DelegatingEqualityComparer();
            var expected = new DelegatingSpecification<PropertyInfo>();
            var dummySpecification = new DelegatingSpecification<FieldInfo>();

            var sut = new MemberComparer(
                dummyComparer,
                expected,
                dummySpecification);
            // Exercise system
            var result = sut.PropertySpecification;
            // Verify outcome
            Assert.Equal(expected, result);
            // Teardown
        }
Example #38
0
		protected MemberCollection (Type type, InfoQuery query, MemberComparer comparer, BindingFlags bindings)
		{
			if (query == null)
				throw new NullReferenceException ("Invalid query delegate.");

			if (comparer == null)
				throw new NullReferenceException ("Invalid comparer.");

			this.comparer = comparer;
			this.bindings = bindings;

			this.list = new SortedList ();

			MemberInfo [] data = query (type, bindings);
			foreach (MemberInfo info in data) {
				this.list [info.Name] = info;
			}
		}
Example #39
0
 private void LoadItems()
 {
     int num;
     BindingFlags bindingAttr = BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
     IComparer comparer = new MemberComparer();
     if (!this._type.IsInterface)
     {
         this._fields = new ArrayList();
         if (!this._type.IsEnum)
         {
             this._constructors = new ArrayList();
         }
     }
     if (!this._type.IsEnum)
     {
         this._properties = new ArrayList();
         this._events = new ArrayList();
         this._methods = new ArrayList();
     }
     if (this._fields != null)
     {
         FieldInfo[] fields = this._type.GetFields(bindingAttr);
         if ((fields != null) && ((num = fields.Length) != 0))
         {
             Array.Sort(fields, comparer);
             for (int i = 0; i < num; i++)
             {
                 FieldInfo fi = fields[i];
                 if (!fi.IsSpecialName)
                 {
                     this._fields.Add(new FieldItem(this._owner, fi));
                 }
             }
         }
     }
     if (this._constructors != null)
     {
         ConstructorInfo[] constructors = this._type.GetConstructors(bindingAttr);
         if ((constructors != null) && ((num = constructors.Length) != 0))
         {
             for (int j = 0; j < num; j++)
             {
                 ConstructorInfo method = constructors[j];
                 this._constructors.Add(new MethodItem(this._owner, method));
             }
         }
     }
     if (this._properties != null)
     {
         PropertyInfo[] properties = this._type.GetProperties(bindingAttr);
         if ((properties != null) && ((num = properties.Length) != 0))
         {
             Array.Sort(properties, comparer);
             for (int k = 0; k < num; k++)
             {
                 try
                 {
                     PropertyInfo pi = properties[k];
                     MethodInfo getMethod = pi.GetGetMethod(true);
                     MethodInfo setMethod = pi.GetSetMethod(true);
                     MethodInfo underlyingMethod = (getMethod != null) ? getMethod : setMethod;
                     if (underlyingMethod != null)
                     {
                         this._properties.Add(new PropertyItem(this._owner, pi, underlyingMethod));
                     }
                 }
                 catch
                 {
                 }
             }
         }
     }
     if (this._events != null)
     {
         EventInfo[] events = this._type.GetEvents(bindingAttr);
         if ((events != null) && ((num = events.Length) != 0))
         {
             Array.Sort(events, comparer);
             for (int m = 0; m < num; m++)
             {
                 try
                 {
                     EventInfo ei = events[m];
                     MethodInfo addMethod = ei.GetAddMethod(true);
                     MethodInfo removeMethod = ei.GetRemoveMethod(true);
                     MethodInfo info10 = (addMethod != null) ? addMethod : removeMethod;
                     if (info10 != null)
                     {
                         this._events.Add(new EventItem(this._owner, ei, info10));
                     }
                 }
                 catch
                 {
                 }
             }
         }
     }
     if (this._methods != null)
     {
         MethodInfo[] methods = this._type.GetMethods(bindingAttr);
         if ((methods != null) && ((num = methods.Length) != 0))
         {
             Array.Sort(methods, comparer);
             for (int n = 0; n < num; n++)
             {
                 try
                 {
                     MethodInfo info11 = methods[n];
                     if (!info11.IsSpecialName)
                     {
                         this._methods.Add(new MethodItem(this._owner, info11));
                     }
                 }
                 catch
                 {
                 }
             }
         }
     }
 }
        public void IsSatisfiedByForFieldReturnsCorrectResult(bool expected)
        {
            // Fixture setup
            var field = typeof(FieldHolder<int>).GetField("Field");
            var dummyComparer = new DelegatingEqualityComparer();
            var dummySpecification = new DelegatingSpecification<PropertyInfo>();

            var fieldSpecificationStub =
                new DelegatingSpecification<FieldInfo>
                {
                    OnIsSatisfiedBy = x => expected
                };

            var sut = new MemberComparer(
                dummyComparer,
                dummySpecification,
                fieldSpecificationStub);
            // Exercise system
            var result = sut.IsSatisfiedBy(field);
            // Verify outcome
            Assert.Equal(expected, result);
            // Teardown
        }
 public void IsSatisfiedByWithDefaultSpecificationForFieldReturnsCorrectResult()
 {
     // Fixture setup
     var field = typeof(FieldHolder<int>).GetProperty("Field");
     var dummyComparer = new DelegatingEqualityComparer();
     var sut = new MemberComparer(dummyComparer);
     // Exercise system
     var result = sut.IsSatisfiedBy(field);
     // Verify outcome
     Assert.True(result);
     // Teardown
 }