blob: 0f09f26d8f5a08af4e179c2ca86d772343d51c55 (
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
|
;;; test-custom-buffer-file-copy-to-top-of-buffer.el --- Tests for cj/copy-to-top-of-buffer -*- lexical-binding: t; -*-
;;; Commentary:
;; Tests for the cj/copy-to-top-of-buffer function from custom-buffer-file.el
;;
;; This function copies all text from the beginning of the buffer to point
;; to the kill ring without modifying the buffer.
;;; 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 ps-print package
(provide 'ps-print)
;; Now load the actual production module
(require 'custom-buffer-file)
;;; Setup and Teardown
(defun test-copy-to-top-setup ()
"Set up test environment."
(setq kill-ring nil))
(defun test-copy-to-top-teardown ()
"Clean up test environment."
(setq kill-ring nil))
;;; Normal Cases
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-normal-point-in-middle-copies-from-beginning ()
"Should copy from beginning to point when point in middle."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(insert "Line 1\nLine 2\nLine 3")
(goto-char (point-min))
(forward-line 2) ; Point at start of "Line 3"
(let ((original-content (buffer-string)))
(cj/copy-to-top-of-buffer)
;; Buffer should be unchanged
(should (equal (buffer-string) original-content))
;; Kill ring should contain from beginning to point
(should (equal (car kill-ring) "Line 1\nLine 2\n"))))
(test-copy-to-top-teardown)))
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-normal-single-line-copies-partial ()
"Should copy partial line content from beginning to middle of line."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(insert "Hello World")
(goto-char (point-min))
(forward-char 5) ; Point after "Hello"
(cj/copy-to-top-of-buffer)
(should (equal (buffer-string) "Hello World"))
(should (equal (car kill-ring) "Hello")))
(test-copy-to-top-teardown)))
;;; Boundary Cases
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-boundary-point-at-end-copies-all ()
"Should copy entire buffer when point at end."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(insert "Line 1\nLine 2\nLine 3")
(goto-char (point-max))
(cj/copy-to-top-of-buffer)
(should (equal (buffer-string) "Line 1\nLine 2\nLine 3"))
(should (equal (car kill-ring) "Line 1\nLine 2\nLine 3")))
(test-copy-to-top-teardown)))
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-boundary-point-at-beginning-copies-empty ()
"Should copy empty string when point at beginning."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(insert "Line 1\nLine 2\nLine 3")
(goto-char (point-min))
(cj/copy-to-top-of-buffer)
(should (equal (buffer-string) "Line 1\nLine 2\nLine 3"))
(should (equal (car kill-ring) "")))
(test-copy-to-top-teardown)))
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-boundary-empty-buffer-copies-empty ()
"Should copy empty string in empty buffer."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(cj/copy-to-top-of-buffer)
(should (equal (buffer-string) ""))
(should (equal (car kill-ring) "")))
(test-copy-to-top-teardown)))
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-boundary-point-at-second-char-copies-one ()
"Should copy first character when point at second character."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(insert "Hello")
(goto-char (1+ (point-min))) ; After 'H'
(cj/copy-to-top-of-buffer)
(should (equal (buffer-string) "Hello"))
(should (equal (car kill-ring) "H")))
(test-copy-to-top-teardown)))
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-boundary-unicode-content-copies-correctly ()
"Should handle unicode content correctly."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(insert "Hello 👋\nمرحبا\nWorld")
(goto-char (point-min))
(forward-line 2) ; Point at start of "World"
(cj/copy-to-top-of-buffer)
(should (equal (buffer-string) "Hello 👋\nمرحبا\nWorld"))
(should (equal (car kill-ring) "Hello 👋\nمرحبا\n")))
(test-copy-to-top-teardown)))
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-boundary-narrowed-buffer-respects-narrowing ()
"Should respect narrowing and only copy within narrowed region."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(insert "Line 1\nLine 2\nLine 3\nLine 4")
(goto-char (point-min))
(forward-line 1)
(let ((start (point)))
(forward-line 2)
(narrow-to-region start (point))
(goto-char (point-max)) ; Point at end of narrowed region
(cj/copy-to-top-of-buffer)
(should (equal (buffer-string) "Line 2\nLine 3\n"))
(should (equal (car kill-ring) "Line 2\nLine 3\n"))))
(test-copy-to-top-teardown)))
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-boundary-whitespace-only-copies-whitespace ()
"Should copy whitespace-only content."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(insert " \n\t\t\n ")
(goto-char (point-min))
(forward-char 7) ; After second newline
(cj/copy-to-top-of-buffer)
(should (equal (buffer-string) " \n\t\t\n "))
(should (equal (car kill-ring) " \n\t\t\n")))
(test-copy-to-top-teardown)))
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-boundary-single-character-copies-char ()
"Should copy single character buffer."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(insert "x")
(goto-char (point-max))
(cj/copy-to-top-of-buffer)
(should (equal (buffer-string) "x"))
(should (equal (car kill-ring) "x")))
(test-copy-to-top-teardown)))
;;; Error Cases
(ert-deftest test-custom-buffer-file-copy-to-top-of-buffer-error-read-only-buffer-succeeds ()
"Should work in read-only buffer since it doesn't modify content."
(test-copy-to-top-setup)
(unwind-protect
(with-temp-buffer
(insert "Read-only content")
(goto-char (point-max))
(read-only-mode 1)
(cj/copy-to-top-of-buffer)
(should (equal (car kill-ring) "Read-only content")))
(test-copy-to-top-teardown)))
(provide 'test-custom-buffer-file-copy-to-top-of-buffer)
;;; test-custom-buffer-file-copy-to-top-of-buffer.el ends here
|