Example #1
0
        private static List <string> CompareExpectedTokenWithActual <T>(JToken expectedToken, MatchingRuleCollection matchingRules, JToken actualRootToken) where T : IEquatable <T>
        {
            var expectedValue = expectedToken.Value <T>();
            var actualToken   = actualRootToken.SelectToken(expectedToken.Path);

            if (actualToken == null)
            {
                var actualRootTokenWithLowerCasePropertyNames = JTokenParser.ParseToLower(actualRootToken);
                if (actualRootTokenWithLowerCasePropertyNames.SelectToken(expectedToken.Path.ToLowerInvariant()) != null)
                {
                    return(new List <string> {
                        $"A property with a name like \'{expectedToken.Path}\' was present in the actual response, but the case did not match. Note that Pact is case sensitive."
                    });
                }
                return(new List <string> {
                    $"Property \'{expectedToken.Path}\' was not present in the actual response."
                });
            }

            if (expectedToken.Type != actualToken.Type)
            {
                return(new List <string> {
                    $"Property \'{expectedToken.Path}\' has a different type in the actual response. Expected value: {expectedToken}, actual value: {actualToken}"
                });
            }

            var actualValue = actualToken.Value <T>();

            if (expectedToken.Type != JTokenType.Null && actualValue == null)
            {
                return(new List <string> {
                    $"Expected \'{expectedValue}\' at \'{expectedToken.Path}\', but had no value."
                });
            }
            if (matchingRules != null && matchingRules.TryGetApplicableMatcherListForToken(expectedToken, out var matcherList))
            {
                return(matcherList.Match(expectedToken, actualToken));
            }
            else if (expectedToken.Type != actualToken.Type)
            {
                return(new List <string> {
                    $"Expected value of type {expectedToken.Type} (like: \'{expectedToken.ToString()}\') at \'{expectedToken.Path}\', but was value of type {actualToken.Type}."
                });
            }
            else if (!actualValue.Equals(expectedValue))
            {
                return(new List <string> {
                    $"Expected \'{expectedValue}\' at \'{expectedToken.Path}\', but was \'{actualValue}\'."
                });
            }

            return(new List <string>());
        }
Example #2
0
        private static List <string> CompareExpectedTokenWithActual <T>(JToken expectedToken, MatchingRuleCollection matchingRules, JToken actualRootToken) where T : IEquatable <T>
        {
            var expectedValue = expectedToken.Value <T>();
            var actualToken   = actualRootToken.SelectToken(expectedToken.Path);

            if (actualToken == null)
            {
                return(new List <string> {
                    $"Property \'{expectedToken.Path}\' was not present in the actual response."
                });
            }
            var actualValue = actualToken.Value <T>();

            if (expectedToken.Type != JTokenType.Null && actualValue == null)
            {
                return(new List <string> {
                    $"Expected \'{expectedValue}\' at \'{expectedToken.Path}\', but had no value."
                });
            }
            if (matchingRules != null && matchingRules.TryGetApplicableMatcherListForToken(expectedToken, out var matcherList))
            {
                return(matcherList.Match(expectedToken, actualToken));
            }
            else if (expectedToken.Type != actualToken.Type)
            {
                return(new List <string> {
                    $"Expected value of type {expectedToken.Type} (like: \'{expectedToken.ToString()}\') at \'{expectedToken.Path}\', but was value of type {actualToken.Type}."
                });
            }
            else if (!actualValue.Equals(expectedValue))
            {
                return(new List <string> {
                    $"Expected \'{expectedValue}\' at \'{expectedToken.Path}\', but was \'{actualValue}\'."
                });
            }

            return(new List <string>());
        }
Example #3
0
        private static List <string> CompareExpectedArrayWithActual(JToken expectedToken, MatchingRuleCollection matchingRules, JToken actualRootToken)
        {
            var actualToken = actualRootToken.SelectToken(expectedToken.Path);

            if (actualToken == null)
            {
                return(new List <string> {
                    $"Array \'{expectedToken.Path}\' was not present in the actual response."
                });
            }
            if (matchingRules != null && matchingRules.TryGetApplicableMatcherListForToken(expectedToken, out var matcherList))
            {
                return(matcherList.Match(expectedToken, actualToken));
            }
            else if (actualToken.Children().Count() != expectedToken.Children().Count())
            {
                return(new List <string> {
                    $"Expected array at \'{expectedToken.Path}\' to have {expectedToken.Children().Count()} items, but was {actualToken.Children().Count()}."
                });
            }

            return(new List <string>());
        }