summaryrefslogtreecommitdiff
path: root/test/lisp/progmodes/uniquify-files-test.el
blob: ad19e6ad1880f7fc0a8ff28d8cc7de0e796d2150 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
;;; uniquify-files-test.el - Test functions in uniquify-files.el -*- lexical-binding:t no-byte-compile:t -*-
;;
;; Copyright (C) 2017, 2019  Free Software Foundation, Inc.
;;
;; Author: Stephen Leake <stephen_leake@stephe-leake.org>
;; Maintainer: Stephen Leake <stephen_leake@stephe-leake.org>
;;
;; 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 <http://www.gnu.org/licenses/>.

;;; Commentary:
;;;
;; This is not a complete test of the completion style; the way the
;; completion functions interact with completing-read is not fully
;; tested. The following table gives useful test cases for a manual
;; interactive test (copy it to an org-mode buffer).

;; See `test-uniquify-file-all-completions-face' below for an
;; explanation of `no-byte-compile'.

(require 'ert)
(require 'uniquify-files)

(defconst uft-root
  (concat
   (file-name-directory (or load-file-name (buffer-file-name)))
   ;; We deliberately leave out the trailing '/' here, because users
   ;; often do; the code must cope.
   "uniquify-files-resources"))

(defconst uft-alice1 (concat uft-root "/Alice/alice-1"))
(defconst uft-alice2 (concat uft-root "/Alice/alice-2"))
(defconst uft-Alice-alice3 (concat uft-root "/Alice/alice-3"))
(defconst uft-Bob-alice3 (concat uft-root "/Bob/alice-3"))
(defconst uft-bob1 (concat uft-root "/Bob/bob-1"))
(defconst uft-bob2 (concat uft-root "/Bob/bob-2"))

(defconst uft-path
   (list uft-root
	 (concat uft-root "/Alice")
	 uft-alice1
	 uft-alice2
	 uft-Alice-alice3
	 (concat uft-root "/Bob")
	 uft-Bob-alice3
	 uft-bob1
	 uft-bob2))

(defun uft-table ()
  (apply-partially 'uniq-file-completion-table (uniq-file-uniquify (path-files uft-path))))

(ert-deftest test-uniq-file-test-completion ()
  (let ((table (uft-table))
	(completion-current-style 'uniquify-file))
    (should (equal (test-completion "foo-fi" table)
		   nil))

    (should (equal (test-completion "f-fi<dir" table)
		   nil))

    (should (equal (test-completion "foo-file1.text<>" table)
		   t))

    (should (equal (test-completion "foo-file1.text" table)
		   nil))

    (should (equal (test-completion "foo-file1.text<Alice/alice-1/>" table)
		   t))

    (should (equal (test-completion "foo-file3.tex" table) ;; partial file name
		   nil))

    (should (equal (test-completion "foo-file3.texts2" table)
		   t))

    (should (equal (test-completion "bar-file2.text<Alice/alice-" table)
		   nil))
    ))

(ert-deftest test-uniq-file-all-completions-noface ()
  (let ((table (uft-table))
	(completion-current-style 'uniquify-file)
	(completion-ignore-case nil))
    (should (equal
	     (sort (uniq-file-all-completions "" table nil nil) #'string-lessp)
	     (list
	      "bar-file1.text<alice-1/>"
	      "bar-file1.text<alice-2/>"
	      "bar-file2.text<alice-1/>"
	      "bar-file2.text<alice-2/>"
	      "foo-file1.text<>"
	      "foo-file1.text<Alice/alice-1/>"
	      "foo-file1.text<Alice/alice-2/>"
	      "foo-file1.text<Bob/bob-1/>"
	      "foo-file1.text<Bob/bob-2/>"
	      "foo-file2.text<Alice/alice-1/>"
	      "foo-file2.text<Bob/bob-1/>"
	      "foo-file3.text"
	      "foo-file3.texts"
	      "foo-file3.texts2"
	      "foo-file4.text<Alice/alice-3/>"
	      "foo-file4.text<Bob/alice-3/>"
	      "foo-file5.text"
              "wisitoken-generate-packrat-test.text"
              "wisitoken-syntax_trees-test.text"
              "wisitoken-text_io_trace.text"
	      )))

    (should (equal
	     (sort (uniq-file-all-completions "*-fi" table nil nil) #'string-lessp)
	     (list
	      "bar-file1.text<alice-1/>"
	      "bar-file1.text<alice-2/>"
	      "bar-file2.text<alice-1/>"
	      "bar-file2.text<alice-2/>"
	      "foo-file1.text<>"
	      "foo-file1.text<Alice/alice-1/>"
	      "foo-file1.text<Alice/alice-2/>"
	      "foo-file1.text<Bob/bob-1/>"
	      "foo-file1.text<Bob/bob-2/>"
	      "foo-file2.text<Alice/alice-1/>"
	      "foo-file2.text<Bob/bob-1/>"
	      "foo-file3.text"
	      "foo-file3.texts"
	      "foo-file3.texts2"
	      "foo-file4.text<Alice/alice-3/>"
	      "foo-file4.text<Bob/alice-3/>"
	      "foo-file5.text"
	      )))

    (should (equal
	     (sort (uniq-file-all-completions "a" table nil nil) #'string-lessp)
	     ;; Should _not_ match directory names
	     nil))

    (should (equal
	     (sort (uniq-file-all-completions "b" table nil nil) #'string-lessp)
	     (list
	      "bar-file1.text<alice-1/>"
	      "bar-file1.text<alice-2/>"
	      "bar-file2.text<alice-1/>"
	      "bar-file2.text<alice-2/>"
	      )))

    (should (equal
	     (sort (uniq-file-all-completions "foo" table nil nil) #'string-lessp)
	     (list
	      "foo-file1.text<>"
	      "foo-file1.text<Alice/alice-1/>"
	      "foo-file1.text<Alice/alice-2/>"
	      "foo-file1.text<Bob/bob-1/>"
	      "foo-file1.text<Bob/bob-2/>"
	      "foo-file2.text<Alice/alice-1/>"
	      "foo-file2.text<Bob/bob-1/>"
	      "foo-file3.text"
	      "foo-file3.texts"
	      "foo-file3.texts2"
	      "foo-file4.text<Alice/alice-3/>"
	      "foo-file4.text<Bob/alice-3/>"
	      "foo-file5.text"
	      )))

    (should (equal
	     (sort (uniq-file-all-completions "f-file2" table nil nil) #'string-lessp)
	     (list
	      "foo-file2.text<Alice/alice-1/>"
	      "foo-file2.text<Bob/bob-1/>"
	      )))

    (should (equal
	     (sort (uniq-file-all-completions "b-fi<" table nil nil) #'string-lessp)
	     (list
	      "bar-file1.text<alice-1/>"
	      "bar-file1.text<alice-2/>"
	      "bar-file2.text<alice-1/>"
	      "bar-file2.text<alice-2/>"
	      )))

    (should (equal
	     (sort (uniq-file-all-completions "f-file<" table nil nil) #'string-lessp)
	     (list
	      "foo-file1.text<>"
	      "foo-file1.text<Alice/alice-1/>"
	      "foo-file1.text<Alice/alice-2/>"
	      "foo-file1.text<Bob/bob-1/>"
	      "foo-file1.text<Bob/bob-2/>"
	      "foo-file2.text<Alice/alice-1/>"
	      "foo-file2.text<Bob/bob-1/>"
	      "foo-file3.text"
	      "foo-file3.texts"
	      "foo-file3.texts2"
	      "foo-file4.text<Alice/alice-3/>"
	      "foo-file4.text<Bob/alice-3/>"
	      "foo-file5.text"
	      )))

    (should (equal
	     (sort (uniq-file-all-completions "b-fi<a-" table nil nil) #'string-lessp)
	     (list
	      "bar-file1.text<alice-1/>"
	      "bar-file1.text<alice-2/>"
	      "bar-file2.text<alice-1/>"
	      "bar-file2.text<alice-2/>"
	      )))

    (should (equal
	     (sort (uniq-file-all-completions "b-fi<a-1" table nil nil) #'string-lessp)
	     (list "bar-file1.text<alice-1/>"
		   "bar-file2.text<alice-1/>")))

    (should (equal (uniq-file-all-completions "f-file1.text<a-1" table nil nil)
		   (list "foo-file1.text<Alice/alice-1/>")))

    (should (equal (sort (uniq-file-all-completions "f-file1.text<al" table nil nil) #'string-lessp)
		   (list
		    "foo-file1.text<Alice/alice-1/>"
		    "foo-file1.text<Alice/alice-2/>")))

    (should (equal (sort (uniq-file-all-completions "f-file4.text<a-3" table nil nil) #'string-lessp)
		   (list
		    "foo-file4.text<Alice/alice-3/>"
		    "foo-file4.text<Bob/alice-3/>")))

    (should (equal (sort (uniq-file-all-completions "foo-file4.text<Bob" table nil nil) #'string-lessp)
		   (list
		    "foo-file4.text<Bob/alice-3/>")))

    (should (equal (uniq-file-all-completions "f-file5" table nil nil)
		   (list "foo-file5.text")))

    (should (equal (uniq-file-all-completions "foo-file1.text<Alice/alice-1/>" table nil nil)
		   (list "foo-file1.text<Alice/alice-1/>")))

    (should (equal
	     (sort (uniq-file-all-completions "b-fi<a>" table nil nil) #'string-lessp)
	     (list
	      "bar-file1.text<alice-1/>"
	      "bar-file1.text<alice-2/>"
	      "bar-file2.text<alice-1/>"
	      "bar-file2.text<alice-2/>"
	      )))

    (should (equal
	     (sort (uniq-file-all-completions "foo-file1.text<>" table nil nil) #'string-lessp)
	     ;; This is complete but not unique, because the directory part matches multiple directories.
	     (list
	      "foo-file1.text<>"
	      "foo-file1.text<Alice/alice-1/>"
	      "foo-file1.text<Alice/alice-2/>"
	      "foo-file1.text<Bob/bob-1/>"
	      "foo-file1.text<Bob/bob-2/>"
	      )))
    ))

(defun test-uniq-file-hilit (pos-list string)
  "Set 'face text property to 'completions-first-difference at
all positions in POS-LIST in STRING; return new string."
  (while pos-list
    (let ((pos (pop pos-list)))
      (put-text-property pos (1+ pos) 'face 'completions-first-difference string)))
  string)

(ert-deftest test-uniq-file-all-completions-face ()
  ;; `all-completions' tested above without considering face text
  ;; properties; here we test just those properties. Test cases are
  ;; the same as above.
  ;;
  ;; WORKAROUND: byte-compiling this test makes it fail; it appears to be
  ;; sharing strings that should not be shared because they have
  ;; different text properties.
  (let ((table (uft-table))
	(completion-ignore-case nil))

    (should (equal-including-properties
	     (sort (uniq-file-all-completions "b" table nil nil) #'string-lessp)
	     (list
	      (test-uniq-file-hilit '(8) "bar-file1.text<alice-1/>")
	      (test-uniq-file-hilit '(8) "bar-file1.text<alice-2/>")
	      (test-uniq-file-hilit '(8) "bar-file2.text<alice-1/>")
	      (test-uniq-file-hilit '(8) "bar-file2.text<alice-2/>")
	      )))

    (should (equal-including-properties
	     (sort (uniq-file-all-completions "foo" table nil nil) #'string-lessp)
	     (list
	      (test-uniq-file-hilit '(8) "foo-file1.text<>")
	      (test-uniq-file-hilit '(8) "foo-file1.text<Alice/alice-1/>")
	      (test-uniq-file-hilit '(8) "foo-file1.text<Alice/alice-2/>")
	      (test-uniq-file-hilit '(8) "foo-file1.text<Bob/bob-1/>")
	      (test-uniq-file-hilit '(8) "foo-file1.text<Bob/bob-2/>")
	      (test-uniq-file-hilit '(8) "foo-file2.text<Alice/alice-1/>")
	      (test-uniq-file-hilit '(8) "foo-file2.text<Bob/bob-1/>")
	      (test-uniq-file-hilit '(8) "foo-file3.text")
	      (test-uniq-file-hilit '(8) "foo-file3.texts")
	      (test-uniq-file-hilit '(8) "foo-file3.texts2")
	      (test-uniq-file-hilit '(8) "foo-file4.text<Alice/alice-3/>")
	      (test-uniq-file-hilit '(8) "foo-file4.text<Bob/alice-3/>")
	      (test-uniq-file-hilit '(8) "foo-file5.text")
	      )))

    (should (equal-including-properties
	     (sort (uniq-file-all-completions "f-file2" table nil nil) #'string-lessp)
	     (list
	      (test-uniq-file-hilit '(15) "foo-file2.text<Alice/alice-1/>")
	      (test-uniq-file-hilit '(15) "foo-file2.text<Bob/bob-1/>")
	      )))

    (should (equal-including-properties
	     (sort (uniq-file-all-completions "foo-file3.text" table nil nil) #'string-lessp)
	     (list
	      (test-uniq-file-hilit '()   "foo-file3.text")
	      (test-uniq-file-hilit '(14) "foo-file3.texts")
	      (test-uniq-file-hilit '(14) "foo-file3.texts2")
	      )))

    ;; Two places for possible completion, with different intervening text
    (should (equal-including-properties
	     (sort (uniq-file-all-completions "wisi-te" table nil 5) #'string-lessp)
	     (list                         ;; 0         10        20        30
	      (test-uniq-file-hilit '(10 18) "wisitoken-generate-packrat-test.text")
	      (test-uniq-file-hilit '(10 25) "wisitoken-syntax_trees-test.text")
	      (test-uniq-file-hilit '(10 12) "wisitoken-text_io_trace.text")
	      )))
    ))

(ert-deftest test-uniq-file-try-completion ()
  (let ((table (uft-table))
	(completion-current-style 'uniquify-file)
	(completion-ignore-case nil)
        string)

    (setq string "fo")
    (should (equal (uniq-file-try-completion string table nil (length string))
		   '("foo-file" . 8)))

    (setq string "b")
    (should (equal (uniq-file-try-completion string table nil (length string))
		   '("bar-file" . 8)))

    (setq string "fo<al")
    (should (equal (uniq-file-try-completion string table nil 2)
		   '("foo-file.text<alice-" . 8)))
    (should (equal (uniq-file-try-completion string table nil 5)
		   '("foo-file<alice-" . 15)))

    (let ((completion-ignore-case t))
      (setq string "fo<al")
      (should (equal (uniq-file-try-completion string table nil 2)
		     '("foo-file.text<alice" . 8)))
      (should (equal (uniq-file-try-completion string table nil 5)
		     '("foo-file<alice" . 14)))
      )

    (setq string "foo-file3") ;; not unique, not valid
    (should (equal (uniq-file-try-completion string table nil (length string))
		   '("foo-file3.text" . 14)))

    (setq string "f-file1.text<a-1")
    ;; Not unique, because "a" accidentally matches "packages" in
    ;; uft-root-dir, and "-" covers "/".  Also not valid.
    (should (equal (uniq-file-try-completion string table nil (length string))
		   '("foo-file1.text<Alice/alice-1/>" . 30)))

    (setq string "foo-file1.text") ;; valid but not unique
    (should (equal (uniq-file-try-completion string table nil (length string))
		   (cons "foo-file1.text<" 15)))

    (setq string "foo-file1<") ;; not valid
    (should (equal (uniq-file-try-completion string table nil (length string))
		   (cons "foo-file1.text<" 15)))

    (setq string "foo-file1.text<>") ;; valid but not unique
    (should (equal (uniq-file-try-completion string table nil (length string))
		   (cons "foo-file1.text<>" 15)))

    (setq string "foo-file1.text<Alice/alice-1/>") ;; valid and unique
    (should (equal (uniq-file-try-completion string table nil (length string))
		   t))

    (setq string "foo-file3.texts") ;; not unique, valid
    (should (equal (uniq-file-try-completion string table nil (length string))
		   '("foo-file3.texts" . 15)))

    (setq string "foo-file3.texts2") ;; unique and valid
    (should (equal (uniq-file-try-completion string table nil (length string))
		   t))

    (setq string "fil2") ;; misspelled
    (should (equal (uniq-file-try-completion string table nil (length string))
		   nil))

    (setq string "b-file2")
    (should (equal (uniq-file-try-completion string table nil (length string))
		   '("bar-file2.text<alice-" . 21)))

    ;; prev + <tab>; input is prev output
    (setq string "bar-file2.text<alice-")
    (should (equal (uniq-file-try-completion string table nil (length string))
		   '("bar-file2.text<alice-" . 21)))

    ;; prev + <tab>; input is prev output
    (setq string "bar-file2.text<alice-")
    (should (equal (uniq-file-try-completion string table nil (length string))
		   '("bar-file2.text<alice-" . 21)))

    ;; completion-try-completion called from icomplete-completions with
    ;; result of all-completions instead of table function.
    (setq string "f-file<")
    (let ((comps (uniq-file-all-completions string table nil nil)))
      (should (equal (uniq-file-try-completion string comps nil (length string))
		     (cons "foo-file" 8))))
    ))

(ert-deftest test-uniq-file-uniquify ()
  (should (equal (uniq-file-uniquify
		  '("/Alice/alice1/file1.text"
                    "/Alice/alice1/file2.text"
		    "/Alice/alice2/file1.text"
                    "/Alice/alice2/file3.text"
		    "/Bob/bob1/file1.text"))
		 (list
		  '("file3.text"                . "/Alice/alice2/file3.text")
		  '("file2.text"                . "/Alice/alice1/file2.text")
		  '("file1.text<Bob/bob1/>"     . "/Bob/bob1/file1.text")
		  '("file1.text<Alice/alice2/>" . "/Alice/alice2/file1.text")
                  '("file1.text<Alice/alice1/>" . "/Alice/alice1/file1.text")
                  )))

  (should (equal (uniq-file-uniquify
		  (list
		   (concat uft-alice1 "/foo-file1.text")
		   (concat uft-alice2 "/foo-file1.text")
		   (concat uft-bob1 "/foo-file1.text")
		   (concat uft-bob2 "/foo-file1.text")
		   (concat uft-root "/foo-file1.text")
		   ))
		 (list
		  (cons "foo-file1.text<>"               (concat uft-root "/foo-file1.text"))
		  (cons "foo-file1.text<Bob/bob-2/>"     (concat uft-bob2 "/foo-file1.text"))
                  (cons "foo-file1.text<Bob/bob-1/>"     (concat uft-bob1 "/foo-file1.text"))
                  (cons "foo-file1.text<Alice/alice-2/>" (concat uft-alice2 "/foo-file1.text"))
		  (cons "foo-file1.text<Alice/alice-1/>" (concat uft-alice1 "/foo-file1.text"))
                  )))

  (should (equal (uniq-file-uniquify
		  (list
		   (concat uft-alice1 "/bar-file1.c")
		   (concat uft-alice1 "/bar-file2.c")
		   (concat uft-alice2 "/bar-file1.c")
		   (concat uft-alice2 "/bar-file2.c")
		   (concat uft-bob1 "/foo-file1.c")
		   (concat uft-bob1 "/foo-file2.c")
		   (concat uft-bob2 "/foo-file1.c")
		   (concat uft-bob2 "/foo-file5.c")
		   ))
		 (list
                  (cons "foo-file5.c"           (concat uft-bob2 "/foo-file5.c"))
		  (cons "foo-file2.c"           (concat uft-bob1 "/foo-file2.c"))
                  (cons "foo-file1.c<bob-2/>"   (concat uft-bob2 "/foo-file1.c"))
                  (cons "foo-file1.c<bob-1/>"   (concat uft-bob1 "/foo-file1.c"))
                  (cons "bar-file2.c<alice-2/>" (concat uft-alice2 "/bar-file2.c"))
                  (cons "bar-file2.c<alice-1/>" (concat uft-alice1 "/bar-file2.c"))
                  (cons "bar-file1.c<alice-2/>" (concat uft-alice2 "/bar-file1.c"))
                  (cons "bar-file1.c<alice-1/>" (concat uft-alice1 "/bar-file1.c"))
                  )))
  )

(provide 'uniquify-files-test)
;;; uniquify-files-test.el ends here