summaryrefslogtreecommitdiff
path: root/test.lisp
blob: b1e3f3e1a8818a423b60b717cfbc2d2efcbc1f59 (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
(in-package #:monparser)

(defmacro deftest (title &body tests)
  (let ((result '(progn)))
    (push `(format t "~a:~&" ,title) result)
    (dolist (test tests)
      (let ((parser (first test))
            (input (second test)))
        (push `(format t "~a~&" (parse ,parser ,input)) result)))
    (push '(format t "~%") result)
    (reverse result)))

(defun test ()
  (deftest "unit"
           ((unit) "hello")
           ((unit #\h) "hello"))
  (deftest "many"
           ((many (unit #\h)) "hello")
           ((many (unit #\h)) "hhhhhhhello")
           ((many (unit #\h)) "ello"))
  (deftest "optional many"
           ((optional (many (unit #\h))) "hello")
           ((optional (many (unit #\h))) "hhhhhhhello")
           ((optional (many (unit #\h))) "ello"))
  (deftest "many optional"
           ((many (optional (unit #\h))) "hello")
           ((many (optional (unit #\h))) "hhhhhhhello")
           ((many (optional (unit #\h))) "ello"))
  (deftest "until literal"
           ((comp ((prefix (optional (literal "zy")))
                   (match (if prefix
                            (fail "Reached prefix")
                            (unit))))
              match)
            "zy")
           ((comp (((prefix) (optional (literal "zy")))
                   ((match) (if (parsing-tree prefix)
                              (fail "Reached prefix")
                              (progn
                                (format t "prefix place ~a~&"
                                        (result-place prefix))
                                (unit)))))
              (format t "match place: ~a~&"
                      (result-place match))
              match)
            "ezy")
           ((many (comp ((prefix (optional (literal "zy")))
                         (match (if prefix
                                  (fail "Reached prefix")
                                  (unit))))
                    match))
            "the quick brown fox jumps over the lazy dog.")
           ((optional
              (many
                (comp ((prefix (optional (literal "zy")))
                       (match (if prefix
                                (fail "Reached prefix")
                                (unit))))
                  match)))
            "the quick brown fox jumps over the lazy dog.")))