Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / System / Test / System.ComponentModel / UInt32ConverterTests.cs
1 //
2 // System.ComponentModel.UInt32Converter 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 UInt32ConverterTests
21         {
22                 private UInt32Converter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new UInt32Converter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (uint)), "#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 (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
51                         Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
52                         Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
53                         Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
54                         Assert.AreEqual (uint.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
55                 }
56
57                 [Test]
58                 public void ConvertFrom_MaxValue ()
59                 {
60                         Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffffffff"), "#1");
61                         Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFFFFFF"), "#2");
62                         Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffffffff"), "#3");
63                         Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFFFFFF"), "#4");
64                         Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffffffff"), "#5");
65                         Assert.AreEqual (uint.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFFFFFF"), "#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                 public void ConvertTo_MinValue ()
77                 {
78                         Assert.AreEqual (uint.MinValue.ToString (CultureInfo.InvariantCulture),
79                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, uint.MinValue,
80                                 typeof (string)), "#1");
81                         Assert.AreEqual (uint.MinValue.ToString (CultureInfo.CurrentCulture),
82                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, uint.MinValue,
83                                 typeof (string)), "#2");
84                         Assert.AreEqual (uint.MinValue.ToString (CultureInfo.CurrentCulture),
85                                 converter.ConvertTo (uint.MinValue, typeof (string)), "#3");
86                 }
87
88                 [Test]
89                 public void ConvertTo_MaxValue ()
90                 {
91                         Assert.AreEqual (uint.MaxValue.ToString (CultureInfo.InvariantCulture),
92                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, uint.MaxValue,
93                                 typeof (string)), "#1");
94                         Assert.AreEqual (uint.MaxValue.ToString (CultureInfo.CurrentCulture),
95                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, uint.MaxValue,
96                                 typeof (string)), "#2");
97                         Assert.AreEqual (uint.MaxValue.ToString (CultureInfo.CurrentCulture),
98                                 converter.ConvertTo (uint.MaxValue, typeof (string)), "#3");
99                 }
100
101                 [Test]
102                 public void ConvertToString ()
103                 {
104                         CultureInfo culture = new MyCultureInfo ();
105                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
106
107                         Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
108                 }
109
110                 [Test]
111                 public void ConvertFromString_Invalid1 ()
112                 {
113                         try {
114                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
115                                 Assert.Fail ("#1");
116                         } catch (Exception ex) {
117                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
118                                 Assert.IsNotNull (ex.InnerException, "#3");
119                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
120                         }
121                 }
122
123                 [Test]
124                 public void ConvertFromString_Invalid2 ()
125                 {
126                         try {
127                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
128                                         double.MaxValue.ToString(CultureInfo.InvariantCulture));
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_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
150                 [Test]
151                 public void ConvertFromString_Invalid4 ()
152                 {
153                         try {
154                                 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
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 ConvertFrom_InvalidString1 ()
165                 {
166                         try {
167                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
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_InvalidString2 ()
178                 {
179                         try {
180                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
181                                         double.MaxValue.ToString (CultureInfo.InvariantCulture));
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_InvalidString3 ()
192                 {
193                         try {
194                                 converter.ConvertFrom ("*1");
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_InvalidString4 ()
205                 {
206                         try {
207                                 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
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                 [Serializable]
217                 private sealed class MyCultureInfo : CultureInfo
218                 {
219                         internal MyCultureInfo ()
220                                 : base ("en-US")
221                         {
222                         }
223
224                         public override object GetFormat (Type formatType)
225                         {
226                                 if (formatType == typeof (NumberFormatInfo)) {
227                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
228
229                                         nfi.NegativeSign = "myNegativeSign";
230                                         return NumberFormatInfo.ReadOnly (nfi);
231                                 } else {
232                                         return base.GetFormat (formatType);
233                                 }
234                         }
235 #if NET_2_0
236 // adding this override in 1.x shows different result in .NET (it is ignored).
237 // Some compatibility kids might want to fix this issue.
238                         public override NumberFormatInfo NumberFormat {
239                                 get {
240                                         NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
241                                         nfi.NegativeSign = "myNegativeSign";
242                                         return nfi;
243                                 }
244                                 set { throw new NotSupportedException (); }
245                         }
246 #endif
247                 }
248         }
249 }