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");
76 // ConvertStringToValue
79 [ExpectedException (typeof (InvalidCastException))]
80 public void ConvertValueToStringInvalidCast ()
82 c.ConvertValueToString ("ABC", typeof (char));
86 [ExpectedException (typeof (InvalidCastException))]
87 public void ConvertValueToStringInvalidCast2 ()
89 c.ConvertValueToString (123, typeof (string));
93 [ExpectedException (typeof (InvalidCastException))]
94 public void ConvertValueToStringInvalidCast3 ()
96 c.ConvertValueToString ("123", typeof (int));
100 [ExpectedException (typeof (InvalidCastException))]
101 public void ConvertValueToStringInvalidCast4 ()
103 c.ConvertValueToString (123.45, typeof (int));
107 [ExpectedException (typeof (InvalidCastException))]
108 public void ConvertValueToStringInvalidCast5 ()
111 c.ConvertValueToString (123, typeof (double));
115 [ExpectedException (typeof (ArgumentNullException))]
116 public void ConvertValueToStringNullToValueType ()
118 c.ConvertValueToString (null, typeof (char));
122 [ExpectedException (typeof (NotSupportedException))]
123 public void ConvertValueToStringDbNull ()
125 c.ConvertValueToString (DBNull.Value, typeof (DBNull));
129 public void ConvertValueToStringNullToString ()
131 Assert.IsNull (c.ConvertValueToString (null, typeof (string)));
135 public void ConvertValueToString ()
137 Assert.AreEqual ("A", c.ConvertValueToString ('A', typeof (char)), "#1");
138 Assert.AreEqual ("}}}", c.ConvertValueToString ("}}}", typeof (string)), "#2");
139 Assert.AreEqual ("123", c.ConvertValueToString (123.0, typeof (double)), "#3");
142 // ConvertStringToValue
145 [ExpectedException (typeof (FormatException))]
146 public void ConvertStringToValueInvalidCast ()
148 c.ConvertStringToValue ("ABC", typeof (char));
152 [ExpectedException (typeof (FormatException))]
153 [Category ("NotWorking")]
154 public void ConvertStringToValueInvalidCast2 ()
156 c.ConvertStringToValue ("-123", typeof (uint));
160 [ExpectedException (typeof (FormatException))]
161 public void ConvertStringToValueInvalidCast4 ()
163 c.ConvertStringToValue ("123.45", typeof (int));
167 public void ConvertStringToValueNullToValueType ()
170 Assert.AreEqual (default (char), c.ConvertStringToValue (null, typeof (char)));
174 [ExpectedException (typeof (NotSupportedException))]
175 public void ConvertStringToValueDbNull ()
177 c.ConvertStringToValue (null, typeof (DBNull));
181 public void ConvertStringToValueNullToString ()
183 Assert.IsNull (c.ConvertStringToValue (null, typeof (string)));
187 public void ConvertStringToValue ()
189 Assert.AreEqual ('A', c.ConvertStringToValue ("A", typeof (char)), "#1");
190 Assert.AreEqual ("}}}", c.ConvertStringToValue ("}}}", typeof (string)), "#2");
191 Assert.AreEqual (123.0, c.ConvertStringToValue ("123.0", typeof (double)), "#3");
192 Assert.AreEqual (123.0, c.ConvertStringToValue ("123", typeof (double)), "#4");
197 [TypeConverter (typeof (MyTypeConverter))]
202 class MyTypeConverter : TypeConverter
204 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
206 if (destinationType == typeof (string))
208 throw new Exception ();