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/>.
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
36 (let ((org-edit-src-content-indentation 2)
37 (org-src-preserve-indentation nil
))
41 (should (equal (buffer-string) "
42 #+begin_src emacs-lisp
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
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
68 (let ((org-edit-src-content-indentation 0)
69 (org-src-preserve-indentation nil
))
73 (should (equal (buffer-string) "
74 #+begin_src emacs-lisp
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
89 (let ((org-edit-src-content-indentation 2)
90 (org-src-preserve-indentation nil
))
95 (should (equal (buffer-string) "
96 #+begin_src emacs-lisp
101 (ert-deftest test-org-src
/preserve-tabs
()
102 "Editing block preserve tab characters."
103 ;; With `org-src-preserve-indentation' set to nil.
106 #+begin_src emacs-lisp
109 (org-test-with-temp-text
111 #+begin_src emacs-lisp
112 <point>This is a tab:\t.
114 (let ((org-edit-src-content-indentation 2)
115 (org-src-preserve-indentation nil
))
119 ;; With `org-src-preserve-indentation' set to t.
122 #+begin_src emacs-lisp
125 (org-test-with-temp-text
127 #+begin_src emacs-lisp
128 <point>This is a tab:\t.
130 (let ((org-edit-src-content-indentation 2)
131 (org-src-preserve-indentation t
))
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
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)))))
147 (let ((org-coderef-label-format "foo"))
148 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n0\n#+END_EXAMPLE"
149 (org-src-coderef-format)))))
152 (let ((org-coderef-label-format "foo") result
)
153 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
155 (setq result
(org-src-coderef-format))
158 ;; When a local variable in the source buffer is available, use it.
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)))))
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")
171 (setq result
(org-src-coderef-format))
174 ;; Use provided local format even if in an edit buffer.
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)))))
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"
187 (setq result
(org-src-coderef-format))
190 ;; Local format has precedence over local variables.
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)))))
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")
205 (setq result
(org-src-coderef-format))
208 ;; When optional argument provides a coderef format string, use it.
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
)))))
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
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
)))))
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."
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.
248 (string-match-p (org-src-coderef-regexp "; ref:%s")
249 "#+BEGIN_SRC emacs-lisp\n0 ; ref:label\n#+END_SRC"))
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.
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.
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.
263 (string-match-p (org-src-coderef-regexp "; ref:%s" "label")
264 "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC"))
266 (string-match-p (org-src-coderef-regexp "; ref:%s" "label2")
267 "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC")))
269 (provide 'test-org-src
)
270 ;;; test-org-src.el ends here