diff options
| author | Stefan Monnier | 2014-09-22 14:22:02 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2014-09-22 14:22:02 -0400 |
| commit | 6b33c17c85473d49a01f66f5ce2382d183ba079a (patch) | |
| tree | f048782a2cf4917698bc56c6c4d021a694537b53 /test | |
| parent | f11af8a48cfef05314e6e5d86e18861cffbde9f1 (diff) | |
| parent | f8b25a5169905206935ebf49a9e99a7536106e46 (diff) | |
| download | emacs-6b33c17c85473d49a01f66f5ce2382d183ba079a.tar.gz emacs-6b33c17c85473d49a01f66f5ce2382d183ba079a.zip | |
Add pcase-defmacro, as well as `quote' and `app' patterns.
* loadup.el: Increase max-lisp-eval-depth when macroexpanding macroexp.
* emacs-lisp/pcase.el: Allow (F . ARGS) in `app' patterns.
(pcase--funcall, pcase--eval): New functions.
(pcase--u1): Use them for guard, pred, let, and app.
(\`): Use the new feature to generate better code for vector patterns.
* emacs-lisp/pcase.el: Use pcase-defmacro to handle backquote.
(pcase--upat): Remove.
(pcase--macroexpand): Don't hardcode handling of `.
(pcase--split-consp, pcase--split-vector): Remove.
(pcase--split-equal): Disregard ` since it's expanded away.
(pcase--split-member): Optimize for quote rather than for `.
(pcase--split-pred): Optimize for quote rather than for `.
(pcase--u1): Remove handling of ` (and of `or' and `and').
Quote non-selfquoting values when passing them to `eq'.
Drop `app's let-binding if the variable is not used.
(pcase--q1): Remove.
(`): Define as a pattern macro.
* emacs-lisp/pcase.el (pcase--match): New smart-constructor function.
(pcase--expand pcase--q1, pcase--app-subst-match): Use it.
(pcase--macroexpand): Handle self-quoting patterns here, expand them to
quote patterns.
(pcase--split-match): Don't hoist or/and here any more.
(pcase--split-equal): Optimize quote patterns as well as ` patterns.
(pcase--flip): New helper macro.
(pcase--u1): Optimize the memq case directly.
Don't handle neither self-quoting nor and/or patterns any more.
* emacs-lisp/pcase.el (pcase-defmacro): New macro.
(pcase--macroexpand): New function.
(pcase--expand): Use it.
* emacs-lisp/pcase.el (pcase--app-subst-match, pcase--app-subst-rest):
New optimization functions.
(pcase--u1): Add support for `quote' and `app'.
(pcase): Document them in the docstring.
Diffstat (limited to 'test')
| -rw-r--r-- | test/automated/pcase-tests.el | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/test/automated/pcase-tests.el b/test/automated/pcase-tests.el new file mode 100644 index 00000000000..ec0c3bc7fd5 --- /dev/null +++ b/test/automated/pcase-tests.el | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | ;;; pcase-tests.el --- Test suite for pcase macro. | ||
| 2 | |||
| 3 | ;; Copyright (C) 2012-2014 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; This file is part of GNU Emacs. | ||
| 6 | |||
| 7 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 10 | ;; (at your option) any later version. | ||
| 11 | |||
| 12 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Commentary: | ||
| 21 | |||
| 22 | ;;; Code: | ||
| 23 | |||
| 24 | (require 'ert) | ||
| 25 | (require 'cl-lib) | ||
| 26 | |||
| 27 | (ert-deftest pcase-tests-base () | ||
| 28 | "Test pcase code." | ||
| 29 | (should (equal (pcase '(1 . 2) ((app car '2) 6) ((app car '1) 5)) 5))) | ||
| 30 | |||
| 31 | (pcase-defmacro pcase-tests-plus (pat n) | ||
| 32 | `(app (lambda (v) (- v ,n)) ,pat)) | ||
| 33 | |||
| 34 | (ert-deftest pcase-tests-macro () | ||
| 35 | (should (equal (pcase 5 ((pcase-tests-plus x 3) x)) 2))) | ||
| 36 | |||
| 37 | (defun pcase-tests-grep (fname exp) | ||
| 38 | (when (consp exp) | ||
| 39 | (or (eq fname (car exp)) | ||
| 40 | (cl-some (lambda (exp) (pcase-tests-grep fname exp)) (cdr exp))))) | ||
| 41 | |||
| 42 | (ert-deftest pcase-tests-tests () | ||
| 43 | (should (pcase-tests-grep 'memq '(or (+ 2 3) (memq x y)))) | ||
| 44 | (should-not (pcase-tests-grep 'memq '(or (+ 2 3) (- x y))))) | ||
| 45 | |||
| 46 | (ert-deftest pcase-tests-member () | ||
| 47 | (should (pcase-tests-grep | ||
| 48 | 'memq (macroexpand-all '(pcase x ((or 1 2 3) body))))) | ||
| 49 | (should (pcase-tests-grep | ||
| 50 | 'member (macroexpand-all '(pcase x ((or '"a" '2 '3) body))))) | ||
| 51 | (should-not (pcase-tests-grep | ||
| 52 | 'memq (macroexpand-all '(pcase x ((or "a" 2 3) body))))) | ||
| 53 | (let ((exp (macroexpand-all | ||
| 54 | '(pcase x | ||
| 55 | ("a" body1) | ||
| 56 | (2 body2) | ||
| 57 | ((or "a" 2 3) body))))) | ||
| 58 | (should-not (pcase-tests-grep 'memq exp)) | ||
| 59 | (should-not (pcase-tests-grep 'member exp)))) | ||
| 60 | |||
| 61 | (ert-deftest pcase-tests-vectors () | ||
| 62 | (should (equal (pcase [1 2] (`[,x] 1) (`[,x ,y] (+ x y))) 3))) | ||
| 63 | |||
| 64 | ;; Local Variables: | ||
| 65 | ;; no-byte-compile: t | ||
| 66 | ;; End: | ||
| 67 | |||
| 68 | ;;; pcase-tests.el ends here. | ||