Skip to content

Commit 2ca45cc

Browse files
grep: add generator
1 parent bc39ff0 commit 2ca45cc

File tree

2 files changed

+134
-47
lines changed

2 files changed

+134
-47
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.IO;
3+
using Xunit;
4+
5+
public class {{ testClass }} : IDisposable
6+
{
7+
{{- for test in tests }}
8+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
9+
public void {{ test.shortTestMethod }}()
10+
{
11+
var pattern = {{ test.input.pattern | string.literal }};
12+
var flags = {{ test.input.flags | array.join " " | string.literal }};
13+
string[] files = {{ test.input.files }};
14+
{{- if test.expected.empty? }}
15+
var expected = "";
16+
{{- else }}
17+
var expected =
18+
{{- for line in test.expected }}
19+
{{ if for.last -}}
20+
{{ line | string.literal -}};
21+
{{- else -}}
22+
{{ line | string.append "\n" | string.literal }} +
23+
{{- end -}}
24+
{{- end -}}
25+
{{ end }}
26+
Assert.Equal(expected, {{ testedClass }}.Match(pattern, flags, files));
27+
}
28+
{{ end }}
29+
private const string IliadFileName = "iliad.txt";
30+
private const string IliadContents =
31+
"Achilles sing, O Goddess! Peleus' son;\n" +
32+
"His wrath pernicious, who ten thousand woes\n" +
33+
"Caused to Achaia's host, sent many a soul\n" +
34+
"Illustrious into Ades premature,\n" +
35+
"And Heroes gave (so stood the will of Jove)\n" +
36+
"To dogs and to all ravening fowls a prey,\n" +
37+
"When fierce dispute had separated once\n" +
38+
"The noble Chief Achilles from the son\n" +
39+
"Of Atreus, Agamemnon, King of men.\n";
40+
41+
private const string MidsummerNightFileName = "midsummer-night.txt";
42+
private const string MidsummerNightContents =
43+
"I do entreat your grace to pardon me.\n" +
44+
"I know not by what power I am made bold,\n" +
45+
"Nor how it may concern my modesty,\n" +
46+
"In such a presence here to plead my thoughts;\n" +
47+
"But I beseech your grace that I may know\n" +
48+
"The worst that may befall me in this case,\n" +
49+
"If I refuse to wed Demetrius.\n";
50+
51+
private const string ParadiseLostFileName = "paradise-lost.txt";
52+
private const string ParadiseLostContents =
53+
"Of Mans First Disobedience, and the Fruit\n" +
54+
"Of that Forbidden Tree, whose mortal tast\n" +
55+
"Brought Death into the World, and all our woe,\n" +
56+
"With loss of Eden, till one greater Man\n" +
57+
"Restore us, and regain the blissful Seat,\n" +
58+
"Sing Heav'nly Muse, that on the secret top\n" +
59+
"Of Oreb, or of Sinai, didst inspire\n" +
60+
"That Shepherd, who first taught the chosen Seed\n";
61+
62+
public {{ testClass }}()
63+
{
64+
Directory.SetCurrentDirectory(Path.GetTempPath());
65+
File.WriteAllText(IliadFileName, IliadContents);
66+
File.WriteAllText(MidsummerNightFileName, MidsummerNightContents);
67+
File.WriteAllText(ParadiseLostFileName, ParadiseLostContents);
68+
}
69+
70+
public void Dispose()
71+
{
72+
Directory.SetCurrentDirectory(Path.GetTempPath());
73+
File.Delete(IliadFileName);
74+
File.Delete(MidsummerNightFileName);
75+
File.Delete(ParadiseLostFileName);
76+
}
77+
}

exercises/practice/grep/GrepTests.cs

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ public void One_file_one_match_no_flags()
99
{
1010
var pattern = "Agamemnon";
1111
var flags = "";
12-
var files = new[] { "iliad.txt" };
13-
var expected = "Of Atreus, Agamemnon, King of men.";
12+
string[] files = ["iliad.txt"];
13+
var expected =
14+
"Of Atreus, Agamemnon, King of men.";
1415
Assert.Equal(expected, Grep.Match(pattern, flags, files));
1516
}
1617

@@ -19,8 +20,9 @@ public void One_file_one_match_print_line_numbers_flag()
1920
{
2021
var pattern = "Forbidden";
2122
var flags = "-n";
22-
var files = new[] { "paradise-lost.txt" };
23-
var expected = "2:Of that Forbidden Tree, whose mortal tast";
23+
string[] files = ["paradise-lost.txt"];
24+
var expected =
25+
"2:Of that Forbidden Tree, whose mortal tast";
2426
Assert.Equal(expected, Grep.Match(pattern, flags, files));
2527
}
2628

@@ -29,8 +31,9 @@ public void One_file_one_match_case_insensitive_flag()
2931
{
3032
var pattern = "FORBIDDEN";
3133
var flags = "-i";
32-
var files = new[] { "paradise-lost.txt" };
33-
var expected = "Of that Forbidden Tree, whose mortal tast";
34+
string[] files = ["paradise-lost.txt"];
35+
var expected =
36+
"Of that Forbidden Tree, whose mortal tast";
3437
Assert.Equal(expected, Grep.Match(pattern, flags, files));
3538
}
3639

@@ -39,8 +42,9 @@ public void One_file_one_match_print_file_names_flag()
3942
{
4043
var pattern = "Forbidden";
4144
var flags = "-l";
42-
var files = new[] { "paradise-lost.txt" };
43-
var expected = "paradise-lost.txt";
45+
string[] files = ["paradise-lost.txt"];
46+
var expected =
47+
"paradise-lost.txt";
4448
Assert.Equal(expected, Grep.Match(pattern, flags, files));
4549
}
4650

@@ -49,8 +53,9 @@ public void One_file_one_match_match_entire_lines_flag()
4953
{
5054
var pattern = "With loss of Eden, till one greater Man";
5155
var flags = "-x";
52-
var files = new[] { "paradise-lost.txt" };
53-
var expected = "With loss of Eden, till one greater Man";
56+
string[] files = ["paradise-lost.txt"];
57+
var expected =
58+
"With loss of Eden, till one greater Man";
5459
Assert.Equal(expected, Grep.Match(pattern, flags, files));
5560
}
5661

@@ -59,8 +64,9 @@ public void One_file_one_match_multiple_flags()
5964
{
6065
var pattern = "OF ATREUS, Agamemnon, KIng of MEN.";
6166
var flags = "-n -i -x";
62-
var files = new[] { "iliad.txt" };
63-
var expected = "9:Of Atreus, Agamemnon, King of men.";
67+
string[] files = ["iliad.txt"];
68+
var expected =
69+
"9:Of Atreus, Agamemnon, King of men.";
6470
Assert.Equal(expected, Grep.Match(pattern, flags, files));
6571
}
6672

@@ -69,8 +75,8 @@ public void One_file_several_matches_no_flags()
6975
{
7076
var pattern = "may";
7177
var flags = "";
72-
var files = new[] { "midsummer-night.txt" };
73-
var expected =
78+
string[] files = ["midsummer-night.txt"];
79+
var expected =
7480
"Nor how it may concern my modesty,\n" +
7581
"But I beseech your grace that I may know\n" +
7682
"The worst that may befall me in this case,";
@@ -82,8 +88,8 @@ public void One_file_several_matches_print_line_numbers_flag()
8288
{
8389
var pattern = "may";
8490
var flags = "-n";
85-
var files = new[] { "midsummer-night.txt" };
86-
var expected =
91+
string[] files = ["midsummer-night.txt"];
92+
var expected =
8793
"3:Nor how it may concern my modesty,\n" +
8894
"5:But I beseech your grace that I may know\n" +
8995
"6:The worst that may befall me in this case,";
@@ -95,7 +101,7 @@ public void One_file_several_matches_match_entire_lines_flag()
95101
{
96102
var pattern = "may";
97103
var flags = "-x";
98-
var files = new[] { "midsummer-night.txt" };
104+
string[] files = ["midsummer-night.txt"];
99105
var expected = "";
100106
Assert.Equal(expected, Grep.Match(pattern, flags, files));
101107
}
@@ -105,8 +111,8 @@ public void One_file_several_matches_case_insensitive_flag()
105111
{
106112
var pattern = "ACHILLES";
107113
var flags = "-i";
108-
var files = new[] { "iliad.txt" };
109-
var expected =
114+
string[] files = ["iliad.txt"];
115+
var expected =
110116
"Achilles sing, O Goddess! Peleus' son;\n" +
111117
"The noble Chief Achilles from the son";
112118
Assert.Equal(expected, Grep.Match(pattern, flags, files));
@@ -117,8 +123,8 @@ public void One_file_several_matches_inverted_flag()
117123
{
118124
var pattern = "Of";
119125
var flags = "-v";
120-
var files = new[] { "paradise-lost.txt" };
121-
var expected =
126+
string[] files = ["paradise-lost.txt"];
127+
var expected =
122128
"Brought Death into the World, and all our woe,\n" +
123129
"With loss of Eden, till one greater Man\n" +
124130
"Restore us, and regain the blissful Seat,\n" +
@@ -132,7 +138,7 @@ public void One_file_no_matches_various_flags()
132138
{
133139
var pattern = "Gandalf";
134140
var flags = "-n -l -x -i";
135-
var files = new[] { "iliad.txt" };
141+
string[] files = ["iliad.txt"];
136142
var expected = "";
137143
Assert.Equal(expected, Grep.Match(pattern, flags, files));
138144
}
@@ -142,8 +148,9 @@ public void One_file_one_match_file_flag_takes_precedence_over_line_flag()
142148
{
143149
var pattern = "ten";
144150
var flags = "-n -l";
145-
var files = new[] { "iliad.txt" };
146-
var expected = "iliad.txt";
151+
string[] files = ["iliad.txt"];
152+
var expected =
153+
"iliad.txt";
147154
Assert.Equal(expected, Grep.Match(pattern, flags, files));
148155
}
149156

@@ -152,8 +159,8 @@ public void One_file_several_matches_inverted_and_match_entire_lines_flags()
152159
{
153160
var pattern = "Illustrious into Ades premature,";
154161
var flags = "-x -v";
155-
var files = new[] { "iliad.txt" };
156-
var expected =
162+
string[] files = ["iliad.txt"];
163+
var expected =
157164
"Achilles sing, O Goddess! Peleus' son;\n" +
158165
"His wrath pernicious, who ten thousand woes\n" +
159166
"Caused to Achaia's host, sent many a soul\n" +
@@ -170,8 +177,9 @@ public void Multiple_files_one_match_no_flags()
170177
{
171178
var pattern = "Agamemnon";
172179
var flags = "";
173-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
174-
var expected = "iliad.txt:Of Atreus, Agamemnon, King of men.";
180+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
181+
var expected =
182+
"iliad.txt:Of Atreus, Agamemnon, King of men.";
175183
Assert.Equal(expected, Grep.Match(pattern, flags, files));
176184
}
177185

@@ -180,8 +188,8 @@ public void Multiple_files_several_matches_no_flags()
180188
{
181189
var pattern = "may";
182190
var flags = "";
183-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
184-
var expected =
191+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
192+
var expected =
185193
"midsummer-night.txt:Nor how it may concern my modesty,\n" +
186194
"midsummer-night.txt:But I beseech your grace that I may know\n" +
187195
"midsummer-night.txt:The worst that may befall me in this case,";
@@ -193,8 +201,8 @@ public void Multiple_files_several_matches_print_line_numbers_flag()
193201
{
194202
var pattern = "that";
195203
var flags = "-n";
196-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
197-
var expected =
204+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
205+
var expected =
198206
"midsummer-night.txt:5:But I beseech your grace that I may know\n" +
199207
"midsummer-night.txt:6:The worst that may befall me in this case,\n" +
200208
"paradise-lost.txt:2:Of that Forbidden Tree, whose mortal tast\n" +
@@ -207,8 +215,8 @@ public void Multiple_files_one_match_print_file_names_flag()
207215
{
208216
var pattern = "who";
209217
var flags = "-l";
210-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
211-
var expected =
218+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
219+
var expected =
212220
"iliad.txt\n" +
213221
"paradise-lost.txt";
214222
Assert.Equal(expected, Grep.Match(pattern, flags, files));
@@ -219,8 +227,8 @@ public void Multiple_files_several_matches_case_insensitive_flag()
219227
{
220228
var pattern = "TO";
221229
var flags = "-i";
222-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
223-
var expected =
230+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
231+
var expected =
224232
"iliad.txt:Caused to Achaia's host, sent many a soul\n" +
225233
"iliad.txt:Illustrious into Ades premature,\n" +
226234
"iliad.txt:And Heroes gave (so stood the will of Jove)\n" +
@@ -239,8 +247,8 @@ public void Multiple_files_several_matches_inverted_flag()
239247
{
240248
var pattern = "a";
241249
var flags = "-v";
242-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
243-
var expected =
250+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
251+
var expected =
244252
"iliad.txt:Achilles sing, O Goddess! Peleus' son;\n" +
245253
"iliad.txt:The noble Chief Achilles from the son\n" +
246254
"midsummer-night.txt:If I refuse to wed Demetrius.";
@@ -252,8 +260,9 @@ public void Multiple_files_one_match_match_entire_lines_flag()
252260
{
253261
var pattern = "But I beseech your grace that I may know";
254262
var flags = "-x";
255-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
256-
var expected = "midsummer-night.txt:But I beseech your grace that I may know";
263+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
264+
var expected =
265+
"midsummer-night.txt:But I beseech your grace that I may know";
257266
Assert.Equal(expected, Grep.Match(pattern, flags, files));
258267
}
259268

@@ -262,8 +271,9 @@ public void Multiple_files_one_match_multiple_flags()
262271
{
263272
var pattern = "WITH LOSS OF EDEN, TILL ONE GREATER MAN";
264273
var flags = "-n -i -x";
265-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
266-
var expected = "paradise-lost.txt:4:With loss of Eden, till one greater Man";
274+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
275+
var expected =
276+
"paradise-lost.txt:4:With loss of Eden, till one greater Man";
267277
Assert.Equal(expected, Grep.Match(pattern, flags, files));
268278
}
269279

@@ -272,7 +282,7 @@ public void Multiple_files_no_matches_various_flags()
272282
{
273283
var pattern = "Frodo";
274284
var flags = "-n -l -x -i";
275-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
285+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
276286
var expected = "";
277287
Assert.Equal(expected, Grep.Match(pattern, flags, files));
278288
}
@@ -282,8 +292,8 @@ public void Multiple_files_several_matches_file_flag_takes_precedence_over_line_
282292
{
283293
var pattern = "who";
284294
var flags = "-n -l";
285-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
286-
var expected =
295+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
296+
var expected =
287297
"iliad.txt\n" +
288298
"paradise-lost.txt";
289299
Assert.Equal(expected, Grep.Match(pattern, flags, files));
@@ -294,8 +304,8 @@ public void Multiple_files_several_matches_inverted_and_match_entire_lines_flags
294304
{
295305
var pattern = "Illustrious into Ades premature,";
296306
var flags = "-x -v";
297-
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
298-
var expected =
307+
string[] files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"];
308+
var expected =
299309
"iliad.txt:Achilles sing, O Goddess! Peleus' son;\n" +
300310
"iliad.txt:His wrath pernicious, who ten thousand woes\n" +
301311
"iliad.txt:Caused to Achaia's host, sent many a soul\n" +

0 commit comments

Comments
 (0)