public void TypeSlimExtensions_IsNumeric() { var types = new[] { typeof(char), typeof(byte), typeof(short), typeof(int), typeof(long), typeof(sbyte), typeof(ushort), typeof(uint), typeof(ulong), typeof(float), typeof(double), }; foreach (var t in types) { var n = typeof(Nullable <>).MakeGenericType(t); Assert.IsTrue(TypeSlimExtensions.IsNumeric(t.ToTypeSlim())); Assert.IsTrue(TypeSlimExtensions.IsNumeric(n.ToTypeSlim())); } Assert.IsFalse(TypeSlimExtensions.IsNumeric(typeof(ConsoleColor).ToTypeSlim())); Assert.IsFalse(TypeSlimExtensions.IsNumeric(typeof(string).ToTypeSlim())); Assert.IsTrue(TypeSlimExtensions.IsNumeric(typeof(decimal).ToTypeSlim(), includeDecimal: true)); Assert.IsTrue(TypeSlimExtensions.IsNumeric(typeof(decimal?).ToTypeSlim(), includeDecimal: true)); Assert.IsFalse(TypeSlimExtensions.IsNumeric(typeof(decimal).ToTypeSlim(), includeDecimal: false)); Assert.IsFalse(TypeSlimExtensions.IsNumeric(typeof(decimal?).ToTypeSlim(), includeDecimal: false)); }
public void PropertyInfoSlim_ArgumentChecks() { AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetProperty(type: null, "bar", propertyType: null, indexParameterTypes: null, canWrite: false), ex => Assert.AreEqual("type", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetProperty(name: null, propertyType: null, indexParameterTypes: null, canWrite: false), ex => Assert.AreEqual("name", ex.ParamName)); AssertEx.ThrowsException <ArgumentException>(() => SlimType.GetProperty("", propertyType: null, indexParameterTypes: null, canWrite: false), ex => Assert.AreEqual("name", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetProperty("foo", propertyType: null, indexParameterTypes: null, canWrite: false), ex => Assert.AreEqual("indexParameterTypes", ex.ParamName)); }
public void TypeSlimExtensions_GetConstructor_ArgumentChecking() { var ts = typeof(int).ToTypeSlim(); Assert.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetConstructor(type: null, Array.Empty <TypeSlim>().ToReadOnly())); Assert.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetConstructor(ts, parameterTypes: null)); }
public void TypeSlimExtensions_GetActionType() { var eq = new TypeSlimEqualityComparer(); var action0_0 = TypeSlimExtensions.GetActionType(1); var action0_1 = TypeSlimExtensions.GetActionType(1); var action0_2 = typeof(Action <>).ToTypeSlim(); Assert.IsTrue(eq.Equals(action0_0, action0_1)); Assert.IsTrue(eq.Equals(action0_0, action0_2)); var action1_0 = TypeSlimExtensions.GetActionType(2); var action1_1 = TypeSlimExtensions.GetActionType(2); var action1_2 = typeof(Action <,>).ToTypeSlim(); Assert.IsTrue(eq.Equals(action1_0, action1_1)); Assert.IsTrue(eq.Equals(action1_0, action1_2)); var action7_0 = TypeSlimExtensions.GetActionType(7); var action7_1 = TypeSlimExtensions.GetActionType(7); var action7_2 = typeof(Action <, , , , , ,>).ToTypeSlim(); Assert.IsTrue(eq.Equals(action7_0, action7_1)); Assert.IsTrue(eq.Equals(action7_0, action7_2)); }
public void TypeSlimExtensions_IsVoidType() { Assert.IsTrue(TypeSlimExtensions.IsVoidType(TypeSlimExtensions.VoidType)); Assert.IsTrue(TypeSlimExtensions.IsVoidType(typeof(void).ToTypeSlim())); Assert.IsFalse(TypeSlimExtensions.IsVoidType(typeof(int).ToTypeSlim())); }
public void TypeSlimExtensions_IsBooleanType() { Assert.IsTrue(TypeSlimExtensions.IsBooleanType(TypeSlimExtensions.BooleanType)); Assert.IsTrue(TypeSlimExtensions.IsBooleanType(typeof(bool).ToTypeSlim())); Assert.IsFalse(TypeSlimExtensions.IsBooleanType(typeof(int).ToTypeSlim())); }
public void TypeSlimExtensions_GetFuncType() { var eq = new TypeSlimEqualityComparer(); var func0_0 = TypeSlimExtensions.GetFuncType(0); var func0_1 = TypeSlimExtensions.GetFuncType(0); var func0_2 = typeof(Func <>).ToTypeSlim(); Assert.IsTrue(eq.Equals(func0_0, func0_1)); Assert.IsTrue(eq.Equals(func0_0, func0_2)); var func1_0 = TypeSlimExtensions.GetFuncType(1); var func1_1 = TypeSlimExtensions.GetFuncType(1); var func1_2 = typeof(Func <,>).ToTypeSlim(); Assert.IsTrue(eq.Equals(func1_0, func1_1)); Assert.IsTrue(eq.Equals(func1_0, func1_2)); var func7_0 = TypeSlimExtensions.GetFuncType(7); var func7_1 = TypeSlimExtensions.GetFuncType(7); var func7_2 = typeof(Func <, , , , , , ,>).ToTypeSlim(); Assert.IsTrue(eq.Equals(func7_0, func7_1)); Assert.IsTrue(eq.Equals(func7_0, func7_2)); }
public void TypeSlimExtensions_IsValueType() { Assert.IsTrue(TypeSlimExtensions.IsValueType(typeof(int).ToTypeSlim())); Assert.IsTrue(TypeSlimExtensions.IsValueType(typeof(TimeSpan).ToTypeSlim())); Assert.IsFalse(TypeSlimExtensions.IsValueType(typeof(string).ToTypeSlim())); Assert.IsFalse(TypeSlimExtensions.IsValueType(typeof(AppDomain).ToTypeSlim())); }
public void SimpleMethodInfoSlimBase_ArgumentChecks() { AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetSimpleMethod(type: null, "bar", parameterTypes: null, returnType: null), ex => Assert.AreEqual("type", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetSimpleMethod(name: null, parameterTypes: null, returnType: null), ex => Assert.AreEqual("name", ex.ParamName)); AssertEx.ThrowsException <ArgumentException>(() => SlimType.GetSimpleMethod("", parameterTypes: null, returnType: null), ex => Assert.AreEqual("name", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetSimpleMethod("foo", parameterTypes: null, returnType: null), ex => Assert.AreEqual("parameterTypes", ex.ParamName)); var m = SlimType.GetSimpleMethod("foo", Empty, returnType: null); }
public void GenericDefinitionMethodInfoSlim_ArgumentChecks() { AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetGenericDefinitionMethod(type: null, "bar", genericParameterTypes: null, parameterTypes: null, returnType: null), ex => Assert.AreEqual("type", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetGenericDefinitionMethod(name: null, genericParameterTypes: null, parameterTypes: null, returnType: null), ex => Assert.AreEqual("name", ex.ParamName)); AssertEx.ThrowsException <ArgumentException>(() => SlimType.GetGenericDefinitionMethod("", genericParameterTypes: null, parameterTypes: null, returnType: null), ex => Assert.AreEqual("name", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetGenericDefinitionMethod("foo", genericParameterTypes: null, parameterTypes: null, returnType: null), ex => Assert.AreEqual("parameterTypes", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetGenericDefinitionMethod("foo", genericParameterTypes: null, Empty, returnType: null), ex => Assert.AreEqual("genericParameterTypes", ex.ParamName)); AssertEx.ThrowsException <ArgumentOutOfRangeException>(() => SlimType.GetGenericDefinitionMethod("foo", Empty, Empty, returnType: null), ex => Assert.AreEqual("genericParameterTypes", ex.ParamName)); }
public void FieldInfoSlim_ArgumentChecks() { AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetField(type: null, "bar", fieldType: null), ex => Assert.AreEqual("type", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetField(name: null, fieldType: null), ex => Assert.AreEqual("name", ex.ParamName)); AssertEx.ThrowsException <ArgumentException>(() => SlimType.GetField("", fieldType: null), ex => Assert.AreEqual("name", ex.ParamName)); var f = SlimType.GetField("value", fieldType: null); Assert.IsNull(f.FieldType); }
public void GenericMethodInfoSlim_ArgumentChecks() { AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetGenericMethod(type: null, methodDefinition: null, arguments: null), ex => Assert.AreEqual("type", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetGenericMethod(methodDefinition: null, arguments: null), ex => Assert.AreEqual("methodDefinition", ex.ParamName)); var def = SlimType.GetGenericDefinitionMethod("Foo", new List <TypeSlim> { SlimType }.AsReadOnly(), EmptyReadOnlyCollection <TypeSlim> .Instance, returnType: null); AssertEx.ThrowsException <ArgumentNullException>(() => def.DeclaringType.GetGenericMethod(def, arguments: null), ex => Assert.AreEqual("arguments", ex.ParamName)); }
public void TypeSlimExtensions_IsIntegerOrBool() { var types = new[] { typeof(byte), typeof(short), typeof(int), typeof(long), typeof(sbyte), typeof(ushort), typeof(uint), typeof(ulong), typeof(bool), }; var ntypes = new[] { typeof(char), typeof(float), typeof(double), }; foreach (var t in types) { var n = typeof(Nullable <>).MakeGenericType(t); Assert.IsTrue(TypeSlimExtensions.IsIntegerOrBool(t.ToTypeSlim())); Assert.IsTrue(TypeSlimExtensions.IsIntegerOrBool(n.ToTypeSlim())); } foreach (var t in ntypes) { var n = typeof(Nullable <>).MakeGenericType(t); Assert.IsFalse(TypeSlimExtensions.IsIntegerOrBool(t.ToTypeSlim())); Assert.IsFalse(TypeSlimExtensions.IsIntegerOrBool(n.ToTypeSlim())); } Assert.IsFalse(TypeSlimExtensions.IsIntegerOrBool(typeof(ConsoleColor).ToTypeSlim())); Assert.IsFalse(TypeSlimExtensions.IsIntegerOrBool(typeof(string).ToTypeSlim())); }
private static void AssertAreSameMethod(MethodInfo methodInfo, MethodInfoSlim methodInfoSlim) { Assert.IsTrue(TypeSlimExtensions.Equals(methodInfo.ReturnType.ToTypeSlim(), methodInfoSlim.ReturnType)); Assert.IsTrue(methodInfoSlim.ParameterTypes.SequenceEqual(methodInfo.GetParameters().Select(p => p.ParameterType.ToTypeSlim()))); switch (methodInfoSlim.Kind) { case MethodInfoSlimKind.Simple: AssertAreSameSimpleMethod(methodInfo, (SimpleMethodInfoSlim)methodInfoSlim); return; case MethodInfoSlimKind.GenericDefinition: Assert.IsTrue(methodInfo.IsGenericMethodDefinition); AssertAreSameGenericDefinitionMethod(methodInfo, (GenericDefinitionMethodInfoSlim)methodInfoSlim); return; case MethodInfoSlimKind.Generic: AssertAreSameGenericMethod(methodInfo, (GenericMethodInfoSlim)methodInfoSlim); return; } Fail(methodInfo, methodInfoSlim); }
private static void AssertAreSameField(FieldInfo fieldInfo, FieldInfoSlim fieldInfoSlim) { Assert.AreEqual(fieldInfo.Name, fieldInfoSlim.Name); Assert.IsTrue(TypeSlimExtensions.Equals(fieldInfo.DeclaringType.ToTypeSlim(), fieldInfoSlim.DeclaringType)); Assert.IsTrue(TypeSlimExtensions.Equals(fieldInfo.FieldType.ToTypeSlim(), fieldInfoSlim.FieldType)); }
private static void AssertAreSameProperty(PropertyInfo propertyInfo, PropertyInfoSlim propertyInfoSlim) { Assert.AreEqual(propertyInfo.Name, propertyInfoSlim.Name); Assert.IsTrue(TypeSlimExtensions.Equals(propertyInfo.DeclaringType.ToTypeSlim(), propertyInfoSlim.DeclaringType)); Assert.IsTrue(TypeSlimExtensions.Equals(propertyInfo.PropertyType.ToTypeSlim(), propertyInfoSlim.PropertyType)); }
public void ConstructorInfoSlim_ArgumentChecks() { AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetConstructor(type: null, Empty), ex => Assert.AreEqual("type", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetConstructor(parameterTypes: null), ex => Assert.AreEqual("parameterTypes", ex.ParamName)); }
public void TypeSlimExtensions_ToType_ArgumentChecking() { Assert.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.ToType(type: null)); }
private static void AssertAreSameConstructor(ConstructorInfo constructorInfo, ConstructorInfoSlim constructorInfoSlim) { Assert.IsTrue(TypeSlimExtensions.Equals(constructorInfo.DeclaringType.ToTypeSlim(), constructorInfoSlim.DeclaringType)); Assert.IsTrue(constructorInfoSlim.ParameterTypes.SequenceEqual(constructorInfo.GetParameters().Select(p => p.ParameterType.ToTypeSlim()))); }