Int32ConverterTests.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // System.ComponentModel.Int32Converter 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 Int32ConverterTests
  18. {
  19. private Int32Converter converter;
  20. [SetUp]
  21. public void SetUp ()
  22. {
  23. converter = new Int32Converter ();
  24. }
  25. [Test]
  26. public void CanConvertFrom ()
  27. {
  28. Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
  29. Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#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 (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#80000000"), "#1");
  43. Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x80000000"), "#2");
  44. Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X80000000"), "#3");
  45. Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x80000000"), "#4");
  46. Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X80000000"), "#5");
  47. }
  48. [Test]
  49. public void ConvertFrom_MaxValue ()
  50. {
  51. Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fffffff"), "#1");
  52. Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFFFFFF"), "#2");
  53. Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fffffff"), "#3");
  54. Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFFFFFF"), "#4");
  55. Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fffffff"), "#5");
  56. Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFFFFFF"), "#6");
  57. }
  58. [Test]
  59. [ExpectedException (typeof (NotSupportedException))]
  60. public void ConvertFrom_Object ()
  61. {
  62. converter.ConvertFrom (new object ());
  63. }
  64. [Test]
  65. [ExpectedException (typeof (NotSupportedException))]
  66. public void ConvertFrom_Int32 ()
  67. {
  68. converter.ConvertFrom (int.MaxValue);
  69. }
  70. [Test]
  71. public void ConvertTo_MinValue ()
  72. {
  73. Assert.AreEqual (int.MinValue.ToString (CultureInfo.InvariantCulture),
  74. converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MinValue,
  75. typeof (string)), "#1");
  76. Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
  77. converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MinValue,
  78. typeof (string)), "#2");
  79. Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
  80. converter.ConvertTo (int.MinValue, typeof (string)), "#3");
  81. }
  82. [Test]
  83. public void ConvertTo_MaxValue ()
  84. {
  85. Assert.AreEqual (int.MaxValue.ToString (CultureInfo.InvariantCulture),
  86. converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MaxValue,
  87. typeof (string)), "#1");
  88. Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
  89. converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MaxValue,
  90. typeof (string)), "#2");
  91. Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
  92. converter.ConvertTo (int.MaxValue, typeof (string)), "#3");
  93. }
  94. [Test]
  95. public void ConvertToString ()
  96. {
  97. CultureInfo culture = new MyCultureInfo ();
  98. NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
  99. Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
  100. }
  101. [Test]
  102. public void ConvertFromString ()
  103. {
  104. CultureInfo culture = new MyCultureInfo ();
  105. NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
  106. Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
  107. }
  108. [Test]
  109. public void ConvertFromString_Invalid1 ()
  110. {
  111. try {
  112. converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
  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_Invalid2 ()
  122. {
  123. try {
  124. converter.ConvertFromString (null, CultureInfo.InvariantCulture,
  125. double.MaxValue.ToString(CultureInfo.InvariantCulture));
  126. Assert.Fail ("#1");
  127. } catch (Exception ex) {
  128. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  129. Assert.IsNotNull (ex.InnerException, "#3");
  130. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  131. }
  132. }
  133. [Test]
  134. public void ConvertFromString_Invalid3 ()
  135. {
  136. try {
  137. converter.ConvertFromString ("*1");
  138. Assert.Fail ("#1");
  139. } catch (Exception ex) {
  140. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  141. Assert.IsNotNull (ex.InnerException, "#3");
  142. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  143. }
  144. }
  145. [Test]
  146. public void ConvertFromString_Invalid4 ()
  147. {
  148. try {
  149. converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
  150. Assert.Fail ("#1");
  151. } catch (Exception ex) {
  152. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  153. Assert.IsNotNull (ex.InnerException, "#3");
  154. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  155. }
  156. }
  157. [Test]
  158. public void ConvertFrom_InvalidString1 ()
  159. {
  160. try {
  161. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
  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_InvalidString2 ()
  171. {
  172. try {
  173. converter.ConvertFrom (null, CultureInfo.InvariantCulture,
  174. double.MaxValue.ToString (CultureInfo.InvariantCulture));
  175. Assert.Fail ("#1");
  176. } catch (Exception ex) {
  177. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  178. Assert.IsNotNull (ex.InnerException, "#3");
  179. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  180. }
  181. }
  182. [Test]
  183. public void ConvertFrom_InvalidString3 ()
  184. {
  185. try {
  186. converter.ConvertFrom ("*1");
  187. Assert.Fail ("#1");
  188. } catch (Exception ex) {
  189. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  190. Assert.IsNotNull (ex.InnerException, "#3");
  191. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  192. }
  193. }
  194. [Test]
  195. public void ConvertFrom_InvalidString4 ()
  196. {
  197. try {
  198. converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
  199. Assert.Fail ("#1");
  200. } catch (Exception ex) {
  201. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  202. Assert.IsNotNull (ex.InnerException, "#3");
  203. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  204. }
  205. }
  206. [Serializable]
  207. private sealed class MyCultureInfo : CultureInfo
  208. {
  209. internal MyCultureInfo ()
  210. : base ("en-US")
  211. {
  212. }
  213. public override object GetFormat (Type formatType)
  214. {
  215. if (formatType == typeof (NumberFormatInfo)) {
  216. NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
  217. nfi.NegativeSign = "myNegativeSign";
  218. return NumberFormatInfo.ReadOnly (nfi);
  219. } else {
  220. return base.GetFormat (formatType);
  221. }
  222. }
  223. }
  224. }
  225. }