2010-01-20 Zoltan Varga <vargaz@gmail.com>
[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                 }
45
46                 [Test]
47                 public void ConvertFrom_String ()
48                 {
49                         Assert.AreEqual (10, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "10"), "#1");
50                 }
51
52                 [Test]
53                 [ExpectedException (typeof (NotSupportedException))]
54                 public void ConvertFrom_Object ()
55                 {
56                         converter.ConvertFrom (new object ());
57                 }
58
59                 [Test]
60                 [ExpectedException (typeof (NotSupportedException))]
61                 public void ConvertFrom_Int32 ()
62                 {
63                         converter.ConvertFrom (int.MaxValue);
64                 }
65
66                 [Test]
67                 public void ConvertTo ()
68                 {
69                         Assert.AreEqual (1.5f.ToString (CultureInfo.InvariantCulture),
70                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, 1.5f, 
71                                 typeof (string)), "#1");
72                         Assert.AreEqual (1.5f.ToString (CultureInfo.CurrentCulture),
73                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, 1.5f, 
74                                 typeof (string)), "#2");
75                         Assert.AreEqual (1.5f.ToString (CultureInfo.CurrentCulture),
76                                 converter.ConvertTo (1.5f, typeof (string)), "#3");
77                 }
78
79                 [Test]
80                 public void ConvertToString ()
81                 {
82                         CultureInfo culture = new MyCultureInfo ();
83                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
84
85                         Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (float) -5), "#1");
86                         Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2");
87                 }
88
89                 [Test]
90                 public void ConvertFromString ()
91                 {
92                         CultureInfo culture = new MyCultureInfo ();
93                         NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo));
94
95                         Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5"));
96                 }
97
98                 [Test]
99                 public void ConvertFromString_Invalid1 ()
100                 {
101                         try {
102                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
103                                 Assert.Fail ("#1");
104                         } catch (AssertionException) {
105                                 throw;
106                         } catch (Exception ex) {
107                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
108                                 Assert.IsNotNull (ex.InnerException, "#3");
109                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
110                         }
111                 }
112
113                 [Test]
114                 public void ConvertFromString_Invalid2 ()
115                 {
116                         try {
117                                 converter.ConvertFromString ("*1");
118                                 Assert.Fail ("#1");
119                         } catch (AssertionException) {
120                                 throw;
121                         } catch (Exception ex) {
122                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
123                                 Assert.IsNotNull (ex.InnerException, "#3");
124                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
125                         }
126                 }
127
128                 [Test]
129                 public void ConvertFrom_InvalidString1 ()
130                 {
131                         try {
132                                 converter.ConvertFrom (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 (), "#4");
140                         }
141                 }
142
143                 [Test]
144                 public void ConvertFrom_InvalidString2 ()
145                 {
146                         try {
147                                 converter.ConvertFrom ("*1");
148                                 Assert.Fail ("#1");
149                         } catch (AssertionException) {
150                                 throw;
151                         } catch (Exception ex) {
152                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
153                                 Assert.IsNotNull (ex.InnerException, "#3");
154                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
155                         }
156                 }
157
158                 [Test]
159                 public void ConvertFromString_Hex1 ()
160                 {
161                         try {
162                                 converter.ConvertFromString ("#10");
163                                 Assert.Fail ("#1");
164                         } catch (AssertionException) {
165                                 throw;
166                         } catch (Exception ex) {
167                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
168                                 Assert.IsNotNull (ex.InnerException, "#3");
169                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
170                         }
171                 }
172
173                 [Test]
174                 public void ConvertFromString_Hex2 ()
175                 {
176                         try {
177                                 converter.ConvertFromString ("0x10");
178                                 Assert.Fail ("#1");
179                         } catch (AssertionException) {
180                                 throw;
181                         } catch (Exception ex) {
182                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
183                                 Assert.IsNotNull (ex.InnerException, "#3");
184                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4");
185                         }
186                 }
187
188                 [Test]
189                 public void ConvertFrom_HexString1 ()
190                 {
191                         try {
192                                 converter.ConvertFrom ("#10");
193                                 Assert.Fail ("#1");
194                         } catch (AssertionException) {
195                                 throw;
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 (), "#4");
200                         }
201                 }
202
203                 [Test]
204                 public void ConvertFrom_HexString2 ()
205                 {
206                         try {
207                                 converter.ConvertFrom ("0x10");
208                                 Assert.Fail ("#1");
209                         } catch (AssertionException) {
210                                 throw;
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 (), "#4");
215                         }
216                 }
217
218                 [Serializable]
219                 private sealed class MyCultureInfo : CultureInfo
220                 {
221                         internal MyCultureInfo ()
222                                 : base ("en-US")
223                         {
224                         }
225
226                         public override object GetFormat (Type formatType)
227                         {
228                                 if (formatType == typeof (NumberFormatInfo)) {
229                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
230
231                                         nfi.NegativeSign = "myNegativeSign";
232                                         return NumberFormatInfo.ReadOnly (nfi);
233                                 } else {
234                                         return base.GetFormat (formatType);
235                                 }
236                         }
237
238 #if NET_2_0
239 // adding this override in 1.x shows different result in .NET (it is ignored).
240 // Some compatibility kids might want to fix this issue.
241                         public override NumberFormatInfo NumberFormat {
242                                 get {
243                                         NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone ();
244                                         nfi.NegativeSign = "myNegativeSign";
245                                         return nfi;
246                                 }
247                                 set { throw new NotSupportedException (); }
248                         }
249 #endif
250                 }
251         }
252 }