aboutsummaryrefslogtreecommitdiff
path: root/tests/test-gloss-display--format-candidate.el
blob: 29e077d419b75e6077b3ebc1845b0b262cc3a8f7 (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-gloss-display--format-candidate.el --- Tests for gloss-display--format-candidate -*- lexical-binding: t -*-

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

;;; Commentary:
;; Tests for the pure helper `gloss-display--format-candidate' covering
;; Normal/Boundary/Error cases.  The helper takes a definition plist
;; (:source SYM :text STRING) and returns a single-line "[source] text"
;; row suitable for display in `completing-read'.

;;; Code:

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

(ert-deftest test-gloss-display-format-candidate-normal-short-text ()
  "Normal: short text formats as \"[source] text\" with no truncation."
  (let ((result (gloss-display--format-candidate
                 '(:source wiktionary :text "Hello world."))))
    (should (equal result "[wiktionary] Hello world."))))

(ert-deftest test-gloss-display-format-candidate-normal-different-source ()
  "Normal: source symbol is rendered as its name inside brackets."
  (let ((result (gloss-display--format-candidate
                 '(:source dictionary-api :text "Some text."))))
    (should (string-prefix-p "[dictionary-api] " result))))

(ert-deftest test-gloss-display-format-candidate-boundary-empty-text ()
  "Boundary: empty text yields the prefix only."
  (let ((result (gloss-display--format-candidate
                 '(:source wiktionary :text ""))))
    (should (equal result "[wiktionary] "))))

(ert-deftest test-gloss-display-format-candidate-boundary-single-char-text ()
  "Boundary: one-character text is preserved verbatim."
  (let ((result (gloss-display--format-candidate
                 '(:source wiktionary :text "a"))))
    (should (equal result "[wiktionary] a"))))

(ert-deftest test-gloss-display-format-candidate-boundary-unicode-text ()
  "Boundary: unicode characters in text are preserved."
  (let ((result (gloss-display--format-candidate
                 '(:source wiktionary :text "café — naïve résumé"))))
    (should (equal result "[wiktionary] café — naïve résumé"))))

(ert-deftest test-gloss-display-format-candidate-boundary-long-text-truncated ()
  "Boundary: text longer than the cap is truncated with an ellipsis."
  (let* ((long-text (make-string 200 ?x))
         (result (gloss-display--format-candidate
                  (list :source 'wiktionary :text long-text))))
    (should (<= (length result) gloss-display--candidate-max-length))
    (should (string-suffix-p "..." result))
    (should (string-prefix-p "[wiktionary] " result))))

(ert-deftest test-gloss-display-format-candidate-boundary-text-at-cap-not-truncated ()
  "Boundary: text that fits exactly at the cap is not truncated."
  (let* ((prefix-len (length "[wiktionary] "))
         (text (make-string (- gloss-display--candidate-max-length prefix-len) ?y))
         (result (gloss-display--format-candidate
                  (list :source 'wiktionary :text text))))
    (should (= (length result) gloss-display--candidate-max-length))
    (should-not (string-suffix-p "..." result))))

(ert-deftest test-gloss-display-format-candidate-boundary-missing-source ()
  "Boundary: missing :source falls back to the literal \"unknown\" tag."
  (let ((result (gloss-display--format-candidate
                 '(:text "Some text."))))
    (should (string-prefix-p "[unknown] " result))
    (should (string-suffix-p "Some text." result))))

(ert-deftest test-gloss-display-format-candidate-boundary-newlines-collapsed ()
  "Boundary: embedded newlines are collapsed to spaces (single-line row)."
  (let ((result (gloss-display--format-candidate
                 '(:source wiktionary :text "First line.\nSecond line."))))
    (should-not (string-match-p "\n" result))
    (should (string-match-p "First line. Second line." result))))

(ert-deftest test-gloss-display-format-candidate-error-non-list-input ()
  "Error: passing a non-list raises `wrong-type-argument'."
  (should-error (gloss-display--format-candidate "not a plist")
                :type 'wrong-type-argument))

(provide 'test-gloss-display--format-candidate)
;;; test-gloss-display--format-candidate.el ends here