Merge branch 'emacs-25' of git.sv.gnu.org:/srv/git/emacs into emacs-25
[emacs.git] / test / automated / tabulated-list-test.el
blob0fb8dee7fd137a14b5193ca6196432431e842dde
1 ;;; tabulated-list-test.el --- Tests for emacs-lisp/tabulated-list.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (require 'tabulated-list)
25 (require 'ert)
27 (defconst tabulated-list--test-entries
28 '(("zzzz-game" ["zzzz-game" "zzzz-game" "2113" "installed" " play zzzz in Emacs"])
29 ("4clojure" ["4clojure" "4clojure" "1507" "obsolete" " Open and evaluate 4clojure.com questions"])
30 ("abc-mode" ["abc-mode" "abc-mode" "944" "available" " Major mode for editing abc music files"])
31 ("mode" ["mode" "mode" "1128" "installed" " A simple mode for editing Actionscript 3 files"])))
33 (defun tabulated-list--test-sort-car (a b)
34 (string< (car a) (car b)))
36 (defconst tabulated-list--test-format
37 [("name" 10 tabulated-list--test-sort-car)
38 ("name-2" 10 t)
39 ("Version" 9 nil)
40 ("Status" 10 )
41 ("Description" 0 nil)])
43 (defmacro tabulated-list--test-with-buffer (&rest body)
44 `(with-temp-buffer
45 (tabulated-list-mode)
46 (setq tabulated-list-entries (copy-alist tabulated-list--test-entries))
47 (setq tabulated-list-format tabulated-list--test-format)
48 (setq tabulated-list-padding 7)
49 (tabulated-list-init-header)
50 (tabulated-list-print)
51 ,@body))
54 ;;; Tests
55 (ert-deftest tabulated-list-print ()
56 (tabulated-list--test-with-buffer
57 ;; Basic printing.
58 (should (string= (buffer-substring-no-properties (point-min) (point-max))
59 " zzzz-game zzzz-game 2113 installed play zzzz in Emacs
60 4clojure 4clojure 1507 obsolete Open and evaluate 4clojure.com questions
61 abc-mode abc-mode 944 available Major mode for editing abc music files
62 mode mode 1128 installed A simple mode for editing Actionscript 3 files\n"))
63 ;; Preserve position.
64 (forward-line 3)
65 (let ((pos (thing-at-point 'line)))
66 (pop tabulated-list-entries)
67 (tabulated-list-print t)
68 (should (equal (thing-at-point 'line) pos))
69 (should (string= (buffer-substring-no-properties (point-min) (point-max))
70 " 4clojure 4clojure 1507 obsolete Open and evaluate 4clojure.com questions
71 abc-mode abc-mode 944 available Major mode for editing abc music files
72 mode mode 1128 installed A simple mode for editing Actionscript 3 files\n"))
73 ;; Check the UPDATE argument
74 (pop tabulated-list-entries)
75 (setf (cdr (car tabulated-list-entries)) (list ["x" "x" "944" "available" " XX"]))
76 (tabulated-list-print t t)
77 (should (string= (buffer-substring-no-properties (point-min) (point-max))
78 " x x 944 available XX
79 mode mode 1128 installed A simple mode for editing Actionscript 3 files\n"))
80 (should (equal (thing-at-point 'line) pos)))))
82 (ert-deftest tabulated-list-sort ()
83 (tabulated-list--test-with-buffer
84 ;; Basic sorting
85 (goto-char (point-min))
86 (skip-chars-forward "[:blank:]")
87 (tabulated-list-sort)
88 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
89 (should (string= text " 4clojure 4clojure 1507 obsolete Open and evaluate 4clojure.com questions
90 abc-mode abc-mode 944 available Major mode for editing abc music files
91 mode mode 1128 installed A simple mode for editing Actionscript 3 files
92 zzzz-game zzzz-game 2113 installed play zzzz in Emacs\n"))
94 (skip-chars-forward "^[:blank:]")
95 (skip-chars-forward "[:blank:]")
96 (should (equal (get-text-property (point) 'tabulated-list-column-name)
97 "name-2"))
98 (tabulated-list-sort)
99 ;; Check a `t' as the sorting predicate.
100 (should (string= text (buffer-substring-no-properties (point-min) (point-max))))
101 ;; Invert.
102 (tabulated-list-sort 1)
103 (should (string= (buffer-substring-no-properties (point-min) (point-max))
104 " zzzz-game zzzz-game 2113 installed play zzzz in Emacs
105 mode mode 1128 installed A simple mode for editing Actionscript 3 files
106 abc-mode abc-mode 944 available Major mode for editing abc music files
107 4clojure 4clojure 1507 obsolete Open and evaluate 4clojure.com questions\n"))
108 ;; Again
109 (tabulated-list-sort 1)
110 (should (string= text (buffer-substring-no-properties (point-min) (point-max)))))
111 ;; Check that you can't sort some cols.
112 (skip-chars-forward "^[:blank:]")
113 (skip-chars-forward "[:blank:]")
114 (should-error (tabulated-list-sort) :type 'user-error)
115 (should-error (tabulated-list-sort 4) :type 'user-error)))
117 (provide 'tabulated-list-test)
118 ;;; tabulated-list-test.el ends here