EnumConverterTests.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. //
  2. // System.ComponentModel.EnumConverter test cases
  3. //
  4. // Authors:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (c) 2007 Gert Driesen
  8. //
  9. using System;
  10. using System.ComponentModel;
  11. using System.ComponentModel.Design.Serialization;
  12. using System.Globalization;
  13. using System.Reflection;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.ComponentModel
  16. {
  17. [TestFixture]
  18. public class EnumConverterTests
  19. {
  20. [Test]
  21. public void CanConvertFrom ()
  22. {
  23. EnumConverter converter = new EnumConverter (typeof (E));
  24. Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#A1");
  25. Assert.IsFalse (converter.CanConvertFrom (typeof (Enum)), "#A2");
  26. Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#A3");
  27. Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#A4");
  28. Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#A5");
  29. Assert.IsFalse (converter.CanConvertFrom (typeof (string [])), "#A6");
  30. #if NET_2_0
  31. Assert.IsTrue (converter.CanConvertFrom (typeof (Enum [])), "#A7");
  32. #else
  33. Assert.IsFalse (converter.CanConvertFrom (typeof (Enum [])), "#A7");
  34. #endif
  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. #if NET_2_0
  43. Assert.IsTrue (converter.CanConvertFrom (typeof (Enum [])), "#B7");
  44. #else
  45. Assert.IsFalse (converter.CanConvertFrom (typeof (Enum [])), "#B7");
  46. #endif
  47. }
  48. [Test]
  49. public void CanConvertTo ()
  50. {
  51. EnumConverter converter = new EnumConverter (typeof (E));
  52. Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#A1");
  53. Assert.IsFalse (converter.CanConvertTo (typeof (Enum)), "#A2");
  54. Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#A3");
  55. Assert.IsFalse (converter.CanConvertTo (typeof (int)), "#A4");
  56. Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "#A5");
  57. Assert.IsFalse (converter.CanConvertTo (typeof (string [])), "#A6");
  58. #if NET_2_0
  59. Assert.IsTrue (converter.CanConvertTo (typeof (Enum [])), "#A7");
  60. #else
  61. Assert.IsFalse (converter.CanConvertTo (typeof (Enum [])), "#A7");
  62. #endif
  63. converter = new EnumConverter (typeof (E2));
  64. Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#B1");
  65. Assert.IsFalse (converter.CanConvertTo (typeof (Enum)), "#B2");
  66. Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#B3");
  67. Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#B4");
  68. Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "#B5");
  69. Assert.IsFalse (converter.CanConvertTo (typeof (string [])), "#B6");
  70. #if NET_2_0
  71. Assert.IsTrue (converter.CanConvertTo (typeof (Enum [])), "#B7");
  72. #else
  73. Assert.IsFalse (converter.CanConvertTo (typeof (Enum [])), "#B7");
  74. #endif
  75. }
  76. [Test]
  77. public void ConvertFrom_Null ()
  78. {
  79. EnumConverter converter = new EnumConverter (typeof (E));
  80. try {
  81. converter.ConvertFrom (null, CultureInfo.InvariantCulture, null);
  82. Assert.Fail ("#1");
  83. } catch (NotSupportedException ex) {
  84. // EnumConverter cannot convert from (null)
  85. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  86. Assert.IsNull (ex.InnerException, "#3");
  87. Assert.IsNotNull (ex.Message, "#4");
  88. Assert.IsTrue (ex.Message.IndexOf (typeof (EnumConverter).Name) != -1, "#5");
  89. Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#6");
  90. }
  91. }
  92. #if NET_2_0
  93. [Test]
  94. public void ConvertFrom_EnumArray ()
  95. {
  96. EnumConverter converter = new EnumConverter (typeof (E));
  97. Assert.AreEqual (E.Aa, converter.ConvertFrom (null,
  98. CultureInfo.InvariantCulture,
  99. (Enum []) new Enum [] { E.Aa }), "#A1");
  100. Assert.AreEqual ((E) 8, converter.ConvertFrom (null,
  101. CultureInfo.InvariantCulture,
  102. (Enum []) new Enum [] { E.Aa, E2.Dd }), "#A2");
  103. Assert.AreEqual ((E) 958, converter.ConvertFrom (null,
  104. CultureInfo.InvariantCulture,
  105. (Enum []) new Enum [] { (E2) 444, (E) 666 }), "#A3");
  106. Assert.AreEqual ((E) 0, converter.ConvertFrom (null,
  107. CultureInfo.InvariantCulture,
  108. (Enum []) new Enum [0]), "#A4");
  109. converter = new EnumConverter (typeof (E2));
  110. Assert.AreEqual ((E2) 0, converter.ConvertFrom (null,
  111. CultureInfo.InvariantCulture,
  112. (Enum []) new Enum [] { E.Aa }), "#B1");
  113. Assert.AreEqual (E2.Dd, converter.ConvertFrom (null,
  114. CultureInfo.InvariantCulture,
  115. (Enum []) new Enum [] { E.Aa, E2.Dd }), "#B2");
  116. Assert.AreEqual ((E2) 958, converter.ConvertFrom (null,
  117. CultureInfo.InvariantCulture,
  118. (Enum []) new Enum [] { (E2) 444, (E) 666 }), "#B3");
  119. Assert.AreEqual ((E2) 0, converter.ConvertFrom (null,
  120. CultureInfo.InvariantCulture,
  121. (Enum []) new Enum [0]), "#B4");
  122. Assert.AreEqual (E2.Bb | E2.Dd, converter.ConvertFrom (null,
  123. CultureInfo.InvariantCulture,
  124. (Enum []) new Enum [] { E2.Bb, E2.Dd }), "#B5");
  125. }
  126. #endif
  127. [Test]
  128. public void ConvertFrom_String ()
  129. {
  130. EnumConverter converter = new EnumConverter (typeof (E));
  131. Assert.AreEqual (E.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Bb"), "#A1");
  132. Assert.AreEqual (E.Cc, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "2"), "#A2");
  133. Assert.AreEqual (E.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " Bb "), "#A3");
  134. Assert.AreEqual (E.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " 3 "), "#A4");
  135. Assert.AreEqual ((E) 666, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "666"), "#A5");
  136. Assert.AreEqual (E.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Bb,Dd"), "#A6");
  137. Assert.AreEqual (E.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Dd,Bb"), "#A7");
  138. Assert.AreEqual (E.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Aa,Bb"), "#A8");
  139. try {
  140. converter.ConvertFrom (null, CultureInfo.InvariantCulture, string.Empty);
  141. Assert.Fail ("#B1");
  142. #if NET_2_0
  143. } catch (FormatException ex) {
  144. // is not a valid value for E
  145. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#B2");
  146. Assert.IsNotNull (ex.InnerException, "#B3");
  147. Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#B4");
  148. Assert.IsNotNull (ex.Message, "#B5");
  149. Assert.IsTrue (ex.Message.IndexOf ("E") != -1, "#B6");
  150. // Must specify valid information for parsing in the string
  151. ArgumentException inner = (ArgumentException) ex.InnerException;
  152. Assert.IsNull (inner.InnerException, "#B7");
  153. Assert.IsNotNull (inner.Message, "#B8");
  154. }
  155. #else
  156. } catch (ArgumentException ex) {
  157. // Must specify valid information for parsing in the string
  158. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  159. Assert.IsNull (ex.InnerException, "#B3");
  160. Assert.IsNotNull (ex.Message, "#B4");
  161. }
  162. #endif
  163. try {
  164. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "YY");
  165. Assert.Fail ("#C1");
  166. #if NET_2_0
  167. } catch (FormatException ex) {
  168. // YY is not a valid value for E
  169. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#C2");
  170. Assert.IsNotNull (ex.InnerException, "#C3");
  171. Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#C4");
  172. Assert.IsNotNull (ex.Message, "#C5");
  173. Assert.IsTrue (ex.Message.IndexOf ("YY") != -1, "#C6");
  174. Assert.IsTrue (ex.Message.IndexOf ("E") != -1, "#C7");
  175. // Requested value YY was not found
  176. ArgumentException inner = (ArgumentException) ex.InnerException;
  177. //Assert.IsNull (inner.InnerException, "#C8");
  178. Assert.IsNotNull (inner.Message, "#C9");
  179. Assert.IsTrue (inner.Message.IndexOf ("YY") != -1, "#C10");
  180. Assert.IsNull (inner.ParamName, "#C11");
  181. }
  182. #else
  183. } catch (ArgumentException ex) {
  184. // Requested value YY was not found
  185. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  186. //Assert.IsNull (ex.InnerException, "#C3");
  187. Assert.IsNotNull (ex.Message, "#C4");
  188. Assert.IsTrue (ex.Message.IndexOf ("YY") != -1, "#C5");
  189. Assert.IsNull (ex.ParamName, "#C6");
  190. }
  191. #endif
  192. }
  193. [Test]
  194. public void ConvertFrom_String_Flags ()
  195. {
  196. EnumConverter converter = new EnumConverter (typeof (E2));
  197. Assert.AreEqual (E2.Cc, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Cc"), "#B1");
  198. Assert.AreEqual (E2.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "8"), "#B2");
  199. Assert.AreEqual (E2.Cc | E2.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Cc,Dd"), "#B3");
  200. Assert.AreEqual (E2.Aa | E2.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "3"), "#B4");
  201. Assert.AreEqual (E2.Bb | E2.Cc, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "2,4"), "#B5");
  202. Assert.AreEqual (E2.Aa | E2.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " 1 , 8 "), "#B5");
  203. Assert.AreEqual ((E2) 666, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "666"), "#B6");
  204. Assert.AreEqual (E2.Cc | E2.Dd, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " Dd , Cc "), "#B7");
  205. Assert.AreEqual (E2.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " Bb "), "#B8");
  206. Assert.AreEqual (E2.Aa | E2.Bb, converter.ConvertFrom (null, CultureInfo.InvariantCulture, " 3 "), "#B9");
  207. try {
  208. converter.ConvertFrom (null, CultureInfo.InvariantCulture, string.Empty);
  209. Assert.Fail ("#B1");
  210. #if NET_2_0
  211. } catch (FormatException ex) {
  212. // is not a valid value for E2
  213. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#B2");
  214. Assert.IsNotNull (ex.InnerException, "#B3");
  215. Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#B4");
  216. Assert.IsNotNull (ex.Message, "#B5");
  217. Assert.IsTrue (ex.Message.IndexOf ("E2") != -1, "#B6");
  218. // Must specify valid information for parsing in the string
  219. ArgumentException inner = (ArgumentException) ex.InnerException;
  220. Assert.IsNull (inner.InnerException, "#B7");
  221. Assert.IsNotNull (inner.Message, "#B8");
  222. }
  223. #else
  224. } catch (ArgumentException ex) {
  225. // Must specify valid information for parsing in the string
  226. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  227. Assert.IsNull (ex.InnerException, "#B3");
  228. Assert.IsNotNull (ex.Message, "#B4");
  229. }
  230. #endif
  231. try {
  232. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "Aa Bb");
  233. Assert.Fail ("#C1");
  234. #if NET_2_0
  235. } catch (FormatException ex) {
  236. // Aa Bb is not a valid value for E2
  237. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#C2");
  238. Assert.IsNotNull (ex.InnerException, "#C3");
  239. Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#C4");
  240. Assert.IsNotNull (ex.Message, "#C5");
  241. Assert.IsTrue (ex.Message.IndexOf ("Aa Bb") != -1, "#C6");
  242. Assert.IsTrue (ex.Message.IndexOf ("E2") != -1, "#C7");
  243. // Requested value Aa Bb was not found
  244. ArgumentException inner = (ArgumentException) ex.InnerException;
  245. Assert.IsNotNull (inner.Message, "#C9");
  246. Assert.IsTrue (inner.Message.IndexOf ("Aa Bb") != -1, "#C10");
  247. Assert.IsNull (inner.ParamName, "#C11");
  248. }
  249. #else
  250. } catch (ArgumentException ex) {
  251. // Requested value Aa Bb was not found
  252. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  253. Assert.IsNotNull (ex.Message, "#C3");
  254. Assert.IsTrue (ex.Message.IndexOf ("Aa Bb") != -1, "#C4");
  255. Assert.IsNull (ex.ParamName, "#C5");
  256. }
  257. #endif
  258. try {
  259. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "2,");
  260. Assert.Fail ("#D1");
  261. #if NET_2_0
  262. } catch (FormatException ex) {
  263. // 2, is not a valid value for E2
  264. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#D2");
  265. Assert.IsNotNull (ex.InnerException, "#D3");
  266. Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#D4");
  267. Assert.IsNotNull (ex.Message, "#D5");
  268. Assert.IsTrue (ex.Message.IndexOf ("2,") != -1, "#D6");
  269. Assert.IsTrue (ex.Message.IndexOf ("E2") != -1, "#D7");
  270. // Must specify valid information for parsing in the string
  271. ArgumentException inner = (ArgumentException) ex.InnerException;
  272. Assert.IsNull (inner.InnerException, "#D8");
  273. Assert.IsNotNull (inner.Message, "#D9");
  274. Assert.IsFalse (inner.Message.IndexOf ("2,") != -1, "#D10");
  275. }
  276. #else
  277. } catch (ArgumentException ex) {
  278. // Must specify valid information for parsing in the string
  279. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  280. Assert.IsNull (ex.InnerException, "#D3");
  281. Assert.IsNotNull (ex.Message, "#D4");
  282. Assert.IsFalse (ex.Message.IndexOf ("2,") != -1, "#D5");
  283. }
  284. #endif
  285. }
  286. #if NET_2_0
  287. [Test]
  288. public void ConvertTo_EnumArray ()
  289. {
  290. Enum [] enums;
  291. EnumConverter converter = new EnumConverter (typeof (E));
  292. enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  293. E.Bb, typeof (Enum [])) as Enum [];
  294. Assert.IsNotNull (enums, "#A1");
  295. Assert.AreEqual (1, enums.Length, "#A2");
  296. Assert.AreEqual (typeof (E), enums [0].GetType (), "#A3");
  297. Assert.AreEqual (E.Bb, enums [0], "#A4");
  298. enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  299. 2, typeof (Enum [])) as Enum [];
  300. Assert.IsNotNull (enums, "#B1");
  301. Assert.AreEqual (1, enums.Length, "#B2");
  302. Assert.AreEqual (typeof (E), enums [0].GetType (), "#B3");
  303. Assert.AreEqual (E.Cc, enums [0], "#B4");
  304. try {
  305. enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  306. "2", typeof (Enum [])) as Enum [];
  307. Assert.Fail ("#C1");
  308. } catch (ArgumentException ex) {
  309. // The value passed in must be an enum base or an
  310. // underlying type for an enum, such as an Int32
  311. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  312. Assert.IsNull (ex.InnerException, "#C3");
  313. Assert.IsNotNull (ex.Message, "#C4");
  314. Assert.IsNotNull (ex.ParamName, "#C5");
  315. Assert.AreEqual ("value", ex.ParamName, "#C6");
  316. }
  317. try {
  318. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  319. null, typeof (Enum []));
  320. Assert.Fail ("#D1");
  321. } catch (NotSupportedException ex) {
  322. // 'EnumConverter' is unable to convert '(null)'
  323. // to 'System.Enum[]'
  324. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#D2");
  325. Assert.IsNull (ex.InnerException, "#D3");
  326. Assert.IsNotNull (ex.Message, "#D4");
  327. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#D5");
  328. Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#D6");
  329. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (Enum []).FullName + "'") != -1, "#D7");
  330. }
  331. try {
  332. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  333. "Cc", typeof (Enum []));
  334. Assert.Fail ("#E1");
  335. } catch (ArgumentException ex) {
  336. // The value passed in must be an enum base or an
  337. // underlying type for an enum, such as an Int32
  338. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#E2");
  339. Assert.IsNull (ex.InnerException, "#E3");
  340. Assert.IsNotNull (ex.Message, "#E4");
  341. Assert.IsNotNull (ex.ParamName, "#E5");
  342. Assert.AreEqual ("value", ex.ParamName, "#E6");
  343. }
  344. }
  345. [Test]
  346. public void ConvertTo_EnumArray_Flags ()
  347. {
  348. Enum [] enums;
  349. EnumConverter converter = new EnumConverter (typeof (E2));
  350. enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  351. E.Bb, typeof (Enum [])) as Enum [];
  352. Assert.IsNotNull (enums, "#A1");
  353. Assert.AreEqual (1, enums.Length, "#A2");
  354. Assert.AreEqual (typeof (E2), enums [0].GetType (), "#A3");
  355. Assert.AreEqual (E2.Aa, enums [0], "#A4");
  356. enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  357. E2.Bb, typeof (Enum [])) as Enum [];
  358. Assert.IsNotNull (enums, "#B1");
  359. Assert.AreEqual (1, enums.Length, "#B2");
  360. Assert.AreEqual (typeof (E2), enums [0].GetType (), "#B3");
  361. Assert.AreEqual (E2.Bb, enums [0], "#B4");
  362. enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  363. (E2) 0, typeof (Enum [])) as Enum [];
  364. Assert.IsNotNull (enums, "#C1");
  365. Assert.AreEqual (0, enums.Length, "#C2");
  366. enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  367. (E2) 18, typeof (Enum [])) as Enum [];
  368. Assert.IsNotNull (enums, "#D1");
  369. Assert.AreEqual (2, enums.Length, "#D2");
  370. Assert.AreEqual (typeof (E2), enums [0].GetType (), "#D3");
  371. Assert.AreEqual (E2.Bb, enums [0], "#D4");
  372. Assert.AreEqual (typeof (E2), enums [1].GetType (), "#D5");
  373. Assert.AreEqual ((E2) 16, enums [1], "#D6");
  374. try {
  375. enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  376. 5, typeof (Enum [])) as Enum [];
  377. Assert.Fail ("#E1");
  378. } catch (InvalidCastException ex) {
  379. // Unable to cast object of type 'System.Int32'
  380. // to type 'System.Enum'
  381. Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#E2");
  382. Assert.IsNull (ex.InnerException, "#E3");
  383. Assert.IsNotNull (ex.Message, "#E4");
  384. }
  385. try {
  386. enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  387. "2", typeof (Enum [])) as Enum [];
  388. Assert.Fail ("#F1");
  389. } catch (InvalidCastException ex) {
  390. // Unable to cast object of type 'System.String'
  391. // to type 'System.Enum'
  392. Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#F2");
  393. Assert.IsNull (ex.InnerException, "#F3");
  394. Assert.IsNotNull (ex.Message, "#F4");
  395. }
  396. try {
  397. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  398. null, typeof (Enum []));
  399. Assert.Fail ("#G1");
  400. } catch (NotSupportedException ex) {
  401. // 'EnumConverter' is unable to convert '(null)'
  402. // to 'System.Enum[]'
  403. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#G2");
  404. Assert.IsNull (ex.InnerException, "#G3");
  405. Assert.IsNotNull (ex.Message, "#G4");
  406. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#G5");
  407. Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#G6");
  408. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (Enum []).FullName + "'") != -1, "#G7");
  409. }
  410. try {
  411. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  412. "Bb,Cc", typeof (Enum []));
  413. Assert.Fail ("#H1");
  414. } catch (InvalidCastException ex) {
  415. // Unable to cast object of type 'System.String'
  416. // to type 'System.Enum'
  417. Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#H2");
  418. Assert.IsNull (ex.InnerException, "#H3");
  419. Assert.IsNotNull (ex.Message, "#H4");
  420. }
  421. try {
  422. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  423. "2,4", typeof (Enum []));
  424. Assert.Fail ("#I1");
  425. } catch (InvalidCastException ex) {
  426. // Unable to cast object of type 'System.String'
  427. // to type 'System.Enum'
  428. Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#I2");
  429. Assert.IsNull (ex.InnerException, "#I3");
  430. Assert.IsNotNull (ex.Message, "#I4");
  431. }
  432. converter = new EnumConverter (typeof (F2));
  433. enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  434. (F2) 15, typeof (Enum [])) as Enum [];
  435. Assert.IsNotNull (enums, "#J1");
  436. Assert.AreEqual (3, enums.Length, "#J2");
  437. Assert.AreEqual (typeof (F2), enums [0].GetType (), "#J3");
  438. Assert.AreEqual (F2.Bb, enums [0], "#J4");
  439. Assert.AreEqual (typeof (F2), enums [1].GetType (), "#J5");
  440. Assert.AreEqual (F2.Dd, enums [1], "#J6");
  441. Assert.AreEqual (typeof (F2), enums [2].GetType (), "#J5");
  442. Assert.AreEqual ((F2) 5, enums [2], "#J6");
  443. }
  444. #endif
  445. [Test]
  446. public void ConvertTo_InstanceDescriptor ()
  447. {
  448. InstanceDescriptor idesc;
  449. FieldInfo fi;
  450. EnumConverter converter = new EnumConverter (typeof (E));
  451. idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  452. E.Bb, typeof (InstanceDescriptor)) as InstanceDescriptor;
  453. Assert.IsNotNull (idesc, "#A1");
  454. Assert.IsNotNull (idesc.Arguments, "#A2");
  455. Assert.AreEqual (0, idesc.Arguments.Count, "#A3");
  456. Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#A4");
  457. Assert.IsTrue (idesc.IsComplete, "#A5");
  458. fi = idesc.MemberInfo as FieldInfo;
  459. Assert.IsNotNull (fi, "#A6");
  460. Assert.AreEqual (typeof (E), fi.DeclaringType, "#A7");
  461. Assert.AreEqual (typeof (E), fi.FieldType, "#A8");
  462. Assert.IsTrue (fi.IsStatic, "#A9");
  463. Assert.AreEqual ("Bb", fi.Name, "#A10");
  464. Assert.AreEqual (E.Bb, fi.GetValue (null), "#A11");
  465. idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  466. "2", typeof (InstanceDescriptor)) as InstanceDescriptor;
  467. Assert.IsNotNull (idesc, "#B1");
  468. Assert.IsNotNull (idesc.Arguments, "#B2");
  469. Assert.AreEqual (0, idesc.Arguments.Count, "#B3");
  470. Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#B4");
  471. Assert.IsTrue (idesc.IsComplete, "#B5");
  472. fi = idesc.MemberInfo as FieldInfo;
  473. Assert.IsNotNull (fi, "#B6");
  474. Assert.AreEqual (typeof (E), fi.DeclaringType, "#B7");
  475. Assert.AreEqual (typeof (E), fi.FieldType, "#B8");
  476. Assert.IsTrue (fi.IsStatic, "#B9");
  477. Assert.AreEqual ("Cc", fi.Name, "#B10");
  478. Assert.AreEqual (E.Cc, fi.GetValue (null), "#B11");
  479. idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  480. 2, typeof (InstanceDescriptor)) as InstanceDescriptor;
  481. Assert.IsNotNull (idesc, "#C1");
  482. Assert.IsNotNull (idesc.Arguments, "#C2");
  483. Assert.AreEqual (0, idesc.Arguments.Count, "#C3");
  484. Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#C4");
  485. Assert.IsTrue (idesc.IsComplete, "#C5");
  486. fi = idesc.MemberInfo as FieldInfo;
  487. Assert.IsNotNull (fi, "#C6");
  488. Assert.AreEqual (typeof (E), fi.DeclaringType, "#C7");
  489. Assert.AreEqual (typeof (E), fi.FieldType, "#C8");
  490. Assert.IsTrue (fi.IsStatic, "#C9");
  491. Assert.AreEqual ("Cc", fi.Name, "#C10");
  492. Assert.AreEqual (E.Cc, fi.GetValue (null), "#C11");
  493. idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  494. (E) 2, typeof (InstanceDescriptor)) as InstanceDescriptor;
  495. Assert.IsNotNull (idesc, "#D1");
  496. Assert.IsNotNull (idesc.Arguments, "#D2");
  497. Assert.AreEqual (0, idesc.Arguments.Count, "#D3");
  498. Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#D4");
  499. Assert.IsTrue (idesc.IsComplete, "#D5");
  500. fi = idesc.MemberInfo as FieldInfo;
  501. Assert.IsNotNull (fi, "#D6");
  502. Assert.AreEqual (typeof (E), fi.DeclaringType, "#D7");
  503. Assert.AreEqual (typeof (E), fi.FieldType, "#D8");
  504. Assert.IsTrue (fi.IsStatic, "#D9");
  505. Assert.AreEqual ("Cc", fi.Name, "#D10");
  506. Assert.AreEqual (E.Cc, fi.GetValue (null), "#D11");
  507. try {
  508. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  509. null, typeof (InstanceDescriptor));
  510. Assert.Fail ("#E1");
  511. } catch (NotSupportedException ex) {
  512. // 'EnumConverter' is unable to convert '(null)'
  513. // to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'
  514. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#E2");
  515. Assert.IsNull (ex.InnerException, "#E3");
  516. Assert.IsNotNull (ex.Message, "#E4");
  517. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#E5");
  518. Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#E6");
  519. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (InstanceDescriptor).FullName + "'") != -1, "#E7");
  520. }
  521. try {
  522. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  523. "5", typeof (InstanceDescriptor));
  524. Assert.Fail ("#F1");
  525. } catch (ArgumentException ex) {
  526. // The value '5' is not a valid value for the enum 'E'
  527. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#F2");
  528. Assert.IsNull (ex.InnerException, "#F3");
  529. Assert.IsNotNull (ex.Message, "#F4");
  530. Assert.IsTrue (ex.Message.IndexOf ("'5'") != -1, "#F5");
  531. Assert.IsTrue (ex.Message.IndexOf ("'E'") != -1, "#F6");
  532. Assert.IsNull (ex.ParamName, "#F7");
  533. }
  534. try {
  535. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  536. "Cc", typeof (InstanceDescriptor));
  537. Assert.Fail ("#G1");
  538. } catch (FormatException ex) {
  539. // Input string was not in a correct format
  540. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#G2");
  541. Assert.IsNull (ex.InnerException, "#G3");
  542. Assert.IsNotNull (ex.Message, "#G4");
  543. }
  544. try {
  545. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  546. (E) 666, typeof (InstanceDescriptor));
  547. Assert.Fail ("#F1");
  548. } catch (ArgumentException ex) {
  549. // The value '666' is not a valid value for the enum 'E'
  550. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#F2");
  551. Assert.IsNull (ex.InnerException, "#F3");
  552. Assert.IsNotNull (ex.Message, "#F4");
  553. Assert.IsTrue (ex.Message.IndexOf ("'666'") != -1, "#F5");
  554. Assert.IsTrue (ex.Message.IndexOf ("'E'") != -1, "#F6");
  555. Assert.IsNull (ex.ParamName, "#F7");
  556. }
  557. }
  558. [Test]
  559. public void ConvertTo_InstanceDescriptor_Flags ()
  560. {
  561. InstanceDescriptor idesc;
  562. FieldInfo fi;
  563. MethodInfo mi;
  564. ParameterInfo [] parameters;
  565. object [] arguments;
  566. EnumConverter converter = new EnumConverter (typeof (E2));
  567. idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  568. E2.Bb, typeof (InstanceDescriptor)) as InstanceDescriptor;
  569. Assert.IsNotNull (idesc, "#A1");
  570. Assert.IsNotNull (idesc.Arguments, "#A2");
  571. Assert.AreEqual (0, idesc.Arguments.Count, "#A3");
  572. Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#A4");
  573. Assert.IsTrue (idesc.IsComplete, "#A5");
  574. fi = idesc.MemberInfo as FieldInfo;
  575. Assert.IsNotNull (fi, "#A6");
  576. Assert.AreEqual (typeof (E2), fi.DeclaringType, "#A7");
  577. Assert.AreEqual (typeof (E2), fi.FieldType, "#A8");
  578. Assert.IsTrue (fi.IsStatic, "#A9");
  579. Assert.AreEqual ("Bb", fi.Name, "#A10");
  580. Assert.AreEqual (E2.Bb, fi.GetValue (null), "#A11");
  581. idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  582. E.Bb, typeof (InstanceDescriptor)) as InstanceDescriptor;
  583. Assert.IsNotNull (idesc, "#B1");
  584. Assert.IsNotNull (idesc.Arguments, "#B2");
  585. Assert.AreEqual (0, idesc.Arguments.Count, "#B3");
  586. Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#B4");
  587. Assert.IsTrue (idesc.IsComplete, "#B5");
  588. fi = idesc.MemberInfo as FieldInfo;
  589. Assert.IsNotNull (fi, "#B6");
  590. Assert.AreEqual (typeof (E2), fi.DeclaringType, "#B7");
  591. Assert.AreEqual (typeof (E2), fi.FieldType, "#B8");
  592. Assert.IsTrue (fi.IsStatic, "#B9");
  593. Assert.AreEqual ("Aa", fi.Name, "#B10");
  594. Assert.AreEqual (E2.Aa, fi.GetValue (null), "#B11");
  595. idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  596. E2.Bb | E2.Dd, typeof (InstanceDescriptor))
  597. as InstanceDescriptor;
  598. Assert.IsNotNull (idesc, "#C1");
  599. Assert.IsNotNull (idesc.Arguments, "#C2");
  600. Assert.AreEqual (2, idesc.Arguments.Count, "#C3");
  601. Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#C4");
  602. arguments = (object []) idesc.Arguments;
  603. Assert.AreEqual (typeof (E2), arguments [0], "#C5");
  604. Assert.AreEqual (typeof (int), arguments [1].GetType (), "#C6");
  605. Assert.AreEqual (10, arguments [1], "#C7");
  606. Assert.IsTrue (idesc.IsComplete, "#C8");
  607. mi = idesc.MemberInfo as MethodInfo;
  608. Assert.IsNotNull (mi, "#C9");
  609. Assert.AreEqual ("ToObject", mi.Name, "#C10");
  610. Assert.AreEqual (typeof (Enum), mi.DeclaringType, "#C11");
  611. parameters = mi.GetParameters ();
  612. Assert.AreEqual (2, parameters.Length, "#C12");
  613. Assert.AreEqual (typeof (Type), parameters [0].ParameterType, "#C13");
  614. Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#C14");
  615. idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  616. "5", typeof (InstanceDescriptor)) as InstanceDescriptor;
  617. Assert.IsNotNull (idesc, "#D1");
  618. Assert.IsNotNull (idesc.Arguments, "#D2");
  619. Assert.AreEqual (2, idesc.Arguments.Count, "#D3");
  620. Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#D4");
  621. arguments = (object []) idesc.Arguments;
  622. Assert.AreEqual (typeof (E2), arguments [0], "#D5");
  623. Assert.AreEqual (typeof (int), arguments [1].GetType (), "#D6");
  624. Assert.AreEqual (5, arguments [1], "#D7");
  625. Assert.IsTrue (idesc.IsComplete, "#D8");
  626. mi = idesc.MemberInfo as MethodInfo;
  627. Assert.IsNotNull (mi, "#D9");
  628. Assert.AreEqual ("ToObject", mi.Name, "#D10");
  629. Assert.AreEqual (typeof (Enum), mi.DeclaringType, "#D11");
  630. parameters = mi.GetParameters ();
  631. Assert.AreEqual (2, parameters.Length, "#D12");
  632. Assert.AreEqual (typeof (Type), parameters [0].ParameterType, "#D13");
  633. Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#D14");
  634. idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
  635. 3, typeof (InstanceDescriptor)) as InstanceDescriptor;
  636. Assert.IsNotNull (idesc, "#E1");
  637. Assert.IsNotNull (idesc.Arguments, "#E2");
  638. Assert.AreEqual (2, idesc.Arguments.Count, "#E3");
  639. Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#E4");
  640. arguments = (object []) idesc.Arguments;
  641. Assert.AreEqual (typeof (E2), arguments [0], "#E5");
  642. Assert.AreEqual (typeof (int), arguments [1].GetType (), "#E6");
  643. Assert.AreEqual (3, arguments [1], "#E7");
  644. Assert.IsTrue (idesc.IsComplete, "#E8");
  645. mi = idesc.MemberInfo as MethodInfo;
  646. Assert.IsNotNull (mi, "#E9");
  647. Assert.AreEqual ("ToObject", mi.Name, "#E10");
  648. Assert.AreEqual (typeof (Enum), mi.DeclaringType, "#E11");
  649. parameters = mi.GetParameters ();
  650. Assert.AreEqual (2, parameters.Length, "#E12");
  651. Assert.AreEqual (typeof (Type), parameters [0].ParameterType, "#E13");
  652. Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#E14");
  653. try {
  654. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  655. null, typeof (InstanceDescriptor));
  656. Assert.Fail ("#F1");
  657. } catch (NotSupportedException ex) {
  658. // 'EnumConverter' is unable to convert '(null)'
  659. // to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'
  660. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#F2");
  661. Assert.IsNull (ex.InnerException, "#F3");
  662. Assert.IsNotNull (ex.Message, "#F4");
  663. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#F5");
  664. Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#F6");
  665. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (InstanceDescriptor).FullName + "'") != -1, "#F7");
  666. }
  667. try {
  668. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  669. "2,1", typeof (InstanceDescriptor));
  670. Assert.Fail ("#G1");
  671. } catch (FormatException ex) {
  672. // Input string was not in a correct format
  673. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#G2");
  674. Assert.IsNull (ex.InnerException, "#G3");
  675. Assert.IsNotNull (ex.Message, "#G4");
  676. }
  677. try {
  678. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  679. "Cc", typeof (InstanceDescriptor));
  680. Assert.Fail ("#H1");
  681. } catch (FormatException ex) {
  682. // Input string was not in a correct format
  683. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#H2");
  684. Assert.IsNull (ex.InnerException, "#H3");
  685. Assert.IsNotNull (ex.Message, "#H4");
  686. }
  687. try {
  688. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  689. (E2) 666 | (E2) 222, typeof (InstanceDescriptor));
  690. Assert.Fail ("#I1");
  691. } catch (NotSupportedException ex) {
  692. // 'EnumConverter' is unable to convert 'MonoTests.System.ComponentModel.EnumConverterTests+E2'
  693. // to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'
  694. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#I2");
  695. Assert.IsNull (ex.InnerException, "#I3");
  696. Assert.IsNotNull (ex.Message, "#I4");
  697. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#I5");
  698. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (E2).FullName + "'") != -1, "#I6");
  699. Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (InstanceDescriptor).FullName + "'") != -1, "#I7");
  700. }
  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");
  722. }
  723. [Test]
  724. public void ConvertTo_String ()
  725. {
  726. EnumConverter converter = new EnumConverter (typeof (E));
  727. Assert.AreEqual ("Bb", converter.ConvertTo (null,
  728. CultureInfo.InvariantCulture, E.Bb,
  729. typeof (string)), "#A1");
  730. Assert.AreEqual ("Dd", converter.ConvertTo (null,
  731. CultureInfo.InvariantCulture, 3,
  732. typeof (string)), "#A2");
  733. Assert.AreEqual (string.Empty, converter.ConvertTo (
  734. null, CultureInfo.InvariantCulture, null,
  735. typeof (string)), "#A3");
  736. Assert.AreEqual ("Cc", converter.ConvertTo (
  737. null, CultureInfo.InvariantCulture, (E) 2,
  738. typeof (string)), "#A4");
  739. Assert.AreEqual ("Cc", converter.ConvertTo (null,
  740. CultureInfo.InvariantCulture, E2.Bb,
  741. typeof (string)), "#A5");
  742. Assert.AreEqual ("Dd", converter.ConvertTo (null,
  743. CultureInfo.InvariantCulture, E.Bb | E.Dd,
  744. typeof (string)), "#A6");
  745. try {
  746. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  747. (E) 666, typeof (string));
  748. Assert.Fail ("#B1");
  749. } catch (ArgumentException ex) {
  750. // The value '666' is not a valid value for the enum 'E'
  751. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  752. Assert.IsNull (ex.InnerException, "#B3");
  753. Assert.IsNotNull (ex.Message, "#B4");
  754. Assert.IsTrue (ex.Message.IndexOf ("'666'") != -1, "#B5");
  755. Assert.IsTrue (ex.Message.IndexOf ("'E'") != -1, "#B6");
  756. Assert.IsNull (ex.ParamName, "#B7");
  757. }
  758. try {
  759. converter.ConvertTo (null, CultureInfo.InvariantCulture,
  760. "Cc", typeof (string));
  761. Assert.Fail ("#C1");
  762. } catch (FormatException ex) {
  763. // Input string was not in a correct format
  764. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#C2");
  765. Assert.IsNull (ex.InnerException, "#C3");
  766. Assert.IsNotNull (ex.Message, "#C4");
  767. }
  768. converter = new EnumConverter (typeof (E2));
  769. Assert.AreEqual ("Bb", converter.ConvertTo (null,
  770. CultureInfo.InvariantCulture, E2.Bb,
  771. typeof (string)), "#B1");
  772. Assert.AreEqual ("Aa, Bb", converter.ConvertTo (null,
  773. CultureInfo.InvariantCulture, 3,
  774. typeof (string)), "#B2");
  775. Assert.AreEqual (string.Empty, converter.ConvertTo (
  776. null, CultureInfo.InvariantCulture, null,
  777. typeof (string)), "#B3");
  778. Assert.AreEqual ("Bb", converter.ConvertTo (
  779. null, CultureInfo.InvariantCulture, (E2) 2,
  780. typeof (string)), "#B4");
  781. Assert.AreEqual ("Aa, Bb", converter.ConvertTo (
  782. null, CultureInfo.InvariantCulture, E.Dd,
  783. typeof (string)), "#B5");
  784. Assert.AreEqual ("Bb, Dd", converter.ConvertTo (null,
  785. CultureInfo.InvariantCulture, E2.Bb | E2.Dd,
  786. typeof (string)), "#B6");
  787. }
  788. enum E
  789. {
  790. Aa = 0,
  791. Bb = 1,
  792. Cc = 2,
  793. Dd = 3,
  794. }
  795. [Flags]
  796. enum E2
  797. {
  798. Aa = 1,
  799. Bb = 2,
  800. Cc = 4,
  801. Dd = 8,
  802. }
  803. enum F : byte
  804. {
  805. Bb = 1,
  806. Dd = 3
  807. }
  808. [Flags]
  809. enum F2 : byte
  810. {
  811. Bb = 2,
  812. Dd = 8
  813. }
  814. }
  815. }