summaryrefslogtreecommitdiff
path: root/tests/test-jumper.el
blob: fa65d3f449b79ad6bafe0ce1ee9e8b449837f046 (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
;;; test-jumper.el --- Tests for jumper.el -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for jumper.el - location navigation using registers.
;;
;; Testing approach:
;; - Tests focus on internal `jumper--do-*` functions (pure business logic)
;; - Interactive wrappers are thin UI layers and tested minimally
;; - Each test is isolated with setup/teardown to reset global state
;; - Tests verify return values, not user messages

;;; Code:

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

;; Add modules directory to load path
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))

;; Load the module
(require 'jumper)

;;; Test Utilities

(defvar test-jumper--original-registers nil
  "Backup of jumper registers before test.")

(defvar test-jumper--original-index nil
  "Backup of jumper index before test.")

(defun test-jumper-setup ()
  "Reset jumper state before each test."
  ;; Backup current state
  (setq test-jumper--original-registers jumper--registers)
  (setq test-jumper--original-index jumper--next-index)
  ;; Reset to clean state
  (setq jumper--registers (make-vector jumper-max-locations nil))
  (setq jumper--next-index 0))

(defun test-jumper-teardown ()
  "Restore jumper state after each test."
  (setq jumper--registers test-jumper--original-registers)
  (setq jumper--next-index test-jumper--original-index))

;;; Normal Cases - Store Location

(ert-deftest test-jumper-store-first-location ()
  "Should store first location and return register character."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "test content")
    (goto-char (point-min))
    (let ((result (jumper--do-store-location)))
      (should (= result ?0))
      (should (= jumper--next-index 1))))
  (test-jumper-teardown))

(ert-deftest test-jumper-store-multiple-locations ()
  "Should store multiple locations in sequence."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "line 1\nline 2\nline 3")
    (goto-char (point-min))
    (should (= (jumper--do-store-location) ?0))
    (forward-line 1)
    (should (= (jumper--do-store-location) ?1))
    (forward-line 1)
    (should (= (jumper--do-store-location) ?2))
    (should (= jumper--next-index 3)))
  (test-jumper-teardown))

(ert-deftest test-jumper-store-duplicate-location ()
  "Should detect and reject duplicate locations."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "test content")
    (goto-char (point-min))
    (should (= (jumper--do-store-location) ?0))
    (should (eq (jumper--do-store-location) 'already-exists))
    (should (= jumper--next-index 1)))
  (test-jumper-teardown))

;;; Normal Cases - Jump to Location

(ert-deftest test-jumper-jump-to-stored-location ()
  "Should jump to a previously stored location."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "line 1\nline 2\nline 3")
    (goto-char (point-min))
    (jumper--do-store-location)
    (goto-char (point-max))
    (let ((result (jumper--do-jump-to-location 0)))
      (should (eq result 'jumped))
      (should (= (point) (point-min)))))
  (test-jumper-teardown))

(ert-deftest test-jumper-jump-toggle-with-single-location ()
  "Should toggle between current and stored location."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "line 1\nline 2\nline 3")
    (goto-char (point-min))
    (jumper--do-store-location)
    ;; Move away
    (goto-char (point-max))
    ;; Toggle should jump back
    (let ((result (jumper--do-jump-to-location nil)))
      (should (eq result 'jumped))
      (should (= (point) (point-min)))))
  (test-jumper-teardown))

(ert-deftest test-jumper-jump-already-at-location ()
  "Should detect when already at the only stored location."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "line 1\nline 2")
    (goto-char (point-min))
    (jumper--do-store-location)
    ;; Try to toggle while at the location
    (let ((result (jumper--do-jump-to-location nil)))
      (should (eq result 'already-there))))
  (test-jumper-teardown))

;;; Normal Cases - Remove Location

(ert-deftest test-jumper-remove-location ()
  "Should remove a stored location."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "test content")
    (goto-char (point-min))
    (jumper--do-store-location)
    (let ((result (jumper--do-remove-location 0)))
      (should (eq result t))
      (should (= jumper--next-index 0))))
  (test-jumper-teardown))

(ert-deftest test-jumper-remove-reorders-registers ()
  "Should reorder registers after removal from middle."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "line 1\nline 2\nline 3")
    (goto-char (point-min))
    (jumper--do-store-location)  ; Register 0
    (forward-line 1)
    (jumper--do-store-location)  ; Register 1
    (forward-line 1)
    (jumper--do-store-location)  ; Register 2
    ;; Remove middle (index 1)
    (jumper--do-remove-location 1)
    (should (= jumper--next-index 2))
    ;; What was at index 2 should now be at index 1
    (should (= (aref jumper--registers 1) ?2)))
  (test-jumper-teardown))

;;; Boundary Cases - Store Location

(ert-deftest test-jumper-store-at-capacity ()
  "Should successfully store location at maximum capacity."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "test content")
    (goto-char (point-min))
    ;; Fill to capacity
    (dotimes (i jumper-max-locations)
      (forward-char 1)
      (should (= (jumper--do-store-location) (+ ?0 i))))
    (should (= jumper--next-index jumper-max-locations)))
  (test-jumper-teardown))

(ert-deftest test-jumper-store-when-full ()
  "Should return 'no-space when all registers are full."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "01234567890123456789")
    (goto-char (point-min))
    ;; Fill to capacity
    (dotimes (i jumper-max-locations)
      (forward-char 1)
      (jumper--do-store-location))
    ;; Try to store one more
    (forward-char 1)
    (should (eq (jumper--do-store-location) 'no-space))
    (should (= jumper--next-index jumper-max-locations)))
  (test-jumper-teardown))

(ert-deftest test-jumper-store-in-different-buffers ()
  "Should store locations across different buffers."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "buffer 1")
    (goto-char (point-min))
    (should (= (jumper--do-store-location) ?0))
    (with-temp-buffer
      (insert "buffer 2")
      (goto-char (point-min))
      (should (= (jumper--do-store-location) ?1))
      (should (= jumper--next-index 2))))
  (test-jumper-teardown))

;;; Boundary Cases - Jump to Location

(ert-deftest test-jumper-jump-with-no-locations ()
  "Should return 'no-locations when nothing is stored."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "test")
    (let ((result (jumper--do-jump-to-location 0)))
      (should (eq result 'no-locations))))
  (test-jumper-teardown))

(ert-deftest test-jumper-jump-to-first-location ()
  "Should jump to location at index 0."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "line 1\nline 2")
    (goto-char (point-min))
    (jumper--do-store-location)
    (forward-line 1)
    (jumper--do-store-location)
    (goto-char (point-max))
    (jumper--do-jump-to-location 0)
    (should (= (point) (point-min))))
  (test-jumper-teardown))

(ert-deftest test-jumper-jump-to-last-location ()
  "Should jump to last location (register 'z)."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "line 1\nline 2\nline 3")
    (goto-char (point-min))
    (jumper--do-store-location)
    (let ((line2-pos (line-beginning-position 2)))
      (goto-char line2-pos)
      ;; Jump to location 0 (this stores current location in 'z)
      (jumper--do-jump-to-location 0)
      (should (= (point) (point-min)))
      ;; Jump to last location should go back to line 2
      (let ((result (jumper--do-jump-to-location -1)))
        (should (eq result 'jumped))
        (should (= (point) line2-pos)))))
  (test-jumper-teardown))

(ert-deftest test-jumper-jump-to-max-index ()
  "Should jump to location at maximum index."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "0123456789012345678")
    (goto-char (point-min))
    ;; Store at all positions
    (dotimes (i jumper-max-locations)
      (forward-char 1)
      (jumper--do-store-location))
    (goto-char (point-min))
    ;; Jump to last one (index 9, which is at position 10)
    (jumper--do-jump-to-location (1- jumper-max-locations))
    (should (= (point) (1+ jumper-max-locations))))
  (test-jumper-teardown))

;;; Boundary Cases - Remove Location

(ert-deftest test-jumper-remove-first-location ()
  "Should remove location at index 0."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "line 1\nline 2")
    (goto-char (point-min))
    (jumper--do-store-location)
    (forward-line 1)
    (jumper--do-store-location)
    (jumper--do-remove-location 0)
    (should (= jumper--next-index 1))
    ;; What was at index 1 should now be at index 0
    (should (= (aref jumper--registers 0) ?1)))
  (test-jumper-teardown))

(ert-deftest test-jumper-remove-last-location ()
  "Should remove location at last index."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "line 1\nline 2\nline 3")
    (goto-char (point-min))
    (jumper--do-store-location)
    (forward-line 1)
    (jumper--do-store-location)
    (forward-line 1)
    (jumper--do-store-location)
    (jumper--do-remove-location 2)
    (should (= jumper--next-index 2)))
  (test-jumper-teardown))

(ert-deftest test-jumper-remove-with-cancel ()
  "Should return 'cancelled when index is -1."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "test")
    (goto-char (point-min))
    (jumper--do-store-location)
    (let ((result (jumper--do-remove-location -1)))
      (should (eq result 'cancelled))
      (should (= jumper--next-index 1))))
  (test-jumper-teardown))

;;; Error Cases

(ert-deftest test-jumper-remove-when-empty ()
  "Should return 'no-locations when removing from empty list."
  (test-jumper-setup)
  (let ((result (jumper--do-remove-location 0)))
    (should (eq result 'no-locations)))
  (test-jumper-teardown))

;;; Helper Function Tests

(ert-deftest test-jumper-location-key-format ()
  "Should generate unique location keys."
  (with-temp-buffer
    (insert "line 1\nline 2")
    (goto-char (point-min))
    (let ((key1 (jumper--location-key)))
      (forward-line 1)
      (let ((key2 (jumper--location-key)))
        (should-not (string= key1 key2))
        ;; Keys should contain buffer name and position info
        (should (string-match-p ":" key1))
        (should (string-match-p ":" key2))))))

(ert-deftest test-jumper-register-available-p ()
  "Should correctly report register availability."
  (test-jumper-setup)
  (should (jumper--register-available-p))
  ;; Fill to capacity
  (setq jumper--next-index jumper-max-locations)
  (should-not (jumper--register-available-p))
  (test-jumper-teardown))

(ert-deftest test-jumper-format-location ()
  "Should format location for display."
  (test-jumper-setup)
  (with-temp-buffer
    (insert "test line with some content")
    (goto-char (point-min))
    (jumper--do-store-location)
    (let ((formatted (jumper--format-location 0)))
      (should formatted)
      (should (string-match-p "\\[0\\]" formatted))
      (should (string-match-p "test line" formatted))))
  (test-jumper-teardown))

(provide 'test-jumper)
;;; test-jumper.el ends here