/// <summary> /// Check to see if an expectation is met. /// </summary> /// <param name="key">The name of the expectation being checked.</param> /// <param name="actualValue">The value for the expectation to check.</param> /// <param name="expectedValues">Can either be a single value or an array of values that are considered valid.</param> /// <param name="detectedErrors">A collection of validation errors that will be added to when errors are found.</param> /// <returns></returns> private static bool ExpectationSatisfied(string key, object actualValue, object expectedValues, List <ValidationError> detectedErrors) { if (null == key) { throw new ArgumentNullException("key"); } if (null == detectedErrors) { throw new ArgumentNullException("detectedErrors"); } if (actualValue == null && expectedValues != null) { detectedErrors.Add(new ValidationError(ValidationErrorCode.ExpectationConditionFailed, null, "Expectation {0}={1} failed. Actual value was null and a value was expected.", key, expectedValues)); return(false); } if (expectedValues == null) { return(true); } if (null != (expectedValues as IList <JToken>)) { if (((IList <JToken>)expectedValues).Any(possibleValue => JsonPath.TokenEquals(possibleValue, actualValue))) { return(true); } } else { var token = expectedValues as JToken; if (token != null) { if (JsonPath.TokenEquals(token, actualValue)) { return(true); } } else if (actualValue.Equals(expectedValues)) { return(true); } } detectedErrors.Add(new ValidationError(ValidationErrorCode.ExpectationConditionFailed, null, "Expectation {0} = {1} failed. Actual value: {2}", key, expectedValues, actualValue)); return(false); }
/// <summary> /// Check to see if an expectation is met. /// </summary> /// <param name="key">The name of the expectation being checked.</param> /// <param name="actualValue">The value for the expectation to check.</param> /// <param name="expectedValues">Can either be a single value or an array of values that are considered valid.</param> /// <param name="issues">A collection of validation errors that will be added to when errors are found.</param> /// <returns></returns> private static bool ExpectationSatisfied(string key, object actualValue, object expectedValues, IssueLogger issues) { if (null == key) { throw new ArgumentNullException("key"); } if (null == issues) { throw new ArgumentNullException("issues"); } if (actualValue == null && expectedValues != null) { issues.Error(ValidationErrorCode.ExpectationConditionFailed, $"Expectation {key}={expectedValues} failed. Actual value was null and a value was expected."); return(false); } if (expectedValues == null) { return(true); } // Possible states // 1. expectedValues is an Array, but actualValue is a value type. ==> actualValue must match a value the expected values array // 2. expectedValues is an Array, and actualValue is an array ==> Arrays must match // 3. expectedValues is a value, and actualValue is a value. ==> Values must match // 4. expectedValues is a value, and actualValue is an array ==> Error var actualValueArray = actualValue as IList <JToken>; var expectedValueArray = expectedValues as IList <JToken>; if (null != actualValueArray && null != expectedValueArray) { // All items must match if (actualValueArray.Count == expectedValueArray.Count) { var sortedActualValue = actualValueArray.ToList(); sortedActualValue.Sort(); var sortedExpectedValue = expectedValueArray.ToList(); sortedExpectedValue.Sort(); bool foundMismatch = false; for (int i = 0; i < sortedActualValue.Count; i++) { if (!sortedActualValue[i].Equals(sortedExpectedValue[i])) { foundMismatch = true; break; } } if (!foundMismatch) { return(true); } } } else if (null != actualValueArray) { // Error state, because we're comparing an array to a single value. } else if (null != expectedValueArray) { // actualValue is a single value, which must exist in expectedValueArray if (expectedValueArray.Any(possibleVAlue => JsonPath.TokenEquals(possibleVAlue, actualValue))) { return(true); } } else { var token = expectedValues as JToken; if (token != null) { if (JsonPath.TokenEquals(token, actualValue)) { return(true); } } else if (actualValue.Equals(expectedValues)) { return(true); } } issues.Error(ValidationErrorCode.ExpectationConditionFailed, $"Expectation {key} = {expectedValues} failed. Actual value: {actualValue}"); return(false); }