2 // EnumDataTypeAttributeTest.cs
5 // Marek Habersack <mhabersack@novell.com>
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.ComponentModel.DataAnnotations;
31 using System.ComponentModel.Design;
34 using NUnit.Framework;
36 namespace MonoTests.System.ComponentModel.DataAnnotations
39 public class EnumDataTypeAttributeTest
42 public void Constructor ()
44 var attr = new EnumDataTypeAttribute (typeof (string));
46 Assert.AreEqual (DataType.Custom, attr.DataType, "#A1-1");
47 Assert.AreEqual (typeof (string), attr.EnumType, "#A1-2");
49 attr = new EnumDataTypeAttribute (typeof (TestEnum));
50 Assert.AreEqual (DataType.Custom, attr.DataType, "#B1-1");
51 Assert.AreEqual (typeof (TestEnum), attr.EnumType, "#B1-2");
53 attr = new EnumDataTypeAttribute (null);
54 Assert.AreEqual (DataType.Custom, attr.DataType, "#C1-1");
55 Assert.AreEqual (null, attr.EnumType, "#C1-2");
59 public void IsValid ()
61 var attr = new EnumDataTypeAttribute (typeof (string));
65 Assert.Fail ("#A1-1");
66 } catch (InvalidOperationException) {
71 attr.IsValid ("stuff");
72 Assert.Fail ("#A1-2");
73 } catch (InvalidOperationException) {
77 attr = new EnumDataTypeAttribute (typeof (TestEnum));
78 Assert.IsTrue (attr.IsValid (null), "#A2-1");
79 Assert.IsTrue (attr.IsValid (1), "#A2-2");
80 Assert.IsFalse (attr.IsValid (0), "#A2-3");
81 Assert.IsTrue (attr.IsValid (TestEnum.Two), "#A2-4");
82 Assert.IsFalse (attr.IsValid (AnotherEnum.Five), "#A2-5");
83 Assert.IsFalse (attr.IsValid ("stuff"), "#A2-5");
85 AnotherEnum val = AnotherEnum.Six;
86 Assert.IsFalse (attr.IsValid (val), "#A2-6");
88 Assert.IsTrue (attr.IsValid (String.Empty), "#A2-7");
89 Assert.IsTrue (attr.IsValid ("Three"), "#A2-8");
90 Assert.IsFalse (attr.IsValid ("Four"), "#A2-9");
91 Assert.IsFalse (attr.IsValid (true), "#A2-10");
92 Assert.IsFalse (attr.IsValid (' '), "#A2-11");
93 Assert.IsFalse (attr.IsValid (0.12F), "#A2-12");
94 Assert.IsTrue (attr.IsValid ((short) 1), "#A2-13");
95 Assert.IsFalse (attr.IsValid (12.3M), "#A2-14");
96 Assert.IsFalse (attr.IsValid (12.3D), "#A2-15");
97 Assert.IsTrue (attr.IsValid ((long) 1), "#A2-16");
99 attr = new EnumDataTypeAttribute (typeof (AnotherEnum));
100 Assert.IsTrue (attr.IsValid (null), "#A3-1");
101 Assert.IsTrue (attr.IsValid (4), "#A3-2");
102 Assert.IsFalse (attr.IsValid (0), "#A3-3");
103 Assert.IsTrue (attr.IsValid (AnotherEnum.Five), "#A3-4");
104 Assert.IsFalse (attr.IsValid (TestEnum.One), "#A3-5");
105 Assert.IsFalse (attr.IsValid ("stuff"), "#A3-5");
107 val = AnotherEnum.Four;
108 Assert.IsTrue (attr.IsValid (val), "#A3-6");
110 Assert.IsTrue (attr.IsValid (String.Empty), "#A3-7");
111 Assert.IsTrue (attr.IsValid ("Four"), "#A3-8");
112 Assert.IsFalse (attr.IsValid ("Three"), "#A3-9");
113 Assert.IsTrue (attr.IsValid (12), "#A3-10");
114 Assert.IsTrue (attr.IsValid ("Five, Six"), "#A3-11");
115 Assert.IsFalse (attr.IsValid (true), "#A3-12");
116 Assert.IsFalse (attr.IsValid (' '), "#A3-13");
117 Assert.IsFalse (attr.IsValid (0.12), "#A3-14");