2 using System.Collections.Generic;
4 using System.Reflection;
5 using Microsoft.CSharp.RuntimeBinder;
7 // Dynamic member lookup tests
13 internal int IntValue = 5;
14 internal string StringStatic = "hi";
16 public const decimal Decimal = -0.3m;
19 protected internal uint this[byte i] {
47 delegate void EmptyDelegate ();
48 delegate int IntDelegate ();
50 static void Assert<T> (T expected, T value, string name)
52 if (!EqualityComparer<T>.Default.Equals (expected, value)) {
54 throw new ApplicationException (name + expected + " != " + value);
58 static void AssertError (Action expected, string name)
62 throw new ApplicationException (name + ": RuntimeBinderException expected");
63 } catch (RuntimeBinderException) {
68 #pragma warning disable 169
72 dynamic d = new[] { 5, 8, 2 };
73 Assert (8, d[1], "#1");
75 d = new int[,] { { 1, 2 }, { 3, 4 } };
76 Assert (3, d[1, 0], "#2");
78 dynamic d2 = new Class ();
79 Assert<uint> (154, d2[2], "#3");
80 Assert<uint> (154, d2[i:2], "#3a");
83 void GetIndexError_Null ()
86 AssertError (() => { var v = d[1]; }, "#1");
91 dynamic d = new Class ();
92 Assert (5, d.IntValue, "#1");
93 Assert (180, d.Prop, "#1a");
95 Assert ("hi", d.StringStatic, "#2");
98 Assert (4, d.Length, "#3");
100 // d.Event += delegate () { }; CS0019
103 void MemberGetError_Null ()
106 AssertError (() => { var v = d.Foo; }, "#1");
109 void MemberSetTest ()
111 dynamic d = new Class ();
113 Assert (22, d.IntValue, "#1");
115 Assert (19, d.Prop, "#1a");
117 d.Prop = byte.MaxValue;
119 Assert (0, d.Prop, "#1b");
121 d.StringStatic = "no";
122 Assert ("no", d.StringStatic, "#2");
124 var r = d.FixedValue = 44;
125 Assert (44, r, "#3");
128 void MemberSetError_Null ()
131 AssertError (() => { d.Fo1 = 1; }, "#1");
136 dynamic d = new[] { "b", "v" };
138 Assert ("c", d[1], "#1");
140 d = new int[,] { { 1, 2 }, { 3, 4 } };
142 Assert (100, d[1, 0], "#2");
144 Assert (101, d[1, 0], "#2a");
146 d [0, 0] = d [1, 0] = 55;
147 Assert (55, d [0, 0], "#2a");
149 dynamic d2 = new Class ();
151 Assert<uint> (1000, d2[2], "#3");
153 Assert<uint> (2002, d2[2], "#3a");
155 Assert<uint> (3, d2[1], "#3b");
157 uint r = d2 [1] = 200;
158 Assert<uint> (200, r, "#4");
161 void SetIndexError_Null ()
164 AssertError (() => { d [1] = 0; }, "#1");
167 #pragma warning restore 169
169 static bool RunTest (MethodInfo test)
171 Console.Write ("Running test {0, -25}", test.Name);
173 test.Invoke (new Tester (), null);
174 Console.WriteLine ("OK");
176 } catch (Exception e) {
177 Console.WriteLine ("FAILED");
178 Console.WriteLine (e.ToString ());
179 // Console.WriteLine (e.InnerException.Message);
184 public static int Main ()
186 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
187 where test.GetParameters ().Length == 0
189 select RunTest (test);
191 int failures = tests.Count (a => !a);
192 Console.WriteLine (failures + " tests failed");