-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrep_test.rb
402 lines (344 loc) · 12.5 KB
/
grep_test.rb
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
require 'minitest/autorun'
require_relative 'grep'
class GrepTest < Minitest::Test
def skip; end
def setup
IO.write 'iliad.txt', <<~ILIAD
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.
ILIAD
IO.write 'midsummer-night.txt', <<~MSNIGHT
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.
MSNIGHT
IO.write 'paradise-lost.txt', <<~PARADISE
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
PARADISE
end
def teardown
File.delete('iliad.txt')
File.delete('midsummer-night.txt')
File.delete('paradise-lost.txt')
end
def test_one_file_one_match_no_flags
# skip
pattern = "Agamemnon"
flags = []
files = ["iliad.txt"]
expected = <<~EXPECTED.rstrip
Of Atreus, Agamemnon, King of men.
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_one_match_print_line_numbers_flag
# skip
pattern = "Forbidden"
flags = ["-n"]
files = ["paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
2:Of that Forbidden Tree, whose mortal tast
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_one_match_case_insensitive_flag
pattern = "FORBIDDEN"
flags = ["-i"]
files = ["paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
Of that Forbidden Tree, whose mortal tast
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_one_match_print_file_names_flag
# skip
pattern = "Forbidden"
flags = ["-l"]
files = ["paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
paradise-lost.txt
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_one_match_match_entire_lines_flag
skip
pattern = "With loss of Eden, till one greater Man"
flags = ["-x"]
files = ["paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
With loss of Eden, till one greater Man
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_one_match_multiple_flags
skip
pattern = "OF ATREUS, Agamemnon, KIng of MEN."
flags = ["-n", "-i", "-x"]
files = ["iliad.txt"]
expected = <<~EXPECTED.rstrip
9:Of Atreus, Agamemnon, King of men.
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_several_matches_no_flags
# skip
pattern = "may"
flags = []
files = ["midsummer-night.txt"]
expected = <<~EXPECTED.rstrip
Nor how it may concern my modesty,
But I beseech your grace that I may know
The worst that may befall me in this case,
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_several_matches_print_line_numbers_flag
# skip
pattern = "may"
flags = ["-n"]
files = ["midsummer-night.txt"]
expected = <<~EXPECTED.rstrip
3:Nor how it may concern my modesty,
5:But I beseech your grace that I may know
6:The worst that may befall me in this case,
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_several_matches_match_entire_lines_flag
skip
pattern = "may"
flags = ["-x"]
files = ["midsummer-night.txt"]
expected = <<~EXPECTED.rstrip
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_several_matches_case_insensitive_flag
skip
pattern = "ACHILLES"
flags = ["-i"]
files = ["iliad.txt"]
expected = <<~EXPECTED.rstrip
Achilles sing, O Goddess! Peleus' son;
The noble Chief Achilles from the son
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_several_matches_inverted_flag
# skip
pattern = "Of"
flags = ["-v"]
files = ["paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
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
That Shepherd, who first taught the chosen Seed
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_no_matches_various_flags
skip
pattern = "Gandalf"
flags = ["-n", "-l", "-x", "-i"]
files = ["iliad.txt"]
expected = <<~EXPECTED.rstrip
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_one_match_file_flag_takes_precedence_over_line_flag
# skip
pattern = "ten"
flags = ["-n", "-l"]
files = ["iliad.txt"]
expected = <<~EXPECTED.rstrip
iliad.txt
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_one_file_several_matches_inverted_and_match_entire_lines_flags
skip
pattern = "Illustrious into Ades premature,"
flags = ["-x", "-v"]
files = ["iliad.txt"]
expected = <<~EXPECTED.rstrip
Achilles sing, O Goddess! Peleus' son;
His wrath pernicious, who ten thousand woes
Caused to Achaia's host, sent many a soul
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.
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_one_match_no_flags
# skip
pattern = "Agamemnon"
flags = []
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
iliad.txt:Of Atreus, Agamemnon, King of men.
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_several_matches_no_flags
# skip
pattern = "may"
flags = []
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
midsummer-night.txt:Nor how it may concern my modesty,
midsummer-night.txt:But I beseech your grace that I may know
midsummer-night.txt:The worst that may befall me in this case,
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_several_matches_print_line_numbers_flag
skip
pattern = "that"
flags = ["-n"]
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
midsummer-night.txt:5:But I beseech your grace that I may know
midsummer-night.txt:6:The worst that may befall me in this case,
paradise-lost.txt:2:Of that Forbidden Tree, whose mortal tast
paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_one_match_print_file_names_flag
skip
pattern = "who"
flags = ["-l"]
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
iliad.txt
paradise-lost.txt
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_several_matches_case_insensitive_flag
skip
pattern = "TO"
flags = ["-i"]
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
iliad.txt:Caused to Achaia's host, sent many a soul
iliad.txt:Illustrious into Ades premature,
iliad.txt:And Heroes gave (so stood the will of Jove)
iliad.txt:To dogs and to all ravening fowls a prey,
midsummer-night.txt:I do entreat your grace to pardon me.
midsummer-night.txt:In such a presence here to plead my thoughts;
midsummer-night.txt:If I refuse to wed Demetrius.
paradise-lost.txt:Brought Death into the World, and all our woe,
paradise-lost.txt:Restore us, and regain the blissful Seat,
paradise-lost.txt:Sing Heav'nly Muse, that on the secret top
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_several_matches_inverted_flag
skip
pattern = "a"
flags = ["-v"]
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
iliad.txt:Achilles sing, O Goddess! Peleus' son;
iliad.txt:The noble Chief Achilles from the son
midsummer-night.txt:If I refuse to wed Demetrius.
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_one_match_match_entire_lines_flag
skip
pattern = "But I beseech your grace that I may know"
flags = ["-x"]
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
midsummer-night.txt:But I beseech your grace that I may know
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_one_match_multiple_flags
skip
pattern = "WITH LOSS OF EDEN, TILL ONE GREATER MAN"
flags = ["-n", "-i", "-x"]
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
paradise-lost.txt:4:With loss of Eden, till one greater Man
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_no_matches_various_flags
skip
pattern = "Frodo"
flags = ["-n", "-l", "-x", "-i"]
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_several_matches_file_flag_takes_precedence_over_line_number_flag
skip
pattern = "who"
flags = ["-n", "-l"]
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
iliad.txt
paradise-lost.txt
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
def test_multiple_files_several_matches_inverted_and_match_entire_lines_flags
skip
pattern = "Illustrious into Ades premature,"
flags = ["-x", "-v"]
files = ["iliad.txt", "midsummer-night.txt", "paradise-lost.txt"]
expected = <<~EXPECTED.rstrip
iliad.txt:Achilles sing, O Goddess! Peleus' son;
iliad.txt:His wrath pernicious, who ten thousand woes
iliad.txt:Caused to Achaia's host, sent many a soul
iliad.txt:And Heroes gave (so stood the will of Jove)
iliad.txt:To dogs and to all ravening fowls a prey,
iliad.txt:When fierce dispute had separated once
iliad.txt:The noble Chief Achilles from the son
iliad.txt:Of Atreus, Agamemnon, King of men.
midsummer-night.txt:I do entreat your grace to pardon me.
midsummer-night.txt:I know not by what power I am made bold,
midsummer-night.txt:Nor how it may concern my modesty,
midsummer-night.txt:In such a presence here to plead my thoughts;
midsummer-night.txt:But I beseech your grace that I may know
midsummer-night.txt:The worst that may befall me in this case,
midsummer-night.txt:If I refuse to wed Demetrius.
paradise-lost.txt:Of Mans First Disobedience, and the Fruit
paradise-lost.txt:Of that Forbidden Tree, whose mortal tast
paradise-lost.txt:Brought Death into the World, and all our woe,
paradise-lost.txt:With loss of Eden, till one greater Man
paradise-lost.txt:Restore us, and regain the blissful Seat,
paradise-lost.txt:Sing Heav'nly Muse, that on the secret top
paradise-lost.txt:Of Oreb, or of Sinai, didst inspire
paradise-lost.txt:That Shepherd, who first taught the chosen Seed
EXPECTED
assert_equal expected, Grep.grep(pattern, flags, files)
end
end