-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThsearch_function_tests.cs
285 lines (213 loc) · 10.6 KB
/
Thsearch_function_tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
namespace Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using Tests.Helpers;
using thsearch;
[TestClass]
public class Thsearch_function_tests
{
private string[] _expectedAbsIncludedDirectories1, _expectedAbsExcludedDirectories1, _expectedAbsIncludedFiles1,_expectedAbsExcludedFiles, _expectedAbsExcludedFiles1;
private string[] _expectedAbsIncludedDirectories2, _expectedAbsExcludedDirectories2, _expectedAbsIncludedFiles2,_expectedAbsExcludedFiles2;
public TestContext TestContext { get; set; }
[TestInitialize]
public void Initialize()
{
#region 1 Test files 1
// note: matching string in include lowercase for both 1 & 2 is tëst
string[] relativeIncludedDirectories1 = new string[] {
@"TestData\Include1\IncludeSub11",
@"TestData\Include1\IncludeSub12",
};
string[] relativeExcludedDirectories1 = new string[] {
@"TestData\Exclude1\ExcludeSub11",
@"TestData\Exclude1\ExcludeSub12",
};
_expectedAbsIncludedDirectories1 = relativeIncludedDirectories1.Select(x => Path.GetFullPath(x)).ToArray();
_expectedAbsExcludedDirectories1 = relativeExcludedDirectories1.Select(x => Path.GetFullPath(x)).ToArray();
string[] relativeIncludedFiles1 = new string[]
{
@"TestData\Include1\IncludeSub11\IncludeText11Lowercase.txt",
@"TestData\Include1\IncludeSub11\IncludeText11Uppercase.txt",
@"TestData\Include1\IncludeSub12\IncludeText12Lowercase.txt",
@"TestData\Include1\IncludeSub12\IncludeText12Uppercase.txt",
};
string[] relativeExcludedFiles1 = new string[]
{
@"TestData\Exclude1\ExcludeSub11\ExcludeText11Lowercase.txt",
@"TestData\Exclude1\ExcludeSub11\ExcludeText11Uppercase.txt",
@"TestData\Exclude1\ExcludeSub12\ExcludeText12Lowercase.txt",
@"TestData\Exclude1\ExcludeSub12\ExcludeText12Uppercase.txt",
};
_expectedAbsIncludedFiles1 = relativeIncludedFiles1.Select(x => Path.GetFullPath(x)).ToArray();
_expectedAbsExcludedFiles1 = relativeExcludedFiles1.Select(x => Path.GetFullPath(x)).ToArray();
// takes _expectedAbsIncludedDirectories and _expectedAbsExcludedDirectories, adds "+" to the beginning of each, concats both arrays and assigns it to string[] configDirectoryLines
string[] configDirectoryLines1 = _expectedAbsIncludedDirectories1.Select(x => "+" + x).Concat(_expectedAbsExcludedDirectories1.Select(x => "-" + x)).ToArray();
string[] configFileExtLines1 = new string[]
{
">.txt"
};
// combine the absolute paths with the file matcher lines to make the config file
string[] configLines1 = configDirectoryLines1.Concat(configFileExtLines1).ToArray();
// alt config can just have a different file matcher
FileModel.WriteAllLines(@"thsearch.txt", configLines1);
#endregion
#region 2 Test files 2
string[] relativeIncludedDirectories2 = new string[] {
@"TestData\Include2\IncludeSub21",
@"TestData\Include2\IncludeSub22",
};
string[] relativeExcludedDirectories2 = new string[] {
@"TestData\Exclude2\ExcludeSub21",
@"TestData\Exclude2\ExcludeSub22",
};
_expectedAbsIncludedDirectories2 = relativeIncludedDirectories2.Select(x => Path.GetFullPath(x)).ToArray();
_expectedAbsExcludedDirectories2 = relativeExcludedDirectories2.Select(x => Path.GetFullPath(x)).ToArray();
string[] relativeIncludedFiles2 = new string[]
{
@"TestData\Include2\IncludeSub21\IncludeText21Lowercase.txt",
@"TestData\Include2\IncludeSub21\IncludeText21Uppercase.txt",
@"TestData\Include2\IncludeSub22\IncludeText22Lowercase.txt",
@"TestData\Include2\IncludeSub22\IncludeText22Uppercase.txt",
};
string[] relativeExcludedFiles2 = new string[]
{
@"TestData\Exclude2\ExcludeSub21\ExcludeText21Lowercase.txt",
@"TestData\Exclude2\ExcludeSub21\ExcludeText21Uppercase.txt",
@"TestData\Exclude2\ExcludeSub22\ExcludeText22Lowercase.txt",
@"TestData\Exclude2\ExcludeSub22\ExcludeText22Uppercase.txt",
};
_expectedAbsIncludedFiles2 = relativeIncludedFiles2.Select(x => Path.GetFullPath(x)).ToArray();
_expectedAbsExcludedFiles = relativeExcludedFiles2.Select(x => Path.GetFullPath(x)).ToArray();
// takes _expectedAbsIncludedDirectories and _expectedAbsExcludedDirectories, adds "+" to the beginning of each, concats both arrays and assigns it to string[] configDirectoryLines
string[] configDirectoryLines2 = _expectedAbsIncludedDirectories2.Select(x => "+" + x).Concat(_expectedAbsExcludedDirectories2.Select(x => "-" + x)).ToArray();
string[] configFileExtLines2 = new string[]
{
">.txt"
};
// combine the absolute paths with the file matcher lines to make the config file
string[] configLines2 = configDirectoryLines2.Concat(configFileExtLines2).ToArray();
// alt config can just have a different file matcher
FileModel.WriteAllLines(@"thsearch2.txt", configLines2);
#endregion
}
[TestMethod]
public void ConfigFileParser_ShouldHaveCorrectIncludedDirectories1()
{
// Arrange
List<string> testGeneratedIncludedDirs = _expectedAbsIncludedDirectories1.ToList();
ConfigFileParser config = new ConfigFileParser("thsearch.txt");
// Act
List<string> configIncludedDirs = config.IncludedDirectories;
// Assert
CollectionAssert.AreEqual(testGeneratedIncludedDirs, configIncludedDirs);
}
public void ConfigFileParser_ShouldHaveCorrectIncludedDirectories2()
{
// Arrange
List<string> testGeneratedIncludedDirs = _expectedAbsIncludedDirectories2.ToList();
ConfigFileParser config = new ConfigFileParser("thsearch2.txt");
// Act
List<string> configIncludedDirs = config.IncludedDirectories;
// Assert
CollectionAssert.AreEqual(testGeneratedIncludedDirs, configIncludedDirs);
}
[TestMethod]
public void ConfigFileParser_ShouldHaveCorrectExcludedDirectories1()
{
// Arrange
List<string> testGeneratedExcludedDirs = _expectedAbsExcludedDirectories1.ToList();;
ConfigFileParser config = new ConfigFileParser("thsearch.txt");
// Act
List<string> configExcludedDirs = config.ExcludedDirectories;
// Assert
CollectionAssert.AreEqual(testGeneratedExcludedDirs, configExcludedDirs);
}
public void ConfigFileParser_ShouldHaveCorrectExcludedDirectories2()
{
// Arrange
List<string> testGeneratedExcludedDirs = _expectedAbsExcludedDirectories2.ToList();;
ConfigFileParser config = new ConfigFileParser("thsearch2.txt");
// Act
List<string> configExcludedDirs = config.ExcludedDirectories;
// Assert
CollectionAssert.AreEqual(testGeneratedExcludedDirs, configExcludedDirs);
}
[TestMethod]
public void Main_WithIncludedFile_ShouldPrintIncludedFiles1()
{
// Arrange
string searchString = "tëst";
string configName = "thsearch";
List<string> expectedOutput = _expectedAbsIncludedFiles1.Select(Path.GetFullPath).ToList();
// Expected that the main method will add a new line character after the last path. Using split below this gets converted to an empty string at the end of the list. Let's emulate that:
expectedOutput.Add("");
// Act
using (ConsoleOutput consoleOutput = new ConsoleOutput())
{
Program.Main(new string[] { searchString, configName });
List<string> output = consoleOutput.GetOutput().Split(Environment.NewLine).ToList();
// Assert - Currently not working because threads, unlike expectedOutput can deliver paths in any order. This assertion needs to be order independent
CollectionAssert.AreEquivalent(expectedOutput, output);
}
}
[TestMethod]
public void Main_WithIncludedFile_ShouldPrintIncludedFiles2()
{
// Arrange
string searchString = "tëst";
string configName = "thsearch2";
List<string> expectedOutput = _expectedAbsIncludedFiles2.Select(Path.GetFullPath).ToList();
// Expected that the main method will add a new line character after the last path. Using split below this gets converted to an empty string at the end of the list. Let's emulate that:
expectedOutput.Add("");
// Act
using (ConsoleOutput consoleOutput = new ConsoleOutput())
{
Program.Main(new string[] { searchString, configName });
List<string> output = consoleOutput.GetOutput().Split(Environment.NewLine).ToList();
// Assert - Currently not working because threads, unlike expectedOutput can deliver paths in any order. This assertion needs to be order independent
CollectionAssert.AreEquivalent(expectedOutput, output);
}
}
[TestMethod]
public void Main_PerformanceTest()
{
// Arrange
var searchString = "tëst";
var configName = "thsearch";
// Start the stopwatch
var stopwatch = new Stopwatch();
stopwatch.Start();
// Act
Program.Main(new string[] { searchString, configName });
// Stop the stopwatch
stopwatch.Stop();
// Write the elapsed time to the test results
TestContext.WriteLine($"Elapsed time: {stopwatch.ElapsedMilliseconds}ms");
}
[TestMethod]
public void TestFileContainsString()
{
// Arrange
string file = _expectedAbsIncludedFiles1[0];
string searchString = "tëst";
// Act
bool result = Program.FileContainsString(file, searchString);
// Assert
Assert.IsTrue(result);
}
// no need for an alt version
[TestMethod]
public void TestGetMatchingFiles1()
{
// Arrange
string[] directories = _expectedAbsIncludedDirectories1;
List<string> fileExtensions = new List<string> {".txt"};
List<string> excludedDirs = _expectedAbsExcludedDirectories1.ToList();
List<string> expectedOutput = _expectedAbsIncludedFiles1.ToList();
// Act
// runs Program.GetMatchingFiles for each item of directories
List<string> output = directories.SelectMany(d => Program.GetMatchingFiles(d, fileExtensions, excludedDirs)).ToList();
// Assert
CollectionAssert.AreEquivalent(expectedOutput, output);
}
}