test-org.el 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956
  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. ;; Corner case: correctly insert a headline after an empty one.
  810. (should
  811. (equal "* \n* "
  812. (org-test-with-temp-text "* <point>"
  813. (org-insert-heading)
  814. (buffer-string)))))
  815. (ert-deftest test-org/insert-todo-heading-respect-content ()
  816. "Test `org-insert-todo-heading-respect-content' specifications."
  817. ;; Create a TODO heading.
  818. (should
  819. (org-test-with-temp-text "* H1\n Body"
  820. (org-insert-todo-heading-respect-content)
  821. (nth 2 (org-heading-components))))
  822. ;; Add headline at the end of the first subtree
  823. (should
  824. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  825. (search-forward "H1Body")
  826. (org-insert-todo-heading-respect-content)
  827. (and (eobp) (org-at-heading-p))))
  828. ;; In a list, do not create a new item.
  829. (should
  830. (org-test-with-temp-text "* H\n- an item\n- another one"
  831. (search-forward "an ")
  832. (org-insert-todo-heading-respect-content)
  833. (and (eobp) (org-at-heading-p)))))
  834. ;;; Fixed-Width Areas
  835. (ert-deftest test-org/toggle-fixed-width ()
  836. "Test `org-toggle-fixed-width' specifications."
  837. ;; No region: Toggle on fixed-width marker in paragraphs.
  838. (should
  839. (equal ": A"
  840. (org-test-with-temp-text "A"
  841. (org-toggle-fixed-width)
  842. (buffer-string))))
  843. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  844. (should
  845. (equal "A"
  846. (org-test-with-temp-text ": A"
  847. (org-toggle-fixed-width)
  848. (buffer-string))))
  849. ;; No region: Toggle on marker in blank lines after elements or just
  850. ;; after a headline.
  851. (should
  852. (equal "* H\n: "
  853. (org-test-with-temp-text "* H\n"
  854. (forward-line)
  855. (org-toggle-fixed-width)
  856. (buffer-string))))
  857. (should
  858. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  859. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  860. (goto-char (point-max))
  861. (org-toggle-fixed-width)
  862. (buffer-string))))
  863. ;; No region: Toggle on marker in front of one line elements (e.g.,
  864. ;; headlines, clocks)
  865. (should
  866. (equal ": * Headline"
  867. (org-test-with-temp-text "* Headline"
  868. (org-toggle-fixed-width)
  869. (buffer-string))))
  870. (should
  871. (equal ": #+KEYWORD: value"
  872. (org-test-with-temp-text "#+KEYWORD: value"
  873. (org-toggle-fixed-width)
  874. (buffer-string))))
  875. ;; No region: error in other situations.
  876. (should-error
  877. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  878. (forward-line)
  879. (org-toggle-fixed-width)
  880. (buffer-string)))
  881. ;; No region: Indentation is preserved.
  882. (should
  883. (equal "- A\n : B"
  884. (org-test-with-temp-text "- A\n B"
  885. (forward-line)
  886. (org-toggle-fixed-width)
  887. (buffer-string))))
  888. ;; Region: If it contains only fixed-width elements and blank lines,
  889. ;; toggle off fixed-width markup.
  890. (should
  891. (equal "A\n\nB"
  892. (org-test-with-temp-text ": A\n\n: B"
  893. (transient-mark-mode 1)
  894. (push-mark (point) t t)
  895. (goto-char (point-max))
  896. (org-toggle-fixed-width)
  897. (buffer-string))))
  898. ;; Region: If it contains anything else, toggle on fixed-width but
  899. ;; not on fixed-width areas.
  900. (should
  901. (equal ": A\n: \n: B\n: \n: C"
  902. (org-test-with-temp-text "A\n\n: B\n\nC"
  903. (transient-mark-mode 1)
  904. (push-mark (point) t t)
  905. (goto-char (point-max))
  906. (org-toggle-fixed-width)
  907. (buffer-string))))
  908. ;; Region: Ignore blank lines at its end, unless it contains only
  909. ;; such lines.
  910. (should
  911. (equal ": A\n\n"
  912. (org-test-with-temp-text "A\n\n"
  913. (transient-mark-mode 1)
  914. (push-mark (point) t t)
  915. (goto-char (point-max))
  916. (org-toggle-fixed-width)
  917. (buffer-string))))
  918. (should
  919. (equal ": \n: \n"
  920. (org-test-with-temp-text "\n\n"
  921. (transient-mark-mode 1)
  922. (push-mark (point) t t)
  923. (goto-char (point-max))
  924. (org-toggle-fixed-width)
  925. (buffer-string)))))
  926. ;;; Headline
  927. (ert-deftest test-org/in-commented-heading-p ()
  928. "Test `org-in-commented-heading-p' specifications."
  929. ;; Commented headline.
  930. (should
  931. (org-test-with-temp-text "* COMMENT Headline\nBody"
  932. (goto-char (point-max))
  933. (org-in-commented-heading-p)))
  934. ;; Commented ancestor.
  935. (should
  936. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  937. (goto-char (point-max))
  938. (org-in-commented-heading-p)))
  939. ;; Comment keyword is case-sensitive.
  940. (should-not
  941. (org-test-with-temp-text "* Comment Headline\nBody"
  942. (goto-char (point-max))
  943. (org-in-commented-heading-p)))
  944. ;; Keyword is standalone.
  945. (should-not
  946. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  947. (goto-char (point-max))
  948. (org-in-commented-heading-p)))
  949. ;; Optional argument.
  950. (should-not
  951. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  952. (goto-char (point-max))
  953. (org-in-commented-heading-p t))))
  954. ;;; Keywords
  955. (ert-deftest test-org/set-regexps-and-options ()
  956. "Test `org-set-regexps-and-options' specifications."
  957. ;; TAGS keyword.
  958. (should
  959. (equal '(("A" . ?a) ("B") ("C"))
  960. (org-test-with-temp-text "#+TAGS: A(a) B C"
  961. (org-mode-restart)
  962. org-tag-alist)))
  963. (should
  964. (equal '(("A") (:newline) ("B"))
  965. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  966. (org-mode-restart)
  967. org-tag-alist)))
  968. (should
  969. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  970. (org-test-with-temp-text "#+TAGS: { A B } C"
  971. (org-mode-restart)
  972. org-tag-alist)))
  973. (should
  974. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  975. (org-test-with-temp-text "#+TAGS: { A : B C }"
  976. (org-mode-restart)
  977. org-tag-alist)))
  978. (should
  979. (equal '(("A" "B" "C"))
  980. (org-test-with-temp-text "#+TAGS: { A : B C }"
  981. (org-mode-restart)
  982. org-tag-groups-alist)))
  983. ;; FILETAGS keyword.
  984. (should
  985. (equal '("A" "B" "C")
  986. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  987. (org-mode-restart)
  988. org-file-tags)))
  989. ;; PROPERTY keyword. Property names are case-insensitive.
  990. (should
  991. (equal "foo=1"
  992. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  993. (org-mode-restart)
  994. (cdr (assoc "var" org-file-properties)))))
  995. (should
  996. (equal
  997. "foo=1 bar=2"
  998. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  999. (org-mode-restart)
  1000. (cdr (assoc "var" org-file-properties)))))
  1001. (should
  1002. (equal
  1003. "foo=1 bar=2"
  1004. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1005. (org-mode-restart)
  1006. (cdr (assoc "var" org-file-properties)))))
  1007. ;; ARCHIVE keyword.
  1008. (should
  1009. (equal "%s_done::"
  1010. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1011. (org-mode-restart)
  1012. org-archive-location)))
  1013. ;; CATEGORY keyword.
  1014. (should
  1015. (eq 'test
  1016. (org-test-with-temp-text "#+CATEGORY: test"
  1017. (org-mode-restart)
  1018. org-category)))
  1019. (should
  1020. (equal "test"
  1021. (org-test-with-temp-text "#+CATEGORY: test"
  1022. (org-mode-restart)
  1023. (cdr (assoc "CATEGORY" org-file-properties)))))
  1024. ;; COLUMNS keyword.
  1025. (should
  1026. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1027. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1028. (org-mode-restart)
  1029. org-columns-default-format)))
  1030. ;; CONSTANTS keyword. Constants names are case sensitive.
  1031. (should
  1032. (equal '("299792458." "3.14")
  1033. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1034. (org-mode-restart)
  1035. (mapcar
  1036. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1037. '("c" "pi")))))
  1038. (should
  1039. (equal "3.14"
  1040. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1041. (org-mode-restart)
  1042. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1043. (should
  1044. (equal "22/7"
  1045. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1046. (org-mode-restart)
  1047. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1048. ;; LINK keyword.
  1049. (should
  1050. (equal
  1051. '("url1" "url2")
  1052. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1053. (org-mode-restart)
  1054. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1055. '("a" "b")))))
  1056. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1057. (should
  1058. (equal
  1059. '(?X ?Z ?Y)
  1060. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1061. (org-mode-restart)
  1062. (list org-highest-priority org-lowest-priority org-default-priority))))
  1063. (should
  1064. (equal
  1065. '(?A ?C ?B)
  1066. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1067. (org-mode-restart)
  1068. (list org-highest-priority org-lowest-priority org-default-priority))))
  1069. ;; STARTUP keyword.
  1070. (should
  1071. (equal '(t t)
  1072. (org-test-with-temp-text "#+STARTUP: fold odd"
  1073. (org-mode-restart)
  1074. (list org-startup-folded org-odd-levels-only))))
  1075. ;; TODO keywords.
  1076. (should
  1077. (equal '(("A" "B") ("C"))
  1078. (org-test-with-temp-text "#+TODO: A B | C"
  1079. (org-mode-restart)
  1080. (list org-not-done-keywords org-done-keywords))))
  1081. (should
  1082. (equal '(("A" "C") ("B" "D"))
  1083. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1084. (org-mode-restart)
  1085. (list org-not-done-keywords org-done-keywords))))
  1086. (should
  1087. (equal '(("A" "B") ("C"))
  1088. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1089. (org-mode-restart)
  1090. (list org-not-done-keywords org-done-keywords))))
  1091. (should
  1092. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1093. (org-test-with-temp-text "#+TODO: A(a)"
  1094. (org-mode-restart)
  1095. org-todo-key-alist)))
  1096. (should
  1097. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1098. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1099. (org-mode-restart)
  1100. org-todo-log-states)))
  1101. ;; Enter SETUPFILE keyword.
  1102. (should
  1103. (equal "1"
  1104. (org-test-with-temp-text
  1105. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1106. (org-mode-restart)
  1107. (cdr (assoc "a" org-file-properties))))))
  1108. ;;; Links
  1109. ;;;; Coderefs
  1110. (ert-deftest test-org/coderef ()
  1111. "Test coderef links specifications."
  1112. (should
  1113. (org-test-with-temp-text "
  1114. #+BEGIN_SRC emacs-lisp
  1115. \(+ 1 1) (ref:sc)
  1116. #+END_SRC
  1117. \[[(sc)]]"
  1118. (goto-char (point-max))
  1119. (org-open-at-point)
  1120. (looking-at "(ref:sc)"))))
  1121. ;;;; Custom ID
  1122. (ert-deftest test-org/custom-id ()
  1123. "Test custom ID links specifications."
  1124. (should
  1125. (org-test-with-temp-text
  1126. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  1127. (goto-char (point-max))
  1128. (org-open-at-point)
  1129. (org-looking-at-p "\\* H1"))))
  1130. ;;;; Fuzzy Links
  1131. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1132. ;; a named element (#+name: text) and to headlines (* Text).
  1133. (ert-deftest test-org/fuzzy-links ()
  1134. "Test fuzzy links specifications."
  1135. ;; Fuzzy link goes in priority to a matching target.
  1136. (should
  1137. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  1138. (goto-line 5)
  1139. (org-open-at-point)
  1140. (looking-at "<<Test>>")))
  1141. ;; Then fuzzy link points to an element with a given name.
  1142. (should
  1143. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  1144. (goto-line 5)
  1145. (org-open-at-point)
  1146. (looking-at "#\\+NAME: Test")))
  1147. ;; A target still lead to a matching headline otherwise.
  1148. (should
  1149. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  1150. (goto-line 4)
  1151. (org-open-at-point)
  1152. (looking-at "\\* Head2")))
  1153. ;; With a leading star in link, enforce heading match.
  1154. (should
  1155. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  1156. (goto-line 3)
  1157. (org-open-at-point)
  1158. (looking-at "\\* Test")))
  1159. ;; Correctly un-hexify fuzzy links.
  1160. (should
  1161. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  1162. (goto-char (point-max))
  1163. (org-open-at-point)
  1164. (bobp))))
  1165. ;;;; Link Escaping
  1166. (ert-deftest test-org/org-link-escape-ascii-character ()
  1167. "Escape an ascii character."
  1168. (should
  1169. (string=
  1170. "%5B"
  1171. (org-link-escape "["))))
  1172. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  1173. "Escape an ascii control character."
  1174. (should
  1175. (string=
  1176. "%09"
  1177. (org-link-escape "\t"))))
  1178. (ert-deftest test-org/org-link-escape-multibyte-character ()
  1179. "Escape an unicode multibyte character."
  1180. (should
  1181. (string=
  1182. "%E2%82%AC"
  1183. (org-link-escape "€"))))
  1184. (ert-deftest test-org/org-link-escape-custom-table ()
  1185. "Escape string with custom character table."
  1186. (should
  1187. (string=
  1188. "Foo%3A%42ar%0A"
  1189. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  1190. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  1191. "Escape string with custom table merged with default table."
  1192. (should
  1193. (string=
  1194. "%5BF%6F%6F%3A%42ar%0A%5D"
  1195. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  1196. (ert-deftest test-org/org-link-unescape-ascii-character ()
  1197. "Unescape an ascii character."
  1198. (should
  1199. (string=
  1200. "["
  1201. (org-link-unescape "%5B"))))
  1202. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  1203. "Unescpae an ascii control character."
  1204. (should
  1205. (string=
  1206. "\n"
  1207. (org-link-unescape "%0A"))))
  1208. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  1209. "Unescape unicode multibyte character."
  1210. (should
  1211. (string=
  1212. "€"
  1213. (org-link-unescape "%E2%82%AC"))))
  1214. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  1215. "Unescape old style percent escaped character."
  1216. (should
  1217. (string=
  1218. "àâçèéêîôùû"
  1219. (decode-coding-string
  1220. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  1221. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  1222. "Escape and unescape a URL that includes an escaped char.
  1223. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  1224. (should
  1225. (string=
  1226. "http://some.host.com/form?&id=blah%2Bblah25"
  1227. (org-link-unescape
  1228. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  1229. (ert-deftest test-org/org-link-escape-chars-browser ()
  1230. "Test of the constant `org-link-escape-chars-browser'.
  1231. See there why this test is a candidate to be removed once Org
  1232. drops support for Emacs 24.1 and 24.2."
  1233. (should
  1234. (string=
  1235. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1236. "%22Release%208.2%22&idxname=emacs-orgmode")
  1237. (org-link-escape-browser ; Do not replace with `url-encode-url',
  1238. ; see docstring above.
  1239. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1240. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  1241. ;;;; Open at point
  1242. (ert-deftest test-org/open-at-point-in-property ()
  1243. "Does `org-open-at-point' open link in property drawer?"
  1244. (should
  1245. (org-test-with-temp-text
  1246. "* Headline
  1247. :PROPERTIES:
  1248. :URL: <point>[[info:emacs#Top]]
  1249. :END:"
  1250. (org-open-at-point) t)))
  1251. (ert-deftest test-org/open-at-point-in-comment ()
  1252. "Does `org-open-at-point' open link in a commented line?"
  1253. (should
  1254. (org-test-with-temp-text
  1255. "# <point>[[info:emacs#Top]]"
  1256. (org-open-at-point) t)))
  1257. (ert-deftest test-org/open-at-point/info ()
  1258. "Test `org-open-at-point' on info links."
  1259. (should
  1260. (org-test-with-temp-text
  1261. "<point>[[info:emacs#Top]]"
  1262. (org-open-at-point)
  1263. (and (switch-to-buffer "*info*")
  1264. (prog1
  1265. (looking-at "\nThe Emacs Editor")
  1266. (kill-buffer))))))
  1267. ;;; Node Properties
  1268. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1269. "Ensure properties accumulate in subtree drawers."
  1270. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1271. (org-babel-next-src-block)
  1272. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1273. (ert-deftest test-org/custom-properties ()
  1274. "Test custom properties specifications."
  1275. ;; Standard test.
  1276. (should
  1277. (let ((org-custom-properties '("FOO")))
  1278. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1279. (org-toggle-custom-properties-visibility)
  1280. (org-invisible-p2))))
  1281. ;; Properties are case-insensitive.
  1282. (should
  1283. (let ((org-custom-properties '("FOO")))
  1284. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  1285. (org-toggle-custom-properties-visibility)
  1286. (org-invisible-p2))))
  1287. (should
  1288. (let ((org-custom-properties '("foo")))
  1289. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1290. (org-toggle-custom-properties-visibility)
  1291. (org-invisible-p2))))
  1292. ;; Multiple custom properties in the same drawer.
  1293. (should
  1294. (let ((org-custom-properties '("FOO" "BAR")))
  1295. (org-test-with-temp-text
  1296. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  1297. (org-toggle-custom-properties-visibility)
  1298. (and (org-invisible-p2)
  1299. (not (progn (forward-line) (org-invisible-p2)))
  1300. (progn (forward-line) (org-invisible-p2))))))
  1301. ;; Hide custom properties with an empty value.
  1302. (should
  1303. (let ((org-custom-properties '("FOO")))
  1304. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  1305. (org-toggle-custom-properties-visibility)
  1306. (org-invisible-p2))))
  1307. ;; Do not hide fake properties.
  1308. (should-not
  1309. (let ((org-custom-properties '("FOO")))
  1310. (org-test-with-temp-text ":FOO: val\n"
  1311. (org-toggle-custom-properties-visibility)
  1312. (org-invisible-p2))))
  1313. (should-not
  1314. (let ((org-custom-properties '("A")))
  1315. (org-test-with-temp-text
  1316. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  1317. (org-toggle-custom-properties-visibility)
  1318. (org-invisible-p2)))))
  1319. ;;; Mark Region
  1320. (ert-deftest test-org/mark-subtree ()
  1321. "Test `org-mark-subtree' specifications."
  1322. ;; Error when point is before first headline.
  1323. (should-error
  1324. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1325. (progn (transient-mark-mode 1)
  1326. (org-mark-subtree))))
  1327. ;; Without argument, mark current subtree.
  1328. (should
  1329. (equal
  1330. '(12 32)
  1331. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1332. (progn (transient-mark-mode 1)
  1333. (forward-line 2)
  1334. (org-mark-subtree)
  1335. (list (region-beginning) (region-end))))))
  1336. ;; With an argument, move ARG up.
  1337. (should
  1338. (equal
  1339. '(1 32)
  1340. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1341. (progn (transient-mark-mode 1)
  1342. (forward-line 2)
  1343. (org-mark-subtree 1)
  1344. (list (region-beginning) (region-end))))))
  1345. ;; Do not get fooled by inlinetasks.
  1346. (when (featurep 'org-inlinetask)
  1347. (should
  1348. (= 1
  1349. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1350. (progn (transient-mark-mode 1)
  1351. (forward-line 1)
  1352. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1353. (region-beginning)))))))
  1354. ;;; Navigation
  1355. (ert-deftest test-org/beginning-of-line ()
  1356. "Test `org-beginning-of-line' specifications."
  1357. ;; Standard test.
  1358. (should
  1359. (org-test-with-temp-text "Some text\nSome other text"
  1360. (progn (org-beginning-of-line) (bolp))))
  1361. ;; Standard test with `visual-line-mode'.
  1362. (should-not
  1363. (org-test-with-temp-text "A long line of text\nSome other text"
  1364. (progn (visual-line-mode)
  1365. (forward-char 2)
  1366. (dotimes (i 1000) (insert "very "))
  1367. (org-beginning-of-line)
  1368. (bolp))))
  1369. ;; At an headline with special movement.
  1370. (should
  1371. (org-test-with-temp-text "* TODO Headline"
  1372. (let ((org-special-ctrl-a/e t))
  1373. (org-end-of-line)
  1374. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1375. (progn (org-beginning-of-line) (bolp))
  1376. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1377. (ert-deftest test-org/end-of-line ()
  1378. "Test `org-end-of-line' specifications."
  1379. ;; Standard test.
  1380. (should
  1381. (org-test-with-temp-text "Some text\nSome other text"
  1382. (progn (org-end-of-line) (eolp))))
  1383. ;; Standard test with `visual-line-mode'.
  1384. (should-not
  1385. (org-test-with-temp-text "A long line of text\nSome other text"
  1386. (progn (visual-line-mode)
  1387. (forward-char 2)
  1388. (dotimes (i 1000) (insert "very "))
  1389. (goto-char (point-min))
  1390. (org-end-of-line)
  1391. (eolp))))
  1392. ;; At an headline with special movement.
  1393. (should
  1394. (org-test-with-temp-text "* Headline1 :tag:\n"
  1395. (let ((org-special-ctrl-a/e t))
  1396. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1397. (progn (org-end-of-line) (eolp))
  1398. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1399. ;; At an headline without special movement.
  1400. (should
  1401. (org-test-with-temp-text "* Headline2 :tag:\n"
  1402. (let ((org-special-ctrl-a/e nil))
  1403. (and (progn (org-end-of-line) (eolp))
  1404. (progn (org-end-of-line) (eolp))))))
  1405. ;; At an headline, with reversed movement.
  1406. (should
  1407. (org-test-with-temp-text "* Headline3 :tag:\n"
  1408. (let ((org-special-ctrl-a/e 'reversed)
  1409. (this-command last-command))
  1410. (and (progn (org-end-of-line) (eolp))
  1411. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1412. ;; At a block without hidden contents.
  1413. (should
  1414. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1415. (progn (org-end-of-line) (eolp))))
  1416. ;; At a block with hidden contents.
  1417. (should-not
  1418. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1419. (let ((org-special-ctrl-a/e t))
  1420. (org-hide-block-toggle)
  1421. (org-end-of-line)
  1422. (eobp)))))
  1423. (ert-deftest test-org/forward-paragraph ()
  1424. "Test `org-forward-paragraph' specifications."
  1425. ;; At end of buffer, return an error.
  1426. (should-error
  1427. (org-test-with-temp-text "Paragraph"
  1428. (goto-char (point-max))
  1429. (org-forward-paragraph)))
  1430. ;; Standard test.
  1431. (should
  1432. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1433. (org-forward-paragraph)
  1434. (looking-at "P2")))
  1435. ;; Ignore depth.
  1436. (should
  1437. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1438. (org-forward-paragraph)
  1439. (looking-at "P1")))
  1440. ;; Do not enter elements with invisible contents.
  1441. (should
  1442. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1443. (org-hide-block-toggle)
  1444. (org-forward-paragraph)
  1445. (looking-at "P3")))
  1446. ;; On an affiliated keyword, jump to the beginning of the element.
  1447. (should
  1448. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1449. (org-forward-paragraph)
  1450. (looking-at "Para")))
  1451. ;; On an item or a footnote definition, move to the second element
  1452. ;; inside, if any.
  1453. (should
  1454. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1455. (org-forward-paragraph)
  1456. (looking-at " Paragraph")))
  1457. (should
  1458. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1459. (org-forward-paragraph)
  1460. (looking-at "Paragraph")))
  1461. ;; On an item, or a footnote definition, when the first line is
  1462. ;; empty, move to the first item.
  1463. (should
  1464. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1465. (org-forward-paragraph)
  1466. (looking-at " Paragraph")))
  1467. (should
  1468. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1469. (org-forward-paragraph)
  1470. (looking-at "Paragraph")))
  1471. ;; On a table (resp. a property drawer) do not move through table
  1472. ;; rows (resp. node properties).
  1473. (should
  1474. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1475. (org-forward-paragraph)
  1476. (looking-at "Paragraph")))
  1477. (should
  1478. (org-test-with-temp-text
  1479. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1480. (org-forward-paragraph)
  1481. (looking-at "Paragraph")))
  1482. ;; On a verse or source block, stop after blank lines.
  1483. (should
  1484. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1485. (org-forward-paragraph)
  1486. (looking-at "L2")))
  1487. (should
  1488. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1489. (org-forward-paragraph)
  1490. (looking-at "L2"))))
  1491. (ert-deftest test-org/backward-paragraph ()
  1492. "Test `org-backward-paragraph' specifications."
  1493. ;; Error at beginning of buffer.
  1494. (should-error
  1495. (org-test-with-temp-text "Paragraph"
  1496. (org-backward-paragraph)))
  1497. ;; Regular test.
  1498. (should
  1499. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1500. (goto-char (point-max))
  1501. (org-backward-paragraph)
  1502. (looking-at "P3")))
  1503. (should
  1504. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1505. (goto-char (point-max))
  1506. (beginning-of-line)
  1507. (org-backward-paragraph)
  1508. (looking-at "P2")))
  1509. ;; Ignore depth.
  1510. (should
  1511. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1512. (goto-char (point-max))
  1513. (beginning-of-line)
  1514. (org-backward-paragraph)
  1515. (looking-at "P2")))
  1516. ;; Ignore invisible elements.
  1517. (should
  1518. (org-test-with-temp-text "* H1\n P1\n* H2"
  1519. (org-cycle)
  1520. (goto-char (point-max))
  1521. (beginning-of-line)
  1522. (org-backward-paragraph)
  1523. (bobp)))
  1524. ;; On an affiliated keyword, jump to the first one.
  1525. (should
  1526. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1527. (search-forward "c2")
  1528. (org-backward-paragraph)
  1529. (looking-at "#\\+name")))
  1530. ;; On the second element in an item or a footnote definition, jump
  1531. ;; to item or the definition.
  1532. (should
  1533. (org-test-with-temp-text "- line1\n\n line2"
  1534. (goto-char (point-max))
  1535. (beginning-of-line)
  1536. (org-backward-paragraph)
  1537. (looking-at "- line1")))
  1538. (should
  1539. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1540. (goto-char (point-max))
  1541. (beginning-of-line)
  1542. (org-backward-paragraph)
  1543. (looking-at "\\[fn:1\\] line1")))
  1544. ;; On a table (resp. a property drawer), ignore table rows
  1545. ;; (resp. node properties).
  1546. (should
  1547. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1548. (goto-char (point-max))
  1549. (beginning-of-line)
  1550. (org-backward-paragraph)
  1551. (bobp)))
  1552. (should
  1553. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  1554. (org-backward-paragraph)
  1555. (looking-at ":PROPERTIES:")))
  1556. ;; On a source or verse block, stop before blank lines.
  1557. (should
  1558. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1559. (search-forward "L3")
  1560. (beginning-of-line)
  1561. (org-backward-paragraph)
  1562. (looking-at "L2")))
  1563. (should
  1564. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1565. (search-forward "L3")
  1566. (beginning-of-line)
  1567. (org-backward-paragraph)
  1568. (looking-at "L2"))))
  1569. (ert-deftest test-org/forward-element ()
  1570. "Test `org-forward-element' specifications."
  1571. ;; 1. At EOB: should error.
  1572. (org-test-with-temp-text "Some text\n"
  1573. (goto-char (point-max))
  1574. (should-error (org-forward-element)))
  1575. ;; 2. Standard move: expected to ignore blank lines.
  1576. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1577. (org-forward-element)
  1578. (should (looking-at (regexp-quote "Second paragraph."))))
  1579. ;; 3. Headline tests.
  1580. (org-test-with-temp-text "
  1581. * Head 1
  1582. ** Head 1.1
  1583. *** Head 1.1.1
  1584. ** Head 1.2"
  1585. ;; 3.1. At an headline beginning: move to next headline at the
  1586. ;; same level.
  1587. (goto-line 3)
  1588. (org-forward-element)
  1589. (should (looking-at (regexp-quote "** Head 1.2")))
  1590. ;; 3.2. At an headline beginning: move to parent headline if no
  1591. ;; headline at the same level.
  1592. (goto-line 3)
  1593. (org-forward-element)
  1594. (should (looking-at (regexp-quote "** Head 1.2"))))
  1595. ;; 4. Greater element tests.
  1596. (org-test-with-temp-text
  1597. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1598. ;; 4.1. At a greater element: expected to skip contents.
  1599. (org-forward-element)
  1600. (should (looking-at (regexp-quote "Outside.")))
  1601. ;; 4.2. At the end of greater element contents: expected to skip
  1602. ;; to the end of the greater element.
  1603. (goto-line 2)
  1604. (org-forward-element)
  1605. (should (looking-at (regexp-quote "Outside."))))
  1606. ;; 5. List tests.
  1607. (org-test-with-temp-text "
  1608. - item1
  1609. - sub1
  1610. - sub2
  1611. - sub3
  1612. Inner paragraph.
  1613. - item2
  1614. Outside."
  1615. ;; 5.1. At list top point: expected to move to the element after
  1616. ;; the list.
  1617. (goto-line 2)
  1618. (org-forward-element)
  1619. (should (looking-at (regexp-quote "Outside.")))
  1620. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1621. ;; beginning of line, move to next item.
  1622. (goto-line 2)
  1623. (forward-char)
  1624. (org-forward-element)
  1625. (should (looking-at "- item2"))
  1626. (goto-line 4)
  1627. (forward-char)
  1628. (org-forward-element)
  1629. (should (looking-at " - sub2"))
  1630. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1631. (goto-line 4)
  1632. (org-forward-element)
  1633. (should (looking-at (regexp-quote " Inner paragraph.")))
  1634. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1635. (goto-line 8)
  1636. (org-forward-element)
  1637. (should (looking-at (regexp-quote " Inner paragraph.")))
  1638. ;; 5.5. At an item: expected to move to next item, if any.
  1639. (goto-line 6)
  1640. (org-forward-element)
  1641. (should (looking-at " - sub3"))))
  1642. (ert-deftest test-org/backward-element ()
  1643. "Test `org-backward-element' specifications."
  1644. ;; 1. Should error at BOB.
  1645. (org-test-with-temp-text " \nParagraph."
  1646. (should-error (org-backward-element)))
  1647. ;; 2. Should move at BOB when called on the first element in buffer.
  1648. (should
  1649. (org-test-with-temp-text "\n#+TITLE: test"
  1650. (progn (forward-line)
  1651. (org-backward-element)
  1652. (bobp))))
  1653. ;; 3. Not at the beginning of an element: move at its beginning.
  1654. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1655. (goto-line 3)
  1656. (end-of-line)
  1657. (org-backward-element)
  1658. (should (looking-at (regexp-quote "Paragraph2."))))
  1659. ;; 4. Headline tests.
  1660. (org-test-with-temp-text "
  1661. * Head 1
  1662. ** Head 1.1
  1663. *** Head 1.1.1
  1664. ** Head 1.2"
  1665. ;; 4.1. At an headline beginning: move to previous headline at the
  1666. ;; same level.
  1667. (goto-line 5)
  1668. (org-backward-element)
  1669. (should (looking-at (regexp-quote "** Head 1.1")))
  1670. ;; 4.2. At an headline beginning: move to parent headline if no
  1671. ;; headline at the same level.
  1672. (goto-line 3)
  1673. (org-backward-element)
  1674. (should (looking-at (regexp-quote "* Head 1")))
  1675. ;; 4.3. At the first top-level headline: should error.
  1676. (goto-line 2)
  1677. (should-error (org-backward-element)))
  1678. ;; 5. At beginning of first element inside a greater element:
  1679. ;; expected to move to greater element's beginning.
  1680. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1681. (goto-line 3)
  1682. (org-backward-element)
  1683. (should (looking-at "#\\+BEGIN_CENTER")))
  1684. ;; 6. At the beginning of the first element in a section: should
  1685. ;; move back to headline, if any.
  1686. (should
  1687. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1688. (progn (goto-char (point-max))
  1689. (beginning-of-line)
  1690. (org-backward-element)
  1691. (org-at-heading-p))))
  1692. ;; 7. List tests.
  1693. (org-test-with-temp-text "
  1694. - item1
  1695. - sub1
  1696. - sub2
  1697. - sub3
  1698. Inner paragraph.
  1699. - item2
  1700. Outside."
  1701. ;; 7.1. At beginning of sub-list: expected to move to the
  1702. ;; paragraph before it.
  1703. (goto-line 4)
  1704. (org-backward-element)
  1705. (should (looking-at "item1"))
  1706. ;; 7.2. At an item in a list: expected to move at previous item.
  1707. (goto-line 8)
  1708. (org-backward-element)
  1709. (should (looking-at " - sub2"))
  1710. (goto-line 12)
  1711. (org-backward-element)
  1712. (should (looking-at "- item1"))
  1713. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1714. ;; beginning.
  1715. (goto-line 10)
  1716. (org-backward-element)
  1717. (should (looking-at " - sub1"))
  1718. (goto-line 15)
  1719. (org-backward-element)
  1720. (should (looking-at "- item1"))
  1721. ;; 7.4. At blank-lines before list end: expected to move to top
  1722. ;; item.
  1723. (goto-line 14)
  1724. (org-backward-element)
  1725. (should (looking-at "- item1"))))
  1726. (ert-deftest test-org/up-element ()
  1727. "Test `org-up-element' specifications."
  1728. ;; 1. At BOB or with no surrounding element: should error.
  1729. (org-test-with-temp-text "Paragraph."
  1730. (should-error (org-up-element)))
  1731. (org-test-with-temp-text "* Head1\n* Head2"
  1732. (goto-line 2)
  1733. (should-error (org-up-element)))
  1734. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1735. (goto-line 3)
  1736. (should-error (org-up-element)))
  1737. ;; 2. At an headline: move to parent headline.
  1738. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1739. (goto-line 3)
  1740. (org-up-element)
  1741. (should (looking-at "\\* Head1")))
  1742. ;; 3. Inside a greater element: move to greater element beginning.
  1743. (org-test-with-temp-text
  1744. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1745. (goto-line 3)
  1746. (org-up-element)
  1747. (should (looking-at "#\\+BEGIN_CENTER")))
  1748. ;; 4. List tests.
  1749. (org-test-with-temp-text "* Top
  1750. - item1
  1751. - sub1
  1752. - sub2
  1753. Paragraph within sub2.
  1754. - item2"
  1755. ;; 4.1. Within an item: move to the item beginning.
  1756. (goto-line 8)
  1757. (org-up-element)
  1758. (should (looking-at " - sub2"))
  1759. ;; 4.2. At an item in a sub-list: move to parent item.
  1760. (goto-line 4)
  1761. (org-up-element)
  1762. (should (looking-at "- item1"))
  1763. ;; 4.3. At an item in top list: move to beginning of whole list.
  1764. (goto-line 10)
  1765. (org-up-element)
  1766. (should (looking-at "- item1"))
  1767. ;; 4.4. Special case. At very top point: should move to parent of
  1768. ;; list.
  1769. (goto-line 2)
  1770. (org-up-element)
  1771. (should (looking-at "\\* Top"))))
  1772. (ert-deftest test-org/down-element ()
  1773. "Test `org-down-element' specifications."
  1774. ;; Error when the element hasn't got a recursive type.
  1775. (org-test-with-temp-text "Paragraph."
  1776. (should-error (org-down-element)))
  1777. ;; Error when the element has no contents
  1778. (org-test-with-temp-text "* Headline"
  1779. (should-error (org-down-element)))
  1780. ;; When at a plain-list, move to first item.
  1781. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1782. (goto-line 2)
  1783. (org-down-element)
  1784. (should (looking-at " - Item 1.1")))
  1785. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1786. (org-down-element)
  1787. (should (looking-at " Item 1")))
  1788. ;; When at a table, move to first row
  1789. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1790. (org-down-element)
  1791. (should (looking-at " a | b |")))
  1792. ;; Otherwise, move inside the greater element.
  1793. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1794. (org-down-element)
  1795. (should (looking-at "Paragraph"))))
  1796. (ert-deftest test-org/drag-element-backward ()
  1797. "Test `org-drag-element-backward' specifications."
  1798. ;; Standard test.
  1799. (should
  1800. (equal
  1801. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  1802. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  1803. (org-drag-element-backward)
  1804. (buffer-string))))
  1805. (should
  1806. (equal
  1807. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  1808. (org-test-with-temp-text
  1809. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  1810. (org-drag-element-backward)
  1811. (buffer-string))))
  1812. ;; Preserve blank lines.
  1813. (should
  1814. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  1815. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  1816. (org-drag-element-backward)
  1817. (buffer-string))))
  1818. ;; Preserve column.
  1819. (should
  1820. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  1821. (org-drag-element-backward)
  1822. (org-looking-at-p "2")))
  1823. ;; Error when trying to move first element of buffer.
  1824. (should-error
  1825. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1826. (org-drag-element-backward)))
  1827. ;; Error when trying to swap nested elements.
  1828. (should-error
  1829. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1830. (forward-line)
  1831. (org-drag-element-backward)))
  1832. ;; Error when trying to swap an headline element and a non-headline
  1833. ;; element.
  1834. (should-error
  1835. (org-test-with-temp-text "Test.\n* Head 1"
  1836. (forward-line)
  1837. (org-drag-element-backward)))
  1838. ;; Preserve visibility of elements and their contents.
  1839. (should
  1840. (equal '((63 . 82) (26 . 48))
  1841. (org-test-with-temp-text "
  1842. #+BEGIN_CENTER
  1843. Text.
  1844. #+END_CENTER
  1845. - item 1
  1846. #+BEGIN_QUOTE
  1847. Text.
  1848. #+END_QUOTE"
  1849. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1850. (search-backward "- item 1")
  1851. (org-drag-element-backward)
  1852. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1853. (overlays-in (point-min) (point-max)))))))
  1854. (ert-deftest test-org/drag-element-forward ()
  1855. "Test `org-drag-element-forward' specifications."
  1856. ;; 1. Error when trying to move first element of buffer.
  1857. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1858. (goto-line 3)
  1859. (should-error (org-drag-element-forward)))
  1860. ;; 2. Error when trying to swap nested elements.
  1861. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1862. (forward-line)
  1863. (should-error (org-drag-element-forward)))
  1864. ;; 3. Error when trying to swap a non-headline element and an
  1865. ;; headline.
  1866. (org-test-with-temp-text "Test.\n* Head 1"
  1867. (should-error (org-drag-element-forward)))
  1868. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1869. ;; between elements.
  1870. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1871. (search-forward "graph")
  1872. (org-drag-element-forward)
  1873. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1874. (should (looking-at " 1")))
  1875. ;; 5. Preserve visibility of elements and their contents.
  1876. (org-test-with-temp-text "
  1877. #+BEGIN_CENTER
  1878. Text.
  1879. #+END_CENTER
  1880. - item 1
  1881. #+BEGIN_QUOTE
  1882. Text.
  1883. #+END_QUOTE"
  1884. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1885. (search-backward "#+BEGIN_CENTER")
  1886. (org-drag-element-forward)
  1887. (should
  1888. (equal
  1889. '((63 . 82) (26 . 48))
  1890. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1891. (overlays-in (point-min) (point-max)))))))
  1892. ;;; Outline structure
  1893. (ert-deftest test-org/demote ()
  1894. "Test `org-demote' specifications."
  1895. ;; Add correct number of stars according to `org-odd-levels-only'.
  1896. (should
  1897. (= 2
  1898. (org-test-with-temp-text "* H"
  1899. (let ((org-odd-levels-only nil)) (org-demote))
  1900. (org-current-level))))
  1901. (should
  1902. (= 3
  1903. (org-test-with-temp-text "* H"
  1904. (let ((org-odd-levels-only t)) (org-demote))
  1905. (org-current-level))))
  1906. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  1907. (should
  1908. (org-test-with-temp-text "* H :tag:"
  1909. (let ((org-tags-column 10)
  1910. (org-auto-align-tags t)
  1911. (org-odd-levels-only nil))
  1912. (org-demote))
  1913. (org-move-to-column 10)
  1914. (org-looking-at-p ":tag:$")))
  1915. (should-not
  1916. (org-test-with-temp-text "* H :tag:"
  1917. (let ((org-tags-column 10)
  1918. (org-auto-align-tags nil)
  1919. (org-odd-levels-only nil))
  1920. (org-demote))
  1921. (org-move-to-column 10)
  1922. (org-looking-at-p ":tag:$")))
  1923. ;; When `org-adapt-indentation' is non-nil, always indent planning
  1924. ;; info and property drawers accordingly.
  1925. (should
  1926. (= 3
  1927. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  1928. (let ((org-odd-levels-only nil)
  1929. (org-adapt-indentation t))
  1930. (org-demote))
  1931. (forward-line)
  1932. (org-get-indentation))))
  1933. (should
  1934. (= 3
  1935. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  1936. (let ((org-odd-levels-only nil)
  1937. (org-adapt-indentation t))
  1938. (org-demote))
  1939. (forward-line)
  1940. (org-get-indentation))))
  1941. (should-not
  1942. (= 3
  1943. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  1944. (let ((org-odd-levels-only nil)
  1945. (org-adapt-indentation nil))
  1946. (org-demote))
  1947. (forward-line)
  1948. (org-get-indentation))))
  1949. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  1950. ;; section accordingly. Ignore, however, footnote definitions and
  1951. ;; inlinetasks boundaries.
  1952. (should
  1953. (= 3
  1954. (org-test-with-temp-text "* H\n Paragraph"
  1955. (let ((org-odd-levels-only nil)
  1956. (org-adapt-indentation t))
  1957. (org-demote))
  1958. (forward-line)
  1959. (org-get-indentation))))
  1960. (should
  1961. (= 2
  1962. (org-test-with-temp-text "* H\n Paragraph"
  1963. (let ((org-odd-levels-only nil)
  1964. (org-adapt-indentation nil))
  1965. (org-demote))
  1966. (forward-line)
  1967. (org-get-indentation))))
  1968. (should
  1969. (zerop
  1970. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  1971. (let ((org-odd-levels-only nil)
  1972. (org-adapt-indentation t))
  1973. (org-demote))
  1974. (goto-char (point-max))
  1975. (org-get-indentation))))
  1976. (should
  1977. (= 3
  1978. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  1979. (let ((org-odd-levels-only nil)
  1980. (org-adapt-indentation t))
  1981. (org-demote))
  1982. (goto-char (point-max))
  1983. (org-get-indentation))))
  1984. (when (featurep 'org-inlinetask)
  1985. (should
  1986. (zerop
  1987. (let ((org-inlinetask-min-level 5)
  1988. (org-adapt-indentation t))
  1989. (org-test-with-temp-text "* H\n***** I\n***** END"
  1990. (org-demote)
  1991. (forward-line)
  1992. (org-get-indentation))))))
  1993. (when (featurep 'org-inlinetask)
  1994. (should
  1995. (= 3
  1996. (let ((org-inlinetask-min-level 5)
  1997. (org-adapt-indentation t))
  1998. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  1999. (org-demote)
  2000. (forward-line 2)
  2001. (org-get-indentation))))))
  2002. ;; Ignore contents of source blocks or example blocks when
  2003. ;; indentation should be preserved (through
  2004. ;; `org-src-preserve-indentation' or "-i" flag).
  2005. (should-not
  2006. (zerop
  2007. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2008. (let ((org-adapt-indentation t)
  2009. (org-src-preserve-indentation nil))
  2010. (org-demote))
  2011. (forward-line 2)
  2012. (org-get-indentation))))
  2013. (should
  2014. (zerop
  2015. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2016. (let ((org-adapt-indentation t)
  2017. (org-src-preserve-indentation t))
  2018. (org-demote))
  2019. (forward-line 2)
  2020. (org-get-indentation))))
  2021. (should
  2022. (zerop
  2023. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2024. (let ((org-adapt-indentation t)
  2025. (org-src-preserve-indentation t))
  2026. (org-demote))
  2027. (forward-line 2)
  2028. (org-get-indentation))))
  2029. (should
  2030. (zerop
  2031. (org-test-with-temp-text
  2032. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  2033. (let ((org-adapt-indentation t)
  2034. (org-src-preserve-indentation nil))
  2035. (org-demote))
  2036. (forward-line 2)
  2037. (org-get-indentation)))))
  2038. (ert-deftest test-org/promote ()
  2039. "Test `org-promote' specifications."
  2040. ;; Return an error if headline is to be promoted to level 0, unless
  2041. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  2042. ;; headline becomes a comment.
  2043. (should-error
  2044. (org-test-with-temp-text "* H"
  2045. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  2046. (should
  2047. (equal "# H"
  2048. (org-test-with-temp-text "* H"
  2049. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  2050. (buffer-string))))
  2051. ;; Remove correct number of stars according to
  2052. ;; `org-odd-levels-only'.
  2053. (should
  2054. (= 2
  2055. (org-test-with-temp-text "*** H"
  2056. (let ((org-odd-levels-only nil)) (org-promote))
  2057. (org-current-level))))
  2058. (should
  2059. (= 1
  2060. (org-test-with-temp-text "*** H"
  2061. (let ((org-odd-levels-only t)) (org-promote))
  2062. (org-current-level))))
  2063. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  2064. (should
  2065. (org-test-with-temp-text "** H :tag:"
  2066. (let ((org-tags-column 10)
  2067. (org-auto-align-tags t)
  2068. (org-odd-levels-only nil))
  2069. (org-promote))
  2070. (org-move-to-column 10)
  2071. (org-looking-at-p ":tag:$")))
  2072. (should-not
  2073. (org-test-with-temp-text "** H :tag:"
  2074. (let ((org-tags-column 10)
  2075. (org-auto-align-tags nil)
  2076. (org-odd-levels-only nil))
  2077. (org-promote))
  2078. (org-move-to-column 10)
  2079. (org-looking-at-p ":tag:$")))
  2080. ;; When `org-adapt-indentation' is non-nil, always indent planning
  2081. ;; info and property drawers.
  2082. (should
  2083. (= 2
  2084. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2085. (let ((org-odd-levels-only nil)
  2086. (org-adapt-indentation t))
  2087. (org-promote))
  2088. (forward-line)
  2089. (org-get-indentation))))
  2090. (should
  2091. (= 2
  2092. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2093. (let ((org-odd-levels-only nil)
  2094. (org-adapt-indentation t))
  2095. (org-promote))
  2096. (forward-line)
  2097. (org-get-indentation))))
  2098. (should-not
  2099. (= 2
  2100. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2101. (let ((org-odd-levels-only nil)
  2102. (org-adapt-indentation nil))
  2103. (org-promote))
  2104. (forward-line)
  2105. (org-get-indentation))))
  2106. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2107. ;; section accordingly. Ignore, however, footnote definitions and
  2108. ;; inlinetasks boundaries.
  2109. (should
  2110. (= 2
  2111. (org-test-with-temp-text "** H\n Paragraph"
  2112. (let ((org-odd-levels-only nil)
  2113. (org-adapt-indentation t))
  2114. (org-promote))
  2115. (forward-line)
  2116. (org-get-indentation))))
  2117. (should-not
  2118. (= 2
  2119. (org-test-with-temp-text "** H\n Paragraph"
  2120. (let ((org-odd-levels-only nil)
  2121. (org-adapt-indentation nil))
  2122. (org-promote))
  2123. (forward-line)
  2124. (org-get-indentation))))
  2125. (should
  2126. (= 2
  2127. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  2128. (let ((org-odd-levels-only nil)
  2129. (org-adapt-indentation t))
  2130. (org-promote))
  2131. (forward-line)
  2132. (org-get-indentation))))
  2133. (when (featurep 'org-inlinetask)
  2134. (should
  2135. (zerop
  2136. (let ((org-inlinetask-min-level 5)
  2137. (org-adapt-indentation t))
  2138. (org-test-with-temp-text "** H\n***** I\n***** END"
  2139. (org-promote)
  2140. (forward-line)
  2141. (org-get-indentation))))))
  2142. (when (featurep 'org-inlinetask)
  2143. (should
  2144. (= 2
  2145. (let ((org-inlinetask-min-level 5)
  2146. (org-adapt-indentation t))
  2147. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  2148. (org-promote)
  2149. (forward-line 2)
  2150. (org-get-indentation))))))
  2151. ;; Give up shifting if it would break document's structure
  2152. ;; otherwise.
  2153. (should
  2154. (= 3
  2155. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  2156. (let ((org-odd-levels-only nil)
  2157. (org-adapt-indentation t))
  2158. (org-promote))
  2159. (forward-line)
  2160. (org-get-indentation))))
  2161. (should
  2162. (= 3
  2163. (org-test-with-temp-text "** H\n Paragraph\n * list."
  2164. (let ((org-odd-levels-only nil)
  2165. (org-adapt-indentation t))
  2166. (org-promote))
  2167. (forward-line)
  2168. (org-get-indentation))))
  2169. ;; Ignore contents of source blocks or example blocks when
  2170. ;; indentation should be preserved (through
  2171. ;; `org-src-preserve-indentation' or "-i" flag).
  2172. (should-not
  2173. (zerop
  2174. (org-test-with-temp-text
  2175. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2176. (let ((org-adapt-indentation t)
  2177. (org-src-preserve-indentation nil)
  2178. (org-odd-levels-only nil))
  2179. (org-promote))
  2180. (forward-line)
  2181. (org-get-indentation))))
  2182. (should
  2183. (zerop
  2184. (org-test-with-temp-text
  2185. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  2186. (let ((org-adapt-indentation t)
  2187. (org-src-preserve-indentation t)
  2188. (org-odd-levels-only nil))
  2189. (org-promote))
  2190. (forward-line)
  2191. (org-get-indentation))))
  2192. (should
  2193. (zerop
  2194. (org-test-with-temp-text
  2195. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2196. (let ((org-adapt-indentation t)
  2197. (org-src-preserve-indentation t)
  2198. (org-odd-levels-only nil))
  2199. (org-promote))
  2200. (forward-line)
  2201. (org-get-indentation))))
  2202. (should
  2203. (zerop
  2204. (org-test-with-temp-text
  2205. "** H\n #+BEGIN_SRC emacs-lisp -i\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. ;;; Planning
  2213. (ert-deftest test-org/timestamp-has-time-p ()
  2214. "Test `org-timestamp-has-time-p' specifications."
  2215. ;; With time.
  2216. (should
  2217. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  2218. (org-timestamp-has-time-p (org-element-context))))
  2219. ;; Without time.
  2220. (should-not
  2221. (org-test-with-temp-text "<2012-03-29 Thu>"
  2222. (org-timestamp-has-time-p (org-element-context)))))
  2223. (ert-deftest test-org/timestamp-format ()
  2224. "Test `org-timestamp-format' specifications."
  2225. ;; Regular test.
  2226. (should
  2227. (equal
  2228. "2012-03-29 16:40"
  2229. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  2230. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  2231. ;; Range end.
  2232. (should
  2233. (equal
  2234. "2012-03-29"
  2235. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  2236. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  2237. (ert-deftest test-org/timestamp-split-range ()
  2238. "Test `org-timestamp-split-range' specifications."
  2239. ;; Extract range start (active).
  2240. (should
  2241. (equal '(2012 3 29)
  2242. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2243. (let ((ts (org-timestamp-split-range (org-element-context))))
  2244. (mapcar (lambda (p) (org-element-property p ts))
  2245. '(:year-end :month-end :day-end))))))
  2246. ;; Extract range start (inactive)
  2247. (should
  2248. (equal '(2012 3 29)
  2249. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  2250. (let ((ts (org-timestamp-split-range (org-element-context))))
  2251. (mapcar (lambda (p) (org-element-property p ts))
  2252. '(:year-end :month-end :day-end))))))
  2253. ;; Extract range end (active).
  2254. (should
  2255. (equal '(2012 3 30)
  2256. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2257. (let ((ts (org-timestamp-split-range
  2258. (org-element-context) t)))
  2259. (mapcar (lambda (p) (org-element-property p ts))
  2260. '(:year-end :month-end :day-end))))))
  2261. ;; Extract range end (inactive)
  2262. (should
  2263. (equal '(2012 3 30)
  2264. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  2265. (let ((ts (org-timestamp-split-range
  2266. (org-element-context) t)))
  2267. (mapcar (lambda (p) (org-element-property p ts))
  2268. '(:year-end :month-end :day-end))))))
  2269. ;; Return the timestamp if not a range.
  2270. (should
  2271. (org-test-with-temp-text "[2012-03-29 Thu]"
  2272. (let* ((ts-orig (org-element-context))
  2273. (ts-copy (org-timestamp-split-range ts-orig)))
  2274. (eq ts-orig ts-copy))))
  2275. (should
  2276. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  2277. (let* ((ts-orig (org-element-context))
  2278. (ts-copy (org-timestamp-split-range ts-orig)))
  2279. (eq ts-orig ts-copy))))
  2280. ;; Check that parent is the same when a range was split.
  2281. (should
  2282. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  2283. (let* ((ts-orig (org-element-context))
  2284. (ts-copy (org-timestamp-split-range ts-orig)))
  2285. (eq (org-element-property :parent ts-orig)
  2286. (org-element-property :parent ts-copy))))))
  2287. (ert-deftest test-org/timestamp-translate ()
  2288. "Test `org-timestamp-translate' specifications."
  2289. ;; Translate whole date range.
  2290. (should
  2291. (equal "<29>--<30>"
  2292. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2293. (let ((org-display-custom-times t)
  2294. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2295. (org-timestamp-translate (org-element-context))))))
  2296. ;; Translate date range start.
  2297. (should
  2298. (equal "<29>"
  2299. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2300. (let ((org-display-custom-times t)
  2301. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2302. (org-timestamp-translate (org-element-context) 'start)))))
  2303. ;; Translate date range end.
  2304. (should
  2305. (equal "<30>"
  2306. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2307. (let ((org-display-custom-times t)
  2308. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2309. (org-timestamp-translate (org-element-context) 'end)))))
  2310. ;; Translate time range.
  2311. (should
  2312. (equal "<08>--<16>"
  2313. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  2314. (let ((org-display-custom-times t)
  2315. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  2316. (org-timestamp-translate (org-element-context))))))
  2317. ;; Translate non-range timestamp.
  2318. (should
  2319. (equal "<29>"
  2320. (org-test-with-temp-text "<2012-03-29 Thu>"
  2321. (let ((org-display-custom-times t)
  2322. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2323. (org-timestamp-translate (org-element-context))))))
  2324. ;; Do not change `diary' timestamps.
  2325. (should
  2326. (equal "<%%(org-float t 4 2)>"
  2327. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  2328. (let ((org-display-custom-times t)
  2329. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2330. (org-timestamp-translate (org-element-context)))))))
  2331. ;;; Property API
  2332. (ert-deftest test-org/buffer-property-keys ()
  2333. "Test `org-buffer-property-keys' specifications."
  2334. ;; Retrieve properties accross siblings.
  2335. (should
  2336. (equal '("A" "B")
  2337. (org-test-with-temp-text "
  2338. * H1
  2339. :PROPERTIES:
  2340. :A: 1
  2341. :END:
  2342. * H2
  2343. :PROPERTIES:
  2344. :B: 1
  2345. :END:"
  2346. (org-buffer-property-keys))))
  2347. ;; Retrieve properties accross children.
  2348. (should
  2349. (equal '("A" "B")
  2350. (org-test-with-temp-text "
  2351. * H1
  2352. :PROPERTIES:
  2353. :A: 1
  2354. :END:
  2355. ** H2
  2356. :PROPERTIES:
  2357. :B: 1
  2358. :END:"
  2359. (org-buffer-property-keys))))
  2360. ;; Retrieve muliple properties in the same drawer.
  2361. (should
  2362. (equal '("A" "B")
  2363. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2364. (org-buffer-property-keys))))
  2365. ;; Ignore extension symbol in property name.
  2366. (should
  2367. (equal '("A")
  2368. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  2369. (org-buffer-property-keys))))
  2370. ;; With non-nil COLUMNS, extract property names from columns.
  2371. (should
  2372. (equal '("A" "B")
  2373. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  2374. (org-buffer-property-keys nil nil t))))
  2375. (should
  2376. (equal '("A" "B" "COLUMNS")
  2377. (org-test-with-temp-text
  2378. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  2379. (org-buffer-property-keys nil nil t)))))
  2380. (ert-deftest test-org/property-values ()
  2381. "Test `org-property-values' specifications."
  2382. ;; Regular test.
  2383. (should
  2384. (equal '("2" "1")
  2385. (org-test-with-temp-text
  2386. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  2387. (org-property-values "A"))))
  2388. ;; Ignore empty values.
  2389. (should-not
  2390. (org-test-with-temp-text
  2391. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  2392. (org-property-values "A")))
  2393. ;; Take into consideration extended values.
  2394. (should
  2395. (equal '("1 2")
  2396. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  2397. (org-property-values "A")))))
  2398. (ert-deftest test-org/entry-delete ()
  2399. "Test `org-entry-delete' specifications."
  2400. ;; Regular test.
  2401. (should
  2402. (string-match
  2403. " *:PROPERTIES:\n *:B: +2\n *:END:"
  2404. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2405. (org-entry-delete (point) "A")
  2406. (buffer-string))))
  2407. ;; When last property is removed, remove the property drawer.
  2408. (should-not
  2409. (string-match
  2410. ":PROPERTIES:"
  2411. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  2412. (org-entry-delete (point) "A")
  2413. (buffer-string)))))
  2414. (ert-deftest test-org/entry-get ()
  2415. "Test `org-entry-get' specifications."
  2416. ;; Regular test.
  2417. (should
  2418. (equal "1"
  2419. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2420. (org-entry-get (point) "A"))))
  2421. ;; Ignore case.
  2422. (should
  2423. (equal "1"
  2424. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2425. (org-entry-get (point) "a"))))
  2426. ;; Handle extended values, both before and after base value.
  2427. (should
  2428. (equal "1 2 3"
  2429. (org-test-with-temp-text
  2430. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2431. (org-entry-get (point) "A"))))
  2432. ;; Empty values are returned as the empty string.
  2433. (should
  2434. (equal ""
  2435. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  2436. (org-entry-get (point) "A"))))
  2437. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  2438. ;; otherwise, return nil.
  2439. (should-not
  2440. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2441. (org-entry-get (point) "A")))
  2442. (should
  2443. (equal "nil"
  2444. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2445. (org-entry-get (point) "A" nil t))))
  2446. ;; Return nil when no property is found, independently on the
  2447. ;; LITERAL-NIL argument.
  2448. (should-not
  2449. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2450. (org-entry-get (point) "B")))
  2451. (should-not
  2452. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2453. (org-entry-get (point) "B" nil t)))
  2454. ;; Handle inheritance, when allowed.
  2455. (should
  2456. (equal
  2457. "1"
  2458. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2459. (org-entry-get (point) "A" t))))
  2460. (should
  2461. (equal
  2462. "1"
  2463. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2464. (let ((org-use-property-inheritance t))
  2465. (org-entry-get (point) "A" 'selective)))))
  2466. (should-not
  2467. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2468. (let ((org-use-property-inheritance nil))
  2469. (org-entry-get (point) "A" 'selective)))))
  2470. (ert-deftest test-org/entry-properties ()
  2471. "Test `org-entry-properties' specifications."
  2472. ;; Get "ITEM" property.
  2473. (should
  2474. (equal "* H"
  2475. (org-test-with-temp-text "* TODO H"
  2476. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  2477. ;; Get "TODO" property.
  2478. (should
  2479. (equal "TODO"
  2480. (org-test-with-temp-text "* TODO H"
  2481. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  2482. (should-not
  2483. (org-test-with-temp-text "* H"
  2484. (assoc "TODO" (org-entry-properties nil "TODO"))))
  2485. ;; Get "PRIORITY" property.
  2486. (should
  2487. (equal "A"
  2488. (org-test-with-temp-text "* [#A] H"
  2489. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  2490. (should-not
  2491. (org-test-with-temp-text "* H"
  2492. (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
  2493. ;; Get "FILE" property.
  2494. (should
  2495. (org-test-with-temp-text-in-file "* H\nParagraph"
  2496. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  2497. (buffer-file-name))))
  2498. (should-not
  2499. (org-test-with-temp-text "* H\nParagraph"
  2500. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  2501. ;; Get "TAGS" property.
  2502. (should
  2503. (equal ":tag1:tag2:"
  2504. (org-test-with-temp-text "* H :tag1:tag2:"
  2505. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  2506. (should-not
  2507. (org-test-with-temp-text "* H"
  2508. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  2509. ;; Get "ALLTAGS" property.
  2510. (should
  2511. (equal ":tag1:tag2:"
  2512. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  2513. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  2514. (should-not
  2515. (org-test-with-temp-text "* H"
  2516. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  2517. ;; Get "BLOCKED" property.
  2518. (should
  2519. (equal "t"
  2520. (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
  2521. (let ((org-enforce-todo-dependencies t)
  2522. (org-blocker-hook
  2523. '(org-block-todo-from-children-or-siblings-or-parent)))
  2524. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2525. (should
  2526. (equal ""
  2527. (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
  2528. (let ((org-enforce-todo-dependencies t))
  2529. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2530. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  2531. (should
  2532. (equal
  2533. "[2012-03-29 thu.]"
  2534. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  2535. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  2536. (should-not
  2537. (org-test-with-temp-text "* H"
  2538. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  2539. (should
  2540. (equal
  2541. "<2014-03-04 tue.>"
  2542. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2543. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  2544. (should-not
  2545. (org-test-with-temp-text "* H"
  2546. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  2547. (should
  2548. (equal
  2549. "<2014-03-04 tue.>"
  2550. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2551. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  2552. (should-not
  2553. (org-test-with-temp-text "* H"
  2554. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  2555. ;; Get "CATEGORY"
  2556. (should
  2557. (equal "cat"
  2558. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  2559. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2560. (should
  2561. (equal "cat"
  2562. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  2563. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2564. ;; Get standard properties.
  2565. (should
  2566. (equal "1"
  2567. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2568. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2569. ;; Handle extended properties.
  2570. (should
  2571. (equal "1 2 3"
  2572. (org-test-with-temp-text
  2573. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2574. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2575. (should
  2576. (equal "1 2 3"
  2577. (org-test-with-temp-text
  2578. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  2579. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2580. ;; Ignore forbidden (special) properties.
  2581. (should-not
  2582. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  2583. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  2584. (ert-deftest test-org/entry-put ()
  2585. "Test `org-entry-put' specifications."
  2586. ;; Error when not a string or nil.
  2587. (should-error
  2588. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  2589. (org-entry-put 1 "test" 2)))
  2590. ;; Set "TODO" property.
  2591. (should
  2592. (string-match (regexp-quote " TODO H")
  2593. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2594. (org-entry-put (point) "TODO" "TODO")
  2595. (buffer-string))))
  2596. (should
  2597. (string-match (regexp-quote "* H")
  2598. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2599. (org-entry-put (point) "TODO" nil)
  2600. (buffer-string))))
  2601. ;; Set "PRIORITY" property.
  2602. (should
  2603. (equal "* [#A] H"
  2604. (org-test-with-temp-text "* [#B] H"
  2605. (org-entry-put (point) "PRIORITY" "A")
  2606. (buffer-string))))
  2607. (should
  2608. (equal "* H"
  2609. (org-test-with-temp-text "* [#B] H"
  2610. (org-entry-put (point) "PRIORITY" nil)
  2611. (buffer-string))))
  2612. ;; Set "SCHEDULED" property.
  2613. (should
  2614. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  2615. (org-test-with-temp-text "* H"
  2616. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  2617. (buffer-string))))
  2618. (should
  2619. (string= "* H\n"
  2620. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2621. (org-entry-put (point) "SCHEDULED" nil)
  2622. (buffer-string))))
  2623. (should
  2624. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  2625. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2626. (org-entry-put (point) "SCHEDULED" "earlier")
  2627. (buffer-string))))
  2628. (should
  2629. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  2630. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2631. (org-entry-put (point) "SCHEDULED" "later")
  2632. (buffer-string))))
  2633. ;; Set "DEADLINE" property.
  2634. (should
  2635. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  2636. (org-test-with-temp-text "* H"
  2637. (org-entry-put (point) "DEADLINE" "2014-03-04")
  2638. (buffer-string))))
  2639. (should
  2640. (string= "* H\n"
  2641. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2642. (org-entry-put (point) "DEADLINE" nil)
  2643. (buffer-string))))
  2644. (should
  2645. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  2646. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2647. (org-entry-put (point) "DEADLINE" "earlier")
  2648. (buffer-string))))
  2649. (should
  2650. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  2651. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2652. (org-entry-put (point) "DEADLINE" "later")
  2653. (buffer-string))))
  2654. ;; Regular properties, with or without pre-existing drawer.
  2655. (should
  2656. (string-match "^ *:A: +2$"
  2657. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2658. (org-entry-put (point) "A" "2")
  2659. (buffer-string))))
  2660. (should
  2661. (string-match "^ *:A: +1$"
  2662. (org-test-with-temp-text "* H"
  2663. (org-entry-put (point) "A" "1")
  2664. (buffer-string))))
  2665. ;; Special case: two consecutive headlines.
  2666. (should
  2667. (string-match "\\* A\n *:PROPERTIES:"
  2668. (org-test-with-temp-text "* A\n** B"
  2669. (org-entry-put (point) "A" "1")
  2670. (buffer-string)))))
  2671. ;;; Radio Targets
  2672. (ert-deftest test-org/update-radio-target-regexp ()
  2673. "Test `org-update-radio-target-regexp' specifications."
  2674. ;; Properly update cache with no previous radio target regexp.
  2675. (should
  2676. (eq 'link
  2677. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2678. (save-excursion (goto-char (point-max)) (org-element-context))
  2679. (insert "<<<")
  2680. (search-forward "o")
  2681. (insert ">>>")
  2682. (org-update-radio-target-regexp)
  2683. (goto-char (point-max))
  2684. (org-element-type (org-element-context)))))
  2685. ;; Properly update cache with previous radio target regexp.
  2686. (should
  2687. (eq 'link
  2688. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2689. (save-excursion (goto-char (point-max)) (org-element-context))
  2690. (insert "<<<")
  2691. (search-forward "o")
  2692. (insert ">>>")
  2693. (org-update-radio-target-regexp)
  2694. (search-backward "r")
  2695. (delete-char 5)
  2696. (insert "new")
  2697. (org-update-radio-target-regexp)
  2698. (goto-char (point-max))
  2699. (delete-region (line-beginning-position) (point))
  2700. (insert "new")
  2701. (org-element-type (org-element-context))))))
  2702. ;;; Visibility
  2703. (ert-deftest test-org/flag-drawer ()
  2704. "Test `org-flag-drawer' specifications."
  2705. ;; Hide drawer.
  2706. (should
  2707. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2708. (org-flag-drawer t)
  2709. (get-char-property (line-end-position) 'invisible)))
  2710. ;; Show drawer.
  2711. (should-not
  2712. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2713. (org-flag-drawer t)
  2714. (org-flag-drawer nil)
  2715. (get-char-property (line-end-position) 'invisible)))
  2716. ;; Test optional argument.
  2717. (should
  2718. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2719. (let ((drawer (save-excursion (search-forward ":D2")
  2720. (org-element-at-point))))
  2721. (org-flag-drawer t drawer)
  2722. (get-char-property (progn (search-forward ":D2") (line-end-position))
  2723. 'invisible))))
  2724. (should-not
  2725. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2726. (let ((drawer (save-excursion (search-forward ":D2")
  2727. (org-element-at-point))))
  2728. (org-flag-drawer t drawer)
  2729. (get-char-property (line-end-position) 'invisible))))
  2730. ;; Do not hide fake drawers.
  2731. (should-not
  2732. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  2733. (forward-line 1)
  2734. (org-flag-drawer t)
  2735. (get-char-property (line-end-position) 'invisible)))
  2736. ;; Do not hide incomplete drawers.
  2737. (should-not
  2738. (org-test-with-temp-text ":D:\nparagraph"
  2739. (forward-line 1)
  2740. (org-flag-drawer t)
  2741. (get-char-property (line-end-position) 'invisible)))
  2742. ;; Do not hide drawers when called from final blank lines.
  2743. (should-not
  2744. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  2745. (goto-char (point-max))
  2746. (org-flag-drawer t)
  2747. (goto-char (point-min))
  2748. (get-char-property (line-end-position) 'invisible)))
  2749. ;; Don't leave point in an invisible part of the buffer when hiding
  2750. ;; a drawer away.
  2751. (should-not
  2752. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2753. (goto-char (point-max))
  2754. (org-flag-drawer t)
  2755. (get-char-property (point) 'invisible))))
  2756. (ert-deftest test-org/hide-block-toggle ()
  2757. "Test `org-hide-block-toggle' specifications."
  2758. ;; Error when not at a block.
  2759. (should-error
  2760. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  2761. (org-hide-block-toggle 'off)
  2762. (get-char-property (line-end-position) 'invisible)))
  2763. ;; Hide block.
  2764. (should
  2765. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2766. (org-hide-block-toggle)
  2767. (get-char-property (line-end-position) 'invisible)))
  2768. (should
  2769. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  2770. (org-hide-block-toggle)
  2771. (get-char-property (line-end-position) 'invisible)))
  2772. ;; Show block unconditionally when optional argument is `off'.
  2773. (should-not
  2774. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2775. (org-hide-block-toggle)
  2776. (org-hide-block-toggle 'off)
  2777. (get-char-property (line-end-position) 'invisible)))
  2778. (should-not
  2779. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2780. (org-hide-block-toggle 'off)
  2781. (get-char-property (line-end-position) 'invisible)))
  2782. ;; Hide block unconditionally when optional argument is non-nil.
  2783. (should
  2784. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2785. (org-hide-block-toggle t)
  2786. (get-char-property (line-end-position) 'invisible)))
  2787. (should
  2788. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2789. (org-hide-block-toggle)
  2790. (org-hide-block-toggle t)
  2791. (get-char-property (line-end-position) 'invisible)))
  2792. ;; Do not hide block when called from final blank lines.
  2793. (should-not
  2794. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  2795. (org-hide-block-toggle)
  2796. (goto-char (point-min))
  2797. (get-char-property (line-end-position) 'invisible)))
  2798. ;; Don't leave point in an invisible part of the buffer when hiding
  2799. ;; a block away.
  2800. (should-not
  2801. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  2802. (org-hide-block-toggle)
  2803. (get-char-property (point) 'invisible))))
  2804. (ert-deftest test-org/hide-block-toggle-maybe ()
  2805. "Test `org-hide-block-toggle-maybe' specifications."
  2806. (should
  2807. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  2808. (org-hide-block-toggle-maybe)))
  2809. (should-not
  2810. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  2811. (provide 'test-org)
  2812. ;;; test-org.el ends here