From d0f7589354d052e8567d2aaf036476288bde6a9a Mon Sep 17 00:00:00 2001 From: rowo360 <59574371+rowo360@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:22:44 +0200 Subject: [PATCH 1/2] Refactor TestSelection class to implement IEnumerable interface and use Hashset internally as collection --- .../Presenters/DisplayStrategy.cs | 3 +- .../Presenters/GroupDisplayStrategy.cs | 9 +- .../Presenters/TreeViewPresenter.cs | 2 +- .../FixtureListDisplayStrategyTests.cs | 3 +- .../TestListDisplayStrategyTests.cs | 3 +- .../TestTree/TestSelection_WithCheckboxes.cs | 3 +- .../TestSelection_WithoutCheckboxes.cs | 3 +- .../TestTree/TreeViewPresenterTests.cs | 101 ++++++++++++++++++ src/TestModel/model/TestFilter.cs | 7 +- src/TestModel/model/TestModel.cs | 2 +- src/TestModel/model/TestNode.cs | 10 -- src/TestModel/model/TestSelection.cs | 84 +++++++++------ src/TestModel/tests/TestSelectionTests.cs | 10 -- 13 files changed, 171 insertions(+), 69 deletions(-) diff --git a/src/TestCentric/testcentric.gui/Presenters/DisplayStrategy.cs b/src/TestCentric/testcentric.gui/Presenters/DisplayStrategy.cs index 890286419..a40179b47 100644 --- a/src/TestCentric/testcentric.gui/Presenters/DisplayStrategy.cs +++ b/src/TestCentric/testcentric.gui/Presenters/DisplayStrategy.cs @@ -13,6 +13,7 @@ namespace TestCentric.Gui.Presenters using Views; using Elements; using System.IO; + using System.Linq; /// /// DisplayStrategy is the abstract base for the various @@ -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) diff --git a/src/TestCentric/testcentric.gui/Presenters/GroupDisplayStrategy.cs b/src/TestCentric/testcentric.gui/Presenters/GroupDisplayStrategy.cs index 6edf92f23..4f2f433c3 100644 --- a/src/TestCentric/testcentric.gui/Presenters/GroupDisplayStrategy.cs +++ b/src/TestCentric/testcentric.gui/Presenters/GroupDisplayStrategy.cs @@ -8,6 +8,7 @@ namespace TestCentric.Gui.Presenters { + using System.Linq; using Model; using Views; @@ -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 { @@ -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) @@ -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) diff --git a/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs b/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs index 3d0a98f85..e74a467e1 100644 --- a/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs +++ b/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs @@ -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; }; diff --git a/src/TestCentric/tests/Presenters/FixtureListDisplayStrategyTests.cs b/src/TestCentric/tests/Presenters/FixtureListDisplayStrategyTests.cs index db55f6168..22c478382 100644 --- a/src/TestCentric/tests/Presenters/FixtureListDisplayStrategyTests.cs +++ b/src/TestCentric/tests/Presenters/FixtureListDisplayStrategyTests.cs @@ -6,6 +6,7 @@ namespace TestCentric.Gui.Presenters { using System.Collections.Generic; + using System.Linq; using System.Windows.Forms; using NSubstitute; using NUnit.Framework; @@ -200,7 +201,7 @@ private static void AssertTreeNodeAndTestGroup(List 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) diff --git a/src/TestCentric/tests/Presenters/TestListDisplayStrategyTests.cs b/src/TestCentric/tests/Presenters/TestListDisplayStrategyTests.cs index e2c101f2d..2f40eb6e2 100644 --- a/src/TestCentric/tests/Presenters/TestListDisplayStrategyTests.cs +++ b/src/TestCentric/tests/Presenters/TestListDisplayStrategyTests.cs @@ -6,6 +6,7 @@ namespace TestCentric.Gui.Presenters { using System.Collections.Generic; + using System.Linq; using System.Windows.Forms; using System.Xml; using NSubstitute; @@ -287,7 +288,7 @@ private static void AssertTreeNodeAndTestGroup(List 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) diff --git a/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithCheckboxes.cs b/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithCheckboxes.cs index af41c5596..81fcec33c 100644 --- a/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithCheckboxes.cs +++ b/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithCheckboxes.cs @@ -4,6 +4,7 @@ // *********************************************************************** using System.Collections.Generic; +using System.Linq; using System.Windows.Forms; using NSubstitute; using NUnit.Framework; @@ -60,7 +61,7 @@ public void WhenCheckedNodesChange_SelectedTestsAreSet() _view.CheckedNodes.Returns(new List( new[] { TEST_CASE_TREE_NODE })); _view.AfterCheck += Raise.Event(TEST_CASE_TREE_NODE); - _model.Received().SelectedTests = Arg.Is(s => s.Count == 1 && s.Contains(TEST_CASE_NODE)); + _model.Received().SelectedTests = Arg.Is(s => s.Count() == 1 && s.Contains(TEST_CASE_NODE)); } [Test] diff --git a/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithoutCheckboxes.cs b/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithoutCheckboxes.cs index f51c8e851..6da4d7a37 100644 --- a/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithoutCheckboxes.cs +++ b/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithoutCheckboxes.cs @@ -10,6 +10,7 @@ namespace TestCentric.Gui.Presenters.TestTree { + using System.Linq; using Elements; using TestCentric.Gui.Model; using Views; @@ -52,7 +53,7 @@ public void WhenSelectedNodeChanges_ActiveItemIsSet(TreeNode treeNode, TestNode public void WhenSelectedNodeChanges_SelectedTestsAreSet(TreeNode treeNode, TestNode testNode) { _view.SelectedNodeChanged += Raise.Event(treeNode); - _model.Received().SelectedTests = Arg.Is(s => s.Count == 1 && s[0] == testNode); + _model.Received().SelectedTests = Arg.Is(s => s.Count() == 1 && s.First() == testNode); } } } diff --git a/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs b/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs index b6683db85..c4205da67 100644 --- a/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs +++ b/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs @@ -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 { @@ -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 checkedNodes = new List(); + _view.CheckedNodes.Returns(checkedNodes); + + // 2. Act + _view.AfterCheck += Raise.Event((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("") }; + var treeNode2 = new TreeNode() { Tag = new TestNode("") }; + + IList checkedNodes = new List() { treeNode1, treeNode2 }; + _view.CheckedNodes.Returns(checkedNodes); + + // 2. Act + _view.AfterCheck += Raise.Event((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(""); + var subTestNode2 = new TestNode(""); + var testGroup = new TestGroup("Category_1") { subTestNode1, subTestNode2 }; + + var treeNode1 = new TreeNode() { Tag = testGroup }; + + IList checkedNodes = new List() { treeNode1 }; + _view.CheckedNodes.Returns(checkedNodes); + + // 2. Act + _view.AfterCheck += Raise.Event((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(""); + var subTestNode2 = new TestNode(""); + var testGroup = new TestGroup("Category_1") { subTestNode1, subTestNode2 }; + + var treeNode1 = new TreeNode() { Tag = testGroup }; + var treeNode2 = new TreeNode() { Tag = new TestNode("") }; + + IList checkedNodes = new List() { treeNode1, treeNode2 }; + _view.CheckedNodes.Returns(checkedNodes); + + // 2. Act + _view.AfterCheck += Raise.Event((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(""); + var subTestNode2 = new TestNode(""); + var testGroup = new TestGroup("Category_1") { subTestNode1, subTestNode2 }; + + var treeNode1 = new TreeNode() { Tag = testGroup }; + var treeNode2 = new TreeNode() { Tag = subTestNode1 }; + + IList checkNodes = new List() { treeNode1, treeNode2 }; + _view.CheckedNodes.Returns(checkNodes); + + // 2. Act + _view.AfterCheck += Raise.Event((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() diff --git a/src/TestModel/model/TestFilter.cs b/src/TestModel/model/TestFilter.cs index 1e7f1daac..f044646ef 100644 --- a/src/TestModel/model/TestFilter.cs +++ b/src/TestModel/model/TestFilter.cs @@ -4,6 +4,7 @@ // *********************************************************************** using System.Collections.Generic; +using System.Linq; using System.Text; using System.Xml; @@ -46,10 +47,10 @@ public static TestFilter MakeIdFilter(TestNode test) return new TestFilter($"{test.Id}"); } - public static TestFilter MakeIdFilter(IList testNodes) + public static TestFilter MakeIdFilter(IEnumerable testNodes) { - if (testNodes.Count == 1) - return MakeIdFilter(testNodes[0]); + if (testNodes.Count() == 1) + return MakeIdFilter(testNodes.First()); StringBuilder sb = new StringBuilder(""); diff --git a/src/TestModel/model/TestModel.cs b/src/TestModel/model/TestModel.cs index b3466f09e..ce197fadd 100644 --- a/src/TestModel/model/TestModel.cs +++ b/src/TestModel/model/TestModel.cs @@ -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) { diff --git a/src/TestModel/model/TestNode.cs b/src/TestModel/model/TestNode.cs index 5c8266e1f..724e1e84c 100644 --- a/src/TestModel/model/TestNode.cs +++ b/src/TestModel/model/TestNode.cs @@ -166,19 +166,9 @@ public string[] GetAllProperties(bool displayHiddenProperties) } public TestSelection Select(TestNodePredicate predicate) - { - return Select(predicate, null); - } - - public TestSelection Select(TestNodePredicate predicate, Comparison comparer) { var selection = new TestSelection(); - Accumulate(selection, this, predicate); - - if (comparer != null) - selection.Sort(comparer); - return selection; } diff --git a/src/TestModel/model/TestSelection.cs b/src/TestModel/model/TestSelection.cs index 10fe707cc..862cbfd5f 100644 --- a/src/TestModel/model/TestSelection.cs +++ b/src/TestModel/model/TestSelection.cs @@ -4,29 +4,37 @@ // *********************************************************************** using System; +using System.Collections; using System.Collections.Generic; +using System.Linq; namespace TestCentric.Gui.Model { - public delegate string GroupingFunction(TestNode testNode); - /// - /// TestSelection is an List of TestNodes, implementing + /// TestSelection is a collection of TestNodes, implementing /// the ITestItem interface so that the entire selection /// may be selected for execution. /// - public class TestSelection : List, ITestItem + public class TestSelection : IEnumerable, ITestItem { - public TestSelection() : base() { } + private HashSet _nodes; + + public TestSelection() + { + _nodes = new HashSet(); + } - public TestSelection(IEnumerable tests) : base(tests) { } + public TestSelection(IEnumerable tests) + { + _nodes = new HashSet(tests); + } public virtual string Name { get { return GetType().Name; } } public virtual TestFilter GetTestFilter() { - return Count == 1 - ? this[0].GetTestFilter() // This allows nodes with special handling for filters to apply it. + return _nodes.Count == 1 + ? this.First().GetTestFilter() // This allows nodes with special handling for filters to apply it. : TestFilter.MakeIdFilter(this); } @@ -36,38 +44,44 @@ public virtual TestFilter GetTestFilter() /// public void RemoveId(string id) { - for (int index = 0; index < Count; index++) - if (this[index].Id == id) - { - RemoveAt(index); - break; - } + TestNode testNode = this.FirstOrDefault(x => x.Id == id); + if (testNode != null) + _nodes.Remove(testNode); } - // TODO: Not used now but wait till all experimental classes are ported - //public IDictionary GroupBy(GroupingFunction groupingFunction) - //{ - // var groups = new Dictionary(); + /// + /// Add a ITestItem to the TestSelection (either a TestNode or a TestSelection) + /// + public void Add(ITestItem item) + { + if (item == null) + throw new ArgumentNullException("item"); - // foreach (TestNode testNode in this) - // { - // var groupName = groupingFunction(testNode); + if (item is TestNode testNode) + _nodes.Add(testNode); + else if (item is TestSelection testSelection) + { + foreach (var subItem in testSelection) + _nodes.Add(subItem); + } + } - // TestSelection group = null; - // if (!groups.ContainsKey(groupName)) - // { - // group = new TestSelection(); - // groups[groupName] = group; - // } - // else - // { - // group = groups[groupName]; - // } + /// + /// Clear all nodes from collection + /// + public void Clear() + { + _nodes.Clear(); + } - // group.Add(testNode); - // } + public IEnumerator GetEnumerator() + { + return _nodes.GetEnumerator(); + } - // return groups; - //} + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } } } diff --git a/src/TestModel/tests/TestSelectionTests.cs b/src/TestModel/tests/TestSelectionTests.cs index c3c89ec0f..e19e21d47 100644 --- a/src/TestModel/tests/TestSelectionTests.cs +++ b/src/TestModel/tests/TestSelectionTests.cs @@ -21,16 +21,6 @@ public void CreateSelection() _selection.Add(MakeTestNode("3", "Harry", "Passed")); } - [Test] - public void SortByNameTest() - { - _selection.Sort((x, y) => x.Name.CompareTo(y.Name)); - - Assert.That(_selection[0].Name, Is.EqualTo("Dick")); - Assert.That(_selection[1].Name, Is.EqualTo("Harry")); - Assert.That(_selection[2].Name, Is.EqualTo("Tom")); - } - [Test] public void RemoveTestNode() { From a8b4ec4fc7f5a5657d6441ee1ffa9d4d315bb780 Mon Sep 17 00:00:00 2001 From: rowo360 <59574371+rowo360@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:22:44 +0200 Subject: [PATCH 2/2] Refactor TestSelection class to implement IEnumerable interface and use Hashset internally as collection --- .../Presenters/DisplayStrategy.cs | 3 +- .../Presenters/GroupDisplayStrategy.cs | 9 +- .../Presenters/TreeViewPresenter.cs | 2 +- .../FixtureListDisplayStrategyTests.cs | 3 +- .../TestListDisplayStrategyTests.cs | 3 +- .../TestTree/TestSelection_WithCheckboxes.cs | 3 +- .../TestSelection_WithoutCheckboxes.cs | 3 +- .../TestTree/TreeViewPresenterTests.cs | 101 ++++++++++++++++++ src/TestModel/model/TestFilter.cs | 7 +- src/TestModel/model/TestModel.cs | 2 +- src/TestModel/model/TestNode.cs | 10 -- src/TestModel/model/TestSelection.cs | 84 +++++++++------ src/TestModel/tests/TestSelectionTests.cs | 10 -- 13 files changed, 171 insertions(+), 69 deletions(-) diff --git a/src/TestCentric/testcentric.gui/Presenters/DisplayStrategy.cs b/src/TestCentric/testcentric.gui/Presenters/DisplayStrategy.cs index 890286419..a40179b47 100644 --- a/src/TestCentric/testcentric.gui/Presenters/DisplayStrategy.cs +++ b/src/TestCentric/testcentric.gui/Presenters/DisplayStrategy.cs @@ -13,6 +13,7 @@ namespace TestCentric.Gui.Presenters using Views; using Elements; using System.IO; + using System.Linq; /// /// DisplayStrategy is the abstract base for the various @@ -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) diff --git a/src/TestCentric/testcentric.gui/Presenters/GroupDisplayStrategy.cs b/src/TestCentric/testcentric.gui/Presenters/GroupDisplayStrategy.cs index 6edf92f23..4f2f433c3 100644 --- a/src/TestCentric/testcentric.gui/Presenters/GroupDisplayStrategy.cs +++ b/src/TestCentric/testcentric.gui/Presenters/GroupDisplayStrategy.cs @@ -8,6 +8,7 @@ namespace TestCentric.Gui.Presenters { + using System.Linq; using Model; using Views; @@ -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 { @@ -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) @@ -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) diff --git a/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs b/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs index 3d0a98f85..e74a467e1 100644 --- a/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs +++ b/src/TestCentric/testcentric.gui/Presenters/TreeViewPresenter.cs @@ -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; }; diff --git a/src/TestCentric/tests/Presenters/FixtureListDisplayStrategyTests.cs b/src/TestCentric/tests/Presenters/FixtureListDisplayStrategyTests.cs index db55f6168..22c478382 100644 --- a/src/TestCentric/tests/Presenters/FixtureListDisplayStrategyTests.cs +++ b/src/TestCentric/tests/Presenters/FixtureListDisplayStrategyTests.cs @@ -6,6 +6,7 @@ namespace TestCentric.Gui.Presenters { using System.Collections.Generic; + using System.Linq; using System.Windows.Forms; using NSubstitute; using NUnit.Framework; @@ -200,7 +201,7 @@ private static void AssertTreeNodeAndTestGroup(List 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) diff --git a/src/TestCentric/tests/Presenters/TestListDisplayStrategyTests.cs b/src/TestCentric/tests/Presenters/TestListDisplayStrategyTests.cs index e2c101f2d..2f40eb6e2 100644 --- a/src/TestCentric/tests/Presenters/TestListDisplayStrategyTests.cs +++ b/src/TestCentric/tests/Presenters/TestListDisplayStrategyTests.cs @@ -6,6 +6,7 @@ namespace TestCentric.Gui.Presenters { using System.Collections.Generic; + using System.Linq; using System.Windows.Forms; using System.Xml; using NSubstitute; @@ -287,7 +288,7 @@ private static void AssertTreeNodeAndTestGroup(List 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) diff --git a/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithCheckboxes.cs b/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithCheckboxes.cs index af41c5596..81fcec33c 100644 --- a/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithCheckboxes.cs +++ b/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithCheckboxes.cs @@ -4,6 +4,7 @@ // *********************************************************************** using System.Collections.Generic; +using System.Linq; using System.Windows.Forms; using NSubstitute; using NUnit.Framework; @@ -60,7 +61,7 @@ public void WhenCheckedNodesChange_SelectedTestsAreSet() _view.CheckedNodes.Returns(new List( new[] { TEST_CASE_TREE_NODE })); _view.AfterCheck += Raise.Event(TEST_CASE_TREE_NODE); - _model.Received().SelectedTests = Arg.Is(s => s.Count == 1 && s.Contains(TEST_CASE_NODE)); + _model.Received().SelectedTests = Arg.Is(s => s.Count() == 1 && s.Contains(TEST_CASE_NODE)); } [Test] diff --git a/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithoutCheckboxes.cs b/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithoutCheckboxes.cs index f51c8e851..6da4d7a37 100644 --- a/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithoutCheckboxes.cs +++ b/src/TestCentric/tests/Presenters/TestTree/TestSelection_WithoutCheckboxes.cs @@ -10,6 +10,7 @@ namespace TestCentric.Gui.Presenters.TestTree { + using System.Linq; using Elements; using TestCentric.Gui.Model; using Views; @@ -52,7 +53,7 @@ public void WhenSelectedNodeChanges_ActiveItemIsSet(TreeNode treeNode, TestNode public void WhenSelectedNodeChanges_SelectedTestsAreSet(TreeNode treeNode, TestNode testNode) { _view.SelectedNodeChanged += Raise.Event(treeNode); - _model.Received().SelectedTests = Arg.Is(s => s.Count == 1 && s[0] == testNode); + _model.Received().SelectedTests = Arg.Is(s => s.Count() == 1 && s.First() == testNode); } } } diff --git a/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs b/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs index b6683db85..c4205da67 100644 --- a/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs +++ b/src/TestCentric/tests/Presenters/TestTree/TreeViewPresenterTests.cs @@ -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 { @@ -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 checkedNodes = new List(); + _view.CheckedNodes.Returns(checkedNodes); + + // 2. Act + _view.AfterCheck += Raise.Event((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("") }; + var treeNode2 = new TreeNode() { Tag = new TestNode("") }; + + IList checkedNodes = new List() { treeNode1, treeNode2 }; + _view.CheckedNodes.Returns(checkedNodes); + + // 2. Act + _view.AfterCheck += Raise.Event((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(""); + var subTestNode2 = new TestNode(""); + var testGroup = new TestGroup("Category_1") { subTestNode1, subTestNode2 }; + + var treeNode1 = new TreeNode() { Tag = testGroup }; + + IList checkedNodes = new List() { treeNode1 }; + _view.CheckedNodes.Returns(checkedNodes); + + // 2. Act + _view.AfterCheck += Raise.Event((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(""); + var subTestNode2 = new TestNode(""); + var testGroup = new TestGroup("Category_1") { subTestNode1, subTestNode2 }; + + var treeNode1 = new TreeNode() { Tag = testGroup }; + var treeNode2 = new TreeNode() { Tag = new TestNode("") }; + + IList checkedNodes = new List() { treeNode1, treeNode2 }; + _view.CheckedNodes.Returns(checkedNodes); + + // 2. Act + _view.AfterCheck += Raise.Event((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(""); + var subTestNode2 = new TestNode(""); + var testGroup = new TestGroup("Category_1") { subTestNode1, subTestNode2 }; + + var treeNode1 = new TreeNode() { Tag = testGroup }; + var treeNode2 = new TreeNode() { Tag = subTestNode1 }; + + IList checkNodes = new List() { treeNode1, treeNode2 }; + _view.CheckedNodes.Returns(checkNodes); + + // 2. Act + _view.AfterCheck += Raise.Event((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() diff --git a/src/TestModel/model/TestFilter.cs b/src/TestModel/model/TestFilter.cs index 1e7f1daac..f044646ef 100644 --- a/src/TestModel/model/TestFilter.cs +++ b/src/TestModel/model/TestFilter.cs @@ -4,6 +4,7 @@ // *********************************************************************** using System.Collections.Generic; +using System.Linq; using System.Text; using System.Xml; @@ -46,10 +47,10 @@ public static TestFilter MakeIdFilter(TestNode test) return new TestFilter($"{test.Id}"); } - public static TestFilter MakeIdFilter(IList testNodes) + public static TestFilter MakeIdFilter(IEnumerable testNodes) { - if (testNodes.Count == 1) - return MakeIdFilter(testNodes[0]); + if (testNodes.Count() == 1) + return MakeIdFilter(testNodes.First()); StringBuilder sb = new StringBuilder(""); diff --git a/src/TestModel/model/TestModel.cs b/src/TestModel/model/TestModel.cs index b3466f09e..ce197fadd 100644 --- a/src/TestModel/model/TestModel.cs +++ b/src/TestModel/model/TestModel.cs @@ -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) { diff --git a/src/TestModel/model/TestNode.cs b/src/TestModel/model/TestNode.cs index 5c8266e1f..724e1e84c 100644 --- a/src/TestModel/model/TestNode.cs +++ b/src/TestModel/model/TestNode.cs @@ -166,19 +166,9 @@ public string[] GetAllProperties(bool displayHiddenProperties) } public TestSelection Select(TestNodePredicate predicate) - { - return Select(predicate, null); - } - - public TestSelection Select(TestNodePredicate predicate, Comparison comparer) { var selection = new TestSelection(); - Accumulate(selection, this, predicate); - - if (comparer != null) - selection.Sort(comparer); - return selection; } diff --git a/src/TestModel/model/TestSelection.cs b/src/TestModel/model/TestSelection.cs index 10fe707cc..862cbfd5f 100644 --- a/src/TestModel/model/TestSelection.cs +++ b/src/TestModel/model/TestSelection.cs @@ -4,29 +4,37 @@ // *********************************************************************** using System; +using System.Collections; using System.Collections.Generic; +using System.Linq; namespace TestCentric.Gui.Model { - public delegate string GroupingFunction(TestNode testNode); - /// - /// TestSelection is an List of TestNodes, implementing + /// TestSelection is a collection of TestNodes, implementing /// the ITestItem interface so that the entire selection /// may be selected for execution. /// - public class TestSelection : List, ITestItem + public class TestSelection : IEnumerable, ITestItem { - public TestSelection() : base() { } + private HashSet _nodes; + + public TestSelection() + { + _nodes = new HashSet(); + } - public TestSelection(IEnumerable tests) : base(tests) { } + public TestSelection(IEnumerable tests) + { + _nodes = new HashSet(tests); + } public virtual string Name { get { return GetType().Name; } } public virtual TestFilter GetTestFilter() { - return Count == 1 - ? this[0].GetTestFilter() // This allows nodes with special handling for filters to apply it. + return _nodes.Count == 1 + ? this.First().GetTestFilter() // This allows nodes with special handling for filters to apply it. : TestFilter.MakeIdFilter(this); } @@ -36,38 +44,44 @@ public virtual TestFilter GetTestFilter() /// public void RemoveId(string id) { - for (int index = 0; index < Count; index++) - if (this[index].Id == id) - { - RemoveAt(index); - break; - } + TestNode testNode = this.FirstOrDefault(x => x.Id == id); + if (testNode != null) + _nodes.Remove(testNode); } - // TODO: Not used now but wait till all experimental classes are ported - //public IDictionary GroupBy(GroupingFunction groupingFunction) - //{ - // var groups = new Dictionary(); + /// + /// Add a ITestItem to the TestSelection (either a TestNode or a TestSelection) + /// + public void Add(ITestItem item) + { + if (item == null) + throw new ArgumentNullException("item"); - // foreach (TestNode testNode in this) - // { - // var groupName = groupingFunction(testNode); + if (item is TestNode testNode) + _nodes.Add(testNode); + else if (item is TestSelection testSelection) + { + foreach (var subItem in testSelection) + _nodes.Add(subItem); + } + } - // TestSelection group = null; - // if (!groups.ContainsKey(groupName)) - // { - // group = new TestSelection(); - // groups[groupName] = group; - // } - // else - // { - // group = groups[groupName]; - // } + /// + /// Clear all nodes from collection + /// + public void Clear() + { + _nodes.Clear(); + } - // group.Add(testNode); - // } + public IEnumerator GetEnumerator() + { + return _nodes.GetEnumerator(); + } - // return groups; - //} + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } } } diff --git a/src/TestModel/tests/TestSelectionTests.cs b/src/TestModel/tests/TestSelectionTests.cs index c3c89ec0f..e19e21d47 100644 --- a/src/TestModel/tests/TestSelectionTests.cs +++ b/src/TestModel/tests/TestSelectionTests.cs @@ -21,16 +21,6 @@ public void CreateSelection() _selection.Add(MakeTestNode("3", "Harry", "Passed")); } - [Test] - public void SortByNameTest() - { - _selection.Sort((x, y) => x.Name.CompareTo(y.Name)); - - Assert.That(_selection[0].Name, Is.EqualTo("Dick")); - Assert.That(_selection[1].Name, Is.EqualTo("Harry")); - Assert.That(_selection[2].Name, Is.EqualTo("Tom")); - } - [Test] public void RemoveTestNode() {