blob: 5a846e7f184629dfbdd6d15b33d7a7568c36df7f (
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
|
;;; test-custom-whitespace-remove-leading-trailing.el --- Tests for cj/--remove-leading-trailing-whitespace -*- lexical-binding: t; -*-
;;; Commentary:
;; Tests for the cj/--remove-leading-trailing-whitespace function from custom-whitespace.el
;;
;; This function removes leading and trailing whitespace (spaces and tabs) from text.
;; - Removes leading whitespace: ^[ \t]+
;; - Removes trailing whitespace: [ \t]+$
;; - Preserves interior whitespace
;; - Operates on any region defined by START and END
;;
;; We test the NON-INTERACTIVE implementation (cj/--remove-leading-trailing-whitespace)
;; to avoid mocking region selection and prefix arguments. This follows our testing
;; best practice of separating business logic from UI interaction.
;;; 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.")
;; Now load the actual production module
(require 'custom-whitespace)
;;; Test Helpers
(defun test-remove-leading-trailing (input-text)
"Test cj/--remove-leading-trailing-whitespace on INPUT-TEXT.
Returns the buffer string after operation."
(with-temp-buffer
(insert input-text)
(cj/--remove-leading-trailing-whitespace (point-min) (point-max))
(buffer-string)))
;;; Normal Cases
(ert-deftest test-remove-leading-trailing-leading-spaces ()
"Should remove leading spaces from single line."
(let ((result (test-remove-leading-trailing " hello world")))
(should (string= result "hello world"))))
(ert-deftest test-remove-leading-trailing-trailing-spaces ()
"Should remove trailing spaces from single line."
(let ((result (test-remove-leading-trailing "hello world ")))
(should (string= result "hello world"))))
(ert-deftest test-remove-leading-trailing-both-spaces ()
"Should remove both leading and trailing spaces."
(let ((result (test-remove-leading-trailing " hello world ")))
(should (string= result "hello world"))))
(ert-deftest test-remove-leading-trailing-leading-tabs ()
"Should remove leading tabs from single line."
(let ((result (test-remove-leading-trailing "\t\thello world")))
(should (string= result "hello world"))))
(ert-deftest test-remove-leading-trailing-trailing-tabs ()
"Should remove trailing tabs from single line."
(let ((result (test-remove-leading-trailing "hello world\t\t")))
(should (string= result "hello world"))))
(ert-deftest test-remove-leading-trailing-mixed-tabs-spaces ()
"Should remove mixed tabs and spaces."
(let ((result (test-remove-leading-trailing " \t hello world \t ")))
(should (string= result "hello world"))))
(ert-deftest test-remove-leading-trailing-preserve-interior ()
"Should preserve interior whitespace."
(let ((result (test-remove-leading-trailing " hello world \t")))
(should (string= result "hello world"))))
(ert-deftest test-remove-leading-trailing-multiple-lines ()
"Should handle multiple lines with leading/trailing whitespace."
(let ((result (test-remove-leading-trailing " line1 \n\t\tline2\t\n line3 ")))
(should (string= result "line1\nline2\nline3"))))
(ert-deftest test-remove-leading-trailing-multiline-preserve-interior ()
"Should preserve interior whitespace on multiple lines."
(let ((result (test-remove-leading-trailing " hello world \n foo bar ")))
(should (string= result "hello world\nfoo bar"))))
;;; Boundary Cases
(ert-deftest test-remove-leading-trailing-empty-string ()
"Should handle empty string."
(let ((result (test-remove-leading-trailing "")))
(should (string= result ""))))
(ert-deftest test-remove-leading-trailing-single-char ()
"Should handle single character with surrounding spaces."
(let ((result (test-remove-leading-trailing " x ")))
(should (string= result "x"))))
(ert-deftest test-remove-leading-trailing-only-whitespace ()
"Should handle lines with only whitespace."
(let ((result (test-remove-leading-trailing " \t ")))
(should (string= result ""))))
(ert-deftest test-remove-leading-trailing-no-whitespace ()
"Should handle text with no leading/trailing whitespace (no-op)."
(let ((result (test-remove-leading-trailing "hello world")))
(should (string= result "hello world"))))
(ert-deftest test-remove-leading-trailing-very-long-line ()
"Should handle very long lines with whitespace."
(let* ((long-text (make-string 500 ?x))
(input (concat " " long-text " "))
(result (test-remove-leading-trailing input)))
(should (string= result long-text))))
(ert-deftest test-remove-leading-trailing-whitespace-between-lines ()
"Should handle lines that become empty after removal."
(let ((result (test-remove-leading-trailing "line1\n \nline2")))
(should (string= result "line1\n\nline2"))))
(ert-deftest test-remove-leading-trailing-newlines-only ()
"Should preserve newlines while removing spaces."
(let ((result (test-remove-leading-trailing "\n\n\n")))
(should (string= result "\n\n\n"))))
(ert-deftest test-remove-leading-trailing-partial-region ()
"Should work on partial buffer region."
(with-temp-buffer
(insert " hello \n world \n test ")
;; Only operate on middle line
(let ((start (+ (point-min) 10)) ; Start of second line
(end (+ (point-min) 19))) ; End of second line
(cj/--remove-leading-trailing-whitespace start end)
(should (string= (buffer-string) " hello \nworld\n test ")))))
;;; Error Cases
(ert-deftest test-remove-leading-trailing-start-greater-than-end ()
"Should error when start > end."
(should-error
(with-temp-buffer
(insert "hello world")
(cj/--remove-leading-trailing-whitespace (point-max) (point-min)))
:type 'error))
(ert-deftest test-remove-leading-trailing-empty-region ()
"Should handle empty region (start == end) without error."
(with-temp-buffer
(insert "hello world")
(let ((pos (/ (+ (point-min) (point-max)) 2)))
(cj/--remove-leading-trailing-whitespace pos pos)
;; Should complete without error and not change buffer
(should (string= (buffer-string) "hello world")))))
(provide 'test-custom-whitespace-remove-leading-trailing)
;;; test-custom-whitespace-remove-leading-trailing.el ends here
|