0.9.18.5: Support DISASSEMBLE for interpreted functions.
[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 (use-package "ASSERTOID")
16
17 (defun (setf foo) (x)
18   "(setf foo) documentation"
19   x)
20
21 (assert (string= (documentation '(setf foo) 'function)
22                  "(setf foo) documentation"))
23 (assert (string= (documentation #'(setf foo) 'function)
24                  "(setf foo) documentation"))
25
26 (assert (string= (documentation '(setf foo) 'function)
27                  "(setf foo) documentation"))
28 (assert (string= (documentation #'(setf foo) 'function)
29                  "(setf foo) documentation"))
30 \f
31 ;;; DISASSEMBLE shouldn't fail on closures or unpurified functions
32 (defun disassemble-fun (x) x)
33 (disassemble 'disassemble-fun)
34
35 (let ((x 1)) (defun disassemble-closure (y) (if y (setq x y) x)))
36 (disassemble 'disassemble-closure)
37
38 #+sb-eval
39 (progn
40   ;; Nor should it fail on interpreted functions
41   (let ((sb-ext:*evaluator-mode* :interpret))
42     (eval `(defun disassemble-eval (x) x))
43     (disassemble 'disassemble-eval))
44
45   ;; disassemble-eval should still be an interpreted function.
46   ;; clhs disassemble: "(If that function is an interpreted function,
47   ;; it is first compiled but the result of this implicit compilation
48   ;; is not installed.)"
49   (assert (sb-eval:interpreted-function-p #'disassemble-eval)))
50 \f
51 ;;; support for DESCRIBE tests
52 (defstruct to-be-described a b)
53 (defclass forward-describe-class (forward-describe-ref) (a))
54
55 ;;; DESCRIBE should run without signalling an error.
56 (describe (make-to-be-described))
57 (describe 12)
58 (describe "a string")
59 (describe 'symbolism)
60 (describe (find-package :cl))
61 (describe '(a list))
62 (describe #(a vector))
63
64 ;;; The DESCRIBE-OBJECT methods for built-in CL stuff should do
65 ;;; FRESH-LINE and TERPRI neatly.
66 (dolist (i (list (make-to-be-described :a 14) 12 "a string"
67                  #0a0 #(1 2 3) #2a((1 2) (3 4)) 'sym :keyword
68                  (find-package :keyword) (list 1 2 3)
69                  nil (cons 1 2) (make-hash-table)
70                  (let ((h (make-hash-table)))
71                    (setf (gethash 10 h) 100
72                          (gethash 11 h) 121)
73                    h)
74                  (make-condition 'simple-error)
75                  (make-condition 'simple-error :format-control "fc")
76                  #'car #'make-to-be-described (lambda (x) (+ x 11))
77                  (constantly 'foo) #'(setf to-be-described-a)
78                  #'describe-object (find-class 'to-be-described)
79                  (find-class 'forward-describe-class)
80                  (find-class 'forward-describe-ref) (find-class 'cons)))
81   (let ((s (with-output-to-string (s)
82              (write-char #\x s)
83              (describe i s))))
84     (unless (and (char= #\x (char s 0))
85                  ;; one leading #\NEWLINE from FRESH-LINE or the like, no more
86                  (char= #\newline (char s 1))
87                  (char/= #\newline (char s 2))
88                  ;; one trailing #\NEWLINE from TERPRI or the like, no more
89                  (let ((n (length s)))
90                    (and (char= #\newline (char s (- n 1)))
91                         (char/= #\newline (char s (- n 2))))))
92       (error "misbehavior in DESCRIBE of ~S" i))))
93
94 \f
95 ;;; Tests of documentation on types and classes
96 (defclass foo ()
97   ()
98   (:documentation "FOO"))
99 (defstruct bar "BAR")
100 (define-condition baz ()
101   ()
102   (:documentation "BAZ"))
103 (deftype quux ()
104   "QUUX"
105   't)
106 (defstruct (frob (:type vector)) "FROB")
107 (macrolet
108     ((do-class (name expected &optional structurep)
109        `(progn
110          (assert (string= (documentation ',name 'type) ,expected))
111          (assert (string= (documentation (find-class ',name) 'type) ,expected))
112          (assert (string= (documentation (find-class ',name) 't) ,expected))
113          ,@(when structurep
114             `((assert (string= (documentation ',name 'structure) ,expected))))
115          (let ((new1 (symbol-name (gensym "NEW1")))
116                (new2 (symbol-name (gensym "NEW2")))
117                (new3 (symbol-name (gensym "NEW3")))
118                (new4 (symbol-name (gensym "NEW4"))))
119            (declare (ignorable new4))
120            (setf (documentation ',name 'type) new1)
121            (assert (string= (documentation (find-class ',name) 'type) new1))
122            (setf (documentation (find-class ',name) 'type) new2)
123            (assert (string= (documentation (find-class ',name) 't) new2))
124            (setf (documentation (find-class ',name) 't) new3)
125            (assert (string= (documentation ',name 'type) new3))
126            ,@(when structurep
127               `((assert (string= (documentation ',name 'structure) new3))
128                 (setf (documentation ',name 'structure) new4)
129                 (assert (string= (documentation ',name 'structure) new4))))))))
130   (do-class foo "FOO")
131   (do-class bar "BAR" t)
132   (do-class baz "BAZ"))
133
134 (assert (string= (documentation 'quux 'type) "QUUX"))
135 (setf (documentation 'quux 'type) "NEW4")
136 (assert (string= (documentation 'quux 'type) "NEW4"))
137
138 (assert (string= (documentation 'frob 'structure) "FROB"))
139 (setf (documentation 'frob 'structure) "NEW5")
140 (assert (string= (documentation 'frob 'structure) "NEW5"))
141 \f
142 ;;;; success