SByteConverterTests.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // System.ComponentModel.SByteConverter 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 SByteConverterTests
  18. {
  19. private SByteConverter converter;
  20. [SetUp]
  21. public void SetUp ()
  22. {
  23. converter = new SByteConverter ();
  24. }
  25. [Test]
  26. public void CanConvertFrom ()
  27. {
  28. Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
  29. Assert.IsFalse (converter.CanConvertFrom (typeof (sbyte)), "#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_String ()
  41. {
  42. Assert.AreEqual ((sbyte) 10, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "10"), "#1");
  43. Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#10"), "#2");
  44. Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x10"), "#3");
  45. Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X10"), "#4");
  46. Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x10"), "#5");
  47. Assert.AreEqual ((sbyte) 16, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X10"), "#6");
  48. }
  49. [Test]
  50. [ExpectedException (typeof (NotSupportedException))]
  51. public void ConvertFrom_Object ()
  52. {
  53. converter.ConvertFrom (new object ());
  54. }
  55. [Test]
  56. [ExpectedException (typeof (NotSupportedException))]
  57. public void ConvertFrom_Byte ()
  58. {
  59. converter.ConvertFrom (sbyte.MaxValue);
  60. }
  61. [Test]
  62. [ExpectedException (typeof (NotSupportedException))]
  63. public void ConvertFrom_Int16 ()
  64. {
  65. converter.ConvertFrom ((short) 10);
  66. }
  67. [Test]
  68. [ExpectedException (typeof (NotSupportedException))]
  69. public void ConvertFrom_Int32 ()
  70. {
  71. converter.ConvertFrom (10);
  72. }
  73. [Test]
  74. public void ConvertTo_MinValue ()
  75. {
  76. Assert.AreEqual (sbyte.MinValue.ToString (CultureInfo.InvariantCulture),
  77. converter.ConvertTo (null, CultureInfo.InvariantCulture, sbyte.MinValue,
  78. typeof (string)), "#1");
  79. Assert.AreEqual (sbyte.MinValue.ToString (CultureInfo.CurrentCulture),
  80. converter.ConvertTo (null, CultureInfo.CurrentCulture, sbyte.MinValue,
  81. typeof (string)), "#2");
  82. Assert.AreEqual (sbyte.MinValue.ToString (CultureInfo.CurrentCulture),
  83. converter.ConvertTo (sbyte.MinValue, typeof (string)), "#3");
  84. }
  85. [Test]
  86. public void ConvertTo_MaxValue ()
  87. {
  88. Assert.AreEqual (sbyte.MaxValue.ToString (CultureInfo.InvariantCulture),
  89. converter.ConvertTo (null, CultureInfo.InvariantCulture, sbyte.MaxValue,
  90. typeof (string)), "#1");
  91. Assert.AreEqual (sbyte.MaxValue.ToString (CultureInfo.CurrentCulture),
  92. converter.ConvertTo (null, CultureInfo.CurrentCulture, sbyte.MaxValue,
  93. typeof (string)), "#2");
  94. Assert.AreEqual (sbyte.MaxValue.ToString (CultureInfo.CurrentCulture),
  95. converter.ConvertTo (sbyte.MaxValue, typeof (string)), "#3");
  96. }
  97. [Test]
  98. public void ConvertToString ()
  99. {
  100. CultureInfo culture = new MyCultureInfo ();
  101. NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
  102. Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (sbyte) -5), "#1");
  103. Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (short) -5), "#2");
  104. }
  105. [Test]
  106. public void ConvertFromString ()
  107. {
  108. CultureInfo culture = new MyCultureInfo ();
  109. NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
  110. Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
  111. }
  112. [Test]
  113. public void ConvertFromString_Invalid1 ()
  114. {
  115. try {
  116. converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
  117. Assert.Fail ("#1");
  118. } catch (Exception ex) {
  119. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  120. Assert.IsNotNull (ex.InnerException, "#3");
  121. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  122. }
  123. }
  124. [Test]
  125. public void ConvertFromString_Invalid2 ()
  126. {
  127. try {
  128. converter.ConvertFromString (null, CultureInfo.InvariantCulture,
  129. double.MaxValue.ToString(CultureInfo.InvariantCulture));
  130. Assert.Fail ("#1");
  131. } catch (Exception ex) {
  132. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  133. Assert.IsNotNull (ex.InnerException, "#3");
  134. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  135. }
  136. }
  137. [Test]
  138. public void ConvertFromString_Invalid3 ()
  139. {
  140. try {
  141. converter.ConvertFromString ("*1");
  142. Assert.Fail ("#1");
  143. } catch (Exception ex) {
  144. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  145. Assert.IsNotNull (ex.InnerException, "#3");
  146. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  147. }
  148. }
  149. [Test]
  150. public void ConvertFromString_Invalid4 ()
  151. {
  152. try {
  153. converter.ConvertFromString ("256");
  154. Assert.Fail ("#1");
  155. } catch (Exception ex) {
  156. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  157. Assert.IsNotNull (ex.InnerException, "#3");
  158. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  159. }
  160. }
  161. [Test]
  162. public void ConvertFrom_InvalidString1 ()
  163. {
  164. try {
  165. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
  166. Assert.Fail ("#1");
  167. } catch (Exception ex) {
  168. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  169. Assert.IsNotNull (ex.InnerException, "#3");
  170. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  171. }
  172. }
  173. [Test]
  174. public void ConvertFrom_InvalidString2 ()
  175. {
  176. try {
  177. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "256");
  178. Assert.Fail ("#1");
  179. } catch (Exception ex) {
  180. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  181. Assert.IsNotNull (ex.InnerException, "#3");
  182. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  183. }
  184. }
  185. [Test]
  186. public void ConvertFrom_InvalidString3 ()
  187. {
  188. try {
  189. converter.ConvertFrom ("*1");
  190. Assert.Fail ("#1");
  191. } catch (Exception ex) {
  192. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  193. Assert.IsNotNull (ex.InnerException, "#3");
  194. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  195. }
  196. }
  197. [Test]
  198. public void ConvertFrom_InvalidString4 ()
  199. {
  200. try {
  201. converter.ConvertFrom ("256");
  202. Assert.Fail ("#1");
  203. } catch (Exception ex) {
  204. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  205. Assert.IsNotNull (ex.InnerException, "#3");
  206. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  207. }
  208. }
  209. [Test]
  210. public void ConvertFrom_InvalidString5 ()
  211. {
  212. try {
  213. converter.ConvertFrom ("#0b10");
  214. Assert.Fail ("#1");
  215. } catch (Exception ex) {
  216. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  217. Assert.IsNotNull (ex.InnerException, "#3");
  218. Assert.AreEqual (typeof (OverflowException), ex.InnerException.GetType (), "#3");
  219. }
  220. }
  221. [Serializable]
  222. private sealed class MyCultureInfo : CultureInfo
  223. {
  224. internal MyCultureInfo ()
  225. : base ("en-US")
  226. {
  227. }
  228. public override object GetFormat (Type formatType)
  229. {
  230. if (formatType == typeof (NumberFormatInfo)) {
  231. NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
  232. nfi.NegativeSign = "myNegativeSign";
  233. return NumberFormatInfo.ReadOnly (nfi);
  234. } else {
  235. return base.GetFormat (formatType);
  236. }
  237. }
  238. #if NET_2_0
  239. // adding this override in 1.x shows different result in .NET (it is ignored).
  240. // Some compatibility kids might want to fix this issue.
  241. public override NumberFormatInfo NumberFormat {
  242. get {
  243. NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
  244. nfi.NegativeSign = "myNegativeSign";
  245. return nfi;
  246. }
  247. set { throw new NotSupportedException (); }
  248. }
  249. #endif
  250. }
  251. }
  252. }