test-org-macro.el 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. (ert-deftest test-org-macro/property ()
  80. "Test {{{property}}} macro."
  81. ;; With only one argument, retrieve property from current headline.
  82. ;; Otherwise, the second argument is a search option to get the
  83. ;; property from another headline.
  84. (should
  85. (equal "1"
  86. (org-test-with-temp-text
  87. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
  88. (org-macro-initialize-templates)
  89. (org-macro-replace-all org-macro-templates)
  90. (buffer-substring-no-properties
  91. (line-beginning-position) (line-end-position)))))
  92. (should
  93. (equal "1"
  94. (org-test-with-temp-text
  95. "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
  96. (org-macro-initialize-templates)
  97. (org-macro-replace-all org-macro-templates)
  98. (buffer-substring-no-properties
  99. (line-beginning-position) (line-end-position)))))
  100. (should
  101. (equal
  102. "1"
  103. (org-test-with-temp-text
  104. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<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-error
  110. (org-test-with-temp-text
  111. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
  112. (org-macro-initialize-templates)
  113. (org-macro-replace-all org-macro-templates))))
  114. (ert-deftest test-org-macro/n ()
  115. "Test {{{n}}} macro."
  116. ;; Standard test with default counter.
  117. (should
  118. (equal "1 2"
  119. (org-test-with-temp-text "{{{n}}} {{{n}}}"
  120. (org-macro-initialize-templates)
  121. (org-macro-replace-all org-macro-templates)
  122. (buffer-substring-no-properties
  123. (line-beginning-position) (line-end-position)))))
  124. (should
  125. (equal "1 2"
  126. (org-test-with-temp-text "{{{n()}}} {{{n}}}"
  127. (org-macro-initialize-templates)
  128. (org-macro-replace-all org-macro-templates)
  129. (buffer-substring-no-properties
  130. (line-beginning-position) (line-end-position)))))
  131. ;; Test alternative counters.
  132. (should
  133. (equal "1 1 1 2"
  134. (org-test-with-temp-text "{{{n}}} {{{n(c1)}}} {{{n(c2)}}} {{{n(c1)}}}"
  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. ;; Second argument set a counter to a given value. A non-numeric
  140. ;; value resets the counter to 1.
  141. (should
  142. (equal "9 10"
  143. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c)}}}"
  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. (should
  149. (equal "9 1"
  150. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c,reset)}}}"
  151. (org-macro-initialize-templates)
  152. (org-macro-replace-all org-macro-templates)
  153. (buffer-substring-no-properties
  154. (line-beginning-position) (line-end-position)))))
  155. ;; Tolerate spaces in second argument.
  156. (should
  157. (equal "9 10"
  158. (org-test-with-temp-text "{{{n(c, 9)}}} {{{n(c)}}}"
  159. (org-macro-initialize-templates)
  160. (org-macro-replace-all org-macro-templates)
  161. (buffer-substring-no-properties
  162. (line-beginning-position) (line-end-position)))))
  163. (should
  164. (equal "9 1"
  165. (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c, reset)}}}"
  166. (org-macro-initialize-templates)
  167. (org-macro-replace-all org-macro-templates)
  168. (buffer-substring-no-properties
  169. (line-beginning-position) (line-end-position)))))
  170. ;; Second argument also applies to default counter.
  171. (should
  172. (equal "9 10 1"
  173. (org-test-with-temp-text "{{{n(,9)}}} {{{n}}} {{{n(,reset)}}}"
  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. ;; An empty second argument is equivalent to no argument.
  179. (should
  180. (equal "2 3"
  181. (org-test-with-temp-text "{{{n(c,2)}}} {{{n(c,)}}}"
  182. (org-macro-initialize-templates)
  183. (org-macro-replace-all org-macro-templates)
  184. (buffer-substring-no-properties
  185. (line-beginning-position) (line-end-position))))))
  186. (ert-deftest test-org-macro/escape-arguments ()
  187. "Test `org-macro-escape-arguments' specifications."
  188. ;; Regular tests.
  189. (should (equal "a" (org-macro-escape-arguments "a")))
  190. (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
  191. ;; Handle empty arguments.
  192. (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
  193. ;; Properly escape commas and backslashes preceding them.
  194. (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
  195. (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
  196. (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
  197. (ert-deftest test-org-macro/extract-arguments ()
  198. "Test `org-macro-extract-arguments' specifications."
  199. ;; Regular tests.
  200. (should (equal '("a") (org-macro-extract-arguments "a")))
  201. (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
  202. ;; Handle empty arguments.
  203. (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
  204. ;; Handle escaped commas and backslashes.
  205. (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
  206. (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
  207. (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
  208. (provide 'test-org-macro)
  209. ;;; test-org-macro.el ends here