aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorGlenn Morris2013-07-11 09:13:38 -0700
committerGlenn Morris2013-07-11 09:13:38 -0700
commita19b3c2d975c605c3ed76f1c178cdec7c3c7bdcf (patch)
treecd87d97c915194a3e615fac8445c90b7419876b0 /lisp
parent17bd3d0493fa7d0ecfa60a646141abebfc8290eb (diff)
downloademacs-a19b3c2d975c605c3ed76f1c178cdec7c3c7bdcf.tar.gz
emacs-a19b3c2d975c605c3ed76f1c178cdec7c3c7bdcf.zip
Stop reimplementing a bunch of cl- functions in ert
* lisp/emacs-lisp/ert.el: Require cl-lib at runtime too. (ert--cl-do-remf, ert--remprop, ert--remove-if-not) (ert--intersection, ert--set-difference, ert--set-difference-eq) (ert--union, ert--gensym-counter, ert--gensym-counter) (ert--coerce-to-vector, ert--remove*, ert--string-position) (ert--mismatch, ert--subseq): Remove reimplementations of cl funcs. (ert-make-test-unbound, ert--expand-should-1) (ert--expand-should, ert--should-error-handle-error) (should-error, ert--explain-equal-rec) (ert--plist-difference-explanation, ert-select-tests) (ert--make-stats, ert--remove-from-list, ert--string-first-line): Use cl-lib functions rather than reimplementations. * test/automated/ert-tests.el: Require cl-lib at runtime too. (ert-test-special-operator-p): Use cl-gensym rather than ert-- version. (ert-test-remprop, ert-test-remove-if-not, ert-test-remove*) (ert-test-set-functions, ert-test-gensym) (ert-test-coerce-to-vector, ert-test-string-position) (ert-test-mismatch): Remove tests. * test/automated/cl-lib.el: New, split from ert-tests.el.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/ChangeLog15
-rw-r--r--lisp/emacs-lisp/ert.el173
2 files changed, 41 insertions, 147 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 29f053c5ae1..7f5fd1efb23 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,18 @@
12013-07-11 Glenn Morris <rgm@gnu.org>
2
3 * emacs-lisp/ert.el: Require cl-lib at runtime too.
4 (ert--cl-do-remf, ert--remprop, ert--remove-if-not)
5 (ert--intersection, ert--set-difference, ert--set-difference-eq)
6 (ert--union, ert--gensym-counter, ert--gensym-counter)
7 (ert--coerce-to-vector, ert--remove*, ert--string-position)
8 (ert--mismatch, ert--subseq): Remove reimplementations of cl funcs.
9 (ert-make-test-unbound, ert--expand-should-1)
10 (ert--expand-should, ert--should-error-handle-error)
11 (should-error, ert--explain-equal-rec)
12 (ert--plist-difference-explanation, ert-select-tests)
13 (ert--make-stats, ert--remove-from-list, ert--string-first-line):
14 Use cl-lib functions rather than reimplementations.
15
12013-07-11 Michael Albinus <michael.albinus@gmx.de> 162013-07-11 Michael Albinus <michael.albinus@gmx.de>
2 17
3 * net/tramp.el (tramp-methods): Extend docstring. 18 * net/tramp.el (tramp-methods): Extend docstring.
diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el
index 656cb0a6a14..1f5edefea08 100644
--- a/lisp/emacs-lisp/ert.el
+++ b/lisp/emacs-lisp/ert.el
@@ -54,7 +54,7 @@
54 54
55;;; Code: 55;;; Code:
56 56
57(eval-when-compile (require 'cl-lib)) 57(require 'cl-lib)
58(require 'button) 58(require 'button)
59(require 'debug) 59(require 'debug)
60(require 'easymenu) 60(require 'easymenu)
@@ -87,127 +87,6 @@
87 87
88;;; Copies/reimplementations of cl functions. 88;;; Copies/reimplementations of cl functions.
89 89
90(defun ert--cl-do-remf (plist tag)
91 "Copy of `cl-do-remf'. Modify PLIST by removing TAG."
92 (let ((p (cdr plist)))
93 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
94 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
95
96(defun ert--remprop (sym tag)
97 "Copy of `cl-remprop'. Modify SYM's plist by removing TAG."
98 (let ((plist (symbol-plist sym)))
99 (if (and plist (eq tag (car plist)))
100 (progn (setplist sym (cdr (cdr plist))) t)
101 (ert--cl-do-remf plist tag))))
102
103(defun ert--remove-if-not (ert-pred ert-list)
104 "A reimplementation of `remove-if-not'.
105
106ERT-PRED is a predicate, ERT-LIST is the input list."
107 (cl-loop for ert-x in ert-list
108 if (funcall ert-pred ert-x)
109 collect ert-x))
110
111(defun ert--intersection (a b)
112 "A reimplementation of `intersection'. Intersect the sets A and B.
113
114Elements are compared using `eql'."
115 (cl-loop for x in a
116 if (memql x b)
117 collect x))
118
119(defun ert--set-difference (a b)
120 "A reimplementation of `set-difference'. Subtract the set B from the set A.
121
122Elements are compared using `eql'."
123 (cl-loop for x in a
124 unless (memql x b)
125 collect x))
126
127(defun ert--set-difference-eq (a b)
128 "A reimplementation of `set-difference'. Subtract the set B from the set A.
129
130Elements are compared using `eq'."
131 (cl-loop for x in a
132 unless (memq x b)
133 collect x))
134
135(defun ert--union (a b)
136 "A reimplementation of `union'. Compute the union of the sets A and B.
137
138Elements are compared using `eql'."
139 (append a (ert--set-difference b a)))
140
141(eval-and-compile
142 (defvar ert--gensym-counter 0))
143
144(eval-and-compile
145 (defun ert--gensym (&optional prefix)
146 "Only allows string PREFIX, not compatible with CL."
147 (unless prefix (setq prefix "G"))
148 (make-symbol (format "%s%s"
149 prefix
150 (prog1 ert--gensym-counter
151 (cl-incf ert--gensym-counter))))))
152
153(defun ert--coerce-to-vector (x)
154 "Coerce X to a vector."
155 (when (char-table-p x) (error "Not supported"))
156 (if (vectorp x)
157 x
158 (vconcat x)))
159
160(cl-defun ert--remove* (x list &key key test)
161 "Does not support all the keywords of remove*."
162 (unless key (setq key #'identity))
163 (unless test (setq test #'eql))
164 (cl-loop for y in list
165 unless (funcall test x (funcall key y))
166 collect y))
167
168(defun ert--string-position (c s)
169 "Return the position of the first occurrence of C in S, or nil if none."
170 (cl-loop for i from 0
171 for x across s
172 when (eql x c) return i))
173
174(defun ert--mismatch (a b)
175 "Return index of first element that differs between A and B.
176
177Like `mismatch'. Uses `equal' for comparison."
178 (cond ((or (listp a) (listp b))
179 (ert--mismatch (ert--coerce-to-vector a)
180 (ert--coerce-to-vector b)))
181 ((> (length a) (length b))
182 (ert--mismatch b a))
183 (t
184 (let ((la (length a))
185 (lb (length b)))
186 (cl-assert (arrayp a) t)
187 (cl-assert (arrayp b) t)
188 (cl-assert (<= la lb) t)
189 (cl-loop for i below la
190 when (not (equal (aref a i) (aref b i))) return i
191 finally (cl-return (if (/= la lb)
192 la
193 (cl-assert (equal a b) t)
194 nil)))))))
195
196(defun ert--subseq (seq start &optional end)
197 "Return a subsequence of SEQ from START to END."
198 (when (char-table-p seq) (error "Not supported"))
199 (let ((vector (substring (ert--coerce-to-vector seq) start end)))
200 (cl-etypecase seq
201 (vector vector)
202 (string (concat vector))
203 (list (append vector nil))
204 (bool-vector (cl-loop with result
205 = (make-bool-vector (length vector) nil)
206 for i below (length vector) do
207 (setf (aref result i) (aref vector i))
208 finally (cl-return result)))
209 (char-table (cl-assert nil)))))
210
211(defun ert-equal-including-properties (a b) 90(defun ert-equal-including-properties (a b)
212 "Return t if A and B have similar structure and contents. 91 "Return t if A and B have similar structure and contents.
213 92
@@ -258,7 +137,7 @@ Emacs bug 6581 at URL `http://debbugs.gnu.org/cgi/bugreport.cgi?bug=6581'."
258 137
259(defun ert-make-test-unbound (symbol) 138(defun ert-make-test-unbound (symbol)
260 "Make SYMBOL name no test. Return SYMBOL." 139 "Make SYMBOL name no test. Return SYMBOL."
261 (ert--remprop symbol 'ert--test) 140 (cl-remprop symbol 'ert--test)
262 symbol) 141 symbol)
263 142
264(defun ert--parse-keys-and-body (keys-and-body) 143(defun ert--parse-keys-and-body (keys-and-body)
@@ -396,8 +275,8 @@ DATA is displayed to the user and should state the reason of the failure."
396 cl-macro-environment))))) 275 cl-macro-environment)))))
397 (cond 276 (cond
398 ((or (atom form) (ert--special-operator-p (car form))) 277 ((or (atom form) (ert--special-operator-p (car form)))
399 (let ((value (ert--gensym "value-"))) 278 (let ((value (cl-gensym "value-")))
400 `(let ((,value (ert--gensym "ert-form-evaluation-aborted-"))) 279 `(let ((,value (cl-gensym "ert-form-evaluation-aborted-")))
401 ,(funcall inner-expander 280 ,(funcall inner-expander
402 `(setq ,value ,form) 281 `(setq ,value ,form)
403 `(list ',whole :form ',form :value ,value) 282 `(list ',whole :form ',form :value ,value)
@@ -410,10 +289,10 @@ DATA is displayed to the user and should state the reason of the failure."
410 (and (consp fn-name) 289 (and (consp fn-name)
411 (eql (car fn-name) 'lambda) 290 (eql (car fn-name) 'lambda)
412 (listp (cdr fn-name))))) 291 (listp (cdr fn-name)))))
413 (let ((fn (ert--gensym "fn-")) 292 (let ((fn (cl-gensym "fn-"))
414 (args (ert--gensym "args-")) 293 (args (cl-gensym "args-"))
415 (value (ert--gensym "value-")) 294 (value (cl-gensym "value-"))
416 (default-value (ert--gensym "ert-form-evaluation-aborted-"))) 295 (default-value (cl-gensym "ert-form-evaluation-aborted-")))
417 `(let ((,fn (function ,fn-name)) 296 `(let ((,fn (function ,fn-name))
418 (,args (list ,@arg-forms))) 297 (,args (list ,@arg-forms)))
419 (let ((,value ',default-value)) 298 (let ((,value ',default-value))
@@ -450,7 +329,7 @@ FORM-DESCRIPTION-FORM before it has called INNER-FORM."
450 (ert--expand-should-1 329 (ert--expand-should-1
451 whole form 330 whole form
452 (lambda (inner-form form-description-form value-var) 331 (lambda (inner-form form-description-form value-var)
453 (let ((form-description (ert--gensym "form-description-"))) 332 (let ((form-description (cl-gensym "form-description-")))
454 `(let (,form-description) 333 `(let (,form-description)
455 ,(funcall inner-expander 334 ,(funcall inner-expander
456 `(unwind-protect 335 `(unwind-protect
@@ -491,7 +370,7 @@ and aborts the current test as failed if it doesn't."
491 (list type) 370 (list type)
492 (symbol (list type))))) 371 (symbol (list type)))))
493 (cl-assert signaled-conditions) 372 (cl-assert signaled-conditions)
494 (unless (ert--intersection signaled-conditions handled-conditions) 373 (unless (cl-intersection signaled-conditions handled-conditions)
495 (ert-fail (append 374 (ert-fail (append
496 (funcall form-description-fn) 375 (funcall form-description-fn)
497 (list 376 (list
@@ -528,8 +407,8 @@ failed."
528 `(should-error ,form ,@keys) 407 `(should-error ,form ,@keys)
529 form 408 form
530 (lambda (inner-form form-description-form value-var) 409 (lambda (inner-form form-description-form value-var)
531 (let ((errorp (ert--gensym "errorp")) 410 (let ((errorp (cl-gensym "errorp"))
532 (form-description-fn (ert--gensym "form-description-fn-"))) 411 (form-description-fn (cl-gensym "form-description-fn-")))
533 `(let ((,errorp nil) 412 `(let ((,errorp nil)
534 (,form-description-fn (lambda () ,form-description-form))) 413 (,form-description-fn (lambda () ,form-description-form)))
535 (condition-case -condition- 414 (condition-case -condition-
@@ -591,7 +470,7 @@ Returns nil if they are."
591 `(proper-lists-of-different-length ,(length a) ,(length b) 470 `(proper-lists-of-different-length ,(length a) ,(length b)
592 ,a ,b 471 ,a ,b
593 first-mismatch-at 472 first-mismatch-at
594 ,(ert--mismatch a b)) 473 ,(cl-mismatch a b :test 'equal))
595 (cl-loop for i from 0 474 (cl-loop for i from 0
596 for ai in a 475 for ai in a
597 for bi in b 476 for bi in b
@@ -611,7 +490,7 @@ Returns nil if they are."
611 ,a ,b 490 ,a ,b
612 ,@(unless (char-table-p a) 491 ,@(unless (char-table-p a)
613 `(first-mismatch-at 492 `(first-mismatch-at
614 ,(ert--mismatch a b)))) 493 ,(cl-mismatch a b :test 'equal))))
615 (cl-loop for i from 0 494 (cl-loop for i from 0
616 for ai across a 495 for ai across a
617 for bi across b 496 for bi across b
@@ -656,8 +535,8 @@ key/value pairs in each list does not matter."
656 ;; work, so let's punt on it for now. 535 ;; work, so let's punt on it for now.
657 (let* ((keys-a (ert--significant-plist-keys a)) 536 (let* ((keys-a (ert--significant-plist-keys a))
658 (keys-b (ert--significant-plist-keys b)) 537 (keys-b (ert--significant-plist-keys b))
659 (keys-in-a-not-in-b (ert--set-difference-eq keys-a keys-b)) 538 (keys-in-a-not-in-b (cl-set-difference keys-a keys-b :test 'eq))
660 (keys-in-b-not-in-a (ert--set-difference-eq keys-b keys-a))) 539 (keys-in-b-not-in-a (cl-set-difference keys-b keys-a :test 'eq)))
661 (cl-flet ((explain-with-key (key) 540 (cl-flet ((explain-with-key (key)
662 (let ((value-a (plist-get a key)) 541 (let ((value-a (plist-get a key))
663 (value-b (plist-get b key))) 542 (value-b (plist-get b key)))
@@ -1090,7 +969,7 @@ contained in UNIVERSE."
1090 (cl-etypecase universe 969 (cl-etypecase universe
1091 ((member t) (mapcar #'ert-get-test 970 ((member t) (mapcar #'ert-get-test
1092 (apropos-internal selector #'ert-test-boundp))) 971 (apropos-internal selector #'ert-test-boundp)))
1093 (list (ert--remove-if-not (lambda (test) 972 (list (cl-remove-if-not (lambda (test)
1094 (and (ert-test-name test) 973 (and (ert-test-name test)
1095 (string-match selector 974 (string-match selector
1096 (ert-test-name test)))) 975 (ert-test-name test))))
@@ -1123,13 +1002,13 @@ contained in UNIVERSE."
1123 (not 1002 (not
1124 (cl-assert (eql (length operands) 1)) 1003 (cl-assert (eql (length operands) 1))
1125 (let ((all-tests (ert-select-tests 't universe))) 1004 (let ((all-tests (ert-select-tests 't universe)))
1126 (ert--set-difference all-tests 1005 (cl-set-difference all-tests
1127 (ert-select-tests (car operands) 1006 (ert-select-tests (car operands)
1128 all-tests)))) 1007 all-tests))))
1129 (or 1008 (or
1130 (cl-case (length operands) 1009 (cl-case (length operands)
1131 (0 (ert-select-tests 'nil universe)) 1010 (0 (ert-select-tests 'nil universe))
1132 (t (ert--union (ert-select-tests (car operands) universe) 1011 (t (cl-union (ert-select-tests (car operands) universe)
1133 (ert-select-tests `(or ,@(cdr operands)) 1012 (ert-select-tests `(or ,@(cdr operands))
1134 universe))))) 1013 universe)))))
1135 (tag 1014 (tag
@@ -1141,7 +1020,7 @@ contained in UNIVERSE."
1141 universe))) 1020 universe)))
1142 (satisfies 1021 (satisfies
1143 (cl-assert (eql (length operands) 1)) 1022 (cl-assert (eql (length operands) 1))
1144 (ert--remove-if-not (car operands) 1023 (cl-remove-if-not (car operands)
1145 (ert-select-tests 't universe)))))))) 1024 (ert-select-tests 't universe))))))))
1146 1025
1147(defun ert--insert-human-readable-selector (selector) 1026(defun ert--insert-human-readable-selector (selector)
@@ -1285,7 +1164,7 @@ Also changes the counters in STATS to match."
1285 "Create a new `ert--stats' object for running TESTS. 1164 "Create a new `ert--stats' object for running TESTS.
1286 1165
1287SELECTOR is the selector that was used to select TESTS." 1166SELECTOR is the selector that was used to select TESTS."
1288 (setq tests (ert--coerce-to-vector tests)) 1167 (setq tests (cl-coerce tests 'vector))
1289 (let ((map (make-hash-table :size (length tests)))) 1168 (let ((map (make-hash-table :size (length tests))))
1290 (cl-loop for i from 0 1169 (cl-loop for i from 0
1291 for test across tests 1170 for test across tests
@@ -1548,10 +1427,10 @@ This can be used as an inverse of `add-to-list'."
1548 (unless key (setq key #'identity)) 1427 (unless key (setq key #'identity))
1549 (unless test (setq test #'equal)) 1428 (unless test (setq test #'equal))
1550 (setf (symbol-value list-var) 1429 (setf (symbol-value list-var)
1551 (ert--remove* element 1430 (cl-remove element
1552 (symbol-value list-var) 1431 (symbol-value list-var)
1553 :key key 1432 :key key
1554 :test test))) 1433 :test test)))
1555 1434
1556 1435
1557;;; Some basic interactive functions. 1436;;; Some basic interactive functions.
@@ -1810,7 +1689,7 @@ BEGIN and END specify a region in the current buffer."
1810 "Return the first line of S, or S if it contains no newlines. 1689 "Return the first line of S, or S if it contains no newlines.
1811 1690
1812The return value does not include the line terminator." 1691The return value does not include the line terminator."
1813 (substring s 0 (ert--string-position ?\n s))) 1692 (substring s 0 (cl-position ?\n s)))
1814 1693
1815(defun ert-face-for-test-result (expectedp) 1694(defun ert-face-for-test-result (expectedp)
1816 "Return a face that shows whether a test result was expected or unexpected. 1695 "Return a face that shows whether a test result was expected or unexpected.