public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            if (!IsCollection(comparands.GetExpectedType(context.Options)))
            {
                return(EquivalencyResult.ContinueWithNext);
            }

            if (AssertSubjectIsCollection(comparands.Subject))
            {
                var validator = new EnumerableEquivalencyValidator(nestedValidator, context)
                {
                    Recursive     = context.CurrentNode.IsRoot || context.Options.IsRecursive,
                    OrderingRules = context.Options.OrderingRules
                };

                validator.Execute(ToArray(comparands.Subject), ToArray(comparands.Expectation));
            }

            return(EquivalencyResult.AssertionCompleted);
        }
Exemplo n.º 2
0
#pragma warning restore SA1110

        public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            Type expectedType = comparands.GetExpectedType(context.Options);

            if (comparands.Expectation is null || !IsGenericCollection(expectedType))
            {
                return(EquivalencyResult.ContinueWithNext);
            }

            Type[] interfaceTypes = GetIEnumerableInterfaces(expectedType);

            AssertionScope.Current
            .ForCondition(interfaceTypes.Length == 1)
            .FailWith(() => new FailReason("{context:Expectation} implements {0}, so cannot determine which one " +
                                           "to use for asserting the equivalency of the collection. ",
                                           interfaceTypes.Select(type => "IEnumerable<" + type.GetGenericArguments().Single() + ">")));

            if (AssertSubjectIsCollection(comparands.Subject))
            {
                var validator = new EnumerableEquivalencyValidator(nestedValidator, context)
                {
                    Recursive     = context.CurrentNode.IsRoot || context.Options.IsRecursive,
                    OrderingRules = context.Options.OrderingRules
                };

                Type typeOfEnumeration = GetTypeOfEnumeration(expectedType);

                var subjectAsArray = EnumerableEquivalencyStep.ToArray(comparands.Subject);

                try
                {
                    HandleMethod.MakeGenericMethod(typeOfEnumeration).Invoke(null, new[] { validator, subjectAsArray, comparands.Expectation });
                }
                catch (TargetInvocationException e)
                {
                    e.Unwrap().Throw();
                }
            }

            return(EquivalencyResult.AssertionCompleted);
        }
Exemplo n.º 3
0
 private static void HandleImpl <T>(EnumerableEquivalencyValidator validator, object[] subject, IEnumerable <T> expectation)
 => validator.Execute(subject, expectation?.ToArray());