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