Created tests for the NodeSearcher

This commit is contained in:
David Sparer
2016-10-17 13:14:29 -06:00
parent 8ad5f3426c
commit 9a1cb822d1
3 changed files with 113 additions and 6 deletions

View File

@@ -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;
}
}
}

View File

@@ -139,6 +139,7 @@
<Compile Include="Security\CryptographyProviderFactoryTests.cs" />
<Compile Include="Security\EncryptedSecureStringTests.cs" />
<Compile Include="Security\LegacyRijndaelCryptographyProviderTests.cs" />
<Compile Include="Tree\NodeSearcherTests.cs" />
<Compile Include="UI\Controls\CustomListViewTests.cs" />
<Compile Include="UI\Controls\TestForm.cs">
<SubType>Form</SubType>

View File

@@ -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<ConnectionInfo> SearchByName(string searchText)
public IEnumerable<ConnectionInfo> 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<ConnectionInfo>();