зеркало из https://github.com/dotnet/winforms.git
Add tests for TreeNodeCollection Find method (#3497)
This commit is contained in:
Родитель
13f53be160
Коммит
c7e371c86b
|
@ -269,6 +269,11 @@ namespace System.Windows.Forms
|
|||
|
||||
public TreeNode[] Find(string key, bool searchAllChildren)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key), SR.FindKeyMayNotBeEmptyOrNull);
|
||||
}
|
||||
|
||||
ArrayList foundNodes = FindInternal(key, searchAllChildren, this, new ArrayList());
|
||||
|
||||
//
|
||||
|
|
|
@ -1452,7 +1452,7 @@ namespace System.Windows.Forms.Tests
|
|||
// Don't search all children.
|
||||
Assert.Equal(new Control[] { child2, child3 }, collection.Find(key, searchAllChildren: false));
|
||||
|
||||
// Call again..
|
||||
// Call again.
|
||||
Assert.Equal(new Control[] { child2, child3 }, collection.Find(key, searchAllChildren: false));
|
||||
}
|
||||
|
||||
|
|
|
@ -79,5 +79,96 @@ namespace System.Windows.Forms.Tests
|
|||
collection.Add("text");
|
||||
Assert.Throws<ArgumentOutOfRangeException>("index", () => collection[index] = new TreeNode());
|
||||
}
|
||||
|
||||
[WinFormsTheory]
|
||||
[InlineData("name2")]
|
||||
[InlineData("NAME2")]
|
||||
public void TreeNodeCollection_Find_InvokeKeyExists_ReturnsExpected(string key)
|
||||
{
|
||||
using var treeView = new TreeView();
|
||||
var child1 = new TreeNode
|
||||
{
|
||||
Name = "name1"
|
||||
};
|
||||
var child2 = new TreeNode
|
||||
{
|
||||
Name = "name2"
|
||||
};
|
||||
var child3 = new TreeNode
|
||||
{
|
||||
Name = "name2"
|
||||
};
|
||||
|
||||
var grandchild1 = new TreeNode
|
||||
{
|
||||
Name = "name1"
|
||||
};
|
||||
var grandchild2 = new TreeNode
|
||||
{
|
||||
Name = "name2"
|
||||
};
|
||||
var grandchild3 = new TreeNode
|
||||
{
|
||||
Name = "name2"
|
||||
};
|
||||
child3.Nodes.Add(grandchild1);
|
||||
child3.Nodes.Add(grandchild2);
|
||||
child3.Nodes.Add(grandchild3);
|
||||
TreeNodeCollection collection = treeView.Nodes;
|
||||
collection.Add(child1);
|
||||
collection.Add(child2);
|
||||
collection.Add(child3);
|
||||
|
||||
// Search all children.
|
||||
Assert.Equal(new TreeNode[] { child2, child3, grandchild2, grandchild3 }, collection.Find(key, searchAllChildren: true));
|
||||
|
||||
// Call again.
|
||||
Assert.Equal(new TreeNode[] { child2, child3, grandchild2, grandchild3 }, collection.Find(key, searchAllChildren: true));
|
||||
|
||||
// Don't search all children.
|
||||
Assert.Equal(new TreeNode[] { child2, child3 }, collection.Find(key, searchAllChildren: false));
|
||||
|
||||
// Call again.
|
||||
Assert.Equal(new TreeNode[] { child2, child3 }, collection.Find(key, searchAllChildren: false));
|
||||
}
|
||||
|
||||
[WinFormsTheory]
|
||||
[InlineData("NoSuchName")]
|
||||
[InlineData("abcd")]
|
||||
[InlineData("abcde")]
|
||||
[InlineData("abcdef")]
|
||||
public void TreeNodeCollection_Find_InvokeNoSuchKey_ReturnsEmpty(string key)
|
||||
{
|
||||
using var treeView = new TreeView();
|
||||
var child1 = new TreeNode
|
||||
{
|
||||
Name = "name1"
|
||||
};
|
||||
var child2 = new TreeNode
|
||||
{
|
||||
Name = "name2"
|
||||
};
|
||||
var child3 = new TreeNode
|
||||
{
|
||||
Name = "name2"
|
||||
};
|
||||
TreeNodeCollection collection = treeView.Nodes;
|
||||
collection.Add(child1);
|
||||
collection.Add(child2);
|
||||
collection.Add(child3);
|
||||
|
||||
Assert.Empty(collection.Find(key, searchAllChildren: true));
|
||||
Assert.Empty(collection.Find(key, searchAllChildren: false));
|
||||
}
|
||||
|
||||
[WinFormsTheory]
|
||||
[CommonMemberData(nameof(CommonTestHelper.GetNullOrEmptyStringTheoryData))]
|
||||
public void TreeNodeCollection_Find_NullOrEmptyKey_ThrowsArgumentNullException(string key)
|
||||
{
|
||||
using var treeView = new TreeView();
|
||||
var collection = treeView.Nodes;
|
||||
Assert.Throws<ArgumentNullException>("key", () => collection.Find(key, searchAllChildren: true));
|
||||
Assert.Throws<ArgumentNullException>("key", () => collection.Find(key, searchAllChildren: false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче