]> code.delx.au - gnu-emacs/blob - test/automated/let-alist.el
* test/automated/viper-tests.el (viper-test-undo-kmacro):
[gnu-emacs] / test / automated / let-alist.el
1 ;;; let-alist.el --- tests for file handling. -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2012-2016 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 ;;; Code:
21
22 (require 'ert)
23 (require 'cl-lib)
24 (require 'let-alist)
25
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"))))
50
51 (ert-deftest let-alist-cons ()
52 (should
53 (equal
54 (let ((.external "ext")
55 (.external.too "et"))
56 (let-alist '((test-two . 0)
57 (test-three . 1)
58 (sublist . ((foo . 2)
59 (bar . 3))))
60 (list `(, .test-one . , .test-two)
61 .sublist.bar ..external)))
62 (list '(nil . 0) 3 "ext"))))
63
64 (defvar let-alist--test-counter 0
65 "Used to count number of times a function is called.")
66
67 (ert-deftest let-alist-evaluate-once ()
68 "Check that the alist argument is only evaluated once."
69 (let ((let-alist--test-counter 0))
70 (should
71 (equal
72 (let-alist (list
73 (cons 'test-two (cl-incf let-alist--test-counter))
74 (cons 'test-three (cl-incf let-alist--test-counter)))
75 (list .test-one .test-two .test-two .test-three .cl-incf))
76 '(nil 1 1 2 nil)))))
77
78 (ert-deftest let-alist-remove-dot ()
79 "Remove first dot from symbol."
80 (should (equal (let-alist--remove-dot 'hi) 'hi))
81 (should (equal (let-alist--remove-dot '.hi) 'hi))
82 (should (equal (let-alist--remove-dot '..hi) '.hi)))
83
84 (ert-deftest let-alist-list-to-sexp ()
85 "Check that multiple dots are handled correctly."
86 (should (= 1 (eval (let-alist--list-to-sexp '(a b c d) ''((d (c (b (a . 1)))))))))
87 (should (equal (let-alist--access-sexp '.foo.bar.baz 'var)
88 '(cdr (assq 'baz (cdr (assq 'bar (cdr (assq 'foo var))))))))
89 (should (equal (let-alist--access-sexp '..foo.bar.baz 'var) '.foo.bar.baz)))
90
91 ;;; let-alist.el ends here