test-ob-tangle.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ;;; test-ob-tangle.el --- tests for ob-tangle.el
  2. ;; Copyright (c) 2010-2016 Eric Schulte
  3. ;; Authors: Eric Schulte
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Comments:
  16. ;; Template test file for Org-mode tests
  17. ;;; Code:
  18. ;; TODO
  19. ;; (ert-deftest ob-tangle/noweb-on-tangle ()
  20. ;; "Noweb header arguments tangle correctly.
  21. ;; - yes expand on both export and tangle
  22. ;; - no expand on neither export or tangle
  23. ;; - tangle expand on only tangle not export"
  24. ;; (let ((target-file (make-temp-file "ob-tangle-test-")))
  25. ;; (org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7"
  26. ;; (org-narrow-to-subtree)
  27. ;; (org-babel-tangle target-file))
  28. ;; (let ((tang (with-temp-buffer
  29. ;; (insert-file-contents target-file)
  30. ;; (buffer-string))))
  31. ;; (flet ((exp-p (arg)
  32. ;; (and
  33. ;; (string-match
  34. ;; (format "noweb-%s-start\\([^\000]*\\)noweb-%s-end" arg arg)
  35. ;; tang)
  36. ;; (string-match "expanded" (match-string 1 tang)))))
  37. ;; (should (exp-p "yes"))
  38. ;; (should-not (exp-p "no"))
  39. ;; (should (exp-p "tangle"))))))
  40. (ert-deftest ob-tangle/no-excessive-id-insertion-on-tangle ()
  41. "Don't add IDs to headings without tangling code blocks."
  42. (org-test-at-id "ef06fd7f-012b-4fde-87a2-2ae91504ea7e"
  43. (org-babel-next-src-block)
  44. (org-narrow-to-subtree)
  45. (org-babel-tangle)
  46. (should (null (org-id-get)))))
  47. (ert-deftest ob-tangle/continued-code-blocks-w-noweb-ref ()
  48. "Test that the :noweb-ref header argument is used correctly."
  49. (org-test-at-id "54d68d4b-1544-4745-85ab-4f03b3cbd8a0"
  50. (let ((tangled
  51. "df|sed '1d'|awk '{print $5 \" \" $6}'|sort -n |tail -1|awk '{print $2}'"))
  52. (org-narrow-to-subtree)
  53. (org-babel-tangle)
  54. (with-temp-buffer
  55. (insert-file-contents "babel.sh")
  56. (goto-char (point-min))
  57. (should (re-search-forward (regexp-quote tangled) nil t)))
  58. (delete-file "babel.sh"))))
  59. (ert-deftest ob-tangle/expand-headers-as-noweb-references ()
  60. "Test that references to headers are expanded during noweb expansion."
  61. (org-test-at-id "2409e8ba-7b5f-4678-8888-e48aa02d8cb4"
  62. (org-babel-next-src-block 2)
  63. (let ((expanded (org-babel-expand-noweb-references)))
  64. (should (string-match (regexp-quote "simple") expanded))
  65. (should (string-match (regexp-quote "length 14") expanded)))))
  66. (ert-deftest ob-tangle/comment-links-at-left-margin ()
  67. "Test commenting of links at left margin."
  68. (should
  69. (string-match
  70. (regexp-quote "# [[http://orgmode.org][Org mode]]")
  71. (org-test-with-temp-text-in-file
  72. "[[http://orgmode.org][Org mode]]
  73. #+header: :comments org :tangle \"test-ob-tangle.sh\"
  74. #+begin_src sh
  75. echo 1
  76. #+end_src"
  77. (unwind-protect
  78. (progn (org-babel-tangle)
  79. (with-temp-buffer (insert-file-contents "test-ob-tangle.sh")
  80. (buffer-string)))
  81. (delete-file "test-ob-tangle.sh"))))))
  82. (ert-deftest ob-tangle/comment-links-numbering ()
  83. "Test numbering of source blocks when commenting with links."
  84. (should
  85. (org-test-with-temp-text-in-file
  86. "* H
  87. #+header: :tangle \"test-ob-tangle.el\" :comments link
  88. #+begin_src emacs-lisp
  89. 1
  90. #+end_src
  91. #+header: :tangle \"test-ob-tangle.el\" :comments link
  92. #+begin_src emacs-lisp
  93. 2
  94. #+end_src"
  95. (unwind-protect
  96. (progn
  97. (org-babel-tangle)
  98. (with-temp-buffer
  99. (insert-file-contents "test-ob-tangle.el")
  100. (buffer-string)
  101. (goto-char (point-min))
  102. (and (search-forward "[H:1]]" nil t)
  103. (search-forward "[H:2]]" nil t))))
  104. (delete-file "test-ob-tangle.el")))))
  105. (ert-deftest ob-tangle/jump-to-org ()
  106. "Test `org-babel-tangle-jump-to-org' specifications."
  107. ;; Standard test.
  108. (should
  109. (equal
  110. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  111. (org-test-with-temp-text-in-file
  112. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  113. (let ((file (buffer-file-name)))
  114. (org-test-with-temp-text
  115. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  116. (file-name-nondirectory file))
  117. (org-babel-tangle-jump-to-org)
  118. (buffer-string))))))
  119. ;; Multiple blocks in the same section.
  120. (should
  121. (equal
  122. "2"
  123. (org-test-with-temp-text-in-file
  124. "* H
  125. first block
  126. #+begin_src emacs-lisp
  127. 1
  128. #+end_src
  129. another block
  130. #+begin_src emacs-lisp
  131. 2
  132. #+end_src
  133. "
  134. (let ((file (buffer-file-name)))
  135. (org-test-with-temp-text
  136. (format ";; [[file:%s][H:2]]\n<point>2\n;; H:2 ends here\n"
  137. (file-name-nondirectory file))
  138. (org-babel-tangle-jump-to-org)
  139. (buffer-substring (line-beginning-position)
  140. (line-end-position)))))))
  141. ;; Preserve position within the source code.
  142. (should
  143. (equal
  144. "1)"
  145. (org-test-with-temp-text-in-file
  146. "* H\n#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src"
  147. (let ((file (buffer-file-name)))
  148. (org-test-with-temp-text
  149. (format ";; [[file:%s][H:1]]\n(+ 1 <point>1)\n;; H:1 ends here\n"
  150. (file-name-nondirectory file))
  151. (org-babel-tangle-jump-to-org)
  152. (buffer-substring-no-properties (point) (line-end-position)))))))
  153. ;; Blocks before first heading.
  154. (should
  155. (equal
  156. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  157. (org-test-with-temp-text-in-file
  158. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  159. (let ((file (buffer-file-name)))
  160. (org-test-with-temp-text
  161. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  162. (file-name-nondirectory file))
  163. (org-babel-tangle-jump-to-org)
  164. (buffer-string))))))
  165. ;; Special case: buffer starts with a source block.
  166. (should
  167. (equal
  168. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  169. (org-test-with-temp-text-in-file
  170. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  171. (let ((file (buffer-file-name)))
  172. (org-test-with-temp-text
  173. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  174. (file-name-nondirectory file))
  175. (org-babel-tangle-jump-to-org)
  176. (buffer-string)))))))
  177. (provide 'test-ob-tangle)
  178. ;;; test-ob-tangle.el ends here