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:
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
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.
21 // PropertyConverterTest.cs
24 // Jackson Harper (jackson@ximian.com)
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
33 using System.Reflection;
34 using System.Globalization;
35 using System.ComponentModel;
37 using NUnit.Framework;
39 namespace MonoTests.System.Web.UI {
56 public class PropertyConverterTest {
59 public void EnumFromString ()
61 object e = TestEnum.Default;
63 e = PropertyConverter.EnumFromString (typeof (TestEnum), "Normal");
64 Assert.AreEqual (TestEnum.Normal, e, "Normal");
66 e = PropertyConverter.EnumFromString (typeof (TestEnum), "lowercase");
67 Assert.AreEqual (TestEnum.LowerCase, e, "Lower Case");
69 e = PropertyConverter.EnumFromString (typeof (TestEnum), "UPPERCASE");
70 Assert.AreEqual (TestEnum.UpperCase, e, "Upper Case");
72 e = PropertyConverter.EnumFromString (typeof (TestEnum), "DoesntExist");
73 Assert.AreEqual (null, e, "Doesn't Exist");
75 e = PropertyConverter.EnumFromString (typeof (TestEnum), "TestEnum.Normal");
76 Assert.AreEqual (null, e, "Full Name");
80 public void TestFromStringFlags ()
82 object e = TestEnum.Default;
84 e = PropertyConverter.EnumFromString (typeof (TestFlags), "A");
85 Assert.AreEqual (e, TestFlags.A, "Normal");
87 e = PropertyConverter.EnumFromString (typeof (TestFlags), "A, B");
88 Assert.AreEqual (e, TestFlags.A | TestFlags.B, "Multiple");
90 e = PropertyConverter.EnumFromString (typeof (TestFlags), "foo");
91 Assert.AreEqual (e, null, "Bad");
95 public void EnumToString ()
97 Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestEnum), 1),
99 Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestEnum), 25),
104 public void EnumToStringFlags ()
106 Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestFlags), 0),
108 Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestFlags), 3),
113 [ExpectedException (typeof (ArgumentException))]
114 public void EnumToStringWrongBaseType ()
116 PropertyConverter.EnumToString (typeof (TestEnum), "foo");
119 public void TestObjectFromString ()
121 Assert.AreEqual (PropertyConverter.ObjectFromString (
122 typeof (string), null, "value"),
123 "value", "String Type");
124 MemberInfo mi = this.GetType ().GetProperty ("AllowedConverterProperty");
125 Assert.AreEqual (PropertyConverter.ObjectFromString (
126 typeof (int), mi, "ConverterValue"),
127 "ConverterValue", "Converter Value");
131 [ExpectedException (typeof (NullReferenceException))]
132 public void TestObjectFromStringNullRef ()
134 PropertyConverter.ObjectFromString (typeof (int), // can't be string
139 [ExpectedException (typeof (HttpException))]
140 public void TestObjectFromStringCantConvert ()
142 MemberInfo mi = this.GetType ().GetProperty ("NotAllowedConverterProperty");
143 PropertyConverter.ObjectFromString (typeof (int), // can't be string
147 [TypeConverter (typeof (AllowedMagicTypeConverter))]
148 public string AllowedConverterProperty {
149 get { return "AllowedConverterProperty"; }
152 [TypeConverter (typeof (NotAllowedMagicTypeConverter))]
153 public string NotAllowedConverterProperty {
154 get { return "NotAllowedConverterProperty"; }
157 public class AllowedMagicTypeConverter : MagicTypeConverter {
159 public AllowedMagicTypeConverter () : base (true)
164 public class NotAllowedMagicTypeConverter : MagicTypeConverter {
166 public NotAllowedMagicTypeConverter () : base (false)
171 public abstract class MagicTypeConverter : TypeConverter {
175 public MagicTypeConverter (bool allow)
180 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
185 public override object ConvertFrom (ITypeDescriptorContext context,
186 CultureInfo culture, object value)
188 return "ConverterValue";