aboutsummaryrefslogtreecommitdiff
path: root/tests/test-gloss--add-finish-internal.el
blob: 688d6f271927e86fe9d89cca3aa90d4d196cdc6d (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
;;; test-gloss--add-finish-internal.el --- Tests for gloss--add-finish-internal -*- lexical-binding: t -*-

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

;;; Commentary:
;; Tests for the pure save-side helper `gloss--add-finish-internal'.
;; The interactive temp-buffer UI is exercised separately at the smoke
;; level; this file covers the term/body validation and the persistence
;; side effect via a real temp glossary.

;;; Code:

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

(ert-deftest test-gloss-add-finish-internal-saves-and-shows ()
  "Normal: a fresh term + body is saved with source `manual' and shown."
  (gloss-test--with-missing-glossary
    (let (shown)
      (cl-letf (((symbol-function 'gloss-display-show-entry)
                 (lambda (term body) (setq shown (list term body)))))
        (gloss--add-finish-internal "newterm" "A new definition.")
        (let ((saved (gloss-core-lookup "newterm")))
          (should saved)
          (should (equal (plist-get saved :body) "A new definition."))
          (should (eq (plist-get saved :source) 'manual)))
        (should (equal shown '("newterm" "A new definition.")))))))

(ert-deftest test-gloss-add-finish-internal-empty-term-raises ()
  "Error: empty TERM raises `user-error'."
  (gloss-test--with-missing-glossary
    (cl-letf (((symbol-function 'gloss-display-show-entry)
               (lambda (_ _) nil)))
      (should-error (gloss--add-finish-internal "" "Body.")
                    :type 'user-error)
      (should-error (gloss--add-finish-internal "   " "Body.")
                    :type 'user-error))))

(ert-deftest test-gloss-add-finish-internal-empty-body-raises ()
  "Error: empty BODY raises `user-error'."
  (gloss-test--with-missing-glossary
    (cl-letf (((symbol-function 'gloss-display-show-entry)
               (lambda (_ _) nil)))
      (should-error (gloss--add-finish-internal "term" "")
                    :type 'user-error)
      (should-error (gloss--add-finish-internal "term" "   \n  ")
                    :type 'user-error))))

(ert-deftest test-gloss-add-finish-internal-trims-body-whitespace ()
  "Boundary: leading/trailing whitespace in BODY is trimmed before save."
  (gloss-test--with-missing-glossary
    (cl-letf (((symbol-function 'gloss-display-show-entry)
               (lambda (_ _) nil)))
      (gloss--add-finish-internal "term" "  Body content.\n\n")
      (let ((saved (gloss-core-lookup "term")))
        (should (equal (plist-get saved :body) "Body content."))))))

(provide 'test-gloss--add-finish-internal)
;;; test-gloss--add-finish-internal.el ends here