Let ‘cl-defun’ handle keyword arguments.
[apt-sources-list.git] / apt-sources-list-test.el
1 ;;; apt-sources-list-test.el --- Tests for apt-sources-list -*- lexical-binding: t; -*-
2 ;;
3 ;; Copyright (C) 2017 Joe Wreschnig
4 ;;
5 ;; Author: Joe Wreschnig
6 ;;
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11
12
13 ;;; Commentary:
14 ;;
15 ;; This file contains test cases for apt-sources-list. Unless you’re
16 ;; hacking on it you shouldn’t need to edit or run this file.
17
18
19 ;;; Code:
20
21 (require 'apt-sources-list)
22 (require 'ert)
23
24 (defmacro with-temp-windowed-buffer (&rest body)
25 "Like ‘with-temp-buffer’ but give the buffer a window during BODY.
26
27 This is necessary if you want to simulate sending keys."
28 `(with-temp-buffer
29 (set-window-buffer (selected-window) (current-buffer) t)
30 ,@body))
31
32 (defmacro with-apt-sources-list (sources &rest body)
33 "With buffer contents SOURCES, run BODY forms in ‘apt-sources-list-mode’."
34 (declare (indent 1))
35 `(with-temp-windowed-buffer
36 (apt-sources-list-mode)
37 (insert ,sources)
38 (goto-char (point-min))
39 ,@body))
40
41 (defmacro should-equal (expected actual)
42 "Assert that EXPECTED is ‘equal’ to ACTUAL."
43 `(should (equal ,expected ,actual)))
44
45 (defmacro should-eq (expected actual)
46 "Assert that EXPECTED is ‘eq’ to ACTUAL."
47 `(should (eq ,expected ,actual)))
48
49 (defmacro should-equal-buffer (contents)
50 "Assert that the current buffer’s contents equals CONTENTS."
51 `(should-equal (buffer-string) ,contents))
52
53 (defmacro should-use-face (face)
54 "Assert that the current point’s face is FACE."
55 `(should-eq (get-text-property (point) 'face) ,face))
56
57 (defmacro should-be-at-line (lineno)
58 "Assert that the point is on line number LINENO."
59 `(should-equal ,lineno (line-number-at-pos)))
60
61 (defun type (keys)
62 "Simulate typing KEYS."
63 (execute-kbd-macro (kbd keys)))
64
65 (ert-deftest apt-sources-list-test-invalid ()
66 (with-apt-sources-list "invalid"
67 (should-error (apt-sources-list-change-type)
68 :type 'apt-sources-list-not-found)
69 (should-error (apt-sources-list-change-uri "http://foo")
70 :type 'apt-sources-list-not-found)))
71
72 (ert-deftest apt-sources-list-test-insert ()
73 (with-apt-sources-list ""
74 (type "C-c C-i RET deb.test RET RET RET")
75 (should-equal-buffer "deb https://deb.test stable main"))
76
77 (with-apt-sources-list ""
78 (type "C-c C-i example RET deb.test RET RET RET")
79 (should-equal-buffer "# example\ndeb https://deb.test stable main"))
80
81 (with-apt-sources-list ""
82 (type "C-c C-i RET deb.test RET M-0 C-k path/ RET")
83 (should-equal-buffer "deb https://deb.test path/"))
84
85 (with-apt-sources-list ""
86 (type "C-u C-c C-i RET -src RET arch=amd64 RET deb.test RET RET")
87 (should-equal-buffer
88 "deb-src [arch=amd64] https://deb.test stable main")))
89
90 (ert-deftest apt-sources-list-test-replicate ()
91 (with-apt-sources-list "deb http://deb.test/debian stable main"
92 (type "C-c C-r")
93 (should-equal-buffer
94 "deb http://deb.test/debian stable main
95 deb-src http://deb.test/debian stable main")))
96
97 (ert-deftest apt-sources-list-test-change-type ()
98 (with-apt-sources-list "deb http://deb.test/debian stable main"
99 (type "C-c C-t")
100 (should-equal-buffer "deb-src http://deb.test/debian stable main")
101 (type "C-c C-t")
102 (should-equal-buffer "deb http://deb.test/debian stable main")))
103
104 (ert-deftest apt-sources-list-test-change-options ()
105 (with-apt-sources-list "deb http://deb.test/ stable main"
106 (type "C-c C-o arch=amd64 RET")
107 (should-equal-buffer "deb [arch=amd64] http://deb.test/ stable main")
108 (type "C-c C-o SPC lang=en RET")
109 (should-equal-buffer
110 "deb [arch=amd64 lang=en] http://deb.test/ stable main")
111 (type "C-c C-o M-0 C-k RET")
112 (should-equal-buffer "deb http://deb.test/ stable main")))
113
114 (ert-deftest apt-sources-list-test-change-url ()
115 (with-apt-sources-list "deb http://deb.test/debian stable main"
116 (type "C-c C-u M-0 C-k ftp://deb2.test/debian2 RET")
117 (should-equal-buffer "deb ftp://deb2.test/debian2 stable main")))
118
119 (ert-deftest apt-sources-list-test-change-suite ()
120 (with-apt-sources-list "deb http://deb.test/debian stable main # foo"
121 (type "C-c C-s path/ RET")
122 (should-equal-buffer "deb http://deb.test/debian path/ # foo")
123 (type "C-c C-s unstable RET M-0 C-k xxx RET")
124 (should-equal-buffer "deb http://deb.test/debian unstable xxx # foo")
125 (type "C-c C-s stable RET")
126 (should-equal-buffer "deb http://deb.test/debian stable xxx # foo")))
127
128 (ert-deftest apt-sources-list-test-change-components ()
129 (with-apt-sources-list "deb http://deb.test/debian stable main # foo"
130 (type "C-c C-c M-0 C-k a SPC b")
131 (should-equal-buffer "deb http://deb.test/debian stable a b # foo")))
132
133 (ert-deftest apt-sources-list-test-motion ()
134 (with-apt-sources-list
135 "deb [arch=armel] http://deb.test/debian stable main
136 # comment
137 deb invalid line
138 deb http://deb.test/debian stable main"
139 (type "C-M-n")
140 (should-be-at-line 4)
141 (type "C-u - 1 C-M-n")
142 (should-be-at-line 1)
143 (type "C-u - 1 C-M-p")
144 (should-be-at-line 4)
145 (type "C-M-p")
146 (should-be-at-line 1)
147
148 (should-error (type "C-M-p"))
149 (should-error (type "C-u 2 C-M-n"))))
150
151 (ert-deftest apt-sources-list-test-font-lock ()
152 (with-apt-sources-list
153 "deb [arch=armel] http://deb.test/debian stable main # bar"
154 (font-lock-ensure)
155 (should-use-face 'apt-sources-list-type)
156 (search-forward "arch")
157 (should-use-face 'apt-sources-list-options)
158 (search-forward "http")
159 (should-use-face 'apt-sources-list-uri)
160 (search-forward "stabl")
161 (should-use-face 'apt-sources-list-suite)
162 (search-forward "mai")
163 (should-use-face 'apt-sources-list-components)
164 (search-forward "#")
165 (should-use-face 'font-lock-comment-delimiter-face)
166 (search-forward "b")
167 (should-use-face 'font-lock-comment-face)))
168
169
170 ;;; apt-sources-list-test.el ends here