1 //
2 // System.Configuration.GenericEnumConverterTest.cs - Unit tests
3 // for System.Configuration.GenericEnumConverter.
4 //
5 // Author:
6 //	Chris Toshok  <toshok@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 
30 
31 using System;
32 using System.Configuration;
33 using NUnit.Framework;
34 
35 namespace MonoTests.System.Configuration {
36 	[TestFixture]
37 	public class GenericEnumConverterTest
38 	{
39 		enum FooEnum {
40 			Foo = 1,
41 			Bar = 2
42 		}
43 
44 		[Test]
45 		[ExpectedException (typeof (ArgumentNullException))]
Ctor_Null()46 		public void Ctor_Null ()
47 		{
48 			GenericEnumConverter cv = new GenericEnumConverter (null);
49 		}
50 
51 		[Test]
Ctor_TypeError()52 		public void Ctor_TypeError ()
53 		{
54 			GenericEnumConverter cv = new GenericEnumConverter (typeof (object));
55 		}
56 
57 		[Test]
CanConvertFrom()58 		public void CanConvertFrom ()
59 		{
60 			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
61 
62 			Assert.IsTrue (cv.CanConvertFrom (null, typeof (string)), "A1");
63 			Assert.IsFalse (cv.CanConvertFrom (null, typeof (TimeSpan)), "A2");
64 			Assert.IsFalse (cv.CanConvertFrom (null, typeof (int)), "A3");
65 			Assert.IsFalse (cv.CanConvertFrom (null, typeof (object)), "A4");
66 		}
67 
68 		[Test]
CanConvertTo()69 		public void CanConvertTo ()
70 		{
71 			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
72 
73 			Assert.IsTrue (cv.CanConvertTo (null, typeof (string)), "A1");
74 			Assert.IsFalse (cv.CanConvertTo (null, typeof (TimeSpan)), "A2");
75 			Assert.IsFalse (cv.CanConvertTo (null, typeof (int)), "A3");
76 			Assert.IsFalse (cv.CanConvertTo (null, typeof (object)), "A4");
77 		}
78 
79 		[Test]
ConvertFrom()80 		public void ConvertFrom ()
81 		{
82 			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
83 			Assert.AreEqual (FooEnum.Foo, cv.ConvertFrom (null, null, "Foo"), "A1");
84 			Assert.AreEqual (FooEnum.Bar, cv.ConvertFrom (null, null, "Bar"), "A2");
85 		}
86 
87 		[Test]
88 		[ExpectedException (typeof (ArgumentException))]
ConvertFrom_Case()89 		public void ConvertFrom_Case ()
90 		{
91 			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
92 			Assert.AreEqual (FooEnum.Foo, cv.ConvertFrom (null, null, "foo"), "A1");
93 		}
94 
95 		[Test]
96 		[ExpectedException (typeof (ArgumentException))]
ConvertFrom_InvalidString()97 		public void ConvertFrom_InvalidString ()
98 		{
99 			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
100 			object o;
101 
102 			o = cv.ConvertFrom (null, null, "baz");
103 			Assert.IsNull (o, "A1");
104 		}
105 
106 		[Test]
107 		[ExpectedException (typeof (ArgumentException))]
ConvertFrom_Null()108 		public void ConvertFrom_Null ()
109 		{
110 			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
111 			object o;
112 
113 			o = cv.ConvertFrom (null, null, null);
114 			Assert.IsNull (o, "A1");
115 		}
116 
117 		[Test]
ConvertTo()118 		public void ConvertTo ()
119 		{
120 			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
121 
122 			Assert.AreEqual ("Foo", cv.ConvertTo (null, null, FooEnum.Foo, typeof (string)), "A1");
123 			Assert.AreEqual ("Bar", cv.ConvertTo (null, null, FooEnum.Bar, typeof (string)), "A2");
124 		}
125 
126 		[Test]
127 		[ExpectedException (typeof (NullReferenceException))]
ConvertTo_NullError()128 		public void ConvertTo_NullError ()
129 		{
130 			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
131 
132 			Assert.AreEqual ("", cv.ConvertTo (null, null, null, typeof (string)), "A1");
133 		}
134 
135 		[Test]
ConvertTo_TypeError1()136 		public void ConvertTo_TypeError1 ()
137 		{
138 			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
139 
140 			Assert.AreEqual ("0", cv.ConvertTo (null, null, 0, typeof (string)), "A1");
141 		}
142 
143 		[Test]
ConvertTo_TypeError2()144 		public void ConvertTo_TypeError2 ()
145 		{
146 			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
147 
148 			Assert.AreEqual ("", cv.ConvertTo (null, null, "", typeof (string)), "A1");
149 		}
150 	}
151 }
152 
153