示例#1
0
        private static bool AssertSameLength(Comparands comparands, DictionaryInterfaceInfo actualDictionary,
                                             DictionaryInterfaceInfo expectedDictionary)
        {
            if (comparands.Subject is ICollection subjectCollection &&
                comparands.Expectation is ICollection expectationCollection &&
                subjectCollection.Count == expectationCollection.Count)
            {
                return(true);
            }

            return((bool)AssertSameLengthMethod
                   .MakeGenericMethod(actualDictionary.Key, actualDictionary.Value, expectedDictionary.Key, expectedDictionary.Value)
                   .Invoke(null, new[] { comparands.Subject, comparands.Expectation }));
        }
示例#2
0
#pragma warning restore SA1110

        public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
                                        IEquivalencyValidator nestedValidator)
        {
            if (comparands.Expectation != null)
            {
                Type expectationType = comparands.GetExpectedType(context.Options);
                bool isDictionary    = DictionaryInterfaceInfo.TryGetFrom(expectationType, "expectation", out var expectedDictionary);
                if (isDictionary)
                {
                    Handle(comparands, expectedDictionary, context, nestedValidator);

                    return(EquivalencyResult.AssertionCompleted);
                }
            }

            return(EquivalencyResult.ContinueWithNext);
        }
示例#3
0
 private static void Handle(Comparands comparands, DictionaryInterfaceInfo expectedDictionary,
                            IEquivalencyValidationContext context,
                            IEquivalencyValidator nestedValidator)
 {
     if (AssertSubjectIsNotNull(comparands.Subject) &&
         AssertExpectationIsNotNull(comparands.Subject, comparands.Expectation))
     {
         var(isDictionary, actualDictionary) = EnsureSubjectIsDictionary(comparands, expectedDictionary);
         if (isDictionary)
         {
             if (AssertSameLength(comparands, actualDictionary, expectedDictionary))
             {
                 AssertDictionaryEquivalence(comparands, context, nestedValidator, actualDictionary, expectedDictionary);
             }
         }
     }
 }
示例#4
0
        private static (bool isDictionary, DictionaryInterfaceInfo info) EnsureSubjectIsDictionary(Comparands comparands,
                                                                                                   DictionaryInterfaceInfo expectedDictionary)
        {
            bool isDictionary = DictionaryInterfaceInfo.TryGetFromWithKey(comparands.Subject.GetType(), "subject",
                                                                          expectedDictionary.Key, out var actualDictionary);

            if (!isDictionary)
            {
                if (expectedDictionary.TryConvertFrom(comparands.Subject, out var convertedSubject))
                {
                    comparands.Subject = convertedSubject;
                    isDictionary       = DictionaryInterfaceInfo.TryGetFrom(comparands.Subject.GetType(), "subject", out actualDictionary);
                }
            }

            if (!isDictionary)
            {
                AssertionScope.Current.FailWith(
                    $"Expected {{context:subject}} to be a dictionary or collection of key-value pairs that is keyed to type {expectedDictionary.Key}. " +
                    $"It implements {actualDictionary}.");
            }

            return(isDictionary, actualDictionary);
        }
示例#5
0
 private static void AssertDictionaryEquivalence(Comparands comparands, IEquivalencyValidationContext context,
                                                 IEquivalencyValidator parent, DictionaryInterfaceInfo actualDictionary, DictionaryInterfaceInfo expectedDictionary)
 {
     AssertDictionaryEquivalenceMethod
     .MakeGenericMethod(actualDictionary.Key, actualDictionary.Value, expectedDictionary.Key, expectedDictionary.Value)
     .Invoke(null, new[] { context, parent, context.Options, comparands.Subject, comparands.Expectation });
 }