test-org-macro.el 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. ;; Macro expansion ignores narrowing.
  70. (should
  71. (string-match-p
  72. "{{{macro}}}"
  73. (org-test-with-temp-text
  74. "#+MACRO: macro expansion\n{{{macro}}}\n<point>Contents"
  75. (narrow-to-region (point) (point-max))
  76. (org-macro-initialize-templates)
  77. (org-macro-replace-all org-macro-templates)
  78. (org-with-wide-buffer (buffer-string)))))
  79. ;; Macros in a commented tree are not expanded.
  80. (should
  81. (string-match-p
  82. "{{{macro}}}"
  83. (org-test-with-temp-text
  84. "#+MACRO: macro expansion\n* COMMENT H\n<point>{{{macro}}}"
  85. (org-macro-initialize-templates)
  86. (org-macro-replace-all org-macro-templates)
  87. (org-with-wide-buffer (buffer-string)))))
  88. (should
  89. (string-match-p
  90. "{{{macro}}}"
  91. (org-test-with-temp-text
  92. "#+MACRO: macro expansion\n* COMMENT H1\n** H2\n<point>{{{macro}}}"
  93. (org-macro-initialize-templates)
  94. (org-macro-replace-all org-macro-templates)
  95. (org-with-wide-buffer (buffer-string))))))
  96. (ert-deftest test-org-macro/property ()
  97. "Test {{{property}}} macro."
  98. ;; With only one argument, retrieve property from current headline.
  99. ;; Otherwise, the second argument is a search option to get the
  100. ;; property from another headline.
  101. (should
  102. (equal "1"
  103. (org-test-with-temp-text
  104. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
  105. (org-macro-initialize-templates)
  106. (org-macro-replace-all org-macro-templates)
  107. (buffer-substring-no-properties
  108. (line-beginning-position) (line-end-position)))))
  109. (should
  110. (equal "1"
  111. (org-test-with-temp-text
  112. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
  113. (org-macro-initialize-templates)
  114. (org-macro-replace-all org-macro-templates)
  115. (buffer-substring-no-properties
  116. (line-beginning-position) (line-end-position)))))
  117. (should
  118. (equal
  119. "1"
  120. (org-test-with-temp-text
  121. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
  122. (org-macro-initialize-templates)
  123. (org-macro-replace-all org-macro-templates)
  124. (buffer-substring-no-properties
  125. (line-beginning-position) (line-end-position)))))
  126. (should-error
  127. (org-test-with-temp-text
  128. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
  129. (org-macro-initialize-templates)
  130. (org-macro-replace-all org-macro-templates))))
  131. (ert-deftest test-org-macro/n ()
  132. "Test {{{n}}} macro."
  133. ;; Standard test with default counter.
  134. (should
  135. (equal "1 2"
  136. (org-test-with-temp-text "{{{n}}} {{{n}}}"
  137. (org-macro-initialize-templates)
  138. (org-macro-replace-all org-macro-templates)
  139. (buffer-substring-no-properties
  140. (line-beginning-position) (line-end-position)))))
  141. (should
  142. (equal "1 2"
  143. (org-test-with-temp-text "{{{n()}}} {{{n}}}"
  144. (org-macro-initialize-templates)
  145. (org-macro-replace-all org-macro-templates)
  146. (buffer-substring-no-properties
  147. (line-beginning-position) (line-end-position)))))
  148. ;; Test alternative counters.
  149. (should
  150. (equal "1 1 1 2"
  151. (org-test-with-temp-text "{{{n}}} {{{n(c1)}}} {{{n(c2)}}} {{{n(c1)}}}"
  152. (org-macro-initialize-templates)
  153. (org-macro-replace-all org-macro-templates)
  154. (buffer-substring-no-properties
  155. (line-beginning-position) (line-end-position)))))
  156. ;; Second argument set a counter to a given value. A non-numeric
  157. ;; value resets the counter to 1.
  158. (should
  159. (equal "9 10"
  160. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c)}}}"
  161. (org-macro-initialize-templates)
  162. (org-macro-replace-all org-macro-templates)
  163. (buffer-substring-no-properties
  164. (line-beginning-position) (line-end-position)))))
  165. (should
  166. (equal "9 1"
  167. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c,reset)}}}"
  168. (org-macro-initialize-templates)
  169. (org-macro-replace-all org-macro-templates)
  170. (buffer-substring-no-properties
  171. (line-beginning-position) (line-end-position)))))
  172. ;; Tolerate spaces in second argument.
  173. (should
  174. (equal "9 10"
  175. (org-test-with-temp-text "{{{n(c, 9)}}} {{{n(c)}}}"
  176. (org-macro-initialize-templates)
  177. (org-macro-replace-all org-macro-templates)
  178. (buffer-substring-no-properties
  179. (line-beginning-position) (line-end-position)))))
  180. (should
  181. (equal "9 1"
  182. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c, reset)}}}"
  183. (org-macro-initialize-templates)
  184. (org-macro-replace-all org-macro-templates)
  185. (buffer-substring-no-properties
  186. (line-beginning-position) (line-end-position)))))
  187. ;; Second argument also applies to default counter.
  188. (should
  189. (equal "9 10 1"
  190. (org-test-with-temp-text "{{{n(,9)}}} {{{n}}} {{{n(,reset)}}}"
  191. (org-macro-initialize-templates)
  192. (org-macro-replace-all org-macro-templates)
  193. (buffer-substring-no-properties
  194. (line-beginning-position) (line-end-position)))))
  195. ;; An empty second argument is equivalent to no argument.
  196. (should
  197. (equal "2 3"
  198. (org-test-with-temp-text "{{{n(c,2)}}} {{{n(c,)}}}"
  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. (ert-deftest test-org-macro/escape-arguments ()
  204. "Test `org-macro-escape-arguments' specifications."
  205. ;; Regular tests.
  206. (should (equal "a" (org-macro-escape-arguments "a")))
  207. (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
  208. ;; Handle empty arguments.
  209. (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
  210. ;; Properly escape commas and backslashes preceding them.
  211. (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
  212. (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
  213. (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
  214. (ert-deftest test-org-macro/extract-arguments ()
  215. "Test `org-macro-extract-arguments' specifications."
  216. ;; Regular tests.
  217. (should (equal '("a") (org-macro-extract-arguments "a")))
  218. (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
  219. ;; Handle empty arguments.
  220. (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
  221. ;; Handle escaped commas and backslashes.
  222. (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
  223. (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
  224. (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
  225. (provide 'test-org-macro)
  226. ;;; test-org-macro.el ends here