* simple.el (delete-trailing-whitespace): Fix last change.
[emacs.git] / test / automated / ert-tests.el
blobcea994f64b834a471bb2b5b8a21c8b87e512d24a
1 ;;; ert-tests.el --- ERT's self-tests
3 ;; Copyright (C) 2007-2008, 2010-2011 Free Software Foundation, Inc.
5 ;; Author: Christian Ohler <ohler@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; This program is free software: you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation, either version 3 of the
12 ;; License, or (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
22 ;;; Commentary:
24 ;; This file is part of ERT, the Emacs Lisp Regression Testing tool.
25 ;; See ert.el or the texinfo manual for more details.
27 ;;; Code:
29 (eval-when-compile
30 (require 'cl))
31 (require 'ert)
34 ;;; Self-test that doesn't rely on ERT, for bootstrapping.
36 ;; This is used to test that bodies actually run.
37 (defvar ert--test-body-was-run)
38 (ert-deftest ert-test-body-runs ()
39 (setq ert--test-body-was-run t))
41 (defun ert-self-test ()
42 "Run ERT's self-tests and make sure they actually ran."
43 (let ((window-configuration (current-window-configuration)))
44 (let ((ert--test-body-was-run nil))
45 ;; The buffer name chosen here should not compete with the default
46 ;; results buffer name for completion in `switch-to-buffer'.
47 (let ((stats (ert-run-tests-interactively "^ert-" " *ert self-tests*")))
48 (assert ert--test-body-was-run)
49 (if (zerop (ert-stats-completed-unexpected stats))
50 ;; Hide results window only when everything went well.
51 (set-window-configuration window-configuration)
52 (error "ERT self-test failed"))))))
54 (defun ert-self-test-and-exit ()
55 "Run ERT's self-tests and exit Emacs.
57 The exit code will be zero if the tests passed, nonzero if they
58 failed or if there was a problem."
59 (unwind-protect
60 (progn
61 (ert-self-test)
62 (kill-emacs 0))
63 (unwind-protect
64 (progn
65 (message "Error running tests")
66 (backtrace))
67 (kill-emacs 1))))
70 ;;; Further tests are defined using ERT.
72 (ert-deftest ert-test-nested-test-body-runs ()
73 "Test that nested test bodies run."
74 (lexical-let ((was-run nil))
75 (let ((test (make-ert-test :body (lambda ()
76 (setq was-run t)))))
77 (assert (not was-run))
78 (ert-run-test test)
79 (assert was-run))))
82 ;;; Test that pass/fail works.
83 (ert-deftest ert-test-pass ()
84 (let ((test (make-ert-test :body (lambda ()))))
85 (let ((result (ert-run-test test)))
86 (assert (ert-test-passed-p result)))))
88 (ert-deftest ert-test-fail ()
89 (let ((test (make-ert-test :body (lambda () (ert-fail "failure message")))))
90 (let ((result (let ((ert-debug-on-error nil))
91 (ert-run-test test))))
92 (assert (ert-test-failed-p result) t)
93 (assert (equal (ert-test-result-with-condition-condition result)
94 '(ert-test-failed "failure message"))
95 t))))
97 (ert-deftest ert-test-fail-debug-with-condition-case ()
98 (let ((test (make-ert-test :body (lambda () (ert-fail "failure message")))))
99 (condition-case condition
100 (progn
101 (let ((ert-debug-on-error t))
102 (ert-run-test test))
103 (assert nil))
104 ((error)
105 (assert (equal condition '(ert-test-failed "failure message")) t)))))
107 (ert-deftest ert-test-fail-debug-with-debugger-1 ()
108 (let ((test (make-ert-test :body (lambda () (ert-fail "failure message")))))
109 (let ((debugger (lambda (&rest debugger-args)
110 (assert nil))))
111 (let ((ert-debug-on-error nil))
112 (ert-run-test test)))))
114 (ert-deftest ert-test-fail-debug-with-debugger-2 ()
115 (let ((test (make-ert-test :body (lambda () (ert-fail "failure message")))))
116 (block nil
117 (let ((debugger (lambda (&rest debugger-args)
118 (return-from nil nil))))
119 (let ((ert-debug-on-error t))
120 (ert-run-test test))
121 (assert nil)))))
123 (ert-deftest ert-test-fail-debug-nested-with-debugger ()
124 (let ((test (make-ert-test :body (lambda ()
125 (let ((ert-debug-on-error t))
126 (ert-fail "failure message"))))))
127 (let ((debugger (lambda (&rest debugger-args)
128 (assert nil nil "Assertion a"))))
129 (let ((ert-debug-on-error nil))
130 (ert-run-test test))))
131 (let ((test (make-ert-test :body (lambda ()
132 (let ((ert-debug-on-error nil))
133 (ert-fail "failure message"))))))
134 (block nil
135 (let ((debugger (lambda (&rest debugger-args)
136 (return-from nil nil))))
137 (let ((ert-debug-on-error t))
138 (ert-run-test test))
139 (assert nil nil "Assertion b")))))
141 (ert-deftest ert-test-error ()
142 (let ((test (make-ert-test :body (lambda () (error "Error message")))))
143 (let ((result (let ((ert-debug-on-error nil))
144 (ert-run-test test))))
145 (assert (ert-test-failed-p result) t)
146 (assert (equal (ert-test-result-with-condition-condition result)
147 '(error "Error message"))
148 t))))
150 (ert-deftest ert-test-error-debug ()
151 (let ((test (make-ert-test :body (lambda () (error "Error message")))))
152 (condition-case condition
153 (progn
154 (let ((ert-debug-on-error t))
155 (ert-run-test test))
156 (assert nil))
157 ((error)
158 (assert (equal condition '(error "Error message")) t)))))
161 ;;; Test that `should' works.
162 (ert-deftest ert-test-should ()
163 (let ((test (make-ert-test :body (lambda () (should nil)))))
164 (let ((result (let ((ert-debug-on-error nil))
165 (ert-run-test test))))
166 (assert (ert-test-failed-p result) t)
167 (assert (equal (ert-test-result-with-condition-condition result)
168 '(ert-test-failed ((should nil) :form nil :value nil)))
169 t)))
170 (let ((test (make-ert-test :body (lambda () (should t)))))
171 (let ((result (ert-run-test test)))
172 (assert (ert-test-passed-p result) t))))
174 (ert-deftest ert-test-should-value ()
175 (should (eql (should 'foo) 'foo))
176 (should (eql (should 'bar) 'bar)))
178 (ert-deftest ert-test-should-not ()
179 (let ((test (make-ert-test :body (lambda () (should-not t)))))
180 (let ((result (let ((ert-debug-on-error nil))
181 (ert-run-test test))))
182 (assert (ert-test-failed-p result) t)
183 (assert (equal (ert-test-result-with-condition-condition result)
184 '(ert-test-failed ((should-not t) :form t :value t)))
185 t)))
186 (let ((test (make-ert-test :body (lambda () (should-not nil)))))
187 (let ((result (ert-run-test test)))
188 (assert (ert-test-passed-p result)))))
190 (ert-deftest ert-test-should-with-macrolet ()
191 (let ((test (make-ert-test :body (lambda ()
192 (macrolet ((foo () `(progn t nil)))
193 (should (foo)))))))
194 (let ((result (let ((ert-debug-on-error nil))
195 (ert-run-test test))))
196 (should (ert-test-failed-p result))
197 (should (equal
198 (ert-test-result-with-condition-condition result)
199 '(ert-test-failed ((should (foo))
200 :form (progn t nil)
201 :value nil)))))))
203 (ert-deftest ert-test-should-error ()
204 ;; No error.
205 (let ((test (make-ert-test :body (lambda () (should-error (progn))))))
206 (let ((result (let ((ert-debug-on-error nil))
207 (ert-run-test test))))
208 (should (ert-test-failed-p result))
209 (should (equal (ert-test-result-with-condition-condition result)
210 '(ert-test-failed
211 ((should-error (progn))
212 :form (progn)
213 :value nil
214 :fail-reason "did not signal an error"))))))
215 ;; A simple error.
216 (should (equal (should-error (error "Foo"))
217 '(error "Foo")))
218 ;; Error of unexpected type.
219 (let ((test (make-ert-test :body (lambda ()
220 (should-error (error "Foo")
221 :type 'singularity-error)))))
222 (let ((result (ert-run-test test)))
223 (should (ert-test-failed-p result))
224 (should (equal
225 (ert-test-result-with-condition-condition result)
226 '(ert-test-failed
227 ((should-error (error "Foo") :type 'singularity-error)
228 :form (error "Foo")
229 :condition (error "Foo")
230 :fail-reason
231 "the error signalled did not have the expected type"))))))
232 ;; Error of the expected type.
233 (let* ((error nil)
234 (test (make-ert-test
235 :body (lambda ()
236 (setq error
237 (should-error (signal 'singularity-error nil)
238 :type 'singularity-error))))))
239 (let ((result (ert-run-test test)))
240 (should (ert-test-passed-p result))
241 (should (equal error '(singularity-error))))))
243 (ert-deftest ert-test-should-error-subtypes ()
244 (should-error (signal 'singularity-error nil)
245 :type 'singularity-error
246 :exclude-subtypes t)
247 (let ((test (make-ert-test
248 :body (lambda ()
249 (should-error (signal 'arith-error nil)
250 :type 'singularity-error)))))
251 (let ((result (ert-run-test test)))
252 (should (ert-test-failed-p result))
253 (should (equal
254 (ert-test-result-with-condition-condition result)
255 '(ert-test-failed
256 ((should-error (signal 'arith-error nil)
257 :type 'singularity-error)
258 :form (signal arith-error nil)
259 :condition (arith-error)
260 :fail-reason
261 "the error signalled did not have the expected type"))))))
262 (let ((test (make-ert-test
263 :body (lambda ()
264 (should-error (signal 'arith-error nil)
265 :type 'singularity-error
266 :exclude-subtypes t)))))
267 (let ((result (ert-run-test test)))
268 (should (ert-test-failed-p result))
269 (should (equal
270 (ert-test-result-with-condition-condition result)
271 '(ert-test-failed
272 ((should-error (signal 'arith-error nil)
273 :type 'singularity-error
274 :exclude-subtypes t)
275 :form (signal arith-error nil)
276 :condition (arith-error)
277 :fail-reason
278 "the error signalled did not have the expected type"))))))
279 (let ((test (make-ert-test
280 :body (lambda ()
281 (should-error (signal 'singularity-error nil)
282 :type 'arith-error
283 :exclude-subtypes t)))))
284 (let ((result (ert-run-test test)))
285 (should (ert-test-failed-p result))
286 (should (equal
287 (ert-test-result-with-condition-condition result)
288 '(ert-test-failed
289 ((should-error (signal 'singularity-error nil)
290 :type 'arith-error
291 :exclude-subtypes t)
292 :form (signal singularity-error nil)
293 :condition (singularity-error)
294 :fail-reason
295 "the error signalled was a subtype of the expected type")))))
298 (defmacro ert--test-my-list (&rest args)
299 "Don't use this. Instead, call `list' with ARGS, it does the same thing.
301 This macro is used to test if macroexpansion in `should' works."
302 `(list ,@args))
304 (ert-deftest ert-test-should-failure-debugging ()
305 "Test that `should' errors contain the information we expect them to."
306 (loop for (body expected-condition) in
307 `((,(lambda () (let ((x nil)) (should x)))
308 (ert-test-failed ((should x) :form x :value nil)))
309 (,(lambda () (let ((x t)) (should-not x)))
310 (ert-test-failed ((should-not x) :form x :value t)))
311 (,(lambda () (let ((x t)) (should (not x))))
312 (ert-test-failed ((should (not x)) :form (not t) :value nil)))
313 (,(lambda () (let ((x nil)) (should-not (not x))))
314 (ert-test-failed ((should-not (not x)) :form (not nil) :value t)))
315 (,(lambda () (let ((x t) (y nil)) (should-not
316 (ert--test-my-list x y))))
317 (ert-test-failed
318 ((should-not (ert--test-my-list x y))
319 :form (list t nil)
320 :value (t nil))))
321 (,(lambda () (let ((x t)) (should (error "Foo"))))
322 (error "Foo")))
324 (let ((test (make-ert-test :body body)))
325 (condition-case actual-condition
326 (progn
327 (let ((ert-debug-on-error t))
328 (ert-run-test test))
329 (assert nil))
330 ((error)
331 (should (equal actual-condition expected-condition)))))))
333 (ert-deftest ert-test-deftest ()
334 (should (equal (macroexpand '(ert-deftest abc () "foo" :tags '(bar)))
335 '(progn
336 (ert-set-test 'abc
337 (make-ert-test :name 'abc
338 :documentation "foo"
339 :tags '(bar)
340 :body (lambda ())))
341 (push '(ert-deftest . abc) current-load-list)
342 'abc)))
343 (should (equal (macroexpand '(ert-deftest def ()
344 :expected-result ':passed))
345 '(progn
346 (ert-set-test 'def
347 (make-ert-test :name 'def
348 :expected-result-type ':passed
349 :body (lambda ())))
350 (push '(ert-deftest . def) current-load-list)
351 'def)))
352 ;; :documentation keyword is forbidden
353 (should-error (macroexpand '(ert-deftest ghi ()
354 :documentation "foo"))))
356 (ert-deftest ert-test-record-backtrace ()
357 (let ((test (make-ert-test :body (lambda () (ert-fail "foo")))))
358 (let ((result (ert-run-test test)))
359 (should (ert-test-failed-p result))
360 (with-temp-buffer
361 (ert--print-backtrace (ert-test-failed-backtrace result))
362 (goto-char (point-min))
363 (end-of-line)
364 (let ((first-line (buffer-substring-no-properties (point-min) (point))))
365 (should (equal first-line " signal(ert-test-failed (\"foo\"))")))))))
367 (ert-deftest ert-test-messages ()
368 :tags '(:causes-redisplay)
369 (let* ((message-string "Test message")
370 (messages-buffer (get-buffer-create "*Messages*"))
371 (test (make-ert-test :body (lambda () (message "%s" message-string)))))
372 (with-current-buffer messages-buffer
373 (let ((result (ert-run-test test)))
374 (should (equal (concat message-string "\n")
375 (ert-test-result-messages result)))))))
377 (ert-deftest ert-test-running-tests ()
378 (let ((outer-test (ert-get-test 'ert-test-running-tests)))
379 (should (equal (ert-running-test) outer-test))
380 (let (test1 test2 test3)
381 (setq test1 (make-ert-test
382 :name "1"
383 :body (lambda ()
384 (should (equal (ert-running-test) outer-test))
385 (should (equal ert--running-tests
386 (list test1 test2 test3
387 outer-test)))))
388 test2 (make-ert-test
389 :name "2"
390 :body (lambda ()
391 (should (equal (ert-running-test) outer-test))
392 (should (equal ert--running-tests
393 (list test3 test2 outer-test)))
394 (ert-run-test test1)))
395 test3 (make-ert-test
396 :name "3"
397 :body (lambda ()
398 (should (equal (ert-running-test) outer-test))
399 (should (equal ert--running-tests
400 (list test3 outer-test)))
401 (ert-run-test test2))))
402 (should (ert-test-passed-p (ert-run-test test3))))))
404 (ert-deftest ert-test-test-result-expected-p ()
405 "Test `ert-test-result-expected-p' and (implicitly) `ert-test-result-type-p'."
406 ;; passing test
407 (let ((test (make-ert-test :body (lambda ()))))
408 (should (ert-test-result-expected-p test (ert-run-test test))))
409 ;; unexpected failure
410 (let ((test (make-ert-test :body (lambda () (ert-fail "failed")))))
411 (should-not (ert-test-result-expected-p test (ert-run-test test))))
412 ;; expected failure
413 (let ((test (make-ert-test :body (lambda () (ert-fail "failed"))
414 :expected-result-type ':failed)))
415 (should (ert-test-result-expected-p test (ert-run-test test))))
416 ;; `not' expected type
417 (let ((test (make-ert-test :body (lambda ())
418 :expected-result-type '(not :failed))))
419 (should (ert-test-result-expected-p test (ert-run-test test))))
420 (let ((test (make-ert-test :body (lambda ())
421 :expected-result-type '(not :passed))))
422 (should-not (ert-test-result-expected-p test (ert-run-test test))))
423 ;; `and' expected type
424 (let ((test (make-ert-test :body (lambda ())
425 :expected-result-type '(and :passed :failed))))
426 (should-not (ert-test-result-expected-p test (ert-run-test test))))
427 (let ((test (make-ert-test :body (lambda ())
428 :expected-result-type '(and :passed
429 (not :failed)))))
430 (should (ert-test-result-expected-p test (ert-run-test test))))
431 ;; `or' expected type
432 (let ((test (make-ert-test :body (lambda ())
433 :expected-result-type '(or (and :passed :failed)
434 :passed))))
435 (should (ert-test-result-expected-p test (ert-run-test test))))
436 (let ((test (make-ert-test :body (lambda ())
437 :expected-result-type '(or (and :passed :failed)
438 nil (not t)))))
439 (should-not (ert-test-result-expected-p test (ert-run-test test)))))
441 ;;; Test `ert-select-tests'.
442 (ert-deftest ert-test-select-regexp ()
443 (should (equal (ert-select-tests "^ert-test-select-regexp$" t)
444 (list (ert-get-test 'ert-test-select-regexp)))))
446 (ert-deftest ert-test-test-boundp ()
447 (should (ert-test-boundp 'ert-test-test-boundp))
448 (should-not (ert-test-boundp (make-symbol "ert-not-a-test"))))
450 (ert-deftest ert-test-select-member ()
451 (should (equal (ert-select-tests '(member ert-test-select-member) t)
452 (list (ert-get-test 'ert-test-select-member)))))
454 (ert-deftest ert-test-select-test ()
455 (should (equal (ert-select-tests (ert-get-test 'ert-test-select-test) t)
456 (list (ert-get-test 'ert-test-select-test)))))
458 (ert-deftest ert-test-select-symbol ()
459 (should (equal (ert-select-tests 'ert-test-select-symbol t)
460 (list (ert-get-test 'ert-test-select-symbol)))))
462 (ert-deftest ert-test-select-and ()
463 (let ((test (make-ert-test
464 :name nil
465 :body nil
466 :most-recent-result (make-ert-test-failed
467 :condition nil
468 :backtrace nil
469 :infos nil))))
470 (should (equal (ert-select-tests `(and (member ,test) :failed) t)
471 (list test)))))
473 (ert-deftest ert-test-select-tag ()
474 (let ((test (make-ert-test
475 :name nil
476 :body nil
477 :tags '(a b))))
478 (should (equal (ert-select-tests `(tag a) (list test)) (list test)))
479 (should (equal (ert-select-tests `(tag b) (list test)) (list test)))
480 (should (equal (ert-select-tests `(tag c) (list test)) '()))))
483 ;;; Tests for utility functions.
484 (ert-deftest ert-test-proper-list-p ()
485 (should (ert--proper-list-p '()))
486 (should (ert--proper-list-p '(1)))
487 (should (ert--proper-list-p '(1 2)))
488 (should (ert--proper-list-p '(1 2 3)))
489 (should (ert--proper-list-p '(1 2 3 4)))
490 (should (not (ert--proper-list-p 'a)))
491 (should (not (ert--proper-list-p '(1 . a))))
492 (should (not (ert--proper-list-p '(1 2 . a))))
493 (should (not (ert--proper-list-p '(1 2 3 . a))))
494 (should (not (ert--proper-list-p '(1 2 3 4 . a))))
495 (let ((a (list 1)))
496 (setf (cdr (last a)) a)
497 (should (not (ert--proper-list-p a))))
498 (let ((a (list 1 2)))
499 (setf (cdr (last a)) a)
500 (should (not (ert--proper-list-p a))))
501 (let ((a (list 1 2 3)))
502 (setf (cdr (last a)) a)
503 (should (not (ert--proper-list-p a))))
504 (let ((a (list 1 2 3 4)))
505 (setf (cdr (last a)) a)
506 (should (not (ert--proper-list-p a))))
507 (let ((a (list 1 2)))
508 (setf (cdr (last a)) (cdr a))
509 (should (not (ert--proper-list-p a))))
510 (let ((a (list 1 2 3)))
511 (setf (cdr (last a)) (cdr a))
512 (should (not (ert--proper-list-p a))))
513 (let ((a (list 1 2 3 4)))
514 (setf (cdr (last a)) (cdr a))
515 (should (not (ert--proper-list-p a))))
516 (let ((a (list 1 2 3)))
517 (setf (cdr (last a)) (cddr a))
518 (should (not (ert--proper-list-p a))))
519 (let ((a (list 1 2 3 4)))
520 (setf (cdr (last a)) (cddr a))
521 (should (not (ert--proper-list-p a))))
522 (let ((a (list 1 2 3 4)))
523 (setf (cdr (last a)) (cdddr a))
524 (should (not (ert--proper-list-p a)))))
526 (ert-deftest ert-test-parse-keys-and-body ()
527 (should (equal (ert--parse-keys-and-body '(foo)) '(nil (foo))))
528 (should (equal (ert--parse-keys-and-body '(:bar foo)) '((:bar foo) nil)))
529 (should (equal (ert--parse-keys-and-body '(:bar foo a (b)))
530 '((:bar foo) (a (b)))))
531 (should (equal (ert--parse-keys-and-body '(:bar foo :a (b)))
532 '((:bar foo :a (b)) nil)))
533 (should (equal (ert--parse-keys-and-body '(bar foo :a (b)))
534 '(nil (bar foo :a (b)))))
535 (should-error (ert--parse-keys-and-body '(:bar foo :a))))
538 (ert-deftest ert-test-run-tests-interactively ()
539 :tags '(:causes-redisplay)
540 (let ((passing-test (make-ert-test :name 'passing-test
541 :body (lambda () (ert-pass))))
542 (failing-test (make-ert-test :name 'failing-test
543 :body (lambda () (ert-fail
544 "failure message")))))
545 (let ((ert-debug-on-error nil))
546 (let* ((buffer-name (generate-new-buffer-name " *ert-test-run-tests*"))
547 (messages nil)
548 (mock-message-fn
549 (lambda (format-string &rest args)
550 (push (apply #'format format-string args) messages))))
551 (save-window-excursion
552 (unwind-protect
553 (let ((case-fold-search nil))
554 (ert-run-tests-interactively
555 `(member ,passing-test ,failing-test) buffer-name
556 mock-message-fn)
557 (should (equal messages `(,(concat
558 "Ran 2 tests, 1 results were "
559 "as expected, 1 unexpected"))))
560 (with-current-buffer buffer-name
561 (goto-char (point-min))
562 (should (equal
563 (buffer-substring (point-min)
564 (save-excursion
565 (forward-line 4)
566 (point)))
567 (concat
568 "Selector: (member <passing-test> <failing-test>)\n"
569 "Passed: 1\n"
570 "Failed: 1 (1 unexpected)\n"
571 "Total: 2/2\n")))))
572 (when (get-buffer buffer-name)
573 (kill-buffer buffer-name))))))))
575 (ert-deftest ert-test-special-operator-p ()
576 (should (ert--special-operator-p 'if))
577 (should-not (ert--special-operator-p 'car))
578 (should-not (ert--special-operator-p 'ert--special-operator-p))
579 (let ((b (ert--gensym)))
580 (should-not (ert--special-operator-p b))
581 (fset b 'if)
582 (should (ert--special-operator-p b))))
584 (ert-deftest ert-test-list-of-should-forms ()
585 (let ((test (make-ert-test :body (lambda ()
586 (should t)
587 (should (null '()))
588 (should nil)
589 (should t)))))
590 (let ((result (let ((ert-debug-on-error nil))
591 (ert-run-test test))))
592 (should (equal (ert-test-result-should-forms result)
593 '(((should t) :form t :value t)
594 ((should (null '())) :form (null nil) :value t)
595 ((should nil) :form nil :value nil)))))))
597 (ert-deftest ert-test-list-of-should-forms-observers-should-not-stack ()
598 (let ((test (make-ert-test
599 :body (lambda ()
600 (let ((test2 (make-ert-test
601 :body (lambda ()
602 (should t)))))
603 (let ((result (ert-run-test test2)))
604 (should (ert-test-passed-p result))))))))
605 (let ((result (let ((ert-debug-on-error nil))
606 (ert-run-test test))))
607 (should (ert-test-passed-p result))
608 (should (eql (length (ert-test-result-should-forms result))
609 1)))))
611 (ert-deftest ert-test-list-of-should-forms-no-deep-copy ()
612 (let ((test (make-ert-test :body (lambda ()
613 (let ((obj (list 'a)))
614 (should (equal obj '(a)))
615 (setf (car obj) 'b)
616 (should (equal obj '(b))))))))
617 (let ((result (let ((ert-debug-on-error nil))
618 (ert-run-test test))))
619 (should (ert-test-passed-p result))
620 (should (equal (ert-test-result-should-forms result)
621 '(((should (equal obj '(a))) :form (equal (b) (a)) :value t
622 :explanation nil)
623 ((should (equal obj '(b))) :form (equal (b) (b)) :value t
624 :explanation nil)
625 ))))))
627 (ert-deftest ert-test-remprop ()
628 (let ((x (ert--gensym)))
629 (should (equal (symbol-plist x) '()))
630 ;; Remove nonexistent property on empty plist.
631 (ert--remprop x 'b)
632 (should (equal (symbol-plist x) '()))
633 (put x 'a 1)
634 (should (equal (symbol-plist x) '(a 1)))
635 ;; Remove nonexistent property on nonempty plist.
636 (ert--remprop x 'b)
637 (should (equal (symbol-plist x) '(a 1)))
638 (put x 'b 2)
639 (put x 'c 3)
640 (put x 'd 4)
641 (should (equal (symbol-plist x) '(a 1 b 2 c 3 d 4)))
642 ;; Remove property that is neither first nor last.
643 (ert--remprop x 'c)
644 (should (equal (symbol-plist x) '(a 1 b 2 d 4)))
645 ;; Remove last property from a plist of length >1.
646 (ert--remprop x 'd)
647 (should (equal (symbol-plist x) '(a 1 b 2)))
648 ;; Remove first property from a plist of length >1.
649 (ert--remprop x 'a)
650 (should (equal (symbol-plist x) '(b 2)))
651 ;; Remove property when there is only one.
652 (ert--remprop x 'b)
653 (should (equal (symbol-plist x) '()))))
655 (ert-deftest ert-test-remove-if-not ()
656 (let ((list (list 'a 'b 'c 'd))
657 (i 0))
658 (let ((result (ert--remove-if-not (lambda (x)
659 (should (eql x (nth i list)))
660 (incf i)
661 (member i '(2 3)))
662 list)))
663 (should (equal i 4))
664 (should (equal result '(b c)))
665 (should (equal list '(a b c d)))))
666 (should (equal '()
667 (ert--remove-if-not (lambda (x) (should nil)) '()))))
669 (ert-deftest ert-test-remove* ()
670 (let ((list (list 'a 'b 'c 'd))
671 (key-index 0)
672 (test-index 0))
673 (let ((result
674 (ert--remove* 'foo list
675 :key (lambda (x)
676 (should (eql x (nth key-index list)))
677 (prog1
678 (list key-index x)
679 (incf key-index)))
680 :test
681 (lambda (a b)
682 (should (eql a 'foo))
683 (should (equal b (list test-index
684 (nth test-index list))))
685 (incf test-index)
686 (member test-index '(2 3))))))
687 (should (equal key-index 4))
688 (should (equal test-index 4))
689 (should (equal result '(a d)))
690 (should (equal list '(a b c d)))))
691 (let ((x (cons nil nil))
692 (y (cons nil nil)))
693 (should (equal (ert--remove* x (list x y))
694 ;; or (list x), since we use `equal' -- the
695 ;; important thing is that only one element got
696 ;; removed, this proves that the default test is
697 ;; `eql', not `equal'
698 (list y)))))
701 (ert-deftest ert-test-set-functions ()
702 (let ((c1 (cons nil nil))
703 (c2 (cons nil nil))
704 (sym (make-symbol "a")))
705 (let ((e '())
706 (a (list 'a 'b sym nil "" "x" c1 c2))
707 (b (list c1 'y 'b sym 'x)))
708 (should (equal (ert--set-difference e e) e))
709 (should (equal (ert--set-difference a e) a))
710 (should (equal (ert--set-difference e a) e))
711 (should (equal (ert--set-difference a a) e))
712 (should (equal (ert--set-difference b e) b))
713 (should (equal (ert--set-difference e b) e))
714 (should (equal (ert--set-difference b b) e))
715 (should (equal (ert--set-difference a b) (list 'a nil "" "x" c2)))
716 (should (equal (ert--set-difference b a) (list 'y 'x)))
718 ;; We aren't testing whether this is really using `eq' rather than `eql'.
719 (should (equal (ert--set-difference-eq e e) e))
720 (should (equal (ert--set-difference-eq a e) a))
721 (should (equal (ert--set-difference-eq e a) e))
722 (should (equal (ert--set-difference-eq a a) e))
723 (should (equal (ert--set-difference-eq b e) b))
724 (should (equal (ert--set-difference-eq e b) e))
725 (should (equal (ert--set-difference-eq b b) e))
726 (should (equal (ert--set-difference-eq a b) (list 'a nil "" "x" c2)))
727 (should (equal (ert--set-difference-eq b a) (list 'y 'x)))
729 (should (equal (ert--union e e) e))
730 (should (equal (ert--union a e) a))
731 (should (equal (ert--union e a) a))
732 (should (equal (ert--union a a) a))
733 (should (equal (ert--union b e) b))
734 (should (equal (ert--union e b) b))
735 (should (equal (ert--union b b) b))
736 (should (equal (ert--union a b) (list 'a 'b sym nil "" "x" c1 c2 'y 'x)))
737 (should (equal (ert--union b a) (list c1 'y 'b sym 'x 'a nil "" "x" c2)))
739 (should (equal (ert--intersection e e) e))
740 (should (equal (ert--intersection a e) e))
741 (should (equal (ert--intersection e a) e))
742 (should (equal (ert--intersection a a) a))
743 (should (equal (ert--intersection b e) e))
744 (should (equal (ert--intersection e b) e))
745 (should (equal (ert--intersection b b) b))
746 (should (equal (ert--intersection a b) (list 'b sym c1)))
747 (should (equal (ert--intersection b a) (list c1 'b sym))))))
749 (ert-deftest ert-test-gensym ()
750 ;; Since the expansion of `should' calls `ert--gensym' and thus has a
751 ;; side-effect on `ert--gensym-counter', we have to make sure all
752 ;; macros in our test body are expanded before we rebind
753 ;; `ert--gensym-counter' and run the body. Otherwise, the test would
754 ;; fail if run interpreted.
755 (let ((body (byte-compile
756 '(lambda ()
757 (should (equal (symbol-name (ert--gensym)) "G0"))
758 (should (equal (symbol-name (ert--gensym)) "G1"))
759 (should (equal (symbol-name (ert--gensym)) "G2"))
760 (should (equal (symbol-name (ert--gensym "foo")) "foo3"))
761 (should (equal (symbol-name (ert--gensym "bar")) "bar4"))
762 (should (equal ert--gensym-counter 5))))))
763 (let ((ert--gensym-counter 0))
764 (funcall body))))
766 (ert-deftest ert-test-coerce-to-vector ()
767 (let* ((a (vector))
768 (b (vector 1 a 3))
769 (c (list))
770 (d (list b a)))
771 (should (eql (ert--coerce-to-vector a) a))
772 (should (eql (ert--coerce-to-vector b) b))
773 (should (equal (ert--coerce-to-vector c) (vector)))
774 (should (equal (ert--coerce-to-vector d) (vector b a)))))
776 (ert-deftest ert-test-string-position ()
777 (should (eql (ert--string-position ?x "") nil))
778 (should (eql (ert--string-position ?a "abc") 0))
779 (should (eql (ert--string-position ?b "abc") 1))
780 (should (eql (ert--string-position ?c "abc") 2))
781 (should (eql (ert--string-position ?d "abc") nil))
782 (should (eql (ert--string-position ?A "abc") nil)))
784 (ert-deftest ert-test-mismatch ()
785 (should (eql (ert--mismatch "" "") nil))
786 (should (eql (ert--mismatch "" "a") 0))
787 (should (eql (ert--mismatch "a" "a") nil))
788 (should (eql (ert--mismatch "ab" "a") 1))
789 (should (eql (ert--mismatch "Aa" "aA") 0))
790 (should (eql (ert--mismatch '(a b c) '(a b d)) 2)))
792 (ert-deftest ert-test-string-first-line ()
793 (should (equal (ert--string-first-line "") ""))
794 (should (equal (ert--string-first-line "abc") "abc"))
795 (should (equal (ert--string-first-line "abc\n") "abc"))
796 (should (equal (ert--string-first-line "foo\nbar") "foo"))
797 (should (equal (ert--string-first-line " foo\nbar\nbaz\n") " foo")))
799 (ert-deftest ert-test-explain-equal ()
800 (should (equal (ert--explain-equal nil 'foo)
801 '(different-atoms nil foo)))
802 (should (equal (ert--explain-equal '(a a) '(a b))
803 '(list-elt 1 (different-atoms a b))))
804 (should (equal (ert--explain-equal '(1 48) '(1 49))
805 '(list-elt 1 (different-atoms (48 "#x30" "?0")
806 (49 "#x31" "?1")))))
807 (should (equal (ert--explain-equal 'nil '(a))
808 '(different-types nil (a))))
809 (should (equal (ert--explain-equal '(a b c) '(a b c d))
810 '(proper-lists-of-different-length 3 4 (a b c) (a b c d)
811 first-mismatch-at 3)))
812 (let ((sym (make-symbol "a")))
813 (should (equal (ert--explain-equal 'a sym)
814 `(different-symbols-with-the-same-name a ,sym)))))
816 (ert-deftest ert-test-explain-equal-improper-list ()
817 (should (equal (ert--explain-equal '(a . b) '(a . c))
818 '(cdr (different-atoms b c)))))
820 (ert-deftest ert-test-explain-equal-keymaps ()
821 ;; This used to be very slow.
822 (should (equal (make-keymap) (make-keymap)))
823 (should (equal (make-sparse-keymap) (make-sparse-keymap))))
825 (ert-deftest ert-test-significant-plist-keys ()
826 (should (equal (ert--significant-plist-keys '()) '()))
827 (should (equal (ert--significant-plist-keys '(a b c d e f c g p q r nil s t))
828 '(a c e p s))))
830 (ert-deftest ert-test-plist-difference-explanation ()
831 (should (equal (ert--plist-difference-explanation
832 '(a b c nil) '(a b))
833 nil))
834 (should (equal (ert--plist-difference-explanation
835 '(a b c t) '(a b))
836 '(different-properties-for-key c (different-atoms t nil))))
837 (should (equal (ert--plist-difference-explanation
838 '(a b c t) '(c nil a b))
839 '(different-properties-for-key c (different-atoms t nil))))
840 (should (equal (ert--plist-difference-explanation
841 '(a b c (foo . bar)) '(c (foo . baz) a b))
842 '(different-properties-for-key c
843 (cdr
844 (different-atoms bar baz))))))
846 (ert-deftest ert-test-abbreviate-string ()
847 (should (equal (ert--abbreviate-string "foo" 4 nil) "foo"))
848 (should (equal (ert--abbreviate-string "foo" 3 nil) "foo"))
849 (should (equal (ert--abbreviate-string "foo" 3 nil) "foo"))
850 (should (equal (ert--abbreviate-string "foo" 2 nil) "fo"))
851 (should (equal (ert--abbreviate-string "foo" 1 nil) "f"))
852 (should (equal (ert--abbreviate-string "foo" 0 nil) ""))
853 (should (equal (ert--abbreviate-string "bar" 4 t) "bar"))
854 (should (equal (ert--abbreviate-string "bar" 3 t) "bar"))
855 (should (equal (ert--abbreviate-string "bar" 3 t) "bar"))
856 (should (equal (ert--abbreviate-string "bar" 2 t) "ar"))
857 (should (equal (ert--abbreviate-string "bar" 1 t) "r"))
858 (should (equal (ert--abbreviate-string "bar" 0 t) "")))
860 (ert-deftest ert-test-explain-equal-string-properties ()
861 (should
862 (equal (ert--explain-equal-including-properties #("foo" 0 1 (a b))
863 "foo")
864 '(char 0 "f"
865 (different-properties-for-key a (different-atoms b nil))
866 context-before ""
867 context-after "oo")))
868 (should (equal (ert--explain-equal-including-properties
869 #("foo" 1 3 (a b))
870 #("goo" 0 1 (c d)))
871 '(array-elt 0 (different-atoms (?f "#x66" "?f")
872 (?g "#x67" "?g")))))
873 (should
874 (equal (ert--explain-equal-including-properties
875 #("foo" 0 1 (a b c d) 1 3 (a b))
876 #("foo" 0 1 (c d a b) 1 2 (a foo)))
877 '(char 1 "o" (different-properties-for-key a (different-atoms b foo))
878 context-before "f" context-after "o"))))
880 (ert-deftest ert-test-equal-including-properties ()
881 (should (equal-including-properties "foo" "foo"))
882 (should (ert-equal-including-properties "foo" "foo"))
884 (should (equal-including-properties #("foo" 0 3 (a b))
885 (propertize "foo" 'a 'b)))
886 (should (ert-equal-including-properties #("foo" 0 3 (a b))
887 (propertize "foo" 'a 'b)))
889 (should (equal-including-properties #("foo" 0 3 (a b c d))
890 (propertize "foo" 'a 'b 'c 'd)))
891 (should (ert-equal-including-properties #("foo" 0 3 (a b c d))
892 (propertize "foo" 'a 'b 'c 'd)))
894 (should-not (equal-including-properties #("foo" 0 3 (a b c e))
895 (propertize "foo" 'a 'b 'c 'd)))
896 (should-not (ert-equal-including-properties #("foo" 0 3 (a b c e))
897 (propertize "foo" 'a 'b 'c 'd)))
899 ;; This is bug 6581.
900 (should-not (equal-including-properties #("foo" 0 3 (a (t)))
901 (propertize "foo" 'a (list t))))
902 (should (ert-equal-including-properties #("foo" 0 3 (a (t)))
903 (propertize "foo" 'a (list t)))))
905 (ert-deftest ert-test-stats-set-test-and-result ()
906 (let* ((test-1 (make-ert-test :name 'test-1
907 :body (lambda () nil)))
908 (test-2 (make-ert-test :name 'test-2
909 :body (lambda () nil)))
910 (test-3 (make-ert-test :name 'test-2
911 :body (lambda () nil)))
912 (stats (ert--make-stats (list test-1 test-2) 't))
913 (failed (make-ert-test-failed :condition nil
914 :backtrace nil
915 :infos nil)))
916 (should (eql 2 (ert-stats-total stats)))
917 (should (eql 0 (ert-stats-completed stats)))
918 (should (eql 0 (ert-stats-completed-expected stats)))
919 (should (eql 0 (ert-stats-completed-unexpected stats)))
920 (ert--stats-set-test-and-result stats 0 test-1 (make-ert-test-passed))
921 (should (eql 2 (ert-stats-total stats)))
922 (should (eql 1 (ert-stats-completed stats)))
923 (should (eql 1 (ert-stats-completed-expected stats)))
924 (should (eql 0 (ert-stats-completed-unexpected stats)))
925 (ert--stats-set-test-and-result stats 0 test-1 failed)
926 (should (eql 2 (ert-stats-total stats)))
927 (should (eql 1 (ert-stats-completed stats)))
928 (should (eql 0 (ert-stats-completed-expected stats)))
929 (should (eql 1 (ert-stats-completed-unexpected stats)))
930 (ert--stats-set-test-and-result stats 0 test-1 nil)
931 (should (eql 2 (ert-stats-total stats)))
932 (should (eql 0 (ert-stats-completed stats)))
933 (should (eql 0 (ert-stats-completed-expected stats)))
934 (should (eql 0 (ert-stats-completed-unexpected stats)))
935 (ert--stats-set-test-and-result stats 0 test-3 failed)
936 (should (eql 2 (ert-stats-total stats)))
937 (should (eql 1 (ert-stats-completed stats)))
938 (should (eql 0 (ert-stats-completed-expected stats)))
939 (should (eql 1 (ert-stats-completed-unexpected stats)))
940 (ert--stats-set-test-and-result stats 1 test-2 (make-ert-test-passed))
941 (should (eql 2 (ert-stats-total stats)))
942 (should (eql 2 (ert-stats-completed stats)))
943 (should (eql 1 (ert-stats-completed-expected stats)))
944 (should (eql 1 (ert-stats-completed-unexpected stats)))
945 (ert--stats-set-test-and-result stats 0 test-1 (make-ert-test-passed))
946 (should (eql 2 (ert-stats-total stats)))
947 (should (eql 2 (ert-stats-completed stats)))
948 (should (eql 2 (ert-stats-completed-expected stats)))
949 (should (eql 0 (ert-stats-completed-unexpected stats)))))
952 (provide 'ert-tests)
954 ;;; ert-tests.el ends here