SingleConverterTests.cs 6.8 KB

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