1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
//
// System.ComponentModel.DisplayNameAttributeTests test cases
//
// Authors:
// Marek Habersack (grendello@gmail.com)
//
// (c) 2006 Marek Habersack
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.ComponentModel;
using System.Reflection;
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 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.IsFalse (dn.DisplayName == null, "#1_2");
Assert.AreEqual (dn.DisplayName, "", "#1_3");
dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
Assert.IsNotNull (dn, "#2_1");
Assert.IsFalse (dn.DisplayName == null, "#2_2");
Assert.AreEqual (dn.DisplayName, "", "#2_3");
dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
Assert.IsNotNull (dn, "#3_1");
Assert.IsFalse (dn.DisplayName == null, "#3_2");
Assert.AreEqual (dn.DisplayName, "", "#3_3");
}
[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.IsTrue (dn.IsDefaultAttribute (), "#3_2");
dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
Assert.IsNotNull (dn, "#3_3");
Assert.IsTrue (dn.IsDefaultAttribute (), "#3_4");
dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
Assert.IsNotNull (dn, "#3_5");
Assert.IsTrue (dn.IsDefaultAttribute (), "#3_6");
}
}
}
#endif
|