test-org.el 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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. (ert-deftest test-org/insert-todo-heading-respect-content ()
  385. "Test `org-insert-todo-heading-respect-content' specifications."
  386. ;; Create a TODO heading.
  387. (should
  388. (org-test-with-temp-text "* H1\n Body"
  389. (org-insert-todo-heading-respect-content)
  390. (nth 2 (org-heading-components))))
  391. ;; Add headline at the end of the first subtree
  392. (should
  393. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  394. (search-forward "H1Body")
  395. (org-insert-todo-heading-respect-content)
  396. (and (eobp) (org-at-heading-p))))
  397. ;; In a list, do not create a new item.
  398. (should
  399. (org-test-with-temp-text "* H\n- an item\n- another one"
  400. (search-forward "an ")
  401. (org-insert-todo-heading-respect-content)
  402. (and (eobp) (org-at-heading-p)))))
  403. ;;; Links
  404. ;;;; Fuzzy Links
  405. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  406. ;; a named element (#+name: text) and to headlines (* Text).
  407. (ert-deftest test-org/fuzzy-links ()
  408. "Test fuzzy links specifications."
  409. ;; 1. Fuzzy link goes in priority to a matching target.
  410. (should
  411. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  412. (goto-line 5)
  413. (org-open-at-point)
  414. (looking-at "<<Test>>")))
  415. ;; 2. Then fuzzy link points to an element with a given name.
  416. (should
  417. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  418. (goto-line 5)
  419. (org-open-at-point)
  420. (looking-at "#\\+NAME: Test")))
  421. ;; 3. A target still lead to a matching headline otherwise.
  422. (should
  423. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  424. (goto-line 4)
  425. (org-open-at-point)
  426. (looking-at "\\* Head2")))
  427. ;; 4. With a leading star in link, enforce heading match.
  428. (should
  429. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  430. (goto-line 3)
  431. (org-open-at-point)
  432. (looking-at "\\* Test"))))
  433. ;;;; Link Escaping
  434. (ert-deftest test-org/org-link-escape-ascii-character ()
  435. "Escape an ascii character."
  436. (should
  437. (string=
  438. "%5B"
  439. (org-link-escape "["))))
  440. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  441. "Escape an ascii control character."
  442. (should
  443. (string=
  444. "%09"
  445. (org-link-escape "\t"))))
  446. (ert-deftest test-org/org-link-escape-multibyte-character ()
  447. "Escape an unicode multibyte character."
  448. (should
  449. (string=
  450. "%E2%82%AC"
  451. (org-link-escape "€"))))
  452. (ert-deftest test-org/org-link-escape-custom-table ()
  453. "Escape string with custom character table."
  454. (should
  455. (string=
  456. "Foo%3A%42ar%0A"
  457. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  458. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  459. "Escape string with custom table merged with default table."
  460. (should
  461. (string=
  462. "%5BF%6F%6F%3A%42ar%0A%5D"
  463. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  464. (ert-deftest test-org/org-link-unescape-ascii-character ()
  465. "Unescape an ascii character."
  466. (should
  467. (string=
  468. "["
  469. (org-link-unescape "%5B"))))
  470. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  471. "Unescpae an ascii control character."
  472. (should
  473. (string=
  474. "\n"
  475. (org-link-unescape "%0A"))))
  476. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  477. "Unescape unicode multibyte character."
  478. (should
  479. (string=
  480. "€"
  481. (org-link-unescape "%E2%82%AC"))))
  482. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  483. "Unescape old style percent escaped character."
  484. (should
  485. (string=
  486. "àâçèéêîôùû"
  487. (decode-coding-string
  488. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  489. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  490. "Escape and unescape a URL that includes an escaped char.
  491. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  492. (should
  493. (string=
  494. "http://some.host.com/form?&id=blah%2Bblah25"
  495. (org-link-unescape
  496. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  497. (ert-deftest test-org/org-link-escape-chars-browser ()
  498. "Escape a URL to pass to `browse-url'."
  499. (should
  500. (string=
  501. "http://some.host.com/search?q=%22Org%20mode%22"
  502. (org-link-escape "http://some.host.com/search?q=\"Org mode\""
  503. org-link-escape-chars-browser))))
  504. ;;; Node Properties
  505. (ert-deftest test-org/accumulated-properties-in-drawers ()
  506. "Ensure properties accumulate in subtree drawers."
  507. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  508. (org-babel-next-src-block)
  509. (should (equal '(2 1) (org-babel-execute-src-block)))))
  510. ;;; Mark Region
  511. (ert-deftest test-org/mark-subtree ()
  512. "Test `org-mark-subtree' specifications."
  513. ;; Error when point is before first headline.
  514. (should-error
  515. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  516. (progn (transient-mark-mode 1)
  517. (org-mark-subtree))))
  518. ;; Without argument, mark current subtree.
  519. (should
  520. (equal
  521. '(12 32)
  522. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  523. (progn (transient-mark-mode 1)
  524. (forward-line 2)
  525. (org-mark-subtree)
  526. (list (region-beginning) (region-end))))))
  527. ;; With an argument, move ARG up.
  528. (should
  529. (equal
  530. '(1 32)
  531. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  532. (progn (transient-mark-mode 1)
  533. (forward-line 2)
  534. (org-mark-subtree 1)
  535. (list (region-beginning) (region-end))))))
  536. ;; Do not get fooled by inlinetasks.
  537. (when (featurep 'org-inlinetask)
  538. (should
  539. (= 1
  540. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  541. (progn (transient-mark-mode 1)
  542. (forward-line 1)
  543. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  544. (region-beginning)))))))
  545. ;;; Navigation
  546. (ert-deftest test-org/beginning-of-line ()
  547. "Test `org-beginning-of-line' specifications."
  548. ;; Standard test.
  549. (should
  550. (org-test-with-temp-text "Some text\nSome other text"
  551. (progn (org-beginning-of-line) (bolp))))
  552. ;; Standard test with `visual-line-mode'.
  553. (should-not
  554. (org-test-with-temp-text "A long line of text\nSome other text"
  555. (progn (visual-line-mode)
  556. (forward-char 2)
  557. (dotimes (i 1000) (insert "very "))
  558. (org-beginning-of-line)
  559. (bolp))))
  560. ;; At an headline with special movement.
  561. (should
  562. (org-test-with-temp-text "* TODO Headline"
  563. (let ((org-special-ctrl-a/e t))
  564. (org-end-of-line)
  565. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  566. (progn (org-beginning-of-line) (bolp))
  567. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  568. (ert-deftest test-org/end-of-line ()
  569. "Test `org-end-of-line' specifications."
  570. ;; Standard test.
  571. (should
  572. (org-test-with-temp-text "Some text\nSome other text"
  573. (progn (org-end-of-line) (eolp))))
  574. ;; Standard test with `visual-line-mode'.
  575. (should-not
  576. (org-test-with-temp-text "A long line of text\nSome other text"
  577. (progn (visual-line-mode)
  578. (forward-char 2)
  579. (dotimes (i 1000) (insert "very "))
  580. (goto-char (point-min))
  581. (org-end-of-line)
  582. (eolp))))
  583. ;; At an headline with special movement.
  584. (should
  585. (org-test-with-temp-text "* Headline1 :tag:\n"
  586. (let ((org-special-ctrl-a/e t))
  587. (and (progn (org-end-of-line) (looking-at " :tag:"))
  588. (progn (org-end-of-line) (eolp))
  589. (progn (org-end-of-line) (looking-at " :tag:"))))))
  590. ;; At an headline without special movement.
  591. (should
  592. (org-test-with-temp-text "* Headline2 :tag:\n"
  593. (let ((org-special-ctrl-a/e nil))
  594. (and (progn (org-end-of-line) (eolp))
  595. (progn (org-end-of-line) (eolp))))))
  596. ;; At an headline, with reversed movement.
  597. (should
  598. (org-test-with-temp-text "* Headline3 :tag:\n"
  599. (let ((org-special-ctrl-a/e 'reversed)
  600. (this-command last-command))
  601. (and (progn (org-end-of-line) (eolp))
  602. (progn (org-end-of-line) (looking-at " :tag:"))))))
  603. ;; At a block without hidden contents.
  604. (should
  605. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  606. (progn (org-end-of-line) (eolp))))
  607. ;; At a block with hidden contents.
  608. (should-not
  609. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  610. (let ((org-special-ctrl-a/e t))
  611. (org-hide-block-toggle)
  612. (org-end-of-line)
  613. (eobp)))))
  614. (ert-deftest test-org/forward-paragraph ()
  615. "Test `org-forward-paragraph' specifications."
  616. ;; At end of buffer, return an error.
  617. (should-error
  618. (org-test-with-temp-text "Paragraph"
  619. (goto-char (point-max))
  620. (org-forward-paragraph)))
  621. ;; Standard test.
  622. (should
  623. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  624. (org-forward-paragraph)
  625. (looking-at "P2")))
  626. ;; Ignore depth.
  627. (should
  628. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  629. (org-forward-paragraph)
  630. (looking-at "P1")))
  631. ;; Do not enter elements with invisible contents.
  632. (should
  633. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  634. (org-hide-block-toggle)
  635. (org-forward-paragraph)
  636. (looking-at "P3")))
  637. ;; On an affiliated keyword, jump to the beginning of the element.
  638. (should
  639. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  640. (org-forward-paragraph)
  641. (looking-at "Para")))
  642. ;; On an item or a footnote definition, move to the second element
  643. ;; inside, if any.
  644. (should
  645. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  646. (org-forward-paragraph)
  647. (looking-at " Paragraph")))
  648. (should
  649. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  650. (org-forward-paragraph)
  651. (looking-at "Paragraph")))
  652. ;; On an item, or a footnote definition, when the first line is
  653. ;; empty, move to the first item.
  654. (should
  655. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  656. (org-forward-paragraph)
  657. (looking-at " Paragraph")))
  658. (should
  659. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  660. (org-forward-paragraph)
  661. (looking-at "Paragraph")))
  662. ;; On a table (resp. a property drawer) do not move through table
  663. ;; rows (resp. node properties).
  664. (should
  665. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  666. (org-forward-paragraph)
  667. (looking-at "Paragraph")))
  668. (should
  669. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
  670. (org-forward-paragraph)
  671. (looking-at "Paragraph")))
  672. ;; On a verse or source block, stop after blank lines.
  673. (should
  674. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  675. (org-forward-paragraph)
  676. (looking-at "L2")))
  677. (should
  678. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  679. (org-forward-paragraph)
  680. (looking-at "L2"))))
  681. (ert-deftest test-org/backward-paragraph ()
  682. "Test `org-backward-paragraph' specifications."
  683. ;; Error at beginning of buffer.
  684. (should-error
  685. (org-test-with-temp-text "Paragraph"
  686. (org-backward-paragraph)))
  687. ;; Regular test.
  688. (should
  689. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  690. (goto-char (point-max))
  691. (org-backward-paragraph)
  692. (looking-at "P3")))
  693. (should
  694. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  695. (goto-char (point-max))
  696. (beginning-of-line)
  697. (org-backward-paragraph)
  698. (looking-at "P2")))
  699. ;; Ignore depth.
  700. (should
  701. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  702. (goto-char (point-max))
  703. (beginning-of-line)
  704. (org-backward-paragraph)
  705. (looking-at "P2")))
  706. ;; Ignore invisible elements.
  707. (should
  708. (org-test-with-temp-text "* H1\n P1\n* H2"
  709. (org-cycle)
  710. (goto-char (point-max))
  711. (beginning-of-line)
  712. (org-backward-paragraph)
  713. (bobp)))
  714. ;; On an affiliated keyword, jump to the first one.
  715. (should
  716. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  717. (search-forward "c2")
  718. (org-backward-paragraph)
  719. (looking-at "#\\+name")))
  720. ;; On the second element in an item or a footnote definition, jump
  721. ;; to item or the definition.
  722. (should
  723. (org-test-with-temp-text "- line1\n\n line2"
  724. (goto-char (point-max))
  725. (beginning-of-line)
  726. (org-backward-paragraph)
  727. (looking-at "- line1")))
  728. (should
  729. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  730. (goto-char (point-max))
  731. (beginning-of-line)
  732. (org-backward-paragraph)
  733. (looking-at "\\[fn:1\\] line1")))
  734. ;; On a table (resp. a property drawer), ignore table rows
  735. ;; (resp. node properties).
  736. (should
  737. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  738. (goto-char (point-max))
  739. (beginning-of-line)
  740. (org-backward-paragraph)
  741. (bobp)))
  742. (should
  743. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
  744. (goto-char (point-max))
  745. (beginning-of-line)
  746. (org-backward-paragraph)
  747. (bobp)))
  748. ;; On a source or verse block, stop before blank lines.
  749. (should
  750. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  751. (search-forward "L3")
  752. (beginning-of-line)
  753. (org-backward-paragraph)
  754. (looking-at "L2")))
  755. (should
  756. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  757. (search-forward "L3")
  758. (beginning-of-line)
  759. (org-backward-paragraph)
  760. (looking-at "L2"))))
  761. (ert-deftest test-org/forward-element ()
  762. "Test `org-forward-element' specifications."
  763. ;; 1. At EOB: should error.
  764. (org-test-with-temp-text "Some text\n"
  765. (goto-char (point-max))
  766. (should-error (org-forward-element)))
  767. ;; 2. Standard move: expected to ignore blank lines.
  768. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  769. (org-forward-element)
  770. (should (looking-at (regexp-quote "Second paragraph."))))
  771. ;; 3. Headline tests.
  772. (org-test-with-temp-text "
  773. * Head 1
  774. ** Head 1.1
  775. *** Head 1.1.1
  776. ** Head 1.2"
  777. ;; 3.1. At an headline beginning: move to next headline at the
  778. ;; same level.
  779. (goto-line 3)
  780. (org-forward-element)
  781. (should (looking-at (regexp-quote "** Head 1.2")))
  782. ;; 3.2. At an headline beginning: move to parent headline if no
  783. ;; headline at the same level.
  784. (goto-line 3)
  785. (org-forward-element)
  786. (should (looking-at (regexp-quote "** Head 1.2"))))
  787. ;; 4. Greater element tests.
  788. (org-test-with-temp-text
  789. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  790. ;; 4.1. At a greater element: expected to skip contents.
  791. (org-forward-element)
  792. (should (looking-at (regexp-quote "Outside.")))
  793. ;; 4.2. At the end of greater element contents: expected to skip
  794. ;; to the end of the greater element.
  795. (goto-line 2)
  796. (org-forward-element)
  797. (should (looking-at (regexp-quote "Outside."))))
  798. ;; 5. List tests.
  799. (org-test-with-temp-text "
  800. - item1
  801. - sub1
  802. - sub2
  803. - sub3
  804. Inner paragraph.
  805. - item2
  806. Outside."
  807. ;; 5.1. At list top point: expected to move to the element after
  808. ;; the list.
  809. (goto-line 2)
  810. (org-forward-element)
  811. (should (looking-at (regexp-quote "Outside.")))
  812. ;; 5.2. Special case: at the first line of a sub-list, but not at
  813. ;; beginning of line, move to next item.
  814. (goto-line 2)
  815. (forward-char)
  816. (org-forward-element)
  817. (should (looking-at "- item2"))
  818. (goto-line 4)
  819. (forward-char)
  820. (org-forward-element)
  821. (should (looking-at " - sub2"))
  822. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  823. (goto-line 4)
  824. (org-forward-element)
  825. (should (looking-at (regexp-quote " Inner paragraph.")))
  826. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  827. (goto-line 8)
  828. (org-forward-element)
  829. (should (looking-at (regexp-quote " Inner paragraph.")))
  830. ;; 5.5. At an item: expected to move to next item, if any.
  831. (goto-line 6)
  832. (org-forward-element)
  833. (should (looking-at " - sub3"))))
  834. (ert-deftest test-org/backward-element ()
  835. "Test `org-backward-element' specifications."
  836. ;; 1. Should error at BOB.
  837. (org-test-with-temp-text " \nParagraph."
  838. (should-error (org-backward-element)))
  839. ;; 2. Should move at BOB when called on the first element in buffer.
  840. (should
  841. (org-test-with-temp-text "\n#+TITLE: test"
  842. (progn (forward-line)
  843. (org-backward-element)
  844. (bobp))))
  845. ;; 3. Not at the beginning of an element: move at its beginning.
  846. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  847. (goto-line 3)
  848. (end-of-line)
  849. (org-backward-element)
  850. (should (looking-at (regexp-quote "Paragraph2."))))
  851. ;; 4. Headline tests.
  852. (org-test-with-temp-text "
  853. * Head 1
  854. ** Head 1.1
  855. *** Head 1.1.1
  856. ** Head 1.2"
  857. ;; 4.1. At an headline beginning: move to previous headline at the
  858. ;; same level.
  859. (goto-line 5)
  860. (org-backward-element)
  861. (should (looking-at (regexp-quote "** Head 1.1")))
  862. ;; 4.2. At an headline beginning: move to parent headline if no
  863. ;; headline at the same level.
  864. (goto-line 3)
  865. (org-backward-element)
  866. (should (looking-at (regexp-quote "* Head 1")))
  867. ;; 4.3. At the first top-level headline: should error.
  868. (goto-line 2)
  869. (should-error (org-backward-element)))
  870. ;; 5. At beginning of first element inside a greater element:
  871. ;; expected to move to greater element's beginning.
  872. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  873. (goto-line 3)
  874. (org-backward-element)
  875. (should (looking-at "#\\+BEGIN_CENTER")))
  876. ;; 6. At the beginning of the first element in a section: should
  877. ;; move back to headline, if any.
  878. (should
  879. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  880. (progn (goto-char (point-max))
  881. (beginning-of-line)
  882. (org-backward-element)
  883. (org-at-heading-p))))
  884. ;; 7. List tests.
  885. (org-test-with-temp-text "
  886. - item1
  887. - sub1
  888. - sub2
  889. - sub3
  890. Inner paragraph.
  891. - item2
  892. Outside."
  893. ;; 7.1. At beginning of sub-list: expected to move to the
  894. ;; paragraph before it.
  895. (goto-line 4)
  896. (org-backward-element)
  897. (should (looking-at "item1"))
  898. ;; 7.2. At an item in a list: expected to move at previous item.
  899. (goto-line 8)
  900. (org-backward-element)
  901. (should (looking-at " - sub2"))
  902. (goto-line 12)
  903. (org-backward-element)
  904. (should (looking-at "- item1"))
  905. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  906. ;; beginning.
  907. (goto-line 10)
  908. (org-backward-element)
  909. (should (looking-at " - sub1"))
  910. (goto-line 15)
  911. (org-backward-element)
  912. (should (looking-at "- item1"))
  913. ;; 7.4. At blank-lines before list end: expected to move to top
  914. ;; item.
  915. (goto-line 14)
  916. (org-backward-element)
  917. (should (looking-at "- item1"))))
  918. (ert-deftest test-org/up-element ()
  919. "Test `org-up-element' specifications."
  920. ;; 1. At BOB or with no surrounding element: should error.
  921. (org-test-with-temp-text "Paragraph."
  922. (should-error (org-up-element)))
  923. (org-test-with-temp-text "* Head1\n* Head2"
  924. (goto-line 2)
  925. (should-error (org-up-element)))
  926. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  927. (goto-line 3)
  928. (should-error (org-up-element)))
  929. ;; 2. At an headline: move to parent headline.
  930. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  931. (goto-line 3)
  932. (org-up-element)
  933. (should (looking-at "\\* Head1")))
  934. ;; 3. Inside a greater element: move to greater element beginning.
  935. (org-test-with-temp-text
  936. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  937. (goto-line 3)
  938. (org-up-element)
  939. (should (looking-at "#\\+BEGIN_CENTER")))
  940. ;; 4. List tests.
  941. (org-test-with-temp-text "* Top
  942. - item1
  943. - sub1
  944. - sub2
  945. Paragraph within sub2.
  946. - item2"
  947. ;; 4.1. Within an item: move to the item beginning.
  948. (goto-line 8)
  949. (org-up-element)
  950. (should (looking-at " - sub2"))
  951. ;; 4.2. At an item in a sub-list: move to parent item.
  952. (goto-line 4)
  953. (org-up-element)
  954. (should (looking-at "- item1"))
  955. ;; 4.3. At an item in top list: move to beginning of whole list.
  956. (goto-line 10)
  957. (org-up-element)
  958. (should (looking-at "- item1"))
  959. ;; 4.4. Special case. At very top point: should move to parent of
  960. ;; list.
  961. (goto-line 2)
  962. (org-up-element)
  963. (should (looking-at "\\* Top"))))
  964. (ert-deftest test-org/down-element ()
  965. "Test `org-down-element' specifications."
  966. ;; Error when the element hasn't got a recursive type.
  967. (org-test-with-temp-text "Paragraph."
  968. (should-error (org-down-element)))
  969. ;; Error when the element has no contents
  970. (org-test-with-temp-text "* Headline"
  971. (should-error (org-down-element)))
  972. ;; When at a plain-list, move to first item.
  973. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  974. (goto-line 2)
  975. (org-down-element)
  976. (should (looking-at " - Item 1.1")))
  977. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  978. (org-down-element)
  979. (should (looking-at " Item 1")))
  980. ;; When at a table, move to first row
  981. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  982. (org-down-element)
  983. (should (looking-at " a | b |")))
  984. ;; Otherwise, move inside the greater element.
  985. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  986. (org-down-element)
  987. (should (looking-at "Paragraph"))))
  988. (ert-deftest test-org/drag-element-backward ()
  989. "Test `org-drag-element-backward' specifications."
  990. ;; 1. Error when trying to move first element of buffer.
  991. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  992. (should-error (org-drag-element-backward)))
  993. ;; 2. Error when trying to swap nested elements.
  994. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  995. (forward-line)
  996. (should-error (org-drag-element-backward)))
  997. ;; 3. Error when trying to swap an headline element and
  998. ;; a non-headline element.
  999. (org-test-with-temp-text "Test.\n* Head 1"
  1000. (forward-line)
  1001. (should-error (org-drag-element-backward)))
  1002. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1003. ;; between elements.
  1004. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  1005. (search-forward "graph")
  1006. (org-drag-element-backward)
  1007. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  1008. (should (looking-at " 2")))
  1009. ;; 5. Preserve visibility of elements and their contents.
  1010. (org-test-with-temp-text "
  1011. #+BEGIN_CENTER
  1012. Text.
  1013. #+END_CENTER
  1014. - item 1
  1015. #+BEGIN_QUOTE
  1016. Text.
  1017. #+END_QUOTE"
  1018. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1019. (search-backward "- item 1")
  1020. (org-drag-element-backward)
  1021. (should
  1022. (equal
  1023. '((63 . 82) (26 . 48))
  1024. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1025. (overlays-in (point-min) (point-max)))))))
  1026. (ert-deftest test-org/drag-element-forward ()
  1027. "Test `org-drag-element-forward' specifications."
  1028. ;; 1. Error when trying to move first element of buffer.
  1029. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1030. (goto-line 3)
  1031. (should-error (org-drag-element-forward)))
  1032. ;; 2. Error when trying to swap nested elements.
  1033. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1034. (forward-line)
  1035. (should-error (org-drag-element-forward)))
  1036. ;; 3. Error when trying to swap a non-headline element and an
  1037. ;; headline.
  1038. (org-test-with-temp-text "Test.\n* Head 1"
  1039. (should-error (org-drag-element-forward)))
  1040. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1041. ;; between elements.
  1042. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1043. (search-forward "graph")
  1044. (org-drag-element-forward)
  1045. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1046. (should (looking-at " 1")))
  1047. ;; 5. Preserve visibility of elements and their contents.
  1048. (org-test-with-temp-text "
  1049. #+BEGIN_CENTER
  1050. Text.
  1051. #+END_CENTER
  1052. - item 1
  1053. #+BEGIN_QUOTE
  1054. Text.
  1055. #+END_QUOTE"
  1056. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1057. (search-backward "#+BEGIN_CENTER")
  1058. (org-drag-element-forward)
  1059. (should
  1060. (equal
  1061. '((63 . 82) (26 . 48))
  1062. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1063. (overlays-in (point-min) (point-max)))))))
  1064. ;;; Planning
  1065. (ert-deftest test-org/timestamp-has-time-p ()
  1066. "Test `org-timestamp-has-time-p' specifications."
  1067. ;; With time.
  1068. (should
  1069. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1070. (org-timestamp-has-time-p (org-element-context))))
  1071. ;; Without time.
  1072. (should-not
  1073. (org-test-with-temp-text "<2012-03-29 Thu>"
  1074. (org-timestamp-has-time-p (org-element-context)))))
  1075. (ert-deftest test-org/timestamp-format ()
  1076. "Test `org-timestamp-format' specifications."
  1077. ;; Regular test.
  1078. (should
  1079. (equal
  1080. "2012-03-29 16:40"
  1081. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1082. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1083. ;; Range end.
  1084. (should
  1085. (equal
  1086. "2012-03-29"
  1087. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1088. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1089. (ert-deftest test-org/timestamp-split-range ()
  1090. "Test `org-timestamp-split-range' specifications."
  1091. ;; Extract range start (active).
  1092. (should
  1093. (equal '(2012 3 29)
  1094. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1095. (let ((ts (org-timestamp-split-range (org-element-context))))
  1096. (mapcar (lambda (p) (org-element-property p ts))
  1097. '(:year-end :month-end :day-end))))))
  1098. ;; Extract range start (inactive)
  1099. (should
  1100. (equal '(2012 3 29)
  1101. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1102. (let ((ts (org-timestamp-split-range (org-element-context))))
  1103. (mapcar (lambda (p) (org-element-property p ts))
  1104. '(:year-end :month-end :day-end))))))
  1105. ;; Extract range end (active).
  1106. (should
  1107. (equal '(2012 3 30)
  1108. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1109. (let ((ts (org-timestamp-split-range
  1110. (org-element-context) t)))
  1111. (mapcar (lambda (p) (org-element-property p ts))
  1112. '(:year-end :month-end :day-end))))))
  1113. ;; Extract range end (inactive)
  1114. (should
  1115. (equal '(2012 3 30)
  1116. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1117. (let ((ts (org-timestamp-split-range
  1118. (org-element-context) t)))
  1119. (mapcar (lambda (p) (org-element-property p ts))
  1120. '(:year-end :month-end :day-end))))))
  1121. ;; Return the timestamp if not a range.
  1122. (should
  1123. (org-test-with-temp-text "[2012-03-29 Thu]"
  1124. (let* ((ts-orig (org-element-context))
  1125. (ts-copy (org-timestamp-split-range ts-orig)))
  1126. (eq ts-orig ts-copy))))
  1127. (should
  1128. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1129. (let* ((ts-orig (org-element-context))
  1130. (ts-copy (org-timestamp-split-range ts-orig)))
  1131. (eq ts-orig ts-copy))))
  1132. ;; Check that parent is the same when a range was split.
  1133. (should
  1134. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1135. (let* ((ts-orig (org-element-context))
  1136. (ts-copy (org-timestamp-split-range ts-orig)))
  1137. (eq (org-element-property :parent ts-orig)
  1138. (org-element-property :parent ts-copy))))))
  1139. (ert-deftest test-org/timestamp-translate ()
  1140. "Test `org-timestamp-translate' specifications."
  1141. ;; Translate whole date range.
  1142. (should
  1143. (equal "<29>--<30>"
  1144. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1145. (let ((org-display-custom-times t)
  1146. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1147. (org-timestamp-translate (org-element-context))))))
  1148. ;; Translate date range start.
  1149. (should
  1150. (equal "<29>"
  1151. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1152. (let ((org-display-custom-times t)
  1153. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1154. (org-timestamp-translate (org-element-context) 'start)))))
  1155. ;; Translate date range end.
  1156. (should
  1157. (equal "<30>"
  1158. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1159. (let ((org-display-custom-times t)
  1160. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1161. (org-timestamp-translate (org-element-context) 'end)))))
  1162. ;; Translate time range.
  1163. (should
  1164. (equal "<08>--<16>"
  1165. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1166. (let ((org-display-custom-times t)
  1167. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1168. (org-timestamp-translate (org-element-context))))))
  1169. ;; Translate non-range timestamp.
  1170. (should
  1171. (equal "<29>"
  1172. (org-test-with-temp-text "<2012-03-29 Thu>"
  1173. (let ((org-display-custom-times t)
  1174. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1175. (org-timestamp-translate (org-element-context))))))
  1176. ;; Do not change `diary' timestamps.
  1177. (should
  1178. (equal "<%%(org-float t 4 2)>"
  1179. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1180. (let ((org-display-custom-times t)
  1181. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1182. (org-timestamp-translate (org-element-context)))))))
  1183. ;;; Targets and Radio Targets
  1184. (ert-deftest test-org/all-targets ()
  1185. "Test `org-all-targets' specifications."
  1186. ;; Without an argument.
  1187. (should
  1188. (equal '("radio-target" "target")
  1189. (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
  1190. (org-all-targets))))
  1191. (should
  1192. (equal '("radio-target")
  1193. (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
  1194. ;; With argument.
  1195. (should
  1196. (equal '("radio-target")
  1197. (org-test-with-temp-text "<<target>> <<<radio-target>>>"
  1198. (org-all-targets t)))))
  1199. (provide 'test-org)
  1200. ;;; test-org.el ends here