test-org-macro.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. ;;; test-org-macro.el --- Tests for org-macro.el
  2. ;; Copyright (C) 2013, 2014, 2019 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. ;;; Macros
  16. (ert-deftest test-org/macro-replace-all ()
  17. "Test `org-macro-replace-all' specifications."
  18. ;; Standard test.
  19. (should
  20. (equal
  21. "#+MACRO: A B\n1 B 3"
  22. (org-test-with-temp-text "#+MACRO: A B\n1 {{{A}}} 3"
  23. (org-macro-initialize-templates)
  24. (org-macro-replace-all org-macro-templates)
  25. (buffer-string))))
  26. ;; Macro with arguments.
  27. (should
  28. (equal
  29. "#+MACRO: macro $1 $2\nsome text"
  30. (org-test-with-temp-text "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
  31. (progn (org-macro-initialize-templates)
  32. (org-macro-replace-all org-macro-templates)
  33. (buffer-string)))))
  34. ;; Macro with "eval".
  35. (should
  36. (equal
  37. "3"
  38. (org-test-with-temp-text
  39. "#+MACRO: add (eval (+ (string-to-number $1) (string-to-number $2)))
  40. <point>{{{add(1,2)}}}"
  41. (org-macro-initialize-templates)
  42. (org-macro-replace-all org-macro-templates)
  43. (buffer-substring-no-properties (point) (line-end-position)))))
  44. ;; Nested macros.
  45. (should
  46. (equal
  47. "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\ninner outer"
  48. (org-test-with-temp-text
  49. "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
  50. (org-macro-initialize-templates)
  51. (org-macro-replace-all org-macro-templates)
  52. (buffer-string))))
  53. ;; Error out when macro expansion is circular.
  54. (should-error
  55. (org-test-with-temp-text
  56. "#+MACRO: mac1 {{{mac2}}}\n#+MACRO: mac2 {{{mac1}}}\n{{{mac1}}}"
  57. (org-macro-initialize-templates)
  58. (org-macro-replace-all org-macro-templates)))
  59. ;; Macros in setup file.
  60. (should
  61. (string-match
  62. "success success\\'"
  63. (org-test-with-temp-text
  64. (format "#+MACRO: other-macro success
  65. #+SETUPFILE: \"%sexamples/macro-templates.org\"
  66. {{{included-macro}}} {{{other-macro}}}"
  67. org-test-dir)
  68. (org-macro-initialize-templates)
  69. (org-macro-replace-all org-macro-templates)
  70. (buffer-string))))
  71. ;; Macro expansion ignores narrowing.
  72. (should
  73. (string-match
  74. "expansion"
  75. (org-test-with-temp-text
  76. "#+MACRO: macro expansion\n{{{macro}}}\n<point>Contents"
  77. (narrow-to-region (point) (point-max))
  78. (org-macro-initialize-templates)
  79. (org-macro-replace-all org-macro-templates)
  80. (org-with-wide-buffer (buffer-string)))))
  81. ;; Macros in a commented tree are not expanded.
  82. (should
  83. (string-match-p
  84. "{{{macro}}}"
  85. (org-test-with-temp-text
  86. "#+MACRO: macro expansion\n* COMMENT H\n<point>{{{macro}}}"
  87. (org-macro-initialize-templates)
  88. (org-macro-replace-all org-macro-templates)
  89. (org-with-wide-buffer (buffer-string)))))
  90. (should
  91. (string-match-p
  92. "{{{macro}}}"
  93. (org-test-with-temp-text
  94. "#+MACRO: macro expansion\n* COMMENT H1\n** H2\n<point>{{{macro}}}"
  95. (org-macro-initialize-templates)
  96. (org-macro-replace-all org-macro-templates)
  97. (org-with-wide-buffer (buffer-string)))))
  98. ;; User-defined macros take precedence over built-in macros.
  99. (should
  100. (equal
  101. "foo"
  102. (org-test-with-temp-text
  103. "#+MACRO: title foo\n#+TITLE: bar\n<point>{{{title}}}"
  104. (org-macro-initialize-templates)
  105. (org-macro-replace-all org-macro-templates)
  106. (goto-char (point-max))
  107. (buffer-substring-no-properties (line-beginning-position)
  108. (line-end-position))))))
  109. (ert-deftest test-org-macro/property ()
  110. "Test {{{property}}} macro."
  111. ;; With only one argument, retrieve property from current headline.
  112. ;; Otherwise, the second argument is a search option to get the
  113. ;; property from another headline.
  114. (should
  115. (equal "1"
  116. (org-test-with-temp-text
  117. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
  118. (org-macro-initialize-templates)
  119. (org-macro-replace-all org-macro-templates)
  120. (buffer-substring-no-properties
  121. (line-beginning-position) (line-end-position)))))
  122. (should
  123. (equal "1"
  124. (org-test-with-temp-text
  125. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
  126. (org-macro-initialize-templates)
  127. (org-macro-replace-all org-macro-templates)
  128. (buffer-substring-no-properties
  129. (line-beginning-position) (line-end-position)))))
  130. (should
  131. (equal
  132. "1"
  133. (org-test-with-temp-text
  134. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
  135. (org-macro-initialize-templates)
  136. (org-macro-replace-all org-macro-templates)
  137. (buffer-substring-no-properties
  138. (line-beginning-position) (line-end-position)))))
  139. (should-error
  140. (org-test-with-temp-text
  141. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
  142. (org-macro-initialize-templates)
  143. (org-macro-replace-all org-macro-templates))))
  144. (ert-deftest test-org-macro/n ()
  145. "Test {{{n}}} macro."
  146. ;; Standard test with default counter.
  147. (should
  148. (equal "1 2"
  149. (org-test-with-temp-text "{{{n}}} {{{n}}}"
  150. (org-macro-initialize-templates)
  151. (org-macro-replace-all org-macro-templates)
  152. (buffer-substring-no-properties
  153. (line-beginning-position) (line-end-position)))))
  154. (should
  155. (equal "1 2"
  156. (org-test-with-temp-text "{{{n()}}} {{{n}}}"
  157. (org-macro-initialize-templates)
  158. (org-macro-replace-all org-macro-templates)
  159. (buffer-substring-no-properties
  160. (line-beginning-position) (line-end-position)))))
  161. ;; Test alternative counters.
  162. (should
  163. (equal "1 1 1 2"
  164. (org-test-with-temp-text "{{{n}}} {{{n(c1)}}} {{{n(c2)}}} {{{n(c1)}}}"
  165. (org-macro-initialize-templates)
  166. (org-macro-replace-all org-macro-templates)
  167. (buffer-substring-no-properties
  168. (line-beginning-position) (line-end-position)))))
  169. ;; Second argument set a counter to a given value. A non-numeric
  170. ;; value resets the counter to 1.
  171. (should
  172. (equal "9 10"
  173. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c)}}}"
  174. (org-macro-initialize-templates)
  175. (org-macro-replace-all org-macro-templates)
  176. (buffer-substring-no-properties
  177. (line-beginning-position) (line-end-position)))))
  178. (should
  179. (equal "9 1"
  180. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c,reset)}}}"
  181. (org-macro-initialize-templates)
  182. (org-macro-replace-all org-macro-templates)
  183. (buffer-substring-no-properties
  184. (line-beginning-position) (line-end-position)))))
  185. ;; Check that reset happens when the second argument is neither "-"
  186. ;; nor a number.
  187. (should
  188. (equal "9 1 1 1"
  189. (org-test-with-temp-text
  190. (concat "{{{n(c,9)}}} {{{n(c,reiniciar)}}}"
  191. " {{{n(c,réinitialiser)}}} {{{n(c,zurückstellen)}}}")
  192. (org-macro-initialize-templates)
  193. (org-macro-replace-all org-macro-templates)
  194. (buffer-substring-no-properties
  195. (line-beginning-position) (line-end-position)))))
  196. ;; Tolerate spaces in first argument.
  197. (should
  198. (equal "1 2 3 4"
  199. (org-test-with-temp-text "{{{n(c)}}} {{{n(c )}}} {{{n( c)}}} {{{n( c )}}}"
  200. (org-macro-initialize-templates)
  201. (org-macro-replace-all org-macro-templates)
  202. (buffer-substring-no-properties
  203. (line-beginning-position) (line-end-position)))))
  204. ;; Tolerate spaces when second argument is an integer.
  205. (should
  206. (equal "2 3 5 7"
  207. (org-test-with-temp-text
  208. (concat "{{{n(c,2)}}} {{{n(c, 3)}}}"
  209. " {{{n(c,5 )}}} {{{n(c, 7 )}}}")
  210. (org-macro-initialize-templates)
  211. (org-macro-replace-all org-macro-templates)
  212. (buffer-substring-no-properties
  213. (line-beginning-position) (line-end-position)))))
  214. ;; Tolerate spaces when second argument is the hold argument.
  215. (should
  216. (equal "7 7 8 8 9 9"
  217. (org-test-with-temp-text
  218. (concat "{{{n(,7)}}} {{{n(, -)}}}"
  219. " {{{n}}} {{{n(,- )}}} {{{n}}} {{{n(, - )}}}")
  220. (org-macro-initialize-templates)
  221. (org-macro-replace-all org-macro-templates)
  222. (buffer-substring-no-properties
  223. (line-beginning-position) (line-end-position)))))
  224. ;; Tolerate spaces when second argument is used to reset the counter.
  225. (should
  226. (equal "9 1 1 1 1"
  227. (org-test-with-temp-text
  228. (concat "{{{n(c,9)}}} {{{n(c,reset)}}} {{{n(c, reset)}}}"
  229. " {{{n(c,reset )}}} {{{n(c, reset )}}}")
  230. (org-macro-initialize-templates)
  231. (org-macro-replace-all org-macro-templates)
  232. (buffer-substring-no-properties
  233. (line-beginning-position) (line-end-position)))))
  234. ;; Second argument also applies to default counter.
  235. (should
  236. (equal "9 10 1"
  237. (org-test-with-temp-text "{{{n(,9)}}} {{{n}}} {{{n(,reset)}}}"
  238. (org-macro-initialize-templates)
  239. (org-macro-replace-all org-macro-templates)
  240. (buffer-substring-no-properties
  241. (line-beginning-position) (line-end-position)))))
  242. ;; An empty second argument is equivalent to no argument.
  243. (should
  244. (equal "2 3"
  245. (org-test-with-temp-text "{{{n(c,2)}}} {{{n(c,)}}}"
  246. (org-macro-initialize-templates)
  247. (org-macro-replace-all org-macro-templates)
  248. (buffer-substring-no-properties
  249. (line-beginning-position) (line-end-position)))))
  250. ;; Hold value at reset value of 1 if the counter hasn't yet started.
  251. (should
  252. (equal "1"
  253. (org-test-with-temp-text "{{{n(,-)}}}"
  254. (org-macro-initialize-templates)
  255. (org-macro-replace-all org-macro-templates)
  256. (buffer-substring-no-properties
  257. (line-beginning-position) (line-end-position)))))
  258. ;; Increment counter following a hold.
  259. (should
  260. (equal "1 1 2"
  261. (org-test-with-temp-text "{{{n}}} {{{n(,-)}}} {{{n}}}"
  262. (org-macro-initialize-templates)
  263. (org-macro-replace-all org-macro-templates)
  264. (buffer-substring-no-properties
  265. (line-beginning-position) (line-end-position)))))
  266. ;; Hold counter value following a counter value set.
  267. (should
  268. (equal "1 10 10"
  269. (org-test-with-temp-text "{{{n}}} {{{n(,10)}}} {{{n(,-)}}}"
  270. (org-macro-initialize-templates)
  271. (org-macro-replace-all org-macro-templates)
  272. (buffer-substring-no-properties
  273. (line-beginning-position) (line-end-position)))))
  274. ;; Hold counter value in a multiple-counter situation.
  275. (should
  276. (equal "1.1 1.2 1.3"
  277. (org-test-with-temp-text
  278. "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
  279. (org-macro-initialize-templates)
  280. (org-macro-replace-all org-macro-templates)
  281. (buffer-substring-no-properties
  282. (line-beginning-position) (line-end-position)))))
  283. ;; Hold counter values on one or multiple counters at the same time.
  284. (should
  285. (equal "1.1 1.2 2.2 2.2"
  286. (org-test-with-temp-text
  287. (concat "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
  288. " {{{n}}}.{{{n(c,-)}}} {{{n(,-)}}}.{{{n(c,-)}}}")
  289. (org-macro-initialize-templates)
  290. (org-macro-replace-all org-macro-templates)
  291. (buffer-substring-no-properties
  292. (line-beginning-position) (line-end-position))))))
  293. (ert-deftest test-org-macro/keyword ()
  294. "Test {{{keyword}}} macro."
  295. ;; Replace macro with keyword's value.
  296. (should
  297. (equal
  298. "value"
  299. (org-test-with-temp-text
  300. "#+keyword: value\n<point>{{{keyword(KEYWORD)}}}"
  301. (org-macro-initialize-templates)
  302. (org-macro-replace-all org-macro-templates)
  303. (buffer-substring-no-properties
  304. (line-beginning-position) (point-max)))))
  305. ;; Replace macro with keyword's value.
  306. (should
  307. (equal
  308. "value value2"
  309. (org-test-with-temp-text
  310. "#+keyword: value\n#+keyword: value2\n<point>{{{keyword(KEYWORD)}}}"
  311. (org-macro-initialize-templates)
  312. (org-macro-replace-all org-macro-templates)
  313. (buffer-substring-no-properties
  314. (line-beginning-position) (point-max))))))
  315. (ert-deftest test-org-macro/escape-arguments ()
  316. "Test `org-macro-escape-arguments' specifications."
  317. ;; Regular tests.
  318. (should (equal "a" (org-macro-escape-arguments "a")))
  319. (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
  320. ;; Handle empty arguments.
  321. (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
  322. ;; Properly escape commas and backslashes preceding them.
  323. (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
  324. (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
  325. (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
  326. (ert-deftest test-org-macro/extract-arguments ()
  327. "Test `org-macro-extract-arguments' specifications."
  328. ;; Regular tests.
  329. (should (equal '("a") (org-macro-extract-arguments "a")))
  330. (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
  331. ;; Handle empty arguments.
  332. (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
  333. ;; Handle escaped commas and backslashes.
  334. (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
  335. (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
  336. (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
  337. (provide 'test-org-macro)
  338. ;;; test-org-macro.el ends here