public string Get(string keyword, string linkToSearch, string engines) { string result; if (cache.TryGetValue <string>(keyword + linkToSearch + engines, out result)) { return(result); } StringBuilder returnString = new StringBuilder(); ISearchEngine searchEngine; foreach (string engine in engines.Split(",")) { searchEngine = new SearchEngineFactory().Create(engine); searchEngine.Process(keyword, linkToSearch); if (searchEngine.ResultItems != null && searchEngine.ResultItems.Count() > 0) { returnString.AppendLine($"[{searchEngine.Name} : {string.Join(", ", searchEngine.ResultItems.Where(x => x.IsMatch).Select(x => x.Index))}]"); } else { returnString.AppendLine($"[{searchEngine.Name} : 0]"); } } result = returnString.ToString(); cache.Set(keyword + linkToSearch + engines, result, System.TimeSpan.FromMinutes(60)); return(result); }
public async void FileIndexingEvents_FileMovedToWatchedDirectory_Raised(string eventName) { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var sourceFolder = new TestFolder()) { using (var file = new TestFile("foo", sourceFolder.Path)) { var fileName = file.Name; using (var destFolder = new TestFolder()) { var destFolderPath = destFolder.Path; using (new TestFile("bar", destFolderPath)) { engine.Add(destFolderPath); await Task.Delay(100); engine.AddHandler(eventName, args => { tcs.TrySetResult(Path.GetFullPath($"{destFolderPath}\\{fileName}") == args.Path); }); file.Move(destFolderPath); Assert.True(await tcs.Task); } } } } }
private static async void TestEncoding(string filePath, string searchQuery, int expectedLineNumber, int expectedPosition) { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); engine.FileIndexingEnded += args => { var wordEntries = engine.Search(searchQuery).ToList(); if (wordEntries.Count != 1) { tcs.TrySetResult(false); } var entry = wordEntries.First(); var result = expectedLineNumber == entry.LineNumber && expectedPosition == entry.Position; tcs.TrySetResult(result); }; var isAdded = engine.Add(filePath); Assert.True(isAdded); Assert.True(await tcs.Task); }
public async void FilePathChanged_NotEmptyWatchedFolderRenamed_Raised() { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var folder = new TestFolder()) { var folderPath = folder.Path; using (new TestFile("foo", folderPath)) { engine.Add(folderPath); await Task.Delay(100); var newName = $"new-test-{Guid.NewGuid()}"; engine.FilePathChanged += args => { var expectedPath = Path.GetFullPath($".\\{newName}"); tcs.TrySetResult(expectedPath == args.Path); }; folder.Rename(newName); Assert.True(await tcs.Task); } } }
public frmMain() { InitializeComponent(); cmbSearchEngine.DataSource = SearchEngineFactory.GetAllSearchEngines(); cmbSearchEngine.SelectedIndex = 0; }
public async void PathWatchingEnded_EmptyWatchedFolderRemoved_Raised() { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var folder = new TestFolder()) { var folderPath = folder.Path; using (var file = new TestFile("foo", folderPath)) { engine.Add(folder.Path); await Task.Delay(100); engine.PathWatchingEnded += args => { tcs.TrySetResult(Path.GetFullPath(folderPath) == args.Path); }; file.Delete(); folder.Delete(); Assert.True(await tcs.Task); } } }
public void Remove_RemoveAdded_ReturnsTrue() { var engine = SearchEngineFactory.New(); engine.Add(TestTextFiles.OneLineFile); Assert.True(engine.Remove(TestTextFiles.OneLineFile)); }
static async Task <int> Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Exactly 2 parameters are required (KEYWORDS and URL)"); Console.WriteLine("Example: ConsoleApp \"e-settlements\" www.sympli.com"); return(-1); } var descriptors = new SearchEngineFactory(new Cache.Factory(), new Client()).Engines; Console.WriteLine("Found Search Engines:"); foreach (var d in descriptors) { Console.WriteLine(d); } var descriptor = descriptors.First(engine => engine.Available); Console.WriteLine($"Using First Available ({descriptor.Name})"); var engine = descriptor.Get(); var result = await engine.SearchAsync(args[0], args[1]); Console.WriteLine($"The URL {args[1]} has been found in the following results:"); Console.WriteLine(result); return(0); }
public async void FileIndexingEvents_EmptyFolderIndexed_NotRaised(string eventName) { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var folder = new TestFolder()) { var folderPath = folder.Path; #pragma warning disable CS4014 Task.Run(async() => { await Task.Delay(1000); tcs.TrySetResult(true); }); #pragma warning restore CS4014 engine.Add(folderPath); await Task.Delay(100); engine.AddHandler(eventName, args => { tcs.TrySetResult(false); }); Assert.True(await tcs.Task); } }
public async void FileIndexingEvents_FileCreatedToWatchedDirectory_Raised(string eventName) { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var folder = new TestFolder()) { var fileName = TestFile.GenerateFileName(); var folderPath = folder.Path; engine.Add(folderPath); await Task.Delay(100); engine.AddHandler(eventName, args => { tcs.TrySetResult(Path.GetFullPath($"{folderPath}\\{fileName}") == args.Path); }); using (new TestFile("foo", folderPath, fileName)) { Assert.True(await tcs.Task); } await Task.Delay(100); } }
public async void FileIndexingEvents_NotIndexedFileMovedToSubFolder_Raised(string eventName) { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var folder = new TestFolder()) { var folderPath = folder.Path; using (var file = new TestFile("foo", folderPath)) { var fileName = file.Name; using (var subFolder = new TestFolder(folderPath)) { var subFolderPath = subFolder.Path; engine.Add(subFolderPath); await Task.Delay(300); engine.AddHandler(eventName, args => { var expectedPath = Path.GetFullPath($"{subFolderPath}\\{fileName}"); tcs.TrySetResult(expectedPath == args.Path); }); file.Move(subFolderPath); Assert.True(await tcs.Task); } } } }
public void Add_TwoFiles_ReturnsTrue() { var engine = SearchEngineFactory.New(); Assert.True(engine.Add(TestTextFiles.OneLineFile)); Assert.True(engine.Add(TestTextFiles.TwoLinesFile)); }
public async void FileUpdateInitiated_FileInFolderChanged_Raised() { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var folder = new TestFolder()) { var folderPath = folder.Path; using (var file = new TestFile("foo", folderPath)) { var filePath = file.Path; engine.Add(folderPath); await Task.Delay(100); engine.FileUpdateInitiated += args => { tcs.TrySetResult(Path.GetFullPath(filePath) == args.Path); }; file.ChangeContent("bar"); Assert.True(await tcs.Task); } } }
public void Add_AlreadyAdded_ReturnsFalse() { var engine = SearchEngineFactory.New(); engine.Add(TestTextFiles.OneLineFile); Assert.False(engine.Add(TestTextFiles.OneLineFile)); }
public async void FileRemovingEvents_FileInFolderRemoved_Raised(string eventName) { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var folder = new TestFolder()) { var folderPath = folder.Path; using (var subFolder = new TestFolder(folderPath)) { var subFolderPath = subFolder.Path; using (var file = new TestFile("foo", subFolderPath)) { var filePath = file.Path; engine.Add(folderPath); await Task.Delay(100); engine.AddHandler(eventName, args => { tcs.TrySetResult(Path.GetFullPath(filePath) == args.Path); }); file.Delete(); Assert.True(await tcs.Task); } } } }
private void Init() { var descriptors = new SearchEngineFactory(new Cache.Factory(), new Client()).Engines; EngineCombo.Items.AddRange(descriptors); var descriptor = descriptors.First(engine => engine.Available); EngineCombo.SelectedItem = descriptor; }
private void searchToolStripMenuItem_Click(object sender, EventArgs e) { searchEngine = SearchEngineFactory.Get(this.searchEngineData); searchEngine.BoxesBeingSearched += SearchEngineBoxesBeingSearched; searchEngine.FoundWord += SearchEngineFoundWord; this.ClearFormState(); SetButtonStateToOperating(); workerThread.RunWorkerAsync(); }
public Tests() { var mock = new Mock <IClient>(); mock.Setup(client => client.GetAsync(It.IsAny <string>())).ReturnsAsync((Func <string, string>)Respond); var client = mock.Object; factory = new SearchEngineFactory(new Cache.Factory(), client); }
public async Task <List <Route> > GetRoutesBetween(Airport departure, Airport destination) { var searchEngine = SearchEngineFactory <Airport> .GetSearchEngine(SearchAlgorithm.Dijkstra); var map = await searchEngine.GetMap(new AirportNode(departure), new AirportNode(destination)); var requiredRoutes = await ProcessRoutesInPath(map); return(requiredRoutes); }
public void SearchEngineWordsAreNotFound(string letters, int width, string[] expectedWords) { // arrange var data = new SearchEngineData(letters, width, expectedWords); var searchEngine = SearchEngineFactory.Get(data); // act searchEngine.CheckAllPossibleWords(); var wordsNotFound = data.ExpectedWords.Where(expectedWord => !searchEngine.FoundWords.Contains(expectedWord)).ToList(); // assert Assert.IsNotEmpty(wordsNotFound, "All words were found"); }
public void SearchEngineFindsExpectedWordsInTestData(string testName) { // arrange var data = new WordSearchResourceData(testName); var searchEngine = SearchEngineFactory.Get(data); // act searchEngine.CheckAllPossibleWords(); var wordsNotFound = data.ExpectedWords.Where(expectedWord => !searchEngine.FoundWords.Contains(expectedWord)).ToList(); // assert Assert.IsEmpty(wordsNotFound, "Expected words weren't found", wordsNotFound); }
public void exist_engines() { // arrange var factory = new SearchEngineFactory(); var expectedEngines = new string[] { "Bing", "Google" }; // act var existEngines = factory.GetSearchEngines() .Keys.Select(k => k); // assert Assert.IsTrue(existEngines.SequenceEqual(expectedEngines)); }
private SearchEngineFactory GetFactoryInstance(string engine) { SearchEngineFactory factory = null; if (engine == Constants.Google.ENGINE) { factory = new GoogleSearchEngineFactory(); } else { factory = new BingSearchEngineFactory(); } return(factory); }
public async void FileIndexingEvents_FileAdded_Raised(string eventName) { var filePath = TestTextFiles.OneLineFile; var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); engine.AddHandler(eventName, args => { tcs.TrySetResult(Path.GetFullPath(filePath) == args.Path); }); Assert.True(engine.Add(filePath)); Assert.True(await tcs.Task); }
public void RunWholeSearchEngineUsingTestDataAndJustOutput(string testName) { // arrange var data = new WordSearchResourceData(testName); var searchEngine = SearchEngineFactory.Get(data); var resultsOutput = new SearchResultsOutput(); searchEngine.BoxesBeingSearched += resultsOutput.OutputBoxesBeingSearched; searchEngine.FoundWord += resultsOutput.OutputFoundWord; // act searchEngine.CheckAllPossibleWords(); // assert resultsOutput.OutputAllFoundWords(searchEngine.FoundWords); }
private static async void IndexFileAndDoAction(string filePath, Action <ISearchEngine, string> action) { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); engine.FileIndexingEnded += args => { action(engine, args.Path); tcs.TrySetResult(true); }; var isAdded = engine.Add(filePath); Assert.True(isAdded); Assert.True(await tcs.Task); }
public async void PathWatchingStarted_EmptyFolderAdded_Raised() { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var folder = new TestFolder()) { var folderPath = folder.Path; engine.PathWatchingStarted += args => { tcs.TrySetResult(Path.GetFullPath(folderPath) == args.Path); }; engine.Add(folderPath); await Task.Delay(100); Assert.True(await tcs.Task); } }
public async void FilePathChanged_FileRenamed_Raised() { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var file = new TestFile("foo")) { engine.Add(file.Path); await Task.Delay(100); var newName = $"new-test-{Guid.NewGuid()}.txt"; engine.FilePathChanged += args => { tcs.TrySetResult(Path.GetFullPath(".\\" + newName) == args.Path); }; file.Rename(newName); Assert.True(await tcs.Task); } }
public async void FileIndexingEvents_FileChanged_Raised(string eventName) { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var file = new TestFile("foo")) { var filePath = file.Path; engine.Add(filePath); await Task.Delay(100); engine.AddHandler(eventName, args => { tcs.TrySetResult(Path.GetFullPath(filePath) == args.Path); }); file.ChangeContent("bar"); Assert.True(await tcs.Task); } }
public async void PathWatchingStarted_FolderWithFileAddedAgain_NotRaised() { var engine = SearchEngineFactory.New(); var tcs = new TaskCompletionSource <bool>(); using (var folder = new TestFolder()) { using (new TestFile("foo", folder.Path)) { engine.Add(folder.Path); await Task.Delay(100); } } engine.PathWatchingStarted += args => { tcs.TrySetResult(false); }; #pragma warning disable CS4014 Task.Run(async() => { await Task.Delay(1000); tcs.TrySetResult(true); }); #pragma warning restore CS4014 using (var folder = new TestFolder()) { var folderPath = folder.Path; using (new TestFile("foo", folderPath)) { Assert.True(await tcs.Task); } } }