merged Sys.Web.Services 2.0 support in my branch:
[mono.git] / mcs / class / System / Test / System.ComponentModel / Int32ConverterTests.cs
1 //
2 // System.ComponentModel.Int32Converter 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 Int32ConverterTests
21         {
22                 private Int32Converter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new Int32Converter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#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 (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#80000000"), "#1");
50                         Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x80000000"), "#2");
51                         Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X80000000"), "#3");
52                         Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x80000000"), "#4");
53                         Assert.AreEqual (int.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X80000000"), "#5");
54                 }
55
56                 [Test]
57                 public void ConvertFrom_MaxValue ()
58                 {
59                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fffffff"), "#1");
60                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFFFFFF"), "#2");
61                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fffffff"), "#3");
62                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFFFFFF"), "#4");
63                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fffffff"), "#5");
64                         Assert.AreEqual (int.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFFFFFF"), "#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_Int32 ()
77                 {
78                         converter.ConvertFrom (int.MaxValue);
79                 }
80
81                 [Test]
82                 public void ConvertTo_MinValue ()
83                 {
84                         Assert.AreEqual (int.MinValue.ToString (CultureInfo.InvariantCulture),
85                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MinValue,
86                                 typeof (string)), "#1");
87                         Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
88                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MinValue,
89                                 typeof (string)), "#2");
90                         Assert.AreEqual (int.MinValue.ToString (CultureInfo.CurrentCulture),
91                                 converter.ConvertTo (int.MinValue, typeof (string)), "#3");
92                 }
93
94                 [Test]
95                 public void ConvertTo_MaxValue ()
96                 {
97                         Assert.AreEqual (int.MaxValue.ToString (CultureInfo.InvariantCulture),
98                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, int.MaxValue,
99                                 typeof (string)), "#1");
100                         Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
101                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, int.MaxValue,
102                                 typeof (string)), "#2");
103                         Assert.AreEqual (int.MaxValue.ToString (CultureInfo.CurrentCulture),
104                                 converter.ConvertTo (int.MaxValue, typeof (string)), "#3");
105                 }
106
107                 [Test]
108                 public void ConvertToString ()
109                 {
110                         CultureInfo culture = new MyCultureInfo ();
111                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
112
113                         Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, -5));
114                 }
115
116                 [Test]
117                 public void ConvertFromString ()
118                 {
119                         CultureInfo culture = new MyCultureInfo ();
120                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
121
122                         Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
123                 }
124
125                 [Test]
126                 public void ConvertFromString_Invalid1 ()
127                 {
128                         try {
129                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
130                                 Assert.Fail ("#1");
131                         } catch (Exception ex) {
132                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
133                                 Assert.IsNotNull (ex.InnerException, "#3");
134                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
135                         }
136                 }
137
138                 [Test]
139                 public void ConvertFromString_Invalid2 ()
140                 {
141                         try {
142                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
143                                         double.MaxValue.ToString(CultureInfo.InvariantCulture));
144                                 Assert.Fail ("#1");
145                         } catch (Exception ex) {
146                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
147                                 Assert.IsNotNull (ex.InnerException, "#3");
148                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
149                         }
150                 }
151
152                 [Test]
153                 public void ConvertFromString_Invalid3 ()
154                 {
155                         try {
156                                 converter.ConvertFromString ("*1");
157                                 Assert.Fail ("#1");
158                         } catch (Exception ex) {
159                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
160                                 Assert.IsNotNull (ex.InnerException, "#3");
161                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
162                         }
163                 }
164
165                 [Test]
166                 public void ConvertFromString_Invalid4 ()
167                 {
168                         try {
169                                 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
170                                 Assert.Fail ("#1");
171                         } catch (Exception ex) {
172                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
173                                 Assert.IsNotNull (ex.InnerException, "#3");
174                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
175                         }
176                 }
177
178                 [Test]
179                 public void ConvertFrom_InvalidString1 ()
180                 {
181                         try {
182                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
183                                 Assert.Fail ("#1");
184                         } catch (Exception ex) {
185                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
186                                 Assert.IsNotNull (ex.InnerException, "#3");
187                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
188                         }
189                 }
190
191                 [Test]
192                 public void ConvertFrom_InvalidString2 ()
193                 {
194                         try {
195                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
196                                         double.MaxValue.ToString (CultureInfo.InvariantCulture));
197                                 Assert.Fail ("#1");
198                         } catch (Exception ex) {
199                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
200                                 Assert.IsNotNull (ex.InnerException, "#3");
201                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
202                         }
203                 }
204
205                 [Test]
206                 public void ConvertFrom_InvalidString3 ()
207                 {
208                         try {
209                                 converter.ConvertFrom ("*1");
210                                 Assert.Fail ("#1");
211                         } catch (Exception ex) {
212                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
213                                 Assert.IsNotNull (ex.InnerException, "#3");
214                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
215                         }
216                 }
217
218                 [Test]
219                 public void ConvertFrom_InvalidString4 ()
220                 {
221                         try {
222                                 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
223                                 Assert.Fail ("#1");
224                         } catch (Exception ex) {
225                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
226                                 Assert.IsNotNull (ex.InnerException, "#3");
227                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
228                         }
229                 }
230
231                 [Serializable]
232                 private sealed class MyCultureInfo : CultureInfo
233                 {
234                         internal MyCultureInfo ()
235                                 : base ("en-US")
236                         {
237                         }
238
239                         public override object GetFormat (Type formatType)
240                         {
241                                 if (formatType == typeof (NumberFormatInfo)) {
242                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
243
244                                         nfi.NegativeSign = "myNegativeSign";
245                                         return NumberFormatInfo.ReadOnly (nfi);
246                                 } else {
247                                         return base.GetFormat (formatType);
248                                 }
249                         }
250                 }
251         }
252 }
253