Ejemplo n.º 1
0
        private static void AddFailuresForArray(JArray expected, JArray actual, MatchingContext context)
        {
            var expectedItems = expected.ToArray();
            var actualItems   = actual.ToArray();

            var lengthMatchers = context.MatchersForProperty.OfType <IArrayLengthMatcher>().Where(m => m.AppliesTo(expectedItems, actualItems, context)).ToArray();

            if (lengthMatchers.Any())
            {
                AddFailures(expectedItems, actualItems, lengthMatchers, context);
            }

            if (!lengthMatchers.Any(m => m.IsTerminal))
            {
                if (!context.IgnoreExpected)
                {
                    if (context.IsRequest)
                    {
                        if (actualItems.Length != expectedItems.Length)
                        {
                            context.Result.AddFailure(context.PropertyPath,
                                                      $"Expected {expectedItems.Length} items, but got {actualItems.Length} items");
                        }
                    }
                    else
                    {
                        if (actualItems.Length < expectedItems.Length)
                        {
                            context.Result.AddFailure(context.PropertyPath,
                                                      $"Expected at least {expectedItems.Length} items, but got only {actualItems.Length} items");
                        }
                    }
                }
            }

            for (var i = 0; i < actualItems.Length; i++)
            {
                AddFailures(i < expectedItems.Length ? expectedItems[i] : expectedItems.LastOrDefault(), actualItems[i],
                            context.For(new ArrayIndexPathPart(i.ToString()), ignoreExpected: i >= expectedItems.Length));
            }
        }
Ejemplo n.º 2
0
        private static void AddFailuresForObject(JObject expected, JObject actual, MatchingContext context)
        {
            var applyingMatchers = context.MatchersForProperty.Where(m => m.AppliesTo(expected, actual, context)).ToArray();

            if (applyingMatchers.Any())
            {
                AddFailures(expected, actual, applyingMatchers, context);
                if (applyingMatchers.Any(m => m.IsTerminal) || context.TerminationRequested)
                {
                    return;
                }
            }

            foreach (var property in expected.Properties().Concat(actual.Properties()).Select(p => p.Name).Distinct().ToArray())
            {
                var expectedProperty = expected.Properties().FirstOrDefault(p => p.Name.Equals(property));
                var actualProperty   = actual.Properties().FirstOrDefault(p => p.Name.Equals(property));

                var propertyName = (expectedProperty ?? actualProperty)?.Name ?? property;
                AddFailures(expectedProperty?.Value, actualProperty?.Value, context.For(new PropertyPathPart(propertyName)));
            }
        }