summaryrefslogtreecommitdiff
path: root/tests/test-custom-line-paragraph-underscore-line.el
blob: b3c092e0373e00ad2b7568d6dcf9d939966da25c (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
;;; test-custom-line-paragraph-underscore-line.el --- Tests for cj/underscore-line -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the cj/underscore-line function from custom-line-paragraph.el
;;
;; This function underlines the current line by inserting a row of characters below it.
;; If the line is empty or contains only whitespace, it aborts with a message.
;;
;; The function uses (read-char) to get the underline character from the user.
;; In tests, we mock this using cl-letf.

;;; Code:

(require 'ert)
(require 'testutil-general)

;; 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.")

;; Stub expand-region package
(provide 'expand-region)

;; Now load the actual production module
(require 'custom-line-paragraph)

;;; Setup and Teardown

(defun test-underscore-line-setup ()
  "Setup for underscore-line tests."
  (cj/create-test-base-dir))

(defun test-underscore-line-teardown ()
  "Teardown for underscore-line tests."
  (cj/delete-test-base-dir))

;;; Normal Cases

(ert-deftest test-underscore-line-simple-text ()
  "Should underline simple text line."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Hello World")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          (should (string-match-p "Hello World\n-----------" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-preserve-original ()
  "Should preserve original line text."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Original Text")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?=)))
          (cj/underscore-line)
          (goto-char (point-min))
          (should (looking-at "Original Text"))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-use-specified-character ()
  "Should use the character provided by user."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Test")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?*)))
          (cj/underscore-line)
          (should (string-match-p "\\*\\*\\*\\*" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-match-width ()
  "Should create underline matching line width."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "12345")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          ;; Should have exactly 5 dashes
          (should (string-match-p "12345\n-----$" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-insert-newline ()
  "Should insert newline before underline."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Line")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          (should (= 2 (count-lines (point-min) (point-max))))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-cursor-preserved ()
  "Should preserve cursor position."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Some text here")
        (goto-char (point-min))
        (forward-char 5)  ; Position in middle
        (let ((original-pos (point)))
          (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
            (cj/underscore-line)
            (should (= (point) original-pos)))))
    (test-underscore-line-teardown)))

;;; Boundary Cases

(ert-deftest test-underscore-line-empty-line-aborts ()
  "Should abort on empty line."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "")
        (cj/underscore-line)
        ;; Buffer should remain empty
        (should (string= "" (buffer-string))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-whitespace-only-aborts ()
  "Should abort on whitespace-only line."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "   \t  ")
        (goto-char (point-min))
        (let ((original (buffer-string)))
          (cj/underscore-line)
          ;; Buffer should be unchanged
          (should (string= original (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-single-character ()
  "Should underline single character line."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "X")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          (should (string= "X\n-" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-very-long-line ()
  "Should handle very long lines (5000+ chars)."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (let ((long-line (make-string 5000 ?x)))
          (insert long-line)
          (goto-char (point-min))
          (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
            (cj/underscore-line)
            ;; Should have 5000 dashes
            (should (= 5000 (how-many "-" (point-min) (point-max)))))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-with-tabs ()
  "Should account for tab expansion in column width."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "a\tb")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          ;; Tab expands to column, underline should match visual width
          (let ((underline-length (save-excursion
                                    (goto-char (point-min))
                                    (forward-line 1)
                                    (- (line-end-position) (line-beginning-position)))))
            (should (> underline-length 2)))))  ; More than just "a" and "b"
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-leading-whitespace ()
  "Should include leading whitespace in width calculation."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "  text")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          ;; Should have 6 dashes (2 spaces + 4 chars)
          (should (string= "  text\n------" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-trailing-whitespace ()
  "Should include trailing whitespace in width calculation."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "text  ")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          ;; Should have 6 dashes (4 chars + 2 spaces)
          (should (string= "text  \n------" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-unicode-emoji ()
  "Should handle Unicode and emoji characters."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Hello 👋")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          ;; Should create underline
          (should (string-match-p "Hello 👋\n-" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-rtl-text ()
  "Should handle RTL text."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "مرحبا")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          (should (string-match-p "مرحبا\n-" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-combining-characters ()
  "Should handle Unicode combining characters."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "cafe\u0301")  ; e with combining acute
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          (should (string-match-p "cafe\u0301\n-" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-at-buffer-start ()
  "Should work on first line in buffer."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "First line\nSecond line")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?=)))
          (cj/underscore-line)
          (should (string-match-p "First line\n==========" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-at-buffer-end ()
  "Should work on last line in buffer."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "First line\nLast line")
        (goto-char (point-max))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?=)))
          (cj/underscore-line)
          (should (string-match-p "Last line\n=========$" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-different-characters ()
  "Should work with various underline characters."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Test")
        (goto-char (point-min))
        (dolist (char '(?- ?= ?* ?# ?~ ?_))
          (goto-char (point-min))
          (delete-region (point-min) (point-max))
          (insert "Test")
          (goto-char (point-min))
          (cl-letf (((symbol-function 'read-char) (lambda (&rest _) char)))
            (cj/underscore-line)
            (should (string-match-p (regexp-quote (make-string 4 char)) (buffer-string))))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-special-characters ()
  "Should work with special non-alphanumeric characters."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Text")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?@)))
          (cj/underscore-line)
          (should (string-match-p "@@@@" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-cursor-in-middle ()
  "Should work regardless of cursor position on line."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Hello World")
        (goto-char (point-min))
        (forward-char 6)  ; Position after "Hello "
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          (should (string-match-p "Hello World\n-----------" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-cursor-at-start ()
  "Should work when cursor at line beginning."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Text")
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          (should (string-match-p "Text\n----" (buffer-string)))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-cursor-at-end ()
  "Should work when cursor at line end."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Text")
        (goto-char (point-max))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          (should (string-match-p "Text\n----" (buffer-string)))))
    (test-underscore-line-teardown)))

;;; Error Cases

(ert-deftest test-underscore-line-read-only-buffer ()
  "Should error when attempting to modify read-only buffer."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Read only text")
        (goto-char (point-min))
        (read-only-mode 1)
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (should-error (cj/underscore-line))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-buffer-modified-flag ()
  "Should set buffer modified flag."
  (test-underscore-line-setup)
  (unwind-protect
      (with-temp-buffer
        (insert "Text")
        (set-buffer-modified-p nil)
        (goto-char (point-min))
        (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
          (cj/underscore-line)
          (should (buffer-modified-p))))
    (test-underscore-line-teardown)))

(ert-deftest test-underscore-line-undo-behavior ()
  "Should support undo after underlining."
  (test-underscore-line-setup)
  (unwind-protect
      (let* ((temp-file (expand-file-name "test-undo-underline.txt" cj/test-base-dir))
             (original-content "Test line"))
        ;; Create file with initial content
        (with-temp-file temp-file
          (insert original-content))
        ;; Open file and test undo
        (find-file temp-file)
        (buffer-enable-undo)
        ;; Establish undo history
        (goto-char (point-min))
        (insert " ")
        (delete-char -1)
        (undo-boundary)
        (goto-char (point-min))
        (let ((before-underline (buffer-string)))
          (cl-letf (((symbol-function 'read-char) (lambda (&rest _) ?-)))
            (cj/underscore-line))
          (undo-boundary)
          (let ((after-underline (buffer-string)))
            (should-not (string= before-underline after-underline))
            (undo)
            (should (string= before-underline (buffer-string)))))
        (kill-buffer (current-buffer)))
    (test-underscore-line-teardown)))

(provide 'test-custom-line-paragraph-underscore-line)
;;; test-custom-line-paragraph-underscore-line.el ends here