test-org.el 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986
  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. ;;; Drawers
  192. (ert-deftest test-org/insert-property-drawer ()
  193. "Test `org-insert-property-drawer' specifications."
  194. ;; Error before first headline.
  195. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  196. ;; Insert drawer right after headline if there is no planning line,
  197. ;; or after it otherwise.
  198. (should
  199. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  200. (org-test-with-temp-text "* H\nParagraph<point>"
  201. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  202. (buffer-string))))
  203. (should
  204. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  205. (org-test-with-temp-text
  206. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  207. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  208. (buffer-string))))
  209. ;; Indent inserted drawer.
  210. (should
  211. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  212. (org-test-with-temp-text "* H\nParagraph<point>"
  213. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  214. (buffer-string))))
  215. ;; Handle insertion at eob.
  216. (should
  217. (equal "* H\n:PROPERTIES:\n:END:\n"
  218. (org-test-with-temp-text "* H"
  219. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  220. (buffer-string))))
  221. ;; Skip inlinetasks before point.
  222. (when (featurep 'org-inlinetask)
  223. (should
  224. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  225. (org-test-with-temp-text
  226. "* H\n*************** I\n*************** END\nP<point>"
  227. (let ((org-adapt-indentation nil)
  228. (org-inlinetask-min-level 15))
  229. (org-insert-property-drawer))
  230. (buffer-string)))))
  231. ;; Correctly set drawer in an inlinetask.
  232. (when (featurep 'org-inlinetask)
  233. (should
  234. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  235. (org-test-with-temp-text
  236. "* H\n*************** I\nP<point>\n*************** END"
  237. (let ((org-adapt-indentation nil)
  238. (org-inlinetask-min-level 15))
  239. (org-insert-property-drawer))
  240. (buffer-string))))))
  241. ;;; Filling
  242. (ert-deftest test-org/fill-paragraph ()
  243. "Test `org-fill-paragraph' specifications."
  244. ;; At an Org table, align it.
  245. (should
  246. (equal "| a |\n"
  247. (org-test-with-temp-text "|a|"
  248. (org-fill-paragraph)
  249. (buffer-string))))
  250. (should
  251. (equal "#+name: table\n| a |\n"
  252. (org-test-with-temp-text "#+name: table\n| a |"
  253. (org-fill-paragraph)
  254. (buffer-string))))
  255. ;; At a paragraph, preserve line breaks.
  256. (org-test-with-temp-text "some \\\\\nlong\ntext"
  257. (let ((fill-column 20))
  258. (org-fill-paragraph)
  259. (should (equal (buffer-string) "some \\\\\nlong text"))))
  260. ;; Correctly fill a paragraph when point is at its very end.
  261. (should
  262. (equal "A B"
  263. (org-test-with-temp-text "A\nB"
  264. (let ((fill-column 20))
  265. (goto-char (point-max))
  266. (org-fill-paragraph)
  267. (buffer-string)))))
  268. ;; Correctly fill the last paragraph of a greater element.
  269. (should
  270. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  271. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  272. (let ((fill-column 8))
  273. (forward-line)
  274. (end-of-line)
  275. (org-fill-paragraph)
  276. (buffer-string)))))
  277. ;; Correctly fill an element in a narrowed buffer.
  278. (should
  279. (equal "01234\n6"
  280. (org-test-with-temp-text "01234 6789"
  281. (let ((fill-column 5))
  282. (narrow-to-region 1 8)
  283. (org-fill-paragraph)
  284. (buffer-string)))))
  285. ;; Handle `adaptive-fill-regexp' in paragraphs.
  286. (should
  287. (equal "> a b"
  288. (org-test-with-temp-text "> a\n> b"
  289. (let ((fill-column 5)
  290. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  291. (org-fill-paragraph)
  292. (buffer-string)))))
  293. ;; Special case: Fill first paragraph when point is at an item or
  294. ;; a plain-list or a footnote reference.
  295. (should
  296. (equal "- A B"
  297. (org-test-with-temp-text "- A\n B"
  298. (let ((fill-column 20))
  299. (org-fill-paragraph)
  300. (buffer-string)))))
  301. (should
  302. (equal "[fn:1] A B"
  303. (org-test-with-temp-text "[fn:1] A\nB"
  304. (let ((fill-column 20))
  305. (org-fill-paragraph)
  306. (buffer-string)))))
  307. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  308. (let ((fill-column 20))
  309. (org-fill-paragraph)
  310. (should (equal (buffer-string)
  311. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  312. ;; Fill contents of `comment-block' elements.
  313. (should
  314. (equal
  315. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  316. (let ((fill-column 20))
  317. (forward-line)
  318. (org-fill-paragraph)
  319. (buffer-string)))
  320. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  321. ;; Fill `comment' elements.
  322. (should
  323. (equal " # A B"
  324. (org-test-with-temp-text " # A\n # B"
  325. (let ((fill-column 20))
  326. (org-fill-paragraph)
  327. (buffer-string)))))
  328. ;; Do not mix consecutive comments when filling one of them.
  329. (should
  330. (equal "# A B\n\n# C"
  331. (org-test-with-temp-text "# A\n# B\n\n# C"
  332. (let ((fill-column 20))
  333. (org-fill-paragraph)
  334. (buffer-string)))))
  335. ;; Use commented empty lines as separators when filling comments.
  336. (should
  337. (equal "# A B\n#\n# C"
  338. (org-test-with-temp-text "# A\n# B\n#\n# C"
  339. (let ((fill-column 20))
  340. (org-fill-paragraph)
  341. (buffer-string)))))
  342. ;; Handle `adaptive-fill-regexp' in comments.
  343. (should
  344. (equal "# > a b"
  345. (org-test-with-temp-text "# > a\n# > b"
  346. (let ((fill-column 20)
  347. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  348. (org-fill-paragraph)
  349. (buffer-string)))))
  350. ;; Do nothing at affiliated keywords.
  351. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  352. (let ((fill-column 20))
  353. (org-fill-paragraph)
  354. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  355. ;; Do not move point after table when filling a table.
  356. (should-not
  357. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  358. (forward-char)
  359. (org-fill-paragraph)
  360. (eobp))))
  361. (ert-deftest test-org/auto-fill-function ()
  362. "Test auto-filling features."
  363. ;; Auto fill paragraph.
  364. (should
  365. (equal "12345\n7890"
  366. (org-test-with-temp-text "12345 7890"
  367. (let ((fill-column 5))
  368. (end-of-line)
  369. (org-auto-fill-function)
  370. (buffer-string)))))
  371. ;; Auto fill first paragraph in an item.
  372. (should
  373. (equal "- 12345\n 7890"
  374. (org-test-with-temp-text "- 12345 7890"
  375. (let ((fill-column 7))
  376. (end-of-line)
  377. (org-auto-fill-function)
  378. (buffer-string)))))
  379. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  380. (should
  381. (equal "> 12345\n 7890"
  382. (org-test-with-temp-text "> 12345 7890"
  383. (let ((fill-column 10)
  384. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  385. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  386. (end-of-line)
  387. (org-auto-fill-function)
  388. (buffer-string)))))
  389. (should
  390. (equal "> 12345\n> 12345\n> 7890"
  391. (org-test-with-temp-text "> 12345\n> 12345 7890"
  392. (let ((fill-column 10)
  393. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  394. (goto-char (point-max))
  395. (org-auto-fill-function)
  396. (buffer-string)))))
  397. (should-not
  398. (equal " 12345\n *12345\n *12345"
  399. (org-test-with-temp-text " 12345\n *12345 12345"
  400. (let ((fill-column 10)
  401. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  402. (goto-char (point-max))
  403. (org-auto-fill-function)
  404. (buffer-string)))))
  405. ;; Auto fill comments.
  406. (should
  407. (equal " # 12345\n # 7890"
  408. (org-test-with-temp-text " # 12345 7890"
  409. (let ((fill-column 10))
  410. (end-of-line)
  411. (org-auto-fill-function)
  412. (buffer-string)))))
  413. ;; A hash within a line isn't a comment.
  414. (should-not
  415. (equal "12345 # 7890\n# 1"
  416. (org-test-with-temp-text "12345 # 7890 1"
  417. (let ((fill-column 12))
  418. (end-of-line)
  419. (org-auto-fill-function)
  420. (buffer-string)))))
  421. ;; Correctly interpret empty prefix.
  422. (should-not
  423. (equal "# a\n# b\nRegular\n# paragraph"
  424. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  425. (let ((fill-column 12))
  426. (end-of-line 3)
  427. (org-auto-fill-function)
  428. (buffer-string)))))
  429. ;; Comment block: auto fill contents.
  430. (should
  431. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  432. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  433. (let ((fill-column 5))
  434. (forward-line)
  435. (end-of-line)
  436. (org-auto-fill-function)
  437. (buffer-string)))))
  438. (should
  439. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  440. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  441. (let ((fill-column 5))
  442. (forward-line)
  443. (end-of-line)
  444. (org-auto-fill-function)
  445. (buffer-string)))))
  446. ;; Do not fill if a new item could be created.
  447. (should-not
  448. (equal "12345\n- 90"
  449. (org-test-with-temp-text "12345 - 90"
  450. (let ((fill-column 5))
  451. (end-of-line)
  452. (org-auto-fill-function)
  453. (buffer-string)))))
  454. ;; Do not fill if a line break could be introduced.
  455. (should-not
  456. (equal "123\\\\\n7890"
  457. (org-test-with-temp-text "123\\\\ 7890"
  458. (let ((fill-column 6))
  459. (end-of-line)
  460. (org-auto-fill-function)
  461. (buffer-string)))))
  462. ;; Do not fill affiliated keywords.
  463. (should-not
  464. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  465. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  466. (let ((fill-column 20))
  467. (end-of-line)
  468. (org-auto-fill-function)
  469. (buffer-string))))))
  470. ;;; Indentation
  471. (ert-deftest test-org/indent-line ()
  472. "Test `org-indent-line' specifications."
  473. ;; Do not indent footnote definitions or headlines.
  474. (should
  475. (zerop
  476. (org-test-with-temp-text "* H"
  477. (org-indent-line)
  478. (org-get-indentation))))
  479. (should
  480. (zerop
  481. (org-test-with-temp-text "[fn:1] fn"
  482. (let ((org-adapt-indentation t)) (org-indent-line))
  483. (org-get-indentation))))
  484. ;; Do not indent before first headline.
  485. (should
  486. (zerop
  487. (org-test-with-temp-text ""
  488. (org-indent-line)
  489. (org-get-indentation))))
  490. ;; Indent according to headline level otherwise, unless
  491. ;; `org-adapt-indentation' is nil.
  492. (should
  493. (= 2
  494. (org-test-with-temp-text "* H\nA"
  495. (forward-line)
  496. (let ((org-adapt-indentation t)) (org-indent-line))
  497. (org-get-indentation))))
  498. (should
  499. (= 2
  500. (org-test-with-temp-text "* H\n\nA"
  501. (forward-line)
  502. (let ((org-adapt-indentation t)) (org-indent-line))
  503. (org-get-indentation))))
  504. (should
  505. (zerop
  506. (org-test-with-temp-text "* H\nA"
  507. (forward-line)
  508. (let ((org-adapt-indentation nil)) (org-indent-line))
  509. (org-get-indentation))))
  510. ;; Indenting preserves point position.
  511. (should
  512. (org-test-with-temp-text "* H\nAB"
  513. (forward-line)
  514. (forward-char)
  515. (let ((org-adapt-indentation t)) (org-indent-line))
  516. (looking-at "B")))
  517. ;; Do not change indentation at an item.
  518. (should
  519. (= 1
  520. (org-test-with-temp-text "* H\n - A"
  521. (forward-line)
  522. (let ((org-adapt-indentation t)) (org-indent-line))
  523. (org-get-indentation))))
  524. ;; On blank lines at the end of a list, indent like last element
  525. ;; within it if the line is still in the list. Otherwise, indent
  526. ;; like the whole list.
  527. (should
  528. (= 4
  529. (org-test-with-temp-text "* H\n- A\n - AA\n"
  530. (goto-char (point-max))
  531. (let ((org-adapt-indentation t)) (org-indent-line))
  532. (org-get-indentation))))
  533. (should
  534. (zerop
  535. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
  536. (goto-char (point-max))
  537. (let ((org-adapt-indentation t)) (org-indent-line))
  538. (org-get-indentation))))
  539. ;; Likewise, on a blank line at the end of a footnote definition,
  540. ;; indent at column 0 if line belongs to the definition. Otherwise,
  541. ;; indent like the definition itself.
  542. (should
  543. (zerop
  544. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  545. (goto-char (point-max))
  546. (let ((org-adapt-indentation t)) (org-indent-line))
  547. (org-get-indentation))))
  548. (should
  549. (zerop
  550. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  551. (goto-char (point-max))
  552. (let ((org-adapt-indentation t)) (org-indent-line))
  553. (org-get-indentation))))
  554. ;; After the end of the contents of a greater element, indent like
  555. ;; the beginning of the element.
  556. (should
  557. (= 1
  558. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  559. (forward-line 2)
  560. (org-indent-line)
  561. (org-get-indentation))))
  562. ;; At the first line of an element, indent like previous element's
  563. ;; first line, ignoring footnotes definitions and inline tasks, or
  564. ;; according to parent.
  565. (should
  566. (= 2
  567. (org-test-with-temp-text "A\n\n B\n\nC"
  568. (goto-char (point-max))
  569. (org-indent-line)
  570. (org-get-indentation))))
  571. (should
  572. (= 1
  573. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  574. (goto-char (point-max))
  575. (org-indent-line)
  576. (org-get-indentation))))
  577. (should
  578. (= 1
  579. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  580. (forward-line 1)
  581. (org-indent-line)
  582. (org-get-indentation))))
  583. ;; Within code part of a source block, use language major mode if
  584. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  585. ;; according to line above.
  586. (should
  587. (= 6
  588. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  589. (forward-line 2)
  590. (let ((org-src-tab-acts-natively t)
  591. (org-edit-src-content-indentation 0))
  592. (org-indent-line))
  593. (org-get-indentation))))
  594. (should
  595. (= 1
  596. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  597. (forward-line 2)
  598. (let ((org-src-tab-acts-natively nil)
  599. (org-edit-src-content-indentation 0))
  600. (org-indent-line))
  601. (org-get-indentation))))
  602. ;; Otherwise, indent like the first non-blank line above.
  603. (should
  604. (zerop
  605. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  606. (forward-line 3)
  607. (org-indent-line)
  608. (org-get-indentation))))
  609. ;; Align node properties according to `org-property-format'. Handle
  610. ;; nicely empty values.
  611. (should
  612. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  613. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  614. (let ((org-property-format "%-10s %s")) (org-indent-line))
  615. (buffer-string))))
  616. (should
  617. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  618. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  619. (let ((org-property-format "%-10s %s")) (org-indent-line))
  620. (buffer-string)))))
  621. (ert-deftest test-org/indent-region ()
  622. "Test `org-indent-region' specifications."
  623. ;; Indent paragraph.
  624. (should
  625. (equal "A\nB\nC"
  626. (org-test-with-temp-text " A\nB\n C"
  627. (org-indent-region (point-min) (point-max))
  628. (buffer-string))))
  629. ;; Indent greater elements along with their contents.
  630. (should
  631. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  632. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  633. (org-indent-region (point-min) (point-max))
  634. (buffer-string))))
  635. ;; Ignore contents of verse blocks and example blocks.
  636. (should
  637. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  638. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  639. (org-indent-region (point-min) (point-max))
  640. (buffer-string))))
  641. (should
  642. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  643. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  644. (org-indent-region (point-min) (point-max))
  645. (buffer-string))))
  646. ;; Indent according to mode if `org-src-tab-acts-natively' is
  647. ;; non-nil. Otherwise, do not indent code at all.
  648. (should
  649. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  650. (org-test-with-temp-text
  651. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  652. (let ((org-src-tab-acts-natively t)
  653. (org-edit-src-content-indentation 0))
  654. (org-indent-region (point-min) (point-max)))
  655. (buffer-string))))
  656. (should
  657. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  658. (org-test-with-temp-text
  659. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  660. (let ((org-src-tab-acts-natively nil)
  661. (org-edit-src-content-indentation 0))
  662. (org-indent-region (point-min) (point-max)))
  663. (buffer-string))))
  664. ;; Align node properties according to `org-property-format'. Handle
  665. ;; nicely empty values.
  666. (should
  667. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  668. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  669. (let ((org-property-format "%-10s %s")
  670. (org-adapt-indentation nil))
  671. (org-indent-region (point) (point-max)))
  672. (buffer-string))))
  673. (should
  674. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  675. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  676. (let ((org-property-format "%-10s %s")
  677. (org-adapt-indentation nil))
  678. (org-indent-region (point) (point-max)))
  679. (buffer-string))))
  680. ;; Indent plain lists.
  681. (should
  682. (equal "- A\n B\n - C\n\n D"
  683. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  684. (org-indent-region (point-min) (point-max))
  685. (buffer-string))))
  686. (should
  687. (equal "- A\n\n- B"
  688. (org-test-with-temp-text " - A\n\n - B"
  689. (org-indent-region (point-min) (point-max))
  690. (buffer-string))))
  691. ;; Indent footnote definitions.
  692. (should
  693. (equal "[fn:1] Definition\n\nDefinition"
  694. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  695. (org-indent-region (point-min) (point-max))
  696. (buffer-string))))
  697. ;; Special case: Start indenting on a blank line.
  698. (should
  699. (equal "\nParagraph"
  700. (org-test-with-temp-text "\n Paragraph"
  701. (org-indent-region (point-min) (point-max))
  702. (buffer-string)))))
  703. ;;; Editing
  704. (ert-deftest test-org/return ()
  705. "Test RET (`org-return') specifications."
  706. ;; Regular test.
  707. (should
  708. (equal "Para\ngraph"
  709. (org-test-with-temp-text "Para<point>graph"
  710. (org-return)
  711. (buffer-string))))
  712. ;; With optional argument, indent line.
  713. (should
  714. (equal " Para\n graph"
  715. (org-test-with-temp-text " Para<point>graph"
  716. (org-return t)
  717. (buffer-string))))
  718. ;; On a table, call `org-table-next-row'.
  719. (should
  720. (org-test-with-temp-text "| <point>a |\n| b |"
  721. (org-return)
  722. (org-looking-at-p "b")))
  723. ;; Open link or timestamp under point when `org-return-follows-link'
  724. ;; is non-nil.
  725. (should
  726. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  727. (let ((org-return-follows-link t)) (org-return))
  728. (org-looking-at-p "<<target>>")))
  729. (should-not
  730. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  731. (let ((org-return-follows-link nil)) (org-return))
  732. (org-looking-at-p "<<target>>")))
  733. ;; However, do not open link when point is in a table.
  734. (should
  735. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  736. (let ((org-return-follows-link t)) (org-return))
  737. (org-looking-at-p "between")))
  738. ;; Special case: in a list, when indenting, do not break structure.
  739. (should
  740. (equal "- A\n B"
  741. (org-test-with-temp-text "- A <point>B"
  742. (org-return t)
  743. (buffer-string))))
  744. ;; Special case: on tags part of a headline, add a newline below it
  745. ;; instead of breaking it.
  746. (should
  747. (equal "* H :tag:\n"
  748. (org-test-with-temp-text "* H :<point>tag:"
  749. (org-return)
  750. (buffer-string)))))
  751. (ert-deftest test-org/meta-return ()
  752. "Test M-RET (`org-meta-return') specifications."
  753. ;; In a table field insert a row above.
  754. (should
  755. (org-test-with-temp-text "| a |"
  756. (forward-char)
  757. (org-meta-return)
  758. (forward-line -1)
  759. (looking-at "| |$")))
  760. ;; In a paragraph change current line into a header.
  761. (should
  762. (org-test-with-temp-text "a"
  763. (org-meta-return)
  764. (beginning-of-line)
  765. (looking-at "\* a$")))
  766. ;; In an item insert an item, in this case above.
  767. (should
  768. (org-test-with-temp-text "- a"
  769. (org-meta-return)
  770. (beginning-of-line)
  771. (looking-at "- $")))
  772. ;; In a drawer and item insert an item, in this case above.
  773. (should
  774. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  775. (forward-line)
  776. (org-meta-return)
  777. (beginning-of-line)
  778. (looking-at "- $"))))
  779. (ert-deftest test-org/insert-heading ()
  780. "Test `org-insert-heading' specifications."
  781. ;; FIXME: Test coverage is incomplete yet.
  782. ;;
  783. ;; In an empty buffer, insert a new headline.
  784. (should
  785. (equal "* "
  786. (org-test-with-temp-text ""
  787. (org-insert-heading)
  788. (buffer-string))))
  789. ;; At the beginning of a line, turn it into a headline
  790. (should
  791. (equal "* P"
  792. (org-test-with-temp-text "<point>P"
  793. (org-insert-heading)
  794. (buffer-string))))
  795. ;; In the middle of a line, split the line if allowed, otherwise,
  796. ;; insert the headline at its end.
  797. (should
  798. (equal "Para\n* graph"
  799. (org-test-with-temp-text "Para<point>graph"
  800. (let ((org-M-RET-may-split-line '((default . t))))
  801. (org-insert-heading))
  802. (buffer-string))))
  803. (should
  804. (equal "Paragraph\n* "
  805. (org-test-with-temp-text "Para<point>graph"
  806. (let ((org-M-RET-may-split-line '((default . nil))))
  807. (org-insert-heading))
  808. (buffer-string))))
  809. ;; When on a list, insert an item instead, unless called with an
  810. ;; universal argument or if list is invisible. In this case, create
  811. ;; a new headline after contents.
  812. (should
  813. (equal "* H\n- item\n- "
  814. (org-test-with-temp-text "* H\n- item<point>"
  815. (let ((org-insert-heading-respect-content nil))
  816. (org-insert-heading))
  817. (buffer-string))))
  818. (should
  819. (equal "* H\n- item\n- item 2\n* "
  820. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  821. (let ((org-insert-heading-respect-content nil))
  822. (org-insert-heading '(4)))
  823. (buffer-string))))
  824. (should
  825. (equal "* H\n- item\n* "
  826. (org-test-with-temp-text "* H\n- item"
  827. (org-cycle)
  828. (goto-char (point-max))
  829. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  830. (buffer-string))))
  831. ;; When called with two universal arguments, insert a new headline
  832. ;; at the end of the grandparent subtree.
  833. (should
  834. (equal "* H1\n** H3\n- item\n** H2\n** "
  835. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  836. (let ((org-insert-heading-respect-content nil))
  837. (org-insert-heading '(16)))
  838. (buffer-string))))
  839. ;; Corner case: correctly insert a headline after an empty one.
  840. (should
  841. (equal "* \n* "
  842. (org-test-with-temp-text "* <point>"
  843. (org-insert-heading)
  844. (buffer-string)))))
  845. (ert-deftest test-org/insert-todo-heading-respect-content ()
  846. "Test `org-insert-todo-heading-respect-content' specifications."
  847. ;; Create a TODO heading.
  848. (should
  849. (org-test-with-temp-text "* H1\n Body"
  850. (org-insert-todo-heading-respect-content)
  851. (nth 2 (org-heading-components))))
  852. ;; Add headline at the end of the first subtree
  853. (should
  854. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  855. (search-forward "H1Body")
  856. (org-insert-todo-heading-respect-content)
  857. (and (eobp) (org-at-heading-p))))
  858. ;; In a list, do not create a new item.
  859. (should
  860. (org-test-with-temp-text "* H\n- an item\n- another one"
  861. (search-forward "an ")
  862. (org-insert-todo-heading-respect-content)
  863. (and (eobp) (org-at-heading-p)))))
  864. ;;; Fixed-Width Areas
  865. (ert-deftest test-org/toggle-fixed-width ()
  866. "Test `org-toggle-fixed-width' specifications."
  867. ;; No region: Toggle on fixed-width marker in paragraphs.
  868. (should
  869. (equal ": A"
  870. (org-test-with-temp-text "A"
  871. (org-toggle-fixed-width)
  872. (buffer-string))))
  873. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  874. (should
  875. (equal "A"
  876. (org-test-with-temp-text ": A"
  877. (org-toggle-fixed-width)
  878. (buffer-string))))
  879. ;; No region: Toggle on marker in blank lines after elements or just
  880. ;; after a headline.
  881. (should
  882. (equal "* H\n: "
  883. (org-test-with-temp-text "* H\n"
  884. (forward-line)
  885. (org-toggle-fixed-width)
  886. (buffer-string))))
  887. (should
  888. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  889. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  890. (goto-char (point-max))
  891. (org-toggle-fixed-width)
  892. (buffer-string))))
  893. ;; No region: Toggle on marker in front of one line elements (e.g.,
  894. ;; headlines, clocks)
  895. (should
  896. (equal ": * Headline"
  897. (org-test-with-temp-text "* Headline"
  898. (org-toggle-fixed-width)
  899. (buffer-string))))
  900. (should
  901. (equal ": #+KEYWORD: value"
  902. (org-test-with-temp-text "#+KEYWORD: value"
  903. (org-toggle-fixed-width)
  904. (buffer-string))))
  905. ;; No region: error in other situations.
  906. (should-error
  907. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  908. (forward-line)
  909. (org-toggle-fixed-width)
  910. (buffer-string)))
  911. ;; No region: Indentation is preserved.
  912. (should
  913. (equal "- A\n : B"
  914. (org-test-with-temp-text "- A\n B"
  915. (forward-line)
  916. (org-toggle-fixed-width)
  917. (buffer-string))))
  918. ;; Region: If it contains only fixed-width elements and blank lines,
  919. ;; toggle off fixed-width markup.
  920. (should
  921. (equal "A\n\nB"
  922. (org-test-with-temp-text ": A\n\n: B"
  923. (transient-mark-mode 1)
  924. (push-mark (point) t t)
  925. (goto-char (point-max))
  926. (org-toggle-fixed-width)
  927. (buffer-string))))
  928. ;; Region: If it contains anything else, toggle on fixed-width but
  929. ;; not on fixed-width areas.
  930. (should
  931. (equal ": A\n: \n: B\n: \n: C"
  932. (org-test-with-temp-text "A\n\n: B\n\nC"
  933. (transient-mark-mode 1)
  934. (push-mark (point) t t)
  935. (goto-char (point-max))
  936. (org-toggle-fixed-width)
  937. (buffer-string))))
  938. ;; Region: Ignore blank lines at its end, unless it contains only
  939. ;; such lines.
  940. (should
  941. (equal ": A\n\n"
  942. (org-test-with-temp-text "A\n\n"
  943. (transient-mark-mode 1)
  944. (push-mark (point) t t)
  945. (goto-char (point-max))
  946. (org-toggle-fixed-width)
  947. (buffer-string))))
  948. (should
  949. (equal ": \n: \n"
  950. (org-test-with-temp-text "\n\n"
  951. (transient-mark-mode 1)
  952. (push-mark (point) t t)
  953. (goto-char (point-max))
  954. (org-toggle-fixed-width)
  955. (buffer-string)))))
  956. ;;; Headline
  957. (ert-deftest test-org/in-commented-heading-p ()
  958. "Test `org-in-commented-heading-p' specifications."
  959. ;; Commented headline.
  960. (should
  961. (org-test-with-temp-text "* COMMENT Headline\nBody"
  962. (goto-char (point-max))
  963. (org-in-commented-heading-p)))
  964. ;; Commented ancestor.
  965. (should
  966. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  967. (goto-char (point-max))
  968. (org-in-commented-heading-p)))
  969. ;; Comment keyword is case-sensitive.
  970. (should-not
  971. (org-test-with-temp-text "* Comment Headline\nBody"
  972. (goto-char (point-max))
  973. (org-in-commented-heading-p)))
  974. ;; Keyword is standalone.
  975. (should-not
  976. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  977. (goto-char (point-max))
  978. (org-in-commented-heading-p)))
  979. ;; Optional argument.
  980. (should-not
  981. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  982. (goto-char (point-max))
  983. (org-in-commented-heading-p t))))
  984. ;;; Keywords
  985. (ert-deftest test-org/set-regexps-and-options ()
  986. "Test `org-set-regexps-and-options' specifications."
  987. ;; TAGS keyword.
  988. (should
  989. (equal '(("A" . ?a) ("B") ("C"))
  990. (org-test-with-temp-text "#+TAGS: A(a) B C"
  991. (org-mode-restart)
  992. org-tag-alist)))
  993. (should
  994. (equal '(("A") (:newline) ("B"))
  995. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  996. (org-mode-restart)
  997. org-tag-alist)))
  998. (should
  999. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1000. (org-test-with-temp-text "#+TAGS: { A B } C"
  1001. (org-mode-restart)
  1002. org-tag-alist)))
  1003. (should
  1004. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1005. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1006. (org-mode-restart)
  1007. org-tag-alist)))
  1008. (should
  1009. (equal '(("A" "B" "C"))
  1010. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1011. (org-mode-restart)
  1012. org-tag-groups-alist)))
  1013. ;; FILETAGS keyword.
  1014. (should
  1015. (equal '("A" "B" "C")
  1016. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1017. (org-mode-restart)
  1018. org-file-tags)))
  1019. ;; PROPERTY keyword. Property names are case-insensitive.
  1020. (should
  1021. (equal "foo=1"
  1022. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1023. (org-mode-restart)
  1024. (cdr (assoc "var" org-file-properties)))))
  1025. (should
  1026. (equal
  1027. "foo=1 bar=2"
  1028. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1029. (org-mode-restart)
  1030. (cdr (assoc "var" org-file-properties)))))
  1031. (should
  1032. (equal
  1033. "foo=1 bar=2"
  1034. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1035. (org-mode-restart)
  1036. (cdr (assoc "var" org-file-properties)))))
  1037. ;; ARCHIVE keyword.
  1038. (should
  1039. (equal "%s_done::"
  1040. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1041. (org-mode-restart)
  1042. org-archive-location)))
  1043. ;; CATEGORY keyword.
  1044. (should
  1045. (eq 'test
  1046. (org-test-with-temp-text "#+CATEGORY: test"
  1047. (org-mode-restart)
  1048. org-category)))
  1049. (should
  1050. (equal "test"
  1051. (org-test-with-temp-text "#+CATEGORY: test"
  1052. (org-mode-restart)
  1053. (cdr (assoc "CATEGORY" org-file-properties)))))
  1054. ;; COLUMNS keyword.
  1055. (should
  1056. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1057. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1058. (org-mode-restart)
  1059. org-columns-default-format)))
  1060. ;; CONSTANTS keyword. Constants names are case sensitive.
  1061. (should
  1062. (equal '("299792458." "3.14")
  1063. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1064. (org-mode-restart)
  1065. (mapcar
  1066. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1067. '("c" "pi")))))
  1068. (should
  1069. (equal "3.14"
  1070. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1071. (org-mode-restart)
  1072. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1073. (should
  1074. (equal "22/7"
  1075. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1076. (org-mode-restart)
  1077. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1078. ;; LINK keyword.
  1079. (should
  1080. (equal
  1081. '("url1" "url2")
  1082. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1083. (org-mode-restart)
  1084. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1085. '("a" "b")))))
  1086. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1087. (should
  1088. (equal
  1089. '(?X ?Z ?Y)
  1090. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1091. (org-mode-restart)
  1092. (list org-highest-priority org-lowest-priority org-default-priority))))
  1093. (should
  1094. (equal
  1095. '(?A ?C ?B)
  1096. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1097. (org-mode-restart)
  1098. (list org-highest-priority org-lowest-priority org-default-priority))))
  1099. ;; STARTUP keyword.
  1100. (should
  1101. (equal '(t t)
  1102. (org-test-with-temp-text "#+STARTUP: fold odd"
  1103. (org-mode-restart)
  1104. (list org-startup-folded org-odd-levels-only))))
  1105. ;; TODO keywords.
  1106. (should
  1107. (equal '(("A" "B") ("C"))
  1108. (org-test-with-temp-text "#+TODO: A B | C"
  1109. (org-mode-restart)
  1110. (list org-not-done-keywords org-done-keywords))))
  1111. (should
  1112. (equal '(("A" "C") ("B" "D"))
  1113. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1114. (org-mode-restart)
  1115. (list org-not-done-keywords org-done-keywords))))
  1116. (should
  1117. (equal '(("A" "B") ("C"))
  1118. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1119. (org-mode-restart)
  1120. (list org-not-done-keywords org-done-keywords))))
  1121. (should
  1122. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1123. (org-test-with-temp-text "#+TODO: A(a)"
  1124. (org-mode-restart)
  1125. org-todo-key-alist)))
  1126. (should
  1127. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1128. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1129. (org-mode-restart)
  1130. org-todo-log-states)))
  1131. ;; Enter SETUPFILE keyword.
  1132. (should
  1133. (equal "1"
  1134. (org-test-with-temp-text
  1135. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1136. (org-mode-restart)
  1137. (cdr (assoc "a" org-file-properties))))))
  1138. ;;; Links
  1139. ;;;; Coderefs
  1140. (ert-deftest test-org/coderef ()
  1141. "Test coderef links specifications."
  1142. (should
  1143. (org-test-with-temp-text "
  1144. #+BEGIN_SRC emacs-lisp
  1145. \(+ 1 1) (ref:sc)
  1146. #+END_SRC
  1147. \[[(sc)]]"
  1148. (goto-char (point-max))
  1149. (org-open-at-point)
  1150. (looking-at "(ref:sc)"))))
  1151. ;;;; Custom ID
  1152. (ert-deftest test-org/custom-id ()
  1153. "Test custom ID links specifications."
  1154. (should
  1155. (org-test-with-temp-text
  1156. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  1157. (goto-char (point-max))
  1158. (org-open-at-point)
  1159. (org-looking-at-p "\\* H1"))))
  1160. ;;;; Fuzzy Links
  1161. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1162. ;; a named element (#+name: text) and to headlines (* Text).
  1163. (ert-deftest test-org/fuzzy-links ()
  1164. "Test fuzzy links specifications."
  1165. ;; Fuzzy link goes in priority to a matching target.
  1166. (should
  1167. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  1168. (goto-line 5)
  1169. (org-open-at-point)
  1170. (looking-at "<<Test>>")))
  1171. ;; Then fuzzy link points to an element with a given name.
  1172. (should
  1173. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  1174. (goto-line 5)
  1175. (org-open-at-point)
  1176. (looking-at "#\\+NAME: Test")))
  1177. ;; A target still lead to a matching headline otherwise.
  1178. (should
  1179. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  1180. (goto-line 4)
  1181. (org-open-at-point)
  1182. (looking-at "\\* Head2")))
  1183. ;; With a leading star in link, enforce heading match.
  1184. (should
  1185. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  1186. (goto-line 3)
  1187. (org-open-at-point)
  1188. (looking-at "\\* Test")))
  1189. ;; Correctly un-hexify fuzzy links.
  1190. (should
  1191. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  1192. (goto-char (point-max))
  1193. (org-open-at-point)
  1194. (bobp))))
  1195. ;;;; Link Escaping
  1196. (ert-deftest test-org/org-link-escape-ascii-character ()
  1197. "Escape an ascii character."
  1198. (should
  1199. (string=
  1200. "%5B"
  1201. (org-link-escape "["))))
  1202. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  1203. "Escape an ascii control character."
  1204. (should
  1205. (string=
  1206. "%09"
  1207. (org-link-escape "\t"))))
  1208. (ert-deftest test-org/org-link-escape-multibyte-character ()
  1209. "Escape an unicode multibyte character."
  1210. (should
  1211. (string=
  1212. "%E2%82%AC"
  1213. (org-link-escape "€"))))
  1214. (ert-deftest test-org/org-link-escape-custom-table ()
  1215. "Escape string with custom character table."
  1216. (should
  1217. (string=
  1218. "Foo%3A%42ar%0A"
  1219. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  1220. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  1221. "Escape string with custom table merged with default table."
  1222. (should
  1223. (string=
  1224. "%5BF%6F%6F%3A%42ar%0A%5D"
  1225. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  1226. (ert-deftest test-org/org-link-unescape-ascii-character ()
  1227. "Unescape an ascii character."
  1228. (should
  1229. (string=
  1230. "["
  1231. (org-link-unescape "%5B"))))
  1232. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  1233. "Unescpae an ascii control character."
  1234. (should
  1235. (string=
  1236. "\n"
  1237. (org-link-unescape "%0A"))))
  1238. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  1239. "Unescape unicode multibyte character."
  1240. (should
  1241. (string=
  1242. "€"
  1243. (org-link-unescape "%E2%82%AC"))))
  1244. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  1245. "Unescape old style percent escaped character."
  1246. (should
  1247. (string=
  1248. "àâçèéêîôùû"
  1249. (decode-coding-string
  1250. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  1251. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  1252. "Escape and unescape a URL that includes an escaped char.
  1253. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  1254. (should
  1255. (string=
  1256. "http://some.host.com/form?&id=blah%2Bblah25"
  1257. (org-link-unescape
  1258. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  1259. (ert-deftest test-org/org-link-escape-chars-browser ()
  1260. "Test of the constant `org-link-escape-chars-browser'.
  1261. See there why this test is a candidate to be removed once Org
  1262. drops support for Emacs 24.1 and 24.2."
  1263. (should
  1264. (string=
  1265. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1266. "%22Release%208.2%22&idxname=emacs-orgmode")
  1267. (org-link-escape-browser ; Do not replace with `url-encode-url',
  1268. ; see docstring above.
  1269. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1270. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  1271. ;;;; Open at point
  1272. (ert-deftest test-org/open-at-point-in-property ()
  1273. "Does `org-open-at-point' open link in property drawer?"
  1274. (should
  1275. (org-test-with-temp-text
  1276. "* Headline
  1277. :PROPERTIES:
  1278. :URL: <point>[[info:emacs#Top]]
  1279. :END:"
  1280. (org-open-at-point) t)))
  1281. (ert-deftest test-org/open-at-point-in-comment ()
  1282. "Does `org-open-at-point' open link in a commented line?"
  1283. (should
  1284. (org-test-with-temp-text
  1285. "# <point>[[info:emacs#Top]]"
  1286. (org-open-at-point) t)))
  1287. (ert-deftest test-org/open-at-point/info ()
  1288. "Test `org-open-at-point' on info links."
  1289. (should
  1290. (org-test-with-temp-text
  1291. "<point>[[info:emacs#Top]]"
  1292. (org-open-at-point)
  1293. (and (switch-to-buffer "*info*")
  1294. (prog1
  1295. (looking-at "\nThe Emacs Editor")
  1296. (kill-buffer))))))
  1297. ;;; Node Properties
  1298. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1299. "Ensure properties accumulate in subtree drawers."
  1300. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1301. (org-babel-next-src-block)
  1302. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1303. (ert-deftest test-org/custom-properties ()
  1304. "Test custom properties specifications."
  1305. ;; Standard test.
  1306. (should
  1307. (let ((org-custom-properties '("FOO")))
  1308. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1309. (org-toggle-custom-properties-visibility)
  1310. (org-invisible-p2))))
  1311. ;; Properties are case-insensitive.
  1312. (should
  1313. (let ((org-custom-properties '("FOO")))
  1314. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  1315. (org-toggle-custom-properties-visibility)
  1316. (org-invisible-p2))))
  1317. (should
  1318. (let ((org-custom-properties '("foo")))
  1319. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1320. (org-toggle-custom-properties-visibility)
  1321. (org-invisible-p2))))
  1322. ;; Multiple custom properties in the same drawer.
  1323. (should
  1324. (let ((org-custom-properties '("FOO" "BAR")))
  1325. (org-test-with-temp-text
  1326. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  1327. (org-toggle-custom-properties-visibility)
  1328. (and (org-invisible-p2)
  1329. (not (progn (forward-line) (org-invisible-p2)))
  1330. (progn (forward-line) (org-invisible-p2))))))
  1331. ;; Hide custom properties with an empty value.
  1332. (should
  1333. (let ((org-custom-properties '("FOO")))
  1334. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  1335. (org-toggle-custom-properties-visibility)
  1336. (org-invisible-p2))))
  1337. ;; Do not hide fake properties.
  1338. (should-not
  1339. (let ((org-custom-properties '("FOO")))
  1340. (org-test-with-temp-text ":FOO: val\n"
  1341. (org-toggle-custom-properties-visibility)
  1342. (org-invisible-p2))))
  1343. (should-not
  1344. (let ((org-custom-properties '("A")))
  1345. (org-test-with-temp-text
  1346. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  1347. (org-toggle-custom-properties-visibility)
  1348. (org-invisible-p2)))))
  1349. ;;; Mark Region
  1350. (ert-deftest test-org/mark-subtree ()
  1351. "Test `org-mark-subtree' specifications."
  1352. ;; Error when point is before first headline.
  1353. (should-error
  1354. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1355. (progn (transient-mark-mode 1)
  1356. (org-mark-subtree))))
  1357. ;; Without argument, mark current subtree.
  1358. (should
  1359. (equal
  1360. '(12 32)
  1361. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1362. (progn (transient-mark-mode 1)
  1363. (forward-line 2)
  1364. (org-mark-subtree)
  1365. (list (region-beginning) (region-end))))))
  1366. ;; With an argument, move ARG up.
  1367. (should
  1368. (equal
  1369. '(1 32)
  1370. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1371. (progn (transient-mark-mode 1)
  1372. (forward-line 2)
  1373. (org-mark-subtree 1)
  1374. (list (region-beginning) (region-end))))))
  1375. ;; Do not get fooled by inlinetasks.
  1376. (when (featurep 'org-inlinetask)
  1377. (should
  1378. (= 1
  1379. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1380. (progn (transient-mark-mode 1)
  1381. (forward-line 1)
  1382. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1383. (region-beginning)))))))
  1384. ;;; Navigation
  1385. (ert-deftest test-org/beginning-of-line ()
  1386. "Test `org-beginning-of-line' specifications."
  1387. ;; Standard test.
  1388. (should
  1389. (org-test-with-temp-text "Some text\nSome other text"
  1390. (progn (org-beginning-of-line) (bolp))))
  1391. ;; Standard test with `visual-line-mode'.
  1392. (should-not
  1393. (org-test-with-temp-text "A long line of text\nSome other text"
  1394. (progn (visual-line-mode)
  1395. (forward-char 2)
  1396. (dotimes (i 1000) (insert "very "))
  1397. (org-beginning-of-line)
  1398. (bolp))))
  1399. ;; At an headline with special movement.
  1400. (should
  1401. (org-test-with-temp-text "* TODO Headline"
  1402. (let ((org-special-ctrl-a/e t))
  1403. (org-end-of-line)
  1404. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1405. (progn (org-beginning-of-line) (bolp))
  1406. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1407. (ert-deftest test-org/end-of-line ()
  1408. "Test `org-end-of-line' specifications."
  1409. ;; Standard test.
  1410. (should
  1411. (org-test-with-temp-text "Some text\nSome other text"
  1412. (progn (org-end-of-line) (eolp))))
  1413. ;; Standard test with `visual-line-mode'.
  1414. (should-not
  1415. (org-test-with-temp-text "A long line of text\nSome other text"
  1416. (progn (visual-line-mode)
  1417. (forward-char 2)
  1418. (dotimes (i 1000) (insert "very "))
  1419. (goto-char (point-min))
  1420. (org-end-of-line)
  1421. (eolp))))
  1422. ;; At an headline with special movement.
  1423. (should
  1424. (org-test-with-temp-text "* Headline1 :tag:\n"
  1425. (let ((org-special-ctrl-a/e t))
  1426. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1427. (progn (org-end-of-line) (eolp))
  1428. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1429. ;; At an headline without special movement.
  1430. (should
  1431. (org-test-with-temp-text "* Headline2 :tag:\n"
  1432. (let ((org-special-ctrl-a/e nil))
  1433. (and (progn (org-end-of-line) (eolp))
  1434. (progn (org-end-of-line) (eolp))))))
  1435. ;; At an headline, with reversed movement.
  1436. (should
  1437. (org-test-with-temp-text "* Headline3 :tag:\n"
  1438. (let ((org-special-ctrl-a/e 'reversed)
  1439. (this-command last-command))
  1440. (and (progn (org-end-of-line) (eolp))
  1441. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1442. ;; At a block without hidden contents.
  1443. (should
  1444. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1445. (progn (org-end-of-line) (eolp))))
  1446. ;; At a block with hidden contents.
  1447. (should-not
  1448. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1449. (let ((org-special-ctrl-a/e t))
  1450. (org-hide-block-toggle)
  1451. (org-end-of-line)
  1452. (eobp)))))
  1453. (ert-deftest test-org/forward-paragraph ()
  1454. "Test `org-forward-paragraph' specifications."
  1455. ;; At end of buffer, return an error.
  1456. (should-error
  1457. (org-test-with-temp-text "Paragraph"
  1458. (goto-char (point-max))
  1459. (org-forward-paragraph)))
  1460. ;; Standard test.
  1461. (should
  1462. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1463. (org-forward-paragraph)
  1464. (looking-at "P2")))
  1465. ;; Ignore depth.
  1466. (should
  1467. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1468. (org-forward-paragraph)
  1469. (looking-at "P1")))
  1470. ;; Do not enter elements with invisible contents.
  1471. (should
  1472. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1473. (org-hide-block-toggle)
  1474. (org-forward-paragraph)
  1475. (looking-at "P3")))
  1476. ;; On an affiliated keyword, jump to the beginning of the element.
  1477. (should
  1478. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1479. (org-forward-paragraph)
  1480. (looking-at "Para")))
  1481. ;; On an item or a footnote definition, move to the second element
  1482. ;; inside, if any.
  1483. (should
  1484. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1485. (org-forward-paragraph)
  1486. (looking-at " Paragraph")))
  1487. (should
  1488. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1489. (org-forward-paragraph)
  1490. (looking-at "Paragraph")))
  1491. ;; On an item, or a footnote definition, when the first line is
  1492. ;; empty, move to the first item.
  1493. (should
  1494. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1495. (org-forward-paragraph)
  1496. (looking-at " Paragraph")))
  1497. (should
  1498. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1499. (org-forward-paragraph)
  1500. (looking-at "Paragraph")))
  1501. ;; On a table (resp. a property drawer) do not move through table
  1502. ;; rows (resp. node properties).
  1503. (should
  1504. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1505. (org-forward-paragraph)
  1506. (looking-at "Paragraph")))
  1507. (should
  1508. (org-test-with-temp-text
  1509. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1510. (org-forward-paragraph)
  1511. (looking-at "Paragraph")))
  1512. ;; On a verse or source block, stop after blank lines.
  1513. (should
  1514. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1515. (org-forward-paragraph)
  1516. (looking-at "L2")))
  1517. (should
  1518. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1519. (org-forward-paragraph)
  1520. (looking-at "L2"))))
  1521. (ert-deftest test-org/backward-paragraph ()
  1522. "Test `org-backward-paragraph' specifications."
  1523. ;; Error at beginning of buffer.
  1524. (should-error
  1525. (org-test-with-temp-text "Paragraph"
  1526. (org-backward-paragraph)))
  1527. ;; Regular test.
  1528. (should
  1529. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1530. (goto-char (point-max))
  1531. (org-backward-paragraph)
  1532. (looking-at "P3")))
  1533. (should
  1534. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1535. (goto-char (point-max))
  1536. (beginning-of-line)
  1537. (org-backward-paragraph)
  1538. (looking-at "P2")))
  1539. ;; Ignore depth.
  1540. (should
  1541. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1542. (goto-char (point-max))
  1543. (beginning-of-line)
  1544. (org-backward-paragraph)
  1545. (looking-at "P2")))
  1546. ;; Ignore invisible elements.
  1547. (should
  1548. (org-test-with-temp-text "* H1\n P1\n* H2"
  1549. (org-cycle)
  1550. (goto-char (point-max))
  1551. (beginning-of-line)
  1552. (org-backward-paragraph)
  1553. (bobp)))
  1554. ;; On an affiliated keyword, jump to the first one.
  1555. (should
  1556. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1557. (search-forward "c2")
  1558. (org-backward-paragraph)
  1559. (looking-at "#\\+name")))
  1560. ;; On the second element in an item or a footnote definition, jump
  1561. ;; to item or the definition.
  1562. (should
  1563. (org-test-with-temp-text "- line1\n\n line2"
  1564. (goto-char (point-max))
  1565. (beginning-of-line)
  1566. (org-backward-paragraph)
  1567. (looking-at "- line1")))
  1568. (should
  1569. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1570. (goto-char (point-max))
  1571. (beginning-of-line)
  1572. (org-backward-paragraph)
  1573. (looking-at "\\[fn:1\\] line1")))
  1574. ;; On a table (resp. a property drawer), ignore table rows
  1575. ;; (resp. node properties).
  1576. (should
  1577. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1578. (goto-char (point-max))
  1579. (beginning-of-line)
  1580. (org-backward-paragraph)
  1581. (bobp)))
  1582. (should
  1583. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  1584. (org-backward-paragraph)
  1585. (looking-at ":PROPERTIES:")))
  1586. ;; On a source or verse block, stop before blank lines.
  1587. (should
  1588. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1589. (search-forward "L3")
  1590. (beginning-of-line)
  1591. (org-backward-paragraph)
  1592. (looking-at "L2")))
  1593. (should
  1594. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1595. (search-forward "L3")
  1596. (beginning-of-line)
  1597. (org-backward-paragraph)
  1598. (looking-at "L2"))))
  1599. (ert-deftest test-org/forward-element ()
  1600. "Test `org-forward-element' specifications."
  1601. ;; 1. At EOB: should error.
  1602. (org-test-with-temp-text "Some text\n"
  1603. (goto-char (point-max))
  1604. (should-error (org-forward-element)))
  1605. ;; 2. Standard move: expected to ignore blank lines.
  1606. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1607. (org-forward-element)
  1608. (should (looking-at (regexp-quote "Second paragraph."))))
  1609. ;; 3. Headline tests.
  1610. (org-test-with-temp-text "
  1611. * Head 1
  1612. ** Head 1.1
  1613. *** Head 1.1.1
  1614. ** Head 1.2"
  1615. ;; 3.1. At an headline beginning: move to next headline at the
  1616. ;; same level.
  1617. (goto-line 3)
  1618. (org-forward-element)
  1619. (should (looking-at (regexp-quote "** Head 1.2")))
  1620. ;; 3.2. At an headline beginning: move to parent headline if no
  1621. ;; headline at the same level.
  1622. (goto-line 3)
  1623. (org-forward-element)
  1624. (should (looking-at (regexp-quote "** Head 1.2"))))
  1625. ;; 4. Greater element tests.
  1626. (org-test-with-temp-text
  1627. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1628. ;; 4.1. At a greater element: expected to skip contents.
  1629. (org-forward-element)
  1630. (should (looking-at (regexp-quote "Outside.")))
  1631. ;; 4.2. At the end of greater element contents: expected to skip
  1632. ;; to the end of the greater element.
  1633. (goto-line 2)
  1634. (org-forward-element)
  1635. (should (looking-at (regexp-quote "Outside."))))
  1636. ;; 5. List tests.
  1637. (org-test-with-temp-text "
  1638. - item1
  1639. - sub1
  1640. - sub2
  1641. - sub3
  1642. Inner paragraph.
  1643. - item2
  1644. Outside."
  1645. ;; 5.1. At list top point: expected to move to the element after
  1646. ;; the list.
  1647. (goto-line 2)
  1648. (org-forward-element)
  1649. (should (looking-at (regexp-quote "Outside.")))
  1650. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1651. ;; beginning of line, move to next item.
  1652. (goto-line 2)
  1653. (forward-char)
  1654. (org-forward-element)
  1655. (should (looking-at "- item2"))
  1656. (goto-line 4)
  1657. (forward-char)
  1658. (org-forward-element)
  1659. (should (looking-at " - sub2"))
  1660. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1661. (goto-line 4)
  1662. (org-forward-element)
  1663. (should (looking-at (regexp-quote " Inner paragraph.")))
  1664. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1665. (goto-line 8)
  1666. (org-forward-element)
  1667. (should (looking-at (regexp-quote " Inner paragraph.")))
  1668. ;; 5.5. At an item: expected to move to next item, if any.
  1669. (goto-line 6)
  1670. (org-forward-element)
  1671. (should (looking-at " - sub3"))))
  1672. (ert-deftest test-org/backward-element ()
  1673. "Test `org-backward-element' specifications."
  1674. ;; 1. Should error at BOB.
  1675. (org-test-with-temp-text " \nParagraph."
  1676. (should-error (org-backward-element)))
  1677. ;; 2. Should move at BOB when called on the first element in buffer.
  1678. (should
  1679. (org-test-with-temp-text "\n#+TITLE: test"
  1680. (progn (forward-line)
  1681. (org-backward-element)
  1682. (bobp))))
  1683. ;; 3. Not at the beginning of an element: move at its beginning.
  1684. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1685. (goto-line 3)
  1686. (end-of-line)
  1687. (org-backward-element)
  1688. (should (looking-at (regexp-quote "Paragraph2."))))
  1689. ;; 4. Headline tests.
  1690. (org-test-with-temp-text "
  1691. * Head 1
  1692. ** Head 1.1
  1693. *** Head 1.1.1
  1694. ** Head 1.2"
  1695. ;; 4.1. At an headline beginning: move to previous headline at the
  1696. ;; same level.
  1697. (goto-line 5)
  1698. (org-backward-element)
  1699. (should (looking-at (regexp-quote "** Head 1.1")))
  1700. ;; 4.2. At an headline beginning: move to parent headline if no
  1701. ;; headline at the same level.
  1702. (goto-line 3)
  1703. (org-backward-element)
  1704. (should (looking-at (regexp-quote "* Head 1")))
  1705. ;; 4.3. At the first top-level headline: should error.
  1706. (goto-line 2)
  1707. (should-error (org-backward-element)))
  1708. ;; 5. At beginning of first element inside a greater element:
  1709. ;; expected to move to greater element's beginning.
  1710. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1711. (goto-line 3)
  1712. (org-backward-element)
  1713. (should (looking-at "#\\+BEGIN_CENTER")))
  1714. ;; 6. At the beginning of the first element in a section: should
  1715. ;; move back to headline, if any.
  1716. (should
  1717. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1718. (progn (goto-char (point-max))
  1719. (beginning-of-line)
  1720. (org-backward-element)
  1721. (org-at-heading-p))))
  1722. ;; 7. List tests.
  1723. (org-test-with-temp-text "
  1724. - item1
  1725. - sub1
  1726. - sub2
  1727. - sub3
  1728. Inner paragraph.
  1729. - item2
  1730. Outside."
  1731. ;; 7.1. At beginning of sub-list: expected to move to the
  1732. ;; paragraph before it.
  1733. (goto-line 4)
  1734. (org-backward-element)
  1735. (should (looking-at "item1"))
  1736. ;; 7.2. At an item in a list: expected to move at previous item.
  1737. (goto-line 8)
  1738. (org-backward-element)
  1739. (should (looking-at " - sub2"))
  1740. (goto-line 12)
  1741. (org-backward-element)
  1742. (should (looking-at "- item1"))
  1743. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1744. ;; beginning.
  1745. (goto-line 10)
  1746. (org-backward-element)
  1747. (should (looking-at " - sub1"))
  1748. (goto-line 15)
  1749. (org-backward-element)
  1750. (should (looking-at "- item1"))
  1751. ;; 7.4. At blank-lines before list end: expected to move to top
  1752. ;; item.
  1753. (goto-line 14)
  1754. (org-backward-element)
  1755. (should (looking-at "- item1"))))
  1756. (ert-deftest test-org/up-element ()
  1757. "Test `org-up-element' specifications."
  1758. ;; 1. At BOB or with no surrounding element: should error.
  1759. (org-test-with-temp-text "Paragraph."
  1760. (should-error (org-up-element)))
  1761. (org-test-with-temp-text "* Head1\n* Head2"
  1762. (goto-line 2)
  1763. (should-error (org-up-element)))
  1764. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1765. (goto-line 3)
  1766. (should-error (org-up-element)))
  1767. ;; 2. At an headline: move to parent headline.
  1768. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1769. (goto-line 3)
  1770. (org-up-element)
  1771. (should (looking-at "\\* Head1")))
  1772. ;; 3. Inside a greater element: move to greater element beginning.
  1773. (org-test-with-temp-text
  1774. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1775. (goto-line 3)
  1776. (org-up-element)
  1777. (should (looking-at "#\\+BEGIN_CENTER")))
  1778. ;; 4. List tests.
  1779. (org-test-with-temp-text "* Top
  1780. - item1
  1781. - sub1
  1782. - sub2
  1783. Paragraph within sub2.
  1784. - item2"
  1785. ;; 4.1. Within an item: move to the item beginning.
  1786. (goto-line 8)
  1787. (org-up-element)
  1788. (should (looking-at " - sub2"))
  1789. ;; 4.2. At an item in a sub-list: move to parent item.
  1790. (goto-line 4)
  1791. (org-up-element)
  1792. (should (looking-at "- item1"))
  1793. ;; 4.3. At an item in top list: move to beginning of whole list.
  1794. (goto-line 10)
  1795. (org-up-element)
  1796. (should (looking-at "- item1"))
  1797. ;; 4.4. Special case. At very top point: should move to parent of
  1798. ;; list.
  1799. (goto-line 2)
  1800. (org-up-element)
  1801. (should (looking-at "\\* Top"))))
  1802. (ert-deftest test-org/down-element ()
  1803. "Test `org-down-element' specifications."
  1804. ;; Error when the element hasn't got a recursive type.
  1805. (org-test-with-temp-text "Paragraph."
  1806. (should-error (org-down-element)))
  1807. ;; Error when the element has no contents
  1808. (org-test-with-temp-text "* Headline"
  1809. (should-error (org-down-element)))
  1810. ;; When at a plain-list, move to first item.
  1811. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1812. (goto-line 2)
  1813. (org-down-element)
  1814. (should (looking-at " - Item 1.1")))
  1815. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1816. (org-down-element)
  1817. (should (looking-at " Item 1")))
  1818. ;; When at a table, move to first row
  1819. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1820. (org-down-element)
  1821. (should (looking-at " a | b |")))
  1822. ;; Otherwise, move inside the greater element.
  1823. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1824. (org-down-element)
  1825. (should (looking-at "Paragraph"))))
  1826. (ert-deftest test-org/drag-element-backward ()
  1827. "Test `org-drag-element-backward' specifications."
  1828. ;; Standard test.
  1829. (should
  1830. (equal
  1831. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  1832. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  1833. (org-drag-element-backward)
  1834. (buffer-string))))
  1835. (should
  1836. (equal
  1837. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  1838. (org-test-with-temp-text
  1839. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  1840. (org-drag-element-backward)
  1841. (buffer-string))))
  1842. ;; Preserve blank lines.
  1843. (should
  1844. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  1845. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  1846. (org-drag-element-backward)
  1847. (buffer-string))))
  1848. ;; Preserve column.
  1849. (should
  1850. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  1851. (org-drag-element-backward)
  1852. (org-looking-at-p "2")))
  1853. ;; Error when trying to move first element of buffer.
  1854. (should-error
  1855. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1856. (org-drag-element-backward)))
  1857. ;; Error when trying to swap nested elements.
  1858. (should-error
  1859. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1860. (forward-line)
  1861. (org-drag-element-backward)))
  1862. ;; Error when trying to swap an headline element and a non-headline
  1863. ;; element.
  1864. (should-error
  1865. (org-test-with-temp-text "Test.\n* Head 1"
  1866. (forward-line)
  1867. (org-drag-element-backward)))
  1868. ;; Preserve visibility of elements and their contents.
  1869. (should
  1870. (equal '((63 . 82) (26 . 48))
  1871. (org-test-with-temp-text "
  1872. #+BEGIN_CENTER
  1873. Text.
  1874. #+END_CENTER
  1875. - item 1
  1876. #+BEGIN_QUOTE
  1877. Text.
  1878. #+END_QUOTE"
  1879. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1880. (search-backward "- item 1")
  1881. (org-drag-element-backward)
  1882. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1883. (overlays-in (point-min) (point-max)))))))
  1884. (ert-deftest test-org/drag-element-forward ()
  1885. "Test `org-drag-element-forward' specifications."
  1886. ;; 1. Error when trying to move first element of buffer.
  1887. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1888. (goto-line 3)
  1889. (should-error (org-drag-element-forward)))
  1890. ;; 2. Error when trying to swap nested elements.
  1891. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1892. (forward-line)
  1893. (should-error (org-drag-element-forward)))
  1894. ;; 3. Error when trying to swap a non-headline element and an
  1895. ;; headline.
  1896. (org-test-with-temp-text "Test.\n* Head 1"
  1897. (should-error (org-drag-element-forward)))
  1898. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1899. ;; between elements.
  1900. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1901. (search-forward "graph")
  1902. (org-drag-element-forward)
  1903. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1904. (should (looking-at " 1")))
  1905. ;; 5. Preserve visibility of elements and their contents.
  1906. (org-test-with-temp-text "
  1907. #+BEGIN_CENTER
  1908. Text.
  1909. #+END_CENTER
  1910. - item 1
  1911. #+BEGIN_QUOTE
  1912. Text.
  1913. #+END_QUOTE"
  1914. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1915. (search-backward "#+BEGIN_CENTER")
  1916. (org-drag-element-forward)
  1917. (should
  1918. (equal
  1919. '((63 . 82) (26 . 48))
  1920. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1921. (overlays-in (point-min) (point-max)))))))
  1922. ;;; Outline structure
  1923. (ert-deftest test-org/demote ()
  1924. "Test `org-demote' specifications."
  1925. ;; Add correct number of stars according to `org-odd-levels-only'.
  1926. (should
  1927. (= 2
  1928. (org-test-with-temp-text "* H"
  1929. (let ((org-odd-levels-only nil)) (org-demote))
  1930. (org-current-level))))
  1931. (should
  1932. (= 3
  1933. (org-test-with-temp-text "* H"
  1934. (let ((org-odd-levels-only t)) (org-demote))
  1935. (org-current-level))))
  1936. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  1937. (should
  1938. (org-test-with-temp-text "* H :tag:"
  1939. (let ((org-tags-column 10)
  1940. (org-auto-align-tags t)
  1941. (org-odd-levels-only nil))
  1942. (org-demote))
  1943. (org-move-to-column 10)
  1944. (org-looking-at-p ":tag:$")))
  1945. (should-not
  1946. (org-test-with-temp-text "* H :tag:"
  1947. (let ((org-tags-column 10)
  1948. (org-auto-align-tags nil)
  1949. (org-odd-levels-only nil))
  1950. (org-demote))
  1951. (org-move-to-column 10)
  1952. (org-looking-at-p ":tag:$")))
  1953. ;; When `org-adapt-indentation' is non-nil, always indent planning
  1954. ;; info and property drawers accordingly.
  1955. (should
  1956. (= 3
  1957. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  1958. (let ((org-odd-levels-only nil)
  1959. (org-adapt-indentation t))
  1960. (org-demote))
  1961. (forward-line)
  1962. (org-get-indentation))))
  1963. (should
  1964. (= 3
  1965. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  1966. (let ((org-odd-levels-only nil)
  1967. (org-adapt-indentation t))
  1968. (org-demote))
  1969. (forward-line)
  1970. (org-get-indentation))))
  1971. (should-not
  1972. (= 3
  1973. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  1974. (let ((org-odd-levels-only nil)
  1975. (org-adapt-indentation nil))
  1976. (org-demote))
  1977. (forward-line)
  1978. (org-get-indentation))))
  1979. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  1980. ;; section accordingly. Ignore, however, footnote definitions and
  1981. ;; inlinetasks boundaries.
  1982. (should
  1983. (= 3
  1984. (org-test-with-temp-text "* H\n Paragraph"
  1985. (let ((org-odd-levels-only nil)
  1986. (org-adapt-indentation t))
  1987. (org-demote))
  1988. (forward-line)
  1989. (org-get-indentation))))
  1990. (should
  1991. (= 2
  1992. (org-test-with-temp-text "* H\n Paragraph"
  1993. (let ((org-odd-levels-only nil)
  1994. (org-adapt-indentation nil))
  1995. (org-demote))
  1996. (forward-line)
  1997. (org-get-indentation))))
  1998. (should
  1999. (zerop
  2000. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  2001. (let ((org-odd-levels-only nil)
  2002. (org-adapt-indentation t))
  2003. (org-demote))
  2004. (goto-char (point-max))
  2005. (org-get-indentation))))
  2006. (should
  2007. (= 3
  2008. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  2009. (let ((org-odd-levels-only nil)
  2010. (org-adapt-indentation t))
  2011. (org-demote))
  2012. (goto-char (point-max))
  2013. (org-get-indentation))))
  2014. (when (featurep 'org-inlinetask)
  2015. (should
  2016. (zerop
  2017. (let ((org-inlinetask-min-level 5)
  2018. (org-adapt-indentation t))
  2019. (org-test-with-temp-text "* H\n***** I\n***** END"
  2020. (org-demote)
  2021. (forward-line)
  2022. (org-get-indentation))))))
  2023. (when (featurep 'org-inlinetask)
  2024. (should
  2025. (= 3
  2026. (let ((org-inlinetask-min-level 5)
  2027. (org-adapt-indentation t))
  2028. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  2029. (org-demote)
  2030. (forward-line 2)
  2031. (org-get-indentation))))))
  2032. ;; Ignore contents of source blocks or example blocks when
  2033. ;; indentation should be preserved (through
  2034. ;; `org-src-preserve-indentation' or "-i" flag).
  2035. (should-not
  2036. (zerop
  2037. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2038. (let ((org-adapt-indentation t)
  2039. (org-src-preserve-indentation nil))
  2040. (org-demote))
  2041. (forward-line 2)
  2042. (org-get-indentation))))
  2043. (should
  2044. (zerop
  2045. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2046. (let ((org-adapt-indentation t)
  2047. (org-src-preserve-indentation t))
  2048. (org-demote))
  2049. (forward-line 2)
  2050. (org-get-indentation))))
  2051. (should
  2052. (zerop
  2053. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2054. (let ((org-adapt-indentation t)
  2055. (org-src-preserve-indentation t))
  2056. (org-demote))
  2057. (forward-line 2)
  2058. (org-get-indentation))))
  2059. (should
  2060. (zerop
  2061. (org-test-with-temp-text
  2062. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  2063. (let ((org-adapt-indentation t)
  2064. (org-src-preserve-indentation nil))
  2065. (org-demote))
  2066. (forward-line 2)
  2067. (org-get-indentation)))))
  2068. (ert-deftest test-org/promote ()
  2069. "Test `org-promote' specifications."
  2070. ;; Return an error if headline is to be promoted to level 0, unless
  2071. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  2072. ;; headline becomes a comment.
  2073. (should-error
  2074. (org-test-with-temp-text "* H"
  2075. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  2076. (should
  2077. (equal "# H"
  2078. (org-test-with-temp-text "* H"
  2079. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  2080. (buffer-string))))
  2081. ;; Remove correct number of stars according to
  2082. ;; `org-odd-levels-only'.
  2083. (should
  2084. (= 2
  2085. (org-test-with-temp-text "*** H"
  2086. (let ((org-odd-levels-only nil)) (org-promote))
  2087. (org-current-level))))
  2088. (should
  2089. (= 1
  2090. (org-test-with-temp-text "*** H"
  2091. (let ((org-odd-levels-only t)) (org-promote))
  2092. (org-current-level))))
  2093. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  2094. (should
  2095. (org-test-with-temp-text "** H :tag:"
  2096. (let ((org-tags-column 10)
  2097. (org-auto-align-tags t)
  2098. (org-odd-levels-only nil))
  2099. (org-promote))
  2100. (org-move-to-column 10)
  2101. (org-looking-at-p ":tag:$")))
  2102. (should-not
  2103. (org-test-with-temp-text "** H :tag:"
  2104. (let ((org-tags-column 10)
  2105. (org-auto-align-tags nil)
  2106. (org-odd-levels-only nil))
  2107. (org-promote))
  2108. (org-move-to-column 10)
  2109. (org-looking-at-p ":tag:$")))
  2110. ;; When `org-adapt-indentation' is non-nil, always indent planning
  2111. ;; info and property drawers.
  2112. (should
  2113. (= 2
  2114. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2115. (let ((org-odd-levels-only nil)
  2116. (org-adapt-indentation t))
  2117. (org-promote))
  2118. (forward-line)
  2119. (org-get-indentation))))
  2120. (should
  2121. (= 2
  2122. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2123. (let ((org-odd-levels-only nil)
  2124. (org-adapt-indentation t))
  2125. (org-promote))
  2126. (forward-line)
  2127. (org-get-indentation))))
  2128. (should-not
  2129. (= 2
  2130. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2131. (let ((org-odd-levels-only nil)
  2132. (org-adapt-indentation nil))
  2133. (org-promote))
  2134. (forward-line)
  2135. (org-get-indentation))))
  2136. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2137. ;; section accordingly. Ignore, however, footnote definitions and
  2138. ;; inlinetasks boundaries.
  2139. (should
  2140. (= 2
  2141. (org-test-with-temp-text "** H\n Paragraph"
  2142. (let ((org-odd-levels-only nil)
  2143. (org-adapt-indentation t))
  2144. (org-promote))
  2145. (forward-line)
  2146. (org-get-indentation))))
  2147. (should-not
  2148. (= 2
  2149. (org-test-with-temp-text "** H\n Paragraph"
  2150. (let ((org-odd-levels-only nil)
  2151. (org-adapt-indentation nil))
  2152. (org-promote))
  2153. (forward-line)
  2154. (org-get-indentation))))
  2155. (should
  2156. (= 2
  2157. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  2158. (let ((org-odd-levels-only nil)
  2159. (org-adapt-indentation t))
  2160. (org-promote))
  2161. (forward-line)
  2162. (org-get-indentation))))
  2163. (when (featurep 'org-inlinetask)
  2164. (should
  2165. (zerop
  2166. (let ((org-inlinetask-min-level 5)
  2167. (org-adapt-indentation t))
  2168. (org-test-with-temp-text "** H\n***** I\n***** END"
  2169. (org-promote)
  2170. (forward-line)
  2171. (org-get-indentation))))))
  2172. (when (featurep 'org-inlinetask)
  2173. (should
  2174. (= 2
  2175. (let ((org-inlinetask-min-level 5)
  2176. (org-adapt-indentation t))
  2177. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  2178. (org-promote)
  2179. (forward-line 2)
  2180. (org-get-indentation))))))
  2181. ;; Give up shifting if it would break document's structure
  2182. ;; otherwise.
  2183. (should
  2184. (= 3
  2185. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  2186. (let ((org-odd-levels-only nil)
  2187. (org-adapt-indentation t))
  2188. (org-promote))
  2189. (forward-line)
  2190. (org-get-indentation))))
  2191. (should
  2192. (= 3
  2193. (org-test-with-temp-text "** H\n Paragraph\n * list."
  2194. (let ((org-odd-levels-only nil)
  2195. (org-adapt-indentation t))
  2196. (org-promote))
  2197. (forward-line)
  2198. (org-get-indentation))))
  2199. ;; Ignore contents of source blocks or example blocks when
  2200. ;; indentation should be preserved (through
  2201. ;; `org-src-preserve-indentation' or "-i" flag).
  2202. (should-not
  2203. (zerop
  2204. (org-test-with-temp-text
  2205. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2206. (let ((org-adapt-indentation t)
  2207. (org-src-preserve-indentation nil)
  2208. (org-odd-levels-only nil))
  2209. (org-promote))
  2210. (forward-line)
  2211. (org-get-indentation))))
  2212. (should
  2213. (zerop
  2214. (org-test-with-temp-text
  2215. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  2216. (let ((org-adapt-indentation t)
  2217. (org-src-preserve-indentation t)
  2218. (org-odd-levels-only nil))
  2219. (org-promote))
  2220. (forward-line)
  2221. (org-get-indentation))))
  2222. (should
  2223. (zerop
  2224. (org-test-with-temp-text
  2225. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2226. (let ((org-adapt-indentation t)
  2227. (org-src-preserve-indentation t)
  2228. (org-odd-levels-only nil))
  2229. (org-promote))
  2230. (forward-line)
  2231. (org-get-indentation))))
  2232. (should
  2233. (zerop
  2234. (org-test-with-temp-text
  2235. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  2236. (let ((org-adapt-indentation t)
  2237. (org-src-preserve-indentation nil)
  2238. (org-odd-levels-only nil))
  2239. (org-promote))
  2240. (forward-line)
  2241. (org-get-indentation)))))
  2242. ;;; Planning
  2243. (ert-deftest test-org/timestamp-has-time-p ()
  2244. "Test `org-timestamp-has-time-p' specifications."
  2245. ;; With time.
  2246. (should
  2247. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  2248. (org-timestamp-has-time-p (org-element-context))))
  2249. ;; Without time.
  2250. (should-not
  2251. (org-test-with-temp-text "<2012-03-29 Thu>"
  2252. (org-timestamp-has-time-p (org-element-context)))))
  2253. (ert-deftest test-org/timestamp-format ()
  2254. "Test `org-timestamp-format' specifications."
  2255. ;; Regular test.
  2256. (should
  2257. (equal
  2258. "2012-03-29 16:40"
  2259. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  2260. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  2261. ;; Range end.
  2262. (should
  2263. (equal
  2264. "2012-03-29"
  2265. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  2266. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  2267. (ert-deftest test-org/timestamp-split-range ()
  2268. "Test `org-timestamp-split-range' specifications."
  2269. ;; Extract range start (active).
  2270. (should
  2271. (equal '(2012 3 29)
  2272. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2273. (let ((ts (org-timestamp-split-range (org-element-context))))
  2274. (mapcar (lambda (p) (org-element-property p ts))
  2275. '(:year-end :month-end :day-end))))))
  2276. ;; Extract range start (inactive)
  2277. (should
  2278. (equal '(2012 3 29)
  2279. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  2280. (let ((ts (org-timestamp-split-range (org-element-context))))
  2281. (mapcar (lambda (p) (org-element-property p ts))
  2282. '(:year-end :month-end :day-end))))))
  2283. ;; Extract range end (active).
  2284. (should
  2285. (equal '(2012 3 30)
  2286. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2287. (let ((ts (org-timestamp-split-range
  2288. (org-element-context) t)))
  2289. (mapcar (lambda (p) (org-element-property p ts))
  2290. '(:year-end :month-end :day-end))))))
  2291. ;; Extract range end (inactive)
  2292. (should
  2293. (equal '(2012 3 30)
  2294. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  2295. (let ((ts (org-timestamp-split-range
  2296. (org-element-context) t)))
  2297. (mapcar (lambda (p) (org-element-property p ts))
  2298. '(:year-end :month-end :day-end))))))
  2299. ;; Return the timestamp if not a range.
  2300. (should
  2301. (org-test-with-temp-text "[2012-03-29 Thu]"
  2302. (let* ((ts-orig (org-element-context))
  2303. (ts-copy (org-timestamp-split-range ts-orig)))
  2304. (eq ts-orig ts-copy))))
  2305. (should
  2306. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  2307. (let* ((ts-orig (org-element-context))
  2308. (ts-copy (org-timestamp-split-range ts-orig)))
  2309. (eq ts-orig ts-copy))))
  2310. ;; Check that parent is the same when a range was split.
  2311. (should
  2312. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  2313. (let* ((ts-orig (org-element-context))
  2314. (ts-copy (org-timestamp-split-range ts-orig)))
  2315. (eq (org-element-property :parent ts-orig)
  2316. (org-element-property :parent ts-copy))))))
  2317. (ert-deftest test-org/timestamp-translate ()
  2318. "Test `org-timestamp-translate' specifications."
  2319. ;; Translate whole date range.
  2320. (should
  2321. (equal "<29>--<30>"
  2322. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2323. (let ((org-display-custom-times t)
  2324. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2325. (org-timestamp-translate (org-element-context))))))
  2326. ;; Translate date range start.
  2327. (should
  2328. (equal "<29>"
  2329. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2330. (let ((org-display-custom-times t)
  2331. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2332. (org-timestamp-translate (org-element-context) 'start)))))
  2333. ;; Translate date range end.
  2334. (should
  2335. (equal "<30>"
  2336. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2337. (let ((org-display-custom-times t)
  2338. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2339. (org-timestamp-translate (org-element-context) 'end)))))
  2340. ;; Translate time range.
  2341. (should
  2342. (equal "<08>--<16>"
  2343. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  2344. (let ((org-display-custom-times t)
  2345. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  2346. (org-timestamp-translate (org-element-context))))))
  2347. ;; Translate non-range timestamp.
  2348. (should
  2349. (equal "<29>"
  2350. (org-test-with-temp-text "<2012-03-29 Thu>"
  2351. (let ((org-display-custom-times t)
  2352. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2353. (org-timestamp-translate (org-element-context))))))
  2354. ;; Do not change `diary' timestamps.
  2355. (should
  2356. (equal "<%%(org-float t 4 2)>"
  2357. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  2358. (let ((org-display-custom-times t)
  2359. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2360. (org-timestamp-translate (org-element-context)))))))
  2361. ;;; Property API
  2362. (ert-deftest test-org/buffer-property-keys ()
  2363. "Test `org-buffer-property-keys' specifications."
  2364. ;; Retrieve properties accross siblings.
  2365. (should
  2366. (equal '("A" "B")
  2367. (org-test-with-temp-text "
  2368. * H1
  2369. :PROPERTIES:
  2370. :A: 1
  2371. :END:
  2372. * H2
  2373. :PROPERTIES:
  2374. :B: 1
  2375. :END:"
  2376. (org-buffer-property-keys))))
  2377. ;; Retrieve properties accross children.
  2378. (should
  2379. (equal '("A" "B")
  2380. (org-test-with-temp-text "
  2381. * H1
  2382. :PROPERTIES:
  2383. :A: 1
  2384. :END:
  2385. ** H2
  2386. :PROPERTIES:
  2387. :B: 1
  2388. :END:"
  2389. (org-buffer-property-keys))))
  2390. ;; Retrieve muliple properties in the same drawer.
  2391. (should
  2392. (equal '("A" "B")
  2393. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2394. (org-buffer-property-keys))))
  2395. ;; Ignore extension symbol in property name.
  2396. (should
  2397. (equal '("A")
  2398. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  2399. (org-buffer-property-keys))))
  2400. ;; With non-nil COLUMNS, extract property names from columns.
  2401. (should
  2402. (equal '("A" "B")
  2403. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  2404. (org-buffer-property-keys nil nil t))))
  2405. (should
  2406. (equal '("A" "B" "COLUMNS")
  2407. (org-test-with-temp-text
  2408. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  2409. (org-buffer-property-keys nil nil t)))))
  2410. (ert-deftest test-org/property-values ()
  2411. "Test `org-property-values' specifications."
  2412. ;; Regular test.
  2413. (should
  2414. (equal '("2" "1")
  2415. (org-test-with-temp-text
  2416. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  2417. (org-property-values "A"))))
  2418. ;; Ignore empty values.
  2419. (should-not
  2420. (org-test-with-temp-text
  2421. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  2422. (org-property-values "A")))
  2423. ;; Take into consideration extended values.
  2424. (should
  2425. (equal '("1 2")
  2426. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  2427. (org-property-values "A")))))
  2428. (ert-deftest test-org/entry-delete ()
  2429. "Test `org-entry-delete' specifications."
  2430. ;; Regular test.
  2431. (should
  2432. (string-match
  2433. " *:PROPERTIES:\n *:B: +2\n *:END:"
  2434. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2435. (org-entry-delete (point) "A")
  2436. (buffer-string))))
  2437. ;; When last property is removed, remove the property drawer.
  2438. (should-not
  2439. (string-match
  2440. ":PROPERTIES:"
  2441. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  2442. (org-entry-delete (point) "A")
  2443. (buffer-string)))))
  2444. (ert-deftest test-org/entry-get ()
  2445. "Test `org-entry-get' specifications."
  2446. ;; Regular test.
  2447. (should
  2448. (equal "1"
  2449. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2450. (org-entry-get (point) "A"))))
  2451. ;; Ignore case.
  2452. (should
  2453. (equal "1"
  2454. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2455. (org-entry-get (point) "a"))))
  2456. ;; Handle extended values, both before and after base value.
  2457. (should
  2458. (equal "1 2 3"
  2459. (org-test-with-temp-text
  2460. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2461. (org-entry-get (point) "A"))))
  2462. ;; Empty values are returned as the empty string.
  2463. (should
  2464. (equal ""
  2465. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  2466. (org-entry-get (point) "A"))))
  2467. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  2468. ;; otherwise, return nil.
  2469. (should-not
  2470. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2471. (org-entry-get (point) "A")))
  2472. (should
  2473. (equal "nil"
  2474. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2475. (org-entry-get (point) "A" nil t))))
  2476. ;; Return nil when no property is found, independently on the
  2477. ;; LITERAL-NIL argument.
  2478. (should-not
  2479. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2480. (org-entry-get (point) "B")))
  2481. (should-not
  2482. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2483. (org-entry-get (point) "B" nil t)))
  2484. ;; Handle inheritance, when allowed.
  2485. (should
  2486. (equal
  2487. "1"
  2488. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2489. (org-entry-get (point) "A" t))))
  2490. (should
  2491. (equal
  2492. "1"
  2493. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2494. (let ((org-use-property-inheritance t))
  2495. (org-entry-get (point) "A" 'selective)))))
  2496. (should-not
  2497. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2498. (let ((org-use-property-inheritance nil))
  2499. (org-entry-get (point) "A" 'selective)))))
  2500. (ert-deftest test-org/entry-properties ()
  2501. "Test `org-entry-properties' specifications."
  2502. ;; Get "ITEM" property.
  2503. (should
  2504. (equal "* H"
  2505. (org-test-with-temp-text "* TODO H"
  2506. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  2507. ;; Get "TODO" property.
  2508. (should
  2509. (equal "TODO"
  2510. (org-test-with-temp-text "* TODO H"
  2511. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  2512. (should-not
  2513. (org-test-with-temp-text "* H"
  2514. (assoc "TODO" (org-entry-properties nil "TODO"))))
  2515. ;; Get "PRIORITY" property.
  2516. (should
  2517. (equal "A"
  2518. (org-test-with-temp-text "* [#A] H"
  2519. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  2520. (should-not
  2521. (org-test-with-temp-text "* H"
  2522. (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
  2523. ;; Get "FILE" property.
  2524. (should
  2525. (org-test-with-temp-text-in-file "* H\nParagraph"
  2526. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  2527. (buffer-file-name))))
  2528. (should-not
  2529. (org-test-with-temp-text "* H\nParagraph"
  2530. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  2531. ;; Get "TAGS" property.
  2532. (should
  2533. (equal ":tag1:tag2:"
  2534. (org-test-with-temp-text "* H :tag1:tag2:"
  2535. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  2536. (should-not
  2537. (org-test-with-temp-text "* H"
  2538. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  2539. ;; Get "ALLTAGS" property.
  2540. (should
  2541. (equal ":tag1:tag2:"
  2542. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  2543. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  2544. (should-not
  2545. (org-test-with-temp-text "* H"
  2546. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  2547. ;; Get "BLOCKED" property.
  2548. (should
  2549. (equal "t"
  2550. (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
  2551. (let ((org-enforce-todo-dependencies t)
  2552. (org-blocker-hook
  2553. '(org-block-todo-from-children-or-siblings-or-parent)))
  2554. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2555. (should
  2556. (equal ""
  2557. (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
  2558. (let ((org-enforce-todo-dependencies t))
  2559. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2560. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  2561. (should
  2562. (equal
  2563. "[2012-03-29 thu.]"
  2564. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  2565. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  2566. (should-not
  2567. (org-test-with-temp-text "* H"
  2568. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  2569. (should
  2570. (equal
  2571. "<2014-03-04 tue.>"
  2572. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2573. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  2574. (should-not
  2575. (org-test-with-temp-text "* H"
  2576. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  2577. (should
  2578. (equal
  2579. "<2014-03-04 tue.>"
  2580. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2581. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  2582. (should-not
  2583. (org-test-with-temp-text "* H"
  2584. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  2585. ;; Get "CATEGORY"
  2586. (should
  2587. (equal "cat"
  2588. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  2589. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2590. (should
  2591. (equal "cat"
  2592. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  2593. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2594. ;; Get standard properties.
  2595. (should
  2596. (equal "1"
  2597. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2598. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2599. ;; Handle extended properties.
  2600. (should
  2601. (equal "1 2 3"
  2602. (org-test-with-temp-text
  2603. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2604. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2605. (should
  2606. (equal "1 2 3"
  2607. (org-test-with-temp-text
  2608. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  2609. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2610. ;; Ignore forbidden (special) properties.
  2611. (should-not
  2612. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  2613. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  2614. (ert-deftest test-org/entry-put ()
  2615. "Test `org-entry-put' specifications."
  2616. ;; Error when not a string or nil.
  2617. (should-error
  2618. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  2619. (org-entry-put 1 "test" 2)))
  2620. ;; Set "TODO" property.
  2621. (should
  2622. (string-match (regexp-quote " TODO H")
  2623. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2624. (org-entry-put (point) "TODO" "TODO")
  2625. (buffer-string))))
  2626. (should
  2627. (string-match (regexp-quote "* H")
  2628. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2629. (org-entry-put (point) "TODO" nil)
  2630. (buffer-string))))
  2631. ;; Set "PRIORITY" property.
  2632. (should
  2633. (equal "* [#A] H"
  2634. (org-test-with-temp-text "* [#B] H"
  2635. (org-entry-put (point) "PRIORITY" "A")
  2636. (buffer-string))))
  2637. (should
  2638. (equal "* H"
  2639. (org-test-with-temp-text "* [#B] H"
  2640. (org-entry-put (point) "PRIORITY" nil)
  2641. (buffer-string))))
  2642. ;; Set "SCHEDULED" property.
  2643. (should
  2644. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  2645. (org-test-with-temp-text "* H"
  2646. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  2647. (buffer-string))))
  2648. (should
  2649. (string= "* H\n"
  2650. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2651. (org-entry-put (point) "SCHEDULED" nil)
  2652. (buffer-string))))
  2653. (should
  2654. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  2655. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2656. (org-entry-put (point) "SCHEDULED" "earlier")
  2657. (buffer-string))))
  2658. (should
  2659. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  2660. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2661. (org-entry-put (point) "SCHEDULED" "later")
  2662. (buffer-string))))
  2663. ;; Set "DEADLINE" property.
  2664. (should
  2665. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  2666. (org-test-with-temp-text "* H"
  2667. (org-entry-put (point) "DEADLINE" "2014-03-04")
  2668. (buffer-string))))
  2669. (should
  2670. (string= "* H\n"
  2671. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2672. (org-entry-put (point) "DEADLINE" nil)
  2673. (buffer-string))))
  2674. (should
  2675. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  2676. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2677. (org-entry-put (point) "DEADLINE" "earlier")
  2678. (buffer-string))))
  2679. (should
  2680. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  2681. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2682. (org-entry-put (point) "DEADLINE" "later")
  2683. (buffer-string))))
  2684. ;; Regular properties, with or without pre-existing drawer.
  2685. (should
  2686. (string-match "^ *:A: +2$"
  2687. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2688. (org-entry-put (point) "A" "2")
  2689. (buffer-string))))
  2690. (should
  2691. (string-match "^ *:A: +1$"
  2692. (org-test-with-temp-text "* H"
  2693. (org-entry-put (point) "A" "1")
  2694. (buffer-string))))
  2695. ;; Special case: two consecutive headlines.
  2696. (should
  2697. (string-match "\\* A\n *:PROPERTIES:"
  2698. (org-test-with-temp-text "* A\n** B"
  2699. (org-entry-put (point) "A" "1")
  2700. (buffer-string)))))
  2701. ;;; Radio Targets
  2702. (ert-deftest test-org/update-radio-target-regexp ()
  2703. "Test `org-update-radio-target-regexp' specifications."
  2704. ;; Properly update cache with no previous radio target regexp.
  2705. (should
  2706. (eq 'link
  2707. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2708. (save-excursion (goto-char (point-max)) (org-element-context))
  2709. (insert "<<<")
  2710. (search-forward "o")
  2711. (insert ">>>")
  2712. (org-update-radio-target-regexp)
  2713. (goto-char (point-max))
  2714. (org-element-type (org-element-context)))))
  2715. ;; Properly update cache with previous radio target regexp.
  2716. (should
  2717. (eq 'link
  2718. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2719. (save-excursion (goto-char (point-max)) (org-element-context))
  2720. (insert "<<<")
  2721. (search-forward "o")
  2722. (insert ">>>")
  2723. (org-update-radio-target-regexp)
  2724. (search-backward "r")
  2725. (delete-char 5)
  2726. (insert "new")
  2727. (org-update-radio-target-regexp)
  2728. (goto-char (point-max))
  2729. (delete-region (line-beginning-position) (point))
  2730. (insert "new")
  2731. (org-element-type (org-element-context))))))
  2732. ;;; Visibility
  2733. (ert-deftest test-org/flag-drawer ()
  2734. "Test `org-flag-drawer' specifications."
  2735. ;; Hide drawer.
  2736. (should
  2737. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2738. (org-flag-drawer t)
  2739. (get-char-property (line-end-position) 'invisible)))
  2740. ;; Show drawer.
  2741. (should-not
  2742. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2743. (org-flag-drawer t)
  2744. (org-flag-drawer nil)
  2745. (get-char-property (line-end-position) 'invisible)))
  2746. ;; Test optional argument.
  2747. (should
  2748. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2749. (let ((drawer (save-excursion (search-forward ":D2")
  2750. (org-element-at-point))))
  2751. (org-flag-drawer t drawer)
  2752. (get-char-property (progn (search-forward ":D2") (line-end-position))
  2753. 'invisible))))
  2754. (should-not
  2755. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2756. (let ((drawer (save-excursion (search-forward ":D2")
  2757. (org-element-at-point))))
  2758. (org-flag-drawer t drawer)
  2759. (get-char-property (line-end-position) 'invisible))))
  2760. ;; Do not hide fake drawers.
  2761. (should-not
  2762. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  2763. (forward-line 1)
  2764. (org-flag-drawer t)
  2765. (get-char-property (line-end-position) 'invisible)))
  2766. ;; Do not hide incomplete drawers.
  2767. (should-not
  2768. (org-test-with-temp-text ":D:\nparagraph"
  2769. (forward-line 1)
  2770. (org-flag-drawer t)
  2771. (get-char-property (line-end-position) 'invisible)))
  2772. ;; Do not hide drawers when called from final blank lines.
  2773. (should-not
  2774. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  2775. (goto-char (point-max))
  2776. (org-flag-drawer t)
  2777. (goto-char (point-min))
  2778. (get-char-property (line-end-position) 'invisible)))
  2779. ;; Don't leave point in an invisible part of the buffer when hiding
  2780. ;; a drawer away.
  2781. (should-not
  2782. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2783. (goto-char (point-max))
  2784. (org-flag-drawer t)
  2785. (get-char-property (point) 'invisible))))
  2786. (ert-deftest test-org/hide-block-toggle ()
  2787. "Test `org-hide-block-toggle' specifications."
  2788. ;; Error when not at a block.
  2789. (should-error
  2790. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  2791. (org-hide-block-toggle 'off)
  2792. (get-char-property (line-end-position) 'invisible)))
  2793. ;; Hide block.
  2794. (should
  2795. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2796. (org-hide-block-toggle)
  2797. (get-char-property (line-end-position) 'invisible)))
  2798. (should
  2799. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  2800. (org-hide-block-toggle)
  2801. (get-char-property (line-end-position) 'invisible)))
  2802. ;; Show block unconditionally when optional argument is `off'.
  2803. (should-not
  2804. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2805. (org-hide-block-toggle)
  2806. (org-hide-block-toggle 'off)
  2807. (get-char-property (line-end-position) 'invisible)))
  2808. (should-not
  2809. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2810. (org-hide-block-toggle 'off)
  2811. (get-char-property (line-end-position) 'invisible)))
  2812. ;; Hide block unconditionally when optional argument is non-nil.
  2813. (should
  2814. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2815. (org-hide-block-toggle t)
  2816. (get-char-property (line-end-position) 'invisible)))
  2817. (should
  2818. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2819. (org-hide-block-toggle)
  2820. (org-hide-block-toggle t)
  2821. (get-char-property (line-end-position) 'invisible)))
  2822. ;; Do not hide block when called from final blank lines.
  2823. (should-not
  2824. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  2825. (org-hide-block-toggle)
  2826. (goto-char (point-min))
  2827. (get-char-property (line-end-position) 'invisible)))
  2828. ;; Don't leave point in an invisible part of the buffer when hiding
  2829. ;; a block away.
  2830. (should-not
  2831. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  2832. (org-hide-block-toggle)
  2833. (get-char-property (point) 'invisible))))
  2834. (ert-deftest test-org/hide-block-toggle-maybe ()
  2835. "Test `org-hide-block-toggle-maybe' specifications."
  2836. (should
  2837. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  2838. (org-hide-block-toggle-maybe)))
  2839. (should-not
  2840. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  2841. (provide 'test-org)
  2842. ;;; test-org.el ends here