aboutsummaryrefslogtreecommitdiff
path: root/tests/test-custom-ordering-wrappers.el
blob: 8651818b3a4d2cf69619ca1a8f49bf0f0c89e398 (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
;;; test-custom-ordering-wrappers.el --- Tests for the user-facing ordering wrappers -*- lexical-binding: t; -*-

;;; Commentary:
;; Sibling tests cover each `cj/--<op>' internal in isolation.  This
;; file covers the buffer-mutating wrapper commands:
;;
;;   cj/arrayify, cj/listify, cj/arrayify-json, cj/arrayify-python
;;   cj/unarrayify
;;   cj/toggle-quotes
;;   cj/reverse-lines
;;   cj/number-lines
;;   cj/alphabetize-region
;;   cj/comma-separated-text-to-lines
;;
;; Each test runs the wrapper inside `with-temp-buffer' with the whole
;; buffer as the region and asserts the contents after replacement.

;;; Code:

(require 'ert)
(require 'cl-lib)

(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'custom-ordering)

(defmacro test-custom-ordering--with (input &rest body)
  "Insert INPUT into a temp buffer, evaluate BODY, return buffer string."
  (declare (indent 1))
  `(with-temp-buffer
     (insert ,input)
     (set-mark (point-min))
     (goto-char (point-max))
     ,@body
     (buffer-string)))

;;; cj/arrayify (quoted, comma-separated)

(ert-deftest test-custom-ordering-arrayify-replaces-region-with-quoted-list ()
  "Normal: arrayify quotes every word and joins with \", \"."
  (let ((result (test-custom-ordering--with "apple banana cherry"
                  (cj/arrayify (point-min) (point-max) "'"))))
    (should (equal result "'apple', 'banana', 'cherry'"))))

;;; cj/listify (unquoted)

(ert-deftest test-custom-ordering-listify-joins-with-comma-space ()
  "Normal: listify produces a bare comma-separated list."
  (let ((result (test-custom-ordering--with "apple banana cherry"
                  (cj/listify (point-min) (point-max)))))
    (should (equal result "apple, banana, cherry"))))

;;; cj/arrayify-json

(ert-deftest test-custom-ordering-arrayify-json-wraps-in-brackets ()
  "Normal: arrayify-json wraps the quoted list in square brackets."
  (let ((result (test-custom-ordering--with "apple banana"
                  (cj/arrayify-json (point-min) (point-max)))))
    (should (equal result "[\"apple\", \"banana\"]"))))

;;; cj/arrayify-python

(ert-deftest test-custom-ordering-arrayify-python-wraps-in-brackets ()
  "Normal: arrayify-python wraps the quoted list in square brackets."
  (let ((result (test-custom-ordering--with "apple banana"
                  (cj/arrayify-python (point-min) (point-max)))))
    (should (equal result "[\"apple\", \"banana\"]"))))

;;; cj/unarrayify

(ert-deftest test-custom-ordering-unarrayify-splits-on-comma-space ()
  "Normal: unarrayify splits on \", \" and strips quotes."
  (let ((result (test-custom-ordering--with "'a', 'b', 'c'"
                  (cj/unarrayify (point-min) (point-max)))))
    (should (equal result "a\nb\nc"))))

;;; cj/toggle-quotes

(ert-deftest test-custom-ordering-toggle-quotes-swaps-quote-styles ()
  "Normal: toggle-quotes swaps single and double quote characters."
  (let ((result (test-custom-ordering--with "say \"hi\" and 'bye'"
                  (cj/toggle-quotes (point-min) (point-max)))))
    (should (equal result "say 'hi' and \"bye\""))))

;;; cj/reverse-lines

(ert-deftest test-custom-ordering-reverse-lines-inverts-line-order ()
  "Normal: reverse-lines flips top-to-bottom into bottom-to-top."
  (let ((result (test-custom-ordering--with "one\ntwo\nthree"
                  (cj/reverse-lines (point-min) (point-max)))))
    (should (equal result "three\ntwo\none"))))

;;; cj/number-lines

(ert-deftest test-custom-ordering-number-lines-prefixes-with-format ()
  "Normal: number-lines prefixes each line per the format string."
  (let ((result (test-custom-ordering--with "alpha\nbeta\ngamma"
                  (cj/number-lines (point-min) (point-max) "N. " nil))))
    (should (equal result "1. alpha\n2. beta\n3. gamma"))))

;;; cj/alphabetize-region

(ert-deftest test-custom-ordering-alphabetize-sorts-words ()
  "Normal: alphabetize-region sorts and joins with \", \"."
  (let ((result
         (with-temp-buffer
           (insert "cherry apple banana")
           (transient-mark-mode 1)
           (set-mark (point-min))
           (goto-char (point-max))
           (activate-mark)
           (cj/alphabetize-region)
           (buffer-string))))
    (should (equal result "apple, banana, cherry"))))

(ert-deftest test-custom-ordering-alphabetize-without-region-errors ()
  "Error: alphabetize-region without an active region signals."
  (with-temp-buffer
    (insert "anything")
    (deactivate-mark)
    (should-error (cj/alphabetize-region) :type 'user-error)))

;;; cj/comma-separated-text-to-lines

(ert-deftest test-custom-ordering-comma-to-lines-replaces-region ()
  "Normal: comma-separated-text-to-lines splits commas into newlines."
  (let ((result
         (with-temp-buffer
           (insert "a,b,c")
           (transient-mark-mode 1)
           (set-mark (point-min))
           (goto-char (point-max))
           (activate-mark)
           (cj/comma-separated-text-to-lines)
           (buffer-string))))
    (should (equal result "a\nb\nc"))))

(ert-deftest test-custom-ordering-comma-to-lines-without-region-errors ()
  "Error: comma-separated-text-to-lines without a region signals."
  (with-temp-buffer
    (insert "a,b,c")
    (deactivate-mark)
    (should-error (cj/comma-separated-text-to-lines))))

(provide 'test-custom-ordering-wrappers)
;;; test-custom-ordering-wrappers.el ends here