aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMarcin Borkowski2017-05-14 07:09:54 +0200
committerMarcin Borkowski2017-05-14 07:09:54 +0200
commit9a5e864de731e113badbe300b1e4174f103547fa (patch)
tree3afa2ff6f2da1dad927520e55c0ea7f26685683c /test
parent91ccb2661eac1bd5b2bbf87780d9113e75535d6b (diff)
parentaa779b0f15faa114fa5e3f59b17e628b1a837af8 (diff)
downloademacs-9a5e864de731e113badbe300b1e4174f103547fa.tar.gz
emacs-9a5e864de731e113badbe300b1e4174f103547fa.zip
Merge branch 'fix/bug-21072'
Diffstat (limited to 'test')
-rw-r--r--test/lisp/emacs-lisp/lisp-tests.el286
1 files changed, 286 insertions, 0 deletions
diff --git a/test/lisp/emacs-lisp/lisp-tests.el b/test/lisp/emacs-lisp/lisp-tests.el
index 8cba7fc526a..ddbf378683b 100644
--- a/test/lisp/emacs-lisp/lisp-tests.el
+++ b/test/lisp/emacs-lisp/lisp-tests.el
@@ -5,6 +5,7 @@
5;; Author: Aaron S. Hawley <aaron.s.hawley@gmail.com> 5;; Author: Aaron S. Hawley <aaron.s.hawley@gmail.com>
6;; Author: Stefan Monnier <monnier@iro.umontreal.ca> 6;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7;; Author: Daniel Colascione <dancol@dancol.org> 7;; Author: Daniel Colascione <dancol@dancol.org>
8;; Author: Marcin Borkowski <mbork@mbork.pl>
8;; Keywords: internal 9;; Keywords: internal
9 10
10;; GNU Emacs is free software: you can redistribute it and/or modify 11;; GNU Emacs is free software: you can redistribute it and/or modify
@@ -303,5 +304,290 @@
303 ;; abcdefghijklmnopqrstuv 304 ;; abcdefghijklmnopqrstuv
304 i f a scan-error) 305 i f a scan-error)
305 306
307;;; Helpers
308
309(eval-and-compile
310 (defvar elisp-test-point-position-regex "=!\\([a-zA-Z0-9-]+\\)="
311 "A regexp matching placeholders for point position for
312`elisp-tests-with-temp-buffer'."))
313
314;; Copied and heavily modified from `python-tests-with-temp-buffer'
315(defmacro elisp-tests-with-temp-buffer (contents &rest body)
316 "Create an `emacs-lisp-mode' enabled temp buffer with CONTENTS.
317BODY is the code to be executed within the temp buffer. Point is
318always located at the beginning of buffer. CONTENTS is an
319expression that must evaluate to a string at compile time. Words
320of the form =!NAME= in CONTENTS are removed, and a for each one a
321variable called NAME is bound to the position of the word's
322start."
323 (declare (indent 1) (debug (def-form body)))
324 (let* ((var-pos nil)
325 (text (with-temp-buffer
326 (insert (eval contents))
327 (goto-char (point-min))
328 (while (re-search-forward elisp-test-point-position-regex nil t)
329 (push (list (intern (match-string-no-properties 1))
330 (match-beginning 0))
331 var-pos)
332 (delete-region (match-beginning 0)
333 (match-end 0)))
334 (buffer-string))))
335 `(with-temp-buffer
336 (emacs-lisp-mode)
337 (insert ,text)
338 (goto-char (point-min))
339 (let ,var-pos
340 ;; Let the =!POSITION= variables be ignorable.
341 ,@(mapcar (lambda (v-p) `(ignore ,(car v-p))) var-pos)
342 ,@body))))
343
344;;; mark-defun
345
346(eval-and-compile
347 (defvar mark-defun-test-buffer
348 ";; Comment header
349=!before-1=
350\(defun func-1 (arg)
351 =!inside-1=\"docstring\"
352 body)
353=!after-1==!before-2=
354;; Comment before a defun
355\(d=!inside-2=efun func-2 (arg)
356 \"docstring\"
357 body)
358=!after-2==!before-3=
359\(defun func-3 (arg)
360 \"docstring\"=!inside-3=
361 body)
362=!after-3==!before-4=(defun func-4 (arg)
363 \"docstring\"=!inside-4=
364 body)
365=!after-4=
366;; end
367"
368 "Test buffer for `mark-defun'."))
369
370(ert-deftest mark-defun-no-arg-region-inactive ()
371 "Test `mark-defun' with no prefix argument and inactive
372region."
373 (setq last-command nil)
374 (elisp-tests-with-temp-buffer
375 mark-defun-test-buffer
376 ;; mark-defun inside a defun, with comments and an empty line
377 ;; before
378 (goto-char inside-1)
379 (mark-defun)
380 (should (= (point) before-1))
381 (should (= (mark) after-1))
382 ;; mark-defun inside a defun with comments before
383 (deactivate-mark)
384 (goto-char inside-2)
385 (mark-defun)
386 (should (= (point) before-2))
387 (should (= (mark) after-2))
388 ;; mark-defun inside a defun with empty line before
389 (deactivate-mark)
390 (goto-char inside-3)
391 (mark-defun)
392 (should (= (point) before-3))
393 (should (= (mark) after-3))
394 ;; mark-defun inside a defun with another one right before
395 (deactivate-mark)
396 (goto-char inside-4)
397 (mark-defun)
398 (should (= (point) before-4))
399 (should (= (mark) after-4))
400 ;; mark-defun between a comment and a defun
401 (deactivate-mark)
402 (goto-char before-1)
403 (mark-defun)
404 (should (= (point) before-1))
405 (should (= (mark) after-1))
406 ;; mark-defun between defuns
407 (deactivate-mark)
408 (goto-char before-3)
409 (mark-defun)
410 (should (= (point) before-3))
411 (should (= (mark) after-3))
412 ;; mark-defun in comment right before the defun
413 (deactivate-mark)
414 (goto-char before-2)
415 (mark-defun)
416 (should (= (point) before-2))
417 (should (= (mark) after-2))))
418
419(ert-deftest mark-defun-no-arg-region-active ()
420 "Test `mark-defun' with no prefix argument and active
421region."
422 (transient-mark-mode 1)
423 (setq last-command nil)
424 (elisp-tests-with-temp-buffer
425 mark-defun-test-buffer
426 ;; mark-defun when a defun is marked
427 (goto-char before-1)
428 (set-mark after-1)
429 (mark-defun)
430 (should (= (point) before-1))
431 (should (= (mark) after-2))
432 ;; mark-defun when two defuns are marked
433 (deactivate-mark)
434 (goto-char before-1)
435 (set-mark after-2)
436 (mark-defun)
437 (should (= (point) before-1))
438 (should (= (mark) after-3))))
439
440(ert-deftest mark-defun-arg-region-active ()
441 "Test `mark-defun' with a prefix arg and active region."
442 (transient-mark-mode 1)
443 (setq last-command nil)
444 (elisp-tests-with-temp-buffer
445 mark-defun-test-buffer
446 ;; mark-defun with positive arg when a defun is marked
447 (goto-char before-1)
448 (set-mark after-1)
449 (mark-defun 2)
450 (should (= (point) before-1))
451 (should (= (mark) after-3))
452 ;; mark-defun with arg=-1 when a defun is marked
453 (goto-char before-2)
454 (set-mark after-2)
455 (mark-defun -1)
456 (should (= (point) before-1))
457 (should (= (mark) after-2))
458 ;; mark-defun with arg=-2 when a defun is marked
459 (goto-char before-3)
460 (set-mark after-3)
461 (mark-defun -2)
462 (should (= (point) before-1))
463 (should (= (mark) after-3))))
464
465(ert-deftest mark-defun-pos-arg-region-inactive ()
466 "Test `mark-defun' with positive argument and inactive
467 region."
468 (setq last-command nil)
469 (elisp-tests-with-temp-buffer
470 mark-defun-test-buffer
471 ;; mark-defun with positive arg inside a defun
472 (goto-char inside-1)
473 (mark-defun 2)
474 (should (= (point) before-1))
475 (should (= (mark) after-2))
476 ;; mark-defun with positive arg between defuns
477 (deactivate-mark)
478 (goto-char before-3)
479 (mark-defun 2)
480 (should (= (point) before-3))
481 (should (= (mark) after-4))
482 ;; mark-defun with positive arg in a comment
483 (deactivate-mark)
484 (goto-char before-2)
485 (mark-defun 2)
486 (should (= (point) before-2))
487 (should (= (mark) after-3))))
488
489(ert-deftest mark-defun-neg-arg-region-inactive ()
490 "Test `mark-defun' with negative argument and inactive
491 region."
492 (setq last-command nil)
493 (elisp-tests-with-temp-buffer
494 mark-defun-test-buffer
495 ;; mark-defun with arg=-1 inside a defun
496 (goto-char inside-1)
497 (mark-defun -1)
498 (should (= (point) before-1))
499 (should (= (mark) after-1))
500 ;; mark-defun with arg=-1 between defuns
501 (deactivate-mark)
502 (goto-char after-2)
503 (mark-defun -1)
504 (should (= (point) before-2))
505 (should (= (mark) after-2))
506 ;; mark-defun with arg=-1 in a comment
507 ;; (this is probably not an optimal behavior...)
508 (deactivate-mark)
509 (goto-char before-2)
510 (mark-defun -1)
511 (should (= (point) before-1))
512 (should (= (mark) after-1))
513 ;; mark-defun with arg=-2 inside a defun
514 (deactivate-mark)
515 (goto-char inside-4)
516 (mark-defun -2)
517 (should (= (point) before-3))
518 (should (= (mark) after-4))
519 ;; mark-defun with arg=-2 between defuns
520 (deactivate-mark)
521 (goto-char before-3)
522 (mark-defun -2)
523 (should (= (point) before-1))
524 (should (= (mark) after-2)))
525 (elisp-tests-with-temp-buffer ; test case submitted by Drew Adams
526 "(defun a ()
527 nil)
528=!before-b=(defun b ()
529=!in-b= nil)
530=!after-b=;;;;
531\(defun c ()
532 nil)
533"
534 (setq last-command nil)
535 (goto-char in-b)
536 (mark-defun -1)
537 (should (= (point) before-b))
538 (should (= (mark) after-b))))
539
540(ert-deftest mark-defun-bob ()
541 "Test `mark-defun' at the beginning of buffer."
542 ;; Bob, comment, newline, defun
543 (setq last-command nil)
544 (elisp-tests-with-temp-buffer
545 ";; Comment at the bob
546=!before=
547\(defun func (arg)=!inside=
548 \"docstring\"
549 body)
550=!after="
551 (goto-char inside)
552 (mark-defun)
553 (should (= (point) before))
554 (should (= (mark) after)))
555 ;; Bob, newline, comment, defun
556 (elisp-tests-with-temp-buffer
557 "=!before=
558;; Comment before the defun
559\(defun func (arg)=!inside=
560 \"docstring\"
561 body)
562=!after="
563 (goto-char inside)
564 (mark-defun)
565 (should (= (point) before))
566 (should (= (mark) after)))
567 ;; Bob, comment, defun
568 (elisp-tests-with-temp-buffer
569 "=!before=;; Comment at the bob before the defun
570\(defun func (arg)=!inside=
571 \"docstring\"
572 body)
573=!after="
574 (goto-char inside)
575 (mark-defun)
576 (should (= (point) before))
577 (should (= (mark) after)))
578 ;; Bob, newline, comment, newline, defun
579 (elisp-tests-with-temp-buffer
580 "
581;; Comment before the defun
582=!before=
583\(defun func (arg)=!inside=
584 \"docstring\"
585 body)
586=!after="
587 (goto-char inside)
588 (mark-defun)
589 (should (= (point) before))
590 (should (= (mark) after))))
591
306(provide 'lisp-tests) 592(provide 'lisp-tests)
307;;; lisp-tests.el ends here 593;;; lisp-tests.el ends here