summaryrefslogtreecommitdiff
path: root/tests/test-custom-ordering-comma-to-lines.el
blob: 93e37ec687667f11c135156e1cc7ce7d0b821a7e (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
;;; test-custom-ordering-comma-to-lines.el --- Tests for cj/--comma-separated-text-to-lines -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the cj/--comma-separated-text-to-lines function from custom-ordering.el
;;
;; This function converts comma-separated text to separate lines.
;; It replaces commas with newlines and removes trailing whitespace from each line.
;;
;; Examples:
;; Input:  "apple, banana, cherry"
;; Output: "apple\nbanana\ncherry"
;;
;; Input:  "one,two,three"
;; Output: "one\ntwo\nthree"
;;
;; We test the NON-INTERACTIVE implementation (cj/--comma-separated-text-to-lines)
;; 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-comma-to-lines (input-text)
  "Test cj/--comma-separated-text-to-lines on INPUT-TEXT.
Returns the transformed string."
  (with-temp-buffer
    (insert input-text)
    (cj/--comma-separated-text-to-lines (point-min) (point-max))))

;;; Normal Cases - Simple Comma-Separated

(ert-deftest test-comma-to-lines-simple ()
  "Should convert simple comma-separated text to lines."
  (let ((result (test-comma-to-lines "apple, banana, cherry")))
    (should (string= result "apple\n banana\n cherry"))))

(ert-deftest test-comma-to-lines-no-spaces ()
  "Should convert comma-separated text without spaces."
  (let ((result (test-comma-to-lines "one,two,three")))
    (should (string= result "one\ntwo\nthree"))))

(ert-deftest test-comma-to-lines-two-elements ()
  "Should convert two comma-separated elements."
  (let ((result (test-comma-to-lines "hello,world")))
    (should (string= result "hello\nworld"))))

(ert-deftest test-comma-to-lines-with-varied-spacing ()
  "Should preserve leading spaces after commas."
  (let ((result (test-comma-to-lines "alpha,  beta,   gamma")))
    (should (string= result "alpha\n  beta\n   gamma"))))

;;; Normal Cases - Trailing Whitespace

(ert-deftest test-comma-to-lines-trailing-spaces ()
  "Should remove trailing spaces but preserve leading spaces."
  (let ((result (test-comma-to-lines "apple  ,  banana  ,  cherry  ")))
    (should (string= result "apple\n  banana\n  cherry"))))

(ert-deftest test-comma-to-lines-trailing-tabs ()
  "Should remove trailing tabs after conversion."
  (let ((result (test-comma-to-lines "apple\t,banana\t,cherry\t")))
    (should (string= result "apple\nbanana\ncherry"))))

;;; Boundary Cases

(ert-deftest test-comma-to-lines-empty-string ()
  "Should handle empty string."
  (let ((result (test-comma-to-lines "")))
    (should (string= result ""))))

(ert-deftest test-comma-to-lines-single-element ()
  "Should handle single element with no comma."
  (let ((result (test-comma-to-lines "hello")))
    (should (string= result "hello"))))

(ert-deftest test-comma-to-lines-single-element-with-trailing-comma ()
  "Should handle single element with trailing comma."
  (let ((result (test-comma-to-lines "hello,")))
    (should (string= result "hello\n"))))

(ert-deftest test-comma-to-lines-leading-comma ()
  "Should handle leading comma."
  (let ((result (test-comma-to-lines ",apple,banana")))
    (should (string= result "\napple\nbanana"))))

(ert-deftest test-comma-to-lines-consecutive-commas ()
  "Should handle consecutive commas."
  (let ((result (test-comma-to-lines "apple,,banana")))
    (should (string= result "apple\n\nbanana"))))

(ert-deftest test-comma-to-lines-many-consecutive-commas ()
  "Should handle many consecutive commas."
  (let ((result (test-comma-to-lines "apple,,,banana")))
    (should (string= result "apple\n\n\nbanana"))))

(ert-deftest test-comma-to-lines-only-commas ()
  "Should handle string with only commas (trailing blank lines removed)."
  (let ((result (test-comma-to-lines ",,,")))
    ;; delete-trailing-whitespace removes trailing blank lines
    (should (string= result "\n"))))

;;; Normal Cases - With Spaces Around Elements

(ert-deftest test-comma-to-lines-leading-spaces ()
  "Should preserve leading spaces within elements."
  (let ((result (test-comma-to-lines " apple,  banana,   cherry")))
    (should (string= result " apple\n  banana\n   cherry"))))

(ert-deftest test-comma-to-lines-mixed-content ()
  "Should handle mixed alphanumeric content."
  (let ((result (test-comma-to-lines "item1,item2,item3")))
    (should (string= result "item1\nitem2\nitem3"))))

(ert-deftest test-comma-to-lines-with-numbers ()
  "Should handle numbers."
  (let ((result (test-comma-to-lines "1,2,3,4,5")))
    (should (string= result "1\n2\n3\n4\n5"))))

(ert-deftest test-comma-to-lines-very-long-list ()
  "Should handle very long list."
  (let* ((elements (mapcar #'number-to-string (number-sequence 1 100)))
         (input (mapconcat #'identity elements ","))
         (result (test-comma-to-lines input))
         (lines (split-string result "\n")))
    (should (= 100 (length lines)))))

;;; Error Cases

(ert-deftest test-comma-to-lines-start-greater-than-end ()
  "Should error when start > end."
  (should-error
   (with-temp-buffer
     (insert "a,b,c")
     (cj/--comma-separated-text-to-lines (point-max) (point-min)))
   :type 'error))

(ert-deftest test-comma-to-lines-empty-region ()
  "Should handle empty region (start == end)."
  (with-temp-buffer
    (insert "a,b,c")
    (let ((pos (/ (+ (point-min) (point-max)) 2)))
      (should (string= "" (cj/--comma-separated-text-to-lines pos pos))))))

(provide 'test-custom-ordering-comma-to-lines)
;;; test-custom-ordering-comma-to-lines.el ends here