test-org.el 11 KB

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