aboutsummaryrefslogtreecommitdiff
path: root/tests/test-org-refile-config-commands.el
blob: 9bdd33647d9d5ab5ab4385fb79b1dd5e1a9677a1 (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
;;; test-org-refile-config-commands.el --- Tests for org-refile-config command wrappers -*- lexical-binding: t; -*-

;;; Commentary:
;; Sibling tests cover the ensure-org-mode helper and the build-targets
;; pipeline.  This file covers the wrapper commands:
;;
;;   cj/org-refile-refresh-targets
;;   cj/org-refile
;;   cj/org-refile-in-file

;;; Code:

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

(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'org-refile-config)

;; `org-refile-targets' is normally provided by org-refile; the module's
;; `cj/org-refile-in-file' let-binds it.  Declare it at top level so the
;; let-binding is dynamic under lexical scope.
(defvar org-refile-targets nil
  "Dynamic stand-in for `org-refile-targets'.")

;;; cj/org-refile-refresh-targets

(ert-deftest test-org-refile-refresh-targets-forces-rebuild ()
  "Normal: refresh-targets calls build-targets with the force-rebuild flag."
  (let ((forced nil))
    (cl-letf (((symbol-function 'cj/build-org-refile-targets)
               (lambda (&optional flag) (setq forced flag))))
      (cj/org-refile-refresh-targets))
    (should (eq forced 'force-rebuild))))

;;; cj/org-refile

(ert-deftest test-org-refile-builds-targets-then-delegates ()
  "Normal: cj/org-refile builds the cache then calls `org-refile' with the args."
  (let ((built nil)
        (call-args nil))
    (cl-letf (((symbol-function 'cj/build-org-refile-targets)
               (lambda (&optional _) (setq built t)))
              ((symbol-function 'org-refile)
               (lambda (&rest args) (setq call-args args))))
      (cj/org-refile 'arg 'buffer 'rfloc 'msg))
    (should built)
    (should (equal call-args '(arg buffer rfloc msg)))))

;;; cj/org-refile-in-file

(ert-deftest test-org-refile-in-file-uses-current-file-as-target ()
  "Normal: refile-in-file scopes `org-refile-targets' to the current file."
  (let ((seen-targets nil))
    (with-temp-buffer
      (setq buffer-file-name "/tmp/notes.org")
      (cl-letf (((symbol-function 'call-interactively)
                 (lambda (_fn)
                   (setq seen-targets org-refile-targets)))
                ((symbol-function 'save-buffer) #'ignore))
        (cj/org-refile-in-file))
      (setq buffer-file-name nil))
    ;; The let-bound targets are: (((file) :maxlevel . 6))
    (should seen-targets)
    (let* ((entry (car seen-targets))
           (files (car entry)))
      (should (member "/tmp/notes.org" files))
      (should (equal (cdr entry) '(:maxlevel . 6))))))

(ert-deftest test-org-refile-in-file-saves-after-refile ()
  "Normal: refile-in-file calls `save-buffer' after the refile."
  (let ((saved nil))
    (with-temp-buffer
      (setq buffer-file-name "/tmp/notes.org")
      (cl-letf (((symbol-function 'call-interactively) #'ignore)
                ((symbol-function 'save-buffer)
                 (lambda () (setq saved t))))
        (cj/org-refile-in-file))
      (setq buffer-file-name nil))
    (should saved)))

(provide 'test-org-refile-config-commands)
;;; test-org-refile-config-commands.el ends here