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