test-org.el 32 KB

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