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