aboutsummaryrefslogtreecommitdiff
path: root/tests/test-gloss-display--pick-definition.el
blob: 0c11fe1929bef086cbe86c4dda27d73d3c90fc5e (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
;;; test-gloss-display--pick-definition.el --- Tests for gloss-display-pick-definition -*- lexical-binding: t -*-

;; SPDX-License-Identifier: GPL-3.0-or-later

;;; Commentary:
;; Tests for `gloss-display-pick-definition' covering Normal/Boundary/Error
;; cases.  The picker mocks `completing-read' at the boundary; the function
;; under test is responsible for building the alist of formatted-string ->
;; plist and mapping the user's choice back to the chosen plist.

;;; Code:

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

(defmacro gloss-test--with-completing-read (return-value &rest body)
  "Run BODY with `completing-read' mocked to return RETURN-VALUE.
If RETURN-VALUE is the symbol `quit', simulate the user pressing C-g
by signalling `quit' inside the mock."
  (declare (indent 1) (debug t))
  `(cl-letf (((symbol-function 'completing-read)
              (lambda (&rest _args)
                ,(if (eq return-value 'quit)
                     '(signal 'quit nil)
                   return-value))))
     ,@body))

(ert-deftest test-gloss-display-pick-definition-normal-pick-first-of-three ()
  "Normal: choosing the first row returns the first definition plist."
  (let* ((def1 '(:source wiktionary :text "First sense."))
         (def2 '(:source wiktionary :text "Second sense."))
         (def3 '(:source wiktionary :text "Third sense."))
         (defs (list def1 def2 def3))
         (chosen-row (gloss-display--format-candidate def1)))
    (gloss-test--with-completing-read chosen-row
      (should (equal (gloss-display-pick-definition "term" defs) def1)))))

(ert-deftest test-gloss-display-pick-definition-normal-pick-middle-of-three ()
  "Normal: choosing the middle row returns the middle definition plist."
  (let* ((def1 '(:source wiktionary :text "First sense."))
         (def2 '(:source wiktionary :text "Second sense."))
         (def3 '(:source wiktionary :text "Third sense."))
         (defs (list def1 def2 def3))
         (chosen-row (gloss-display--format-candidate def2)))
    (gloss-test--with-completing-read chosen-row
      (should (equal (gloss-display-pick-definition "term" defs) def2)))))

(ert-deftest test-gloss-display-pick-definition-boundary-single-definition ()
  "Boundary: a single-definition list still returns that plist."
  (let* ((def '(:source wiktionary :text "Only sense."))
         (chosen-row (gloss-display--format-candidate def)))
    (gloss-test--with-completing-read chosen-row
      (should (equal (gloss-display-pick-definition "term" (list def))
                     def)))))

(ert-deftest test-gloss-display-pick-definition-boundary-empty-list ()
  "Boundary: an empty definition list returns nil without prompting."
  (let ((completing-read-called nil))
    (cl-letf (((symbol-function 'completing-read)
               (lambda (&rest _args)
                 (setq completing-read-called t) "")))
      (should-not (gloss-display-pick-definition "term" nil))
      (should-not completing-read-called))))

(ert-deftest test-gloss-display-pick-definition-error-quit-returns-nil ()
  "Error: C-g during the picker (quit signal) returns nil, not propagate."
  (let ((def '(:source wiktionary :text "Only sense.")))
    (gloss-test--with-completing-read quit
      (should-not (gloss-display-pick-definition "term" (list def))))))

(ert-deftest test-gloss-display-pick-definition-prompt-mentions-term ()
  "Normal: the completing-read prompt names the term being disambiguated."
  (let* ((captured-prompt nil)
         (def '(:source wiktionary :text "Only sense."))
         (chosen-row (gloss-display--format-candidate def)))
    (cl-letf (((symbol-function 'completing-read)
               (lambda (prompt &rest _args)
                 (setq captured-prompt prompt)
                 chosen-row)))
      (gloss-display-pick-definition "anaphora" (list def))
      (should (string-match-p "anaphora" captured-prompt)))))

(provide 'test-gloss-display--pick-definition)
;;; test-gloss-display--pick-definition.el ends here