test-org.el 123 KB

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