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/>.
25 (ert-deftest test-org
/macro-replace-all
()
26 "Test `org-macro-replace-all' specifications."
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
)
35 ;; Macro with arguments.
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
)
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
)
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
)
60 ;; Error out when macro expansion is circular.
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.
70 (org-test-with-temp-text
71 (format "#+MACRO: other-macro success
72 #+SETUPFILE: \"%sexamples/macro-templates.org\"
73 {{{included-macro}}} {{{other-macro}}}"
75 (org-macro-initialize-templates)
76 (org-macro-replace-all org-macro-templates
)
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.
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)))))
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)))))
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)))))
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."
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."
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