aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test/test/lux/lexer/regex.lux
blob: c5c21df2abff002c849240e09d567502b3132a50 (plain)
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
(;module:
  lux
  (lux [io]
       (control monad)
       (data [error #- fail]
             [product]
             [text "T/" Eq<Text>]
             text/format)
       [compiler]
       (macro [ast]
              ["s" syntax #+ syntax:])
       ["R" math/random]
       pipe
       [lexer]
       (lexer ["&" regex]))
  lux/test)

## [Utils]
(def: (should-pass regex input)
  (-> (lexer;Lexer Text) Text Bool)
  (|> (lexer;run input regex)
      (case> (#;Right parsed)
             (T/= parsed input)

             _
             false)))

(def: (should-passT test regex input)
  (-> Text (lexer;Lexer Text) Text Bool)
  (|> (lexer;run input regex)
      (case> (#;Right parsed)
             (T/= test parsed)

             _
             false)))

(def: (should-fail regex input)
  (All [a] (-> (lexer;Lexer a) Text Bool))
  (|> (lexer;run input regex)
      (case> (#;Left _) true _ false)))

(syntax: (should-check pattern regex input)
  (wrap (list (` (|> (lexer;run (~ input) (~ regex))
                     (case> (^ (#;Right (~ pattern)))
                            true

                            (~' _)
                            false))))))

## [Tests]
(test: "Regular Expressions [Basics]"
  (assert "Can parse character literals."
          (and (should-pass (&;regex "a") "a")
               (should-fail (&;regex "a") ".")
               (should-pass (&;regex "\\.") ".")
               (should-fail (&;regex "\\.") "a"))))

(test: "Regular Expressions [System character classes]"
  ($_ seq
      (assert "Can parse anything."
              (should-pass (&;regex ".") "a"))

      (assert "Can parse digits."
              (and (should-pass (&;regex "\\d") "0")
                   (should-fail (&;regex "\\d") "m")))

      (assert "Can parse non digits."
              (and (should-pass (&;regex "\\D") "m")
                   (should-fail (&;regex "\\D") "0")))

      (assert "Can parse white-space."
              (and (should-pass (&;regex "\\s") " ")
                   (should-fail (&;regex "\\s") "m")))

      (assert "Can parse non white-space."
              (and (should-pass (&;regex "\\S") "m")
                   (should-fail (&;regex "\\S") " ")))

      (assert "Can parse word characters."
              (and (should-pass (&;regex "\\w") "_")
                   (should-fail (&;regex "\\w") "^")))

      (assert "Can parse non word characters."
              (and (should-pass (&;regex "\\W") ".")
                   (should-fail (&;regex "\\W") "a")))
      ))

(test: "Regular Expressions [Special system character classes : Part 1]"
  ($_ seq
      (assert "Can parse using special character classes."
              (and (and (should-pass (&;regex "\\p{Lower}") "m")
                        (should-fail (&;regex "\\p{Lower}") "M"))

                   (and (should-pass (&;regex "\\p{Upper}") "M")
                        (should-fail (&;regex "\\p{Upper}") "m"))

                   (and (should-pass (&;regex "\\p{Alpha}") "M")
                        (should-fail (&;regex "\\p{Alpha}") "0"))

                   (and (should-pass (&;regex "\\p{Digit}") "1")
                        (should-fail (&;regex "\\p{Digit}") "n"))

                   (and (should-pass (&;regex "\\p{Alnum}") "1")
                        (should-fail (&;regex "\\p{Alnum}") "."))

                   (and (should-pass (&;regex "\\p{Space}") " ")
                        (should-fail (&;regex "\\p{Space}") "."))
                   ))
      ))

(test: "Regular Expressions [Special system character classes : Part 2]"
  ($_ seq
      (assert "Can parse using special character classes."
              (and (and (should-pass (&;regex "\\p{HexDigit}") "a")
                        (should-fail (&;regex "\\p{HexDigit}") "."))

                   (and (should-pass (&;regex "\\p{OctDigit}") "6")
                        (should-fail (&;regex "\\p{OctDigit}") "."))

                   (and (should-pass (&;regex "\\p{Blank}") "\t")
                        (should-fail (&;regex "\\p{Blank}") "."))

                   (and (should-pass (&;regex "\\p{ASCII}") "\t")
                        (should-fail (&;regex "\\p{ASCII}") "\u1234"))

                   (and (should-pass (&;regex "\\p{Contrl}") "\u0012")
                        (should-fail (&;regex "\\p{Contrl}") "a"))

                   (and (should-pass (&;regex "\\p{Punct}") "@")
                        (should-fail (&;regex "\\p{Punct}") "a"))

                   (and (should-pass (&;regex "\\p{Graph}") "@")
                        (should-fail (&;regex "\\p{Graph}") " "))

                   (and (should-pass (&;regex "\\p{Print}") "\u0020")
                        (should-fail (&;regex "\\p{Print}") "\u1234"))
                   ))
      ))

(test: "Regular Expressions [Custom character classes : Part 1]"
  ($_ seq
      (assert "Can parse using custom character classes."
              (and (should-pass (&;regex "[abc]") "a")
                   (should-fail (&;regex "[abc]") "m")))

      (assert "Can parse using character ranges."
              (and (should-pass (&;regex "[a-z]") "a")
                   (should-pass (&;regex "[a-z]") "m")
                   (should-pass (&;regex "[a-z]") "z")))

      (assert "Can combine character ranges."
              (and (should-pass (&;regex "[a-zA-Z]") "a")
                   (should-pass (&;regex "[a-zA-Z]") "m")
                   (should-pass (&;regex "[a-zA-Z]") "z")
                   (should-pass (&;regex "[a-zA-Z]") "A")
                   (should-pass (&;regex "[a-zA-Z]") "M")
                   (should-pass (&;regex "[a-zA-Z]") "Z")))
      ))

(test: "Regular Expressions [Custom character classes : Part 2]"
  ($_ seq
      (assert "Can negate custom character classes."
              (and (should-fail (&;regex "[^abc]") "a")
                   (should-pass (&;regex "[^abc]") "m")))

      (assert "Can negate character ranges.."
              (and (should-fail (&;regex "[^a-z]") "a")
                   (should-pass (&;regex "[^a-z]") "0")))

      (assert "Can parse negate combinations of character ranges."
              (and (should-fail (&;regex "[^a-zA-Z]") "a")
                   (should-pass (&;regex "[^a-zA-Z]") "0")))
      ))

(test: "Regular Expressions [Custom character classes : Part 3]"
  ($_ seq
      (assert "Can make custom character classes more specific."
              (and (let [RE (&;regex "[a-z&&[def]]")]
                     (and (should-fail RE "a")
                          (should-pass RE "d")))

                   (let [RE (&;regex "[a-z&&[^bc]]")]
                     (and (should-pass RE "a")
                          (should-fail RE "b")))

                   (let [RE (&;regex "[a-z&&[^m-p]]")]
                     (and (should-pass RE "a")
                          (should-fail RE "m")
                          (should-fail RE "p")))))
      ))

(test: "Regular Expressions [Reference]"
  (let [number (&;regex "\\d+")]
    (assert "Can build complex regexs by combining simpler ones."
            (should-check ["809-345-6789" "809" "345" "6789"] (&;regex "(\\@<number>)-(\\@<number>)-(\\@<number>)") "809-345-6789"))))

(test: "Regular Expressions [Fuzzy Quantifiers]"
  ($_ seq
      (assert "Can sequentially combine patterns."
              (should-passT "aa" (&;regex "aa") "aa"))

      (assert "Can match patterns optionally."
              (and (should-passT "a" (&;regex "a?") "a")
                   (should-passT "" (&;regex "a?") "")))

      (assert "Can match a pattern 0 or more times."
              (and (should-passT "aaa" (&;regex "a*") "aaa")
                   (should-passT "" (&;regex "a*") "")))

      (assert "Can match a pattern 1 or more times."
              (and (should-passT "aaa" (&;regex "a+") "aaa")
                   (should-passT "a" (&;regex "a+") "a")
                   (should-fail (&;regex "a+") "")))
      ))

(test: "Regular Expressions [Crisp Quantifiers]"
  ($_ seq
      (assert "Can match a pattern N times."
              (and (should-passT "aa" (&;regex "a{2}") "aa")
                   (should-passT "a" (&;regex "a{1}") "aa")
                   (should-fail (&;regex "a{3}") "aa")))

      (assert "Can match a pattern at-least N times."
              (and (should-passT "aa" (&;regex "a{1,}") "aa")
                   (should-passT "aa" (&;regex "a{2,}") "aa")
                   (should-fail (&;regex "a{3,}") "aa")))

      (assert "Can match a pattern at-most N times."
              (and (should-passT "a" (&;regex "a{,1}") "aa")
                   (should-passT "aa" (&;regex "a{,2}") "aa")
                   (should-passT "aa" (&;regex "a{,3}") "aa")))

      (assert "Can match a pattern between N and M times."
              (and (should-passT "a" (&;regex "a{1,2}") "a")
                   (should-passT "aa" (&;regex "a{1,2}") "aa")
                   (should-passT "aa" (&;regex "a{1,2}") "aaa")))
      ))

(test: "Regular Expressions [Groups]"
  ($_ seq
      (assert "Can extract groups of sub-matches specified in a pattern."
              (and (should-check ["abc" "b"] (&;regex "a(.)c") "abc")
                   (should-check ["abbbbbc" "bbbbb"] (&;regex "a(b+)c") "abbbbbc")
                   (should-check ["809-345-6789" "809" "345" "6789"] (&;regex "(\\d{3})-(\\d{3})-(\\d{4})") "809-345-6789")
                   (should-check ["809-345-6789" "809" "6789"] (&;regex "(\\d{3})-(?:\\d{3})-(\\d{4})") "809-345-6789")
                   (should-check ["809-809-6789" "809" "6789"] (&;regex "(\\d{3})-\\0-(\\d{4})") "809-809-6789")
                   (should-check ["809-809-6789" "809" "6789"] (&;regex "(?<code>\\d{3})-\\k<code>-(\\d{4})") "809-809-6789")
                   (should-check ["809-809-6789-6789" "809" "6789"] (&;regex "(?<code>\\d{3})-\\k<code>-(\\d{4})-\\0") "809-809-6789-6789")))

      (assert "Can specify groups within groups."
              (should-check ["809-345-6789" "809" ["345-6789" "345" "6789"]] (&;regex "(\\d{3})-((\\d{3})-(\\d{4}))") "809-345-6789"))
      ))

(test: "Regular Expressions [Alternation]"
  ($_ seq
      (assert "Can specify alternative patterns."
              (and (should-check ["a" (+0 [])] (&;regex "a|b") "a")
                   (should-check ["b" (+1 [])] (&;regex "a|b") "b")
                   (should-fail (&;regex "a|b") "c")))

      (assert "Can have groups within alternations."
              (and (should-check ["abc" (+0 ["b" "c"])] (&;regex "a(.)(.)|b(.)(.)") "abc")
                   (should-check ["bcd" (+1 ["c" "d"])] (&;regex "a(.)(.)|b(.)(.)") "bcd")
                   (should-fail (&;regex "a(.)(.)|b(.)(.)") "cde")

                   (should-check ["809-345-6789" (+0 ["809" "345-6789" "345" "6789"])]
                                 (&;regex "(\\d{3})-((\\d{3})-(\\d{4}))|b(.)d")
                                 "809-345-6789")))
      ))