aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/console.lux
blob: 055ee1466856df0947416df1400fb7f2eb183753 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [monad (#+ do)]]
    [control
     ["." io]
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]]
    [data
     ["." text ("#\." equivalence)
      ["%" format (#+ format)]]]
    [math
     ["." random]]]]
  [\\library
   ["." /]]
  [\\spec
   ["$." /]])

(exception: dead)

(def: mock
  (/.Mock [Bit Text])
  (implementation
   (def: (on_read [dead? content])
     (do try.monad
       [char (try.from_maybe (text.nth 0 content))
        [_ content] (try.from_maybe (text.split 1 content))]
       (if dead?
         (exception.throw ..dead [])
         (wrap [[dead? content] char]))))

   (def: (on_read_line [dead? content])
     (do try.monad
       [[line content] (try.from_maybe (text.split_with text.new_line content))]
       (if dead?
         (exception.throw ..dead [])
         (wrap [[dead? content] line]))))

   (def: (on_write message [dead? content])
     (if dead?
       (exception.throw ..dead [])
       (#try.Success [dead? (format content message)])))

   (def: (on_close [dead? content])
     (if dead?
       (exception.throw ..dead [])
       (#try.Success [true content])))))

(def: #export test
  Test
  (<| (_.covering /._)
      ($_ _.and
          (_.for [/.async /.mock /.Mock]
                 ($/.spec (io.io (/.async (/.mock ..mock [false ""])))))
          (do random.monad
            [expected (random.ascii/alpha 10)
             #let [console (/.mock ..mock [false ""])]]
            (_.cover [/.write_line]
                     (io.run
                      (do io.monad
                        [?_ (/.write_line expected console)
                         ?actual (\ console read_line [])]
                        (wrap (<| (try.default false)
                                  (do try.monad
                                    [_ ?_
                                     actual ?actual]
                                    (wrap (text\= expected actual)))))))))
          )))