public static void VerifyQueryNodesAreEqual(QueryNode expected, QueryNode actual, AssertionHandler assert) { try { if (expected == null) { assert.IsNull(actual, "The node should be null."); return; } else { assert.IsNotNull(actual, "The node should not be null."); } assert.AreEqual(expected.InternalKind, actual.InternalKind, "The node kind differs from expected one."); switch (expected.InternalKind) { case InternalQueryNodeKind.Constant: VerifyConstantQueryNodesAreEqual((ConstantNode)expected, (ConstantNode)actual, assert); break; case InternalQueryNodeKind.Convert: VerifyConvertQueryNodesAreEqual((ConvertNode)expected, (ConvertNode)actual, assert); break; case InternalQueryNodeKind.NonentityRangeVariableReference: VerifyNonentityRangeVariableReferenceNodesAreEqual((NonentityRangeVariableReferenceNode)expected, (NonentityRangeVariableReferenceNode)actual, assert); break; case InternalQueryNodeKind.EntityRangeVariableReference: VerifyEntityRangeVariableReferenceNodesAreEqual((EntityRangeVariableReferenceNode)expected, (EntityRangeVariableReferenceNode)actual, assert); break; case InternalQueryNodeKind.BinaryOperator: VerifyBinaryOperatorQueryNodesAreEqual((BinaryOperatorNode)expected, (BinaryOperatorNode)actual, assert); break; case InternalQueryNodeKind.UnaryOperator: VerifyUnaryOperatorQueryNodesAreEqual((UnaryOperatorNode)expected, (UnaryOperatorNode)actual, assert); break; case InternalQueryNodeKind.SingleValuePropertyAccess: VerifyPropertyAccessQueryNodesAreEqual((SingleValuePropertyAccessNode)expected, (SingleValuePropertyAccessNode)actual, assert); break; case InternalQueryNodeKind.SingleValueFunctionCall: VerifySingleValueFunctionCallQueryNodesAreEqual((SingleValueFunctionCallNode)expected, (SingleValueFunctionCallNode)actual, assert); break; default: throw new Exception("Query node of kind '" + expected.InternalKind.ToString() + "' not yet supported by VerifyQueryNodesAreEqual."); } } catch (Exception) { assert.Warn("Expected query node: " + expected.ToDebugString()); assert.Warn("Actual query node: " + actual.ToDebugString()); throw; } }
/// <summary> /// Runs a single BufferingJsonReaderTestCaseDescriptor test with a single toggle index in it /// and verifies that the reader state after turning off buffering is correct. /// </summary> /// <param name="testCase">The test case descriptor to run.</param> /// <param name="assert"></param> public static void ReadAndVerifyStateAfterStopBuffering(BufferingJsonReaderTestCaseDescriptor testCase, AssertionHandler assert) { assert.AreEqual(2, testCase.ToggleBufferingCallCounts.Length, "Expected a single toggle position."); TextReader testReader = new StringReader(testCase.JsonText); Exception exception = TestExceptionUtils.RunCatching(() => { BufferingJsonReader bufferingJsonReader = new BufferingJsonReader(testReader, ODataConstants.DefaultMaxRecursionDepth, assert, isIeee754Compatible: true); int callCount = -1; int startBuffering = testCase.ToggleBufferingCallCounts[0]; int stopBuffering = testCase.ToggleBufferingCallCounts[1]; bool isBuffering = false; List <BufferingJsonReaderTestCaseDescriptor.ReaderNode> bufferedNodes = new List <BufferingJsonReaderTestCaseDescriptor.ReaderNode>(); bool hasMore = false; do { callCount++; if (startBuffering == callCount) { BufferingJsonReaderTestCaseDescriptor.ReaderNode bufferedNode = new BufferingJsonReaderTestCaseDescriptor.ReaderNode(bufferingJsonReader.NodeType, bufferingJsonReader.Value); bufferedNodes.Add(bufferedNode); bufferingJsonReader.StartBuffering(); isBuffering = true; } if (stopBuffering == callCount) { bufferingJsonReader.StopBuffering(); isBuffering = false; assert.AreEqual(bufferedNodes[0].NodeType, bufferingJsonReader.NodeType, "Node types must be equal."); assert.AreEqual(bufferedNodes[0].Value, bufferingJsonReader.Value, "Values must be equal."); bufferedNodes.RemoveAt(0); } hasMore = bufferingJsonReader.Read(); if (isBuffering) { bufferedNodes.Add(new BufferingJsonReaderTestCaseDescriptor.ReaderNode(bufferingJsonReader.NodeType, bufferingJsonReader.Value)); } else if (bufferedNodes.Count > 0) { assert.AreEqual(bufferedNodes[0].NodeType, bufferingJsonReader.NodeType, "Node types must be equal."); assert.AreEqual(bufferedNodes[0].Value, bufferingJsonReader.Value, "Values must be equal."); bufferedNodes.RemoveAt(0); } }while (hasMore); }); assert.IsNull(exception, "Did not expect an exception."); }
private static bool VerifyNullnessMatches(object expected, object actual, AssertionHandler assert, string description) { if (expected == null) { assert.IsNull(actual, string.Format("The actual {0} should have been null.", description)); return(false); } assert.IsNotNull(actual, string.Format("The actual {0} should have been not-null.", description)); return(true); }
public static void VerifyServiceOperationsAreEqual(IEdmOperationImport expected, IEdmOperationImport actual, AssertionHandler assert) { if (expected == null) { assert.IsNull(actual, "The service operation should be null."); } else { assert.IsNotNull(actual, "The service operation should not be null."); assert.AreEqual(expected.Name, actual.Name, "The service operation names are different."); } }
public static void VerifyEntitySetsAreEqual(IEdmEntitySet expected, IEdmEntitySet actual, AssertionHandler assert) { if (expected == null) { assert.IsNull(actual, "The entity set should be null."); } else { assert.IsNotNull(actual, "The entity set should not be null."); assert.AreEqual(expected.Name, actual.Name, "The entity set names are different."); } }
public static void VerifyQueryNodesAreEqual(QueryNode expected, QueryNode actual, AssertionHandler assert) { try { if (expected == null) { assert.IsNull(actual, "The node should be null."); return; } else { assert.IsNotNull(actual, "The node should not be null."); } assert.AreEqual(expected.InternalKind, actual.InternalKind, "The node kind differs from expected one."); switch (expected.InternalKind) { case InternalQueryNodeKind.Constant: VerifyConstantQueryNodesAreEqual((ConstantNode)expected, (ConstantNode)actual, assert); break; case InternalQueryNodeKind.Convert: VerifyConvertQueryNodesAreEqual((ConvertNode)expected, (ConvertNode)actual, assert); break; case InternalQueryNodeKind.NonentityRangeVariableReference: VerifyNonentityRangeVariableReferenceNodesAreEqual((NonentityRangeVariableReferenceNode) expected, (NonentityRangeVariableReferenceNode) actual,assert); break; case InternalQueryNodeKind.EntityRangeVariableReference: VerifyEntityRangeVariableReferenceNodesAreEqual((EntityRangeVariableReferenceNode)expected, (EntityRangeVariableReferenceNode)actual, assert); break; case InternalQueryNodeKind.BinaryOperator: VerifyBinaryOperatorQueryNodesAreEqual((BinaryOperatorNode)expected, (BinaryOperatorNode)actual, assert); break; case InternalQueryNodeKind.UnaryOperator: VerifyUnaryOperatorQueryNodesAreEqual((UnaryOperatorNode)expected, (UnaryOperatorNode)actual, assert); break; case InternalQueryNodeKind.SingleValuePropertyAccess: VerifyPropertyAccessQueryNodesAreEqual((SingleValuePropertyAccessNode)expected, (SingleValuePropertyAccessNode)actual, assert); break; case InternalQueryNodeKind.SingleValueFunctionCall: VerifySingleValueFunctionCallQueryNodesAreEqual((SingleValueFunctionCallNode)expected, (SingleValueFunctionCallNode)actual, assert); break; default: throw new Exception("Query node of kind '" + expected.InternalKind.ToString() + "' not yet supported by VerifyQueryNodesAreEqual."); } } catch (Exception) { assert.Warn("Expected query node: " + expected.ToDebugString()); assert.Warn("Actual query node: " + actual.ToDebugString()); throw; } }
/// <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)); } }
public static void VerifyPropertiesAreEqual(IEdmProperty expected, IEdmProperty actual, AssertionHandler assert) { if (expected == null) { assert.IsNull(actual, "The property should be null."); } else { assert.IsNotNull(actual, "The property should not be null."); assert.AreEqual(expected.Name, actual.Name, "The property names are different."); // No need to verify the type of the property, we just need to verify the declaring type, which can't be done here // if the declaring type is the same and the names are the same, it's the same property. } }
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; } }
/// <summary> /// Compares the given values of the given type, and throws a DataComparisonException or AssertionFailedException if values don't match /// </summary> /// <param name="type">The expected type</param> /// <param name="expected">The expected value</param> /// <param name="actual">The actual value</param> /// <param name="assert">The assertion handler to use</param> protected virtual void Compare(QueryScalarType type, object expected, object actual, AssertionHandler assert) { ExceptionUtilities.CheckArgumentNotNull(type, "type"); ExceptionUtilities.CheckArgumentNotNull(assert, "assert"); if (expected == type.NullValue.Value) { assert.IsNull(actual, "Primitive value unexpectedly non-null"); } else { assert.IsNotNull(actual, "Primitive value unexpectedly null"); assert.AreEqual(expected.GetType(), actual.GetType(), EqualityComparer <Type> .Default, "Types did not match"); var comparer = new DelegateBasedEqualityComparer <QueryScalarValue>((v1, v2) => v1.Type.EvaluationStrategy.Compare(v1, v2) == 0); assert.AreEqual(type.CreateValue(expected), type.CreateValue(actual), comparer, "Primitive value did not match"); } }
/// <summary> /// Compares the given values of the given type, and throws a DataComparisonException or AssertionFailedException if values don't match /// </summary> /// <param name="type">The expected type</param> /// <param name="expected">The expected value</param> /// <param name="actual">The actual value</param> /// <param name="assert">The assertion handler to use</param> protected virtual void Compare(QueryScalarType type, object expected, object actual, AssertionHandler assert) { ExceptionUtilities.CheckArgumentNotNull(type, "type"); ExceptionUtilities.CheckArgumentNotNull(assert, "assert"); if (expected == type.NullValue.Value) { assert.IsNull(actual, "Primitive value unexpectedly non-null"); } else { assert.IsNotNull(actual, "Primitive value unexpectedly null"); assert.AreEqual(expected.GetType(), actual.GetType(), EqualityComparer<Type>.Default, "Types did not match"); var comparer = new DelegateBasedEqualityComparer<QueryScalarValue>((v1, v2) => v1.Type.EvaluationStrategy.Compare(v1, v2) == 0); assert.AreEqual(type.CreateValue(expected), type.CreateValue(actual), comparer, "Primitive value did not match"); } }
private static void RunHeaderTest( Func <IEnumerable <KeyValuePair <string, string> > > getHeadersFunc, bool writing, Func <string, string> getHeaderFunc, Action <string, string> setHeaderAction, AssertionHandler assert, IExceptionVerifier exceptionVerifier) { assert.IsNotNull(getHeadersFunc(), "Non-null headers expected."); assert.AreEqual(0, getHeadersFunc().Count(), "Empty header collection expected."); assert.IsNull(getHeaderFunc("a"), "Unexpectedly found header."); ExpectedException expectedException = writing ? null : ODataExpectedExceptions.ODataException("ODataMessage_MustNotModifyMessage"); TestExceptionUtils.ExpectedException( assert, () => { setHeaderAction("a", "b"); assert.AreEqual(1, getHeadersFunc().Count(), "One header expected."); assert.AreEqual("b", getHeaderFunc("a"), "Header not found or invalid header value."); List <KeyValuePair <string, string> > expectedHeaders = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("a", "b") }; VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("a", "c"); assert.AreEqual(1, getHeadersFunc().Count(), "One header expected."); assert.AreEqual("c", getHeaderFunc("a"), "Header not found or invalid header value."); expectedHeaders[0] = new KeyValuePair <string, string>("a", "c"); VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("d", "e"); assert.AreEqual(2, getHeadersFunc().Count(), "Two headers expected."); assert.AreEqual("c", getHeaderFunc("a"), "Header not found or invalid header value."); assert.AreEqual("e", getHeaderFunc("d"), "Header not found or invalid header value."); expectedHeaders.Add(new KeyValuePair <string, string>("d", "e")); VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("d", null); setHeaderAction("a", null); assert.AreEqual(0, getHeadersFunc().Count(), "Empty header collection expected."); assert.IsNull(getHeaderFunc("a"), "Unexpectedly found header."); }, expectedException, exceptionVerifier); }
public static void VerifyPropertiesAreEqual(IEdmProperty expected, IEdmProperty actual, AssertionHandler assert) { if (expected == null) { assert.IsNull(actual, "The property should be null."); } else { assert.IsNotNull(actual, "The property should not be null."); assert.AreEqual(expected.Name, actual.Name, "The property names are different."); // No need to verify the type of the property, we just need to verify the declaring type, which can't be done here // if the declaring type is the same and the names are the same, it's the same property. } }
private static bool VerifyNullnessMatches(object expected, object actual, AssertionHandler assert, string description) { if (expected == null) { assert.IsNull(actual, string.Format("The actual {0} should have been null.", description)); return false; } assert.IsNotNull(actual, string.Format("The actual {0} should have been not-null.", description)); return true; }
public static void VerifyEntitySetsAreEqual(IEdmEntitySet expected, IEdmEntitySet actual, AssertionHandler assert) { if (expected == null) { assert.IsNull(actual, "The entity set should be null."); } else { assert.IsNotNull(actual, "The entity set should not be null."); assert.AreEqual(expected.Name, actual.Name, "The entity set names are different."); } }
private static void RunHeaderTest( Func<IEnumerable<KeyValuePair<string, string>>> getHeadersFunc, bool writing, Func<string, string> getHeaderFunc, Action<string, string> setHeaderAction, AssertionHandler assert, IExceptionVerifier exceptionVerifier) { assert.IsNotNull(getHeadersFunc(), "Non-null headers expected."); assert.AreEqual(0, getHeadersFunc().Count(), "Empty header collection exptected."); assert.IsNull(getHeaderFunc("a"), "Unexpectedly found header."); ExpectedException expectedException = writing ? null : ODataExpectedExceptions.ODataException("ODataMessage_MustNotModifyMessage"); TestExceptionUtils.ExpectedException( assert, () => { setHeaderAction("a", "b"); assert.AreEqual(1, getHeadersFunc().Count(), "One header expected."); assert.AreEqual("b", getHeaderFunc("a"), "Header not found or invalid header value."); List<KeyValuePair<string, string>> expectedHeaders = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("a", "b") }; VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("a", "c"); assert.AreEqual(1, getHeadersFunc().Count(), "One header expected."); assert.AreEqual("c", getHeaderFunc("a"), "Header not found or invalid header value."); expectedHeaders[0] = new KeyValuePair<string, string>("a", "c"); VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("d", "e"); assert.AreEqual(2, getHeadersFunc().Count(), "Two headers expected."); assert.AreEqual("c", getHeaderFunc("a"), "Header not found or invalid header value."); assert.AreEqual("e", getHeaderFunc("d"), "Header not found or invalid header value."); expectedHeaders.Add(new KeyValuePair<string, string>("d", "e")); VerificationUtils.VerifyEnumerationsAreEqual( expectedHeaders, getHeadersFunc(), (first, second, assert2) => assert2.AreEqual(first, second, "Items differ."), (item) => item.Key + " = " + item.Value, assert); setHeaderAction("d", null); setHeaderAction("a", null); assert.AreEqual(0, getHeadersFunc().Count(), "Empty header collection expected."); assert.IsNull(getHeaderFunc("a"), "Unexpectedly found header."); }, expectedException, exceptionVerifier); }
/// <summary> /// Runs a single BufferingJsonReaderTestCaseDescriptor test with a single toggle index in it /// and verifies that the reader state after turning off buffering is correct. /// </summary> /// <param name="testCase">The test case descriptor to run.</param> /// <param name="assert"></param> public static void ReadAndVerifyStateAfterStopBuffering(BufferingJsonReaderTestCaseDescriptor testCase, AssertionHandler assert) { assert.AreEqual(2, testCase.ToggleBufferingCallCounts.Length, "Expected a single toggle position."); TextReader testReader = new StringReader(testCase.JsonText); Exception exception = TestExceptionUtils.RunCatching(() => { BufferingJsonReader bufferingJsonReader = new BufferingJsonReader(testReader, ODataConstants.DefaultMaxRecursionDepth, assert, ODataFormat.Json, isIeee754Compatible: true); int callCount = -1; int startBuffering = testCase.ToggleBufferingCallCounts[0]; int stopBuffering = testCase.ToggleBufferingCallCounts[1]; bool isBuffering = false; List<BufferingJsonReaderTestCaseDescriptor.ReaderNode> bufferedNodes = new List<BufferingJsonReaderTestCaseDescriptor.ReaderNode>(); bool hasMore = false; do { callCount++; if (startBuffering == callCount) { BufferingJsonReaderTestCaseDescriptor.ReaderNode bufferedNode = new BufferingJsonReaderTestCaseDescriptor.ReaderNode(bufferingJsonReader.NodeType, bufferingJsonReader.Value); bufferedNodes.Add(bufferedNode); bufferingJsonReader.StartBuffering(); isBuffering = true; } if (stopBuffering == callCount) { bufferingJsonReader.StopBuffering(); isBuffering = false; assert.AreEqual(bufferedNodes[0].NodeType, bufferingJsonReader.NodeType, "Node types must be equal."); assert.AreEqual(bufferedNodes[0].Value, bufferingJsonReader.Value, "Values must be equal."); bufferedNodes.RemoveAt(0); } hasMore = bufferingJsonReader.Read(); if (isBuffering) { bufferedNodes.Add(new BufferingJsonReaderTestCaseDescriptor.ReaderNode(bufferingJsonReader.NodeType, bufferingJsonReader.Value)); } else if (bufferedNodes.Count > 0) { assert.AreEqual(bufferedNodes[0].NodeType, bufferingJsonReader.NodeType, "Node types must be equal."); assert.AreEqual(bufferedNodes[0].Value, bufferingJsonReader.Value, "Values must be equal."); bufferedNodes.RemoveAt(0); } } while (hasMore); }); assert.IsNull(exception, "Did not expect an exception."); }
public static void VerifyServiceOperationsAreEqual(IEdmOperationImport expected, IEdmOperationImport actual, AssertionHandler assert) { if (expected == null) { assert.IsNull(actual, "The service operation should be null."); } else { assert.IsNotNull(actual, "The service operation should not be null."); assert.AreEqual(expected.Name, actual.Name, "The service operation names are different."); } }