1 ;;; subr-tests.el --- Tests for subr.el
3 ;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>,
6 ;; Nicolas Petton <nicolas@petton.fr>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 (ert-deftest let-when-compile
()
34 (should (equal (macroexpand '(let-when-compile ((foo (+ 2 3)))
35 (setq bar
(eval-when-compile (+ foo foo
)))
36 (setq boo
(eval-when-compile (* foo foo
)))))
39 (setq boo
(quote 25)))))
40 ;; bad case: `eval-when-compile' omitted, byte compiler should catch this
41 (should (equal (macroexpand
42 '(let-when-compile ((foo (+ 2 3)))
43 (setq bar
(+ foo foo
))
44 (setq boo
(eval-when-compile (* foo foo
)))))
46 (setq bar
(+ foo foo
))
47 (setq boo
(quote 25)))))
48 ;; something practical
49 (should (equal (macroexpand
50 '(let-when-compile ((keywords '("true" "false")))
51 (font-lock-add-keywords
53 `((,(eval-when-compile
54 (format "\\<%s\\>" (regexp-opt keywords
)))
55 0 font-lock-keyword-face
)))))
56 '(font-lock-add-keywords
60 "\\<\\(?:\\(?:fals\\|tru\\)e\\)\\>")
62 (0 font-lock-keyword-face
))))))))
64 (ert-deftest number-sequence-test
()
66 (number-sequence (1- most-positive-fixnum
) most-positive-fixnum
))
70 (1+ most-negative-fixnum
) most-negative-fixnum -
1))
73 (ert-deftest string-comparison-test
()
74 (should (string-lessp "abc" "acb"))
75 (should (string-lessp "aBc" "abc"))
76 (should (string-lessp "abc" "abcd"))
77 (should (string-lessp "abc" "abcd"))
78 (should-not (string-lessp "abc" "abc"))
79 (should-not (string-lessp "" ""))
81 (should (string-greaterp "acb" "abc"))
82 (should (string-greaterp "abc" "aBc"))
83 (should (string-greaterp "abcd" "abc"))
84 (should (string-greaterp "abcd" "abc"))
85 (should-not (string-greaterp "abc" "abc"))
86 (should-not (string-greaterp "" ""))
88 ;; Symbols are also accepted
89 (should (string-lessp 'abc
'acb
))
90 (should (string-lessp "abc" 'acb
))
91 (should (string-greaterp 'acb
'abc
))
92 (should (string-greaterp "acb" 'abc
)))
94 (ert-deftest subr-test-when
()
95 (should (equal (when t
1) 1))
96 (should (equal (when t
2) 2))
97 (should (equal (when nil
1) nil
))
98 (should (equal (when nil
2) nil
))
99 (should (equal (when t
'x
1) 1))
100 (should (equal (when t
'x
2) 2))
101 (should (equal (when nil
'x
1) nil
))
102 (should (equal (when nil
'x
2) nil
))
104 (should-not (when nil
112 (should (equal (macroexpand-all '(when a b c d
))
113 '(if a
(progn b c d
)))))
115 (ert-deftest subr-test-version-parsing
()
116 (should (equal (version-to-list ".5") '(0 5)))
117 (should (equal (version-to-list "0.9 alpha1") '(0 9 -
3 1)))
118 (should (equal (version-to-list "0.9 snapshot") '(0 9 -
4)))
119 (should (equal (version-to-list "0.9-alpha1") '(0 9 -
3 1)))
120 (should (equal (version-to-list "0.9-snapshot") '(0 9 -
4)))
121 (should (equal (version-to-list "0.9.snapshot") '(0 9 -
4)))
122 (should (equal (version-to-list "0.9_snapshot") '(0 9 -
4)))
123 (should (equal (version-to-list "0.9alpha1") '(0 9 -
3 1)))
124 (should (equal (version-to-list "0.9snapshot") '(0 9 -
4)))
125 (should (equal (version-to-list "1.0 git") '(1 0 -
4)))
126 (should (equal (version-to-list "1.0 pre2") '(1 0 -
1 2)))
127 (should (equal (version-to-list "1.0-git") '(1 0 -
4)))
128 (should (equal (version-to-list "1.0-pre2") '(1 0 -
1 2)))
129 (should (equal (version-to-list "1.0.1-a") '(1 0 1 1)))
130 (should (equal (version-to-list "1.0.1-f") '(1 0 1 6)))
131 (should (equal (version-to-list "1.0.1.a") '(1 0 1 1)))
132 (should (equal (version-to-list "1.0.1.f") '(1 0 1 6)))
133 (should (equal (version-to-list "1.0.1_a") '(1 0 1 1)))
134 (should (equal (version-to-list "1.0.1_f") '(1 0 1 6)))
135 (should (equal (version-to-list "1.0.1a") '(1 0 1 1)))
136 (should (equal (version-to-list "1.0.1f") '(1 0 1 6)))
137 (should (equal (version-to-list "1.0.7.5") '(1 0 7 5)))
138 (should (equal (version-to-list "1.0.git") '(1 0 -
4)))
139 (should (equal (version-to-list "1.0.pre2") '(1 0 -
1 2)))
140 (should (equal (version-to-list "1.0_git") '(1 0 -
4)))
141 (should (equal (version-to-list "1.0_pre2") '(1 0 -
1 2)))
142 (should (equal (version-to-list "1.0git") '(1 0 -
4)))
143 (should (equal (version-to-list "1.0pre2") '(1 0 -
1 2)))
144 (should (equal (version-to-list "22.8 beta3") '(22 8 -
2 3)))
145 (should (equal (version-to-list "22.8-beta3") '(22 8 -
2 3)))
146 (should (equal (version-to-list "22.8.beta3") '(22 8 -
2 3)))
147 (should (equal (version-to-list "22.8_beta3") '(22 8 -
2 3)))
148 (should (equal (version-to-list "22.8beta3") '(22 8 -
2 3)))
149 (should (equal (version-to-list "6.9.30 Beta") '(6 9 30 -
2)))
150 (should (equal (version-to-list "6.9.30-Beta") '(6 9 30 -
2)))
151 (should (equal (version-to-list "6.9.30.Beta") '(6 9 30 -
2)))
152 (should (equal (version-to-list "6.9.30Beta") '(6 9 30 -
2)))
153 (should (equal (version-to-list "6.9.30_Beta") '(6 9 30 -
2)))
156 (error-message-string (should-error (version-to-list "OTP-18.1.5")))
157 "Invalid version syntax: `OTP-18.1.5' (must start with a number)"))
159 (error-message-string (should-error (version-to-list "")))
160 "Invalid version syntax: `' (must start with a number)"))
162 (error-message-string (should-error (version-to-list "1.0..7.5")))
163 "Invalid version syntax: `1.0..7.5'"))
165 (error-message-string (should-error (version-to-list "1.0prepre2")))
166 "Invalid version syntax: `1.0prepre2'"))
168 (error-message-string (should-error (version-to-list "22.8X3")))
169 "Invalid version syntax: `22.8X3'"))
171 (error-message-string (should-error (version-to-list "beta22.8alpha3")))
172 "Invalid version syntax: `beta22.8alpha3' (must start with a number)"))
174 (error-message-string (should-error (version-to-list "honk")))
175 "Invalid version syntax: `honk' (must start with a number)"))
177 (error-message-string (should-error (version-to-list 9)))
178 "Version must be a string"))
180 (let ((version-separator "_"))
181 (should (equal (version-to-list "_5") '(0 5)))
182 (should (equal (version-to-list "0_9 alpha1") '(0 9 -
3 1)))
183 (should (equal (version-to-list "0_9 snapshot") '(0 9 -
4)))
184 (should (equal (version-to-list "0_9-alpha1") '(0 9 -
3 1)))
185 (should (equal (version-to-list "0_9-snapshot") '(0 9 -
4)))
186 (should (equal (version-to-list "0_9.alpha1") '(0 9 -
3 1)))
187 (should (equal (version-to-list "0_9.snapshot") '(0 9 -
4)))
188 (should (equal (version-to-list "0_9alpha1") '(0 9 -
3 1)))
189 (should (equal (version-to-list "0_9snapshot") '(0 9 -
4)))
190 (should (equal (version-to-list "1_0 git") '(1 0 -
4)))
191 (should (equal (version-to-list "1_0 pre2") '(1 0 -
1 2)))
192 (should (equal (version-to-list "1_0-git") '(1 0 -
4)))
193 (should (equal (version-to-list "1_0.pre2") '(1 0 -
1 2)))
194 (should (equal (version-to-list "1_0_1-a") '(1 0 1 1)))
195 (should (equal (version-to-list "1_0_1-f") '(1 0 1 6)))
196 (should (equal (version-to-list "1_0_1.a") '(1 0 1 1)))
197 (should (equal (version-to-list "1_0_1.f") '(1 0 1 6)))
198 (should (equal (version-to-list "1_0_1_a") '(1 0 1 1)))
199 (should (equal (version-to-list "1_0_1_f") '(1 0 1 6)))
200 (should (equal (version-to-list "1_0_1a") '(1 0 1 1)))
201 (should (equal (version-to-list "1_0_1f") '(1 0 1 6)))
202 (should (equal (version-to-list "1_0_7_5") '(1 0 7 5)))
203 (should (equal (version-to-list "1_0_git") '(1 0 -
4)))
204 (should (equal (version-to-list "1_0pre2") '(1 0 -
1 2)))
205 (should (equal (version-to-list "22_8 beta3") '(22 8 -
2 3)))
206 (should (equal (version-to-list "22_8-beta3") '(22 8 -
2 3)))
207 (should (equal (version-to-list "22_8.beta3") '(22 8 -
2 3)))
208 (should (equal (version-to-list "22_8beta3") '(22 8 -
2 3)))
209 (should (equal (version-to-list "6_9_30 Beta") '(6 9 30 -
2)))
210 (should (equal (version-to-list "6_9_30-Beta") '(6 9 30 -
2)))
211 (should (equal (version-to-list "6_9_30.Beta") '(6 9 30 -
2)))
212 (should (equal (version-to-list "6_9_30Beta") '(6 9 30 -
2)))
215 (error-message-string (should-error (version-to-list "1_0__7_5")))
216 "Invalid version syntax: `1_0__7_5'"))
218 (error-message-string (should-error (version-to-list "1_0prepre2")))
219 "Invalid version syntax: `1_0prepre2'"))
221 (error-message-string (should-error (version-to-list "22.8X3")))
222 "Invalid version syntax: `22.8X3'"))
224 (error-message-string (should-error (version-to-list "beta22_8alpha3")))
225 "Invalid version syntax: `beta22_8alpha3' (must start with a number)"))))
227 (defun subr-test--backtrace-frames-with-backtrace-frame (base)
228 "Reference implementation of `backtrace-frames'."
232 (while (setq frame
(backtrace-frame idx base
))
237 (defun subr-test--frames-2 (base)
239 (progn ;; Add a few frames to top of stack
241 (cons (mapcar (pcase-lambda (`(,evald
,func
,args
,_
))
242 `(,evald
,func
,@args
))
243 (backtrace-frames base
))
244 (subr-test--backtrace-frames-with-backtrace-frame base
))))))
246 (defun subr-test--frames-1 (base)
247 (subr-test--frames-2 base
))
249 (ert-deftest subr-test-backtrace-simple-tests
()
250 "Test backtrace-related functions (simple tests).
251 This exercises `backtrace-frame', and indirectly `mapbacktrace'."
252 ;; `mapbacktrace' returns nil
253 (should (equal (mapbacktrace #'ignore
) nil
))
254 ;; Unbound BASE is silently ignored
255 (let ((unbound (make-symbol "ub")))
256 (should (equal (backtrace-frame 0 unbound
) nil
))
257 (should (equal (mapbacktrace #'error unbound
) nil
)))
258 ;; First frame is backtrace-related function
259 (should (equal (backtrace-frame 0) '(t backtrace-frame
0)))
260 (should (equal (catch 'ret
261 (mapbacktrace (lambda (&rest args
) (throw 'ret args
))))
262 '(t mapbacktrace
((lambda (&rest args
) (throw 'ret args
))) nil
)))
263 ;; Past-end NFRAMES is silently ignored
264 (should (equal (backtrace-frame most-positive-fixnum
) nil
)))
266 (ert-deftest subr-test-backtrace-integration-test
()
267 "Test backtrace-related functions (integration test).
268 This exercises `backtrace-frame', `backtrace-frames', and
269 indirectly `mapbacktrace'."
270 ;; Compare two implementations of backtrace-frames
271 (let ((frame-lists (subr-test--frames-1 'subr-test--frames-2
)))
272 (should (equal (car frame-lists
) (cdr frame-lists
)))))
274 (ert-deftest subr-tests--string-match-p--blank
()
275 "Test that [:blank:] matches horizontal whitespace, cf. Bug#25366."
276 (should (equal (string-match-p "\\`[[:blank:]]\\'" " ") 0))
277 (should (equal (string-match-p "\\`[[:blank:]]\\'" "\t") 0))
278 (should-not (string-match-p "\\`[[:blank:]]\\'" "\n"))
279 (should-not (string-match-p "\\`[[:blank:]]\\'" "a"))
280 (should (equal (string-match-p "\\`[[:blank:]]\\'" "\N{HAIR SPACE}") 0))
281 (should (equal (string-match-p "\\`[[:blank:]]\\'" "\u3000") 0))
282 (should-not (string-match-p "\\`[[:blank:]]\\'" "\N{LINE SEPARATOR}")))
284 (provide 'subr-tests
)
285 ;;; subr-tests.el ends here