aboutsummaryrefslogtreecommitdiff
path: root/tests/test.scm
blob: 562c736ad39586f24e16d5d73fb8b876d3556ebd (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
#!/usr/bin/guile \
-s
!#

(add-to-load-path (dirname (dirname (current-filename))))

(use-modules (srfi srfi-64))

(use-modules (monad)
             (monad optional))

(define (div d)
  (if (zero? d)
      (nothing)
      (just (/ 1 d))))

(test-begin "mapM-test")

(test-assert (equal? (mapM div '(1 2 3))
                     (just '(1 1/2 1/3))))

(test-assert (equal? (mapM div (map just '(1 2 3)))
                     (just '(1 1/2 1/3))))

;;; --------------------------------------------------

(test-assert (equal? (mapM div '(0 1 2 3))
                     (nothing)))

(test-assert (equal? (mapM div (map just '(0 1 2 3)))
                     (nothing)))

(test-end "mapM-test")

(test-begin "state-test")

(use-modules (monad state)
             (monad stack))

(test-equal (run-state (return-state 10) 20)
  '(10 20))

(test-equal
    (car
     (run-state
      (do let y = 7
          (modify (lambda (x) (+ x y)))
          (get))
      10))
  17)


(test-end "state-test")

(test-begin "writer-test")

(use-modules (monad writer))

(test-equal '(10 "HelloWorld")
  (run-writer
   (do (w "Hello")
       (w "World")
     (return-writer 10))))

(test-equal '(10 "HelloWorld")
  (run-writer
   (do "Hello" "World"
     (return-writer 10))))

(test-equal '(3 "Applied + to (1 2) ⇒ 3\n")
  (run-writer (with-writer + 1 2)))

(test-equal '(10 "Hello, World")
  (run-writer
   (do (string-append "Hello" ", ")
       "World"
     (return-writer 10))))

(test-end "writer-test")