| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- //
- // System.ComponentModel.DisplayNameAttribute test cases
- //
- // Authors:
- // Marek Habersack ([email protected])
- // Gert Driesen ([email protected]
- //
- // (c) 2006 Marek Habersack
- //
- #if NET_2_0
- using System;
- using System.ComponentModel;
- using System.Reflection;
- using NUnit.Framework;
- namespace MonoTests.System.ComponentModel
- {
- [DisplayName ()]
- class TestClass1
- {
- [DisplayName ()]
- public string Property1
- {
- get { return String.Empty; }
- }
- [DisplayName ()]
- public string Method1 ()
- {
- return String.Empty;
- }
- }
- [DisplayName ("TestClassTwo")]
- class TestClass2
- {
- [DisplayName ("PropertyTwo")]
- public string Property2
- {
- get { return String.Empty; }
- }
- [DisplayName ("MethodTwo")]
- public string Method2 ()
- {
- return String.Empty;
- }
- }
- [DisplayName (null)]
- class TestClass3
- {
- [DisplayName (null)]
- public string Property3
- {
- get { return String.Empty; }
- }
- [DisplayName (null)]
- public string Method3 ()
- {
- return String.Empty;
- }
- }
-
- [TestFixture]
- public class DisplayNameAttributeTests
- {
- private TestClass1 tc1;
- private TestClass2 tc2;
- private TestClass3 tc3;
- DisplayNameAttribute GetDisplayNameAttribute (object[] attrs)
- {
- DisplayNameAttribute dn = null;
- foreach (object attr in attrs) {
- dn = attr as DisplayNameAttribute;
- if (dn != null)
- break;
- }
- return dn;
- }
-
- DisplayNameAttribute GetAttribute (Type type)
- {
- return GetDisplayNameAttribute (type.GetCustomAttributes (false));
- }
- DisplayNameAttribute GetAttribute (Type type, string memberName, MemberTypes expectedType)
- {
- MemberInfo[] mi = type.GetMember (memberName, expectedType, BindingFlags.Instance | BindingFlags.Public);
- return GetDisplayNameAttribute (mi[0].GetCustomAttributes (false));
- }
- [SetUp]
- public void FixtureSetUp ()
- {
- tc1 = new TestClass1 ();
- tc2 = new TestClass2 ();
- tc3 = new TestClass3 ();
- }
-
- [Test]
- public void Constructor0 ()
- {
- DisplayNameAttribute dn = new DisplayNameAttribute ();
- Assert.IsNotNull (dn.DisplayName, "#1");
- Assert.AreEqual (string.Empty, dn.DisplayName, "#2");
- Assert.IsTrue (dn.IsDefaultAttribute (), "#3");
- }
- [Test]
- public void Constructor1 ()
- {
- DisplayNameAttribute dn = new DisplayNameAttribute (string.Empty);
- Assert.IsNotNull (dn.DisplayName, "#A1");
- Assert.AreEqual (string.Empty, dn.DisplayName, "#A2");
- Assert.IsTrue (dn.IsDefaultAttribute (), "#A3");
- dn = new DisplayNameAttribute (null);
- Assert.IsNull (dn.DisplayName, "#B1");
- Assert.IsFalse (dn.IsDefaultAttribute (), "#B2");
- dn = new DisplayNameAttribute ("category");
- Assert.IsNotNull (dn.DisplayName, "#C1");
- Assert.AreEqual ("category", dn.DisplayName, "#C2");
- Assert.IsFalse (dn.IsDefaultAttribute (), "#C3");
- }
- [Test]
- public void Default ()
- {
- DisplayNameAttribute dn = DisplayNameAttribute.Default;
- Assert.IsNotNull (dn.DisplayName, "#1");
- Assert.AreEqual (string.Empty, dn.DisplayName, "#2");
- Assert.IsTrue (dn.IsDefaultAttribute (), "#3");
- }
- [Test]
- public void Equals ()
- {
- DisplayNameAttribute dn = new DisplayNameAttribute ();
- Assert.IsTrue (dn.Equals (DisplayNameAttribute.Default), "#A1");
- Assert.IsTrue (dn.Equals (new DisplayNameAttribute (string.Empty)), "#A2");
- Assert.IsFalse (dn.Equals (new DisplayNameAttribute ("category")), "#A3");
- Assert.IsFalse (dn.Equals (new DisplayNameAttribute (null)), "#A4");
- Assert.IsFalse (dn.Equals (null), "#A5");
- Assert.IsTrue (dn.Equals (dn), "#A6");
- Assert.IsFalse (dn.Equals (55), "#A7");
- dn = new DisplayNameAttribute ("category");
- Assert.IsFalse (dn.Equals (DisplayNameAttribute.Default), "#B1");
- Assert.IsFalse (dn.Equals (new DisplayNameAttribute (string.Empty)), "#B2");
- Assert.IsTrue (dn.Equals (new DisplayNameAttribute ("category")), "#B3");
- Assert.IsFalse (dn.Equals (new DisplayNameAttribute (null)), "#B4");
- Assert.IsFalse (dn.Equals (null), "#B5");
- Assert.IsTrue (dn.Equals (dn), "#B6");
- Assert.IsFalse (dn.Equals (55), "#B7");
- }
- [Test]
- public void GetHashCodeTest ()
- {
- DisplayNameAttribute dn = new DisplayNameAttribute ();
- Assert.AreEqual (string.Empty.GetHashCode (), dn.GetHashCode (), "#A1");
- dn = new DisplayNameAttribute ("A");
- Assert.AreEqual ("A".GetHashCode (), dn.GetHashCode (), "#A2");
- // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=288534
- dn = new DisplayNameAttribute (null);
- try {
- dn.GetHashCode ();
- Assert.Fail ("#B1");
- } catch (NullReferenceException) {
- }
- }
- [Test]
- public void TestEmptyName ()
- {
- Type tc1t = tc1.GetType ();
- DisplayNameAttribute dn = GetAttribute (tc1t);
- Assert.IsNotNull (dn, "#1_1");
- Assert.IsFalse (dn.DisplayName == null, "#1_2");
- Assert.AreEqual (dn.DisplayName, "", "#1_3");
- dn = GetAttribute (tc1t, "Property1", MemberTypes.Property);
- Assert.IsNotNull (dn, "#2_1");
- Assert.IsFalse (dn.DisplayName == null, "#2_2");
- Assert.AreEqual (dn.DisplayName, "", "#2_3");
-
- dn = GetAttribute (tc1t, "Method1", MemberTypes.Method);
- Assert.IsNotNull (dn, "#3_1");
- Assert.IsFalse (dn.DisplayName == null, "#3_2");
- Assert.AreEqual (dn.DisplayName, "", "#3_3");
- }
- [Test]
- public void TestNonEmptyName ()
- {
- Type tc2t = tc2.GetType ();
- DisplayNameAttribute dn = GetAttribute (tc2t);
- Assert.IsNotNull (dn, "#1_1");
- Assert.IsFalse (dn.DisplayName == null, "#1_2");
- Assert.AreEqual (dn.DisplayName, "TestClassTwo", "#1_3");
- dn = GetAttribute (tc2t, "Property2", MemberTypes.Property);
- Assert.IsNotNull (dn, "#2_1");
- Assert.IsFalse (dn.DisplayName == null, "#2_2");
- Assert.AreEqual (dn.DisplayName, "PropertyTwo", "#2_3");
-
- dn = GetAttribute (tc2t, "Method2", MemberTypes.Method);
- Assert.IsNotNull (dn, "#3_1");
- Assert.IsFalse (dn.DisplayName == null, "#3_2");
- Assert.AreEqual (dn.DisplayName, "MethodTwo", "#3_3");
- }
- [Test]
- public void TestNullName ()
- {
- Type tc3t = tc3.GetType ();
- DisplayNameAttribute dn = GetAttribute (tc3t);
- Assert.IsNotNull (dn, "#1_1");
- Assert.IsNull (dn.DisplayName, "#1_2");
-
- dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
- Assert.IsNotNull (dn, "#2_1");
- Assert.IsNull (dn.DisplayName, "#2_2");
-
- dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
- Assert.IsNotNull (dn, "#3_1");
- Assert.IsNull (dn.DisplayName, "#3_2");
- }
-
- [Test]
- public void TestDefaultAttribute ()
- {
- Type tc1t = tc1.GetType ();
- DisplayNameAttribute dn = GetAttribute (tc1t);
- Assert.IsNotNull (dn, "#1_1");
- Assert.IsTrue (dn.IsDefaultAttribute (), "#1_2");
- dn = GetAttribute (tc1t, "Property1", MemberTypes.Property);
- Assert.IsNotNull (dn, "#1_3");
- Assert.IsTrue (dn.IsDefaultAttribute (), "#1_4");
- dn = GetAttribute (tc1t, "Method1", MemberTypes.Method);
- Assert.IsNotNull (dn, "#1_5");
- Assert.IsTrue (dn.IsDefaultAttribute (), "#1_6");
- Type tc2t = tc2.GetType ();
- dn = GetAttribute (tc2t);
- Assert.IsNotNull (dn, "#2_1");
- Assert.IsFalse (dn.IsDefaultAttribute (), "#2_2");
- dn = GetAttribute (tc2t, "Property2", MemberTypes.Property);
- Assert.IsNotNull (dn, "#2_3");
- Assert.IsFalse (dn.IsDefaultAttribute (), "#2_4");
- dn = GetAttribute (tc2t, "Method2", MemberTypes.Method);
- Assert.IsNotNull (dn, "#2_5");
- Assert.IsFalse (dn.IsDefaultAttribute (), "#2_6");
- Type tc3t = tc3.GetType ();
- dn = GetAttribute (tc3t);
- Assert.IsNotNull (dn, "#3_1");
- Assert.IsFalse (dn.IsDefaultAttribute (), "#3_2");
- dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
- Assert.IsNotNull (dn, "#3_3");
- Assert.IsFalse (dn.IsDefaultAttribute (), "#3_4");
- dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
- Assert.IsNotNull (dn, "#3_5");
- Assert.IsFalse (dn.IsDefaultAttribute (), "#3_6");
- }
- }
- }
- #endif
|