aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/text.lux
blob: 7223497d154f9530d9214f3be10e296753834171 (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
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
(.module:
  [library
   [lux (#- char)
    ["_" test (#+ Test)]
    [abstract
     [monad (#+ do)]
     [\\specification
      ["$." equivalence]
      ["$." hash]
      ["$." order]
      ["$." monoid]]]
    [control
     pipe]
    [data
     ["." maybe]
     [collection
      ["." list]
      ["." set]]]
    [math
     ["." random]
     [number
      ["n" nat]]]]]
  ["." / #_
   ["#." buffer]
   ["#." encoding]
   ["#." format]
   ["#." regex]
   ["#." escape]
   ["#." unicode #_
    ["#" set]]]
  [\\library
   ["." /]])

(def: bounded_size
  (random.Random Nat)
  (|> random.nat
      (\ random.monad map (|>> (n.% 20) (n.+ 1)))))

(def: size
  Test
  (do {! random.monad}
    [size (\ ! map (n.% 10) random.nat)
     sample (random.unicode size)]
    ($_ _.and
        (_.cover [/.size]
                 (n.= size (/.size sample)))
        (_.cover [/.empty?]
                 (or (/.empty? sample)
                     (not (n.= 0 size)))))))

(def: affix
  Test
  (do {! random.monad}
    [inner (random.unicode 1)
     outer (random.only (|>> (\ /.equivalence = inner) not)
                        (random.unicode 1))
     left (random.unicode 1)
     right (random.unicode 1)
     #let [full (\ /.monoid compose inner outer)
           fake_index (dec 0)]]
    (`` ($_ _.and
            (~~ (template [<affix> <predicate>]
                  [(_.cover [<affix> <predicate>]
                            (<predicate> outer (<affix> outer inner)))]

                  [/.prefix /.starts_with?]
                  [/.suffix /.ends_with?]
                  [/.enclose' /.encloses?]
                  ))
            (_.cover [/.enclose]
                     (let [value (/.enclose [left right] inner)]
                       (and (/.starts_with? left value)
                            (/.ends_with? right value))))
            (_.cover [/.format]
                     (let [sample (/.format inner)]
                       (and (/.encloses? /.double_quote sample)
                            (/.contains? inner sample))))
            ))))

(def: index
  Test
  (do {! random.monad}
    [inner (random.unicode 1)
     outer (random.only (|>> (\ /.equivalence = inner) not)
                        (random.unicode 1))
     #let [fake_index (dec 0)]]
    ($_ _.and
        (_.cover [/.contains?]
                 (let [full (\ /.monoid compose inner outer)]
                   (and (/.contains? inner full)
                        (/.contains? outer full))))
        (_.cover [/.index_of]
                 (and (|> (/.index_of inner (\ /.monoid compose inner outer))
                          (maybe.default fake_index)
                          (n.= 0))
                      (|> (/.index_of outer (\ /.monoid compose inner outer))
                          (maybe.default fake_index)
                          (n.= 1))))
        (_.cover [/.index_of']
                 (let [full (\ /.monoid compose inner outer)]
                   (and (|> (/.index_of' inner 0 full)
                            (maybe.default fake_index)
                            (n.= 0))
                        (|> (/.index_of' inner 1 full)
                            (maybe.default fake_index)
                            (n.= fake_index))
                        
                        (|> (/.index_of' outer 0 full)
                            (maybe.default fake_index)
                            (n.= 1))
                        (|> (/.index_of' outer 1 full)
                            (maybe.default fake_index)
                            (n.= 1))
                        (|> (/.index_of' outer 2 full)
                            (maybe.default fake_index)
                            (n.= fake_index)))))
        (_.cover [/.last_index_of]
                 (let [full ($_ (\ /.monoid compose) outer inner outer)]
                   (and (|> (/.last_index_of inner full)
                            (maybe.default fake_index)
                            (n.= 1))
                        (|> (/.last_index_of outer full)
                            (maybe.default fake_index)
                            (n.= 2)))))
        (_.cover [/.last_index_of']
                 (let [full ($_ (\ /.monoid compose) outer inner outer)]
                   (and (|> (/.last_index_of' inner 0 full)
                            (maybe.default fake_index)
                            (n.= 1))
                        (|> (/.last_index_of' inner 2 full)
                            (maybe.default fake_index)
                            (n.= fake_index))
                        
                        (|> (/.last_index_of' outer 0 full)
                            (maybe.default fake_index)
                            (n.= 2))
                        (|> (/.last_index_of' outer 2 full)
                            (maybe.default fake_index)
                            (n.= 2))
                        (|> (/.last_index_of' outer 3 full)
                            (maybe.default fake_index)
                            (n.= fake_index)))))
        )))

(def: char
  Test
  ($_ _.and
      (_.for [/.Char /.of_code]
             (`` ($_ _.and
                     (~~ (template [<short> <long>]
                           [(_.cover [<short> <long>]
                                     (\ /.equivalence = <short> <long>))]

                           [/.\0  /.null]
                           [/.\a  /.alarm]
                           [/.\b  /.back_space]
                           [/.\t  /.tab]
                           [/.\n  /.new_line]
                           [/.\v  /.vertical_tab]
                           [/.\f  /.form_feed]
                           [/.\r  /.carriage_return]
                           [/.\'' /.double_quote]))
                     (_.cover [/.line_feed]
                              (\ /.equivalence = /.new_line /.line_feed))
                     )))
      (do {! random.monad}
        [size (\ ! map (|>> (n.% 10) inc) random.nat)
         characters (random.set /.hash size (random.ascii/alpha 1))
         #let [sample (|> characters set.to_list /.concat)]
         expected (\ ! map (n.% size) random.nat)]
        (_.cover [/.nth]
                 (case (/.nth expected sample)
                   (#.Some char)
                   (case (/.index_of (/.of_code char) sample)
                     (#.Some actual)
                     (n.= expected actual)

                     _
                     false)
                   
                   #.None
                   false)))
      (_.cover [/.space /.space?]
               (`` (and (~~ (template [<char>]
                              [(/.space? (`` (.char (~~ (static <char>)))))]
                              
                              [/.tab]
                              [/.vertical_tab]
                              [/.space]
                              [/.new_line]
                              [/.carriage_return]
                              [/.form_feed]
                              )))))
      ))

(def: manipulation
  Test
  (do {! random.monad}
    [size (\ ! map (|>> (n.% 10) (n.+ 2)) random.nat)
     characters (random.set /.hash size (random.ascii/alpha 1))
     separator (random.only (|>> (set.member? characters) not)
                            (random.ascii/alpha 1))
     #let [with_no_separator (|> characters set.to_list /.concat)]
     static (random.ascii/alpha 1)
     #let [dynamic (random.only (|>> (\ /.equivalence = static) not)
                                (random.ascii/alpha 1))]
     pre dynamic
     post dynamic

     lower (random.ascii/lower 1)
     upper (random.ascii/upper 1)]
    ($_ _.and
        (_.cover [/.concat]
                 (n.= (set.size characters)
                      (/.size (/.concat (set.to_list characters)))))
        (_.cover [/.join_with /.split_all_with]
                 (and (|> (set.to_list characters)
                          (/.join_with separator)
                          (/.split_all_with separator)
                          (set.of_list /.hash)
                          (\ set.equivalence = characters))
                      (\ /.equivalence =
                         (/.concat (set.to_list characters))
                         (/.join_with "" (set.to_list characters)))))
        (_.cover [/.replace_once]
                 (\ /.equivalence =
                    (\ /.monoid compose post static)
                    (/.replace_once pre post (\ /.monoid compose pre static))))
        (_.cover [/.split_with]
                 (case (/.split_with static ($_ (\ /.monoid compose) pre static post))
                   (#.Some [left right])
                   (and (\ /.equivalence = pre left)
                        (\ /.equivalence = post right))
                   
                   #.None
                   false))
        (_.cover [/.lower_case]
                 (let [effectiveness!
                       (|> upper
                           /.lower_case
                           (\ /.equivalence = upper)
                           not)

                       idempotence!
                       (|> lower
                           /.lower_case
                           (\ /.equivalence = lower))
                       
                       inverse!
                       (|> lower
                           /.upper_case
                           /.lower_case
                           (\ /.equivalence = lower))]
                   (and effectiveness!
                        idempotence!
                        inverse!)))
        (_.cover [/.upper_case]
                 (let [effectiveness!
                       (|> lower
                           /.upper_case
                           (\ /.equivalence = lower)
                           not)

                       idempotence!
                       (|> upper
                           /.upper_case
                           (\ /.equivalence = upper))
                       
                       inverse!
                       (|> upper
                           /.lower_case
                           /.upper_case
                           (\ /.equivalence = upper))]
                   (and effectiveness!
                        idempotence!
                        inverse!)))
        )))

(def: #export test
  Test
  (<| (_.covering /._)
      (_.for [.Text])
      ($_ _.and
          (_.for [/.equivalence]
                 ($equivalence.spec /.equivalence (random.ascii 2)))
          (_.for [/.hash]
                 (|> (random.ascii 1)
                     ($hash.spec /.hash)))
          (_.for [/.order]
                 ($order.spec /.order (random.ascii 2)))
          (_.for [/.monoid]
                 ($monoid.spec /.equivalence /.monoid (random.ascii 2)))

          ..size
          ..affix
          ..index
          ..char
          ..manipulation
          
          (do random.monad
            [sizeL bounded_size
             sizeR bounded_size
             sampleL (random.unicode sizeL)
             sampleR (random.unicode sizeR)
             middle (random.unicode 1)
             #let [sample (/.concat (list sampleL sampleR))
                   (^open "/\.") /.equivalence]]
            ($_ _.and
                (_.cover [/.split]
                         (|> (/.split sizeL sample)
                             (case> (#.Right [_l _r])
                                    (and (/\= sampleL _l)
                                         (/\= sampleR _r)
                                         (/\= sample (/.concat (list _l _r))))

                                    _
                                    #0)))
                (_.cover [/.clip /.clip']
                         (|> [(/.clip 0 sizeL sample)
                              (/.clip sizeL (n.- sizeL (/.size sample)) sample)
                              (/.clip' sizeL sample)
                              (/.clip' 0 sample)]
                             (case> [(#.Right _l) (#.Right _r) (#.Right _r') (#.Right _f)]
                                    (and (/\= sampleL _l)
                                         (/\= sampleR _r)
                                         (/\= _r _r')
                                         (/\= sample _f))

                                    _
                                    #0)))
                ))
          (do {! random.monad}
            [sizeP bounded_size
             sizeL bounded_size
             #let [## The wider unicode charset includes control characters that
                   ## can make text replacement work improperly.
                   ## Because of that, I restrict the charset.
                   normal_char_gen (|> random.nat (\ ! map (|>> (n.% 128) (n.max 1))))]
             sep1 (random.text normal_char_gen 1)
             sep2 (random.text normal_char_gen 1)
             #let [part_gen (|> (random.text normal_char_gen sizeP)
                                (random.only (|>> (/.contains? sep1) not)))]
             parts (random.list sizeL part_gen)
             #let [sample1 (/.concat (list.interpose sep1 parts))
                   sample2 (/.concat (list.interpose sep2 parts))
                   (^open "/\.") /.equivalence]]
            (_.cover [/.replace_all]
                     (/\= sample2
                          (/.replace_all sep1 sep2 sample1))))

          /buffer.test
          /encoding.test
          /format.test
          /regex.test
          /escape.test
          /unicode.test
          )))