aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/pipe.lux
blob: 21d7b8b9070b6a346867fe66518ed5980f29aea9 (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [control
    [monad (#+ do)]]
   [data
    ["." identity]
    [text ("text/." equivalence)
     format]]
   [math
    ["r" random]]]
  {1
   /})

(def: #export test
  Test
  (do r.monad
    [sample r.nat]
    ($_ _.and
        (do @
          [another r.nat]
          (_.test "Can dismiss previous pipeline results and begin a new one."
                  (n/= (inc another)
                       (|> sample
                           (n/* 3)
                           (n/+ 4)
                           (new> another [inc])))))
        
        (_.test "Let-binding"
                (n/= (n/+ sample sample)
                     (|> sample
                         (let> x [(n/+ x x)]))))
        
        (_.test "'Conditional' branching."
                (text/= (cond (n/= 0 sample) "zero"
                              (n/even? sample) "even"
                              "odd")
                        (|> sample
                            (cond> [(n/= 0)] [(new> "zero" [])]
                                   [n/even?] [(new> "even" [])]
                                   [(new> "odd" [])]))))

        (_.test "'If' branching."
                (text/= (if (n/even? sample)
                          "even"
                          "odd")
                        (|> sample
                            (if> [n/even?]
                                 [(new> "even" [])]
                                 [(new> "odd" [])]))))

        (_.test "'When' branching."
                (n/= (if (n/even? sample)
                       (n/* 2 sample)
                       sample)
                     (|> sample
                         (when> [n/even?]
                                [(n/* 2)]))))

        (_.test "Can loop."
                (n/= (n/* 10 sample)
                     (|> sample
                         (loop> [(n/= (n/* 10 sample)) not]
                                [(n/+ sample)]))))
        
        (_.test "Monads."
                (n/= (inc (n/+ 4 (n/* 3 sample)))
                     (|> sample
                         (do> identity.monad
                              [(n/* 3)]
                              [(n/+ 4)]
                              [inc]))))

        (_.test "Execution."
                (n/= (n/* 10 sample)
                     (|> sample
                         (exec> [%n (format "sample = ") log!])
                         (n/* 10))))

        (_.test "Tuple."
                (let [[left middle right] (|> sample
                                              (tuple> [inc]
                                                      [dec]
                                                      [%n]))]
                  (and (n/= (inc sample) left)
                       (n/= (dec sample) middle)
                       (text/= (%n sample) right))))
        
        (_.test "Pattern-matching."
                (text/= (case (n/% 10 sample)
                          0 "zero"
                          1 "one"
                          2 "two"
                          3 "three"
                          4 "four"
                          5 "five"
                          6 "six"
                          7 "seven"
                          8 "eight"
                          9 "nine"
                          _ "???")
                        (|> sample
                            (n/% 10)
                            (case> 0 "zero"
                                   1 "one"
                                   2 "two"
                                   3 "three"
                                   4 "four"
                                   5 "five"
                                   6 "six"
                                   7 "seven"
                                   8 "eight"
                                   9 "nine"
                                   _ "???"))))
        )))