1
exercism fetch csharp grep

GrepTest.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.IO;
using Xunit;

public class GrepTest : IDisposable
{
    private const string IliadFileName = "iliad.txt";
    private const string IliadContents =
        @"Achilles sing, O Goddess! Peleus' son;
His wrath pernicious, who ten thousand woes
Caused to Achaia's host, sent many a soul
Illustrious into Ades premature,
And Heroes gave (so stood the will of Jove)
To dogs and to all ravening fowls a prey,
When fierce dispute had separated once
The noble Chief Achilles from the son
Of Atreus, Agamemnon, King of men.
";

    private const string MidsummerNightFileName = "midsummer-night.txt";
    private const string MidsummerNightContents =
        @"I do entreat your grace to pardon me.
I know not by what power I am made bold,
Nor how it may concern my modesty,
In such a presence here to plead my thoughts;
But I beseech your grace that I may know
The worst that may befall me in this case,
If I refuse to wed Demetrius.
";

    private const string ParadiseLostFileName = "paradise-lost.txt";
    private const string ParadiseLostContents =
        @"Of Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
That Shepherd, who first taught the chosen Seed
";
    
    public GrepTest()
    {
        Directory.SetCurrentDirectory(Path.GetTempPath());
        File.WriteAllText(IliadFileName, IliadContents);
        File.WriteAllText(MidsummerNightFileName, MidsummerNightContents);
        File.WriteAllText(ParadiseLostFileName, ParadiseLostContents);
    }
    
    public void Dispose()
    {
        Directory.SetCurrentDirectory(Path.GetTempPath());
        File.Delete(IliadFileName);
        File.Delete(MidsummerNightFileName);
        File.Delete(ParadiseLostFileName);
    }

    [Fact]
    public void One_file_one_match_no_flags()
    {
        const string pattern = "Agamemnon";
        const string flags = "";
        var files = new[] { IliadFileName };

        const string expected =
            "Of Atreus, Agamemnon, King of men.\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_file_one_match_print_line_numbers_flag()
    {
        const string pattern = "Forbidden";
        const string flags = "-n";
        var files = new[] { ParadiseLostFileName };

        const string expected =
            "2:Of that Forbidden Tree, whose mortal tast\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_file_one_match_case_insensitive_flag()
    {
        const string pattern = "Forbidden";
        const string flags = "-i";
        var files = new[] { ParadiseLostFileName };

        const string expected =
            "Of that Forbidden Tree, whose mortal tast\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_file_one_match_print_file_names_flag()
    {
        const string pattern = "Forbidden";
        const string flags = "-l";
        var files = new[] { ParadiseLostFileName };

        var expected =
            $"{ParadiseLostFileName}\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_file_one_match_match_entire_lines_flag()
    {
        const string pattern = "With loss of Eden, till one greater Man";
        const string flags = "-x";
        var files = new[] { ParadiseLostFileName };

        const string expected =
            "With loss of Eden, till one greater Man\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_file_one_match_multiple_flags()
    {
        const string pattern = "OF ATREUS, Agamemnon, KIng of MEN.";
        var files = new[] { IliadFileName };
        const string flags = "-n -i -x";
        const string expected =
            "9:Of Atreus, Agamemnon, King of men.\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_file_several_matches_no_flags()
    {
        const string pattern = "may";
        const string flags = "";
        var files = new[] { MidsummerNightFileName };

        const string expected =
            "Nor how it may concern my modesty,\n" +
            "But I beseech your grace that I may know\n" +
            "The worst that may befall me in this case,\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_file_several_matches_print_line_numbers_flag()
    {
        const string pattern = "may";
        const string flags = "-n";
        var files = new[] { MidsummerNightFileName };

        const string expected =
            "3:Nor how it may concern my modesty,\n" +
            "5:But I beseech your grace that I may know\n" +
            "6:The worst that may befall me in this case,\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_file_several_matches_match_entire_lines_flag()
    {
        const string pattern = "may";
        const string flags = "-x";
        var files = new[] { MidsummerNightFileName };

        const string expected = "";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_file_several_matches_case_insensitive_flag()
    {
        const string pattern = "ACHILLES";
        const string flags = "-i";
        var files = new[] { IliadFileName };

        const string expected =
            "Achilles sing, O Goddess! Peleus' son;\n" +
            "The noble Chief Achilles from the son\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_file_several_matches_inverted_flag()
    {
        const string pattern = "Of";
        const string flags = "-v";
        var files = new[] { ParadiseLostFileName };

        const string expected =
            "Brought Death into the World, and all our woe,\n" +
            "With loss of Eden, till one greater Man\n" +
            "Restore us, and regain the blissful Seat,\n" +
            "Sing Heav'nly Muse, that on the secret top\n" +
            "That Shepherd, who first taught the chosen Seed\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Theory(Skip = "Remove to run test")]
    [InlineData("")]
    [InlineData("-n")]
    [InlineData("-l")]
    [InlineData("-x")]
    [InlineData("-i")]
    [InlineData("-n -l -x -i")]
    public void One_file_no_matches_various_flags(string flags)
    {
        const string pattern = "Gandalf";
        var files = new[] { IliadFileName };
        const string expected = "";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_files_one_match_no_flags()
    {
        const string pattern = "Agamemnon";
        const string flags = "";
        var files = new[] { IliadFileName, MidsummerNightFileName, ParadiseLostFileName };

        var expected =
            $"{IliadFileName}:Of Atreus, Agamemnon, King of men.\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_files_several_matches_no_flags()
    {
        const string pattern = "may";
        const string flags = "";
        var files = new[] { IliadFileName, MidsummerNightFileName, ParadiseLostFileName };

        var expected =
            $"{MidsummerNightFileName}:Nor how it may concern my modesty,\n" +
            $"{MidsummerNightFileName}:But I beseech your grace that I may know\n" +
            $"{MidsummerNightFileName}:The worst that may befall me in this case,\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_files_several_matches_print_line_numbers_flag()
    {
        const string pattern = "that";
        const string flags = "-n";
        var files = new[] { IliadFileName, MidsummerNightFileName, ParadiseLostFileName };

        var expected =
            $"{MidsummerNightFileName}:5:But I beseech your grace that I may know\n" +
            $"{MidsummerNightFileName}:6:The worst that may befall me in this case,\n" +
            $"{ParadiseLostFileName}:2:Of that Forbidden Tree, whose mortal tast\n" +
            $"{ParadiseLostFileName}:6:Sing Heav'nly Muse, that on the secret top\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_files_several_matches_print_file_names_flag()
    {
        const string pattern = "who";
        const string flags = "-l";
        var files = new[] { IliadFileName, MidsummerNightFileName, ParadiseLostFileName };

        var expected =
            $"{IliadFileName}\n" +
            $"{ParadiseLostFileName}\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_files_several_matches_case_insensitive_flag()
    {
        const string pattern = "TO";
        const string flags = "-i";
        var files = new[] { IliadFileName, MidsummerNightFileName, ParadiseLostFileName };

        var expected =
            $"{IliadFileName}:Caused to Achaia's host, sent many a soul\n" +
            $"{IliadFileName}:Illustrious into Ades premature,\n" +
            $"{IliadFileName}:And Heroes gave (so stood the will of Jove)\n" +
            $"{IliadFileName}:To dogs and to all ravening fowls a prey,\n" +
            $"{MidsummerNightFileName}:I do entreat your grace to pardon me.\n" +
            $"{MidsummerNightFileName}:In such a presence here to plead my thoughts;\n" +
            $"{MidsummerNightFileName}:If I refuse to wed Demetrius.\n" +
            $"{ParadiseLostFileName}:Brought Death into the World, and all our woe,\n" +
            $"{ParadiseLostFileName}:Restore us, and regain the blissful Seat,\n" +
            $"{ParadiseLostFileName}:Sing Heav'nly Muse, that on the secret top\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_files_several_matches_inverted_flag()
    {
        const string pattern = "a";
        const string flags = "-v";
        var files = new[] { IliadFileName, MidsummerNightFileName, ParadiseLostFileName };

        var expected =
            $"{IliadFileName}:Achilles sing, O Goddess! Peleus' son;\n" +
            $"{IliadFileName}:The noble Chief Achilles from the son\n" +
            $"{MidsummerNightFileName}:If I refuse to wed Demetrius.\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_files_one_match_match_entire_lines_flag()
    {
        const string pattern = "But I beseech your grace that I may know";
        const string flags = "-x";
        var files = new[] { IliadFileName, MidsummerNightFileName, ParadiseLostFileName };

        var expected =
            $"{MidsummerNightFileName}:But I beseech your grace that I may know\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Fact(Skip = "Remove to run test")]
    public void Multiple_files_one_match_multiple_flags()
    {
        const string pattern = "WITH LOSS OF EDEN, TILL ONE GREATER MAN";
        var files = new[] { IliadFileName, MidsummerNightFileName, ParadiseLostFileName };
        const string flags = "-n -i -x";
        var expected =
            $"{ParadiseLostFileName}:4:With loss of Eden, till one greater Man\n";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }

    [Theory(Skip = "Remove to run test")]
    [InlineData("")]
    [InlineData("-n")]
    [InlineData("-l")]
    [InlineData("-x")]
    [InlineData("-i")]
    [InlineData("-n -l -x -i")]
    public void Multiple_files_no_matches_various_flags(string flags)
    {
        const string pattern = "Frodo";
        var files = new[] { IliadFileName, MidsummerNightFileName, ParadiseLostFileName };

        const string expected = "";

        Assert.Equal(expected, Grep.Find(pattern, flags, files));
    }
}