UInt16ConverterTests.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // System.ComponentModel.UInt16Converter 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 UInt16ConverterTests
  18. {
  19. private UInt16Converter converter;
  20. [SetUp]
  21. public void SetUp ()
  22. {
  23. converter = new UInt16Converter ();
  24. }
  25. [Test]
  26. public void CanConvertFrom ()
  27. {
  28. Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
  29. Assert.IsFalse (converter.CanConvertFrom (typeof (short)), "#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 (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
  43. Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
  44. Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
  45. Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
  46. Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
  47. }
  48. [Test]
  49. public void ConvertFrom_MaxValue ()
  50. {
  51. Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffff"), "#1");
  52. Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFF"), "#2");
  53. Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffff"), "#3");
  54. Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFF"), "#4");
  55. Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffff"), "#5");
  56. Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFF"), "#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_Int16 ()
  67. {
  68. converter.ConvertFrom ((short) 10);
  69. }
  70. [Test]
  71. [ExpectedException (typeof (NotSupportedException))]
  72. public void ConvertFrom_Int32 ()
  73. {
  74. converter.ConvertFrom (10);
  75. }
  76. [Test]
  77. public void ConvertTo_MinValue ()
  78. {
  79. Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.InvariantCulture),
  80. converter.ConvertTo (null, CultureInfo.InvariantCulture, ushort.MinValue,
  81. typeof (string)), "#1");
  82. Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.CurrentCulture),
  83. converter.ConvertTo (null, CultureInfo.CurrentCulture, ushort.MinValue,
  84. typeof (string)), "#2");
  85. Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.CurrentCulture),
  86. converter.ConvertTo (ushort.MinValue, typeof (string)), "#3");
  87. }
  88. [Test]
  89. public void ConvertTo_MaxValue ()
  90. {
  91. Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.InvariantCulture),
  92. converter.ConvertTo (null, CultureInfo.InvariantCulture, ushort.MaxValue,
  93. typeof (string)), "#1");
  94. Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.CurrentCulture),
  95. converter.ConvertTo (null, CultureInfo.CurrentCulture, ushort.MaxValue,
  96. typeof (string)), "#2");
  97. Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.CurrentCulture),
  98. converter.ConvertTo (ushort.MaxValue, typeof (string)), "#3");
  99. }
  100. [Test]
  101. public void ConvertToString ()
  102. {
  103. CultureInfo culture = new MyCultureInfo ();
  104. NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
  105. Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
  106. }
  107. [Test]
  108. public void ConvertFromString_Invalid1 ()
  109. {
  110. try {
  111. converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
  112. Assert.Fail ("#1");
  113. } catch (Exception ex) {
  114. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  115. Assert.IsNotNull (ex.InnerException, "#3");
  116. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  117. }
  118. }
  119. [Test]
  120. public void ConvertFromString_Invalid2 ()
  121. {
  122. try {
  123. converter.ConvertFromString (null, CultureInfo.InvariantCulture,
  124. double.MaxValue.ToString(CultureInfo.InvariantCulture));
  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_Invalid3 ()
  134. {
  135. try {
  136. converter.ConvertFromString ("*1");
  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 ConvertFromString_Invalid4 ()
  146. {
  147. try {
  148. converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
  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_InvalidString1 ()
  158. {
  159. try {
  160. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
  161. Assert.Fail ("#1");
  162. } catch (Exception ex) {
  163. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  164. Assert.IsNotNull (ex.InnerException, "#3");
  165. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  166. }
  167. }
  168. [Test]
  169. public void ConvertFrom_InvalidString2 ()
  170. {
  171. try {
  172. converter.ConvertFrom (null, CultureInfo.InvariantCulture,
  173. double.MaxValue.ToString (CultureInfo.InvariantCulture));
  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_InvalidString3 ()
  183. {
  184. try {
  185. converter.ConvertFrom ("*1");
  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. [Test]
  194. public void ConvertFrom_InvalidString4 ()
  195. {
  196. try {
  197. converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
  198. Assert.Fail ("#1");
  199. } catch (Exception ex) {
  200. Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
  201. Assert.IsNotNull (ex.InnerException, "#3");
  202. Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
  203. }
  204. }
  205. [Serializable]
  206. private sealed class MyCultureInfo : CultureInfo
  207. {
  208. internal MyCultureInfo ()
  209. : base ("en-US")
  210. {
  211. }
  212. public override object GetFormat (Type formatType)
  213. {
  214. if (formatType == typeof (NumberFormatInfo)) {
  215. NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
  216. nfi.NegativeSign = "myNegativeSign";
  217. return NumberFormatInfo.ReadOnly (nfi);
  218. } else {
  219. return base.GetFormat (formatType);
  220. }
  221. }
  222. #if NET_2_0
  223. // adding this override in 1.x shows different result in .NET (it is ignored).
  224. // Some compatibility kids might want to fix this issue.
  225. public override NumberFormatInfo NumberFormat {
  226. get {
  227. NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
  228. nfi.NegativeSign = "myNegativeSign";
  229. return nfi;
  230. }
  231. set { throw new NotSupportedException (); }
  232. }
  233. #endif
  234. }
  235. }
  236. }