Ejemplo n.º 1
0
        /// <summary>
        /// Check if two collections of <see typeparamref="T" /> are equal.
        /// </summary>
        /// <remarks>
        /// The parameters are first checked for null, an exception is thrown if only one is null.
        /// Second, the cardinalities of the collections are checked if the <see cref="IEnumerable{T}" /> is
        /// also <see cref="ICollection{T}" /> which means that it supports <see cref="ICollection{T}.Count" />
        /// If these checks are passed, each item is compared in turn.
        /// </remarks>
        /// <param name="checker"></param>
        /// <param name="expected"></param>
        /// <param name="candidate"></param>
        /// <param name="objectName"></param>
        protected void Check(IChecker checker, IEnumerable expected, IEnumerable candidate, string objectName)
        {
            // Do we have two actual lists
            if (CheckNullNotNull(expected, candidate, objectName))
            {
                return;
            }

            CheckCardinality(expected, candidate, objectName);
            if (this.CompareTarget == CompareTarget.Count)
            {
                // We're done
                return;
            }

            // Ok, step both iterator togeter, will work as these are now confirmed to have the same cardinality
            var  i             = 0;
            var  enumExpected  = expected.GetEnumerator();
            var  enumCandidate = candidate.GetEnumerator();
            var  target        = CompareTarget.Entity;
            Type type          = null;

            enumExpected.Reset();
            enumCandidate.Reset();
            while (enumExpected.MoveNext())
            {
                if (type == null)
                {
                    type = enumExpected.Current.GetType();
                    if (Targeter == null)
                    {
                        throw new NotSupportedException("No ITypeCompareTargeter assigned to PropertyCheck");
                    }
                    target = Targeter.DetermineCompareTarget(type);
                }
                enumCandidate.MoveNext();
                this.Check(target, checker, enumExpected.Current, enumCandidate.Current, objectName + "[" + i++ + "]");
            }
        }