2008-04-16 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System / Test / System.ComponentModel / DisplayNameAttributeTests.cs
1 //
2 // System.ComponentModel.DisplayNameAttribute test cases
3 //
4 // Authors:
5 //      Marek Habersack (grendello@gmail.com)
6 //      Gert Driesen (drieseng@users.sourceforge.net
7 //
8 // (c) 2006 Marek Habersack
9 //
10
11 #if NET_2_0
12 using System;
13 using System.ComponentModel;
14 using System.Reflection;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System.ComponentModel 
19 {
20         [DisplayName ()]
21         class TestClass1
22         {
23                 [DisplayName ()]
24                 public string Property1
25                 {
26                         get { return String.Empty; }
27                 }
28
29                 [DisplayName ()]
30                 public string Method1 ()
31                 {
32                         return String.Empty;
33                 }
34         }
35
36         [DisplayName ("TestClassTwo")]
37         class TestClass2
38         {
39                 [DisplayName ("PropertyTwo")]
40                 public string Property2
41                 {
42                         get { return String.Empty; }
43                 }
44
45                 [DisplayName ("MethodTwo")]
46                 public string Method2 ()
47                 {
48                         return String.Empty;
49                 }
50         }
51
52         [DisplayName (null)]
53         class TestClass3
54         {
55                 [DisplayName (null)]
56                 public string Property3
57                 {
58                         get { return String.Empty; }
59                 }
60
61                 [DisplayName (null)]
62                 public string Method3 ()
63                 {
64                         return String.Empty;
65                 }
66         }
67         
68         [TestFixture]
69         public class DisplayNameAttributeTests
70         {
71                 private TestClass1 tc1;
72                 private TestClass2 tc2;
73                 private TestClass3 tc3;
74
75                 DisplayNameAttribute GetDisplayNameAttribute (object[] attrs)
76                 {
77                         DisplayNameAttribute dn = null;
78                         foreach (object attr in attrs) {
79                                 dn = attr as DisplayNameAttribute;
80                                 if (dn != null)
81                                         break;
82                         }
83                         return dn;
84                 }
85                 
86                 DisplayNameAttribute GetAttribute (Type type)
87                 {
88                         return GetDisplayNameAttribute (type.GetCustomAttributes (false));
89                 }
90
91                 DisplayNameAttribute GetAttribute (Type type, string memberName, MemberTypes expectedType)
92                 {
93                         MemberInfo[] mi = type.GetMember (memberName, expectedType, BindingFlags.Instance | BindingFlags.Public);
94                         return GetDisplayNameAttribute (mi[0].GetCustomAttributes (false));
95                 }
96
97                 [SetUp]
98                 public void FixtureSetUp ()
99                 {
100                         tc1 = new TestClass1 ();
101                         tc2 = new TestClass2 ();
102                         tc3 = new TestClass3 ();
103                 }
104                 
105                 [Test]
106                 public void Constructor0 ()
107                 {
108                         DisplayNameAttribute dn = new DisplayNameAttribute ();
109                         Assert.IsNotNull (dn.DisplayName, "#1");
110                         Assert.AreEqual (string.Empty, dn.DisplayName, "#2");
111                         Assert.IsTrue (dn.IsDefaultAttribute (), "#3");
112                 }
113
114                 [Test]
115                 public void Constructor1 ()
116                 {
117                         DisplayNameAttribute dn = new DisplayNameAttribute (string.Empty);
118                         Assert.IsNotNull (dn.DisplayName, "#A1");
119                         Assert.AreEqual (string.Empty, dn.DisplayName, "#A2");
120                         Assert.IsTrue (dn.IsDefaultAttribute (), "#A3");
121
122                         dn = new DisplayNameAttribute (null);
123                         Assert.IsNull (dn.DisplayName, "#B1");
124                         Assert.IsFalse (dn.IsDefaultAttribute (), "#B2");
125
126                         dn = new DisplayNameAttribute ("category");
127                         Assert.IsNotNull (dn.DisplayName, "#C1");
128                         Assert.AreEqual ("category", dn.DisplayName, "#C2");
129                         Assert.IsFalse (dn.IsDefaultAttribute (), "#C3");
130                 }
131
132                 [Test]
133                 public void Default ()
134                 {
135                         DisplayNameAttribute dn = DisplayNameAttribute.Default;
136                         Assert.IsNotNull (dn.DisplayName, "#1");
137                         Assert.AreEqual (string.Empty, dn.DisplayName, "#2");
138                         Assert.IsTrue (dn.IsDefaultAttribute (), "#3");
139                 }
140
141                 [Test]
142                 public void Equals ()
143                 {
144                         DisplayNameAttribute dn = new DisplayNameAttribute ();
145                         Assert.IsTrue (dn.Equals (DisplayNameAttribute.Default), "#A1");
146                         Assert.IsTrue (dn.Equals (new DisplayNameAttribute (string.Empty)), "#A2");
147                         Assert.IsFalse (dn.Equals (new DisplayNameAttribute ("category")), "#A3");
148                         Assert.IsFalse (dn.Equals (new DisplayNameAttribute (null)), "#A4");
149                         Assert.IsFalse (dn.Equals (null), "#A5");
150                         Assert.IsTrue (dn.Equals (dn), "#A6");
151                         Assert.IsFalse (dn.Equals (55), "#A7");
152
153                         dn = new DisplayNameAttribute ("category");
154                         Assert.IsFalse (dn.Equals (DisplayNameAttribute.Default), "#B1");
155                         Assert.IsFalse (dn.Equals (new DisplayNameAttribute (string.Empty)), "#B2");
156                         Assert.IsTrue (dn.Equals (new DisplayNameAttribute ("category")), "#B3");
157                         Assert.IsFalse (dn.Equals (new DisplayNameAttribute (null)), "#B4");
158                         Assert.IsFalse (dn.Equals (null), "#B5");
159                         Assert.IsTrue (dn.Equals (dn), "#B6");
160                         Assert.IsFalse (dn.Equals (55), "#B7");
161                 }
162
163                 [Test]
164                 public void GetHashCodeTest ()
165                 {
166                         DisplayNameAttribute dn = new DisplayNameAttribute ();
167                         Assert.AreEqual (string.Empty.GetHashCode (), dn.GetHashCode (), "#A1");
168                         dn = new DisplayNameAttribute ("A");
169                         Assert.AreEqual ("A".GetHashCode (), dn.GetHashCode (), "#A2");
170
171                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=288534
172                         dn = new DisplayNameAttribute (null);
173                         try {
174                                 dn.GetHashCode ();
175                                 Assert.Fail ("#B1");
176                         } catch (NullReferenceException) {
177                         }
178                 }
179
180                 [Test]
181                 public void TestEmptyName ()
182                 {
183                         Type tc1t = tc1.GetType ();
184                         DisplayNameAttribute dn = GetAttribute (tc1t);
185                         Assert.IsNotNull (dn, "#1_1");
186                         Assert.IsFalse (dn.DisplayName == null, "#1_2");
187                         Assert.AreEqual (dn.DisplayName, "", "#1_3");
188
189                         dn = GetAttribute (tc1t, "Property1", MemberTypes.Property);
190                         Assert.IsNotNull (dn, "#2_1");
191                         Assert.IsFalse (dn.DisplayName == null, "#2_2");
192                         Assert.AreEqual (dn.DisplayName, "", "#2_3");
193                         
194                         dn = GetAttribute (tc1t, "Method1", MemberTypes.Method);
195                         Assert.IsNotNull (dn, "#3_1");
196                         Assert.IsFalse (dn.DisplayName == null, "#3_2");
197                         Assert.AreEqual (dn.DisplayName, "", "#3_3");
198                 }
199
200                 [Test]
201                 public void TestNonEmptyName ()
202                 {
203                         Type tc2t = tc2.GetType ();
204                         DisplayNameAttribute dn = GetAttribute (tc2t);
205                         Assert.IsNotNull (dn, "#1_1");
206                         Assert.IsFalse (dn.DisplayName == null, "#1_2");
207                         Assert.AreEqual (dn.DisplayName, "TestClassTwo", "#1_3");
208
209                         dn = GetAttribute (tc2t, "Property2", MemberTypes.Property);
210                         Assert.IsNotNull (dn, "#2_1");
211                         Assert.IsFalse (dn.DisplayName == null, "#2_2");
212                         Assert.AreEqual (dn.DisplayName, "PropertyTwo", "#2_3");
213                         
214                         dn = GetAttribute (tc2t, "Method2", MemberTypes.Method);
215                         Assert.IsNotNull (dn, "#3_1");
216                         Assert.IsFalse (dn.DisplayName == null, "#3_2");
217                         Assert.AreEqual (dn.DisplayName, "MethodTwo", "#3_3");
218                 }
219
220                 [Test]
221                 public void TestNullName ()
222                 {
223                         Type tc3t = tc3.GetType ();
224                         DisplayNameAttribute dn = GetAttribute (tc3t);
225                         Assert.IsNotNull (dn, "#1_1");
226                         Assert.IsNull (dn.DisplayName, "#1_2");
227                         
228                         dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
229                         Assert.IsNotNull (dn, "#2_1");
230                         Assert.IsNull (dn.DisplayName, "#2_2");
231                         
232                         dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
233                         Assert.IsNotNull (dn, "#3_1");
234                         Assert.IsNull (dn.DisplayName, "#3_2");
235                 }
236                 
237                 [Test]
238                 public void TestDefaultAttribute ()
239                 {
240                         Type tc1t = tc1.GetType ();
241                         DisplayNameAttribute dn = GetAttribute (tc1t);
242                         Assert.IsNotNull (dn, "#1_1");
243                         Assert.IsTrue (dn.IsDefaultAttribute (), "#1_2");
244
245                         dn = GetAttribute (tc1t, "Property1", MemberTypes.Property);
246                         Assert.IsNotNull (dn, "#1_3");
247                         Assert.IsTrue (dn.IsDefaultAttribute (), "#1_4");
248
249                         dn = GetAttribute (tc1t, "Method1", MemberTypes.Method);
250                         Assert.IsNotNull (dn, "#1_5");
251                         Assert.IsTrue (dn.IsDefaultAttribute (), "#1_6");
252
253                         Type tc2t = tc2.GetType ();
254                         dn = GetAttribute (tc2t);
255                         Assert.IsNotNull (dn, "#2_1");
256                         Assert.IsFalse (dn.IsDefaultAttribute (), "#2_2");
257
258                         dn = GetAttribute (tc2t, "Property2", MemberTypes.Property);
259                         Assert.IsNotNull (dn, "#2_3");
260                         Assert.IsFalse (dn.IsDefaultAttribute (), "#2_4");
261
262                         dn = GetAttribute (tc2t, "Method2", MemberTypes.Method);
263                         Assert.IsNotNull (dn, "#2_5");
264                         Assert.IsFalse (dn.IsDefaultAttribute (), "#2_6");
265
266                         Type tc3t = tc3.GetType ();
267                         dn = GetAttribute (tc3t);
268                         Assert.IsNotNull (dn, "#3_1");
269                         Assert.IsFalse (dn.IsDefaultAttribute (), "#3_2");
270
271                         dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
272                         Assert.IsNotNull (dn, "#3_3");
273                         Assert.IsFalse (dn.IsDefaultAttribute (), "#3_4");
274
275                         dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
276                         Assert.IsNotNull (dn, "#3_5");
277                         Assert.IsFalse (dn.IsDefaultAttribute (), "#3_6");
278                 }
279         }
280 }
281 #endif