DisplayNameAttributeTests.cs 7.7 KB

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