2 // System.ComponentModel.DisplayNameAttribute test cases
5 // Marek Habersack (grendello@gmail.com)
6 // Gert Driesen (drieseng@users.sourceforge.net
8 // (c) 2006 Marek Habersack
13 using System.ComponentModel;
14 using System.Reflection;
16 using NUnit.Framework;
18 namespace MonoTests.System.ComponentModel
24 public string Property1
26 get { return String.Empty; }
30 public string Method1 ()
36 [DisplayName ("TestClassTwo")]
39 [DisplayName ("PropertyTwo")]
40 public string Property2
42 get { return String.Empty; }
45 [DisplayName ("MethodTwo")]
46 public string Method2 ()
56 public string Property3
58 get { return String.Empty; }
62 public string Method3 ()
69 public class DisplayNameAttributeTests
71 private TestClass1 tc1;
72 private TestClass2 tc2;
73 private TestClass3 tc3;
75 DisplayNameAttribute GetDisplayNameAttribute (object[] attrs)
77 DisplayNameAttribute dn = null;
78 foreach (object attr in attrs) {
79 dn = attr as DisplayNameAttribute;
86 DisplayNameAttribute GetAttribute (Type type)
88 return GetDisplayNameAttribute (type.GetCustomAttributes (false));
91 DisplayNameAttribute GetAttribute (Type type, string memberName, MemberTypes expectedType)
93 MemberInfo[] mi = type.GetMember (memberName, expectedType, BindingFlags.Instance | BindingFlags.Public);
94 return GetDisplayNameAttribute (mi[0].GetCustomAttributes (false));
98 public void FixtureSetUp ()
100 tc1 = new TestClass1 ();
101 tc2 = new TestClass2 ();
102 tc3 = new TestClass3 ();
106 public void Constructor0 ()
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");
115 public void Constructor1 ()
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");
122 dn = new DisplayNameAttribute (null);
123 Assert.IsNull (dn.DisplayName, "#B1");
124 Assert.IsFalse (dn.IsDefaultAttribute (), "#B2");
126 dn = new DisplayNameAttribute ("category");
127 Assert.IsNotNull (dn.DisplayName, "#C1");
128 Assert.AreEqual ("category", dn.DisplayName, "#C2");
129 Assert.IsFalse (dn.IsDefaultAttribute (), "#C3");
133 public void Default ()
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");
142 public void Equals ()
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");
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");
164 public void GetHashCodeTest ()
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");
171 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=288534
172 dn = new DisplayNameAttribute (null);
176 } catch (NullReferenceException) {
181 public void TestEmptyName ()
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");
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");
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");
201 public void TestNonEmptyName ()
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");
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");
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");
221 public void TestNullName ()
223 Type tc3t = tc3.GetType ();
224 DisplayNameAttribute dn = GetAttribute (tc3t);
225 Assert.IsNotNull (dn, "#1_1");
226 Assert.IsNull (dn.DisplayName, "#1_2");
228 dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
229 Assert.IsNotNull (dn, "#2_1");
230 Assert.IsNull (dn.DisplayName, "#2_2");
232 dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
233 Assert.IsNotNull (dn, "#3_1");
234 Assert.IsNull (dn.DisplayName, "#3_2");
238 public void TestDefaultAttribute ()
240 Type tc1t = tc1.GetType ();
241 DisplayNameAttribute dn = GetAttribute (tc1t);
242 Assert.IsNotNull (dn, "#1_1");
243 Assert.IsTrue (dn.IsDefaultAttribute (), "#1_2");
245 dn = GetAttribute (tc1t, "Property1", MemberTypes.Property);
246 Assert.IsNotNull (dn, "#1_3");
247 Assert.IsTrue (dn.IsDefaultAttribute (), "#1_4");
249 dn = GetAttribute (tc1t, "Method1", MemberTypes.Method);
250 Assert.IsNotNull (dn, "#1_5");
251 Assert.IsTrue (dn.IsDefaultAttribute (), "#1_6");
253 Type tc2t = tc2.GetType ();
254 dn = GetAttribute (tc2t);
255 Assert.IsNotNull (dn, "#2_1");
256 Assert.IsFalse (dn.IsDefaultAttribute (), "#2_2");
258 dn = GetAttribute (tc2t, "Property2", MemberTypes.Property);
259 Assert.IsNotNull (dn, "#2_3");
260 Assert.IsFalse (dn.IsDefaultAttribute (), "#2_4");
262 dn = GetAttribute (tc2t, "Method2", MemberTypes.Method);
263 Assert.IsNotNull (dn, "#2_5");
264 Assert.IsFalse (dn.IsDefaultAttribute (), "#2_6");
266 Type tc3t = tc3.GetType ();
267 dn = GetAttribute (tc3t);
268 Assert.IsNotNull (dn, "#3_1");
269 Assert.IsFalse (dn.IsDefaultAttribute (), "#3_2");
271 dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
272 Assert.IsNotNull (dn, "#3_3");
273 Assert.IsFalse (dn.IsDefaultAttribute (), "#3_4");
275 dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
276 Assert.IsNotNull (dn, "#3_5");
277 Assert.IsFalse (dn.IsDefaultAttribute (), "#3_6");