Fix (documentation closure) test.
[sbcl.git] / tests / interface.impure.lisp
1 ;;;; tests for problems in the interface presented to the user/programmer
2
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.
13
14 (load "assertoid.lisp")
15 (load "test-util.lisp")
16 (use-package "ASSERTOID")
17 (use-package "TEST-UTIL")
18
19 (defun (setf foo) (x)
20   "(setf foo) documentation"
21   x)
22
23 (assert (string= (documentation '(setf foo) 'function)
24                  "(setf foo) documentation"))
25 (assert (string= (documentation #'(setf foo) 'function)
26                  "(setf foo) documentation"))
27
28 (assert (string= (documentation '(setf foo) 'function)
29                  "(setf foo) documentation"))
30 (assert (string= (documentation #'(setf foo) 'function)
31                  "(setf foo) documentation"))
32 \f
33 ;;; DISASSEMBLE shouldn't fail on closures or unpurified functions
34 (defun disassemble-fun (x) x)
35 (disassemble 'disassemble-fun)
36
37 (let ((x 1)) (defun disassemble-closure (y) (if y (setq x y) x)))
38 (disassemble 'disassemble-closure)
39
40 #+sb-eval
41 (progn
42   ;; Nor should it fail on interpreted functions
43   (let ((sb-ext:*evaluator-mode* :interpret))
44     (eval `(defun disassemble-eval (x) x))
45     (disassemble 'disassemble-eval))
46
47   ;; disassemble-eval should still be an interpreted function.
48   ;; clhs disassemble: "(If that function is an interpreted function,
49   ;; it is first compiled but the result of this implicit compilation
50   ;; is not installed.)"
51   (assert (sb-eval:interpreted-function-p #'disassemble-eval)))
52
53 ;; nor should it fail on generic functions or other funcallable instances
54 (defgeneric disassemble-generic (x))
55 (disassemble 'disassemble-generic)
56 (let ((fin (sb-mop:make-instance 'sb-mop:funcallable-standard-object)))
57   (disassemble fin))
58
59 ;;; while we're at it, much the same applies to
60 ;;; FUNCTION-LAMBDA-EXPRESSION:
61 (defun fle-fun (x) x)
62 (function-lambda-expression #'fle-fun)
63
64 (let ((x 1)) (defun fle-closure (y) (if y (setq x y) x)))
65 (function-lambda-expression #'fle-closure)
66
67 #+sb-eval
68 (progn
69   ;; Nor should it fail on interpreted functions
70   (let ((sb-ext:*evaluator-mode* :interpret))
71     (eval `(defun fle-eval (x) x))
72     (function-lambda-expression #'fle-eval))
73
74   ;; fle-eval should still be an interpreted function.
75   (assert (sb-eval:interpreted-function-p #'fle-eval)))
76
77 ;; nor should it fail on generic functions or other funcallable instances
78 (defgeneric fle-generic (x))
79 (function-lambda-expression #'fle-generic)
80 (let ((fin (sb-mop:make-instance 'sb-mop:funcallable-standard-object)))
81   (function-lambda-expression fin))
82 \f
83 ;;; support for DESCRIBE tests
84 (defstruct to-be-described a b)
85 (defclass forward-describe-class (forward-describe-ref) (a))
86 (let ((sb-ext:*evaluator-mode* :compile))
87   (eval `(let (x) (defun closure-to-describe () (incf x)))))
88
89 ;;; DESCRIBE should run without signalling an error.
90 (with-test (:name (describe :no-error))
91   (describe (make-to-be-described))
92   (describe 12)
93   (describe "a string")
94   (describe 'symbolism)
95   (describe (find-package :cl))
96   (describe '(a list))
97   (describe #(a vector))
98 ;; bug 824974
99   (describe 'closure-to-describe))
100
101 ;;; The DESCRIBE-OBJECT methods for built-in CL stuff should do
102 ;;; FRESH-LINE and TERPRI neatly.
103 (dolist (i (list (make-to-be-described :a 14) 12 "a string"
104                  #0a0 #(1 2 3) #2a((1 2) (3 4)) 'sym :keyword
105                  (find-package :keyword) (list 1 2 3)
106                  nil (cons 1 2) (make-hash-table)
107                  (let ((h (make-hash-table)))
108                    (setf (gethash 10 h) 100
109                          (gethash 11 h) 121)
110                    h)
111                  (make-condition 'simple-error)
112                  (make-condition 'simple-error :format-control "fc")
113                  #'car #'make-to-be-described (lambda (x) (+ x 11))
114                  (constantly 'foo) #'(setf to-be-described-a)
115                  #'describe-object (find-class 'to-be-described)
116                  (find-class 'forward-describe-class)
117                  (find-class 'forward-describe-ref) (find-class 'cons)))
118   (let ((s (with-output-to-string (s)
119              (write-char #\x s)
120              (describe i s))))
121     (macrolet ((check (form)
122                  `(or ,form
123                       (error "misbehavior in DESCRIBE of ~S:~%   ~S" i ',form))))
124       (check (char= #\x (char s 0)))
125       ;; one leading #\NEWLINE from FRESH-LINE or the like, no more
126       (check (char= #\newline (char s 1)))
127       (check (char/= #\newline (char s 2)))
128       ;; one trailing #\NEWLINE from TERPRI or the like, no more
129       (let ((n (length s)))
130         (check (char= #\newline (char s (- n 1))))
131         (check (char/= #\newline (char s (- n 2))))))))
132
133 \f
134 ;;; Tests of documentation on types and classes
135 (defclass foo ()
136   ()
137   (:documentation "FOO"))
138 (defstruct bar "BAR")
139 (define-condition baz ()
140   ()
141   (:documentation "BAZ"))
142 (deftype quux ()
143   "QUUX"
144   't)
145 (defstruct (frob (:type vector)) "FROB")
146 (macrolet
147     ((do-class (name expected &optional structurep)
148        `(progn
149          (assert (string= (documentation ',name 'type) ,expected))
150          (assert (string= (documentation (find-class ',name) 'type) ,expected))
151          (assert (string= (documentation (find-class ',name) 't) ,expected))
152          ,@(when structurep
153             `((assert (string= (documentation ',name 'structure) ,expected))))
154          (let ((new1 (symbol-name (gensym "NEW1")))
155                (new2 (symbol-name (gensym "NEW2")))
156                (new3 (symbol-name (gensym "NEW3")))
157                (new4 (symbol-name (gensym "NEW4"))))
158            (declare (ignorable new4))
159            (setf (documentation ',name 'type) new1)
160            (assert (string= (documentation (find-class ',name) 'type) new1))
161            (setf (documentation (find-class ',name) 'type) new2)
162            (assert (string= (documentation (find-class ',name) 't) new2))
163            (setf (documentation (find-class ',name) 't) new3)
164            (assert (string= (documentation ',name 'type) new3))
165            ,@(when structurep
166               `((assert (string= (documentation ',name 'structure) new3))
167                 (setf (documentation ',name 'structure) new4)
168                 (assert (string= (documentation ',name 'structure) new4))))))))
169   (do-class foo "FOO")
170   (do-class bar "BAR" t)
171   (do-class baz "BAZ"))
172
173 (assert (string= (documentation 'quux 'type) "QUUX"))
174 (setf (documentation 'quux 'type) "NEW4")
175 (assert (string= (documentation 'quux 'type) "NEW4"))
176
177 (assert (string= (documentation 'frob 'structure) "FROB"))
178 (setf (documentation 'frob 'structure) "NEW5")
179 (assert (string= (documentation 'frob 'structure) "NEW5"))
180
181 (define-compiler-macro cmacro (x)
182   "compiler macro"
183   x)
184
185 (define-compiler-macro (setf cmacro) (y x)
186   "setf compiler macro"
187   y)
188
189 (with-test (:name (documentation compiler-macro))
190   (unless (equal "compiler macro"
191                  (documentation 'cmacro 'compiler-macro))
192     (error "got ~S for cmacro"
193            (documentation 'cmacro 'compiler-macro)))
194   (unless (equal "setf compiler macro"
195                  (documentation '(setf cmacro) 'compiler-macro))
196     (error "got ~S for setf macro" (documentation '(setf cmacro) 'compiler-macro))))
197
198 (with-test (:name (documentation lambda))
199   (let ((f (lambda () "aos the zos" t))
200         (g (sb-int:named-lambda fii () "zoot the fruit" t)))
201     (dolist (doc-type '(t function))
202       (assert (string= (documentation f doc-type) "aos the zos"))
203       (assert (string= (documentation g doc-type) "zoot the fruit")))
204     (setf (documentation f t) "fire")
205     (assert (string= (documentation f t) "fire"))
206     (assert (string= (documentation g t) "zoot the fruit"))))
207
208 (with-test (:name (documentation flet))
209   (assert
210    (string= (documentation
211              (flet ((quux (x)
212                       "this is FLET quux"
213                       (/ x 2)))
214                #'quux)
215              t)
216             "this is FLET quux")))
217
218 (with-test (:name (documentation labels))
219   (assert
220    (string= (documentation
221              (labels ((rec (x)
222                         "this is LABELS rec"
223                         (if (plusp x)
224                             (* x (rec (1- x)))
225                             1)))
226                #'rec)
227              t)
228             "this is LABELS rec")))
229
230 (let ((x 1))
231   (defun docfoo (y)
232     "bar"
233     (incf x y)))
234
235 (with-test (:name (documentation closure))
236   (assert (string= (documentation 'docfoo 'function) "bar"))
237   (assert (string= (setf (documentation 'docfoo 'function) "baz") "baz"))
238   (assert (string= (documentation 'docfoo 'function) "baz"))
239   (assert (string= (documentation #'docfoo t) "baz"))
240   (assert (string= (setf (documentation #'docfoo t) "zot") "zot"))
241   (assert (string= (documentation #'docfoo t) "zot"))
242   (assert (string= (documentation 'docfoo 'function) "zot"))
243   (assert (not (setf (documentation 'docfoo 'function) nil)))
244   (assert (not (documentation 'docfoo 'function))))
245
246 (with-test (:name (documentation built-in-macro) :skipped-on '(not :sb-doc))
247   (assert (documentation 'trace 'function)))
248
249 (with-test (:name (documentation built-in-function) :skipped-on '(not :sb-doc))
250   (assert (documentation 'cons 'function)))
251
252 (with-test (:name :describe-generic-function-with-assumed-type)
253   ;; Signalled an error at one point
254   (flet ((zoo () (gogo)))
255     (defmethod gogo () nil)
256     (describe 'gogo)))
257
258 (defmacro bug-643958-test ()
259   "foo"
260   :ding!)
261
262 (with-test (:name :bug-643958)
263   (assert (equal "foo" (documentation 'bug-643958-test 'function)))
264   (setf (documentation 'bug-643958-test 'function) "bar")
265   (assert (equal "bar" (documentation 'bug-643958-test 'function))))
266
267 (defclass cannot-print-this ()
268   ())
269 (defmethod print-object ((oops cannot-print-this) stream)
270   (error "No go!"))
271 (with-test (:name :describe-suppresses-print-errors)
272   (handler-bind ((error #'continue))
273     (with-output-to-string (s)
274       (describe (make-instance 'cannot-print-this) s))))
275 (with-test (:name :backtrace-suppresses-print-errors)
276   (handler-bind ((error #'continue))
277     (with-output-to-string (s)
278       (labels ((foo (n x)
279                  (when (plusp n)
280                    (foo (1- n) x))
281                  (when (zerop n)
282                    (sb-debug:backtrace 100 s))))
283         (foo 100 (make-instance 'cannot-print-this))))))
284 (with-test (:name :backtrace-and-circles)
285   (handler-bind ((error #'continue))
286     (with-output-to-string (s)
287       (labels ((foo (n x)
288                  (when (plusp n)
289                    (foo (1- n) x))
290                  (when (zerop n)
291                    (sb-debug:backtrace 100 s))))
292         (foo 100 (let ((list (list t)))
293                    (nconc list list)))))))
294
295 (with-test (:name :endianness-in-features)
296   (assert
297    (or (member :big-endian *features*)
298        (member :little-endian *features*))))
299
300 (with-test (:name :function-documentation-mismatch)
301   (defun test ()
302     "X"
303     nil)
304   (setf (symbol-function 'test2) #'test)
305   (setf (documentation 'test 'function) "Y")
306   (assert (equal (documentation #'test t)
307                  (documentation 'test 'function)))
308   (setf (documentation 'test2 'function) "Z")
309   (assert (not
310            (equal (documentation 'test 'function)
311                   (documentation 'test2 'function)))))
312
313 \f
314 ;;;; success