.gitignore tweaks
[emacs.git] / test / automated / let-alist.el
blobc436d89ca1f6870e6d1d4bca35211b09aa39fdae
1 ;;; let-alist.el --- tests for file handling. -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2015 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)
24 (require 'let-alist)
26 (ert-deftest let-alist-surface-test ()
27 "Tests basic macro expansion for `let-alist'."
28 (should
29 (equal '(let ((symbol data))
30 (let ((.test-one (cdr (assq 'test-one symbol)))
31 (.test-two (cdr (assq 'test-two symbol))))
32 (list .test-one .test-two
33 .test-two .test-two)))
34 (cl-letf (((symbol-function #'make-symbol) (lambda (x) 'symbol)))
35 (macroexpand
36 '(let-alist data (list .test-one .test-two
37 .test-two .test-two))))))
38 (should
39 (equal
40 (let ((.external "ext")
41 (.external.too "et"))
42 (let-alist '((test-two . 0)
43 (test-three . 1)
44 (sublist . ((foo . 2)
45 (bar . 3))))
46 (list .test-one .test-two .test-three
47 .sublist.foo .sublist.bar
48 ..external ..external.too)))
49 (list nil 0 1 2 3 "ext" "et"))))
51 (defvar let-alist--test-counter 0
52 "Used to count number of times a function is called.")
54 (ert-deftest let-alist-evaluate-once ()
55 "Check that the alist argument is only evaluated once."
56 (let ((let-alist--test-counter 0))
57 (should
58 (equal
59 (let-alist (list
60 (cons 'test-two (cl-incf let-alist--test-counter))
61 (cons 'test-three (cl-incf let-alist--test-counter)))
62 (list .test-one .test-two .test-two .test-three .cl-incf))
63 '(nil 1 1 2 nil)))))
65 (ert-deftest let-alist-remove-dot ()
66 "Remove first dot from symbol."
67 (should (equal (let-alist--remove-dot 'hi) 'hi))
68 (should (equal (let-alist--remove-dot '.hi) 'hi))
69 (should (equal (let-alist--remove-dot '..hi) '.hi)))
71 (ert-deftest let-alist-list-to-sexp ()
72 "Check that multiple dots are handled correctly."
73 (should (= 1 (eval (let-alist--list-to-sexp '(a b c d) ''((d (c (b (a . 1)))))))))
74 (should (equal (let-alist--access-sexp '.foo.bar.baz 'var)
75 '(cdr (assq 'baz (cdr (assq 'bar (cdr (assq 'foo var))))))))
76 (should (equal (let-alist--access-sexp '..foo.bar.baz 'var) '.foo.bar.baz)))
78 ;;; let-alist.el ends here