summaryrefslogtreecommitdiff
path: root/test.scm
blob: 02f5f0a350a99470f38d74d42a5b39276078b71b (about) (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
(set! %load-path (cons "." %load-path))
(use-modules (monad maybe)
             (monad either)
             (monad state))

(define (test name expr expectation)
  (let ((res (eval expr (interaction-environment))))
    (if (equal? res expectation)
      (format #t "[~s] passed\n" name)
      (format #t "[~s] ERROR: expected: ~s, but got: ~s\n" name expectation res))))

(define (f x)
  (if (> x 0)
      (Just x)
      (Nothing)))

(begin
  (test "maybe 1"
        '(with-monad maybe-monad
                     (a <- (Just 5))
                     (b <- (f 2))
                     (c <- (Just 10))
                     (return (+ a b c)))
        (Just 17))

  (test "maybe 2"
        '(with-monad maybe-monad
                     (a <- (Just 5))
                     (b <- (f 0))
                     (c <- (Just 10))
                     (return (+ a b c)))
        (Nothing))

  (test "state 1"
        '(run-state 0 (with-monad state-monad
                                  (put 10)
                                  (put 9)
                                  (modify (lambda (x) (+ 2 x)))
                                  (modify (lambda (x) (+ 2 x)))
                                  (get)))
        13)

  (test "state 2"
        '(run-state 10 (with-monad state-monad
                                   (get)))
        10)

  (test "either 1"
        '(with-monad either-monad
                     (a <- (Right 5))
                     (b <- (Right 0))
                     (c <- (Right 10))
                     (return (+ a b c)))
        (Right 15))

  (test "either 2"
        '(with-monad either-monad
                     (a <- (Right 5))
                     (b <- (Left "an error"))
                     (c <- (Right 10))
                     (return (+ a b c)))
        (Left "an error")))