Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetrou committed Feb 9, 2025
1 parent d9a8679 commit 7ddb206
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,8 @@ namespace System.ComponentModel.Design.Tests;
public class ArrayEditorTests
{
[Theory]
[InlineData(typeof(object), null)]
[InlineData(typeof(string), null)]
[InlineData(typeof(string[]), typeof(string))]
[InlineData(typeof(int[]), typeof(int))]
[InlineData(typeof(IList<int>), null)]
[InlineData(typeof(IList), null)]
[InlineData(typeof(ClassWithItem), null)]
[InlineData(typeof(ClassWithPrivateItem), null)]
[InlineData(typeof(ClassWithStaticItem), null)]
[InlineData(typeof(ClassWithItems), null)]
[InlineData(typeof(ClassWithPrivateItems), null)]
[InlineData(typeof(ClassWithStaticItems), null)]
public void ArrayEditor_Ctor_Type(Type type, Type expectedItemType)
{
SubArrayEditor editor = new(type);
Expand All @@ -34,16 +25,27 @@ public void ArrayEditor_Ctor_Type(Type type, Type expectedItemType)
Assert.Equal([expectedItemType], editor.NewItemTypes);
}

[Theory]
[InlineData(typeof(object))]
[InlineData(typeof(string))]
[InlineData(typeof(IList<int>))]
[InlineData(typeof(IList))]
[InlineData(typeof(ClassWithItem))]
[InlineData(typeof(ClassWithPrivateItem))]
[InlineData(typeof(ClassWithStaticItem))]
[InlineData(typeof(ClassWithItems))]
[InlineData(typeof(ClassWithPrivateItems))]
[InlineData(typeof(ClassWithStaticItems))]
public void ArrayEditor_Ctor_Invalid_Type(Type type)
{
SubArrayEditor editor = new(type);
Assert.Throws<InvalidOperationException>(() => editor.CollectionItemType);
}

[Fact]
public void ArrayEditor_Ctor_NullType()
public void ArrayEditor_Ctor_NullType_ThrowsArgumentNullException()
{
SubArrayEditor editor = new(null);
Assert.Null(editor.CollectionItemType);
Assert.Null(editor.CollectionType);
Assert.Null(editor.Context);
Assert.Equal("net.ComponentModel.CollectionEditor", editor.HelpTopic);
Assert.False(editor.IsDropDownResizable);
Assert.Equal([null], editor.NewItemTypes);
Assert.Throws<ArgumentNullException>(() => new SubArrayEditor(null));
}

public static IEnumerable<object[]> CanRemoveInstance_TestData()
Expand All @@ -58,7 +60,7 @@ public static IEnumerable<object[]> CanRemoveInstance_TestData()
[MemberData(nameof(CanRemoveInstance_TestData))]
public void ArrayEditor_CanRemoveInstance_Invoke_ReturnsExpected(object value)
{
SubArrayEditor editor = new(null);
SubArrayEditor editor = new(typeof(string[]));
Assert.True(editor.CanRemoveInstance(value));
}

Expand All @@ -77,29 +79,19 @@ public void ArrayEditor_CanRemoveInstance_InheritanceAttribute_ReturnsExpected(I
{
using Component component = new();
TypeDescriptor.AddAttributes(component, attribute);
SubArrayEditor editor = new(null);
SubArrayEditor editor = new(typeof(string[]));
Assert.Equal(expected, editor.CanRemoveInstance(component));
}

[Fact]
public void ArrayEditor_CanSelectMultipleInstances_Invoke_ReturnsFalse()
{
SubArrayEditor editor = new(null);
SubArrayEditor editor = new(typeof(string[]));
Assert.True(editor.CanSelectMultipleInstances());
}

public static IEnumerable<object[]> GetDisplayText_TestData()
{
yield return new object[] { null, null, string.Empty };
yield return new object[] { null, string.Empty, "String" };
yield return new object[] { null, "string", "string" };

yield return new object[] { null, new ClassWithStringName { Name = "CustomName" }, "CustomName" };
yield return new object[] { null, new ClassWithStringName { Name = string.Empty }, "ClassWithStringName" };
yield return new object[] { null, new ClassWithStringName { Name = null }, "ClassWithStringName" };
yield return new object[] { null, new ClassWithNonStringName { Name = 1 }, "ClassWithNonStringName" };
yield return new object[] { null, new ClassWithNullToString(), "ClassWithNullToString" };

yield return new object[] { typeof(int), null, string.Empty };
yield return new object[] { typeof(int), "", "String" };
yield return new object[] { typeof(int), "value", "value" };
Expand Down Expand Up @@ -135,7 +127,7 @@ public void ArrayEditor_GetDisplayText_ValueDoesNotMatchCollectionType_ThrowsTar
[CommonMemberData(typeof(CommonTestHelperEx), nameof(CommonTestHelperEx.GetITypeDescriptorContextTestData))]
public void ArrayEditor_GetEditStyle_Invoke_ReturnsModal(ITypeDescriptorContext context)
{
ArrayEditor editor = new(null);
ArrayEditor editor = new(typeof(string[]));
Assert.Equal(UITypeEditorEditStyle.Modal, editor.GetEditStyle(context));
}

Expand All @@ -151,7 +143,7 @@ public static IEnumerable<object[]> GetItems_TestData()
[MemberData(nameof(GetItems_TestData))]
public void ArrayEditor_GetItems_Invoke_ReturnsExpected(object editValue, object[] expected)
{
SubArrayEditor editor = new(null);
SubArrayEditor editor = new(typeof(string[]));
object[] items = editor.GetItems(editValue);
Assert.Equal(expected, items);
Assert.IsType(expected.GetType(), items);
Expand All @@ -162,7 +154,7 @@ public void ArrayEditor_GetItems_Invoke_ReturnsExpected(object editValue, object
[CommonMemberData(typeof(CommonTestHelperEx), nameof(CommonTestHelperEx.GetITypeDescriptorContextTestData))]
public void ArrayEditor_GetPaintValueSupported_Invoke_ReturnsFalse(ITypeDescriptorContext context)
{
ArrayEditor editor = new(null);
ArrayEditor editor = new(typeof(string[]));
Assert.False(editor.GetPaintValueSupported(context));
}

Expand Down Expand Up @@ -202,17 +194,6 @@ public void ArrayEditor_SetItems_InvokeNonArrayValue_ReturnsExpected(object edit
Assert.Same(expected, editor.SetItems(editValue, value));
}

[Theory]
[InlineData(typeof(object))]
[InlineData(null)]
public void ArrayEditor_SetItems_NullCollectionItemType_ThrowsArgumentNullException(Type type)
{
SubArrayEditor editor = new(type);
Assert.Null(editor.CollectionItemType);
Assert.Throws<ArgumentNullException>("elementType", () => editor.SetItems(null, Array.Empty<object>()));
Assert.Throws<ArgumentNullException>("elementType", () => editor.SetItems(Array.Empty<object>(), Array.Empty<object>()));
}

private class SubArrayEditor : ArrayEditor
{
public SubArrayEditor(Type type) : base(type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,15 @@ public void CollectionEditor_Ctor_Type(Type type, Type expectedItemType)
}

[Fact]
public void CollectionEditor_Ctor_NullType()
public void CollectionEditor_Ctor_NullType_ThrowsArgumentNullException()
{
SubCollectionEditor editor = new(null);
Assert.Throws<ArgumentNullException>("type", () => editor.CollectionItemType);
Assert.Null(editor.CollectionType);
Assert.Null(editor.Context);
Assert.Equal("net.ComponentModel.CollectionEditor", editor.HelpTopic);
Assert.False(editor.IsDropDownResizable);
Assert.Throws<ArgumentNullException>("type", () => editor.NewItemTypes);
Assert.Throws<ArgumentNullException>(() => new SubCollectionEditor(null));
}

[Fact]
public void CollectionEditor_CollectionEditor_CancelChanges_Invoke_Nop()
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
editor.CancelChanges();
}

Expand All @@ -68,7 +62,7 @@ public static IEnumerable<object[]> CanRemoveInstance_TestData()
[MemberData(nameof(CanRemoveInstance_TestData))]
public void CollectionEditor_CanRemoveInstance_Invoke_ReturnsExpected(object value)
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
Assert.True(editor.CanRemoveInstance(value));
}

Expand All @@ -87,14 +81,14 @@ public void CollectionEditor_CanRemoveInstance_InheritanceAttribute_ReturnsExpec
{
using Component component = new();
TypeDescriptor.AddAttributes(component, attribute);
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
Assert.Equal(expected, editor.CanRemoveInstance(component));
}

[Fact]
public void CollectionEditor_CanSelectMultipleInstances_Invoke_ReturnsFalse()
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
Assert.True(editor.CanSelectMultipleInstances());
}

Expand All @@ -106,13 +100,6 @@ public void CollectionEditor_CreateCollectionForm_Invoke_Success()
Assert.NotSame(form, editor.CreateCollectionForm());
}

[Fact]
public void CollectionEditor_CreateCollectionForm_NullCollectionType_ThrowsArgumentNullException()
{
SubCollectionEditor editor = new(null);
Assert.Throws<ArgumentNullException>("type", editor.CreateCollectionForm);
}

[Theory]
[InlineData(typeof(object), typeof(object))]
[InlineData(typeof(int[]), typeof(object))]
Expand All @@ -131,13 +118,6 @@ public void CollectionEditor_CreateCollectionItemType_Invoke_ReturnsExpected(Typ
Assert.Same(itemType, editor.CreateCollectionItemType());
}

[Fact]
public void CollectionEditor_CreateCollectionItemType_NullType_ThrowsArgumentNullException()
{
SubCollectionEditor editor = new(null);
Assert.Throws<ArgumentNullException>("type", editor.CreateCollectionItemType);
}

public static IEnumerable<object[]> InvalidDesignerHost_TestData()
{
yield return new object[] { null };
Expand Down Expand Up @@ -350,14 +330,14 @@ public static IEnumerable<object[]> CreateInstance_InvokeWithoutContext_TestData
[MemberData(nameof(CreateInstance_InvokeWithoutContext_TestData))]
public void CollectionEditor_CreateInstance_InvokeWithoutContext_ReturnsExpected(Type type, object expected)
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
Assert.Equal(expected, editor.CreateInstance(type));
}

[Fact]
public void CollectionEditor_CreateInstance_NullItemType_ThrowsArgumentNullException()
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
Assert.Throws<ArgumentNullException>("itemType", () => editor.CreateInstance(null));
}

Expand All @@ -379,13 +359,6 @@ public void CollectionEditor_CreateNewItemTypes_Invoke_ReturnsExpected(Type type
Assert.NotSame(itemTypes, editor.CreateNewItemTypes());
}

[Fact]
public void CollectionEditor_CreateNewItemTypes_NullType_ThrowsArgumentNullException()
{
SubCollectionEditor editor = new(null);
Assert.Throws<ArgumentNullException>("type", editor.CreateNewItemTypes);
}

public static IEnumerable<object[]> DestroyInstance_NormalObject_TestData()
{
yield return new object[] { null };
Expand All @@ -396,7 +369,7 @@ public static IEnumerable<object[]> DestroyInstance_NormalObject_TestData()
[MemberData(nameof(DestroyInstance_NormalObject_TestData))]
public void CollectionEditor_DestroyInstance_NormalObject_Nop(object instance)
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
editor.DestroyInstance(instance);
}

Expand Down Expand Up @@ -496,7 +469,7 @@ public void CollectionEditor_DestroyInstance_IComponentWithoutHost_CallsDispose(
.Setup(c => c.Dispose())
.Verifiable();

SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
editor.DestroyInstance(mockComponent.Object);
mockComponent.Verify(c => c.Dispose(), Times.Once());
}
Expand All @@ -509,7 +482,7 @@ public void CollectionEditor_DestroyInstance_IDisposable_CallsDispose()
.Setup(d => d.Dispose())
.Verifiable();

SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
editor.DestroyInstance(mockDisposable.Object);
mockDisposable.Verify(d => d.Dispose(), Times.Once());
}
Expand Down Expand Up @@ -724,32 +697,11 @@ public void CollectionEditor_EditValue_ValidProviderValidHostWithIComponentChang
mockEditorService.Verify(s => s.ShowDialog(It.IsAny<Form>()), Times.Once());
}

[Fact]
public void CollectionEditor_EditValue_NullType_ThrowsArgumentNullException()
{
Mock<IWindowsFormsEditorService> mockEditorService = new(MockBehavior.Strict);
Mock<IServiceProvider> mockServiceProvider = new(MockBehavior.Strict);
mockServiceProvider
.Setup(p => p.GetService(typeof(IWindowsFormsEditorService)))
.Returns(mockEditorService.Object);
Mock<ITypeDescriptorContext> mockContext = new(MockBehavior.Strict);
mockContext
.Setup(c => c.GetService(typeof(IDesignerHost)))
.Returns((IDesignerHost)null);
mockContext
.Setup(c => c.GetService(typeof(AmbientProperties)))
.Returns(null);

SubCollectionEditor editor = new(null);
object value = new();
Assert.Throws<ArgumentNullException>("type", () => editor.EditValue(mockContext.Object, mockServiceProvider.Object, value));
}

[Theory]
[CommonMemberData(typeof(CommonTestHelperEx), nameof(CommonTestHelperEx.GetEditValueInvalidProviderTestData))]
public void CollectionEditor_EditValue_InvalidProvider_ReturnsValue(IServiceProvider provider, object value)
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
Assert.Same(value, editor.EditValue(null, provider, value));
Assert.Null(editor.Context);
}
Expand All @@ -758,30 +710,20 @@ public void CollectionEditor_EditValue_InvalidProvider_ReturnsValue(IServiceProv
[CommonMemberData(typeof(CommonTestHelperEx), nameof(CommonTestHelperEx.GetITypeDescriptorContextTestData))]
public void CollectionEditor_GetEditStyle_Invoke_ReturnsModal(ITypeDescriptorContext context)
{
CollectionEditor editor = new(null);
CollectionEditor editor = new(typeof(string));
Assert.Equal(UITypeEditorEditStyle.Modal, editor.GetEditStyle(context));
}

[Theory]
[CommonMemberData(typeof(CommonTestHelperEx), nameof(CommonTestHelperEx.GetITypeDescriptorContextTestData))]
public void CollectionEditor_GetPaintValueSupported_Invoke_ReturnsFalse(ITypeDescriptorContext context)
{
CollectionEditor editor = new(null);
CollectionEditor editor = new(typeof(string));
Assert.False(editor.GetPaintValueSupported(context));
}

public static IEnumerable<object[]> GetDisplayText_TestData()
{
yield return new object[] { null, null, string.Empty };
yield return new object[] { null, string.Empty, "String" };
yield return new object[] { null, "string", "string" };

yield return new object[] { null, new ClassWithStringName { Name = "CustomName" }, "CustomName" };
yield return new object[] { null, new ClassWithStringName { Name = string.Empty }, "ClassWithStringName" };
yield return new object[] { null, new ClassWithStringName { Name = null }, "ClassWithStringName" };
yield return new object[] { null, new ClassWithNonStringName { Name = 1 }, "ClassWithNonStringName" };
yield return new object[] { null, new ClassWithNullToString(), "ClassWithNullToString" };

yield return new object[] { typeof(int), null, string.Empty };
yield return new object[] { typeof(int), "", "String" };
yield return new object[] { typeof(int), "value", "value" };
Expand Down Expand Up @@ -825,7 +767,7 @@ public static IEnumerable<object[]> GetItems_TestData()
[MemberData(nameof(GetItems_TestData))]
public void CollectionEditor_GetItems_Invoke_ReturnsExpected(object editValue, object[] expected)
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
object[] items = editor.GetItems(editValue);
Assert.Equal(expected, items);
Assert.IsType(expected.GetType(), items);
Expand Down Expand Up @@ -869,12 +811,11 @@ public void CollectionEditor_GetService_WithContext_CallsContextGetService(Type
Assert.Same(result, editor.GetService(serviceType));
}

[Theory]
[CommonMemberData(typeof(CommonTestHelperEx), nameof(CommonTestHelperEx.GetTypeWithNullTheoryData))]
public void CollectionEditor_GetService_InvokeWithoutContext_ReturnsNull(Type serviceType)
[Fact]
public void CollectionEditor_GetService_InvokeWithoutContext_ReturnsNull()
{
SubCollectionEditor editor = new(serviceType);
Assert.Null(editor.GetService(serviceType));
SubCollectionEditor editor = new(typeof(int));
Assert.Null(editor.GetService(typeof(int)));
}

public static IEnumerable<object[]> GetObjectsFromInstance_TestData()
Expand All @@ -887,7 +828,7 @@ public static IEnumerable<object[]> GetObjectsFromInstance_TestData()
[MemberData(nameof(GetObjectsFromInstance_TestData))]
public void CollectionEditor_GetObjectsFromInstance_Invoke_ReturnsExpected(object instance)
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
IList objects = editor.GetObjectsFromInstance(instance);
Assert.Equal(new object[] { instance }, objects);
Assert.IsType<ArrayList>(objects);
Expand Down Expand Up @@ -917,7 +858,7 @@ public static IEnumerable<object[]> SetItems_TestData()
[MemberData(nameof(SetItems_TestData))]
public void CollectionEditor_SetItems_Invoke_ReturnsExpected(object editValue, object[] value, object expected)
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
object items = editor.SetItems(editValue, value);
Assert.Equal(expected, items);
Assert.Same(editValue, items);
Expand All @@ -926,7 +867,7 @@ public void CollectionEditor_SetItems_Invoke_ReturnsExpected(object editValue, o
[Fact]
public void CollectionEditor_SetItems_InvokeArray_ThrowsNotSupportedException()
{
SubCollectionEditor editor = new(null);
SubCollectionEditor editor = new(typeof(string));
Assert.Throws<NotSupportedException>(() => editor.SetItems(new object[1], new object[1]));
}

Expand Down
Loading

0 comments on commit 7ddb206

Please sign in to comment.