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