private void ValidateActionReturnType(Type typeOfExpectedReturnValue, bool canBeAssignable = false, bool allowDifferentGenericTypeDefinitions = false) { CommonValidator.CheckForException(this.CaughtException); var typeInfoOfExpectedReturnValue = typeOfExpectedReturnValue.GetTypeInfo(); var typeOfActionResult = this.ActionResult?.GetType(); if (typeOfActionResult == null) { this.ThrowNewGenericActionResultAssertionException( typeOfExpectedReturnValue.ToFriendlyTypeName(), "null"); } var typeInfoOfActionResult = typeOfActionResult.GetTypeInfo(); var isAssignableCheck = canBeAssignable && Reflection.AreNotAssignable(typeOfExpectedReturnValue, typeOfActionResult); if (isAssignableCheck && allowDifferentGenericTypeDefinitions && Reflection.IsGeneric(typeOfExpectedReturnValue) && Reflection.IsGenericTypeDefinition(typeOfExpectedReturnValue)) { isAssignableCheck = Reflection.AreAssignableByGeneric(typeOfExpectedReturnValue, typeOfActionResult); if (!isAssignableCheck && !typeInfoOfActionResult.IsGenericType) { isAssignableCheck = true; } else { isAssignableCheck = !Reflection.ContainsGenericTypeDefinitionInterface(typeOfExpectedReturnValue, typeOfActionResult); } } var strictlyEqualCheck = !canBeAssignable && Reflection.AreDifferentTypes(typeOfExpectedReturnValue, typeOfActionResult); var invalid = isAssignableCheck || strictlyEqualCheck; if (invalid && typeInfoOfExpectedReturnValue.IsGenericTypeDefinition && typeInfoOfActionResult.IsGenericType) { var actionResultGenericDefinition = typeInfoOfActionResult.GetGenericTypeDefinition(); if (actionResultGenericDefinition == typeOfExpectedReturnValue) { invalid = false; } } if (invalid && typeInfoOfExpectedReturnValue.IsGenericType && typeInfoOfActionResult.IsGenericType) { invalid = !Reflection.AreAssignableByGeneric(typeOfExpectedReturnValue, typeOfActionResult); } if (invalid) { this.ThrowNewGenericActionResultAssertionException( typeOfExpectedReturnValue.ToFriendlyTypeName(), typeOfActionResult.ToFriendlyTypeName()); } }
public void AreAssignableShouldReturnFalseWithOneGenericTypeDefinition() { var baseType = typeof(IEnumerable <>); var inheritedType = typeof(IList <int>); Assert.False(Reflection.AreAssignable(baseType, inheritedType)); Assert.True(Reflection.AreNotAssignable(baseType, inheritedType)); }
public void AreAssignableShouldReturnTrueWithInvertedInheritedTypes() { var baseType = typeof(IList <int>); var inheritedType = typeof(IEnumerable <int>); Assert.False(Reflection.AreAssignable(baseType, inheritedType)); Assert.True(Reflection.AreNotAssignable(baseType, inheritedType)); }
public void AreAssignableShouldReturnTrueWithTheSameTypes() { var first = typeof(int); var second = typeof(int); Assert.True(Reflection.AreAssignable(first, second)); Assert.False(Reflection.AreNotAssignable(first, second)); }
/// <summary> /// Testuje, či je obsah správy HTTP odpovede určitého typu. /// </summary> /// <typeparam name="TContentType">Type of expected HTTP content.</typeparam> /// <param name="content">Actual HTTP content.</param> /// <param name="failedValidationAction">Action to call in case of failed validation.</param> public static void WithContentOfType <TContentType>( HttpContent content, Action <string, string, string> failedValidationAction) where TContentType : HttpContent { var expectedType = typeof(TContentType); var actualType = content.GetType(); if (Reflection.AreNotAssignable(actualType, expectedType)) { failedValidationAction( "content", string.Format("to be {0}", expectedType.ToFriendlyTypeName()), string.Format("was in fact {0}", actualType.ToFriendlyTypeName())); } }
public static void ValidateInvocationResultType( ComponentTestContext testContext, Type typeOfExpectedReturnValue, bool canBeAssignable = false, bool allowDifferentGenericTypeDefinitions = false) { InvocationValidator.CheckForException(testContext.CaughtException, testContext.ExceptionMessagePrefix); var typeInfoOfExpectedReturnValue = typeOfExpectedReturnValue.GetTypeInfo(); var typeOfActionResult = testContext.MethodResult?.GetType(); if (typeOfActionResult == null) { ThrowNewInvocationResultAssertionException( testContext, typeOfExpectedReturnValue.ToFriendlyTypeName(), "null"); } var typeInfoOfActionResult = typeOfActionResult.GetTypeInfo(); var isAssignableCheck = canBeAssignable && Reflection.AreNotAssignable(typeOfExpectedReturnValue, typeOfActionResult); if (isAssignableCheck && allowDifferentGenericTypeDefinitions && Reflection.IsGeneric(typeOfExpectedReturnValue) && Reflection.IsGenericTypeDefinition(typeOfExpectedReturnValue)) { isAssignableCheck = Reflection.AreAssignableByGeneric(typeOfExpectedReturnValue, typeOfActionResult); if (!isAssignableCheck && !typeInfoOfActionResult.IsGenericType) { isAssignableCheck = true; } else { isAssignableCheck = !Reflection.ContainsGenericTypeDefinitionInterface(typeOfExpectedReturnValue, typeOfActionResult); } } var strictlyEqualCheck = !canBeAssignable && Reflection.AreDifferentTypes(typeOfExpectedReturnValue, typeOfActionResult); var invalid = isAssignableCheck || strictlyEqualCheck; if (invalid && typeInfoOfExpectedReturnValue.IsGenericTypeDefinition && typeInfoOfActionResult.IsGenericType) { var actionResultGenericDefinition = typeInfoOfActionResult.GetGenericTypeDefinition(); if (actionResultGenericDefinition == typeOfExpectedReturnValue) { invalid = false; } } if (invalid && typeInfoOfExpectedReturnValue.IsGenericType && typeInfoOfActionResult.IsGenericType) { invalid = !Reflection.AreAssignableByGeneric(typeOfExpectedReturnValue, typeOfActionResult); } if (invalid) { ThrowNewInvocationResultAssertionException( testContext, typeOfExpectedReturnValue.ToFriendlyTypeName(), typeOfActionResult.ToFriendlyTypeName()); } }