aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/meta/annotation.lux
blob: 791e5759a12e03185614ae7a0743e9dcc3d28dbf (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." try (#+ Try)]]
   [data
    ["." product]
    ["." bit]
    ["." name ("#\." equivalence)]
    ["." text]
    [collection
     ["." list ("#\." functor)]]]
   [macro
    ["." code ("#\." equivalence)]]
   [math
    ["." random (#+ Random)]
    [number
     ["." nat]
     ["." int]
     ["." rev]
     ["." frac]]]]
  [\\
   ["." /]]
  [///
   [macro
    ["_." code]]])

(def: random_key
  (Random Name)
  (random.and (random.ascii/alpha 1)
              (random.ascii/alpha 1)))

(def: (random_sequence random)
  (All [a] (-> (Random a) (Random (List a))))
  (do {! random.monad}
    [size (|> random.nat (\ ! map (nat.% 3)))]
    (random.list size random)))

(def: (random_record random)
  (All [a] (-> (Random a) (Random (List [a a]))))
  (do {! random.monad}
    [size (|> random.nat (\ ! map (nat.% 3)))]
    (random.list size (random.and random random))))

(template: (!expect <pattern> <value>)
  (case <value>
    <pattern> true
    _ false))

(def: (annotation key value)
  (-> Name Code /.Annotation)
  (code.record (list [(code.tag key)
                      value])))

(def: typed_value
  Test
  (do {! random.monad}
    [key ..random_key]
    (`` ($_ _.and
            (~~ (template [<definition> <random> <constructor> <equivalence>]
                  [(do {! random.monad}
                     [expected <random>]
                     (_.cover [<definition>]
                              (|> expected <constructor>
                                  (..annotation key)
                                  (<definition> key)
                                  (!expect (^multi (#.Some actual)
                                                   (\ <equivalence> = expected actual))))))]

                  [/.bit random.bit code.bit bit.equivalence]
                  [/.nat random.nat code.nat nat.equivalence]
                  [/.int random.int code.int int.equivalence]
                  [/.rev random.rev code.rev rev.equivalence]
                  [/.frac random.safe_frac code.frac frac.equivalence]
                  [/.text (random.ascii/alpha 1) code.text text.equivalence]
                  [/.identifier ..random_key code.identifier name.equivalence]
                  [/.tag ..random_key code.tag name.equivalence]
                  [/.form (..random_sequence _code.random) code.form (list.equivalence code.equivalence)]
                  [/.tuple (..random_sequence _code.random) code.tuple (list.equivalence code.equivalence)]
                  [/.record (..random_record _code.random) code.record (list.equivalence (product.equivalence code.equivalence code.equivalence))]
                  ))
            ))))

(def: flag
  Test
  (do {! random.monad}
    [key ..random_key]
    (`` ($_ _.and
            (do !
              [dummy (random.filter (|>> (name\= key) not)
                                    ..random_key)
               expected random.bit]
              (_.cover [/.flagged?]
                       (and (|> expected code.bit
                                (..annotation key)
                                (/.flagged? key)
                                (\ bit.equivalence = expected))
                            (not (|> expected code.bit
                                     (..annotation dummy)
                                     (/.flagged? key))))))
            (~~ (template [<definition> <tag>]
                  [(do !
                     [expected random.bit]
                     (_.cover [<definition>]
                              (and (|> expected code.bit
                                       (..annotation (name_of <tag>))
                                       <definition>
                                       (\ bit.equivalence = expected))
                                   (not (|> expected code.bit
                                            (..annotation key)
                                            <definition>)))))]

                  [/.implementation? #.implementation?]
                  [/.recursive_type? #.type-rec?]
                  [/.signature? #.sig?]
                  ))
            ))))

(def: arguments
  Test
  (do {! random.monad}
    [key ..random_key]
    (`` ($_ _.and
            (~~ (template [<definition> <tag>]
                  [(do !
                     [expected (random.list 5 (random.ascii/alpha 1))]
                     (_.cover [<definition>]
                              (and (|> expected (list\map code.text) code.tuple
                                       (..annotation (name_of <tag>))
                                       <definition>
                                       (\ (list.equivalence text.equivalence) = expected))
                                   (|> expected (list\map code.text) code.tuple
                                       (..annotation key)
                                       <definition>
                                       (\ (list.equivalence text.equivalence) = (list))))))]

                  [/.function_arguments #.func-args]
                  [/.type_arguments #.type-args]
                  ))
            ))))

(def: #export test
  Test
  (<| (_.covering /._)
      (_.for [/.Annotation])
      (do {! random.monad}
        [key ..random_key]
        ($_ _.and
            (do !
              [expected _code.random]
              (_.cover [/.value]
                       (|> expected
                           (..annotation key)
                           (/.value key)
                           (!expect (^multi (#.Some actual)
                                            (code\= expected actual))))))
            
            ..typed_value
            
            (do !
              [expected (random.ascii/alpha 10)]
              (_.cover [/.documentation]
                       (and (not (|> expected code.text
                                     (..annotation key)
                                     /.documentation
                                     (!expect (^multi (#.Some actual)
                                                      (\ text.equivalence = expected actual)))))
                            (|> expected code.text
                                (..annotation (name_of #.doc))
                                /.documentation
                                (!expect (^multi (#.Some actual)
                                                 (\ text.equivalence = expected actual)))))))

            ..flag
            ..arguments
            ))))