Beispiel #1
0
        private string[] TryToMatch <T>(object subject, T expectation, int expectationIndex)
        {
            using var scope = new AssertionScope();
            parent.AssertEqualityUsing(context.CreateForCollectionItem(expectationIndex.ToString(), subject, expectation));

            return(scope.Discard());
        }
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent,
                           IEquivalencyAssertionOptions config)
        {
            Array expectationAsArray = (Array)context.Expectation;

            if (AreComparable(context, expectationAsArray))
            {
                Digit digit = BuildDigitsRepresentingAllIndices(expectationAsArray);

                do
                {
                    object subject       = ((Array)context.Subject).GetValue(digit.Indices);
                    string listOfIndices = string.Join(",", digit.Indices);
                    object expectation   = expectationAsArray.GetValue(digit.Indices);
                    IEquivalencyValidationContext itemContext = context.CreateForCollectionItem(
                        listOfIndices,
                        subject,
                        expectation);

                    parent.AssertEqualityUsing(itemContext);
                }while (digit.Increment());
            }

            return(true);
        }
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator,
                           IEquivalencyAssertionOptions config)
        {
            if ((context.Expectation is null) || (context.Subject is null))
            {
                return(false);
            }

            Type subjectType     = context.Subject.GetType();
            Type expectationType = context.Expectation.GetType();

            if (subjectType.IsSameOrInherits(expectationType))
            {
                return(false);
            }

            if (TryChangeType(context.Subject, expectationType, out object convertedSubject))
            {
                context.TraceSingle(path => $"Converted subject {context.Subject} at {path} to {expectationType}");

                var newContext = context.CreateWithDifferentSubject(convertedSubject, expectationType);

                structuralEqualityValidator.AssertEqualityUsing(newContext);
                return(true);
            }

            context.TraceSingle(path => $"Subject {context.Subject} at {path} could not be converted to {expectationType}");

            return(false);
        }
Beispiel #4
0
        public virtual bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            var subject     = context.Subject as IDictionary;
            var expectation = context.Expectation as IDictionary;

            if (PreconditionsAreMet(expectation, subject))
            {
                if (expectation != null)
                {
                    foreach (object key in expectation.Keys)
                    {
                        if (config.IsRecursive)
                        {
                            context.TraceSingle(path => $"Recursing into dictionary item {key} at {path}");
                            parent.AssertEqualityUsing(context.CreateForDictionaryItem(key, subject[key], expectation[key]));
                        }
                        else
                        {
                            context.TraceSingle(path => $"Comparing dictionary item {key} at {path} between subject and expectation");
                            subject[key].Should().Be(expectation[key], context.Because, context.BecauseArgs);
                        }
                    }
                }
            }

            return(true);
        }
Beispiel #5
0
        private static void AssertDictionaryEquivalence <TSubjectKey, TSubjectValue, TExpectKey, TExpectedValue>(
            EquivalencyValidationContext context,
            IEquivalencyValidator parent,
            IEquivalencyAssertionOptions config,
            IDictionary <TSubjectKey, TSubjectValue> subject,
            IDictionary <TExpectKey, TExpectedValue> expectation) where TSubjectKey : TExpectKey
        {
            foreach (TSubjectKey key in subject.Keys)
            {
                TExpectedValue expectedValue;

                if (expectation.TryGetValue(key, out expectedValue))
                {
                    if (config.IsRecursive)
                    {
                        parent.AssertEqualityUsing(context.CreateForDictionaryItem(key, subject[key], expectation[key]));
                    }
                    else
                    {
                        subject[key].Should().Be(expectation[key], context.Because, context.BecauseArgs);
                    }
                }
                else
                {
                    AssertionScope.Current.FailWith("{context:subject} contains unexpected key {0}", key);
                }
            }
        }
 private void AssertPropertyEquality(EquivalencyValidationContext context, IEquivalencyValidator parent,
     PropertyInfo propertyInfo)
 {
     var nestedContext = context.CreateForNestedProperty(propertyInfo);
     if (nestedContext != null)
     {
         parent.AssertEqualityUsing(nestedContext);
     }
 }
 private void EnumerateElements(EquivalencyValidationContext context, object[] subject, object[] expectation,
     IEquivalencyValidator parent)
 {
     if (!subject.SequenceEqual(expectation))
     {
         for (int i = 0; i < subject.Length; i++)
         {
             parent.AssertEqualityUsing(context.CreateForCollectionItem(i, subject[i], expectation[i]));
         }
     }
 }
 private void AssertPropertyEquality(EquivalencyValidationContext context, IEquivalencyValidator parent, PropertyInfo propertyInfo, IEquivalencyAssertionOptions config)
 {
     var matchingProperty = FindMatchFor(propertyInfo, context, config.MatchingRules);
     if (matchingProperty != null)
     {
         EquivalencyValidationContext nestedContext = context.CreateForNestedProperty(propertyInfo, matchingProperty);
         if (nestedContext != null)
         {
             parent.AssertEqualityUsing(nestedContext);
         }
     }
 }
 private static void AssertMemberEquality(IEquivalencyValidationContext context, IEquivalencyValidator parent, SelectedMemberInfo selectedMemberInfo, IEquivalencyAssertionOptions config)
 {
     var matchingMember = FindMatchFor(selectedMemberInfo, context, config);
     if (matchingMember != null)
     {
         var nestedContext = context.CreateForNestedMember(selectedMemberInfo, matchingMember);
         if (nestedContext != null)
         {
             parent.AssertEqualityUsing(nestedContext);
         }
     }
 }
        private static void AssertMemberEquality(IEquivalencyValidationContext context, IEquivalencyValidator parent, SelectedMemberInfo selectedMemberInfo, IEquivalencyAssertionOptions config)
        {
            var matchingMember = FindMatchFor(selectedMemberInfo, context, config);

            if (matchingMember != null)
            {
                var nestedContext = context.CreateForNestedMember(selectedMemberInfo, matchingMember);
                if (nestedContext != null)
                {
                    parent.AssertEqualityUsing(nestedContext);
                }
            }
        }
        private void AssertPropertyEquality(EquivalencyValidationContext context, IEquivalencyValidator parent, PropertyInfo propertyInfo, IEquivalencyAssertionOptions config)
        {
            var matchingProperty = FindMatchFor(propertyInfo, context, config.MatchingRules);

            if (matchingProperty != null)
            {
                EquivalencyValidationContext nestedContext = context.CreateForNestedProperty(propertyInfo, matchingProperty);
                if (nestedContext != null)
                {
                    parent.AssertEqualityUsing(nestedContext);
                }
            }
        }
Beispiel #12
0
        private static void HandleCore <TSubject, TExpectation>(IMaybe <TSubject> subject, IMaybe <TExpectation> expectation, IEquivalencyValidationContext context, IEquivalencyValidator parent)
        {
            expectation.Match(none, some)();

            void none()
            {
                subject.Match(none: () => { },
                              some: _ => new Action(() => AssertionScope.Current.FailWith("Expected subject to be empty, but it was filled.")))();
            }

            void some(TExpectation e)
            {
                subject.Match(none: () => AssertionScope.Current.FailWith("Expected {context:subject} to be filled, but it was empty."),
                              some: (s) => new Action(() => parent.AssertEqualityUsing(context.CreateForMaybeValue(s, e))))();
            }
        }
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config)
        {
            if (!ReferenceEquals(context.Expectation, null) && !ReferenceEquals(context.Subject, null)
                && !context.Subject.GetType().IsSameOrInherits(context.Expectation.GetType()))
            {
                Type expectationType = context.Expectation.GetType();

                object convertedSubject;
                if (TryChangeType(context.Subject, expectationType, out convertedSubject))
                {
                    var newContext = context.CreateWithDifferentSubject(convertedSubject, expectationType);

                    structuralEqualityValidator.AssertEqualityUsing(newContext);
                    return true;
                }
            }

            return false;
        }
Beispiel #14
0
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config)
        {
            if (!ReferenceEquals(context.Expectation, null) && !ReferenceEquals(context.Subject, null) &&
                !context.Subject.GetType().IsSameOrInherits(context.Expectation.GetType()))
            {
                Type expectationType = context.Expectation.GetType();

                object convertedSubject;
                if (TryChangeType(context.Subject, expectationType, out convertedSubject))
                {
                    var newContext = context.CreateWithDifferentSubject(convertedSubject, expectationType);

                    structuralEqualityValidator.AssertEqualityUsing(newContext);
                    return(true);
                }
            }

            return(false);
        }
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public virtual bool Handle(EquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            var subject     = (IDictionary)context.Subject;
            var expectation = context.Expectation as IDictionary;

            if (PreconditionsAreMet(context, expectation, subject))
            {
                foreach (object key in subject.Keys)
                {
                    if (config.IsRecursive)
                    {
                        parent.AssertEqualityUsing(context.CreateForDictionaryItem(key, subject[key], expectation[key]));
                    }
                    else
                    {
                        subject[key].Should().Be(expectation[key], context.Reason, context.ReasonArgs);
                    }
                }
            }

            return(true);
        }
        /// <summary>
        /// Applies a step as part of the task to compare two objects for structural equality.
        /// </summary>
        /// <value>
        /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions
        /// have to be executed. Should return <c>false</c> otherwise.
        /// </value>
        /// <remarks>
        /// May throw when preconditions are not met or if it detects mismatching data.
        /// </remarks>
        public virtual bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            var subject = (IDictionary)context.Subject;
            var expectation = context.Expectation as IDictionary;

            if (PreconditionsAreMet(context, expectation, subject))
            {
                foreach (object key in subject.Keys)
                {
                    if (config.IsRecursive)
                    {
                        parent.AssertEqualityUsing(context.CreateForDictionaryItem(key, subject[key], expectation[key]));
                    }
                    else
                    {
                        subject[key].Should().Be(expectation[key], context.Reason, context.ReasonArgs);
                    }
                }
            }

            return true;
        }
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent,
            IEquivalencyAssertionOptions config)
        {
            Array subjectAsArray = (Array) context.Subject;

            if (AreComparable(context, subjectAsArray))
            {
                Digit digit = BuildDigitsRepresentingAllIndices(subjectAsArray);

                do
                {
                    var expectation = ((Array) context.Expectation).GetValue(digit.Indices);
                    IEquivalencyValidationContext itemContext = context.CreateForCollectionItem(
                        string.Join(",", digit.Indices),
                        subjectAsArray.GetValue(digit.Indices),
                        expectation);

                    parent.AssertEqualityUsing(itemContext);
                }
                while (digit.Increment());
            }

            return true;
        }