Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<Compile Include="UnitTestGenericArrayArgumentTests.cs" />
<Compile Include="UnitTestDualInstantiationTests.cs" />
<Compile Include="UnitTestInvokeTargetTests.cs" />
<Compile Include="UnitTestConstructorVisibilityTests.cs" />
<Compile Include="UnitTestGenericLimitTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
20 changes: 10 additions & 10 deletions Tests/NFUnitTestSystemLibReflection3/UnitTestActivatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ public void CreateInstanceOfT_ReturnsTheTypedInstance()
}

[TestMethod]
public void CreateInstanceOfT_WithoutAPublicParameterlessConstructor_ThrowsNotSupportedException_Deviation()
{
Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance<string>());
Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance<AcAbstract>());
Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance<AcOnlyArgument>());
Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance<AcPrivate>());
Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance<AcProtected>());
Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance<IAcMark>());
Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance<int[]>());
Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance<Action>());
public void CreateInstanceOfT_WithoutAPublicParameterlessConstructor_ThrowsMissingMethodException_Deviation()
{
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<string>());
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<AcAbstract>());
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<AcOnlyArgument>());
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<AcPrivate>());
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<AcProtected>());
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<IAcMark>());
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<int[]>());
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<Action>());
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Reflection;
using nanoFramework.TestFramework;

namespace NFUnitTestSystemLibReflection3
{
public class ClassWithPublicConstructor
{
public int X = 1;
}

public class ClassWithPrivateConstructors
{
public int X;

private ClassWithPrivateConstructors()
{
X = 2;
}

private ClassWithPrivateConstructors(int x)
{
X = x;
}
}

public class ClassWithProtectedConstructor
{
protected ClassWithProtectedConstructor()
{
}
}

public class ClassWithInternalConstructor
{
internal ClassWithInternalConstructor()
{
}
}

public class ClassWithMixedConstructors
{
public ClassWithMixedConstructors()
{
}

public ClassWithMixedConstructors(char c)
{
}

protected ClassWithMixedConstructors(string s)
{
}

internal ClassWithMixedConstructors(long l)
{
}

private ClassWithMixedConstructors(int i)
{
}
}

public abstract class AbstractClassWithPublicConstructor
{
public AbstractClassWithPublicConstructor()
{
}
}

public abstract class AbstractGenericClass<T>
{
public AbstractGenericClass()
{
}
}

public class GenericClassWithPublicConstructor<T>
{
public GenericClassWithPublicConstructor()
{
}
}

public interface IMarkerInterface
{
}

[TestClass]
public class UnitTestConstructorVisibilityTests
{
[TestMethod]
public void GetConstructor_PublicParameterless_IsFound()
{
ConstructorInfo constructor = typeof(ClassWithPublicConstructor).GetConstructor(new Type[0]);

Assert.IsNotNull(constructor);
Assert.IsTrue(constructor.IsPublic);
}

[TestMethod]
public void GetConstructor_NonPublicParameterless_IsNull()
{
Assert.IsNull(typeof(ClassWithPrivateConstructors).GetConstructor(new Type[0]));
Assert.IsNull(typeof(ClassWithProtectedConstructor).GetConstructor(new Type[0]));
Assert.IsNull(typeof(ClassWithInternalConstructor).GetConstructor(new Type[0]));
}

[TestMethod]
public void GetConstructor_NonPublicWithParameters_IsNull()
{
Assert.IsNull(typeof(ClassWithPrivateConstructors).GetConstructor(new Type[] { typeof(int) }));
Assert.IsNull(typeof(ClassWithMixedConstructors).GetConstructor(new Type[] { typeof(int) }));
Assert.IsNull(typeof(ClassWithMixedConstructors).GetConstructor(new Type[] { typeof(string) }));
Assert.IsNull(typeof(ClassWithMixedConstructors).GetConstructor(new Type[] { typeof(long) }));
Assert.IsNotNull(typeof(ClassWithMixedConstructors).GetConstructor(new Type[] { typeof(char) }));
}

[TestMethod]
public void GetConstructors_ReturnsOnlyPublicOnes()
{
Assert.AreEqual(2, typeof(ClassWithMixedConstructors).GetConstructors().Length);
Assert.AreEqual(0, typeof(ClassWithPrivateConstructors).GetConstructors().Length);
}

[TestMethod]
public void Invoke_PublicConstructor_CreatesTheObject()
{
object created = typeof(ClassWithPublicConstructor).GetConstructor(new Type[0]).Invoke(null);

Assert.IsInstanceOfType(created, typeof(ClassWithPublicConstructor));
Assert.AreEqual(1, ((ClassWithPublicConstructor)created).X);

Assert.IsInstanceOfType(typeof(GenericClassWithPublicConstructor<int>).GetConstructor(new Type[0]).Invoke(null), typeof(GenericClassWithPublicConstructor<int>));
}

[TestMethod]
public void Invoke_AbstractClassConstructor_ThrowsMemberAccessException()
{
ConstructorInfo constructor = typeof(AbstractClassWithPublicConstructor).GetConstructor(new Type[0]);

Assert.IsNotNull(constructor);
Assert.ThrowsException(typeof(MemberAccessException), () => constructor.Invoke(null));

ConstructorInfo closed = typeof(AbstractGenericClass<int>).GetConstructor(new Type[0]);

Assert.ThrowsException(typeof(MemberAccessException), () => closed.Invoke(null));
}

[TestMethod]
public void Invoke_OpenGenericConstructor_ThrowsMemberAccessException()
{
ConstructorInfo open = typeof(GenericClassWithPublicConstructor<>).GetConstructor(new Type[0]);

Assert.IsNotNull(open);
Assert.ThrowsException(typeof(MemberAccessException), () => open.Invoke(null));

ConstructorInfo openAbstract = typeof(AbstractGenericClass<>).GetConstructor(new Type[0]);

Assert.ThrowsException(typeof(MemberAccessException), () => openAbstract.Invoke(null));
}

[TestMethod]
public void InterfaceType_HasNoConstructors()
{
Assert.IsNull(typeof(IMarkerInterface).GetConstructor(new Type[0]));
Assert.AreEqual(0, typeof(IMarkerInterface).GetConstructors().Length);
}

[TestMethod]
public void Activator_AbstractOrNonPublic_ThrowsMissingMethodException()
{
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<AbstractClassWithPublicConstructor>());
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<ClassWithPrivateConstructors>());
Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance<IMarkerInterface>());
Assert.IsNotNull(Activator.CreateInstance<ClassWithPublicConstructor>());
}
}
}
126 changes: 126 additions & 0 deletions Tests/NFUnitTestSystemLibReflection3/UnitTestGenericLimitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Reflection;
using nanoFramework.TestFramework;

namespace NFUnitTestSystemLibReflection3
{
public class NestableBox<T>
{
public T V;
}

public class GenericWith15Arguments<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>
{
}

public class GenericWith16Arguments<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>
{
}

public static class GenericCastHelper
{
public static bool Is<T>(object o)
{
return o is T;
}

public static object Cast<T>(object o)
{
return (T)o;
}
}

[TestClass]
public class UnitTestGenericLimitTests
{
[TestMethod]
public void Cast_WithinTheLimits_ComparesTheArguments()
{
object many = new GenericWith15Arguments<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int>();
object box = new NestableBox<NestableBox<NestableBox<NestableBox<int>>>>();

Assert.IsTrue(many is GenericWith15Arguments<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int>);
Assert.IsFalse(many is GenericWith15Arguments<int, int, int, int, int, int, int, int, int, int, int, int, int, int, string>);
Assert.ThrowsException(
typeof(InvalidCastException),
() => { var other = (GenericWith15Arguments<int, int, int, int, int, int, int, int, int, int, int, int, int, int, string>)many; });

Assert.IsTrue(box is NestableBox<NestableBox<NestableBox<NestableBox<int>>>>);
Assert.IsFalse(box is NestableBox<NestableBox<NestableBox<NestableBox<string>>>>);
Assert.IsTrue(GenericCastHelper.Is<NestableBox<NestableBox<NestableBox<NestableBox<int>>>>>(box));
Assert.IsFalse(GenericCastHelper.Is<NestableBox<NestableBox<NestableBox<NestableBox<string>>>>>(box));
}

[TestMethod]
public void Cast_ToTypeWithTooManyNodes_IsNotSupported()
{
object many = new GenericWith16Arguments<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int>();

Assert.ThrowsException(
typeof(NotSupportedException),
() => { bool b = many is GenericWith16Arguments<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, string>; });
Assert.ThrowsException(
typeof(NotSupportedException),
() => { var other = (GenericWith16Arguments<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, string>)many; });
Assert.ThrowsException(
typeof(NotSupportedException),
() => GenericCastHelper.Is<GenericWith16Arguments<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, string>>(many));
Assert.ThrowsException(
typeof(NotSupportedException),
() => GenericCastHelper.Cast<GenericWith16Arguments<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, string>>(many));
}

[TestMethod]
public void Cast_ToTypeNestedTooDeep_IsNotSupported()
{
object box = new NestableBox<NestableBox<NestableBox<NestableBox<string>>>>();

Assert.ThrowsException(typeof(NotSupportedException), () => GenericCastHelper.Is<NestableBox<NestableBox<NestableBox<NestableBox<NestableBox<int>>>>>>(box));
Assert.ThrowsException(typeof(NotSupportedException), () => GenericCastHelper.Cast<NestableBox<NestableBox<NestableBox<NestableBox<NestableBox<int>>>>>>(box));
}

[TestMethod]
public void Cast_ArrayToArrayOfTypeNestedTooDeep_IsNotSupported()
{
object boxes = new NestableBox<NestableBox<NestableBox<NestableBox<string>>>>[1];

Assert.ThrowsException(
typeof(NotSupportedException),
() => { bool b = boxes is NestableBox<NestableBox<NestableBox<NestableBox<NestableBox<int>>>>>[]; });
Assert.ThrowsException(
typeof(NotSupportedException),
() => GenericCastHelper.Is<NestableBox<NestableBox<NestableBox<NestableBox<NestableBox<int>>>>>[]>(boxes));
Assert.ThrowsException(
typeof(NotSupportedException),
() => GenericCastHelper.Cast<NestableBox<NestableBox<NestableBox<NestableBox<NestableBox<int>>>>>[]>(boxes));
Assert.IsFalse(boxes is NestableBox<NestableBox<NestableBox<NestableBox<int>>>>[]);
}

[TestMethod]
public void MakeGenericType_OverTheLimits_IsNotSupported()
{
Type[] sixteen = new Type[16];

for (int i = 0; i < sixteen.Length; i++)
{
sixteen[i] = typeof(int);
}

Assert.ThrowsException(typeof(NotSupportedException), () => typeof(GenericWith16Arguments<,,,,,,,,,,,,,,,>).MakeGenericType(sixteen));

Type deep = typeof(int);

for (int i = 0; i < 4; i++)
{
deep = typeof(NestableBox<>).MakeGenericType(deep);
}

Type tooDeep = deep;

Assert.ThrowsException(typeof(NotSupportedException), () => typeof(NestableBox<>).MakeGenericType(tooDeep));
}
}
}
3 changes: 3 additions & 0 deletions nanoFramework.CoreLibrary/CoreLibrary.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
<Compile Include="System\IO\IOException.cs" />
<Compile Include="System\MarshalByRefObject.cs" />
<Compile Include="System\Math.cs" />
<Compile Include="System\MemberAccessException.cs" />
<Compile Include="System\MissingMemberException.cs" />
<Compile Include="System\MissingMethodException.cs" />
<Compile Include="System\MulticastDelegate.cs" />
<Compile Include="System\NonSerializedAttribute.cs" />
<Compile Include="System\NotImplementedException.cs" />
Expand Down
4 changes: 2 additions & 2 deletions nanoFramework.CoreLibrary/System/Activator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class Activator
/// </summary>
/// <typeparam name="T">The type to create.</typeparam>
/// <returns>A reference to the newly created object, or the default value for a value type.</returns>
/// <exception cref="NotSupportedException">The type is abstract or has no public parameterless constructor.</exception>
/// <exception cref="MissingMethodException">The type is abstract or has no public parameterless constructor.</exception>
public static T CreateInstance<T>()
{
Type type = typeof(T);
Expand All @@ -31,7 +31,7 @@ public static T CreateInstance<T>()

if (constructor == null || !constructor.IsPublic)
{
throw new NotSupportedException();
throw new MissingMethodException();
}

return (T)constructor.Invoke(null);
Expand Down
Loading