* let-alist.el (let-alist): Enable access to deeper alists
[emacs.git] / test / automated / let-alist.el
blob391ccb44a8d5adc23ca85c76cabd7b1f22dd82ee
1 ;;; let-alist.el --- tests for file handling. -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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.
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.
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/>.
20 ;;; Code:
22 (require 'ert)
23 (require 'cl-lib)
25 (ert-deftest let-alist-surface-test ()
26 "Tests basic macro expansion for `let-alist'."
27 (should
28 (equal '(let ((symbol data))
29 (let ((.test-one (cdr (assq 'test-one symbol)))
30 (.test-two (cdr (assq 'test-two symbol))))
31 (list .test-one .test-two
32 .test-two .test-two)))
33 (cl-letf (((symbol-function #'gensym) (lambda (x) 'symbol)))
34 (macroexpand
35 '(let-alist data (list .test-one .test-two
36 .test-two .test-two))))))
37 (should
38 (equal
39 (let ((.external "ext")
40 (.external.too "et"))
41 (let-alist '((test-two . 0)
42 (test-three . 1)
43 (sublist . ((foo . 2)
44 (bar . 3))))
45 (list .test-one .test-two .test-three
46 .sublist.foo .sublist.bar
47 ..external ..external.too)))
48 (list nil 0 1 2 3 "ext" "et"))))
50 (defvar let-alist--test-counter 0
51 "Used to count number of times a function is called.")
53 (ert-deftest let-alist-evaluate-once ()
54 "Check that the alist argument is only evaluated once."
55 (let ((let-alist--test-counter 0))
56 (should
57 (equal
58 (let-alist (list
59 (cons 'test-two (cl-incf let-alist--test-counter))
60 (cons 'test-three (cl-incf let-alist--test-counter)))
61 (list .test-one .test-two .test-two .test-three .cl-incf))
62 '(nil 1 1 2 nil)))))
64 (ert-deftest let-alist-remove-dot ()
65 "Remove firt dot from symbol."
66 (should (equal (let-alist--remove-dot 'hi) 'hi))
67 (should (equal (let-alist--remove-dot '.hi) 'hi))
68 (should (equal (let-alist--remove-dot '..hi) '.hi)))
70 (ert-deftest let-alist-list-to-sexp ()
71 "Check that multiple dots are handled correctly."
72 (should (= 1 (eval (let-alist--list-to-sexp '(a b c d) ''((d (c (b (a . 1)))))))))
73 (should (equal (let-alist--access-sexp '.foo.bar.baz 'var)
74 '(cdr (assq 'baz (cdr (assq 'bar (cdr (assq 'foo var))))))))
75 (should (equal (let-alist--access-sexp '..foo.bar.baz 'var) '.foo.bar.baz)))
77 ;;; let-alist.el ends here