Merge pull request #268 from pcc/menudeactivate
[mono.git] / mcs / class / System / Test / System.ComponentModel / SingleConverterTests.cs
1 //
2 // System.ComponentModel.SingleConverter 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 SingleConverterTests
21         {
22                 private SingleConverter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new SingleConverter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (float)), "#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_String ()
49                 {
50                         Assert.AreEqual (10, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "10"), "#1");
51                 }
52
53                 [Test]
54                 [ExpectedException (typeof (NotSupportedException))]
55                 public void ConvertFrom_Object ()
56                 {
57                         converter.ConvertFrom (new object ());
58                 }
59
60                 [Test]
61                 [ExpectedException (typeof (NotSupportedException))]
62                 public void ConvertFrom_Int32 ()
63                 {
64                         converter.ConvertFrom (int.MaxValue);
65                 }
66
67                 [Test]
68                 public void ConvertTo ()
69                 {
70                         Assert.AreEqual (1.5f.ToString (CultureInfo.InvariantCulture),
71                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, 1.5f, 
72                                 typeof (string)), "#1");
73                         Assert.AreEqual (1.5f.ToString (CultureInfo.CurrentCulture),
74                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, 1.5f, 
75                                 typeof (string)), "#2");
76                         Assert.AreEqual (1.5f.ToString (CultureInfo.CurrentCulture),
77                                 converter.ConvertTo (1.5f, typeof (string)), "#3");
78                 }
79
80                 [Test]
81                 public void ConvertToString ()
82                 {
83                         CultureInfo culture = new MyCultureInfo ();
84                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
85
86                         Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (float) -5), "#1");
87                         Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
88                 }
89
90                 [Test]
91                 public void ConvertFromString ()
92                 {
93                         CultureInfo culture = new MyCultureInfo ();
94                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
95
96                         Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
97                 }
98
99                 [Test]
100                 public void ConvertFromString_Invalid1 ()
101                 {
102                         try {
103                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
104                                 Assert.Fail ("#1");
105                         } catch (AssertionException) {
106                                 throw;
107                         } catch (Exception ex) {
108                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
109                                 Assert.IsNotNull (ex.InnerException, "#3");
110                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
111                         }
112                 }
113
114                 [Test]
115                 public void ConvertFromString_Invalid2 ()
116                 {
117                         try {
118                                 converter.ConvertFromString ("*1");
119                                 Assert.Fail ("#1");
120                         } catch (AssertionException) {
121                                 throw;
122                         } catch (Exception ex) {
123                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
124                                 Assert.IsNotNull (ex.InnerException, "#3");
125                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
126                         }
127                 }
128
129                 [Test]
130                 public void ConvertFrom_InvalidString1 ()
131                 {
132                         try {
133                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
134                                 Assert.Fail ("#1");
135                         } catch (AssertionException) {
136                                 throw;
137                         } catch (Exception ex) {
138                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
139                                 Assert.IsNotNull (ex.InnerException, "#3");
140                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
141                         }
142                 }
143
144                 [Test]
145                 public void ConvertFrom_InvalidString2 ()
146                 {
147                         try {
148                                 converter.ConvertFrom ("*1");
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 (), "#4");
156                         }
157                 }
158
159                 [Test]
160                 public void ConvertFromString_Hex1 ()
161                 {
162                         try {
163                                 converter.ConvertFromString ("#10");
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 (), "#4");
171                         }
172                 }
173
174                 [Test]
175                 public void ConvertFromString_Hex2 ()
176                 {
177                         try {
178                                 converter.ConvertFromString ("0x10");
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 (), "#4");
186                         }
187                 }
188
189                 [Test]
190                 public void ConvertFrom_HexString1 ()
191                 {
192                         try {
193                                 converter.ConvertFrom ("#10");
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 (), "#4");
201                         }
202                 }
203
204                 [Test]
205                 public void ConvertFrom_HexString2 ()
206                 {
207                         try {
208                                 converter.ConvertFrom ("0x10");
209                                 Assert.Fail ("#1");
210                         } catch (AssertionException) {
211                                 throw;
212                         } catch (Exception ex) {
213                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
214                                 Assert.IsNotNull (ex.InnerException, "#3");
215                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
216                         }
217                 }
218
219                 [Serializable]
220                 private sealed class MyCultureInfo : CultureInfo
221                 {
222                         internal MyCultureInfo ()
223                                 : base ("en-US")
224                         {
225                         }
226
227                         public override object GetFormat (Type formatType)
228                         {
229                                 if (formatType == typeof (NumberFormatInfo)) {
230                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
231
232                                         nfi.NegativeSign = "myNegativeSign";
233                                         return NumberFormatInfo.ReadOnly (nfi);
234                                 } else {
235                                         return base.GetFormat (formatType);
236                                 }
237                         }
238
239 #if NET_2_0
240 // adding this override in 1.x shows different result in .NET (it is ignored).
241 // Some compatibility kids might want to fix this issue.
242                         public override NumberFormatInfo NumberFormat {
243                                 get {
244                                         NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
245                                         nfi.NegativeSign = "myNegativeSign";
246                                         return nfi;
247                                 }
248                                 set { throw new NotSupportedException (); }
249                         }
250 #endif
251                 }
252         }
253 }