0.9.18.16: disassembly of funcallable instances
[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
51 ;; nor should it fail on generic functions or other funcallable instances
52 (defgeneric disassemble-generic (x))
53 (disassemble 'disassemble-generic)
54 (let ((fin (sb-mop:make-instance 'sb-mop:funcallable-standard-object)))
55   (disassemble fin))
56 \f
57 ;;; support for DESCRIBE tests
58 (defstruct to-be-described a b)
59 (defclass forward-describe-class (forward-describe-ref) (a))
60
61 ;;; DESCRIBE should run without signalling an error.
62 (describe (make-to-be-described))
63 (describe 12)
64 (describe "a string")
65 (describe 'symbolism)
66 (describe (find-package :cl))
67 (describe '(a list))
68 (describe #(a vector))
69
70 ;;; The DESCRIBE-OBJECT methods for built-in CL stuff should do
71 ;;; FRESH-LINE and TERPRI neatly.
72 (dolist (i (list (make-to-be-described :a 14) 12 "a string"
73                  #0a0 #(1 2 3) #2a((1 2) (3 4)) 'sym :keyword
74                  (find-package :keyword) (list 1 2 3)
75                  nil (cons 1 2) (make-hash-table)
76                  (let ((h (make-hash-table)))
77                    (setf (gethash 10 h) 100
78                          (gethash 11 h) 121)
79                    h)
80                  (make-condition 'simple-error)
81                  (make-condition 'simple-error :format-control "fc")
82                  #'car #'make-to-be-described (lambda (x) (+ x 11))
83                  (constantly 'foo) #'(setf to-be-described-a)
84                  #'describe-object (find-class 'to-be-described)
85                  (find-class 'forward-describe-class)
86                  (find-class 'forward-describe-ref) (find-class 'cons)))
87   (let ((s (with-output-to-string (s)
88              (write-char #\x s)
89              (describe i s))))
90     (unless (and (char= #\x (char s 0))
91                  ;; one leading #\NEWLINE from FRESH-LINE or the like, no more
92                  (char= #\newline (char s 1))
93                  (char/= #\newline (char s 2))
94                  ;; one trailing #\NEWLINE from TERPRI or the like, no more
95                  (let ((n (length s)))
96                    (and (char= #\newline (char s (- n 1)))
97                         (char/= #\newline (char s (- n 2))))))
98       (error "misbehavior in DESCRIBE of ~S" i))))
99
100 \f
101 ;;; Tests of documentation on types and classes
102 (defclass foo ()
103   ()
104   (:documentation "FOO"))
105 (defstruct bar "BAR")
106 (define-condition baz ()
107   ()
108   (:documentation "BAZ"))
109 (deftype quux ()
110   "QUUX"
111   't)
112 (defstruct (frob (:type vector)) "FROB")
113 (macrolet
114     ((do-class (name expected &optional structurep)
115        `(progn
116          (assert (string= (documentation ',name 'type) ,expected))
117          (assert (string= (documentation (find-class ',name) 'type) ,expected))
118          (assert (string= (documentation (find-class ',name) 't) ,expected))
119          ,@(when structurep
120             `((assert (string= (documentation ',name 'structure) ,expected))))
121          (let ((new1 (symbol-name (gensym "NEW1")))
122                (new2 (symbol-name (gensym "NEW2")))
123                (new3 (symbol-name (gensym "NEW3")))
124                (new4 (symbol-name (gensym "NEW4"))))
125            (declare (ignorable new4))
126            (setf (documentation ',name 'type) new1)
127            (assert (string= (documentation (find-class ',name) 'type) new1))
128            (setf (documentation (find-class ',name) 'type) new2)
129            (assert (string= (documentation (find-class ',name) 't) new2))
130            (setf (documentation (find-class ',name) 't) new3)
131            (assert (string= (documentation ',name 'type) new3))
132            ,@(when structurep
133               `((assert (string= (documentation ',name 'structure) new3))
134                 (setf (documentation ',name 'structure) new4)
135                 (assert (string= (documentation ',name 'structure) new4))))))))
136   (do-class foo "FOO")
137   (do-class bar "BAR" t)
138   (do-class baz "BAZ"))
139
140 (assert (string= (documentation 'quux 'type) "QUUX"))
141 (setf (documentation 'quux 'type) "NEW4")
142 (assert (string= (documentation 'quux 'type) "NEW4"))
143
144 (assert (string= (documentation 'frob 'structure) "FROB"))
145 (setf (documentation 'frob 'structure) "NEW5")
146 (assert (string= (documentation 'frob 'structure) "NEW5"))
147 \f
148 ;;;; success