update readme (#21797)
[mono-project.git] / mcs / tests / dtest-error-01.cs
blob2cba37f3e8f32417a72aef55721f2934eac6f0ff
1 // Compiler options: -unsafe
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Reflection;
7 using Microsoft.CSharp.RuntimeBinder;
9 class Helper
11 public unsafe static int* Foo (int i)
13 return null;
17 class Tester
19 #pragma warning disable 169
20 void NonInvocable ()
22 AssertError (
23 () => {
24 dynamic d = 1;
25 d ();
26 }, "Cannot invoke a non-delegate type");
29 void Using_1 ()
31 AssertError (
32 () => {
33 using (dynamic d = 1) { }
34 }, "Cannot implicitly convert type 'int' to 'System.IDisposable'");
37 void Unsafe_1 ()
39 dynamic d = 1;
40 AssertError (
41 () => Helper.Foo (d),
42 "Dynamic calls cannot be used in conjunction with pointers");
45 void NullableConversion ()
47 dynamic d = 1;
48 AssertError (
49 () => {
50 dynamic b = false;
51 byte? b2 = null;
52 b &= b2;
53 }, "Operator '&=' cannot be applied to operands of type 'bool' and 'byte?'");
56 #pragma warning restore 169
58 static void AssertError (Action a, string msg)
60 try {
61 a ();
62 } catch (RuntimeBinderException e) {
63 if (e.Message != msg)
64 throw new ApplicationException ("Expected error message: " + e.Message);
66 return;
69 throw new ApplicationException ("Expected error");
72 static bool RunTest (MethodInfo test)
74 Console.Write ("Running test {0, -25}", test.Name);
75 try {
76 test.Invoke (new Tester (), null);
77 Console.WriteLine ("OK");
78 return true;
79 } catch (Exception e) {
80 Console.WriteLine ("FAILED");
81 Console.WriteLine (e.InnerException.Message);
82 return false;
86 public static int Main ()
88 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
89 where test.GetParameters ().Length == 0
90 orderby test.Name
91 select RunTest (test);
93 int failures = tests.Count (a => !a);
94 Console.WriteLine (failures + " tests failed");
95 return failures;