Пример #1
0
        //x.Equals(default) && y.Equals(default) (y/x are left on their default values)
        //though x.Equals(null) is better form, many fields and properties are set to seomthing besides null on construction and can never be null (short of reflection as we do below). So By testing
        //against the original value rather than null, we save ourselves a lot of boilerplate in the Equals method.
        public void Equal_SecondFieldIsNull_ReturnsFalse()
        {
            T itemToGetFieldsFrom = CreateNewCloneable();
            IEnumerable <FieldInfo> fieldInfos = GetAllFields(itemToGetFieldsFrom);

            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                T itemWithFieldToChange = CreateNewCloneable();
                T itemWithDefaultField  = CreateNewCloneable();
                Assert.That(itemWithFieldToChange, Is.EqualTo(itemWithDefaultField).Using <T>(Compare), "The two items were not equal on creation. You may need to override Equals(object other).");
                string fieldName = fieldInfo.Name;
                if (fieldInfo.Name.Contains("<"))
                {
                    var splitResult = fieldInfo.Name.Split(new[] { '<', '>' });
                    fieldName = splitResult[1];
                }
                if (ExceptionList.Contains("|" + fieldName + "|") || EqualsExceptionList.Contains("|" + fieldName + "|"))
                {
                    continue;
                }
                ValuesToSet valueToSet = null;
                try
                {
                    valueToSet = DefaultValuesForTypes.Single(dv => dv.TypeOfValueToSet == fieldInfo.FieldType);
                }
                catch (InvalidOperationException)
                {
                    Assert.Fail(
                        "Unhandled field type - please update the test to handle type \"{0}\". The field that uses this type is \"{1}\".",
                        fieldInfo.FieldType.Name, fieldName);
                }
                //This conditional is here in case the ValueToSet is identical to the fields default value.
                //That way developers who inherit this test case don't have to worry about what the fields default value is.
                //It also works around an issue with bool values. If you have two fields that are initialized with different
                //values (i.e. one is true and the other false) this will ensure that the value is chosen which is not equal
                //to the default value and the test therefor has a chance of succeeding.
                if (valueToSet.ValueToSet.Equals(fieldInfo.GetValue(itemWithDefaultField)))
                {
                    fieldInfo.SetValue(itemWithFieldToChange, valueToSet.NotEqualValueToSet);
                }
                else
                {
                    fieldInfo.SetValue(itemWithFieldToChange, valueToSet.ValueToSet);
                }
                Assert.AreNotEqual(itemWithFieldToChange, itemWithDefaultField, "Field \"{0}\" is not evaluated in Equals(T other) or the ValueToSet is equal to the fields default value. Please update Equals(T other) or add the field name to the ExceptionList or EqualsExceptionList property.", fieldName);
                Assert.AreNotEqual(itemWithDefaultField, itemWithFieldToChange, "Field \"{0}\" is not evaluated in Equals(T other) or the ValueToSet is equal to the fields default value. Please update Equals(T other) or add the field name to the ExceptionList or EqualsExceptionList property.", fieldName);
            }
        }
Пример #2
0
        //x.Equals(y) && y.Equals(x) where x==y
        public void Equal_ItemsAreEqual_ReturnsTrue()
        {
            T itemToGetFieldsFrom = CreateNewCloneable();
            IEnumerable <FieldInfo> fieldInfos = GetAllFields(itemToGetFieldsFrom);

            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                T item        = CreateNewCloneable();
                T unequalItem = CreateNewCloneable();
                Assert.That(item, Is.EqualTo(unequalItem).Using <T>(Compare), "The two items were not equal on creation. You may need to override Equals(object other).");
                var fieldName = fieldInfo.Name;
                if (fieldInfo.Name.Contains("<"))
                {
                    var splitResult = fieldInfo.Name.Split(new[] { '<', '>' });
                    fieldName = splitResult[1];
                }
                if (ExceptionList.Contains("|" + fieldName + "|") || EqualsExceptionList.Contains("|" + fieldName + "|"))
                {
                    continue;
                }
                ValuesToSet valueToSet = null;
                try
                {
                    valueToSet = DefaultValuesForTypes.Single(dv => dv.TypeOfValueToSet == fieldInfo.FieldType);
                }
                catch (InvalidOperationException)
                {
                    Assert.Fail(
                        "Unhandled field type - please update the test to handle type \"{0}\". The field that uses this type is \"{1}\".",
                        fieldInfo.FieldType.Name, fieldName);
                }
                fieldInfo.SetValue(item, valueToSet.ValueToSet);
                fieldInfo.SetValue(unequalItem, valueToSet.ValueToSet);
                Assert.That(item, Is.EqualTo(unequalItem).Using <T>(Compare), "Field \"{0}\" is not evaluated in Equals(T other). Please update Equals(T other) or add the field name to the ExceptionList or EqualsExceptionList property.", fieldName);
                Assert.That(unequalItem, Is.EqualTo(item).Using <T>(Compare), "Field \"{0}\" is not evaluated in Equals(T other). Please update Equals(T other) or add the field name to the ExceptionList or EqualsExceptionList property.", fieldName);
            }
        }