Merge pull request #268 from pcc/menudeactivate
[mono.git] / mcs / class / System / Test / System.ComponentModel / Int64ConverterTests.cs
1 //
2 // System.ComponentModel.Int64Converter 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 Int64ConverterTests
21         {
22                 private Int64Converter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new Int64Converter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (long)), "#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 (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#8000000000000000"), "#1");
51                         Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x8000000000000000"), "#2");
52                         Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X8000000000000000"), "#3");
53                         Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x8000000000000000"), "#4");
54                         Assert.AreEqual (long.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X8000000000000000"), "#5");
55                 }
56
57                 [Test]
58                 public void ConvertFrom_MaxValue ()
59                 {
60                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7fffffffffffffff"), "#1");
61                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#7FFFFFFFFFFFFFFF"), "#2");
62                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x7fffffffffffffff"), "#3");
63                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X7FFFFFFFFFFFFFFF"), "#4");
64                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x7fffffffffffffff"), "#5");
65                         Assert.AreEqual (long.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X7FFFFFFFFFFFFFFF"), "#6");
66                 }
67
68
69                 [Test]
70                 [ExpectedException (typeof (NotSupportedException))]
71                 public void ConvertFrom_Object ()
72                 {
73                         converter.ConvertFrom (new object ());
74                 }
75
76                 [Test]
77                 [ExpectedException (typeof (NotSupportedException))]
78                 public void ConvertFrom_Int32 ()
79                 {
80                         converter.ConvertFrom (10);
81                 }
82
83                 [Test]
84                 public void ConvertTo_MinValue ()
85                 {
86                         Assert.AreEqual (long.MinValue.ToString (CultureInfo.InvariantCulture),
87                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, long.MinValue,
88                                 typeof (string)), "#1");
89                         Assert.AreEqual (long.MinValue.ToString (CultureInfo.CurrentCulture),
90                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, long.MinValue,
91                                 typeof (string)), "#2");
92                         Assert.AreEqual (long.MinValue.ToString (CultureInfo.CurrentCulture),
93                                 converter.ConvertTo (long.MinValue, typeof (string)), "#3");
94                 }
95
96                 [Test]
97                 public void ConvertTo_MaxValue ()
98                 {
99                         Assert.AreEqual (long.MaxValue.ToString (CultureInfo.InvariantCulture),
100                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, long.MaxValue,
101                                 typeof (string)), "#1");
102                         Assert.AreEqual (long.MaxValue.ToString (CultureInfo.CurrentCulture),
103                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, long.MaxValue,
104                                 typeof (string)), "#2");
105                         Assert.AreEqual (long.MaxValue.ToString (CultureInfo.CurrentCulture),
106                                 converter.ConvertTo (long.MaxValue, typeof (string)), "#3");
107                 }
108
109                 [Test]
110                 public void ConvertToString ()
111                 {
112                         CultureInfo culture = new MyCultureInfo ();
113                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
114
115                         Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (long) -5), "#1");
116                         Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
117                 }
118
119                 [Test]
120                 public void ConvertFromString ()
121                 {
122                         CultureInfo culture = new MyCultureInfo ();
123                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
124
125                         Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
126                 }
127
128                 [Test]
129                 public void ConvertFromString_Invalid1 ()
130                 {
131                         try {
132                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
133                                 Assert.Fail ("#1");
134                         } catch (AssertionException) {
135                                 throw;
136                         } catch (Exception ex) {
137                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
138                                 Assert.IsNotNull (ex.InnerException, "#3");
139                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
140                         }
141                 }
142
143                 [Test]
144                 public void ConvertFromString_Invalid2 ()
145                 {
146                         try {
147                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
148                                         double.MaxValue.ToString(CultureInfo.InvariantCulture));
149                                 Assert.Fail ("#1");
150                         } catch (AssertionException) {
151                                 throw;
152                         } catch (Exception ex) {
153                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
154                                 Assert.IsNotNull (ex.InnerException, "#3");
155                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
156                         }
157                 }
158
159                 [Test]
160                 public void ConvertFromString_Invalid3 ()
161                 {
162                         try {
163                                 converter.ConvertFromString ("*1");
164                                 Assert.Fail ("#1");
165                         } catch (AssertionException) {
166                                 throw;
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 (AssertionException) {
181                                 throw;
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_InvalidString1 ()
191                 {
192                         try {
193                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
194                                 Assert.Fail ("#1");
195                         } catch (AssertionException) {
196                                 throw;
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_InvalidString2 ()
206                 {
207                         try {
208                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
209                                         double.MaxValue.ToString (CultureInfo.InvariantCulture));
210                                 Assert.Fail ("#1");
211                         } catch (AssertionException) {
212                                 throw;
213                         } catch (Exception ex) {
214                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
215                                 Assert.IsNotNull (ex.InnerException, "#3");
216                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
217                         }
218                 }
219
220                 [Test]
221                 public void ConvertFrom_InvalidString3 ()
222                 {
223                         try {
224                                 converter.ConvertFrom ("*1");
225                                 Assert.Fail ("#1");
226                         } catch (AssertionException) {
227                                 throw;
228                         } catch (Exception ex) {
229                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
230                                 Assert.IsNotNull (ex.InnerException, "#3");
231                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
232                         }
233                 }
234
235                 [Test]
236                 public void ConvertFrom_InvalidString4 ()
237                 {
238                         try {
239                                 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
240                                 Assert.Fail ("#1");
241                         } catch (AssertionException) {
242                                 throw;
243                         } catch (Exception ex) {
244                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
245                                 Assert.IsNotNull (ex.InnerException, "#3");
246                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
247                         }
248                 }
249
250                 [Serializable]
251                 private sealed class MyCultureInfo : CultureInfo
252                 {
253                         internal MyCultureInfo ()
254                                 : base ("en-US")
255                         {
256                         }
257
258                         public override object GetFormat (Type formatType)
259                         {
260                                 if (formatType == typeof (NumberFormatInfo)) {
261                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
262
263                                         nfi.NegativeSign = "myNegativeSign";
264                                         return NumberFormatInfo.ReadOnly (nfi);
265                                 } else {
266                                         return base.GetFormat (formatType);
267                                 }
268                         }
269
270 #if NET_2_0
271 // adding this override in 1.x shows different result in .NET (it is ignored).
272 // Some compatibility kids might want to fix this issue.
273                         public override NumberFormatInfo NumberFormat {
274                                 get {
275                                         NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
276                                         nfi.NegativeSign = "myNegativeSign";
277                                         return nfi;
278                                 }
279                                 set { throw new NotSupportedException (); }
280                         }
281 #endif
282                 }
283         }
284 }