Produce only one warning for (typep x 'bad-type)
[sbcl.git] / tests / interface.impure.lisp
bloba155d4c53365f96485a7f24e62d71216ff5a86ac
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
4 ;;;; more information.
5 ;;;;
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
8 ;;;; from CMU CL.
9 ;;;;
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 function) :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)))
32 #+sb-eval
33 (eval-when (:compile-toplevel :load-toplevel :execute)
34 (import 'sb-eval:interpreted-function-p))
35 #+sb-fasteval
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-function))
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:
61 (defun fle-fun (x) x)
63 (let ((x 1)) (defun fle-closure (y) (if y (setq x y) x)))
65 (defclass non-standard-generic-function (generic-function) ()
66 (:metaclass sb-mop:funcallable-standard-class))
67 (defmethod sb-mop:generic-function-name ((generic-function non-standard-generic-function))
68 'name)
70 (with-test (:name function-lambda-expression)
71 (flet ((fle-name (x)
72 (nth-value 2 (function-lambda-expression x))))
73 (assert (eql (fle-name #'fle-fun) 'fle-fun))
74 (assert (eql (fle-name #'fle-closure) 'fle-closure))
75 (assert (eql (fle-name #'disassemble-generic) 'disassemble-generic))
76 (function-lambda-expression
77 (make-instance 'sb-mop:funcallable-standard-object))
78 (function-lambda-expression
79 (make-instance 'non-standard-generic-function))
80 (function-lambda-expression
81 (make-instance 'standard-generic-function))
82 #+(or sb-eval sb-fasteval)
83 (progn
84 (let ((sb-ext:*evaluator-mode* :interpret))
85 (eval `(defun fle-eval (x) x))
86 (assert (eql (fle-name (symbol-function 'fle-eval)) 'fle-eval)))
88 ;; fle-eval should still be an interpreted function.
89 (assert (interpreted-function-p (symbol-function 'fle-eval))))))
91 ;;; Tests of documentation on types and classes
93 (defun assert-documentation (thing doc-type expected)
94 ;; This helper function makes ASSERT errors print THING, DOC-TYPE,
95 ;; the return value of DOCUMENTATION and EXPECTED.
96 (flet ((assert-documentation-helper (thing doc-type documentation expected)
97 (declare (ignore thing doc-type))
98 (equal documentation expected)))
99 (assert (assert-documentation-helper
100 thing doc-type (documentation thing doc-type) expected))))
102 (defpackage #:documentation.package
103 (:documentation "PACKAGE"))
105 (with-test (:name (documentation package))
106 (assert-documentation (find-package '#:documentation.package) t "PACKAGE")
107 (setf (documentation (find-package '#:documentation.package) t) "PACKAGE2")
108 (assert-documentation (find-package '#:documentation.package) t "PACKAGE2"))
110 (defclass foo ()
112 (:documentation "FOO"))
114 (defclass documentation.funcallable-instance ()
116 (:metaclass sb-mop:funcallable-standard-class)
117 (:documentation "FEZ"))
119 (defstruct bar "BAR")
121 (define-condition baz ()
123 (:documentation "BAZ"))
125 (macrolet
126 ((do-class (name expected &optional structurep)
127 `(progn
128 (assert-documentation ',name 'type ,expected)
129 (assert-documentation (find-class ',name) 'type ,expected)
130 (assert-documentation (find-class ',name) 't ,expected)
131 ,@(when structurep
132 `((assert-documentation ',name 'structure ,expected)))
134 (let ((new1 (symbol-name (gensym "NEW1")))
135 (new2 (symbol-name (gensym "NEW2")))
136 (new3 (symbol-name (gensym "NEW3")))
137 (new4 (symbol-name (gensym "NEW4"))))
138 (declare (ignorable new4))
139 (setf (documentation ',name 'type) new1)
140 (assert-documentation (find-class ',name) 'type new1)
141 (setf (documentation (find-class ',name) 'type) new2)
142 (assert-documentation (find-class ',name) 't new2)
143 (setf (documentation (find-class ',name) 't) new3)
144 (assert-documentation ',name 'type new3)
145 ,@(when structurep
146 `((assert-documentation ',name 'structure new3)
147 (setf (documentation ',name 'structure) new4)
148 (assert-documentation ',name 'structure new4)))))))
150 (with-test (:name (documentation class standard-class))
151 (do-class foo "FOO"))
153 (with-test (:name (documentation class sb-mop:funcallable-standard-class))
154 (do-class documentation.funcallable-instance "FEZ"))
156 (with-test (:name (documentation struct 1))
157 (do-class bar "BAR" t))
159 (with-test (:name (documentation condition))
160 (do-class baz "BAZ")))
162 (defclass documentation-metaclass (standard-class)
164 (:documentation "metaclass with methods on DOCUMENTATION."))
166 (defmethod documentation ((thing documentation-metaclass)
167 (doc-type (eql 't)))
168 (sb-int:awhen (call-next-method)
169 (concatenate 'string ":" sb-int:it)))
171 (defmethod (setf documentation) (new-value
172 (thing documentation-metaclass)
173 (doc-type (eql 't)))
174 (call-next-method (when new-value
175 (substitute #\! #\. new-value))
176 thing doc-type))
178 (defmethod sb-mop:validate-superclass ((class documentation-metaclass)
179 (superclass standard-class))
182 (defclass documentation-class ()
184 (:metaclass documentation-metaclass)
185 (:documentation "normal"))
187 (with-test (:name (documentation :non-stanadard :metaclass))
188 (flet ((check (expected class-name)
189 (let ((class (find-class class-name)))
190 (assert-documentation class-name 'type expected)
191 (assert-documentation class 'type expected)
192 (assert-documentation class t expected))))
193 ;; Make sure methods specialized on the metaclass are not bypassed
194 ;; when retrieving and modifying class documentation.
195 (check ":normal" 'documentation-class)
196 (setf (documentation 'documentation-class 'type) "2.")
197 (check ":2!" 'documentation-class)
198 (setf (documentation 'documentation-class 'type) nil)
199 (check nil 'documentation-class)
201 ;; Sanity check: make sure the metaclass has its own documentation
202 ;; and is not affected by the above modifications.
203 (check "metaclass with methods on DOCUMENTATION."
204 'documentation-metaclass)))
206 (defstruct (frob (:type vector)) "FROB")
208 (with-test (:name (documentation struct 2))
209 (assert-documentation 'frob 'structure "FROB")
210 (setf (documentation 'frob 'structure) "NEW5")
211 (assert-documentation 'frob 'structure "NEW5"))
213 (deftype quux ()
214 "QUUX"
217 (with-test (:name (documentation type))
218 (assert-documentation 'quux 'type "QUUX")
219 (setf (documentation 'quux 'type) "NEW4")
220 (assert-documentation 'quux 'type "NEW4"))
222 (define-compiler-macro cmacro (x)
223 "compiler macro"
226 (define-compiler-macro (setf cmacro) (y x)
227 "setf compiler macro"
228 (declare (ignore x))
231 (with-test (:name (documentation compiler-macro))
232 (assert-documentation 'cmacro 'compiler-macro "compiler macro")
233 (assert-documentation '(setf cmacro) 'compiler-macro "setf compiler macro"))
235 (defun (setf documentation.setf) (x)
236 "(setf foo) documentation"
239 (with-test (:name (documentation function setf))
240 (flet ((expect (documentation)
241 (assert-documentation
242 '(setf documentation.setf) 'function documentation)
243 (assert-documentation
244 #'(setf documentation.setf) 'function documentation)
245 (assert-documentation
246 #'(setf documentation.setf) t documentation)))
247 (expect "(setf foo) documentation")
248 ;; The original test checked this twice. No idea why.
249 (expect "(setf foo) documentation")
251 ;; Modification
252 (setf (documentation '(setf documentation.setf) 'function)
253 "(setf bar) documentation")
254 (expect "(setf bar) documentation")
256 (setf (documentation #'(setf documentation.setf) 'function)
257 "(setf baz) documentation")
258 (expect "(setf baz) documentation")
260 (setf (documentation #'(setf documentation.setf) t)
261 "(setf fez) documentation")
262 (expect "(setf fez) documentation")))
264 (with-test (:name (documentation lambda))
265 (let ((f (lambda () "aos the zos" t))
266 (g (sb-int:named-lambda fii () "zoot the fruit" t)))
267 (dolist (doc-type '(t function))
268 (assert-documentation f doc-type "aos the zos")
269 (assert-documentation g doc-type "zoot the fruit"))
270 (setf (documentation f t) "fire")
271 (assert-documentation f t "fire")
272 (assert-documentation g t "zoot the fruit")))
274 (with-test (:name (documentation flet))
275 (assert
276 (string= (documentation
277 (flet ((quux (x)
278 "this is FLET quux"
279 (/ x 2)))
280 #'quux)
282 "this is FLET quux")))
284 (with-test (:name (documentation labels))
285 (assert
286 (string= (documentation
287 (labels ((rec (x)
288 "this is LABELS rec"
289 (if (plusp x)
290 (* x (rec (1- x)))
291 1)))
292 #'rec)
294 "this is LABELS rec")))
296 (let ((x 1))
297 (defun docfoo (y)
298 "bar"
299 (incf x y)))
301 (with-test (:name (documentation :closure))
302 (assert-documentation 'docfoo 'function "bar")
303 (assert (string= (setf (documentation 'docfoo 'function) "baz") "baz"))
304 (assert-documentation 'docfoo 'function "baz")
305 (assert-documentation #'docfoo t "baz")
306 (assert (string= (setf (documentation #'docfoo t) "zot") "zot"))
307 (assert-documentation #'docfoo t "zot")
308 (assert-documentation 'docfoo 'function "zot")
309 (assert (not (setf (documentation 'docfoo 'function) nil)))
310 (assert-documentation 'docfoo 'function nil))
312 (with-test (:name (documentation :built-in-macro) :skipped-on (not :sb-doc))
313 (assert (documentation 'trace 'function)))
315 (with-test (:name (documentation :built-in-function) :skipped-on (not :sb-doc))
316 (assert (documentation 'cons 'function)))
318 (defvar documentation.variable nil
319 "foo variable documentation")
321 (with-test (:name (documentation variable))
322 (assert-documentation 'documentation.variable 'variable
323 "foo variable documentation")
324 (setf (documentation 'documentation.variable 'variable)
325 "baz variable documentation")
326 (assert-documentation 'documentation.variable 'variable
327 "baz variable documentation"))
329 (with-test (:name (documentation :mismatch-for-function))
330 (defun test ()
332 nil)
333 (setf (symbol-function 'test2) #'test)
334 (setf (documentation 'test 'function) "Y")
335 (assert (equal (documentation #'test t)
336 (documentation 'test 'function)))
337 (setf (documentation 'test2 'function) "Z")
338 (assert (not
339 (equal (documentation 'test 'function)
340 (documentation 'test2 'function)))))
342 (with-test (:name (documentation setf :on nil))
343 (assert
344 (handler-case
345 (assert (equal (setf (documentation nil 'function) "foo") "foo"))
346 (style-warning () t)
347 (:no-error (x)
348 (declare (ignore x))
349 nil))))
351 (with-test (:name (describe generic-function :assumed-type))
352 ;; Signalled an error at one point
353 (let ((fun (checked-compile '(lambda ()
354 (flet ((zoo () (gogo)))
355 (defmethod gogo () nil)
356 (describe 'gogo)))
357 :allow-style-warnings t)))
358 (handler-bind ((warning #'muffle-warning)) ; implicit gf
359 (silently (funcall fun)))))
361 (defmacro bug-643958-test ()
362 "foo"
363 :ding!)
365 (with-test (:name :bug-643958)
366 (assert (equal "foo" (documentation 'bug-643958-test 'function)))
367 (setf (documentation 'bug-643958-test 'function) "bar")
368 (assert (equal "bar" (documentation 'bug-643958-test 'function))))
370 (with-test (:name (:endianness :in *features*))
371 (assert
372 (or (member :big-endian *features*)
373 (member :little-endian *features*))))
375 (with-test (:name (trace generic-function))
376 (defgeneric traced-gf (x))
377 (defmethod traced-gf (x) (1+ x))
378 (assert (= (traced-gf 3) 4))
379 (trace traced-gf)
380 (let ((output (with-output-to-string (*trace-output*)
381 (assert (= (traced-gf 3) 4)))))
382 (assert (> (length output) 0)))
383 (assert (typep #'traced-gf 'standard-generic-function))
384 (untrace traced-gf)
385 (let ((output (with-output-to-string (*trace-output*)
386 (assert (= (traced-gf 3) 4)))))
387 (assert (= (length output) 0))))
389 (with-test (:name (apropos :inherited :bug-1364413))
390 (let* ((package (make-package "BUGGALO" :use nil))
391 (symbol (intern "BUGGALO" package)))
392 (export (list symbol) package)
393 (let ((inherits (make-package "BUGGALO-INHERITS" :use (list package))))
394 (assert (= (length (apropos-list "BUGGALO" package)) 1))
395 (assert (= (length (apropos-list "BUGGALO" inherits)) 1))
396 (delete-package inherits))
397 (delete-package package)))
399 (with-test (:name (apropos :inherited :external-only :bug-1364413))
400 (let* ((package (make-package "BUGGALO" :use nil))
401 (symbol (intern "BUGGALO" package)))
402 (export (list symbol) package)
403 (let ((inherits (make-package "BUGGALO-INHERITS" :use (list package))))
404 (assert (= (length (apropos-list "BUGGALO" package t)) 1))
405 (assert (= (length (apropos-list "BUGGALO" inherits t)) 0))
406 (delete-package inherits))
407 (delete-package package)))
409 (with-test (:name (apropos :once-only))
410 (assert (= (length (apropos-list "UPDATE-INSTANCE-FOR-REDEFINED-CLASS")) 1)))
412 (defgeneric gf-arglist-1 (x &key y))
413 (defmethod gf-arglist-1 (x &key (y nil) (z nil z-p))
414 (list x y z z-p))
416 (defgeneric gf-arglist-2 (x &key y))
417 (defmethod gf-arglist-2 ((x integer) &key (y nil) ((z f) nil z-p)) (list x y f z-p))
418 (defmethod gf-arglist-2 ((x string) &key (y nil) ((z w) nil z-p)) (list x y w z-p))
420 (defgeneric gf-arglist-3 (x &key ((:y y))))
422 (defgeneric gf-arglist-4 (x &key ((:y z))))
424 (defgeneric gf-arglist-5 (x &key y))
425 (defmethod gf-arglist-5 ((x integer) &key z &allow-other-keys) (list x z))
427 (with-test (:name (:generic-function-pretty-arglist 1))
428 (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-1)
429 '(x &key y z))))
430 (with-test (:name (:generic-function-pretty-arglist 2))
431 (assert (or (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-2)
432 '(x &key y ((z w))))
433 (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-2)
434 '(x &key y ((z f)))))))
435 (with-test (:name (:generic-function-pretty-arglist 3))
436 (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-3)
437 '(x &key y))))
438 (with-test (:name (:generic-function-pretty-arglist 4))
439 (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-4)
440 '(x &key ((:y z))))))
441 (with-test (:name (:generic-function-pretty-arglist 5))
442 (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-5)
443 '(x &key y z &allow-other-keys))))
444 ;;;; success