test-ob-tangle.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 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. (should (unwind-protect
  55. (with-temp-buffer
  56. (insert-file-contents "babel.sh")
  57. (goto-char (point-min))
  58. (re-search-forward (regexp-quote tangled) nil t))
  59. (when (file-exists-p "babel.sh") (delete-file "babel.sh")))))))
  60. (ert-deftest ob-tangle/expand-headers-as-noweb-references ()
  61. "Test that references to headers are expanded during noweb expansion."
  62. (org-test-at-id "2409e8ba-7b5f-4678-8888-e48aa02d8cb4"
  63. (org-babel-next-src-block 2)
  64. (let ((expanded (org-babel-expand-noweb-references)))
  65. (should (string-match (regexp-quote "simple") expanded))
  66. (should (string-match (regexp-quote "length 14") expanded)))))
  67. (ert-deftest ob-tangle/comment-links-at-left-margin ()
  68. "Test commenting of links at left margin."
  69. (should
  70. (string-match
  71. (regexp-quote "# [[http://orgmode.org][Org mode]]")
  72. (org-test-with-temp-text-in-file
  73. "[[http://orgmode.org][Org mode]]
  74. #+header: :comments org :tangle \"test-ob-tangle.sh\"
  75. #+begin_src sh
  76. echo 1
  77. #+end_src"
  78. (unwind-protect
  79. (progn (org-babel-tangle)
  80. (with-temp-buffer (insert-file-contents "test-ob-tangle.sh")
  81. (buffer-string)))
  82. (delete-file "test-ob-tangle.sh"))))))
  83. (ert-deftest ob-tangle/comment-links-numbering ()
  84. "Test numbering of source blocks when commenting with links."
  85. (should
  86. (org-test-with-temp-text-in-file
  87. "* H
  88. #+header: :tangle \"test-ob-tangle.el\" :comments link
  89. #+begin_src emacs-lisp
  90. 1
  91. #+end_src
  92. #+header: :tangle \"test-ob-tangle.el\" :comments link
  93. #+begin_src emacs-lisp
  94. 2
  95. #+end_src"
  96. (unwind-protect
  97. (progn
  98. (org-babel-tangle)
  99. (with-temp-buffer
  100. (insert-file-contents "test-ob-tangle.el")
  101. (buffer-string)
  102. (goto-char (point-min))
  103. (and (search-forward "[H:1]]" nil t)
  104. (search-forward "[H:2]]" nil t))))
  105. (delete-file "test-ob-tangle.el")))))
  106. (ert-deftest ob-tangle/jump-to-org ()
  107. "Test `org-babel-tangle-jump-to-org' specifications."
  108. ;; Standard test.
  109. (should
  110. (equal
  111. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  112. (org-test-with-temp-text-in-file
  113. "* H\n#+begin_src emacs-lisp\n1\n#+end_src"
  114. (let ((file (buffer-file-name)))
  115. (org-test-with-temp-text
  116. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  117. (file-name-nondirectory file))
  118. (org-babel-tangle-jump-to-org)
  119. (buffer-string))))))
  120. ;; Multiple blocks in the same section.
  121. (should
  122. (equal
  123. "2"
  124. (org-test-with-temp-text-in-file
  125. "* H
  126. first block
  127. #+begin_src emacs-lisp
  128. 1
  129. #+end_src
  130. another block
  131. #+begin_src emacs-lisp
  132. 2
  133. #+end_src
  134. "
  135. (let ((file (buffer-file-name)))
  136. (org-test-with-temp-text
  137. (format ";; [[file:%s][H:2]]\n<point>2\n;; H:2 ends here\n"
  138. (file-name-nondirectory file))
  139. (org-babel-tangle-jump-to-org)
  140. (buffer-substring (line-beginning-position)
  141. (line-end-position)))))))
  142. ;; Preserve position within the source code.
  143. (should
  144. (equal
  145. "1)"
  146. (org-test-with-temp-text-in-file
  147. "* H\n#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src"
  148. (let ((file (buffer-file-name)))
  149. (org-test-with-temp-text
  150. (format ";; [[file:%s][H:1]]\n(+ 1 <point>1)\n;; H:1 ends here\n"
  151. (file-name-nondirectory file))
  152. (org-babel-tangle-jump-to-org)
  153. (buffer-substring-no-properties (point) (line-end-position)))))))
  154. ;; Blocks before first heading.
  155. (should
  156. (equal
  157. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  158. (org-test-with-temp-text-in-file
  159. "Buffer start\n#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  160. (let ((file (buffer-file-name)))
  161. (org-test-with-temp-text
  162. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  163. (file-name-nondirectory file))
  164. (org-babel-tangle-jump-to-org)
  165. (buffer-string))))))
  166. ;; Special case: buffer starts with a source block.
  167. (should
  168. (equal
  169. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  170. (org-test-with-temp-text-in-file
  171. "#+begin_src emacs-lisp\n1\n#+end_src\n* H"
  172. (let ((file (buffer-file-name)))
  173. (org-test-with-temp-text
  174. (format ";; [[file:%s][H:1]]\n<point>1\n;; H:1 ends here\n"
  175. (file-name-nondirectory file))
  176. (org-babel-tangle-jump-to-org)
  177. (buffer-string)))))))
  178. (ert-deftest ob-tangle/nested-block ()
  179. "Test tangling of org file with nested block."
  180. (should
  181. (string=
  182. "#+begin_src org
  183. ,#+begin_src emacs-lisp
  184. 1
  185. ,#+end_src
  186. #+end_src
  187. "
  188. (org-test-with-temp-text-in-file
  189. "#+header: :tangle \"test-ob-tangle.org\"
  190. #+begin_src org
  191. ,#+begin_src org
  192. ,,#+begin_src emacs-lisp
  193. 1
  194. ,,#+end_src
  195. ,#+end_src
  196. #+end_src"
  197. (unwind-protect
  198. (progn (org-babel-tangle)
  199. (with-temp-buffer (insert-file-contents "test-ob-tangle.org")
  200. (buffer-string)))
  201. (delete-file "test-ob-tangle.org"))))))
  202. (provide 'test-ob-tangle)
  203. ;;; test-ob-tangle.el ends here