Commit a19b3c2d authored by Glenn Morris's avatar Glenn Morris
Browse files

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.
parent 17bd3d04
2013-07-11 Glenn Morris <rgm@gnu.org>
* 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.
2013-07-11 Michael Albinus <michael.albinus@gmx.de>
* net/tramp.el (tramp-methods): Extend docstring.
......
......@@ -54,7 +54,7 @@
;;; Code:
(eval-when-compile (require 'cl-lib))
(require 'cl-lib)
(require 'button)
(require 'debug)
(require 'easymenu)
......@@ -87,127 +87,6 @@
;;; Copies/reimplementations of cl functions.
(defun ert--cl-do-remf (plist tag)
"Copy of `cl-do-remf'. Modify PLIST by removing TAG."
(let ((p (cdr plist)))
(while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
(and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
(defun ert--remprop (sym tag)
"Copy of `cl-remprop'. Modify SYM's plist by removing TAG."
(let ((plist (symbol-plist sym)))
(if (and plist (eq tag (car plist)))
(progn (setplist sym (cdr (cdr plist))) t)
(ert--cl-do-remf plist tag))))
(defun ert--remove-if-not (ert-pred ert-list)
"A reimplementation of `remove-if-not'.
ERT-PRED is a predicate, ERT-LIST is the input list."
(cl-loop for ert-x in ert-list
if (funcall ert-pred ert-x)
collect ert-x))
(defun ert--intersection (a b)
"A reimplementation of `intersection'. Intersect the sets A and B.
Elements are compared using `eql'."
(cl-loop for x in a
if (memql x b)
collect x))
(defun ert--set-difference (a b)
"A reimplementation of `set-difference'. Subtract the set B from the set A.
Elements are compared using `eql'."
(cl-loop for x in a
unless (memql x b)
collect x))
(defun ert--set-difference-eq (a b)
"A reimplementation of `set-difference'. Subtract the set B from the set A.
Elements are compared using `eq'."
(cl-loop for x in a
unless (memq x b)
collect x))
(defun ert--union (a b)
"A reimplementation of `union'. Compute the union of the sets A and B.
Elements are compared using `eql'."
(append a (ert--set-difference b a)))
(eval-and-compile
(defvar ert--gensym-counter 0))
(eval-and-compile
(defun ert--gensym (&optional prefix)
"Only allows string PREFIX, not compatible with CL."
(unless prefix (setq prefix "G"))
(make-symbol (format "%s%s"
prefix
(prog1 ert--gensym-counter
(cl-incf ert--gensym-counter))))))
(defun ert--coerce-to-vector (x)
"Coerce X to a vector."
(when (char-table-p x) (error "Not supported"))
(if (vectorp x)
x
(vconcat x)))
(cl-defun ert--remove* (x list &key key test)
"Does not support all the keywords of remove*."
(unless key (setq key #'identity))
(unless test (setq test #'eql))
(cl-loop for y in list
unless (funcall test x (funcall key y))
collect y))
(defun ert--string-position (c s)
"Return the position of the first occurrence of C in S, or nil if none."
(cl-loop for i from 0
for x across s
when (eql x c) return i))
(defun ert--mismatch (a b)
"Return index of first element that differs between A and B.
Like `mismatch'. Uses `equal' for comparison."
(cond ((or (listp a) (listp b))
(ert--mismatch (ert--coerce-to-vector a)
(ert--coerce-to-vector b)))
((> (length a) (length b))
(ert--mismatch b a))
(t
(let ((la (length a))
(lb (length b)))
(cl-assert (arrayp a) t)
(cl-assert (arrayp b) t)
(cl-assert (<= la lb) t)
(cl-loop for i below la
when (not (equal (aref a i) (aref b i))) return i
finally (cl-return (if (/= la lb)
la
(cl-assert (equal a b) t)
nil)))))))
(defun ert--subseq (seq start &optional end)
"Return a subsequence of SEQ from START to END."
(when (char-table-p seq) (error "Not supported"))
(let ((vector (substring (ert--coerce-to-vector seq) start end)))
(cl-etypecase seq
(vector vector)
(string (concat vector))
(list (append vector nil))
(bool-vector (cl-loop with result
= (make-bool-vector (length vector) nil)
for i below (length vector) do
(setf (aref result i) (aref vector i))
finally (cl-return result)))
(char-table (cl-assert nil)))))
(defun ert-equal-including-properties (a b)
"Return t if A and B have similar structure and contents.
......@@ -258,7 +137,7 @@ Emacs bug 6581 at URL `http://debbugs.gnu.org/cgi/bugreport.cgi?bug=6581'."
(defun ert-make-test-unbound (symbol)
"Make SYMBOL name no test. Return SYMBOL."
(ert--remprop symbol 'ert--test)
(cl-remprop symbol 'ert--test)
symbol)
(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."
cl-macro-environment)))))
(cond
((or (atom form) (ert--special-operator-p (car form)))
(let ((value (ert--gensym "value-")))
`(let ((,value (ert--gensym "ert-form-evaluation-aborted-")))
(let ((value (cl-gensym "value-")))
`(let ((,value (cl-gensym "ert-form-evaluation-aborted-")))
,(funcall inner-expander
`(setq ,value ,form)
`(list ',whole :form ',form :value ,value)
......@@ -410,10 +289,10 @@ DATA is displayed to the user and should state the reason of the failure."
(and (consp fn-name)
(eql (car fn-name) 'lambda)
(listp (cdr fn-name)))))
(let ((fn (ert--gensym "fn-"))
(args (ert--gensym "args-"))
(value (ert--gensym "value-"))
(default-value (ert--gensym "ert-form-evaluation-aborted-")))
(let ((fn (cl-gensym "fn-"))
(args (cl-gensym "args-"))
(value (cl-gensym "value-"))
(default-value (cl-gensym "ert-form-evaluation-aborted-")))
`(let ((,fn (function ,fn-name))
(,args (list ,@arg-forms)))
(let ((,value ',default-value))
......@@ -450,7 +329,7 @@ FORM-DESCRIPTION-FORM before it has called INNER-FORM."
(ert--expand-should-1
whole form
(lambda (inner-form form-description-form value-var)
(let ((form-description (ert--gensym "form-description-")))
(let ((form-description (cl-gensym "form-description-")))
`(let (,form-description)
,(funcall inner-expander
`(unwind-protect
......@@ -491,7 +370,7 @@ and aborts the current test as failed if it doesn't."
(list type)
(symbol (list type)))))
(cl-assert signaled-conditions)
(unless (ert--intersection signaled-conditions handled-conditions)
(unless (cl-intersection signaled-conditions handled-conditions)
(ert-fail (append
(funcall form-description-fn)
(list
......@@ -528,8 +407,8 @@ failed."
`(should-error ,form ,@keys)
form
(lambda (inner-form form-description-form value-var)
(let ((errorp (ert--gensym "errorp"))
(form-description-fn (ert--gensym "form-description-fn-")))
(let ((errorp (cl-gensym "errorp"))
(form-description-fn (cl-gensym "form-description-fn-")))
`(let ((,errorp nil)
(,form-description-fn (lambda () ,form-description-form)))
(condition-case -condition-
......@@ -591,7 +470,7 @@ Returns nil if they are."
`(proper-lists-of-different-length ,(length a) ,(length b)
,a ,b
first-mismatch-at
,(ert--mismatch a b))
,(cl-mismatch a b :test 'equal))
(cl-loop for i from 0
for ai in a
for bi in b
......@@ -611,7 +490,7 @@ Returns nil if they are."
,a ,b
,@(unless (char-table-p a)
`(first-mismatch-at
,(ert--mismatch a b))))
,(cl-mismatch a b :test 'equal))))
(cl-loop for i from 0
for ai across a
for bi across b
......@@ -656,8 +535,8 @@ key/value pairs in each list does not matter."
;; work, so let's punt on it for now.
(let* ((keys-a (ert--significant-plist-keys a))
(keys-b (ert--significant-plist-keys b))
(keys-in-a-not-in-b (ert--set-difference-eq keys-a keys-b))
(keys-in-b-not-in-a (ert--set-difference-eq keys-b keys-a)))
(keys-in-a-not-in-b (cl-set-difference keys-a keys-b :test 'eq))
(keys-in-b-not-in-a (cl-set-difference keys-b keys-a :test 'eq)))
(cl-flet ((explain-with-key (key)
(let ((value-a (plist-get a key))
(value-b (plist-get b key)))
......@@ -1090,7 +969,7 @@ contained in UNIVERSE."
(cl-etypecase universe
((member t) (mapcar #'ert-get-test
(apropos-internal selector #'ert-test-boundp)))
(list (ert--remove-if-not (lambda (test)
(list (cl-remove-if-not (lambda (test)
(and (ert-test-name test)
(string-match selector
(ert-test-name test))))
......@@ -1123,13 +1002,13 @@ contained in UNIVERSE."
(not
(cl-assert (eql (length operands) 1))
(let ((all-tests (ert-select-tests 't universe)))
(ert--set-difference all-tests
(cl-set-difference all-tests
(ert-select-tests (car operands)
all-tests))))
(or
(cl-case (length operands)
(0 (ert-select-tests 'nil universe))
(t (ert--union (ert-select-tests (car operands) universe)
(t (cl-union (ert-select-tests (car operands) universe)
(ert-select-tests `(or ,@(cdr operands))
universe)))))
(tag
......@@ -1141,7 +1020,7 @@ contained in UNIVERSE."
universe)))
(satisfies
(cl-assert (eql (length operands) 1))
(ert--remove-if-not (car operands)
(cl-remove-if-not (car operands)
(ert-select-tests 't universe))))))))
(defun ert--insert-human-readable-selector (selector)
......@@ -1285,7 +1164,7 @@ Also changes the counters in STATS to match."
"Create a new `ert--stats' object for running TESTS.
SELECTOR is the selector that was used to select TESTS."
(setq tests (ert--coerce-to-vector tests))
(setq tests (cl-coerce tests 'vector))
(let ((map (make-hash-table :size (length tests))))
(cl-loop for i from 0
for test across tests
......@@ -1548,10 +1427,10 @@ This can be used as an inverse of `add-to-list'."
(unless key (setq key #'identity))
(unless test (setq test #'equal))
(setf (symbol-value list-var)
(ert--remove* element
(symbol-value list-var)
:key key
:test test)))
(cl-remove element
(symbol-value list-var)
:key key
:test test)))
;;; Some basic interactive functions.
......@@ -1810,7 +1689,7 @@ BEGIN and END specify a region in the current buffer."
"Return the first line of S, or S if it contains no newlines.
The return value does not include the line terminator."
(substring s 0 (ert--string-position ?\n s)))
(substring s 0 (cl-position ?\n s)))
(defun ert-face-for-test-result (expectedp)
"Return a face that shows whether a test result was expected or unexpected.
......
2013-07-11 Glenn Morris <rgm@gnu.org>
* 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.
* automated/cl-lib.el: New, split from ert-tests.el.
* automated/ruby-mode-tests.el (ruby-deftest-move-to-block):
Goto point-min.
(works-on-do, zero-is-noop, ok-with-three, ok-with-minus-two)
......
;;; cl-lib.el --- tests for emacs-lisp/cl-lib.el
;; Copyright (C) 2013 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see `http://www.gnu.org/licenses/'.
;;; Commentary:
;; Extracted from ert-tests.el, back when ert used to reimplement some
;; cl functions.
;;; Code:
(require 'cl-lib)
(require 'ert)
(ert-deftest cl-lib-test-remprop ()
(let ((x (cl-gensym)))
(should (equal (symbol-plist x) '()))
;; Remove nonexistent property on empty plist.
(cl-remprop x 'b)
(should (equal (symbol-plist x) '()))
(put x 'a 1)
(should (equal (symbol-plist x) '(a 1)))
;; Remove nonexistent property on nonempty plist.
(cl-remprop x 'b)
(should (equal (symbol-plist x) '(a 1)))
(put x 'b 2)
(put x 'c 3)
(put x 'd 4)
(should (equal (symbol-plist x) '(a 1 b 2 c 3 d 4)))
;; Remove property that is neither first nor last.
(cl-remprop x 'c)
(should (equal (symbol-plist x) '(a 1 b 2 d 4)))
;; Remove last property from a plist of length >1.
(cl-remprop x 'd)
(should (equal (symbol-plist x) '(a 1 b 2)))
;; Remove first property from a plist of length >1.
(cl-remprop x 'a)
(should (equal (symbol-plist x) '(b 2)))
;; Remove property when there is only one.
(cl-remprop x 'b)
(should (equal (symbol-plist x) '()))))
(ert-deftest cl-lib-test-remove-if-not ()
(let ((list (list 'a 'b 'c 'd))
(i 0))
(let ((result (cl-remove-if-not (lambda (x)
(should (eql x (nth i list)))
(cl-incf i)
(member i '(2 3)))
list)))
(should (equal i 4))
(should (equal result '(b c)))
(should (equal list '(a b c d)))))
(should (equal '()
(cl-remove-if-not (lambda (_x) (should nil)) '()))))
(ert-deftest cl-lib-test-remove ()
(let ((list (list 'a 'b 'c 'd))
(key-index 0)
(test-index 0))
(let ((result
(cl-remove 'foo list
:key (lambda (x)
(should (eql x (nth key-index list)))
(prog1
(list key-index x)
(cl-incf key-index)))
:test
(lambda (a b)
(should (eql a 'foo))
(should (equal b (list test-index
(nth test-index list))))
(cl-incf test-index)
(member test-index '(2 3))))))
(should (equal key-index 4))
(should (equal test-index 4))
(should (equal result '(a d)))
(should (equal list '(a b c d)))))
(let ((x (cons nil nil))
(y (cons nil nil)))
(should (equal (cl-remove x (list x y))
;; or (list x), since we use `equal' -- the
;; important thing is that only one element got
;; removed, this proves that the default test is
;; `eql', not `equal'
(list y)))))
(ert-deftest cl-lib-test-set-functions ()
(let ((c1 (cons nil nil))
(c2 (cons nil nil))
(sym (make-symbol "a")))
(let ((e '())
(a (list 'a 'b sym nil "" "x" c1 c2))
(b (list c1 'y 'b sym 'x)))
(should (equal (cl-set-difference e e) e))
(should (equal (cl-set-difference a e) a))
(should (equal (cl-set-difference e a) e))
(should (equal (cl-set-difference a a) e))
(should (equal (cl-set-difference b e) b))
(should (equal (cl-set-difference e b) e))
(should (equal (cl-set-difference b b) e))
;; Note: this test (and others) is sensitive to the order of the
;; result, which is not documented.
(should (equal (cl-set-difference a b) (list c2 "x" "" nil 'a)))
(should (equal (cl-set-difference b a) (list 'x 'y)))
;; We aren't testing whether this is really using `eq' rather than `eql'.
(should (equal (cl-set-difference e e :test 'eq) e))
(should (equal (cl-set-difference a e :test 'eq) a))
(should (equal (cl-set-difference e a :test 'eq) e))
(should (equal (cl-set-difference a a :test 'eq) e))
(should (equal (cl-set-difference b e :test 'eq) b))
(should (equal (cl-set-difference e b :test 'eq) e))
(should (equal (cl-set-difference b b :test 'eq) e))
(should (equal (cl-set-difference a b :test 'eq) (list c2 "x" "" nil 'a)))
(should (equal (cl-set-difference b a :test 'eq) (list 'x 'y)))
(should (equal (cl-union e e) e))
(should (equal (cl-union a e) a))
(should (equal (cl-union e a) a))
(should (equal (cl-union a a) a))
(should (equal (cl-union b e) b))
(should (equal (cl-union e b) b))
(should (equal (cl-union b b) b))
(should (equal (cl-union a b) (list 'x 'y 'a 'b sym nil "" "x" c1 c2)))
(should (equal (cl-union b a) (list 'x 'y 'a 'b sym nil "" "x" c1 c2)))
(should (equal (cl-intersection e e) e))
(should (equal (cl-intersection a e) e))
(should (equal (cl-intersection e a) e))
(should (equal (cl-intersection a a) a))
(should (equal (cl-intersection b e) e))
(should (equal (cl-intersection e b) e))
(should (equal (cl-intersection b b) b))
(should (equal (cl-intersection a b) (list sym 'b c1)))
(should (equal (cl-intersection b a) (list sym 'b c1))))))
(ert-deftest cl-lib-test-gensym ()
;; Since the expansion of `should' calls `cl-gensym' and thus has a
;; side-effect on `cl--gensym-counter', we have to make sure all
;; macros in our test body are expanded before we rebind
;; `cl--gensym-counter' and run the body. Otherwise, the test would
;; fail if run interpreted.
(let ((body (byte-compile
'(lambda ()
(should (equal (symbol-name (cl-gensym)) "G0"))
(should (equal (symbol-name (cl-gensym)) "G1"))
(should (equal (symbol-name (cl-gensym)) "G2"))
(should (equal (symbol-name (cl-gensym "foo")) "foo3"))
(should (equal (symbol-name (cl-gensym "bar")) "bar4"))
(should (equal cl--gensym-counter 5))))))
(let ((cl--gensym-counter 0))
(funcall body))))
(ert-deftest cl-lib-test-coerce-to-vector ()
(let* ((a (vector))
(b (vector 1 a 3))
(c (list))
(d (list b a)))
(should (eql (cl-coerce a 'vector) a))
(should (eql (cl-coerce b 'vector) b))
(should (equal (cl-coerce c 'vector) (vector)))
(should (equal (cl-coerce d 'vector) (vector b a)))))
(ert-deftest cl-lib-test-string-position ()
(should (eql (cl-position ?x "") nil))
(should (eql (cl-position ?a "abc") 0))
(should (eql (cl-position ?b "abc") 1))
(should (eql (cl-position ?c "abc") 2))
(should (eql (cl-position ?d "abc") nil))
(should (eql (cl-position ?A "abc") nil)))
(ert-deftest cl-lib-test-mismatch ()
(should (eql (cl-mismatch "" "") nil))
(should (eql (cl-mismatch "" "a") 0))
(should (eql (cl-mismatch "a" "a") nil))
(should (eql (cl-mismatch "ab" "a") 1))
(should (eql (cl-mismatch "Aa" "aA") 0))
(should (eql (cl-mismatch '(a b c) '(a b d)) 2)))
;;; cl-lib.el ends here
......@@ -26,11 +26,9 @@
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'cl-lib)
(require 'ert)
;;; Self-test that doesn't rely on ERT, for bootstrapping.
;; This is used to test that bodies actually run.
......@@ -578,7 +576,7 @@ This macro is used to test if macroexpansion in `should' works."
(should (ert--special-operator-p 'if))
(should-not (ert--special-operator-p 'car))
(should-not (ert--special-operator-p 'ert--special-operator-p))
(let ((b (ert--gensym)))
(let ((b (cl-gensym)))
(should-not (ert--special-operator-p b))
(fset b 'if)
(should (ert--special-operator-p b))))
......@@ -626,171 +624,6 @@ This macro is used to test if macroexpansion in `should' works."
:explanation nil)
))))))
(ert-deftest ert-test-remprop ()
(let ((x (ert--gensym)))
(should (equal (symbol-plist x) '()))
;; Remove nonexistent property on empty plist.
(ert--remprop x 'b)
(should (equal (symbol-plist x) '()))
(put x 'a 1)
(should (equal (symbol-plist x) '(a 1)))
;; Remove nonexistent property on nonempty plist.
(ert--remprop x 'b)
(should (equal (symbol-plist x) '(a 1)))
(put x 'b 2)
(put x 'c 3)
(put x 'd 4)
(should (equal (symbol-plist x) '(a 1 b 2 c 3 d 4)))
;; Remove property that is neither first nor last.
(ert--remprop x 'c)
(should (equal (symbol-plist x) '(a 1 b 2 d 4)))
;; Remove last property from a plist of length >1.
(ert--remprop x 'd)
(should (equal (symbol-plist x) '(a 1 b 2)))
;; Remove first property from a plist of length >1.
(ert--remprop x 'a)
(should (equal (symbol-plist x) '(b 2)))
;; Remove property when there is only one.
(ert--remprop x 'b)
(should (equal (symbol-plist x) '()))))
(ert-deftest ert-test-remove-if-not ()
(let ((list (list 'a 'b 'c 'd))
(i 0))
(let ((result (ert--remove-if-not (lambda (x)
(should (eql x (nth i list)))
(cl-incf i)
(member i '(2 3)))
list)))
(should (equal i 4))
(should (equal result '(b c)))
(should (equal list '(a b c d)))))
(should (equal '()
(ert--remove-if-not (lambda (_x) (should nil)) '()))))
(ert-deftest ert-test-remove* ()
(let ((list (list 'a 'b 'c 'd))
(key-index 0)
(test-index 0))
(let ((result
(ert--remove* 'foo list
:key (lambda (x)
(should (eql x (nth key-index list)))
(prog1
(list key-index x)
(cl-incf key-index)))
:test
(lambda (a b)
(should (eql a 'foo))
(should (equal b (list test-index
(nth test-index list))))
(cl-incf test-index)
(member test-index '(2 3))))))
(should (equal key-index 4))
(should (equal test-index 4))
(should (equal result '(a d)))
(should (equal list '(a b c d)))))
(let ((x (cons nil nil))
(y (cons nil nil)))