blob: 81d1546e127edb1ddded7cb8c3af939a9099c1a5 (
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
|
;;; test-custom-misc-replace-fraction-glyphs.el --- Tests for cj/--replace-fraction-glyphs -*- lexical-binding: t; -*-
;;; Commentary:
;; Tests for the cj/--replace-fraction-glyphs function from custom-misc.el
;;
;; This function bidirectionally converts between text fractions (1/4) and
;; Unicode fraction glyphs (¼). It supports 5 common fractions:
;; - 1/4 ↔ ¼
;; - 1/2 ↔ ½
;; - 3/4 ↔ ¾
;; - 1/3 ↔ ⅓
;; - 2/3 ↔ ⅔
;;
;; We test the NON-INTERACTIVE implementation (cj/--replace-fraction-glyphs)
;; to avoid mocking 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-misc)
;;; Test Helpers
(defun test-replace-fraction-glyphs (input-text to-glyphs)
"Test cj/--replace-fraction-glyphs on INPUT-TEXT.
TO-GLYPHS determines conversion direction.
Returns the buffer string after operation."
(with-temp-buffer
(insert input-text)
(cj/--replace-fraction-glyphs (point-min) (point-max) to-glyphs)
(buffer-string)))
;;; Normal Cases - Text to Glyphs
(ert-deftest test-replace-fraction-glyphs-text-to-glyph-quarter ()
"Should convert 1/4 to ¼."
(let ((result (test-replace-fraction-glyphs "1/4" t)))
(should (string= result "¼"))))
(ert-deftest test-replace-fraction-glyphs-text-to-glyph-half ()
"Should convert 1/2 to ½."
(let ((result (test-replace-fraction-glyphs "1/2" t)))
(should (string= result "½"))))
(ert-deftest test-replace-fraction-glyphs-text-to-glyph-three-quarters ()
"Should convert 3/4 to ¾."
(let ((result (test-replace-fraction-glyphs "3/4" t)))
(should (string= result "¾"))))
(ert-deftest test-replace-fraction-glyphs-text-to-glyph-third ()
"Should convert 1/3 to ⅓."
(let ((result (test-replace-fraction-glyphs "1/3" t)))
(should (string= result "⅓"))))
(ert-deftest test-replace-fraction-glyphs-text-to-glyph-two-thirds ()
"Should convert 2/3 to ⅔."
(let ((result (test-replace-fraction-glyphs "2/3" t)))
(should (string= result "⅔"))))
(ert-deftest test-replace-fraction-glyphs-text-to-glyph-multiple ()
"Should convert multiple fractions in text."
(let ((result (test-replace-fraction-glyphs "Use 1/4 cup and 1/2 teaspoon" t)))
(should (string= result "Use ¼ cup and ½ teaspoon"))))
(ert-deftest test-replace-fraction-glyphs-text-to-glyph-all-types ()
"Should convert all fraction types."
(let ((result (test-replace-fraction-glyphs "1/4 1/2 3/4 1/3 2/3" t)))
(should (string= result "¼ ½ ¾ ⅓ ⅔"))))
;;; Normal Cases - Glyphs to Text
(ert-deftest test-replace-fraction-glyphs-glyph-to-text-quarter ()
"Should convert ¼ to 1/4."
(let ((result (test-replace-fraction-glyphs "¼" nil)))
(should (string= result "1/4"))))
(ert-deftest test-replace-fraction-glyphs-glyph-to-text-half ()
"Should convert ½ to 1/2."
(let ((result (test-replace-fraction-glyphs "½" nil)))
(should (string= result "1/2"))))
(ert-deftest test-replace-fraction-glyphs-glyph-to-text-three-quarters ()
"Should convert ¾ to 3/4."
(let ((result (test-replace-fraction-glyphs "¾" nil)))
(should (string= result "3/4"))))
(ert-deftest test-replace-fraction-glyphs-glyph-to-text-third ()
"Should convert ⅓ to 1/3."
(let ((result (test-replace-fraction-glyphs "⅓" nil)))
(should (string= result "1/3"))))
(ert-deftest test-replace-fraction-glyphs-glyph-to-text-two-thirds ()
"Should convert ⅔ to 2/3."
(let ((result (test-replace-fraction-glyphs "⅔" nil)))
(should (string= result "2/3"))))
(ert-deftest test-replace-fraction-glyphs-glyph-to-text-multiple ()
"Should convert multiple glyphs in text."
(let ((result (test-replace-fraction-glyphs "Use ¼ cup and ½ teaspoon" nil)))
(should (string= result "Use 1/4 cup and 1/2 teaspoon"))))
(ert-deftest test-replace-fraction-glyphs-glyph-to-text-all-types ()
"Should convert all glyph types."
(let ((result (test-replace-fraction-glyphs "¼ ½ ¾ ⅓ ⅔" nil)))
(should (string= result "1/4 1/2 3/4 1/3 2/3"))))
;;; Boundary Cases
(ert-deftest test-replace-fraction-glyphs-empty-string ()
"Should handle empty string."
(let ((result (test-replace-fraction-glyphs "" t)))
(should (string= result ""))))
(ert-deftest test-replace-fraction-glyphs-no-fractions-to-glyphs ()
"Should handle text with no fractions (no-op) when converting to glyphs."
(let ((result (test-replace-fraction-glyphs "hello world" t)))
(should (string= result "hello world"))))
(ert-deftest test-replace-fraction-glyphs-no-fractions-to-text ()
"Should handle text with no glyphs (no-op) when converting to text."
(let ((result (test-replace-fraction-glyphs "hello world" nil)))
(should (string= result "hello world"))))
(ert-deftest test-replace-fraction-glyphs-at-start ()
"Should handle fraction at start of text."
(let ((result (test-replace-fraction-glyphs "1/2 of the total" t)))
(should (string= result "½ of the total"))))
(ert-deftest test-replace-fraction-glyphs-at-end ()
"Should handle fraction at end of text."
(let ((result (test-replace-fraction-glyphs "Reduce by 1/4" t)))
(should (string= result "Reduce by ¼"))))
(ert-deftest test-replace-fraction-glyphs-repeated ()
"Should handle repeated fractions."
(let ((result (test-replace-fraction-glyphs "1/4 and 1/4 and 1/4" t)))
(should (string= result "¼ and ¼ and ¼"))))
(ert-deftest test-replace-fraction-glyphs-very-long-text ()
"Should handle very long text with many fractions."
(let* ((long-text (mapconcat (lambda (_) "1/4") (make-list 50 nil) " "))
(result (test-replace-fraction-glyphs long-text t)))
(should (string-match-p "¼" result))
(should-not (string-match-p "1/4" result))))
(ert-deftest test-replace-fraction-glyphs-bidirectional ()
"Should correctly convert back and forth."
(let* ((original "Use 1/4 cup")
(to-glyph (test-replace-fraction-glyphs original t))
(back-to-text (test-replace-fraction-glyphs to-glyph nil)))
(should (string= to-glyph "Use ¼ cup"))
(should (string= back-to-text original))))
;;; Error Cases
(ert-deftest test-replace-fraction-glyphs-start-greater-than-end ()
"Should error when start > end."
(should-error
(with-temp-buffer
(insert "1/4")
(cj/--replace-fraction-glyphs (point-max) (point-min) t))
:type 'error))
(ert-deftest test-replace-fraction-glyphs-empty-region ()
"Should handle empty region (start == end) without error."
(with-temp-buffer
(insert "1/4")
(let ((pos (/ (+ (point-min) (point-max)) 2)))
(cj/--replace-fraction-glyphs pos pos t)
;; Should complete without error
(should (string= (buffer-string) "1/4")))))
(provide 'test-custom-misc-replace-fraction-glyphs)
;;; test-custom-misc-replace-fraction-glyphs.el ends here
|