From 06667b7aa76e4f81ba18d8a62225f7a279b5c49f Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Tue, 19 May 2020 20:10:46 +1000 Subject: [PATCH] Add tests for TopLevelFolderSearch behaviour --- Flow.Launcher.Test/Plugins/ExplorerTest.cs | 69 ++++++++++++++++++- .../Search/SearchManager.cs | 23 +++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs diff --git a/Flow.Launcher.Test/Plugins/ExplorerTest.cs b/Flow.Launcher.Test/Plugins/ExplorerTest.cs index 6f3c41aff..780feae3f 100644 --- a/Flow.Launcher.Test/Plugins/ExplorerTest.cs +++ b/Flow.Launcher.Test/Plugins/ExplorerTest.cs @@ -1,13 +1,41 @@ +using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.Explorer; +using Flow.Launcher.Plugin.Explorer.Search; using Flow.Launcher.Plugin.Explorer.Search.WindowsIndex; using NUnit.Framework; using System; +using System.Collections.Generic; namespace Flow.Launcher.Test.Plugins { [TestFixture] public class ExplorerTest { + private List MethodWindowsIndexSearchReturnsZeroResults(string dummyString) + { + return new List(); + } + + private List MethodDirectoryInfoClassSearchReturnsTwoResults(string dummyString) + { + return new List + { + new Result + { + Title="Result 1" + }, + + new Result + { + Title="Result 2" + } + }; + } + + private bool MethodIndexExistsReturnsTrue(string dummyString) => true; + + private bool MethodIndexExistsReturnsFalse(string dummyString) => false; + [TestCase("C:\\Dropbox", "directory='file:C:\\Dropbox'")] public void GivenWindowsIndexSearch_WhenProvidedFolderPath_ThenQueryWhereRestrictionsShouldUseDirectoryString(string path, string expectedString) { @@ -33,6 +61,7 @@ namespace Flow.Launcher.Test.Plugins //When var queryString = queryConstructor.QueryForTopLevelDirectorySearch(folderPath); + // Then Assert.IsTrue(queryString == expectedString, $"Expected QueryWhereRestrictions string: {expectedString}{Environment.NewLine} " + $"Actual string was: {queryString}{Environment.NewLine}"); @@ -47,6 +76,7 @@ namespace Flow.Launcher.Test.Plugins //When var resultString = queryConstructor.QueryWhereRestrictionsForAllFilesAndFoldersSearch(); + // Then Assert.IsTrue(resultString == expectedString, $"Expected QueryWhereRestrictions string: {expectedString}{Environment.NewLine} " + $"Actual string was: {resultString}{Environment.NewLine}"); @@ -64,12 +94,49 @@ namespace Flow.Launcher.Test.Plugins //When var resultString = queryConstructor.QueryForAllFilesAndFolders(userSearchString); + // Then Assert.IsTrue(resultString == expectedString, $"Expected query string: {expectedString}{Environment.NewLine} " + $"Actual string was: {resultString}{Environment.NewLine}"); } - public void GivenWindowsIndexSearch_WhenReturnedNilAndIsNotIndexed_ThenSearchMethodShouldContinueDirectoryInfoClassSearch() { } + [TestCase] + public void GivenWindowsIndexSearch_WhenReturnedZeroResultsAndIsNotIndexed_ThenSearchMethodShouldContinueDirectoryInfoClassSearch() + { + // Given + var searchManager = new SearchManager(); + + // When + var results = searchManager.TopLevelFolderSearch( + MethodWindowsIndexSearchReturnsZeroResults, + MethodDirectoryInfoClassSearchReturnsTwoResults, + MethodIndexExistsReturnsFalse, + "path string not used"); + + // Then + Assert.IsTrue(results.Count == 2, + $"Expected to have 2 results from DirectoryInfoClassSearch {Environment.NewLine} " + + $"Actual number of results is {results.Count} {Environment.NewLine}"); + } + + [TestCase] + public void GivenWindowsIndexSearch_WhenReturnedZeroResultsAndIsIndexed_ThenSearchMethodShouldNotContinueDirectoryInfoClassSearch() + { + // Given + var searchManager = new SearchManager(); + + // When + var results = searchManager.TopLevelFolderSearch( + MethodWindowsIndexSearchReturnsZeroResults, + MethodDirectoryInfoClassSearchReturnsTwoResults, + MethodIndexExistsReturnsTrue, + "path string not used"); + + // Then + Assert.IsTrue(results.Count == 0, + $"Expected to have 0 results because location is indexed {Environment.NewLine} " + + $"Actual number of results is {results.Count} {Environment.NewLine}"); + } public void GivenWindowsIndexSearch_WhenSearchPatternHotKeyIsSearchAll_ThenQueryWhereRestrictionsShouldUseScopeString() { } } diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs new file mode 100644 index 000000000..c2a250ae7 --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Flow.Launcher.Plugin.Explorer.Search +{ + public class SearchManager + { + public List TopLevelFolderSearch( + Func> windowsIndexSearch, + Func> directoryInfoClassSearch, + Func indexExists, + string path) + { + var results = windowsIndexSearch(path); + + if (results.Count == 0 && !indexExists(path)) + return directoryInfoClassSearch(path); + + return results; + } + } +}