test-org-macro.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. (ert-deftest test-org-macro/property ()
  99. "Test {{{property}}} macro."
  100. ;; With only one argument, retrieve property from current headline.
  101. ;; Otherwise, the second argument is a search option to get the
  102. ;; property from another headline.
  103. (should
  104. (equal "1"
  105. (org-test-with-temp-text
  106. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
  107. (org-macro-initialize-templates)
  108. (org-macro-replace-all org-macro-templates)
  109. (buffer-substring-no-properties
  110. (line-beginning-position) (line-end-position)))))
  111. (should
  112. (equal "1"
  113. (org-test-with-temp-text
  114. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
  115. (org-macro-initialize-templates)
  116. (org-macro-replace-all org-macro-templates)
  117. (buffer-substring-no-properties
  118. (line-beginning-position) (line-end-position)))))
  119. (should
  120. (equal
  121. "1"
  122. (org-test-with-temp-text
  123. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
  124. (org-macro-initialize-templates)
  125. (org-macro-replace-all org-macro-templates)
  126. (buffer-substring-no-properties
  127. (line-beginning-position) (line-end-position)))))
  128. (should-error
  129. (org-test-with-temp-text
  130. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
  131. (org-macro-initialize-templates)
  132. (org-macro-replace-all org-macro-templates))))
  133. (ert-deftest test-org-macro/n ()
  134. "Test {{{n}}} macro."
  135. ;; Standard test with default counter.
  136. (should
  137. (equal "1 2"
  138. (org-test-with-temp-text "{{{n}}} {{{n}}}"
  139. (org-macro-initialize-templates)
  140. (org-macro-replace-all org-macro-templates)
  141. (buffer-substring-no-properties
  142. (line-beginning-position) (line-end-position)))))
  143. (should
  144. (equal "1 2"
  145. (org-test-with-temp-text "{{{n()}}} {{{n}}}"
  146. (org-macro-initialize-templates)
  147. (org-macro-replace-all org-macro-templates)
  148. (buffer-substring-no-properties
  149. (line-beginning-position) (line-end-position)))))
  150. ;; Test alternative counters.
  151. (should
  152. (equal "1 1 1 2"
  153. (org-test-with-temp-text "{{{n}}} {{{n(c1)}}} {{{n(c2)}}} {{{n(c1)}}}"
  154. (org-macro-initialize-templates)
  155. (org-macro-replace-all org-macro-templates)
  156. (buffer-substring-no-properties
  157. (line-beginning-position) (line-end-position)))))
  158. ;; Second argument set a counter to a given value. A non-numeric
  159. ;; value resets the counter to 1.
  160. (should
  161. (equal "9 10"
  162. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c)}}}"
  163. (org-macro-initialize-templates)
  164. (org-macro-replace-all org-macro-templates)
  165. (buffer-substring-no-properties
  166. (line-beginning-position) (line-end-position)))))
  167. (should
  168. (equal "9 1"
  169. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c,reset)}}}"
  170. (org-macro-initialize-templates)
  171. (org-macro-replace-all org-macro-templates)
  172. (buffer-substring-no-properties
  173. (line-beginning-position) (line-end-position)))))
  174. ;; Check that reset happens when the second argument is neither "-"
  175. ;; nor a number.
  176. (should
  177. (equal "9 1 1 1"
  178. (org-test-with-temp-text
  179. (concat "{{{n(c,9)}}} {{{n(c,reiniciar)}}}"
  180. " {{{n(c,réinitialiser)}}} {{{n(c,zurückstellen)}}}")
  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. ;; Tolerate spaces in first argument.
  186. (should
  187. (equal "1 2 3 4"
  188. (org-test-with-temp-text "{{{n(c)}}} {{{n(c )}}} {{{n( c)}}} {{{n( c )}}}"
  189. (org-macro-initialize-templates)
  190. (org-macro-replace-all org-macro-templates)
  191. (buffer-substring-no-properties
  192. (line-beginning-position) (line-end-position)))))
  193. ;; Tolerate spaces when second argument is an integer.
  194. (should
  195. (equal "2 3 5 7"
  196. (org-test-with-temp-text
  197. (concat "{{{n(c,2)}}} {{{n(c, 3)}}}"
  198. " {{{n(c,5 )}}} {{{n(c, 7 )}}}")
  199. (org-macro-initialize-templates)
  200. (org-macro-replace-all org-macro-templates)
  201. (buffer-substring-no-properties
  202. (line-beginning-position) (line-end-position)))))
  203. ;; Tolerate spaces when second argument is the hold argument.
  204. (should
  205. (equal "7 7 8 8 9 9"
  206. (org-test-with-temp-text
  207. (concat "{{{n(,7)}}} {{{n(, -)}}}"
  208. " {{{n}}} {{{n(,- )}}} {{{n}}} {{{n(, - )}}}")
  209. (org-macro-initialize-templates)
  210. (org-macro-replace-all org-macro-templates)
  211. (buffer-substring-no-properties
  212. (line-beginning-position) (line-end-position)))))
  213. ;; Tolerate spaces when second argument is used to reset the counter.
  214. (should
  215. (equal "9 1 1 1 1"
  216. (org-test-with-temp-text
  217. (concat "{{{n(c,9)}}} {{{n(c,reset)}}} {{{n(c, reset)}}}"
  218. " {{{n(c,reset )}}} {{{n(c, reset )}}}")
  219. (org-macro-initialize-templates)
  220. (org-macro-replace-all org-macro-templates)
  221. (buffer-substring-no-properties
  222. (line-beginning-position) (line-end-position)))))
  223. ;; Second argument also applies to default counter.
  224. (should
  225. (equal "9 10 1"
  226. (org-test-with-temp-text "{{{n(,9)}}} {{{n}}} {{{n(,reset)}}}"
  227. (org-macro-initialize-templates)
  228. (org-macro-replace-all org-macro-templates)
  229. (buffer-substring-no-properties
  230. (line-beginning-position) (line-end-position)))))
  231. ;; An empty second argument is equivalent to no argument.
  232. (should
  233. (equal "2 3"
  234. (org-test-with-temp-text "{{{n(c,2)}}} {{{n(c,)}}}"
  235. (org-macro-initialize-templates)
  236. (org-macro-replace-all org-macro-templates)
  237. (buffer-substring-no-properties
  238. (line-beginning-position) (line-end-position)))))
  239. ;; Hold value at reset value of 1 if the counter hasn't yet started.
  240. (should
  241. (equal "1"
  242. (org-test-with-temp-text "{{{n(,-)}}}"
  243. (org-macro-initialize-templates)
  244. (org-macro-replace-all org-macro-templates)
  245. (buffer-substring-no-properties
  246. (line-beginning-position) (line-end-position)))))
  247. ;; Increment counter following a hold.
  248. (should
  249. (equal "1 1 2"
  250. (org-test-with-temp-text "{{{n}}} {{{n(,-)}}} {{{n}}}"
  251. (org-macro-initialize-templates)
  252. (org-macro-replace-all org-macro-templates)
  253. (buffer-substring-no-properties
  254. (line-beginning-position) (line-end-position)))))
  255. ;; Hold counter value following a counter value set.
  256. (should
  257. (equal "1 10 10"
  258. (org-test-with-temp-text "{{{n}}} {{{n(,10)}}} {{{n(,-)}}}"
  259. (org-macro-initialize-templates)
  260. (org-macro-replace-all org-macro-templates)
  261. (buffer-substring-no-properties
  262. (line-beginning-position) (line-end-position)))))
  263. ;; Hold counter value in a multiple-counter situation.
  264. (should
  265. (equal "1.1 1.2 1.3"
  266. (org-test-with-temp-text
  267. "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
  268. (org-macro-initialize-templates)
  269. (org-macro-replace-all org-macro-templates)
  270. (buffer-substring-no-properties
  271. (line-beginning-position) (line-end-position)))))
  272. ;; Hold counter values on one or multiple counters at the same time.
  273. (should
  274. (equal "1.1 1.2 2.2 2.2"
  275. (org-test-with-temp-text
  276. (concat "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
  277. " {{{n}}}.{{{n(c,-)}}} {{{n(,-)}}}.{{{n(c,-)}}}")
  278. (org-macro-initialize-templates)
  279. (org-macro-replace-all org-macro-templates)
  280. (buffer-substring-no-properties
  281. (line-beginning-position) (line-end-position))))))
  282. (ert-deftest test-org-macro/keyword ()
  283. "Test {{{keyword}}} macro."
  284. ;; Replace macro with keyword's value.
  285. (should
  286. (equal
  287. "value"
  288. (org-test-with-temp-text
  289. "#+keyword: value\n<point>{{{keyword(KEYWORD)}}}"
  290. (org-macro-initialize-templates)
  291. (org-macro-replace-all org-macro-templates)
  292. (buffer-substring-no-properties
  293. (line-beginning-position) (point-max))))))
  294. (ert-deftest test-org-macro/escape-arguments ()
  295. "Test `org-macro-escape-arguments' specifications."
  296. ;; Regular tests.
  297. (should (equal "a" (org-macro-escape-arguments "a")))
  298. (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
  299. ;; Handle empty arguments.
  300. (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
  301. ;; Properly escape commas and backslashes preceding them.
  302. (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
  303. (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
  304. (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
  305. (ert-deftest test-org-macro/extract-arguments ()
  306. "Test `org-macro-extract-arguments' specifications."
  307. ;; Regular tests.
  308. (should (equal '("a") (org-macro-extract-arguments "a")))
  309. (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
  310. ;; Handle empty arguments.
  311. (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
  312. ;; Handle escaped commas and backslashes.
  313. (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
  314. (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
  315. (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
  316. (provide 'test-org-macro)
  317. ;;; test-org-macro.el ends here