2 // System.ComponentModel.EnumConverter test cases
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // (c) 2007 Gert Driesen
11 using System.ComponentModel;
12 using System.ComponentModel.Design.Serialization;
13 using System.Globalization;
14 using System.Reflection;
16 using NUnit.Framework;
18 namespace MonoTests.System.ComponentModel
21 public class EnumConverterTests
24 public void CanConvertFrom ()
26 EnumConverter converter = new EnumConverter (typeof (E));
27 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#A1");
28 Assert.IsFalse (converter.CanConvertFrom (typeof (Enum)), "#A2");
29 Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#A3");
30 Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#A4");
31 Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#A5");
32 Assert.IsFalse (converter.CanConvertFrom (typeof (string [])), "#A6");
33 Assert.IsTrue (converter.CanConvertFrom (typeof (Enum [])), "#A7");
35 converter = new EnumConverter (typeof (E2));
36 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#B1");
37 Assert.IsFalse (converter.CanConvertFrom (typeof (Enum)), "#B2");
38 Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#B3");
39 Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#B4");
40 Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#B5");
41 Assert.IsFalse (converter.CanConvertFrom (typeof (string [])), "#B6");
42 Assert.IsTrue (converter.CanConvertFrom (typeof (Enum [])), "#B7");
46 public void CanConvertTo ()
48 EnumConverter converter = new EnumConverter (typeof (E));
49 Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#A1");
50 Assert.IsFalse (converter.CanConvertTo (typeof (Enum)), "#A2");
51 Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#A3");
52 Assert.IsFalse (converter.CanConvertTo (typeof (int)), "#A4");
53 Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "#A5");
54 Assert.IsFalse (converter.CanConvertTo (typeof (string [])), "#A6");
55 Assert.IsTrue (converter.CanConvertTo (typeof (Enum [])), "#A7");
57 converter = new EnumConverter (typeof (E2));
58 Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#B1");
59 Assert.IsFalse (converter.CanConvertTo (typeof (Enum)), "#B2");
60 Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#B3");
61 Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#B4");
62 Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "#B5");
63 Assert.IsFalse (converter.CanConvertTo (typeof (string [])), "#B6");
64 Assert.IsTrue (converter.CanConvertTo (typeof (Enum [])), "#B7");
68 public void ConvertFrom_Null ()
70 EnumConverter converter = new EnumConverter (typeof (E));
72 converter.ConvertFrom (null, CultureInfo.InvariantCulture, null);
74 } catch (NotSupportedException ex) {
75 // EnumConverter cannot convert from (null)
76 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
77 Assert.IsNull (ex.InnerException, "#3");
78 Assert.IsNotNull (ex.Message, "#4");
79 Assert.IsTrue (ex.Message.IndexOf (typeof (EnumConverter).Name) != -1, "#5");
80 Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#6");
85 public void ConvertFrom_EnumArray ()
87 EnumConverter converter = new EnumConverter (typeof (E));
88 Assert.AreEqual (E.Aa, converter.ConvertFrom (null,
89 CultureInfo.InvariantCulture,
90 (Enum []) new Enum [] { E.Aa }), "#A1");
91 Assert.AreEqual ((E) 8, converter.ConvertFrom (null,
92 CultureInfo.InvariantCulture,
93 (Enum []) new Enum [] { E.Aa, E2.Dd }), "#A2");
94 Assert.AreEqual ((E) 958, converter.ConvertFrom (null,
95 CultureInfo.InvariantCulture,
96 (Enum []) new Enum [] { (E2) 444, (E) 666 }), "#A3");
97 Assert.AreEqual ((E) 0, converter.ConvertFrom (null,
98 CultureInfo.InvariantCulture,
99 (Enum []) new Enum [0]), "#A4");
101 converter = new EnumConverter (typeof (E2));
102 Assert.AreEqual ((E2) 0, converter.ConvertFrom (null,
103 CultureInfo.InvariantCulture,
104 (Enum []) new Enum [] { E.Aa }), "#B1");
105 Assert.AreEqual (E2.Dd, converter.ConvertFrom (null,
106 CultureInfo.InvariantCulture,
107 (Enum []) new Enum [] { E.Aa, E2.Dd }), "#B2");
108 Assert.AreEqual ((E2) 958, converter.ConvertFrom (null,
109 CultureInfo.InvariantCulture,
110 (Enum []) new Enum [] { (E2) 444, (E) 666 }), "#B3");
111 Assert.AreEqual ((E2) 0, converter.ConvertFrom (null,
112 CultureInfo.InvariantCulture,
113 (Enum []) new Enum [0]), "#B4");
114 Assert.AreEqual (E2.Bb | E2.Dd, converter.ConvertFrom (null,
115 CultureInfo.InvariantCulture,
116 (Enum []) new Enum [] { E2.Bb, E2.Dd }), "#B5");
120 public void ConvertFrom_String ()
122 EnumConverter converter = new EnumConverter (typeof (E));
123 Assert.AreEqual (E.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Bb"), "#A1");
124 Assert.AreEqual (E.Cc, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "2"), "#A2");
125 Assert.AreEqual (E.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " Bb "), "#A3");
126 Assert.AreEqual (E.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " 3 "), "#A4");
127 Assert.AreEqual ((E) 666, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "666"), "#A5");
128 Assert.AreEqual (E.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Bb,Dd"), "#A6");
129 Assert.AreEqual (E.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Dd,Bb"), "#A7");
130 Assert.AreEqual (E.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Aa,Bb"), "#A8");
133 converter.ConvertFrom (null, CultureInfo.InvariantCulture, string.Empty);
135 } catch (FormatException ex) {
136 // is not a valid value for E
137 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#B2");
138 Assert.IsNotNull (ex.InnerException, "#B3");
139 Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#B4");
140 Assert.IsNotNull (ex.Message, "#B5");
141 Assert.IsTrue (ex.Message.IndexOf ("E") != -1, "#B6");
143 // Must specify valid information for parsing in the string
144 ArgumentException inner = (ArgumentException) ex.InnerException;
145 Assert.IsNull (inner.InnerException, "#B7");
146 Assert.IsNotNull (inner.Message, "#B8");
150 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "YY");
152 } catch (FormatException ex) {
153 // YY is not a valid value for E
154 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#C2");
155 Assert.IsNotNull (ex.InnerException, "#C3");
156 Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#C4");
157 Assert.IsNotNull (ex.Message, "#C5");
158 Assert.IsTrue (ex.Message.IndexOf ("YY") != -1, "#C6");
159 Assert.IsTrue (ex.Message.IndexOf ("E") != -1, "#C7");
161 // Requested value YY was not found
162 ArgumentException inner = (ArgumentException) ex.InnerException;
163 //Assert.IsNull (inner.InnerException, "#C8");
164 Assert.IsNotNull (inner.Message, "#C9");
165 Assert.IsTrue (inner.Message.IndexOf ("YY") != -1, "#C10");
166 Assert.IsNull (inner.ParamName, "#C11");
171 public void ConvertFrom_String_Flags ()
173 EnumConverter converter = new EnumConverter (typeof (E2));
174 Assert.AreEqual (E2.Cc, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Cc"), "#B1");
175 Assert.AreEqual (E2.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "8"), "#B2");
176 Assert.AreEqual (E2.Cc | E2.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Cc,Dd"), "#B3");
177 Assert.AreEqual (E2.Aa | E2.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "3"), "#B4");
178 Assert.AreEqual (E2.Bb | E2.Cc, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "2,4"), "#B5");
179 Assert.AreEqual (E2.Aa | E2.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " 1 , 8 "), "#B5");
180 Assert.AreEqual ((E2) 666, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "666"), "#B6");
181 Assert.AreEqual (E2.Cc | E2.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " Dd , Cc "), "#B7");
182 Assert.AreEqual (E2.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " Bb "), "#B8");
183 Assert.AreEqual (E2.Aa | E2.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " 3 "), "#B9");
186 converter.ConvertFrom (null, CultureInfo.InvariantCulture, string.Empty);
188 } catch (FormatException ex) {
189 // is not a valid value for E2
190 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#B2");
191 Assert.IsNotNull (ex.InnerException, "#B3");
192 Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#B4");
193 Assert.IsNotNull (ex.Message, "#B5");
194 Assert.IsTrue (ex.Message.IndexOf ("E2") != -1, "#B6");
196 // Must specify valid information for parsing in the string
197 ArgumentException inner = (ArgumentException) ex.InnerException;
198 Assert.IsNull (inner.InnerException, "#B7");
199 Assert.IsNotNull (inner.Message, "#B8");
203 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Aa Bb");
205 } catch (FormatException ex) {
206 // Aa Bb is not a valid value for E2
207 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#C2");
208 Assert.IsNotNull (ex.InnerException, "#C3");
209 Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#C4");
210 Assert.IsNotNull (ex.Message, "#C5");
211 Assert.IsTrue (ex.Message.IndexOf ("Aa Bb") != -1, "#C6");
212 Assert.IsTrue (ex.Message.IndexOf ("E2") != -1, "#C7");
214 // Requested value Aa Bb was not found
215 ArgumentException inner = (ArgumentException) ex.InnerException;
216 Assert.IsNotNull (inner.Message, "#C9");
217 Assert.IsTrue (inner.Message.IndexOf ("Aa Bb") != -1, "#C10");
218 Assert.IsNull (inner.ParamName, "#C11");
222 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "2,");
224 } catch (FormatException ex) {
225 // 2, is not a valid value for E2
226 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#D2");
227 Assert.IsNotNull (ex.InnerException, "#D3");
228 Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#D4");
229 Assert.IsNotNull (ex.Message, "#D5");
230 Assert.IsTrue (ex.Message.IndexOf ("2,") != -1, "#D6");
231 Assert.IsTrue (ex.Message.IndexOf ("E2") != -1, "#D7");
233 // Must specify valid information for parsing in the string
234 ArgumentException inner = (ArgumentException) ex.InnerException;
235 Assert.IsNull (inner.InnerException, "#D8");
236 Assert.IsNotNull (inner.Message, "#D9");
237 Assert.IsFalse (inner.Message.IndexOf ("2,") != -1, "#D10");
242 public void ConvertTo_EnumArray ()
245 EnumConverter converter = new EnumConverter (typeof (E));
247 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
248 E.Bb, typeof (Enum [])) as Enum [];
249 Assert.IsNotNull (enums, "#A1");
250 Assert.AreEqual (1, enums.Length, "#A2");
251 Assert.AreEqual (typeof (E), enums [0].GetType (), "#A3");
252 Assert.AreEqual (E.Bb, enums [0], "#A4");
254 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
255 2, typeof (Enum [])) as Enum [];
256 Assert.IsNotNull (enums, "#B1");
257 Assert.AreEqual (1, enums.Length, "#B2");
258 Assert.AreEqual (typeof (E), enums [0].GetType (), "#B3");
259 Assert.AreEqual (E.Cc, enums [0], "#B4");
262 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
263 "2", typeof (Enum [])) as Enum [];
265 } catch (ArgumentException ex) {
266 // The value passed in must be an enum base or an
267 // underlying type for an enum, such as an Int32
268 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
269 Assert.IsNull (ex.InnerException, "#C3");
270 Assert.IsNotNull (ex.Message, "#C4");
271 Assert.IsNotNull (ex.ParamName, "#C5");
272 Assert.AreEqual ("value", ex.ParamName, "#C6");
276 converter.ConvertTo (null, CultureInfo.InvariantCulture,
277 null, typeof (Enum []));
279 } catch (NotSupportedException ex) {
280 // 'EnumConverter' is unable to convert '(null)'
281 // to 'System.Enum[]'
282 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#D2");
283 Assert.IsNull (ex.InnerException, "#D3");
284 Assert.IsNotNull (ex.Message, "#D4");
285 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#D5");
286 Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#D6");
287 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (Enum []).FullName + "'") != -1, "#D7");
291 converter.ConvertTo (null, CultureInfo.InvariantCulture,
292 "Cc", typeof (Enum []));
294 } catch (ArgumentException ex) {
295 // The value passed in must be an enum base or an
296 // underlying type for an enum, such as an Int32
297 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#E2");
298 Assert.IsNull (ex.InnerException, "#E3");
299 Assert.IsNotNull (ex.Message, "#E4");
300 Assert.IsNotNull (ex.ParamName, "#E5");
301 Assert.AreEqual ("value", ex.ParamName, "#E6");
306 public void ConvertTo_EnumArray_Flags ()
309 EnumConverter converter = new EnumConverter (typeof (E2));
311 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
312 E.Bb, typeof (Enum [])) as Enum [];
313 Assert.IsNotNull (enums, "#A1");
314 Assert.AreEqual (1, enums.Length, "#A2");
315 Assert.AreEqual (typeof (E2), enums [0].GetType (), "#A3");
316 Assert.AreEqual (E2.Aa, enums [0], "#A4");
318 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
319 E2.Bb, typeof (Enum [])) as Enum [];
320 Assert.IsNotNull (enums, "#B1");
321 Assert.AreEqual (1, enums.Length, "#B2");
322 Assert.AreEqual (typeof (E2), enums [0].GetType (), "#B3");
323 Assert.AreEqual (E2.Bb, enums [0], "#B4");
325 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
326 (E2) 0, typeof (Enum [])) as Enum [];
327 Assert.IsNotNull (enums, "#C1");
328 Assert.AreEqual (0, enums.Length, "#C2");
330 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
331 (E2) 18, typeof (Enum [])) as Enum [];
332 Assert.IsNotNull (enums, "#D1");
333 Assert.AreEqual (2, enums.Length, "#D2");
334 Assert.AreEqual (typeof (E2), enums [0].GetType (), "#D3");
335 Assert.AreEqual (E2.Bb, enums [0], "#D4");
336 Assert.AreEqual (typeof (E2), enums [1].GetType (), "#D5");
337 Assert.AreEqual ((E2) 16, enums [1], "#D6");
340 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
341 5, typeof (Enum [])) as Enum [];
343 } catch (InvalidCastException ex) {
344 // Unable to cast object of type 'System.Int32'
345 // to type 'System.Enum'
346 Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#E2");
347 Assert.IsNull (ex.InnerException, "#E3");
348 Assert.IsNotNull (ex.Message, "#E4");
352 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
353 "2", typeof (Enum [])) as Enum [];
355 } catch (InvalidCastException ex) {
356 // Unable to cast object of type 'System.String'
357 // to type 'System.Enum'
358 Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#F2");
359 Assert.IsNull (ex.InnerException, "#F3");
360 Assert.IsNotNull (ex.Message, "#F4");
364 converter.ConvertTo (null, CultureInfo.InvariantCulture,
365 null, typeof (Enum []));
367 } catch (NotSupportedException ex) {
368 // 'EnumConverter' is unable to convert '(null)'
369 // to 'System.Enum[]'
370 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#G2");
371 Assert.IsNull (ex.InnerException, "#G3");
372 Assert.IsNotNull (ex.Message, "#G4");
373 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#G5");
374 Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#G6");
375 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (Enum []).FullName + "'") != -1, "#G7");
379 converter.ConvertTo (null, CultureInfo.InvariantCulture,
380 "Bb,Cc", typeof (Enum []));
382 } catch (InvalidCastException ex) {
383 // Unable to cast object of type 'System.String'
384 // to type 'System.Enum'
385 Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#H2");
386 Assert.IsNull (ex.InnerException, "#H3");
387 Assert.IsNotNull (ex.Message, "#H4");
391 converter.ConvertTo (null, CultureInfo.InvariantCulture,
392 "2,4", typeof (Enum []));
394 } catch (InvalidCastException ex) {
395 // Unable to cast object of type 'System.String'
396 // to type 'System.Enum'
397 Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#I2");
398 Assert.IsNull (ex.InnerException, "#I3");
399 Assert.IsNotNull (ex.Message, "#I4");
402 converter = new EnumConverter (typeof (F2));
404 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
405 (F2) 15, typeof (Enum [])) as Enum [];
406 Assert.IsNotNull (enums, "#J1");
407 Assert.AreEqual (3, enums.Length, "#J2");
408 Assert.AreEqual (typeof (F2), enums [0].GetType (), "#J3");
409 Assert.AreEqual (F2.Bb, enums [0], "#J4");
410 Assert.AreEqual (typeof (F2), enums [1].GetType (), "#J5");
411 Assert.AreEqual (F2.Dd, enums [1], "#J6");
412 Assert.AreEqual (typeof (F2), enums [2].GetType (), "#J5");
413 Assert.AreEqual ((F2) 5, enums [2], "#J6");
415 // Test Flags conversion of enum value 0
416 converter = new EnumConverter (typeof (E3));
417 enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
418 E3.Aa, typeof (Enum [])) as Enum [];
419 Assert.AreEqual (1, enums.Length, "#H1");
420 Assert.AreEqual (typeof (E3), enums [0].GetType (), "#H2");
421 Assert.AreEqual (E3.Aa, enums[0], "#H3");
425 public void ConvertTo_InstanceDescriptor ()
427 InstanceDescriptor idesc;
429 EnumConverter converter = new EnumConverter (typeof (E));
431 idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
432 E.Bb, typeof (InstanceDescriptor)) as InstanceDescriptor;
433 Assert.IsNotNull (idesc, "#A1");
434 Assert.IsNotNull (idesc.Arguments, "#A2");
435 Assert.AreEqual (0, idesc.Arguments.Count, "#A3");
436 Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#A4");
437 Assert.IsTrue (idesc.IsComplete, "#A5");
438 fi = idesc.MemberInfo as FieldInfo;
439 Assert.IsNotNull (fi, "#A6");
440 Assert.AreEqual (typeof (E), fi.DeclaringType, "#A7");
441 Assert.AreEqual (typeof (E), fi.FieldType, "#A8");
442 Assert.IsTrue (fi.IsStatic, "#A9");
443 Assert.AreEqual ("Bb", fi.Name, "#A10");
444 Assert.AreEqual (E.Bb, fi.GetValue (null), "#A11");
447 idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
448 "2", typeof (InstanceDescriptor)) as InstanceDescriptor;
449 Assert.IsNotNull (idesc, "#B1");
450 Assert.IsNotNull (idesc.Arguments, "#B2");
451 Assert.AreEqual (0, idesc.Arguments.Count, "#B3");
452 Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#B4");
453 Assert.IsTrue (idesc.IsComplete, "#B5");
454 fi = idesc.MemberInfo as FieldInfo;
455 Assert.IsNotNull (fi, "#B6");
456 Assert.AreEqual (typeof (E), fi.DeclaringType, "#B7");
457 Assert.AreEqual (typeof (E), fi.FieldType, "#B8");
458 Assert.IsTrue (fi.IsStatic, "#B9");
459 Assert.AreEqual ("Cc", fi.Name, "#B10");
460 Assert.AreEqual (E.Cc, fi.GetValue (null), "#B11");
463 idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
464 2, typeof (InstanceDescriptor)) as InstanceDescriptor;
465 Assert.IsNotNull (idesc, "#C1");
466 Assert.IsNotNull (idesc.Arguments, "#C2");
467 Assert.AreEqual (0, idesc.Arguments.Count, "#C3");
468 Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#C4");
469 Assert.IsTrue (idesc.IsComplete, "#C5");
470 fi = idesc.MemberInfo as FieldInfo;
471 Assert.IsNotNull (fi, "#C6");
472 Assert.AreEqual (typeof (E), fi.DeclaringType, "#C7");
473 Assert.AreEqual (typeof (E), fi.FieldType, "#C8");
474 Assert.IsTrue (fi.IsStatic, "#C9");
475 Assert.AreEqual ("Cc", fi.Name, "#C10");
476 Assert.AreEqual (E.Cc, fi.GetValue (null), "#C11");
478 idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
479 (E) 2, typeof (InstanceDescriptor)) as InstanceDescriptor;
480 Assert.IsNotNull (idesc, "#D1");
481 Assert.IsNotNull (idesc.Arguments, "#D2");
482 Assert.AreEqual (0, idesc.Arguments.Count, "#D3");
483 Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#D4");
484 Assert.IsTrue (idesc.IsComplete, "#D5");
485 fi = idesc.MemberInfo as FieldInfo;
486 Assert.IsNotNull (fi, "#D6");
487 Assert.AreEqual (typeof (E), fi.DeclaringType, "#D7");
488 Assert.AreEqual (typeof (E), fi.FieldType, "#D8");
489 Assert.IsTrue (fi.IsStatic, "#D9");
490 Assert.AreEqual ("Cc", fi.Name, "#D10");
491 Assert.AreEqual (E.Cc, fi.GetValue (null), "#D11");
494 converter.ConvertTo (null, CultureInfo.InvariantCulture,
495 null, typeof (InstanceDescriptor));
497 } catch (NotSupportedException ex) {
498 // 'EnumConverter' is unable to convert '(null)'
499 // to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'
500 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#E2");
501 Assert.IsNull (ex.InnerException, "#E3");
502 Assert.IsNotNull (ex.Message, "#E4");
503 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#E5");
504 Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#E6");
505 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (InstanceDescriptor).FullName + "'") != -1, "#E7");
509 converter.ConvertTo (null, CultureInfo.InvariantCulture,
510 "5", typeof (InstanceDescriptor));
512 } catch (ArgumentException ex) {
513 // The value '5' is not a valid value for the enum 'E'
514 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#F2");
515 Assert.IsNull (ex.InnerException, "#F3");
516 Assert.IsNotNull (ex.Message, "#F4");
517 Assert.IsTrue (ex.Message.IndexOf ("'5'") != -1, "#F5");
518 Assert.IsTrue (ex.Message.IndexOf ("'E'") != -1, "#F6");
519 Assert.IsNull (ex.ParamName, "#F7");
523 converter.ConvertTo (null, CultureInfo.InvariantCulture,
524 "Cc", typeof (InstanceDescriptor));
526 } catch (FormatException ex) {
527 // Input string was not in a correct format
528 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#G2");
529 Assert.IsNull (ex.InnerException, "#G3");
530 Assert.IsNotNull (ex.Message, "#G4");
534 converter.ConvertTo (null, CultureInfo.InvariantCulture,
535 (E) 666, typeof (InstanceDescriptor));
537 } catch (ArgumentException ex) {
538 // The value '666' is not a valid value for the enum 'E'
539 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#F2");
540 Assert.IsNull (ex.InnerException, "#F3");
541 Assert.IsNotNull (ex.Message, "#F4");
542 Assert.IsTrue (ex.Message.IndexOf ("'666'") != -1, "#F5");
543 Assert.IsTrue (ex.Message.IndexOf ("'E'") != -1, "#F6");
544 Assert.IsNull (ex.ParamName, "#F7");
549 public void ConvertTo_InstanceDescriptor_Flags ()
551 InstanceDescriptor idesc;
554 ParameterInfo [] parameters;
556 EnumConverter converter = new EnumConverter (typeof (E2));
558 idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
559 E2.Bb, typeof (InstanceDescriptor)) as InstanceDescriptor;
560 Assert.IsNotNull (idesc, "#A1");
561 Assert.IsNotNull (idesc.Arguments, "#A2");
562 Assert.AreEqual (0, idesc.Arguments.Count, "#A3");
563 Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#A4");
564 Assert.IsTrue (idesc.IsComplete, "#A5");
565 fi = idesc.MemberInfo as FieldInfo;
566 Assert.IsNotNull (fi, "#A6");
567 Assert.AreEqual (typeof (E2), fi.DeclaringType, "#A7");
568 Assert.AreEqual (typeof (E2), fi.FieldType, "#A8");
569 Assert.IsTrue (fi.IsStatic, "#A9");
570 Assert.AreEqual ("Bb", fi.Name, "#A10");
571 Assert.AreEqual (E2.Bb, fi.GetValue (null), "#A11");
573 idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
574 E.Bb, typeof (InstanceDescriptor)) as InstanceDescriptor;
575 Assert.IsNotNull (idesc, "#B1");
576 Assert.IsNotNull (idesc.Arguments, "#B2");
577 Assert.AreEqual (0, idesc.Arguments.Count, "#B3");
578 Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#B4");
579 Assert.IsTrue (idesc.IsComplete, "#B5");
580 fi = idesc.MemberInfo as FieldInfo;
581 Assert.IsNotNull (fi, "#B6");
582 Assert.AreEqual (typeof (E2), fi.DeclaringType, "#B7");
583 Assert.AreEqual (typeof (E2), fi.FieldType, "#B8");
584 Assert.IsTrue (fi.IsStatic, "#B9");
585 Assert.AreEqual ("Aa", fi.Name, "#B10");
586 Assert.AreEqual (E2.Aa, fi.GetValue (null), "#B11");
588 idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
589 E2.Bb | E2.Dd, typeof (InstanceDescriptor))
590 as InstanceDescriptor;
591 Assert.IsNotNull (idesc, "#C1");
592 Assert.IsNotNull (idesc.Arguments, "#C2");
593 Assert.AreEqual (2, idesc.Arguments.Count, "#C3");
594 Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#C4");
595 arguments = (object []) idesc.Arguments;
596 Assert.AreEqual (typeof (E2), arguments [0], "#C5");
597 Assert.AreEqual (typeof (int), arguments [1].GetType (), "#C6");
598 Assert.AreEqual (10, arguments [1], "#C7");
599 Assert.IsTrue (idesc.IsComplete, "#C8");
600 mi = idesc.MemberInfo as MethodInfo;
601 Assert.IsNotNull (mi, "#C9");
602 Assert.AreEqual ("ToObject", mi.Name, "#C10");
603 Assert.AreEqual (typeof (Enum), mi.DeclaringType, "#C11");
604 parameters = mi.GetParameters ();
605 Assert.AreEqual (2, parameters.Length, "#C12");
606 Assert.AreEqual (typeof (Type), parameters [0].ParameterType, "#C13");
607 Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#C14");
609 idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
610 "5", typeof (InstanceDescriptor)) as InstanceDescriptor;
611 Assert.IsNotNull (idesc, "#D1");
612 Assert.IsNotNull (idesc.Arguments, "#D2");
613 Assert.AreEqual (2, idesc.Arguments.Count, "#D3");
614 Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#D4");
615 arguments = (object []) idesc.Arguments;
616 Assert.AreEqual (typeof (E2), arguments [0], "#D5");
617 Assert.AreEqual (typeof (int), arguments [1].GetType (), "#D6");
618 Assert.AreEqual (5, arguments [1], "#D7");
619 Assert.IsTrue (idesc.IsComplete, "#D8");
620 mi = idesc.MemberInfo as MethodInfo;
621 Assert.IsNotNull (mi, "#D9");
622 Assert.AreEqual ("ToObject", mi.Name, "#D10");
623 Assert.AreEqual (typeof (Enum), mi.DeclaringType, "#D11");
624 parameters = mi.GetParameters ();
625 Assert.AreEqual (2, parameters.Length, "#D12");
626 Assert.AreEqual (typeof (Type), parameters [0].ParameterType, "#D13");
627 Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#D14");
629 idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
630 3, typeof (InstanceDescriptor)) as InstanceDescriptor;
631 Assert.IsNotNull (idesc, "#E1");
632 Assert.IsNotNull (idesc.Arguments, "#E2");
633 Assert.AreEqual (2, idesc.Arguments.Count, "#E3");
634 Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#E4");
635 arguments = (object []) idesc.Arguments;
636 Assert.AreEqual (typeof (E2), arguments [0], "#E5");
637 Assert.AreEqual (typeof (int), arguments [1].GetType (), "#E6");
638 Assert.AreEqual (3, arguments [1], "#E7");
639 Assert.IsTrue (idesc.IsComplete, "#E8");
640 mi = idesc.MemberInfo as MethodInfo;
641 Assert.IsNotNull (mi, "#E9");
642 Assert.AreEqual ("ToObject", mi.Name, "#E10");
643 Assert.AreEqual (typeof (Enum), mi.DeclaringType, "#E11");
644 parameters = mi.GetParameters ();
645 Assert.AreEqual (2, parameters.Length, "#E12");
646 Assert.AreEqual (typeof (Type), parameters [0].ParameterType, "#E13");
647 Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#E14");
650 converter.ConvertTo (null, CultureInfo.InvariantCulture,
651 null, typeof (InstanceDescriptor));
653 } catch (NotSupportedException ex) {
654 // 'EnumConverter' is unable to convert '(null)'
655 // to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'
656 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#F2");
657 Assert.IsNull (ex.InnerException, "#F3");
658 Assert.IsNotNull (ex.Message, "#F4");
659 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#F5");
660 Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#F6");
661 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (InstanceDescriptor).FullName + "'") != -1, "#F7");
665 converter.ConvertTo (null, CultureInfo.InvariantCulture,
666 "2,1", typeof (InstanceDescriptor));
668 } catch (FormatException ex) {
669 // Input string was not in a correct format
670 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#G2");
671 Assert.IsNull (ex.InnerException, "#G3");
672 Assert.IsNotNull (ex.Message, "#G4");
676 converter.ConvertTo (null, CultureInfo.InvariantCulture,
677 "Cc", typeof (InstanceDescriptor));
679 } catch (FormatException ex) {
680 // Input string was not in a correct format
681 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#H2");
682 Assert.IsNull (ex.InnerException, "#H3");
683 Assert.IsNotNull (ex.Message, "#H4");
687 converter.ConvertTo (null, CultureInfo.InvariantCulture,
688 (E2) 666 | (E2) 222, typeof (InstanceDescriptor));
690 } catch (NotSupportedException ex) {
691 // 'EnumConverter' is unable to convert 'MonoTests.System.ComponentModel.EnumConverterTests+E2'
692 // to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'
693 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#I2");
694 Assert.IsNull (ex.InnerException, "#I3");
695 Assert.IsNotNull (ex.Message, "#I4");
696 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#I5");
697 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (E2).FullName + "'") != -1, "#I6");
698 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (InstanceDescriptor).FullName + "'") != -1, "#I7");
701 converter = new EnumConverter (typeof (F2));
702 idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
703 F2.Bb | F2.Dd, typeof (InstanceDescriptor))
704 as InstanceDescriptor;
705 Assert.IsNotNull (idesc, "#J1");
706 Assert.IsNotNull (idesc.Arguments, "#J2");
707 Assert.AreEqual (2, idesc.Arguments.Count, "#J3");
708 Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#J4");
709 arguments = (object []) idesc.Arguments;
710 Assert.AreEqual (typeof (F2), arguments [0], "#J5");
711 Assert.AreEqual (typeof (byte), arguments [1].GetType (), "#J6");
712 Assert.AreEqual (10, arguments [1], "#J7");
713 Assert.IsTrue (idesc.IsComplete, "#J8");
714 mi = idesc.MemberInfo as MethodInfo;
715 Assert.IsNotNull (mi, "#J9");
716 Assert.AreEqual ("ToObject", mi.Name, "#J10");
717 Assert.AreEqual (typeof (Enum), mi.DeclaringType, "#J11");
718 parameters = mi.GetParameters ();
719 Assert.AreEqual (2, parameters.Length, "#J12");
720 Assert.AreEqual (typeof (Type), parameters [0].ParameterType, "#J13");
721 Assert.AreEqual (typeof (byte), parameters [1].ParameterType, "#J14");
725 public void ConvertTo_String ()
727 EnumConverter converter = new EnumConverter (typeof (E));
729 Assert.AreEqual ("Bb", converter.ConvertTo (null,
730 CultureInfo.InvariantCulture, E.Bb,
731 typeof (string)), "#A1");
732 Assert.AreEqual ("Dd", converter.ConvertTo (null,
733 CultureInfo.InvariantCulture, 3,
734 typeof (string)), "#A2");
735 Assert.AreEqual (string.Empty, converter.ConvertTo (
736 null, CultureInfo.InvariantCulture, null,
737 typeof (string)), "#A3");
738 Assert.AreEqual ("Cc", converter.ConvertTo (
739 null, CultureInfo.InvariantCulture, (E) 2,
740 typeof (string)), "#A4");
741 Assert.AreEqual ("Cc", converter.ConvertTo (null,
742 CultureInfo.InvariantCulture, E2.Bb,
743 typeof (string)), "#A5");
744 Assert.AreEqual ("Dd", converter.ConvertTo (null,
745 CultureInfo.InvariantCulture, E.Bb | E.Dd,
746 typeof (string)), "#A6");
749 converter.ConvertTo (null, CultureInfo.InvariantCulture,
750 (E) 666, typeof (string));
752 } catch (ArgumentException ex) {
753 // The value '666' is not a valid value for the enum 'E'
754 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
755 Assert.IsNull (ex.InnerException, "#B3");
756 Assert.IsNotNull (ex.Message, "#B4");
757 Assert.IsTrue (ex.Message.IndexOf ("'666'") != -1, "#B5");
758 Assert.IsTrue (ex.Message.IndexOf ("'E'") != -1, "#B6");
759 Assert.IsNull (ex.ParamName, "#B7");
763 converter.ConvertTo (null, CultureInfo.InvariantCulture,
764 "Cc", typeof (string));
766 } catch (FormatException ex) {
767 // Input string was not in a correct format
768 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#C2");
769 Assert.IsNull (ex.InnerException, "#C3");
770 Assert.IsNotNull (ex.Message, "#C4");
774 converter = new EnumConverter (typeof (E2));
776 Assert.AreEqual ("Bb", converter.ConvertTo (null,
777 CultureInfo.InvariantCulture, E2.Bb,
778 typeof (string)), "#B1");
779 Assert.AreEqual ("Aa, Bb", converter.ConvertTo (null,
780 CultureInfo.InvariantCulture, 3,
781 typeof (string)), "#B2");
782 Assert.AreEqual (string.Empty, converter.ConvertTo (
783 null, CultureInfo.InvariantCulture, null,
784 typeof (string)), "#B3");
785 Assert.AreEqual ("Bb", converter.ConvertTo (
786 null, CultureInfo.InvariantCulture, (E2) 2,
787 typeof (string)), "#B4");
788 Assert.AreEqual ("Aa, Bb", converter.ConvertTo (
789 null, CultureInfo.InvariantCulture, E.Dd,
790 typeof (string)), "#B5");
791 Assert.AreEqual ("Bb, Dd", converter.ConvertTo (null,
792 CultureInfo.InvariantCulture, E2.Bb | E2.Dd,
793 typeof (string)), "#B6");