Fix filling when point is at the very end of a paragraph
[org-mode.git] / testing / lisp / test-org.el
blob97474917215beb17dc414463767bcff4c1dd9710
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 ;; Special case: fill correctly a paragraph when point is at its
147 ;; very end.
148 (should
149 (equal "A B"
150 (org-test-with-temp-text "A\nB"
151 (let ((fill-column 20))
152 (goto-char (point-max))
153 (org-fill-paragraph)
154 (buffer-string)))))
155 ;; At a verse block, fill paragraph at point, also preserving line
156 ;; breaks. Though, do nothing when point is at the block
157 ;; boundaries.
158 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
159 (forward-line)
160 (let ((fill-column 20))
161 (org-fill-paragraph)
162 (should (equal (buffer-string)
163 "#+BEGIN_VERSE\nSome \\\\\nlong text\n#+END_VERSE"))))
164 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
165 (let ((fill-column 20))
166 (org-fill-paragraph)
167 (should (equal (buffer-string)
168 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
169 ;; Fill contents of `comment-block' elements.
170 (should
171 (equal
172 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
173 (let ((fill-column 20))
174 (forward-line)
175 (org-fill-paragraph)
176 (buffer-string)))
177 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
178 ;; Fill `comment' elements, indented or not.
179 (should
180 (equal "# A B"
181 (org-test-with-temp-text "# A\n#B"
182 (let ((fill-column 20))
183 (org-fill-paragraph)
184 (buffer-string)))))
185 (should
186 (equal " #+ A B"
187 (org-test-with-temp-text " #+ A\n #+ B"
188 (let ((fill-column 20))
189 (org-fill-paragraph)
190 (buffer-string)))))
191 ;; Do nothing at affiliated keywords.
192 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
193 (let ((fill-column 20))
194 (org-fill-paragraph)
195 (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
197 (ert-deftest test-org/auto-fill-function ()
198 "Test auto-filling features."
199 ;; Auto fill paragraph.
200 (should
201 (equal "12345\n7890"
202 (org-test-with-temp-text "12345 7890"
203 (let ((fill-column 5))
204 (end-of-line)
205 (org-auto-fill-function)
206 (buffer-string)))))
207 ;; Auto fill first paragraph in an item.
208 (should
209 (equal "- 12345\n 7890"
210 (org-test-with-temp-text "- 12345 7890"
211 (let ((fill-column 7))
212 (end-of-line)
213 (org-auto-fill-function)
214 (buffer-string)))))
215 ;; Auto fill comments, indented or not.
216 (should
217 (equal "# 12345\n# 7890"
218 (org-test-with-temp-text "# 12345 7890"
219 (let ((fill-column 7))
220 (end-of-line)
221 (org-auto-fill-function)
222 (buffer-string)))))
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))))))
330 (provide 'test-org)
332 ;;; test-org.el ends here