; Revert "Guard terminal par. in XTerm mouse mode"
[emacs.git] / test / automated / subr-tests.el
blobce212903c9d3cb41deb4c52d97d0305e4a49a0ec
1 ;;; subr-tests.el --- Tests for subr.el
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>,
6 ;; Nicolas Petton <nicolas@petton.fr>
7 ;; Keywords:
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/>.
24 ;;; Commentary:
28 ;;; Code:
30 (require 'ert)
32 (ert-deftest let-when-compile ()
33 ;; good case
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)))))
37 '(progn
38 (setq bar (quote 10))
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)))))
45 '(progn
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
52 'c++-mode
53 `((,(eval-when-compile
54 (format "\\<%s\\>" (regexp-opt keywords)))
55 0 font-lock-keyword-face)))))
56 '(font-lock-add-keywords
57 (quote c++-mode)
58 (list
59 (cons (quote
60 "\\<\\(?:\\(?:fals\\|tru\\)e\\)\\>")
61 (quote
62 (0 font-lock-keyword-face))))))))
64 (ert-deftest number-sequence-test ()
65 (should (= (length
66 (number-sequence (1- most-positive-fixnum) most-positive-fixnum))
67 2))
68 (should (= (length
69 (number-sequence
70 (1+ most-negative-fixnum) most-negative-fixnum -1))
71 2)))
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))
103 (let ((x 1))
104 (should-not (when nil
105 (setq x (1+ x))
107 (should (= x 1))
108 (should (= 2 (when t
109 (setq x (1+ x))
110 x)))
111 (should (= x 2)))
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)))
155 (should (equal
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)"))
158 (should (equal
159 (error-message-string (should-error (version-to-list "")))
160 "Invalid version syntax: `' (must start with a number)"))
161 (should (equal
162 (error-message-string (should-error (version-to-list "1.0..7.5")))
163 "Invalid version syntax: `1.0..7.5'"))
164 (should (equal
165 (error-message-string (should-error (version-to-list "1.0prepre2")))
166 "Invalid version syntax: `1.0prepre2'"))
167 (should (equal
168 (error-message-string (should-error (version-to-list "22.8X3")))
169 "Invalid version syntax: `22.8X3'"))
170 (should (equal
171 (error-message-string (should-error (version-to-list "beta22.8alpha3")))
172 "Invalid version syntax: `beta22.8alpha3' (must start with a number)"))
173 (should (equal
174 (error-message-string (should-error (version-to-list "honk")))
175 "Invalid version syntax: `honk' (must start with a number)"))
176 (should (equal
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)))
214 (should (equal
215 (error-message-string (should-error (version-to-list "1_0__7_5")))
216 "Invalid version syntax: `1_0__7_5'"))
217 (should (equal
218 (error-message-string (should-error (version-to-list "1_0prepre2")))
219 "Invalid version syntax: `1_0prepre2'"))
220 (should (equal
221 (error-message-string (should-error (version-to-list "22.8X3")))
222 "Invalid version syntax: `22.8X3'"))
223 (should (equal
224 (error-message-string (should-error (version-to-list "beta22_8alpha3")))
225 "Invalid version syntax: `beta22_8alpha3' (must start with a number)"))))
227 (provide 'subr-tests)
228 ;;; subr-tests.el ends here