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-fixup-whitespace.el --- -*- lexical-binding: t; -*-
;;; Commentary:
;; Test cj/fixup-whitespace-line-or-region in custom-functions.el
;; The function under test should:
;; - ensure there is exactly one space between words
;; - remove tab characters
;; - remove leading and trailing whitespace
;; - operate on a line, or a region, if selected
;;; Code:
(require 'ert)
(add-to-list 'load-path (concat user-emacs-directory "modules"))
(require 'custom-functions)
(ert-deftest test-cj/fixup-whitespace-positive-first-line-only ()
"Test a positive case with two lines.
Both lines have whitespace at the beginning and the end. This tests that when
this function is called on the first line, only that line is affected."
(let ((testdata " Hello, world! \n Foo bar ")
(expected "Hello, world!\n Foo bar ")
(actual))
(with-temp-buffer
(insert testdata)
(goto-char (point-min))
(cj/fixup-whitespace-line-or-region)
(setq actual (buffer-string))
(should (string= actual expected)))))
(ert-deftest test-cj/fixup-whitespace-positive-first-line-only-tabs ()
"Test a positive case with two lines.
Both lines have extraneous whitespace at the beginning and the end, includuing
tabs. This tests that when this function is called on the first line, only that
line is affected."
(let ((testdata " Hello,\t world! \n Foo\tbar ")
(expected "Hello, world!\n Foo\tbar ")
(actual))
(with-temp-buffer
(insert testdata)
(goto-char (point-min))
(cj/fixup-whitespace-line-or-region)
(setq actual (buffer-string))
(should (string= actual expected)))))
(ert-deftest test-cj/fixup-whitespace-positive-first-line-only-tabs2 ()
"Test a positive case with two lines.
Both lines have extraneous whitespace at the beginning and the end, includuing
tabs. This tests that when this function is called on the first line, only that
line is affected."
(let ((testdata "\t Hello,\tworld! \n Foo\t bar\t ")
(expected "Hello, world!\n Foo\t bar\t ")
(actual))
(with-temp-buffer
(insert testdata)
(goto-char (point-min))
(cj/fixup-whitespace-line-or-region)
(setq actual (buffer-string))
(should (string= actual expected)))))
(ert-deftest test-cj/fixup-whitespace-negative-first-line-only ()
"Test a negative case with two lines.
Only the second line has whitespace at the beginning and the end. This tests
that when this function is called on the first line, neither line changes."
(let ((testdata "Hello, world!\n Foo bar ")
(expected "Hello, world!\n Foo bar ")
(actual))
(with-temp-buffer
(insert testdata)
(goto-char (point-min))
(cj/fixup-whitespace-line-or-region)
(setq actual (buffer-string))
(should (string= actual expected)))))
(ert-deftest test-cj/fixup-whitespace-positive-second-line-only ()
"Test a positive case with two lines.
Both lines have whitespace at the beginning and the end. This tests that when
function is called on the second line, only that line is affected."
(let ((testdata " Hello, world! \n Foo bar ")
(expected " Hello, world! \nFoo bar")
(actual))
(with-temp-buffer
(insert testdata)
(goto-char (point-min))
(forward-line)
(cj/fixup-whitespace-line-or-region)
(setq actual (buffer-string))
(should (string= actual expected)))))
(ert-deftest test-cj/fixup-whitespace-negative-second-line-only ()
"Test a negative case with two lines.
Only the first line has whitespace at the beginning and the end. This tests
that when this function is called on the first line, neither line changes."
(let ((testdata " Hello, world! \nFoo bar")
(expected " Hello, world! \nFoo bar")
(actual))
(with-temp-buffer
(insert testdata)
(goto-char (point-min))
(forward-line)
(cj/fixup-whitespace-line-or-region)
(setq actual (buffer-string))
(should (string= actual expected)))))
(ert-deftest test-cj/fixup-whitespace-positive-region ()
"Test a positive case with a region.
Two lines have whitespace at the beginning, the middle, and the end. This tests
that when this function is called with a region, all whitespace is cleaned up as
expected."
(let ((testdata " Hello, world! \n Foo bar ")
(expected "Hello, world!\nFoo bar")
(actual))
(with-temp-buffer
(insert testdata)
(goto-char (point-min))
(set-mark (point))
(goto-char (point-max))
(cj/fixup-whitespace-line-or-region t)
(setq actual (buffer-string))
(should (string= actual expected)))))
(ert-deftest test-cj/fixup-whitespace-positive-region-tabs ()
"Test a positive case with a region and tabs.
Two lines have extraneous whitespace at the beginning, the middle, and the end.
This tests that when this function is called with a region, all whitespace is
cleaned up as expected."
(let ((testdata " \t \t Hello, world! \n Foo\t bar ")
(expected "Hello, world!\nFoo bar")
(actual))
(with-temp-buffer
(insert testdata)
(goto-char (point-min))
(set-mark (point))
(goto-char (point-max))
(cj/fixup-whitespace-line-or-region t)
(setq actual (buffer-string))
(should (string= actual expected)))))
(ert-deftest test-cj/fixup-whitespace-negative-region ()
"Test a negative case with a region.
Two lines are inserted, neither of which have extraneous whitespace. This tests
that when this function is called with a region, there's no unwanted
side-effects and nothing changes."
(let ((testdata "Hello, world!\nFoo bar")
(expected "Hello, world!\nFoo bar")
(actual))
(with-temp-buffer
(insert testdata)
(goto-char (point-min))
(set-mark (point))
(goto-char (point-max))
(cj/fixup-whitespace-line-or-region t)
(setq actual (buffer-string))
(should (string= actual expected)))))
(provide 'test-fixup-whitespace)
;;; test-fixup-whitespace.el ends here.
|