Skip to content

Commit

Permalink
Add tests relating to TypeDescriptor (dotnet/corefx#38938)
Browse files Browse the repository at this point in the history
Commit migrated from dotnet/corefx@cc51975
  • Loading branch information
hughbe authored and maryamariyan committed Sep 9, 2019
1 parent e563451 commit 03b7461
Show file tree
Hide file tree
Showing 8 changed files with 2,566 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public virtual string GetFullComponentName(object component)
return _parent.GetFullComponentName(component);
}

return GetTypeDescriptor(component).GetComponentName();
return GetTypeDescriptor(component)?.GetComponentName();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2839,7 +2839,9 @@ private sealed class MemberDescriptorComparer : IComparer

public int Compare(object left, object right)
{
return CultureInfo.InvariantCulture.CompareInfo.Compare(((MemberDescriptor)left).Name, ((MemberDescriptor)right).Name);
MemberDescriptor leftMember = left as MemberDescriptor;
MemberDescriptor rightMember = right as MemberDescriptor;
return CultureInfo.InvariantCulture.CompareInfo.Compare(leftMember?.Name, rightMember?.Name);
}
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,153 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using Xunit;

namespace System.ComponentModel.Design.Tests
{
public class DesignerVerbTests
{
public static IEnumerable<object[]> Ctor_Text_EventHandler_TestData()
public static IEnumerable<object[]> Ctor_String_EventHandler_TestData()
{
yield return new object[] { "Text", new EventHandler(EventHandler), "Text" };
yield return new object[] { "(&.)Text", new EventHandler(EventHandler), "Text" };
yield return new object[] { null, null, null };
yield return new object[] { "Text", new EventHandler(EventHandler), "Text", "Text" };
yield return new object[] { "(&.)Text", new EventHandler(EventHandler), "Text", "Text" };
yield return new object[] { null, null, string.Empty, null };
}

[Theory]
[MemberData(nameof(Ctor_Text_EventHandler_TestData))]
public void Ctor_Text_EventHandler(string text, EventHandler handler, string expectedText)
[MemberData(nameof(Ctor_String_EventHandler_TestData))]
public void Ctor_String_EventHandler(string text, EventHandler handler, string expectedText, string expectedPropertiesText)
{
var verb = new DesignerVerb(text, handler);
Assert.Equal(expectedText ?? string.Empty, verb.Text);
Assert.Equal(new Guid("{74D21313-2AEE-11d1-8BFB-00A0C90F26F7}"), verb.CommandID.Guid);
Assert.Equal(0x2000, verb.CommandID.ID);
Assert.Empty(verb.Description);
Assert.True(verb.Enabled);
Assert.Equal(3, verb.OleStatus);
Assert.True(verb.Enabled);
Assert.Equal(expectedText, verb.Text);
Assert.IsType<HybridDictionary>(verb.Properties);
Assert.Same(verb.Properties, verb.Properties);
DictionaryEntry entry = Assert.IsType<DictionaryEntry>(Assert.Single(verb.Properties));
Assert.Equal("Text", entry.Key);
Assert.Equal(expectedPropertiesText, entry.Value);
Assert.True(verb.Supported);
Assert.True(verb.Visible);
}

public static IEnumerable<object[]> Ctor_Text_EventHandler_CommandID_TestData()
public static IEnumerable<object[]> Ctor_String_EventHandler_CommandID_TestData()
{
yield return new object[] { "Text", new EventHandler(EventHandler), new CommandID(Guid.NewGuid(), 10), "Text" };
yield return new object[] { "(&.)Text", new EventHandler(EventHandler), new CommandID(Guid.NewGuid(), 10), "Text" };
yield return new object[] { null, null, null, null };
yield return new object[] { "Text", new EventHandler(EventHandler), new CommandID(Guid.NewGuid(), 10), "Text", "Text" };
yield return new object[] { "(&.)Text", new EventHandler(EventHandler), new CommandID(Guid.NewGuid(), 10), "Text", "Text" };
yield return new object[] { null, null, null, string.Empty, null };
}

[Theory]
[MemberData(nameof(Ctor_Text_EventHandler_CommandID_TestData))]
public void Ctor_Text_EventHandler_CommandID(string text, EventHandler handler, CommandID commandID, string expectedText)
[MemberData(nameof(Ctor_String_EventHandler_CommandID_TestData))]
public void Ctor_String_EventHandler_CommandID(string text, EventHandler handler, CommandID commandID, string expectedText, string expectedPropertiesText)
{
var verb = new DesignerVerb(text, handler, commandID);
Assert.Equal(expectedText ?? string.Empty, verb.Text);
Assert.Same(commandID, verb.CommandID);
Assert.Equal(commandID, verb.CommandID);
Assert.Empty(verb.Description);
Assert.True(verb.Enabled);
Assert.Equal(3, verb.OleStatus);
Assert.True(verb.Enabled);
Assert.Equal(expectedText, verb.Text);
Assert.IsType<HybridDictionary>(verb.Properties);
Assert.Same(verb.Properties, verb.Properties);
DictionaryEntry entry = Assert.IsType<DictionaryEntry>(Assert.Single(verb.Properties));
Assert.Equal("Text", entry.Key);
Assert.Equal(expectedPropertiesText, entry.Value);
Assert.True(verb.Supported);
Assert.True(verb.Visible);
}

[Fact]
public void Ctor_NullProperties_ThrowsNullReferenceException()
{
Assert.Throws<NullReferenceException>(() => new NullPropertiesDesignerVerb("Text", new EventHandler(EventHandler)));
Assert.Throws<NullReferenceException>(() => new NullPropertiesDesignerVerb("Text", new EventHandler(EventHandler), new CommandID(Guid.NewGuid(), 10)));
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("Description")]
public void Description_Set_GetReturnsExpected(string value)
[InlineData(null, "")]
[InlineData("", "")]
[InlineData("Description", "Description")]
public void Description_GetWithProperties_ReturnsExpected(string value, string expected)
{
var verb = new DesignerVerb("Text", new EventHandler(EventHandler)) { Description = value };
Assert.Equal(value ?? string.Empty, verb.Description);
var verb = new DesignerVerb("Text", new EventHandler(EventHandler));
verb.Properties["Description"] = value;
Assert.Equal(expected, verb.Description);
}

[Fact]
public void Description_GetWithPropertiesInvalidType_ThrowsInvalidCastException()
{
var verb = new DesignerVerb("Text", new EventHandler(EventHandler));
verb.Properties["Description"] = new object();
Assert.Throws<InvalidCastException>(() => verb.Description);
}

[Fact]
public void Description_GetWithNullProperties_ThrowsNullReferenceException()
{
var verb = new NullPropertiesAfterConstructionDesignerVerb("Text", new EventHandler(EventHandler));
Assert.Throws<NullReferenceException>(() => verb.Description = "value");
}

[Theory]
[InlineData(null, "")]
[InlineData("", "")]
[InlineData("Description", "Description")]
public void Description_Set_GetReturnsExpected(string value, string expected)
{
var verb = new DesignerVerb("Text", new EventHandler(EventHandler))
{
Description = value
};
Assert.Equal(expected, verb.Description);
Assert.Equal(value, verb.Properties["Description"]);

// Set same.
verb.Description = value;
Assert.Equal(expected, verb.Description);
Assert.Equal(value, verb.Properties["Description"]);
}

[Fact]
public void Description_SetWithNullProperties_ThrowsNullReferenceException()
{
var verb = new NullPropertiesAfterConstructionDesignerVerb("Text", new EventHandler(EventHandler));
Assert.Throws<NullReferenceException>(() => verb.Description);
}

[Theory]
[InlineData(null, "")]
[InlineData("", "")]
[InlineData("Text", "Text")]
public void Text_GetWithProperties_ReturnsExpected(string value, string expected)
{
var verb = new DesignerVerb("Text", new EventHandler(EventHandler));
verb.Properties["Text"] = value;
Assert.Equal(expected, verb.Text);
}

[Fact]
public void Text_GetWithPropertiesInvalidType_ThrowsInvalidCastException()
{
var verb = new DesignerVerb("Text", new EventHandler(EventHandler));
verb.Properties["Text"] = new object();
Assert.Throws<InvalidCastException>(() => verb.Text);
}

[Fact]
public void Text_GetWithNullProperties_ThrowsNullReferenceException()
{
var verb = new NullPropertiesAfterConstructionDesignerVerb("Text", new EventHandler(EventHandler));
Assert.Throws<NullReferenceException>(() => verb.Text);
}

[Fact]
Expand All @@ -62,5 +159,45 @@ public void ToString_Invoke_ReturnsExpected()
}

private static void EventHandler(object sender, EventArgs e) { }

private class NullPropertiesDesignerVerb : DesignerVerb
{
public NullPropertiesDesignerVerb(string text, EventHandler handler) : base(text, handler)
{
}

public NullPropertiesDesignerVerb(string text, EventHandler handler, CommandID startCommandID) : base(text, handler, startCommandID)
{
}

public override IDictionary Properties => null;
}

private class NullPropertiesAfterConstructionDesignerVerb : DesignerVerb
{
public NullPropertiesAfterConstructionDesignerVerb(string text, EventHandler handler) : base(text, handler)
{
}

public NullPropertiesAfterConstructionDesignerVerb(string text, EventHandler handler, CommandID startCommandID) : base(text, handler, startCommandID)
{
}

private bool Constructed { get; set; }

public override IDictionary Properties
{
get
{
if (!Constructed)
{
Constructed = true;
return base.Properties;
}

return null;
}
}
}
}
}
Loading

0 comments on commit 03b7461

Please sign in to comment.