Пример #1
0
 private static bool ListsAreEqual(IList tested, IList expected, PropertiesComparisonOptions options,
                                   CheckingTrace trace)
 {
     if (expected != null)
     {
         if (tested == null)
         {
             trace.SetFailure($"Tested list is null but expected list {expected.ShortDisplay()} is not null");
             return(false);
         }
         if (tested.Count != expected.Count)
         {
             trace.SetFailure(
                 $"Tested list {tested.ShortDisplay()} has not same number of element that expected list {expected.ShortDisplay()}");
             return(false);
         }
         for (var i = 0; i < expected.Count; ++i)
         {
             trace.PushIndex(expected.GetType(), i);
             if (!ObjectsAreEqual(expected[i], tested[i], options, trace))
             {
                 return(false);
             }
             trace.Pop();
         }
         return(true);
     }
     if (tested != null)
     {
         trace.SetFailure($"Tested list {tested.ShortDisplay()} is not null but expecting null list");
         return(false);
     }
     return(true);
 }
Пример #2
0
        /// <summary>
        ///     Determines whether properties are initialized or not.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertiesContainer">The properties container.</param>
        /// <param name="trace">The trace.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        ///     propertiesContainer - Cannot check if properties are initialized on null
        ///     instance
        /// </exception>
        // ReSharper disable once ExcessiveIndentation
        public static bool PropertiesAreInitialized <T>(T propertiesContainer, CheckingTrace trace,
                                                        PropertiesComparisonOptions options)
        {
            if (propertiesContainer == null)
            {
                throw new ArgumentNullException(nameof(propertiesContainer),
                                                "Cannot check if properties are initialized on null instance");
            }
            var type       = propertiesContainer.GetType();
            var properties = ObjectComparer.SelectProperties(type.GetProperties(), options.ExcludedProperties);

            foreach (var propertyName in properties)
            {
                var property = type.GetProperty(propertyName);
                if (options.IgnoreReadOnlyProperties && !property.CanWrite)
                {
                    continue;
                }
                if (property.CanRead)
                {
                    // Indexed Properties are managed
                    if (property.GetIndexParameters().Length == 0)
                    {
                        var value = property.GetValue(propertiesContainer, null);
                        trace.PushProperty(type, property.PropertyType, propertyName);
                        if (!IsFilled(value, trace, options))
                        {
                            return(false);
                        }
                        trace.Pop();
                    }
                }
            }
            return(true);
        }
Пример #3
0
 private static bool PropertiesAreEqual(object expected, object tested, IEnumerable <string> properties,
                                        PropertiesComparisonOptions comparisonOptions, CheckingTrace trace)
 {
     foreach (var propertyName in properties)
     {
         if (propertyName == "SyncRoot")
         {
             continue;
         }
         var expectedType = expected.GetType();
         // get property infos
         var expectedProperty = expectedType.GetProperty(propertyName);
         var getter           = expectedProperty.GetGetMethod();
         if (getter == null || getter.IsStatic)
         {
             // no getter, or static
             continue;
         }
         var testedType     = tested.GetType();
         var testedProperty = testedType.GetProperty(expectedProperty.Name);
         if (testedProperty == null)
         {
             trace.SetFailure(
                 $"{testedType}:{tested} has not the expected property {expectedProperty.Name} of {expectedType}");
             return(false);
         }
         // compare values
         if (expectedProperty.CanRead && testedProperty.CanRead)
         {
             if (expectedProperty.GetIndexParameters().Length != 0)
             {
                 continue;
             }
             var expectedValue = expectedProperty.GetValue(expected, null);
             var testedValue   = testedProperty.GetValue(tested, null);
             trace.PushProperty(expectedType, expectedProperty.PropertyType, expectedProperty.Name);
             if (!ObjectsAreEqual(expectedValue, testedValue, comparisonOptions, trace))
             {
                 return(false);
             }
             trace.Pop();
         }
     }
     return(true);
 }