11using Moq ;
2+ using System ;
3+ using System . IO ;
4+ using Xunit ;
25
36namespace GitContentSearch . Tests
47{
58 public class GitContentSearcherTests
69 {
710 [ Fact ]
8- public void SearchContent_ShouldLogCorrectly_ForFoundString ( )
11+ public void SearchContent_ShouldLogFirstAndLastAppearance_Correctly ( )
912 {
1013 // Arrange
1114 var gitHelperMock = new Mock < IGitHelper > ( ) ;
1215 var fileSearcherMock = new Mock < IFileSearcher > ( ) ;
1316
14- gitHelperMock . Setup ( g => g . GetGitCommits ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
15- . Returns ( new [ ] { "commit1" , "commit2" } ) ;
16- gitHelperMock . Setup ( g => g . GetCommitTime ( It . IsAny < string > ( ) ) )
17- . Returns ( "2023-08-21" ) ;
17+ // Simulate commits
18+ var commits = new [ ] { "commit1" , "commit2" , "commit3" , "commit4" , "commit5" } ;
19+ gitHelperMock . Setup ( g => g . GetGitCommits ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) ) . Returns ( commits ) ;
20+ gitHelperMock . Setup ( g => g . GetCommitTime ( It . IsAny < string > ( ) ) ) . Returns ( "2023-08-21 12:00:00 " ) ;
1821
19- fileSearcherMock . Setup ( f => f . SearchInFile ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
20- . Returns ( true ) ;
22+ // Simulate string appearances based on the specific commit
23+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . Is < string > ( file => file . Contains ( "commit1" ) ) , It . IsAny < string > ( ) ) ) . Returns ( false ) ;
24+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . Is < string > ( file => file . Contains ( "commit2" ) ) , It . IsAny < string > ( ) ) ) . Returns ( true ) ;
25+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . Is < string > ( file => file . Contains ( "commit3" ) ) , It . IsAny < string > ( ) ) ) . Returns ( true ) ;
26+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . Is < string > ( file => file . Contains ( "commit4" ) ) , It . IsAny < string > ( ) ) ) . Returns ( true ) ;
27+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . Is < string > ( file => file . Contains ( "commit5" ) ) , It . IsAny < string > ( ) ) ) . Returns ( false ) ;
2128
2229 var gitContentSearcher = new GitContentSearcher ( gitHelperMock . Object , fileSearcherMock . Object ) ;
2330
@@ -28,24 +35,62 @@ public void SearchContent_ShouldLogCorrectly_ForFoundString()
2835
2936 // Assert
3037 var logContent = stringWriter . ToString ( ) ;
31- Assert . Contains ( "found: True" , logContent ) ;
38+
39+ // Check the log for the correct first and last appearance commits
40+ Assert . Contains ( "Search string \" search string\" first appears in commit commit2." , logContent ) ;
41+ Assert . Contains ( "Search string \" search string\" last appears in commit commit4." , logContent ) ;
42+ }
43+ }
44+
45+ [ Fact ]
46+ public void SearchContent_ShouldLogCorrectly_WhenStringNotFound ( )
47+ {
48+ // Arrange
49+ var gitHelperMock = new Mock < IGitHelper > ( ) ;
50+ var fileSearcherMock = new Mock < IFileSearcher > ( ) ;
51+
52+ // Simulate commits
53+ var commits = new [ ] { "commit1" , "commit2" , "commit3" , "commit4" , "commit5" } ;
54+ gitHelperMock . Setup ( g => g . GetGitCommits ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) ) . Returns ( commits ) ;
55+ gitHelperMock . Setup ( g => g . GetCommitTime ( It . IsAny < string > ( ) ) ) . Returns ( "2023-08-21 12:00:00" ) ;
56+
57+ // Simulate no appearances of the string
58+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) ) . Returns ( false ) ;
59+
60+ var gitContentSearcher = new GitContentSearcher ( gitHelperMock . Object , fileSearcherMock . Object ) ;
61+
62+ using ( var stringWriter = new StringWriter ( ) )
63+ {
64+ // Act
65+ gitContentSearcher . SearchContent ( "dummy/path.txt" , "search string" , logWriter : stringWriter ) ;
66+
67+ // Assert
68+ var logContent = stringWriter . ToString ( ) ;
69+
70+ // Check the log to confirm that the string was not found in any commits
71+ Assert . Contains ( "Search string \" search string\" does not appear in any of the checked commits." , logContent ) ;
3272 }
3373 }
3474
3575 [ Fact ]
36- public void SearchContent_ShouldLogCorrectly_ForNotFoundString ( )
76+ public void SearchContent_ShouldLogCorrectly_WhenStringAppearsInSingleCommit ( )
3777 {
3878 // Arrange
3979 var gitHelperMock = new Mock < IGitHelper > ( ) ;
4080 var fileSearcherMock = new Mock < IFileSearcher > ( ) ;
4181
42- gitHelperMock . Setup ( g => g . GetGitCommits ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
43- . Returns ( new [ ] { "commit1" , "commit2" } ) ;
44- gitHelperMock . Setup ( g => g . GetCommitTime ( It . IsAny < string > ( ) ) )
45- . Returns ( "2023-08-21" ) ;
82+ // Simulate commits
83+ var commits = new [ ] { "commit1" , "commit2" , "commit3" , "commit4" , "commit5" } ;
84+ gitHelperMock . Setup ( g => g . GetGitCommits ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) ) . Returns ( commits ) ;
85+ gitHelperMock . Setup ( g => g . GetCommitTime ( It . IsAny < string > ( ) ) ) . Returns ( "2023-08-21 12:00:00" ) ;
86+
87+ // Simulate string appearance: Found only in commit3
88+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . Is < string > ( file => file . Contains ( "commit1" ) ) , It . IsAny < string > ( ) ) ) . Returns ( false ) ;
89+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . Is < string > ( file => file . Contains ( "commit2" ) ) , It . IsAny < string > ( ) ) ) . Returns ( false ) ;
90+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . Is < string > ( file => file . Contains ( "commit3" ) ) , It . IsAny < string > ( ) ) ) . Returns ( true ) ;
91+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . Is < string > ( file => file . Contains ( "commit4" ) ) , It . IsAny < string > ( ) ) ) . Returns ( false ) ;
92+ fileSearcherMock . Setup ( f => f . SearchInFile ( It . Is < string > ( file => file . Contains ( "commit5" ) ) , It . IsAny < string > ( ) ) ) . Returns ( false ) ;
4693
47- fileSearcherMock . Setup ( f => f . SearchInFile ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
48- . Returns ( false ) ;
4994
5095 var gitContentSearcher = new GitContentSearcher ( gitHelperMock . Object , fileSearcherMock . Object ) ;
5196
@@ -56,7 +101,10 @@ public void SearchContent_ShouldLogCorrectly_ForNotFoundString()
56101
57102 // Assert
58103 var logContent = stringWriter . ToString ( ) ;
59- Assert . Contains ( "found: False" , logContent ) ;
104+
105+ // Check the log for the correct first and last appearance commits
106+ Assert . Contains ( "Search string \" search string\" first appears in commit commit3." , logContent ) ;
107+ Assert . Contains ( "Search string \" search string\" last appears in commit commit3." , logContent ) ;
60108 }
61109 }
62110 }
0 commit comments