test-org-macro.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. (ert-deftest test-org-macro/escape-arguments ()
  70. "Test `org-macro-escape-arguments' specifications."
  71. ;; Regular tests.
  72. (should (equal "a" (org-macro-escape-arguments "a")))
  73. (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
  74. ;; Handle empty arguments.
  75. (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
  76. ;; Properly escape commas and backslashes preceding them.
  77. (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
  78. (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
  79. (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
  80. (ert-deftest test-org-macro/extract-arguments ()
  81. "Test `org-macro-extract-arguments' specifications."
  82. ;; Regular tests.
  83. (should (equal '("a") (org-macro-extract-arguments "a")))
  84. (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
  85. ;; Handle empty arguments.
  86. (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
  87. ;; Handle escaped commas and backslashes.
  88. (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
  89. (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
  90. (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
  91. (provide 'test-org-macro)
  92. ;;; test-org-macro.el ends here