2 // QueryStringConverterTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.ComponentModel;
30 using System.Globalization;
31 using System.ServiceModel.Dispatcher;
34 using NUnit.Framework;
36 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
38 namespace MonoTests.System.ServiceModel.Description
41 public class QueryStringConverterTest
43 QueryStringConverter c;
48 c = new QueryStringConverter ();
52 public void CanConvert ()
54 Assert.IsTrue (c.CanConvert (typeof (bool)), "#1");
55 Assert.IsTrue (c.CanConvert (typeof (char)), "#2");
56 Assert.IsTrue (c.CanConvert (typeof (double)), "#3");
57 Assert.IsTrue (c.CanConvert (typeof (decimal)), "#4");
58 Assert.IsTrue (c.CanConvert (typeof (float)), "#5");
59 Assert.IsTrue (c.CanConvert (typeof (string)), "#6");
60 Assert.IsTrue (c.CanConvert (typeof (int)), "#7");
61 Assert.IsTrue (c.CanConvert (typeof (byte)), "#8");
62 Assert.IsTrue (c.CanConvert (typeof (sbyte)), "#9");
63 Assert.IsTrue (c.CanConvert (typeof (long)), "#10");
64 Assert.IsTrue (c.CanConvert (typeof (ulong)), "#11");
65 Assert.IsTrue (c.CanConvert (typeof (DateTime)), "#12");
66 Assert.IsTrue (c.CanConvert (typeof (DateTimeOffset)), "#13");
67 Assert.IsTrue (c.CanConvert (typeof (TimeSpan)), "#14");
68 Assert.IsTrue (c.CanConvert (typeof (Guid)), "#15");
69 Assert.IsFalse (c.CanConvert (typeof (XmlQualifiedName)), "#16");
70 Assert.IsTrue (c.CanConvert (typeof (object)), "#17");
71 Assert.IsFalse (c.CanConvert (typeof (QueryStringConverter)), "#18");
72 // TypeConverterAttribute does not help it.
73 Assert.IsFalse (c.CanConvert (typeof (MyConvertible)), "#19");
74 Assert.IsTrue (c.CanConvert (typeof (DemoEnum)), "#20");
77 // ConvertStringToValue
80 [ExpectedException (typeof (InvalidCastException))]
81 public void ConvertValueToStringInvalidCast ()
83 c.ConvertValueToString ("ABC", typeof (char));
87 [ExpectedException (typeof (InvalidCastException))]
88 public void ConvertValueToStringInvalidCast2 ()
90 c.ConvertValueToString (123, typeof (string));
94 [ExpectedException (typeof (InvalidCastException))]
95 public void ConvertValueToStringInvalidCast3 ()
97 c.ConvertValueToString ("123", typeof (int));
101 [ExpectedException (typeof (InvalidCastException))]
102 public void ConvertValueToStringInvalidCast4 ()
104 c.ConvertValueToString (123.45, typeof (int));
108 [ExpectedException (typeof (InvalidCastException))]
109 public void ConvertValueToStringInvalidCast5 ()
112 c.ConvertValueToString (123, typeof (double));
116 [ExpectedException (typeof (ArgumentNullException))]
117 public void ConvertValueToStringNullToValueType ()
119 c.ConvertValueToString (null, typeof (char));
123 [ExpectedException (typeof (NotSupportedException))]
124 public void ConvertValueToStringDbNull ()
126 c.ConvertValueToString (DBNull.Value, typeof (DBNull));
130 public void ConvertValueToStringNullToString ()
132 Assert.IsNull (c.ConvertValueToString (null, typeof (string)));
136 public void ConvertValueToString ()
138 Assert.AreEqual ("A", c.ConvertValueToString ('A', typeof (char)), "#1");
139 Assert.AreEqual ("}}}", c.ConvertValueToString ("}}}", typeof (string)), "#2");
140 Assert.AreEqual ("123", c.ConvertValueToString (123.0, typeof (double)), "#3");
144 public void ConvertValueToStringEnum ()
146 string stringValue = c.ConvertValueToString (DemoEnum.Value2, typeof (DemoEnum));
147 Assert.AreEqual ("Value2", stringValue);
150 // ConvertStringToValue
153 public void ConvertStringToValueEnum ()
155 Assert.AreEqual (DemoEnum.Value3, (DemoEnum)c.ConvertStringToValue ("Value3", typeof(DemoEnum)));
156 Assert.AreEqual (DemoEnum.Value2, (DemoEnum)c.ConvertStringToValue ("value2", typeof(DemoEnum)));
160 [ExpectedException (typeof (FormatException))]
161 public void ConvertStringToValueInvalidCast ()
163 c.ConvertStringToValue ("ABC", typeof (char));
167 [ExpectedException (typeof (FormatException))]
168 [Category ("NotWorking")]
169 public void ConvertStringToValueInvalidCast2 ()
171 c.ConvertStringToValue ("-123", typeof (uint));
175 [ExpectedException (typeof (FormatException))]
176 public void ConvertStringToValueInvalidCast4 ()
178 c.ConvertStringToValue ("123.45", typeof (int));
182 public void ConvertStringToValueNullToValueType ()
185 Assert.AreEqual (default (char), c.ConvertStringToValue (null, typeof (char)));
189 [ExpectedException (typeof (NotSupportedException))]
190 public void ConvertStringToValueDbNull ()
192 c.ConvertStringToValue (null, typeof (DBNull));
196 public void ConvertStringToValueNullToString ()
198 Assert.IsNull (c.ConvertStringToValue (null, typeof (string)));
202 public void ConvertStringToValue ()
204 Assert.AreEqual ('A', c.ConvertStringToValue ("A", typeof (char)), "#1");
205 Assert.AreEqual ("}}}", c.ConvertStringToValue ("}}}", typeof (string)), "#2");
206 Assert.AreEqual (123.0, c.ConvertStringToValue ("123.0", typeof (double)), "#3");
207 Assert.AreEqual (123.0, c.ConvertStringToValue ("123", typeof (double)), "#4");
220 [TypeConverter (typeof (MyTypeConverter))]
225 class MyTypeConverter : TypeConverter
227 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
229 if (destinationType == typeof (string))
231 throw new Exception ();