public void FileSearcher_FindsAMatch_FiresEvents() { const string fileName = "Test"; const string fileContent = "Speedy thing goes in, speedy thing comes out!"; var dataSource = new TestDataSource(fileName, fileContent, Encoding.UTF8); var fileSearcher = new FileSearcher( new Regex("speedy", RegexOptions.None), new[] { dataSource }, false, 0); using var monitor = fileSearcher.Monitor(); fileSearcher.Begin(); fileSearcher.Wait(); monitor.OccurredEvents.Should().Contain(x => x.EventName == nameof(fileSearcher.MatchFound)) .Which.Parameters[1].Should().BeOfType <MatchFoundEventArgs>() .Which.Matches.Should().BeEquivalentTo(new[] { new SearchMatch(1, fileContent, 22, 6, null, null) }); monitor.OccurredEvents.Should().Contain(x => x.EventName == nameof(fileSearcher.Completed)) .Which.Parameters[1].Should().BeOfType <CompletedEventArgs>() .Which.FailureReason.Should().BeNull(); monitor.OccurredEvents.Should().NotContain(x => x.EventName == nameof(fileSearcher.Error)); }
static void DoTest( string searchPattern, IDataSource dataSource, int contextLineCount, params SearchMatch[] expectedResults) { var searcher = new FileSearcher( new Regex(Regex.Escape(searchPattern)), new[] { dataSource }, false, contextLineCount, c_MaxContextLength); using var monitor = searcher.Monitor(); searcher.Begin(); searcher.Wait(); if (expectedResults.Length > 0) { monitor.OccurredEvents.Should().Contain(x => x.EventName == nameof(searcher.MatchFound)) .Which.Parameters[1].Should().BeOfType <MatchFoundEventArgs>() .Which.Matches.Should() .SatisfyRespectively( expectedResults.Select <SearchMatch, Action <SearchMatch> >( e => m => m.Should().BeEquivalentTo(e))); } else { monitor.OccurredEvents.Should().NotContain(x => x.EventName == nameof(searcher.MatchFound)); } monitor.OccurredEvents.Should().Contain(x => x.EventName == nameof(searcher.Completed)) .Which.Parameters[1].Should().BeOfType <CompletedEventArgs>() .Which.FailureReason.Should().BeNull(); monitor.OccurredEvents.Should().NotContain(x => x.EventName == nameof(searcher.Error)); }