forked from nikodemus/esrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.lisp
206 lines (174 loc) · 7.22 KB
/
tests.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
;;;; Copyright (c) 2007-2012 Nikodemus Siivola <nikodemus@sb-studio.net>
;;;;
;;;; Permission is hereby granted, free of charge, to any person
;;;; obtaining a copy of this software and associated documentation files
;;;; (the "Software"), to deal in the Software without restriction,
;;;; including without limitation the rights to use, copy, modify, merge,
;;;; publish, distribute, sublicense, and/or sell copies of the Software,
;;;; and to permit persons to whom the Software is furnished to do so,
;;;; subject to the following conditions:
;;;;
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;;;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
;;;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
;;;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;;;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(in-package :cl-user)
(defpackage :esrap-tests
(:use :alexandria :cl :esrap :eos)
(:shadowing-import-from :esrap "!")
(:export #:run-tests))
(in-package :esrap-tests)
;;;; A few semantic predicates
(defun not-doublequote (char)
(not (eql #\" char)))
(defun not-digit (char)
(when (find-if-not #'digit-char-p char)
t))
(defun not-newline (char)
(not (eql #\newline char)))
(defun not-space (char)
(not (eql #\space char)))
;;;; Utility rules
(defrule whitespace (+ (or #\space #\tab #\newline))
(:text t))
(defrule empty-line #\newline
(:constant ""))
(defrule non-empty-line (and (+ (not-newline character)) (? #\newline))
(:destructure (text newline)
(declare (ignore newline))
(text text)))
(defrule line (or empty-line non-empty-line)
(:identity t))
(defrule trimmed-line line
(:lambda (line)
(string-trim '(#\space #\tab) line)))
(defrule trimmed-lines (* trimmed-line)
(:identity t))
(defrule digits (+ (digit-char-p character))
(:text t))
(defrule integer (and (? whitespace)
digits
(and (? whitespace) (or (& #\,) (! character))))
(:destructure (whitespace digits tail)
(declare (ignore whitespace tail))
(parse-integer digits)))
(defrule list-of-integers (+ (or (and integer #\, list-of-integers)
integer))
(:destructure (match)
(if (integerp match)
(list match)
(destructuring-bind (int comma list) match
(declare (ignore comma))
(cons int list)))))
(defun smoke-test ()
(is (equal '("1," "2," "" "3," "4.")
(parse 'trimmed-lines "1,
2,
3,
4.")))
(is (eql 123 (parse 'integer " 123")))
(is (eql 123 (parse 'integer " 123 ")))
(is (eql 123 (parse 'integer "123 ")))
(is (equal '(123 45 6789 0) (parse 'list-of-integers "123, 45 , 6789, 0")))
(is (equal '(123 45 6789 0) (parse 'list-of-integers " 123 ,45,6789, 0 "))))
(defrule single-token/bounds.1 (+ (not-space character))
(:lambda (result &bounds start end)
(format nil "~A[~S-~S]" (text result) start end)))
(defrule single-token/bounds.2 (and (not-space character) (* (not-space character)))
(:destructure (first &rest rest &bounds start end)
(format nil "~C~A(~S-~S)" first (text rest) start end)))
(defrule tokens/bounds.1 (and (? whitespace)
(or (and single-token/bounds.1 whitespace tokens/bounds.1)
single-token/bounds.1))
(:destructure (whitespace match)
(declare (ignore whitespace))
(if (stringp match)
(list match)
(destructuring-bind (token whitespace list) match
(declare (ignore whitespace))
(cons token list)))))
(defrule tokens/bounds.2 (and (? whitespace)
(or (and single-token/bounds.2 whitespace tokens/bounds.2)
single-token/bounds.2))
(:destructure (whitespace match)
(declare (ignore whitespace))
(if (stringp match)
(list match)
(destructuring-bind (token whitespace list) match
(declare (ignore whitespace))
(cons token list)))))
(defrule left-recursion (and left-recursion "l"))
(defun bounds-test.1 ()
(is (equal '("foo[0-3]")
(parse 'tokens/bounds.1 "foo")))
(is (equal '("foo[0-3]" "bar[4-7]" "quux[11-15]")
(parse 'tokens/bounds.1 "foo bar quux"))))
(defun bounds-test.2 ()
(is (equal '("foo(0-3)")
(parse 'tokens/bounds.2 "foo")))
(is (equal '("foo(0-3)" "bar(4-7)" "quux(11-15)")
(parse 'tokens/bounds.2 "foo bar quux"))))
(defun condition-test.1 ()
(macrolet
((signals-esrap-error ((input position &optional messages) &body body)
`(progn
(signals (esrap-error)
,@body)
(handler-case (progn ,@body)
(esrap-error (condition)
(is (string= (esrap-error-text condition) ,input))
(is (= (esrap-error-position condition) ,position))
,@(when messages
`((let ((report (princ-to-string condition)))
,@(mapcar (lambda (message)
`(is (search ,message report)))
(ensure-list messages))))))))))
(signals-esrap-error ("" 0 ("Could not parse subexpression"
"Encountered at"))
(parse 'integer ""))
(signals-esrap-error ("123foo" 3 ("Could not parse subexpression"
"Encountered at"))
(parse 'integer "123foo"))
(signals-esrap-error ("1, " 1 ("Incomplete parse."
"Encountered at"))
(parse 'list-of-integers "1, "))))
(defun condition-test.2 ()
(signals (left-recursion)
(parse 'left-recursion "l"))
(handler-case (parse 'left-recursion "l")
(left-recursion (condition)
(is (string= (esrap-error-text condition) "l"))
(is (= (esrap-error-position condition) 0))
(is (eq (left-recursion-nonterminal condition)
'left-recursion))
(is (equal (left-recursion-path condition)
'(left-recursion left-recursion))))))
(defun negation-test ()
(let* ((text "FooBazBar")
(t1c (text (parse '(+ (not "Baz")) text :junk-allowed t)))
(t1e (text (parse (identity '(+ (not "Baz"))) text :junk-allowed t)))
(t2c (text (parse '(+ (not "Bar")) text :junk-allowed t)))
(t2e (text (parse (identity '(+ (not "Bar"))) text :junk-allowed t)))
(t3c (text (parse '(+ (not (or "Bar" "Baz"))) text :junk-allowed t)))
(t3e (text (parse (identity '(+ (not (or "Bar" "Baz")))) text :junk-allowed t))))
(is (equal "Foo" t1c))
(is (equal "Foo" t1e))
(is (equal "FooBaz" t2c))
(is (equal "FooBaz" t2e))
(is (equal "Foo" t3c))
(is (equal "Foo" t3e))))
(test esrap
(smoke-test)
(bounds-test.1)
(bounds-test.2)
(condition-test.1)
(condition-test.2)
(negation-test))
(defun run-tests ()
(let ((results (run 'esrap)))
(eos:explain! results)
(unless (eos:results-status results)
(error "Tests failed."))))