DisplayNameAttributeTests.cs 5.2 KB

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