2 // System.ComponentModel.DisplayNameAttributeTests test cases
5 // Marek Habersack (grendello@gmail.com)
7 // (c) 2006 Marek Habersack
10 using NUnit.Framework;
12 using System.ComponentModel;
13 using System.Reflection;
15 namespace MonoTests.System.ComponentModel
21 public string Property1
23 get { return String.Empty; }
27 public string Method1 ()
33 [DisplayName ("TestClassTwo")]
36 [DisplayName ("PropertyTwo")]
37 public string Property2
39 get { return String.Empty; }
42 [DisplayName ("MethodTwo")]
43 public string Method2 ()
53 public string Property3
55 get { return String.Empty; }
59 public string Method3 ()
66 public class DisplayNameAttributeTests
68 private TestClass1 tc1;
69 private TestClass2 tc2;
70 private TestClass3 tc3;
72 DisplayNameAttribute GetDisplayNameAttribute (object[] attrs)
74 DisplayNameAttribute dn = null;
75 foreach (object attr in attrs) {
76 dn = attr as DisplayNameAttribute;
83 DisplayNameAttribute GetAttribute (Type type)
85 return GetDisplayNameAttribute (type.GetCustomAttributes (false));
88 DisplayNameAttribute GetAttribute (Type type, string memberName, MemberTypes expectedType)
90 MemberInfo[] mi = type.GetMember (memberName, expectedType, BindingFlags.Instance | BindingFlags.Public);
91 return GetDisplayNameAttribute (mi[0].GetCustomAttributes (false));
95 public void FixtureSetUp ()
97 tc1 = new TestClass1 ();
98 tc2 = new TestClass2 ();
99 tc3 = new TestClass3 ();
103 public void TestEmptyName ()
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");
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");
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");
123 public void TestNonEmptyName ()
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");
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");
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");
143 public void TestNullName ()
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");
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");
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");
163 public void TestDefaultAttribute ()
165 Type tc1t = tc1.GetType ();
166 DisplayNameAttribute dn = GetAttribute (tc1t);
167 Assert.IsNotNull (dn, "#1_1");
168 Assert.IsTrue (dn.IsDefaultAttribute (), "#1_2");
170 dn = GetAttribute (tc1t, "Property1", MemberTypes.Property);
171 Assert.IsNotNull (dn, "#1_3");
172 Assert.IsTrue (dn.IsDefaultAttribute (), "#1_4");
174 dn = GetAttribute (tc1t, "Method1", MemberTypes.Method);
175 Assert.IsNotNull (dn, "#1_5");
176 Assert.IsTrue (dn.IsDefaultAttribute (), "#1_6");
178 Type tc2t = tc2.GetType ();
179 dn = GetAttribute (tc2t);
180 Assert.IsNotNull (dn, "#2_1");
181 Assert.IsFalse (dn.IsDefaultAttribute (), "#2_2");
183 dn = GetAttribute (tc2t, "Property2", MemberTypes.Property);
184 Assert.IsNotNull (dn, "#2_3");
185 Assert.IsFalse (dn.IsDefaultAttribute (), "#2_4");
187 dn = GetAttribute (tc2t, "Method2", MemberTypes.Method);
188 Assert.IsNotNull (dn, "#2_5");
189 Assert.IsFalse (dn.IsDefaultAttribute (), "#2_6");
191 Type tc3t = tc3.GetType ();
192 dn = GetAttribute (tc3t);
193 Assert.IsNotNull (dn, "#3_1");
194 Assert.IsTrue (dn.IsDefaultAttribute (), "#3_2");
196 dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
197 Assert.IsNotNull (dn, "#3_3");
198 Assert.IsTrue (dn.IsDefaultAttribute (), "#3_4");
200 dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
201 Assert.IsNotNull (dn, "#3_5");
202 Assert.IsTrue (dn.IsDefaultAttribute (), "#3_6");