dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

diff-hl-test.el (5451B)


      1 ;;; diff-hl-test.el --- tests for diff-hl -*- lexical-binding: t -*-
      2 
      3 ;; Copyright (C) 2020, 2021  Free Software Foundation, Inc.
      4 
      5 ;; Author:   Nathan Moreau <nathan.moreau@m4x.org>
      6 
      7 ;; This file is part of GNU Emacs.
      8 
      9 ;; GNU Emacs is free software: you can redistribute it and/or modify
     10 ;; it under the terms of the GNU General Public License as published by
     11 ;; the Free Software Foundation, either version 3 of the License, or
     12 ;; (at your option) any later version.
     13 
     14 ;; GNU Emacs is distributed in the hope that it will be useful,
     15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 ;; GNU General Public License for more details.
     18 
     19 ;; You should have received a copy of the GNU General Public License
     20 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 ;;; Commentary:
     23 
     24 ;;; Code:
     25 
     26 (require 'diff-hl)
     27 (require 'subr-x) ;; string-trim
     28 (require 'ert)
     29 (require 'vc-git)
     30 
     31 (defvar diff-hl-test-source-file
     32   (expand-file-name (concat (file-name-directory (locate-library "diff-hl"))
     33                             "test/empty")))
     34 
     35 (defvar diff-hl-test-initial-content nil)
     36 
     37 (defmacro diff-hl-test-in-source (&rest body)
     38   `(save-window-excursion
     39      (find-file diff-hl-test-source-file)
     40      ,@body))
     41 (put 'diff-hl-test-in-source 'lisp-indent-function 0)
     42 
     43 (defun diff-hl-test-init ()
     44   (diff-hl-test-in-source
     45     (setq diff-hl-test-initial-content (buffer-string)))
     46   t)
     47 
     48 (defun diff-hl-test-teardown ()
     49   (diff-hl-test-in-source
     50     (erase-buffer)
     51     (insert diff-hl-test-initial-content)
     52     (save-buffer)
     53     (pcase (vc-backend buffer-file-name)
     54       (`Git
     55        (vc-git-command nil 0 buffer-file-name "reset"))
     56       (`Hg
     57        (vc-hg-command nil 0 buffer-file-name "revert")))))
     58 
     59 (defun diff-hl-test-compute-diff-lines ()
     60   (diff-hl-test-in-source
     61     (save-buffer)
     62     (let ((vc-diff-switches "-w"))
     63       (diff-hl-diff-goto-hunk))
     64     (switch-to-buffer "*vc-diff*")
     65     (let ((lines nil)
     66           (previous-line (point-min)))
     67       (goto-char (point-min))
     68       (while (< (point) (point-max))
     69         (forward-line 1)
     70         (push (string-trim (buffer-substring-no-properties previous-line (point))) lines)
     71         (setq previous-line (point)))
     72       (delq nil (nreverse lines)))))
     73 
     74 (defmacro diff-hl-deftest (name &rest body)
     75   `(ert-deftest ,name ()
     76      (diff-hl-test-init)
     77      (unwind-protect
     78          (progn ,@body)
     79        (diff-hl-test-teardown))))
     80 (put 'diff-hl-deftest 'lisp-indent-function 'defun)
     81 
     82 (diff-hl-deftest diff-hl-insert ()
     83   (diff-hl-test-in-source
     84     (goto-char (point-max))
     85     (insert "added\n")
     86     (should (equal "+added"
     87                    (car (last (diff-hl-test-compute-diff-lines)))))))
     88 
     89 (diff-hl-deftest diff-hl-remove ()
     90   (diff-hl-test-in-source
     91     (delete-region (point-min) (point-max))
     92     (should (equal "-last line"
     93                    (car (last (diff-hl-test-compute-diff-lines)))))))
     94 
     95 (diff-hl-deftest diff-hl-indirect-buffer-insert ()
     96   (diff-hl-test-in-source
     97     (narrow-to-region (point-min) (point-max))
     98     (goto-char (point-max))
     99     (insert "added\n")
    100     (should (equal "+added"
    101                    (car (last (diff-hl-test-compute-diff-lines)))))))
    102 
    103 (diff-hl-deftest diff-hl-indirect-buffer-remove ()
    104   (diff-hl-test-in-source
    105     (narrow-to-region (point-min) (point-max))
    106     (goto-char (point-min))
    107     (delete-region (point) (point-max))
    108     (should (equal "-last line"
    109                    (car (last (diff-hl-test-compute-diff-lines)))))))
    110 
    111 (diff-hl-deftest diff-hl-indirect-buffer-move ()
    112   (diff-hl-test-in-source
    113     (narrow-to-region (point-min) (point-max))
    114     (goto-char (point-min))
    115     (kill-whole-line 3)
    116     (goto-char (point-max))
    117     (insert "added\n")
    118     (save-buffer)
    119     (diff-hl-mode 1)
    120     (diff-hl-previous-hunk)
    121     (should (looking-at "added"))
    122     (diff-hl-previous-hunk)
    123     (should (looking-at "function2"))
    124     (should-error (diff-hl-previous-hunk) :type 'user-error)
    125     (diff-hl-next-hunk)
    126     (should (looking-at "added"))
    127     (should-error (diff-hl-next-hunk) :type 'user-error)))
    128 
    129 (diff-hl-deftest diff-hl-can-ignore-staged-changes ()
    130   (diff-hl-test-in-source
    131     (goto-char (point-min))
    132     (insert "new line 1\n")
    133     (save-buffer)
    134     (vc-git-command nil 0 buffer-file-name "add")
    135     (goto-char (point-max))
    136     (insert "new line 2\n")
    137     (save-buffer)
    138     (let ((diff-hl-show-staged-changes t))
    139       (should
    140        (equal (diff-hl-changes)
    141               '((1 1 insert)
    142                 (12 1 insert)))))
    143     (let ((diff-hl-show-staged-changes nil))
    144       (should
    145        (equal (diff-hl-changes)
    146               '((12 1 insert)))))))
    147 
    148 (diff-hl-deftest diff-hl-flydiff-can-ignore-staged-changes ()
    149   (diff-hl-test-in-source
    150     (goto-char (point-min))
    151     (insert "new line 1\n")
    152     (save-buffer)
    153     (vc-git-command nil 0 buffer-file-name "add")
    154     (goto-char (point-max))
    155     (insert "new line 2\n")
    156     (let ((diff-hl-show-staged-changes t))
    157       (should
    158        (equal (diff-hl-changes-from-buffer
    159                (diff-hl-diff-buffer-with-reference buffer-file-name))
    160               '((1 1 insert)
    161                 (12 1 insert)))))
    162     (let ((diff-hl-show-staged-changes nil))
    163       (should
    164        (equal (diff-hl-changes-from-buffer
    165                (diff-hl-diff-buffer-with-reference buffer-file-name))
    166               '((12 1 insert)))))))
    167 
    168 (defun diff-hl-run-tests ()
    169   (ert-run-tests-batch))
    170 
    171 (provide 'diff-hl-test)
    172 
    173 ;;; diff-hl-test.el ends here