test-org-macro.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. ;;; test-org-macro.el --- Tests for org-macro.el
  2. ;; Copyright (C) 2013, 2014 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. (progn (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. "#+MACRO: add (eval (+ $1 $2))\n3"
  38. (org-test-with-temp-text "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
  39. (progn (org-macro-initialize-templates)
  40. (org-macro-replace-all org-macro-templates)
  41. (buffer-string)))))
  42. ;; Nested macros.
  43. (should
  44. (equal
  45. "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\ninner outer"
  46. (org-test-with-temp-text
  47. "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
  48. (progn (org-macro-initialize-templates)
  49. (org-macro-replace-all org-macro-templates)
  50. (buffer-string)))))
  51. ;; Error out when macro expansion is circular.
  52. (should-error
  53. (org-test-with-temp-text
  54. "#+MACRO: mac1 {{{mac2}}}\n#+MACRO: mac2 {{{mac1}}}\n{{{mac1}}}"
  55. (org-macro-initialize-templates)
  56. (org-macro-replace-all org-macro-templates)))
  57. ;; Macros in setup file.
  58. (should
  59. (string-match
  60. "success success\\'"
  61. (org-test-with-temp-text
  62. (format "#+MACRO: other-macro success
  63. #+SETUPFILE: \"%sexamples/macro-templates.org\"
  64. {{{included-macro}}} {{{other-macro}}}"
  65. org-test-dir)
  66. (org-macro-initialize-templates)
  67. (org-macro-replace-all org-macro-templates)
  68. (buffer-string))))
  69. ;; Test special "property" macro. With only one argument, retrieve
  70. ;; property from current headline. Otherwise, the second argument
  71. ;; is a search option to get the property from another headline.
  72. (should
  73. (equal "1"
  74. (org-test-with-temp-text
  75. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
  76. (org-macro-initialize-templates)
  77. (org-macro-replace-all org-macro-templates)
  78. (buffer-substring-no-properties
  79. (line-beginning-position) (line-end-position)))))
  80. (should
  81. (equal "1"
  82. (org-test-with-temp-text
  83. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
  84. (org-macro-initialize-templates)
  85. (org-macro-replace-all org-macro-templates)
  86. (buffer-substring-no-properties
  87. (line-beginning-position) (line-end-position)))))
  88. (should
  89. (equal
  90. "1"
  91. (org-test-with-temp-text
  92. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<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-error
  98. (org-test-with-temp-text
  99. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
  100. (org-macro-initialize-templates)
  101. (org-macro-replace-all org-macro-templates)))
  102. ;; Macro expansion ignores narrowing.
  103. (should
  104. (string-match
  105. "expansion"
  106. (org-test-with-temp-text
  107. "#+MACRO: macro expansion\n{{{macro}}}\n<point>Contents"
  108. (narrow-to-region (point) (point-max))
  109. (org-macro-initialize-templates)
  110. (org-macro-replace-all org-macro-templates)
  111. (org-with-wide-buffer (buffer-string)))))
  112. ;; Macros in a commented tree are not expanded.
  113. (should
  114. (string-match-p
  115. "{{{macro}}}"
  116. (org-test-with-temp-text
  117. "#+MACRO: macro expansion\n* COMMENT H\n<point>{{{macro}}}"
  118. (org-macro-initialize-templates)
  119. (org-macro-replace-all org-macro-templates)
  120. (org-with-wide-buffer (buffer-string)))))
  121. (should
  122. (string-match-p
  123. "{{{macro}}}"
  124. (org-test-with-temp-text
  125. "#+MACRO: macro expansion\n* COMMENT H1\n** H2\n<point>{{{macro}}}"
  126. (org-macro-initialize-templates)
  127. (org-macro-replace-all org-macro-templates)
  128. (org-with-wide-buffer (buffer-string))))))
  129. (ert-deftest test-org-macro/property ()
  130. "Test {{{property}}} macro."
  131. ;; With only one argument, retrieve property from current headline.
  132. ;; Otherwise, the second argument is a search option to get the
  133. ;; property from another headline.
  134. (should
  135. (equal "1"
  136. (org-test-with-temp-text
  137. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
  138. (org-macro-initialize-templates)
  139. (org-macro-replace-all org-macro-templates)
  140. (buffer-substring-no-properties
  141. (line-beginning-position) (line-end-position)))))
  142. (should
  143. (equal "1"
  144. (org-test-with-temp-text
  145. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
  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. (should
  151. (equal
  152. "1"
  153. (org-test-with-temp-text
  154. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
  155. (org-macro-initialize-templates)
  156. (org-macro-replace-all org-macro-templates)
  157. (buffer-substring-no-properties
  158. (line-beginning-position) (line-end-position)))))
  159. (should-error
  160. (org-test-with-temp-text
  161. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
  162. (org-macro-initialize-templates)
  163. (org-macro-replace-all org-macro-templates))))
  164. (ert-deftest test-org-macro/n ()
  165. "Test {{{n}}} macro."
  166. ;; Standard test with default counter.
  167. (should
  168. (equal "1 2"
  169. (org-test-with-temp-text "{{{n}}} {{{n}}}"
  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. (should
  175. (equal "1 2"
  176. (org-test-with-temp-text "{{{n()}}} {{{n}}}"
  177. (org-macro-initialize-templates)
  178. (org-macro-replace-all org-macro-templates)
  179. (buffer-substring-no-properties
  180. (line-beginning-position) (line-end-position)))))
  181. ;; Test alternative counters.
  182. (should
  183. (equal "1 1 1 2"
  184. (org-test-with-temp-text "{{{n}}} {{{n(c1)}}} {{{n(c2)}}} {{{n(c1)}}}"
  185. (org-macro-initialize-templates)
  186. (org-macro-replace-all org-macro-templates)
  187. (buffer-substring-no-properties
  188. (line-beginning-position) (line-end-position)))))
  189. ;; Second argument set a counter to a given value. A non-numeric
  190. ;; value resets the counter to 1.
  191. (should
  192. (equal "9 10"
  193. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c)}}}"
  194. (org-macro-initialize-templates)
  195. (org-macro-replace-all org-macro-templates)
  196. (buffer-substring-no-properties
  197. (line-beginning-position) (line-end-position)))))
  198. (should
  199. (equal "9 1"
  200. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c,reset)}}}"
  201. (org-macro-initialize-templates)
  202. (org-macro-replace-all org-macro-templates)
  203. (buffer-substring-no-properties
  204. (line-beginning-position) (line-end-position)))))
  205. ;; Check that reset happens when the second argument is neither "-"
  206. ;; nor a number.
  207. (should
  208. (equal "9 1 1 1"
  209. (org-test-with-temp-text
  210. (concat "{{{n(c,9)}}} {{{n(c,reiniciar)}}}"
  211. " {{{n(c,réinitialiser)}}} {{{n(c,zurückstellen)}}}")
  212. (org-macro-initialize-templates)
  213. (org-macro-replace-all org-macro-templates)
  214. (buffer-substring-no-properties
  215. (line-beginning-position) (line-end-position)))))
  216. ;; Tolerate spaces in first argument.
  217. (should
  218. (equal "1 2 3 4"
  219. (org-test-with-temp-text "{{{n(c)}}} {{{n(c )}}} {{{n( c)}}} {{{n( c )}}}"
  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 an integer.
  225. (should
  226. (equal "2 3 5 7"
  227. (org-test-with-temp-text
  228. (concat "{{{n(c,2)}}} {{{n(c, 3)}}}"
  229. " {{{n(c,5 )}}} {{{n(c, 7 )}}}")
  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. ;; Tolerate spaces when second argument is the hold argument.
  235. (should
  236. (equal "7 7 8 8 9 9"
  237. (org-test-with-temp-text
  238. (concat "{{{n(,7)}}} {{{n(, -)}}}"
  239. " {{{n}}} {{{n(,- )}}} {{{n}}} {{{n(, - )}}}")
  240. (org-macro-initialize-templates)
  241. (org-macro-replace-all org-macro-templates)
  242. (buffer-substring-no-properties
  243. (line-beginning-position) (line-end-position)))))
  244. ;; Tolerate spaces when second argument is used to reset the counter.
  245. (should
  246. (equal "9 1 1 1 1"
  247. (org-test-with-temp-text
  248. (concat "{{{n(c,9)}}} {{{n(c,reset)}}} {{{n(c, reset)}}}"
  249. " {{{n(c,reset )}}} {{{n(c, reset )}}}")
  250. (org-macro-initialize-templates)
  251. (org-macro-replace-all org-macro-templates)
  252. (buffer-substring-no-properties
  253. (line-beginning-position) (line-end-position)))))
  254. ;; Second argument also applies to default counter.
  255. (should
  256. (equal "9 10 1"
  257. (org-test-with-temp-text "{{{n(,9)}}} {{{n}}} {{{n(,reset)}}}"
  258. (org-macro-initialize-templates)
  259. (org-macro-replace-all org-macro-templates)
  260. (buffer-substring-no-properties
  261. (line-beginning-position) (line-end-position)))))
  262. ;; An empty second argument is equivalent to no argument.
  263. (should
  264. (equal "2 3"
  265. (org-test-with-temp-text "{{{n(c,2)}}} {{{n(c,)}}}"
  266. (org-macro-initialize-templates)
  267. (org-macro-replace-all org-macro-templates)
  268. (buffer-substring-no-properties
  269. (line-beginning-position) (line-end-position)))))
  270. ;; Hold value at reset value of 1 if the counter hasn't yet started.
  271. (should
  272. (equal "1"
  273. (org-test-with-temp-text "{{{n(,-)}}}"
  274. (org-macro-initialize-templates)
  275. (org-macro-replace-all org-macro-templates)
  276. (buffer-substring-no-properties
  277. (line-beginning-position) (line-end-position)))))
  278. ;; Increment counter following a hold.
  279. (should
  280. (equal "1 1 2"
  281. (org-test-with-temp-text "{{{n}}} {{{n(,-)}}} {{{n}}}"
  282. (org-macro-initialize-templates)
  283. (org-macro-replace-all org-macro-templates)
  284. (buffer-substring-no-properties
  285. (line-beginning-position) (line-end-position)))))
  286. ;; Hold counter value following a counter value set.
  287. (should
  288. (equal "1 10 10"
  289. (org-test-with-temp-text "{{{n}}} {{{n(,10)}}} {{{n(,-)}}}"
  290. (org-macro-initialize-templates)
  291. (org-macro-replace-all org-macro-templates)
  292. (buffer-substring-no-properties
  293. (line-beginning-position) (line-end-position)))))
  294. ;; Hold counter value in a multiple-counter situation.
  295. (should
  296. (equal "1.1 1.2 1.3"
  297. (org-test-with-temp-text
  298. "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
  299. (org-macro-initialize-templates)
  300. (org-macro-replace-all org-macro-templates)
  301. (buffer-substring-no-properties
  302. (line-beginning-position) (line-end-position)))))
  303. ;; Hold counter values on one or multiple counters at the same time.
  304. (should
  305. (equal "1.1 1.2 2.2 2.2"
  306. (org-test-with-temp-text
  307. (concat "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
  308. " {{{n}}}.{{{n(c,-)}}} {{{n(,-)}}}.{{{n(c,-)}}}")
  309. (org-macro-initialize-templates)
  310. (org-macro-replace-all org-macro-templates)
  311. (buffer-substring-no-properties
  312. (line-beginning-position) (line-end-position))))))
  313. (ert-deftest test-org-macro/escape-arguments ()
  314. "Test `org-macro-escape-arguments' specifications."
  315. ;; Regular tests.
  316. (should (equal "a" (org-macro-escape-arguments "a")))
  317. (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
  318. ;; Handle empty arguments.
  319. (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
  320. ;; Properly escape commas and backslashes preceding them.
  321. (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
  322. (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
  323. (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
  324. (ert-deftest test-org-macro/extract-arguments ()
  325. "Test `org-macro-extract-arguments' specifications."
  326. ;; Regular tests.
  327. (should (equal '("a") (org-macro-extract-arguments "a")))
  328. (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
  329. ;; Handle empty arguments.
  330. (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
  331. ;; Handle escaped commas and backslashes.
  332. (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
  333. (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
  334. (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
  335. (provide 'test-org-macro)
  336. ;;; test-org-macro.el ends here