summaryrefslogtreecommitdiff
path: root/test/lisp/emacs-lisp/pp-tests.el
blob: 7606183d64522cae13a30bb80a9fef1c2ab091a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
;;; pp-tests.el --- Test suite for pretty printer.  -*- lexical-binding: t -*-

;; Copyright (C) 2017-2024 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'pp)
(require 'ert-x)

(ert-deftest pp-print-quote ()
  (should (string= (pp-to-string 'quote) "quote\n"))
  (should (string= (pp-to-string ''quote) "'quote\n"))
  (should (string= (pp-to-string '('a 'b)) "('a 'b)\n"))
  (should (string= (pp-to-string '(''quote 'quote)) "(''quote 'quote)\n"))
  (should (string= (pp-to-string '(quote)) "(quote)\n"))
  (should (string= (pp-to-string '(quote . quote)) "(quote . quote)\n"))
  (should (string= (pp-to-string '(quote a b)) "(quote a b)\n"))
  (should (string= (pp-to-string '(quotefoo)) "(quotefoo)\n"))
  (should (string= (pp-to-string '(a b)) "(a b)\n")))

(ert-deftest test-indentation ()
  (ert-test-erts-file (ert-resource-file "code-formats.erts")))

(defun pp-tests--dimensions ()
  (save-excursion
    (let ((width 0)
          (height 0))
      (goto-char (point-min))
      (while (not (eobp))
        (end-of-line)
        (setq height (1+ height))
        (setq width (max width (current-column)))
        (forward-char 1))
      (cons width height))))

(ert-deftest pp-tests--cut-before ()
  (with-temp-buffer
    (lisp-data-mode)
    (pp '(1 (quite-a-long-package-name
             . [(0 10 0) ((avy (0 5 0))) "Quickly switch windows." tar
                ((:url . "https://github.com/abo-abo/ace-window")
                 (:maintainer "Oleh Krehel" . "ohwoeowho@gmail.com")
                 (:authors ("Oleh Krehel" . "ohwoeowho@gmail.com"))
                 (:keywords "window" "location"))]))
        (current-buffer))
    ;; (message "Filled:\n%s" (buffer-string))
    (let ((dimensions (pp-tests--dimensions)))
      (should (< (car dimensions) 80))
      (should (< (cdr dimensions) 8)))
    (goto-char (point-min))
    (while (search-forward "." nil t)
      (should (not (eolp))))))

(ert-deftest pp-tests--sanity ()
  (with-temp-buffer
    (lisp-data-mode)
    (let ((testdata "(a b c #1=#[0 \"\" [] 0] #s(foo #1# bar))"))
      (let ((res (car (read-from-string testdata))))
      (dotimes (i (length testdata))
        (erase-buffer)
        (insert testdata)
        (let ((fill-column i))
          (pp-fill (point-min) (point-max))
          (goto-char (point-min))
          (condition-case err
              (should (equal (read (current-buffer)) res))
            (invalid-read-syntax
             (message "Invalid fill result with i=%d:\n%s"
                      i (buffer-string))
             (signal (car err) (cdr err))
             ))))))))

;;; pp-tests.el ends here.