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