/// <summary> /// Asserts that the given action throws the specified exception type. /// </summary> /// <param name="action">Action to try.</param> /// <param name="expectedException">The expected exception.</param> /// <param name="exceptionVerifier">The exception verifier.</param> public static void ExpectedException(this AssertionHandler assert, Action action, ExpectedException expectedException, IExceptionVerifier exceptionVerifier) { ExceptionUtilities.CheckArgumentNotNull(assert, "assert"); ExceptionUtilities.CheckArgumentNotNull(action, "action"); ExceptionUtilities.CheckArgumentNotNull(exceptionVerifier, "exceptionVerifier"); Exception exception = RunCatching(action); if (exception == null && expectedException == null) { return; } else if (exception == null) { assert.IsNotNull(exception, "Expected exception of type '{0}' with message resource ID '{1}' but none was thrown.", expectedException.ExpectedExceptionType.ToString(), expectedException.ExpectedMessage == null ? "<null>" : expectedException.ExpectedMessage.ResourceIdentifier); } else if (expectedException == null) { assert.IsNotNull(expectedException, "Did not expect an exception but an exception of type '{0}' with message '{1}' was thrown.", exception.GetType().ToString(), exception.Message); } exception = TestExceptionUtils.UnwrapAggregateException(exception, assert); exceptionVerifier.VerifyExceptionResult(expectedException, exception); }
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."); } }
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; } }
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. } }
private static void CompareStructuralValue(IEdmValue edmValue, ODataComplexValue complexValue, AssertionHandler assert) { assert.IsNotNull(edmValue, "EDM value instance must not be null."); if (complexValue == null) { ValidateNullValue(edmValue, assert); return; } assert.AreEqual(EdmValueKind.Structured, edmValue.ValueKind, "Value kinds differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Complex, edmValue.Type.TypeKind(), "Type kinds differ."); } CompareStructuralValue(edmValue, complexValue.Properties, assert); }
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); }
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; }
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); }