2
0

QueryStringConverterTest.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // QueryStringConverterTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. using System;
  29. using System.ComponentModel;
  30. using System.Globalization;
  31. using System.ServiceModel.Dispatcher;
  32. using System.Text;
  33. using System.Xml;
  34. using NUnit.Framework;
  35. using CategoryAttribute = NUnit.Framework.CategoryAttribute;
  36. namespace MonoTests.System.ServiceModel.Description
  37. {
  38. [TestFixture]
  39. public class QueryStringConverterTest
  40. {
  41. QueryStringConverter c;
  42. [SetUp]
  43. public void Setup ()
  44. {
  45. c = new QueryStringConverter ();
  46. }
  47. [Test]
  48. public void CanConvert ()
  49. {
  50. Assert.IsTrue (c.CanConvert (typeof (bool)), "#1");
  51. Assert.IsTrue (c.CanConvert (typeof (char)), "#2");
  52. Assert.IsTrue (c.CanConvert (typeof (double)), "#3");
  53. Assert.IsTrue (c.CanConvert (typeof (decimal)), "#4");
  54. Assert.IsTrue (c.CanConvert (typeof (float)), "#5");
  55. Assert.IsTrue (c.CanConvert (typeof (string)), "#6");
  56. Assert.IsTrue (c.CanConvert (typeof (int)), "#7");
  57. Assert.IsTrue (c.CanConvert (typeof (byte)), "#8");
  58. Assert.IsTrue (c.CanConvert (typeof (sbyte)), "#9");
  59. Assert.IsTrue (c.CanConvert (typeof (long)), "#10");
  60. Assert.IsTrue (c.CanConvert (typeof (ulong)), "#11");
  61. Assert.IsTrue (c.CanConvert (typeof (DateTime)), "#12");
  62. Assert.IsTrue (c.CanConvert (typeof (DateTimeOffset)), "#13");
  63. Assert.IsTrue (c.CanConvert (typeof (TimeSpan)), "#14");
  64. Assert.IsTrue (c.CanConvert (typeof (Guid)), "#15");
  65. Assert.IsFalse (c.CanConvert (typeof (XmlQualifiedName)), "#16");
  66. Assert.IsTrue (c.CanConvert (typeof (object)), "#17");
  67. Assert.IsFalse (c.CanConvert (typeof (QueryStringConverter)), "#18");
  68. // TypeConverterAttribute does not help it.
  69. Assert.IsFalse (c.CanConvert (typeof (MyConvertible)), "#19");
  70. }
  71. // ConvertStringToValue
  72. [Test]
  73. [ExpectedException (typeof (InvalidCastException))]
  74. public void ConvertValueToStringInvalidCast ()
  75. {
  76. c.ConvertValueToString ("ABC", typeof (char));
  77. }
  78. [Test]
  79. [ExpectedException (typeof (InvalidCastException))]
  80. public void ConvertValueToStringInvalidCast2 ()
  81. {
  82. c.ConvertValueToString (123, typeof (string));
  83. }
  84. [Test]
  85. [ExpectedException (typeof (InvalidCastException))]
  86. public void ConvertValueToStringInvalidCast3 ()
  87. {
  88. c.ConvertValueToString ("123", typeof (int));
  89. }
  90. [Test]
  91. [ExpectedException (typeof (InvalidCastException))]
  92. public void ConvertValueToStringInvalidCast4 ()
  93. {
  94. c.ConvertValueToString (123.45, typeof (int));
  95. }
  96. [Test]
  97. [ExpectedException (typeof (InvalidCastException))]
  98. public void ConvertValueToStringInvalidCast5 ()
  99. {
  100. // umm...
  101. c.ConvertValueToString (123, typeof (double));
  102. }
  103. [Test]
  104. [ExpectedException (typeof (ArgumentNullException))]
  105. public void ConvertValueToStringNullToValueType ()
  106. {
  107. c.ConvertValueToString (null, typeof (char));
  108. }
  109. [Test]
  110. [ExpectedException (typeof (NotSupportedException))]
  111. public void ConvertValueToStringDbNull ()
  112. {
  113. c.ConvertValueToString (DBNull.Value, typeof (DBNull));
  114. }
  115. [Test]
  116. public void ConvertValueToStringNullToString ()
  117. {
  118. Assert.IsNull (c.ConvertValueToString (null, typeof (string)));
  119. }
  120. [Test]
  121. public void ConvertValueToString ()
  122. {
  123. Assert.AreEqual ("A", c.ConvertValueToString ('A', typeof (char)), "#1");
  124. Assert.AreEqual ("}}}", c.ConvertValueToString ("}}}", typeof (string)), "#2");
  125. Assert.AreEqual ("123", c.ConvertValueToString (123.0, typeof (double)), "#3");
  126. }
  127. // ConvertStringToValue
  128. [Test]
  129. [ExpectedException (typeof (FormatException))]
  130. public void ConvertStringToValueInvalidCast ()
  131. {
  132. c.ConvertStringToValue ("ABC", typeof (char));
  133. }
  134. [Test]
  135. [ExpectedException (typeof (FormatException))]
  136. [Category ("NotWorking")]
  137. public void ConvertStringToValueInvalidCast2 ()
  138. {
  139. c.ConvertStringToValue ("-123", typeof (uint));
  140. }
  141. [Test]
  142. [ExpectedException (typeof (FormatException))]
  143. public void ConvertStringToValueInvalidCast4 ()
  144. {
  145. c.ConvertStringToValue ("123.45", typeof (int));
  146. }
  147. [Test]
  148. public void ConvertStringToValueNullToValueType ()
  149. {
  150. // hmm, it passes.
  151. Assert.AreEqual (default (char), c.ConvertStringToValue (null, typeof (char)));
  152. }
  153. [Test]
  154. [ExpectedException (typeof (NotSupportedException))]
  155. public void ConvertStringToValueDbNull ()
  156. {
  157. c.ConvertStringToValue (null, typeof (DBNull));
  158. }
  159. [Test]
  160. public void ConvertStringToValueNullToString ()
  161. {
  162. Assert.IsNull (c.ConvertStringToValue (null, typeof (string)));
  163. }
  164. [Test]
  165. public void ConvertStringToValue ()
  166. {
  167. Assert.AreEqual ('A', c.ConvertStringToValue ("A", typeof (char)), "#1");
  168. Assert.AreEqual ("}}}", c.ConvertStringToValue ("}}}", typeof (string)), "#2");
  169. Assert.AreEqual (123.0, c.ConvertStringToValue ("123.0", typeof (double)), "#3");
  170. Assert.AreEqual (123.0, c.ConvertStringToValue ("123", typeof (double)), "#4");
  171. }
  172. // Types
  173. [TypeConverter (typeof (MyTypeConverter))]
  174. class MyConvertible
  175. {
  176. }
  177. class MyTypeConverter : TypeConverter
  178. {
  179. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  180. {
  181. if (destinationType == typeof (string))
  182. return "hogehoge";
  183. throw new Exception ();
  184. }
  185. }
  186. }
  187. }