aboutsummaryrefslogtreecommitdiff
path: root/tests/test-latex-config.el
blob: cb8aaecafbd855456d364a72d712632a4580e403 (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
;;; test-latex-config.el --- Tests for latex-config PDF viewer selection -*- lexical-binding: t; -*-

;;; Commentary:
;; latex-config picks the first available external PDF viewer and pushes it
;; onto `TeX-view-program-selection' for output-pdf.  These tests exercise
;; `cj/--latex-select-pdf-viewer' with `executable-find' mocked, so they don't
;; depend on which viewers happen to be installed on the test machine.

;;; Code:

(require 'ert)
(require 'cl-lib)
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))

;; Declare special so the let-bindings below are dynamic, matching the global
;; valued `defvar' AUCTeX's tex.el provides at runtime.  Without it the module's
;; file-local `(defvar TeX-view-program-selection)' does not reach a test's let.
(defvar TeX-view-program-selection nil)

(require 'latex-config)

(defun test-latex--with-available (available thunk)
  "Run THUNK with `executable-find' returning non-nil only for AVAILABLE names."
  (cl-letf (((symbol-function 'executable-find)
             (lambda (cmd &rest _)
               (and (member cmd available) (concat "/usr/bin/" cmd)))))
    (funcall thunk)))

(ert-deftest test-latex-config-select-pdf-viewer-picks-available ()
  "Normal: selects the available external viewer for output-pdf."
  (let ((TeX-view-program-selection nil))
    (test-latex--with-available
     '("zathura")
     (lambda () (cj/--latex-select-pdf-viewer)))
    (should (equal (assq 'output-pdf TeX-view-program-selection)
                   '(output-pdf "Zathura")))))

(ert-deftest test-latex-config-select-pdf-viewer-honors-preference-order ()
  "Normal: picks the first candidate in preference order when several exist."
  (let ((TeX-view-program-selection nil))
    ;; both available; evince precedes okular in the candidate list
    (test-latex--with-available
     '("okular" "evince")
     (lambda () (cj/--latex-select-pdf-viewer)))
    (should (equal (assq 'output-pdf TeX-view-program-selection)
                   '(output-pdf "Evince")))))

(ert-deftest test-latex-config-select-pdf-viewer-falls-back-to-pdf-tools ()
  "Boundary: no external viewer on PATH falls back to PDF Tools."
  (let ((TeX-view-program-selection nil))
    (test-latex--with-available
     '()
     (lambda () (cj/--latex-select-pdf-viewer)))
    (should (equal (assq 'output-pdf TeX-view-program-selection)
                   '(output-pdf "PDF Tools")))))

(ert-deftest test-latex-config-select-pdf-viewer-idempotent ()
  "Boundary: re-running leaves exactly one output-pdf entry."
  (let ((TeX-view-program-selection nil))
    (test-latex--with-available
     '("zathura")
     (lambda ()
       (cj/--latex-select-pdf-viewer)
       (cj/--latex-select-pdf-viewer)
       (cj/--latex-select-pdf-viewer)))
    (should (= 1 (cl-count 'output-pdf TeX-view-program-selection :key #'car)))
    (should (equal (assq 'output-pdf TeX-view-program-selection)
                   '(output-pdf "Zathura")))))

(ert-deftest test-latex-config-select-pdf-viewer-overrides-existing-default ()
  "Boundary: an existing output-pdf default is replaced, not stacked."
  (let ((TeX-view-program-selection '((output-pdf "Evince") (output-dvi "xdvi"))))
    (test-latex--with-available
     '("zathura")
     (lambda () (cj/--latex-select-pdf-viewer)))
    (should (= 1 (cl-count 'output-pdf TeX-view-program-selection :key #'car)))
    (should (equal (assq 'output-pdf TeX-view-program-selection)
                   '(output-pdf "Zathura")))
    ;; an unrelated output type survives the replacement
    (should (equal (assq 'output-dvi TeX-view-program-selection)
                   '(output-dvi "xdvi")))))

(provide 'test-latex-config)
;;; test-latex-config.el ends here