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

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

(def: size
  Test
  (do [! random.monad]
    [size (# ! each (n.% 10) random.nat)
     sample (random.unicode size)]
    (all _.and
         (_.coverage [/.size]
           (n.= size (/.size sample)))
         (_.coverage [/.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 composite inner outer)
           fake_index (-- 0)]]
    (`` (all _.and
             (~~ (template [<affix> <predicate>]
                   [(_.coverage [<affix> <predicate>]
                      (<predicate> outer (<affix> outer inner)))]

                   [/.prefix /.starts_with?]
                   [/.suffix /.ends_with?]
                   [/.enclosed' /.enclosed_by?]
                   ))
             (_.coverage [/.enclosed]
               (let [value (/.enclosed [left right] inner)]
                 (and (/.starts_with? left value)
                      (/.ends_with? right value))))
             (_.coverage [/.format]
               (let [sample (/.format inner)]
                 (and (/.enclosed_by? /.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 (-- 0)]]
    (all _.and
         (_.coverage [/.contains?]
           (let [full (# /.monoid composite inner outer)]
             (and (/.contains? inner full)
                  (/.contains? outer full))))
         (_.coverage [/.index]
           (and (|> (/.index inner (# /.monoid composite inner outer))
                    (maybe.else fake_index)
                    (n.= 0))
                (|> (/.index outer (# /.monoid composite inner outer))
                    (maybe.else fake_index)
                    (n.= 1))))
         (_.coverage [/.index_since]
           (let [full (# /.monoid composite inner outer)]
             (and (|> (/.index_since 0 inner full)
                      (maybe.else fake_index)
                      (n.= 0))
                  (|> (/.index_since 1 inner full)
                      (maybe.else fake_index)
                      (n.= fake_index))
                  
                  (|> (/.index_since 0 outer full)
                      (maybe.else fake_index)
                      (n.= 1))
                  (|> (/.index_since 1 outer full)
                      (maybe.else fake_index)
                      (n.= 1))
                  (|> (/.index_since 2 outer full)
                      (maybe.else fake_index)
                      (n.= fake_index)))))
         (_.coverage [/.last_index]
           (let [full (all (# /.monoid composite) outer inner outer)]
             (and (|> (/.last_index inner full)
                      (maybe.else fake_index)
                      (n.= 1))
                  (|> (/.last_index outer full)
                      (maybe.else fake_index)
                      (n.= 2)))))
         )))

(def: char
  Test
  (all _.and
       (_.for [/.Char /.of_char]
              (`` (all _.and
                       (~~ (template [<short> <long>]
                             [(_.coverage [<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]))
                       (_.coverage [/.line_feed]
                         (# /.equivalence = /.new_line /.line_feed))
                       )))
       (do [! random.monad]
         [size (# ! each (|>> (n.% 10) ++) random.nat)
          characters (random.set /.hash size (random.alphabetic 1))
          .let [sample (|> characters set.list /.together)]
          expected (# ! each (n.% size) random.nat)]
         (_.coverage [/.char]
           (case (/.char expected sample)
             {.#Some char}
             (case (/.index (/.of_char char) sample)
               {.#Some actual}
               (n.= expected actual)

               _
               false)
             
             {.#None}
             false)))
       (_.coverage [/.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 (# ! each (|>> (n.% 10) (n.+ 2)) random.nat)
     characters (random.set /.hash size (random.alphabetic 1))
     separator (random.only (|>> (set.member? characters) not)
                            (random.alphabetic 1))
     .let [with_no_separator (|> characters set.list /.together)]
     static (random.alphabetic 1)
     .let [dynamic (random.only (|>> (# /.equivalence = static) not)
                                (random.alphabetic 1))]
     pre dynamic
     post dynamic

     lower (random.lower_case 1)
     upper (random.upper_case 1)]
    (all _.and
         (_.coverage [/.together]
           (n.= (set.size characters)
                (/.size (/.together (set.list characters)))))
         (_.coverage [/.interposed /.all_split_by]
           (and (|> (set.list characters)
                    (/.interposed separator)
                    (/.all_split_by separator)
                    (set.of_list /.hash)
                    (# set.equivalence = characters))
                (# /.equivalence =
                   (/.together (set.list characters))
                   (/.interposed "" (set.list characters)))))
         (_.coverage [/.replaced_once]
           (# /.equivalence =
              (# /.monoid composite post static)
              (/.replaced_once pre post (# /.monoid composite pre static))))
         (_.coverage [/.split_by]
           (case (/.split_by static (all (# /.monoid composite) pre static post))
             {.#Some [left right]}
             (and (# /.equivalence = pre left)
                  (# /.equivalence = post right))
             
             {.#None}
             false))
         (_.coverage [/.lower_cased]
           (let [effectiveness!
                 (|> upper
                     /.lower_cased
                     (# /.equivalence = upper)
                     not)

                 idempotence!
                 (|> lower
                     /.lower_cased
                     (# /.equivalence = lower))
                 
                 inverse!
                 (|> lower
                     /.upper_cased
                     /.lower_cased
                     (# /.equivalence = lower))]
             (and effectiveness!
                  idempotence!
                  inverse!)))
         (_.coverage [/.upper_cased]
           (let [effectiveness!
                 (|> lower
                     /.upper_cased
                     (# /.equivalence = lower)
                     not)

                 idempotence!
                 (|> upper
                     /.upper_cased
                     (# /.equivalence = upper))
                 
                 inverse!
                 (|> upper
                     /.lower_cased
                     /.upper_cased
                     (# /.equivalence = upper))]
             (and effectiveness!
                  idempotence!
                  inverse!)))
         )))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [.Text])
      (all _.and
           (_.for [/.equivalence]
                  ($equivalence.spec /.equivalence (random.ascii 2)))
           (_.for [/.hash]
                  ($hash.spec /.hash (random.ascii 1)))
           (_.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 (/.together (list sampleL sampleR))
                    (open "/#[0]") /.equivalence]]
             (all _.and
                  (_.coverage [/.split_at]
                    (|> (/.split_at sizeL sample)
                        (pipe.case
                          {.#Right [_l _r]}
                          (and (/#= sampleL _l)
                               (/#= sampleR _r)
                               (/#= sample (/.together (list _l _r))))

                          _
                          #0)))
                  (_.coverage [/.clip /.clip_since]
                    (|> [(/.clip 0 sizeL sample)
                         (/.clip sizeL (n.- sizeL (/.size sample)) sample)
                         (/.clip_since sizeL sample)
                         (/.clip_since 0 sample)]
                        (pipe.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 (# ! each (|>> (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 (/.together (list.interposed sep1 parts))
                    sample2 (/.together (list.interposed sep2 parts))
                    (open "/#[0]") /.equivalence]]
             (_.coverage [/.replaced]
               (/#= sample2
                    (/.replaced sep1 sep2 sample1))))

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