Files
mono/mcs/class/corlib/Test/System.Reflection/ModuleTest.cs
T
Gert Driesen 165e7a7b5b * MonoCustomAttrs.cs: In IsDefined, throw ArgumentNullException if
attributeType is null to avoid a SIGSEGV (and match MS). Only partial
fix for bug #82431 on 1.0 profile; it fully fixes the problem for the
1.0 profile, but more changes (in the runtime) are required for the
2.0 profile. Added a FIXME explaining the problem.
* TypeTest.cs: Improved test for GetCustomAttributes, and avoid
dependency on order of attributes. Split IsDefined test and improved
part for bug #82431.
* AssemblyTest.cs: Added IsDefined test with null value for
attributeType argument.
* ModuleTest.cs: Same. Remove use of deprecated Assertion class, code
formatting and fixed line endings.
* ParameterInfoTest.cs: Same. Remove use of deprecated Assertion class
and code formatting.
* MethodInfoTest.cs: Same.
* EventInfoTest.cs: Same. Code formatting.
* FieldInfoTest.cs: Same. Code formatting.
* PropertyInfoTest.cs: Same. Improved coverage of GetAccessors. 
Improved tests for GetCustomAttributes. Code formatting.
* TypeBuilderTest.cs: Added IsDefined test with null value for
attributeType argument.

svn path=/trunk/mcs/; revision=84239
2007-08-16 20:11:53 +00:00

343 lines
8.5 KiB
C#

//
// ModuleTest - NUnit Test Cases for the Module class
//
// Zoltan Varga ([email protected])
//
// (C) Ximian, Inc. http://www.ximian.com
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Serialization;
using System.IO;
using System.Collections;
using NUnit.Framework;
namespace MonoTests.System.Reflection
{
[TestFixture]
public class ModuleTest
{
static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.ModuleTest");
[SetUp]
public void SetUp ()
{
while (Directory.Exists (TempFolder))
TempFolder = Path.Combine (TempFolder, "2");
Directory.CreateDirectory (TempFolder);
}
[TearDown]
public void TearDown ()
{
try {
// This throws an exception under MS.NET, since the directory contains loaded
// assemblies.
Directory.Delete (TempFolder, true);
} catch (Exception) {
}
}
[Test]
public void IsDefined_AttributeType_Null ()
{
Type t = typeof (ModuleTest);
Module module = t.Module;
try {
module.IsDefined ((Type) null, false);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNotNull (ex.ParamName, "#5");
Assert.AreEqual ("attributeType", ex.ParamName, "#6");
}
}
[Test]
public void GetField_Name_Null ()
{
Type t = typeof (ModuleTest);
Module module = t.Module;
try {
module.GetField (null);
Assert.Fail ("#A1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
// bug #82459
#if ONLY_1_1
Assert.IsNotNull (ex.ParamName, "#A5");
Assert.AreEqual ("name", ex.ParamName, "#A6");
#endif
}
try {
module.GetField (null, 0);
Assert.Fail ("#B1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
// bug #82459
#if ONLY_1_1
Assert.IsNotNull (ex.ParamName, "#B5");
Assert.AreEqual ("name", ex.ParamName, "#B6");
#endif
}
}
// Some of these tests overlap with the tests for ModuleBuilder
[Test]
[Category("NotDotNet")] // path length can cause suprious failures
public void TestGlobalData () {
string name = "moduletest-assembly";
string fileName = name + ".dll";
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = name;
AssemblyBuilder ab
= Thread.GetDomain().DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.RunAndSave, TempFolder);
string resfile = Path.Combine (TempFolder, "res");
using (StreamWriter sw = new StreamWriter (resfile)) {
sw.WriteLine ("FOO");
}
ab.AddResourceFile ("res", "res");
ModuleBuilder mb = ab.DefineDynamicModule(fileName, fileName);
mb.DefineInitializedData ("DATA", new byte [100], FieldAttributes.Public);
mb.DefineInitializedData ("DATA2", new byte [100], FieldAttributes.Public);
mb.DefineInitializedData ("DATA3", new byte [99], FieldAttributes.Public);
mb.DefineUninitializedData ("DATA4", 101, FieldAttributes.Public);
mb.DefineInitializedData ("DATA_PRIVATE", new byte [100], 0);
mb.CreateGlobalFunctions ();
ab.Save (fileName);
Assembly assembly = Assembly.LoadFrom (Path.Combine (TempFolder, fileName));
Module module = assembly.GetLoadedModules ()[0];
string[] expectedFieldNames = new string [] {
"DATA", "DATA2", "DATA3", "DATA4"
};
ArrayList fieldNames = new ArrayList ();
foreach (FieldInfo fi in module.GetFields ()) {
fieldNames.Add (fi.Name);
}
AssertArrayEqualsSorted (expectedFieldNames, fieldNames.ToArray (typeof (string)));
Assert.IsNotNull (module.GetField ("DATA"), "#A1");
Assert.IsNotNull (module.GetField ("DATA2"), "#A2");
Assert.IsNotNull (module.GetField ("DATA3"), "#A3");
Assert.IsNotNull (module.GetField ("DATA4"), "#A4");
Assert.IsNull (module.GetField ("DATA_PRIVATE"), "#A5");
Assert.IsNotNull (module.GetField ("DATA_PRIVATE", BindingFlags.NonPublic | BindingFlags.Static), "#A6");
// Check that these methods work correctly on resource modules
Module m2 = assembly.GetModule ("res");
Assert.IsNotNull (m2, "#B1");
Assert.AreEqual (0, m2.GetFields ().Length, "#B2");
Assert.IsNull (m2.GetField ("DATA"), "#B3");
Assert.IsNull (m2.GetField ("DATA", BindingFlags.Public), "#B4");
}
#if NET_2_0
[Test]
public void ResolveType ()
{
Type t = typeof (ModuleTest);
Module module = t.Module;
Assert.AreEqual (t, module.ResolveType (t.MetadataToken), "#1");
/* We currently throw ArgumentException for this one */
try {
module.ResolveType (1234);
Assert.Fail ("#2");
} catch (ArgumentException) {
}
try {
module.ResolveType (t.GetMethod ("ResolveType").MetadataToken);
Assert.Fail ("#3");
} catch (ArgumentException) {
}
try {
module.ResolveType (t.MetadataToken + 10000);
Assert.Fail ("#4");
} catch (ArgumentOutOfRangeException) {
}
}
[Test]
public void ResolveMethod ()
{
Type t = typeof (ModuleTest);
Module module = t.Module;
Assert.AreEqual (t.GetMethod ("ResolveMethod"), module.ResolveMethod (t.GetMethod ("ResolveMethod").MetadataToken));
try {
module.ResolveMethod (1234);
Assert.Fail ();
} catch (ArgumentException) {
}
try {
module.ResolveMethod (t.MetadataToken);
Assert.Fail ();
} catch (ArgumentException) {
}
try {
module.ResolveMethod (t.GetMethod ("ResolveMethod").MetadataToken + 10000);
Assert.Fail ();
} catch (ArgumentOutOfRangeException) {
}
}
public int aField;
[Test]
public void ResolveField ()
{
Type t = typeof (ModuleTest);
Module module = t.Module;
Assert.AreEqual (t.GetField ("aField"), module.ResolveField (t.GetField ("aField").MetadataToken));
try {
module.ResolveField (1234);
Assert.Fail ();
} catch (ArgumentException) {
}
try {
module.ResolveField (t.MetadataToken);
Assert.Fail ();
} catch (ArgumentException) {
}
try {
module.ResolveField (t.GetField ("aField").MetadataToken + 10000);
Assert.Fail ();
} catch (ArgumentOutOfRangeException) {
}
}
[Ignore ("it breaks nunit-console.exe execution under .NET 2.0")]
[Test]
public void ResolveString ()
{
Type t = typeof (ModuleTest);
Module module = t.Module;
for (int i = 1; i < 10000; ++i) {
try {
module.ResolveString (0x70000000 + i);
} catch (Exception) {
}
}
try {
module.ResolveString (1234);
Assert.Fail ();
} catch (ArgumentException) {
}
try {
module.ResolveString (t.MetadataToken);
Assert.Fail ();
} catch (ArgumentException) {
}
try {
module.ResolveString (0x70000000 | 10000);
Assert.Fail ();
} catch (ArgumentOutOfRangeException) {
}
}
[Test]
public void ResolveMember ()
{
Type t = typeof (ModuleTest);
Module module = t.Module;
Assert.AreEqual (t, module.ResolveMember (t.MetadataToken), "#1");
Assert.AreEqual (t.GetField ("aField"), module.ResolveMember (t.GetField ("aField").MetadataToken), "#2");
Assert.AreEqual (t.GetMethod ("ResolveMember"), module.ResolveMember (t.GetMethod ("ResolveMember").MetadataToken), "#3");
try {
module.ResolveMember (module.MetadataToken);
Assert.Fail ("#4");
} catch (ArgumentException) {
}
}
#endif
[Test]
public void FindTypes ()
{
Module m = typeof (ModuleTest).Module;
Type[] t;
t = m.FindTypes (Module.FilterTypeName, "FindTypesTest*");
Assert.AreEqual (2, t.Length, "#A1");
Assert.AreEqual ("FindTypesTestFirstClass", t [0].Name, "#A2");
Assert.AreEqual ("FindTypesTestSecondClass", t [1].Name, "#A3");
t = m.FindTypes (Module.FilterTypeNameIgnoreCase, "findtypestest*");
Assert.AreEqual (2, t.Length, "#B1");
Assert.AreEqual ("FindTypesTestFirstClass", t [0].Name, "#B2");
Assert.AreEqual ("FindTypesTestSecondClass", t [1].Name, "#B3");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void GetObjectData_Null ()
{
Module m = typeof (ModuleTest).Module;
m.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
}
class FindTypesTestFirstClass {
}
class FindTypesTestSecondClass {
}
private static void AssertArrayEqualsSorted (Array o1, Array o2) {
Array s1 = (Array)o1.Clone ();
Array s2 = (Array)o2.Clone ();
Array.Sort (s1);
Array.Sort (s2);
Assert.AreEqual (s1.Length, s2.Length);
for (int i = 0; i < s1.Length; ++i)
Assert.AreEqual (s1.GetValue (i), s2.GetValue (i));
}
}
}