PropertyConverterTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. // PropertyConverterTest.cs
  22. //
  23. // Author:
  24. // Jackson Harper ([email protected])
  25. //
  26. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  27. //
  28. using System;
  29. using System.Web;
  30. using System.Web.UI;
  31. using System.Reflection;
  32. using System.Globalization;
  33. using System.ComponentModel;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Web.UI {
  36. enum TestEnum {
  37. Default,
  38. Normal,
  39. LowerCase,
  40. UpperCase,
  41. }
  42. [Flags]
  43. enum TestFlags {
  44. A,
  45. B,
  46. C
  47. }
  48. [TestFixture]
  49. public class PropertyConverterTest {
  50. [Test]
  51. public void EnumFromString ()
  52. {
  53. object e = TestEnum.Default;
  54. e = PropertyConverter.EnumFromString (typeof (TestEnum), "Normal");
  55. Assert.AreEqual (TestEnum.Normal, e, "Normal");
  56. e = PropertyConverter.EnumFromString (typeof (TestEnum), "lowercase");
  57. Assert.AreEqual (TestEnum.LowerCase, e, "Lower Case");
  58. e = PropertyConverter.EnumFromString (typeof (TestEnum), "UPPERCASE");
  59. Assert.AreEqual (TestEnum.UpperCase, e, "Upper Case");
  60. e = PropertyConverter.EnumFromString (typeof (TestEnum), "DoesntExist");
  61. Assert.AreEqual (null, e, "Doesn't Exist");
  62. e = PropertyConverter.EnumFromString (typeof (TestEnum), "TestEnum.Normal");
  63. Assert.AreEqual (null, e, "Full Name");
  64. }
  65. [Test]
  66. public void TestFromStringFlags ()
  67. {
  68. object e = TestEnum.Default;
  69. e = PropertyConverter.EnumFromString (typeof (TestFlags), "A");
  70. Assert.AreEqual (e, TestFlags.A, "Normal");
  71. e = PropertyConverter.EnumFromString (typeof (TestFlags), "A, B");
  72. Assert.AreEqual (e, TestFlags.A | TestFlags.B, "Multiple");
  73. e = PropertyConverter.EnumFromString (typeof (TestFlags), "foo");
  74. Assert.AreEqual (e, null, "Bad");
  75. }
  76. [Test]
  77. public void EnumToString ()
  78. {
  79. Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestEnum), 1),
  80. "Normal", "Normal");
  81. Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestEnum), 25),
  82. "25", "Decimal");
  83. }
  84. [Test]
  85. public void EnumToStringFlags ()
  86. {
  87. Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestFlags), 0),
  88. "A", "A");
  89. Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestFlags), 3),
  90. "B, C", "Multiple");
  91. }
  92. [Test]
  93. [ExpectedException (typeof (ArgumentException))]
  94. public void EnumToStringWrongBaseType ()
  95. {
  96. PropertyConverter.EnumToString (typeof (TestEnum), "foo");
  97. }
  98. public void TestObjectFromString ()
  99. {
  100. Assert.AreEqual (PropertyConverter.ObjectFromString (
  101. typeof (string), null, "value"),
  102. "value", "String Type");
  103. MemberInfo mi = this.GetType ().GetProperty ("AllowedConverterProperty");
  104. Assert.AreEqual (PropertyConverter.ObjectFromString (
  105. typeof (int), mi, "ConverterValue"),
  106. "ConverterValue", "Converter Value");
  107. }
  108. [Test]
  109. [ExpectedException (typeof (NullReferenceException))]
  110. public void TestObjectFromStringNullRef ()
  111. {
  112. PropertyConverter.ObjectFromString (typeof (int), // can't be string
  113. null, "foobar");
  114. }
  115. [Test]
  116. [ExpectedException (typeof (HttpException))]
  117. public void TestObjectFromStringCantConvert ()
  118. {
  119. MemberInfo mi = this.GetType ().GetProperty ("NotAllowedConverterProperty");
  120. PropertyConverter.ObjectFromString (typeof (int), // can't be string
  121. mi, "foobar");
  122. }
  123. [TypeConverter (typeof (AllowedMagicTypeConverter))]
  124. public string AllowedConverterProperty {
  125. get { return "AllowedConverterProperty"; }
  126. }
  127. [TypeConverter (typeof (NotAllowedMagicTypeConverter))]
  128. public string NotAllowedConverterProperty {
  129. get { return "NotAllowedConverterProperty"; }
  130. }
  131. public class AllowedMagicTypeConverter : MagicTypeConverter {
  132. public AllowedMagicTypeConverter () : base (true)
  133. {
  134. }
  135. }
  136. public class NotAllowedMagicTypeConverter : MagicTypeConverter {
  137. public NotAllowedMagicTypeConverter () : base (false)
  138. {
  139. }
  140. }
  141. public abstract class MagicTypeConverter : TypeConverter {
  142. private bool allow;
  143. public MagicTypeConverter (bool allow)
  144. {
  145. this.allow = allow;
  146. }
  147. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  148. {
  149. return allow;
  150. }
  151. public override object ConvertFrom (ITypeDescriptorContext context,
  152. CultureInfo culture, object value)
  153. {
  154. return "ConverterValue";
  155. }
  156. }
  157. }
  158. }