aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/text/lexer.lux
blob: 77419362a3b364d965480171ffe30273fa3043b1 (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
(.module:
  [lux #*
   [control
    [monad (#+ do Monad)]
    pipe
    ["p" parser]]
   [data
    ["." error (#+ Error)]
    ["." text ("#;." equivalence)
     format
     ["&" lexer]]
    [collection
     ["." list]]]
   [math
    ["r" random]]]
  lux/test)

## [Utils]
(def: (should-fail input)
  (All [a] (-> (Error a) Bit))
  (case input
    (#.Left _) #1
    _          #0))

(def: (should-passT test input)
  (-> Text (Error Text) Bit)
  (case input
    (#.Right output)
    (text;= test output)
    
    _
    #0))

(def: (should-passL test input)
  (-> (List Text) (Error (List Text)) Bit)
  (let [(^open "list;.") (list.equivalence text.equivalence)]
    (case input
      (#.Right output)
      (list;= test output)
      
      _
      #0)))

(def: (should-passE test input)
  (-> (Either Text Text) (Error (Either Text Text)) Bit)
  (case input
    (#.Right output)
    (case [test output]
      [(#.Left test) (#.Left output)]
      (text;= test output)

      [(#.Right test) (#.Right output)]
      (text;= test output)

      _
      #0)
    
    _
    #0))

## [Tests]
(context: "End"
  ($_ seq
      (test "Can detect the end of the input."
            (|> (&.run ""
                       &.end)
                (case> (#.Right _) #1 _ #0)))
      
      (test "Won't mistake non-empty text for no more input."
            (|> (&.run "YOLO"
                       &.end)
                (case> (#.Left _) #1 _ #0)))
      ))

(context: "Literals"
  (<| (times 100)
      (do @
        [size (|> r.nat (:: @ map (|>> (n/% 100) (n/max 10))))
         sample (r.unicode size)
         non-sample (|> (r.unicode size)
                        (r.filter (|>> (text;= sample) not)))]
        ($_ seq
            (test "Can find literal text fragments."
                  (and (|> (&.run sample
                                  (&.this sample))
                           (case> (#.Right []) #1 _ #0))
                       (|> (&.run non-sample
                                  (&.this sample))
                           (case> (#.Left _) #1 _ #0))))
            ))))

(context: "Custom lexers"
  ($_ seq
      (test "Can lex anything"
            (and (should-passT "A" (&.run "A"
                                          &.any))
                 (should-fail (&.run ""
                                     &.any))))

      (test "Can lex characters ranges."
            (and (should-passT "Y" (&.run "Y"
                                          (&.range (char "X") (char "Z"))))
                 (should-fail (&.run "M"
                                     (&.range (char "X") (char "Z"))))))
      
      (test "Can lex upper-case and lower-case letters."
            (and (should-passT "Y" (&.run "Y"
                                          &.upper))
                 (should-fail (&.run "m"
                                     &.upper))
                 
                 (should-passT "y" (&.run "y"
                                          &.lower))
                 (should-fail (&.run "M"
                                     &.lower))))

      (test "Can lex numbers."
            (and (should-passT "1" (&.run "1"
                                          &.decimal))
                 (should-fail (&.run " "
                                     &.decimal))

                 (should-passT "7" (&.run "7"
                                          &.octal))
                 (should-fail (&.run "8"
                                     &.octal))

                 (should-passT "1" (&.run "1"
                                          &.hexadecimal))
                 (should-passT "a" (&.run "a"
                                          &.hexadecimal))
                 (should-passT "A" (&.run "A"
                                          &.hexadecimal))
                 (should-fail (&.run " "
                                     &.hexadecimal))
                 ))

      (test "Can lex alphabetic characters."
            (and (should-passT "A" (&.run "A"
                                          &.alpha))
                 (should-passT "a" (&.run "a"
                                          &.alpha))
                 (should-fail (&.run "1"
                                     &.alpha))))

      (test "Can lex alphanumeric characters."
            (and (should-passT "A" (&.run "A"
                                          &.alpha-num))
                 (should-passT "a" (&.run "a"
                                          &.alpha-num))
                 (should-passT "1" (&.run "1"
                                          &.alpha-num))
                 (should-fail (&.run " "
                                     &.alpha-num))))

      (test "Can lex white-space."
            (and (should-passT " " (&.run " "
                                          &.space))
                 (should-fail (&.run "8"
                                     &.space))))
      ))

(context: "Combinators"
  ($_ seq
      (test "Can combine lexers sequentially."
            (and (|> (&.run "YO"
                            (p.and &.any &.any))
                     (case> (#.Right ["Y" "O"]) #1
                            _ #0))
                 (should-fail (&.run "Y"
                                     (p.and &.any &.any)))))
      
      (test "Can create the opposite of a lexer."
            (and (should-passT "a" (&.run "a"
                                          (&.not (p.or &.decimal &.upper))))
                 (should-fail (&.run "A"
                                     (&.not (p.or &.decimal &.upper))))))
      
      (test "Can select from among a set of characters."
            (and (should-passT "C" (&.run "C"
                                          (&.one-of "ABC")))
                 (should-fail (&.run "D"
                                     (&.one-of "ABC")))))

      (test "Can avoid a set of characters."
            (and (should-passT "D" (&.run "D"
                                          (&.none-of "ABC")))
                 (should-fail (&.run "C"
                                     (&.none-of "ABC")))))
      
      (test "Can lex using arbitrary predicates."
            (and (should-passT "D" (&.run "D"
                                          (&.satisfies (function (_ c) #1))))
                 (should-fail (&.run "C"
                                     (&.satisfies (function (_ c) #0))))))
      
      (test "Can apply a lexer multiple times."
            (and (should-passT "0123456789ABCDEF" (&.run "0123456789ABCDEF"
                                                         (&.many &.hexadecimal)))
                 (should-fail (&.run "yolo"
                                     (&.many &.hexadecimal)))

                 (should-passT "" (&.run ""
                                         (&.some &.hexadecimal)))))
      ))