Add methods to test for previous directory location

This commit is contained in:
Jeremy Wu 2020-05-26 20:18:18 +10:00
parent 436cfe2e2b
commit 2d4e2f5662
3 changed files with 53 additions and 25 deletions

View file

@ -160,5 +160,30 @@ namespace Flow.Launcher.Plugin.SharedCommands
return false; return false;
} }
///<summary>
/// Gets the previous level directory from a path string.
/// Checks that previous level directory exists and returns it
/// as a path string, or empty string if doesnt exit
///</summary>
public static string GetPreviousExistingDirectory(Func<string, bool> locationExists, string path)
{
var previousDirectoryPath = "";
int index = path.LastIndexOf('\\');
if (index > 0 && index < (path.Length - 1))
{
previousDirectoryPath = path.Substring(0, index + 1);
if (!locationExists(path))
{
return "";
}
}
else
{
return "";
}
return previousDirectoryPath;
}
} }
} }

View file

@ -37,9 +37,9 @@ namespace Flow.Launcher.Test.Plugins
private bool MethodIndexExistsReturnsFalse(string dummyString) => false; private bool MethodIndexExistsReturnsFalse(string dummyString) => false;
private bool LocationExistsReturnsTrue(string dummyString) => true; private bool PreviousLocationExistsReturnsTrue(string dummyString) => true;
private bool LocationNotExistReturnsFalse(string dummyString) => false; private bool PreviousLocationNotExistReturnsFalse(string dummyString) => false;
[TestCase("C:\\Dropbox", "directory='file:C:\\Dropbox'")] [TestCase("C:\\Dropbox", "directory='file:C:\\Dropbox'")]
public void GivenWindowsIndexSearch_WhenProvidedFolderPath_ThenQueryWhereRestrictionsShouldUseDirectoryString(string path, string expectedString) public void GivenWindowsIndexSearch_WhenProvidedFolderPath_ThenQueryWhereRestrictionsShouldUseDirectoryString(string path, string expectedString)
@ -200,17 +200,30 @@ namespace Flow.Launcher.Test.Plugins
} }
[TestCase(@"C:\Dropbox\Drop", @"C:\Dropbox")] [TestCase(@"C:\Dropbox\Drop", true, @"C:\Dropbox\")]
[TestCase(@"C:\Dropbox\Drop\App", @"C:\Dropbox\Drop")] [TestCase(@"C:\Dropbox\Drop\App", true, @"C:\Dropbox\Drop\")]
public void GivenAPartialPath_WhenPreviousLevelDirectoryExists_ThenShouldReturnThePreviousDirectoryPathString() [TestCase(@"C:\Dropbox\Drop", false, "")]
public void GivenAPartialPath_WhenPreviousLevelDirectoryExists_ThenShouldReturnThePreviousDirectoryPathString(
string path, bool previousDirectoryExists, string expectedString)
{ {
// When
Func<string, bool> previousLocationExists = null;
if (previousDirectoryExists)
{
previousLocationExists = PreviousLocationExistsReturnsTrue;
}
else
{
previousLocationExists = PreviousLocationNotExistReturnsFalse;
}
} // Given
var previousDirectoryPath = FilesFolders.GetPreviousExistingDirectory(previousLocationExists, path);
[TestCase(@"C:\Dropbox\Drop", "")]
public void GivenAPartialPath_WhenPreviousLevelDirectoryNotExists_ThenShouldReturnEmptyString()
{
//Then
Assert.IsTrue(previousDirectoryPath == expectedString,
$"Expected path string: {expectedString} {Environment.NewLine} " +
$"Actual path string is {previousDirectoryPath} {Environment.NewLine}");
} }
public void GivenWindowsIndexSearch_WhenSearchPatternHotKeyIsSearchAll_ThenQueryWhereRestrictionsShouldUseScopeString() { } public void GivenWindowsIndexSearch_WhenSearchPatternHotKeyIsSearchAll_ThenQueryWhereRestrictionsShouldUseScopeString() { }

View file

@ -113,22 +113,12 @@ namespace Flow.Launcher.Plugin.Explorer.Search
if (locationExists(querySearchString)) if (locationExists(querySearchString))
return ResultManager.CreateOpenCurrentFolderResult(querySearchString, false); return ResultManager.CreateOpenCurrentFolderResult(querySearchString, false);
var partialPath = ""; var previousDirectoryPath = FilesFolders.GetPreviousExistingDirectory(FilesFolders.LocationExists, querySearchString);
int index = querySearchString.LastIndexOf('\\');
if (index > 0 && index < (querySearchString.Length - 1))
{
partialPath = querySearchString.Substring(0, index + 1);
if (!locationExists(partialPath))
{
return null;
}
}
else
{
return null;
}
return ResultManager.CreateOpenCurrentFolderResult(partialPath, true); if (string.IsNullOrEmpty(previousDirectoryPath))
return null;
return ResultManager.CreateOpenCurrentFolderResult(previousDirectoryPath, true);
} }
} }
} }