Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }