org-agenda: Fix small bug
[org-mode/org-kjn.git] / testing / lisp / test-org-macro.el
blobd28cb7f22ca65a65ae711b9ddb3f57b83664131d
1 ;;; test-org-macro.el --- Tests for org-macro.el
3 ;; Copyright (C) 2013, 2014 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
7 ;; This program 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 ;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Code:
23 ;;; Macros
25 (ert-deftest test-org/macro-replace-all ()
26 "Test `org-macro-replace-all' specifications."
27 ;; Standard test.
28 (should
29 (equal
30 "#+MACRO: A B\n1 B 3"
31 (org-test-with-temp-text "#+MACRO: A B\n1 {{{A}}} 3"
32 (progn (org-macro-initialize-templates)
33 (org-macro-replace-all org-macro-templates)
34 (buffer-string)))))
35 ;; Macro with arguments.
36 (should
37 (equal
38 "#+MACRO: macro $1 $2\nsome text"
39 (org-test-with-temp-text "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
40 (progn (org-macro-initialize-templates)
41 (org-macro-replace-all org-macro-templates)
42 (buffer-string)))))
43 ;; Macro with "eval".
44 (should
45 (equal
46 "#+MACRO: add (eval (+ $1 $2))\n3"
47 (org-test-with-temp-text "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
48 (progn (org-macro-initialize-templates)
49 (org-macro-replace-all org-macro-templates)
50 (buffer-string)))))
51 ;; Nested macros.
52 (should
53 (equal
54 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\ninner outer"
55 (org-test-with-temp-text
56 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
57 (progn (org-macro-initialize-templates)
58 (org-macro-replace-all org-macro-templates)
59 (buffer-string)))))
60 ;; Error out when macro expansion is circular.
61 (should-error
62 (org-test-with-temp-text
63 "#+MACRO: mac1 {{{mac2}}}\n#+MACRO: mac2 {{{mac1}}}\n{{{mac1}}}"
64 (org-macro-initialize-templates)
65 (org-macro-replace-all org-macro-templates)))
66 ;; Macros in setup file.
67 (should
68 (string-match
69 "success success\\'"
70 (org-test-with-temp-text
71 (format "#+MACRO: other-macro success
72 #+SETUPFILE: \"%sexamples/macro-templates.org\"
73 {{{included-macro}}} {{{other-macro}}}"
74 org-test-dir)
75 (org-macro-initialize-templates)
76 (org-macro-replace-all org-macro-templates)
77 (buffer-string)))))
79 (ert-deftest test-org-macro/escape-arguments ()
80 "Test `org-macro-escape-arguments' specifications."
81 ;; Regular tests.
82 (should (equal "a" (org-macro-escape-arguments "a")))
83 (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
84 ;; Handle empty arguments.
85 (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
86 ;; Properly escape commas and backslashes preceding them.
87 (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
88 (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
89 (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
91 (ert-deftest test-org-macro/extract-arguments ()
92 "Test `org-macro-extract-arguments' specifications."
93 ;; Regular tests.
94 (should (equal '("a") (org-macro-extract-arguments "a")))
95 (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
96 ;; Handle empty arguments.
97 (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
98 ;; Handle escaped commas and backslashes.
99 (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
100 (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
101 (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
104 (provide 'test-org-macro)
105 ;;; test-org-macro.el ends here