aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/text/escape.lux
blob: 43242b1cfc134f535d5c4213764f3952a8d7ca76 (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:
  [library
   [lux "*"
    ["_" test {"+" [Test]}]
    ["[0]" debug]
    ["[0]" meta]
    [abstract
     [monad {"+" [do]}]]
    [control
     ["[0]" try]
     ["[0]" exception]
     [parser
      ["<[0]>" code]]]
    [data
     ["[0]" bit ("[1]\[0]" equivalence)]
     ["[0]" text {"+" [Char]} ("[1]\[0]" equivalence)
      ["%" format {"+" [format]}]]
     [collection
      ["[0]" set {"+" [Set]}]]]
    [macro
     [syntax {"+" [syntax:]}]
     ["[0]" code]
     ["[0]" template]]
    [math
     ["[0]" random {"+" [Random]}]
     [number {"+" [hex]}
      ["n" nat]]]]]
  [\\library
   ["[0]" /
    [//
     ["[0]" unicode "_"
      ["[1]" set]]]]])

(def: (range max min)
  (-> Char Char (Random Char))
  (let [range (n.- min max)]
    (\ random.monad each
       (|>> (n.% range) (n.+ min))
       random.nat)))

(def: under_range
  (Random Char)
  (..range (debug.private /.ascii_bottom) 0))

(def: over_range
  (Random Char)
  (..range (hex "FFFF") (++ (debug.private /.ascii_top))))

(def: in_range
  (Random Char)
  (..range (++ (debug.private /.ascii_top)) (debug.private /.ascii_bottom)))

(def: ascii_range
  (Random Char)
  (..range (++ (debug.private /.ascii_top)) 0))

(def: valid_sigils
  (Set Char)
  (set.of_list n.hash
               (list (debug.private /.\0_sigil)
                     (debug.private /.\a_sigil)
                     (debug.private /.\b_sigil)
                     (debug.private /.\t_sigil)
                     (debug.private /.\n_sigil)
                     (debug.private /.\v_sigil)
                     (debug.private /.\f_sigil)
                     (debug.private /.\r_sigil)
                     (debug.private /.\''_sigil)
                     (debug.private /.\\_sigil)
                     (debug.private /.\u_sigil))))

(syntax: (static_sample [])
  (do meta.monad
    [seed meta.seed
     .let [[_ expected] (random.result (random.pcg_32 [seed seed])
                                       (random.ascii 10))]]
    (in (list (code.text expected)))))

(syntax: (static_escaped [un_escaped <code>.text])
  (in (list (code.text (/.escaped un_escaped)))))

(def: .public test
  Test
  (<| (_.covering /._)
      ($_ _.and
          (do random.monad
            [ascii ..ascii_range]
            (_.cover [/.escapable?]
                     (`` (if (or (~~ (template [<char>]
                                       [(n.= (debug.private <char>) ascii)]
                                       
                                       [/.\0] [/.\a] [/.\b] [/.\t]
                                       [/.\n] [/.\v] [/.\f] [/.\r]
                                       [/.\''] [/.\\])))
                           (/.escapable? ascii)
                           (bit\= (/.escapable? ascii)
                                  (or (n.< (debug.private /.ascii_bottom) ascii)
                                      (n.> (debug.private /.ascii_top) ascii)))))))
          (do random.monad
            [left (random.char unicode.character)
             right (random.char unicode.character)]
            (_.cover [/.escaped /.un_escaped]
                     (let [expected (format (text.of_char left) (text.of_char right))]
                       (if (or (/.escapable? left)
                               (/.escapable? right))
                         (let [escaped (/.escaped expected)]
                           (case (/.un_escaped escaped)
                             (#try.Success un_escaped)
                             (and (not (text\= escaped expected))
                                  (text\= un_escaped expected))
                             
                             (#try.Failure error)
                             false))
                         (text\= expected (/.escaped expected))))))
          (do [! random.monad]
            [dummy (|> (random.char unicode.character)
                       (\ ! each text.of_char))]
            (_.cover [/.dangling_escape]
                     (case (/.un_escaped (format (/.escaped dummy) "\"))
                       (#try.Success _)
                       false

                       (#try.Failure error)
                       (exception.match? /.dangling_escape error))))
          (do [! random.monad]
            [dummy (|> (random.char unicode.character)
                       (random.only (|>> (set.member? ..valid_sigils) not))
                       (\ ! each text.of_char))]
            (_.cover [/.invalid_escape]
                     (case (/.un_escaped (format "\" dummy))
                       (#try.Success _)
                       false

                       (#try.Failure error)
                       (exception.match? /.invalid_escape error))))
          (do [! random.monad]
            [too_short (|> (random.char unicode.character)
                           (\ ! each (n.% (hex "1000"))))
             code (|> (random.unicode 4)
                      (random.only (function (_ code)
                                     (case (\ n.hex decoded code)
                                       (#try.Failure error) true
                                       (#try.Success _) false))))]
            (_.cover [/.invalid_unicode_escape]
                     (template.let [(!invalid <code>)
                                    [(case (/.un_escaped (format "\u" <code>))
                                       (#try.Success _)
                                       false

                                       (#try.Failure error)
                                       (exception.match? /.invalid_unicode_escape error))]]
                       (and (!invalid (\ n.hex encoded too_short))
                            (!invalid code)))))
          (_.cover [/.literal]
                   (with_expansions [<example> (..static_sample)]
                     (text\= <example> (`` (/.literal (~~ (..static_escaped <example>)))))))
          )))