summaryrefslogtreecommitdiff
blob: 7318293e61c4d571f12d3fb394ba107047c7221e (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
;;; glep-mode-tests.el --- tests for glep-mode.el  -*-lexical-binding:t-*-

;; Copyright 2024 Gentoo Authors

;; Author: Ulrich Müller <ulm@gentoo.org>
;; Maintainer: <emacs@gentoo.org>

;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 2 of the License, or
;; (at your option) any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(require 'ert)
(require 'glep-mode)

(defmacro glep-mode-test-run-with-fixed-time (&rest body)
  `(cl-letf* ((fixed-time (date-to-time "2024-08-10 00:00:00 +0000"))
	      (orig-fun (symbol-function 'format-time-string))
	      ((symbol-function 'format-time-string)
	       (lambda (fmt-string &optional time zone)
		 (funcall orig-fun fmt-string (or time fixed-time) zone))))
     ,@body))

(defvar glep-mode-test-input nil)

(defun glep-mode-test-input (&rest _args)
  (concat (pop glep-mode-test-input)))

(ert-deftest glep-mode-test-font-lock ()
  (with-temp-buffer
    (glep-mode)
    (insert "---\n"
	    "Author: Larry the Cow\n"
	    "---\n"
	    "Author: not highlighted\n")
    (font-lock-ensure)
    (goto-char (point-min))
    (search-forward "---")
    (should (equal (get-text-property (match-beginning 0) 'face)
		   'font-lock-comment-delimiter-face))
    (search-forward "Author")
    (should (equal (get-text-property (match-beginning 0) 'face)
		   'font-lock-keyword-face))
    (search-forward "Author")
    (should-not (get-text-property (match-beginning 0) 'face))))

(ert-deftest glep-mode-test-update-last-modified ()
  (glep-mode-test-run-with-fixed-time
   (with-temp-buffer
     (insert "---\n"
	     "GLEP: 2\n"
	     "Created: 2003-05-31\n"
	     "Last-Modified: 2023-02-22\n"
	     "---\n")
     (glep-mode-update-last-modified)
     (should (string-equal
	      (buffer-string)
	      (concat "---\n"
		      "GLEP: 2\n"
		      "Created: 2003-05-31\n"
		      "Last-Modified: 2024-08-10\n"
		      "---\n")))
     (erase-buffer)
     (insert "---\n"
	     "GLEP: 2\n"
	     "---\n"
	     ;; Last-Modified outside header must not be touched
	     "Last-Modified: 2023-02-22\n")
     (glep-mode-update-last-modified)
     (should (string-equal
	      (buffer-string)
	      (concat "---\n"
		      "GLEP: 2\n"
		      "---\n"
		      "Last-Modified: 2023-02-22\n"))))))

(ert-deftest glep-mode-test-skeleton ()
  (with-temp-buffer
    (cl-letf (((symbol-function 'read-from-minibuffer)
	       #'glep-mode-test-input)
	      ((symbol-function 'read-string)
	       #'glep-mode-test-input)
	      (buffer-file-name
	       "/home/larry/devmanual/quickstart/text.xml"))
      (setq glep-mode-test-input
	    '("9999"			; GLEP
	      "Skeleton test"		; Title
	      "Larry the Cow"		; Author
	      "Informational"		; Type
	      "Draft"			; Status
	      "1"			; Version
	      ""			; Requires
	      ""))			; Replaces
      (glep-mode-test-run-with-fixed-time
       (glep-mode-insert-skeleton)))
    (goto-char (point-min))
    (search-forward "---\n" nil nil 2)
    (should (string-equal
	     (buffer-substring (point-min) (point))
	     (concat "---\n"
		     "GLEP: 9999\n"
		     "Title: Skeleton test\n"
		     "Author: Larry the Cow\n"
		     "Type: Informational\n"
		     "Status: Draft\n"
		     "Version: 1\n"
		     "Created: 2024-08-10\n"
		     "Last-Modified: 2024-08-10\n"
		     "Post-History: \n"
		     "Content-Type: text/x-rst\n"
		     "---\n")))))

(ert-deftest glep-mode-test-in-preamble-p ()
  (with-temp-buffer
    (let ((preamble "---\nGLEP: 2\n---\n"))
      (insert preamble "Body text.\n")
      (should (equal
	       (glep-mode-preamble-bounds)
	       (list 1 (length preamble)))))
    (goto-char (point-min))
    (should (glep-mode-in-preamble-p (point)))
    (forward-line 3)
    (should-not (glep-mode-in-preamble-p (point)))))

(ert-deftest glep-mode-test-keybindings ()
  (should (equal (lookup-key glep-mode-map "\C-c\C-n")
		 'glep-mode-insert-skeleton))
  (with-temp-buffer
    (glep-mode)
    (should (equal (local-key-binding "\C-c\C-n")
		   'glep-mode-insert-skeleton))))

(provide 'glep-mode-tests)

;; Local Variables:
;; coding: utf-8
;; End:

;;; glep-mode-tests.el ends here