aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Kangas2020-09-23 19:41:59 +0200
committerStefan Kangas2020-09-23 20:03:19 +0200
commit37a2a427e4afaab963ba9753d11d7b665ac67ea0 (patch)
treeb0cf8c8e25d1d5922c6148b801759ef6ff0c9a35
parent610b771d4a7fcb9d704bbd31032dc51009670e8f (diff)
downloademacs-37a2a427e4afaab963ba9753d11d7b665ac67ea0.tar.gz
emacs-37a2a427e4afaab963ba9753d11d7b665ac67ea0.zip
Convert some completion.el tests to ERT
* test/lisp/completion-tests.el: New file. * lisp/completion.el: Move commented out tests to completion-tests.el.
-rw-r--r--lisp/completion.el121
-rw-r--r--test/lisp/completion-tests.el170
2 files changed, 170 insertions, 121 deletions
diff --git a/lisp/completion.el b/lisp/completion.el
index ed13455b711..e4a004f1908 100644
--- a/lisp/completion.el
+++ b/lisp/completion.el
@@ -399,13 +399,6 @@ Used to decide whether to save completions.")
399 :up) 399 :up)
400 (t :neither)))))) 400 (t :neither))))))
401 401
402;; Tests -
403;; (cmpl-string-case-type "123ABCDEF456") --> :up
404;; (cmpl-string-case-type "123abcdef456") --> :down
405;; (cmpl-string-case-type "123aBcDeF456") --> :mixed
406;; (cmpl-string-case-type "123456") --> :neither
407;; (cmpl-string-case-type "Abcde123") --> :capitalized
408
409(defun cmpl-coerce-string-case (string case-type) 402(defun cmpl-coerce-string-case (string case-type)
410 (cond ((eq case-type :down) (downcase string)) 403 (cond ((eq case-type :down) (downcase string))
411 ((eq case-type :up) (upcase string)) 404 ((eq case-type :up) (upcase string))
@@ -424,12 +417,6 @@ Used to decide whether to save completions.")
424 ;; as is 417 ;; as is
425 string-to-coerce)))) 418 string-to-coerce))))
426 419
427;; Tests -
428;; (cmpl-merge-string-cases "AbCdEf456" "abc") --> AbCdEf456
429;; (cmpl-merge-string-cases "abcdef456" "ABC") --> ABCDEF456
430;; (cmpl-merge-string-cases "ABCDEF456" "Abc") --> Abcdef456
431;; (cmpl-merge-string-cases "ABCDEF456" "abc") --> abcdef456
432
433 420
434(defun cmpl-hours-since-origin () 421(defun cmpl-hours-since-origin ()
435 (floor (time-convert nil 'integer) 3600)) 422 (floor (time-convert nil 'integer) 3600))
@@ -1226,45 +1213,6 @@ String must be longer than `completion-prefix-min-length'."
1226 (set cmpl-db-prefix-symbol nil))))) 1213 (set cmpl-db-prefix-symbol nil)))))
1227 (error "Unknown completion `%s'" completion-string)))) 1214 (error "Unknown completion `%s'" completion-string))))
1228 1215
1229;; Tests --
1230;; - Add and Find -
1231;; (add-completion-to-head "banana") --> ("banana" 0 nil 0)
1232;; (find-exact-completion "banana") --> ("banana" 0 nil 0)
1233;; (find-exact-completion "bana") --> nil
1234;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...))
1235;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banana" ...))
1236;; (add-completion-to-head "banish") --> ("banish" 0 nil 0)
1237;; (find-exact-completion "banish") --> ("banish" 0 nil 0)
1238;; (car (find-cmpl-prefix-entry "ban")) --> (("banish" ...) ("banana" ...))
1239;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banana" ...))
1240;; (add-completion-to-head "banana") --> ("banana" 0 nil 0)
1241;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...) ("banish" ...))
1242;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banish" ...))
1243;;
1244;; - Deleting -
1245;; (add-completion-to-head "banner") --> ("banner" 0 nil 0)
1246;; (delete-completion "banner")
1247;; (find-exact-completion "banner") --> nil
1248;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...) ("banish" ...))
1249;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banish" ...))
1250;; (add-completion-to-head "banner") --> ("banner" 0 nil 0)
1251;; (delete-completion "banana")
1252;; (car (find-cmpl-prefix-entry "ban")) --> (("banner" ...) ("banish" ...))
1253;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banish" ...))
1254;; (delete-completion "banner")
1255;; (delete-completion "banish")
1256;; (find-cmpl-prefix-entry "ban") --> nil
1257;; (delete-completion "banner") --> error
1258;;
1259;; - Tail -
1260;; (add-completion-to-tail-if-new "banana") --> ("banana" 0 nil 0)
1261;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...))
1262;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banana" ...))
1263;; (add-completion-to-tail-if-new "banish") --> ("banish" 0 nil 0)
1264;; (car (find-cmpl-prefix-entry "ban")) -->(("banana" ...) ("banish" ...))
1265;; (cdr (find-cmpl-prefix-entry "ban")) -->(("banish" ...))
1266;;
1267
1268 1216
1269;;--------------------------------------------------------------------------- 1217;;---------------------------------------------------------------------------
1270;; Database Update :: Interface level routines 1218;; Database Update :: Interface level routines
@@ -1361,29 +1309,6 @@ Completions added this way will automatically be saved if
1361 (set-completion-num-uses entry 1) 1309 (set-completion-num-uses entry 1)
1362 (setq cmpl-completions-accepted-p t))))))) 1310 (setq cmpl-completions-accepted-p t)))))))
1363 1311
1364;; Tests --
1365;; - Add and Find -
1366;; (add-completion "banana" 5 10)
1367;; (find-exact-completion "banana") --> ("banana" 5 10 0)
1368;; (add-completion "banana" 6)
1369;; (find-exact-completion "banana") --> ("banana" 6 10 0)
1370;; (add-completion "banish")
1371;; (car (find-cmpl-prefix-entry "ban")) --> (("banish" ...) ("banana" ...))
1372;;
1373;; - Accepting -
1374;; (setq completion-to-accept "banana")
1375;; (accept-completion)
1376;; (find-exact-completion "banana") --> ("banana" 7 10)
1377;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...) ("banish" ...))
1378;; (setq completion-to-accept "banish")
1379;; (add-completion "banner")
1380;; (car (find-cmpl-prefix-entry "ban"))
1381;; --> (("banner" ...) ("banish" 1 ...) ("banana" 7 ...))
1382;;
1383;; - Deleting -
1384;; (kill-completion "banish")
1385;; (car (find-cmpl-prefix-entry "ban")) --> (("banner" ...) ("banana" ...))
1386
1387 1312
1388;;--------------------------------------------------------------------------- 1313;;---------------------------------------------------------------------------
1389;; Searching the database 1314;; Searching the database
@@ -1505,46 +1430,6 @@ If there are no more entries, try cdabbrev and then return only a string."
1505 ;; Completely unsuccessful, return nil 1430 ;; Completely unsuccessful, return nil
1506 )) 1431 ))
1507 1432
1508;; Tests --
1509;; - Add and Find -
1510;; (add-completion "banana")
1511;; (completion-search-reset "ban")
1512;; (completion-search-next 0) --> "banana"
1513;;
1514;; - Discrimination -
1515;; (add-completion "cumberland")
1516;; (add-completion "cumberbund")
1517;; cumbering
1518;; (completion-search-reset "cumb")
1519;; (completion-search-peek t) --> "cumberbund"
1520;; (completion-search-next 0) --> "cumberbund"
1521;; (completion-search-peek t) --> "cumberland"
1522;; (completion-search-next 1) --> "cumberland"
1523;; (completion-search-peek nil) --> nil
1524;; (completion-search-next 2) --> "cumbering" {cdabbrev}
1525;; (completion-search-next 3) --> nil or "cumming"{depends on context}
1526;; (completion-search-next 1) --> "cumberland"
1527;; (completion-search-peek t) --> "cumbering" {cdabbrev}
1528;;
1529;; - Accepting -
1530;; (completion-search-next 1) --> "cumberland"
1531;; (setq completion-to-accept "cumberland")
1532;; (completion-search-reset "foo")
1533;; (completion-search-reset "cum")
1534;; (completion-search-next 0) --> "cumberland"
1535;;
1536;; - Deleting -
1537;; (kill-completion "cumberland")
1538;; cummings
1539;; (completion-search-reset "cum")
1540;; (completion-search-next 0) --> "cumberbund"
1541;; (completion-search-next 1) --> "cummings"
1542;;
1543;; - Ignoring Capitalization -
1544;; (completion-search-reset "CuMb")
1545;; (completion-search-next 0) --> "cumberbund"
1546
1547
1548 1433
1549;;----------------------------------------------- 1434;;-----------------------------------------------
1550;; COMPLETE 1435;; COMPLETE
@@ -1733,12 +1618,6 @@ Prefix args ::
1733 "\n(\\(\\w*:\\)?def\\(\\w\\|\\s_\\)*\\s +(*" 1618 "\n(\\(\\w*:\\)?def\\(\\w\\|\\s_\\)*\\s +(*"
1734 "A regexp that searches for Lisp definition form.") 1619 "A regexp that searches for Lisp definition form.")
1735 1620
1736;; Tests -
1737;; (and (string-match *lisp-def-regexp* "\n(defun foo") (match-end 0)) -> 8
1738;; (and (string-match *lisp-def-regexp* "\n(si:def foo") (match-end 0)) -> 9
1739;; (and (string-match *lisp-def-regexp* "\n(def-bar foo")(match-end 0)) -> 10
1740;; (and (string-match *lisp-def-regexp* "\n(defun (foo") (match-end 0)) -> 9
1741
1742;; Parses all the definition names from a Lisp mode buffer and adds them to 1621;; Parses all the definition names from a Lisp mode buffer and adds them to
1743;; the completion database. 1622;; the completion database.
1744(defun add-completions-from-lisp-buffer () 1623(defun add-completions-from-lisp-buffer ()
diff --git a/test/lisp/completion-tests.el b/test/lisp/completion-tests.el
new file mode 100644
index 00000000000..7473bbbb0c5
--- /dev/null
+++ b/test/lisp/completion-tests.el
@@ -0,0 +1,170 @@
1;;; completion-tests.el --- Tests for completion.el -*- lexical-binding: t -*-
2
3;; Copyright (C) 2020 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs 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;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
19
20;;; Commentary:
21
22;;; Code:
23
24(require 'ert)
25(require 'completion)
26
27(ert-deftest completion-test-cmpl-string-case-type ()
28 (should (eq (cmpl-string-case-type "123ABCDEF456") :up))
29 (should (eq (cmpl-string-case-type "123abcdef456") :down))
30 (should (eq (cmpl-string-case-type "123aBcDeF456") :mixed))
31 (should (eq (cmpl-string-case-type "123456") :neither))
32 (should (eq (cmpl-string-case-type "Abcde123") :capitalized)))
33
34(ert-deftest completion-test-cmpl-merge-string-cases ()
35 (should (equal (cmpl-merge-string-cases "AbCdEf456" "abc") "AbCdEf456"))
36 (should (equal (cmpl-merge-string-cases "abcdef456" "ABC") "ABCDEF456"))
37 (should (equal (cmpl-merge-string-cases "ABCDEF456" "Abc") "Abcdef456"))
38 (should (equal (cmpl-merge-string-cases "ABCDEF456" "abc") "abcdef456")))
39
40(ert-deftest completion-test-add-find-delete-tail ()
41 (unwind-protect
42 (progn
43 ;; - Add and Find -
44 (should (equal (add-completion-to-head "banana") '("banana" 0 nil 0)))
45 (should (equal (find-exact-completion "banana") '("banana" 0 nil 0)))
46 (should (equal (find-exact-completion "bana") nil))
47 (should (equal (car (find-cmpl-prefix-entry "ban")) '(("banana" 0 nil 0))))
48 (should (equal (cdr (find-cmpl-prefix-entry "ban")) '(("banana" 0 nil 0))))
49
50 (should (equal (add-completion-to-head "banish") '("banish" 0 nil 0)))
51 (should (equal (find-exact-completion "banish") '("banish" 0 nil 0)))
52 (should (equal (car (find-cmpl-prefix-entry "ban")) '(("banish" 0 nil 0) ("banana" 0 nil 0))))
53 (should (equal (cdr (find-cmpl-prefix-entry "ban")) '(("banana" 0 nil 0))))
54
55 (should (equal (add-completion-to-head "banana") '("banana" 0 nil 0)))
56 (should (equal (car (find-cmpl-prefix-entry "ban")) '(("banana" 0 nil 0) ("banish" 0 nil 0))))
57 (should (equal (cdr (find-cmpl-prefix-entry "ban")) '(("banish" 0 nil 0))))
58
59 ;; - Deleting -
60 (should (equal (add-completion-to-head "banner") '("banner" 0 nil 0)))
61 (delete-completion "banner")
62 (should-not (find-exact-completion "banner"))
63 (should (equal (car (find-cmpl-prefix-entry "ban")) '(("banana" 0 nil 0) ("banish" 0 nil 0))))
64 (should (equal (cdr (find-cmpl-prefix-entry "ban")) '(("banish" 0 nil 0))))
65 (should (equal (add-completion-to-head "banner") '("banner" 0 nil 0)))
66 (delete-completion "banana")
67 (should (equal (car (find-cmpl-prefix-entry "ban")) '(("banner" 0 nil 0) ("banish" 0 nil 0))))
68 (should (equal (cdr (find-cmpl-prefix-entry "ban")) '(("banish" 0 nil 0))))
69 (delete-completion "banner")
70 (delete-completion "banish")
71 (should-not (find-cmpl-prefix-entry "ban"))
72 (should-error (delete-completion "banner"))
73
74 ;; - Tail -
75 (should (equal (add-completion-to-tail-if-new "banana") '("banana" 0 nil 0)))
76 (should (equal (car (find-cmpl-prefix-entry "ban")) '(("banana" 0 nil 0))))
77 (should (equal (cdr (find-cmpl-prefix-entry "ban")) '(("banana" 0 nil 0))))
78 (add-completion-to-tail-if-new "banish") '("banish" 0 nil 0)
79 (should (equal (car (find-cmpl-prefix-entry "ban")) '(("banana" 0 nil 0) ("banish" 0 nil 0))))
80 (should (equal (cdr (find-cmpl-prefix-entry "ban")) '(("banish" 0 nil 0)))))
81 (ignore-errors (kill-completion "banana"))
82 (ignore-errors (kill-completion "banner"))
83 (ignore-errors (kill-completion "banish"))))
84
85(ert-deftest completion-test-add-find-accept-delete ()
86 (unwind-protect
87 (progn
88 ;; - Add and Find -
89 (add-completion "banana" 5 10)
90 (should (equal (find-exact-completion "banana") '("banana" 5 10 0)))
91 (add-completion "banana" 6)
92 (should (equal (find-exact-completion "banana") '("banana" 6 10 0)))
93 (add-completion "banish")
94 (should (equal (car (find-cmpl-prefix-entry "ban")) '(("banish" 0 nil 0) ("banana" 6 10 0))))
95
96 ;; - Accepting -
97 (setq completion-to-accept "banana")
98 (accept-completion)
99 (should (equal (find-exact-completion "banana") '("banana" 7 10 0)))
100 (should (equal (car (find-cmpl-prefix-entry "ban")) '(("banana" 7 10 0) ("banish" 0 nil 0))))
101 (setq completion-to-accept "banish")
102 (add-completion "banner")
103 (should (equal (car (find-cmpl-prefix-entry "ban"))
104 '(("banner" 0 nil 0) ("banish" 1 nil 0) ("banana" 7 10 0))))
105
106 ;; - Deleting -
107 (kill-completion "banish")
108 (should (equal (car (find-cmpl-prefix-entry "ban")) '(("banner" 0 nil 0) ("banana" 7 10 0)))))
109 (ignore-errors (kill-completion "banish"))
110 (ignore-errors (kill-completion "banana"))
111 (ignore-errors (kill-completion "banner"))))
112
113(ert-deftest completion-test-search ()
114 (unwind-protect
115 (progn
116 ;; - Add and Find -
117 (add-completion "banana")
118 (completion-search-reset "ban")
119 (should (equal (car (completion-search-next 0)) "banana"))
120
121 ;; - Discrimination -
122 (add-completion "cumberland")
123 (add-completion "cumberbund")
124 ;; cumbering
125 (completion-search-reset "cumb")
126 (should (equal (car (completion-search-peek t)) "cumberbund"))
127 (should (equal (car (completion-search-next 0)) "cumberbund"))
128 (should (equal (car (completion-search-peek t)) "cumberland"))
129 (should (equal (car (completion-search-next 1)) "cumberland"))
130 (should-not (completion-search-peek nil))
131
132 ;; FIXME
133 ;; (should (equal (completion-search-next 2) "cumbering")) ; {cdabbrev}
134 ;;(completion-search-next 3) --> nil or "cumming" {depends on context}
135
136 (should (equal (car (completion-search-next 1)) "cumberland"))
137
138 ;; FIXME
139 ;; (should (equal (completion-search-peek t) "cumbering")) ; {cdabbrev}
140
141 ;; - Accepting -
142 (should (equal (car (completion-search-next 1)) "cumberland"))
143 (setq completion-to-accept "cumberland")
144 (completion-search-reset "foo")
145 (completion-search-reset "cum")
146 (should (equal (car (completion-search-next 0)) "cumberland"))
147
148 ;; - Deleting -
149 (kill-completion "cumberland")
150 (add-completion "cummings")
151 (completion-search-reset "cum")
152 (should (equal (car (completion-search-next 0)) "cummings"))
153 (should (equal (car (completion-search-next 1)) "cumberbund"))
154
155 ;; - Ignoring Capitalization -
156 (completion-search-reset "CuMb")
157 (should (equal (car (completion-search-next 0)) "cumberbund")))
158 (ignore-errors (kill-completion "banana"))
159 (ignore-errors (kill-completion "cumberland"))
160 (ignore-errors (kill-completion "cumberbund"))
161 (ignore-errors (kill-completion "cummings"))))
162
163(ert-deftest completion-test-lisp-def-regexp ()
164 (should (= (and (string-match *lisp-def-regexp* "\n(defun foo") (match-end 0)) 8))
165 (should (= (and (string-match *lisp-def-regexp* "\n(si:def foo") (match-end 0)) 9))
166 (should (= (and (string-match *lisp-def-regexp* "\n(def-bar foo")(match-end 0)) 10))
167 (should (= (and (string-match *lisp-def-regexp* "\n(defun (foo") (match-end 0)) 9)))
168
169(provide 'completion-tests)
170;;; completion-tests.el ends here