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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
//
// System.ComponentModel.DisplayNameAttribute test cases
//
// Authors:
// Marek Habersack (grendello@gmail.com)
// Gert Driesen (drieseng@users.sourceforge.net
//
// (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
|