test-org-macro.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. (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. ;; Test special "property" macro. With only one argument, retrieve
  72. ;; property from current headline. Otherwise, the second argument
  73. ;; is a search option to get the property from another headline.
  74. (should
  75. (equal "1"
  76. (org-test-with-temp-text
  77. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
  78. (org-macro-initialize-templates)
  79. (org-macro-replace-all org-macro-templates)
  80. (buffer-substring-no-properties
  81. (line-beginning-position) (line-end-position)))))
  82. (should
  83. (equal "1"
  84. (org-test-with-temp-text
  85. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
  86. (org-macro-initialize-templates)
  87. (org-macro-replace-all org-macro-templates)
  88. (buffer-substring-no-properties
  89. (line-beginning-position) (line-end-position)))))
  90. (should
  91. (equal
  92. "1"
  93. (org-test-with-temp-text
  94. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
  95. (org-macro-initialize-templates)
  96. (org-macro-replace-all org-macro-templates)
  97. (buffer-substring-no-properties
  98. (line-beginning-position) (line-end-position)))))
  99. (should-error
  100. (org-test-with-temp-text
  101. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
  102. (org-macro-initialize-templates)
  103. (org-macro-replace-all org-macro-templates)))
  104. ;; Macro expansion ignores narrowing.
  105. (should
  106. (string-match
  107. "expansion"
  108. (org-test-with-temp-text
  109. "#+MACRO: macro expansion\n{{{macro}}}\n<point>Contents"
  110. (narrow-to-region (point) (point-max))
  111. (org-macro-initialize-templates)
  112. (org-macro-replace-all org-macro-templates)
  113. (org-with-wide-buffer (buffer-string)))))
  114. ;; Macros in a commented tree are not expanded.
  115. (should
  116. (string-match-p
  117. "{{{macro}}}"
  118. (org-test-with-temp-text
  119. "#+MACRO: macro expansion\n* COMMENT H\n<point>{{{macro}}}"
  120. (org-macro-initialize-templates)
  121. (org-macro-replace-all org-macro-templates)
  122. (org-with-wide-buffer (buffer-string)))))
  123. (should
  124. (string-match-p
  125. "{{{macro}}}"
  126. (org-test-with-temp-text
  127. "#+MACRO: macro expansion\n* COMMENT H1\n** H2\n<point>{{{macro}}}"
  128. (org-macro-initialize-templates)
  129. (org-macro-replace-all org-macro-templates)
  130. (org-with-wide-buffer (buffer-string))))))
  131. (ert-deftest test-org-macro/property ()
  132. "Test {{{property}}} macro."
  133. ;; With only one argument, retrieve property from current headline.
  134. ;; Otherwise, the second argument is a search option to get the
  135. ;; property from another headline.
  136. (should
  137. (equal "1"
  138. (org-test-with-temp-text
  139. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
  140. (org-macro-initialize-templates)
  141. (org-macro-replace-all org-macro-templates)
  142. (buffer-substring-no-properties
  143. (line-beginning-position) (line-end-position)))))
  144. (should
  145. (equal "1"
  146. (org-test-with-temp-text
  147. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
  148. (org-macro-initialize-templates)
  149. (org-macro-replace-all org-macro-templates)
  150. (buffer-substring-no-properties
  151. (line-beginning-position) (line-end-position)))))
  152. (should
  153. (equal
  154. "1"
  155. (org-test-with-temp-text
  156. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
  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. (should-error
  162. (org-test-with-temp-text
  163. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
  164. (org-macro-initialize-templates)
  165. (org-macro-replace-all org-macro-templates))))
  166. (ert-deftest test-org-macro/n ()
  167. "Test {{{n}}} macro."
  168. ;; Standard test with default counter.
  169. (should
  170. (equal "1 2"
  171. (org-test-with-temp-text "{{{n}}} {{{n}}}"
  172. (org-macro-initialize-templates)
  173. (org-macro-replace-all org-macro-templates)
  174. (buffer-substring-no-properties
  175. (line-beginning-position) (line-end-position)))))
  176. (should
  177. (equal "1 2"
  178. (org-test-with-temp-text "{{{n()}}} {{{n}}}"
  179. (org-macro-initialize-templates)
  180. (org-macro-replace-all org-macro-templates)
  181. (buffer-substring-no-properties
  182. (line-beginning-position) (line-end-position)))))
  183. ;; Test alternative counters.
  184. (should
  185. (equal "1 1 1 2"
  186. (org-test-with-temp-text "{{{n}}} {{{n(c1)}}} {{{n(c2)}}} {{{n(c1)}}}"
  187. (org-macro-initialize-templates)
  188. (org-macro-replace-all org-macro-templates)
  189. (buffer-substring-no-properties
  190. (line-beginning-position) (line-end-position)))))
  191. ;; Second argument set a counter to a given value. A non-numeric
  192. ;; value resets the counter to 1.
  193. (should
  194. (equal "9 10"
  195. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c)}}}"
  196. (org-macro-initialize-templates)
  197. (org-macro-replace-all org-macro-templates)
  198. (buffer-substring-no-properties
  199. (line-beginning-position) (line-end-position)))))
  200. (should
  201. (equal "9 1"
  202. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c,reset)}}}"
  203. (org-macro-initialize-templates)
  204. (org-macro-replace-all org-macro-templates)
  205. (buffer-substring-no-properties
  206. (line-beginning-position) (line-end-position)))))
  207. ;; Check that reset happens when the second argument is neither "-"
  208. ;; nor a number.
  209. (should
  210. (equal "9 1 1 1"
  211. (org-test-with-temp-text
  212. (concat "{{{n(c,9)}}} {{{n(c,reiniciar)}}}"
  213. " {{{n(c,réinitialiser)}}} {{{n(c,zurückstellen)}}}")
  214. (org-macro-initialize-templates)
  215. (org-macro-replace-all org-macro-templates)
  216. (buffer-substring-no-properties
  217. (line-beginning-position) (line-end-position)))))
  218. ;; Tolerate spaces in first argument.
  219. (should
  220. (equal "1 2 3 4"
  221. (org-test-with-temp-text "{{{n(c)}}} {{{n(c )}}} {{{n( c)}}} {{{n( c )}}}"
  222. (org-macro-initialize-templates)
  223. (org-macro-replace-all org-macro-templates)
  224. (buffer-substring-no-properties
  225. (line-beginning-position) (line-end-position)))))
  226. ;; Tolerate spaces when second argument is an integer.
  227. (should
  228. (equal "2 3 5 7"
  229. (org-test-with-temp-text
  230. (concat "{{{n(c,2)}}} {{{n(c, 3)}}}"
  231. " {{{n(c,5 )}}} {{{n(c, 7 )}}}")
  232. (org-macro-initialize-templates)
  233. (org-macro-replace-all org-macro-templates)
  234. (buffer-substring-no-properties
  235. (line-beginning-position) (line-end-position)))))
  236. ;; Tolerate spaces when second argument is the hold argument.
  237. (should
  238. (equal "7 7 8 8 9 9"
  239. (org-test-with-temp-text
  240. (concat "{{{n(,7)}}} {{{n(, -)}}}"
  241. " {{{n}}} {{{n(,- )}}} {{{n}}} {{{n(, - )}}}")
  242. (org-macro-initialize-templates)
  243. (org-macro-replace-all org-macro-templates)
  244. (buffer-substring-no-properties
  245. (line-beginning-position) (line-end-position)))))
  246. ;; Tolerate spaces when second argument is used to reset the counter.
  247. (should
  248. (equal "9 1 1 1 1"
  249. (org-test-with-temp-text
  250. (concat "{{{n(c,9)}}} {{{n(c,reset)}}} {{{n(c, reset)}}}"
  251. " {{{n(c,reset )}}} {{{n(c, reset )}}}")
  252. (org-macro-initialize-templates)
  253. (org-macro-replace-all org-macro-templates)
  254. (buffer-substring-no-properties
  255. (line-beginning-position) (line-end-position)))))
  256. ;; Second argument also applies to default counter.
  257. (should
  258. (equal "9 10 1"
  259. (org-test-with-temp-text "{{{n(,9)}}} {{{n}}} {{{n(,reset)}}}"
  260. (org-macro-initialize-templates)
  261. (org-macro-replace-all org-macro-templates)
  262. (buffer-substring-no-properties
  263. (line-beginning-position) (line-end-position)))))
  264. ;; An empty second argument is equivalent to no argument.
  265. (should
  266. (equal "2 3"
  267. (org-test-with-temp-text "{{{n(c,2)}}} {{{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 value at reset value of 1 if the counter hasn't yet started.
  273. (should
  274. (equal "1"
  275. (org-test-with-temp-text "{{{n(,-)}}}"
  276. (org-macro-initialize-templates)
  277. (org-macro-replace-all org-macro-templates)
  278. (buffer-substring-no-properties
  279. (line-beginning-position) (line-end-position)))))
  280. ;; Increment counter following a hold.
  281. (should
  282. (equal "1 1 2"
  283. (org-test-with-temp-text "{{{n}}} {{{n(,-)}}} {{{n}}}"
  284. (org-macro-initialize-templates)
  285. (org-macro-replace-all org-macro-templates)
  286. (buffer-substring-no-properties
  287. (line-beginning-position) (line-end-position)))))
  288. ;; Hold counter value following a counter value set.
  289. (should
  290. (equal "1 10 10"
  291. (org-test-with-temp-text "{{{n}}} {{{n(,10)}}} {{{n(,-)}}}"
  292. (org-macro-initialize-templates)
  293. (org-macro-replace-all org-macro-templates)
  294. (buffer-substring-no-properties
  295. (line-beginning-position) (line-end-position)))))
  296. ;; Hold counter value in a multiple-counter situation.
  297. (should
  298. (equal "1.1 1.2 1.3"
  299. (org-test-with-temp-text
  300. "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
  301. (org-macro-initialize-templates)
  302. (org-macro-replace-all org-macro-templates)
  303. (buffer-substring-no-properties
  304. (line-beginning-position) (line-end-position)))))
  305. ;; Hold counter values on one or multiple counters at the same time.
  306. (should
  307. (equal "1.1 1.2 2.2 2.2"
  308. (org-test-with-temp-text
  309. (concat "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
  310. " {{{n}}}.{{{n(c,-)}}} {{{n(,-)}}}.{{{n(c,-)}}}")
  311. (org-macro-initialize-templates)
  312. (org-macro-replace-all org-macro-templates)
  313. (buffer-substring-no-properties
  314. (line-beginning-position) (line-end-position))))))
  315. (ert-deftest test-org-macro/keyword ()
  316. "Test {{{keyword}}} macro."
  317. ;; Replace macro with keyword's value.
  318. (should
  319. (equal
  320. "value"
  321. (org-test-with-temp-text
  322. "#+keyword: value\n<point>{{{keyword(KEYWORD)}}}"
  323. (org-macro-initialize-templates)
  324. (org-macro-replace-all org-macro-templates)
  325. (buffer-substring-no-properties
  326. (line-beginning-position) (point-max)))))
  327. ;; Replace macro with keyword's value.
  328. (should
  329. (equal
  330. "value value2"
  331. (org-test-with-temp-text
  332. "#+keyword: value\n#+keyword: value2\n<point>{{{keyword(KEYWORD)}}}"
  333. (org-macro-initialize-templates)
  334. (org-macro-replace-all org-macro-templates)
  335. (buffer-substring-no-properties
  336. (line-beginning-position) (point-max))))))
  337. (ert-deftest test-org-macro/escape-arguments ()
  338. "Test `org-macro-escape-arguments' specifications."
  339. ;; Regular tests.
  340. (should (equal "a" (org-macro-escape-arguments "a")))
  341. (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
  342. ;; Handle empty arguments.
  343. (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
  344. ;; Properly escape commas and backslashes preceding them.
  345. (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
  346. (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
  347. (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
  348. (ert-deftest test-org-macro/extract-arguments ()
  349. "Test `org-macro-extract-arguments' specifications."
  350. ;; Regular tests.
  351. (should (equal '("a") (org-macro-extract-arguments "a")))
  352. (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
  353. ;; Handle empty arguments.
  354. (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
  355. ;; Handle escaped commas and backslashes.
  356. (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
  357. (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
  358. (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
  359. (provide 'test-org-macro)
  360. ;;; test-org-macro.el ends here