1
exercism fetch fsharp grep

GrepTest.fs

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
module GrepTest

open Xunit
open FsUnit.Xunit

open Grep

open System.IO

let iliadFileName = "iliad.txt"
let 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.
"""

let midsummerNightFileName = "midsummer-night.txt"
let 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.
"""

let paradiseLostFileName = "paradise-lost.txt"
let 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
"""

type GrepFixture() =
    do
        Directory.SetCurrentDirectory(Path.GetTempPath());
        File.WriteAllText(iliadFileName, iliadContents)
        File.WriteAllText(midsummerNightFileName, midsummerNightContents)
        File.WriteAllText(paradiseLostFileName, paradiseLostContents)

    interface System.IDisposable with
        member this.Dispose() =
            File.Delete(iliadFileName)
            File.Delete(midsummerNightFileName)
            File.Delete(paradiseLostFileName)

type GrepTest(fixture: GrepFixture) =    
    interface IClassFixture<GrepFixture>

    [<Fact>]
    member this.``One file, one match, no flags`` () =
        let pattern = "Agamemnon"
        let flags = ""
        let files = [iliadFileName]

        let expected = 
            "Of Atreus, Agamemnon, King of men.\n"
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, one match, print line numbers flag`` () =
        let pattern = "Forbidden"
        let flags = "-n"
        let files = [paradiseLostFileName]

        let expected = 
            "2:Of that Forbidden Tree, whose mortal tast\n"
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, one match, case-insensitive flag`` () =
        let pattern = "Forbidden"
        let flags = "-i"
        let files = [paradiseLostFileName]

        let expected = 
            "Of that Forbidden Tree, whose mortal tast\n"
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, one match, print file names flag`` () =
        let pattern = "Forbidden"
        let flags = "-l"
        let files = [paradiseLostFileName]

        let expected = 
            sprintf "%s\n" paradiseLostFileName
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, one match, match entire lines flag`` () =
        let pattern = "With loss of Eden, till one greater Man"
        let flags = "-x"
        let files = [paradiseLostFileName]

        let expected = 
            "With loss of Eden, till one greater Man\n"
        
        grep pattern flags files |> should equal expected
        
    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, one match, multiple flags`` () =
        let pattern = "OF ATREUS, Agamemnon, KIng of MEN."
        let files = [iliadFileName]
        let flags = "-n -i -x"
        let expected = 
            "9:Of Atreus, Agamemnon, King of men.\n"
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, several matches, no flags`` () =
        let pattern = "may"
        let flags = ""
        let files = [midsummerNightFileName]

        let 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"
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, several matches, print line numbers flag`` () =
        let pattern = "may"
        let flags = "-n"
        let files = [midsummerNightFileName]

        let 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"
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, several matches, match entire lines flag`` () =
        let pattern = "may"
        let flags = "-x"
        let files = [midsummerNightFileName]

        let expected = ""
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, several matches, case-insensitive flag`` () =
        let pattern = "ACHILLES"
        let flags = "-i"
        let files = [iliadFileName]

        let expected = 
            "Achilles sing, O Goddess! Peleus' son;\n" + 
            "The noble Chief Achilles from the son\n"
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, several matches, inverted flag`` () =
        let pattern = "Of"
        let flags = "-v"
        let files = [paradiseLostFileName]

        let 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"
        
        grep pattern flags files |> should equal expected
        
    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, case-insensitive and entire line flags`` () =
        let pattern = "ATREUS"
        let files = [iliadFileName]
        let flags = "-i -x"
        let expected = ""
        
        grep pattern flags files |> should equal expected
        
    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, case-insensitive and inverted flags`` () =
        let pattern = "THE"
        let files = [paradiseLostFileName]
        let flags = "-i -v"
        let expected =
            "Of that Forbidden Tree, whose mortal tast\n" +
            "With loss of Eden, till one greater Man\n" +
            "Of Oreb, or of Sinai, didst inspire\n"

        grep pattern flags files |> should equal expected
        
    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, inverted and entire line flags`` () =
        let pattern = "the"
        let files = [paradiseLostFileName]
        let flags = "-v -x"
        let expected =
            "Of Mans First Disobedience, and the Fruit\n" +
            "Of that Forbidden Tree, whose mortal tast\n" +
            "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" +
            "Of Oreb, or of Sinai, didst inspire\n" +
            "That Shepherd, who first taught the chosen Seed\n"

        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``One file, case-insensitive, inverted and entire line flags`` () =
        let pattern = "if i rEFuse To Wed DeMETrius."
        let files = [midsummerNightFileName]
        let flags = "-i -v -x"
        let expected =
            "I do entreat your grace to pardon me.\n" +
            "I know not by what power I am made bold,\n" +
            "Nor how it may concern my modesty,\n" +
            "In such a presence here to plead my thoughts;\n" +
            "But I beseech your grace that I may know\n" +
            "The worst that may befall me in this case,\n"

        grep pattern flags files |> should equal expected

    [<Theory(Skip = "Remove to run test")>]
    [<InlineData("")>]
    [<InlineData("-n")>]
    [<InlineData("-l")>]
    [<InlineData("-x")>]
    [<InlineData("-i")>]
    [<InlineData("-n -l -x -i")>]
    member this.``One file, no matches, various flags`` (flags) =
        let pattern = "Gandalf"
        let files = [iliadFileName]
        let expected = ""
        
        grep pattern flags files |> should equal expected
        
    [<Fact(Skip = "Remove to run test")>]
    member this.``Multiple files, one match, no flags`` () =
        let pattern = "Agamemnon"
        let flags = ""
        let files = [iliadFileName; midsummerNightFileName; paradiseLostFileName]

        let expected = 
            sprintf "%s:Of Atreus, Agamemnon, King of men.\n" iliadFileName
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``Multiple files, several matches, no flags`` () =
        let pattern = "may"
        let flags = ""
        let files = [iliadFileName; midsummerNightFileName; paradiseLostFileName]

        let expected = 
            sprintf "%s:Nor how it may concern my modesty,\n" midsummerNightFileName +
            sprintf "%s:But I beseech your grace that I may know\n" midsummerNightFileName +
            sprintf "%s:The worst that may befall me in this case,\n" midsummerNightFileName
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``Multiple files, several matches, print line numbers flag`` () =
        let pattern = "that"
        let flags = "-n"
        let files = [iliadFileName; midsummerNightFileName; paradiseLostFileName]

        let expected = 
            sprintf "%s:5:But I beseech your grace that I may know\n" midsummerNightFileName +
            sprintf "%s:6:The worst that may befall me in this case,\n" midsummerNightFileName +
            sprintf "%s:2:Of that Forbidden Tree, whose mortal tast\n" paradiseLostFileName +
            sprintf "%s:6:Sing Heav'nly Muse, that on the secret top\n" paradiseLostFileName
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``Multiple files, several matches, print file names flag`` () =
        let pattern = "who"
        let flags = "-l"
        let files = [iliadFileName; midsummerNightFileName; paradiseLostFileName]

        let expected = 
            sprintf "%s\n" iliadFileName +
            sprintf "%s\n" paradiseLostFileName
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``Multiple files, several matches, case-insensitive flag`` () =
        let pattern = "TO"
        let flags = "-i"
        let files = [iliadFileName; midsummerNightFileName; paradiseLostFileName]

        let expected = 
            sprintf "%s:Caused to Achaia's host, sent many a soul\n" iliadFileName +
            sprintf "%s:Illustrious into Ades premature,\n" iliadFileName +
            sprintf "%s:And Heroes gave (so stood the will of Jove)\n" iliadFileName +
            sprintf "%s:To dogs and to all ravening fowls a prey,\n" iliadFileName +
            sprintf "%s:I do entreat your grace to pardon me.\n" midsummerNightFileName +
            sprintf "%s:In such a presence here to plead my thoughts;\n" midsummerNightFileName +
            sprintf "%s:If I refuse to wed Demetrius.\n" midsummerNightFileName +
            sprintf "%s:Brought Death into the World, and all our woe,\n" paradiseLostFileName +
            sprintf "%s:Restore us, and regain the blissful Seat,\n" paradiseLostFileName +
            sprintf "%s:Sing Heav'nly Muse, that on the secret top\n" paradiseLostFileName 
                
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``Multiple files, several matches, inverted flag`` () =
        let pattern = "a"
        let flags = "-v"
        let files = [iliadFileName; midsummerNightFileName; paradiseLostFileName]

        let expected = 
            sprintf "%s:Achilles sing, O Goddess! Peleus' son;\n" iliadFileName +
            sprintf "%s:The noble Chief Achilles from the son\n" iliadFileName +
            sprintf "%s:If I refuse to wed Demetrius.\n" midsummerNightFileName
        
        grep pattern flags files |> should equal expected

    [<Fact(Skip = "Remove to run test")>]
    member this.``Multiple files, one match, match entire lines flag`` () =
        let pattern = "But I beseech your grace that I may know"
        let flags = "-x"
        let files = [iliadFileName; midsummerNightFileName; paradiseLostFileName]

        let expected = 
            sprintf "%s:But I beseech your grace that I may know\n" midsummerNightFileName
        
        grep pattern flags files |> should equal expected
        
    [<Fact(Skip = "Remove to run test")>]
    member this.``Multiple files, one match, multiple flags`` () =
        let pattern = "WITH LOSS OF EDEN, TILL ONE GREATER MAN"
        let files = [iliadFileName; midsummerNightFileName; paradiseLostFileName]
        let flags = "-n -i -x"
        let expected = 
            sprintf "%s:4:With loss of Eden, till one greater Man\n" paradiseLostFileName
        
        grep pattern flags files |> should equal expected

    [<Theory(Skip = "Remove to run test")>]
    [<InlineData("")>]
    [<InlineData("-n")>]
    [<InlineData("-l")>]
    [<InlineData("-x")>]
    [<InlineData("-i")>]
    [<InlineData("-n -l -x -i")>]
    member this.``Multiple files, no matches, various flags`` (flags) =
        let pattern = "Frodo"
        let files = [iliadFileName; midsummerNightFileName; paradiseLostFileName]

        let expected = ""
        
        grep pattern flags files |> should equal expected