1.0.29.38: better DESCRIBE
[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
87 ;;; DESCRIBE should run without signalling an error.
88 (describe (make-to-be-described))
89 (describe 12)
90 (describe "a string")
91 (describe 'symbolism)
92 (describe (find-package :cl))
93 (describe '(a list))
94 (describe #(a vector))
95
96 ;;; The DESCRIBE-OBJECT methods for built-in CL stuff should do
97 ;;; FRESH-LINE and TERPRI neatly.
98 (dolist (i (list (make-to-be-described :a 14) 12 "a string"
99                  #0a0 #(1 2 3) #2a((1 2) (3 4)) 'sym :keyword
100                  (find-package :keyword) (list 1 2 3)
101                  nil (cons 1 2) (make-hash-table)
102                  (let ((h (make-hash-table)))
103                    (setf (gethash 10 h) 100
104                          (gethash 11 h) 121)
105                    h)
106                  (make-condition 'simple-error)
107                  (make-condition 'simple-error :format-control "fc")
108                  #'car #'make-to-be-described (lambda (x) (+ x 11))
109                  (constantly 'foo) #'(setf to-be-described-a)
110                  #'describe-object (find-class 'to-be-described)
111                  (find-class 'forward-describe-class)
112                  (find-class 'forward-describe-ref) (find-class 'cons)))
113   (let ((s (with-output-to-string (s)
114              (write-char #\x s)
115              (describe i s))))
116     (macrolet ((check (form)
117                  `(or ,form
118                       (error "misbehavior in DESCRIBE of ~S:~%   ~S" i ',form))))
119       (check (char= #\x (char s 0)))
120       ;; one leading #\NEWLINE from FRESH-LINE or the like, no more
121       (check (char= #\newline (char s 1)))
122       (check (char/= #\newline (char s 2)))
123       ;; one trailing #\NEWLINE from TERPRI or the like, no more
124       (let ((n (length s)))
125         (check (char= #\newline (char s (- n 1))))
126         (check (char/= #\newline (char s (- n 2))))))))
127
128 \f
129 ;;; Tests of documentation on types and classes
130 (defclass foo ()
131   ()
132   (:documentation "FOO"))
133 (defstruct bar "BAR")
134 (define-condition baz ()
135   ()
136   (:documentation "BAZ"))
137 (deftype quux ()
138   "QUUX"
139   't)
140 (defstruct (frob (:type vector)) "FROB")
141 (macrolet
142     ((do-class (name expected &optional structurep)
143        `(progn
144          (assert (string= (documentation ',name 'type) ,expected))
145          (assert (string= (documentation (find-class ',name) 'type) ,expected))
146          (assert (string= (documentation (find-class ',name) 't) ,expected))
147          ,@(when structurep
148             `((assert (string= (documentation ',name 'structure) ,expected))))
149          (let ((new1 (symbol-name (gensym "NEW1")))
150                (new2 (symbol-name (gensym "NEW2")))
151                (new3 (symbol-name (gensym "NEW3")))
152                (new4 (symbol-name (gensym "NEW4"))))
153            (declare (ignorable new4))
154            (setf (documentation ',name 'type) new1)
155            (assert (string= (documentation (find-class ',name) 'type) new1))
156            (setf (documentation (find-class ',name) 'type) new2)
157            (assert (string= (documentation (find-class ',name) 't) new2))
158            (setf (documentation (find-class ',name) 't) new3)
159            (assert (string= (documentation ',name 'type) new3))
160            ,@(when structurep
161               `((assert (string= (documentation ',name 'structure) new3))
162                 (setf (documentation ',name 'structure) new4)
163                 (assert (string= (documentation ',name 'structure) new4))))))))
164   (do-class foo "FOO")
165   (do-class bar "BAR" t)
166   (do-class baz "BAZ"))
167
168 (assert (string= (documentation 'quux 'type) "QUUX"))
169 (setf (documentation 'quux 'type) "NEW4")
170 (assert (string= (documentation 'quux 'type) "NEW4"))
171
172 (assert (string= (documentation 'frob 'structure) "FROB"))
173 (setf (documentation 'frob 'structure) "NEW5")
174 (assert (string= (documentation 'frob 'structure) "NEW5"))
175
176 (define-compiler-macro cmacro (x)
177   "compiler macro"
178   x)
179
180 (define-compiler-macro (setf cmacro) (y x)
181   "setf compiler macro"
182   y)
183
184 (with-test (:name (documentation compiler-macro))
185   (unless (equal "compiler macro"
186                  (documentation 'cmacro 'compiler-macro))
187     (error "got ~S for cmacro"
188            (documentation 'cmacro 'compiler-macro)))
189   (unless (equal "setf compiler macro"
190                  (documentation '(setf cmacro) 'compiler-macro))
191     (error "got ~S for setf macro" (documentation '(setf cmacro) 'compiler-macro))))
192
193 (with-test (:name (documentation lambda))
194   (let ((f (lambda () "aos the zos" t))
195         (g (sb-int:named-lambda fii () "zoot the fruit" t)))
196     (dolist (doc-type '(t function))
197       (assert (string= (documentation f doc-type) "aos the zos"))
198       (assert (string= (documentation g doc-type) "zoot the fruit")))
199     (setf (documentation f t) "fire")
200     (assert (string= (documentation f t) "fire"))
201     (assert (string= (documentation g t) "zoot the fruit"))))
202
203 (with-test (:name (documentation flet))
204   (assert
205    (string= (documentation
206              (flet ((quux (x)
207                       "this is FLET quux"
208                       (/ x 2)))
209                #'quux)
210              t)
211             "this is FLET quux")))
212
213 (with-test (:name (documentation labels))
214   (assert
215    (string= (documentation
216              (labels ((rec (x)
217                         "this is LABELS rec"
218                         (if (plusp x)
219                             (* x (rec (1- x)))
220                             1)))
221                #'rec)
222              t)
223             "this is LABELS rec")))
224
225 (let ((x 1))
226   (defun docfoo (y)
227     "bar"
228     (incf x y)))
229
230 (with-test (:name (documentation closure))
231   (assert (string= (documentation 'docfoo 'function) "bar"))
232   (assert (string= (documentation #'docfoo t) "bar"))
233   (assert (string= (setf (documentation 'docfoo 'function) "baz") "baz"))
234   (assert (string= (documentation 'docfoo 'function) "baz"))
235   (assert (string= (documentation #'docfoo t) "baz")))
236
237 #+sb-doc
238 (with-test (:name (documentation built-in-macro))
239   (assert (documentation 'trace 'function)))
240
241 #+sb-doc
242 (with-test (:name (documentation built-in-function))
243   (assert (documentation 'cons 'function)))
244 \f
245 ;;;; success