summaryrefslogtreecommitdiff
path: root/tests/ss-org-tests.el
blob: b8e1eb5b9bbd97ebd014c212137dcc607dfed7e4 (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
;;; ss-org-tests.el --- Tests for ss-org -*- lexical-binding: t; -*-

;;; Commentary:

;; Focused ERT coverage for Org refile configuration.

;;; Code:

(add-to-list 'load-path (expand-file-name "../lisp" (file-name-directory load-file-name)))

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

(ert-deftest ss-org-configure-refile-sets-org-variables ()
  (let ((old-refile-targets (and (boundp 'org-refile-targets)
             org-refile-targets))
    (old-refile-use-outline-path (and (boundp 'org-refile-use-outline-path)
              org-refile-use-outline-path))
    (old-outline-path-complete-in-steps
     (and (boundp 'org-outline-path-complete-in-steps)
      org-outline-path-complete-in-steps)))
    (unwind-protect
    (progn
        (advice-remove 'org-refile #'ss-org-refresh-agenda-files-for-refile)
      (setq org-refile-targets nil
        org-refile-use-outline-path nil
        org-outline-path-complete-in-steps t)
      (ss-org-configure-refile)
      (should (equal org-refile-targets
                         '((org-agenda-files :regexp . "^\\*+ "))))
      (should (eq org-refile-use-outline-path 'file))
      (should-not org-outline-path-complete-in-steps))
  (setq org-refile-targets old-refile-targets
    org-refile-use-outline-path old-refile-use-outline-path
    org-outline-path-complete-in-steps
    old-outline-path-complete-in-steps)
  (advice-remove 'org-refile #'ss-org-refresh-agenda-files-for-refile))))

(ert-deftest ss-org-configure-refile-discovers-headings-in-agenda-files ()
  (let* ((file (make-temp-file "ss-org-refile-" nil ".org"
                               "* Alpha\n** Beta\n"))
         (file-name (file-name-nondirectory file))
         (org-agenda-files (list file))
         (old-refile-targets (and (boundp 'org-refile-targets)
                                  org-refile-targets))
         (old-refile-use-outline-path (and (boundp 'org-refile-use-outline-path)
                                           org-refile-use-outline-path))
         (old-outline-path-complete-in-steps
          (and (boundp 'org-outline-path-complete-in-steps)
               org-outline-path-complete-in-steps))
         targets)
    (unwind-protect
        (progn
            (advice-remove 'org-refile #'ss-org-refresh-agenda-files-for-refile)
          (ss-org-configure-refile)
          (with-current-buffer (find-file-noselect file)
            (setq targets (org-refile-get-targets)))
          (should (assoc (format "%s/Alpha" file-name) targets))
          (should (assoc (format "%s/Alpha/Beta" file-name) targets)))
      (setq org-refile-targets old-refile-targets
            org-refile-use-outline-path old-refile-use-outline-path
            org-outline-path-complete-in-steps
            old-outline-path-complete-in-steps)
      (advice-remove 'org-refile #'ss-org-refresh-agenda-files-for-refile)
      (when-let ((buffer (get-file-buffer file)))
        (kill-buffer buffer))
      (delete-file file))))

(ert-deftest ss-org-configure-refile-adds-refresh-advice ()
  (unwind-protect
      (progn
        (advice-remove 'org-refile #'ss-org-refresh-agenda-files-for-refile)
        (ss-org-configure-refile)
        (should (advice-member-p #'ss-org-refresh-agenda-files-for-refile
                                 'org-refile)))
    (advice-remove 'org-refile #'ss-org-refresh-agenda-files-for-refile)))

(ert-deftest ss-org-refresh-agenda-files-for-refile-reuses-agenda-helper ()
  (let (called)
    (cl-letf (((symbol-function 'ss-refresh-org-agenda-files)
               (lambda (&rest _args)
                 (setq called t))))
      (ss-org-refresh-agenda-files-for-refile)
      (should called))))

;;; ss-org-tests.el ends here