1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
14 (defvar *format-mode
*)
16 (defun format* (format-control &rest arguments
)
19 (eval `(format nil
,format-control
,@arguments
)))
21 (let ((names (sb-int:make-gensym-list
(length arguments
))))
22 (funcall (checked-compile
23 `(lambda ,names
(format nil
,format-control
,@names
)))
26 (defmacro with-compiled-and-interpreted-format
(() &body body
)
27 `(flet ((run-body (mode)
28 (let ((*format-mode
* mode
))
32 (error "~@<Error in ~A FORMAT: ~A~@:>"
37 (defun format-error-format-control-string-p (condition)
38 (and (typep condition
'sb-format
:format-error
)
39 (sb-format::format-error-control-string condition
)))
41 (deftype format-error-with-control-string
()
42 `(and sb-format
:format-error
43 (satisfies format-error-format-control-string-p
)))
45 (with-test (:name
(:[-directive
:non-integer-argument
))
46 (with-compiled-and-interpreted-format ()
47 (assert-error (format* "~[~]" 1d0
) format-error-with-control-string
)))
49 (with-test (:name
(:P-directive
:no-previous-argument
))
50 (with-compiled-and-interpreted-format ()
51 (assert-error (format* "~@<~:P~@:>" '()) format-error-with-control-string
)))
53 (with-test (:name
(:*-directive
:out-of-bounds
))
54 (with-compiled-and-interpreted-format ()
55 (assert-error (format* "~2@*" '()) format-error-with-control-string
)
56 (assert-error (format* "~1:*" '()) format-error-with-control-string
)))
58 (with-test (:name
:encapsulated-~
/-formatter
)
59 (let ((s (make-string-output-stream)))
60 (declare (notinline format
))
61 (sb-int:encapsulate
'sb-ext
:print-symbol-with-prefix
'test
62 (lambda (f stream obj
&rest args
)
63 (write-string "{{" stream
)
64 (apply f stream obj args
)
65 (write-string "}}" stream
)))
66 (format s
"~/sb-ext:print-symbol-with-prefix/" 'cl-user
::test
)
67 (sb-int:unencapsulate
'sb-ext
:print-symbol-with-prefix
'test
)
68 (assert (string= "{{COMMON-LISP-USER::TEST}}" (get-output-stream-string s
)))))
70 (with-test (:name
:non-simple-string
)
71 (let ((control (make-array 2 :element-type
'base-char
74 (checked-compile-and-assert
76 `(lambda () (with-output-to-string (stream)
77 (funcall (formatter ,control
) stream
)))
78 (() "A" :test
#'equal
))
79 (checked-compile-and-assert
81 `(lambda () (format nil
,control
))
82 (() "A" :test
#'equal
))))