Merge branch 'maint'
[org-mode/org-tableheadings.git] / testing / lisp / test-org-src.el
blob86f08eccbc5334382a0602b4c8f956ba2ff2a891
1 ;;; test-org-src.el --- tests for org-src.el
3 ;; Copyright (C) 2012-2015 Le Wang
5 ;; Author: Le Wang <l26wang at gmail dot com>
7 ;; This file is not part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (require 'org-test)
28 (ert-deftest test-org-src/basic ()
29 "Editing regular block works, with point on source block."
30 (org-test-with-temp-text
32 <point>#+begin_src emacs-lisp
33 (message hello)
34 #+end_src
36 (let ((org-edit-src-content-indentation 2)
37 (org-src-preserve-indentation nil))
38 (org-edit-special)
39 (insert "blah")
40 (org-edit-src-exit)
41 (should (equal (buffer-string) "
42 #+begin_src emacs-lisp
43 blah(message hello)
44 #+end_src
45 "))
46 (should (looking-at-p "(message hello)")))))
48 (ert-deftest test-org-src/point-outside-block ()
49 "Editing with point before/after block signals expected error."
50 (org-test-with-temp-text
52 #+begin_src emacs-lisp
53 (message hello)
54 #+end_src
56 (goto-line 1)
57 (should-error (org-edit-special))
58 (goto-char (point-max))
59 (should-error (org-edit-special))))
61 (ert-deftest test-org-src/empty-block ()
62 "Editing empty block."
63 (org-test-with-temp-text
65 <point>#+begin_src emacs-lisp
66 #+end_src
68 (let ((org-edit-src-content-indentation 0)
69 (org-src-preserve-indentation nil))
70 (org-edit-special)
71 (insert "blah")
72 (org-edit-src-exit)
73 (should (equal (buffer-string) "
74 #+begin_src emacs-lisp
75 blah
76 #+end_src
77 "))
78 (should
79 (equal (buffer-substring (line-beginning-position) (point)) "blah")))))
81 (ert-deftest test-org-src/blank-line-block ()
82 "Editing block with just a blank line."
83 (org-test-with-temp-text-in-file
85 #+begin_src emacs-lisp
87 #+end_src
89 (let ((org-edit-src-content-indentation 2)
90 (org-src-preserve-indentation nil))
91 (goto-line 2)
92 (org-edit-special)
93 (insert "blah")
94 (org-edit-src-exit)
95 (should (equal (buffer-string) "
96 #+begin_src emacs-lisp
97 blah
98 #+end_src
99 ")))))
101 (ert-deftest test-org-src/preserve-tabs ()
102 "Editing block preserve tab characters."
103 ;; With `org-src-preserve-indentation' set to nil.
104 (should
105 (equal "
106 #+begin_src emacs-lisp
107 This is a tab:\t.
108 #+end_src"
109 (org-test-with-temp-text
111 #+begin_src emacs-lisp
112 <point>This is a tab:\t.
113 #+end_src"
114 (let ((org-edit-src-content-indentation 2)
115 (org-src-preserve-indentation nil))
116 (org-edit-special)
117 (org-edit-src-exit)
118 (buffer-string)))))
119 ;; With `org-src-preserve-indentation' set to t.
120 (should
121 (equal "
122 #+begin_src emacs-lisp
123 This is a tab:\t.
124 #+end_src"
125 (org-test-with-temp-text
127 #+begin_src emacs-lisp
128 <point>This is a tab:\t.
129 #+end_src"
130 (let ((org-edit-src-content-indentation 2)
131 (org-src-preserve-indentation t))
132 (org-edit-special)
133 (org-edit-src-exit)
134 (buffer-string))))))
136 (ert-deftest test-org-src/coderef-format ()
137 "Test `org-src-coderef-format' specifications."
138 ;; Regular tests in a src block, an example block and an edit
139 ;; buffer.
140 (should
141 (equal "foo"
142 (let ((org-coderef-label-format "foo"))
143 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
144 (org-src-coderef-format)))))
145 (should
146 (equal "foo"
147 (let ((org-coderef-label-format "foo"))
148 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n0\n#+END_EXAMPLE"
149 (org-src-coderef-format)))))
150 (should
151 (equal "foo"
152 (let ((org-coderef-label-format "foo") result)
153 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
154 (org-edit-special)
155 (setq result (org-src-coderef-format))
156 (org-edit-src-exit)
157 result))))
158 ;; When a local variable in the source buffer is available, use it.
159 (should
160 (equal "bar"
161 (let ((org-coderef-label-format "foo"))
162 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
163 (setq-local org-coderef-label-format "bar")
164 (org-src-coderef-format)))))
165 (should
166 (equal "bar"
167 (let ((org-coderef-label-format "foo") result)
168 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
169 (setq-local org-coderef-label-format "bar")
170 (org-edit-special)
171 (setq result (org-src-coderef-format))
172 (org-edit-src-exit)
173 result))))
174 ;; Use provided local format even if in an edit buffer.
175 (should
176 (equal "bar"
177 (let ((org-coderef-label-format "foo"))
178 (org-test-with-temp-text
179 "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
180 (org-src-coderef-format)))))
181 (should
182 (equal "bar"
183 (let ((org-coderef-label-format "foo") result)
184 (org-test-with-temp-text
185 "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
186 (org-edit-special)
187 (setq result (org-src-coderef-format))
188 (org-edit-src-exit)
189 result))))
190 ;; Local format has precedence over local variables.
191 (should
192 (equal "bar"
193 (let ((org-coderef-label-format "foo"))
194 (org-test-with-temp-text
195 "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
196 (setq-local org-coderef-label-format "foo")
197 (org-src-coderef-format)))))
198 (should
199 (equal "bar"
200 (let ((org-coderef-label-format "foo") result)
201 (org-test-with-temp-text
202 "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
203 (setq-local org-coderef-label-format "foo")
204 (org-edit-special)
205 (setq result (org-src-coderef-format))
206 (org-edit-src-exit)
207 result))))
208 ;; When optional argument provides a coderef format string, use it.
209 (should
210 (equal "bar"
211 (let ((org-coderef-label-format "foo")
212 (element (org-element-create 'src-block '(:label-fmt "bar"))))
213 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
214 (org-src-coderef-format element)))))
215 (should
216 (equal "baz"
217 (let ((org-coderef-label-format "foo")
218 (element (org-element-create 'src-block '(:label-fmt "baz"))))
219 (org-test-with-temp-text
220 "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
221 (setq-local org-coderef-label-format "foo")
222 (org-src-coderef-format element)))))
223 ;; If it doesn't provide any label format string, fall back to
224 ;; regular checks.
225 (should
226 (equal "foo"
227 (let ((org-coderef-label-format "foo")
228 (element (org-element-create 'src-block)))
229 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
230 (org-src-coderef-format element)))))
231 (should
232 (equal "bar"
233 (let ((org-coderef-label-format "foo")
234 (element (org-element-create 'src-block)))
235 (org-test-with-temp-text
236 "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
237 (setq-local org-coderef-label-format "foo")
238 (org-src-coderef-format element))))))
240 (ert-deftest test-org-src/coderef-regexp ()
241 "Test `org-src-coderef-regexp' specifications."
242 ;; Regular test.
243 (should
244 (string-match-p (org-src-coderef-regexp "; ref:%s")
245 "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC"))
246 ;; Ignore white space around the coderef.
247 (should
248 (string-match-p (org-src-coderef-regexp "; ref:%s")
249 "#+BEGIN_SRC emacs-lisp\n0 ; ref:label\n#+END_SRC"))
250 (should
251 (string-match-p (org-src-coderef-regexp "; ref:%s")
252 "#+BEGIN_SRC emacs-lisp\n0 ; ref:label \n#+END_SRC"))
253 ;; Only match regexp at the end of the line.
254 (should-not
255 (string-match-p (org-src-coderef-regexp "; ref:%s")
256 "#+BEGIN_SRC emacs-lisp\n0; ref:label (+ 1 2)\n#+END_SRC"))
257 ;; Do not match an empty label.
258 (should-not
259 (string-match-p (org-src-coderef-regexp "; ref:%s")
260 "#+BEGIN_SRC emacs-lisp\n0; ref:\n#+END_SRC"))
261 ;; When optional argument LABEL is provided, match given label only.
262 (should
263 (string-match-p (org-src-coderef-regexp "; ref:%s" "label")
264 "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC"))
265 (should-not
266 (string-match-p (org-src-coderef-regexp "; ref:%s" "label2")
267 "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC")))
269 (ert-deftest test-org-src/indented-blocks ()
270 "Test editing indented blocks."
271 ;; Editing a block should preserve its global indentation, unless
272 ;; `org-src-preserve-indentation' is non-nil.
273 (should
274 (equal
275 "- Item\n #+BEGIN_SRC emacs-lisp\n Foo\n #+END_SRC"
276 (org-test-with-temp-text
277 "- Item\n<point> #+BEGIN_SRC emacs-lisp\n (+ 1 1)\n #+END_SRC"
278 (let ((org-edit-src-content-indentation 2)
279 (org-src-preserve-indentation nil))
280 (org-edit-special)
281 (erase-buffer)
282 (insert "Foo")
283 (org-edit-src-exit)
284 (buffer-string)))))
285 (should
286 (equal
287 "- Item\n #+BEGIN_SRC emacs-lisp\n Foo\n #+END_SRC"
288 (org-test-with-temp-text
289 "- Item\n<point> #+BEGIN_SRC emacs-lisp\n (+ 1 1)\n #+END_SRC"
290 (let ((org-src-preserve-indentation t))
291 (org-edit-special)
292 (erase-buffer)
293 (insert " Foo")
294 (org-edit-src-exit)
295 (buffer-string)))))
296 ;; Global indentation obeys `indent-tabs-mode' from the original
297 ;; buffer.
298 (should
299 (string-match-p
300 "^\t+\s*argument2"
301 (org-test-with-temp-text
303 - Item
304 #+BEGIN_SRC emacs-lisp<point>
305 (progn
306 (function argument1
307 argument2))
308 #+END_SRC"
309 (setq-local indent-tabs-mode t)
310 (let ((org-edit-src-content-indentation 2)
311 (org-src-preserve-indentation nil))
312 (org-edit-special)
313 (org-edit-src-exit)
314 (buffer-string)))))
315 (should
316 (string-match-p
317 "^\s+argument2"
318 (org-test-with-temp-text
320 - Item
321 #+BEGIN_SRC emacs-lisp<point>
322 (progn\n (function argument1\n\t\targument2))
323 #+END_SRC"
324 (setq-local indent-tabs-mode nil)
325 (let ((org-edit-src-content-indentation 2)
326 (org-src-preserve-indentation nil))
327 (org-edit-special)
328 (org-edit-src-exit)
329 (buffer-string)))))
330 ;; Global indentation also obeys `tab-width' from original buffer.
331 (should
332 (string-match-p
333 "^\t\\{3\\}\s\\{2\\}argument2"
334 (org-test-with-temp-text
336 - Item
337 #+BEGIN_SRC emacs-lisp<point>
338 (progn
339 (function argument1
340 argument2))
341 #+END_SRC"
342 (setq-local indent-tabs-mode t)
343 (setq-local tab-width 4)
344 (let ((org-edit-src-content-indentation 0)
345 (org-src-preserve-indentation nil))
346 (org-edit-special)
347 (org-edit-src-exit)
348 (buffer-string)))))
349 (should
350 (string-match-p
351 "^\t\s\\{6\\}argument2"
352 (org-test-with-temp-text
354 - Item
355 #+BEGIN_SRC emacs-lisp<point>
356 (progn
357 (function argument1
358 argument2))
359 #+END_SRC"
360 (setq-local indent-tabs-mode t)
361 (setq-local tab-width 8)
362 (let ((org-edit-src-content-indentation 0)
363 (org-src-preserve-indentation nil))
364 (org-edit-special)
365 (org-edit-src-exit)
366 (buffer-string))))))
368 (ert-deftest test-org-src/footnote-references ()
369 "Test editing footnote references."
370 ;; Error when there is no definition to edit.
371 (should-error
372 (org-test-with-temp-text "A footnote<point>[fn:1]"
373 (org-edit-special)))
374 ;; Error when trying to edit an anonymous footnote.
375 (should-error
376 (org-test-with-temp-text "A footnote[fn::<point>edit me!]"
377 (org-edit-special)))
378 ;; Edit a regular definition.
379 (should
380 (equal "[fn:1] Definition"
381 (org-test-with-temp-text "A footnote<point>[fn:1]\n[fn:1] Definition"
382 (org-edit-special)
383 (prog1 (buffer-string) (org-edit-src-exit)))))
384 ;; Label should be protected against editing.
385 (should
386 (org-test-with-temp-text "A footnote<point>[fn:1]\n[fn:1] Definition"
387 (org-edit-special)
388 (prog1 (get-text-property 0 'read-only (buffer-string))
389 (org-edit-src-exit))))
390 (should
391 (org-test-with-temp-text "A footnote<point>[fn:1]\n[fn:1] Definition"
392 (org-edit-special)
393 (prog1 (get-text-property 5 'read-only (buffer-string))
394 (org-edit-src-exit))))
395 ;; Edit a regular definition.
396 (should
397 (equal
398 "A footnote[fn:1][fn:2]\n[fn:1] D1\n\n[fn:2] D2"
399 (org-test-with-temp-text
400 "A footnote<point>[fn:1][fn:2]\n[fn:1] D1\n\n[fn:2] D2"
401 (org-edit-special)
402 (org-edit-src-exit)
403 (buffer-string))))
404 ;; Edit an inline definition.
405 (should
406 (equal
407 "[fn:1:definition]"
408 (org-test-with-temp-text
409 "An inline<point>[fn:1] footnote[fn:1:definition]"
410 (org-edit-special)
411 (prog1 (buffer-string) (org-edit-src-exit)))))
412 ;; Label and closing square bracket should be protected against
413 ;; editing.
414 (should
415 (org-test-with-temp-text "An inline<point>[fn:1] footnote[fn:1:definition]"
416 (org-edit-special)
417 (prog1 (get-text-property 0 'read-only (buffer-string))
418 (org-edit-src-exit))))
419 (should
420 (org-test-with-temp-text "An inline<point>[fn:1] footnote[fn:1:definition]"
421 (org-edit-special)
422 (prog1 (get-text-property 5 'read-only (buffer-string))
423 (org-edit-src-exit))))
424 (should
425 (org-test-with-temp-text "An inline<point>[fn:1] footnote[fn:1:definition]"
426 (org-edit-special)
427 (prog1 (get-text-property 16 'read-only (buffer-string))
428 (org-edit-src-exit))))
429 ;; Do not include trailing white spaces when displaying the inline
430 ;; footnote definition.
431 (should
432 (equal
433 "[fn:1:definition]"
434 (org-test-with-temp-text
435 "An inline<point>[fn:1] footnote[fn:1:definition] and some text"
436 (org-edit-special)
437 (prog1 (buffer-string) (org-edit-src-exit)))))
438 ;; Preserve local variables when editing a footnote definition.
439 (should
440 (eq 'bar
441 (org-test-with-temp-text "A footnote<point>[fn:1]\n[fn:1] Definition"
442 (setq-local foo 'bar)
443 (org-edit-special)
444 (prog1 foo (org-edit-src-exit))))))
446 ;;; Code escaping
448 (ert-deftest test-org-src/escape-code-in-string ()
449 "Test `org-escape-code-in-string' specifications."
450 ;; Escape lines starting with "*" or "#+".
451 (should (equal ",*" (org-escape-code-in-string "*")))
452 (should (equal ",#+" (org-escape-code-in-string "#+")))
453 ;; Escape lines starting with ",*" and ",#+". Number of leading
454 ;; commas does not matter.
455 (should (equal ",,*" (org-escape-code-in-string ",*")))
456 (should (equal ",,#+" (org-escape-code-in-string ",#+")))
457 (should (equal ",,,*" (org-escape-code-in-string ",,*")))
458 (should (equal ",,,#+" (org-escape-code-in-string ",,#+")))
459 ;; Indentation does not matter.
460 (should (equal " ,*" (org-escape-code-in-string " *")))
461 (should (equal " ,#+" (org-escape-code-in-string " #+")))
462 ;; Do nothing on other cases.
463 (should (equal "a" (org-escape-code-in-string "a")))
464 (should (equal "#" (org-escape-code-in-string "#")))
465 (should (equal "," (org-escape-code-in-string ","))))
467 (ert-deftest test-org-src/unescape-code-in-string ()
468 "Test `org-unescape-code-in-string' specifications."
469 ;; Unescape lines starting with ",*" or ",#+". Number of leading
470 ;; commas does not matter.
471 (should (equal "*" (org-unescape-code-in-string ",*")))
472 (should (equal "#+" (org-unescape-code-in-string ",#+")))
473 (should (equal ",*" (org-unescape-code-in-string ",,*")))
474 (should (equal ",#+" (org-unescape-code-in-string ",,#+")))
475 ;; Indentation does not matter.
476 (should (equal " *" (org-unescape-code-in-string " ,*")))
477 (should (equal " #+" (org-unescape-code-in-string " ,#+")))
478 ;; Do nothing on other cases.
479 (should (equal "a" (org-unescape-code-in-string "a")))
480 (should (equal "#" (org-unescape-code-in-string "#")))
481 (should (equal "," (org-unescape-code-in-string ","))))
484 (provide 'test-org-src)
485 ;;; test-org-src.el ends here