cf7c641f9fede6ac070dcf5984f418a1b3e2c80e
[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 #if NET_4_0
39         [TestFixture]
40         public class EnumDataTypeAttributeTest
41         {
42                 [Test]
43                 public void Constructor ()
44                 {
45                         var attr = new EnumDataTypeAttribute (typeof (string));
46
47                         Assert.AreEqual (DataType.Custom, attr.DataType, "#A1-1");
48                         Assert.AreEqual (typeof (string), attr.EnumType, "#A1-2");
49
50                         attr = new EnumDataTypeAttribute (typeof (TestEnum));
51                         Assert.AreEqual (DataType.Custom, attr.DataType, "#B1-1");
52                         Assert.AreEqual (typeof (TestEnum), attr.EnumType, "#B1-2");
53
54                         attr = new EnumDataTypeAttribute (null);
55                         Assert.AreEqual (DataType.Custom, attr.DataType, "#C1-1");
56                         Assert.AreEqual (null, attr.EnumType, "#C1-2");
57                 }
58
59                 [Test]
60                 public void IsValid ()
61                 {
62                         var attr = new EnumDataTypeAttribute (typeof (string));
63                         
64                         try {
65                                 attr.IsValid (null);
66                                 Assert.Fail ("#A1-1");
67                         } catch (InvalidOperationException) {
68                                 // success
69                         }
70
71                         try {
72                                 attr.IsValid ("stuff");
73                                 Assert.Fail ("#A1-2");
74                         } catch (InvalidOperationException) {
75                                 // success
76                         }
77
78                         attr = new EnumDataTypeAttribute (typeof (TestEnum));
79                         Assert.IsTrue (attr.IsValid (null), "#A2-1");
80                         Assert.IsTrue (attr.IsValid (1), "#A2-2");
81                         Assert.IsFalse (attr.IsValid (0), "#A2-3");
82                         Assert.IsTrue (attr.IsValid (TestEnum.Two), "#A2-4");
83                         Assert.IsFalse (attr.IsValid (AnotherEnum.Five), "#A2-5");
84                         Assert.IsFalse (attr.IsValid ("stuff"), "#A2-5");
85
86                         AnotherEnum val = AnotherEnum.Six;
87                         Assert.IsFalse (attr.IsValid (val), "#A2-6");
88
89                         Assert.IsTrue (attr.IsValid (String.Empty), "#A2-7");
90                         Assert.IsTrue (attr.IsValid ("Three"), "#A2-8");
91                         Assert.IsFalse (attr.IsValid ("Four"), "#A2-9");
92                         Assert.IsFalse (attr.IsValid (true), "#A2-10");
93                         Assert.IsFalse (attr.IsValid (' '), "#A2-11");
94                         Assert.IsFalse (attr.IsValid (0.12F), "#A2-12");
95                         Assert.IsTrue (attr.IsValid ((short) 1), "#A2-13");
96                         Assert.IsFalse (attr.IsValid (12.3M), "#A2-14");
97                         Assert.IsFalse (attr.IsValid (12.3D), "#A2-15");
98                         Assert.IsTrue (attr.IsValid ((long) 1), "#A2-16");
99
100                         attr = new EnumDataTypeAttribute (typeof (AnotherEnum));
101                         Assert.IsTrue (attr.IsValid (null), "#A3-1");
102                         Assert.IsTrue (attr.IsValid (4), "#A3-2");
103                         Assert.IsFalse (attr.IsValid (0), "#A3-3");
104                         Assert.IsTrue (attr.IsValid (AnotherEnum.Five), "#A3-4");
105                         Assert.IsFalse (attr.IsValid (TestEnum.One), "#A3-5");
106                         Assert.IsFalse (attr.IsValid ("stuff"), "#A3-5");
107
108                         val = AnotherEnum.Four;
109                         Assert.IsTrue (attr.IsValid (val), "#A3-6");
110
111                         Assert.IsTrue (attr.IsValid (String.Empty), "#A3-7");
112                         Assert.IsTrue (attr.IsValid ("Four"), "#A3-8");
113                         Assert.IsFalse (attr.IsValid ("Three"), "#A3-9");
114                         Assert.IsTrue (attr.IsValid (12), "#A3-10");
115                         Assert.IsTrue (attr.IsValid ("Five, Six"), "#A3-11");
116                         Assert.IsFalse (attr.IsValid (true), "#A3-12");
117                         Assert.IsFalse (attr.IsValid (' '), "#A3-13");
118                         Assert.IsFalse (attr.IsValid (0.12), "#A3-14");
119                 }
120         }
121
122         enum TestEnum
123         {
124                 One = 1,
125                 Two,
126                 Three
127         }
128
129         [Flags]
130         enum AnotherEnum
131         {
132                 Four = 4,
133                 Five = 8,
134                 Six = 16
135         }
136 #endif
137 }