* DisplayNameAttribute.cs: To match MS, do not change null DisplayName
authorGert Driesen <drieseng@users.sourceforge.net>
Sat, 21 Jul 2007 13:37:11 +0000 (13:37 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Sat, 21 Jul 2007 13:37:11 +0000 (13:37 -0000)
to a zero-length string. Modified IsDefaultAttribute to take into
account null DisplayName.
* DisplayNameAttribute.cs: Added ctor tests. Fixed tests to pass on
MS (now that our implementation matches that of MS). Added tests
for Default, GetHashCode and Equals.

svn path=/trunk/mcs/; revision=82414

mcs/class/System/System.ComponentModel/ChangeLog
mcs/class/System/System.ComponentModel/DisplayNameAttribute.cs
mcs/class/System/Test/System.ComponentModel/ChangeLog
mcs/class/System/Test/System.ComponentModel/DisplayNameAttributeTests.cs

index a39800a526a834eeea500c596fc9c974fa71cbe8..316cf2a2771a726b2d4537d468bf1e270d5b4c87 100644 (file)
@@ -1,3 +1,9 @@
+2007-07-21  Gert Driesen  <drieseng@users.souceforge.net>
+
+       * DisplayNameAttribute.cs: To match MS, do not change null DisplayName
+       to a zero-length string. Modified IsDefaultAttribute to take into
+       account null DisplayName.
+
 2007-07-16  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * ReflectionPropertyDescriptor.cs: For read-only properties,
index 62bbb087ef91559b712a0fc10d1d09171f5a1cd8..38fac63034b08a07e474601f36610e9cb6d54e9f 100644 (file)
@@ -29,7 +29,7 @@
 #if NET_2_0
 namespace System.ComponentModel
 {
-       [AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Event)]
+       [AttributeUsageAttribute (AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event)]
        public class DisplayNameAttribute : Attribute
        {
                public static readonly DisplayNameAttribute Default = new DisplayNameAttribute ();
@@ -43,12 +43,14 @@ namespace System.ComponentModel
 
                public DisplayNameAttribute (string displayName)
                {
-                       this.attributeDisplayName = displayName != null ? displayName : String.Empty;
+                       this.attributeDisplayName = displayName;
                }
 
                public override bool IsDefaultAttribute ()
                {
-                       return attributeDisplayName.Length == 0;
+                       if (attributeDisplayName != null)
+                               return attributeDisplayName.Length == 0;
+                       return false;
                }
 
                public override int GetHashCode ()
@@ -74,7 +76,7 @@ namespace System.ComponentModel
                
                protected string DisplayNameValue {
                        get { return attributeDisplayName; }
-                       set { attributeDisplayName = value != null ? value : String.Empty; }
+                       set { attributeDisplayName = value; }
                }
        }
 }
index 1b4abe3fdb165a18930ca31963a880113a613ff6..84efc63d0994aad75c5365efe76b454138e1cbcb 100644 (file)
@@ -1,3 +1,9 @@
+2007-07-21  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * DisplayNameAttribute.cs: Added ctor tests. Fixed tests to pass on
+       MS (now that our implementation matches that of MS). Added tests
+       for Default, GetHashCode and Equals.
+
 2007-07-16  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * PropertyDescriptorTests.cs: Added test for ShouldSerializeValue
index a48540ebe0210e49b5f375befefd2a7b1a8273f2..5a247f1286de945f157e8f3c4af838b6e059fb2b 100644 (file)
@@ -1,17 +1,20 @@
 //
-// System.ComponentModel.DisplayNameAttributeTests test cases
+// 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 NUnit.Framework;
 using System;
 using System.ComponentModel;
 using System.Reflection;
 
+using NUnit.Framework;
+
 namespace MonoTests.System.ComponentModel 
 {
        [DisplayName ()]
@@ -99,6 +102,81 @@ namespace MonoTests.System.ComponentModel
                        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 ()
                {
@@ -145,18 +223,15 @@ namespace MonoTests.System.ComponentModel
                        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");
+                       Assert.IsNull (dn.DisplayName, "#1_2");
                        
                        dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
                        Assert.IsNotNull (dn, "#2_1");
-                       Assert.IsFalse (dn.DisplayName == null, "#2_2");
-                       Assert.AreEqual (dn.DisplayName, "", "#2_3");
+                       Assert.IsNull (dn.DisplayName, "#2_2");
                        
                        dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
                        Assert.IsNotNull (dn, "#3_1");
-                       Assert.IsFalse (dn.DisplayName == null, "#3_2");
-                       Assert.AreEqual (dn.DisplayName, "", "#3_3");
+                       Assert.IsNull (dn.DisplayName, "#3_2");
                }
                
                [Test]
@@ -191,17 +266,16 @@ namespace MonoTests.System.ComponentModel
                        Type tc3t = tc3.GetType ();
                        dn = GetAttribute (tc3t);
                        Assert.IsNotNull (dn, "#3_1");
-                       Assert.IsTrue (dn.IsDefaultAttribute (), "#3_2");
+                       Assert.IsFalse (dn.IsDefaultAttribute (), "#3_2");
 
                        dn = GetAttribute (tc3t, "Property3", MemberTypes.Property);
                        Assert.IsNotNull (dn, "#3_3");
-                       Assert.IsTrue (dn.IsDefaultAttribute (), "#3_4");
+                       Assert.IsFalse (dn.IsDefaultAttribute (), "#3_4");
 
                        dn = GetAttribute (tc3t, "Method3", MemberTypes.Method);
                        Assert.IsNotNull (dn, "#3_5");
-                       Assert.IsTrue (dn.IsDefaultAttribute (), "#3_6");
+                       Assert.IsFalse (dn.IsDefaultAttribute (), "#3_6");
                }
        }
 }
 #endif
-