Skip to content

Commit a244326

Browse files
committed
add test SearchContent_ShouldFindMatch_WhenBinarySearchMisses_LinearSearchEnabled
1 parent d2ff0e1 commit a244326

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

GitContentSearch.Tests/GitContentSearcherTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,39 @@ public void SearchContent_ShouldLogError_WhenEarliestCommitIsMoreRecentThanLates
178178
Assert.Contains("Error: The earliest commit is more recent than the latest commit.", logContent);
179179
}
180180
}
181+
182+
[Fact]
183+
public void SearchContent_ShouldFindMatch_WhenBinarySearchMisses_LinearSearchEnabled()
184+
{
185+
// Arrange
186+
var gitHelperMock = new Mock<IGitHelper>();
187+
var fileSearcherMock = new Mock<IFileSearcher>();
188+
189+
// Simulate commits
190+
var commits = new[] { "commit5", "commit4", "commit3", "commit2", "commit1" };
191+
gitHelperMock.Setup(g => g.GetGitCommits(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(commits);
192+
gitHelperMock.Setup(g => g.GetCommitTime(It.IsAny<string>())).Returns("2023-08-21 12:00:00");
193+
194+
// Simulate binary search skipping over commit2 where the string actually appears
195+
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit1")), It.IsAny<string>())).Returns(false);
196+
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit2")), It.IsAny<string>())).Returns(true); // Should be found by linear search
197+
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit3")), It.IsAny<string>())).Returns(false);
198+
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit4")), It.IsAny<string>())).Returns(false);
199+
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit5")), It.IsAny<string>())).Returns(false);
200+
201+
using (var stringWriter = new StringWriter())
202+
{
203+
// Act
204+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), disableLinearSearch: false, logWriter: stringWriter); // Linear search enabled
205+
gitContentSearcher.SearchContent("dummy/path.txt", "search string");
206+
207+
// Assert
208+
var logContent = stringWriter.ToString();
209+
210+
// Check the log for the correct first and last appearance commits
211+
Assert.Contains("Search string \"search string\" first appears in commit commit2.", logContent);
212+
Assert.Contains("Search string \"search string\" last appears in commit commit2.", logContent);
213+
}
214+
}
181215
}
182216
}

0 commit comments

Comments
 (0)