New test.
[mono.git] / mcs / class / System / Test / System.ComponentModel / DisplayNameAttributeTests.cs
1 //
2 // System.ComponentModel.DisplayNameAttributeTests test cases
3 //
4 // Authors:
5 //      Marek Habersack (grendello@gmail.com)
6 //
7 // (c) 2006 Marek Habersack
8 //
9 #if NET_2_0
10 using NUnit.Framework;
11 using System;
12 using System.ComponentModel;
13 using System.Reflection;
14
15 namespace MonoTests.System.ComponentModel 
16 {
17         [DisplayName ()]
18         class TestClass1
19         {
20                 [DisplayName ()]
21                 public string Property1
22                 {
23                         get { return String.Empty; }
24                 }
25
26                 [DisplayName ()]
27                 public string Method1 ()
28                 {
29                         return String.Empty;
30                 }
31         }
32
33         [DisplayName ("TestClassTwo")]
34         class TestClass2
35         {
36                 [DisplayName ("PropertyTwo")]
37                 public string Property2
38                 {
39                         get { return String.Empty; }
40                 }
41
42                 [DisplayName ("MethodTwo")]
43                 public string Method2 ()
44                 {
45                         return String.Empty;
46                 }
47         }
48
49         [DisplayName (null)]
50         class TestClass3
51         {
52                 [DisplayName (null)]
53                 public string Property3
54                 {
55                         get { return String.Empty; }
56                 }
57
58                 [DisplayName (null)]
59                 public string Method3 ()
60                 {
61                         return String.Empty;
62                 }
63         }
64         
65         [TestFixture]
66         public class DisplayNameAttributeTests
67         {
68                 private TestClass1 tc1;
69                 private TestClass2 tc2;
70                 private TestClass3 tc3;
71
72                 DisplayNameAttribute GetDisplayNameAttribute (object[] attrs)
73                 {
74                         DisplayNameAttribute dn = null;
75                         foreach (object attr in attrs) {
76                                 dn = attr as DisplayNameAttribute;
77                                 if (dn != null)
78                                         break;
79                         }
80                         return dn;
81                 }
82                 
83                 DisplayNameAttribute GetAttribute (Type type)
84                 {
85                         return GetDisplayNameAttribute (type.GetCustomAttributes (false));
86                 }
87
88                 DisplayNameAttribute GetAttribute (Type type, string memberName, MemberTypes expectedType)
89                 {
90                         MemberInfo[] mi = type.GetMember (memberName, expectedType, BindingFlags.Instance | BindingFlags.Public);
91                         return GetDisplayNameAttribute (mi[0].GetCustomAttributes (false));
92                 }
93
94                 [SetUp]
95                 public void FixtureSetUp ()
96                 {
97                         tc1 = new TestClass1 ();
98                         tc2 = new TestClass2 ();
99                         tc3 = new TestClass3 ();
100                 }
101                 
102                 [Test]
103                 public void TestEmptyName ()
104                 {
105                         Type tc1t = tc1.GetType ();
106                         DisplayNameAttribute dn = GetAttribute (tc1t);
107                         Assert.IsNotNull (dn, "#1_1");
108                         Assert.IsFalse (dn.DisplayName == null, "#1_2");
109                         Assert.AreEqual (dn.DisplayName, "", "#1_3");
110
111                         dn = GetAttribute (tc1t, "Property1", MemberTypes.Property);
112                         Assert.IsNotNull (dn, "#2_1");
113                         Assert.IsFalse (dn.DisplayName == null, "#2_2");
114                         Assert.AreEqual (dn.DisplayName, "", "#2_3");
115                         
116                         dn = GetAttribute (tc1t, "Method1", MemberTypes.Method);
117                         Assert.IsNotNull (dn, "#3_1");
118                         Assert.IsFalse (dn.DisplayName == null, "#3_2");
119                         Assert.AreEqual (dn.DisplayName, "", "#3_3");
120                 }
121
122                 [Test]
123                 public void TestNonEmptyName ()
124                 {
125                         Type tc2t = tc2.GetType ();
126                         DisplayNameAttribute dn = GetAttribute (tc2t);
127                         Assert.IsNotNull (dn, "#1_1");
128                         Assert.IsFalse (dn.DisplayName == null, "#1_2");
129                         Assert.AreEqual (dn.DisplayName, "TestClassTwo", "#1_3");
130
131                         dn = GetAttribute (tc2t, "Property2", MemberTypes.Property);
132                         Assert.IsNotNull (dn, "#2_1");
133                         Assert.IsFalse (dn.DisplayName == null, "#2_2");
134                         Assert.AreEqual (dn.DisplayName, "PropertyTwo", "#2_3");
135                         
136                         dn = GetAttribute (tc2t, "Method2", MemberTypes.Method);
137                         Assert.IsNotNull (dn, "#3_1");
138                         Assert.IsFalse (dn.DisplayName == null, "#3_2");
139                         Assert.AreEqual (dn.DisplayName, "MethodTwo", "#3_3");
140                 }
141
142                 [Test]
143                 public void TestNullName ()
144                 {
145                         Type tc3t = tc3.GetType ();
146                         DisplayNameAttribute dn = GetAttribute (tc3t);
147                         Assert.IsNotNull (dn, "#1_1");
148                         Assert.IsFalse (dn.DisplayName == null, "#1_2");
149                         Assert.AreEqual (dn.DisplayName, "", "#1_3");
150                         
151                         dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
152                         Assert.IsNotNull (dn, "#2_1");
153                         Assert.IsFalse (dn.DisplayName == null, "#2_2");
154                         Assert.AreEqual (dn.DisplayName, "", "#2_3");
155                         
156                         dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
157                         Assert.IsNotNull (dn, "#3_1");
158                         Assert.IsFalse (dn.DisplayName == null, "#3_2");
159                         Assert.AreEqual (dn.DisplayName, "", "#3_3");
160                 }
161                 
162                 [Test]
163                 public void TestDefaultAttribute ()
164                 {
165                         Type tc1t = tc1.GetType ();
166                         DisplayNameAttribute dn = GetAttribute (tc1t);
167                         Assert.IsNotNull (dn, "#1_1");
168                         Assert.IsTrue (dn.IsDefaultAttribute (), "#1_2");
169
170                         dn = GetAttribute (tc1t, "Property1", MemberTypes.Property);
171                         Assert.IsNotNull (dn, "#1_3");
172                         Assert.IsTrue (dn.IsDefaultAttribute (), "#1_4");
173
174                         dn = GetAttribute (tc1t, "Method1", MemberTypes.Method);
175                         Assert.IsNotNull (dn, "#1_5");
176                         Assert.IsTrue (dn.IsDefaultAttribute (), "#1_6");
177
178                         Type tc2t = tc2.GetType ();
179                         dn = GetAttribute (tc2t);
180                         Assert.IsNotNull (dn, "#2_1");
181                         Assert.IsFalse (dn.IsDefaultAttribute (), "#2_2");
182
183                         dn = GetAttribute (tc2t, "Property2", MemberTypes.Property);
184                         Assert.IsNotNull (dn, "#2_3");
185                         Assert.IsFalse (dn.IsDefaultAttribute (), "#2_4");
186
187                         dn = GetAttribute (tc2t, "Method2", MemberTypes.Method);
188                         Assert.IsNotNull (dn, "#2_5");
189                         Assert.IsFalse (dn.IsDefaultAttribute (), "#2_6");
190
191                         Type tc3t = tc3.GetType ();
192                         dn = GetAttribute (tc3t);
193                         Assert.IsNotNull (dn, "#3_1");
194                         Assert.IsTrue (dn.IsDefaultAttribute (), "#3_2");
195
196                         dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
197                         Assert.IsNotNull (dn, "#3_3");
198                         Assert.IsTrue (dn.IsDefaultAttribute (), "#3_4");
199
200                         dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
201                         Assert.IsNotNull (dn, "#3_5");
202                         Assert.IsTrue (dn.IsDefaultAttribute (), "#3_6");
203                 }
204         }
205 }
206 #endif
207