test-org.el 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. ;;; test-org.el --- tests for org.el
  2. ;; Copyright (c) David Maus
  3. ;; Authors: David Maus
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Comments:
  16. ;; Template test file for Org-mode tests
  17. ;;; Code:
  18. ;;; Comments
  19. (ert-deftest test-org/comment-dwim ()
  20. "Test `comment-dwim' behaviour in an Org buffer."
  21. ;; No region selected, no comment on current line and line not
  22. ;; empty: insert comment on line above.
  23. (should
  24. (equal "# \nComment"
  25. (org-test-with-temp-text "Comment"
  26. (progn (call-interactively 'comment-dwim)
  27. (buffer-string)))))
  28. ;; No region selected, no comment on current line and line empty:
  29. ;; insert comment on this line.
  30. (should
  31. (equal "# \nParagraph"
  32. (org-test-with-temp-text "\nParagraph"
  33. (progn (call-interactively 'comment-dwim)
  34. (buffer-string)))))
  35. ;; No region selected, and a comment on this line: indent it.
  36. (should
  37. (equal "* Headline\n # Comment"
  38. (org-test-with-temp-text "* Headline\n# Comment"
  39. (progn (forward-line)
  40. (let ((org-adapt-indentation t))
  41. (call-interactively 'comment-dwim))
  42. (buffer-string)))))
  43. ;; Also recognize single # at column 0 as comments.
  44. (should
  45. (equal "# Comment"
  46. (org-test-with-temp-text "# Comment"
  47. (progn (forward-line)
  48. (call-interactively 'comment-dwim)
  49. (buffer-string)))))
  50. ;; Region selected and only comments and blank lines within it:
  51. ;; un-comment all commented lines.
  52. (should
  53. (equal "Comment 1\n\nComment 2"
  54. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  55. (progn
  56. (transient-mark-mode 1)
  57. (push-mark (point) t t)
  58. (goto-char (point-max))
  59. (call-interactively 'comment-dwim)
  60. (buffer-string)))))
  61. ;; Region selected without comments: comment all lines if
  62. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  63. (should
  64. (equal "# Comment 1\n\n# Comment 2"
  65. (org-test-with-temp-text "Comment 1\n\nComment 2"
  66. (progn
  67. (transient-mark-mode 1)
  68. (push-mark (point) t t)
  69. (goto-char (point-max))
  70. (let ((comment-empty-lines nil))
  71. (call-interactively 'comment-dwim))
  72. (buffer-string)))))
  73. (should
  74. (equal "# Comment 1\n# \n# Comment 2"
  75. (org-test-with-temp-text "Comment 1\n\nComment 2"
  76. (progn
  77. (transient-mark-mode 1)
  78. (push-mark (point) t t)
  79. (goto-char (point-max))
  80. (let ((comment-empty-lines t))
  81. (call-interactively 'comment-dwim))
  82. (buffer-string)))))
  83. ;; In front of a keyword without region, insert a new comment.
  84. (should
  85. (equal "# \n#+KEYWORD: value"
  86. (org-test-with-temp-text "#+KEYWORD: value"
  87. (progn (call-interactively 'comment-dwim)
  88. (buffer-string))))))
  89. ;;; Date and time analysis
  90. (ert-deftest test-org/org-read-date ()
  91. "Test `org-read-date' specifications."
  92. ;; Parse ISO date with abbreviated year and month.
  93. (should (equal "2012-03-29 16:40"
  94. (let ((org-time-was-given t))
  95. (org-read-date t nil "12-3-29 16:40"))))
  96. ;; Parse Europeans dates.
  97. (should (equal "2012-03-29 16:40"
  98. (let ((org-time-was-given t))
  99. (org-read-date t nil "29.03.2012 16:40"))))
  100. ;; Parse Europeans dates without year.
  101. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  102. (let ((org-time-was-given t))
  103. (org-read-date t nil "29.03. 16:40")))))
  104. (ert-deftest test-org/org-parse-time-string ()
  105. "Test `org-parse-time-string'."
  106. (should (equal (org-parse-time-string "2012-03-29 16:40")
  107. '(0 40 16 29 3 2012 nil nil nil)))
  108. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  109. '(0 40 16 29 3 2012 nil nil nil)))
  110. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  111. '(0 40 16 29 3 2012 nil nil nil)))
  112. (should (equal (org-parse-time-string "<2012-03-29>")
  113. '(0 0 0 29 3 2012 nil nil nil)))
  114. (should (equal (org-parse-time-string "<2012-03-29>" t)
  115. '(0 nil nil 29 3 2012 nil nil nil))))
  116. ;;; Filling
  117. (ert-deftest test-org/fill-paragraph ()
  118. "Test `org-fill-paragraph' specifications."
  119. ;; At an Org table, align it.
  120. (should
  121. (equal "| a |\n"
  122. (org-test-with-temp-text "|a|"
  123. (org-fill-paragraph)
  124. (buffer-string))))
  125. (should
  126. (equal "#+name: table\n| a |\n"
  127. (org-test-with-temp-text "#+name: table\n| a |"
  128. (org-fill-paragraph)
  129. (buffer-string))))
  130. ;; At a paragraph, preserve line breaks.
  131. (org-test-with-temp-text "some \\\\\nlong\ntext"
  132. (let ((fill-column 20))
  133. (org-fill-paragraph)
  134. (should (equal (buffer-string) "some \\\\\nlong text"))))
  135. ;; Correctly fill a paragraph when point is at its very end.
  136. (should
  137. (equal "A B"
  138. (org-test-with-temp-text "A\nB"
  139. (let ((fill-column 20))
  140. (goto-char (point-max))
  141. (org-fill-paragraph)
  142. (buffer-string)))))
  143. ;; Correctly fill the last paragraph of a greater element.
  144. (should
  145. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  146. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  147. (let ((fill-column 8))
  148. (forward-line)
  149. (end-of-line)
  150. (org-fill-paragraph)
  151. (buffer-string)))))
  152. ;; Correctly fill an element in a narrowed buffer.
  153. (should
  154. (equal "01234\n6"
  155. (org-test-with-temp-text "01234 6789"
  156. (let ((fill-column 5))
  157. (narrow-to-region 1 8)
  158. (org-fill-paragraph)
  159. (buffer-string)))))
  160. ;; Handle `adaptive-fill-regexp' in paragraphs.
  161. (should
  162. (equal "> a b"
  163. (org-test-with-temp-text "> a\n> b"
  164. (let ((fill-column 5)
  165. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  166. (org-fill-paragraph)
  167. (buffer-string)))))
  168. ;; Special case: Fill first paragraph when point is at an item or
  169. ;; a plain-list or a footnote reference.
  170. (should
  171. (equal "- A B"
  172. (org-test-with-temp-text "- A\n B"
  173. (let ((fill-column 20))
  174. (org-fill-paragraph)
  175. (buffer-string)))))
  176. (should
  177. (equal "[fn:1] A B"
  178. (org-test-with-temp-text "[fn:1] A\nB"
  179. (let ((fill-column 20))
  180. (org-fill-paragraph)
  181. (buffer-string)))))
  182. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  183. (let ((fill-column 20))
  184. (org-fill-paragraph)
  185. (should (equal (buffer-string)
  186. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  187. ;; Fill contents of `comment-block' elements.
  188. (should
  189. (equal
  190. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  191. (let ((fill-column 20))
  192. (forward-line)
  193. (org-fill-paragraph)
  194. (buffer-string)))
  195. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  196. ;; Fill `comment' elements.
  197. (should
  198. (equal " # A B"
  199. (org-test-with-temp-text " # A\n # B"
  200. (let ((fill-column 20))
  201. (org-fill-paragraph)
  202. (buffer-string)))))
  203. ;; Do not mix consecutive comments when filling one of them.
  204. (should
  205. (equal "# A B\n\n# C"
  206. (org-test-with-temp-text "# A\n# B\n\n# C"
  207. (let ((fill-column 20))
  208. (org-fill-paragraph)
  209. (buffer-string)))))
  210. ;; Use commented empty lines as separators when filling comments.
  211. (should
  212. (equal "# A B\n#\n# C"
  213. (org-test-with-temp-text "# A\n# B\n#\n# C"
  214. (let ((fill-column 20))
  215. (org-fill-paragraph)
  216. (buffer-string)))))
  217. ;; Handle `adaptive-fill-regexp' in comments.
  218. (should
  219. (equal "# > a b"
  220. (org-test-with-temp-text "# > a\n# > b"
  221. (let ((fill-column 20)
  222. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  223. (org-fill-paragraph)
  224. (buffer-string)))))
  225. ;; Do nothing at affiliated keywords.
  226. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  227. (let ((fill-column 20))
  228. (org-fill-paragraph)
  229. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  230. ;; Do not move point after table when filling a table.
  231. (should-not
  232. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  233. (forward-char)
  234. (org-fill-paragraph)
  235. (eobp))))
  236. (ert-deftest test-org/auto-fill-function ()
  237. "Test auto-filling features."
  238. ;; Auto fill paragraph.
  239. (should
  240. (equal "12345\n7890"
  241. (org-test-with-temp-text "12345 7890"
  242. (let ((fill-column 5))
  243. (end-of-line)
  244. (org-auto-fill-function)
  245. (buffer-string)))))
  246. ;; Auto fill first paragraph in an item.
  247. (should
  248. (equal "- 12345\n 7890"
  249. (org-test-with-temp-text "- 12345 7890"
  250. (let ((fill-column 7))
  251. (end-of-line)
  252. (org-auto-fill-function)
  253. (buffer-string)))))
  254. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  255. (should
  256. (equal "> 12345\n 7890"
  257. (org-test-with-temp-text "> 12345 7890"
  258. (let ((fill-column 10)
  259. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  260. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  261. (end-of-line)
  262. (org-auto-fill-function)
  263. (buffer-string)))))
  264. (should
  265. (equal "> 12345\n> 12345\n> 7890"
  266. (org-test-with-temp-text "> 12345\n> 12345 7890"
  267. (let ((fill-column 10)
  268. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  269. (goto-char (point-max))
  270. (org-auto-fill-function)
  271. (buffer-string)))))
  272. (should-not
  273. (equal " 12345\n *12345\n *12345"
  274. (org-test-with-temp-text " 12345\n *12345 12345"
  275. (let ((fill-column 10)
  276. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  277. (goto-char (point-max))
  278. (org-auto-fill-function)
  279. (buffer-string)))))
  280. ;; Auto fill comments.
  281. (should
  282. (equal " # 12345\n # 7890"
  283. (org-test-with-temp-text " # 12345 7890"
  284. (let ((fill-column 10))
  285. (end-of-line)
  286. (org-auto-fill-function)
  287. (buffer-string)))))
  288. ;; A hash within a line isn't a comment.
  289. (should-not
  290. (equal "12345 # 7890\n# 1"
  291. (org-test-with-temp-text "12345 # 7890 1"
  292. (let ((fill-column 12))
  293. (end-of-line)
  294. (org-auto-fill-function)
  295. (buffer-string)))))
  296. ;; Correctly interpret empty prefix.
  297. (should-not
  298. (equal "# a\n# b\nRegular\n# paragraph"
  299. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  300. (let ((fill-column 12))
  301. (end-of-line 3)
  302. (org-auto-fill-function)
  303. (buffer-string)))))
  304. ;; Comment block: auto fill contents.
  305. (should
  306. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  307. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  308. (let ((fill-column 5))
  309. (forward-line)
  310. (end-of-line)
  311. (org-auto-fill-function)
  312. (buffer-string)))))
  313. (should
  314. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  315. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  316. (let ((fill-column 5))
  317. (forward-line)
  318. (end-of-line)
  319. (org-auto-fill-function)
  320. (buffer-string)))))
  321. ;; Do not fill if a new item could be created.
  322. (should-not
  323. (equal "12345\n- 90"
  324. (org-test-with-temp-text "12345 - 90"
  325. (let ((fill-column 5))
  326. (end-of-line)
  327. (org-auto-fill-function)
  328. (buffer-string)))))
  329. ;; Do not fill if a line break could be introduced.
  330. (should-not
  331. (equal "123\\\\\n7890"
  332. (org-test-with-temp-text "123\\\\ 7890"
  333. (let ((fill-column 6))
  334. (end-of-line)
  335. (org-auto-fill-function)
  336. (buffer-string)))))
  337. ;; Do not fill affiliated keywords.
  338. (should-not
  339. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  340. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  341. (let ((fill-column 20))
  342. (end-of-line)
  343. (org-auto-fill-function)
  344. (buffer-string))))))
  345. ;;; Editing
  346. ;;;; Insert elements
  347. (ert-deftest test-org/meta-return ()
  348. "Test M-RET (`org-meta-return')."
  349. ;; In a table field insert a row above.
  350. (should
  351. (org-test-with-temp-text "| a |"
  352. (forward-char)
  353. (org-meta-return)
  354. (forward-line -1)
  355. (looking-at "| |$")))
  356. ;; In a paragraph change current line into a header.
  357. (should
  358. (org-test-with-temp-text "a"
  359. (org-meta-return)
  360. (beginning-of-line)
  361. (looking-at "\* a$")))
  362. ;; In an item insert an item, in this case above.
  363. (should
  364. (org-test-with-temp-text "- a"
  365. (org-meta-return)
  366. (beginning-of-line)
  367. (looking-at "- $")))
  368. ;; In a drawer and paragraph insert an empty line, in this case above.
  369. (should
  370. (let ((org-drawers '("MYDRAWER")))
  371. (org-test-with-temp-text ":MYDRAWER:\na\n:END:"
  372. (forward-line)
  373. (org-meta-return)
  374. (forward-line -1)
  375. (looking-at "$"))))
  376. ;; In a drawer and item insert an item, in this case above.
  377. (should
  378. (let ((org-drawers '("MYDRAWER")))
  379. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  380. (forward-line)
  381. (org-meta-return)
  382. (beginning-of-line)
  383. (looking-at "- $")))))
  384. ;;; Links
  385. ;;;; Fuzzy Links
  386. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  387. ;; a named element (#+name: text) and to headlines (* Text).
  388. (ert-deftest test-org/fuzzy-links ()
  389. "Test fuzzy links specifications."
  390. ;; 1. Fuzzy link goes in priority to a matching target.
  391. (should
  392. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  393. (goto-line 5)
  394. (org-open-at-point)
  395. (looking-at "<<Test>>")))
  396. ;; 2. Then fuzzy link points to an element with a given name.
  397. (should
  398. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  399. (goto-line 5)
  400. (org-open-at-point)
  401. (looking-at "#\\+NAME: Test")))
  402. ;; 3. A target still lead to a matching headline otherwise.
  403. (should
  404. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  405. (goto-line 4)
  406. (org-open-at-point)
  407. (looking-at "\\* Head2")))
  408. ;; 4. With a leading star in link, enforce heading match.
  409. (should
  410. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  411. (goto-line 3)
  412. (org-open-at-point)
  413. (looking-at "\\* Test"))))
  414. ;;;; Link Escaping
  415. (ert-deftest test-org/org-link-escape-ascii-character ()
  416. "Escape an ascii character."
  417. (should
  418. (string=
  419. "%5B"
  420. (org-link-escape "["))))
  421. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  422. "Escape an ascii control character."
  423. (should
  424. (string=
  425. "%09"
  426. (org-link-escape "\t"))))
  427. (ert-deftest test-org/org-link-escape-multibyte-character ()
  428. "Escape an unicode multibyte character."
  429. (should
  430. (string=
  431. "%E2%82%AC"
  432. (org-link-escape "€"))))
  433. (ert-deftest test-org/org-link-escape-custom-table ()
  434. "Escape string with custom character table."
  435. (should
  436. (string=
  437. "Foo%3A%42ar%0A"
  438. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  439. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  440. "Escape string with custom table merged with default table."
  441. (should
  442. (string=
  443. "%5BF%6F%6F%3A%42ar%0A%5D"
  444. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  445. (ert-deftest test-org/org-link-unescape-ascii-character ()
  446. "Unescape an ascii character."
  447. (should
  448. (string=
  449. "["
  450. (org-link-unescape "%5B"))))
  451. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  452. "Unescpae an ascii control character."
  453. (should
  454. (string=
  455. "\n"
  456. (org-link-unescape "%0A"))))
  457. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  458. "Unescape unicode multibyte character."
  459. (should
  460. (string=
  461. "€"
  462. (org-link-unescape "%E2%82%AC"))))
  463. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  464. "Unescape old style percent escaped character."
  465. (should
  466. (string=
  467. "àâçèéêîôùû"
  468. (decode-coding-string
  469. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  470. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  471. "Escape and unescape a URL that includes an escaped char.
  472. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  473. (should
  474. (string=
  475. "http://some.host.com/form?&id=blah%2Bblah25"
  476. (org-link-unescape
  477. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  478. (ert-deftest test-org/org-link-escape-chars-browser ()
  479. "Escape a URL to pass to `browse-url'."
  480. (should
  481. (string=
  482. "http://some.host.com/search?q=%22Org%20mode%22"
  483. (org-link-escape "http://some.host.com/search?q=\"Org mode\""
  484. org-link-escape-chars-browser))))
  485. ;;; Node Properties
  486. (ert-deftest test-org/accumulated-properties-in-drawers ()
  487. "Ensure properties accumulate in subtree drawers."
  488. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  489. (org-babel-next-src-block)
  490. (should (equal '(2 1) (org-babel-execute-src-block)))))
  491. ;;; Mark Region
  492. (ert-deftest test-org/mark-subtree ()
  493. "Test `org-mark-subtree' specifications."
  494. ;; Error when point is before first headline.
  495. (should-error
  496. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  497. (progn (transient-mark-mode 1)
  498. (org-mark-subtree))))
  499. ;; Without argument, mark current subtree.
  500. (should
  501. (equal
  502. '(12 32)
  503. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  504. (progn (transient-mark-mode 1)
  505. (forward-line 2)
  506. (org-mark-subtree)
  507. (list (region-beginning) (region-end))))))
  508. ;; With an argument, move ARG up.
  509. (should
  510. (equal
  511. '(1 32)
  512. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  513. (progn (transient-mark-mode 1)
  514. (forward-line 2)
  515. (org-mark-subtree 1)
  516. (list (region-beginning) (region-end))))))
  517. ;; Do not get fooled by inlinetasks.
  518. (when (featurep 'org-inlinetask)
  519. (should
  520. (= 1
  521. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  522. (progn (transient-mark-mode 1)
  523. (forward-line 1)
  524. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  525. (region-beginning)))))))
  526. ;;; Navigation
  527. (ert-deftest test-org/beginning-of-line ()
  528. "Test `org-beginning-of-line' specifications."
  529. ;; Standard test.
  530. (should
  531. (org-test-with-temp-text "Some text\nSome other text"
  532. (progn (org-beginning-of-line) (bolp))))
  533. ;; Standard test with `visual-line-mode'.
  534. (should-not
  535. (org-test-with-temp-text "A long line of text\nSome other text"
  536. (progn (visual-line-mode)
  537. (forward-char 2)
  538. (dotimes (i 1000) (insert "very "))
  539. (org-beginning-of-line)
  540. (bolp))))
  541. ;; At an headline with special movement.
  542. (should
  543. (org-test-with-temp-text "* TODO Headline"
  544. (let ((org-special-ctrl-a/e t))
  545. (org-end-of-line)
  546. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  547. (progn (org-beginning-of-line) (bolp))
  548. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  549. (ert-deftest test-org/end-of-line ()
  550. "Test `org-end-of-line' specifications."
  551. ;; Standard test.
  552. (should
  553. (org-test-with-temp-text "Some text\nSome other text"
  554. (progn (org-end-of-line) (eolp))))
  555. ;; Standard test with `visual-line-mode'.
  556. (should-not
  557. (org-test-with-temp-text "A long line of text\nSome other text"
  558. (progn (visual-line-mode)
  559. (forward-char 2)
  560. (dotimes (i 1000) (insert "very "))
  561. (goto-char (point-min))
  562. (org-end-of-line)
  563. (eolp))))
  564. ;; At an headline with special movement.
  565. (should
  566. (org-test-with-temp-text "* Headline1 :tag:\n"
  567. (let ((org-special-ctrl-a/e t))
  568. (and (progn (org-end-of-line) (looking-at " :tag:"))
  569. (progn (org-end-of-line) (eolp))
  570. (progn (org-end-of-line) (looking-at " :tag:"))))))
  571. ;; At an headline without special movement.
  572. (should
  573. (org-test-with-temp-text "* Headline2 :tag:\n"
  574. (let ((org-special-ctrl-a/e nil))
  575. (and (progn (org-end-of-line) (eolp))
  576. (progn (org-end-of-line) (eolp))))))
  577. ;; At an headline, with reversed movement.
  578. (should
  579. (org-test-with-temp-text "* Headline3 :tag:\n"
  580. (let ((org-special-ctrl-a/e 'reversed)
  581. (this-command last-command))
  582. (and (progn (org-end-of-line) (eolp))
  583. (progn (org-end-of-line) (looking-at " :tag:"))))))
  584. ;; At a block without hidden contents.
  585. (should
  586. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  587. (progn (org-end-of-line) (eolp))))
  588. ;; At a block with hidden contents.
  589. (should-not
  590. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  591. (let ((org-special-ctrl-a/e t))
  592. (org-hide-block-toggle)
  593. (org-end-of-line)
  594. (eobp)))))
  595. (ert-deftest test-org/forward-element ()
  596. "Test `org-forward-element' specifications."
  597. ;; 1. At EOB: should error.
  598. (org-test-with-temp-text "Some text\n"
  599. (goto-char (point-max))
  600. (should-error (org-forward-element)))
  601. ;; 2. Standard move: expected to ignore blank lines.
  602. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  603. (org-forward-element)
  604. (should (looking-at (regexp-quote "Second paragraph."))))
  605. ;; 3. Headline tests.
  606. (org-test-with-temp-text "
  607. * Head 1
  608. ** Head 1.1
  609. *** Head 1.1.1
  610. ** Head 1.2"
  611. ;; 3.1. At an headline beginning: move to next headline at the
  612. ;; same level.
  613. (goto-line 3)
  614. (org-forward-element)
  615. (should (looking-at (regexp-quote "** Head 1.2")))
  616. ;; 3.2. At an headline beginning: move to parent headline if no
  617. ;; headline at the same level.
  618. (goto-line 3)
  619. (org-forward-element)
  620. (should (looking-at (regexp-quote "** Head 1.2"))))
  621. ;; 4. Greater element tests.
  622. (org-test-with-temp-text
  623. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  624. ;; 4.1. At a greater element: expected to skip contents.
  625. (org-forward-element)
  626. (should (looking-at (regexp-quote "Outside.")))
  627. ;; 4.2. At the end of greater element contents: expected to skip
  628. ;; to the end of the greater element.
  629. (goto-line 2)
  630. (org-forward-element)
  631. (should (looking-at (regexp-quote "Outside."))))
  632. ;; 5. List tests.
  633. (org-test-with-temp-text "
  634. - item1
  635. - sub1
  636. - sub2
  637. - sub3
  638. Inner paragraph.
  639. - item2
  640. Outside."
  641. ;; 5.1. At list top point: expected to move to the element after
  642. ;; the list.
  643. (goto-line 2)
  644. (org-forward-element)
  645. (should (looking-at (regexp-quote "Outside.")))
  646. ;; 5.2. Special case: at the first line of a sub-list, but not at
  647. ;; beginning of line, move to next item.
  648. (goto-line 2)
  649. (forward-char)
  650. (org-forward-element)
  651. (should (looking-at "- item2"))
  652. (goto-line 4)
  653. (forward-char)
  654. (org-forward-element)
  655. (should (looking-at " - sub2"))
  656. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  657. (goto-line 4)
  658. (org-forward-element)
  659. (should (looking-at (regexp-quote " Inner paragraph.")))
  660. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  661. (goto-line 8)
  662. (org-forward-element)
  663. (should (looking-at (regexp-quote " Inner paragraph.")))
  664. ;; 5.5. At an item: expected to move to next item, if any.
  665. (goto-line 6)
  666. (org-forward-element)
  667. (should (looking-at " - sub3"))))
  668. (ert-deftest test-org/backward-element ()
  669. "Test `org-backward-element' specifications."
  670. ;; 1. Should error at BOB.
  671. (org-test-with-temp-text " \nParagraph."
  672. (should-error (org-backward-element)))
  673. ;; 2. Should move at BOB when called on the first element in buffer.
  674. (should
  675. (org-test-with-temp-text "\n#+TITLE: test"
  676. (progn (forward-line)
  677. (org-backward-element)
  678. (bobp))))
  679. ;; 3. Not at the beginning of an element: move at its beginning.
  680. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  681. (goto-line 3)
  682. (end-of-line)
  683. (org-backward-element)
  684. (should (looking-at (regexp-quote "Paragraph2."))))
  685. ;; 4. Headline tests.
  686. (org-test-with-temp-text "
  687. * Head 1
  688. ** Head 1.1
  689. *** Head 1.1.1
  690. ** Head 1.2"
  691. ;; 4.1. At an headline beginning: move to previous headline at the
  692. ;; same level.
  693. (goto-line 5)
  694. (org-backward-element)
  695. (should (looking-at (regexp-quote "** Head 1.1")))
  696. ;; 4.2. At an headline beginning: move to parent headline if no
  697. ;; headline at the same level.
  698. (goto-line 3)
  699. (org-backward-element)
  700. (should (looking-at (regexp-quote "* Head 1")))
  701. ;; 4.3. At the first top-level headline: should error.
  702. (goto-line 2)
  703. (should-error (org-backward-element)))
  704. ;; 5. At beginning of first element inside a greater element:
  705. ;; expected to move to greater element's beginning.
  706. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  707. (goto-line 3)
  708. (org-backward-element)
  709. (should (looking-at "#\\+BEGIN_CENTER")))
  710. ;; 6. At the beginning of the first element in a section: should
  711. ;; move back to headline, if any.
  712. (should
  713. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  714. (progn (goto-char (point-max))
  715. (beginning-of-line)
  716. (org-backward-element)
  717. (org-at-heading-p))))
  718. ;; 7. List tests.
  719. (org-test-with-temp-text "
  720. - item1
  721. - sub1
  722. - sub2
  723. - sub3
  724. Inner paragraph.
  725. - item2
  726. Outside."
  727. ;; 7.1. At beginning of sub-list: expected to move to the
  728. ;; paragraph before it.
  729. (goto-line 4)
  730. (org-backward-element)
  731. (should (looking-at "item1"))
  732. ;; 7.2. At an item in a list: expected to move at previous item.
  733. (goto-line 8)
  734. (org-backward-element)
  735. (should (looking-at " - sub2"))
  736. (goto-line 12)
  737. (org-backward-element)
  738. (should (looking-at "- item1"))
  739. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  740. ;; beginning.
  741. (goto-line 10)
  742. (org-backward-element)
  743. (should (looking-at " - sub1"))
  744. (goto-line 15)
  745. (org-backward-element)
  746. (should (looking-at "- item1"))
  747. ;; 7.4. At blank-lines before list end: expected to move to top
  748. ;; item.
  749. (goto-line 14)
  750. (org-backward-element)
  751. (should (looking-at "- item1"))))
  752. (ert-deftest test-org/up-element ()
  753. "Test `org-up-element' specifications."
  754. ;; 1. At BOB or with no surrounding element: should error.
  755. (org-test-with-temp-text "Paragraph."
  756. (should-error (org-up-element)))
  757. (org-test-with-temp-text "* Head1\n* Head2"
  758. (goto-line 2)
  759. (should-error (org-up-element)))
  760. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  761. (goto-line 3)
  762. (should-error (org-up-element)))
  763. ;; 2. At an headline: move to parent headline.
  764. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  765. (goto-line 3)
  766. (org-up-element)
  767. (should (looking-at "\\* Head1")))
  768. ;; 3. Inside a greater element: move to greater element beginning.
  769. (org-test-with-temp-text
  770. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  771. (goto-line 3)
  772. (org-up-element)
  773. (should (looking-at "#\\+BEGIN_CENTER")))
  774. ;; 4. List tests.
  775. (org-test-with-temp-text "* Top
  776. - item1
  777. - sub1
  778. - sub2
  779. Paragraph within sub2.
  780. - item2"
  781. ;; 4.1. Within an item: move to the item beginning.
  782. (goto-line 8)
  783. (org-up-element)
  784. (should (looking-at " - sub2"))
  785. ;; 4.2. At an item in a sub-list: move to parent item.
  786. (goto-line 4)
  787. (org-up-element)
  788. (should (looking-at "- item1"))
  789. ;; 4.3. At an item in top list: move to beginning of whole list.
  790. (goto-line 10)
  791. (org-up-element)
  792. (should (looking-at "- item1"))
  793. ;; 4.4. Special case. At very top point: should move to parent of
  794. ;; list.
  795. (goto-line 2)
  796. (org-up-element)
  797. (should (looking-at "\\* Top"))))
  798. (ert-deftest test-org/down-element ()
  799. "Test `org-down-element' specifications."
  800. ;; Error when the element hasn't got a recursive type.
  801. (org-test-with-temp-text "Paragraph."
  802. (should-error (org-down-element)))
  803. ;; Error when the element has no contents
  804. (org-test-with-temp-text "* Headline"
  805. (should-error (org-down-element)))
  806. ;; When at a plain-list, move to first item.
  807. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  808. (goto-line 2)
  809. (org-down-element)
  810. (should (looking-at " - Item 1.1")))
  811. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  812. (org-down-element)
  813. (should (looking-at " Item 1")))
  814. ;; When at a table, move to first row
  815. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  816. (org-down-element)
  817. (should (looking-at " a | b |")))
  818. ;; Otherwise, move inside the greater element.
  819. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  820. (org-down-element)
  821. (should (looking-at "Paragraph"))))
  822. (ert-deftest test-org/drag-element-backward ()
  823. "Test `org-drag-element-backward' specifications."
  824. ;; 1. Error when trying to move first element of buffer.
  825. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  826. (should-error (org-drag-element-backward)))
  827. ;; 2. Error when trying to swap nested elements.
  828. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  829. (forward-line)
  830. (should-error (org-drag-element-backward)))
  831. ;; 3. Error when trying to swap an headline element and
  832. ;; a non-headline element.
  833. (org-test-with-temp-text "Test.\n* Head 1"
  834. (forward-line)
  835. (should-error (org-drag-element-backward)))
  836. ;; 4. Otherwise, swap elements, preserving column and blank lines
  837. ;; between elements.
  838. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  839. (search-forward "graph")
  840. (org-drag-element-backward)
  841. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  842. (should (looking-at " 2")))
  843. ;; 5. Preserve visibility of elements and their contents.
  844. (org-test-with-temp-text "
  845. #+BEGIN_CENTER
  846. Text.
  847. #+END_CENTER
  848. - item 1
  849. #+BEGIN_QUOTE
  850. Text.
  851. #+END_QUOTE"
  852. (while (search-forward "BEGIN_" nil t) (org-cycle))
  853. (search-backward "- item 1")
  854. (org-drag-element-backward)
  855. (should
  856. (equal
  857. '((63 . 82) (26 . 48))
  858. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  859. (overlays-in (point-min) (point-max)))))))
  860. (ert-deftest test-org/drag-element-forward ()
  861. "Test `org-drag-element-forward' specifications."
  862. ;; 1. Error when trying to move first element of buffer.
  863. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  864. (goto-line 3)
  865. (should-error (org-drag-element-forward)))
  866. ;; 2. Error when trying to swap nested elements.
  867. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  868. (forward-line)
  869. (should-error (org-drag-element-forward)))
  870. ;; 3. Error when trying to swap a non-headline element and an
  871. ;; headline.
  872. (org-test-with-temp-text "Test.\n* Head 1"
  873. (should-error (org-drag-element-forward)))
  874. ;; 4. Otherwise, swap elements, preserving column and blank lines
  875. ;; between elements.
  876. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  877. (search-forward "graph")
  878. (org-drag-element-forward)
  879. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  880. (should (looking-at " 1")))
  881. ;; 5. Preserve visibility of elements and their contents.
  882. (org-test-with-temp-text "
  883. #+BEGIN_CENTER
  884. Text.
  885. #+END_CENTER
  886. - item 1
  887. #+BEGIN_QUOTE
  888. Text.
  889. #+END_QUOTE"
  890. (while (search-forward "BEGIN_" nil t) (org-cycle))
  891. (search-backward "#+BEGIN_CENTER")
  892. (org-drag-element-forward)
  893. (should
  894. (equal
  895. '((63 . 82) (26 . 48))
  896. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  897. (overlays-in (point-min) (point-max)))))))
  898. ;;; Planning
  899. (ert-deftest test-org/timestamp-has-time-p ()
  900. "Test `org-timestamp-has-time-p' specifications."
  901. ;; With time.
  902. (should
  903. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  904. (org-timestamp-has-time-p (org-element-context))))
  905. ;; Without time.
  906. (should-not
  907. (org-test-with-temp-text "<2012-03-29 Thu>"
  908. (org-timestamp-has-time-p (org-element-context)))))
  909. (ert-deftest test-org/timestamp-format ()
  910. "Test `org-timestamp-format' specifications."
  911. ;; Regular test.
  912. (should
  913. (equal
  914. "2012-03-29 16:40"
  915. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  916. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  917. ;; Range end.
  918. (should
  919. (equal
  920. "2012-03-29"
  921. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  922. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  923. (ert-deftest test-org/timestamp-split-range ()
  924. "Test `org-timestamp-split-range' specifications."
  925. ;; Extract range start (active).
  926. (should
  927. (equal '(2012 3 29)
  928. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  929. (let ((ts (org-timestamp-split-range (org-element-context))))
  930. (mapcar (lambda (p) (org-element-property p ts))
  931. '(:year-end :month-end :day-end))))))
  932. ;; Extract range start (inactive)
  933. (should
  934. (equal '(2012 3 29)
  935. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  936. (let ((ts (org-timestamp-split-range (org-element-context))))
  937. (mapcar (lambda (p) (org-element-property p ts))
  938. '(:year-end :month-end :day-end))))))
  939. ;; Extract range end (active).
  940. (should
  941. (equal '(2012 3 30)
  942. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  943. (let ((ts (org-timestamp-split-range
  944. (org-element-context) t)))
  945. (mapcar (lambda (p) (org-element-property p ts))
  946. '(:year-end :month-end :day-end))))))
  947. ;; Extract range end (inactive)
  948. (should
  949. (equal '(2012 3 30)
  950. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  951. (let ((ts (org-timestamp-split-range
  952. (org-element-context) t)))
  953. (mapcar (lambda (p) (org-element-property p ts))
  954. '(:year-end :month-end :day-end))))))
  955. ;; Return the timestamp if not a range.
  956. (should
  957. (org-test-with-temp-text "[2012-03-29 Thu]"
  958. (let* ((ts-orig (org-element-context))
  959. (ts-copy (org-timestamp-split-range ts-orig)))
  960. (eq ts-orig ts-copy))))
  961. (should
  962. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  963. (let* ((ts-orig (org-element-context))
  964. (ts-copy (org-timestamp-split-range ts-orig)))
  965. (eq ts-orig ts-copy))))
  966. ;; Check that parent is the same when a range was split.
  967. (should
  968. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  969. (let* ((ts-orig (org-element-context))
  970. (ts-copy (org-timestamp-split-range ts-orig)))
  971. (eq (org-element-property :parent ts-orig)
  972. (org-element-property :parent ts-copy))))))
  973. (ert-deftest test-org/timestamp-translate ()
  974. "Test `org-timestamp-translate' specifications."
  975. ;; Translate whole date range.
  976. (should
  977. (equal "<29>--<30>"
  978. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  979. (let ((org-display-custom-times t)
  980. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  981. (org-timestamp-translate (org-element-context))))))
  982. ;; Translate date range start.
  983. (should
  984. (equal "<29>"
  985. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  986. (let ((org-display-custom-times t)
  987. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  988. (org-timestamp-translate (org-element-context) 'start)))))
  989. ;; Translate date range end.
  990. (should
  991. (equal "<30>"
  992. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  993. (let ((org-display-custom-times t)
  994. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  995. (org-timestamp-translate (org-element-context) 'end)))))
  996. ;; Translate time range.
  997. (should
  998. (equal "<08>--<16>"
  999. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1000. (let ((org-display-custom-times t)
  1001. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1002. (org-timestamp-translate (org-element-context))))))
  1003. ;; Translate non-range timestamp.
  1004. (should
  1005. (equal "<29>"
  1006. (org-test-with-temp-text "<2012-03-29 Thu>"
  1007. (let ((org-display-custom-times t)
  1008. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1009. (org-timestamp-translate (org-element-context))))))
  1010. ;; Do not change `diary' timestamps.
  1011. (should
  1012. (equal "<%%(org-float t 4 2)>"
  1013. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1014. (let ((org-display-custom-times t)
  1015. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1016. (org-timestamp-translate (org-element-context)))))))
  1017. ;;; Targets and Radio Targets
  1018. (ert-deftest test-org/all-targets ()
  1019. "Test `org-all-targets' specifications."
  1020. ;; Without an argument.
  1021. (should
  1022. (equal '("radio-target" "target")
  1023. (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
  1024. (org-all-targets))))
  1025. (should
  1026. (equal '("radio-target")
  1027. (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
  1028. ;; With argument.
  1029. (should
  1030. (equal '("radio-target")
  1031. (org-test-with-temp-text "<<target>> <<<radio-target>>>"
  1032. (org-all-targets t)))))
  1033. (provide 'test-org)
  1034. ;;; test-org.el ends here