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