1
+ using AutoFixture ;
2
+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
3
+ using Moq ;
4
+ using System . Collections . Generic ;
5
+ using System . Linq ;
6
+ using TextSearchEngine . DTO ;
7
+ using TextSearchEngine . Interfaces ;
8
+ using TextSearchEngine . Library . FileSearchEngines ;
9
+
10
+ namespace TextSearchEngine . UnitTest
11
+ {
12
+ [ Microsoft . VisualStudio . TestTools . UnitTesting . TestClass ]
13
+ public class FileSearchEngineTest
14
+ {
15
+ private Fixture specimenBuilders ;
16
+ private Mock < IFileProvider > moqFileProvider ;
17
+ private Mock < IFileTextSearcher > moqFileTextSearcher ;
18
+ private Mock < IConsoleProvider > moqConsoleProvider ;
19
+
20
+ private IFileSearchEngine fileSearchEngine ;
21
+
22
+ public FileSearchEngineTest ( )
23
+ {
24
+ specimenBuilders = new Fixture ( ) ;
25
+ }
26
+
27
+ private Mock < IConsoleProvider > ConfigureMockConsoleProvider ( )
28
+ {
29
+ var moq = new Mock < IConsoleProvider > ( ) ;
30
+
31
+ moq . Setup ( m => m . ReadLine ( ) )
32
+ . Returns ( specimenBuilders . Create < string > ( ) ) ;
33
+
34
+ moq . Setup ( m => m . ReadKey ( ) )
35
+ . Returns ( specimenBuilders . Create < string > ( ) ) ;
36
+
37
+ return moq ;
38
+ }
39
+
40
+ private Mock < IFileTextSearcher > ConfigureMockFileTextSearcher ( )
41
+ {
42
+ var moq = new Mock < IFileTextSearcher > ( ) ;
43
+
44
+ moq . Setup ( m => m . SearchOccurrences ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
45
+ . Returns ( specimenBuilders . Create < int > ( ) ) ;
46
+
47
+ return moq ;
48
+ }
49
+
50
+ private Mock < IFileProvider > ConfigureMockFileProvider ( )
51
+ {
52
+ var moq = new Mock < IFileProvider > ( ) ;
53
+
54
+ return moq ;
55
+ }
56
+
57
+ [ TestInitialize ]
58
+ public void TestInitialize ( )
59
+ {
60
+ moqFileProvider = ConfigureMockFileProvider ( ) ;
61
+ moqFileTextSearcher = ConfigureMockFileTextSearcher ( ) ;
62
+ moqConsoleProvider = ConfigureMockConsoleProvider ( ) ;
63
+
64
+ fileSearchEngine = new FileSearchEngine (
65
+ moqFileProvider . Object ,
66
+ moqFileTextSearcher . Object ,
67
+ moqConsoleProvider . Object ) ;
68
+ }
69
+
70
+ [ TestCleanup ]
71
+ public void TestCleanup ( )
72
+ {
73
+ moqConsoleProvider . VerifyNoOtherCalls ( ) ;
74
+ moqFileProvider . VerifyNoOtherCalls ( ) ;
75
+ moqFileTextSearcher . VerifyNoOtherCalls ( ) ;
76
+ }
77
+
78
+ [ TestMethod ]
79
+ public void FileSearchEngine_When_DirectoryHasFilesThatMatch_Then_Performs_Search ( )
80
+ {
81
+ var fileRepresentations = specimenBuilders . CreateMany < FileRepresentation > ( ) . ToList ( ) ;
82
+ moqFileProvider . Setup ( m => m . GetFileRepresentationsFromDirectory ( It . IsAny < string > ( ) ) )
83
+ . Returns ( fileRepresentations ) ;
84
+
85
+ moqConsoleProvider . SetupSequence ( m => m . ReadLine ( ) )
86
+ . Returns ( specimenBuilders . Create < string > ( ) )
87
+ . Returns ( "$end" ) ;
88
+
89
+ fileSearchEngine . StartEngine ( "TestDirectory" ) ;
90
+
91
+ moqFileProvider . Verify ( m => m . GetFileRepresentationsFromDirectory ( It . IsAny < string > ( ) ) , Times . Once ) ;
92
+ moqFileTextSearcher . Verify ( m => m . SearchOccurrences ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) , Times . Exactly ( fileRepresentations . Count ) ) ;
93
+ // We check that the console writes as many lines in this case as there are files in the directory + the initial WriteLine at the beggining of the method
94
+ moqConsoleProvider . Verify ( m => m . WriteLine ( It . IsAny < string > ( ) ) , Times . Exactly ( 1 + fileRepresentations . Count ) ) ;
95
+ // We check that these methods has been called twice as we iterate 2 times through the engine loop
96
+ moqConsoleProvider . Verify ( m => m . ReadLine ( ) , Times . Exactly ( 2 ) ) ;
97
+ moqConsoleProvider . Verify ( m => m . Write ( It . IsAny < string > ( ) ) , Times . Exactly ( 2 ) ) ;
98
+ }
99
+
100
+ [ TestMethod ]
101
+ public void FileSearchEngine_When_DirectoryHasFilesThatDoNotMatch_Then_Performs_Search ( )
102
+ {
103
+ var fileRepresentations = specimenBuilders . CreateMany < FileRepresentation > ( ) . ToList ( ) ;
104
+ moqFileProvider . Setup ( m => m . GetFileRepresentationsFromDirectory ( It . IsAny < string > ( ) ) )
105
+ . Returns ( fileRepresentations ) ;
106
+
107
+ moqFileTextSearcher . Setup ( m => m . SearchOccurrences ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
108
+ . Returns ( 0 ) ;
109
+
110
+ moqConsoleProvider . SetupSequence ( m => m . ReadLine ( ) )
111
+ . Returns ( specimenBuilders . Create < string > ( ) )
112
+ . Returns ( "$end" ) ;
113
+
114
+ fileSearchEngine . StartEngine ( "TestDirectory" ) ;
115
+
116
+ moqFileProvider . Verify ( m => m . GetFileRepresentationsFromDirectory ( It . IsAny < string > ( ) ) , Times . Once ) ;
117
+ moqFileTextSearcher . Verify ( m => m . SearchOccurrences ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) , Times . Exactly ( fileRepresentations . Count ) ) ;
118
+ // We check that the console writes the initial line and the one that informs that no files in the directory match
119
+ moqConsoleProvider . Verify ( m => m . WriteLine ( It . IsAny < string > ( ) ) , Times . Exactly ( 2 ) ) ;
120
+ // We check that these methods has been called twice as we iterate 2 times through the engine loop
121
+ moqConsoleProvider . Verify ( m => m . ReadLine ( ) , Times . Exactly ( 2 ) ) ;
122
+ moqConsoleProvider . Verify ( m => m . Write ( It . IsAny < string > ( ) ) , Times . Exactly ( 2 ) ) ;
123
+ }
124
+
125
+ [ TestMethod ]
126
+ public void FileSearchEngine_When_EmptyDirectorySpecified_Then_Not_Performs_Search ( )
127
+ {
128
+ moqFileProvider . Setup ( m => m . GetFileRepresentationsFromDirectory ( It . IsAny < string > ( ) ) )
129
+ . Returns ( new List < FileRepresentation > ( ) ) ;
130
+
131
+ moqConsoleProvider . SetupSequence ( m => m . ReadLine ( ) )
132
+ . Returns ( specimenBuilders . Create < string > ( ) )
133
+ . Returns ( "$end" ) ;
134
+
135
+ fileSearchEngine . StartEngine ( "TestDirectory" ) ;
136
+
137
+ moqFileProvider . Verify ( m => m . GetFileRepresentationsFromDirectory ( It . IsAny < string > ( ) ) , Times . Once ) ;
138
+ moqFileTextSearcher . Verify ( m => m . SearchOccurrences ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) , Times . Never ) ;
139
+ // We check that the console writes the initial line and the one that informs that no files were found in the directory
140
+ moqConsoleProvider . Verify ( m => m . WriteLine ( It . IsAny < string > ( ) ) , Times . Exactly ( 2 ) ) ;
141
+ // We check that these methods has been called twice as we iterate 2 times through the engine loop
142
+ moqConsoleProvider . Verify ( m => m . ReadLine ( ) , Times . Never ) ;
143
+ moqConsoleProvider . Verify ( m => m . Write ( It . IsAny < string > ( ) ) , Times . Never ) ;
144
+ }
145
+
146
+ [ TestMethod ]
147
+ public void FileSearchEngine_When_EmptySearchTerm_Then_Not_Performs_Search ( )
148
+ {
149
+ var fileRepresentations = specimenBuilders . CreateMany < FileRepresentation > ( ) . ToList ( ) ;
150
+ moqFileProvider . Setup ( m => m . GetFileRepresentationsFromDirectory ( It . IsAny < string > ( ) ) )
151
+ . Returns ( fileRepresentations ) ;
152
+
153
+ moqConsoleProvider . SetupSequence ( m => m . ReadLine ( ) )
154
+ . Returns ( string . Empty )
155
+ . Returns ( "$end" ) ;
156
+
157
+ fileSearchEngine . StartEngine ( "TestDirectory" ) ;
158
+
159
+ moqFileProvider . Verify ( m => m . GetFileRepresentationsFromDirectory ( It . IsAny < string > ( ) ) , Times . Once ) ;
160
+ // We check that the console writes only one line as no searchs are performed in this case
161
+ moqConsoleProvider . Verify ( m => m . WriteLine ( It . IsAny < string > ( ) ) , Times . Once ) ;
162
+ // We check that these methods has been called twice as we iterate 2 times through the engine loop
163
+ moqConsoleProvider . Verify ( m => m . ReadLine ( ) , Times . Exactly ( 2 ) ) ;
164
+ moqConsoleProvider . Verify ( m => m . Write ( It . IsAny < string > ( ) ) , Times . Exactly ( 2 ) ) ;
165
+ }
166
+
167
+ [ TestMethod ]
168
+ public void FileSearchEngine_When_EndEngineStringRead_Then_Not_Performs_Search ( )
169
+ {
170
+ var fileRepresentations = specimenBuilders . CreateMany < FileRepresentation > ( ) . ToList ( ) ;
171
+ moqFileProvider . Setup ( m => m . GetFileRepresentationsFromDirectory ( It . IsAny < string > ( ) ) )
172
+ . Returns ( fileRepresentations ) ;
173
+
174
+ moqConsoleProvider . SetupSequence ( m => m . ReadLine ( ) )
175
+ . Returns ( "$end" ) ;
176
+
177
+ fileSearchEngine . StartEngine ( "TestDirectory" ) ;
178
+
179
+ moqFileProvider . Verify ( m => m . GetFileRepresentationsFromDirectory ( It . IsAny < string > ( ) ) , Times . Once ) ;
180
+ // We check that the console writes only one line as no searchs are performed in this case
181
+ moqConsoleProvider . Verify ( m => m . WriteLine ( It . IsAny < string > ( ) ) , Times . Once ) ;
182
+ // We check that these methods has been called twice as we iterate 2 times through the engine loop
183
+ moqConsoleProvider . Verify ( m => m . ReadLine ( ) , Times . Once ) ;
184
+ moqConsoleProvider . Verify ( m => m . Write ( It . IsAny < string > ( ) ) , Times . Once ) ;
185
+ }
186
+ }
187
+ }
0 commit comments