New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port the ArrayEditor from the classic framework and add unit test. #575
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/System.Windows.Forms.Design.Editors/src/System/ComponentModel/Design/ArrayEditor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// 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; | ||
|
||
namespace System.ComponentModel.Design | ||
{ | ||
/// <summary> | ||
/// Edits an array of values. | ||
/// </summary> | ||
public class ArrayEditor : CollectionEditor | ||
{ | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref='System.ComponentModel.Design.ArrayEditor'/> | ||
/// using the specified type for the array. | ||
/// </summary> | ||
public ArrayEditor(Type type) : base(type) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the data type this collection contains. | ||
/// </summary> | ||
protected override Type CreateCollectionItemType() => CollectionType.GetElementType(); | ||
|
||
/// <summary> | ||
/// Gets the items in the array. | ||
/// </summary> | ||
/// <param name="editValue"></param> | ||
protected override object[] GetItems(object editValue) | ||
{ | ||
if (editValue is Array) | ||
{ | ||
Array valueArray = (Array)editValue; | ||
object[] items = new object[valueArray.GetLength(0)]; | ||
Array.Copy(valueArray, items, items.Length); | ||
return items; | ||
} | ||
else | ||
{ | ||
return new object[0]; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Sets the items in the array. | ||
/// </summary> | ||
protected override object SetItems(object editValue, object[] value) | ||
{ | ||
if (editValue is Array || editValue == null) | ||
{ | ||
Array newArray = Array.CreateInstance(CollectionItemType, value.Length); | ||
Array.Copy(value, newArray, value.Length); | ||
return newArray; | ||
} | ||
return editValue; | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/System.Windows.Forms.Design.Editors/tests/UnitTests/ArrayEditorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// 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.ComponentModel; | ||
using System.Globalization; | ||
using System.Reflection; | ||
using Xunit; | ||
|
||
namespace System.ComponentModel.Design.Tests | ||
{ | ||
public class ArrayEditorTests | ||
{ | ||
[Fact] | ||
public void ArrayEditor_Constructor() | ||
{ | ||
var underTest = GetNewEditor(); | ||
Assert.NotNull(underTest); | ||
} | ||
|
||
[Fact] | ||
public void ArrayEditor_Getters() | ||
{ | ||
var underTest = GetNewEditor(); | ||
Assert.Equal(typeof(string), underTest.GetCollectionType()); | ||
Assert.True(underTest.CanSelectMultiple()); | ||
Assert.True(underTest.CanRemove("some string")); | ||
Assert.True(underTest.CanRemove(1234)); | ||
Assert.Equal("net.ComponentModel.CollectionEditor", underTest.GetHelpTopic()); | ||
Assert.Equal("my string", underTest.GetItemDisplayText("my string")); | ||
} | ||
|
||
private TestArrayEditor GetNewEditor() | ||
{ | ||
return new TestArrayEditor(typeof(string)); | ||
} | ||
|
||
private class TestArrayEditor : ArrayEditor | ||
{ | ||
public TestArrayEditor(Type type) : base(type) | ||
{ | ||
} | ||
|
||
public Type GetCollectionType() | ||
{ | ||
return base.CollectionType; | ||
} | ||
|
||
public bool CanSelectMultiple() | ||
{ | ||
return base.CanSelectMultipleInstances(); | ||
} | ||
|
||
public bool CanRemove(object value) | ||
{ | ||
return base.CanRemoveInstance(value); | ||
} | ||
|
||
public string GetHelpTopic() | ||
{ | ||
return base.HelpTopic; | ||
} | ||
|
||
public string GetItemDisplayText(object value) | ||
{ | ||
return base.GetDisplayText(value); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe here I would add a few more asserts to test the overridden SetItems, GetItems, CreateCollectionItemType methods in ArrayEditor.