Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Add tests for changes in magit keybindings
Browse files Browse the repository at this point in the history
Use `ert` to detect when any of the bindings evil-magit changes have
changed on the magit side.
  • Loading branch information
justbur committed Dec 7, 2015
1 parent 7c9f633 commit d45808c
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 71 deletions.
81 changes: 81 additions & 0 deletions evil-magit-tests.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
;;; evil-magit-tests.el --- evil-based key bindings for magit

;; Copyright (C) 2015 Justin Burkett

;; Author: Justin Burkett <justin@burkett.cc>
;; Homepage: https://github.com/justbur/evil-magit

;; This file 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, or (at your
;; option) any later version.
;;
;; This file 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.
;;
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.

(require 'evil-magit)

(ert-deftest evil-magit-mode-map-tests ()
"Test that original bindings in `evil-magit-mode-map-bindings'
are correct."
(dolist (binding evil-magit-mode-map-bindings)
(when (nth 4 binding)
(should (eq (lookup-key (symbol-value (nth 1 binding)) (nth 4 binding))
(nth 3 binding))))))

(ert-deftest evil-magit-section-map-tests ()
"Test that original bindings in
`evil-magit-original-section-bindings' are correct."
(dolist (binding evil-magit-original-section-bindings)
(should (eq (lookup-key (symbol-value (nth 0 binding)) (nth 1 binding))
(nth 2 binding)))))

(ert-deftest evil-magit-dispatch-popup-test ()
"Test that the actions in `magit-dispatch-popup' are unchanged"
(should (equal (plist-get evil-magit-dispatch-popup-backup :actions)
'("Popup and dwim commands"
(?A "Cherry-picking" magit-cherry-pick-popup)
(?b "Branching" magit-branch-popup)
(?B "Bisecting" magit-bisect-popup)
(?c "Committing" magit-commit-popup)
(?d "Diffing" magit-diff-popup)
(?D "Change diffs" magit-diff-refresh-popup)
(?e "Ediff dwimming" magit-ediff-dwim)
(?E "Ediffing" magit-ediff-popup)
(?f "Fetching" magit-fetch-popup)
(?F "Pulling" magit-pull-popup)
(?l "Logging" magit-log-popup)
(?m "Merging" magit-merge-popup)
(?M "Remoting" magit-remote-popup)
(?o "Submodules" magit-submodule-popup)
(?P "Pushing" magit-push-popup)
(?r "Rebasing" magit-rebase-popup)
(?t "Tagging" magit-tag-popup)
(?T "Notes" magit-notes-popup)
(?V "Reverting" magit-revert-popup)
(?w "Apply patches" magit-am-popup)
(?W "Format patches" magit-patch-popup)
(?y "Show Refs" magit-show-refs-popup)
(?z "Stashing" magit-stash-popup)
(?! "Running" magit-run-popup)
"Applying changes"
(?a "Apply" magit-apply)
(?s "Stage" magit-stage)
(?u "Unstage" magit-unstage)
nil
(?v "Reverse" magit-reverse)
(?S "Stage all" magit-stage-modified)
(?U "Unstage all" magit-unstage-all)
nil
(?k "Discard" magit-discard)
"\
g refresh current buffer
TAB toggle section at point
RET visit thing at point
C-h m show all key bindings" nil))))
161 changes: 90 additions & 71 deletions evil-magit.el
Original file line number Diff line number Diff line change
Expand Up @@ -221,69 +221,81 @@ evil-magit."
(eval-after-load 'magit-gh-pulls
`(evil-make-overriding-map magit-gh-pulls-mode-map ',evil-magit-state))

(evil-define-key evil-magit-state magit-mode-map
"g" nil
"\C-j" 'magit-section-forward ; was n
"gj" 'magit-section-forward-sibling ; was M-n
"]" 'magit-section-forward-sibling ; was M-n
"\C-k" 'magit-section-backward ; was p
"gk" 'magit-section-backward-sibling ; was M-p
"[" 'magit-section-backward-sibling ; was M-p
"gr" 'magit-refresh ; was g
"gR" 'magit-refresh-all ; was G
"x" 'magit-delete-thing ; was k
"X" 'magit-file-untrack ; was K
"o" 'magit-revert-no-commit ; was v
"O" 'magit-revert-popup ; was V
"\C-r" 'magit-reset ; was x
"|" 'magit-git-command ; was :
">" 'magit-submodule-popup ; was o
;; evil-specific bindings
"j" 'evil-next-visual-line
"k" 'evil-previous-visual-line
"v" 'set-mark-command
"V" 'set-mark-command
"gg" 'evil-goto-first-line
"G" 'evil-goto-line
"\C-d" 'evil-scroll-down
"\C-f" 'evil-scroll-page-down
"\C-b" 'evil-scroll-page-up
":" 'evil-ex
"/" 'evil-search-forward
"n" 'evil-search-next
"N" 'evil-search-previous
"\C-z" 'evil-emacs-state
[escape] 'evil-magit-maybe-deactivate-mark)

(when evil-want-C-u-scroll
(evil-define-key evil-magit-state magit-mode-map "\C-u" 'evil-scroll-up))

(evil-define-key evil-magit-state magit-status-mode-map
"gz" 'magit-jump-to-stashes
"gt" 'magit-jump-to-tracked
"gn" 'magit-jump-to-untracked
"gu" 'magit-jump-to-unstaged
"gs" 'magit-jump-to-staged
"gf" 'magit-jump-to-unpulled
"gp" 'magit-jump-to-unpushed)

(evil-define-key evil-magit-state magit-blob-mode-map
"gj" 'magit-blob-next
"gk" 'magit-blob-previous)

(evil-define-key evil-magit-state magit-diff-mode-map
"gj" 'magit-section-forward
"gd" 'magit-jump-to-diffstat-or-diff)

(evil-define-key 'normal magit-blame-mode-map
"j" 'evil-next-visual-line
"\C-j" 'magit-blame-next-chunk ; was n
"gj" 'magit-blame-next-chunk ; was n
"gJ" 'magit-blame-next-chunk-same-commit ; was N
"k" 'evil-previous-visual-line
"\C-k" 'magit-blame-previous-chunk ; was p
"gk" 'magit-blame-previous-chunk ; was p
"gK" 'magit-blame-previous-chunk-same-commit) ; was P
(defvar evil-magit-mode-map-bindings
`((,evil-magit-state magit-mode-map "g")
(,evil-magit-state magit-mode-map "\C-j" magit-section-forward "n")
(,evil-magit-state magit-mode-map "gj" magit-section-forward-sibling "\M-n")
(,evil-magit-state magit-mode-map "]" magit-section-forward-sibling "\M-n")
(,evil-magit-state magit-mode-map "\C-k" magit-section-backward "p")
(,evil-magit-state magit-mode-map "gk" magit-section-backward-sibling "\M-p")
(,evil-magit-state magit-mode-map "[" magit-section-backward-sibling "\M-p")
(,evil-magit-state magit-mode-map "gr" magit-refresh "g")
(,evil-magit-state magit-mode-map "gR" magit-refresh-all "G")
(,evil-magit-state magit-mode-map "x" magit-delete-thing "k")
(,evil-magit-state magit-mode-map "X" magit-file-untrack "K")
(,evil-magit-state magit-mode-map "o" magit-revert-no-commit "v")
(,evil-magit-state magit-mode-map "O" magit-revert-popup "V")
(,evil-magit-state magit-mode-map "\C-r" magit-reset "x")
(,evil-magit-state magit-mode-map "|" magit-git-command ":")
(,evil-magit-state magit-mode-map ">" magit-submodule-popup "o")
(,evil-magit-state magit-mode-map "j" evil-next-visual-line)
(,evil-magit-state magit-mode-map "k" evil-previous-visual-line)
(,evil-magit-state magit-mode-map "v" set-mark-command)
(,evil-magit-state magit-mode-map "V" set-mark-command)
(,evil-magit-state magit-mode-map "gg" evil-goto-first-line)
(,evil-magit-state magit-mode-map "G" evil-goto-line)
(,evil-magit-state magit-mode-map "\C-d" evil-scroll-down)
(,evil-magit-state magit-mode-map "\C-f" evil-scroll-page-down)
(,evil-magit-state magit-mode-map "\C-b" evil-scroll-page-up)
(,evil-magit-state magit-mode-map ":" evil-ex)
(,evil-magit-state magit-mode-map "/" evil-search-forward)
(,evil-magit-state magit-mode-map "n" evil-search-next)
(,evil-magit-state magit-mode-map "N" evil-search-previous)
,(when evil-want-C-u-scroll
(list evil-magit-state 'magit-mode-map "\C-u" 'evil-scroll-up))
(,evil-magit-state magit-mode-map "\C-z" evil-emacs-state)
(,evil-magit-state magit-mode-map [escape] evil-magit-maybe-deactivate-mark)

(,evil-magit-state magit-status-mode-map "gz" magit-jump-to-stashes "jz")
(,evil-magit-state magit-status-mode-map "gt" magit-jump-to-tracked "jt")
(,evil-magit-state magit-status-mode-map "gn" magit-jump-to-untracked "jn")
(,evil-magit-state magit-status-mode-map "gu" magit-jump-to-unstaged "ju")
(,evil-magit-state magit-status-mode-map "gs" magit-jump-to-staged "js")
(,evil-magit-state magit-status-mode-map "gfu" magit-jump-to-unpulled-from-upstream "jfu")
(,evil-magit-state magit-status-mode-map "gfp" magit-jump-to-unpulled-from-pushremote "jfp")
(,evil-magit-state magit-status-mode-map "gpu" magit-jump-to-unpushed-to-upstream "jpu")
(,evil-magit-state magit-status-mode-map "gpp" magit-jump-to-unpushed-to-pushremote "jpp")

(,evil-magit-state magit-blob-mode-map "gj" magit-blob-next "n")
(,evil-magit-state magit-blob-mode-map "gk" magit-blob-previous "p")

(,evil-magit-state magit-diff-mode-map "gj" magit-section-forward)
(,evil-magit-state magit-diff-mode-map "gd" magit-jump-to-diffstat-or-diff "j")

(normal magit-blame-mode-map "j" evil-next-visual-line)
(normal magit-blame-mode-map "\C-j" magit-blame-next-chunk "n")
(normal magit-blame-mode-map "gj" magit-blame-next-chunk "n")
(normal magit-blame-mode-map "gJ" magit-blame-next-chunk-same-commit "N")
(normal magit-blame-mode-map "k" evil-previous-visual-line)
(normal magit-blame-mode-map "\C-k" magit-blame-previous-chunk "p")
(normal magit-blame-mode-map "gk" magit-blame-previous-chunk "p")
(normal magit-blame-mode-map "gK" magit-blame-previous-chunk-same-commit "P")

(,evil-magit-state git-commit-mode-map "gk" git-commit-prev-message "\M-p")
(,evil-magit-state git-commit-mode-map "gj" git-commit-next-message "\M-n"))
"All evil-magit bindings not in a section map. Each element of
this list takes the form
\(EVIL-STATE MAGIT-MAP NEW-KEY DEF ORIG-KEY)\.
ORIG-KEY is only used for testing purposes, and
denotes the original magit key for this command.")

(dolist (binding evil-magit-mode-map-bindings)
(evil-define-key (nth 0 binding) (symbol-value (nth 1 binding))
(nth 2 binding) (nth 3 binding)))

;; Need to refresh evil keymaps when blame mode is entered.
(add-hook 'magit-blame-mode-hook 'evil-normalize-keymaps)

(eval-after-load 'git-rebase
Expand All @@ -297,14 +309,14 @@ evil-magit."
("s" git-rebase-squash "squash = use commit, but meld into previous commit")
("f" git-rebase-fixup "fixup = like \"squash\", but discard this commit's log message")
("x" git-rebase-exec "exec = run command (the rest of the line) using shell")
("d" git-rebase-kill-line "drop = remove commit") ; was k (still can use C-k)
("d" git-rebase-kill-line "drop = remove commit" "k")
("u" git-rebase-undo "undo last change")
(nil with-editor-finish "tell Git to make it happen")
(nil with-editor-cancel "tell Git that you changed your mind, i.e. abort")
("k" evil-previous-visual-line "move point to previous line") ; was p
("j" evil-next-visual-line "move point to next line") ; was n
("M-k" git-rebase-move-line-up "move the commit at point up") ; was M-p
("M-j" git-rebase-move-line-down "move the commit at point down") ; was M-n
("k" evil-previous-visual-line "move point to previous line" "p")
("j" evil-next-visual-line "move point to next line" "n")
("M-k" git-rebase-move-line-up "move the commit at point up" "\M-p")
("M-j" git-rebase-move-line-down "move the commit at point down" "\M-n")
(nil git-rebase-show-commit "show the commit at point in another buffer")))

(dolist (cmd evil-magit-rebase-commands-w-descriptions)
Expand Down Expand Up @@ -339,12 +351,19 @@ evil-magit."
(remove-hook 'git-rebase-mode-hook 'git-rebase-mode-show-keybindings)
(add-hook 'git-rebase-mode-hook 'evil-magit-add-rebase-messages t)))

(evil-define-key evil-magit-state git-commit-mode-map
"gk" 'git-commit-prev-message
"gj" 'git-commit-next-message)

;; section maps: evil-define-key doesn't work here, because these maps are text overlays

(defvar evil-magit-original-section-bindings
`((magit-file-section-map "v" magit-reverse)
(magit-file-section-map "\C-j" magit-diff-visit-file-worktree)
(magit-hunk-section-map "v" magit-reverse)
(magit-hunk-section-map "\C-j" magit-diff-visit-file-worktree)
(magit-staged-section-map "v" magit-reverse)
(magit-commit-section-map "v" magit-revert-no-commit))
"For testing purposes only. The original magit keybindings that
evil-magit affects.")

;; These should all have no effect on functionality
(define-key magit-file-section-map "v" nil) ; was magit-reverse
(define-key magit-file-section-map [remap magit-revert-no-commit] 'magit-reverse)
Expand Down

0 comments on commit d45808c

Please sign in to comment.