Ejemplo n.º 1
0
        /// <summary>
        /// Returns the expression that compares two IEnumerable members
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="engine"></param>
        /// <param name="configuration"></param>
        /// <param name="memberType"></param>
        /// <returns></returns>
        private static Expression GetIEnumerableMemberExpression(Context ctx, IComparerEngine engine, EnumerableConfiguration configuration, Type memberType)
        {
            var nullChecked = new NullChecked(ctx, memberType);

            if (configuration != null && !string.IsNullOrEmpty(configuration.Match))
            {
                var itemType = memberType.IsArray ? memberType.GetElementType() : GetGenericIEnumerableType(memberType).First();
                var types    = new[] { itemType, configuration.MatcherType };

                // Static call to CollectionComparer.CompareIEnumerableWithKeyAndDefault<T, TKey> to compare IEnumerable properties
                return(Expression.Call(ctx.List,
                                       _listAddRange,
                                       Expression.Call(CollectionComparer.GetCompareIEnumerableWithKeyAndDefaultMethodInfo(types),
                                                       Expression.Constant(engine),
                                                       Expression.Constant(ctx.Name),
                                                       nullChecked.PropA,
                                                       nullChecked.PropB,
                                                       configuration.Matcher,
                                                       Expression.Convert(
                                                           Expression.Constant(configuration.DefaultId),
                                                           configuration.MatcherType))));
            }
            // Static call to CollectionComparer.CompareIEnumerable<T> to compare IEnumerable properties
            return(Expression.Call(ctx.List,
                                   _listAddRange,
                                   Expression.Call(CollectionComparer.GetCompareIEnumerableMethodInfo(GetGenericIEnumerableType(memberType)),
                                                   Expression.Constant(ctx.Name), nullChecked.PropA, nullChecked.PropB)));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Compares two IDictionary by doing a deep object comparison and returns the list of
        /// updated/added/removed values from the dictionary
        /// </summary>
        /// <typeparam name="TKey">Type of the IDictionary key</typeparam>
        /// <typeparam name="TValue">Type of the IDictionary value</typeparam>
        /// <param name="engine"></param>
        /// <param name="name">Name of the member</param>
        /// <param name="oldModel">The dictionary of values from the old model</param>
        /// <param name="newModel">The dictionary of updated values from the new model</param>
        /// <returns>A list of values that have been updated, added or removed from the dictionary</returns>
        public static IEnumerable <Difference> DeepCompareIDictionary <TKey, TValue>(IComparerEngine engine, string name, IDictionary <TKey, TValue> oldModel, IDictionary <TKey, TValue> newModel) where TValue : class
        {
            var comparer = engine.Get <TValue>();

            var oldHash = new HashSet <TKey>(oldModel.EmptyIfNull().Keys);
            var newHash = new HashSet <TKey>(newModel.EmptyIfNull().Keys);

            foreach (var key in oldHash.Intersect(newHash))
            {
                foreach (var update in comparer(oldModel[key], newModel[key]))
                {
                    update.Name = $"{name}.{key}.{update.Name}";
                    yield return(update);
                }
            }

            foreach (var key in newHash.Except(oldHash))
            {
                foreach (var update in comparer(null, newModel[key]))
                {
                    update.Name     = $"{name}.{key}.{update.Name}";
                    update.OldValue = null;
                    yield return(update);
                }
            }

            foreach (var key in oldHash.Except(newHash))
            {
                foreach (var update in comparer(oldModel[key], null))
                {
                    update.Name     = $"{name}.{key}.{update.Name}";
                    update.NewValue = null;
                    yield return(update);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="engine"></param>
        /// <param name="name"></param>
        /// <param name="oldModel"></param>
        /// <param name="newModel"></param>
        /// <param name="selector"></param>
        /// <param name="defaultKey"></param>
        /// <returns></returns>
        public static IEnumerable <Difference> CompareIEnumerableWithKeyAndDefault <T, TKey>(IComparerEngine engine, string name, IEnumerable <T> oldModel, IEnumerable <T> newModel, Func <T, TKey> selector, TKey defaultKey) where T : class
        {
            var changes  = new List <Difference>();
            var comparer = engine.Get <T>();

            var oldList = oldModel.EmptyIfNull().ToList();
            var newList = newModel.EmptyIfNull().ToList();

            var oldModelWithDefaultKey = oldList.Where(x => selector(x).Equals(defaultKey)).ToList();
            var newModelWithDefaultKey = newList.Where(x => selector(x).Equals(defaultKey)).ToList();
            var oldDict = oldList.Except(oldModelWithDefaultKey).ToDictionary(selector);
            var newDict = newList.Except(newModelWithDefaultKey).ToDictionary(selector);

            changes.AddRange(DeepCompareIDictionary(engine, name, oldDict, newDict));
            var counter = 1;
            Func <Difference, int, Difference> updateName = (x, y) =>
            {
                var id = $"{{New {y}}}";
                x.Name = $"{name}.{id}.{x.Name}";
                return(x);
            };

            foreach (var model in newModelWithDefaultKey)
            {
                changes.AddRange(comparer(null, model).Select(y => updateName(y, counter)));
                counter++;
            }
            return(changes);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns the expression that compares two IDictionary members
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="engine"></param>
        /// <param name="memberType"></param>
        /// <returns></returns>
        private static MethodCallExpression GetIDictionaryMemberExpression(Context ctx, IComparerEngine engine, Type memberType)
        {
            var nullChecked = new NullChecked(ctx, memberType);

            var genericPropTypes = memberType.GetGenericArguments();

            var methodInfo = CollectionComparer.GetCompareIDictionaryMethodInfo(genericPropTypes);

            if (IsSimpleType(genericPropTypes[1]))
            {
                return(Expression.Call(ctx.List,
                                       _listAddRange,
                                       Expression.Call(
                                           methodInfo,
                                           Expression.Constant(ctx.Name),
                                           nullChecked.PropA,
                                           nullChecked.PropB)));
            }
            return(Expression.Call(ctx.List,
                                   _listAddRange,
                                   Expression.Call(
                                       methodInfo,
                                       Expression.Constant(engine),
                                       Expression.Constant(ctx.Name),
                                       nullChecked.PropA,
                                       nullChecked.PropB)));
        }
 public void InitializeSut()
 {
     SutEngine = new Engine();
 }
Ejemplo n.º 6
0
 public ComparerConfiguration(IComparerEngine engine)
 {
     Engine       = engine;
     IgnoredTypes = new List <Type>();
 }
Ejemplo n.º 7
0
 public void InitializeSut()
 {
     SutEngine = new Engine();
 }
 public ComparerConfiguration(IComparerEngine engine)
 {
     Engine = engine;
 }