Do not make a special case for inlinetasks when marking a subtree
[org-mode.git] / testing / lisp / test-org.el
blobcb4852426d8fcb0c33c938cc744d3b8305f16318
1 ;;; test-org.el
3 ;; Copyright (c) ߚ David Maus
4 ;; Authors: David Maus
6 ;; Released under the GNU General Public License version 3
7 ;; see: http://www.gnu.org/licenses/gpl-3.0.html
9 ;;;; Comments:
11 ;; Template test file for Org-mode tests
13 ;;; Code:
14 (ert-deftest test-org/org-link-escape-ascii-character ()
15 "Escape an ascii character."
16 (should
17 (string=
18 "%5B"
19 (org-link-escape "["))))
21 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
22 "Escape an ascii control character."
23 (should
24 (string=
25 "%09"
26 (org-link-escape "\t"))))
28 (ert-deftest test-org/org-link-escape-multibyte-character ()
29 "Escape an unicode multibyte character."
30 (should
31 (string=
32 "%E2%82%AC"
33 (org-link-escape "€"))))
35 (ert-deftest test-org/org-link-escape-custom-table ()
36 "Escape string with custom character table."
37 (should
38 (string=
39 "Foo%3A%42ar%0A"
40 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
42 (ert-deftest test-org/org-link-escape-custom-table-merge ()
43 "Escape string with custom table merged with default table."
44 (should
45 (string=
46 "%5BF%6F%6F%3A%42ar%0A%5D"
47 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
49 (ert-deftest test-org/org-link-unescape-ascii-character ()
50 "Unescape an ascii character."
51 (should
52 (string=
53 "["
54 (org-link-unescape "%5B"))))
56 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
57 "Unescpae an ascii control character."
58 (should
59 (string=
60 "\n"
61 (org-link-unescape "%0A"))))
63 (ert-deftest test-org/org-link-unescape-multibyte-character ()
64 "Unescape unicode multibyte character."
65 (should
66 (string=
67 "€"
68 (org-link-unescape "%E2%82%AC"))))
70 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
71 "Unescape old style percent escaped character."
72 (should
73 (string=
74 "àâçèéêîôùû"
75 (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
77 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
78 "Escape and unscape a URL that includes an escaped char.
79 http://article.gmane.org/gmane.emacs.orgmode/21459/"
80 (should
81 (string=
82 "http://some.host.com/form?&id=blah%2Bblah25"
83 (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
85 (ert-deftest test-org/accumulated-properties-in-drawers ()
86 "Ensure properties accumulate in subtree drawers."
87 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
88 (org-babel-next-src-block)
89 (should (equal '(2 1) (org-babel-execute-src-block)))))
93 ;;; Links
95 ;;;; Fuzzy links
97 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
98 ;; a target keyword (aka an invisible target: #+TARGET: text), to
99 ;; a named element (#+name: text) and to headlines (* Text).
101 (ert-deftest test-org/fuzzy-links ()
102 "Test fuzzy links specifications."
103 ;; 1. Fuzzy link goes in priority to a matching target.
104 (org-test-with-temp-text
105 "#+TARGET: Test\n#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
106 (goto-line 6)
107 (org-open-at-point)
108 (should (looking-at "<<Test>>")))
109 ;; 2. Fuzzy link should then go to a matching target keyword.
110 (org-test-with-temp-text
111 "#+NAME: Test\n|a|b|\n#+TARGET: Test\n* Test\n[[Test]]"
112 (goto-line 5)
113 (org-open-at-point)
114 (should (looking-at "#\\+TARGET: Test")))
115 ;; 3. Then fuzzy link points to an element with a given name.
116 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
117 (goto-line 5)
118 (org-open-at-point)
119 (should (looking-at "#\\+NAME: Test")))
120 ;; 4. A target still lead to a matching headline otherwise.
121 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
122 (goto-line 4)
123 (org-open-at-point)
124 (should (looking-at "\\* Head2")))
125 ;; 5. With a leading star in link, enforce heading match.
126 (org-test-with-temp-text "#+TARGET: Test\n* Test\n<<Test>>\n[[*Test]]"
127 (goto-line 4)
128 (org-open-at-point)
129 (should (looking-at "\\* Test"))))
133 ;;; Filling
135 (ert-deftest test-org/fill-paragraph ()
136 "Test `org-fill-paragraph' specifications."
137 ;; At an Org table, align it.
138 (org-test-with-temp-text "|a|"
139 (org-fill-paragraph)
140 (should (equal (buffer-string) "| a |\n")))
141 ;; At a paragraph, preserve line breaks.
142 (org-test-with-temp-text "some \\\\\nlong\ntext"
143 (let ((fill-column 20))
144 (org-fill-paragraph)
145 (should (equal (buffer-string) "some \\\\\nlong text"))))
146 ;; Correctly fill a paragraph when point is at its very end.
147 (should
148 (equal "A B"
149 (org-test-with-temp-text "A\nB"
150 (let ((fill-column 20))
151 (goto-char (point-max))
152 (org-fill-paragraph)
153 (buffer-string)))))
154 ;; Special case: Fill first paragraph when point is at an item or
155 ;; a plain-list or a footnote reference.
156 (should
157 (equal "- A B"
158 (org-test-with-temp-text "- A\n B"
159 (let ((fill-column 20))
160 (org-fill-paragraph)
161 (buffer-string)))))
162 (should
163 (equal "[fn:1] A B"
164 (org-test-with-temp-text "[fn:1] A\nB"
165 (let ((fill-column 20))
166 (org-fill-paragraph)
167 (buffer-string)))))
168 ;; At a verse block, fill paragraph at point, also preserving line
169 ;; breaks. Though, do nothing when point is at the block
170 ;; boundaries.
171 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
172 (forward-line)
173 (let ((fill-column 20))
174 (org-fill-paragraph)
175 (should (equal (buffer-string)
176 "#+BEGIN_VERSE\nSome \\\\\nlong text\n#+END_VERSE"))))
177 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
178 (let ((fill-column 20))
179 (org-fill-paragraph)
180 (should (equal (buffer-string)
181 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
182 ;; Fill contents of `comment-block' elements.
183 (should
184 (equal
185 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
186 (let ((fill-column 20))
187 (forward-line)
188 (org-fill-paragraph)
189 (buffer-string)))
190 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
191 ;; Fill `comment' elements.
192 (should
193 (equal " # A B"
194 (org-test-with-temp-text " # A\n # B"
195 (let ((fill-column 20))
196 (org-fill-paragraph)
197 (buffer-string)))))
198 ;; Do nothing at affiliated keywords.
199 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
200 (let ((fill-column 20))
201 (org-fill-paragraph)
202 (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
204 (ert-deftest test-org/auto-fill-function ()
205 "Test auto-filling features."
206 ;; Auto fill paragraph.
207 (should
208 (equal "12345\n7890"
209 (org-test-with-temp-text "12345 7890"
210 (let ((fill-column 5))
211 (end-of-line)
212 (org-auto-fill-function)
213 (buffer-string)))))
214 ;; Auto fill first paragraph in an item.
215 (should
216 (equal "- 12345\n 7890"
217 (org-test-with-temp-text "- 12345 7890"
218 (let ((fill-column 7))
219 (end-of-line)
220 (org-auto-fill-function)
221 (buffer-string)))))
222 ;; Auto fill comments.
223 (should
224 (equal " # 12345\n # 7890"
225 (org-test-with-temp-text " # 12345 7890"
226 (let ((fill-column 10))
227 (end-of-line)
228 (org-auto-fill-function)
229 (buffer-string)))))
230 ;; Verse and comment block: auto fill contents.
231 (should
232 (equal "#+BEGIN_VERSE\n12345\n7890\n#+END_VERSE"
233 (org-test-with-temp-text "#+BEGIN_VERSE\n12345 7890\n#+END_VERSE"
234 (let ((fill-column 5))
235 (forward-line)
236 (end-of-line)
237 (org-auto-fill-function)
238 (buffer-string)))))
239 (should
240 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
241 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
242 (let ((fill-column 5))
243 (forward-line)
244 (end-of-line)
245 (org-auto-fill-function)
246 (buffer-string)))))
247 ;; Do not fill if a new item could be created.
248 (should-not
249 (equal "12345\n- 90"
250 (org-test-with-temp-text "12345 - 90"
251 (let ((fill-column 5))
252 (end-of-line)
253 (org-auto-fill-function)
254 (buffer-string)))))
255 ;; Do not fill if a line break could be introduced.
256 (should-not
257 (equal "123\\\\\n7890"
258 (org-test-with-temp-text "123\\\\ 7890"
259 (let ((fill-column 6))
260 (end-of-line)
261 (org-auto-fill-function)
262 (buffer-string)))))
263 ;; Do not fill affiliated keywords.
264 (should-not
265 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
266 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
267 (let ((fill-column 20))
268 (end-of-line)
269 (org-auto-fill-function)
270 (buffer-string))))))
274 ;;; Comments
276 (ert-deftest test-org/comment-dwim ()
277 "Test `comment-dwim' behaviour in an Org buffer."
278 ;; No region selected, no comment on current line and line not
279 ;; empty: insert comment on line above.
280 (should
281 (equal "# \nComment"
282 (org-test-with-temp-text "Comment"
283 (progn (call-interactively 'comment-dwim)
284 (buffer-string)))))
285 ;; No region selected, no comment on current line and line empty:
286 ;; insert comment on this line.
287 (should
288 (equal "# \nParagraph"
289 (org-test-with-temp-text "\nParagraph"
290 (progn (call-interactively 'comment-dwim)
291 (buffer-string)))))
292 ;; No region selected, and a comment on this line: indent it.
293 (should
294 (equal "* Headline\n # Comment"
295 (org-test-with-temp-text "* Headline\n# Comment"
296 (progn (forward-line)
297 (let ((org-adapt-indentation t))
298 (call-interactively 'comment-dwim))
299 (buffer-string)))))
300 ;; Also recognize single # at column 0 as comments.
301 (should
302 (equal "# Comment"
303 (org-test-with-temp-text "# Comment"
304 (progn (forward-line)
305 (call-interactively 'comment-dwim)
306 (buffer-string)))))
307 ;; Region selected and only comments and blank lines within it:
308 ;; un-comment all commented lines.
309 (should
310 (equal "Comment 1\n\nComment 2"
311 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
312 (progn
313 (transient-mark-mode 1)
314 (push-mark (point) t t)
315 (goto-char (point-max))
316 (call-interactively 'comment-dwim)
317 (buffer-string)))))
318 ;; Region selected without comments: comment all non-blank lines.
319 (should
320 (equal "# Comment 1\n\n# Comment 2"
321 (org-test-with-temp-text "Comment 1\n\nComment 2"
322 (progn
323 (transient-mark-mode 1)
324 (push-mark (point) t t)
325 (goto-char (point-max))
326 (call-interactively 'comment-dwim)
327 (buffer-string))))))
331 ;;; Mark region
333 (ert-deftest test-org/mark-subtree ()
334 "Test `org-mark-subtree' specifications."
335 ;; Error when point is before first headline.
336 (should-error
337 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
338 (progn (transient-mark-mode 1)
339 (org-mark-subtree))))
340 ;; Without argument, mark current subtree.
341 (should
342 (equal
343 '(12 32)
344 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
345 (progn (transient-mark-mode 1)
346 (forward-line 2)
347 (org-mark-subtree)
348 (list (region-beginning) (region-end))))))
349 ;; With an argument, move ARG up.
350 (should
351 (equal
352 '(1 32)
353 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
354 (progn (transient-mark-mode 1)
355 (forward-line 2)
356 (org-mark-subtree 1)
357 (list (region-beginning) (region-end)))))))
360 (provide 'test-org)
362 ;;; test-org.el ends here