test-org.el 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  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/toggle-comment ()
  20. "Test `org-toggle-comment' specifications."
  21. ;; Simple headline.
  22. (should
  23. (equal "* Test"
  24. (org-test-with-temp-text "* COMMENT Test"
  25. (org-toggle-comment)
  26. (buffer-string))))
  27. (should
  28. (equal "* COMMENT Test"
  29. (org-test-with-temp-text "* Test"
  30. (org-toggle-comment)
  31. (buffer-string))))
  32. ;; Headline with a regular keyword.
  33. (should
  34. (equal "* TODO Test"
  35. (org-test-with-temp-text "* TODO COMMENT Test"
  36. (org-toggle-comment)
  37. (buffer-string))))
  38. (should
  39. (equal "* TODO COMMENT Test"
  40. (org-test-with-temp-text "* TODO Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. ;; Empty headline.
  44. (should
  45. (equal "* "
  46. (org-test-with-temp-text "* COMMENT"
  47. (org-toggle-comment)
  48. (buffer-string))))
  49. (should
  50. (equal "* COMMENT"
  51. (org-test-with-temp-text "* "
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. ;; Headline with a single keyword.
  55. (should
  56. (equal "* TODO "
  57. (org-test-with-temp-text "* TODO COMMENT"
  58. (org-toggle-comment)
  59. (buffer-string))))
  60. (should
  61. (equal "* TODO COMMENT"
  62. (org-test-with-temp-text "* TODO"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. ;; Headline with a keyword, a priority cookie and contents.
  66. (should
  67. (equal "* TODO [#A] Headline"
  68. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  69. (org-toggle-comment)
  70. (buffer-string))))
  71. (should
  72. (equal "* TODO [#A] COMMENT Headline"
  73. (org-test-with-temp-text "* TODO [#A] Headline"
  74. (org-toggle-comment)
  75. (buffer-string)))))
  76. (ert-deftest test-org/comment-dwim ()
  77. "Test `comment-dwim' behaviour in an Org buffer."
  78. ;; No region selected, no comment on current line and line not
  79. ;; empty: insert comment on line above.
  80. (should
  81. (equal "# \nComment"
  82. (org-test-with-temp-text "Comment"
  83. (progn (call-interactively 'comment-dwim)
  84. (buffer-string)))))
  85. ;; No region selected, no comment on current line and line empty:
  86. ;; insert comment on this line.
  87. (should
  88. (equal "# \nParagraph"
  89. (org-test-with-temp-text "\nParagraph"
  90. (progn (call-interactively 'comment-dwim)
  91. (buffer-string)))))
  92. ;; No region selected, and a comment on this line: indent it.
  93. (should
  94. (equal "* Headline\n # Comment"
  95. (org-test-with-temp-text "* Headline\n# Comment"
  96. (progn (forward-line)
  97. (let ((org-adapt-indentation t))
  98. (call-interactively 'comment-dwim))
  99. (buffer-string)))))
  100. ;; Also recognize single # at column 0 as comments.
  101. (should
  102. (equal "# Comment"
  103. (org-test-with-temp-text "# Comment"
  104. (progn (forward-line)
  105. (call-interactively 'comment-dwim)
  106. (buffer-string)))))
  107. ;; Region selected and only comments and blank lines within it:
  108. ;; un-comment all commented lines.
  109. (should
  110. (equal "Comment 1\n\nComment 2"
  111. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  112. (progn
  113. (transient-mark-mode 1)
  114. (push-mark (point) t t)
  115. (goto-char (point-max))
  116. (call-interactively 'comment-dwim)
  117. (buffer-string)))))
  118. ;; Region selected without comments: comment all lines if
  119. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  120. (should
  121. (equal "# Comment 1\n\n# Comment 2"
  122. (org-test-with-temp-text "Comment 1\n\nComment 2"
  123. (progn
  124. (transient-mark-mode 1)
  125. (push-mark (point) t t)
  126. (goto-char (point-max))
  127. (let ((comment-empty-lines nil))
  128. (call-interactively 'comment-dwim))
  129. (buffer-string)))))
  130. (should
  131. (equal "# Comment 1\n# \n# Comment 2"
  132. (org-test-with-temp-text "Comment 1\n\nComment 2"
  133. (progn
  134. (transient-mark-mode 1)
  135. (push-mark (point) t t)
  136. (goto-char (point-max))
  137. (let ((comment-empty-lines t))
  138. (call-interactively 'comment-dwim))
  139. (buffer-string)))))
  140. ;; In front of a keyword without region, insert a new comment.
  141. (should
  142. (equal "# \n#+KEYWORD: value"
  143. (org-test-with-temp-text "#+KEYWORD: value"
  144. (progn (call-interactively 'comment-dwim)
  145. (buffer-string)))))
  146. ;; In a source block, use appropriate syntax.
  147. (should
  148. (equal " ;; "
  149. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
  150. (forward-line)
  151. (let ((org-edit-src-content-indentation 2))
  152. (call-interactively 'comment-dwim))
  153. (buffer-substring-no-properties (line-beginning-position) (point)))))
  154. (should
  155. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  156. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
  157. (forward-line)
  158. (transient-mark-mode 1)
  159. (push-mark (point) t t)
  160. (forward-line 2)
  161. (let ((org-edit-src-content-indentation 2))
  162. (call-interactively 'comment-dwim))
  163. (buffer-string)))))
  164. ;;; Date and time analysis
  165. (ert-deftest test-org/org-read-date ()
  166. "Test `org-read-date' specifications."
  167. ;; Parse ISO date with abbreviated year and month.
  168. (should (equal "2012-03-29 16:40"
  169. (let ((org-time-was-given t))
  170. (org-read-date t nil "12-3-29 16:40"))))
  171. ;; Parse Europeans dates.
  172. (should (equal "2012-03-29 16:40"
  173. (let ((org-time-was-given t))
  174. (org-read-date t nil "29.03.2012 16:40"))))
  175. ;; Parse Europeans dates without year.
  176. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  177. (let ((org-time-was-given t))
  178. (org-read-date t nil "29.03. 16:40")))))
  179. (ert-deftest test-org/org-parse-time-string ()
  180. "Test `org-parse-time-string'."
  181. (should (equal (org-parse-time-string "2012-03-29 16:40")
  182. '(0 40 16 29 3 2012 nil nil nil)))
  183. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  184. '(0 40 16 29 3 2012 nil nil nil)))
  185. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  186. '(0 40 16 29 3 2012 nil nil nil)))
  187. (should (equal (org-parse-time-string "<2012-03-29>")
  188. '(0 0 0 29 3 2012 nil nil nil)))
  189. (should (equal (org-parse-time-string "<2012-03-29>" t)
  190. '(0 nil nil 29 3 2012 nil nil nil))))
  191. ;;; Filling
  192. (ert-deftest test-org/fill-paragraph ()
  193. "Test `org-fill-paragraph' specifications."
  194. ;; At an Org table, align it.
  195. (should
  196. (equal "| a |\n"
  197. (org-test-with-temp-text "|a|"
  198. (org-fill-paragraph)
  199. (buffer-string))))
  200. (should
  201. (equal "#+name: table\n| a |\n"
  202. (org-test-with-temp-text "#+name: table\n| a |"
  203. (org-fill-paragraph)
  204. (buffer-string))))
  205. ;; At a paragraph, preserve line breaks.
  206. (org-test-with-temp-text "some \\\\\nlong\ntext"
  207. (let ((fill-column 20))
  208. (org-fill-paragraph)
  209. (should (equal (buffer-string) "some \\\\\nlong text"))))
  210. ;; Correctly fill a paragraph when point is at its very end.
  211. (should
  212. (equal "A B"
  213. (org-test-with-temp-text "A\nB"
  214. (let ((fill-column 20))
  215. (goto-char (point-max))
  216. (org-fill-paragraph)
  217. (buffer-string)))))
  218. ;; Correctly fill the last paragraph of a greater element.
  219. (should
  220. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  221. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  222. (let ((fill-column 8))
  223. (forward-line)
  224. (end-of-line)
  225. (org-fill-paragraph)
  226. (buffer-string)))))
  227. ;; Correctly fill an element in a narrowed buffer.
  228. (should
  229. (equal "01234\n6"
  230. (org-test-with-temp-text "01234 6789"
  231. (let ((fill-column 5))
  232. (narrow-to-region 1 8)
  233. (org-fill-paragraph)
  234. (buffer-string)))))
  235. ;; Handle `adaptive-fill-regexp' in paragraphs.
  236. (should
  237. (equal "> a b"
  238. (org-test-with-temp-text "> a\n> b"
  239. (let ((fill-column 5)
  240. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  241. (org-fill-paragraph)
  242. (buffer-string)))))
  243. ;; Special case: Fill first paragraph when point is at an item or
  244. ;; a plain-list or a footnote reference.
  245. (should
  246. (equal "- A B"
  247. (org-test-with-temp-text "- A\n B"
  248. (let ((fill-column 20))
  249. (org-fill-paragraph)
  250. (buffer-string)))))
  251. (should
  252. (equal "[fn:1] A B"
  253. (org-test-with-temp-text "[fn:1] A\nB"
  254. (let ((fill-column 20))
  255. (org-fill-paragraph)
  256. (buffer-string)))))
  257. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  258. (let ((fill-column 20))
  259. (org-fill-paragraph)
  260. (should (equal (buffer-string)
  261. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  262. ;; Fill contents of `comment-block' elements.
  263. (should
  264. (equal
  265. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  266. (let ((fill-column 20))
  267. (forward-line)
  268. (org-fill-paragraph)
  269. (buffer-string)))
  270. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  271. ;; Fill `comment' elements.
  272. (should
  273. (equal " # A B"
  274. (org-test-with-temp-text " # A\n # B"
  275. (let ((fill-column 20))
  276. (org-fill-paragraph)
  277. (buffer-string)))))
  278. ;; Do not mix consecutive comments when filling one of them.
  279. (should
  280. (equal "# A B\n\n# C"
  281. (org-test-with-temp-text "# A\n# B\n\n# C"
  282. (let ((fill-column 20))
  283. (org-fill-paragraph)
  284. (buffer-string)))))
  285. ;; Use commented empty lines as separators when filling comments.
  286. (should
  287. (equal "# A B\n#\n# C"
  288. (org-test-with-temp-text "# A\n# B\n#\n# C"
  289. (let ((fill-column 20))
  290. (org-fill-paragraph)
  291. (buffer-string)))))
  292. ;; Handle `adaptive-fill-regexp' in comments.
  293. (should
  294. (equal "# > a b"
  295. (org-test-with-temp-text "# > a\n# > b"
  296. (let ((fill-column 20)
  297. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  298. (org-fill-paragraph)
  299. (buffer-string)))))
  300. ;; Do nothing at affiliated keywords.
  301. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  302. (let ((fill-column 20))
  303. (org-fill-paragraph)
  304. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  305. ;; Do not move point after table when filling a table.
  306. (should-not
  307. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  308. (forward-char)
  309. (org-fill-paragraph)
  310. (eobp))))
  311. (ert-deftest test-org/auto-fill-function ()
  312. "Test auto-filling features."
  313. ;; Auto fill paragraph.
  314. (should
  315. (equal "12345\n7890"
  316. (org-test-with-temp-text "12345 7890"
  317. (let ((fill-column 5))
  318. (end-of-line)
  319. (org-auto-fill-function)
  320. (buffer-string)))))
  321. ;; Auto fill first paragraph in an item.
  322. (should
  323. (equal "- 12345\n 7890"
  324. (org-test-with-temp-text "- 12345 7890"
  325. (let ((fill-column 7))
  326. (end-of-line)
  327. (org-auto-fill-function)
  328. (buffer-string)))))
  329. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  330. (should
  331. (equal "> 12345\n 7890"
  332. (org-test-with-temp-text "> 12345 7890"
  333. (let ((fill-column 10)
  334. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  335. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  336. (end-of-line)
  337. (org-auto-fill-function)
  338. (buffer-string)))))
  339. (should
  340. (equal "> 12345\n> 12345\n> 7890"
  341. (org-test-with-temp-text "> 12345\n> 12345 7890"
  342. (let ((fill-column 10)
  343. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  344. (goto-char (point-max))
  345. (org-auto-fill-function)
  346. (buffer-string)))))
  347. (should-not
  348. (equal " 12345\n *12345\n *12345"
  349. (org-test-with-temp-text " 12345\n *12345 12345"
  350. (let ((fill-column 10)
  351. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  352. (goto-char (point-max))
  353. (org-auto-fill-function)
  354. (buffer-string)))))
  355. ;; Auto fill comments.
  356. (should
  357. (equal " # 12345\n # 7890"
  358. (org-test-with-temp-text " # 12345 7890"
  359. (let ((fill-column 10))
  360. (end-of-line)
  361. (org-auto-fill-function)
  362. (buffer-string)))))
  363. ;; A hash within a line isn't a comment.
  364. (should-not
  365. (equal "12345 # 7890\n# 1"
  366. (org-test-with-temp-text "12345 # 7890 1"
  367. (let ((fill-column 12))
  368. (end-of-line)
  369. (org-auto-fill-function)
  370. (buffer-string)))))
  371. ;; Correctly interpret empty prefix.
  372. (should-not
  373. (equal "# a\n# b\nRegular\n# paragraph"
  374. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  375. (let ((fill-column 12))
  376. (end-of-line 3)
  377. (org-auto-fill-function)
  378. (buffer-string)))))
  379. ;; Comment block: auto fill contents.
  380. (should
  381. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  382. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  383. (let ((fill-column 5))
  384. (forward-line)
  385. (end-of-line)
  386. (org-auto-fill-function)
  387. (buffer-string)))))
  388. (should
  389. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  390. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  391. (let ((fill-column 5))
  392. (forward-line)
  393. (end-of-line)
  394. (org-auto-fill-function)
  395. (buffer-string)))))
  396. ;; Do not fill if a new item could be created.
  397. (should-not
  398. (equal "12345\n- 90"
  399. (org-test-with-temp-text "12345 - 90"
  400. (let ((fill-column 5))
  401. (end-of-line)
  402. (org-auto-fill-function)
  403. (buffer-string)))))
  404. ;; Do not fill if a line break could be introduced.
  405. (should-not
  406. (equal "123\\\\\n7890"
  407. (org-test-with-temp-text "123\\\\ 7890"
  408. (let ((fill-column 6))
  409. (end-of-line)
  410. (org-auto-fill-function)
  411. (buffer-string)))))
  412. ;; Do not fill affiliated keywords.
  413. (should-not
  414. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  415. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  416. (let ((fill-column 20))
  417. (end-of-line)
  418. (org-auto-fill-function)
  419. (buffer-string))))))
  420. ;;; Indentation
  421. (ert-deftest test-org/indent-line ()
  422. "Test `org-indent-line' specifications."
  423. ;; Do not indent footnote definitions or headlines.
  424. (should
  425. (zerop
  426. (org-test-with-temp-text "* H"
  427. (org-indent-line)
  428. (org-get-indentation))))
  429. (should
  430. (zerop
  431. (org-test-with-temp-text "[fn:1] fn"
  432. (let ((org-adapt-indentation t)) (org-indent-line))
  433. (org-get-indentation))))
  434. ;; Do not indent before first headline.
  435. (should
  436. (zerop
  437. (org-test-with-temp-text ""
  438. (org-indent-line)
  439. (org-get-indentation))))
  440. ;; Indent according to headline level otherwise, unless
  441. ;; `org-adapt-indentation' is nil.
  442. (should
  443. (= 2
  444. (org-test-with-temp-text "* H\nA"
  445. (forward-line)
  446. (let ((org-adapt-indentation t)) (org-indent-line))
  447. (org-get-indentation))))
  448. (should
  449. (= 2
  450. (org-test-with-temp-text "* H\n\nA"
  451. (forward-line)
  452. (let ((org-adapt-indentation t)) (org-indent-line))
  453. (org-get-indentation))))
  454. (should
  455. (zerop
  456. (org-test-with-temp-text "* H\nA"
  457. (forward-line)
  458. (let ((org-adapt-indentation nil)) (org-indent-line))
  459. (org-get-indentation))))
  460. ;; Indenting preserves point position.
  461. (should
  462. (org-test-with-temp-text "* H\nAB"
  463. (forward-line)
  464. (forward-char)
  465. (let ((org-adapt-indentation t)) (org-indent-line))
  466. (looking-at "B")))
  467. ;; Do not change indentation at an item.
  468. (should
  469. (= 1
  470. (org-test-with-temp-text "* H\n - A"
  471. (forward-line)
  472. (let ((org-adapt-indentation t)) (org-indent-line))
  473. (org-get-indentation))))
  474. ;; On blank lines at the end of a list, indent like last element
  475. ;; within it if the line is still in the list. Otherwise, indent
  476. ;; like the whole list.
  477. (should
  478. (= 4
  479. (org-test-with-temp-text "* H\n- A\n - AA\n"
  480. (goto-char (point-max))
  481. (let ((org-adapt-indentation t)) (org-indent-line))
  482. (org-get-indentation))))
  483. (should
  484. (zerop
  485. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
  486. (goto-char (point-max))
  487. (let ((org-adapt-indentation t)) (org-indent-line))
  488. (org-get-indentation))))
  489. ;; Likewise, on a blank line at the end of a footnote definition,
  490. ;; indent at column 0 if line belongs to the definition. Otherwise,
  491. ;; indent like the definition itself.
  492. (should
  493. (zerop
  494. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  495. (goto-char (point-max))
  496. (let ((org-adapt-indentation t)) (org-indent-line))
  497. (org-get-indentation))))
  498. (should
  499. (zerop
  500. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  501. (goto-char (point-max))
  502. (let ((org-adapt-indentation t)) (org-indent-line))
  503. (org-get-indentation))))
  504. ;; After the end of the contents of a greater element, indent like
  505. ;; the beginning of the element.
  506. (should
  507. (= 1
  508. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  509. (forward-line 2)
  510. (org-indent-line)
  511. (org-get-indentation))))
  512. ;; At the first line of an element, indent like previous element's
  513. ;; first line, ignoring footnotes definitions and inline tasks, or
  514. ;; according to parent.
  515. (should
  516. (= 2
  517. (org-test-with-temp-text "A\n\n B\n\nC"
  518. (goto-char (point-max))
  519. (org-indent-line)
  520. (org-get-indentation))))
  521. (should
  522. (= 1
  523. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  524. (goto-char (point-max))
  525. (org-indent-line)
  526. (org-get-indentation))))
  527. (should
  528. (= 1
  529. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  530. (forward-line 1)
  531. (org-indent-line)
  532. (org-get-indentation))))
  533. ;; Within code part of a source block, use language major mode if
  534. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  535. ;; according to line above.
  536. (should
  537. (= 6
  538. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  539. (forward-line 2)
  540. (let ((org-src-tab-acts-natively t)
  541. (org-edit-src-content-indentation 0))
  542. (org-indent-line))
  543. (org-get-indentation))))
  544. (should
  545. (= 1
  546. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  547. (forward-line 2)
  548. (let ((org-src-tab-acts-natively nil)
  549. (org-edit-src-content-indentation 0))
  550. (org-indent-line))
  551. (org-get-indentation))))
  552. ;; Otherwise, indent like the first non-blank line above.
  553. (should
  554. (zerop
  555. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  556. (forward-line 3)
  557. (org-indent-line)
  558. (org-get-indentation))))
  559. ;; Align node properties according to `org-property-format'. Handle
  560. ;; nicely empty values.
  561. (should
  562. (equal ":PROPERTIES:\n:key: value\n:END:"
  563. (org-test-with-temp-text ":PROPERTIES:\n:key: value\n:END:"
  564. (forward-line)
  565. (let ((org-property-format "%-10s %s"))
  566. (org-indent-line)
  567. (buffer-string)))))
  568. (should
  569. (equal ":PROPERTIES:\n:key:\n:END:"
  570. (org-test-with-temp-text ":PROPERTIES:\n:key:\n:END:"
  571. (forward-line)
  572. (let ((org-property-format "%-10s %s"))
  573. (org-indent-line)
  574. (buffer-string))))))
  575. (ert-deftest test-org/indent-region ()
  576. "Test `org-indent-region' specifications."
  577. ;; Indent paragraph.
  578. (should
  579. (equal "A\nB\nC"
  580. (org-test-with-temp-text " A\nB\n C"
  581. (org-indent-region (point-min) (point-max))
  582. (buffer-string))))
  583. ;; Indent greater elements along with their contents.
  584. (should
  585. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  586. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  587. (org-indent-region (point-min) (point-max))
  588. (buffer-string))))
  589. ;; Ignore contents of verse blocks and example blocks.
  590. (should
  591. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  592. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  593. (org-indent-region (point-min) (point-max))
  594. (buffer-string))))
  595. (should
  596. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  597. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  598. (org-indent-region (point-min) (point-max))
  599. (buffer-string))))
  600. ;; Indent according to mode if `org-src-tab-acts-natively' is
  601. ;; non-nil. Otherwise, do not indent code at all.
  602. (should
  603. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  604. (org-test-with-temp-text
  605. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  606. (let ((org-src-tab-acts-natively t)
  607. (org-edit-src-content-indentation 0))
  608. (org-indent-region (point-min) (point-max)))
  609. (buffer-string))))
  610. (should
  611. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  612. (org-test-with-temp-text
  613. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  614. (let ((org-src-tab-acts-natively nil)
  615. (org-edit-src-content-indentation 0))
  616. (org-indent-region (point-min) (point-max)))
  617. (buffer-string))))
  618. ;; Align node properties according to `org-property-format'. Handle
  619. ;; nicely empty values.
  620. (should
  621. (equal ":PROPERTIES:\n:key: value\n:END:"
  622. (org-test-with-temp-text ":PROPERTIES:\n:key: value\n:END:"
  623. (let ((org-property-format "%-10s %s"))
  624. (org-indent-region (point-min) (point-max)))
  625. (buffer-string))))
  626. (should
  627. (equal ":PROPERTIES:\n:key:\n:END:"
  628. (org-test-with-temp-text ":PROPERTIES:\n:key:\n:END:"
  629. (let ((org-property-format "%-10s %s"))
  630. (org-indent-region (point-min) (point-max)))
  631. (buffer-string))))
  632. ;; Special case: plain lists and footnote definitions.
  633. (should
  634. (equal "- A\n B\n - C\n\n D"
  635. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  636. (org-indent-region (point-min) (point-max))
  637. (buffer-string))))
  638. (should
  639. (equal "[fn:1] Definition\n\nDefinition"
  640. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  641. (org-indent-region (point-min) (point-max))
  642. (buffer-string)))))
  643. ;;; Editing
  644. ;;;; Insert elements
  645. (ert-deftest test-org/meta-return ()
  646. "Test M-RET (`org-meta-return')."
  647. ;; In a table field insert a row above.
  648. (should
  649. (org-test-with-temp-text "| a |"
  650. (forward-char)
  651. (org-meta-return)
  652. (forward-line -1)
  653. (looking-at "| |$")))
  654. ;; In a paragraph change current line into a header.
  655. (should
  656. (org-test-with-temp-text "a"
  657. (org-meta-return)
  658. (beginning-of-line)
  659. (looking-at "\* a$")))
  660. ;; In an item insert an item, in this case above.
  661. (should
  662. (org-test-with-temp-text "- a"
  663. (org-meta-return)
  664. (beginning-of-line)
  665. (looking-at "- $")))
  666. ;; In a drawer and paragraph insert an empty line, in this case above.
  667. (should
  668. (org-test-with-temp-text ":MYDRAWER:\na\n:END:"
  669. (forward-line)
  670. (org-meta-return)
  671. (forward-line -1)
  672. (looking-at "$")))
  673. ;; In a drawer and item insert an item, in this case above.
  674. (should
  675. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  676. (forward-line)
  677. (org-meta-return)
  678. (beginning-of-line)
  679. (looking-at "- $"))))
  680. (ert-deftest test-org/insert-todo-heading-respect-content ()
  681. "Test `org-insert-todo-heading-respect-content' specifications."
  682. ;; Create a TODO heading.
  683. (should
  684. (org-test-with-temp-text "* H1\n Body"
  685. (org-insert-todo-heading-respect-content)
  686. (nth 2 (org-heading-components))))
  687. ;; Add headline at the end of the first subtree
  688. (should
  689. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  690. (search-forward "H1Body")
  691. (org-insert-todo-heading-respect-content)
  692. (and (eobp) (org-at-heading-p))))
  693. ;; In a list, do not create a new item.
  694. (should
  695. (org-test-with-temp-text "* H\n- an item\n- another one"
  696. (search-forward "an ")
  697. (org-insert-todo-heading-respect-content)
  698. (and (eobp) (org-at-heading-p)))))
  699. ;;; Fixed-Width Areas
  700. (ert-deftest test-org/toggle-fixed-width ()
  701. "Test `org-toggle-fixed-width' specifications."
  702. ;; No region: Toggle on fixed-width marker in paragraphs.
  703. (should
  704. (equal ": A"
  705. (org-test-with-temp-text "A"
  706. (org-toggle-fixed-width)
  707. (buffer-string))))
  708. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  709. (should
  710. (equal "A"
  711. (org-test-with-temp-text ": A"
  712. (org-toggle-fixed-width)
  713. (buffer-string))))
  714. ;; No region: Toggle on marker in blank lines after elements or just
  715. ;; after a headline.
  716. (should
  717. (equal "* H\n: "
  718. (org-test-with-temp-text "* H\n"
  719. (forward-line)
  720. (org-toggle-fixed-width)
  721. (buffer-string))))
  722. (should
  723. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  724. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  725. (goto-char (point-max))
  726. (org-toggle-fixed-width)
  727. (buffer-string))))
  728. ;; No region: Toggle on marker in front of one line elements (e.g.,
  729. ;; headlines, clocks)
  730. (should
  731. (equal ": * Headline"
  732. (org-test-with-temp-text "* Headline"
  733. (org-toggle-fixed-width)
  734. (buffer-string))))
  735. (should
  736. (equal ": #+KEYWORD: value"
  737. (org-test-with-temp-text "#+KEYWORD: value"
  738. (org-toggle-fixed-width)
  739. (buffer-string))))
  740. ;; No region: error in other situations.
  741. (should-error
  742. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  743. (forward-line)
  744. (org-toggle-fixed-width)
  745. (buffer-string)))
  746. ;; No region: Indentation is preserved.
  747. (should
  748. (equal "- A\n : B"
  749. (org-test-with-temp-text "- A\n B"
  750. (forward-line)
  751. (org-toggle-fixed-width)
  752. (buffer-string))))
  753. ;; Region: If it contains only fixed-width elements and blank lines,
  754. ;; toggle off fixed-width markup.
  755. (should
  756. (equal "A\n\nB"
  757. (org-test-with-temp-text ": A\n\n: B"
  758. (transient-mark-mode 1)
  759. (push-mark (point) t t)
  760. (goto-char (point-max))
  761. (org-toggle-fixed-width)
  762. (buffer-string))))
  763. ;; Region: If it contains anything else, toggle on fixed-width but
  764. ;; not on fixed-width areas.
  765. (should
  766. (equal ": A\n: \n: B\n: \n: C"
  767. (org-test-with-temp-text "A\n\n: B\n\nC"
  768. (transient-mark-mode 1)
  769. (push-mark (point) t t)
  770. (goto-char (point-max))
  771. (org-toggle-fixed-width)
  772. (buffer-string))))
  773. ;; Region: Ignore blank lines at its end, unless it contains only
  774. ;; such lines.
  775. (should
  776. (equal ": A\n\n"
  777. (org-test-with-temp-text "A\n\n"
  778. (transient-mark-mode 1)
  779. (push-mark (point) t t)
  780. (goto-char (point-max))
  781. (org-toggle-fixed-width)
  782. (buffer-string))))
  783. (should
  784. (equal ": \n: \n"
  785. (org-test-with-temp-text "\n\n"
  786. (transient-mark-mode 1)
  787. (push-mark (point) t t)
  788. (goto-char (point-max))
  789. (org-toggle-fixed-width)
  790. (buffer-string)))))
  791. ;;; Headline
  792. (ert-deftest test-org/in-commented-heading-p ()
  793. "Test `org-in-commented-heading-p' specifications."
  794. ;; Commented headline.
  795. (should
  796. (org-test-with-temp-text "* COMMENT Headline\nBody"
  797. (goto-char (point-max))
  798. (org-in-commented-heading-p)))
  799. ;; Commented ancestor.
  800. (should
  801. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  802. (goto-char (point-max))
  803. (org-in-commented-heading-p)))
  804. ;; Comment keyword is case-sensitive.
  805. (should-not
  806. (org-test-with-temp-text "* Comment Headline\nBody"
  807. (goto-char (point-max))
  808. (org-in-commented-heading-p)))
  809. ;; Keyword is standalone.
  810. (should-not
  811. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  812. (goto-char (point-max))
  813. (org-in-commented-heading-p)))
  814. ;; Optional argument.
  815. (should-not
  816. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  817. (goto-char (point-max))
  818. (org-in-commented-heading-p t))))
  819. ;;; Links
  820. ;;;; Coderefs
  821. (ert-deftest test-org/coderef ()
  822. "Test coderef links specifications."
  823. (should
  824. (org-test-with-temp-text "
  825. #+BEGIN_SRC emacs-lisp
  826. \(+ 1 1) (ref:sc)
  827. #+END_SRC
  828. \[[(sc)]]"
  829. (goto-char (point-max))
  830. (org-open-at-point)
  831. (looking-at "(ref:sc)"))))
  832. ;;;; Custom ID
  833. (ert-deftest test-org/custom-id ()
  834. "Test custom ID links specifications."
  835. (should
  836. (org-test-with-temp-text
  837. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  838. (goto-char (point-max))
  839. (org-open-at-point)
  840. (org-looking-at-p "\\* H1"))))
  841. ;;;; Fuzzy Links
  842. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  843. ;; a named element (#+name: text) and to headlines (* Text).
  844. (ert-deftest test-org/fuzzy-links ()
  845. "Test fuzzy links specifications."
  846. ;; Fuzzy link goes in priority to a matching target.
  847. (should
  848. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  849. (goto-line 5)
  850. (org-open-at-point)
  851. (looking-at "<<Test>>")))
  852. ;; Then fuzzy link points to an element with a given name.
  853. (should
  854. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  855. (goto-line 5)
  856. (org-open-at-point)
  857. (looking-at "#\\+NAME: Test")))
  858. ;; A target still lead to a matching headline otherwise.
  859. (should
  860. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  861. (goto-line 4)
  862. (org-open-at-point)
  863. (looking-at "\\* Head2")))
  864. ;; With a leading star in link, enforce heading match.
  865. (should
  866. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  867. (goto-line 3)
  868. (org-open-at-point)
  869. (looking-at "\\* Test")))
  870. ;; Correctly un-hexify fuzzy links.
  871. (should
  872. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  873. (goto-char (point-max))
  874. (org-open-at-point)
  875. (bobp))))
  876. ;;;; Link Escaping
  877. (ert-deftest test-org/org-link-escape-ascii-character ()
  878. "Escape an ascii character."
  879. (should
  880. (string=
  881. "%5B"
  882. (org-link-escape "["))))
  883. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  884. "Escape an ascii control character."
  885. (should
  886. (string=
  887. "%09"
  888. (org-link-escape "\t"))))
  889. (ert-deftest test-org/org-link-escape-multibyte-character ()
  890. "Escape an unicode multibyte character."
  891. (should
  892. (string=
  893. "%E2%82%AC"
  894. (org-link-escape "€"))))
  895. (ert-deftest test-org/org-link-escape-custom-table ()
  896. "Escape string with custom character table."
  897. (should
  898. (string=
  899. "Foo%3A%42ar%0A"
  900. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  901. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  902. "Escape string with custom table merged with default table."
  903. (should
  904. (string=
  905. "%5BF%6F%6F%3A%42ar%0A%5D"
  906. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  907. (ert-deftest test-org/org-link-unescape-ascii-character ()
  908. "Unescape an ascii character."
  909. (should
  910. (string=
  911. "["
  912. (org-link-unescape "%5B"))))
  913. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  914. "Unescpae an ascii control character."
  915. (should
  916. (string=
  917. "\n"
  918. (org-link-unescape "%0A"))))
  919. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  920. "Unescape unicode multibyte character."
  921. (should
  922. (string=
  923. "€"
  924. (org-link-unescape "%E2%82%AC"))))
  925. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  926. "Unescape old style percent escaped character."
  927. (should
  928. (string=
  929. "àâçèéêîôùû"
  930. (decode-coding-string
  931. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  932. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  933. "Escape and unescape a URL that includes an escaped char.
  934. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  935. (should
  936. (string=
  937. "http://some.host.com/form?&id=blah%2Bblah25"
  938. (org-link-unescape
  939. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  940. (ert-deftest test-org/org-link-escape-chars-browser ()
  941. "Test of the constant `org-link-escape-chars-browser'.
  942. See there why this test is a candidate to be removed once Org
  943. drops support for Emacs 24.1 and 24.2."
  944. (should
  945. (string=
  946. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  947. "%22Release%208.2%22&idxname=emacs-orgmode")
  948. (org-link-escape-browser ; Do not replace with `url-encode-url',
  949. ; see docstring above.
  950. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  951. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  952. ;;;; Open at point
  953. (ert-deftest test-org/open-at-point ()
  954. "Test `org-open-at-point' with link being a heading property."
  955. (should
  956. (org-test-with-temp-text
  957. "* Headline
  958. :PROPERTIES:
  959. :URL: <point>[[info:org#Top]]
  960. :END:"
  961. (org-open-at-point)
  962. (and (switch-to-buffer "*info*")
  963. (prog1
  964. (looking-at "\nOrg Mode Manual")
  965. (kill-buffer))))))
  966. ;;; Node Properties
  967. (ert-deftest test-org/accumulated-properties-in-drawers ()
  968. "Ensure properties accumulate in subtree drawers."
  969. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  970. (org-babel-next-src-block)
  971. (should (equal '(2 1) (org-babel-execute-src-block)))))
  972. ;;; Mark Region
  973. (ert-deftest test-org/mark-subtree ()
  974. "Test `org-mark-subtree' specifications."
  975. ;; Error when point is before first headline.
  976. (should-error
  977. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  978. (progn (transient-mark-mode 1)
  979. (org-mark-subtree))))
  980. ;; Without argument, mark current subtree.
  981. (should
  982. (equal
  983. '(12 32)
  984. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  985. (progn (transient-mark-mode 1)
  986. (forward-line 2)
  987. (org-mark-subtree)
  988. (list (region-beginning) (region-end))))))
  989. ;; With an argument, move ARG up.
  990. (should
  991. (equal
  992. '(1 32)
  993. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  994. (progn (transient-mark-mode 1)
  995. (forward-line 2)
  996. (org-mark-subtree 1)
  997. (list (region-beginning) (region-end))))))
  998. ;; Do not get fooled by inlinetasks.
  999. (when (featurep 'org-inlinetask)
  1000. (should
  1001. (= 1
  1002. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1003. (progn (transient-mark-mode 1)
  1004. (forward-line 1)
  1005. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1006. (region-beginning)))))))
  1007. ;;; Navigation
  1008. (ert-deftest test-org/beginning-of-line ()
  1009. "Test `org-beginning-of-line' specifications."
  1010. ;; Standard test.
  1011. (should
  1012. (org-test-with-temp-text "Some text\nSome other text"
  1013. (progn (org-beginning-of-line) (bolp))))
  1014. ;; Standard test with `visual-line-mode'.
  1015. (should-not
  1016. (org-test-with-temp-text "A long line of text\nSome other text"
  1017. (progn (visual-line-mode)
  1018. (forward-char 2)
  1019. (dotimes (i 1000) (insert "very "))
  1020. (org-beginning-of-line)
  1021. (bolp))))
  1022. ;; At an headline with special movement.
  1023. (should
  1024. (org-test-with-temp-text "* TODO Headline"
  1025. (let ((org-special-ctrl-a/e t))
  1026. (org-end-of-line)
  1027. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1028. (progn (org-beginning-of-line) (bolp))
  1029. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1030. (ert-deftest test-org/end-of-line ()
  1031. "Test `org-end-of-line' specifications."
  1032. ;; Standard test.
  1033. (should
  1034. (org-test-with-temp-text "Some text\nSome other text"
  1035. (progn (org-end-of-line) (eolp))))
  1036. ;; Standard test with `visual-line-mode'.
  1037. (should-not
  1038. (org-test-with-temp-text "A long line of text\nSome other text"
  1039. (progn (visual-line-mode)
  1040. (forward-char 2)
  1041. (dotimes (i 1000) (insert "very "))
  1042. (goto-char (point-min))
  1043. (org-end-of-line)
  1044. (eolp))))
  1045. ;; At an headline with special movement.
  1046. (should
  1047. (org-test-with-temp-text "* Headline1 :tag:\n"
  1048. (let ((org-special-ctrl-a/e t))
  1049. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1050. (progn (org-end-of-line) (eolp))
  1051. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1052. ;; At an headline without special movement.
  1053. (should
  1054. (org-test-with-temp-text "* Headline2 :tag:\n"
  1055. (let ((org-special-ctrl-a/e nil))
  1056. (and (progn (org-end-of-line) (eolp))
  1057. (progn (org-end-of-line) (eolp))))))
  1058. ;; At an headline, with reversed movement.
  1059. (should
  1060. (org-test-with-temp-text "* Headline3 :tag:\n"
  1061. (let ((org-special-ctrl-a/e 'reversed)
  1062. (this-command last-command))
  1063. (and (progn (org-end-of-line) (eolp))
  1064. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1065. ;; At a block without hidden contents.
  1066. (should
  1067. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1068. (progn (org-end-of-line) (eolp))))
  1069. ;; At a block with hidden contents.
  1070. (should-not
  1071. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1072. (let ((org-special-ctrl-a/e t))
  1073. (org-hide-block-toggle)
  1074. (org-end-of-line)
  1075. (eobp)))))
  1076. (ert-deftest test-org/forward-paragraph ()
  1077. "Test `org-forward-paragraph' specifications."
  1078. ;; At end of buffer, return an error.
  1079. (should-error
  1080. (org-test-with-temp-text "Paragraph"
  1081. (goto-char (point-max))
  1082. (org-forward-paragraph)))
  1083. ;; Standard test.
  1084. (should
  1085. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1086. (org-forward-paragraph)
  1087. (looking-at "P2")))
  1088. ;; Ignore depth.
  1089. (should
  1090. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1091. (org-forward-paragraph)
  1092. (looking-at "P1")))
  1093. ;; Do not enter elements with invisible contents.
  1094. (should
  1095. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1096. (org-hide-block-toggle)
  1097. (org-forward-paragraph)
  1098. (looking-at "P3")))
  1099. ;; On an affiliated keyword, jump to the beginning of the element.
  1100. (should
  1101. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1102. (org-forward-paragraph)
  1103. (looking-at "Para")))
  1104. ;; On an item or a footnote definition, move to the second element
  1105. ;; inside, if any.
  1106. (should
  1107. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1108. (org-forward-paragraph)
  1109. (looking-at " Paragraph")))
  1110. (should
  1111. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1112. (org-forward-paragraph)
  1113. (looking-at "Paragraph")))
  1114. ;; On an item, or a footnote definition, when the first line is
  1115. ;; empty, move to the first item.
  1116. (should
  1117. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1118. (org-forward-paragraph)
  1119. (looking-at " Paragraph")))
  1120. (should
  1121. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1122. (org-forward-paragraph)
  1123. (looking-at "Paragraph")))
  1124. ;; On a table (resp. a property drawer) do not move through table
  1125. ;; rows (resp. node properties).
  1126. (should
  1127. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1128. (org-forward-paragraph)
  1129. (looking-at "Paragraph")))
  1130. (should
  1131. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1132. (org-forward-paragraph)
  1133. (looking-at "Paragraph")))
  1134. ;; On a verse or source block, stop after blank lines.
  1135. (should
  1136. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1137. (org-forward-paragraph)
  1138. (looking-at "L2")))
  1139. (should
  1140. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1141. (org-forward-paragraph)
  1142. (looking-at "L2"))))
  1143. (ert-deftest test-org/backward-paragraph ()
  1144. "Test `org-backward-paragraph' specifications."
  1145. ;; Error at beginning of buffer.
  1146. (should-error
  1147. (org-test-with-temp-text "Paragraph"
  1148. (org-backward-paragraph)))
  1149. ;; Regular test.
  1150. (should
  1151. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1152. (goto-char (point-max))
  1153. (org-backward-paragraph)
  1154. (looking-at "P3")))
  1155. (should
  1156. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1157. (goto-char (point-max))
  1158. (beginning-of-line)
  1159. (org-backward-paragraph)
  1160. (looking-at "P2")))
  1161. ;; Ignore depth.
  1162. (should
  1163. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1164. (goto-char (point-max))
  1165. (beginning-of-line)
  1166. (org-backward-paragraph)
  1167. (looking-at "P2")))
  1168. ;; Ignore invisible elements.
  1169. (should
  1170. (org-test-with-temp-text "* H1\n P1\n* H2"
  1171. (org-cycle)
  1172. (goto-char (point-max))
  1173. (beginning-of-line)
  1174. (org-backward-paragraph)
  1175. (bobp)))
  1176. ;; On an affiliated keyword, jump to the first one.
  1177. (should
  1178. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1179. (search-forward "c2")
  1180. (org-backward-paragraph)
  1181. (looking-at "#\\+name")))
  1182. ;; On the second element in an item or a footnote definition, jump
  1183. ;; to item or the definition.
  1184. (should
  1185. (org-test-with-temp-text "- line1\n\n line2"
  1186. (goto-char (point-max))
  1187. (beginning-of-line)
  1188. (org-backward-paragraph)
  1189. (looking-at "- line1")))
  1190. (should
  1191. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1192. (goto-char (point-max))
  1193. (beginning-of-line)
  1194. (org-backward-paragraph)
  1195. (looking-at "\\[fn:1\\] line1")))
  1196. ;; On a table (resp. a property drawer), ignore table rows
  1197. ;; (resp. node properties).
  1198. (should
  1199. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1200. (goto-char (point-max))
  1201. (beginning-of-line)
  1202. (org-backward-paragraph)
  1203. (bobp)))
  1204. (should
  1205. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
  1206. (goto-char (point-max))
  1207. (beginning-of-line)
  1208. (org-backward-paragraph)
  1209. (bobp)))
  1210. ;; On a source or verse block, stop before blank lines.
  1211. (should
  1212. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1213. (search-forward "L3")
  1214. (beginning-of-line)
  1215. (org-backward-paragraph)
  1216. (looking-at "L2")))
  1217. (should
  1218. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1219. (search-forward "L3")
  1220. (beginning-of-line)
  1221. (org-backward-paragraph)
  1222. (looking-at "L2"))))
  1223. (ert-deftest test-org/forward-element ()
  1224. "Test `org-forward-element' specifications."
  1225. ;; 1. At EOB: should error.
  1226. (org-test-with-temp-text "Some text\n"
  1227. (goto-char (point-max))
  1228. (should-error (org-forward-element)))
  1229. ;; 2. Standard move: expected to ignore blank lines.
  1230. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1231. (org-forward-element)
  1232. (should (looking-at (regexp-quote "Second paragraph."))))
  1233. ;; 3. Headline tests.
  1234. (org-test-with-temp-text "
  1235. * Head 1
  1236. ** Head 1.1
  1237. *** Head 1.1.1
  1238. ** Head 1.2"
  1239. ;; 3.1. At an headline beginning: move to next headline at the
  1240. ;; same level.
  1241. (goto-line 3)
  1242. (org-forward-element)
  1243. (should (looking-at (regexp-quote "** Head 1.2")))
  1244. ;; 3.2. At an headline beginning: move to parent headline if no
  1245. ;; headline at the same level.
  1246. (goto-line 3)
  1247. (org-forward-element)
  1248. (should (looking-at (regexp-quote "** Head 1.2"))))
  1249. ;; 4. Greater element tests.
  1250. (org-test-with-temp-text
  1251. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1252. ;; 4.1. At a greater element: expected to skip contents.
  1253. (org-forward-element)
  1254. (should (looking-at (regexp-quote "Outside.")))
  1255. ;; 4.2. At the end of greater element contents: expected to skip
  1256. ;; to the end of the greater element.
  1257. (goto-line 2)
  1258. (org-forward-element)
  1259. (should (looking-at (regexp-quote "Outside."))))
  1260. ;; 5. List tests.
  1261. (org-test-with-temp-text "
  1262. - item1
  1263. - sub1
  1264. - sub2
  1265. - sub3
  1266. Inner paragraph.
  1267. - item2
  1268. Outside."
  1269. ;; 5.1. At list top point: expected to move to the element after
  1270. ;; the list.
  1271. (goto-line 2)
  1272. (org-forward-element)
  1273. (should (looking-at (regexp-quote "Outside.")))
  1274. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1275. ;; beginning of line, move to next item.
  1276. (goto-line 2)
  1277. (forward-char)
  1278. (org-forward-element)
  1279. (should (looking-at "- item2"))
  1280. (goto-line 4)
  1281. (forward-char)
  1282. (org-forward-element)
  1283. (should (looking-at " - sub2"))
  1284. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1285. (goto-line 4)
  1286. (org-forward-element)
  1287. (should (looking-at (regexp-quote " Inner paragraph.")))
  1288. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1289. (goto-line 8)
  1290. (org-forward-element)
  1291. (should (looking-at (regexp-quote " Inner paragraph.")))
  1292. ;; 5.5. At an item: expected to move to next item, if any.
  1293. (goto-line 6)
  1294. (org-forward-element)
  1295. (should (looking-at " - sub3"))))
  1296. (ert-deftest test-org/backward-element ()
  1297. "Test `org-backward-element' specifications."
  1298. ;; 1. Should error at BOB.
  1299. (org-test-with-temp-text " \nParagraph."
  1300. (should-error (org-backward-element)))
  1301. ;; 2. Should move at BOB when called on the first element in buffer.
  1302. (should
  1303. (org-test-with-temp-text "\n#+TITLE: test"
  1304. (progn (forward-line)
  1305. (org-backward-element)
  1306. (bobp))))
  1307. ;; 3. Not at the beginning of an element: move at its beginning.
  1308. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1309. (goto-line 3)
  1310. (end-of-line)
  1311. (org-backward-element)
  1312. (should (looking-at (regexp-quote "Paragraph2."))))
  1313. ;; 4. Headline tests.
  1314. (org-test-with-temp-text "
  1315. * Head 1
  1316. ** Head 1.1
  1317. *** Head 1.1.1
  1318. ** Head 1.2"
  1319. ;; 4.1. At an headline beginning: move to previous headline at the
  1320. ;; same level.
  1321. (goto-line 5)
  1322. (org-backward-element)
  1323. (should (looking-at (regexp-quote "** Head 1.1")))
  1324. ;; 4.2. At an headline beginning: move to parent headline if no
  1325. ;; headline at the same level.
  1326. (goto-line 3)
  1327. (org-backward-element)
  1328. (should (looking-at (regexp-quote "* Head 1")))
  1329. ;; 4.3. At the first top-level headline: should error.
  1330. (goto-line 2)
  1331. (should-error (org-backward-element)))
  1332. ;; 5. At beginning of first element inside a greater element:
  1333. ;; expected to move to greater element's beginning.
  1334. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1335. (goto-line 3)
  1336. (org-backward-element)
  1337. (should (looking-at "#\\+BEGIN_CENTER")))
  1338. ;; 6. At the beginning of the first element in a section: should
  1339. ;; move back to headline, if any.
  1340. (should
  1341. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1342. (progn (goto-char (point-max))
  1343. (beginning-of-line)
  1344. (org-backward-element)
  1345. (org-at-heading-p))))
  1346. ;; 7. List tests.
  1347. (org-test-with-temp-text "
  1348. - item1
  1349. - sub1
  1350. - sub2
  1351. - sub3
  1352. Inner paragraph.
  1353. - item2
  1354. Outside."
  1355. ;; 7.1. At beginning of sub-list: expected to move to the
  1356. ;; paragraph before it.
  1357. (goto-line 4)
  1358. (org-backward-element)
  1359. (should (looking-at "item1"))
  1360. ;; 7.2. At an item in a list: expected to move at previous item.
  1361. (goto-line 8)
  1362. (org-backward-element)
  1363. (should (looking-at " - sub2"))
  1364. (goto-line 12)
  1365. (org-backward-element)
  1366. (should (looking-at "- item1"))
  1367. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1368. ;; beginning.
  1369. (goto-line 10)
  1370. (org-backward-element)
  1371. (should (looking-at " - sub1"))
  1372. (goto-line 15)
  1373. (org-backward-element)
  1374. (should (looking-at "- item1"))
  1375. ;; 7.4. At blank-lines before list end: expected to move to top
  1376. ;; item.
  1377. (goto-line 14)
  1378. (org-backward-element)
  1379. (should (looking-at "- item1"))))
  1380. (ert-deftest test-org/up-element ()
  1381. "Test `org-up-element' specifications."
  1382. ;; 1. At BOB or with no surrounding element: should error.
  1383. (org-test-with-temp-text "Paragraph."
  1384. (should-error (org-up-element)))
  1385. (org-test-with-temp-text "* Head1\n* Head2"
  1386. (goto-line 2)
  1387. (should-error (org-up-element)))
  1388. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1389. (goto-line 3)
  1390. (should-error (org-up-element)))
  1391. ;; 2. At an headline: move to parent headline.
  1392. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1393. (goto-line 3)
  1394. (org-up-element)
  1395. (should (looking-at "\\* Head1")))
  1396. ;; 3. Inside a greater element: move to greater element beginning.
  1397. (org-test-with-temp-text
  1398. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1399. (goto-line 3)
  1400. (org-up-element)
  1401. (should (looking-at "#\\+BEGIN_CENTER")))
  1402. ;; 4. List tests.
  1403. (org-test-with-temp-text "* Top
  1404. - item1
  1405. - sub1
  1406. - sub2
  1407. Paragraph within sub2.
  1408. - item2"
  1409. ;; 4.1. Within an item: move to the item beginning.
  1410. (goto-line 8)
  1411. (org-up-element)
  1412. (should (looking-at " - sub2"))
  1413. ;; 4.2. At an item in a sub-list: move to parent item.
  1414. (goto-line 4)
  1415. (org-up-element)
  1416. (should (looking-at "- item1"))
  1417. ;; 4.3. At an item in top list: move to beginning of whole list.
  1418. (goto-line 10)
  1419. (org-up-element)
  1420. (should (looking-at "- item1"))
  1421. ;; 4.4. Special case. At very top point: should move to parent of
  1422. ;; list.
  1423. (goto-line 2)
  1424. (org-up-element)
  1425. (should (looking-at "\\* Top"))))
  1426. (ert-deftest test-org/down-element ()
  1427. "Test `org-down-element' specifications."
  1428. ;; Error when the element hasn't got a recursive type.
  1429. (org-test-with-temp-text "Paragraph."
  1430. (should-error (org-down-element)))
  1431. ;; Error when the element has no contents
  1432. (org-test-with-temp-text "* Headline"
  1433. (should-error (org-down-element)))
  1434. ;; When at a plain-list, move to first item.
  1435. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1436. (goto-line 2)
  1437. (org-down-element)
  1438. (should (looking-at " - Item 1.1")))
  1439. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1440. (org-down-element)
  1441. (should (looking-at " Item 1")))
  1442. ;; When at a table, move to first row
  1443. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1444. (org-down-element)
  1445. (should (looking-at " a | b |")))
  1446. ;; Otherwise, move inside the greater element.
  1447. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1448. (org-down-element)
  1449. (should (looking-at "Paragraph"))))
  1450. (ert-deftest test-org/drag-element-backward ()
  1451. "Test `org-drag-element-backward' specifications."
  1452. ;; 1. Error when trying to move first element of buffer.
  1453. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1454. (should-error (org-drag-element-backward)))
  1455. ;; 2. Error when trying to swap nested elements.
  1456. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1457. (forward-line)
  1458. (should-error (org-drag-element-backward)))
  1459. ;; 3. Error when trying to swap an headline element and
  1460. ;; a non-headline element.
  1461. (org-test-with-temp-text "Test.\n* Head 1"
  1462. (forward-line)
  1463. (should-error (org-drag-element-backward)))
  1464. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1465. ;; between elements.
  1466. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  1467. (search-forward "graph")
  1468. (org-drag-element-backward)
  1469. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  1470. (should (looking-at " 2")))
  1471. ;; 5. Preserve visibility of elements and their contents.
  1472. (org-test-with-temp-text "
  1473. #+BEGIN_CENTER
  1474. Text.
  1475. #+END_CENTER
  1476. - item 1
  1477. #+BEGIN_QUOTE
  1478. Text.
  1479. #+END_QUOTE"
  1480. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1481. (search-backward "- item 1")
  1482. (org-drag-element-backward)
  1483. (should
  1484. (equal
  1485. '((63 . 82) (26 . 48))
  1486. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1487. (overlays-in (point-min) (point-max)))))))
  1488. (ert-deftest test-org/drag-element-forward ()
  1489. "Test `org-drag-element-forward' specifications."
  1490. ;; 1. Error when trying to move first element of buffer.
  1491. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1492. (goto-line 3)
  1493. (should-error (org-drag-element-forward)))
  1494. ;; 2. Error when trying to swap nested elements.
  1495. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1496. (forward-line)
  1497. (should-error (org-drag-element-forward)))
  1498. ;; 3. Error when trying to swap a non-headline element and an
  1499. ;; headline.
  1500. (org-test-with-temp-text "Test.\n* Head 1"
  1501. (should-error (org-drag-element-forward)))
  1502. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1503. ;; between elements.
  1504. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1505. (search-forward "graph")
  1506. (org-drag-element-forward)
  1507. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1508. (should (looking-at " 1")))
  1509. ;; 5. Preserve visibility of elements and their contents.
  1510. (org-test-with-temp-text "
  1511. #+BEGIN_CENTER
  1512. Text.
  1513. #+END_CENTER
  1514. - item 1
  1515. #+BEGIN_QUOTE
  1516. Text.
  1517. #+END_QUOTE"
  1518. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1519. (search-backward "#+BEGIN_CENTER")
  1520. (org-drag-element-forward)
  1521. (should
  1522. (equal
  1523. '((63 . 82) (26 . 48))
  1524. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1525. (overlays-in (point-min) (point-max)))))))
  1526. ;;; Planning
  1527. (ert-deftest test-org/timestamp-has-time-p ()
  1528. "Test `org-timestamp-has-time-p' specifications."
  1529. ;; With time.
  1530. (should
  1531. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1532. (org-timestamp-has-time-p (org-element-context))))
  1533. ;; Without time.
  1534. (should-not
  1535. (org-test-with-temp-text "<2012-03-29 Thu>"
  1536. (org-timestamp-has-time-p (org-element-context)))))
  1537. (ert-deftest test-org/timestamp-format ()
  1538. "Test `org-timestamp-format' specifications."
  1539. ;; Regular test.
  1540. (should
  1541. (equal
  1542. "2012-03-29 16:40"
  1543. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1544. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1545. ;; Range end.
  1546. (should
  1547. (equal
  1548. "2012-03-29"
  1549. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1550. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1551. (ert-deftest test-org/timestamp-split-range ()
  1552. "Test `org-timestamp-split-range' specifications."
  1553. ;; Extract range start (active).
  1554. (should
  1555. (equal '(2012 3 29)
  1556. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1557. (let ((ts (org-timestamp-split-range (org-element-context))))
  1558. (mapcar (lambda (p) (org-element-property p ts))
  1559. '(:year-end :month-end :day-end))))))
  1560. ;; Extract range start (inactive)
  1561. (should
  1562. (equal '(2012 3 29)
  1563. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1564. (let ((ts (org-timestamp-split-range (org-element-context))))
  1565. (mapcar (lambda (p) (org-element-property p ts))
  1566. '(:year-end :month-end :day-end))))))
  1567. ;; Extract range end (active).
  1568. (should
  1569. (equal '(2012 3 30)
  1570. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1571. (let ((ts (org-timestamp-split-range
  1572. (org-element-context) t)))
  1573. (mapcar (lambda (p) (org-element-property p ts))
  1574. '(:year-end :month-end :day-end))))))
  1575. ;; Extract range end (inactive)
  1576. (should
  1577. (equal '(2012 3 30)
  1578. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1579. (let ((ts (org-timestamp-split-range
  1580. (org-element-context) t)))
  1581. (mapcar (lambda (p) (org-element-property p ts))
  1582. '(:year-end :month-end :day-end))))))
  1583. ;; Return the timestamp if not a range.
  1584. (should
  1585. (org-test-with-temp-text "[2012-03-29 Thu]"
  1586. (let* ((ts-orig (org-element-context))
  1587. (ts-copy (org-timestamp-split-range ts-orig)))
  1588. (eq ts-orig ts-copy))))
  1589. (should
  1590. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1591. (let* ((ts-orig (org-element-context))
  1592. (ts-copy (org-timestamp-split-range ts-orig)))
  1593. (eq ts-orig ts-copy))))
  1594. ;; Check that parent is the same when a range was split.
  1595. (should
  1596. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1597. (let* ((ts-orig (org-element-context))
  1598. (ts-copy (org-timestamp-split-range ts-orig)))
  1599. (eq (org-element-property :parent ts-orig)
  1600. (org-element-property :parent ts-copy))))))
  1601. (ert-deftest test-org/timestamp-translate ()
  1602. "Test `org-timestamp-translate' specifications."
  1603. ;; Translate whole date range.
  1604. (should
  1605. (equal "<29>--<30>"
  1606. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1607. (let ((org-display-custom-times t)
  1608. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1609. (org-timestamp-translate (org-element-context))))))
  1610. ;; Translate date range start.
  1611. (should
  1612. (equal "<29>"
  1613. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1614. (let ((org-display-custom-times t)
  1615. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1616. (org-timestamp-translate (org-element-context) 'start)))))
  1617. ;; Translate date range end.
  1618. (should
  1619. (equal "<30>"
  1620. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1621. (let ((org-display-custom-times t)
  1622. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1623. (org-timestamp-translate (org-element-context) 'end)))))
  1624. ;; Translate time range.
  1625. (should
  1626. (equal "<08>--<16>"
  1627. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1628. (let ((org-display-custom-times t)
  1629. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1630. (org-timestamp-translate (org-element-context))))))
  1631. ;; Translate non-range timestamp.
  1632. (should
  1633. (equal "<29>"
  1634. (org-test-with-temp-text "<2012-03-29 Thu>"
  1635. (let ((org-display-custom-times t)
  1636. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1637. (org-timestamp-translate (org-element-context))))))
  1638. ;; Do not change `diary' timestamps.
  1639. (should
  1640. (equal "<%%(org-float t 4 2)>"
  1641. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1642. (let ((org-display-custom-times t)
  1643. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1644. (org-timestamp-translate (org-element-context)))))))
  1645. ;;; Radio Targets
  1646. (ert-deftest test-org/update-radio-target-regexp ()
  1647. "Test `org-update-radio-target-regexp' specifications."
  1648. ;; Properly update cache with no previous radio target regexp.
  1649. (should
  1650. (eq 'link
  1651. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  1652. (save-excursion (goto-char (point-max)) (org-element-context))
  1653. (insert "<<<")
  1654. (search-forward "o")
  1655. (insert ">>>")
  1656. (org-update-radio-target-regexp)
  1657. (goto-char (point-max))
  1658. (org-element-type (org-element-context)))))
  1659. ;; Properly update cache with previous radio target regexp.
  1660. (should
  1661. (eq 'link
  1662. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  1663. (save-excursion (goto-char (point-max)) (org-element-context))
  1664. (insert "<<<")
  1665. (search-forward "o")
  1666. (insert ">>>")
  1667. (org-update-radio-target-regexp)
  1668. (search-backward "r")
  1669. (delete-char 5)
  1670. (insert "new")
  1671. (org-update-radio-target-regexp)
  1672. (goto-char (point-max))
  1673. (delete-region (line-beginning-position) (point))
  1674. (insert "new")
  1675. (org-element-type (org-element-context))))))
  1676. ;;; Visibility
  1677. (ert-deftest test-org/flag-drawer ()
  1678. "Test `org-flag-drawer' specifications."
  1679. ;; Hide drawer.
  1680. (should
  1681. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1682. (org-flag-drawer t)
  1683. (get-char-property (line-end-position) 'invisible)))
  1684. ;; Show drawer.
  1685. (should-not
  1686. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1687. (org-flag-drawer t)
  1688. (org-flag-drawer nil)
  1689. (get-char-property (line-end-position) 'invisible)))
  1690. ;; Test optional argument.
  1691. (should
  1692. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1693. (let ((drawer (save-excursion (search-forward ":D2")
  1694. (org-element-at-point))))
  1695. (org-flag-drawer t drawer)
  1696. (get-char-property (progn (search-forward ":D2") (line-end-position))
  1697. 'invisible))))
  1698. (should-not
  1699. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1700. (let ((drawer (save-excursion (search-forward ":D2")
  1701. (org-element-at-point))))
  1702. (org-flag-drawer t drawer)
  1703. (get-char-property (line-end-position) 'invisible))))
  1704. ;; Do not hide fake drawers.
  1705. (should-not
  1706. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  1707. (forward-line 1)
  1708. (org-flag-drawer t)
  1709. (get-char-property (line-end-position) 'invisible)))
  1710. ;; Do not hide incomplete drawers.
  1711. (should-not
  1712. (org-test-with-temp-text ":D:\nparagraph"
  1713. (forward-line 1)
  1714. (org-flag-drawer t)
  1715. (get-char-property (line-end-position) 'invisible)))
  1716. ;; Do not hide drawers when called from final blank lines.
  1717. (should-not
  1718. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  1719. (goto-char (point-max))
  1720. (org-flag-drawer t)
  1721. (goto-char (point-min))
  1722. (get-char-property (line-end-position) 'invisible)))
  1723. ;; Don't leave point in an invisible part of the buffer when hiding
  1724. ;; a drawer away.
  1725. (should-not
  1726. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1727. (goto-char (point-max))
  1728. (org-flag-drawer t)
  1729. (get-char-property (point) 'invisible))))
  1730. (provide 'test-org)
  1731. ;;; test-org.el ends here