* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI / PropertyConverterTest.cs
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 (jackson@ximian.com)
25 //
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
27 //
28
29
30 using System;
31 using System.Web;
32 using System.Web.UI;
33 using System.Reflection;
34 using System.Globalization;
35 using System.ComponentModel;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Web.UI {
40
41         enum TestEnum {
42                 Default,
43                 Normal,
44                 LowerCase,
45                 UpperCase,
46         }
47
48         [Flags]
49         enum TestFlags {
50                 A,
51                 B,
52                 C
53         }
54
55         [TestFixture]
56         public class PropertyConverterTest {
57
58                 [Test]
59                 public void EnumFromString ()
60                 {
61                         object e = TestEnum.Default;
62
63                         e = PropertyConverter.EnumFromString (typeof (TestEnum), "Normal");
64                         Assert.AreEqual (TestEnum.Normal, e, "Normal");
65
66                         e = PropertyConverter.EnumFromString (typeof (TestEnum), "lowercase");
67                         Assert.AreEqual (TestEnum.LowerCase, e, "Lower Case");
68
69                         e = PropertyConverter.EnumFromString (typeof (TestEnum), "UPPERCASE");
70                         Assert.AreEqual (TestEnum.UpperCase, e, "Upper Case");
71
72                         e = PropertyConverter.EnumFromString (typeof (TestEnum), "DoesntExist");
73                         Assert.AreEqual (null, e, "Doesn't Exist");
74
75                         e = PropertyConverter.EnumFromString (typeof (TestEnum), "TestEnum.Normal");
76                         Assert.AreEqual (null, e, "Full Name");
77                 }
78
79                 [Test]
80                 public void TestFromStringFlags ()
81                 {
82                         object e = TestEnum.Default;
83
84                         e = PropertyConverter.EnumFromString (typeof (TestFlags), "A");
85                         Assert.AreEqual (e, TestFlags.A, "Normal");
86
87                         e = PropertyConverter.EnumFromString (typeof (TestFlags), "A, B");
88                         Assert.AreEqual (e, TestFlags.A | TestFlags.B, "Multiple");
89
90                         e = PropertyConverter.EnumFromString (typeof (TestFlags), "foo");
91                         Assert.AreEqual (e, null, "Bad");
92                 }
93
94                 [Test]
95                 public void EnumToString ()
96                 {
97                         Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestEnum), 1),
98                                         "Normal", "Normal");
99                         Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestEnum), 25),
100                                         "25", "Decimal");
101                 }
102
103                 [Test]
104                 public void EnumToStringFlags ()
105                 {
106                         Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestFlags), 0),
107                                         "A", "A");
108                         Assert.AreEqual (PropertyConverter.EnumToString (typeof (TestFlags), 3),
109                                         "B, C", "Multiple");
110                 }
111
112                 [Test]
113                 [ExpectedException (typeof (ArgumentException))]
114                 public void EnumToStringWrongBaseType ()
115                 {
116                         PropertyConverter.EnumToString (typeof (TestEnum), "foo");
117                 }
118
119                 public void TestObjectFromString ()
120                 {
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");
128                 }
129
130                 [Test]
131                 [ExpectedException (typeof (NullReferenceException))]
132                 public void TestObjectFromStringNullRef ()
133                 {
134                         PropertyConverter.ObjectFromString (typeof (int), // can't be string
135                                         null, "foobar");
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (HttpException))]
140                 public void TestObjectFromStringCantConvert ()
141                 {
142                         MemberInfo mi = this.GetType ().GetProperty ("NotAllowedConverterProperty");
143                         PropertyConverter.ObjectFromString (typeof (int), // can't be string
144                                         mi, "foobar");
145                 }
146
147                 [TypeConverter (typeof (AllowedMagicTypeConverter))]
148                 public string AllowedConverterProperty {
149                         get { return "AllowedConverterProperty"; }
150                 }
151
152                 [TypeConverter (typeof (NotAllowedMagicTypeConverter))]
153                 public string NotAllowedConverterProperty {
154                         get { return "NotAllowedConverterProperty"; }
155                 }
156
157                 public class AllowedMagicTypeConverter : MagicTypeConverter {
158
159                         public AllowedMagicTypeConverter () : base (true)
160                         {
161                         }
162                 }
163
164                 public class NotAllowedMagicTypeConverter : MagicTypeConverter {
165
166                         public NotAllowedMagicTypeConverter () : base (false)
167                         {
168                         }
169                 }
170
171                 public abstract class MagicTypeConverter : TypeConverter {
172
173                         private bool allow;
174
175                         public MagicTypeConverter (bool allow)
176                         {
177                                 this.allow = allow;
178                         }
179
180                         public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
181                         {
182                                 return allow;
183                         }
184
185                         public override object ConvertFrom (ITypeDescriptorContext context,
186                                         CultureInfo culture, object value)
187                         {
188                                 return "ConverterValue";
189                         }
190                 }
191         }
192 }