ox: Simplify fuzzy link matching
[org-mode/org-tableheadings.git] / testing / lisp / test-org-macro.el
blob4934d93c1813c48a9e56a6a713a71d92304fa3b9
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))))
78 ;; Test special "property" macro. With only one argument, retrieve
79 ;; property from current headline. Otherwise, the second argument
80 ;; is a search option to get the property from another headline.
81 (should
82 (equal "1"
83 (org-test-with-temp-text
84 "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
85 (org-macro-initialize-templates)
86 (org-macro-replace-all org-macro-templates)
87 (buffer-substring-no-properties
88 (line-beginning-position) (line-end-position)))))
89 (should
90 (equal "1"
91 (org-test-with-temp-text
92 "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
93 (org-macro-initialize-templates)
94 (org-macro-replace-all org-macro-templates)
95 (buffer-substring-no-properties
96 (line-beginning-position) (line-end-position)))))
97 (should
98 (equal
99 "1"
100 (org-test-with-temp-text
101 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
102 (org-macro-initialize-templates)
103 (org-macro-replace-all org-macro-templates)
104 (buffer-substring-no-properties
105 (line-beginning-position) (line-end-position)))))
106 (should-error
107 (org-test-with-temp-text
108 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
109 (org-macro-initialize-templates)
110 (org-macro-replace-all org-macro-templates))))
112 (ert-deftest test-org-macro/escape-arguments ()
113 "Test `org-macro-escape-arguments' specifications."
114 ;; Regular tests.
115 (should (equal "a" (org-macro-escape-arguments "a")))
116 (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
117 ;; Handle empty arguments.
118 (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
119 ;; Properly escape commas and backslashes preceding them.
120 (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
121 (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
122 (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
124 (ert-deftest test-org-macro/extract-arguments ()
125 "Test `org-macro-extract-arguments' specifications."
126 ;; Regular tests.
127 (should (equal '("a") (org-macro-extract-arguments "a")))
128 (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
129 ;; Handle empty arguments.
130 (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
131 ;; Handle escaped commas and backslashes.
132 (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
133 (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
134 (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
137 (provide 'test-org-macro)
138 ;;; test-org-macro.el ends here