aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorF. Jason Park2021-07-12 03:44:28 -0700
committerAmin Bandali2022-11-23 19:56:31 -0500
commit4c4936fab2ecd97ff6e03e5cfe12def4626c718b (patch)
tree573e992298f17fd23709fddafb2979a49646b2d9 /test
parentdc6ff142bc1c1a8596436e08ddbccb39d8fdcf39 (diff)
downloademacs-4c4936fab2ecd97ff6e03e5cfe12def4626c718b.tar.gz
emacs-4c4936fab2ecd97ff6e03e5cfe12def4626c718b.zip
Support local ERC modules in erc-mode buffers
* doc/misc/erc.texi: Mention local modules in Modules chapter. * etc/ERC-NEWS: Mention changes to `erc-update-modules'. * lisp/erc/erc.el (erc-migrate-modules): Add some missing mappings. (erc-modules): When a user removes a module, disable it and kill its local variable in all ERC buffers. (erc-update-modules): Move body of `erc-update-modules' to new internal function. (erc--update-modules): Add new function, a renamed and slightly modified version of `erc-update-modules'. Specifically, change return value from nil to a list of minor-mode commands for local modules. Use `custom-variable-p' to detect flavor. (erc--merge-local-modes): Add helper for finding local modules already active as minor modes in an ERC buffer. (erc-open): Replace `erc-update-modules' with `erc--update-modules'. Defer enabling of local modules via `erc--update-modules' until after buffer is initialized with other local vars. Also defer major-mode hooks so they can detect things like whether the buffer is a server or target buffer. Also ensure local module setup code can detect when `erc-open' was called with a non-nil `erc--server-reconnecting'. * lisp/erc/erc-common.el (erc--module-name-migrations, erc--features-to-modules, erc--modules-to-features): Add alists of old-to-new module names to support module-name migrations. (erc--assemble-toggle): Add new helper for constructing mode toggles, like `erc-sasl-enable'. (define-erc-modules): Defer to `erc--assemble-toggle' to create toggle commands. (erc--normalize-module-symbol): Add helper for `erc-migrate-modules'. * lisp/erc/erc-goodies.el: Require cl-lib. * test/lisp/erc/erc-tests.el (erc-migrate-modules, erc--update-modules): Add rudimentary unit tests asserting correct module-name mappings. (erc--merge-local-modes): Add test for helper. (define-erc-module--global, define-erc-module--local): Add tests asserting module-creation macro. (Bug#57955.)
Diffstat (limited to 'test')
-rw-r--r--test/lisp/erc/erc-tests.el156
1 files changed, 156 insertions, 0 deletions
diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el
index ff5d8026973..b185d850a6f 100644
--- a/test/lisp/erc/erc-tests.el
+++ b/test/lisp/erc/erc-tests.el
@@ -1178,4 +1178,160 @@
1178 (kill-buffer "baznet") 1178 (kill-buffer "baznet")
1179 (kill-buffer "#chan"))) 1179 (kill-buffer "#chan")))
1180 1180
1181(ert-deftest erc-migrate-modules ()
1182 (should (equal (erc-migrate-modules '(autojoin timestamp button))
1183 '(autojoin stamp button)))
1184 ;; Default unchanged
1185 (should (equal (erc-migrate-modules erc-modules) erc-modules)))
1186
1187(ert-deftest erc--update-modules ()
1188 (let (calls
1189 erc-modules
1190 erc-kill-channel-hook erc-kill-server-hook erc-kill-buffer-hook)
1191 (cl-letf (((symbol-function 'require)
1192 (lambda (s &rest _) (push s calls)))
1193
1194 ;; Local modules
1195 ((symbol-function 'erc-fake-bar-mode)
1196 (lambda (n) (push (cons 'fake-bar n) calls)))
1197
1198 ;; Global modules
1199 ((symbol-function 'erc-fake-foo-mode)
1200 (lambda (n) (push (cons 'fake-foo n) calls)))
1201 ((get 'erc-fake-foo-mode 'standard-value) 'ignore)
1202 ((symbol-function 'erc-autojoin-mode)
1203 (lambda (n) (push (cons 'autojoin n) calls)))
1204 ((get 'erc-autojoin-mode 'standard-value) 'ignore)
1205 ((symbol-function 'erc-networks-mode)
1206 (lambda (n) (push (cons 'networks n) calls)))
1207 ((get 'erc-networks-mode 'standard-value) 'ignore)
1208 ((symbol-function 'erc-completion-mode)
1209 (lambda (n) (push (cons 'completion n) calls)))
1210 ((get 'erc-completion-mode 'standard-value) 'ignore))
1211
1212 (ert-info ("Local modules")
1213 (setq erc-modules '(fake-foo fake-bar))
1214 (should (equal (erc--update-modules) '(erc-fake-bar-mode)))
1215 ;; Bar the feature is still required but the mode is not activated
1216 (should (equal (nreverse calls)
1217 '(erc-fake-foo (fake-foo . 1) erc-fake-bar)))
1218 (setq calls nil))
1219
1220 (ert-info ("Module name overrides")
1221 (setq erc-modules '(completion autojoin networks))
1222 (should-not (erc--update-modules)) ; no locals
1223 (should (equal (nreverse calls) '( erc-pcomplete (completion . 1)
1224 erc-join (autojoin . 1)
1225 erc-networks (networks . 1))))
1226 (setq calls nil)))))
1227
1228(ert-deftest erc--merge-local-modes ()
1229
1230 (ert-info ("No existing modes")
1231 (let ((old '((a) (b . t)))
1232 (new '(erc-c-mode erc-d-mode)))
1233 (should (equal (erc--merge-local-modes new old)
1234 '((erc-c-mode erc-d-mode))))))
1235
1236 (ert-info ("Active existing added, inactive existing removed, deduped")
1237 (let ((old '((a) (erc-b-mode) (c . t) (erc-d-mode . t) (erc-e-mode . t)))
1238 (new '(erc-b-mode erc-d-mode)))
1239 (should (equal (erc--merge-local-modes new old)
1240 '((erc-d-mode erc-e-mode) . (erc-b-mode)))))))
1241
1242(ert-deftest define-erc-module--global ()
1243 (let ((global-module '(define-erc-module mname malias
1244 "Some docstring"
1245 ((ignore a) (ignore b))
1246 ((ignore c) (ignore d)))))
1247
1248 (should (equal (macroexpand global-module)
1249 `(progn
1250
1251 (define-minor-mode erc-mname-mode
1252 "Toggle ERC mname mode.
1253With a prefix argument ARG, enable mname if ARG is positive,
1254and disable it otherwise. If called from Lisp, enable the mode
1255if ARG is omitted or nil.
1256Some docstring"
1257 :global t
1258 :group 'erc-mname
1259 (if erc-mname-mode
1260 (erc-mname-enable)
1261 (erc-mname-disable)))
1262
1263 (defun erc-mname-enable ()
1264 "Enable ERC mname mode."
1265 (interactive)
1266 (cl-pushnew 'mname erc-modules)
1267 (setq erc-mname-mode t)
1268 (ignore a) (ignore b))
1269
1270 (defun erc-mname-disable ()
1271 "Disable ERC mname mode."
1272 (interactive)
1273 (setq erc-modules (delq 'mname erc-modules))
1274 (setq erc-mname-mode nil)
1275 (ignore c) (ignore d))
1276
1277 (defalias 'erc-malias-mode #'erc-mname-mode)
1278
1279 (put 'erc-mname-mode 'definition-name 'mname)
1280 (put 'erc-mname-enable 'definition-name 'mname)
1281 (put 'erc-mname-disable 'definition-name 'mname))))))
1282
1283(ert-deftest define-erc-module--local ()
1284 (let* ((global-module '(define-erc-module mname malias
1285 "Some docstring"
1286 ((ignore a) (ignore b))
1287 ((ignore c) (ignore d))
1288 'local))
1289 (got (macroexpand global-module))
1290 (arg-en (cadr (nth 2 (nth 2 got))))
1291 (arg-dis (cadr (nth 2 (nth 3 got)))))
1292
1293 (should (equal got
1294 `(progn
1295 (define-minor-mode erc-mname-mode
1296 "Toggle ERC mname mode.
1297With a prefix argument ARG, enable mname if ARG is positive,
1298and disable it otherwise. If called from Lisp, enable the mode
1299if ARG is omitted or nil.
1300Some docstring"
1301 :global nil
1302 :group 'erc-mname
1303 (if erc-mname-mode
1304 (erc-mname-enable)
1305 (erc-mname-disable)))
1306
1307 (defun erc-mname-enable (&optional ,arg-en)
1308 "Enable ERC mname mode.
1309With ARG, do so in all buffers for the current connection."
1310 (interactive "p")
1311 (when (derived-mode-p 'erc-mode)
1312 (if ,arg-en
1313 (erc-with-all-buffers-of-server
1314 erc-server-process nil
1315 (erc-mname-enable))
1316 (setq erc-mname-mode t)
1317 (ignore a) (ignore b))))
1318
1319 (defun erc-mname-disable (&optional ,arg-dis)
1320 "Disable ERC mname mode.
1321With ARG, do so in all buffers for the current connection."
1322 (interactive "p")
1323 (when (derived-mode-p 'erc-mode)
1324 (if ,arg-dis
1325 (erc-with-all-buffers-of-server
1326 erc-server-process nil
1327 (erc-mname-disable))
1328 (setq erc-mname-mode nil)
1329 (ignore c) (ignore d))))
1330
1331 (defalias 'erc-malias-mode #'erc-mname-mode)
1332
1333 (put 'erc-mname-mode 'definition-name 'mname)
1334 (put 'erc-mname-enable 'definition-name 'mname)
1335 (put 'erc-mname-disable 'definition-name 'mname))))))
1336
1181;;; erc-tests.el ends here 1337;;; erc-tests.el ends here