aboutsummaryrefslogtreecommitdiff
path: root/tests/test-prog-general--find-file-respecting-split.el
blob: 6d45c51c0272a9954e3222ee8864770d71edc2a2 (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
;;; test-prog-general--find-file-respecting-split.el --- split-aware file open -*- lexical-binding: t; -*-

;;; Commentary:
;; `cj/--find-file-respecting-split' opens a file in another window when the
;; frame is split and in the current window otherwise.  It backs
;; `cj/open-project-root-todo' (C-c p t) so the project todo lands in the
;; other pane instead of replacing the buffer in the selected window.
;;
;; `find-file' / `find-file-other-window' are stubbed -- they are the file-I/O
;; boundary -- while the window layout is real.

;;; Code:

(require 'ert)
(require 'cl-lib)

(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'prog-general)

(ert-deftest test-prog-general--find-file-respecting-split-single-window-uses-current ()
  "Normal: a single-window frame opens the file in the current window."
  (save-window-excursion
    (delete-other-windows)
    (let (current-arg other-called)
      (cl-letf (((symbol-function 'find-file)
                 (lambda (f) (setq current-arg f)))
                ((symbol-function 'find-file-other-window)
                 (lambda (_f) (setq other-called t))))
        (cj/--find-file-respecting-split "/tmp/proj/todo.org"))
      (should (equal current-arg "/tmp/proj/todo.org"))
      (should-not other-called))))

(ert-deftest test-prog-general--find-file-respecting-split-two-windows-uses-other ()
  "Normal: a two-window split opens the file in the other window."
  (save-window-excursion
    (delete-other-windows)
    (split-window-right)
    (let (other-arg current-called)
      (cl-letf (((symbol-function 'find-file-other-window)
                 (lambda (f) (setq other-arg f)))
                ((symbol-function 'find-file)
                 (lambda (_f) (setq current-called t))))
        (cj/--find-file-respecting-split "/tmp/proj/todo.org"))
      (should (equal other-arg "/tmp/proj/todo.org"))
      (should-not current-called))))

(ert-deftest test-prog-general--find-file-respecting-split-three-windows-uses-other ()
  "Boundary: more than two windows still counts as split -> other window."
  (save-window-excursion
    (delete-other-windows)
    (split-window-right)
    (split-window-below)
    (let (other-called current-called)
      (cl-letf (((symbol-function 'find-file-other-window)
                 (lambda (_f) (setq other-called t)))
                ((symbol-function 'find-file)
                 (lambda (_f) (setq current-called t))))
        (cj/--find-file-respecting-split "/tmp/proj/todo.org"))
      (should other-called)
      (should-not current-called))))

(provide 'test-prog-general--find-file-respecting-split)
;;; test-prog-general--find-file-respecting-split.el ends here