blob: dbbda00d8104aee209fb93991a681a354007e9e9 (
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
;;; test-custom-misc-cj-count-characters-buffer-or-region.el --- Tests for cj/count-characters-buffer-or-region -*- lexical-binding: t; -*-
;;; Commentary:
;; Tests for the cj/count-characters-buffer-or-region function from custom-misc.el
;;
;; This function counts characters in the active region or the entire buffer
;; if no region is active. It displays the count in the minibuffer.
;;; Code:
(require 'ert)
;; Add modules directory to load path
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
;; Stub dependencies before loading the module
(defvar cj/custom-keymap (make-sparse-keymap)
"Stub keymap for testing.")
;; Now load the actual production module
(require 'custom-misc)
;;; Setup and Teardown
(defun test-count-characters-buffer-or-region-setup ()
"Set up test environment."
;; No setup needed
nil)
(defun test-count-characters-buffer-or-region-teardown ()
"Clean up test environment."
;; Clear any active region
(when (use-region-p)
(deactivate-mark)))
;;; Normal Cases
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-normal-whole-buffer-counts-all ()
"Should count all characters in buffer when no region is active."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(insert "Hello, world!")
;; Ensure no region is active
(deactivate-mark)
(let ((message-output nil))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
(should (string-match-p "13 characters.*buffer" message-output)))))
(test-count-characters-buffer-or-region-teardown)))
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-normal-active-region-counts-region ()
"Should count characters in active region."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(insert "Hello, world!")
;; Select "Hello" (positions 1-6)
(goto-char 1)
(push-mark 1)
(goto-char 6)
(activate-mark)
(let ((message-output nil))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
(should (string-match-p "5 characters.*region" message-output)))))
(test-count-characters-buffer-or-region-teardown)))
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-normal-multiline-buffer-counts-all ()
"Should count characters including newlines in buffer."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(insert "Line 1\nLine 2\nLine 3")
(deactivate-mark)
(let ((message-output nil))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
;; 6 + 1 + 6 + 1 + 6 = 20 characters
(should (string-match-p "20 characters.*buffer" message-output)))))
(test-count-characters-buffer-or-region-teardown)))
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-normal-multiline-region-counts-region ()
"Should count characters including newlines in region."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(insert "Line 1\nLine 2\nLine 3")
;; Select first two lines including newlines
(goto-char 1)
(push-mark 1)
(goto-char 14) ; After "Line 1\nLine 2"
(activate-mark)
(let ((message-output nil))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
;; "Line 1\nLine 2" = 6 + 1 + 6 = 13 characters
(should (string-match-p "13 characters.*region" message-output)))))
(test-count-characters-buffer-or-region-teardown)))
;;; Boundary Cases
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-boundary-empty-buffer-returns-zero ()
"Should return 0 for empty buffer."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(deactivate-mark)
(let ((message-output nil))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
(should (string-match-p "0 characters.*buffer" message-output)))))
(test-count-characters-buffer-or-region-teardown)))
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-boundary-empty-region-counts-buffer ()
"Should count whole buffer when region is empty (point equals mark).
When mark and point are at the same position, use-region-p returns nil,
so the function correctly falls back to counting the entire buffer."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(insert "Hello, world!")
;; Create empty region (point equals mark)
;; Even with activate-mark, use-region-p returns nil when mark == point
(goto-char 5)
(push-mark 5)
(activate-mark)
(let ((message-output nil))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
;; Should count the whole buffer (13 characters) not the empty region
(should (string-match-p "13 characters.*buffer" message-output)))))
(test-count-characters-buffer-or-region-teardown)))
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-boundary-large-buffer-counts-all ()
"Should handle very large buffer."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(let ((large-content (make-string 100000 ?x)))
(insert large-content)
(deactivate-mark)
(let ((message-output nil))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
(should (string-match-p "100000 characters.*buffer" message-output))))))
(test-count-characters-buffer-or-region-teardown)))
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-boundary-unicode-counts-correctly ()
"Should count unicode characters (emoji, RTL text) correctly."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(insert "Hello 👋 مرحبا")
(deactivate-mark)
(let ((message-output nil)
(expected-count (- (point-max) (point-min))))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
(should (string-match-p (format "%d characters.*buffer" expected-count)
message-output)))))
(test-count-characters-buffer-or-region-teardown)))
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-boundary-whitespace-only-counts-whitespace ()
"Should count whitespace characters."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(insert " \t\n ")
(deactivate-mark)
(let ((message-output nil))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
;; 3 spaces + 1 tab + 1 newline + 2 spaces = 7 characters
(should (string-match-p "7 characters.*buffer" message-output)))))
(test-count-characters-buffer-or-region-teardown)))
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-boundary-single-character-returns-one ()
"Should return 1 for single character buffer."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(insert "x")
(deactivate-mark)
(let ((message-output nil))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
(should (string-match-p "1 character.*buffer" message-output)))))
(test-count-characters-buffer-or-region-teardown)))
(ert-deftest test-custom-misc-cj-count-characters-buffer-or-region-boundary-narrowed-buffer-counts-visible ()
"Should count only visible characters in narrowed buffer."
(test-count-characters-buffer-or-region-setup)
(unwind-protect
(with-temp-buffer
(insert "Line 1\nLine 2\nLine 3\n")
(goto-char (point-min))
(forward-line 1)
(narrow-to-region (point) (progn (forward-line 1) (point)))
(deactivate-mark)
(let ((message-output nil))
(cl-letf (((symbol-function 'message)
(lambda (format-string &rest args)
(setq message-output (apply #'format format-string args)))))
(cj/count-characters-buffer-or-region)
;; "Line 2\n" = 7 characters
(should (string-match-p "7 characters.*buffer" message-output)))))
(test-count-characters-buffer-or-region-teardown)))
(provide 'test-custom-misc-cj-count-characters-buffer-or-region)
;;; test-custom-misc-cj-count-characters-buffer-or-region.el ends here
|