* contrib/anything-grep.el: abbreviate buffer-file-name in $buffers
[anything-config.git] / developer-tools / unit-test-anything.el
blobc5efbdf9437590d973376bab845dac6906a08c5a
1 ;;; unit tests for anything.el
2 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
3 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
6 (defmacro anything-test-update (sources pattern)
7 "Test helper macro for anything. It is meant for testing *anything* buffer contents."
8 `(progn (stub anything-get-sources => ,sources)
9 (stub anything-log-run-hook => nil)
10 (stub run-with-idle-timer => nil)
11 (let (anything-test-mode (anything-pattern ,pattern))
12 (anything-update))))
15 (dont-compile
16 (when (fboundp 'expectations)
17 (expectations
18 (desc "anything-current-buffer")
19 (expect "__a_buffer"
20 (with-current-buffer (get-buffer-create "__a_buffer")
21 (anything-test-candidates '(((name . "FOO"))) "")
22 (prog1
23 (buffer-name anything-current-buffer)
24 (kill-buffer "__a_buffer")
25 )))
26 (desc "anything-buffer-file-name")
27 (expect (regexp "/__a_file__")
28 (with-current-buffer (get-buffer-create "__a_file__")
29 (setq buffer-file-name "/__a_file__")
30 (anything-test-candidates '(((name . "FOO"))) "")
31 (prog1
32 anything-buffer-file-name
33 ;;(kill-buffer "__a_file__")
34 )))
35 (desc "anything-interpret-value")
36 (expect "literal"
37 (anything-interpret-value "literal"))
38 (expect "lambda"
39 (anything-interpret-value (lambda () "lambda")))
40 (expect "lambda with source name"
41 (let ((source '((name . "lambda with source name"))))
42 (anything-interpret-value (lambda () anything-source-name) source)))
43 (expect "function symbol"
44 (flet ((f () "function symbol"))
45 (anything-interpret-value 'f)))
46 (expect "variable symbol"
47 (let ((v "variable symbol"))
48 (anything-interpret-value 'v)))
49 (expect (error error *)
50 (anything-interpret-value 'unbounded-1))
51 (desc "anything-compile-sources")
52 (expect '(((name . "foo")))
53 (anything-compile-sources '(((name . "foo"))) nil)
55 (expect '(((name . "foo") (type . test) (action . identity)))
56 (let ((anything-type-attributes '((test (action . identity)))))
57 (anything-compile-sources '(((name . "foo") (type . test)))
58 '(anything-compile-source--type))))
59 (desc "anything-sources accepts symbols")
60 (expect '(((name . "foo")))
61 (let* ((foo '((name . "foo"))))
62 (anything-compile-sources '(foo) nil)))
63 (desc "anything-get-sources action")
64 (expect '(((name . "Actions") (candidates . actions)))
65 (stub anything-action-window => t)
66 (let (anything-compiled-sources
67 (anything-sources '(((name . "Actions") (candidates . actions)))))
68 (anything-get-sources)))
69 (desc "get-buffer-create candidates-buffer")
70 (expect '(((name . "many") (init . many-init)
71 (candidates-in-buffer . anything-candidates-in-buffer)
72 (candidates . anything-candidates-in-buffer)
73 (volatile) (match identity)))
74 (anything-compile-sources
75 '(((name . "many") (init . many-init)
76 (candidates-in-buffer . anything-candidates-in-buffer)))
77 '(anything-compile-source--candidates-in-buffer)))
78 (expect '(((name . "many") (init . many-init)
79 (candidates-in-buffer)
80 (candidates . anything-candidates-in-buffer)
81 (volatile) (match identity)))
82 (anything-compile-sources
83 '(((name . "many") (init . many-init)
84 (candidates-in-buffer)))
85 '(anything-compile-source--candidates-in-buffer)))
86 (expect '(((name . "many") (init . many-init)
87 (candidates-in-buffer)
88 (type . test)
89 (action . identity)
90 (candidates . anything-candidates-in-buffer)
91 (volatile) (match identity)))
92 (let ((anything-type-attributes '((test (action . identity)))))
93 (anything-compile-sources
94 '(((name . "many") (init . many-init)
95 (candidates-in-buffer)
96 (type . test)))
97 '(anything-compile-source--type
98 anything-compile-source--candidates-in-buffer))))
100 (desc "anything-get-candidates")
101 (expect '("foo" "bar")
102 (anything-get-candidates '((name . "foo") (candidates "foo" "bar"))))
103 (expect '("FOO" "BAR")
104 (anything-get-candidates '((name . "foo") (candidates "foo" "bar")
105 (candidate-transformer
106 . (lambda (cands) (mapcar 'upcase cands))))))
107 (expect '("foo" "bar")
108 (anything-get-candidates '((name . "foo")
109 (candidates . (lambda () '("foo" "bar"))))))
110 (expect '("foo" "bar")
111 (let ((var '("foo" "bar")))
112 (anything-get-candidates '((name . "foo")
113 (candidates . var)))))
114 (expect (error error *)
115 (anything-get-candidates '((name . "foo")
116 (candidates . "err"))))
117 (expect (error error *)
118 (let ((var "err"))
119 (anything-get-candidates '((name . "foo")
120 (candidates . var)))))
121 (expect (error error *)
122 (anything-get-candidates '((name . "foo")
123 (candidates . unDeFined-syMbol))))
124 (desc "anything-compute-matches")
125 (expect '("foo" "bar")
126 (let ((anything-pattern ""))
127 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
128 (expect '("foo")
129 (let ((anything-pattern "oo"))
130 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
131 (expect '("bar")
132 (let ((anything-pattern "^b"))
133 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
134 (expect '("a" "b")
135 (let ((anything-pattern "")
136 (anything-candidate-number-limit 2))
137 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
138 (expect '("a" "b")
139 (let ((anything-pattern ".")
140 (anything-candidate-number-limit 2))
141 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
142 (expect '("a" "b" "c")
143 (let ((anything-pattern "")
144 anything-candidate-number-limit)
145 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
146 (expect '("a" "b" "c")
147 (let ((anything-pattern "[abc]")
148 anything-candidate-number-limit)
149 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
150 (expect '(a b c)
151 (let ((anything-pattern "[abc]")
152 anything-candidate-number-limit)
153 (anything-compute-matches '((name . "FOO") (candidates a b c) (volatile)))))
154 (expect '(("foo" . "FOO") ("bar" . "BAR"))
155 (let ((anything-pattern ""))
156 (anything-compute-matches '((name . "FOO") (candidates ("foo" . "FOO") ("bar" . "BAR")) (volatile)))))
157 (expect '(("foo" . "FOO"))
158 (let ((anything-pattern "foo"))
159 (anything-compute-matches '((name . "FOO") (candidates ("foo" . "FOO") ("bar" . "foo")) (volatile)))))
160 ;; using anything-test-candidate-list
161 (desc "anything-test-candidates")
162 (expect '(("FOO" ("foo" "bar")))
163 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")))))
164 (expect '(("FOO" ("bar")))
165 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar"))) "ar"))
166 (expect '(("T1" ("hoge" "aiue"))
167 ("T2" ("test" "boke")))
168 (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
169 ((name . "T2") (candidates "test" "boke")))))
170 (expect '(("T1" ("hoge"))
171 ("T2" ("boke")))
172 (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
173 ((name . "T2") (candidates "test" "boke"))) "o"))
174 (desc "requires-pattern attribute")
175 (expect nil
176 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
177 (requires-pattern . 1)))))
178 (expect '(("FOO" ("bar")))
179 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
180 (requires-pattern . 1))) "b"))
181 (desc "delayed attribute(for test)")
182 (expect '(("T2" ("boke"))
183 ("T1" ("hoge")))
184 (anything-test-candidates
185 '(((name . "T1") (candidates "hoge" "aiue") (delayed))
186 ((name . "T2") (candidates "test" "boke")))
187 "o"))
188 (desc "match attribute(prefix search)")
189 (expect '(("FOO" ("bar")))
190 (anything-test-candidates
191 '(((name . "FOO") (candidates "foo" "bar")
192 (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
193 "ba"))
194 (expect nil
195 (anything-test-candidates
196 '(((name . "FOO") (candidates "foo" "bar")
197 (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
198 "ar"))
199 (expect "TestSource"
200 (let (x)
201 (anything-test-candidates
202 '(((name . "TestSource") (candidates "a")
203 (match (lambda (c) (setq x anything-source-name)))))
204 "a")
206 (desc "init attribute")
207 (expect '(("FOO" ("bar")))
208 (let (v)
209 (anything-test-candidates
210 '(((name . "FOO") (init . (lambda () (setq v '("foo" "bar"))))
211 (candidates . v)))
212 "ar")))
213 (desc "candidate-transformer attribute")
214 (expect '(("FOO" ("BAR")))
215 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
216 (candidate-transformer
217 . (lambda (cands) (mapcar 'upcase cands)))))
218 "ar"))
219 (desc "filtered-candidate-transformer attribute")
220 ;; needs more tests
221 (expect '(("FOO" ("BAR")))
222 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
223 (filtered-candidate-transformer
224 . (lambda (cands src) (mapcar 'upcase cands)))))
225 "ar"))
226 (desc "anything-transform-candidates in process")
227 (expect (mock (anything-composed-funcall-with-source
228 '((name . "FOO") (candidates "foo" "bar")
229 (filtered-candidate-transformer
230 . (lambda (cands src) (mapcar 'upcase cands))))
231 (lambda (cands src) (mapcar 'upcase cands))
232 '("foo" "bar")
233 '((name . "FOO") (candidates "foo" "bar")
234 (filtered-candidate-transformer
235 . (lambda (cands src) (mapcar 'upcase cands))))
237 (stub anything-process-candidate-transformer => '("foo" "bar"))
238 (anything-transform-candidates
239 '("foo" "bar")
240 '((name . "FOO") (candidates "foo" "bar")
241 (filtered-candidate-transformer
242 . (lambda (cands src) (mapcar 'upcase cands))))
245 (desc "anything-candidates-in-buffer-1")
246 (expect nil
247 (anything-candidates-in-buffer-1
248 nil ""
249 'buffer-substring-no-properties '(re-search-forward) 50 nil))
250 (expect '("foo+" "bar+" "baz+")
251 (with-temp-buffer
252 (insert "foo+\nbar+\nbaz+\n")
253 (anything-candidates-in-buffer-1
254 (current-buffer) ""
255 'buffer-substring-no-properties '(re-search-forward) 5 nil)))
256 (expect '("foo+" "bar+")
257 (with-temp-buffer
258 (insert "foo+\nbar+\nbaz+\n")
259 (anything-candidates-in-buffer-1
260 (current-buffer) ""
261 'buffer-substring-no-properties '(re-search-forward) 2 nil)))
262 (expect '("foo+")
263 (with-temp-buffer
264 (insert "foo+\nbar+\nbaz+\n")
265 (anything-candidates-in-buffer-1
266 (current-buffer) "oo\\+"
267 'buffer-substring-no-properties '(re-search-forward) 50 nil)))
268 (expect '("foo+")
269 (with-temp-buffer
270 (insert "foo+\nbar+\nbaz+\n")
271 (anything-candidates-in-buffer-1
272 (current-buffer) "oo+"
273 #'buffer-substring-no-properties '(search-forward) 50 nil)))
274 (expect '("foo+" "bar+")
275 (with-temp-buffer
276 (insert "foo+\nbar+\nbaz+\n")
277 (anything-candidates-in-buffer-1
278 (current-buffer) "."
279 'buffer-substring-no-properties '(re-search-forward) 2 nil)))
280 (expect '(("foo+" "FOO+"))
281 (with-temp-buffer
282 (insert "foo+\nbar+\nbaz+\n")
283 (anything-candidates-in-buffer-1
284 (current-buffer) "oo\\+"
285 (lambda (s e)
286 (let ((l (buffer-substring-no-properties s e)))
287 (list l (upcase l))))
288 '(re-search-forward) 50 nil)))
289 (desc "anything-candidates-in-buffer")
290 (expect '(("TEST" ("foo+" "bar+" "baz+")))
291 (anything-test-candidates
292 '(((name . "TEST")
293 (init
294 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
295 (insert "foo+\nbar+\nbaz+\n"))))
296 (candidates . anything-candidates-in-buffer)
297 (match identity)
298 (volatile)))))
299 (expect '(("TEST" ("foo+" "bar+" "baz+")))
300 (let (anything-candidate-number-limit)
301 (anything-test-candidates
302 '(((name . "TEST")
303 (init
304 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
305 (insert "foo+\nbar+\nbaz+\n"))))
306 (candidates . anything-candidates-in-buffer)
307 (match identity)
308 (volatile))))))
309 (expect '(("TEST" ("foo+")))
310 (anything-test-candidates
311 '(((name . "TEST")
312 (init
313 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
314 (insert "foo+\nbar+\nbaz+\n"))))
315 (candidates . anything-candidates-in-buffer)
316 (match identity)
317 (volatile)))
318 "oo\\+"))
319 ;; BUG remain empty string, but the pattern is rare case.
320 (expect '(("a" ("" "a" "b")))
321 (anything-test-candidates
322 '(((name . "a")
323 (init . (lambda ()
324 (with-current-buffer (anything-candidate-buffer 'global)
325 (insert "a\nb\n"))))
326 (candidates-in-buffer)))
327 "a*"))
328 (desc "search attribute")
329 (expect '(("TEST" ("foo+")))
330 (anything-test-candidates
331 '(((name . "TEST")
332 (init
333 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
334 (insert "foo+\nbar+\nbaz+\nooo\n"))))
335 (search search-forward)
336 (candidates . anything-candidates-in-buffer)
337 (match identity)
338 (volatile)))
339 "oo+"))
340 (expect '(("TEST" ("foo+" "ooo")))
341 (anything-test-candidates
342 '(((name . "TEST")
343 (init
344 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
345 (insert "foo+\nbar+\nbaz+\nooo\n"))))
346 (search search-forward re-search-forward)
347 (candidates . anything-candidates-in-buffer)
348 (match identity)
349 (volatile)))
350 "oo+"))
351 (expect '(("TEST" ("foo+" "ooo")))
352 (anything-test-candidates
353 '(((name . "TEST")
354 (init
355 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
356 (insert "foo+\nbar+\nbaz+\nooo\n"))))
357 (search re-search-forward search-forward)
358 (candidates . anything-candidates-in-buffer)
359 (match identity)
360 (volatile)))
361 "oo+"))
362 (expect '(("TEST" ("ooo" "foo+")))
363 (anything-test-candidates
364 '(((name . "TEST")
365 (init
366 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
367 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
368 (search re-search-forward search-forward)
369 (candidates . anything-candidates-in-buffer)
370 (match identity)
371 (volatile)))
372 "oo+"))
373 ;; faster exact match
374 (expect '(("TEST" ("bar+")))
375 (anything-test-candidates
376 '(((name . "TEST")
377 (init
378 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
379 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
380 (search (lambda (pattern &rest _)
381 (and (search-forward (concat "\n" pattern "\n") nil t)
382 (forward-line -1))))
383 (candidates . anything-candidates-in-buffer)
384 (match identity)
385 (volatile)))
386 "bar+"))
387 ;; faster prefix match
388 (expect '(("TEST" ("bar+")))
389 (anything-test-candidates
390 '(((name . "TEST")
391 (init
392 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
393 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
394 (search (lambda (pattern &rest _)
395 (search-forward (concat "\n" pattern) nil t)))
396 (candidates . anything-candidates-in-buffer)
397 (match identity)
398 (volatile)))
399 "ba"))
400 (desc "anything-current-buffer-is-modified")
401 (expect '(("FOO" ("modified")))
402 (let ((sources '(((name . "FOO")
403 (candidates
404 . (lambda ()
405 (if (anything-current-buffer-is-modified)
406 '("modified")
407 '("unmodified"))))))))
408 (with-temp-buffer
409 (clrhash anything-tick-hash)
410 (insert "1")
411 (anything-test-candidates sources))))
412 (expect '(("FOO" ("unmodified")))
413 (let ((sources '(((name . "FOO")
414 (candidates
415 . (lambda ()
416 (if (anything-current-buffer-is-modified)
417 '("modified")
418 '("unmodified"))))))))
419 (with-temp-buffer
420 (clrhash anything-tick-hash)
421 (insert "1")
422 (anything-test-candidates sources)
423 (anything-test-candidates sources))))
424 (expect '(("FOO" ("modified")))
425 (let ((sources '(((name . "FOO")
426 (candidates
427 . (lambda ()
428 (if (anything-current-buffer-is-modified)
429 '("modified")
430 '("unmodified"))))))))
431 (with-temp-buffer
432 (clrhash anything-tick-hash)
433 (insert "1")
434 (anything-test-candidates sources)
435 (insert "2")
436 (anything-test-candidates sources))))
437 (expect '(("BAR" ("modified")))
438 (let ((sources1 '(((name . "FOO")
439 (candidates
440 . (lambda ()
441 (if (anything-current-buffer-is-modified)
442 '("modified")
443 '("unmodified")))))))
444 (sources2 '(((name . "BAR")
445 (candidates
446 . (lambda ()
447 (if (anything-current-buffer-is-modified)
448 '("modified")
449 '("unmodified"))))))))
450 (with-temp-buffer
451 (clrhash anything-tick-hash)
452 (insert "1")
453 (anything-test-candidates sources1)
454 (anything-test-candidates sources2))))
455 (expect '(("FOO" ("unmodified")))
456 (let ((sources1 '(((name . "FOO")
457 (candidates
458 . (lambda ()
459 (if (anything-current-buffer-is-modified)
460 '("modified")
461 '("unmodified")))))))
462 (sources2 '(((name . "BAR")
463 (candidates
464 . (lambda ()
465 (if (anything-current-buffer-is-modified)
466 '("modified")
467 '("unmodified"))))))))
468 (with-temp-buffer
469 (clrhash anything-tick-hash)
470 (insert "1")
471 (anything-test-candidates sources1)
472 (anything-test-candidates sources2)
473 (anything-test-candidates sources1))))
474 (expect '(("BAR" ("unmodified")))
475 (let ((sources1 '(((name . "FOO")
476 (candidates
477 . (lambda ()
478 (if (anything-current-buffer-is-modified)
479 '("modified")
480 '("unmodified")))))))
481 (sources2 '(((name . "BAR")
482 (candidates
483 . (lambda ()
484 (if (anything-current-buffer-is-modified)
485 '("modified")
486 '("unmodified"))))))))
487 (with-temp-buffer
488 (clrhash anything-tick-hash)
489 (insert "1")
490 (anything-test-candidates sources1)
491 (anything-test-candidates sources2)
492 (anything-test-candidates sources2))))
493 (expect '(("BAR" ("modified")))
494 (let ((sources1 '(((name . "FOO")
495 (candidates
496 . (lambda ()
497 (if (anything-current-buffer-is-modified)
498 '("modified")
499 '("unmodified")))))))
500 (sources2 '(((name . "BAR")
501 (candidates
502 . (lambda ()
503 (if (anything-current-buffer-is-modified)
504 '("modified")
505 '("unmodified"))))))))
506 (with-temp-buffer
507 (clrhash anything-tick-hash)
508 (insert "1")
509 (anything-test-candidates sources1)
510 (anything-test-candidates sources2)
511 (with-temp-buffer
512 (anything-test-candidates sources2)))))
513 (desc "anything-source-name")
514 (expect "FOO"
515 (let (v)
516 (anything-test-candidates '(((name . "FOO")
517 (init
518 . (lambda () (setq v anything-source-name)))
519 (candidates "ok"))))
521 (expect "FOO"
522 (let (v)
523 (anything-test-candidates '(((name . "FOO")
524 (candidates
525 . (lambda ()
526 (setq v anything-source-name)
527 '("ok"))))))
529 (expect "FOO"
530 (let (v)
531 (anything-test-candidates '(((name . "FOO")
532 (candidates "ok")
533 (candidate-transformer
534 . (lambda (c)
535 (setq v anything-source-name)
536 c)))))
538 (expect "FOO"
539 (let (v)
540 (anything-test-candidates '(((name . "FOO")
541 (candidates "ok")
542 (filtered-candidate-transformer
543 . (lambda (c s)
544 (setq v anything-source-name)
545 c)))))
547 (expect "FOO"
548 (let (v)
549 (anything-test-candidates '(((name . "FOO")
550 (candidates "ok")
551 (display-to-real
552 . (lambda (c)
553 (setq v anything-source-name)
555 (action . identity))))
556 (anything-execute-selection-action)
558 (desc "anything-candidate-buffer create")
559 (expect " *anything candidates:FOO*"
560 (let* (anything-candidate-buffer-alist
561 (anything-source-name "FOO")
562 (buf (anything-candidate-buffer 'global)))
563 (prog1 (buffer-name buf)
564 (kill-buffer buf))))
565 (expect " *anything candidates:FOO*aTestBuffer"
566 (let* (anything-candidate-buffer-alist
567 (anything-source-name "FOO")
568 (anything-current-buffer (get-buffer-create "aTestBuffer"))
569 (buf (anything-candidate-buffer 'local)))
570 (prog1 (buffer-name buf)
571 (kill-buffer anything-current-buffer)
572 (kill-buffer buf))))
573 (expect 0
574 (let (anything-candidate-buffer-alist
575 (anything-source-name "FOO") buf)
576 (with-current-buffer (anything-candidate-buffer 'global)
577 (insert "1"))
578 (setq buf (anything-candidate-buffer 'global))
579 (prog1 (buffer-size buf)
580 (kill-buffer buf))))
581 (desc "anything-candidate-buffer get-buffer")
582 (expect " *anything candidates:FOO*"
583 (let* (anything-candidate-buffer-alist
584 (anything-source-name "FOO")
585 (buf (anything-candidate-buffer 'global)))
586 (prog1 (buffer-name (anything-candidate-buffer))
587 (kill-buffer buf))))
588 (expect " *anything candidates:FOO*aTestBuffer"
589 (let* (anything-candidate-buffer-alist
590 (anything-source-name "FOO")
591 (anything-current-buffer (get-buffer-create "aTestBuffer"))
592 (buf (anything-candidate-buffer 'local)))
593 (prog1 (buffer-name (anything-candidate-buffer))
594 (kill-buffer anything-current-buffer)
595 (kill-buffer buf))))
596 (expect " *anything candidates:FOO*"
597 (let* (anything-candidate-buffer-alist
598 (anything-source-name "FOO")
599 (buf-local (anything-candidate-buffer 'local))
600 (buf-global (anything-candidate-buffer 'global)))
601 (prog1 (buffer-name (anything-candidate-buffer))
602 (kill-buffer buf-local)
603 (kill-buffer buf-global))))
604 (expect " *anything candidates:FOO*aTestBuffer"
605 (let* (anything-candidate-buffer-alist
606 (anything-source-name "FOO")
607 (anything-current-buffer (get-buffer-create "aTestBuffer"))
608 (buf-global (anything-candidate-buffer 'global))
609 (buf-local (anything-candidate-buffer 'local)))
610 (prog1 (buffer-name (anything-candidate-buffer))
611 (kill-buffer buf-local)
612 (kill-buffer buf-global))))
613 (expect nil
614 (let* (anything-candidate-buffer-alist
615 (anything-source-name "NOP__"))
616 (anything-candidate-buffer)))
617 (desc "anything-candidate-buffer register-buffer")
618 (expect " *anything test candidates*"
619 (let (anything-candidate-buffer-alist
620 (buf (get-buffer-create " *anything test candidates*")))
621 (with-current-buffer buf
622 (insert "1\n2\n")
623 (prog1 (buffer-name (anything-candidate-buffer buf))
624 (kill-buffer (current-buffer))))))
625 (expect " *anything test candidates*"
626 (let (anything-candidate-buffer-alist
627 (buf (get-buffer-create " *anything test candidates*")))
628 (with-current-buffer buf
629 (insert "1\n2\n")
630 (anything-candidate-buffer buf)
631 (prog1 (buffer-name (anything-candidate-buffer))
632 (kill-buffer (current-buffer))))))
633 (expect "1\n2\n"
634 (let (anything-candidate-buffer-alist
635 (buf (get-buffer-create " *anything test candidates*")))
636 (with-current-buffer buf
637 (insert "1\n2\n")
638 (anything-candidate-buffer buf)
639 (prog1 (buffer-string)
640 (kill-buffer (current-buffer))))))
641 (expect "buf1"
642 (let (anything-candidate-buffer-alist
643 (anything-source-name "foo")
644 (buf1 (get-buffer-create "buf1"))
645 (buf2 (get-buffer-create "buf2")))
646 (anything-candidate-buffer buf1)
647 (anything-candidate-buffer buf2)
648 (prog1 (buffer-name (anything-candidate-buffer buf1))
649 (kill-buffer buf1)
650 (kill-buffer buf2))))
651 (desc "action attribute")
652 (expect "foo"
653 (anything-test-candidates
654 '(((name . "TEST")
655 (candidates "foo")
656 (action ("identity" . identity)))))
657 (anything-execute-selection-action))
658 (expect "foo"
659 (anything-test-candidates
660 '(((name . "TEST")
661 (candidates "foo")
662 (action ("identity" . (lambda (c) (identity c)))))))
663 (anything-execute-selection-action))
664 (desc "anything-get-default-action")
665 (expect 'upcase
666 (anything-get-default-action '(("upcase" . upcase))))
667 (expect 'downcase
668 (anything-get-default-action '(("downcase" . downcase))))
669 (expect (lambda (x) (capitalize x))
670 (anything-get-default-action (lambda (x) (capitalize x))))
671 (expect 'identity
672 (anything-get-default-action 'identity))
673 (desc "anything-execute-selection-action")
674 (expect "FOO"
675 (anything-execute-selection-action
676 "foo" '(("upcase" . upcase)) nil))
677 (expect "FOO"
678 (anything-execute-selection-action
679 "foo" '(("upcase" . (lambda (c) (upcase c)))) nil))
681 (desc "display-to-real attribute")
682 (expect "FOO"
683 (anything-test-candidates
684 '(((name . "TEST")
685 (candidates "foo")
686 (display-to-real . upcase)
687 (action ("identity" . identity)))))
688 (anything-execute-selection-action))
689 (desc "cleanup test")
690 (expect 'cleaned
691 (let (v)
692 (anything-test-candidates
693 '(((name . "TEST")
694 (cleanup . (lambda () (setq v 'cleaned))))))
696 (desc "anything-get-current-source")
697 ;; in init/candidates/action/candidate-transformer/filtered-candidate-transformer
698 ;; display-to-real/cleanup function
699 (expect "FOO"
700 (assoc-default
701 'name
702 (anything-funcall-with-source '((name . "FOO")) 'anything-get-current-source)))
703 ;; init
704 (expect "FOO"
705 (let (v)
706 (anything-test-candidates
707 '(((name . "FOO")
708 (init . (lambda () (setq v (anything-get-current-source)))))))
709 (assoc-default 'name v)))
710 ;; candidates
711 (expect "FOO"
712 (let (v)
713 (anything-test-candidates
714 '(((name . "FOO")
715 (candidates . (lambda () (setq v (anything-get-current-source)) '("a"))))))
716 (assoc-default 'name v)))
717 ;; action
718 (expect "FOO"
719 (let (v)
720 (anything-test-candidates
721 '(((name . "FOO")
722 (candidates "a")
723 (action
724 . (lambda (c) (setq v (anything-get-current-source)) c)))))
725 (anything-execute-selection-action)
726 (assoc-default 'name v)))
727 ;; candidate-transformer
728 (expect "FOO"
729 (let (v)
730 (anything-test-candidates
731 '(((name . "FOO")
732 (candidates "a")
733 (candidate-transformer
734 . (lambda (c) (setq v (anything-get-current-source)) c)))))
735 (assoc-default 'name v)))
736 ;; filtered-candidate-transformer
737 (expect "FOO"
738 (let (v)
739 (anything-test-candidates
740 '(((name . "FOO")
741 (candidates "a")
742 (filtered-candidate-transformer
743 . (lambda (c s) (setq v (anything-get-current-source)) c)))))
744 (assoc-default 'name v)))
745 ;; action-transformer
746 (expect "FOO"
747 (let (v)
748 (anything-test-candidates
749 '(((name . "FOO")
750 (candidates "a")
751 (action-transformer
752 . (lambda (a c) (setq v (anything-get-current-source)) a))
753 (action . identity))))
754 (anything-execute-selection-action)
755 (assoc-default 'name v)))
756 ;; display-to-real
757 (expect "FOO"
758 (let (v)
759 (anything-test-candidates
760 '(((name . "FOO")
761 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
762 (insert "a\n"))))
763 (candidates-in-buffer)
764 (display-to-real
765 . (lambda (c) (setq v (anything-get-current-source)) c))
766 (action . identity))))
767 (anything-execute-selection-action)
768 (assoc-default 'name v)))
769 ;; cleanup
770 (expect "FOO"
771 (let (v)
772 (anything-test-candidates
773 '(((name . "FOO")
774 (candidates "a")
775 (cleanup
776 . (lambda () (setq v (anything-get-current-source)))))))
777 (assoc-default 'name v)))
778 ;; candidates are displayed
779 (expect "TEST"
780 (anything-test-candidates
781 '(((name . "TEST")
782 (candidates "foo")
783 (action ("identity" . identity)))))
784 (assoc-default 'name (anything-get-current-source)))
785 (desc "anything-attr")
786 (expect "FOO"
787 (anything-funcall-with-source
788 '((name . "FOO"))
789 (lambda ()
790 (anything-attr 'name))))
791 (expect 'fuga
792 (let (v)
793 (anything-test-candidates
794 '(((name . "FOO")
795 (hoge . fuga)
796 (init . (lambda () (setq v (anything-attr 'hoge))))
797 (candidates "a"))))
799 (expect nil
800 (let (v)
801 (anything-test-candidates
802 '(((name . "FOO")
803 (init . (lambda () (setq v (anything-attr 'hoge))))
804 (candidates "a"))))
806 (expect nil
807 (let (v)
808 (anything-test-candidates
809 '(((name . "FOO")
810 (hoge) ;INCOMPATIBLE!
811 (init . (lambda () (setq v (anything-attr 'hoge))))
812 (candidates "a"))))
814 (desc "anything-attr*")
815 (expect "generic"
816 (let (v (value1 "generic"))
817 (anything-test-candidates
818 '(((name . "FOO")
819 (hoge . value1)
820 (init . (lambda () (setq v (anything-attr* 'hoge)))))))
822 (desc "anything-attr-defined")
823 (expect (non-nil)
824 (let (v)
825 (anything-test-candidates
826 '(((name . "FOO")
827 (hoge)
828 (init . (lambda () (setq v (anything-attr-defined 'hoge))))
829 (candidates "a"))))
830 v))
831 (expect nil
832 (let (v)
833 (anything-test-candidates
834 '(((name . "FOO")
835 (init . (lambda () (setq v (anything-attr-defined 'hoge))))
836 (candidates "a"))))
837 v))
838 (desc "anything-attrset")
839 (expect '((name . "FOO") (hoge . 77))
840 (let ((src '((name . "FOO") (hoge))))
841 (anything-attrset 'hoge 77 src)
842 src))
843 (expect 77
844 (anything-attrset 'hoge 77 '((name . "FOO") (hoge))))
846 (expect '((name . "FOO") (hoge . 77))
847 (let ((src '((name . "FOO") (hoge . 1))))
848 (anything-attrset 'hoge 77 src)
849 src))
851 (expect '((name . "FOO") (hoge . 77) (x))
852 (let ((src '((name . "FOO") (x))))
853 (anything-attrset 'hoge 77 src)
854 src))
855 (expect 77
856 (anything-attrset 'hoge 77 '((name . "FOO"))))
857 (desc "anything-preselect")
858 ;; entire candidate
859 (expect "foo"
860 (with-current-buffer (anything-create-anything-buffer t)
861 (let ((anything-pattern "")
862 (anything-test-mode t))
863 (anything-process-source '((name . "test")
864 (candidates "hoge" "foo" "bar")))
865 (anything-preselect "foo")
866 (anything-get-selection))))
867 ;; regexp
868 (expect "foo"
869 (with-current-buffer (anything-create-anything-buffer t)
870 (let ((anything-pattern "")
871 (anything-test-mode t))
872 (anything-process-source '((name . "test")
873 (candidates "hoge" "foo" "bar")))
874 (anything-preselect "fo+")
875 (anything-get-selection))))
876 ;; no match -> first entry
877 (expect "hoge"
878 (with-current-buffer (anything-create-anything-buffer t)
879 (let ((anything-pattern "")
880 (anything-test-mode t))
881 (anything-process-source '((name . "test")
882 (candidates "hoge" "foo" "bar")))
883 (anything-preselect "not found")
884 (anything-get-selection))))
885 (desc "anything-check-new-input")
886 (expect "newpattern"
887 (stub anything-update)
888 (stub anything-action-window)
889 (let ((anything-pattern "pattern"))
890 (anything-check-new-input "newpattern")
891 anything-pattern))
892 ;; anything-input == nil when action window is available
893 (expect nil
894 (stub anything-update)
895 (stub anything-action-window => t)
896 (let ((anything-pattern "pattern")
897 anything-input)
898 (anything-check-new-input "newpattern")
899 anything-input))
900 ;; anything-input == anything-pattern unless action window is available
901 (expect "newpattern"
902 (stub anything-update)
903 (stub anything-action-window => nil)
904 (let ((anything-pattern "pattern")
905 anything-input)
906 (anything-check-new-input "newpattern")
907 anything-input))
908 (expect (mock (anything-update))
909 (stub anything-action-window)
910 (let (anything-pattern)
911 (anything-check-new-input "foo")))
912 (desc "anything-update")
913 (expect (mock (anything-process-source '((name . "1"))))
914 (anything-test-update '(((name . "1"))) ""))
915 ;; (find-function 'anything-update)
916 ;; TODO el-mock.el should express 2nd call of function.
917 ;; (expect (mock (anything-process-source '((name . "2"))))
918 ;; (stub anything-get-sources => '(((name . "1")) ((name . "2"))))
919 ;; (stub anything-log-run-hook)
920 ;; (stub anything-maybe-fit-frame)
921 ;; (stub run-with-idle-timer)
922 ;; (anything-update))
923 (expect (mock (run-with-idle-timer * nil 'anything-process-delayed-sources
924 '(((name . "2") (delayed)))))
925 (stub anything-get-sources => '(((name . "1"))
926 ((name . "2") (delayed))))
927 (stub anything-log-run-hook)
928 (stub anything-maybe-fit-frame)
929 (let ((anything-pattern "") anything-test-mode)
930 (anything-update)))
932 (desc "requires-pattern attribute")
933 (expect (not-called anything-process-source)
934 (anything-test-update '(((name . "1") (requires-pattern))) ""))
935 (expect (not-called anything-process-source)
936 (anything-test-update '(((name . "1") (requires-pattern . 3))) "xx"))
938 (desc "anything-normalize-sources")
939 (expect '(anything-c-source-test)
940 (anything-normalize-sources 'anything-c-source-test))
941 (expect '(anything-c-source-test)
942 (anything-normalize-sources '(anything-c-source-test)))
943 (expect '(anything-c-source-test)
944 (let ((anything-sources '(anything-c-source-test)))
945 (anything-normalize-sources nil)))
946 (expect '(((name . "test")))
947 (anything-normalize-sources '((name . "test"))))
948 (expect '(((name . "test")))
949 (anything-normalize-sources '(((name . "test")))))
950 (desc "anything-get-action")
951 (expect '(("identity" . identity))
952 (stub buffer-size => 1)
953 (stub anything-get-current-source => '((name . "test")
954 (action ("identity" . identity))))
955 (anything-get-action))
956 (expect 'identity
957 (stub buffer-size => 1)
958 (stub anything-get-current-source => '((name . "test")
959 (action . identity)))
960 (anything-get-action))
961 (expect '((("identity" . identity)) "action-transformer is called")
962 (stub buffer-size => 1)
963 (stub anything-get-current-source
964 => '((name . "test")
965 (action ("identity" . identity))
966 (action-transformer
967 . (lambda (actions selection)
968 (list actions selection)))))
969 (stub anything-get-selection => "action-transformer is called")
970 (anything-get-action))
971 (desc "anything-select-nth-action")
972 (expect (error error *)
973 (stub anything-get-selection => nil)
974 (anything-select-nth-action 0))
975 (desc "anything-get-nth-action")
976 (expect 'cadr
977 (anything-get-nth-action 2 '(("0" . car) ("1" . cdr) ("2" . cadr))))
978 (expect (error error *)
979 (anything-get-nth-action 2 '(("0" . car))))
980 (expect 'identity
981 (anything-get-nth-action 0 'identity))
982 (expect (error error *)
983 (anything-get-nth-action 1 'identity))
984 (expect (error error *)
985 (anything-get-nth-action 0 'unbound-function-xxx))
986 (expect (error error *)
987 (anything-get-nth-action 0 "invalid data"))
988 (desc "anything-funcall-foreach")
989 (expect (mock (upcase "foo"))
990 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
991 (anything-funcall-foreach 'init))
992 (expect (mock (downcase "bar"))
993 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))
994 ((init . (lambda () (downcase "bar"))))))
995 (anything-funcall-foreach 'init))
996 (expect (not-called anything-funcall-with-source)
997 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
998 (anything-funcall-foreach 'not-found))
999 ;; TODO anything-select-with-digit-shortcut test
1000 (desc "anything-get-cached-candidates")
1001 (expect '("cached" "version")
1002 (let ((anything-candidate-cache '(("test" "cached" "version"))))
1003 (anything-get-cached-candidates '((name . "test")
1004 (candidates "new")))))
1005 (expect '("new")
1006 (let ((anything-candidate-cache '(("other" "cached" "version"))))
1007 (anything-get-cached-candidates '((name . "test")
1008 (candidates "new")))))
1009 (expect '(("test" "new")
1010 ("other" "cached" "version"))
1011 (let ((anything-candidate-cache '(("other" "cached" "version"))))
1012 (anything-get-cached-candidates '((name . "test")
1013 (candidates "new")))
1014 anything-candidate-cache))
1015 (expect '(("other" "cached" "version"))
1016 (let ((anything-candidate-cache '(("other" "cached" "version"))))
1017 (anything-get-cached-candidates '((name . "test")
1018 (candidates "new")
1019 (volatile)))
1020 anything-candidate-cache))
1021 ;; TODO when candidates == process
1022 ;; TODO anything-output-filter
1023 (desc "candidate-number-limit attribute")
1024 (expect '("a" "b")
1025 (let ((anything-pattern "")
1026 (anything-candidate-number-limit 20))
1027 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
1028 (candidate-number-limit . 2) (volatile)))))
1029 (expect '("a" "b")
1030 (let ((anything-pattern "[abc]")
1031 (anything-candidate-number-limit 20))
1032 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
1033 (candidate-number-limit . 2) (volatile)))))
1034 (expect '("a" "b" "c" "d")
1035 (let ((anything-pattern "[abcd]")
1036 (anything-candidate-number-limit 2))
1037 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c" "d")
1038 (candidate-number-limit) (volatile)))))
1039 (expect '(("TEST" ("a" "b" "c")))
1040 (let ((anything-candidate-number-limit 2))
1041 (anything-test-candidates
1042 '(((name . "TEST")
1043 (init
1044 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1045 (insert "a\nb\nc\nd\n"))))
1046 (candidates . anything-candidates-in-buffer)
1047 (match identity)
1048 (candidate-number-limit . 3)
1049 (volatile))))))
1050 (expect '(("TEST" ("a" "b" "c")))
1051 (let ((anything-candidate-number-limit 2))
1052 (anything-test-candidates
1053 '(((name . "TEST")
1054 (init
1055 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1056 (insert "a\nb\nc\nd\n"))))
1057 (candidates . anything-candidates-in-buffer)
1058 (match identity)
1059 (candidate-number-limit . 3)
1060 (volatile)))
1061 ".")))
1062 (desc "multiple init")
1063 (expect '(1 . 2)
1064 (let (a b)
1065 (anything-test-candidates
1066 '(((name . "test")
1067 (init (lambda () (setq a 1))
1068 (lambda () (setq b 2))))))
1069 (cons a b)))
1070 (expect 1
1071 (let (a)
1072 (anything-test-candidates
1073 '(((name . "test")
1074 (init (lambda () (setq a 1))))))
1076 (desc "multiple cleanup")
1077 (expect '(1 . 2)
1078 (let (a b)
1079 (anything-test-candidates
1080 '(((name . "test")
1081 (cleanup (lambda () (setq a 1))
1082 (lambda () (setq b 2))))))
1083 (cons a b)))
1084 (desc "anything-mklist")
1085 (expect '(1)
1086 (anything-mklist 1))
1087 (expect '(2)
1088 (anything-mklist '(2)))
1089 (expect '((lambda ()))
1090 (anything-mklist (lambda ())))
1091 (desc "anything-before-initialize-hook")
1092 (expect 'called
1093 (let ((anything-before-initialize-hook '((lambda () (setq v 'called))))
1095 (anything-initial-setup)
1097 (desc "anything-after-initialize-hook")
1098 (expect '(b a)
1099 (let ((anything-before-initialize-hook
1100 '((lambda () (setq v '(a)))))
1101 (anything-after-initialize-hook
1102 '((lambda () (setq v (cons 'b v)))))
1104 (anything-initial-setup)
1106 (expect 0
1107 (let ((anything-after-initialize-hook
1108 '((lambda () (setq v (buffer-size (get-buffer anything-buffer))))))
1110 (anything-initial-setup)
1112 (desc "get-line attribute")
1113 (expect '(("TEST" ("FOO+")))
1114 (anything-test-candidates
1115 '(((name . "TEST")
1116 (init
1117 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1118 (insert "foo+\nbar+\nbaz+\n"))))
1119 (candidates-in-buffer)
1120 (get-line . (lambda (s e) (upcase (buffer-substring-no-properties s e))))))
1121 "oo\\+"))
1122 (desc "with-anything-restore-variables")
1123 (expect '(7 8)
1124 (let ((a 7) (b 8)
1125 (anything-restored-variables '(a b)))
1126 (with-anything-restore-variables
1127 (setq a 0 b 0))
1128 (list a b)))
1129 (desc "anything-cleanup-hook")
1130 (expect 'called
1131 (let ((anything-cleanup-hook
1132 '((lambda () (setq v 'called))))
1134 (anything-cleanup)
1136 (desc "with-anything-display-same-window")
1137 (expect (non-nil)
1138 (save-window-excursion
1139 (delete-other-windows)
1140 (split-window)
1142 (let ((buf (get-buffer-create " tmp"))
1143 (win (selected-window)))
1144 (with-anything-display-same-window
1145 (display-buffer buf)
1146 (eq win (get-buffer-window buf))))))
1147 (expect (non-nil)
1148 (save-window-excursion
1149 (delete-other-windows)
1150 (split-window)
1152 (let ((buf (get-buffer-create " tmp"))
1153 (win (selected-window)))
1154 (with-anything-display-same-window
1155 (pop-to-buffer buf)
1156 (eq win (get-buffer-window buf))))))
1157 (expect (non-nil)
1158 (save-window-excursion
1159 (delete-other-windows)
1160 (split-window)
1162 (let ((buf (get-buffer-create " tmp"))
1163 (win (selected-window)))
1164 (with-anything-display-same-window
1165 (switch-to-buffer buf)
1166 (eq win (get-buffer-window buf))))))
1167 (expect (non-nil)
1168 (save-window-excursion
1169 (delete-other-windows)
1170 (let ((buf (get-buffer-create " tmp"))
1171 (win (selected-window)))
1172 (with-anything-display-same-window
1173 (display-buffer buf)
1174 (eq win (get-buffer-window buf))))))
1175 (expect (non-nil)
1176 (save-window-excursion
1177 (delete-other-windows)
1178 (let ((buf (get-buffer-create " tmp"))
1179 (win (selected-window)))
1180 (with-anything-display-same-window
1181 (pop-to-buffer buf)
1182 (eq win (get-buffer-window buf))))))
1183 (desc "search-from-end attribute")
1184 (expect '(("TEST" ("baz+" "bar+" "foo+")))
1185 (anything-test-candidates
1186 '(((name . "TEST")
1187 (init
1188 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1189 (insert "foo+\nbar+\nbaz+\n"))))
1190 (candidates-in-buffer)
1191 (search-from-end)))))
1192 (expect '(("TEST" ("baz+" "bar+" "foo+")))
1193 (anything-test-candidates
1194 '(((name . "TEST")
1195 (init
1196 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1197 (insert "foo+\nbar+\nbaz+\n"))))
1198 (candidates-in-buffer)
1199 (search-from-end)))
1200 "\\+"))
1201 (expect '(("TEST" ("baz+" "bar+")))
1202 (anything-test-candidates
1203 '(((name . "TEST")
1204 (init
1205 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1206 (insert "foo+\nbar+\nbaz+\n"))))
1207 (candidates-in-buffer)
1208 (search-from-end)
1209 (candidate-number-limit . 2)))))
1210 (expect '(("TEST" ("baz+" "bar+")))
1211 (anything-test-candidates
1212 '(((name . "TEST")
1213 (init
1214 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1215 (insert "foo+\nbar+\nbaz+\n"))))
1216 (candidates-in-buffer)
1217 (search-from-end)
1218 (candidate-number-limit . 2)))
1219 "\\+"))
1220 (expect '(("a" ("c2" "c1")))
1221 (anything-test-candidates
1222 '(((name . "a")
1223 (init . (lambda ()
1224 (with-current-buffer (anything-candidate-buffer 'global)
1225 (insert "c1\nc2\n"))))
1226 (search-from-end)
1227 (candidates-in-buffer)))))
1228 ;; BUG remain empty string, but the pattern is rare case.
1229 (expect '(("a" ("c" "b" "a" "")))
1230 (anything-test-candidates
1231 '(((name . "a")
1232 (init . (lambda ()
1233 (with-current-buffer (anything-candidate-buffer 'global)
1234 (insert "a\nb\nc\n"))))
1235 (search-from-end)
1236 (candidates-in-buffer)))
1237 "a*"))
1238 (desc "header-name attribute")
1239 (expect "original is transformed"
1240 (anything-test-update '(((name . "original")
1241 (candidates "1")
1242 (header-name
1243 . (lambda (name)
1244 (format "%s is transformed" name)))))
1246 (with-current-buffer (anything-buffer-get)
1247 (buffer-string)
1248 (overlay-get (car (overlays-at (1+(point-min)))) 'display)))
1249 (desc "volatile and match attribute")
1250 ;; candidates function is called once per `anything-process-delayed-sources'
1251 (expect 1
1252 (let ((v 0))
1253 (anything-test-candidates '(((name . "test")
1254 (candidates . (lambda () (incf v) '("ok")))
1255 (volatile)
1256 (match identity identity identity)))
1257 "o")
1259 (desc "accept-empty attribute")
1260 (expect nil
1261 (anything-test-candidates
1262 '(((name . "test") (candidates "") (action . identity))))
1263 (anything-execute-selection-action))
1264 (expect ""
1265 (anything-test-candidates
1266 '(((name . "test") (candidates "") (action . identity) (accept-empty))))
1267 (anything-execute-selection-action))
1268 (desc "anything-tick-hash")
1269 (expect nil
1270 (with-current-buffer (get-buffer-create " *00create+*")
1271 (puthash " *00create+*/xxx" 1 anything-tick-hash)
1272 (kill-buffer (current-buffer)))
1273 (gethash " *00create+*/xxx" anything-tick-hash))
1274 (desc "anything-execute-action-at-once-if-once")
1275 (expect "HOGE"
1276 (let ((anything-execute-action-at-once-if-one t))
1277 (anything '(((name . "one test1")
1278 (candidates "hoge")
1279 (action . upcase))))))
1280 (expect "ANY"
1281 (let ((anything-execute-action-at-once-if-one t))
1282 (anything '(((name . "one test2")
1283 (candidates "hoge" "any")
1284 (action . upcase)))
1285 "an")))
1286 ;; candidates > 1
1287 (expect (mock (read-string "word: " nil))
1288 (let ((anything-execute-action-at-once-if-one t))
1289 (anything '(((name . "one test3")
1290 (candidates "hoge" "foo" "bar")
1291 (action . identity)))
1292 nil "word: ")))
1293 (desc "anything-quit-if-no-candidate")
1294 (expect nil
1295 (let ((anything-quit-if-no-candidate t))
1296 (anything '(((name . "zero test1") (candidates) (action . upcase))))))
1297 (expect 'called
1298 (let (v (anything-quit-if-no-candidate (lambda () (setq v 'called))))
1299 (anything '(((name . "zero test2") (candidates) (action . upcase))))
1301 (desc "real-to-display attribute")
1302 (expect '(("test" (("DDD" . "ddd"))))
1303 (anything-test-candidates '(((name . "test")
1304 (candidates "ddd")
1305 (real-to-display . upcase)
1306 (action . identity)))))
1307 (expect '(("test" (("DDD" . "ddd"))))
1308 (anything-test-candidates '(((name . "test")
1309 (candidates ("ignored" . "ddd"))
1310 (real-to-display . upcase)
1311 (action . identity)))))
1312 (expect '(("Commands" (("xxxhoge" . "hoge") ("xxxboke" . "boke"))))
1313 (anything-test-candidates '(((name . "Commands")
1314 (candidates
1315 "hoge" "boke")
1316 (real-to-display . (lambda (x) (concat "xxx" x)))
1317 (action . identity)))
1318 "xxx"))
1319 (expect "test\nDDD\n"
1320 (anything-test-update '(((name . "test")
1321 (candidates "ddd")
1322 (real-to-display . upcase)
1323 (action . identity)))
1325 (with-current-buffer (anything-buffer-get) (buffer-string)))
1326 (desc "real-to-display and candidate-transformer attribute")
1327 (expect '(("test" (("DDD" . "ddd"))))
1328 (anything-test-candidates
1329 '(((name . "test")
1330 (candidates "ddd")
1331 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c)) cands)))
1332 (real-to-display . upcase)
1333 (action . identity)))))
1334 (expect "test\nDDD\n"
1335 (anything-test-update
1336 '(((name . "test")
1337 (candidates "ddd")
1338 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c)) cands)))
1339 (real-to-display . upcase)
1340 (action . identity)))
1342 (with-current-buffer (anything-buffer-get) (buffer-string)))
1343 (desc "real-to-display and candidates-in-buffer")
1344 (expect '(("test" (("A" . "a") ("B" . "b"))))
1345 (anything-test-candidates
1346 '(((name . "test")
1347 (init
1348 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1349 (erase-buffer)
1350 (insert "a\nb\n"))))
1351 (candidates-in-buffer)
1352 (real-to-display . upcase)
1353 (action . identity)))))
1354 (expect "test\nA\nB\n"
1355 (stub read-string)
1356 (anything
1357 '(((name . "test")
1358 (init
1359 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1360 (erase-buffer)
1361 (insert "a\nb\n"))))
1362 (candidates-in-buffer)
1363 (real-to-display . upcase)
1364 (action . identity))))
1365 (with-current-buffer (anything-buffer-get) (buffer-string)))
1366 (desc "Symbols are acceptable as candidate.")
1367 (expect '(("test" (sym "str")))
1368 (anything-test-candidates
1369 '(((name . "test")
1370 (candidates sym "str")))))
1371 (expect '(("test" ((sym . realsym) ("str" . "realstr"))))
1372 (anything-test-candidates
1373 '(((name . "test")
1374 (candidates (sym . realsym) ("str" . "realstr"))))))
1375 (expect '(("test" (sym)))
1376 (anything-test-candidates
1377 '(((name . "test")
1378 (candidates sym "str")))
1379 "sym"))
1380 (expect '(("test" ("str")))
1381 (anything-test-candidates
1382 '(((name . "test")
1383 (candidates sym "str")))
1384 "str"))
1385 (expect '(("test" ((sym . realsym))))
1386 (anything-test-candidates
1387 '(((name . "test")
1388 (candidates (sym . realsym) ("str" . "realstr"))))
1389 "sym"))
1390 (expect '(("test" (("str" . "realstr"))))
1391 (anything-test-candidates
1392 '(((name . "test")
1393 (candidates (sym . realsym) ("str" . "realstr"))))
1394 "str"))
1395 (desc "multiple transformers")
1396 (expect '(("test" ("<FOO>")))
1397 (anything-test-candidates
1398 '(((name . "test")
1399 (candidates "foo")
1400 (candidate-transformer
1401 . (lambda (cands)
1402 (anything-compose (list cands)
1403 (list (lambda (c) (mapcar 'upcase c))
1404 (lambda (c) (list (concat "<" (car c) ">")))))))))))
1405 (expect '("<FOO>")
1406 (anything-composed-funcall-with-source
1407 '((name . "test"))
1408 (list (lambda (c) (mapcar 'upcase c))
1409 (lambda (c) (list (concat "<" (car c) ">"))))
1410 '("foo"))
1412 (expect '(("test" ("<FOO>")))
1413 (anything-test-candidates
1414 '(((name . "test")
1415 (candidates "foo")
1416 (candidate-transformer
1417 (lambda (c) (mapcar 'upcase c))
1418 (lambda (c) (list (concat "<" (car c) ">"))))))))
1419 (expect '(("test" ("<BAR>")))
1420 (anything-test-candidates
1421 '(((name . "test")
1422 (candidates "bar")
1423 (filtered-candidate-transformer
1424 (lambda (c s) (mapcar 'upcase c))
1425 (lambda (c s) (list (concat "<" (car c) ">"))))))))
1426 (expect '(("find-file" . find-file)
1427 ("view-file" . view-file))
1428 (stub zerop => nil)
1429 (stub anything-get-current-source
1430 => '((name . "test")
1431 (action)
1432 (action-transformer
1433 . (lambda (a s)
1434 (anything-compose
1435 (list a s)
1436 (list (lambda (a s) (push '("view-file" . view-file) a))
1437 (lambda (a s) (push '("find-file" . find-file) a))))))))
1438 (anything-get-action))
1439 (expect '(("find-file" . find-file)
1440 ("view-file" . view-file))
1441 (stub zerop => nil)
1442 (stub anything-get-current-source
1443 => '((name . "test")
1444 (action)
1445 (action-transformer
1446 (lambda (a s) (push '("view-file" . view-file) a))
1447 (lambda (a s) (push '("find-file" . find-file) a)))))
1448 (anything-get-action))
1449 (desc "define-anything-type-attribute")
1450 (expect '((file (action . find-file)))
1451 (let (anything-type-attributes)
1452 (define-anything-type-attribute 'file '((action . find-file)))
1453 anything-type-attributes))
1454 (expect '((file (action . find-file)))
1455 (let ((anything-type-attributes '((file (action . view-file)))))
1456 (define-anything-type-attribute 'file '((action . find-file)))
1457 anything-type-attributes))
1458 (expect '((file (action . find-file))
1459 (buffer (action . switch-to-buffer)))
1460 (let (anything-type-attributes)
1461 (define-anything-type-attribute 'buffer '((action . switch-to-buffer)))
1462 (define-anything-type-attribute 'file '((action . find-file)))
1463 anything-type-attributes))
1464 (desc "anything-approximate-candidate-number")
1465 (expect 0
1466 (with-temp-buffer
1467 (let ((anything-buffer (current-buffer)))
1468 (anything-approximate-candidate-number))))
1469 (expect 1
1470 (with-temp-buffer
1471 (let ((anything-buffer (current-buffer)))
1472 (insert "Title\n"
1473 "candiate1\n")
1474 (anything-approximate-candidate-number))))
1475 (expect t
1476 (with-temp-buffer
1477 (let ((anything-buffer (current-buffer)))
1478 (insert "Title\n"
1479 "candiate1\n"
1480 "candiate2\n")
1481 (<= 2 (anything-approximate-candidate-number)))))
1482 (expect 1
1483 (with-temp-buffer
1484 (let ((anything-buffer (current-buffer)))
1485 (insert "Title\n"
1486 (propertize "multi\nline\n" 'anything-multiline t))
1487 (anything-approximate-candidate-number))))
1488 (expect t
1489 (with-temp-buffer
1490 (let ((anything-buffer (current-buffer))
1491 (anything-candidate-separator "-----"))
1492 (insert "Title\n"
1493 (propertize "multi\nline1\n" 'anything-multiline t)
1494 "-----\n"
1495 (propertize "multi\nline2\n" 'anything-multiline t))
1496 (<= 2 (anything-approximate-candidate-number)))))
1497 (desc "delayed-init attribute")
1498 (expect 0
1499 (let ((value 0))
1500 (anything-test-candidates '(((name . "test")
1501 (delayed-init . (lambda () (incf value)))
1502 (candiates "abc")
1503 (requires-pattern . 2)))
1505 value))
1506 (expect 1
1507 (let ((value 0))
1508 (anything-test-candidates '(((name . "test")
1509 (delayed-init . (lambda () (incf value)))
1510 (candiates "abc")
1511 (requires-pattern . 2)))
1512 "abc")
1513 value))
1514 (expect 2
1515 (let ((value 0))
1516 (anything-test-candidates '(((name . "test")
1517 (delayed-init (lambda () (incf value))
1518 (lambda () (incf value)))
1519 (candiates "abc")
1520 (requires-pattern . 2)))
1521 "abc")
1522 value))
1523 (expect t
1524 (let (value)
1525 (with-temp-buffer
1526 (anything-test-candidates '(((name . "test")
1527 (delayed-init
1528 . (lambda () (setq value
1529 (eq anything-current-buffer (current-buffer)))))
1530 (candiates "abc")
1531 (requires-pattern . 2)))
1532 "abc")
1533 value)))
1534 (desc "pattern-transformer attribute")
1535 (expect '(("test2" ("foo")) ("test3" ("bar")))
1536 (anything-test-candidates '(((name . "test1")
1537 (candidates "foo" "bar"))
1538 ((name . "test2")
1539 (pattern-transformer . (lambda (pat) (substring pat 1)))
1540 (candidates "foo" "bar"))
1541 ((name . "test3")
1542 (pattern-transformer . (lambda (pat) "bar"))
1543 (candidates "foo" "bar")))
1544 "xfoo"))
1545 (expect '(("test2" ("foo")) ("test3" ("bar")))
1546 (anything-test-candidates '(((name . "test1")
1547 (candidates "foo" "bar"))
1548 ((name . "test2")
1549 (pattern-transformer (lambda (pat) (substring pat 1)))
1550 (candidates "foo" "bar"))
1551 ((name . "test3")
1552 (pattern-transformer (lambda (pat) "bar"))
1553 (candidates "foo" "bar")))
1554 "xfoo"))
1555 (expect '(("test2" ("foo")) ("test3" ("bar")))
1556 (anything-test-candidates '(((name . "test1")
1557 (init
1558 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1559 (insert "foo\nbar\n"))))
1560 (candidates-in-buffer))
1561 ((name . "test2")
1562 (pattern-transformer . (lambda (pat) (substring pat 1)))
1563 (init
1564 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1565 (insert "foo\nbar\n"))))
1566 (candidates-in-buffer))
1567 ((name . "test3")
1568 (pattern-transformer . (lambda (pat) "bar"))
1569 (init
1570 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
1571 (insert "foo\nbar\n"))))
1572 (candidates-in-buffer)))
1573 "xfoo"))
1574 (desc "anything-recent-push")
1575 (expect '("foo" "bar" "baz")
1576 (let ((lst '("bar" "baz")))
1577 (anything-recent-push "foo" 'lst)))
1578 (expect '("foo" "bar" "baz")
1579 (let ((lst '("foo" "bar" "baz")))
1580 (anything-recent-push "foo" 'lst)))
1581 (expect '("foo" "bar" "baz")
1582 (let ((lst '("bar" "foo" "baz")))
1583 (anything-recent-push "foo" 'lst)))
1584 (desc "anything-require-at-least-version")
1585 (expect nil
1586 (anything-require-at-least-version "1.1"))
1587 (expect nil
1588 (anything-require-at-least-version "1.200"))
1589 (expect nil
1590 (anything-require-at-least-version
1591 (and (string-match "1\.\\([0-9]+\\)" anything-version)
1592 (match-string 0 anything-version))))
1593 (expect (error)
1594 (anything-require-at-least-version "1.999"))
1595 (expect (error)
1596 (anything-require-at-least-version "1.2000"))
1597 (desc "anything-once")
1598 (expect 2
1599 (let ((i 0))
1600 (anything-test-candidates
1601 '(((name . "1")
1602 (init . (lambda () (incf i))))
1603 ((name . "2")
1604 (init . (lambda () (incf i))))))
1606 (expect 1
1607 (let ((i 0))
1608 (anything-test-candidates
1609 '(((name . "1")
1610 (init . (lambda () (anything-once (lambda () (incf i))))))
1611 ((name . "2")
1612 (init . (lambda () (anything-once (lambda () (incf i))))))))
1614 (expect 1
1615 (let ((i 0))
1616 (flet ((init1 () (anything-once (lambda () (incf i)))))
1617 (anything-test-candidates
1618 '(((name . "1")
1619 (init . init1))
1620 ((name . "2")
1621 (init . init1)))))
1623 (desc "anything-marked-candidates")
1624 (expect '("mark3" "mark1")
1625 (let* ((source '((name . "mark test")))
1626 (anything-marked-candidates
1627 `((,source . "mark1")
1628 (((name . "other")) . "mark2")
1629 (,source . "mark3"))))
1630 (stub anything-buffer-get => (current-buffer))
1631 (stub anything-get-current-source => source)
1632 (anything-marked-candidates)))
1633 (expect '("current")
1634 (let* ((source '((name . "mark test")))
1635 (anything-marked-candidates nil))
1636 (stub anything-get-current-source => source)
1637 (stub anything-get-selection => "current")
1638 (anything-marked-candidates)))
1639 (desc "anything-marked-candidates with coerce")
1640 (expect '(mark3 mark1)
1641 (let* ((source '((name . "mark test")
1642 (coerce . intern)))
1643 (anything-marked-candidates
1644 `((,source . "mark1")
1645 (((name . "other")) . "mark2")
1646 (,source . "mark3"))))
1647 (stub anything-buffer-get => (current-buffer))
1648 (stub anything-get-current-source => source)
1649 (anything-marked-candidates)))
1650 (desc "anything-let")
1651 (expect '(1 10000 nil)
1652 (let ((a 9999)
1653 (b 8)
1655 (anything-buffer (exps-tmpbuf)))
1656 (anything-let ((a 1)
1657 (b (1+ a))
1659 (anything-create-anything-buffer))
1660 (with-current-buffer anything-buffer
1661 (list a b c))))
1662 (expect (non-nil)
1663 (let ((a 9999)
1664 (b 8)
1666 (anything-buffer (exps-tmpbuf)))
1667 (anything-let ((a 1)
1668 (b (1+ a))
1670 (anything-create-anything-buffer))
1671 (with-current-buffer anything-buffer
1672 (and (assq 'a (buffer-local-variables))
1673 (assq 'b (buffer-local-variables))
1674 (assq 'c (buffer-local-variables))))))
1675 (expect 'retval
1676 (let ((a 9999)
1677 (b 8)
1679 (anything-buffer (exps-tmpbuf)))
1680 (anything-let ((a 1)
1681 (b (1+ a))
1683 'retval)))
1684 (desc "anything-let*")
1685 (expect '(1 2 nil)
1686 (let ((a 9999)
1687 (b 8)
1689 (anything-buffer (exps-tmpbuf)))
1690 (anything-let* ((a 1)
1691 (b (1+ a))
1693 (anything-create-anything-buffer))
1694 (with-current-buffer anything-buffer
1695 (list a b c))))
1696 (expect (non-nil)
1697 (let ((a 9999)
1698 (b 8)
1700 (anything-buffer (exps-tmpbuf)))
1701 (anything-let* ((a 1)
1702 (b (1+ a))
1704 (anything-create-anything-buffer))
1705 (with-current-buffer anything-buffer
1706 (and (assq 'a (buffer-local-variables))
1707 (assq 'b (buffer-local-variables))
1708 (assq 'c (buffer-local-variables))))))
1709 (expect 'retval*
1710 (let ((a 9999)
1711 (b 8)
1713 (anything-buffer (exps-tmpbuf)))
1714 (anything-let* ((a 1)
1715 (b (1+ a))
1717 'retval*)))
1718 (desc "anything with keyword")
1719 (expect (mock (anything-internal 'test-source "input" "prompt: " nil "preselect" "*test*" nil))
1720 (anything :sources 'test-source
1721 :input "input"
1722 :prompt "prompt: "
1723 :resume nil
1724 :preselect "preselect"
1725 :buffer "*test*"
1726 :keymap nil))
1727 (expect (mock (anything-internal 'test-source nil nil nil nil "*test*" nil))
1728 (anything :sources 'test-source
1729 :buffer "*test*"
1730 :candidate-number-limit 20))
1731 (expect (mock (anything-internal 'test-source nil nil nil nil "*test*" nil))
1732 (anything 'test-source nil nil nil nil "*test*" nil))
1733 (desc "anything-log-eval-internal")
1734 (expect (mock (anything-log "%S = %S" '(+ 1 2) 3))
1735 (anything-log-eval-internal '((+ 1 2))))
1736 (expect (mock (anything-log "%S = ERROR!" 'unDeFined))
1737 (anything-log-eval-internal '(unDeFined)))
1739 (desc "anything-output-filter--collect-candidates")
1740 (expect '("a" "b" "")
1741 (split-string "a\nb\n" "\n"))
1742 (expect '("a" "b")
1743 (anything-output-filter--collect-candidates
1744 '("a" "b" "") (cons 'incomplete-line "")))
1745 (expect '("a" "b")
1746 (split-string "a\nb" "\n"))
1747 (expect '("a")
1748 (anything-output-filter--collect-candidates
1749 '("a" "b") (cons 'incomplete-line "")))
1750 (expect '(incomplete-line . "b")
1751 (let ((incomplete-line-info (cons 'incomplete-line "")))
1752 (anything-output-filter--collect-candidates
1753 '("a" "b") incomplete-line-info)
1754 incomplete-line-info))
1755 (expect '("" "c" "")
1756 (split-string "\nc\n" "\n"))
1757 (expect '("b" "c")
1758 ;; "a\nb" + "\nc\n"
1759 (let ((incomplete-line-info (cons 'incomplete-line "")))
1760 (anything-output-filter--collect-candidates
1761 '("a" "b") incomplete-line-info)
1762 (anything-output-filter--collect-candidates
1763 '("" "c" "") incomplete-line-info)))
1764 (desc "coerce attribute")
1765 (expect "string"
1766 (anything :sources '(((name . "test")
1767 (candidates "string")
1768 (action . identity)))
1769 :execute-action-at-once-if-one t))
1770 (expect 'symbol
1771 (anything :sources '(((name . "test")
1772 (candidates "symbol")
1773 (coerce . intern)
1774 (action . identity)))
1775 :execute-action-at-once-if-one t))
1776 (expect 'real
1777 (anything :sources '(((name . "test")
1778 (candidates ("display" . "real"))
1779 (coerce . intern)
1780 (action . identity)))
1781 :execute-action-at-once-if-one t))
1782 (expect 'real
1783 (anything :sources '(((name . "test")
1784 (candidates)
1785 (candidate-transformer
1786 (lambda (c) '(("display" . "real"))))
1787 (coerce . intern)
1788 (action . identity)))
1789 :execute-action-at-once-if-one t))
1790 (expect 'real
1791 (anything :sources '(((name . "test")
1792 (candidates)
1793 (filtered-candidate-transformer
1794 (lambda (c s) '(("display" . "real"))))
1795 (coerce . intern)
1796 (action . identity)))
1797 :execute-action-at-once-if-one t))
1798 (expect 'real
1799 (anything :sources '(((name . "test")
1800 (candidates "dummy")
1801 (display-to-real (lambda (disp) "real"))
1802 (coerce . intern)
1803 (action . identity)))
1804 :execute-action-at-once-if-one t))
1805 (desc "anything-next-point-in-list")
1806 (expect 10
1807 (anything-next-point-in-list 5 '(10 20) nil))
1808 (expect 20
1809 (anything-next-point-in-list 15 '(10 20) nil))
1810 (expect 25
1811 (anything-next-point-in-list 25 '(10 20) nil))
1812 (expect 5
1813 (anything-next-point-in-list 5 '(10 20) t))
1814 (expect 10
1815 (anything-next-point-in-list 15 '(10 20) t))
1816 (expect 20
1817 (anything-next-point-in-list 25 '(10 20) t))
1818 (expect 5
1819 (anything-next-point-in-list 5 '() nil))
1820 (expect 5
1821 (anything-next-point-in-list 5 '() t))
1822 (expect 10
1823 (anything-next-point-in-list 5 '(10) nil))
1824 (expect 10
1825 (anything-next-point-in-list 15 '(10) t))
1826 (expect 20
1827 (anything-next-point-in-list 10 '(10 20) nil))
1828 (expect 10
1829 (anything-next-point-in-list 20 '(10 20) t))
1830 (expect 20
1831 (anything-next-point-in-list 30 '(10 20 30) t))