aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/test/unit.lux
blob: 14b09f01430eb0f9b4e79fb22a0bd8530e67e4aa (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
(.require
 [library
  [lux (.except and for)
   ["[0]" debug]
   [abstract
    [monad (.only do)]]
   [control
    ["<>" parser]
    ["[0]" io]
    [concurrency
     ["[0]" async (.only Async) (.use "[1]#[0]" monad)]]]
   [data
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]
     ["[0]" set (.only Set)]]]
   [math
    [number (.only hex)]]
   ["[0]" meta (.only)
    ["[0]" symbol]
    ["[0]" code (.only)
     ["<[1]>" \\parser]]
    [macro
     [syntax (.only syntax)]]]]]
 [//
  ["[0]" coverage (.only Coverage)]
  ["[0]" tally (.only Tally)]])

(type .public Test
  (Async [Tally Text]))

(def separator
  text.new_line)

(def .public (and left right)
  (-> Test Test Test)
  (let [[read! write!] (is [(Async [Tally Text])
                            (async.Resolver [Tally Text])]
                           (async.async []))
        _ (|> left
              (async.upon! (function (_ [l_tally l_documentation])
                             (async.upon! (function (_ [r_tally r_documentation])
                                            (write! [(tally.and l_tally r_tally)
                                                     (format l_documentation ..separator r_documentation)]))
                                          right)))
              io.run!)]
    read!))

(def (context' description)
  (-> Text Test Test)
  (async#each (function (_ [tally documentation])
                [tally (|> documentation
                           (text.all_split_by ..separator)
                           (list#each (|>> (format text.tab)))
                           (text.interposed ..separator)
                           (format description ..separator))])))

(def .public context
  (-> Text Test Test)
  (|>> %.text context'))

(with_template [<prefix> <tally> <raw> <quoted>]
  [(def <raw>
     (-> Text Test)
     (|>> (format <prefix>)
          [<tally>]
          async#in))

   (def .public <quoted>
     (-> Text Test)
     (|>> %.text <raw>))]

  ["[Failure] " tally.failure failure' failure]
  ["[Success] " tally.success success' success]
  )

(def (test' message condition)
  (-> Text Bit Test)
  (if condition
    (success' message)
    (failure' message)))

(def .public (test message condition)
  (-> Text Bit Test)
  (test' (%.text message) condition))

(def definition_separator " & ")

(def clean_up_marker (text.of_char (hex "05")))

(def coverage_format
  (%.Format Symbol)
  (|>> %.symbol (format ..clean_up_marker)))

(def (with_coverage coverage condition)
  (-> (List Symbol) Bit Test)
  (let [message (|> coverage
                    (list#each ..coverage_format)
                    (text.interposed ..definition_separator))
        coverage (set.of_list symbol.hash coverage)]
    (|> (..test' message condition)
        (async#each (function (_ [tally documentation])
                      [(revised tally.#actual (set.union coverage) tally)
                       documentation])))))

(def .public coverage
  (syntax (_ [coverage (<code>.tuple (<>.many <code>.any))
              condition <code>.any])
    (let [coverage (list#each (function (_ definition)
                                (` (coverage.of (, definition))))
                              coverage)]
      (in (list (` ((debug.private ..with_coverage)
                    (is (.List .Symbol)
                        (.list (,* coverage)))
                    (, condition))))))))

(def (for' coverage test)
  (-> (List Symbol) Test Test)
  (let [context (|> coverage
                    (list#each ..coverage_format)
                    (text.interposed ..definition_separator))
        coverage (set.of_list symbol.hash coverage)]
    (async#each (function (_ [tally documentation])
                  [(revised tally.#actual (set.union coverage) tally)
                   documentation])
                (..context' context test))))

(def .public for
  (syntax (_ [coverage (<code>.tuple (<>.many <code>.any))
              test <code>.any])
    (let [coverage (list#each (function (_ definition)
                                (` (coverage.of (, definition))))
                              coverage)]
      (in (list (` ((debug.private ..for')
                    (is (.List .Symbol)
                        (.list (,* coverage)))
                    (, test))))))))

(def (covering' module coverage test)
  (-> Text Text Test Test)
  (let [coverage (coverage.decoded module coverage)]
    (|> (..context' module test)
        (async#each (function (_ [tally documentation])
                      [(revised tally.#expected (set.union coverage) tally)
                       (|> documentation
                           (text.replaced (format ..clean_up_marker module symbol.separator) "")
                           (text.replaced ..clean_up_marker ""))])))))

(def .public covering
  (syntax (_ [module <code>.symbol
              test <code>.any])
    (do meta.monad
      [.let [module (symbol.module module)]
       definitions (meta.definitions module)
       .let [coverage (|> definitions
                          (list#mix (function (_ [short [exported? _]] aggregate)
                                      (if exported?
                                        {.#Item short aggregate}
                                        aggregate))
                                    {.#End})
                          coverage.encoded)]]
      (in (list (` ((debug.private ..covering')
                    (, (code.text module))
                    (, (code.text coverage))
                    (, test))))))))