test-org-macro.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. (ert-deftest test-org-macro/escape-arguments ()
  103. "Test `org-macro-escape-arguments' specifications."
  104. ;; Regular tests.
  105. (should (equal "a" (org-macro-escape-arguments "a")))
  106. (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
  107. ;; Handle empty arguments.
  108. (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
  109. ;; Properly escape commas and backslashes preceding them.
  110. (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
  111. (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
  112. (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
  113. (ert-deftest test-org-macro/extract-arguments ()
  114. "Test `org-macro-extract-arguments' specifications."
  115. ;; Regular tests.
  116. (should (equal '("a") (org-macro-extract-arguments "a")))
  117. (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
  118. ;; Handle empty arguments.
  119. (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
  120. ;; Handle escaped commas and backslashes.
  121. (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
  122. (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
  123. (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
  124. (provide 'test-org-macro)
  125. ;;; test-org-macro.el ends here