public static void VerifyQueryResultsAreEqual(object expected, object actual, AssertionHandler assert) { IEnumerable expectedEnumerable = expected as IEnumerable; IEnumerable actualEnumerable = actual as IEnumerable; if (expectedEnumerable == null && actualEnumerable != null) { assert.IsTrue(false, "Expected a single value result but the actual result is an enumerable."); return; } else if (expectedEnumerable != null && actualEnumerable == null) { assert.IsTrue(false, "Expected an enumerable result but the actual result is a single value."); return; } if (expectedEnumerable != null) { VerificationUtils.VerifyEnumerationsAreEqual(expectedEnumerable.Cast <object>(), actualEnumerable.Cast <object>(), assert); } else { assert.AreEqual(expected, actual, "Expected and actual single value result are different."); } }
/// <summary> /// Calls constructor and verifies expected exception. /// </summary> /// <param name="type">The type to construct.</param> /// <param name="errorMessage">The expected error message.</param> /// <param name="parameters">The parameters for the constructor.</param> public static void CheckInvalidConstructorParameters(AssertionHandler assert, Type type, string errorMessage, params object[] parameters) { try { ConstructorInfo c = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance).Single(); c.Invoke(parameters); assert.Fail(errorMessage); } catch (TargetInvocationException e) { assert.IsTrue(e.InnerException is ArgumentException, "Expecting argument exception"); assert.IsTrue(e.InnerException.Message.Contains(errorMessage), "The exception message doesn't contain the expected string '" + errorMessage + "'."); } }
private static void CompareCollectionValue(IEdmValue edmValue, ODataCollectionValue collectionValue, AssertionHandler assert) { assert.IsNotNull(edmValue, "EDM value instance must not be null."); if (collectionValue == null) { ValidateNullValue(edmValue, assert); return; } assert.AreEqual(EdmValueKind.Collection, edmValue.ValueKind, "Value kinds differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Collection, edmValue.Type.TypeKind(), "Type kinds differ."); } IEdmCollectionValue edmCollectionValue = (IEdmCollectionValue)edmValue; IEnumerable items = collectionValue.Items; if (items != null) { CompareCollectionItems(edmCollectionValue.Elements, items, assert); } else { assert.IsTrue( edmCollectionValue.Elements == null || edmCollectionValue.Elements.Count() == 0, "Expected empty collection value."); } }
/// <summary> /// Verifies that two query tokens are equal. /// </summary> /// <param name="expected">The expected query token.</param> /// <param name="actual">The actual query token.</param> /// <param name="assert">Assertion handler to use.</param> internal static void VerifyStringsAreEqual(ICollection <string> expected, ICollection <string> actual, AssertionHandler assert) { var expectedEnumerator = expected.GetEnumerator(); var actualEnumerator = actual.GetEnumerator(); while (expectedEnumerator.MoveNext()) { assert.IsTrue(actualEnumerator.MoveNext(), "Length should be the same"); assert.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, "Segment text is different"); } }
/// <summary> /// Asserts that the given exception is the specified exception type. /// </summary> /// <typeparam name="TException">Exception type.</typeparam> /// <param name="exception">The exception instance to verify.</param> /// <param name="expectedExceptionMessage">The expected exception message. If this is null, the check will verify that no exception was thrown.</param> /// <param name="desciption">String to attach to all errors so that it's easier to locate what went wrong.</param> public static void IsExpectedException <TException>(this AssertionHandler assert, Exception exception, string expectedExceptionMessage, string description = null) { if (expectedExceptionMessage == null) { assert.IsNull(exception, "No exception was expected, but it occured. " + (description ?? string.Empty) + "\r\n" + (exception == null ? string.Empty : exception.ToString())); } else { assert.IsNotNull(exception, "Expected " + typeof(TException).FullName + " but it was not thrown. " + (description ?? string.Empty)); assert.IsTrue(exception is TException, "Exception had unexpected type " + exception.GetType().FullName + ", expected type is " + typeof(TException).FullName + ". " + description); assert.AreEqual(expectedExceptionMessage, exception.Message, "Unexpected exception message. " + (description ?? string.Empty)); } }
private static void CompareProperties(IEnumerable <IEdmPropertyValue> edmProperties, IEnumerable <ODataProperty> odataProperties, AssertionHandler assert) { using (IEnumerator <IEdmPropertyValue> edmEnumerator = edmProperties.GetEnumerator()) using (IEnumerator <ODataProperty> odataEnumerator = odataProperties.GetEnumerator()) { while (odataEnumerator.MoveNext()) { assert.IsTrue(edmEnumerator.MoveNext(), "Expected more EDM properties."); CompareProperty(edmEnumerator.Current, odataEnumerator.Current, assert); } assert.IsFalse(edmEnumerator.MoveNext(), "Expected no more EDM properties."); } }
private static void CompareCollectionItems(IEnumerable <IEdmDelayedValue> edmItems, IEnumerable odataItems, AssertionHandler assert) { using (IEnumerator <IEdmDelayedValue> edmEnumerator = edmItems.GetEnumerator()) using (IEnumerator <object> odataEnumerator = odataItems.Cast <object>().GetEnumerator()) { while (odataEnumerator.MoveNext()) { assert.IsTrue(edmEnumerator.MoveNext(), "Expected more EDM items."); CompareValue(edmEnumerator.Current.Value, odataEnumerator.Current, assert); } assert.IsFalse(edmEnumerator.MoveNext(), "Expected no more EDM items."); } }
public static void VerifyEnumerationsAreEqual <T>( IEnumerable <T> expectedEnumeration, IEnumerable <T> actualEnumeration, Action <T, T, AssertionHandler> verifyItem, Func <T, string> itemToDebugString, AssertionHandler assert) { if (expectedEnumeration == null) { assert.IsNull(actualEnumeration, "The enumeration of items should have been null."); return; } else { assert.IsNotNull(actualEnumeration, "The enumeration of items should not have been null."); } try { var expectedEnumerator = expectedEnumeration.GetEnumerator(); var actualEnumerator = actualEnumeration.GetEnumerator(); while (expectedEnumerator.MoveNext()) { assert.IsTrue( actualEnumerator.MoveNext(), "The actual enumeration has less items than the expected enumeration.\r\n" + "Expected items: " + string.Join(", ", expectedEnumeration.Select(t => "<" + itemToDebugString(t) + ">")) + "\r\n" + "Actual items: " + string.Join(", ", actualEnumeration.Select(t => "<" + itemToDebugString(t) + ">"))); verifyItem(expectedEnumerator.Current, actualEnumerator.Current, assert); } assert.IsFalse( actualEnumerator.MoveNext(), "The expected enumeration has less items than the actual enumeration.\r\n" + "Expected items: " + string.Join(", ", expectedEnumeration.Select(t => "<" + itemToDebugString(t) + ">")) + "\r\n" + "Actual items: " + string.Join(", ", actualEnumeration.Select(t => "<" + itemToDebugString(t) + ">"))); } catch (Exception) { assert.Warn("Expected items: " + string.Join(", ", expectedEnumeration.Select(t => "<" + itemToDebugString(t) + ">"))); assert.Warn("Actual items: " + string.Join(", ", actualEnumeration.Select(t => "<" + itemToDebugString(t) + ">"))); throw; } }
private static void CompareStructuralValue(IEdmValue edmValue, IEnumerable <ODataProperty> properties, AssertionHandler assert) { IEdmStructuredValue structuredEdmValue = (IEdmStructuredValue)edmValue; if (properties != null) { // Use FindPropertyValue foreach (ODataProperty property in properties) { IEdmPropertyValue edmPropertyValue = structuredEdmValue.FindPropertyValue(property.Name); CompareProperty(edmPropertyValue, property, assert); } // Enumerate the properties CompareProperties(structuredEdmValue.PropertyValues, properties, assert); } else { assert.IsTrue( structuredEdmValue.PropertyValues == null || structuredEdmValue.PropertyValues.Count() == 0, "Expected empty structured value."); } }
private static void ValidateNullValue(IEdmValue edmValue, AssertionHandler assert) { assert.AreEqual(EdmValueKind.Null, edmValue.ValueKind, "Expected null value kind."); assert.IsTrue(edmValue is IEdmNullValue, "Expected IEdmNullValue instance."); }
/// <summary> /// Runs a single BufferingJsonReaderTestCaseDescriptor test. /// </summary> /// <param name="testCase">The test case descriptor to run.</param> /// <param name="assert"></param> public static void ReadAndVerifyBufferingJson(BufferingJsonReaderTestCaseDescriptor testCase, AssertionHandler assert) { TextReader testReader = new StringReader(testCase.JsonText); BufferingJsonReader bufferingJsonReader = new BufferingJsonReader(testReader, ODataConstants.DefaultMaxRecursionDepth, assert, ODataFormat.Json, isIeee754Compatible: true); bool isBuffering = false; int callCount = -1; int index = 0; int[] toggleCallCounts = testCase.ToggleBufferingCallCounts; int toggleAt = toggleCallCounts == null || toggleCallCounts.Length == 0 ? -1 : toggleCallCounts[index]; int nonBufferingResultIndex = -1; int bufferingResultIndex = -1; do { callCount++; int ixToCompare; if (isBuffering) { bufferingResultIndex++; ixToCompare = bufferingResultIndex; } else { nonBufferingResultIndex++; ixToCompare = nonBufferingResultIndex; } assert.IsTrue(!isBuffering || bufferingResultIndex >= nonBufferingResultIndex, "Buffering index must be greater or equal than non-buffering one."); if (testCase.ExpectedNodes != null) { assert.AreEqual(testCase.ExpectedNodes[ixToCompare].NodeType, bufferingJsonReader.NodeType, "Node types don't match."); assert.AreEqual(testCase.ExpectedNodes[ixToCompare].Value, bufferingJsonReader.Value, "Values don't match."); } if (toggleAt == callCount) { if (!isBuffering) { bufferingJsonReader.StartBuffering(); bufferingResultIndex = nonBufferingResultIndex; isBuffering = true; } else { bufferingJsonReader.StopBuffering(); isBuffering = false; } if (index + 1 < toggleCallCounts.Length) { index++; toggleAt = toggleCallCounts[index]; } } } while (bufferingJsonReader.Read()); // we might have hit the end of the input in buffering mode; now empty the buffer. if (isBuffering) { bufferingJsonReader.StopBuffering(); isBuffering = false; if (testCase.ExpectedNodes != null) { assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].NodeType, bufferingJsonReader.NodeType, "Node types don't match."); assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].Value, bufferingJsonReader.Value, "Values don't match."); } } while (bufferingJsonReader.Read()) { nonBufferingResultIndex++; if (testCase.ExpectedNodes != null) { assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].NodeType, bufferingJsonReader.NodeType, "Node types don't match."); assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].Value, bufferingJsonReader.Value, "Values don't match."); } } // reading after end-of-input should stay in state end-of-input bufferingJsonReader.Read(); assert.AreEqual(JsonNodeType.EndOfInput, bufferingJsonReader.NodeType, "Node types don't match."); assert.AreEqual(null, bufferingJsonReader.Value, "Values don't match."); }
/// <summary> /// Runs a single BufferingJsonReaderTestCaseDescriptor test. /// </summary> /// <param name="testCase">The test case descriptor to run.</param> /// <param name="assert"></param> public static void ReadAndVerifyBufferingJson(BufferingJsonReaderTestCaseDescriptor testCase, AssertionHandler assert) { TextReader testReader = new StringReader(testCase.JsonText); BufferingJsonReader bufferingJsonReader = new BufferingJsonReader(testReader, ODataConstants.DefaultMaxRecursionDepth, assert, isIeee754Compatible: true); bool isBuffering = false; int callCount = -1; int index = 0; int[] toggleCallCounts = testCase.ToggleBufferingCallCounts; int toggleAt = toggleCallCounts == null || toggleCallCounts.Length == 0 ? -1 : toggleCallCounts[index]; int nonBufferingResultIndex = -1; int bufferingResultIndex = -1; do { callCount++; int ixToCompare; if (isBuffering) { bufferingResultIndex++; ixToCompare = bufferingResultIndex; } else { nonBufferingResultIndex++; ixToCompare = nonBufferingResultIndex; } assert.IsTrue(!isBuffering || bufferingResultIndex >= nonBufferingResultIndex, "Buffering index must be greater or equal than non-buffering one."); if (testCase.ExpectedNodes != null) { assert.AreEqual(testCase.ExpectedNodes[ixToCompare].NodeType, bufferingJsonReader.NodeType, "Node types don't match."); assert.AreEqual(testCase.ExpectedNodes[ixToCompare].Value, bufferingJsonReader.Value, "Values don't match."); } if (toggleAt == callCount) { if (!isBuffering) { bufferingJsonReader.StartBuffering(); bufferingResultIndex = nonBufferingResultIndex; isBuffering = true; } else { bufferingJsonReader.StopBuffering(); isBuffering = false; } if (index + 1 < toggleCallCounts.Length) { index++; toggleAt = toggleCallCounts[index]; } } }while (bufferingJsonReader.Read()); // we might have hit the end of the input in buffering mode; now empty the buffer. if (isBuffering) { bufferingJsonReader.StopBuffering(); isBuffering = false; if (testCase.ExpectedNodes != null) { assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].NodeType, bufferingJsonReader.NodeType, "Node types don't match."); assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].Value, bufferingJsonReader.Value, "Values don't match."); } } while (bufferingJsonReader.Read()) { nonBufferingResultIndex++; if (testCase.ExpectedNodes != null) { assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].NodeType, bufferingJsonReader.NodeType, "Node types don't match."); assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].Value, bufferingJsonReader.Value, "Values don't match."); } } // reading after end-of-input should stay in state end-of-input bufferingJsonReader.Read(); assert.AreEqual(JsonNodeType.EndOfInput, bufferingJsonReader.NodeType, "Node types don't match."); assert.AreEqual(null, bufferingJsonReader.Value, "Values don't match."); }
public static void VerifyQueryResultsAreEqual(object expected, object actual, AssertionHandler assert) { IEnumerable expectedEnumerable = expected as IEnumerable; IEnumerable actualEnumerable = actual as IEnumerable; if (expectedEnumerable == null && actualEnumerable != null) { assert.IsTrue(false, "Expected a single value result but the actual result is an enumerable."); return; } else if (expectedEnumerable != null && actualEnumerable == null) { assert.IsTrue(false, "Expected an enumerable result but the actual result is a single value."); return; } if (expectedEnumerable != null) { VerificationUtils.VerifyEnumerationsAreEqual(expectedEnumerable.Cast<object>(), actualEnumerable.Cast<object>(), assert); } else { assert.AreEqual(expected, actual, "Expected and actual single value result are different."); } }
public static void VerifyTypesAreEqual(IEdmTypeReference expected, IEdmTypeReference actual, AssertionHandler assert) { assert.IsTrue(expected.IsEquivalentTo(actual), "The expected and the actual types don't match."); }
private static void CompareCollectionItems(IEnumerable<IEdmDelayedValue> edmItems, IEnumerable odataItems, AssertionHandler assert) { using (IEnumerator<IEdmDelayedValue> edmEnumerator = edmItems.GetEnumerator()) using (IEnumerator<object> odataEnumerator = odataItems.Cast<object>().GetEnumerator()) { while (odataEnumerator.MoveNext()) { assert.IsTrue(edmEnumerator.MoveNext(), "Expected more EDM items."); CompareValue(edmEnumerator.Current.Value, odataEnumerator.Current, assert); } assert.IsFalse(edmEnumerator.MoveNext(), "Expected no more EDM items."); } }
private static void CompareProperties(IEnumerable<IEdmPropertyValue> edmProperties, IEnumerable<ODataProperty> odataProperties, AssertionHandler assert) { using (IEnumerator<IEdmPropertyValue> edmEnumerator = edmProperties.GetEnumerator()) using (IEnumerator<ODataProperty> odataEnumerator = odataProperties.GetEnumerator()) { while (odataEnumerator.MoveNext()) { assert.IsTrue(edmEnumerator.MoveNext(), "Expected more EDM properties."); CompareProperty(edmEnumerator.Current, odataEnumerator.Current, assert); } assert.IsFalse(edmEnumerator.MoveNext(), "Expected no more EDM properties."); } }
/// <summary> /// Verifies that two query tokens are equal. /// </summary> /// <param name="expected">The expected query token.</param> /// <param name="actual">The actual query token.</param> /// <param name="assert">Assertion handler to use.</param> internal static void VerifyStringsAreEqual(ICollection<string> expected, ICollection<string> actual, AssertionHandler assert) { var expectedEnumerator = expected.GetEnumerator(); var actualEnumerator = actual.GetEnumerator(); while(expectedEnumerator.MoveNext()) { assert.IsTrue(actualEnumerator.MoveNext(), "Length should be the same"); assert.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, "Segment text is different"); } }
private static void CompareStructuralValue(IEdmValue edmValue, IEnumerable<ODataProperty> properties, AssertionHandler assert) { IEdmStructuredValue structuredEdmValue = (IEdmStructuredValue)edmValue; if (properties != null) { // Use FindPropertyValue foreach (ODataProperty property in properties) { IEdmPropertyValue edmPropertyValue = structuredEdmValue.FindPropertyValue(property.Name); CompareProperty(edmPropertyValue, property, assert); } // Enumerate the properties CompareProperties(structuredEdmValue.PropertyValues, properties, assert); } else { assert.IsTrue( structuredEdmValue.PropertyValues == null || structuredEdmValue.PropertyValues.Count() == 0, "Expected empty structured value."); } }