399dda788bad5ed69294ba485e8f03955cdb92ee
[mono.git] / mcs / class / System / Test / System.ComponentModel / UInt16ConverterTests.cs
1 //
2 // System.ComponentModel.UInt16Converter 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 UInt16ConverterTests
21         {
22                 private UInt16Converter converter;
23                 
24                 [SetUp]
25                 public void SetUp ()
26                 {
27                         converter = new UInt16Converter ();
28                 }
29
30                 [Test]
31                 public void CanConvertFrom ()
32                 {
33                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34                         Assert.IsFalse (converter.CanConvertFrom (typeof (short)), "#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 (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0"), "#1");
50                         Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0x0"), "#2");
51                         Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0X0"), "#3");
52                         Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0x0"), "#4");
53                         Assert.AreEqual (ushort.MinValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0X0"), "#5");
54                 }
55
56                 [Test]
57                 public void ConvertFrom_MaxValue ()
58                 {
59                         Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#ffff"), "#1");
60                         Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#FFFF"), "#2");
61                         Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0xffff"), "#3");
62                         Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "#0XFFFF"), "#4");
63                         Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0xffff"), "#5");
64                         Assert.AreEqual (ushort.MaxValue, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "0XFFFF"), "#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_Int16 ()
77                 {
78                         converter.ConvertFrom ((short) 10);
79                 }
80
81                 [Test]
82                 [ExpectedException (typeof (NotSupportedException))]
83                 public void ConvertFrom_Int32 ()
84                 {
85                         converter.ConvertFrom (10);
86                 }
87
88                 [Test]
89                 public void ConvertTo_MinValue ()
90                 {
91                         Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.InvariantCulture),
92                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, ushort.MinValue,
93                                 typeof (string)), "#1");
94                         Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.CurrentCulture),
95                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, ushort.MinValue,
96                                 typeof (string)), "#2");
97                         Assert.AreEqual (ushort.MinValue.ToString (CultureInfo.CurrentCulture),
98                                 converter.ConvertTo (ushort.MinValue, typeof (string)), "#3");
99                 }
100
101                 [Test]
102                 public void ConvertTo_MaxValue ()
103                 {
104                         Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.InvariantCulture),
105                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, ushort.MaxValue,
106                                 typeof (string)), "#1");
107                         Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.CurrentCulture),
108                                 converter.ConvertTo (null, CultureInfo.CurrentCulture, ushort.MaxValue,
109                                 typeof (string)), "#2");
110                         Assert.AreEqual (ushort.MaxValue.ToString (CultureInfo.CurrentCulture),
111                                 converter.ConvertTo (ushort.MaxValue, typeof (string)), "#3");
112                 }
113
114                 [Test]
115                 public void ConvertFromString_Invalid1 ()
116                 {
117                         try {
118                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
119                                 Assert.Fail ("#1");
120                         } catch (Exception ex) {
121                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
122                                 Assert.IsNotNull (ex.InnerException, "#3");
123                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
124                         }
125                 }
126
127                 [Test]
128                 public void ConvertFromString_Invalid2 ()
129                 {
130                         try {
131                                 converter.ConvertFromString (null, CultureInfo.InvariantCulture,
132                                         double.MaxValue.ToString(CultureInfo.InvariantCulture));
133                                 Assert.Fail ("#1");
134                         } catch (Exception ex) {
135                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
136                                 Assert.IsNotNull (ex.InnerException, "#3");
137                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
138                         }
139                 }
140
141                 [Test]
142                 public void ConvertFromString_Invalid3 ()
143                 {
144                         try {
145                                 converter.ConvertFromString ("*1");
146                                 Assert.Fail ("#1");
147                         } catch (Exception ex) {
148                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
149                                 Assert.IsNotNull (ex.InnerException, "#3");
150                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
151                         }
152                 }
153
154                 [Test]
155                 public void ConvertFromString_Invalid4 ()
156                 {
157                         try {
158                                 converter.ConvertFromString (double.MaxValue.ToString (CultureInfo.CurrentCulture));
159                                 Assert.Fail ("#1");
160                         } catch (Exception ex) {
161                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
162                                 Assert.IsNotNull (ex.InnerException, "#3");
163                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
164                         }
165                 }
166
167                 [Test]
168                 public void ConvertFrom_InvalidString1 ()
169                 {
170                         try {
171                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
172                                 Assert.Fail ("#1");
173                         } catch (Exception ex) {
174                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
175                                 Assert.IsNotNull (ex.InnerException, "#3");
176                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
177                         }
178                 }
179
180                 [Test]
181                 public void ConvertFrom_InvalidString2 ()
182                 {
183                         try {
184                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
185                                         double.MaxValue.ToString (CultureInfo.InvariantCulture));
186                                 Assert.Fail ("#1");
187                         } catch (Exception ex) {
188                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
189                                 Assert.IsNotNull (ex.InnerException, "#3");
190                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
191                         }
192                 }
193
194                 [Test]
195                 public void ConvertFrom_InvalidString3 ()
196                 {
197                         try {
198                                 converter.ConvertFrom ("*1");
199                                 Assert.Fail ("#1");
200                         } catch (Exception ex) {
201                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
202                                 Assert.IsNotNull (ex.InnerException, "#3");
203                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
204                         }
205                 }
206
207                 [Test]
208                 public void ConvertFrom_InvalidString4 ()
209                 {
210                         try {
211                                 converter.ConvertFrom (double.MaxValue.ToString (CultureInfo.CurrentCulture));
212                                 Assert.Fail ("#1");
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 }
221