1 ;;;; tests for problems in the interface presented to the user/programmer
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (load "assertoid.lisp")
15 (load "test-util.lisp")
16 (use-package "ASSERTOID")
17 (use-package "TEST-UTIL")
19 (defmacro silently
(&rest things
)
20 `(let ((*standard-output
* (make-broadcast-stream))) ,@things
))
22 ;; Interpreted closure is a problem for COMPILE
23 (with-test (:name
:disassemble
:skipped-on
:interpreter
)
24 ;;; DISASSEMBLE shouldn't fail on closures or unpurified functions
25 (defun disassemble-fun (x) x
)
26 (silently (disassemble 'disassemble-fun
)))
28 (with-test (:name
:disassemble-closure
:skipped-on
:interpreter
)
29 (let ((x 1)) (defun disassemble-closure (y) (if y
(setq x y
) x
)))
30 (silently (disassemble 'disassemble-closure
)))
33 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
34 (import 'sb-eval
:interpreted-function-p
))
36 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
37 (import 'sb-interpreter
:interpreted-function-p
))
39 #+(or sb-eval sb-fasteval
)
40 (with-test (:name
:disassemble-interpreted
)
41 ;; Nor should it fail on interpreted functions
42 (let ((sb-ext:*evaluator-mode
* :interpret
))
43 (eval `(defun disassemble-eval (x) x
))
44 (silently (disassemble 'disassemble-eval
)))
46 ;; disassemble-eval should still be an interpreted function.
47 ;; clhs disassemble: "(If that function is an interpreted function,
48 ;; it is first compiled but the result of this implicit compilation
49 ;; is not installed.)"
50 (assert (interpreted-function-p (symbol-function 'disassemble-eval
))))
52 (with-test (:name
:disassemble-generic
)
53 ;; nor should it fail on generic functions or other funcallable instances
54 (defgeneric disassemble-generic
(x))
55 (silently (disassemble 'disassemble-generic
))
56 (let ((fin (make-instance 'sb-mop
:funcallable-standard-object
)))
57 (silently (disassemble fin
))))
59 ;;; while we're at it, much the same applies to
60 ;;; FUNCTION-LAMBDA-EXPRESSION:
63 (let ((x 1)) (defun fle-closure (y) (if y
(setq x y
) x
)))
65 (with-test (:name
:function-lambda-expression
)
67 (nth-value 2 (function-lambda-expression x
))))
68 (assert (eql (fle-name #'fle-fun
) 'fle-fun
))
69 (assert (eql (fle-name #'fle-closure
) 'fle-closure
))
70 (assert (eql (fle-name #'disassemble-generic
) 'disassemble-generic
))
71 (function-lambda-expression
72 (make-instance 'sb-mop
:funcallable-standard-object
))
73 (function-lambda-expression
74 (make-instance 'generic-function
))
75 (function-lambda-expression
76 (make-instance 'standard-generic-function
))
77 #+(or sb-eval sb-fasteval
)
79 (let ((sb-ext:*evaluator-mode
* :interpret
))
80 (eval `(defun fle-eval (x) x
))
81 (assert (eql (fle-name (symbol-function 'fle-eval
)) 'fle-eval
)))
83 ;; fle-eval should still be an interpreted function.
84 (assert (interpreted-function-p (symbol-function 'fle-eval
))))))
87 ;;; support for DESCRIBE tests
88 (defstruct to-be-described a b
)
89 (defclass forward-describe-class
(forward-describe-ref) (a))
90 (let ((sb-ext:*evaluator-mode
* :compile
))
91 (eval `(let (x) (defun closure-to-describe () (incf x
)))))
93 (with-test (:name
(describe :empty-gf
))
95 (silently (describe (make-instance 'generic-function
)))
98 (silently (describe (make-instance 'standard-generic-function
)))
101 ;;; DESCRIBE should run without signalling an error.
102 (with-test (:name
(describe :no-error
))
104 (describe (make-to-be-described))
106 (describe "a string")
107 (describe 'symbolism
)
108 (describe (find-package :cl
))
110 (describe #(a vector
))
112 (describe 'closure-to-describe
)))
114 ;;; The DESCRIBE-OBJECT methods for built-in CL stuff should do
115 ;;; FRESH-LINE and TERPRI neatly.
116 (with-test (:name
(describe fresh-line terpri
))
117 (dolist (i (list (make-to-be-described :a
14) 12 "a string"
118 #0a0
#(1 2 3) #2a
((1 2) (3 4)) 'sym
:keyword
119 (find-package :keyword
) (list 1 2 3)
120 nil
(cons 1 2) (make-hash-table)
121 (let ((h (make-hash-table)))
122 (setf (gethash 10 h
) 100
125 (make-condition 'simple-error
)
126 (make-condition 'simple-error
:format-control
"fc")
127 #'car
#'make-to-be-described
(lambda (x) (+ x
11))
128 (constantly 'foo
) #'(setf to-be-described-a
)
129 #'describe-object
(find-class 'to-be-described
)
130 (find-class 'forward-describe-class
)
131 (find-class 'forward-describe-ref
) (find-class 'cons
)))
132 (let ((s (with-output-to-string (s)
135 (macrolet ((check (form)
137 (error "misbehavior in DESCRIBE of ~S:~% ~S" i
',form
))))
138 (check (char= #\x
(char s
0)))
139 ;; one leading #\NEWLINE from FRESH-LINE or the like, no more
140 (check (char= #\newline
(char s
1)))
141 (check (char/= #\newline
(char s
2)))
142 ;; one trailing #\NEWLINE from TERPRI or the like, no more
143 (let ((n (length s
)))
144 (check (char= #\newline
(char s
(- n
1))))
145 (check (char/= #\newline
(char s
(- n
2)))))))))
147 (with-test (:name
(describe :argument-precedence-order
))
148 ;; Argument precedence order information is only interesting for two
149 ;; or more required parameters.
150 (assert (not (search "Argument precedence order"
151 (with-output-to-string (stream)
152 (describe #'class-name stream
)))))
153 (assert (search "Argument precedence order"
154 (with-output-to-string (stream)
155 (describe #'add-method stream
)))))
157 (with-test (:name
(describe sb-kernel
:funcallable-instance
))
158 (assert (search "Slots with :INSTANCE allocation"
159 (with-output-to-string (stream)
160 (describe #'class-name stream
)))))
163 ;;; Tests of documentation on types and classes
165 (defun assert-documentation (thing doc-type expected
)
166 ;; This helper function makes ASSERT errors print THING, DOC-TYPE,
167 ;; the return value of DOCUMENTATION and EXPECTED.
168 (flet ((assert-documentation-helper (thing doc-type documentation expected
)
169 (declare (ignore thing doc-type
))
170 (equal documentation expected
)))
171 (assert (assert-documentation-helper
172 thing doc-type
(documentation thing doc-type
) expected
))))
174 (defpackage #:documentation.package
175 (:documentation
"PACKAGE"))
177 (with-test (:name
(documentation package
))
178 (assert-documentation (find-package '#:documentation.package
) t
"PACKAGE")
179 (setf (documentation (find-package '#:documentation.package
) t
) "PACKAGE2")
180 (assert-documentation (find-package '#:documentation.package
) t
"PACKAGE2"))
184 (:documentation
"FOO"))
186 (defclass documentation.funcallable-instance
()
188 (:metaclass sb-mop
:funcallable-standard-class
)
189 (:documentation
"FEZ"))
191 (defstruct bar
"BAR")
193 (define-condition baz
()
195 (:documentation
"BAZ"))
198 ((do-class (name expected
&optional structurep
)
200 (assert-documentation ',name
'type
,expected
)
201 (assert-documentation (find-class ',name
) 'type
,expected
)
202 (assert-documentation (find-class ',name
) 't
,expected
)
204 `((assert-documentation ',name
'structure
,expected
)))
206 (let ((new1 (symbol-name (gensym "NEW1")))
207 (new2 (symbol-name (gensym "NEW2")))
208 (new3 (symbol-name (gensym "NEW3")))
209 (new4 (symbol-name (gensym "NEW4"))))
210 (declare (ignorable new4
))
211 (setf (documentation ',name
'type
) new1
)
212 (assert-documentation (find-class ',name
) 'type new1
)
213 (setf (documentation (find-class ',name
) 'type
) new2
)
214 (assert-documentation (find-class ',name
) 't new2
)
215 (setf (documentation (find-class ',name
) 't
) new3
)
216 (assert-documentation ',name
'type new3
)
218 `((assert-documentation ',name
'structure new3
)
219 (setf (documentation ',name
'structure
) new4
)
220 (assert-documentation ',name
'structure new4
)))))))
222 (with-test (:name
(documentation class standard-class
))
223 (do-class foo
"FOO"))
225 (with-test (:name
(documentation class sb-mop
:funcallable-standard-class
))
226 (do-class documentation.funcallable-instance
"FEZ"))
228 (with-test (:name
(documentation struct
1))
229 (do-class bar
"BAR" t
))
231 (with-test (:name
(documentation condition
))
232 (do-class baz
"BAZ")))
234 (defclass documentation-metaclass
(standard-class)
236 (:documentation
"metaclass with methods on DOCUMENTATION."))
238 (defmethod documentation ((thing documentation-metaclass
)
240 (sb-int:awhen
(call-next-method)
241 (concatenate 'string
":" sb-int
:it
)))
243 (defmethod (setf documentation
) (new-value
244 (thing documentation-metaclass
)
246 (call-next-method (when new-value
247 (substitute #\
! #\. new-value
))
250 (defmethod sb-mop:validate-superclass
((class documentation-metaclass
)
251 (superclass standard-class
))
254 (defclass documentation-class
()
256 (:metaclass documentation-metaclass
)
257 (:documentation
"normal"))
259 (with-test (:name
(documentation :non-stanadard
:metaclass
))
260 (flet ((check (expected class-name
)
261 (let ((class (find-class class-name
)))
262 (assert-documentation class-name
'type expected
)
263 (assert-documentation class
'type expected
)
264 (assert-documentation class t expected
))))
265 ;; Make sure methods specialized on the metaclass are not bypassed
266 ;; when retrieving and modifying class documentation.
267 (check ":normal" 'documentation-class
)
268 (setf (documentation 'documentation-class
'type
) "2.")
269 (check ":2!" 'documentation-class
)
270 (setf (documentation 'documentation-class
'type
) nil
)
271 (check nil
'documentation-class
)
273 ;; Sanity check: make sure the metaclass has its own documentation
274 ;; and is not affected by the above modifications.
275 (check "metaclass with methods on DOCUMENTATION."
276 'documentation-metaclass
)))
278 (defstruct (frob (:type vector
)) "FROB")
280 (with-test (:name
(documentation struct
2))
281 (assert-documentation 'frob
'structure
"FROB")
282 (setf (documentation 'frob
'structure
) "NEW5")
283 (assert-documentation 'frob
'structure
"NEW5"))
289 (with-test (:name
(documentation type
))
290 (assert-documentation 'quux
'type
"QUUX")
291 (setf (documentation 'quux
'type
) "NEW4")
292 (assert-documentation 'quux
'type
"NEW4"))
294 (define-compiler-macro cmacro
(x)
298 (define-compiler-macro (setf cmacro
) (y x
)
299 "setf compiler macro"
303 (with-test (:name
(documentation compiler-macro
))
304 (assert-documentation 'cmacro
'compiler-macro
"compiler macro")
305 (assert-documentation '(setf cmacro
) 'compiler-macro
"setf compiler macro"))
307 (defun (setf documentation.setf
) (x)
308 "(setf foo) documentation"
311 (with-test (:name
(documentation function setf
))
312 (flet ((expect (documentation)
313 (assert-documentation
314 '(setf documentation.setf
) 'function documentation
)
315 (assert-documentation
316 #'(setf documentation.setf
) 'function documentation
)
317 (assert-documentation
318 #'(setf documentation.setf
) t documentation
)))
319 (expect "(setf foo) documentation")
320 ;; The original test checked this twice. No idea why.
321 (expect "(setf foo) documentation")
324 (setf (documentation '(setf documentation.setf
) 'function
)
325 "(setf bar) documentation")
326 (expect "(setf bar) documentation")
328 (setf (documentation #'(setf documentation.setf
) 'function
)
329 "(setf baz) documentation")
330 (expect "(setf baz) documentation")
332 (setf (documentation #'(setf documentation.setf
) t
)
333 "(setf fez) documentation")
334 (expect "(setf fez) documentation")))
336 (with-test (:name
(documentation lambda
))
337 (let ((f (lambda () "aos the zos" t
))
338 (g (sb-int:named-lambda fii
() "zoot the fruit" t
)))
339 (dolist (doc-type '(t function
))
340 (assert-documentation f doc-type
"aos the zos")
341 (assert-documentation g doc-type
"zoot the fruit"))
342 (setf (documentation f t
) "fire")
343 (assert-documentation f t
"fire")
344 (assert-documentation g t
"zoot the fruit")))
346 (with-test (:name
(documentation flet
))
348 (string= (documentation
354 "this is FLET quux")))
356 (with-test (:name
(documentation labels
))
358 (string= (documentation
366 "this is LABELS rec")))
373 (with-test (:name
(documentation :closure
))
374 (assert-documentation 'docfoo
'function
"bar")
375 (assert (string= (setf (documentation 'docfoo
'function
) "baz") "baz"))
376 (assert-documentation 'docfoo
'function
"baz")
377 (assert-documentation #'docfoo t
"baz")
378 (assert (string= (setf (documentation #'docfoo t
) "zot") "zot"))
379 (assert-documentation #'docfoo t
"zot")
380 (assert-documentation 'docfoo
'function
"zot")
381 (assert (not (setf (documentation 'docfoo
'function
) nil
)))
382 (assert-documentation 'docfoo
'function nil
))
384 (with-test (:name
(documentation :built-in-macro
) :skipped-on
'(not :sb-doc
))
385 (assert (documentation 'trace
'function
)))
387 (with-test (:name
(documentation :built-in-function
) :skipped-on
'(not :sb-doc
))
388 (assert (documentation 'cons
'function
)))
390 (defvar documentation.variable nil
391 "foo variable documentation")
393 (with-test (:name
(documentation variable
))
394 (assert-documentation 'documentation.variable
'variable
395 "foo variable documentation")
396 (setf (documentation 'documentation.variable
'variable
)
397 "baz variable documentation")
398 (assert-documentation 'documentation.variable
'variable
399 "baz variable documentation"))
401 (with-test (:name
(documentation :mismatch-for-function
))
405 (setf (symbol-function 'test2
) #'test
)
406 (setf (documentation 'test
'function
) "Y")
407 (assert (equal (documentation #'test t
)
408 (documentation 'test
'function
)))
409 (setf (documentation 'test2
'function
) "Z")
411 (equal (documentation 'test
'function
)
412 (documentation 'test2
'function
)))))
414 (with-test (:name
(documentation setf
:on nil
))
417 (assert (equal (setf (documentation nil
'function
) "foo") "foo"))
423 (with-test (:name
(describe generic-function
:assumed-type
))
424 ;; Signalled an error at one point
425 (let ((fun (checked-compile '(lambda ()
426 (flet ((zoo () (gogo)))
427 (defmethod gogo () nil
)
429 :allow-style-warnings t
)))
430 (handler-bind ((warning #'muffle-warning
)) ; implicit gf
431 (silently (funcall fun
)))))
433 (defmacro bug-643958-test
()
437 (with-test (:name
:bug-643958
)
438 (assert (equal "foo" (documentation 'bug-643958-test
'function
)))
439 (setf (documentation 'bug-643958-test
'function
) "bar")
440 (assert (equal "bar" (documentation 'bug-643958-test
'function
))))
442 (defclass cannot-print-this
()
444 (defmethod print-object ((oops cannot-print-this
) stream
)
446 (with-test (:name
:describe-suppresses-print-errors
)
447 (handler-bind ((error #'continue
))
448 (with-output-to-string (s)
449 (describe (make-instance 'cannot-print-this
) s
))))
450 (with-test (:name
:backtrace-suppresses-print-errors
)
451 (handler-bind ((error #'continue
))
452 (with-output-to-string (s)
457 (sb-debug:print-backtrace
:count
100 :stream s
))))
458 (foo 100 (make-instance 'cannot-print-this
))))))
459 (with-test (:name
:backtrace-and-circles
)
460 (handler-bind ((error #'continue
))
461 (with-output-to-string (s)
466 (sb-debug:print-backtrace
:count
100 :stream s
))))
467 (foo 100 (let ((list (list t
)))
468 (nconc list list
)))))))
470 (with-test (:name
:endianness-in-features
)
472 (or (member :big-endian
*features
*)
473 (member :little-endian
*features
*))))
475 (with-test (:name
(trace generic-function
))
476 (defgeneric traced-gf
(x))
477 (defmethod traced-gf (x) (1+ x
))
478 (assert (= (traced-gf 3) 4))
480 (let ((output (with-output-to-string (*trace-output
*)
481 (assert (= (traced-gf 3) 4)))))
482 (assert (> (length output
) 0)))
483 (assert (typep #'traced-gf
'standard-generic-function
))
485 (let ((output (with-output-to-string (*trace-output
*)
486 (assert (= (traced-gf 3) 4)))))
487 (assert (= (length output
) 0))))
489 (with-test (:name
(apropos :inherited
:bug-1364413
))
490 (let* ((package (make-package "BUGGALO" :use nil
))
491 (symbol (intern "BUGGALO" package
)))
492 (export (list symbol
) package
)
493 (let ((inherits (make-package "BUGGALO-INHERITS" :use
(list package
))))
494 (assert (= (length (apropos-list "BUGGALO" package
)) 1))
495 (assert (= (length (apropos-list "BUGGALO" inherits
)) 1))
496 (delete-package inherits
))
497 (delete-package package
)))
499 (with-test (:name
(apropos :inherited
:external-only
:bug-1364413
))
500 (let* ((package (make-package "BUGGALO" :use nil
))
501 (symbol (intern "BUGGALO" package
)))
502 (export (list symbol
) package
)
503 (let ((inherits (make-package "BUGGALO-INHERITS" :use
(list package
))))
504 (assert (= (length (apropos-list "BUGGALO" package t
)) 1))
505 (assert (= (length (apropos-list "BUGGALO" inherits t
)) 0))
506 (delete-package inherits
))
507 (delete-package package
)))
509 (with-test (:name
(apropos :once-only
))
510 (assert (= (length (apropos-list "UPDATE-INSTANCE-FOR-REDEFINED-CLASS")) 1)))
512 (defgeneric gf-arglist-1
(x &key y
))
513 (defmethod gf-arglist-1 (x &key
(y nil
) (z nil z-p
))
516 (defgeneric gf-arglist-2
(x &key y
))
517 (defmethod gf-arglist-2 ((x integer
) &key
(y nil
) ((z f
) nil z-p
)) (list x y f z-p
))
518 (defmethod gf-arglist-2 ((x string
) &key
(y nil
) ((z w
) nil z-p
)) (list x y w z-p
))
520 (defgeneric gf-arglist-3
(x &key
((:y y
))))
522 (defgeneric gf-arglist-4
(x &key
((:y z
))))
524 (defgeneric gf-arglist-5
(x &key y
))
525 (defmethod gf-arglist-5 ((x integer
) &key z
&allow-other-keys
) (list x z
))
527 (with-test (:name
(:generic-function-pretty-arglist
1))
528 (assert (equal (sb-pcl::generic-function-pretty-arglist
#'gf-arglist-1
)
530 (with-test (:name
(:generic-function-pretty-arglist
2))
531 (assert (or (equal (sb-pcl::generic-function-pretty-arglist
#'gf-arglist-2
)
533 (equal (sb-pcl::generic-function-pretty-arglist
#'gf-arglist-2
)
534 '(x &key y
((z f
)))))))
535 (with-test (:name
(:generic-function-pretty-arglist
3))
536 (assert (equal (sb-pcl::generic-function-pretty-arglist
#'gf-arglist-3
)
538 (with-test (:name
(:generic-function-pretty-arglist
4))
539 (assert (equal (sb-pcl::generic-function-pretty-arglist
#'gf-arglist-4
)
540 '(x &key
((:y z
))))))
541 (with-test (:name
(:generic-function-pretty-arglist
5))
542 (assert (equal (sb-pcl::generic-function-pretty-arglist
#'gf-arglist-5
)
543 '(x &key y z
&allow-other-keys
))))