summaryrefslogtreecommitdiff
path: root/tests/test-custom-file-buffer-copy-path-to-buffer-file-as-kill.el
blob: e7a6f64b19ce14493f90be195143830d78fa7dd9 (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
;;; test-custom-file-buffer-copy-path-to-buffer-file-as-kill.el --- Tests for cj/copy-path-to-buffer-file-as-kill -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the cj/copy-path-to-buffer-file-as-kill function from custom-file-buffer.el
;;
;; This function copies the full path of the current buffer's file to the kill ring
;; and returns the path. It signals an error if the buffer is not visiting a file.

;;; 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-file-buffer)

;;; Setup and Teardown

(defun test-copy-path-setup ()
  "Set up test environment."
  (setq kill-ring nil))

(defun test-copy-path-teardown ()
  "Clean up test environment."
  ;; Kill all buffers visiting files in the test directory
  (dolist (buf (buffer-list))
    (when (buffer-file-name buf)
      (when (string-prefix-p cj/test-base-dir (buffer-file-name buf))
        (with-current-buffer buf
          (set-buffer-modified-p nil)
          (kill-buffer buf)))))
  (cj/delete-test-base-dir)
  (setq kill-ring nil))

;;; Normal Cases

(ert-deftest test-copy-path-simple-file ()
  "Should copy absolute path for simple file buffer."
  (test-copy-path-setup)
  (unwind-protect
      (let* ((test-dir (cj/create-test-subdirectory "test"))
             (test-file (expand-file-name "test.txt" test-dir)))
        (with-temp-file test-file
          (insert "content"))
        (with-current-buffer (find-file test-file)
          (let ((result (cj/copy-path-to-buffer-file-as-kill)))
            (should (equal result test-file))
            (should (equal (car kill-ring) test-file)))))
    (test-copy-path-teardown)))

(ert-deftest test-copy-path-returns-path ()
  "Should return the path value."
  (test-copy-path-setup)
  (unwind-protect
      (let* ((test-dir (cj/create-test-subdirectory "test"))
             (test-file (expand-file-name "test.txt" test-dir)))
        (with-temp-file test-file
          (insert "content"))
        (with-current-buffer (find-file test-file)
          (let ((result (cj/copy-path-to-buffer-file-as-kill)))
            (should (stringp result))
            (should (equal result test-file)))))
    (test-copy-path-teardown)))

;;; Boundary Cases

(ert-deftest test-copy-path-unicode-filename ()
  "Should handle unicode in filename."
  (test-copy-path-setup)
  (unwind-protect
      (let* ((test-dir (cj/create-test-subdirectory "test"))
             (test-file (expand-file-name "café.txt" test-dir)))
        (with-temp-file test-file
          (insert "content"))
        (with-current-buffer (find-file test-file)
          (cj/copy-path-to-buffer-file-as-kill)
          (should (equal (car kill-ring) test-file))))
    (test-copy-path-teardown)))

(ert-deftest test-copy-path-spaces-in-filename ()
  "Should handle spaces in filename."
  (test-copy-path-setup)
  (unwind-protect
      (let* ((test-dir (cj/create-test-subdirectory "test"))
             (test-file (expand-file-name "my file.txt" test-dir)))
        (with-temp-file test-file
          (insert "content"))
        (with-current-buffer (find-file test-file)
          (cj/copy-path-to-buffer-file-as-kill)
          (should (equal (car kill-ring) test-file))))
    (test-copy-path-teardown)))

(ert-deftest test-copy-path-special-chars-filename ()
  "Should handle special characters in filename."
  (test-copy-path-setup)
  (unwind-protect
      (let* ((test-dir (cj/create-test-subdirectory "test"))
             (test-file (expand-file-name "[test]-(1).txt" test-dir)))
        (with-temp-file test-file
          (insert "content"))
        (with-current-buffer (find-file test-file)
          (cj/copy-path-to-buffer-file-as-kill)
          (should (equal (car kill-ring) test-file))))
    (test-copy-path-teardown)))

(ert-deftest test-copy-path-very-long-path ()
  "Should handle very long path."
  (test-copy-path-setup)
  (unwind-protect
      (let* ((test-dir (cj/create-test-subdirectory "test"))
             (long-name (make-string 200 ?x))
             (test-file (expand-file-name (concat long-name ".txt") test-dir)))
        (with-temp-file test-file
          (insert "content"))
        (with-current-buffer (find-file test-file)
          (cj/copy-path-to-buffer-file-as-kill)
          (should (equal (car kill-ring) test-file))))
    (test-copy-path-teardown)))

(ert-deftest test-copy-path-hidden-file ()
  "Should handle hidden file."
  (test-copy-path-setup)
  (unwind-protect
      (let* ((test-dir (cj/create-test-subdirectory "test"))
             (test-file (expand-file-name ".hidden" test-dir)))
        (with-temp-file test-file
          (insert "content"))
        (with-current-buffer (find-file test-file)
          (cj/copy-path-to-buffer-file-as-kill)
          (should (equal (car kill-ring) test-file))))
    (test-copy-path-teardown)))

(ert-deftest test-copy-path-no-extension ()
  "Should handle file with no extension."
  (test-copy-path-setup)
  (unwind-protect
      (let* ((test-dir (cj/create-test-subdirectory "test"))
             (test-file (expand-file-name "README" test-dir)))
        (with-temp-file test-file
          (insert "content"))
        (with-current-buffer (find-file test-file)
          (cj/copy-path-to-buffer-file-as-kill)
          (should (equal (car kill-ring) test-file))))
    (test-copy-path-teardown)))

(ert-deftest test-copy-path-symlink-file ()
  "Should use buffer's filename for symlink."
  (test-copy-path-setup)
  (unwind-protect
      (let* ((test-dir (cj/create-test-subdirectory "test"))
             (target-file (expand-file-name "target.txt" test-dir))
             (link-file (expand-file-name "link.txt" test-dir)))
        (with-temp-file target-file
          (insert "content"))
        (make-symbolic-link target-file link-file)
        (with-current-buffer (find-file link-file)
          (cj/copy-path-to-buffer-file-as-kill)
          (should (equal (car kill-ring) (buffer-file-name)))))
    (test-copy-path-teardown)))

(ert-deftest test-copy-path-kill-ring-has-content ()
  "Should add to kill ring when it already has content."
  (test-copy-path-setup)
  (unwind-protect
      (let* ((test-dir (cj/create-test-subdirectory "test"))
             (test-file (expand-file-name "test.txt" test-dir)))
        (with-temp-file test-file
          (insert "content"))
        (kill-new "existing content")
        (with-current-buffer (find-file test-file)
          (cj/copy-path-to-buffer-file-as-kill)
          (should (equal (car kill-ring) test-file))
          (should (equal (cadr kill-ring) "existing content"))))
    (test-copy-path-teardown)))

;;; Error Cases

(ert-deftest test-copy-path-non-file-buffer ()
  "Should signal user-error for non-file buffer."
  (test-copy-path-setup)
  (unwind-protect
      (with-temp-buffer
        (should-error (cj/copy-path-to-buffer-file-as-kill) :type 'user-error))
    (test-copy-path-teardown)))

(ert-deftest test-copy-path-scratch-buffer ()
  "Should signal user-error for *scratch* buffer."
  (test-copy-path-setup)
  (unwind-protect
      (with-current-buffer "*scratch*"
        (should-error (cj/copy-path-to-buffer-file-as-kill) :type 'user-error))
    (test-copy-path-teardown)))

(provide 'test-custom-file-buffer-copy-path-to-buffer-file-as-kill)
;;; test-custom-file-buffer-copy-path-to-buffer-file-as-kill.el ends here