aboutsummaryrefslogtreecommitdiffstats
path: root/test/src
diff options
context:
space:
mode:
authorGemini Lasswell2019-08-04 15:56:12 -0700
committerGemini Lasswell2019-09-13 13:43:07 -0700
commit3bd6ef40b55e429a321c87a09fd94e6ca0e50ae7 (patch)
treeb76524f92d867c0d7a7de186fca01c8cb017cfd1 /test/src
parent6eaf39d21b70802e6bc607ee2fc2fff67b79231a (diff)
downloademacs-3bd6ef40b55e429a321c87a09fd94e6ca0e50ae7.tar.gz
emacs-3bd6ef40b55e429a321c87a09fd94e6ca0e50ae7.zip
Create common tests for print.c and cl-print.el
* test/lisp/emacs-lisp/cl-print-tests.el (cl-print--test, cl-print-tests-1, cl-print-tests-2) (cl-print-tests-3, cl-print-tests-4, cl-print-tests-5) (cl-print-tests-strings, cl-print-circle, cl-print-circle-2): Remove. * test/src/print-tests.el (print-tests--prin1-to-string): New alias. (print-tests--deftest): New macro. (print-hex-backslash, print-read-roundtrip, print-bignum): Define with print-tests--deftest and use print-tests--prin1-to-string. (print-tests--prints-with-charset-p): Use print-tests--prin1-to-string. (print-tests--print-charset-text-property-nil) (print-tests--print-charset-text-property-t) (print-tests--print-charset-text-property-default): Define with print-tests--deftest. (print-tests-print-gensym) (print-tests-continuous-numbering, print-tests-1, print-tests-2) (print-tests-3, print-tests-4, print-tests-5) (print-tests-strings, print-circle, print-circle-2): New tests. (print--test, print-tests-struct): New cl-defstructs.
Diffstat (limited to 'test/src')
-rw-r--r--test/src/print-tests.el259
1 files changed, 247 insertions, 12 deletions
diff --git a/test/src/print-tests.el b/test/src/print-tests.el
index 8e377d71808..26d49a5ffba 100644
--- a/test/src/print-tests.el
+++ b/test/src/print-tests.el
@@ -21,42 +21,86 @@
21 21
22(require 'ert) 22(require 'ert)
23 23
24(ert-deftest print-hex-backslash () 24;; Support sharing test code with cl-print-tests.
25
26(defalias 'print-tests--prin1-to-string #'identity
27 "The function to print to a string which is under test.")
28
29(defmacro print-tests--deftest (name arg &rest docstring-keys-and-body)
30 "Test both print.c and cl-print.el at once."
31 (declare (debug ert-deftest)
32 (doc-string 3)
33 (indent 2))
34 (let ((clname (intern (concat (symbol-name name) "-cl-print")))
35 (doc (when (stringp (car-safe docstring-keys-and-body))
36 (list (pop docstring-keys-and-body))))
37 (keys-and-values nil))
38 (while (keywordp (car-safe docstring-keys-and-body))
39 (let ((key (pop docstring-keys-and-body))
40 (val (pop docstring-keys-and-body)))
41 (push val keys-and-values)
42 (push key keys-and-values)))
43 `(progn
44 ;; Set print-tests--prin1-to-string at both declaration and
45 ;; runtime, so that it can be used by the :expected-result
46 ;; keyword.
47 (cl-letf (((symbol-function #'print-tests--prin1-to-string)
48 #'prin1-to-string))
49 (ert-deftest ,name ,arg
50 ,@doc
51 ,@keys-and-values
52 (cl-letf (((symbol-function #'print-tests--prin1-to-string)
53 #'prin1-to-string))
54 ,@docstring-keys-and-body)))
55 (cl-letf (((symbol-function #'print-tests--prin1-to-string)
56 #'cl-prin1-to-string))
57 (ert-deftest ,clname ,arg
58 ,@doc
59 ,@keys-and-values
60 (cl-letf (((symbol-function #'print-tests--prin1-to-string)
61 #'cl-prin1-to-string))
62 ,@docstring-keys-and-body))))))
63
64(print-tests--deftest print-hex-backslash ()
25 (should (string= (let ((print-escape-multibyte t) 65 (should (string= (let ((print-escape-multibyte t)
26 (print-escape-newlines t)) 66 (print-escape-newlines t))
27 (prin1-to-string "\u00A2\ff")) 67 (print-tests--prin1-to-string "\u00A2\ff"))
28 "\"\\x00a2\\ff\""))) 68 "\"\\x00a2\\ff\"")))
29 69
30(defun print-tests--prints-with-charset-p (ch odd-charset) 70(defun print-tests--prints-with-charset-p (ch odd-charset)
31 "Return t if `prin1-to-string' prints CH with the `charset' property. 71 "Return t if print function being tested prints CH with the `charset' property.
32CH is propertized with a `charset' value according to 72CH is propertized with a `charset' value according to
33ODD-CHARSET: if nil, then use the one returned by `char-charset', 73ODD-CHARSET: if nil, then use the one returned by `char-charset',
34otherwise, use a different charset." 74otherwise, use a different charset."
35 (integerp 75 (integerp
36 (string-match 76 (string-match
37 "charset" 77 "charset"
38 (prin1-to-string 78 (print-tests--prin1-to-string
39 (propertize (string ch) 79 (propertize (string ch)
40 'charset 80 'charset
41 (if odd-charset 81 (if odd-charset
42 (cl-find (char-charset ch) charset-list :test-not #'eq) 82 (cl-find (char-charset ch) charset-list :test-not #'eq)
43 (char-charset ch))))))) 83 (char-charset ch)))))))
44 84
45(ert-deftest print-charset-text-property-nil () 85(print-tests--deftest print-charset-text-property-nil ()
86 :expected-result (if (eq (symbol-function #'print-tests--prin1-to-string)
87 #'cl-prin1-to-string) :failed :passed)
46 (let ((print-charset-text-property nil)) 88 (let ((print-charset-text-property nil))
47 (should-not (print-tests--prints-with-charset-p ?\xf6 t)) ; Bug#31376. 89 (should-not (print-tests--prints-with-charset-p ?\xf6 t)) ; Bug#31376.
48 (should-not (print-tests--prints-with-charset-p ?a t)) 90 (should-not (print-tests--prints-with-charset-p ?a t))
49 (should-not (print-tests--prints-with-charset-p ?\xf6 nil)) 91 (should-not (print-tests--prints-with-charset-p ?\xf6 nil))
50 (should-not (print-tests--prints-with-charset-p ?a nil)))) 92 (should-not (print-tests--prints-with-charset-p ?a nil))))
51 93
52(ert-deftest print-charset-text-property-default () 94(print-tests--deftest print-charset-text-property-default ()
95 :expected-result (if (eq (symbol-function #'print-tests--prin1-to-string)
96 #'cl-prin1-to-string) :failed :passed)
53 (let ((print-charset-text-property 'default)) 97 (let ((print-charset-text-property 'default))
54 (should (print-tests--prints-with-charset-p ?\xf6 t)) 98 (should (print-tests--prints-with-charset-p ?\xf6 t))
55 (should-not (print-tests--prints-with-charset-p ?a t)) 99 (should-not (print-tests--prints-with-charset-p ?a t))
56 (should-not (print-tests--prints-with-charset-p ?\xf6 nil)) 100 (should-not (print-tests--prints-with-charset-p ?\xf6 nil))
57 (should-not (print-tests--prints-with-charset-p ?a nil)))) 101 (should-not (print-tests--prints-with-charset-p ?a nil))))
58 102
59(ert-deftest print-charset-text-property-t () 103(print-tests--deftest print-charset-text-property-t ()
60 (let ((print-charset-text-property t)) 104 (let ((print-charset-text-property t))
61 (should (print-tests--prints-with-charset-p ?\xf6 t)) 105 (should (print-tests--prints-with-charset-p ?\xf6 t))
62 (should (print-tests--prints-with-charset-p ?a t)) 106 (should (print-tests--prints-with-charset-p ?a t))
@@ -94,7 +138,7 @@ otherwise, use a different charset."
94 (buffer-string)) 138 (buffer-string))
95 "--------\n")))) 139 "--------\n"))))
96 140
97(ert-deftest print-read-roundtrip () 141(print-tests--deftest print-read-roundtrip ()
98 (let ((syms (list '## '& '* '+ '- '/ '0E '0e '< '= '> 'E 'E0 'NaN '\" 142 (let ((syms (list '## '& '* '+ '- '/ '0E '0e '< '= '> 'E 'E0 'NaN '\"
99 '\# '\#x0 '\' '\'\' '\( '\) '\+00 '\, '\-0 '\. '\.0 143 '\# '\#x0 '\' '\'\' '\( '\) '\+00 '\, '\-0 '\. '\.0
100 '\0 '\0.0 '\0E0 '\0e0 '\1E+ '\1E+NaN '\1e+ '\1e+NaN 144 '\0 '\0.0 '\0E0 '\0e0 '\1E+ '\1E+NaN '\1e+ '\1e+NaN
@@ -105,16 +149,207 @@ otherwise, use a different charset."
105 (intern "\N{ZERO WIDTH SPACE}") 149 (intern "\N{ZERO WIDTH SPACE}")
106 (intern "\0")))) 150 (intern "\0"))))
107 (dolist (sym syms) 151 (dolist (sym syms)
108 (should (eq (read (prin1-to-string sym)) sym)) 152 (should (eq (read (print-tests--prin1-to-string sym)) sym))
109 (dolist (sym1 syms) 153 (dolist (sym1 syms)
110 (let ((sym2 (intern (concat (symbol-name sym) (symbol-name sym1))))) 154 (let ((sym2 (intern (concat (symbol-name sym) (symbol-name sym1)))))
111 (should (eq (read (prin1-to-string sym2)) sym2))))))) 155 (should (eq (read (print-tests--prin1-to-string sym2)) sym2)))))))
112 156
113(ert-deftest print-bignum () 157(print-tests--deftest print-bignum ()
114 (let* ((str "999999999999999999999999999999999") 158 (let* ((str "999999999999999999999999999999999")
115 (val (read str))) 159 (val (read str)))
116 (should (> val most-positive-fixnum)) 160 (should (> val most-positive-fixnum))
117 (should (equal (prin1-to-string val) str)))) 161 (should (equal (print-tests--prin1-to-string val) str))))
162
163(print-tests--deftest print-tests-print-gensym ()
164 "Printing observes `print-gensym'."
165 (let* ((sym1 (gensym))
166 (syms (list sym1 (gensym "x") (make-symbol "y") sym1)))
167 (let* ((print-circle nil)
168 (printed-with (let ((print-gensym t))
169 (print-tests--prin1-to-string syms)))
170 (printed-without (let ((print-gensym nil))
171 (print-tests--prin1-to-string syms))))
172 (should (string-match
173 "(#:\\(g[[:digit:]]+\\) #:x[[:digit:]]+ #:y #:\\(g[[:digit:]]+\\))$"
174 printed-with))
175 (should (string= (match-string 1 printed-with)
176 (match-string 2 printed-with)))
177 (should (string-match "(g[[:digit:]]+ x[[:digit:]]+ y g[[:digit:]]+)$"
178 printed-without)))
179 (let* ((print-circle t)
180 (printed-with (let ((print-gensym t))
181 (print-tests--prin1-to-string syms)))
182 (printed-without (let ((print-gensym nil))
183 (print-tests--prin1-to-string syms))))
184 (should (string-match "(#1=#:g[[:digit:]]+ #:x[[:digit:]]+ #:y #1#)$"
185 printed-with))
186 (should (string-match "(g[[:digit:]]+ x[[:digit:]]+ y g[[:digit:]]+)$"
187 printed-without)))))
188
189(print-tests--deftest print-tests-continuous-numbering ()
190 "Printing observes `print-continuous-numbering'."
191 ;; cl-print does not support print-continuous-numbering.
192 :expected-result (if (eq (symbol-function #'print-tests--prin1-to-string)
193 #'cl-prin1-to-string) :failed :passed)
194 (let* ((x (list 1))
195 (y "hello")
196 (g (gensym))
197 (g2 (gensym))
198 (print-circle t)
199 (print-gensym t))
200 (let ((print-continuous-numbering t)
201 (print-number-table nil))
202 (should (string-match
203 "(#1=(1) #1# #2=\"hello\" #2#)(#3=#:g[[:digit:]]+ #3#)(#1# #2# #3#)#2#$"
204 (mapconcat #'print-tests--prin1-to-string `((,x ,x ,y ,y) (,g ,g) (,x ,y ,g) ,y) ""))))
205
206 ;; This is the special case for byte-compile-output-docform
207 ;; mentioned in a comment in print_preprocess. When
208 ;; print-continuous-numbering and print-circle and print-gensym
209 ;; are all non-nil, print all gensyms with numbers even if they
210 ;; only occur once.
211 (let ((print-continuous-numbering t)
212 (print-number-table nil))
213 (should (string-match
214 "(#1=#:g[[:digit:]]+ #2=#:g[[:digit:]]+)$"
215 (print-tests--prin1-to-string (list g g2)))))))
216
217(cl-defstruct print--test a b)
218
219(print-tests--deftest print-tests-1 ()
220 "Test print code."
221 (let ((x (make-print--test :a 1 :b 2))
222 (rec (cond
223 ((eq (symbol-function #'print-tests--prin1-to-string) 'prin1-to-string)
224 "#s(print--test 1 2)")
225 ((eq (symbol-function #'print-tests--prin1-to-string) 'cl-prin1-to-string)
226 "#s(print--test :a 1 :b 2)")
227 (t (cl-assert nil)))))
228
229 (let ((print-circle nil))
230 (should (equal (print-tests--prin1-to-string `((x . ,x) (y . ,x)))
231 (format "((x . %s) (y . %s))" rec rec))))
232 (let ((print-circle t))
233 (should (equal (print-tests--prin1-to-string `((x . ,x) (y . ,x)))
234 (format "((x . #1=%s) (y . #1#))" rec))))))
235
236(print-tests--deftest print-tests-2 ()
237 (let ((x (record 'foo 1 2 3)))
238 (should (equal
239 x
240 (car (read-from-string (with-output-to-string (prin1 x))))))
241 (let ((print-circle t))
242 (should (string-match
243 "\\`(#1=#s(foo 1 2 3) #1#)\\'"
244 (print-tests--prin1-to-string (list x x)))))))
245
246(cl-defstruct (print-tests-struct
247 (:constructor print-tests-con))
248 a b c d e)
249
250(print-tests--deftest print-tests-3 ()
251 "Printing observes `print-length'."
252 (let ((long-list (make-list 5 'a))
253 (long-vec (make-vector 5 'b))
254 ;; (long-struct (print-tests-con))
255 ;; (long-string (make-string 5 ?a))
256 (print-length 4))
257 (should (equal "(a a a a ...)" (print-tests--prin1-to-string long-list)))
258 (should (equal "[b b b b ...]" (print-tests--prin1-to-string long-vec)))
259 ;; This one only prints 3 nils. Should it print 4?
260 ;; (should (equal "#s(print-tests-struct nil nil nil nil ...)"
261 ;; (print-tests--prin1-to-string long-struct)))
262 ;; This one is only supported by cl-print
263 ;; (should (equal "\"aaaa...\"" (cl-print-tests--prin1-to-string long-string)))
264 ))
265
266(print-tests--deftest print-tests-4 ()
267 "Printing observes `print-level'."
268 (let* ((deep-list '(a (b (c (d (e))))))
269 (buried-vector '(a (b (c (d [e])))))
270 (deep-struct (print-tests-con))
271 (buried-struct `(a (b (c (d ,deep-struct)))))
272 (buried-string '(a (b (c (d #("hello" 0 5 (print-test t)))))))
273 (buried-simple-string '(a (b (c (d "hello")))))
274 (print-level 4))
275 (setf (print-tests-struct-a deep-struct) deep-list)
276 (should (equal "(a (b (c (d ...))))" (print-tests--prin1-to-string deep-list)))
277 (should (equal "(a (b (c (d \"hello\"))))"
278 (print-tests--prin1-to-string buried-simple-string)))
279 (cond
280 ((eq (symbol-function #'print-tests--prin1-to-string) #'prin1-to-string)
281 (should (equal "(a (b (c (d [e]))))" (print-tests--prin1-to-string buried-vector)))
282 (should (equal "(a (b (c (d #s(print-tests-struct ... nil nil nil nil)))))"
283 (print-tests--prin1-to-string buried-struct)))
284 (should (equal "(a (b (c (d #(\"hello\" 0 5 ...)))))"
285 (print-tests--prin1-to-string buried-string)))
286 (should (equal "#s(print-tests-struct (a (b (c ...))) nil nil nil nil)"
287 (print-tests--prin1-to-string deep-struct))))
288
289 ((eq (symbol-function #'print-tests--prin1-to-string) #'cl-prin1-to-string)
290 (should (equal "(a (b (c (d ...))))" (print-tests--prin1-to-string buried-vector)))
291 (should (equal "(a (b (c (d ...))))" (print-tests--prin1-to-string buried-struct)))
292 (should (equal "(a (b (c (d ...))))" (print-tests--prin1-to-string buried-string)))
293 (should (equal "#s(print-tests-struct :a (a (b (c ...))) :b nil :c nil :d nil :e nil)"
294 (print-tests--prin1-to-string deep-struct))))
295 (t (cl-assert nil)))))
296
297(print-tests--deftest print-tests-5 ()
298 "Printing observes `print-quoted'."
299 (let ((quoted-stuff '('a #'b `(,c ,@d))))
300 (let ((print-quoted t))
301 (should (equal "('a #'b `(,c ,@d))"
302 (print-tests--prin1-to-string quoted-stuff))))
303 (let ((print-quoted nil))
304 (should (equal "((quote a) (function b) (\\` ((\\, c) (\\,@ d))))"
305 (print-tests--prin1-to-string quoted-stuff))))))
306
307(print-tests--deftest print-tests-strings ()
308 "Can print strings and propertized strings."
309 (let* ((str1 "abcdefghij")
310 (str2 #("abcdefghij" 3 6 (bold t) 7 9 (italic t)))
311 (str3 #("abcdefghij" 0 10 (test t)))
312 (obj '(a b))
313 ;; Since the byte compiler reuses string literals,
314 ;; and the put-text-property call is destructive, use
315 ;; copy-sequence to make a new string.
316 (str4 (copy-sequence "abcdefghij")))
317 (put-text-property 0 5 'test obj str4)
318 (put-text-property 7 10 'test obj str4)
319
320 (should (equal "\"abcdefghij\"" (print-tests--prin1-to-string str1)))
321 (should (equal "#(\"abcdefghij\" 3 6 (bold t) 7 9 (italic t))"
322 (print-tests--prin1-to-string str2)))
323 (should (equal "#(\"abcdefghij\" 0 10 (test t))"
324 (print-tests--prin1-to-string str3)))
325 (let ((print-circle nil))
326 (should
327 (equal
328 "#(\"abcdefghij\" 0 5 (test (a b)) 7 10 (test (a b)))"
329 (print-tests--prin1-to-string str4))))
330 (let ((print-circle t))
331 (should
332 (equal
333 "#(\"abcdefghij\" 0 5 (test #1=(a b)) 7 10 (test #1#))"
334 (print-tests--prin1-to-string str4))))))
335
336(print-tests--deftest print-circle ()
337 (let ((x '(#1=(a . #1#) #1#)))
338 (let ((print-circle nil))
339 (should (string-match "\\`((a . #[0-9]) (a . #[0-9]))\\'"
340 (print-tests--prin1-to-string x))))
341 (let ((print-circle t))
342 (should (equal "(#1=(a . #1#) #1#)" (print-tests--prin1-to-string x))))))
343
344(print-tests--deftest print-circle-2 ()
345 ;; Bug#31146.
346 (let ((x '(0 . #1=(0 . #1#))))
347 (let ((print-circle nil))
348 (should (string-match "\\`(0 0 . #[0-9])\\'"
349 (print-tests--prin1-to-string x))))
350 (let ((print-circle t))
351 (should (equal "(0 . #1=(0 . #1#))" (print-tests--prin1-to-string x))))))
352
118 353
119(provide 'print-tests) 354(provide 'print-tests)
120;;; print-tests.el ends here 355;;; print-tests.el ends here