forked from Ramarren/cl-parser-combinators
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-basic.lisp
130 lines (112 loc) · 4.46 KB
/
test-basic.lisp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(in-package :parser-combinators-tests)
(in-suite parser-combinators-tests)
(defsuite* basics-tests)
(deftest test-parse-string ()
(let ((parse-result (parse-string (between? #\a nil nil 'string) "aaa")))
(iter (for should-result in '("aaa" "aa" "a" ""))
(for is-result = (tree-of (next-result parse-result)))
(is (string= should-result is-result)))))
(deftest test-parse-string*-complete ()
(multiple-value-bind (result suffix success front) (parse-string* (between? #\a nil nil 'string) "aaa")
(is (string= result "aaa"))
(is (null suffix))
(is success)
(is (null front))))
(deftest test-parse-string*-incomplete ()
(multiple-value-bind (result suffix success front) (parse-string* (between? #\a nil 2 'string) "aaa")
(is (string= result "aa"))
(is (= (position-of suffix) 2))
(is success)
(is (= (position-of front) 1))))
(deftest test-parse-string*-incomplete2 ()
(multiple-value-bind (result suffix success front) (parse-string* (breadth? #\a 2 nil 'string) "aaa")
(is (string= result "aa"))
(is (= (position-of suffix) 2))
(is success)
;; next character is already consumed by queuing
(is (= (position-of front) 2))))
(deftest test-parse-string*-complete-arg ()
(multiple-value-bind (result suffix success front)
(parse-string* (breadth? #\a 2 nil 'string) "aaa" :complete t)
(is (string= result "aaa"))
(is (null suffix))
(is success)
;; next character is already consumed by queuing
(is (null front))))
(deftest test-parse-string*-complete-first ()
(multiple-value-bind (result suffix success front)
(parse-string* (breadth? #\a 2 nil 'string) "aaa" :complete :first)
(is (null result))
(is (null suffix))
(is (null success))
(is (= (position-of front) 2))))
(deftest test-parse-string*-fail ()
(multiple-value-bind (result suffix success front)
(parse-string* #\a "b")
(is (null result))
(is (null suffix))
(is (null success))
(is (= (position-of front) 0))))
(deftest test-parse-string*-fail-complete ()
(multiple-value-bind (result suffix success front)
(parse-string* #\a "ab" :complete t)
(is (null result))
(is (null suffix))
(is (null success))
(is (= (position-of front) 0))))
(deftest test-parse-string* ()
(test-parse-string*-complete)
(test-parse-string*-incomplete)
(test-parse-string*-incomplete2)
(test-parse-string*-complete-arg)
(test-parse-string*-complete-first)
(test-parse-string*-fail)
(test-parse-string*-fail-complete))
;; test combinators
(defsuite* combinators-tests)
(deftest test-bind ()
(is (eql (tree-of (funcall (funcall (mdo #\a #\b) (make-context "ab"))))
#\b)))
(deftest test-choice ()
(let ((continuation (funcall (choice (mdo #\a #\b #\a)
(mdo #\a #\b))
(make-context "aba"))))
(is (eql (tree-of (funcall continuation))
#\a))
(is (eql (tree-of (funcall continuation))
#\b))
(is (null (funcall continuation))))
(let ((continuation (funcall (choice (mdo #\c #\a #\b #\a)
(mdo #\c #\a #\b))
(make-context "aba"))))
(is (null (funcall continuation)))))
(deftest test-choice1 ()
(let ((continuation (funcall (choice1 (mdo #\a #\b #\a)
(mdo #\a #\b))
(make-context "aba"))))
(is (eql (tree-of (funcall continuation))
#\a))
(is (null (funcall continuation)))))
(deftest test-choices ()
(let ((continuation (funcall (choices (mdo #\a #\b #\a)
(mdo #\a #\b)
(mdo #\a #\b #\a #\c))
(make-context "abac"))))
(is (eql (tree-of (funcall continuation))
#\a))
(is (eql (tree-of (funcall continuation))
#\b))
(is (eql (tree-of (funcall continuation))
#\c))
(is (null (funcall continuation)))))
(deftest test-choices1 ()
(let ((continuation (funcall (choices1 (mdo #\a #\b #\a)
(mdo #\c #\a #\b)
(mdo #\c #\a #\b #\a #\c))
(make-context "cabac"))))
(let ((result (funcall continuation)))
(is (eql (tree-of result)
#\b))
(is (= (position-of (suffix-of result))
3)))
(is (null (funcall continuation)))))