[bcl] Remove NET_4_0 defines from class libs
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / Test / System.ComponentModel.DataAnnotations / EnumDataTypeAttributeTest.cs
1 //
2 // EnumDataTypeAttributeTest.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.ComponentModel.DataAnnotations;
31 using System.ComponentModel.Design;
32 using System.Text;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.ComponentModel.DataAnnotations
37 {
38         [TestFixture]
39         public class EnumDataTypeAttributeTest
40         {
41                 [Test]
42                 public void Constructor ()
43                 {
44                         var attr = new EnumDataTypeAttribute (typeof (string));
45
46                         Assert.AreEqual (DataType.Custom, attr.DataType, "#A1-1");
47                         Assert.AreEqual (typeof (string), attr.EnumType, "#A1-2");
48
49                         attr = new EnumDataTypeAttribute (typeof (TestEnum));
50                         Assert.AreEqual (DataType.Custom, attr.DataType, "#B1-1");
51                         Assert.AreEqual (typeof (TestEnum), attr.EnumType, "#B1-2");
52
53                         attr = new EnumDataTypeAttribute (null);
54                         Assert.AreEqual (DataType.Custom, attr.DataType, "#C1-1");
55                         Assert.AreEqual (null, attr.EnumType, "#C1-2");
56                 }
57
58                 [Test]
59                 public void IsValid ()
60                 {
61                         var attr = new EnumDataTypeAttribute (typeof (string));
62                         
63                         try {
64                                 attr.IsValid (null);
65                                 Assert.Fail ("#A1-1");
66                         } catch (InvalidOperationException) {
67                                 // success
68                         }
69
70                         try {
71                                 attr.IsValid ("stuff");
72                                 Assert.Fail ("#A1-2");
73                         } catch (InvalidOperationException) {
74                                 // success
75                         }
76
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");
84
85                         AnotherEnum val = AnotherEnum.Six;
86                         Assert.IsFalse (attr.IsValid (val), "#A2-6");
87
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");
98
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");
106
107                         val = AnotherEnum.Four;
108                         Assert.IsTrue (attr.IsValid (val), "#A3-6");
109
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");
118                 }
119         }
120
121         enum TestEnum
122         {
123                 One = 1,
124                 Two,
125                 Three
126         }
127
128         [Flags]
129         enum AnotherEnum
130         {
131                 Four = 4,
132                 Five = 8,
133                 Six = 16
134         }
135 }