コード例 #1
0
ファイル: AhoMatcher.cs プロジェクト: kzemek/FileScanner
        private KeyValuePair<Match, int> Match(TextReader reader, int position)
        {
            while (reader.Peek() != -1)
            {
                char c = (char) reader.Read();

                ++position;

                var patternMatch = _machine.Feed(c);
                if (patternMatch != null)
                {
                    var match = new Match(position - patternMatch.Length, patternMatch);
                    return new KeyValuePair<Match, int>(match, position);
                }
            }

            return new KeyValuePair<Match, int>(null, position);
        }
コード例 #2
0
ファイル: SearcherTest.cs プロジェクト: kzemek/FileScanner
        public void Search_SearcheesAndMatches_MatchedSearcheesInResult()
        {
            // Arrange
            var matchingReader = new StreamReader(new MemoryStream());
            var matchingSearcheeMock = _mockFactory.CreateMock<ISearchee>();
            matchingSearcheeMock.Expects.Any
                    .GetProperty(x => x.Reader).WillReturn(matchingReader);

            var notMatchingReader = new StreamReader(new MemoryStream());
            var notMatchingSearcheeMock = _mockFactory.CreateMock<ISearchee>();
            notMatchingSearcheeMock.Expects.Any
                .GetProperty(x => x.Reader).WillReturn(notMatchingReader);

            const string SearchPhrase = "s";
            var match = new Match(0, SearchPhrase);

            var matcherMock = _mockFactory.CreateMock<IMatcher>(MockStyle.RecursiveStub);
            matcherMock.Expects.Any
                .MethodWith(x => x.Matches(matchingReader)).WillReturn(new[] { match });

            _matcherFactory.Expects.Any
                .Method(x => x.Create(null, MatcherFactory.MatchAlgorithm.AhoCorasick))
                .WithAnyArguments().WillReturn(matcherMock.MockObject);

            var searchees = new[] { matchingSearcheeMock.MockObject, notMatchingSearcheeMock.MockObject };

            var phrases = new[] { "phrase" };

            _preprocessor.Expects.Any.Method(x => x.GetNormalizedPhrase(null)).WithAnyArguments().WillReturn(string.Empty);
            _preprocessor.Expects.Any.Method(x => x.GetVariations(null)).WithAnyArguments().WillReturn(phrases);

            // Act
            var results = _searcher.Search(searchees, phrases);

            // Assert
            _mockFactory.VerifyAllExpectationsHaveBeenMet();
            Assert.AreEqual(searchees.Count(), results.ProcessedSearcheesCount);
            Assert.AreEqual(1, results.Searchees.Count());
            Assert.AreSame(matchingSearcheeMock.MockObject, results.Searchees.First().Searchee);
            Assert.AreEqual(1, results.Searchees.First().Matches.Count());
            Assert.AreSame(match, results.Searchees.First().Matches.First());
        }