blob: e11305ee26857c3170bcdff1c22f5ac38298bfda (
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
|
;;; test-custom-ordering-toggle-quotes.el --- Tests for cj/--toggle-quotes -*- lexical-binding: t; -*-
;;; Commentary:
;; Tests for the cj/--toggle-quotes function from custom-ordering.el
;;
;; This function toggles between double quotes and single quotes.
;; All " become ' and all ' become ".
;;
;; Examples:
;; Input: "apple", "banana"
;; Output: 'apple', 'banana'
;;
;; Input: 'hello', 'world'
;; Output: "hello", "world"
;;
;; We test the NON-INTERACTIVE implementation (cj/--toggle-quotes) to avoid
;; mocking region selection. 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-ordering)
;;; Test Helpers
(defun test-toggle-quotes (input-text)
"Test cj/--toggle-quotes on INPUT-TEXT.
Returns the transformed string."
(with-temp-buffer
(insert input-text)
(cj/--toggle-quotes (point-min) (point-max))))
;;; Normal Cases - Double to Single
(ert-deftest test-toggle-quotes-double-to-single ()
"Should convert double quotes to single quotes."
(let ((result (test-toggle-quotes "\"apple\", \"banana\"")))
(should (string= result "'apple', 'banana'"))))
(ert-deftest test-toggle-quotes-single-double-quote ()
"Should convert single double quote."
(let ((result (test-toggle-quotes "\"")))
(should (string= result "'"))))
(ert-deftest test-toggle-quotes-multiple-double-quotes ()
"Should convert multiple double quotes."
(let ((result (test-toggle-quotes "\"hello\" \"world\" \"test\"")))
(should (string= result "'hello' 'world' 'test'"))))
;;; Normal Cases - Single to Double
(ert-deftest test-toggle-quotes-single-to-double ()
"Should convert single quotes to double quotes."
(let ((result (test-toggle-quotes "'apple', 'banana'")))
(should (string= result "\"apple\", \"banana\""))))
(ert-deftest test-toggle-quotes-single-single-quote ()
"Should convert single single quote."
(let ((result (test-toggle-quotes "'")))
(should (string= result "\""))))
(ert-deftest test-toggle-quotes-multiple-single-quotes ()
"Should convert multiple single quotes."
(let ((result (test-toggle-quotes "'hello' 'world' 'test'")))
(should (string= result "\"hello\" \"world\" \"test\""))))
;;; Normal Cases - Mixed Quotes
(ert-deftest test-toggle-quotes-mixed ()
"Should toggle mixed quotes."
(let ((result (test-toggle-quotes "\"double\" 'single'")))
(should (string= result "'double' \"single\""))))
(ert-deftest test-toggle-quotes-bidirectional ()
"Should toggle back and forth correctly."
(let* ((original "\"apple\", \"banana\"")
(toggled (test-toggle-quotes original))
(back (test-toggle-quotes toggled)))
(should (string= toggled "'apple', 'banana'"))
(should (string= back original))))
;;; Normal Cases - With Text Content
(ert-deftest test-toggle-quotes-preserves-content ()
"Should preserve content while toggling quotes."
(let ((result (test-toggle-quotes "var x = \"hello world\";")))
(should (string= result "var x = 'hello world';"))))
(ert-deftest test-toggle-quotes-sql-style ()
"Should toggle SQL-style quotes."
(let ((result (test-toggle-quotes "SELECT * FROM users WHERE name='John'")))
(should (string= result "SELECT * FROM users WHERE name=\"John\""))))
(ert-deftest test-toggle-quotes-multiline ()
"Should toggle quotes across multiple lines."
(let ((result (test-toggle-quotes "\"line1\"\n\"line2\"\n\"line3\"")))
(should (string= result "'line1'\n'line2'\n'line3'"))))
;;; Boundary Cases
(ert-deftest test-toggle-quotes-empty-string ()
"Should handle empty string."
(let ((result (test-toggle-quotes "")))
(should (string= result ""))))
(ert-deftest test-toggle-quotes-no-quotes ()
"Should handle text with no quotes."
(let ((result (test-toggle-quotes "hello world")))
(should (string= result "hello world"))))
(ert-deftest test-toggle-quotes-only-double-quotes ()
"Should handle string with only double quotes."
(let ((result (test-toggle-quotes "\"\"\"\"")))
(should (string= result "''''"))))
(ert-deftest test-toggle-quotes-only-single-quotes ()
"Should handle string with only single quotes."
(let ((result (test-toggle-quotes "''''")))
(should (string= result "\"\"\"\""))))
(ert-deftest test-toggle-quotes-adjacent-quotes ()
"Should handle adjacent quotes."
(let ((result (test-toggle-quotes "\"\"''")))
(should (string= result "''\"\""))))
;;; Error Cases
(ert-deftest test-toggle-quotes-start-greater-than-end ()
"Should error when start > end."
(should-error
(with-temp-buffer
(insert "\"hello\"")
(cj/--toggle-quotes (point-max) (point-min)))
:type 'error))
(ert-deftest test-toggle-quotes-empty-region ()
"Should handle empty region (start == end)."
(with-temp-buffer
(insert "\"hello\"")
(let ((pos (/ (+ (point-min) (point-max)) 2)))
(should (string= "" (cj/--toggle-quotes pos pos))))))
(provide 'test-custom-ordering-toggle-quotes)
;;; test-custom-ordering-toggle-quotes.el ends here
|