public void FieldExpressions_GetKey_Exceptions()
        {
            FieldExpressions expressions = new FieldExpressions();

            Assert.ThrowsException <ArgumentNullException>(() => expressions.GetKey(null));
            Assert.ThrowsException <KeyNotFoundException>(() => expressions.GetKey(new FieldExpression(source, "Table", "Field")));
        }
        public void FieldExpressions_GetEnumerator_Empty()
        {
            // Prepare test data
            FieldExpressions expressions = new FieldExpressions();

            // Check test result
            Assert.AreEqual(0, expressions.Count());
            foreach (FieldExpression field in expressions)
            {
                Assert.Fail();
            }
        }
Esempio n. 3
0
        private ISetRuleBuilder <TDestination, TSource> RegisterFieldExpression(string destination, Expression expression)
        {
            if (FieldExpressions.ContainsKey(destination))
            {
                FieldExpressions[destination] = expression;
            }
            else
            {
                FieldExpressions.Add(destination, expression);
            }

            return(this);
        }
Esempio n. 4
0
        /// <summary>
        /// Throws an exception when the field in question is null.
        /// </summary>
        /// <typeparam name="T">The runtime type of the field.</typeparam>
        /// <param name="predicate">An expression of type <see cref="Func{T}"/> which returns a field.</param>
        /// <exception cref="ArgumentNullException">Thrown when the field is null.</exception>
        public static void IfNull <T>(Expression <Func <T> > predicate)
        {
            FieldInfo fieldInfo = FieldExpressions.GetField(predicate);


            object value = fieldInfo
                           .GetValue(((ConstantExpression)((MemberExpression)predicate.Body).Expression).Value);

            if (value == null)
            {
                throw new ArgumentNullException(fieldInfo.Name);
            }
        }
        public void FieldExpressions_Add_SameKey()
        {
            // Prepare test data
            FieldExpressions expressions = new FieldExpressions();
            FieldExpression  expression  = new FieldExpression(source, "Table", "Field");

            // Perform the test operation
            string a = expressions.Add(expression);
            string b = expressions.Add(expression);

            // Check test result
            Assert.AreEqual(a, b);
        }
Esempio n. 6
0
        /// <summary>
        /// Create mapping rule for properties to be skipped in destination type
        /// </summary>
        /// <param name="ignoreAtDestinationExpression">Expression for property to be skipped in destination type</param>
        /// <returns>Current instance of WaylessMap</returns>
        public ISetRuleBuilder <TDestination, TSource> FieldSkip(Expression <Func <TDestination, object> > skipperName)
        {
            var ignore = GetMemberName(skipperName);

            if (!FieldSkips.Contains(ignore))
            {
                IsFinalized = false;
                FieldSkips.Add(ignore);
            }

            if (FieldExpressions.ContainsKey(ignore))
            {
                FieldExpressions.Remove(ignore);
            }

            return(this);
        }
        public void FieldExpressions_GetEnumerator_Populated()
        {
            // Prepare test data
            string[]         fields      = new string[] { "FieldA", "FieldB" };
            FieldExpressions expressions = new FieldExpressions(source, "Table", fields);

            // Check test result
            Assert.AreEqual(fields.Length, expressions.Count());
            int next = 0;

            foreach (FieldExpression field in expressions)
            {
                Assert.AreEqual("Table", field.TableName);
                Assert.IsTrue(fields.Any(x => x == field.FieldName));
                Assert.AreEqual($"f{next}", expressions.GetKey(field));
                next++;
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Performs call to MatchMaker if AutoMatchMembres is True
        /// </summary>
        public void FinalizeRules()
        {
            if (AutoMatchMembers)
            {
                if (MatchMaker == null)
                {
                    throw new NullReferenceException(nameof(MatchMaker));
                }

                var unmappedDestinations = DestinationFields.Where(x => !FieldExpressions.Keys.Contains(x.Key) &&
                                                                   !FieldSkips.Contains(x.Key))
                                           .Select(x => x.Value)
                                           .ToList();

                var matchedPairs = MatchMaker.FindMemberPairs(unmappedDestinations, SourceFields.Values);
                foreach (var pair in matchedPairs)
                {
                    var expression = ExpressionBuilder.GetMapExpression <TSource>(pair.DestinationMember, pair.SourceMember);
                    FieldExpressions.Add(pair.DestinationMember.Name, expression);
                }
            }

            IsFinalized = true;
        }
Esempio n. 9
0
        /// <summary>
        /// Throws an exception when the collection based field in question is null or empty.
        /// </summary>
        /// <typeparam name="T">The runtime type of the field.</typeparam>
        /// <param name="predicate">An expression of type <see cref="Func{T}" /> which returns a field./></param>
        // TODO: document the exceptions thrown
        public static void IfNullOrEmpty <T>(Expression <Func <T> > predicate)
        {
            FieldInfo fieldInfo = FieldExpressions.GetField(predicate);

            Do.If(!typeof(IEnumerable).IsAssignableFrom(fieldInfo.FieldType), () => {
                throw new InvalidOperationException(string.Format("Field {0} is not an IEnumerable.",
                                                                  fieldInfo.Name));
            });


            object value = fieldInfo
                           .GetValue(((ConstantExpression)((MemberExpression)predicate.Body).Expression).Value);

            if (value == null)
            {
                throw new ArgumentNullException(fieldInfo.Name);
            }

            IEnumerable enumerable = (IEnumerable)value;

            IEnumerator enumerator = enumerable.GetEnumerator();

            enumerator.Reset();

            int i = 0;

            while (enumerator.MoveNext() && i == 0)
            {
                i++;
            }

            if (i == 0)
            {
                throw new InvalidOperationException(string.Format("Field {0} should not be empty.", fieldInfo.Name));
            }
        }
        public void FieldExpressions_Add_Exception()
        {
            FieldExpressions expressions = new FieldExpressions();

            expressions.Add(null);
        }