test-org-macro.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/escape-arguments ()
  130. "Test `org-macro-escape-arguments' specifications."
  131. ;; Regular tests.
  132. (should (equal "a" (org-macro-escape-arguments "a")))
  133. (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
  134. ;; Handle empty arguments.
  135. (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
  136. ;; Properly escape commas and backslashes preceding them.
  137. (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
  138. (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
  139. (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
  140. (ert-deftest test-org-macro/extract-arguments ()
  141. "Test `org-macro-extract-arguments' specifications."
  142. ;; Regular tests.
  143. (should (equal '("a") (org-macro-extract-arguments "a")))
  144. (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
  145. ;; Handle empty arguments.
  146. (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
  147. ;; Handle escaped commas and backslashes.
  148. (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
  149. (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
  150. (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
  151. (provide 'test-org-macro)
  152. ;;; test-org-macro.el ends here