test-org.el 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  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 item insert an item, in this case above.
  667. (should
  668. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  669. (forward-line)
  670. (org-meta-return)
  671. (beginning-of-line)
  672. (looking-at "- $"))))
  673. (ert-deftest test-org/insert-heading ()
  674. "Test `org-insert-heading' specifications."
  675. ;; FIXME: Test coverage is incomplete yet.
  676. ;;
  677. ;; In an empty buffer, insert a new headline.
  678. (should
  679. (equal "* "
  680. (org-test-with-temp-text ""
  681. (org-insert-heading)
  682. (buffer-string))))
  683. ;; At the beginning of a line, turn it into a headline
  684. (should
  685. (equal "* P"
  686. (org-test-with-temp-text "<point>P"
  687. (org-insert-heading)
  688. (buffer-string))))
  689. ;; In the middle of a line, split the line if allowed, otherwise,
  690. ;; insert the headline at its end.
  691. (should
  692. (equal "Para\n* graph"
  693. (org-test-with-temp-text "Para<point>graph"
  694. (let ((org-M-RET-may-split-line '((default . t))))
  695. (org-insert-heading))
  696. (buffer-string))))
  697. (should
  698. (equal "Paragraph\n* "
  699. (org-test-with-temp-text "Para<point>graph"
  700. (let ((org-M-RET-may-split-line '((default . nil))))
  701. (org-insert-heading))
  702. (buffer-string)))))
  703. (ert-deftest test-org/insert-todo-heading-respect-content ()
  704. "Test `org-insert-todo-heading-respect-content' specifications."
  705. ;; Create a TODO heading.
  706. (should
  707. (org-test-with-temp-text "* H1\n Body"
  708. (org-insert-todo-heading-respect-content)
  709. (nth 2 (org-heading-components))))
  710. ;; Add headline at the end of the first subtree
  711. (should
  712. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  713. (search-forward "H1Body")
  714. (org-insert-todo-heading-respect-content)
  715. (and (eobp) (org-at-heading-p))))
  716. ;; In a list, do not create a new item.
  717. (should
  718. (org-test-with-temp-text "* H\n- an item\n- another one"
  719. (search-forward "an ")
  720. (org-insert-todo-heading-respect-content)
  721. (and (eobp) (org-at-heading-p)))))
  722. ;;; Fixed-Width Areas
  723. (ert-deftest test-org/toggle-fixed-width ()
  724. "Test `org-toggle-fixed-width' specifications."
  725. ;; No region: Toggle on fixed-width marker in paragraphs.
  726. (should
  727. (equal ": A"
  728. (org-test-with-temp-text "A"
  729. (org-toggle-fixed-width)
  730. (buffer-string))))
  731. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  732. (should
  733. (equal "A"
  734. (org-test-with-temp-text ": A"
  735. (org-toggle-fixed-width)
  736. (buffer-string))))
  737. ;; No region: Toggle on marker in blank lines after elements or just
  738. ;; after a headline.
  739. (should
  740. (equal "* H\n: "
  741. (org-test-with-temp-text "* H\n"
  742. (forward-line)
  743. (org-toggle-fixed-width)
  744. (buffer-string))))
  745. (should
  746. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  747. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  748. (goto-char (point-max))
  749. (org-toggle-fixed-width)
  750. (buffer-string))))
  751. ;; No region: Toggle on marker in front of one line elements (e.g.,
  752. ;; headlines, clocks)
  753. (should
  754. (equal ": * Headline"
  755. (org-test-with-temp-text "* Headline"
  756. (org-toggle-fixed-width)
  757. (buffer-string))))
  758. (should
  759. (equal ": #+KEYWORD: value"
  760. (org-test-with-temp-text "#+KEYWORD: value"
  761. (org-toggle-fixed-width)
  762. (buffer-string))))
  763. ;; No region: error in other situations.
  764. (should-error
  765. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  766. (forward-line)
  767. (org-toggle-fixed-width)
  768. (buffer-string)))
  769. ;; No region: Indentation is preserved.
  770. (should
  771. (equal "- A\n : B"
  772. (org-test-with-temp-text "- A\n B"
  773. (forward-line)
  774. (org-toggle-fixed-width)
  775. (buffer-string))))
  776. ;; Region: If it contains only fixed-width elements and blank lines,
  777. ;; toggle off fixed-width markup.
  778. (should
  779. (equal "A\n\nB"
  780. (org-test-with-temp-text ": A\n\n: B"
  781. (transient-mark-mode 1)
  782. (push-mark (point) t t)
  783. (goto-char (point-max))
  784. (org-toggle-fixed-width)
  785. (buffer-string))))
  786. ;; Region: If it contains anything else, toggle on fixed-width but
  787. ;; not on fixed-width areas.
  788. (should
  789. (equal ": A\n: \n: B\n: \n: C"
  790. (org-test-with-temp-text "A\n\n: B\n\nC"
  791. (transient-mark-mode 1)
  792. (push-mark (point) t t)
  793. (goto-char (point-max))
  794. (org-toggle-fixed-width)
  795. (buffer-string))))
  796. ;; Region: Ignore blank lines at its end, unless it contains only
  797. ;; such lines.
  798. (should
  799. (equal ": A\n\n"
  800. (org-test-with-temp-text "A\n\n"
  801. (transient-mark-mode 1)
  802. (push-mark (point) t t)
  803. (goto-char (point-max))
  804. (org-toggle-fixed-width)
  805. (buffer-string))))
  806. (should
  807. (equal ": \n: \n"
  808. (org-test-with-temp-text "\n\n"
  809. (transient-mark-mode 1)
  810. (push-mark (point) t t)
  811. (goto-char (point-max))
  812. (org-toggle-fixed-width)
  813. (buffer-string)))))
  814. ;;; Headline
  815. (ert-deftest test-org/in-commented-heading-p ()
  816. "Test `org-in-commented-heading-p' specifications."
  817. ;; Commented headline.
  818. (should
  819. (org-test-with-temp-text "* COMMENT Headline\nBody"
  820. (goto-char (point-max))
  821. (org-in-commented-heading-p)))
  822. ;; Commented ancestor.
  823. (should
  824. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  825. (goto-char (point-max))
  826. (org-in-commented-heading-p)))
  827. ;; Comment keyword is case-sensitive.
  828. (should-not
  829. (org-test-with-temp-text "* Comment Headline\nBody"
  830. (goto-char (point-max))
  831. (org-in-commented-heading-p)))
  832. ;; Keyword is standalone.
  833. (should-not
  834. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  835. (goto-char (point-max))
  836. (org-in-commented-heading-p)))
  837. ;; Optional argument.
  838. (should-not
  839. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  840. (goto-char (point-max))
  841. (org-in-commented-heading-p t))))
  842. ;;; Links
  843. ;;;; Coderefs
  844. (ert-deftest test-org/coderef ()
  845. "Test coderef links specifications."
  846. (should
  847. (org-test-with-temp-text "
  848. #+BEGIN_SRC emacs-lisp
  849. \(+ 1 1) (ref:sc)
  850. #+END_SRC
  851. \[[(sc)]]"
  852. (goto-char (point-max))
  853. (org-open-at-point)
  854. (looking-at "(ref:sc)"))))
  855. ;;;; Custom ID
  856. (ert-deftest test-org/custom-id ()
  857. "Test custom ID links specifications."
  858. (should
  859. (org-test-with-temp-text
  860. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  861. (goto-char (point-max))
  862. (org-open-at-point)
  863. (org-looking-at-p "\\* H1"))))
  864. ;;;; Fuzzy Links
  865. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  866. ;; a named element (#+name: text) and to headlines (* Text).
  867. (ert-deftest test-org/fuzzy-links ()
  868. "Test fuzzy links specifications."
  869. ;; Fuzzy link goes in priority to a matching target.
  870. (should
  871. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  872. (goto-line 5)
  873. (org-open-at-point)
  874. (looking-at "<<Test>>")))
  875. ;; Then fuzzy link points to an element with a given name.
  876. (should
  877. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  878. (goto-line 5)
  879. (org-open-at-point)
  880. (looking-at "#\\+NAME: Test")))
  881. ;; A target still lead to a matching headline otherwise.
  882. (should
  883. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  884. (goto-line 4)
  885. (org-open-at-point)
  886. (looking-at "\\* Head2")))
  887. ;; With a leading star in link, enforce heading match.
  888. (should
  889. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  890. (goto-line 3)
  891. (org-open-at-point)
  892. (looking-at "\\* Test")))
  893. ;; Correctly un-hexify fuzzy links.
  894. (should
  895. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  896. (goto-char (point-max))
  897. (org-open-at-point)
  898. (bobp))))
  899. ;;;; Link Escaping
  900. (ert-deftest test-org/org-link-escape-ascii-character ()
  901. "Escape an ascii character."
  902. (should
  903. (string=
  904. "%5B"
  905. (org-link-escape "["))))
  906. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  907. "Escape an ascii control character."
  908. (should
  909. (string=
  910. "%09"
  911. (org-link-escape "\t"))))
  912. (ert-deftest test-org/org-link-escape-multibyte-character ()
  913. "Escape an unicode multibyte character."
  914. (should
  915. (string=
  916. "%E2%82%AC"
  917. (org-link-escape "€"))))
  918. (ert-deftest test-org/org-link-escape-custom-table ()
  919. "Escape string with custom character table."
  920. (should
  921. (string=
  922. "Foo%3A%42ar%0A"
  923. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  924. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  925. "Escape string with custom table merged with default table."
  926. (should
  927. (string=
  928. "%5BF%6F%6F%3A%42ar%0A%5D"
  929. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  930. (ert-deftest test-org/org-link-unescape-ascii-character ()
  931. "Unescape an ascii character."
  932. (should
  933. (string=
  934. "["
  935. (org-link-unescape "%5B"))))
  936. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  937. "Unescpae an ascii control character."
  938. (should
  939. (string=
  940. "\n"
  941. (org-link-unescape "%0A"))))
  942. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  943. "Unescape unicode multibyte character."
  944. (should
  945. (string=
  946. "€"
  947. (org-link-unescape "%E2%82%AC"))))
  948. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  949. "Unescape old style percent escaped character."
  950. (should
  951. (string=
  952. "àâçèéêîôùû"
  953. (decode-coding-string
  954. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  955. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  956. "Escape and unescape a URL that includes an escaped char.
  957. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  958. (should
  959. (string=
  960. "http://some.host.com/form?&id=blah%2Bblah25"
  961. (org-link-unescape
  962. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  963. (ert-deftest test-org/org-link-escape-chars-browser ()
  964. "Test of the constant `org-link-escape-chars-browser'.
  965. See there why this test is a candidate to be removed once Org
  966. drops support for Emacs 24.1 and 24.2."
  967. (should
  968. (string=
  969. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  970. "%22Release%208.2%22&idxname=emacs-orgmode")
  971. (org-link-escape-browser ; Do not replace with `url-encode-url',
  972. ; see docstring above.
  973. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  974. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  975. ;;;; Open at point
  976. (ert-deftest test-org/open-at-point-in-property ()
  977. "Does `org-open-at-point' open link in property drawer?"
  978. (should
  979. (org-test-with-temp-text
  980. "* Headline
  981. :PROPERTIES:
  982. :URL: <point>[[info:emacs#Top]]
  983. :END:"
  984. (org-open-at-point) t)))
  985. (ert-deftest test-org/open-at-point-in-comment ()
  986. "Does `org-open-at-point' open link in a commented line?"
  987. (should
  988. (org-test-with-temp-text
  989. "# <point>[[info:emacs#Top]]"
  990. (org-open-at-point) t)))
  991. (ert-deftest test-org/open-at-point/info ()
  992. "Test `org-open-at-point' on info links."
  993. (should
  994. (org-test-with-temp-text
  995. "<point>[[info:emacs#Top]]"
  996. (org-open-at-point)
  997. (and (switch-to-buffer "*info*")
  998. (prog1
  999. (looking-at "\nThe Emacs Editor")
  1000. (kill-buffer))))))
  1001. ;;; Node Properties
  1002. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1003. "Ensure properties accumulate in subtree drawers."
  1004. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1005. (org-babel-next-src-block)
  1006. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1007. ;;; Mark Region
  1008. (ert-deftest test-org/mark-subtree ()
  1009. "Test `org-mark-subtree' specifications."
  1010. ;; Error when point is before first headline.
  1011. (should-error
  1012. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1013. (progn (transient-mark-mode 1)
  1014. (org-mark-subtree))))
  1015. ;; Without argument, mark current subtree.
  1016. (should
  1017. (equal
  1018. '(12 32)
  1019. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1020. (progn (transient-mark-mode 1)
  1021. (forward-line 2)
  1022. (org-mark-subtree)
  1023. (list (region-beginning) (region-end))))))
  1024. ;; With an argument, move ARG up.
  1025. (should
  1026. (equal
  1027. '(1 32)
  1028. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1029. (progn (transient-mark-mode 1)
  1030. (forward-line 2)
  1031. (org-mark-subtree 1)
  1032. (list (region-beginning) (region-end))))))
  1033. ;; Do not get fooled by inlinetasks.
  1034. (when (featurep 'org-inlinetask)
  1035. (should
  1036. (= 1
  1037. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1038. (progn (transient-mark-mode 1)
  1039. (forward-line 1)
  1040. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1041. (region-beginning)))))))
  1042. ;;; Navigation
  1043. (ert-deftest test-org/beginning-of-line ()
  1044. "Test `org-beginning-of-line' specifications."
  1045. ;; Standard test.
  1046. (should
  1047. (org-test-with-temp-text "Some text\nSome other text"
  1048. (progn (org-beginning-of-line) (bolp))))
  1049. ;; Standard test with `visual-line-mode'.
  1050. (should-not
  1051. (org-test-with-temp-text "A long line of text\nSome other text"
  1052. (progn (visual-line-mode)
  1053. (forward-char 2)
  1054. (dotimes (i 1000) (insert "very "))
  1055. (org-beginning-of-line)
  1056. (bolp))))
  1057. ;; At an headline with special movement.
  1058. (should
  1059. (org-test-with-temp-text "* TODO Headline"
  1060. (let ((org-special-ctrl-a/e t))
  1061. (org-end-of-line)
  1062. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1063. (progn (org-beginning-of-line) (bolp))
  1064. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1065. (ert-deftest test-org/end-of-line ()
  1066. "Test `org-end-of-line' specifications."
  1067. ;; Standard test.
  1068. (should
  1069. (org-test-with-temp-text "Some text\nSome other text"
  1070. (progn (org-end-of-line) (eolp))))
  1071. ;; Standard test with `visual-line-mode'.
  1072. (should-not
  1073. (org-test-with-temp-text "A long line of text\nSome other text"
  1074. (progn (visual-line-mode)
  1075. (forward-char 2)
  1076. (dotimes (i 1000) (insert "very "))
  1077. (goto-char (point-min))
  1078. (org-end-of-line)
  1079. (eolp))))
  1080. ;; At an headline with special movement.
  1081. (should
  1082. (org-test-with-temp-text "* Headline1 :tag:\n"
  1083. (let ((org-special-ctrl-a/e t))
  1084. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1085. (progn (org-end-of-line) (eolp))
  1086. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1087. ;; At an headline without special movement.
  1088. (should
  1089. (org-test-with-temp-text "* Headline2 :tag:\n"
  1090. (let ((org-special-ctrl-a/e nil))
  1091. (and (progn (org-end-of-line) (eolp))
  1092. (progn (org-end-of-line) (eolp))))))
  1093. ;; At an headline, with reversed movement.
  1094. (should
  1095. (org-test-with-temp-text "* Headline3 :tag:\n"
  1096. (let ((org-special-ctrl-a/e 'reversed)
  1097. (this-command last-command))
  1098. (and (progn (org-end-of-line) (eolp))
  1099. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1100. ;; At a block without hidden contents.
  1101. (should
  1102. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1103. (progn (org-end-of-line) (eolp))))
  1104. ;; At a block with hidden contents.
  1105. (should-not
  1106. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1107. (let ((org-special-ctrl-a/e t))
  1108. (org-hide-block-toggle)
  1109. (org-end-of-line)
  1110. (eobp)))))
  1111. (ert-deftest test-org/forward-paragraph ()
  1112. "Test `org-forward-paragraph' specifications."
  1113. ;; At end of buffer, return an error.
  1114. (should-error
  1115. (org-test-with-temp-text "Paragraph"
  1116. (goto-char (point-max))
  1117. (org-forward-paragraph)))
  1118. ;; Standard test.
  1119. (should
  1120. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1121. (org-forward-paragraph)
  1122. (looking-at "P2")))
  1123. ;; Ignore depth.
  1124. (should
  1125. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1126. (org-forward-paragraph)
  1127. (looking-at "P1")))
  1128. ;; Do not enter elements with invisible contents.
  1129. (should
  1130. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1131. (org-hide-block-toggle)
  1132. (org-forward-paragraph)
  1133. (looking-at "P3")))
  1134. ;; On an affiliated keyword, jump to the beginning of the element.
  1135. (should
  1136. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1137. (org-forward-paragraph)
  1138. (looking-at "Para")))
  1139. ;; On an item or a footnote definition, move to the second element
  1140. ;; inside, if any.
  1141. (should
  1142. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1143. (org-forward-paragraph)
  1144. (looking-at " Paragraph")))
  1145. (should
  1146. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1147. (org-forward-paragraph)
  1148. (looking-at "Paragraph")))
  1149. ;; On an item, or a footnote definition, when the first line is
  1150. ;; empty, move to the first item.
  1151. (should
  1152. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1153. (org-forward-paragraph)
  1154. (looking-at " Paragraph")))
  1155. (should
  1156. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1157. (org-forward-paragraph)
  1158. (looking-at "Paragraph")))
  1159. ;; On a table (resp. a property drawer) do not move through table
  1160. ;; rows (resp. node properties).
  1161. (should
  1162. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1163. (org-forward-paragraph)
  1164. (looking-at "Paragraph")))
  1165. (should
  1166. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1167. (org-forward-paragraph)
  1168. (looking-at "Paragraph")))
  1169. ;; On a verse or source block, stop after blank lines.
  1170. (should
  1171. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1172. (org-forward-paragraph)
  1173. (looking-at "L2")))
  1174. (should
  1175. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1176. (org-forward-paragraph)
  1177. (looking-at "L2"))))
  1178. (ert-deftest test-org/backward-paragraph ()
  1179. "Test `org-backward-paragraph' specifications."
  1180. ;; Error at beginning of buffer.
  1181. (should-error
  1182. (org-test-with-temp-text "Paragraph"
  1183. (org-backward-paragraph)))
  1184. ;; Regular test.
  1185. (should
  1186. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1187. (goto-char (point-max))
  1188. (org-backward-paragraph)
  1189. (looking-at "P3")))
  1190. (should
  1191. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1192. (goto-char (point-max))
  1193. (beginning-of-line)
  1194. (org-backward-paragraph)
  1195. (looking-at "P2")))
  1196. ;; Ignore depth.
  1197. (should
  1198. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1199. (goto-char (point-max))
  1200. (beginning-of-line)
  1201. (org-backward-paragraph)
  1202. (looking-at "P2")))
  1203. ;; Ignore invisible elements.
  1204. (should
  1205. (org-test-with-temp-text "* H1\n P1\n* H2"
  1206. (org-cycle)
  1207. (goto-char (point-max))
  1208. (beginning-of-line)
  1209. (org-backward-paragraph)
  1210. (bobp)))
  1211. ;; On an affiliated keyword, jump to the first one.
  1212. (should
  1213. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1214. (search-forward "c2")
  1215. (org-backward-paragraph)
  1216. (looking-at "#\\+name")))
  1217. ;; On the second element in an item or a footnote definition, jump
  1218. ;; to item or the definition.
  1219. (should
  1220. (org-test-with-temp-text "- line1\n\n line2"
  1221. (goto-char (point-max))
  1222. (beginning-of-line)
  1223. (org-backward-paragraph)
  1224. (looking-at "- line1")))
  1225. (should
  1226. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1227. (goto-char (point-max))
  1228. (beginning-of-line)
  1229. (org-backward-paragraph)
  1230. (looking-at "\\[fn:1\\] line1")))
  1231. ;; On a table (resp. a property drawer), ignore table rows
  1232. ;; (resp. node properties).
  1233. (should
  1234. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1235. (goto-char (point-max))
  1236. (beginning-of-line)
  1237. (org-backward-paragraph)
  1238. (bobp)))
  1239. (should
  1240. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
  1241. (goto-char (point-max))
  1242. (beginning-of-line)
  1243. (org-backward-paragraph)
  1244. (bobp)))
  1245. ;; On a source or verse block, stop before blank lines.
  1246. (should
  1247. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1248. (search-forward "L3")
  1249. (beginning-of-line)
  1250. (org-backward-paragraph)
  1251. (looking-at "L2")))
  1252. (should
  1253. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1254. (search-forward "L3")
  1255. (beginning-of-line)
  1256. (org-backward-paragraph)
  1257. (looking-at "L2"))))
  1258. (ert-deftest test-org/forward-element ()
  1259. "Test `org-forward-element' specifications."
  1260. ;; 1. At EOB: should error.
  1261. (org-test-with-temp-text "Some text\n"
  1262. (goto-char (point-max))
  1263. (should-error (org-forward-element)))
  1264. ;; 2. Standard move: expected to ignore blank lines.
  1265. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1266. (org-forward-element)
  1267. (should (looking-at (regexp-quote "Second paragraph."))))
  1268. ;; 3. Headline tests.
  1269. (org-test-with-temp-text "
  1270. * Head 1
  1271. ** Head 1.1
  1272. *** Head 1.1.1
  1273. ** Head 1.2"
  1274. ;; 3.1. At an headline beginning: move to next headline at the
  1275. ;; same level.
  1276. (goto-line 3)
  1277. (org-forward-element)
  1278. (should (looking-at (regexp-quote "** Head 1.2")))
  1279. ;; 3.2. At an headline beginning: move to parent headline if no
  1280. ;; headline at the same level.
  1281. (goto-line 3)
  1282. (org-forward-element)
  1283. (should (looking-at (regexp-quote "** Head 1.2"))))
  1284. ;; 4. Greater element tests.
  1285. (org-test-with-temp-text
  1286. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1287. ;; 4.1. At a greater element: expected to skip contents.
  1288. (org-forward-element)
  1289. (should (looking-at (regexp-quote "Outside.")))
  1290. ;; 4.2. At the end of greater element contents: expected to skip
  1291. ;; to the end of the greater element.
  1292. (goto-line 2)
  1293. (org-forward-element)
  1294. (should (looking-at (regexp-quote "Outside."))))
  1295. ;; 5. List tests.
  1296. (org-test-with-temp-text "
  1297. - item1
  1298. - sub1
  1299. - sub2
  1300. - sub3
  1301. Inner paragraph.
  1302. - item2
  1303. Outside."
  1304. ;; 5.1. At list top point: expected to move to the element after
  1305. ;; the list.
  1306. (goto-line 2)
  1307. (org-forward-element)
  1308. (should (looking-at (regexp-quote "Outside.")))
  1309. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1310. ;; beginning of line, move to next item.
  1311. (goto-line 2)
  1312. (forward-char)
  1313. (org-forward-element)
  1314. (should (looking-at "- item2"))
  1315. (goto-line 4)
  1316. (forward-char)
  1317. (org-forward-element)
  1318. (should (looking-at " - sub2"))
  1319. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1320. (goto-line 4)
  1321. (org-forward-element)
  1322. (should (looking-at (regexp-quote " Inner paragraph.")))
  1323. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1324. (goto-line 8)
  1325. (org-forward-element)
  1326. (should (looking-at (regexp-quote " Inner paragraph.")))
  1327. ;; 5.5. At an item: expected to move to next item, if any.
  1328. (goto-line 6)
  1329. (org-forward-element)
  1330. (should (looking-at " - sub3"))))
  1331. (ert-deftest test-org/backward-element ()
  1332. "Test `org-backward-element' specifications."
  1333. ;; 1. Should error at BOB.
  1334. (org-test-with-temp-text " \nParagraph."
  1335. (should-error (org-backward-element)))
  1336. ;; 2. Should move at BOB when called on the first element in buffer.
  1337. (should
  1338. (org-test-with-temp-text "\n#+TITLE: test"
  1339. (progn (forward-line)
  1340. (org-backward-element)
  1341. (bobp))))
  1342. ;; 3. Not at the beginning of an element: move at its beginning.
  1343. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1344. (goto-line 3)
  1345. (end-of-line)
  1346. (org-backward-element)
  1347. (should (looking-at (regexp-quote "Paragraph2."))))
  1348. ;; 4. Headline tests.
  1349. (org-test-with-temp-text "
  1350. * Head 1
  1351. ** Head 1.1
  1352. *** Head 1.1.1
  1353. ** Head 1.2"
  1354. ;; 4.1. At an headline beginning: move to previous headline at the
  1355. ;; same level.
  1356. (goto-line 5)
  1357. (org-backward-element)
  1358. (should (looking-at (regexp-quote "** Head 1.1")))
  1359. ;; 4.2. At an headline beginning: move to parent headline if no
  1360. ;; headline at the same level.
  1361. (goto-line 3)
  1362. (org-backward-element)
  1363. (should (looking-at (regexp-quote "* Head 1")))
  1364. ;; 4.3. At the first top-level headline: should error.
  1365. (goto-line 2)
  1366. (should-error (org-backward-element)))
  1367. ;; 5. At beginning of first element inside a greater element:
  1368. ;; expected to move to greater element's beginning.
  1369. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1370. (goto-line 3)
  1371. (org-backward-element)
  1372. (should (looking-at "#\\+BEGIN_CENTER")))
  1373. ;; 6. At the beginning of the first element in a section: should
  1374. ;; move back to headline, if any.
  1375. (should
  1376. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1377. (progn (goto-char (point-max))
  1378. (beginning-of-line)
  1379. (org-backward-element)
  1380. (org-at-heading-p))))
  1381. ;; 7. List tests.
  1382. (org-test-with-temp-text "
  1383. - item1
  1384. - sub1
  1385. - sub2
  1386. - sub3
  1387. Inner paragraph.
  1388. - item2
  1389. Outside."
  1390. ;; 7.1. At beginning of sub-list: expected to move to the
  1391. ;; paragraph before it.
  1392. (goto-line 4)
  1393. (org-backward-element)
  1394. (should (looking-at "item1"))
  1395. ;; 7.2. At an item in a list: expected to move at previous item.
  1396. (goto-line 8)
  1397. (org-backward-element)
  1398. (should (looking-at " - sub2"))
  1399. (goto-line 12)
  1400. (org-backward-element)
  1401. (should (looking-at "- item1"))
  1402. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1403. ;; beginning.
  1404. (goto-line 10)
  1405. (org-backward-element)
  1406. (should (looking-at " - sub1"))
  1407. (goto-line 15)
  1408. (org-backward-element)
  1409. (should (looking-at "- item1"))
  1410. ;; 7.4. At blank-lines before list end: expected to move to top
  1411. ;; item.
  1412. (goto-line 14)
  1413. (org-backward-element)
  1414. (should (looking-at "- item1"))))
  1415. (ert-deftest test-org/up-element ()
  1416. "Test `org-up-element' specifications."
  1417. ;; 1. At BOB or with no surrounding element: should error.
  1418. (org-test-with-temp-text "Paragraph."
  1419. (should-error (org-up-element)))
  1420. (org-test-with-temp-text "* Head1\n* Head2"
  1421. (goto-line 2)
  1422. (should-error (org-up-element)))
  1423. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1424. (goto-line 3)
  1425. (should-error (org-up-element)))
  1426. ;; 2. At an headline: move to parent headline.
  1427. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1428. (goto-line 3)
  1429. (org-up-element)
  1430. (should (looking-at "\\* Head1")))
  1431. ;; 3. Inside a greater element: move to greater element beginning.
  1432. (org-test-with-temp-text
  1433. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1434. (goto-line 3)
  1435. (org-up-element)
  1436. (should (looking-at "#\\+BEGIN_CENTER")))
  1437. ;; 4. List tests.
  1438. (org-test-with-temp-text "* Top
  1439. - item1
  1440. - sub1
  1441. - sub2
  1442. Paragraph within sub2.
  1443. - item2"
  1444. ;; 4.1. Within an item: move to the item beginning.
  1445. (goto-line 8)
  1446. (org-up-element)
  1447. (should (looking-at " - sub2"))
  1448. ;; 4.2. At an item in a sub-list: move to parent item.
  1449. (goto-line 4)
  1450. (org-up-element)
  1451. (should (looking-at "- item1"))
  1452. ;; 4.3. At an item in top list: move to beginning of whole list.
  1453. (goto-line 10)
  1454. (org-up-element)
  1455. (should (looking-at "- item1"))
  1456. ;; 4.4. Special case. At very top point: should move to parent of
  1457. ;; list.
  1458. (goto-line 2)
  1459. (org-up-element)
  1460. (should (looking-at "\\* Top"))))
  1461. (ert-deftest test-org/down-element ()
  1462. "Test `org-down-element' specifications."
  1463. ;; Error when the element hasn't got a recursive type.
  1464. (org-test-with-temp-text "Paragraph."
  1465. (should-error (org-down-element)))
  1466. ;; Error when the element has no contents
  1467. (org-test-with-temp-text "* Headline"
  1468. (should-error (org-down-element)))
  1469. ;; When at a plain-list, move to first item.
  1470. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1471. (goto-line 2)
  1472. (org-down-element)
  1473. (should (looking-at " - Item 1.1")))
  1474. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1475. (org-down-element)
  1476. (should (looking-at " Item 1")))
  1477. ;; When at a table, move to first row
  1478. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1479. (org-down-element)
  1480. (should (looking-at " a | b |")))
  1481. ;; Otherwise, move inside the greater element.
  1482. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1483. (org-down-element)
  1484. (should (looking-at "Paragraph"))))
  1485. (ert-deftest test-org/drag-element-backward ()
  1486. "Test `org-drag-element-backward' specifications."
  1487. ;; Standard test.
  1488. (should
  1489. (equal
  1490. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  1491. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  1492. (org-drag-element-backward)
  1493. (buffer-string))))
  1494. (should
  1495. (equal
  1496. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  1497. (org-test-with-temp-text
  1498. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  1499. (org-drag-element-backward)
  1500. (buffer-string))))
  1501. ;; Preserve blank lines.
  1502. (should
  1503. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  1504. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  1505. (org-drag-element-backward)
  1506. (buffer-string))))
  1507. ;; Preserve column.
  1508. (should
  1509. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  1510. (org-drag-element-backward)
  1511. (org-looking-at-p "2")))
  1512. ;; Error when trying to move first element of buffer.
  1513. (should-error
  1514. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1515. (org-drag-element-backward)))
  1516. ;; Error when trying to swap nested elements.
  1517. (should-error
  1518. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1519. (forward-line)
  1520. (org-drag-element-backward)))
  1521. ;; Error when trying to swap an headline element and a non-headline
  1522. ;; element.
  1523. (should-error
  1524. (org-test-with-temp-text "Test.\n* Head 1"
  1525. (forward-line)
  1526. (org-drag-element-backward)))
  1527. ;; Preserve visibility of elements and their contents.
  1528. (should
  1529. (equal '((63 . 82) (26 . 48))
  1530. (org-test-with-temp-text "
  1531. #+BEGIN_CENTER
  1532. Text.
  1533. #+END_CENTER
  1534. - item 1
  1535. #+BEGIN_QUOTE
  1536. Text.
  1537. #+END_QUOTE"
  1538. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1539. (search-backward "- item 1")
  1540. (org-drag-element-backward)
  1541. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1542. (overlays-in (point-min) (point-max)))))))
  1543. (ert-deftest test-org/drag-element-forward ()
  1544. "Test `org-drag-element-forward' specifications."
  1545. ;; 1. Error when trying to move first element of buffer.
  1546. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1547. (goto-line 3)
  1548. (should-error (org-drag-element-forward)))
  1549. ;; 2. Error when trying to swap nested elements.
  1550. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1551. (forward-line)
  1552. (should-error (org-drag-element-forward)))
  1553. ;; 3. Error when trying to swap a non-headline element and an
  1554. ;; headline.
  1555. (org-test-with-temp-text "Test.\n* Head 1"
  1556. (should-error (org-drag-element-forward)))
  1557. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1558. ;; between elements.
  1559. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1560. (search-forward "graph")
  1561. (org-drag-element-forward)
  1562. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1563. (should (looking-at " 1")))
  1564. ;; 5. Preserve visibility of elements and their contents.
  1565. (org-test-with-temp-text "
  1566. #+BEGIN_CENTER
  1567. Text.
  1568. #+END_CENTER
  1569. - item 1
  1570. #+BEGIN_QUOTE
  1571. Text.
  1572. #+END_QUOTE"
  1573. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1574. (search-backward "#+BEGIN_CENTER")
  1575. (org-drag-element-forward)
  1576. (should
  1577. (equal
  1578. '((63 . 82) (26 . 48))
  1579. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1580. (overlays-in (point-min) (point-max)))))))
  1581. ;;; Planning
  1582. (ert-deftest test-org/timestamp-has-time-p ()
  1583. "Test `org-timestamp-has-time-p' specifications."
  1584. ;; With time.
  1585. (should
  1586. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1587. (org-timestamp-has-time-p (org-element-context))))
  1588. ;; Without time.
  1589. (should-not
  1590. (org-test-with-temp-text "<2012-03-29 Thu>"
  1591. (org-timestamp-has-time-p (org-element-context)))))
  1592. (ert-deftest test-org/timestamp-format ()
  1593. "Test `org-timestamp-format' specifications."
  1594. ;; Regular test.
  1595. (should
  1596. (equal
  1597. "2012-03-29 16:40"
  1598. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1599. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1600. ;; Range end.
  1601. (should
  1602. (equal
  1603. "2012-03-29"
  1604. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1605. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1606. (ert-deftest test-org/timestamp-split-range ()
  1607. "Test `org-timestamp-split-range' specifications."
  1608. ;; Extract range start (active).
  1609. (should
  1610. (equal '(2012 3 29)
  1611. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1612. (let ((ts (org-timestamp-split-range (org-element-context))))
  1613. (mapcar (lambda (p) (org-element-property p ts))
  1614. '(:year-end :month-end :day-end))))))
  1615. ;; Extract range start (inactive)
  1616. (should
  1617. (equal '(2012 3 29)
  1618. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1619. (let ((ts (org-timestamp-split-range (org-element-context))))
  1620. (mapcar (lambda (p) (org-element-property p ts))
  1621. '(:year-end :month-end :day-end))))))
  1622. ;; Extract range end (active).
  1623. (should
  1624. (equal '(2012 3 30)
  1625. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1626. (let ((ts (org-timestamp-split-range
  1627. (org-element-context) t)))
  1628. (mapcar (lambda (p) (org-element-property p ts))
  1629. '(:year-end :month-end :day-end))))))
  1630. ;; Extract range end (inactive)
  1631. (should
  1632. (equal '(2012 3 30)
  1633. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1634. (let ((ts (org-timestamp-split-range
  1635. (org-element-context) t)))
  1636. (mapcar (lambda (p) (org-element-property p ts))
  1637. '(:year-end :month-end :day-end))))))
  1638. ;; Return the timestamp if not a range.
  1639. (should
  1640. (org-test-with-temp-text "[2012-03-29 Thu]"
  1641. (let* ((ts-orig (org-element-context))
  1642. (ts-copy (org-timestamp-split-range ts-orig)))
  1643. (eq ts-orig ts-copy))))
  1644. (should
  1645. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1646. (let* ((ts-orig (org-element-context))
  1647. (ts-copy (org-timestamp-split-range ts-orig)))
  1648. (eq ts-orig ts-copy))))
  1649. ;; Check that parent is the same when a range was split.
  1650. (should
  1651. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1652. (let* ((ts-orig (org-element-context))
  1653. (ts-copy (org-timestamp-split-range ts-orig)))
  1654. (eq (org-element-property :parent ts-orig)
  1655. (org-element-property :parent ts-copy))))))
  1656. (ert-deftest test-org/timestamp-translate ()
  1657. "Test `org-timestamp-translate' specifications."
  1658. ;; Translate whole date range.
  1659. (should
  1660. (equal "<29>--<30>"
  1661. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1662. (let ((org-display-custom-times t)
  1663. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1664. (org-timestamp-translate (org-element-context))))))
  1665. ;; Translate date range start.
  1666. (should
  1667. (equal "<29>"
  1668. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1669. (let ((org-display-custom-times t)
  1670. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1671. (org-timestamp-translate (org-element-context) 'start)))))
  1672. ;; Translate date range end.
  1673. (should
  1674. (equal "<30>"
  1675. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1676. (let ((org-display-custom-times t)
  1677. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1678. (org-timestamp-translate (org-element-context) 'end)))))
  1679. ;; Translate time range.
  1680. (should
  1681. (equal "<08>--<16>"
  1682. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1683. (let ((org-display-custom-times t)
  1684. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1685. (org-timestamp-translate (org-element-context))))))
  1686. ;; Translate non-range timestamp.
  1687. (should
  1688. (equal "<29>"
  1689. (org-test-with-temp-text "<2012-03-29 Thu>"
  1690. (let ((org-display-custom-times t)
  1691. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1692. (org-timestamp-translate (org-element-context))))))
  1693. ;; Do not change `diary' timestamps.
  1694. (should
  1695. (equal "<%%(org-float t 4 2)>"
  1696. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1697. (let ((org-display-custom-times t)
  1698. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1699. (org-timestamp-translate (org-element-context)))))))
  1700. ;;; Radio Targets
  1701. (ert-deftest test-org/update-radio-target-regexp ()
  1702. "Test `org-update-radio-target-regexp' specifications."
  1703. ;; Properly update cache with no previous radio target regexp.
  1704. (should
  1705. (eq 'link
  1706. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  1707. (save-excursion (goto-char (point-max)) (org-element-context))
  1708. (insert "<<<")
  1709. (search-forward "o")
  1710. (insert ">>>")
  1711. (org-update-radio-target-regexp)
  1712. (goto-char (point-max))
  1713. (org-element-type (org-element-context)))))
  1714. ;; Properly update cache with previous radio target regexp.
  1715. (should
  1716. (eq 'link
  1717. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  1718. (save-excursion (goto-char (point-max)) (org-element-context))
  1719. (insert "<<<")
  1720. (search-forward "o")
  1721. (insert ">>>")
  1722. (org-update-radio-target-regexp)
  1723. (search-backward "r")
  1724. (delete-char 5)
  1725. (insert "new")
  1726. (org-update-radio-target-regexp)
  1727. (goto-char (point-max))
  1728. (delete-region (line-beginning-position) (point))
  1729. (insert "new")
  1730. (org-element-type (org-element-context))))))
  1731. ;;; Visibility
  1732. (ert-deftest test-org/flag-drawer ()
  1733. "Test `org-flag-drawer' specifications."
  1734. ;; Hide drawer.
  1735. (should
  1736. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1737. (org-flag-drawer t)
  1738. (get-char-property (line-end-position) 'invisible)))
  1739. ;; Show drawer.
  1740. (should-not
  1741. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1742. (org-flag-drawer t)
  1743. (org-flag-drawer nil)
  1744. (get-char-property (line-end-position) 'invisible)))
  1745. ;; Test optional argument.
  1746. (should
  1747. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1748. (let ((drawer (save-excursion (search-forward ":D2")
  1749. (org-element-at-point))))
  1750. (org-flag-drawer t drawer)
  1751. (get-char-property (progn (search-forward ":D2") (line-end-position))
  1752. 'invisible))))
  1753. (should-not
  1754. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1755. (let ((drawer (save-excursion (search-forward ":D2")
  1756. (org-element-at-point))))
  1757. (org-flag-drawer t drawer)
  1758. (get-char-property (line-end-position) 'invisible))))
  1759. ;; Do not hide fake drawers.
  1760. (should-not
  1761. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  1762. (forward-line 1)
  1763. (org-flag-drawer t)
  1764. (get-char-property (line-end-position) 'invisible)))
  1765. ;; Do not hide incomplete drawers.
  1766. (should-not
  1767. (org-test-with-temp-text ":D:\nparagraph"
  1768. (forward-line 1)
  1769. (org-flag-drawer t)
  1770. (get-char-property (line-end-position) 'invisible)))
  1771. ;; Do not hide drawers when called from final blank lines.
  1772. (should-not
  1773. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  1774. (goto-char (point-max))
  1775. (org-flag-drawer t)
  1776. (goto-char (point-min))
  1777. (get-char-property (line-end-position) 'invisible)))
  1778. ;; Don't leave point in an invisible part of the buffer when hiding
  1779. ;; a drawer away.
  1780. (should-not
  1781. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1782. (goto-char (point-max))
  1783. (org-flag-drawer t)
  1784. (get-char-property (point) 'invisible))))
  1785. (ert-deftest test-org/hide-block-toggle ()
  1786. "Test `org-hide-block-toggle' specifications."
  1787. ;; Error when not at a block.
  1788. (should-error
  1789. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  1790. (org-hide-block-toggle 'off)
  1791. (get-char-property (line-end-position) 'invisible)))
  1792. ;; Hide block.
  1793. (should
  1794. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  1795. (org-hide-block-toggle)
  1796. (get-char-property (line-end-position) 'invisible)))
  1797. (should
  1798. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  1799. (org-hide-block-toggle)
  1800. (get-char-property (line-end-position) 'invisible)))
  1801. ;; Show block unconditionally when optional argument is `off'.
  1802. (should-not
  1803. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  1804. (org-hide-block-toggle)
  1805. (org-hide-block-toggle 'off)
  1806. (get-char-property (line-end-position) 'invisible)))
  1807. (should-not
  1808. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  1809. (org-hide-block-toggle 'off)
  1810. (get-char-property (line-end-position) 'invisible)))
  1811. ;; Hide block unconditionally when optional argument is non-nil.
  1812. (should
  1813. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  1814. (org-hide-block-toggle t)
  1815. (get-char-property (line-end-position) 'invisible)))
  1816. (should
  1817. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  1818. (org-hide-block-toggle)
  1819. (org-hide-block-toggle t)
  1820. (get-char-property (line-end-position) 'invisible)))
  1821. ;; Do not hide block when called from final blank lines.
  1822. (should-not
  1823. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  1824. (org-hide-block-toggle)
  1825. (goto-char (point-min))
  1826. (get-char-property (line-end-position) 'invisible)))
  1827. ;; Don't leave point in an invisible part of the buffer when hiding
  1828. ;; a block away.
  1829. (should-not
  1830. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  1831. (org-hide-block-toggle)
  1832. (get-char-property (point) 'invisible))))
  1833. (provide 'test-org)
  1834. ;;; test-org.el ends here