aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test/test/lux/data/text.lux
blob: fd847001eb4f786264f6938c9dae30c0aacbb7fc (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
(;module:
  lux
  (lux [io]
       (control monad)
       (data ["&" text]
             [char]
             text/format
             [number]
             (coll [list]))
       (codata function)
       ["R" random]
       pipe)
  lux/test)

(test: "Size"
  [size (:: @ map (n.% +100) R;nat)
   sample (R;text size)]
  (assert "" (or (and (n.= +0 size)
                      (&;empty? sample))
                 (n.= size (&;size sample)))))

(def: bounded-size
  (R;Random Nat)
  (|> R;nat
      (:: R;Monad<Random> map (|>. (n.% +20) (n.+ +1)))))

(test: "Locations"
  [size bounded-size
   idx (:: @ map (n.% size) R;nat)
   sample (R;text size)]
  (assert "" (|> sample
                 (&;nth idx)
                 (case> (^=> (#;Some char)
                             [(char;as-text char) char']
                             [[(&;index-of char' sample)
                               (&;last-index-of char' sample)
                               (&;index-of' char' idx sample)
                               (&;last-index-of' char' idx sample)]
                              [(#;Some io) (#;Some lio)
                               (#;Some io') (#;Some lio')]])
                        (and (n.<= idx io)
                             (n.>= idx lio)

                             (n.= idx io')
                             (n.>= idx lio')

                             (&;contains? char' sample))

                        _
                        false
                        ))
          ))

(test: "Text functions"
  [sizeL bounded-size
   sizeR bounded-size
   sampleL (R;text sizeL)
   sampleR (R;text sizeR)
   #let [sample (&;concat (list sampleL sampleR))
         fake-sample (&;join-with " " (list sampleL sampleR))
         dup-sample (&;join-with "" (list sampleL sampleR))
         enclosed-sample (&;enclose [sampleR sampleR] sampleL)
         (^open) &;Eq<Text>]]
  (assert "" (and (not (= sample fake-sample))
                  (= sample dup-sample)
                  (&;starts-with? sampleL sample)
                  (&;ends-with? sampleR sample)
                  (= enclosed-sample
                     (&;enclose' sampleR sampleL))
                  
                  (|> (&;split sizeL sample)
                      (case> (#;Right [_l _r])
                             (and (= sampleL _l)
                                  (= sampleR _r)
                                  (= sample (&;concat (list _l _r))))

                             _
                             false))
                  
                  (|> [(&;sub +0 sizeL sample)
                       (&;sub sizeL (&;size sample) sample)
                       (&;sub' sizeL sample)
                       (&;sub' +0 sample)]
                      (case> [(#;Right _l) (#;Right _r) (#;Right _r') (#;Right _f)]
                             (and (= sampleL _l)
                                  (= sampleR _r)
                                  (= _r _r')
                                  (= sample _f))

                             _
                             false))
                  )
          ))

(test: "More text functions"
  [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 (|> R;char (:: @ map (|>. char;code (n.% +128) (n.max +1) char;char)))]
   sep1 (R;text' normal-char-gen +1)
   sep2 (R;text' normal-char-gen +1)
   #let [part-gen (|> (R;text' normal-char-gen sizeP)
                      (R;filter (. not (&;contains? sep1))))]
   parts (R;list sizeL part-gen)
   #let [sample1 (&;concat (list;interpose sep1 parts))
         sample2 (&;concat (list;interpose sep2 parts))
         (^open "&/") &;Eq<Text>]]
  ($_ seq
      (assert "Can split text through a separator."
              (n.= (list;size parts)
                   (list;size (&;split-all-with sep1 sample1))))

      (assert "Can replace occurrences of a piece of text inside a larger text."
              (&/= sample2
                   (&;replace sep1 sep2 sample1)))
      ))

(test: "Other text functions"
  (let [(^open "&/") &;Eq<Text>]
    ($_ seq
        (assert "Can transform texts in certain ways."
                (and (&/= "abc" (&;lower-case "ABC"))
                     (&/= "ABC" (&;upper-case "abc"))
                     (&/= "ABC" (&;trim " \tABC\n\r"))))
        )))

(test: "Structures"
  (let [(^open "&/") &;Ord<Text>]
    ($_ seq
        (assert "" (&/< "bcd" "abc"))
        (assert "" (not (&/< "abc" "abc")))
        (assert "" (not (&/< "abc" "bcd")))
        (assert "" (&/<= "bcd" "abc"))
        (assert "" (&/<= "abc" "abc"))
        (assert "" (not (&/<= "abc" "bcd")))
        (assert "" (&/> "abc" "bcd"))
        (assert "" (not (&/> "abc" "abc")))
        (assert "" (not (&/> "bcd" "abc")))
        (assert "" (&/>= "abc" "bcd"))
        (assert "" (&/>= "abc" "abc"))
        (assert "" (not (&/>= "bcd" "abc")))
        )))

(test: "Codec"
  [size bounded-size
   sample (R;text size)
   #let [(^open) &;Eq<Text>]]
  (assert "" (|> sample
                 (:: &;Codec<Text,Text> encode)
                 (:: &;Codec<Text,Text> decode)
                 (case> (#;Right decoded)
                        (= sample decoded)

                        _
                        false))))