test-org.el 120 KB

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