summaryrefslogtreecommitdiff
path: root/tests/test-custom-whitespace-hyphenate.el
blob: 03462fab5ba1d9d56aabf5b8f70274269c3e7b91 (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
;;; test-custom-whitespace-hyphenate.el --- Tests for cj/--hyphenate-whitespace -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the cj/--hyphenate-whitespace function from custom-whitespace.el
;;
;; This function replaces all runs of whitespace (spaces, tabs, newlines,
;; carriage returns) with single hyphens. Useful for converting text with
;; whitespace into hyphenated identifiers or URLs.
;;
;; Uses the regexp [ \t\n\r]+ to match whitespace runs.
;;
;; We test the NON-INTERACTIVE implementation (cj/--hyphenate-whitespace)
;; 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-whitespace)

;;; Test Helpers

(defun test-hyphenate-whitespace (input-text)
  "Test cj/--hyphenate-whitespace on INPUT-TEXT.
Returns the buffer string after operation."
  (with-temp-buffer
    (insert input-text)
    (cj/--hyphenate-whitespace (point-min) (point-max))
    (buffer-string)))

;;; Normal Cases

(ert-deftest test-hyphenate-whitespace-single-space ()
  "Should replace single space with hyphen."
  (let ((result (test-hyphenate-whitespace "hello world")))
    (should (string= result "hello-world"))))

(ert-deftest test-hyphenate-whitespace-multiple-spaces ()
  "Should replace multiple spaces with single hyphen."
  (let ((result (test-hyphenate-whitespace "hello    world")))
    (should (string= result "hello-world"))))

(ert-deftest test-hyphenate-whitespace-tabs ()
  "Should replace tabs with hyphen."
  (let ((result (test-hyphenate-whitespace "hello\tworld")))
    (should (string= result "hello-world"))))

(ert-deftest test-hyphenate-whitespace-mixed-tabs-spaces ()
  "Should replace mixed tabs and spaces with single hyphen."
  (let ((result (test-hyphenate-whitespace "hello \t world")))
    (should (string= result "hello-world"))))

(ert-deftest test-hyphenate-whitespace-newlines ()
  "Should replace newlines with hyphen (joining lines)."
  (let ((result (test-hyphenate-whitespace "hello\nworld")))
    (should (string= result "hello-world"))))

(ert-deftest test-hyphenate-whitespace-multiple-newlines ()
  "Should replace multiple newlines with single hyphen."
  (let ((result (test-hyphenate-whitespace "hello\n\n\nworld")))
    (should (string= result "hello-world"))))

(ert-deftest test-hyphenate-whitespace-multiple-words ()
  "Should hyphenate multiple words with various whitespace."
  (let ((result (test-hyphenate-whitespace "one two  three\tfour\nfive")))
    (should (string= result "one-two-three-four-five"))))

(ert-deftest test-hyphenate-whitespace-carriage-returns ()
  "Should handle carriage returns."
  (let ((result (test-hyphenate-whitespace "hello\r\nworld")))
    (should (string= result "hello-world"))))

;;; Boundary Cases

(ert-deftest test-hyphenate-whitespace-empty-string ()
  "Should handle empty string."
  (let ((result (test-hyphenate-whitespace "")))
    (should (string= result ""))))

(ert-deftest test-hyphenate-whitespace-no-whitespace ()
  "Should handle text with no whitespace (no-op)."
  (let ((result (test-hyphenate-whitespace "helloworld")))
    (should (string= result "helloworld"))))

(ert-deftest test-hyphenate-whitespace-only-whitespace ()
  "Should convert text with only whitespace to single hyphen."
  (let ((result (test-hyphenate-whitespace "   \t   \n   ")))
    (should (string= result "-"))))

(ert-deftest test-hyphenate-whitespace-single-char ()
  "Should handle single character with surrounding spaces."
  (let ((result (test-hyphenate-whitespace "  x  ")))
    (should (string= result "-x-"))))

(ert-deftest test-hyphenate-whitespace-very-long-text ()
  "Should handle very long text with many spaces."
  (let ((result (test-hyphenate-whitespace "word word word word word word word word")))
    (should (string= result "word-word-word-word-word-word-word-word"))))

(ert-deftest test-hyphenate-whitespace-leading-whitespace ()
  "Should replace leading whitespace with hyphen."
  (let ((result (test-hyphenate-whitespace "   hello world")))
    (should (string= result "-hello-world"))))

(ert-deftest test-hyphenate-whitespace-trailing-whitespace ()
  "Should replace trailing whitespace with hyphen."
  (let ((result (test-hyphenate-whitespace "hello world   ")))
    (should (string= result "hello-world-"))))

;;; Error Cases

(ert-deftest test-hyphenate-whitespace-start-greater-than-end ()
  "Should error when start > end."
  (should-error
   (with-temp-buffer
     (insert "hello world")
     (cj/--hyphenate-whitespace (point-max) (point-min)))
   :type 'error))

(ert-deftest test-hyphenate-whitespace-empty-region ()
  "Should handle empty region (start == end) without error."
  (with-temp-buffer
    (insert "hello world")
    (let ((pos (/ (+ (point-min) (point-max)) 2)))
      (cj/--hyphenate-whitespace pos pos)
      ;; Should complete without error and not change buffer
      (should (string= (buffer-string) "hello world")))))

(provide 'test-custom-whitespace-hyphenate)
;;; test-custom-whitespace-hyphenate.el ends here