Skip to content

Commit

Permalink
Refactor TestSelection class to implement IEnumerable interface and u…
Browse files Browse the repository at this point in the history
…se Hashset internally as collection
  • Loading branch information
rowo360 committed Oct 6, 2024
1 parent 51a313b commit d0f7589
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace TestCentric.Gui.Presenters
using Views;
using Elements;
using System.IO;
using System.Linq;

/// <summary>
/// DisplayStrategy is the abstract base for the various
Expand Down Expand Up @@ -167,7 +168,7 @@ public TreeNode MakeTreeNode(TestNode testNode, bool recursive)

public string GroupDisplayName(TestGroup group)
{
return string.Format("{0} ({1})", group.Name, group.Count);
return string.Format("{0} ({1})", group.Name, group.Count());
}

public static int CalcImageIndex(ResultState outcome)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace TestCentric.Gui.Presenters
{
using System.Linq;
using Model;
using Views;

Expand Down Expand Up @@ -121,7 +122,7 @@ public void ApplyResultToGroup(ResultNode result)
treeNode.Remove();

// If it was last test in group, remove group
if (oldGroup.Count == 0)
if (oldGroup.Count() == 0)
oldParent.Remove();
else // update old group
{
Expand All @@ -132,12 +133,12 @@ public void ApplyResultToGroup(ResultNode result)
newParent.Text = GroupDisplayName(newGroup);
newParent.Expand();

if (newGroup.Count == 1)
if (newGroup.Count() == 1)
{
_view.Clear();
TreeNode topNode = null;
foreach (var group in _grouping.Groups)
if (group.Count > 0)
if (group.Count() > 0)
{
Add(group.TreeNode);
if (topNode == null)
Expand Down Expand Up @@ -188,7 +189,7 @@ protected void UpdateDisplay()
var treeNode = MakeTreeNode(group, true);
group.TreeNode = treeNode;
treeNode.Expand();
if (group.Count > 0)
if (group.Count() > 0)
{
_view.Add(treeNode);
if (topNode == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private void WireUpEvents()
var selection = new TestSelection();

foreach (var node in checkedNodes)
selection.Add(node.Tag as TestNode);
selection.Add(node.Tag as ITestItem);

_model.SelectedTests = selection;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace TestCentric.Gui.Presenters
{
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using NSubstitute;
using NUnit.Framework;
Expand Down Expand Up @@ -200,7 +201,7 @@ private static void AssertTreeNodeAndTestGroup(List<TreeNode> treeNodes, string
// Assert testGroup
TestGroup testGroup = treeNode.Tag as TestGroup;
Assert.That(testGroup, Is.Not.Null);
Assert.That(testGroup.Count, Is.EqualTo(expectedInGroup));
Assert.That(testGroup.Count(), Is.EqualTo(expectedInGroup));
}

private string CreateTestFixtureXml(string testId, string category)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace TestCentric.Gui.Presenters
{
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using NSubstitute;
Expand Down Expand Up @@ -287,7 +288,7 @@ private static void AssertTreeNodeAndTestGroup(List<TreeNode> treeNodes, string
// Assert testGroup
TestGroup testGroup = treeNode.Tag as TestGroup;
Assert.That(testGroup, Is.Not.Null);
Assert.That(testGroup.Count, Is.EqualTo(expectedInGroup));
Assert.That(testGroup.Count(), Is.EqualTo(expectedInGroup));
}

private string CreateTestcaseXml(string testId, string category)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// ***********************************************************************

using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using NSubstitute;
using NUnit.Framework;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void WhenCheckedNodesChange_SelectedTestsAreSet()
_view.CheckedNodes.Returns(new List<TreeNode>( new[] { TEST_CASE_TREE_NODE }));

_view.AfterCheck += Raise.Event<TreeNodeActionHandler>(TEST_CASE_TREE_NODE);
_model.Received().SelectedTests = Arg.Is<TestSelection>(s => s.Count == 1 && s.Contains(TEST_CASE_NODE));
_model.Received().SelectedTests = Arg.Is<TestSelection>(s => s.Count() == 1 && s.Contains(TEST_CASE_NODE));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace TestCentric.Gui.Presenters.TestTree
{
using System.Linq;
using Elements;
using TestCentric.Gui.Model;
using Views;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void WhenSelectedNodeChanges_ActiveItemIsSet(TreeNode treeNode, TestNode
public void WhenSelectedNodeChanges_SelectedTestsAreSet(TreeNode treeNode, TestNode testNode)
{
_view.SelectedNodeChanged += Raise.Event<TreeNodeActionHandler>(treeNode);
_model.Received().SelectedTests = Arg.Is<TestSelection>(s => s.Count == 1 && s[0] == testNode);
_model.Received().SelectedTests = Arg.Is<TestSelection>(s => s.Count() == 1 && s.First() == testNode);
}
}
}
101 changes: 101 additions & 0 deletions src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using TestCentric.Gui.Model;
using System;
using System.Runtime.InteropServices;
using TestCentric.Gui.Views;
using System.Collections.Generic;
using System.Linq;

namespace TestCentric.Gui.Presenters.TestTree
{
Expand Down Expand Up @@ -92,6 +95,104 @@ public void WhenContextMenuIsDisplayed_DebugCommand_EnabledState_IsUpdated(bool
Assert.That(_view.DebugContextCommand.Enabled, Is.EqualTo(expectedEnabled));
}

[Test]
public void TreeCheckBoxClicked_NoNodeSelected_SelectedTestsInModel_AreEmpty()
{
// 1. Arrange
IList<TreeNode> checkedNodes = new List<TreeNode>();
_view.CheckedNodes.Returns(checkedNodes);

// 2. Act
_view.AfterCheck += Raise.Event<TreeNodeActionHandler>((TreeNode)null);

// 3. Assert
TestSelection testSelection = _model.SelectedTests;
Assert.That(testSelection, Is.Empty);
}

[Test]
public void TreeCheckBoxClicked_TreeNodesAreSelected_AllTests_AreSelected()
{
// 1. Arrange
var treeNode1 = new TreeNode() { Tag = new TestNode("<test-case id='1' />") };
var treeNode2 = new TreeNode() { Tag = new TestNode("<test-case id='2' />") };

IList<TreeNode> checkedNodes = new List<TreeNode>() { treeNode1, treeNode2 };
_view.CheckedNodes.Returns(checkedNodes);

// 2. Act
_view.AfterCheck += Raise.Event<TreeNodeActionHandler>((TreeNode)null);

// 3. Assert
TestSelection testSelection = _model.SelectedTests;
Assert.That(testSelection.Count(), Is.EqualTo(2));
}

[Test]
public void TreeCheckBoxClicked_GroupIsSelected_AllTestsOfGroup_AreSelected()
{
// 1. Arrange
var subTestNode1 = new TestNode("<test-case id='2' />");
var subTestNode2 = new TestNode("<test-case id='3' />");
var testGroup = new TestGroup("Category_1") { subTestNode1, subTestNode2 };

var treeNode1 = new TreeNode() { Tag = testGroup };

IList<TreeNode> checkedNodes = new List<TreeNode>() { treeNode1 };
_view.CheckedNodes.Returns(checkedNodes);

// 2. Act
_view.AfterCheck += Raise.Event<TreeNodeActionHandler>((TreeNode)null);

// 3. Assert
TestSelection testSelection = _model.SelectedTests;
Assert.That(testSelection.Count(), Is.EqualTo(2));
}

[Test]
public void TreeCheckBoxClicked_TreeNodeAndGroupIsSelected_AllTests_AreSelected()
{
// 1. Arrange
var subTestNode1 = new TestNode("<test-case id='1' />");
var subTestNode2 = new TestNode("<test-case id='2' />");
var testGroup = new TestGroup("Category_1") { subTestNode1, subTestNode2 };

var treeNode1 = new TreeNode() { Tag = testGroup };
var treeNode2 = new TreeNode() { Tag = new TestNode("<test-case id='3' />") };

IList<TreeNode> checkedNodes = new List<TreeNode>() { treeNode1, treeNode2 };
_view.CheckedNodes.Returns(checkedNodes);

// 2. Act
_view.AfterCheck += Raise.Event<TreeNodeActionHandler>((TreeNode)null);

// 3. Assert
TestSelection testSelection = _model.SelectedTests;
Assert.That(testSelection.Count(), Is.EqualTo(3));
}

[Test]
public void TreeCheckBoxClicked_TreeNodeInsideGroupIsSelected_AllTests_AreSelected()
{
// 1. Arrange
var subTestNode1 = new TestNode("<test-case id='1' />");
var subTestNode2 = new TestNode("<test-case id='2' />");
var testGroup = new TestGroup("Category_1") { subTestNode1, subTestNode2 };

var treeNode1 = new TreeNode() { Tag = testGroup };
var treeNode2 = new TreeNode() { Tag = subTestNode1 };

IList<TreeNode> checkNodes = new List<TreeNode>() { treeNode1, treeNode2 };
_view.CheckedNodes.Returns(checkNodes);

// 2. Act
_view.AfterCheck += Raise.Event<TreeNodeActionHandler>((TreeNode)null);

// 3. Assert
TestSelection testSelection = _model.SelectedTests;
Assert.That(testSelection.Count(), Is.EqualTo(2));
}

// TODO: Version 1 Test - Make it work if needed.
//[Test]
//public void WhenContextNodeIsNotNull_RunCommandExecutesThatTest()
Expand Down
7 changes: 4 additions & 3 deletions src/TestModel/model/TestFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// ***********************************************************************

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

Expand Down Expand Up @@ -46,10 +47,10 @@ public static TestFilter MakeIdFilter(TestNode test)
return new TestFilter($"<filter><id>{test.Id}</id></filter>");
}

public static TestFilter MakeIdFilter(IList<TestNode> testNodes)
public static TestFilter MakeIdFilter(IEnumerable<TestNode> testNodes)
{
if (testNodes.Count == 1)
return MakeIdFilter(testNodes[0]);
if (testNodes.Count() == 1)
return MakeIdFilter(testNodes.First());

StringBuilder sb = new StringBuilder("<filter><or>");

Expand Down
2 changes: 1 addition & 1 deletion src/TestModel/model/TestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private class TestRunSpecification

public bool DebuggingRequested { get; }

public bool IsEmpty => SelectedTests.Count == 0;
public bool IsEmpty => SelectedTests.Count() == 0;

public TestRunSpecification(TestSelection selectedTests, TestFilter filter, bool debuggingRequested)
{
Expand Down
10 changes: 0 additions & 10 deletions src/TestModel/model/TestNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,9 @@ public string[] GetAllProperties(bool displayHiddenProperties)
}

public TestSelection Select(TestNodePredicate predicate)
{
return Select(predicate, null);
}

public TestSelection Select(TestNodePredicate predicate, Comparison<TestNode> comparer)
{
var selection = new TestSelection();

Accumulate(selection, this, predicate);

if (comparer != null)
selection.Sort(comparer);

return selection;
}

Expand Down
Loading

0 comments on commit d0f7589

Please sign in to comment.