test-org.el 32 KB

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