summaryrefslogtreecommitdiff
path: root/tests/test-undead-buffers-kill-other-window.el
blob: e9371a0fe874ed335681569cd0db12029ade0d78 (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
;;; test-undead-buffers-kill-other-window.el --- Tests for cj/kill-other-window -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the cj/kill-other-window function from undead-buffers.el

;;; Code:

(require 'ert)
(require 'undead-buffers)
(require 'testutil-general)

;;; Setup and Teardown

(defun test-kill-other-window-setup ()
  "Setup for kill-other-window tests."
  (cj/create-test-base-dir)
  (delete-other-windows))

(defun test-kill-other-window-teardown ()
  "Teardown for kill-other-window tests."
  (delete-other-windows)
  (cj/delete-test-base-dir))

;;; Normal Cases

(ert-deftest test-kill-other-window-two-windows-should-delete-other-and-kill-buffer ()
  "With two windows, should delete other window and kill its buffer."
  (test-kill-other-window-setup)
  (unwind-protect
      (let ((buf1 (current-buffer))
            (buf2 (generate-new-buffer "*test-other*")))
        (unwind-protect
            (progn
              (split-window)
              (let ((win1 (selected-window))
                    (win2 (next-window)))
                (set-window-buffer win2 buf2)
                (select-window win1)
                (cj/kill-other-window)
                (should-not (window-live-p win2))
                (should-not (buffer-live-p buf2))))
          (when (buffer-live-p buf2) (kill-buffer buf2))))
    (test-kill-other-window-teardown)))

(ert-deftest test-kill-other-window-two-windows-undead-buffer-should-delete-other-and-bury ()
  "With two windows, undead buffer in other window should be buried."
  (test-kill-other-window-setup)
  (unwind-protect
      (let ((orig (copy-sequence cj/undead-buffer-list))
            (buf1 (current-buffer))
            (buf2 (generate-new-buffer "*test-undead-other*")))
        (unwind-protect
            (progn
              (add-to-list 'cj/undead-buffer-list "*test-undead-other*")
              (split-window)
              (let ((win1 (selected-window))
                    (win2 (next-window)))
                (set-window-buffer win2 buf2)
                (select-window win1)
                (cj/kill-other-window)
                (should-not (window-live-p win2))
                (should (buffer-live-p buf2))))
          (setq cj/undead-buffer-list orig)
          (when (buffer-live-p buf2) (kill-buffer buf2))))
    (test-kill-other-window-teardown)))

;;; Boundary Cases

(ert-deftest test-kill-other-window-single-window-should-only-kill-buffer ()
  "With single window, should only kill the current buffer."
  (test-kill-other-window-setup)
  (unwind-protect
      (let ((buf (generate-new-buffer "*test-single-other*")))
        (unwind-protect
            (progn
              (switch-to-buffer buf)
              (should (one-window-p))
              (cj/kill-other-window)
              (should (one-window-p))
              (should-not (buffer-live-p buf)))
          (when (buffer-live-p buf) (kill-buffer buf))))
    (test-kill-other-window-teardown)))

(ert-deftest test-kill-other-window-three-windows-should-delete-one ()
  "With three windows, should delete one window."
  (test-kill-other-window-setup)
  (unwind-protect
      (let ((buf1 (current-buffer))
            (buf2 (generate-new-buffer "*test-three-1*"))
            (buf3 (generate-new-buffer "*test-three-2*")))
        (unwind-protect
            (progn
              (split-window)
              (split-window)
              (set-window-buffer (nth 1 (window-list)) buf2)
              (set-window-buffer (nth 2 (window-list)) buf3)
              (select-window (car (window-list)))
              (should (= 3 (length (window-list))))
              (cj/kill-other-window)
              (should (= 2 (length (window-list)))))
          (when (buffer-live-p buf2) (kill-buffer buf2))
          (when (buffer-live-p buf3) (kill-buffer buf3))))
    (test-kill-other-window-teardown)))

(ert-deftest test-kill-other-window-wraps-to-first-window-correctly ()
  "Should correctly cycle through windows with other-window."
  (test-kill-other-window-setup)
  (unwind-protect
      (let ((buf1 (current-buffer))
            (buf2 (generate-new-buffer "*test-wrap*")))
        (unwind-protect
            (progn
              (split-window)
              (let ((win2 (next-window)))
                (set-window-buffer win2 buf2)
                (select-window (car (window-list)))
                (cj/kill-other-window)
                (should-not (window-live-p win2))))
          (when (buffer-live-p buf2) (kill-buffer buf2))))
    (test-kill-other-window-teardown)))

(provide 'test-undead-buffers-kill-other-window)
;;; test-undead-buffers-kill-other-window.el ends here