1
exercism fetch racket grep

grep-test.rkt

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
#lang racket

(require "grep.rkt")

(module+ test
  (require rackunit rackunit/text-ui)

  (define suite
    (test-suite
     "grep tests"

     (test-equal? "Test grepping a single file"
                  (grep '()              ; flags
                        "Agamemnon"      ; pattern
                        '("iliad.txt"))  ; files
                  '("Of Atreus, Agamemnon, King of men."))

     (test-equal? "One file, one match, print line numbers flag"
                  (grep '("-n")
                        "Forbidden"
                        '("paradise-lost.txt"))
                  '("2:Of that Forbidden Tree, whose mortal tast"))

     (test-equal? "One file, one match, case-insensitive flag"
                  (grep '("-i")
                        "FORBIDDEN"
                        '("paradise-lost.txt"))
                  '("Of that Forbidden Tree, whose mortal tast"))

     (test-equal? "One file, one match, print file names flag"
                  (grep '("-l")
                        "Forbidden"
                        '("paradise-lost.txt"))
                  '("paradise-lost.txt"))

     (test-equal? "One file, one match, match entire lines flag"
                  (grep '("-x")
                        "With loss of Eden, till one greater Man"
                        '("paradise-lost.txt"))
                  '("With loss of Eden, till one greater Man"))

     (test-equal? "One file, match entire lines flag, no match"
                  (grep '("-x")
                        "Eden"
                        '("paradise-lost.txt"))
                  '())

     (test-equal? "One file, one match, multiple flags"
                  (grep '("-n" "-i" "-x")
                        "OF ATREUS, Agamemnon, KIng of MEN."
                        '("iliad.txt"))
                  '("9:Of Atreus, Agamemnon, King of men."))

     (test-equal? "One file, several matches, no flags"
                  (grep '()
                        "may"
                        '("midsummer-night.txt"))
                  '("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,"))

     (test-equal? "One file, several matches, print line numbers flag"
                  (grep '("-n")
                        "may"
                        '("midsummer-night.txt"))
                  '("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,"))

     (test-equal? "One file, several matches, match entire lines flag"
                  (grep '("-x")
                        "may"
                        '("midsummer-night.txt"))
                  '())

     (test-equal? "One file, several matches, case-insensitive flag"
                  (grep '("-i")
                        "ACHILLES"
                        '("iliad.txt"))
                  '("Achilles sing, O Goddess! Peleus' son;"
                    "The noble Chief Achilles from the son"))

     (test-equal? "One file, several matches, inverted flag"
                  (grep '("-v")
                        "Of"
                        '("paradise-lost.txt"))
                  '("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"))

     (test-equal? "One file, no matches, various flags"
                  (grep '("-n" "-l" "-x" "-i")
                        "Gandalf"
                        '("iliad.txt"))
                  '())

     (test-equal? "Test grepping multiples files at once"
                  (grep '()
                        "Agamemnon"
                        '("iliad.txt" "midsummer-night.txt" "paradise-lost.txt"))
                  '("iliad.txt:Of Atreus, Agamemnon, King of men."))

     (test-equal? "Multiple files, several matches, no flags"
                  (grep '()
                        "may"
                        '("iliad.txt" "midsummer-night.txt" "paradise-lost.txt"))
                  '("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,"))

     (test-equal? "Multiple files, several matches, print line numbers flag"
                  (grep '("-n")
                        "that"
                        '("iliad.txt" "midsummer-night.txt" "paradise-lost.txt"))
                  '("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"))

     (test-equal? "Multiple files, one match, print file names flag"
                  (grep '("-l")
                        "who"
                        '("iliad.txt" "midsummer-night.txt" "paradise-lost.txt"))
                  '("iliad.txt"
                    "paradise-lost.txt"))

     (test-equal? "Multiple files, several matches, case-insensitive flag"
                  (grep '("-i")
                        "TO"
                        '("iliad.txt" "midsummer-night.txt" "paradise-lost.txt"))
                  '("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"))

     (test-equal? "Multiple files, several matches, inverted flag"
                  (grep '("-v")
                        "a"
                        '("iliad.txt" "midsummer-night.txt" "paradise-lost.txt"))
                  '("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."))

     (test-equal? "Multiple files, one match, match entire lines flag"
                  (grep '("-x")
                        "But I beseech your grace that I may know"
                        '("iliad.txt" "midsummer-night.txt" "paradise-lost.txt"))
                  '("midsummer-night.txt:But I beseech your grace that I may know"))

     (test-equal? "Multiple files, one match, multiple flags"
                  (grep '("-n" "-i" "-x")
                        "WITH LOSS OF EDEN, TILL ONE GREATER MAN"
                        '("iliad.txt" "midsummer-night.txt" "paradise-lost.txt"))
                  '("paradise-lost.txt:4:With loss of Eden, till one greater Man"))

     (test-equal? "Multiple files, no matches, various flags"
                  (grep '("-n" "-l" "-x" "-i")
                        "Frodo"
                        '("iliad.txt" "midsummer-night.txt" "paradise-lost.txt"))
                  '())))
  (run-tests suite))