[corlib] Throw correct exception for missing properties called via InvokeMember
[mono.git] / mcs / class / corlib / Test / System / TypeTest.cs
index 3bbe3cde42adc064d1e426fe476fdb3fb1042eba..85c3542f0e152c8a7ae120185354895665c0b505 100644 (file)
@@ -141,6 +141,12 @@ namespace MonoTests.System
                        get { return 1; }
                        set { }
                }
+
+               public event EventHandler E;
+               public void Dummy ()
+               {
+                       E += delegate {};
+               }
        }
 
        class Derived1 : Base1
@@ -156,6 +162,12 @@ namespace MonoTests.System
                        get { return 1; }
                        set { }
                }
+
+               public new event Action E;
+               public new void Dummy ()
+               {
+                       E += delegate {};
+               }
        }
 
        public class Foo<T>
@@ -293,16 +305,11 @@ namespace MonoTests.System
                        Assert.AreEqual (typeof (ICloneable[][]).IsAssignableFrom (typeof (Duper[][])), true, "#12");
 
                        // Tests for vectors<->one dimensional arrays */
-#if TARGET_JVM // Lower bounds arrays are not supported for TARGET_JVM.
-                       Array arr1 = Array.CreateInstance (typeof (int), new int[] {1});
-                       Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr1.GetType ()), true, "#13");
-#else
                        Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
                        Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
 
                        Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr1.GetType ()), true, "#13");
                        Assert.AreEqual (typeof (int[]).IsAssignableFrom (arr2.GetType ()), false, "#14");
-#endif // TARGET_JVM
 
                        // Test that arrays of enums can be cast to their base types
                        Assert.AreEqual (typeof (int[]).IsAssignableFrom (typeof (TypeCode[])), true, "#15");
@@ -386,7 +393,6 @@ namespace MonoTests.System
                }
 
                [Test]
-               [Category ("TargetJvmNotWorking")]
                public void TestGetPropertyImpl ()
                {
                        // Test getting property that is exact
@@ -399,6 +405,14 @@ namespace MonoTests.System
                        Assert.AreEqual (1, typeof (Derived1).GetProperties ().Length, "#03");
                }
 
+               [Test]
+               public void GetEvents ()
+               {
+                       // Test hide-by-name
+                       Assert.AreEqual (1, typeof (Derived2).GetEvents ().Length);
+                       Assert.AreEqual (typeof (Derived2), typeof (Derived2).GetEvents ()[0].DeclaringType);
+               }
+
                [Test]
                public void GetProperties ()
                {
@@ -1505,7 +1519,6 @@ namespace MonoTests.System
                                                            typeof (long), new Type[0], null), "#2");
                }
 
-#if !TARGET_JVM // StructLayout not supported for TARGET_JVM
                [StructLayout(LayoutKind.Explicit, Pack = 4, Size = 64)]
                public class Class1
                {
@@ -1531,7 +1544,6 @@ namespace MonoTests.System
                        Assert.AreEqual (LayoutKind.Explicit, attr3.Value);
                        Assert.AreEqual (CharSet.Unicode, attr3.CharSet);
                }
-#endif // TARGET_JVM
 
                [Test]
                public void Namespace ()
@@ -1680,21 +1692,6 @@ namespace MonoTests.System
                        Assert.IsTrue (typeof (TimeSpan).IsValueType, "#6");
                }
 
-               [Test]
-               [Category("NotDotNet")]
-               // Depends on the GAC working, which it doesn't durring make distcheck.
-               [Category ("NotWorking")]
-               public void GetTypeWithWhitespace ()
-               {
-                       Assert.IsNotNull (Type.GetType
-                                                  (@"System.Configuration.NameValueSectionHandler,
-                       System,
-Version=1.0.5000.0,
-Culture=neutral
-,
-PublicKeyToken=b77a5c561934e089"));
-               }
-
                [Test]
                public void GetTypeNonVectorArray ()
                {
@@ -1856,7 +1853,9 @@ PublicKeyToken=b77a5c561934e089"));
 
                struct B
                {
+                       #pragma warning disable 169
                        int value;
+                       #pragma warning restore 169
                }
 
                [Test]
@@ -1872,6 +1871,20 @@ PublicKeyToken=b77a5c561934e089"));
                        typeof(B).InvokeMember ("", BindingFlags.CreateInstance, null, null, new object [] { 1 });
                }
 
+               [Test]
+               [ExpectedException (typeof (MissingMethodException))]
+               public void InvokeGetPropertyMissing ()
+               {
+                       typeof(B).InvokeMember ("", BindingFlags.GetProperty, null, null, new object [] { 1 });
+               }
+
+               [Test]
+               [ExpectedException (typeof (MissingMethodException))]
+               public void InvokeSetPropertyMissing ()
+               {
+                       typeof(B).InvokeMember ("", BindingFlags.SetProperty, null, null, new object [] { 1 });
+               }
+
                internal static string bug336841 (string param1, params string [] param2)
                {
                        StringBuilder sb = new StringBuilder ();
@@ -2149,7 +2162,7 @@ PublicKeyToken=b77a5c561934e089"));
                        a1 = new string [10];
                }
 
-               class X
+               public class X
                {
                        public static int Value;
                }
@@ -2512,6 +2525,47 @@ PublicKeyToken=b77a5c561934e089"));
                        Assert.IsFalse (typeof (bug82431B4).IsDefined (typeof (NotInheritAttribute), true), "#K4");
                }
 
+               class Bug13767Attribute : Attribute
+               {
+                       public object[] field;
+
+                       public Bug13767Attribute (params object[] args)
+                       {
+                               field = args;
+                       }
+               }
+
+               public enum Bug13767Enum
+               {
+                       Value0,
+                       Value1,
+               }
+
+               [Bug13767("Demo", new[] { Bug13767Enum.Value1, Bug13767Enum.Value0 })]
+               public void Bug13767Method(string attributeName, Bug13767Enum[]options)
+               {
+
+               }
+
+               [Test] //Bug 13767
+               public void CustomAttributeWithNestedArrayOfEnum ()
+               {
+                       var m = GetType ().GetMethod ("Bug13767Method");
+
+                       var attr = m.GetCustomAttributes (false);
+                       Assert.AreEqual (1, attr.Length, "#1");
+
+                       var tc = (Bug13767Attribute)attr[0];
+                       Assert.AreEqual (2, tc.field.Length, "#2");
+                       Assert.AreEqual ("Demo", tc.field[0], "#3");
+                       Assert.IsNotNull (tc.field[1], "#4");
+
+                       var arr = (Bug13767Enum[])tc.field [1];
+                       Assert.AreEqual (2, arr.Length, "#5");
+                       Assert.AreEqual (Bug13767Enum.Value1, arr [0], "#6");
+                       Assert.AreEqual (Bug13767Enum.Value0, arr [1], "#7");
+               }
+
                [Test] // GetType (String)
                public void GetType1_TypeName_Null ()
                {
@@ -2733,7 +2787,7 @@ PublicKeyToken=b77a5c561934e089"));
                        Assert.IsNull (i);
                }
 
-#if !TARGET_JVM && !MOBILE // Reflection.Emit is not supported for TARGET_JVM
+#if !MOBILE
                [Test]
                public void EqualsUnderlyingType ()
                {
@@ -2752,7 +2806,7 @@ PublicKeyToken=b77a5c561934e089"));
 
                        Assert.IsTrue (typeof (int).Equals (e));
                }
-#endif // TARGET_JVM
+#endif
 
                [Test]
                public void Equals_Type_Null ()
@@ -3156,6 +3210,14 @@ PublicKeyToken=b77a5c561934e089"));
                        Assert.AreEqual (t1, t2);
                }
 
+#if !MONOTOUCH
+               [Test]
+               public void SpaceAfterComma () {
+                       string strType = "System.Collections.Generic.Dictionary`2[[System.Int32,mscorlib], [System.String,mscorlib]],mscorlib";
+                       Assert.IsTrue (Type.GetType (strType) != null);
+               }
+#endif
+
 #if !MONOTOUCH
                [Test]
                public void Bug506757 ()
@@ -3608,17 +3670,10 @@ PublicKeyToken=b77a5c561934e089"));
 
                        Assert.AreEqual ("A", typeof (MyRealEnum).GetEnumName ((byte)0), "#11");
                        Assert.AreEqual ("A", typeof (MyRealEnum).GetEnumName ((sbyte)0), "#12");
-                       try {
-                               typeof (MyRealEnum).GetEnumName (false);
-                               Assert.Fail ("#13");
-                       } catch (ArgumentException) { }
-
+                       Assert.AreEqual ("A", typeof (MyRealEnum).GetEnumName (false), "#13");
                        Assert.AreEqual ("A", typeof (MyRealEnum).GetEnumName ((short)0), "#14");
                        Assert.AreEqual ("A", typeof (MyRealEnum).GetEnumName ((ushort)0), "#15");
-                       try {
-                               typeof (MyRealEnum).GetEnumName ('c');
-                               Assert.Fail ("#16");
-                       } catch (ArgumentException) { }
+                       Assert.IsNull (typeof (MyRealEnum).GetEnumName ('c'), "#16");
 
                        Assert.AreEqual ("A", typeof (MyRealEnum).GetEnumName ((int)0), "#17");
                        Assert.AreEqual ("A", typeof (MyRealEnum).GetEnumName ((uint)0), "#18");
@@ -3638,18 +3693,12 @@ PublicKeyToken=b77a5c561934e089"));
 
                        Assert.AreEqual ("A", typeof (MyRealEnum2).GetEnumName ((byte)0), "#23");
                        Assert.AreEqual ("A", typeof (MyRealEnum2).GetEnumName ((sbyte)0), "#24");
-                       try {
-                               typeof (MyRealEnum2).GetEnumName (false);
-                               Assert.Fail ("#22", "#25");
-                       } catch (ArgumentException) { }
+                       Assert.AreEqual ("A", typeof (MyRealEnum2).GetEnumName (false), "#25");
 
                        Assert.AreEqual ("A", typeof (MyRealEnum2).GetEnumName ((short)0), "#26");
                        Assert.AreEqual ("A", typeof (MyRealEnum2).GetEnumName ((ushort)0), "#27");
 
-                       try {
-                               typeof (MyRealEnum2).GetEnumName ('c');
-                               Assert.Fail ("#28");
-                       } catch (ArgumentException) { }
+                       Assert.IsNull (typeof (MyRealEnum2).GetEnumName ('c'), "#28");
 
                        Assert.AreEqual ("A", typeof (MyRealEnum2).GetEnumName ((int)0), "#29");
                        Assert.AreEqual ("A", typeof (MyRealEnum2).GetEnumName ((uint)0), "#30");
@@ -3699,7 +3748,7 @@ PublicKeyToken=b77a5c561934e089"));
                        try {
                                typeof (MyRealEnum).IsEnumDefined (true);
                                Assert.Fail ("#6");
-                       } catch (InvalidOperationException) { }
+                       } catch (ArgumentException) { }
 
                        try {
                                typeof (MyRealEnum).IsEnumDefined (MyRealEnum2.A);
@@ -3709,7 +3758,7 @@ PublicKeyToken=b77a5c561934e089"));
                        try {
                                typeof (MyRealEnum).IsEnumDefined (typeof (MyRealEnum));
                                Assert.Fail ("#8");
-                       } catch (InvalidOperationException) { }
+                       } catch (ArgumentException) { }
 
                        Assert.IsTrue (typeof (MyRealEnum).IsEnumDefined ((short)0), "#9");
                        Assert.IsFalse (typeof (MyRealEnum).IsEnumDefined ((short)88), "#10");
@@ -3983,6 +4032,25 @@ PublicKeyToken=b77a5c561934e089"));
                }
 #endif
 
+               [Test]
+               public void GetTypeParseGenericCorrectly () { //Bug #15124
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1"), typeof (Foo<>), "#1");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[System.Int32]"), typeof (Foo<int>), "#2");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[[System.Int32]]"), typeof (Foo<int>), "#3");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[System.Int32][]"), typeof (Foo<int>[]), "#4");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[][System.Int32]"), null, "#5");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[System.Int32][,]"), typeof (Foo<int>[,]), "#6");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[]"), typeof (Foo<>).MakeArrayType(), "#7");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[,]"), typeof (Foo<>).MakeArrayType (2), "#8");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[][]"), typeof (Foo<>).MakeArrayType ().MakeArrayType (), "#9");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1["), null, "#10");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[["), null, "#11");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[[]"), null, "#12");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[,"), null, "#13");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[*"), null, "#14");
+                       Assert.AreEqual (Type.GetType ("MonoTests.System.Foo`1[System.Int32"), null, "#15");
+               }
+
                public abstract class Stream : IDisposable
                {
                        public void Dispose ()