From 9a1cb822d1981ddafe2d520bd42b308fc7cc132c Mon Sep 17 00:00:00 2001 From: David Sparer Date: Mon, 17 Oct 2016 13:14:29 -0600 Subject: [PATCH] Created tests for the NodeSearcher --- mRemoteNGTests/Tree/NodeSearcherTests.cs | 94 ++++++++++++++++++++++++ mRemoteNGTests/mRemoteNGTests.csproj | 1 + mRemoteV1/Tree/NodeSearcher.cs | 24 ++++-- 3 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 mRemoteNGTests/Tree/NodeSearcherTests.cs diff --git a/mRemoteNGTests/Tree/NodeSearcherTests.cs b/mRemoteNGTests/Tree/NodeSearcherTests.cs new file mode 100644 index 00000000..dbf6cf4b --- /dev/null +++ b/mRemoteNGTests/Tree/NodeSearcherTests.cs @@ -0,0 +1,94 @@ +using System.Linq; +using mRemoteNG.Connection; +using mRemoteNG.Container; +using mRemoteNG.Tree; +using mRemoteNG.Tree.Root; +using NUnit.Framework; + + +namespace mRemoteNGTests.Tree +{ + public class NodeSearcherTests + { + private NodeSearcher _nodeSearcher; + private ContainerInfo _folder1; + private ContainerInfo _folder2; + private ConnectionInfo _con1; + private ConnectionInfo _con2; + private ConnectionInfo _con3; + private ConnectionInfo _con4; + private ConnectionInfo _con5; + + [OneTimeSetUp] + public void OnetimeSetup() + { + var connectionTreeModel = SetupConnectionTreeModel(); + _nodeSearcher = new NodeSearcher(connectionTreeModel); + } + + [Test] + public void SearchByNameReturnsAllExpectedMatches() + { + var matches = _nodeSearcher.SearchByName("folder"); + Assert.That(matches.ToList(), Is.EquivalentTo(new[] {_folder1, _folder2})); + } + + [Test] + public void NextMatchAdvancesTheIterator() + { + _nodeSearcher.SearchByName("folder"); + var match1 = _nodeSearcher.CurrentMatch; + var match2 = _nodeSearcher.NextMatch(); + Assert.That(match1, Is.Not.EqualTo(match2)); + } + + [Test] + public void PreviousMatchRollsBackTheIterator() + { + _nodeSearcher.SearchByName("con"); + var match1 = _nodeSearcher.CurrentMatch; + _nodeSearcher.NextMatch(); + var match2 = _nodeSearcher.PreviousMatch(); + Assert.That(match1, Is.EqualTo(match2)); + } + + [Test] + public void SearchingWithEmptyStringReturnsNoMatches() + { + var matches = _nodeSearcher.SearchByName(""); + Assert.That(matches.Count(), Is.EqualTo(0)); + } + + private ConnectionTreeModel SetupConnectionTreeModel() + { + /* + * Tree: + * Root + * |--- folder1 + * | |--- con1 + * | L--- con2 + * |--- folder2 + * | |--- con3 + * | L--- con4 + * L--- con5 + * + */ + var connectionTreeModel = new ConnectionTreeModel(); + var root = new RootNodeInfo(RootNodeType.Connection); + _folder1 = new ContainerInfo { Name = "folder1"}; + _con1 = new ConnectionInfo { Name = "con1"}; + _con2 = new ConnectionInfo { Name = "con2"}; + _folder2 = new ContainerInfo { Name = "folder2" }; + _con3 = new ConnectionInfo { Name = "con3" }; + _con4 = new ConnectionInfo { Name = "con4" }; + _con5 = new ConnectionInfo { Name = "con5" }; + + connectionTreeModel.AddRootNode(root); + root.AddChildRange(new [] { _folder1, _folder2, _con5 }); + _folder1.AddChildRange(new [] { _con1, _con2 }); + _folder2.AddChildRange(new[] { _con3, _con4 }); + + return connectionTreeModel; + } + } +} \ No newline at end of file diff --git a/mRemoteNGTests/mRemoteNGTests.csproj b/mRemoteNGTests/mRemoteNGTests.csproj index 3ee05503..8e037e4a 100644 --- a/mRemoteNGTests/mRemoteNGTests.csproj +++ b/mRemoteNGTests/mRemoteNGTests.csproj @@ -139,6 +139,7 @@ + Form diff --git a/mRemoteV1/Tree/NodeSearcher.cs b/mRemoteV1/Tree/NodeSearcher.cs index 35958579..40cd4aa7 100644 --- a/mRemoteV1/Tree/NodeSearcher.cs +++ b/mRemoteV1/Tree/NodeSearcher.cs @@ -5,7 +5,7 @@ using mRemoteNG.Connection; namespace mRemoteNG.Tree { - internal class NodeSearcher + public class NodeSearcher { private readonly ConnectionTreeModel _connectionTreeModel; @@ -18,7 +18,7 @@ namespace mRemoteNG.Tree _connectionTreeModel = connectionTreeModel; } - internal IEnumerable SearchByName(string searchText) + public IEnumerable SearchByName(string searchText) { ResetMatches(); if (searchText == "") return Matches; @@ -33,22 +33,34 @@ namespace mRemoteNG.Tree return Matches; } - internal ConnectionInfo NextMatch() + public ConnectionInfo NextMatch() { var currentMatchIndex = Matches.IndexOf(CurrentMatch); - if (currentMatchIndex < Matches.Count-1) + if (!CurrentMatchIsTheLastMatchInTheList()) CurrentMatch = Matches[currentMatchIndex + 1]; return CurrentMatch; } - internal ConnectionInfo PreviousMatch() + private bool CurrentMatchIsTheLastMatchInTheList() { var currentMatchIndex = Matches.IndexOf(CurrentMatch); - if (currentMatchIndex > 0) + return currentMatchIndex >= Matches.Count - 1; + } + + public ConnectionInfo PreviousMatch() + { + var currentMatchIndex = Matches.IndexOf(CurrentMatch); + if (!CurrentMatchIsTheFirstMatchInTheList()) CurrentMatch = Matches[currentMatchIndex - 1]; return CurrentMatch; } + private bool CurrentMatchIsTheFirstMatchInTheList() + { + var currentMatchIndex = Matches.IndexOf(CurrentMatch); + return currentMatchIndex <= 0; + } + private void ResetMatches() { Matches = new List();