File: tests.lisp

package info (click to toggle)
cl-named-readtables 20180121.git985b162-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 248 kB
  • sloc: lisp: 1,672; makefile: 2
file content (355 lines) | stat: -rw-r--r-- 9,301 bytes parent folder | download | duplicates (2)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
;;; -*- Mode:Lisp -*-

(in-package :named-readtables-test)

(defun map-alist (car-fn cdr-fn alist)
  (mapcar #'(lambda (entry)
              (cons (funcall car-fn (car entry))
                    (funcall cdr-fn (cdr entry))))
          alist))

(defun length=1 (list)
  (and list (null (cdr list))))

(defmacro signals-condition-p (name &body body)
  `(handler-case (prog1 nil ,@body)
     (,(second name) () t)))

(defmacro continue-condition (name &body body)
  `(handler-bind ((,(second name) #'continue))
     ,@body))

(defun read-with-readtable (name string)
  (let ((*package* '#.*package*)
        (*readtable* (find-readtable name)))
    (values (read-from-string string))))

(defun random-named-readtable ()
  (let ((readtables (list-all-named-readtables)))
    (nth (random (length readtables)) readtables)))


(defun readtable-content (named-readtable-designator)
  (let ((readtable (ensure-readtable named-readtable-designator))
        (result '()))
    ;; Make sure to canonicalize the order and function designators so
    ;; we can compare easily.
    (do-readtable ((char reader-fn ntp disp? table) readtable)
      (setq table (sort (copy-list table) #'char< :key #'car))
      (push (list* char
                   (ensure-function reader-fn)
                   ntp
                   (and disp? (list (map-alist #'identity
                                               #'ensure-function
                                               table))))
            result))
    (sort result #'char< :key #'car)))

(defun readtable= (rt1 rt2)
  (tree-equal (readtable-content rt1) (readtable-content rt2)
              :test #'(lambda (x y)
                        (if (and (functionp x) (functionp y))
                            (function= x y)
                            (eql x y)))))


(defun read-A (stream c &optional n)
  (declare (ignore stream c n))
  :a)

(defun read-A-as-X (stream c &optional n)
  (declare (ignore stream c n))
  :x)

(defun read-B (stream c)
  (declare (ignore stream c))
  :b)

(defun read-sharp-paren (stream c n)
  (declare (ignore stream c n))
  'sharp-paren)

(defun read-C (stream c)
  (declare (ignore stream c))
  :c)

(defreadtable A
  (:macro-char #\A #'read-A))

(defreadtable A-as-X
  (:macro-char #\A #'read-A-as-X))

(defreadtable A-dispatch
  (:macro-char #\A :dispatch)
  (:dispatch-macro-char #\A #\A #'read-A))

(defreadtable A-dispatch-as-X
  (:macro-char #\A :dispatch)
  (:dispatch-macro-char #\A #\A #'read-A-as-X))

(defreadtable B
  (:macro-char #\B #'read-B))

(defreadtable C
  (:macro-char #\C #'read-C))

(defreadtable A+B+C
  (:merge A B C))

(defreadtable standard+A+B+C
  (:merge :standard A+B+C))

(defreadtable sharp-paren
  (:macro-char #\# :dispatch)
  (:dispatch-macro-char #\# #\( #'read-sharp-paren))


(deftest cruft.1
    (function= (get-macro-character #\" (copy-readtable nil))
               (get-macro-character #\" (copy-readtable nil)))
  t)

(deftest cruft.2
    (dispatch-macro-char-p #\# (find-readtable :standard))
  t)

(deftest cruft.3
    (dispatch-macro-char-p #\# (make-readtable))
  nil)

(deftest cruft.4
    (let ((rt (copy-named-readtable :standard)))
      (ensure-dispatch-macro-character #\# t rt)
      (dispatch-macro-char-p #\# rt))
  t)

(deftest cruft.5
    (let ((rt (make-readtable)))
      (values
        (dispatch-macro-char-p #\$ rt)
        (ensure-dispatch-macro-character #\$ t rt)
        (dispatch-macro-char-p #\$ rt)))
  nil t t)

(deftest cruft.6
    (let ((rt (make-readtable))
          (fn (constantly nil)))
      (ensure-dispatch-macro-character #\$ t rt)
      (set-dispatch-macro-character #\$ #\# fn rt)
      (values 
        (eq fn (get-dispatch-macro-character #\$ #\# rt))
        (length=1 (readtable-content rt))))
  t t)

(deftest cruft.7
    (let ((rt (make-readtable))
          (fn (constantly nil)))
      (set-macro-character #\$ fn t rt)
      (values
        (eq fn (get-macro-character #\$ rt))
        (length=1 (readtable-content rt))))
  t t)


(deftest standard.1
    (read-with-readtable :standard "ABC")
  ABC)

(deftest standard.2
    (read-with-readtable :standard "(A B C)")
  (A B C))

(deftest standard.3
    (let ((x (find-readtable nil))
          (y (find-readtable :standard))
          (z (find-readtable :common-lisp)))
      (and (eq x y) (eq y z)))
  t)


(deftest modern.1
    (read-with-readtable :modern "FooF")
  |FooF|)


(deftest empty.1
    (null (readtable-content (make-readtable)))
  t)

(deftest empty.2
    (readtable= (merge-readtables-into (make-readtable) :standard)
                (find-readtable :standard))
  t)

(deftest empty.3
    (let ((rt (copy-named-readtable :standard)))
      (readtable= (merge-readtables-into (make-readtable) rt)
                  (merge-readtables-into rt (make-readtable))))
  t)


(deftest basics.1
    (read-with-readtable 'A "A")
  :a)

(deftest basics.2
    (read-with-readtable 'A-as-X "A")
  :x)

(deftest basics.3
    (read-with-readtable 'A "B")
  B)

(deftest basics.4
    (read-with-readtable 'A "(A B C)")
  |(|)


(deftest unregister.1
    (let ((rt (find-readtable 'A)))
      (register-readtable 'does-not-exist rt)
      (values
        (and (find-readtable 'does-not-exist) t)
        (unregister-readtable 'does-not-exist)
        (and (find-readtable 'does-not-exist) t)))
  t t nil)


(deftest name.1
    (let ((rt (random-named-readtable)))
      (eq rt (find-readtable (readtable-name rt))))
  t)

(deftest ensure.1
    (unwind-protect
         (let* ((x (ensure-readtable 'does-not-exist (find-readtable 'A)))
                (y (find-readtable 'A))
                (z (find-readtable 'does-not-exist)))
           (and (eq x y) (eq y z)))
      (unregister-readtable 'does-not-exist))
  t)


(deftest merge.1
    (values
      (read-with-readtable 'A+B+C "A")
      (read-with-readtable 'A+B+C "B")
      (read-with-readtable 'A+B+C "C"))
  :a :b :c)

(deftest merge.2
    (read-with-readtable 'standard+A+B+C "(A B C)")
  (:a :b :c))

(deftest merge.3
    (read-with-readtable 'standard+A+B+C "#(A B C)")
  #(:a :b :c))

(deftest merge.4
    (let ((A+B+C+standard (merge-readtables-into (copy-named-readtable 'A+B+C)
                                                 :standard)))
      (readtable= 'standard+A+B+C A+B+C+standard))
  t)


(deftest rename.1
    (unwind-protect
         (progn (make-readtable 'A* :merge '(A))
                (rename-readtable 'A* 'A**)
                (values (and (find-readtable 'A*) t)
                        (and (find-readtable 'A**) t)))
      (unregister-readtable 'A*)
      (unregister-readtable 'A**))
  nil
  t)


(deftest reader-macro-conflict.1
    (signals-condition-p 'reader-macro-conflict
      (merge-readtables-into (make-readtable) 'A 'A-as-X))
  t)

(deftest reader-macro-conflict.2
    (signals-condition-p 'reader-macro-conflict
      (merge-readtables-into (make-readtable) :standard :standard))
  nil)

(deftest reader-macro-conflict.3
    (signals-condition-p 'reader-macro-conflict
      (merge-readtables-into (make-readtable) 'A+B+C 'A))
  nil)

(deftest reader-macro-conflict.4
    (signals-condition-p 'reader-macro-conflict
      (merge-readtables-into (make-readtable) :standard 'sharp-paren))
  t)

(deftest reader-macro-conflict.5
    (signals-condition-p 'reader-macro-conflict
      (merge-readtables-into (make-readtable) 'A 'A-dispatch))
  t)

(deftest reader-macro-conflict.6
    (signals-condition-p 'reader-macro-conflict
      (merge-readtables-into (make-readtable) 'A-dispatch 'A))
  t)

(deftest reader-macro-conflict.7
    (signals-condition-p 'reader-macro-conflict
      (merge-readtables-into (make-readtable) 'A-dispatch 'A-dispatch-as-X))
  t)

(deftest reader-macro-conflict.8
    (signals-condition-p 'reader-macro-conflict
      (merge-readtables-into (make-readtable) 'A 'A))
  nil)

(deftest reader-macro-conflict.9
    (signals-condition-p 'reader-macro-conflict
      (merge-readtables-into (make-readtable) 'A-dispatch 'A-dispatch))
  nil)


(deftest readtable-does-not-exist.1
    (signals-condition-p 'readtable-does-not-exist
      (ensure-readtable 'does-not-exist))
  t)


(deftest readtable-does-already-exist.1
    (signals-condition-p 'readtable-does-already-exist
      (make-readtable 'A))
  t)

(deftest readtable-does-already-exist.2
    (signals-condition-p 'readtable-does-already-exist
      (make-readtable 'A))
  t)

(deftest readtable-does-already-exist.3
    (let ((rt (make-readtable 'does-not-exist :merge '(:standard A B))))
      (declare (ignore rt))
      (unwind-protect
           (read-with-readtable (continue-condition 'readtable-does-already-exist
                                  (make-readtable 'does-not-exist
                                                  :merge '(:standard A C)))
                       
                                "(A B C)")
        (unregister-readtable 'does-not-exist)))
    (:a B :c))


(deftest defreadtable.1
    (unwind-protect
         (signals-condition-p 'reader-macro-conflict
           (eval `(defreadtable does-not-exist (:merge A A-as-X))))
      (unregister-readtable 'does-not-exist))
  t)

(deftest defreadtable.2
    (unwind-protect
         (signals-condition-p 't
           (eval `(defreadtable does-not-exist (:fuse A A-as-X))))
      (unregister-readtable 'does-not-exist))
  nil)