UInt32ConverterTests.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // System.ComponentModel.UInt32Converter test cases
  3. //
  4. // Authors:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (c) 2005 Novell, Inc. (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.ComponentModel;
  11. using System.ComponentModel.Design.Serialization;
  12. using System.Globalization;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.ComponentModel
  15. {
  16. [TestFixture]
  17. public class UInt32ConverterTests
  18. {
  19. private UInt32Converter converter;
  20. [SetUp]
  21. public void SetUp ()
  22. {
  23. converter = new UInt32Converter ();
  24. }
  25. [Test]
  26. public void CanConvertFrom ()
  27. {
  28. Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
  29. Assert.IsFalse (converter.CanConvertFrom (typeof (uint)), "#2");
  30. Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#3");
  31. Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#4");
  32. }
  33. [Test]
  34. public void CanConvertTo ()
  35. {
  36. Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1");
  37. Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#2");
  38. }
  39. [Test]
  40. public void ConvertFrom_MinValue ()
  41. {
  42. Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
  43. Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
  44. Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
  45. Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
  46. Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
  47. }
  48. [Test]
  49. public void ConvertFrom_MaxValue ()
  50. {
  51. Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffffffff"), "#1");
  52. Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFFFFFF"), "#2");
  53. Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffffffff"), "#3");
  54. Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFFFFFF"), "#4");
  55. Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffffffff"), "#5");
  56. Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFFFFFF"), "#6");
  57. }
  58. [Test]
  59. [ExpectedException (typeof (NotSupportedException))]
  60. public void ConvertFrom_Object ()
  61. {
  62. converter.ConvertFrom (new object ());
  63. }
  64. [Test]
  65. public void ConvertTo_MinValue ()
  66. {
  67. Assert.AreEqual (uint.MinValue.ToString (CultureInfo.InvariantCulture),
  68. converter.ConvertTo (null, CultureInfo.InvariantCulture, uint.MinValue,
  69. typeof (string)), "#1");
  70. Assert.AreEqual (uint.MinValue.ToString (CultureInfo.CurrentCulture),
  71. converter.ConvertTo (null, CultureInfo.CurrentCulture, uint.MinValue,
  72. typeof (string)), "#2");
  73. Assert.AreEqual (uint.MinValue.ToString (CultureInfo.CurrentCulture),
  74. converter.ConvertTo (uint.MinValue, typeof (string)), "#3");
  75. }
  76. [Test]
  77. public void ConvertTo_MaxValue ()
  78. {
  79. Assert.AreEqual (uint.MaxValue.ToString (CultureInfo.InvariantCulture),
  80. converter.ConvertTo (null, CultureInfo.InvariantCulture, uint.MaxValue,
  81. typeof (string)), "#1");
  82. Assert.AreEqual (uint.MaxValue.ToString (CultureInfo.CurrentCulture),
  83. converter.ConvertTo (null, CultureInfo.CurrentCulture, uint.MaxValue,
  84. typeof (string)), "#2");
  85. Assert.AreEqual (uint.MaxValue.ToString (CultureInfo.CurrentCulture),
  86. converter.ConvertTo (uint.MaxValue, typeof (string)), "#3");
  87. }
  88. [Test]
  89. public void ConvertToString ()
  90. {
  91. CultureInfo culture = new MyCultureInfo ();
  92. NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
  93. Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
  94. }
  95. [Test]
  96. public void ConvertFromString_Invalid1 ()
  97. {
  98. try {
  99. converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
  100. Assert.Fail ("#1");
  101. } catch (Exception ex) {
  102. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  103. Assert.IsNotNull (ex.InnerException, "#3");
  104. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  105. }
  106. }
  107. [Test]
  108. public void ConvertFromString_Invalid2 ()
  109. {
  110. try {
  111. converter.ConvertFromString (null, CultureInfo.InvariantCulture,
  112. double.MaxValue.ToString(CultureInfo.InvariantCulture));
  113. Assert.Fail ("#1");
  114. } catch (Exception ex) {
  115. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  116. Assert.IsNotNull (ex.InnerException, "#3");
  117. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  118. }
  119. }
  120. [Test]
  121. public void ConvertFromString_Invalid3 ()
  122. {
  123. try {
  124. converter.ConvertFromString ("*1");
  125. Assert.Fail ("#1");
  126. } catch (Exception ex) {
  127. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  128. Assert.IsNotNull (ex.InnerException, "#3");
  129. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  130. }
  131. }
  132. [Test]
  133. public void ConvertFromString_Invalid4 ()
  134. {
  135. try {
  136. converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
  137. Assert.Fail ("#1");
  138. } catch (Exception ex) {
  139. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  140. Assert.IsNotNull (ex.InnerException, "#3");
  141. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  142. }
  143. }
  144. [Test]
  145. public void ConvertFrom_InvalidString1 ()
  146. {
  147. try {
  148. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
  149. Assert.Fail ("#1");
  150. } catch (Exception ex) {
  151. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  152. Assert.IsNotNull (ex.InnerException, "#3");
  153. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  154. }
  155. }
  156. [Test]
  157. public void ConvertFrom_InvalidString2 ()
  158. {
  159. try {
  160. converter.ConvertFrom (null, CultureInfo.InvariantCulture,
  161. double.MaxValue.ToString (CultureInfo.InvariantCulture));
  162. Assert.Fail ("#1");
  163. } catch (Exception ex) {
  164. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  165. Assert.IsNotNull (ex.InnerException, "#3");
  166. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  167. }
  168. }
  169. [Test]
  170. public void ConvertFrom_InvalidString3 ()
  171. {
  172. try {
  173. converter.ConvertFrom ("*1");
  174. Assert.Fail ("#1");
  175. } catch (Exception ex) {
  176. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  177. Assert.IsNotNull (ex.InnerException, "#3");
  178. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  179. }
  180. }
  181. [Test]
  182. public void ConvertFrom_InvalidString4 ()
  183. {
  184. try {
  185. converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
  186. Assert.Fail ("#1");
  187. } catch (Exception ex) {
  188. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  189. Assert.IsNotNull (ex.InnerException, "#3");
  190. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  191. }
  192. }
  193. [Serializable]
  194. private sealed class MyCultureInfo : CultureInfo
  195. {
  196. internal MyCultureInfo ()
  197. : base ("en-US")
  198. {
  199. }
  200. public override object GetFormat (Type formatType)
  201. {
  202. if (formatType == typeof (NumberFormatInfo)) {
  203. NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
  204. nfi.NegativeSign = "myNegativeSign";
  205. return NumberFormatInfo.ReadOnly (nfi);
  206. } else {
  207. return base.GetFormat (formatType);
  208. }
  209. }
  210. #if NET_2_0
  211. // adding this override in 1.x shows different result in .NET (it is ignored).
  212. // Some compatibility kids might want to fix this issue.
  213. public override NumberFormatInfo NumberFormat {
  214. get {
  215. NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
  216. nfi.NegativeSign = "myNegativeSign";
  217. return nfi;
  218. }
  219. set { throw new NotSupportedException (); }
  220. }
  221. #endif
  222. }
  223. }
  224. }