/// <summary> /// Asserts that the type being operated on is abstract /// </summary> /// <param name="type"></param> public static void ShouldBeAbstract(this Type type) { if (!type.IsAbstract) { Assert.Fail(type.PrettyName() + " should be abstract"); } }
/// <summary> /// Asserts that the given type being operated on should not /// be assignable from the given type /// </summary> /// <param name="type"></param> /// <param name="shouldNotBeImplemented"></param> public static void ShouldNotBeAssignableFrom(this Type type, Type shouldNotBeImplemented) { if (shouldNotBeImplemented.IsAssignableFrom(type)) { Assert.Fail(type.PrettyName() + " should not implement " + shouldNotBeImplemented.PrettyName()); } }
/// <summary> /// Asserts that a type implements the provided interface type /// </summary> /// <param name="type"></param> /// <param name="shouldImplementType">Provided interface type</param> public static void ShouldImplement(this Type type, Type shouldImplementType) { if (!shouldImplementType.IsInterface) { Assert.Fail(type.PrettyName() + " is not an interface"); } type.ShouldBeAssignableFrom(shouldImplementType); }
/// <summary> /// Asserts that the type being operated on /// should not inherit from the given type /// </summary> /// <param name="type"></param> /// <param name="shouldNotBeAncestor"></param> public static void ShouldNotInheritFrom(this Type type, Type shouldNotBeAncestor) { if (shouldNotBeAncestor.IsInterface) { Assert.Fail(shouldNotBeAncestor.PrettyName() + " is not a class"); } ShouldNotBeAssignableFrom(type, shouldNotBeAncestor); }
/// <summary> /// Asserts that a type has a method with the given name and void return /// </summary> /// <param name="type"></param> /// <param name="methodName"></param> public static void ShouldHaveActionMethodWithName(this Type type, string methodName) { var hasMethod = type.HasActionMethodWithName(methodName); if (hasMethod) { return; } Assert.Fail("Expected to find method '" + methodName + "' on type '" + type.PrettyName() + "' but didn't."); }
/// <summary> /// Asserts that a type does not have the specified property /// by name, and optionally by name and type /// </summary> /// <param name="type"></param> /// <param name="name"></param> /// <param name="withType"></param> public static void ShouldNotHaveProperty(this Type type, string name, Type withType = null) { Assert.IsNotNull(type, "Cannot interrogate properties on NULL type"); var propertyInfo = FindPropertyInfoForPath(type, name); if (withType == null) { Assert.IsNull(propertyInfo, $"Expected not to find property {name} on type {type.Name}"); } if (propertyInfo != null && propertyInfo.PropertyType == withType) { Assert.Fail($"Expected not to find property {name} with type {withType} on type {type.Name}"); } }
/// <summary> /// Asserts that the type has a non-public method with the provided /// name /// </summary> /// <param name="type"></param> /// <param name="name"></param> public static void ShouldHaveNonPublicMethod(this Type type, string name) { var methodInfo = type.GetMethod(name, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (methodInfo == null) { Assert.Fail($"Method not found: {name}"); } // ReSharper disable once PossibleNullReferenceException if (methodInfo.IsPublic) { Assert.Fail($"Expected method '{name}' not to be public"); } }
/// <summary> /// Asserts that a read-only property specified by /// name and property type exists on the type /// being operated on /// </summary> /// <param name="type"></param> /// <param name="name"></param> /// <param name="withType"></param> public static void ShouldHaveReadOnlyProperty( this Type type, string name, Type withType = null ) { var propInfo = FindPropertyInfoForPath(type, name, Assert.Fail); if (withType != null) { Assert.AreEqual(withType, propInfo.PropertyType, $"Expected {type.Name}.{name} to have type {withType}, but found {propInfo.PropertyType}"); } Assert.IsNull(propInfo.GetSetMethod(), $"Expected {type.Name}.{name} to be read-only"); }
/// <summary> /// Asserts that a type has the specified property by name and type /// </summary> /// <param name="type"></param> /// <param name="name"></param> /// <param name="withType"></param> /// <param name="shouldBeVirtual"></param> public static void ShouldHaveProperty(this Type type, string name, Type withType = null, bool shouldBeVirtual = false) { var propertyInfo = GetPropertyForPath(type, name); if (withType != null && withType != propertyInfo.PropertyType) { Assert.AreEqual(withType, propertyInfo.PropertyType, "Found property '" + name + "' but not with expected type '" + withType.PrettyName() + "'"); } if (shouldBeVirtual) { Assert.IsTrue(propertyInfo.GetAccessors().First().IsVirtual); } }
private static void ShouldHaveEnumValueInternal(this Type type, string valueName, int?expectedValue) { if (!type.IsEnum) { throw new InvalidOperationException($"{type.PrettyName()} is not an enum type"); } var enumValues = Enum.GetValues(type); foreach (var value in enumValues) { var thisValueName = Enum.GetName(type, value); if (thisValueName == valueName) { if (expectedValue == null || (int)value == expectedValue.Value) { return; } Assert.Fail( $"Could not find enum key \"{valueName}\" with value \"{expectedValue}\" on enum {type.PrettyName()}" ); } } Assert.Fail($"Could not find value \"{valueName}\" on enum {type.PrettyName()}"); }