test-org.el 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920
  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 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. (cl-letf (((symbol-function 'current-time)
  185. (lambda ()
  186. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  187. (org-read-date
  188. t nil "+1y" nil
  189. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  190. (should
  191. (equal
  192. "2013-03-29"
  193. (cl-letf (((symbol-function 'current-time)
  194. (lambda ()
  195. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  196. (org-read-date
  197. t nil "++1y" nil
  198. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  199. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  200. ;; dates (relatively to now) when incomplete. Otherwise, use
  201. ;; default date.
  202. (should
  203. (equal
  204. "2014-04-01"
  205. (cl-letf (((symbol-function 'current-time)
  206. (lambda ()
  207. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  208. (let ((org-read-date-prefer-future t))
  209. (org-read-date t nil "1")))))
  210. (should
  211. (equal
  212. "2013-03-04"
  213. (cl-letf (((symbol-function 'current-time)
  214. (lambda ()
  215. (apply #'encode-time (org-parse-time-string "2012-03-29")))))
  216. (let ((org-read-date-prefer-future t))
  217. (org-read-date t nil "3-4")))))
  218. (should
  219. (equal
  220. "2012-03-04"
  221. (cl-letf (((symbol-function 'current-time)
  222. (lambda ()
  223. (apply #'encode-time (org-parse-time-string "2012-03-29")))))
  224. (let ((org-read-date-prefer-future nil))
  225. (org-read-date t nil "3-4")))))
  226. ;; When set to `org-read-date-prefer-future' is set to `time', read
  227. ;; day is moved to tomorrow if specified hour is before current
  228. ;; time. However, it only happens in no other part of the date is
  229. ;; specified.
  230. (should
  231. (equal
  232. "2012-03-30"
  233. (cl-letf (((symbol-function 'current-time)
  234. (lambda ()
  235. (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
  236. (let ((org-read-date-prefer-future 'time))
  237. (org-read-date t nil "00:40" nil)))))
  238. (should-not
  239. (equal
  240. "2012-03-30"
  241. (cl-letf (((symbol-function 'current-time)
  242. (lambda ()
  243. (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
  244. (let ((org-read-date-prefer-future 'time))
  245. (org-read-date t nil "29 00:40" nil)))))
  246. ;; Caveat: `org-read-date-prefer-future' always refers to current
  247. ;; time, not default time, when they differ.
  248. (should
  249. (equal
  250. "2014-04-01"
  251. (cl-letf (((symbol-function 'current-time)
  252. (lambda ()
  253. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  254. (let ((org-read-date-prefer-future t))
  255. (org-read-date
  256. t nil "1" nil
  257. (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
  258. (should
  259. (equal
  260. "2014-03-25"
  261. (cl-letf (((symbol-function 'current-time)
  262. (lambda ()
  263. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  264. (let ((org-read-date-prefer-future t))
  265. (org-read-date
  266. t nil "25" nil
  267. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
  268. (ert-deftest test-org/org-parse-time-string ()
  269. "Test `org-parse-time-string'."
  270. (should (equal (org-parse-time-string "2012-03-29 16:40")
  271. '(0 40 16 29 3 2012 nil nil nil)))
  272. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  273. '(0 40 16 29 3 2012 nil nil nil)))
  274. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  275. '(0 40 16 29 3 2012 nil nil nil)))
  276. (should (equal (org-parse-time-string "<2012-03-29>")
  277. '(0 0 0 29 3 2012 nil nil nil)))
  278. (should (equal (org-parse-time-string "<2012-03-29>" t)
  279. '(0 nil nil 29 3 2012 nil nil nil))))
  280. (ert-deftest test-org/closest-date ()
  281. "Test `org-closest-date' specifications."
  282. (require 'calendar)
  283. ;; Time stamps without a repeater are returned unchanged.
  284. (should
  285. (equal
  286. '(3 29 2012)
  287. (calendar-gregorian-from-absolute
  288. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  289. ;; Time stamps with a null repeater are returned unchanged.
  290. (should
  291. (equal
  292. '(3 29 2012)
  293. (calendar-gregorian-from-absolute
  294. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  295. ;; if PREFER is set to `past' always return a date before, or equal
  296. ;; to CURRENT.
  297. (should
  298. (equal
  299. '(3 1 2014)
  300. (calendar-gregorian-from-absolute
  301. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  302. (should
  303. (equal
  304. '(3 4 2014)
  305. (calendar-gregorian-from-absolute
  306. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  307. ;; if PREFER is set to `future' always return a date before, or equal
  308. ;; to CURRENT.
  309. (should
  310. (equal
  311. '(3 29 2014)
  312. (calendar-gregorian-from-absolute
  313. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  314. (should
  315. (equal
  316. '(3 4 2014)
  317. (calendar-gregorian-from-absolute
  318. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  319. ;; If PREFER is neither `past' nor `future', select closest date.
  320. (should
  321. (equal
  322. '(3 1 2014)
  323. (calendar-gregorian-from-absolute
  324. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  325. (should
  326. (equal
  327. '(5 4 2014)
  328. (calendar-gregorian-from-absolute
  329. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  330. ;; Test "day" repeater.
  331. (should
  332. (equal '(3 8 2014)
  333. (calendar-gregorian-from-absolute
  334. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  335. (should
  336. (equal '(3 10 2014)
  337. (calendar-gregorian-from-absolute
  338. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  339. ;; Test "month" repeater.
  340. (should
  341. (equal '(1 5 2015)
  342. (calendar-gregorian-from-absolute
  343. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  344. (should
  345. (equal '(3 29 2014)
  346. (calendar-gregorian-from-absolute
  347. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  348. ;; Test "year" repeater.
  349. (should
  350. (equal '(3 5 2014)
  351. (calendar-gregorian-from-absolute
  352. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  353. (should
  354. (equal '(3 29 2014)
  355. (calendar-gregorian-from-absolute
  356. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  357. (ert-deftest test-org/deadline-close-p ()
  358. "Test `org-deadline-close-p' specifications."
  359. ;; Pretend that the current time is 2016-06-03 Fri 01:43
  360. (cl-letf (((symbol-function 'current-time)
  361. (lambda () '(22353 6425 905205 644000))))
  362. ;; Timestamps are close if they are within `ndays' of lead time.
  363. (org-test-with-temp-text "* Heading"
  364. (should (org-deadline-close-p "2016-06-03 Fri" 0))
  365. (should (org-deadline-close-p "2016-06-02 Thu" 0))
  366. (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
  367. (should (org-deadline-close-p "2016-06-04 Sat" 1))
  368. (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
  369. ;; Read `ndays' from timestamp if argument not given.
  370. (org-test-with-temp-text "* H"
  371. (should (org-deadline-close-p "2016-06-04 Sat -1d"))
  372. (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
  373. (should (org-deadline-close-p "2016-06-10 Fri -1w"))
  374. (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
  375. ;; Prefer `ndays' argument over lead time in timestamp.
  376. (org-test-with-temp-text "* H"
  377. (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
  378. (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
  379. ;; Completed tasks are never close.
  380. (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
  381. (org-test-with-temp-text "* TODO Heading"
  382. (should (org-deadline-close-p "2016-06-03")))
  383. (org-test-with-temp-text "* DONE Heading"
  384. (should-not (org-deadline-close-p "2016-06-03"))))))
  385. ;;; Drawers
  386. (ert-deftest test-org/insert-property-drawer ()
  387. "Test `org-insert-property-drawer' specifications."
  388. ;; Error before first headline.
  389. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  390. ;; Insert drawer right after headline if there is no planning line,
  391. ;; or after it otherwise.
  392. (should
  393. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  394. (org-test-with-temp-text "* H\nParagraph<point>"
  395. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  396. (buffer-string))))
  397. (should
  398. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  399. (org-test-with-temp-text
  400. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  401. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  402. (buffer-string))))
  403. ;; Indent inserted drawer.
  404. (should
  405. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  406. (org-test-with-temp-text "* H\nParagraph<point>"
  407. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  408. (buffer-string))))
  409. ;; Handle insertion at eob.
  410. (should
  411. (equal "* H\n:PROPERTIES:\n:END:\n"
  412. (org-test-with-temp-text "* H"
  413. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  414. (buffer-string))))
  415. ;; Skip inlinetasks before point.
  416. (when (featurep 'org-inlinetask)
  417. (should
  418. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  419. (org-test-with-temp-text
  420. "* H\n*************** I\n*************** END\nP<point>"
  421. (let ((org-adapt-indentation nil)
  422. (org-inlinetask-min-level 15))
  423. (org-insert-property-drawer))
  424. (buffer-string)))))
  425. ;; Correctly set drawer in an inlinetask.
  426. (when (featurep 'org-inlinetask)
  427. (should
  428. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  429. (org-test-with-temp-text
  430. "* H\n*************** I\nP<point>\n*************** END"
  431. (let ((org-adapt-indentation nil)
  432. (org-inlinetask-min-level 15))
  433. (org-insert-property-drawer))
  434. (buffer-string))))))
  435. ;;; Filling
  436. (ert-deftest test-org/fill-paragraph ()
  437. "Test `org-fill-paragraph' specifications."
  438. ;; At an Org table, align it.
  439. (should
  440. (equal "| a |\n"
  441. (org-test-with-temp-text "|a|"
  442. (org-fill-paragraph)
  443. (buffer-string))))
  444. (should
  445. (equal "#+name: table\n| a |\n"
  446. (org-test-with-temp-text "#+name: table\n| a |\n"
  447. (org-fill-paragraph)
  448. (buffer-string))))
  449. ;; At a paragraph, preserve line breaks.
  450. (org-test-with-temp-text "some \\\\\nlong\ntext"
  451. (let ((fill-column 20))
  452. (org-fill-paragraph)
  453. (should (equal (buffer-string) "some \\\\\nlong text"))))
  454. ;; Correctly fill a paragraph when point is at its very end.
  455. (should
  456. (equal "A B"
  457. (org-test-with-temp-text "A\nB"
  458. (let ((fill-column 20))
  459. (goto-char (point-max))
  460. (org-fill-paragraph)
  461. (buffer-string)))))
  462. ;; Correctly fill the last paragraph of a greater element.
  463. (should
  464. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  465. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  466. (let ((fill-column 8))
  467. (forward-line)
  468. (end-of-line)
  469. (org-fill-paragraph)
  470. (buffer-string)))))
  471. ;; Correctly fill an element in a narrowed buffer.
  472. (should
  473. (equal "01234\n6"
  474. (org-test-with-temp-text "01234 6789"
  475. (let ((fill-column 5))
  476. (narrow-to-region 1 8)
  477. (org-fill-paragraph)
  478. (buffer-string)))))
  479. ;; Handle `adaptive-fill-regexp' in paragraphs.
  480. (should
  481. (equal "> a b"
  482. (org-test-with-temp-text "> a\n> b"
  483. (let ((fill-column 5)
  484. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  485. (org-fill-paragraph)
  486. (buffer-string)))))
  487. ;; Special case: Fill first paragraph when point is at an item or
  488. ;; a plain-list or a footnote reference.
  489. (should
  490. (equal "- A B"
  491. (org-test-with-temp-text "- A\n B"
  492. (let ((fill-column 20))
  493. (org-fill-paragraph)
  494. (buffer-string)))))
  495. (should
  496. (equal "[fn:1] A B"
  497. (org-test-with-temp-text "[fn:1] A\nB"
  498. (let ((fill-column 20))
  499. (org-fill-paragraph)
  500. (buffer-string)))))
  501. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  502. (let ((fill-column 20))
  503. (org-fill-paragraph)
  504. (should (equal (buffer-string)
  505. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  506. ;; Fill contents of `comment-block' elements.
  507. (should
  508. (equal
  509. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  510. (let ((fill-column 20))
  511. (forward-line)
  512. (org-fill-paragraph)
  513. (buffer-string)))
  514. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  515. ;; Fill `comment' elements.
  516. (should
  517. (equal " # A B"
  518. (org-test-with-temp-text " # A\n # B"
  519. (let ((fill-column 20))
  520. (org-fill-paragraph)
  521. (buffer-string)))))
  522. ;; Do not mix consecutive comments when filling one of them.
  523. (should
  524. (equal "# A B\n\n# C"
  525. (org-test-with-temp-text "# A\n# B\n\n# C"
  526. (let ((fill-column 20))
  527. (org-fill-paragraph)
  528. (buffer-string)))))
  529. ;; Use commented empty lines as separators when filling comments.
  530. (should
  531. (equal "# A B\n#\n# C"
  532. (org-test-with-temp-text "# A\n# B\n#\n# C"
  533. (let ((fill-column 20))
  534. (org-fill-paragraph)
  535. (buffer-string)))))
  536. ;; Handle `adaptive-fill-regexp' in comments.
  537. (should
  538. (equal "# > a b"
  539. (org-test-with-temp-text "# > a\n# > b"
  540. (let ((fill-column 20)
  541. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  542. (org-fill-paragraph)
  543. (buffer-string)))))
  544. ;; Do nothing at affiliated keywords.
  545. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  546. (let ((fill-column 20))
  547. (org-fill-paragraph)
  548. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  549. ;; Do not move point after table when filling a table.
  550. (should-not
  551. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  552. (forward-char)
  553. (org-fill-paragraph)
  554. (eobp))))
  555. (ert-deftest test-org/auto-fill-function ()
  556. "Test auto-filling features."
  557. ;; Auto fill paragraph.
  558. (should
  559. (equal "12345\n7890"
  560. (org-test-with-temp-text "12345 7890"
  561. (let ((fill-column 5))
  562. (end-of-line)
  563. (org-auto-fill-function)
  564. (buffer-string)))))
  565. ;; Auto fill first paragraph in an item.
  566. (should
  567. (equal "- 12345\n 7890"
  568. (org-test-with-temp-text "- 12345 7890"
  569. (let ((fill-column 7))
  570. (end-of-line)
  571. (org-auto-fill-function)
  572. (buffer-string)))))
  573. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  574. (should
  575. (equal "> 12345\n 7890"
  576. (org-test-with-temp-text "> 12345 7890"
  577. (let ((fill-column 10)
  578. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  579. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  580. (end-of-line)
  581. (org-auto-fill-function)
  582. (buffer-string)))))
  583. (should
  584. (equal "> 12345\n> 12345\n> 7890"
  585. (org-test-with-temp-text "> 12345\n> 12345 7890"
  586. (let ((fill-column 10)
  587. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  588. (goto-char (point-max))
  589. (org-auto-fill-function)
  590. (buffer-string)))))
  591. (should-not
  592. (equal " 12345\n *12345\n *12345"
  593. (org-test-with-temp-text " 12345\n *12345 12345"
  594. (let ((fill-column 10)
  595. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  596. (goto-char (point-max))
  597. (org-auto-fill-function)
  598. (buffer-string)))))
  599. ;; Auto fill comments.
  600. (should
  601. (equal " # 12345\n # 7890"
  602. (org-test-with-temp-text " # 12345 7890"
  603. (let ((fill-column 10))
  604. (end-of-line)
  605. (org-auto-fill-function)
  606. (buffer-string)))))
  607. ;; A hash within a line isn't a comment.
  608. (should-not
  609. (equal "12345 # 7890\n# 1"
  610. (org-test-with-temp-text "12345 # 7890 1"
  611. (let ((fill-column 12))
  612. (end-of-line)
  613. (org-auto-fill-function)
  614. (buffer-string)))))
  615. ;; Correctly interpret empty prefix.
  616. (should-not
  617. (equal "# a\n# b\nRegular\n# paragraph"
  618. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  619. (let ((fill-column 12))
  620. (end-of-line 3)
  621. (org-auto-fill-function)
  622. (buffer-string)))))
  623. ;; Comment block: auto fill contents.
  624. (should
  625. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  626. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  627. (let ((fill-column 5))
  628. (forward-line)
  629. (end-of-line)
  630. (org-auto-fill-function)
  631. (buffer-string)))))
  632. (should
  633. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  634. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  635. (let ((fill-column 5))
  636. (forward-line)
  637. (end-of-line)
  638. (org-auto-fill-function)
  639. (buffer-string)))))
  640. ;; Do not fill if a new item could be created.
  641. (should-not
  642. (equal "12345\n- 90"
  643. (org-test-with-temp-text "12345 - 90"
  644. (let ((fill-column 5))
  645. (end-of-line)
  646. (org-auto-fill-function)
  647. (buffer-string)))))
  648. ;; Do not fill if a line break could be introduced.
  649. (should-not
  650. (equal "123\\\\\n7890"
  651. (org-test-with-temp-text "123\\\\ 7890"
  652. (let ((fill-column 6))
  653. (end-of-line)
  654. (org-auto-fill-function)
  655. (buffer-string)))))
  656. ;; Do not fill affiliated keywords.
  657. (should-not
  658. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  659. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  660. (let ((fill-column 20))
  661. (end-of-line)
  662. (org-auto-fill-function)
  663. (buffer-string))))))
  664. ;;; Indentation
  665. (ert-deftest test-org/indent-line ()
  666. "Test `org-indent-line' specifications."
  667. ;; Do not indent diary sexps, footnote definitions or headlines.
  668. (should
  669. (zerop
  670. (org-test-with-temp-text "%%(org-calendar-holiday)"
  671. (org-indent-line)
  672. (org-get-indentation))))
  673. (should
  674. (zerop
  675. (org-test-with-temp-text "[fn:1] fn"
  676. (let ((org-adapt-indentation t)) (org-indent-line))
  677. (org-get-indentation))))
  678. (should
  679. (zerop
  680. (org-test-with-temp-text "* H"
  681. (org-indent-line)
  682. (org-get-indentation))))
  683. ;; Do not indent before first headline.
  684. (should
  685. (zerop
  686. (org-test-with-temp-text ""
  687. (org-indent-line)
  688. (org-get-indentation))))
  689. ;; Indent according to headline level otherwise, unless
  690. ;; `org-adapt-indentation' is nil.
  691. (should
  692. (= 2
  693. (org-test-with-temp-text "* H\nA"
  694. (forward-line)
  695. (let ((org-adapt-indentation t)) (org-indent-line))
  696. (org-get-indentation))))
  697. (should
  698. (= 2
  699. (org-test-with-temp-text "* H\n\nA"
  700. (forward-line)
  701. (let ((org-adapt-indentation t)) (org-indent-line))
  702. (org-get-indentation))))
  703. (should
  704. (zerop
  705. (org-test-with-temp-text "* H\nA"
  706. (forward-line)
  707. (let ((org-adapt-indentation nil)) (org-indent-line))
  708. (org-get-indentation))))
  709. ;; Indenting preserves point position.
  710. (should
  711. (org-test-with-temp-text "* H\nAB"
  712. (forward-line)
  713. (forward-char)
  714. (let ((org-adapt-indentation t)) (org-indent-line))
  715. (looking-at "B")))
  716. ;; Do not change indentation at an item.
  717. (should
  718. (= 1
  719. (org-test-with-temp-text "* H\n - A"
  720. (forward-line)
  721. (let ((org-adapt-indentation t)) (org-indent-line))
  722. (org-get-indentation))))
  723. ;; On blank lines at the end of a list, indent like last element
  724. ;; within it if the line is still in the list. If the last element
  725. ;; is an item, indent like its contents. Otherwise, indent like the
  726. ;; whole list.
  727. (should
  728. (= 4
  729. (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
  730. (let ((org-adapt-indentation t)) (org-indent-line))
  731. (org-get-indentation))))
  732. (should
  733. (= 4
  734. (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
  735. (let ((org-adapt-indentation t)) (org-indent-line))
  736. (org-get-indentation))))
  737. (should
  738. (zerop
  739. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
  740. (let ((org-adapt-indentation t)) (org-indent-line))
  741. (org-get-indentation))))
  742. (should
  743. (= 4
  744. (org-test-with-temp-text "* H\n- A\n - \n<point>"
  745. (let ((org-adapt-indentation t)) (org-indent-line))
  746. (org-get-indentation))))
  747. (should
  748. (= 4
  749. (org-test-with-temp-text
  750. "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
  751. (let ((org-adapt-indentation t)) (org-indent-line))
  752. (org-get-indentation))))
  753. (should
  754. (= 2
  755. (org-test-with-temp-text "- A\n B\n\n<point>"
  756. (let ((org-adapt-indentation nil)) (org-indent-line))
  757. (org-get-indentation))))
  758. ;; Likewise, on a blank line at the end of a footnote definition,
  759. ;; indent at column 0 if line belongs to the definition. Otherwise,
  760. ;; indent like the definition itself.
  761. (should
  762. (zerop
  763. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  764. (goto-char (point-max))
  765. (let ((org-adapt-indentation t)) (org-indent-line))
  766. (org-get-indentation))))
  767. (should
  768. (zerop
  769. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  770. (goto-char (point-max))
  771. (let ((org-adapt-indentation t)) (org-indent-line))
  772. (org-get-indentation))))
  773. ;; After the end of the contents of a greater element, indent like
  774. ;; the beginning of the element.
  775. (should
  776. (= 1
  777. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  778. (forward-line 2)
  779. (org-indent-line)
  780. (org-get-indentation))))
  781. ;; On blank lines after a paragraph, indent like its last non-empty
  782. ;; line.
  783. (should
  784. (= 1
  785. (org-test-with-temp-text " Paragraph\n\n<point>"
  786. (org-indent-line)
  787. (org-get-indentation))))
  788. ;; At the first line of an element, indent like previous element's
  789. ;; first line, ignoring footnotes definitions and inline tasks, or
  790. ;; according to parent.
  791. (should
  792. (= 2
  793. (org-test-with-temp-text "A\n\n B\n\nC"
  794. (goto-char (point-max))
  795. (org-indent-line)
  796. (org-get-indentation))))
  797. (should
  798. (= 1
  799. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  800. (goto-char (point-max))
  801. (org-indent-line)
  802. (org-get-indentation))))
  803. (should
  804. (= 1
  805. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  806. (forward-line 1)
  807. (org-indent-line)
  808. (org-get-indentation))))
  809. ;; Within code part of a source block, use language major mode if
  810. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  811. ;; according to line above.
  812. (should
  813. (= 6
  814. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  815. (forward-line 2)
  816. (let ((org-src-tab-acts-natively t)
  817. (org-edit-src-content-indentation 0))
  818. (org-indent-line))
  819. (org-get-indentation))))
  820. (should
  821. (= 1
  822. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  823. (forward-line 2)
  824. (let ((org-src-tab-acts-natively nil)
  825. (org-edit-src-content-indentation 0))
  826. (org-indent-line))
  827. (org-get-indentation))))
  828. ;; Otherwise, indent like the first non-blank line above.
  829. (should
  830. (zerop
  831. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  832. (forward-line 3)
  833. (org-indent-line)
  834. (org-get-indentation))))
  835. ;; Align node properties according to `org-property-format'. Handle
  836. ;; nicely empty values.
  837. (should
  838. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  839. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  840. (let ((org-property-format "%-10s %s")) (org-indent-line))
  841. (buffer-string))))
  842. (should
  843. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  844. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  845. (let ((org-property-format "%-10s %s")) (org-indent-line))
  846. (buffer-string)))))
  847. (ert-deftest test-org/indent-region ()
  848. "Test `org-indent-region' specifications."
  849. ;; Indent paragraph.
  850. (should
  851. (equal "A\nB\nC"
  852. (org-test-with-temp-text " A\nB\n C"
  853. (org-indent-region (point-min) (point-max))
  854. (buffer-string))))
  855. ;; Indent greater elements along with their contents.
  856. (should
  857. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  858. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  859. (org-indent-region (point-min) (point-max))
  860. (buffer-string))))
  861. ;; Ignore contents of verse blocks and example blocks.
  862. (should
  863. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  864. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  865. (org-indent-region (point-min) (point-max))
  866. (buffer-string))))
  867. (should
  868. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  869. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  870. (org-indent-region (point-min) (point-max))
  871. (buffer-string))))
  872. ;; Indent according to mode if `org-src-tab-acts-natively' is
  873. ;; non-nil. Otherwise, do not indent code at all.
  874. (should
  875. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  876. (org-test-with-temp-text
  877. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  878. (let ((org-src-tab-acts-natively t)
  879. (org-edit-src-content-indentation 0))
  880. (org-indent-region (point-min) (point-max)))
  881. (buffer-string))))
  882. (should
  883. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  884. (org-test-with-temp-text
  885. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  886. (let ((org-src-tab-acts-natively nil)
  887. (org-edit-src-content-indentation 0))
  888. (org-indent-region (point-min) (point-max)))
  889. (buffer-string))))
  890. ;; Align node properties according to `org-property-format'. Handle
  891. ;; nicely empty values.
  892. (should
  893. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  894. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  895. (let ((org-property-format "%-10s %s")
  896. (org-adapt-indentation nil))
  897. (org-indent-region (point) (point-max)))
  898. (buffer-string))))
  899. (should
  900. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  901. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  902. (let ((org-property-format "%-10s %s")
  903. (org-adapt-indentation nil))
  904. (org-indent-region (point) (point-max)))
  905. (buffer-string))))
  906. ;; Indent plain lists.
  907. (should
  908. (equal "- A\n B\n - C\n\n D"
  909. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  910. (org-indent-region (point-min) (point-max))
  911. (buffer-string))))
  912. (should
  913. (equal "- A\n\n- B"
  914. (org-test-with-temp-text " - A\n\n - B"
  915. (org-indent-region (point-min) (point-max))
  916. (buffer-string))))
  917. ;; Indent footnote definitions.
  918. (should
  919. (equal "[fn:1] Definition\n\nDefinition"
  920. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  921. (org-indent-region (point-min) (point-max))
  922. (buffer-string))))
  923. ;; Special case: Start indenting on a blank line.
  924. (should
  925. (equal "\nParagraph"
  926. (org-test-with-temp-text "\n Paragraph"
  927. (org-indent-region (point-min) (point-max))
  928. (buffer-string)))))
  929. ;;; Editing
  930. (ert-deftest test-org/delete-indentation ()
  931. "Test `org-delete-indentation' specifications."
  932. ;; Regular test.
  933. (should (equal "foo bar"
  934. (org-test-with-temp-text
  935. "foo \n bar<point>"
  936. (org-delete-indentation)
  937. (buffer-string))))
  938. ;; With optional argument.
  939. (should (equal "foo bar"
  940. (org-test-with-temp-text
  941. "foo<point> \n bar"
  942. (org-delete-indentation t)
  943. (buffer-string))))
  944. ;; At headline text should be appended to the headline text.
  945. (should
  946. (equal"* foo bar :tag:"
  947. (let (org-auto-align-tags)
  948. (org-test-with-temp-text
  949. "* foo :tag:\n bar<point>"
  950. (org-delete-indentation)
  951. (buffer-string)))))
  952. (should
  953. (equal "* foo bar :tag:"
  954. (let (org-auto-align-tags)
  955. (org-test-with-temp-text
  956. "* foo <point>:tag:\n bar"
  957. (org-delete-indentation t)
  958. (buffer-string))))))
  959. (ert-deftest test-org/return ()
  960. "Test `org-return' specifications."
  961. ;; Regular test.
  962. (should
  963. (equal "Para\ngraph"
  964. (org-test-with-temp-text "Para<point>graph"
  965. (org-return)
  966. (buffer-string))))
  967. ;; With optional argument, indent line.
  968. (should
  969. (equal " Para\n graph"
  970. (org-test-with-temp-text " Para<point>graph"
  971. (org-return t)
  972. (buffer-string))))
  973. ;; On a table, call `org-table-next-row'.
  974. (should
  975. (org-test-with-temp-text "| <point>a |\n| b |"
  976. (org-return)
  977. (looking-at-p "b")))
  978. ;; Open link or timestamp under point when `org-return-follows-link'
  979. ;; is non-nil.
  980. (should
  981. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  982. (let ((org-return-follows-link t)
  983. (org-link-search-must-match-exact-headline nil))
  984. (org-return))
  985. (looking-at-p "<<target>>")))
  986. (should-not
  987. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  988. (let ((org-return-follows-link nil)) (org-return))
  989. (looking-at-p "<<target>>")))
  990. (should
  991. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  992. (let ((org-return-follows-link t)) (org-return))
  993. (looking-at-p "* b")))
  994. (should
  995. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  996. (let ((org-return-follows-link t)
  997. (org-link-search-must-match-exact-headline nil))
  998. (org-return))
  999. (looking-at-p "<<target>>")))
  1000. (should-not
  1001. (org-test-with-temp-text "Link [[target]]<point> <<target>>"
  1002. (let ((org-return-follows-link t)
  1003. (org-link-search-must-match-exact-headline nil))
  1004. (org-return))
  1005. (looking-at-p "<<target>>")))
  1006. ;; When `org-return-follows-link' is non-nil, tolerate links and
  1007. ;; timestamps in comments, node properties, etc.
  1008. (should
  1009. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1010. (let ((org-return-follows-link t)
  1011. (org-link-search-must-match-exact-headline nil))
  1012. (org-return))
  1013. (looking-at-p "<<target>>")))
  1014. (should-not
  1015. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1016. (let ((org-return-follows-link nil)) (org-return))
  1017. (looking-at-p "<<target>>")))
  1018. (should-not
  1019. (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
  1020. (let ((org-return-follows-link t)
  1021. (org-link-search-must-match-exact-headline nil))
  1022. (org-return))
  1023. (looking-at-p "<<target>>")))
  1024. ;; However, do not open link when point is in a table.
  1025. (should
  1026. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1027. (let ((org-return-follows-link t)) (org-return))
  1028. (looking-at-p "between")))
  1029. ;; Special case: in a list, when indenting, do not break structure.
  1030. (should
  1031. (equal "- A\n B"
  1032. (org-test-with-temp-text "- A <point>B"
  1033. (org-return t)
  1034. (buffer-string))))
  1035. (should
  1036. (equal "- A\n\n- B"
  1037. (org-test-with-temp-text "- A\n<point>- B"
  1038. (org-return t)
  1039. (buffer-string))))
  1040. ;; On tags part of a headline, add a newline below it instead of
  1041. ;; breaking it.
  1042. (should
  1043. (equal "* H :tag:\n"
  1044. (org-test-with-temp-text "* H :<point>tag:"
  1045. (org-return)
  1046. (buffer-string))))
  1047. ;; Before headline text, add a newline below it instead of breaking
  1048. ;; it.
  1049. (should
  1050. (equal "* TODO H :tag:\n"
  1051. (org-test-with-temp-text "* <point>TODO H :tag:"
  1052. (org-return)
  1053. (buffer-string))))
  1054. (should
  1055. (equal "* TODO [#B] H :tag:\n"
  1056. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1057. (org-return)
  1058. (buffer-string))))
  1059. ;; At headline text, break headline text but preserve tags.
  1060. (should
  1061. (equal "* TODO [#B] foo :tag:\nbar"
  1062. (let (org-auto-align-tags)
  1063. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1064. (org-return)
  1065. (buffer-string)))))
  1066. ;; At bol of headline insert newline.
  1067. (should
  1068. (equal "\n* h"
  1069. (org-test-with-temp-text "<point>* h"
  1070. (org-return)
  1071. (buffer-string))))
  1072. ;; Refuse to leave invalid headline in buffer.
  1073. (should
  1074. (equal "* h\n"
  1075. (org-test-with-temp-text "*<point> h"
  1076. (org-return)
  1077. (buffer-string)))))
  1078. (ert-deftest test-org/meta-return ()
  1079. "Test M-RET (`org-meta-return') specifications."
  1080. ;; In a table field insert a row above.
  1081. (should
  1082. (org-test-with-temp-text "| a |"
  1083. (forward-char)
  1084. (org-meta-return)
  1085. (forward-line -1)
  1086. (looking-at "| |$")))
  1087. ;; In a paragraph change current line into a header.
  1088. (should
  1089. (org-test-with-temp-text "a"
  1090. (org-meta-return)
  1091. (beginning-of-line)
  1092. (looking-at "\* a$")))
  1093. ;; In an item insert an item, in this case above.
  1094. (should
  1095. (org-test-with-temp-text "- a"
  1096. (org-meta-return)
  1097. (beginning-of-line)
  1098. (looking-at "- $")))
  1099. ;; In a drawer and item insert an item, in this case above.
  1100. (should
  1101. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1102. (forward-line)
  1103. (org-meta-return)
  1104. (beginning-of-line)
  1105. (looking-at "- $"))))
  1106. (ert-deftest test-org/insert-heading ()
  1107. "Test `org-insert-heading' specifications."
  1108. ;; In an empty buffer, insert a new headline.
  1109. (should
  1110. (equal "* "
  1111. (org-test-with-temp-text ""
  1112. (org-insert-heading)
  1113. (buffer-string))))
  1114. ;; At the beginning of a line, turn it into a headline
  1115. (should
  1116. (equal "* P"
  1117. (org-test-with-temp-text "<point>P"
  1118. (org-insert-heading)
  1119. (buffer-string))))
  1120. ;; In the middle of a line, split the line if allowed, otherwise,
  1121. ;; insert the headline at its end.
  1122. (should
  1123. (equal "Para\n* graph"
  1124. (org-test-with-temp-text "Para<point>graph"
  1125. (let ((org-M-RET-may-split-line '((default . t))))
  1126. (org-insert-heading))
  1127. (buffer-string))))
  1128. (should
  1129. (equal "Paragraph\n* "
  1130. (org-test-with-temp-text "Para<point>graph"
  1131. (let ((org-M-RET-may-split-line '((default . nil))))
  1132. (org-insert-heading))
  1133. (buffer-string))))
  1134. ;; When on a list, insert an item instead, unless called with an
  1135. ;; universal argument or if list is invisible. In this case, create
  1136. ;; a new headline after contents.
  1137. (should
  1138. (equal "* H\n- item\n- "
  1139. (org-test-with-temp-text "* H\n- item<point>"
  1140. (let ((org-insert-heading-respect-content nil))
  1141. (org-insert-heading))
  1142. (buffer-string))))
  1143. (should
  1144. (equal "* H\n- item\n- item 2\n* "
  1145. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  1146. (let ((org-insert-heading-respect-content nil))
  1147. (org-insert-heading '(4)))
  1148. (buffer-string))))
  1149. (should
  1150. (equal "* H\n- item\n* "
  1151. (org-test-with-temp-text "* H\n- item"
  1152. (org-cycle)
  1153. (goto-char (point-max))
  1154. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1155. (buffer-string))))
  1156. ;; Preserve list visibility when inserting an item.
  1157. (should
  1158. (equal
  1159. '(outline outline)
  1160. (org-test-with-temp-text "- A\n - B\n- C\n - D"
  1161. (let ((org-cycle-include-plain-lists t))
  1162. (org-cycle)
  1163. (forward-line 2)
  1164. (org-cycle)
  1165. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1166. (list (get-char-property (line-beginning-position 0) 'invisible)
  1167. (get-char-property (line-end-position 2) 'invisible))))))
  1168. ;; When called with two universal arguments, insert a new headline
  1169. ;; at the end of the grandparent subtree.
  1170. (should
  1171. (equal "* H1\n** H3\n- item\n** H2\n** "
  1172. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1173. (let ((org-insert-heading-respect-content nil))
  1174. (org-insert-heading '(16)))
  1175. (buffer-string))))
  1176. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1177. ;; a level 1 heading.
  1178. (should
  1179. (equal "* H1\n** H2\n* "
  1180. (org-test-with-temp-text "* H1\n** H2<point>"
  1181. (org-insert-heading nil nil t)
  1182. (buffer-string))))
  1183. (should
  1184. (equal "* H1\n- item\n* "
  1185. (org-test-with-temp-text "* H1\n- item<point>"
  1186. (org-insert-heading nil nil t)
  1187. (buffer-string))))
  1188. ;; Corner case: correctly insert a headline after an empty one.
  1189. (should
  1190. (equal "* \n* "
  1191. (org-test-with-temp-text "* <point>"
  1192. (org-insert-heading)
  1193. (buffer-string)))))
  1194. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1195. "Test `org-insert-todo-heading-respect-content' specifications."
  1196. ;; Create a TODO heading.
  1197. (should
  1198. (org-test-with-temp-text "* H1\n Body"
  1199. (org-insert-todo-heading-respect-content)
  1200. (nth 2 (org-heading-components))))
  1201. ;; Add headline at the end of the first subtree
  1202. (should
  1203. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  1204. (search-forward "H1Body")
  1205. (org-insert-todo-heading-respect-content)
  1206. (and (eobp) (org-at-heading-p))))
  1207. ;; In a list, do not create a new item.
  1208. (should
  1209. (org-test-with-temp-text "* H\n- an item\n- another one"
  1210. (search-forward "an ")
  1211. (org-insert-todo-heading-respect-content)
  1212. (and (eobp) (org-at-heading-p)))))
  1213. (ert-deftest test-org/clone-with-time-shift ()
  1214. "Test `org-clone-subtree-with-time-shift'."
  1215. ;; Clone non-repeating once.
  1216. (should
  1217. (equal "\
  1218. * H1\n<2015-06-21>
  1219. * H1\n<2015-06-23>
  1220. "
  1221. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1222. (org-clone-subtree-with-time-shift 1 "+2d")
  1223. (replace-regexp-in-string
  1224. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1225. nil nil 1))))
  1226. ;; Clone repeating once.
  1227. (should
  1228. (equal "\
  1229. * H1\n<2015-06-21>
  1230. * H1\n<2015-06-23>
  1231. * H1\n<2015-06-25 +1w>
  1232. "
  1233. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1234. (org-clone-subtree-with-time-shift 1 "+2d")
  1235. (replace-regexp-in-string
  1236. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1237. nil nil 1))))
  1238. ;; Clone non-repeating zero times.
  1239. (should
  1240. (equal "\
  1241. * H1\n<2015-06-21>
  1242. "
  1243. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1244. (org-clone-subtree-with-time-shift 0 "+2d")
  1245. (replace-regexp-in-string
  1246. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1247. nil nil 1))))
  1248. ;; Clone repeating "zero" times.
  1249. (should
  1250. (equal "\
  1251. * H1\n<2015-06-21>
  1252. * H1\n<2015-06-23 +1w>
  1253. "
  1254. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1255. (org-clone-subtree-with-time-shift 0 "+2d")
  1256. (replace-regexp-in-string
  1257. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1258. nil nil 1)))))
  1259. ;;; Fixed-Width Areas
  1260. (ert-deftest test-org/toggle-fixed-width ()
  1261. "Test `org-toggle-fixed-width' specifications."
  1262. ;; No region: Toggle on fixed-width marker in paragraphs.
  1263. (should
  1264. (equal ": A"
  1265. (org-test-with-temp-text "A"
  1266. (org-toggle-fixed-width)
  1267. (buffer-string))))
  1268. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1269. (should
  1270. (equal "A"
  1271. (org-test-with-temp-text ": A"
  1272. (org-toggle-fixed-width)
  1273. (buffer-string))))
  1274. ;; No region: Toggle on marker in blank lines after elements or just
  1275. ;; after a headline.
  1276. (should
  1277. (equal "* H\n: "
  1278. (org-test-with-temp-text "* H\n"
  1279. (forward-line)
  1280. (org-toggle-fixed-width)
  1281. (buffer-string))))
  1282. (should
  1283. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1284. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1285. (goto-char (point-max))
  1286. (org-toggle-fixed-width)
  1287. (buffer-string))))
  1288. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1289. ;; headlines, clocks)
  1290. (should
  1291. (equal ": * Headline"
  1292. (org-test-with-temp-text "* Headline"
  1293. (org-toggle-fixed-width)
  1294. (buffer-string))))
  1295. (should
  1296. (equal ": #+KEYWORD: value"
  1297. (org-test-with-temp-text "#+KEYWORD: value"
  1298. (org-toggle-fixed-width)
  1299. (buffer-string))))
  1300. ;; No region: error in other situations.
  1301. (should-error
  1302. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1303. (forward-line)
  1304. (org-toggle-fixed-width)
  1305. (buffer-string)))
  1306. ;; No region: Indentation is preserved.
  1307. (should
  1308. (equal "- A\n : B"
  1309. (org-test-with-temp-text "- A\n B"
  1310. (forward-line)
  1311. (org-toggle-fixed-width)
  1312. (buffer-string))))
  1313. ;; Region: If it contains only fixed-width elements and blank lines,
  1314. ;; toggle off fixed-width markup.
  1315. (should
  1316. (equal "A\n\nB"
  1317. (org-test-with-temp-text ": A\n\n: B"
  1318. (transient-mark-mode 1)
  1319. (push-mark (point) t t)
  1320. (goto-char (point-max))
  1321. (org-toggle-fixed-width)
  1322. (buffer-string))))
  1323. ;; Region: If it contains anything else, toggle on fixed-width but
  1324. ;; not on fixed-width areas.
  1325. (should
  1326. (equal ": A\n: \n: B\n: \n: C"
  1327. (org-test-with-temp-text "A\n\n: B\n\nC"
  1328. (transient-mark-mode 1)
  1329. (push-mark (point) t t)
  1330. (goto-char (point-max))
  1331. (org-toggle-fixed-width)
  1332. (buffer-string))))
  1333. ;; Region: Ignore blank lines at its end, unless it contains only
  1334. ;; such lines.
  1335. (should
  1336. (equal ": A\n\n"
  1337. (org-test-with-temp-text "A\n\n"
  1338. (transient-mark-mode 1)
  1339. (push-mark (point) t t)
  1340. (goto-char (point-max))
  1341. (org-toggle-fixed-width)
  1342. (buffer-string))))
  1343. (should
  1344. (equal ": \n: \n"
  1345. (org-test-with-temp-text "\n\n"
  1346. (transient-mark-mode 1)
  1347. (push-mark (point) t t)
  1348. (goto-char (point-max))
  1349. (org-toggle-fixed-width)
  1350. (buffer-string)))))
  1351. ;;; Headline
  1352. (ert-deftest test-org/get-heading ()
  1353. "Test `org-get-heading' specifications."
  1354. ;; Return current heading, even if point is not on it.
  1355. (should
  1356. (equal "H"
  1357. (org-test-with-temp-text "* H"
  1358. (org-get-heading))))
  1359. (should
  1360. (equal "H"
  1361. (org-test-with-temp-text "* H\nText<point>"
  1362. (org-get-heading))))
  1363. ;; Without any optional argument, return TODO keywords and tags.
  1364. (should
  1365. (equal "TODO H"
  1366. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1367. (org-get-heading))))
  1368. (should
  1369. (equal "H :tag:"
  1370. (org-test-with-temp-text "* H :tag:"
  1371. (org-get-heading))))
  1372. ;; With NO-TAGS argument, ignore tags.
  1373. (should
  1374. (equal "TODO H"
  1375. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1376. (org-get-heading t))))
  1377. (should
  1378. (equal "H"
  1379. (org-test-with-temp-text "* H :tag:"
  1380. (org-get-heading t))))
  1381. ;; With NO-TODO, ignore TODO keyword.
  1382. (should
  1383. (equal "H"
  1384. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1385. (org-get-heading nil t))))
  1386. (should
  1387. (equal "H :tag:"
  1388. (org-test-with-temp-text "* H :tag:"
  1389. (org-get-heading nil t))))
  1390. ;; TODO keywords are case-sensitive.
  1391. (should
  1392. (equal "Todo H"
  1393. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1394. (org-get-heading nil t))))
  1395. ;; On an empty headline, return value is consistent.
  1396. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1397. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1398. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1399. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t t)))))
  1400. (ert-deftest test-org/in-commented-heading-p ()
  1401. "Test `org-in-commented-heading-p' specifications."
  1402. ;; Commented headline.
  1403. (should
  1404. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1405. (goto-char (point-max))
  1406. (org-in-commented-heading-p)))
  1407. ;; Commented ancestor.
  1408. (should
  1409. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1410. (goto-char (point-max))
  1411. (org-in-commented-heading-p)))
  1412. ;; Comment keyword is case-sensitive.
  1413. (should-not
  1414. (org-test-with-temp-text "* Comment Headline\nBody"
  1415. (goto-char (point-max))
  1416. (org-in-commented-heading-p)))
  1417. ;; Keyword is standalone.
  1418. (should-not
  1419. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1420. (goto-char (point-max))
  1421. (org-in-commented-heading-p)))
  1422. ;; Optional argument.
  1423. (should-not
  1424. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1425. (goto-char (point-max))
  1426. (org-in-commented-heading-p t))))
  1427. (ert-deftest test-org/entry-blocked-p ()
  1428. ;; Check other dependencies.
  1429. (should
  1430. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1431. (let ((org-enforce-todo-dependencies t)
  1432. (org-blocker-hook
  1433. '(org-block-todo-from-children-or-siblings-or-parent)))
  1434. (org-entry-blocked-p))))
  1435. (should-not
  1436. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1437. (let ((org-enforce-todo-dependencies t)
  1438. (org-blocker-hook
  1439. '(org-block-todo-from-children-or-siblings-or-parent)))
  1440. (org-entry-blocked-p))))
  1441. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1442. ;; blocked.
  1443. (should-not
  1444. (org-test-with-temp-text "* Blocked\n** TODO one"
  1445. (let ((org-enforce-todo-dependencies t)
  1446. (org-blocker-hook
  1447. '(org-block-todo-from-children-or-siblings-or-parent)))
  1448. (org-entry-blocked-p))))
  1449. (should-not
  1450. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1451. (let ((org-enforce-todo-dependencies t)
  1452. (org-blocker-hook
  1453. '(org-block-todo-from-children-or-siblings-or-parent)))
  1454. (org-entry-blocked-p))))
  1455. ;; Follow :ORDERED: specifications.
  1456. (should
  1457. (org-test-with-temp-text
  1458. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1459. (let ((org-enforce-todo-dependencies t)
  1460. (org-blocker-hook
  1461. '(org-block-todo-from-children-or-siblings-or-parent)))
  1462. (org-entry-blocked-p))))
  1463. (should-not
  1464. (org-test-with-temp-text
  1465. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1466. (let ((org-enforce-todo-dependencies t)
  1467. (org-blocker-hook
  1468. '(org-block-todo-from-children-or-siblings-or-parent)))
  1469. (org-entry-blocked-p)))))
  1470. (ert-deftest test-org/get-outline-path ()
  1471. "Test `org-get-outline-path' specifications."
  1472. ;; Top-level headlines have no outline path.
  1473. (should-not
  1474. (org-test-with-temp-text "* H"
  1475. (org-get-outline-path)))
  1476. ;; Otherwise, outline path is the path leading to the headline.
  1477. (should
  1478. (equal '("H")
  1479. (org-test-with-temp-text "* H\n** S<point>"
  1480. (org-get-outline-path))))
  1481. ;; Find path even when point is not on a headline.
  1482. (should
  1483. (equal '("H")
  1484. (org-test-with-temp-text "* H\n** S\nText<point>"
  1485. (org-get-outline-path))))
  1486. ;; TODO keywords, tags and statistics cookies are ignored.
  1487. (should
  1488. (equal '("H")
  1489. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1490. (org-get-outline-path))))
  1491. ;; Links are replaced with their description or their path.
  1492. (should
  1493. (equal '("Org")
  1494. (org-test-with-temp-text
  1495. "* [[http://orgmode.org][Org]]\n** S<point>"
  1496. (org-get-outline-path))))
  1497. (should
  1498. (equal '("http://orgmode.org")
  1499. (org-test-with-temp-text
  1500. "* [[http://orgmode.org]]\n** S<point>"
  1501. (org-get-outline-path))))
  1502. ;; When WITH-SELF is non-nil, include current heading.
  1503. (should
  1504. (equal '("H")
  1505. (org-test-with-temp-text "* H"
  1506. (org-get-outline-path t))))
  1507. (should
  1508. (equal '("H" "S")
  1509. (org-test-with-temp-text "* H\n** S\nText<point>"
  1510. (org-get-outline-path t))))
  1511. ;; Using cache is transparent to the user.
  1512. (should
  1513. (equal '("H")
  1514. (org-test-with-temp-text "* H\n** S<point>"
  1515. (setq org-outline-path-cache nil)
  1516. (org-get-outline-path nil t))))
  1517. ;; Do not corrupt cache when finding outline path in distant part of
  1518. ;; the buffer.
  1519. (should
  1520. (equal '("H2")
  1521. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1522. (setq org-outline-path-cache nil)
  1523. (org-get-outline-path nil t)
  1524. (search-forward "S2")
  1525. (org-get-outline-path nil t))))
  1526. ;; Do not choke on empty headlines.
  1527. (should
  1528. (org-test-with-temp-text "* H\n** <point>"
  1529. (org-get-outline-path)))
  1530. (should
  1531. (org-test-with-temp-text "* \n** H<point>"
  1532. (org-get-outline-path))))
  1533. (ert-deftest test-org/format-outline-path ()
  1534. "Test `org-format-outline-path' specifications."
  1535. (should
  1536. (string= (org-format-outline-path (list "one" "two" "three"))
  1537. "one/two/three"))
  1538. ;; Empty path.
  1539. (should
  1540. (string= (org-format-outline-path '())
  1541. ""))
  1542. (should
  1543. (string= (org-format-outline-path '(nil))
  1544. ""))
  1545. ;; Empty path and prefix.
  1546. (should
  1547. (string= (org-format-outline-path '() nil ">>")
  1548. ">>"))
  1549. ;; Trailing whitespace in headings.
  1550. (should
  1551. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1552. "one/tw o/three"))
  1553. ;; Non-default prefix and separators.
  1554. (should
  1555. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1556. ">>|one|two|three"))
  1557. ;; Truncate.
  1558. (should
  1559. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1560. "one/two/.."))
  1561. ;; Give a very narrow width.
  1562. (should
  1563. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1564. "on"))
  1565. ;; Give a prefix that extends beyond the width.
  1566. (should
  1567. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1568. ">>>>>>>>>>")
  1569. ">>>>>>>>..")))
  1570. (ert-deftest test-org/map-entries ()
  1571. "Test `org-map-entries' specifications."
  1572. ;; Full match.
  1573. (should
  1574. (equal '(1 11)
  1575. (org-test-with-temp-text "* Level 1\n** Level 2"
  1576. (org-map-entries #'point))))
  1577. ;; Level match.
  1578. (should
  1579. (equal '(1)
  1580. (org-test-with-temp-text "* Level 1\n** Level 2"
  1581. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1582. (should
  1583. (equal '(11)
  1584. (org-test-with-temp-text "* Level 1\n** Level 2"
  1585. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1586. ;; Tag match.
  1587. (should
  1588. (equal '(11)
  1589. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1590. (org-map-entries #'point "yes"))))
  1591. (should
  1592. (equal '(14)
  1593. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1594. (org-map-entries #'point "+yes-a"))))
  1595. (should
  1596. (equal '(11 23)
  1597. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1598. (org-map-entries #'point "{yes?}"))))
  1599. ;; Priority match.
  1600. (should
  1601. (equal '(1)
  1602. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1603. (org-map-entries #'point "PRIORITY=\"A\""))))
  1604. ;; Date match.
  1605. (should
  1606. (equal '(36)
  1607. (org-test-with-temp-text "
  1608. * H1
  1609. SCHEDULED: <2012-03-29 thu.>
  1610. * H2
  1611. SCHEDULED: <2014-03-04 tue.>"
  1612. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1613. (should
  1614. (equal '(2)
  1615. (org-test-with-temp-text "
  1616. * H1
  1617. SCHEDULED: <2012-03-29 thu.>
  1618. * H2
  1619. SCHEDULED: <2014-03-04 tue.>"
  1620. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1621. ;; Regular property match.
  1622. (should
  1623. (equal '(2)
  1624. (org-test-with-temp-text "
  1625. * H1
  1626. :PROPERTIES:
  1627. :TEST: 1
  1628. :END:
  1629. * H2
  1630. :PROPERTIES:
  1631. :TEST: 2
  1632. :END:"
  1633. (org-map-entries #'point "TEST=1"))))
  1634. ;; Multiple criteria.
  1635. (should
  1636. (equal '(23)
  1637. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1638. (let (org-odd-levels-only
  1639. (org-use-tag-inheritance nil))
  1640. (org-map-entries #'point "yes+LEVEL=1")))))
  1641. ;; "or" criteria.
  1642. (should
  1643. (equal '(12 24)
  1644. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1645. (let (org-odd-levels-only)
  1646. (org-map-entries #'point "LEVEL=2|no")))))
  1647. (should
  1648. (equal '(1 12)
  1649. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  1650. (let (org-odd-levels-only)
  1651. (org-map-entries #'point "yes|no")))))
  1652. ;; "and" criteria.
  1653. (should
  1654. (equal '(22)
  1655. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  1656. (let (org-odd-levels-only)
  1657. (org-map-entries #'point "yes&no"))))))
  1658. (ert-deftest test-org/edit-headline ()
  1659. "Test `org-edit-headline' specifications."
  1660. (should
  1661. (equal "* B"
  1662. (org-test-with-temp-text "* A"
  1663. (org-edit-headline "B")
  1664. (buffer-string))))
  1665. ;; Handle empty headings.
  1666. (should
  1667. (equal "* "
  1668. (org-test-with-temp-text "* A"
  1669. (org-edit-headline "")
  1670. (buffer-string))))
  1671. (should
  1672. (equal "* A"
  1673. (org-test-with-temp-text "* "
  1674. (org-edit-headline "A")
  1675. (buffer-string))))
  1676. ;; Handle TODO keywords and priority cookies.
  1677. (should
  1678. (equal "* TODO B"
  1679. (org-test-with-temp-text "* TODO A"
  1680. (org-edit-headline "B")
  1681. (buffer-string))))
  1682. (should
  1683. (equal "* [#A] B"
  1684. (org-test-with-temp-text "* [#A] A"
  1685. (org-edit-headline "B")
  1686. (buffer-string))))
  1687. (should
  1688. (equal "* TODO [#A] B"
  1689. (org-test-with-temp-text "* TODO [#A] A"
  1690. (org-edit-headline "B")
  1691. (buffer-string))))
  1692. ;; Handle tags.
  1693. (equal "* B :tag:"
  1694. (org-test-with-temp-text "* A :tag:"
  1695. (let ((org-tags-column 4)) (org-edit-headline "B"))
  1696. (buffer-string))))
  1697. ;;; Keywords
  1698. (ert-deftest test-org/set-regexps-and-options ()
  1699. "Test `org-set-regexps-and-options' specifications."
  1700. ;; TAGS keyword.
  1701. (should
  1702. (equal '(("A"))
  1703. (let ((org-tag-alist '(("A")))
  1704. (org-tag-persistent-alist nil))
  1705. (org-test-with-temp-text ""
  1706. (org-mode-restart)
  1707. org-current-tag-alist))))
  1708. (should
  1709. (equal '(("B"))
  1710. (let ((org-tag-alist '(("A")))
  1711. (org-tag-persistent-alist nil))
  1712. (org-test-with-temp-text "#+TAGS: B"
  1713. (org-mode-restart)
  1714. org-current-tag-alist))))
  1715. (should
  1716. (equal '(("C") ("B"))
  1717. (let ((org-tag-alist '(("A")))
  1718. (org-tag-persistent-alist '(("C"))))
  1719. (org-test-with-temp-text "#+TAGS: B"
  1720. (org-mode-restart)
  1721. org-current-tag-alist))))
  1722. (should
  1723. (equal '(("B"))
  1724. (let ((org-tag-alist '(("A")))
  1725. (org-tag-persistent-alist '(("C"))))
  1726. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  1727. (org-mode-restart)
  1728. org-current-tag-alist))))
  1729. (should
  1730. (equal '(("A" . ?a) ("B") ("C"))
  1731. (let ((org-tag-persistant-alist nil))
  1732. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1733. (org-mode-restart)
  1734. org-current-tag-alist))))
  1735. (should
  1736. (equal '(("A") (:newline) ("B"))
  1737. (let ((org-tag-persistent-alist nil))
  1738. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1739. (org-mode-restart)
  1740. org-current-tag-alist))))
  1741. (should
  1742. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1743. (let ((org-tag-persistent-alist nil))
  1744. (org-test-with-temp-text "#+TAGS: { A B } C"
  1745. (org-mode-restart)
  1746. org-current-tag-alist))))
  1747. (should
  1748. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1749. (let ((org-tag-persistent-alist nil))
  1750. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1751. (org-mode-restart)
  1752. org-current-tag-alist))))
  1753. (should
  1754. (equal '(("A" "B" "C"))
  1755. (let ((org-tag-persistent-alist nil))
  1756. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1757. (org-mode-restart)
  1758. org-tag-groups-alist))))
  1759. (should
  1760. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1761. (let ((org-tag-persistent-alist nil))
  1762. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1763. (org-mode-restart)
  1764. org-current-tag-alist))))
  1765. (should
  1766. (equal '(("A" "B" "C"))
  1767. (let ((org-tag-persistent-alist nil))
  1768. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1769. (org-mode-restart)
  1770. org-tag-groups-alist))))
  1771. ;; FILETAGS keyword.
  1772. (should
  1773. (equal '("A" "B" "C")
  1774. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1775. (org-mode-restart)
  1776. org-file-tags)))
  1777. ;; PROPERTY keyword. Property names are case-insensitive.
  1778. (should
  1779. (equal "foo=1"
  1780. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1781. (org-mode-restart)
  1782. (cdr (assoc "var" org-file-properties)))))
  1783. (should
  1784. (equal
  1785. "foo=1 bar=2"
  1786. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1787. (org-mode-restart)
  1788. (cdr (assoc "var" org-file-properties)))))
  1789. (should
  1790. (equal
  1791. "foo=1 bar=2"
  1792. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1793. (org-mode-restart)
  1794. (cdr (assoc "var" org-file-properties)))))
  1795. ;; ARCHIVE keyword.
  1796. (should
  1797. (equal "%s_done::"
  1798. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1799. (org-mode-restart)
  1800. org-archive-location)))
  1801. ;; CATEGORY keyword.
  1802. (should
  1803. (eq 'test
  1804. (org-test-with-temp-text "#+CATEGORY: test"
  1805. (org-mode-restart)
  1806. org-category)))
  1807. (should
  1808. (equal "test"
  1809. (org-test-with-temp-text "#+CATEGORY: test"
  1810. (org-mode-restart)
  1811. (cdr (assoc "CATEGORY" org-file-properties)))))
  1812. ;; COLUMNS keyword.
  1813. (should
  1814. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1815. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1816. (org-mode-restart)
  1817. org-columns-default-format)))
  1818. ;; CONSTANTS keyword. Constants names are case sensitive.
  1819. (should
  1820. (equal '("299792458." "3.14")
  1821. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1822. (org-mode-restart)
  1823. (mapcar
  1824. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1825. '("c" "pi")))))
  1826. (should
  1827. (equal "3.14"
  1828. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1829. (org-mode-restart)
  1830. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1831. (should
  1832. (equal "22/7"
  1833. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1834. (org-mode-restart)
  1835. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1836. ;; LINK keyword.
  1837. (should
  1838. (equal
  1839. '("url1" "url2")
  1840. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1841. (org-mode-restart)
  1842. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1843. '("a" "b")))))
  1844. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1845. (should
  1846. (equal
  1847. '(?X ?Z ?Y)
  1848. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1849. (org-mode-restart)
  1850. (list org-highest-priority org-lowest-priority org-default-priority))))
  1851. (should
  1852. (equal
  1853. '(?A ?C ?B)
  1854. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1855. (org-mode-restart)
  1856. (list org-highest-priority org-lowest-priority org-default-priority))))
  1857. ;; STARTUP keyword.
  1858. (should
  1859. (equal '(t t)
  1860. (org-test-with-temp-text "#+STARTUP: fold odd"
  1861. (org-mode-restart)
  1862. (list org-startup-folded org-odd-levels-only))))
  1863. ;; TODO keywords.
  1864. (should
  1865. (equal '(("A" "B") ("C"))
  1866. (org-test-with-temp-text "#+TODO: A B | C"
  1867. (org-mode-restart)
  1868. (list org-not-done-keywords org-done-keywords))))
  1869. (should
  1870. (equal '(("A" "C") ("B" "D"))
  1871. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1872. (org-mode-restart)
  1873. (list org-not-done-keywords org-done-keywords))))
  1874. (should
  1875. (equal '(("A" "B") ("C"))
  1876. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1877. (org-mode-restart)
  1878. (list org-not-done-keywords org-done-keywords))))
  1879. (should
  1880. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1881. (org-test-with-temp-text "#+TODO: A(a)"
  1882. (org-mode-restart)
  1883. org-todo-key-alist)))
  1884. (should
  1885. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1886. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1887. (org-mode-restart)
  1888. org-todo-log-states)))
  1889. ;; Enter SETUPFILE keyword.
  1890. (should
  1891. (equal "1"
  1892. (org-test-with-temp-text
  1893. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1894. (org-mode-restart)
  1895. (cdr (assoc "a" org-file-properties))))))
  1896. ;;; Links
  1897. ;;;; Coderefs
  1898. (ert-deftest test-org/coderef ()
  1899. "Test coderef links specifications."
  1900. (should
  1901. (org-test-with-temp-text "
  1902. #+BEGIN_SRC emacs-lisp
  1903. \(+ 1 1) (ref:sc)
  1904. #+END_SRC
  1905. \[[(sc)<point>]]"
  1906. (org-open-at-point)
  1907. (looking-at "(ref:sc)")))
  1908. ;; Find coderef even with alternate label format.
  1909. (should
  1910. (org-test-with-temp-text "
  1911. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  1912. \(+ 1 1) {ref:sc}
  1913. #+END_SRC
  1914. \[[(sc)<point>]]"
  1915. (org-open-at-point)
  1916. (looking-at "{ref:sc}"))))
  1917. ;;;; Custom ID
  1918. (ert-deftest test-org/custom-id ()
  1919. "Test custom ID links specifications."
  1920. (should
  1921. (org-test-with-temp-text
  1922. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  1923. (org-open-at-point)
  1924. (looking-at-p "\\* H1")))
  1925. ;; Throw an error on false positives.
  1926. (should-error
  1927. (org-test-with-temp-text
  1928. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  1929. (org-open-at-point)
  1930. (looking-at-p "\\* H1"))))
  1931. ;;;; Fuzzy Links
  1932. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1933. ;; a named element (#+name: text) and to headlines (* Text).
  1934. (ert-deftest test-org/fuzzy-links ()
  1935. "Test fuzzy links specifications."
  1936. ;; Fuzzy link goes in priority to a matching target.
  1937. (should
  1938. (org-test-with-temp-text
  1939. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  1940. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1941. (looking-at "<<Test>>")))
  1942. ;; Then fuzzy link points to an element with a given name.
  1943. (should
  1944. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  1945. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1946. (looking-at "#\\+NAME: Test")))
  1947. ;; A target still lead to a matching headline otherwise.
  1948. (should
  1949. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  1950. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1951. (looking-at "\\* Head2")))
  1952. ;; With a leading star in link, enforce heading match.
  1953. (should
  1954. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  1955. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1956. (looking-at "\\* Test")))
  1957. ;; With a leading star in link, enforce exact heading match, even
  1958. ;; with `org-link-search-must-match-exact-headline' set to nil.
  1959. (should-error
  1960. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  1961. (let ((org-link-search-must-match-exact-headline nil))
  1962. (org-open-at-point))))
  1963. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  1964. (should
  1965. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  1966. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  1967. (looking-at "\\* Test")))
  1968. (should
  1969. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  1970. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  1971. (looking-at "\\* Test")))
  1972. ;; Heading match should not care about spaces, cookies, TODO
  1973. ;; keywords, priorities, and tags.
  1974. (should
  1975. (let ((first-line
  1976. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1977. (org-test-with-temp-text
  1978. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  1979. (let ((org-link-search-must-match-exact-headline nil)
  1980. (org-todo-regexp "TODO"))
  1981. (org-open-at-point))
  1982. (looking-at (regexp-quote first-line)))))
  1983. ;; Heading match should still be exact.
  1984. (should-error
  1985. (let ((first-line
  1986. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1987. (org-test-with-temp-text
  1988. (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
  1989. (let ((org-link-search-must-match-exact-headline nil)
  1990. (org-todo-regexp "TODO"))
  1991. (org-open-at-point)))))
  1992. ;; Heading match ignores COMMENT keyword.
  1993. (should
  1994. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  1995. (org-open-at-point)
  1996. (looking-at "\\* COMMENT Test")))
  1997. ;; Correctly un-hexify fuzzy links.
  1998. (should
  1999. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2000. (org-open-at-point)
  2001. (bobp))))
  2002. ;;;; Link Escaping
  2003. (ert-deftest test-org/org-link-escape-ascii-character ()
  2004. "Escape an ascii character."
  2005. (should
  2006. (string=
  2007. "%5B"
  2008. (org-link-escape "["))))
  2009. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2010. "Escape an ascii control character."
  2011. (should
  2012. (string=
  2013. "%09"
  2014. (org-link-escape "\t"))))
  2015. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2016. "Escape an unicode multibyte character."
  2017. (should
  2018. (string=
  2019. "%E2%82%AC"
  2020. (org-link-escape "€"))))
  2021. (ert-deftest test-org/org-link-escape-custom-table ()
  2022. "Escape string with custom character table."
  2023. (should
  2024. (string=
  2025. "Foo%3A%42ar%0A"
  2026. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2027. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2028. "Escape string with custom table merged with default table."
  2029. (should
  2030. (string=
  2031. "%5BF%6F%6F%3A%42ar%0A%5D"
  2032. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2033. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2034. "Unescape an ascii character."
  2035. (should
  2036. (string=
  2037. "["
  2038. (org-link-unescape "%5B"))))
  2039. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2040. "Unescpae an ascii control character."
  2041. (should
  2042. (string=
  2043. "\n"
  2044. (org-link-unescape "%0A"))))
  2045. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2046. "Unescape unicode multibyte character."
  2047. (should
  2048. (string=
  2049. "€"
  2050. (org-link-unescape "%E2%82%AC"))))
  2051. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2052. "Unescape old style percent escaped character."
  2053. (should
  2054. (string=
  2055. "àâçèéêîôùû"
  2056. (decode-coding-string
  2057. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2058. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2059. "Escape and unescape a URL that includes an escaped char.
  2060. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2061. (should
  2062. (string=
  2063. "http://some.host.com/form?&id=blah%2Bblah25"
  2064. (org-link-unescape
  2065. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2066. ;;;; Open at point
  2067. (ert-deftest test-org/open-at-point-in-keyword ()
  2068. "Does `org-open-at-point' open link in a keyword line?"
  2069. (should
  2070. (org-test-with-temp-text
  2071. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2072. (org-open-at-point) t)))
  2073. (ert-deftest test-org/open-at-point-in-property ()
  2074. "Does `org-open-at-point' open link in property drawer?"
  2075. (should
  2076. (org-test-with-temp-text
  2077. "* Headline
  2078. :PROPERTIES:
  2079. :URL: <point>[[*Headline]]
  2080. :END:"
  2081. (org-open-at-point) t)))
  2082. (ert-deftest test-org/open-at-point-in-comment ()
  2083. "Does `org-open-at-point' open link in a commented line?"
  2084. (should
  2085. (org-test-with-temp-text
  2086. "<<top>>\n# <point>[[top]]"
  2087. (org-open-at-point) t)))
  2088. (ert-deftest test-org/open-at-point/inline-image ()
  2089. "Test `org-open-at-point' on nested links."
  2090. (should
  2091. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2092. (org-open-at-point)
  2093. (bobp))))
  2094. (ert-deftest test-org/open-at-point/radio-target ()
  2095. "Test `org-open-at-point' on radio targets."
  2096. (should
  2097. (org-test-with-temp-text "<<<target>>> <point>target"
  2098. (org-update-radio-target-regexp)
  2099. (org-open-at-point)
  2100. (eq (org-element-type (org-element-context)) 'radio-target))))
  2101. ;;;; Stored links
  2102. (ert-deftest test-org/store-link ()
  2103. "Test `org-store-link' specifications."
  2104. ;; On a headline, link to that headline. Use heading as the
  2105. ;; description of the link.
  2106. (should
  2107. (let (org-store-link-props org-stored-links)
  2108. (org-test-with-temp-text-in-file "* H1"
  2109. (let ((file (buffer-file-name)))
  2110. (equal (format "[[file:%s::*H1][H1]]" file)
  2111. (org-store-link nil))))))
  2112. ;; On a headline, remove any link from description.
  2113. (should
  2114. (let (org-store-link-props org-stored-links)
  2115. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2116. (let ((file (buffer-file-name)))
  2117. (equal (format "[[file:%s::*%s][d]]"
  2118. file
  2119. (org-link-escape "[[#l][d]]"))
  2120. (org-store-link nil))))))
  2121. (should
  2122. (let (org-store-link-props org-stored-links)
  2123. (org-test-with-temp-text-in-file "* [[l]]"
  2124. (let ((file (buffer-file-name)))
  2125. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2126. (org-store-link nil))))))
  2127. (should
  2128. (let (org-store-link-props org-stored-links)
  2129. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2130. (let ((file (buffer-file-name)))
  2131. (equal (format "[[file:%s::*%s][d1 d2]]"
  2132. file
  2133. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2134. (org-store-link nil))))))
  2135. ;; On a named element, link to that element.
  2136. (should
  2137. (let (org-store-link-props org-stored-links)
  2138. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2139. (let ((file (buffer-file-name)))
  2140. (equal (format "[[file:%s::foo][foo]]" file)
  2141. (org-store-link nil)))))))
  2142. ;;; Node Properties
  2143. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2144. "Ensure properties accumulate in subtree drawers."
  2145. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2146. (org-babel-next-src-block)
  2147. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2148. (ert-deftest test-org/custom-properties ()
  2149. "Test custom properties specifications."
  2150. ;; Standard test.
  2151. (should
  2152. (let ((org-custom-properties '("FOO")))
  2153. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2154. (org-toggle-custom-properties-visibility)
  2155. (org-invisible-p2))))
  2156. ;; Properties are case-insensitive.
  2157. (should
  2158. (let ((org-custom-properties '("FOO")))
  2159. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2160. (org-toggle-custom-properties-visibility)
  2161. (org-invisible-p2))))
  2162. (should
  2163. (let ((org-custom-properties '("foo")))
  2164. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2165. (org-toggle-custom-properties-visibility)
  2166. (org-invisible-p2))))
  2167. ;; Multiple custom properties in the same drawer.
  2168. (should
  2169. (let ((org-custom-properties '("FOO" "BAR")))
  2170. (org-test-with-temp-text
  2171. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2172. (org-toggle-custom-properties-visibility)
  2173. (and (org-invisible-p2)
  2174. (not (progn (forward-line) (org-invisible-p2)))
  2175. (progn (forward-line) (org-invisible-p2))))))
  2176. ;; Hide custom properties with an empty value.
  2177. (should
  2178. (let ((org-custom-properties '("FOO")))
  2179. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2180. (org-toggle-custom-properties-visibility)
  2181. (org-invisible-p2))))
  2182. ;; Do not hide fake properties.
  2183. (should-not
  2184. (let ((org-custom-properties '("FOO")))
  2185. (org-test-with-temp-text ":FOO: val\n"
  2186. (org-toggle-custom-properties-visibility)
  2187. (org-invisible-p2))))
  2188. (should-not
  2189. (let ((org-custom-properties '("A")))
  2190. (org-test-with-temp-text
  2191. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2192. (org-toggle-custom-properties-visibility)
  2193. (org-invisible-p2)))))
  2194. ;;; Mark Region
  2195. (ert-deftest test-org/mark-subtree ()
  2196. "Test `org-mark-subtree' specifications."
  2197. ;; Error when point is before first headline.
  2198. (should-error
  2199. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2200. (progn (transient-mark-mode 1)
  2201. (org-mark-subtree))))
  2202. ;; Without argument, mark current subtree.
  2203. (should
  2204. (equal
  2205. '(12 32)
  2206. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2207. (progn (transient-mark-mode 1)
  2208. (forward-line 2)
  2209. (org-mark-subtree)
  2210. (list (region-beginning) (region-end))))))
  2211. ;; With an argument, move ARG up.
  2212. (should
  2213. (equal
  2214. '(1 32)
  2215. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2216. (progn (transient-mark-mode 1)
  2217. (forward-line 2)
  2218. (org-mark-subtree 1)
  2219. (list (region-beginning) (region-end))))))
  2220. ;; Do not get fooled by inlinetasks.
  2221. (when (featurep 'org-inlinetask)
  2222. (should
  2223. (= 1
  2224. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2225. (progn (transient-mark-mode 1)
  2226. (forward-line 1)
  2227. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2228. (region-beginning)))))))
  2229. ;;; Miscellaneous
  2230. (ert-deftest test-org/in-regexp ()
  2231. "Test `org-in-regexp' specifications."
  2232. ;; Standard tests.
  2233. (should
  2234. (org-test-with-temp-text "xx ab<point>c xx"
  2235. (org-in-regexp "abc")))
  2236. (should-not
  2237. (org-test-with-temp-text "xx abc <point>xx"
  2238. (org-in-regexp "abc")))
  2239. ;; Return non-nil even with multiple matching regexps in the same
  2240. ;; line.
  2241. (should
  2242. (org-test-with-temp-text "abc xx ab<point>c xx"
  2243. (org-in-regexp "abc")))
  2244. ;; With optional argument NLINES, check extra lines around point.
  2245. (should-not
  2246. (org-test-with-temp-text "A\nB<point>\nC"
  2247. (org-in-regexp "A\nB\nC")))
  2248. (should
  2249. (org-test-with-temp-text "A\nB<point>\nC"
  2250. (org-in-regexp "A\nB\nC" 1)))
  2251. (should-not
  2252. (org-test-with-temp-text "A\nB\nC<point>"
  2253. (org-in-regexp "A\nB\nC" 1)))
  2254. ;; When optional argument VISUALLY is non-nil, return nil if at
  2255. ;; regexp boundaries.
  2256. (should
  2257. (org-test-with-temp-text "xx abc<point> xx"
  2258. (org-in-regexp "abc")))
  2259. (should-not
  2260. (org-test-with-temp-text "xx abc<point> xx"
  2261. (org-in-regexp "abc" nil t))))
  2262. ;;; Navigation
  2263. (ert-deftest test-org/end-of-meta-data ()
  2264. "Test `org-end-of-meta-data' specifications."
  2265. ;; Skip planning line.
  2266. (should
  2267. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  2268. (org-end-of-meta-data)
  2269. (eobp)))
  2270. ;; Skip properties drawer.
  2271. (should
  2272. (org-test-with-temp-text
  2273. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  2274. (org-end-of-meta-data)
  2275. (eobp)))
  2276. ;; Skip both.
  2277. (should
  2278. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  2279. (org-end-of-meta-data)
  2280. (eobp)))
  2281. ;; Nothing to skip, go to first line.
  2282. (should
  2283. (org-test-with-temp-text "* Headline\nContents"
  2284. (org-end-of-meta-data)
  2285. (looking-at "Contents")))
  2286. ;; With option argument, skip empty lines, regular drawers and
  2287. ;; clocking lines.
  2288. (should
  2289. (org-test-with-temp-text "* Headline\n\nContents"
  2290. (org-end-of-meta-data t)
  2291. (looking-at "Contents")))
  2292. (should
  2293. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  2294. (org-end-of-meta-data t)
  2295. (looking-at "Contents")))
  2296. (should
  2297. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  2298. (org-end-of-meta-data t)
  2299. (looking-at "Contents")))
  2300. ;; Special case: do not skip incomplete drawers.
  2301. (should
  2302. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  2303. (org-end-of-meta-data t)
  2304. (looking-at ":LOGBOOK:")))
  2305. ;; Special case: Be careful about consecutive headlines.
  2306. (should-not
  2307. (org-test-with-temp-text "* H1\n*H2\nContents"
  2308. (org-end-of-meta-data t)
  2309. (looking-at "Contents"))))
  2310. (ert-deftest test-org/beginning-of-line ()
  2311. "Test `org-beginning-of-line' specifications."
  2312. ;; Standard test.
  2313. (should
  2314. (org-test-with-temp-text "Some text\nSome other text"
  2315. (progn (org-beginning-of-line) (bolp))))
  2316. ;; Standard test with `visual-line-mode'.
  2317. (should-not
  2318. (org-test-with-temp-text "A long line of text\nSome other text"
  2319. (progn (visual-line-mode)
  2320. (forward-char 2)
  2321. (dotimes (i 1000) (insert "very "))
  2322. (org-beginning-of-line)
  2323. (bolp))))
  2324. ;; At an headline with special movement.
  2325. (should
  2326. (org-test-with-temp-text "* TODO Headline"
  2327. (let ((org-special-ctrl-a/e t))
  2328. (org-end-of-line)
  2329. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  2330. (progn (org-beginning-of-line) (bolp))
  2331. (progn (org-beginning-of-line) (looking-at "Headline"))))))
  2332. ;; Special case: Do not error when the buffer contains only a single
  2333. ;; asterisk.
  2334. (should
  2335. (org-test-with-temp-text "*<point>"
  2336. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))))
  2337. (should
  2338. (org-test-with-temp-text "*<point>"
  2339. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line)))))
  2340. (ert-deftest test-org/end-of-line ()
  2341. "Test `org-end-of-line' specifications."
  2342. ;; Standard test.
  2343. (should
  2344. (org-test-with-temp-text "Some text\nSome other text"
  2345. (progn (org-end-of-line) (eolp))))
  2346. ;; Standard test with `visual-line-mode'.
  2347. (should-not
  2348. (org-test-with-temp-text "A long line of text\nSome other text"
  2349. (progn (visual-line-mode)
  2350. (forward-char 2)
  2351. (dotimes (i 1000) (insert "very "))
  2352. (goto-char (point-min))
  2353. (org-end-of-line)
  2354. (eolp))))
  2355. ;; At an headline with special movement.
  2356. (should
  2357. (org-test-with-temp-text "* Headline1 :tag:\n"
  2358. (let ((org-special-ctrl-a/e t))
  2359. (and (progn (org-end-of-line) (looking-at " :tag:"))
  2360. (progn (org-end-of-line) (eolp))
  2361. (progn (org-end-of-line) (looking-at " :tag:"))))))
  2362. ;; At an headline without special movement.
  2363. (should
  2364. (org-test-with-temp-text "* Headline2 :tag:\n"
  2365. (let ((org-special-ctrl-a/e nil))
  2366. (and (progn (org-end-of-line) (eolp))
  2367. (progn (org-end-of-line) (eolp))))))
  2368. ;; At an headline, with reversed movement.
  2369. (should
  2370. (org-test-with-temp-text "* Headline3 :tag:\n"
  2371. (let ((org-special-ctrl-a/e 'reversed)
  2372. (this-command last-command))
  2373. (and (progn (org-end-of-line) (eolp))
  2374. (progn (org-end-of-line) (looking-at " :tag:"))))))
  2375. ;; At a block without hidden contents.
  2376. (should
  2377. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2378. (progn (org-end-of-line) (eolp))))
  2379. ;; At a block with hidden contents.
  2380. (should-not
  2381. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2382. (let ((org-special-ctrl-a/e t))
  2383. (org-hide-block-toggle)
  2384. (org-end-of-line)
  2385. (eobp)))))
  2386. (ert-deftest test-org/forward-sentence ()
  2387. "Test `org-forward-sentence' specifications."
  2388. ;; At the end of a table cell, move to the end of the next one.
  2389. (should
  2390. (org-test-with-temp-text "| a<point> | b |"
  2391. (org-forward-sentence)
  2392. (looking-at " |$")))
  2393. ;; Elsewhere in a cell, move to its end.
  2394. (should
  2395. (org-test-with-temp-text "| a<point>c | b |"
  2396. (org-forward-sentence)
  2397. (looking-at " | b |$")))
  2398. ;; Otherwise, simply call `forward-sentence'.
  2399. (should
  2400. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2401. (org-forward-sentence)
  2402. (looking-at " Sentence 2.")))
  2403. (should
  2404. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2405. (org-forward-sentence)
  2406. (org-forward-sentence)
  2407. (eobp)))
  2408. ;; At the end of an element, jump to the next one, without stopping
  2409. ;; on blank lines in-between.
  2410. (should
  2411. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  2412. (org-forward-sentence)
  2413. (eobp))))
  2414. (ert-deftest test-org/backward-sentence ()
  2415. "Test `org-backward-sentence' specifications."
  2416. ;; At the beginning of a table cell, move to the beginning of the
  2417. ;; previous one.
  2418. (should
  2419. (org-test-with-temp-text "| a | <point>b |"
  2420. (org-backward-sentence)
  2421. (looking-at "a | b |$")))
  2422. ;; Elsewhere in a cell, move to its beginning.
  2423. (should
  2424. (org-test-with-temp-text "| a | b<point>c |"
  2425. (org-backward-sentence)
  2426. (looking-at "bc |$")))
  2427. ;; Otherwise, simply call `backward-sentence'.
  2428. (should
  2429. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2430. (org-backward-sentence)
  2431. (looking-at "Sentence 2.")))
  2432. (should
  2433. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2434. (org-backward-sentence)
  2435. (org-backward-sentence)
  2436. (bobp)))
  2437. ;; Make sure to hit the beginning of a sentence on the same line as
  2438. ;; an item.
  2439. (should
  2440. (org-test-with-temp-text "- Line 1\n line <point>2."
  2441. (org-backward-sentence)
  2442. (looking-at "Line 1"))))
  2443. (ert-deftest test-org/forward-paragraph ()
  2444. "Test `org-forward-paragraph' specifications."
  2445. ;; At end of buffer, return an error.
  2446. (should-error
  2447. (org-test-with-temp-text "Paragraph"
  2448. (goto-char (point-max))
  2449. (org-forward-paragraph)))
  2450. ;; Standard test.
  2451. (should
  2452. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2453. (org-forward-paragraph)
  2454. (looking-at "P2")))
  2455. ;; Ignore depth.
  2456. (should
  2457. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  2458. (org-forward-paragraph)
  2459. (looking-at "P1")))
  2460. ;; Do not enter elements with invisible contents.
  2461. (should
  2462. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  2463. (org-hide-block-toggle)
  2464. (org-forward-paragraph)
  2465. (looking-at "P3")))
  2466. ;; On an affiliated keyword, jump to the beginning of the element.
  2467. (should
  2468. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  2469. (org-forward-paragraph)
  2470. (looking-at "Para")))
  2471. ;; On an item or a footnote definition, move to the second element
  2472. ;; inside, if any.
  2473. (should
  2474. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  2475. (org-forward-paragraph)
  2476. (looking-at " Paragraph")))
  2477. (should
  2478. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  2479. (org-forward-paragraph)
  2480. (looking-at "Paragraph")))
  2481. ;; On an item, or a footnote definition, when the first line is
  2482. ;; empty, move to the first item.
  2483. (should
  2484. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  2485. (org-forward-paragraph)
  2486. (looking-at " Paragraph")))
  2487. (should
  2488. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  2489. (org-forward-paragraph)
  2490. (looking-at "Paragraph")))
  2491. ;; On a table (resp. a property drawer) do not move through table
  2492. ;; rows (resp. node properties).
  2493. (should
  2494. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  2495. (org-forward-paragraph)
  2496. (looking-at "Paragraph")))
  2497. (should
  2498. (org-test-with-temp-text
  2499. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  2500. (org-forward-paragraph)
  2501. (looking-at "Paragraph")))
  2502. ;; On a verse or source block, stop after blank lines.
  2503. (should
  2504. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  2505. (org-forward-paragraph)
  2506. (looking-at "L2")))
  2507. (should
  2508. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  2509. (org-forward-paragraph)
  2510. (looking-at "L2"))))
  2511. (ert-deftest test-org/backward-paragraph ()
  2512. "Test `org-backward-paragraph' specifications."
  2513. ;; Error at beginning of buffer.
  2514. (should-error
  2515. (org-test-with-temp-text "Paragraph"
  2516. (org-backward-paragraph)))
  2517. ;; Regular test.
  2518. (should
  2519. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2520. (goto-char (point-max))
  2521. (org-backward-paragraph)
  2522. (looking-at "P3")))
  2523. (should
  2524. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2525. (goto-char (point-max))
  2526. (beginning-of-line)
  2527. (org-backward-paragraph)
  2528. (looking-at "P2")))
  2529. ;; Ignore depth.
  2530. (should
  2531. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  2532. (goto-char (point-max))
  2533. (beginning-of-line)
  2534. (org-backward-paragraph)
  2535. (looking-at "P2")))
  2536. ;; Ignore invisible elements.
  2537. (should
  2538. (org-test-with-temp-text "* H1\n P1\n* H2"
  2539. (org-cycle)
  2540. (goto-char (point-max))
  2541. (beginning-of-line)
  2542. (org-backward-paragraph)
  2543. (bobp)))
  2544. ;; On an affiliated keyword, jump to the first one.
  2545. (should
  2546. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  2547. (search-forward "c2")
  2548. (org-backward-paragraph)
  2549. (looking-at "#\\+name")))
  2550. ;; On the second element in an item or a footnote definition, jump
  2551. ;; to item or the definition.
  2552. (should
  2553. (org-test-with-temp-text "- line1\n\n line2"
  2554. (goto-char (point-max))
  2555. (beginning-of-line)
  2556. (org-backward-paragraph)
  2557. (looking-at "- line1")))
  2558. (should
  2559. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  2560. (goto-char (point-max))
  2561. (beginning-of-line)
  2562. (org-backward-paragraph)
  2563. (looking-at "\\[fn:1\\] line1")))
  2564. ;; On a table (resp. a property drawer), ignore table rows
  2565. ;; (resp. node properties).
  2566. (should
  2567. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  2568. (goto-char (point-max))
  2569. (beginning-of-line)
  2570. (org-backward-paragraph)
  2571. (bobp)))
  2572. (should
  2573. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  2574. (org-backward-paragraph)
  2575. (looking-at ":PROPERTIES:")))
  2576. ;; On a source or verse block, stop before blank lines.
  2577. (should
  2578. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  2579. (search-forward "L3")
  2580. (beginning-of-line)
  2581. (org-backward-paragraph)
  2582. (looking-at "L2")))
  2583. (should
  2584. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  2585. (search-forward "L3")
  2586. (beginning-of-line)
  2587. (org-backward-paragraph)
  2588. (looking-at "L2"))))
  2589. (ert-deftest test-org/forward-element ()
  2590. "Test `org-forward-element' specifications."
  2591. ;; 1. At EOB: should error.
  2592. (org-test-with-temp-text "Some text\n"
  2593. (goto-char (point-max))
  2594. (should-error (org-forward-element)))
  2595. ;; 2. Standard move: expected to ignore blank lines.
  2596. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  2597. (org-forward-element)
  2598. (should (looking-at (regexp-quote "Second paragraph."))))
  2599. ;; 3. Headline tests.
  2600. (org-test-with-temp-text "
  2601. * Head 1
  2602. ** Head 1.1
  2603. *** Head 1.1.1
  2604. ** Head 1.2"
  2605. ;; 3.1. At an headline beginning: move to next headline at the
  2606. ;; same level.
  2607. (goto-line 3)
  2608. (org-forward-element)
  2609. (should (looking-at (regexp-quote "** Head 1.2")))
  2610. ;; 3.2. At an headline beginning: move to parent headline if no
  2611. ;; headline at the same level.
  2612. (goto-line 3)
  2613. (org-forward-element)
  2614. (should (looking-at (regexp-quote "** Head 1.2"))))
  2615. ;; 4. Greater element tests.
  2616. (org-test-with-temp-text
  2617. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  2618. ;; 4.1. At a greater element: expected to skip contents.
  2619. (org-forward-element)
  2620. (should (looking-at (regexp-quote "Outside.")))
  2621. ;; 4.2. At the end of greater element contents: expected to skip
  2622. ;; to the end of the greater element.
  2623. (goto-line 2)
  2624. (org-forward-element)
  2625. (should (looking-at (regexp-quote "Outside."))))
  2626. ;; 5. List tests.
  2627. (org-test-with-temp-text "
  2628. - item1
  2629. - sub1
  2630. - sub2
  2631. - sub3
  2632. Inner paragraph.
  2633. - item2
  2634. Outside."
  2635. ;; 5.1. At list top point: expected to move to the element after
  2636. ;; the list.
  2637. (goto-line 2)
  2638. (org-forward-element)
  2639. (should (looking-at (regexp-quote "Outside.")))
  2640. ;; 5.2. Special case: at the first line of a sub-list, but not at
  2641. ;; beginning of line, move to next item.
  2642. (goto-line 2)
  2643. (forward-char)
  2644. (org-forward-element)
  2645. (should (looking-at "- item2"))
  2646. (goto-line 4)
  2647. (forward-char)
  2648. (org-forward-element)
  2649. (should (looking-at " - sub2"))
  2650. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  2651. (goto-line 4)
  2652. (org-forward-element)
  2653. (should (looking-at (regexp-quote " Inner paragraph.")))
  2654. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  2655. (goto-line 8)
  2656. (org-forward-element)
  2657. (should (looking-at (regexp-quote " Inner paragraph.")))
  2658. ;; 5.5. At an item: expected to move to next item, if any.
  2659. (goto-line 6)
  2660. (org-forward-element)
  2661. (should (looking-at " - sub3"))))
  2662. (ert-deftest test-org/backward-element ()
  2663. "Test `org-backward-element' specifications."
  2664. ;; 1. Should error at BOB.
  2665. (org-test-with-temp-text " \nParagraph."
  2666. (should-error (org-backward-element)))
  2667. ;; 2. Should move at BOB when called on the first element in buffer.
  2668. (should
  2669. (org-test-with-temp-text "\n#+TITLE: test"
  2670. (progn (forward-line)
  2671. (org-backward-element)
  2672. (bobp))))
  2673. ;; 3. Not at the beginning of an element: move at its beginning.
  2674. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2675. (goto-line 3)
  2676. (end-of-line)
  2677. (org-backward-element)
  2678. (should (looking-at (regexp-quote "Paragraph2."))))
  2679. ;; 4. Headline tests.
  2680. (org-test-with-temp-text "
  2681. * Head 1
  2682. ** Head 1.1
  2683. *** Head 1.1.1
  2684. ** Head 1.2"
  2685. ;; 4.1. At an headline beginning: move to previous headline at the
  2686. ;; same level.
  2687. (goto-line 5)
  2688. (org-backward-element)
  2689. (should (looking-at (regexp-quote "** Head 1.1")))
  2690. ;; 4.2. At an headline beginning: move to parent headline if no
  2691. ;; headline at the same level.
  2692. (goto-line 3)
  2693. (org-backward-element)
  2694. (should (looking-at (regexp-quote "* Head 1")))
  2695. ;; 4.3. At the first top-level headline: should error.
  2696. (goto-line 2)
  2697. (should-error (org-backward-element)))
  2698. ;; 5. At beginning of first element inside a greater element:
  2699. ;; expected to move to greater element's beginning.
  2700. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  2701. (goto-line 3)
  2702. (org-backward-element)
  2703. (should (looking-at "#\\+BEGIN_CENTER")))
  2704. ;; 6. At the beginning of the first element in a section: should
  2705. ;; move back to headline, if any.
  2706. (should
  2707. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  2708. (progn (goto-char (point-max))
  2709. (beginning-of-line)
  2710. (org-backward-element)
  2711. (org-at-heading-p))))
  2712. ;; 7. List tests.
  2713. (org-test-with-temp-text "
  2714. - item1
  2715. - sub1
  2716. - sub2
  2717. - sub3
  2718. Inner paragraph.
  2719. - item2
  2720. Outside."
  2721. ;; 7.1. At beginning of sub-list: expected to move to the
  2722. ;; paragraph before it.
  2723. (goto-line 4)
  2724. (org-backward-element)
  2725. (should (looking-at "item1"))
  2726. ;; 7.2. At an item in a list: expected to move at previous item.
  2727. (goto-line 8)
  2728. (org-backward-element)
  2729. (should (looking-at " - sub2"))
  2730. (goto-line 12)
  2731. (org-backward-element)
  2732. (should (looking-at "- item1"))
  2733. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  2734. ;; beginning.
  2735. (goto-line 10)
  2736. (org-backward-element)
  2737. (should (looking-at " - sub1"))
  2738. (goto-line 15)
  2739. (org-backward-element)
  2740. (should (looking-at "- item1"))
  2741. ;; 7.4. At blank-lines before list end: expected to move to top
  2742. ;; item.
  2743. (goto-line 14)
  2744. (org-backward-element)
  2745. (should (looking-at "- item1"))))
  2746. (ert-deftest test-org/up-element ()
  2747. "Test `org-up-element' specifications."
  2748. ;; 1. At BOB or with no surrounding element: should error.
  2749. (org-test-with-temp-text "Paragraph."
  2750. (should-error (org-up-element)))
  2751. (org-test-with-temp-text "* Head1\n* Head2"
  2752. (goto-line 2)
  2753. (should-error (org-up-element)))
  2754. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2755. (goto-line 3)
  2756. (should-error (org-up-element)))
  2757. ;; 2. At an headline: move to parent headline.
  2758. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  2759. (goto-line 3)
  2760. (org-up-element)
  2761. (should (looking-at "\\* Head1")))
  2762. ;; 3. Inside a greater element: move to greater element beginning.
  2763. (org-test-with-temp-text
  2764. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  2765. (goto-line 3)
  2766. (org-up-element)
  2767. (should (looking-at "#\\+BEGIN_CENTER")))
  2768. ;; 4. List tests.
  2769. (org-test-with-temp-text "* Top
  2770. - item1
  2771. - sub1
  2772. - sub2
  2773. Paragraph within sub2.
  2774. - item2"
  2775. ;; 4.1. Within an item: move to the item beginning.
  2776. (goto-line 8)
  2777. (org-up-element)
  2778. (should (looking-at " - sub2"))
  2779. ;; 4.2. At an item in a sub-list: move to parent item.
  2780. (goto-line 4)
  2781. (org-up-element)
  2782. (should (looking-at "- item1"))
  2783. ;; 4.3. At an item in top list: move to beginning of whole list.
  2784. (goto-line 10)
  2785. (org-up-element)
  2786. (should (looking-at "- item1"))
  2787. ;; 4.4. Special case. At very top point: should move to parent of
  2788. ;; list.
  2789. (goto-line 2)
  2790. (org-up-element)
  2791. (should (looking-at "\\* Top"))))
  2792. (ert-deftest test-org/down-element ()
  2793. "Test `org-down-element' specifications."
  2794. ;; Error when the element hasn't got a recursive type.
  2795. (org-test-with-temp-text "Paragraph."
  2796. (should-error (org-down-element)))
  2797. ;; Error when the element has no contents
  2798. (org-test-with-temp-text "* Headline"
  2799. (should-error (org-down-element)))
  2800. ;; When at a plain-list, move to first item.
  2801. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  2802. (goto-line 2)
  2803. (org-down-element)
  2804. (should (looking-at " - Item 1.1")))
  2805. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  2806. (org-down-element)
  2807. (should (looking-at " Item 1")))
  2808. ;; When at a table, move to first row
  2809. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  2810. (org-down-element)
  2811. (should (looking-at " a | b |")))
  2812. ;; Otherwise, move inside the greater element.
  2813. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  2814. (org-down-element)
  2815. (should (looking-at "Paragraph"))))
  2816. (ert-deftest test-org/drag-element-backward ()
  2817. "Test `org-drag-element-backward' specifications."
  2818. ;; Standard test.
  2819. (should
  2820. (equal
  2821. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  2822. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  2823. (org-drag-element-backward)
  2824. (buffer-string))))
  2825. (should
  2826. (equal
  2827. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  2828. (org-test-with-temp-text
  2829. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  2830. (org-drag-element-backward)
  2831. (buffer-string))))
  2832. ;; Preserve blank lines.
  2833. (should
  2834. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  2835. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  2836. (org-drag-element-backward)
  2837. (buffer-string))))
  2838. ;; Preserve column.
  2839. (should
  2840. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  2841. (org-drag-element-backward)
  2842. (looking-at-p "2")))
  2843. ;; Error when trying to move first element of buffer.
  2844. (should-error
  2845. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2846. (org-drag-element-backward)))
  2847. ;; Error when trying to swap nested elements.
  2848. (should-error
  2849. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2850. (forward-line)
  2851. (org-drag-element-backward)))
  2852. ;; Error when trying to swap an headline element and a non-headline
  2853. ;; element.
  2854. (should-error
  2855. (org-test-with-temp-text "Test.\n* Head 1"
  2856. (forward-line)
  2857. (org-drag-element-backward)))
  2858. ;; Preserve visibility of elements and their contents.
  2859. (should
  2860. (equal '((63 . 82) (26 . 48))
  2861. (org-test-with-temp-text "
  2862. #+BEGIN_CENTER
  2863. Text.
  2864. #+END_CENTER
  2865. - item 1
  2866. #+BEGIN_QUOTE
  2867. Text.
  2868. #+END_QUOTE"
  2869. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2870. (search-backward "- item 1")
  2871. (org-drag-element-backward)
  2872. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2873. (overlays-in (point-min) (point-max)))))))
  2874. (ert-deftest test-org/drag-element-forward ()
  2875. "Test `org-drag-element-forward' specifications."
  2876. ;; 1. Error when trying to move first element of buffer.
  2877. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2878. (goto-line 3)
  2879. (should-error (org-drag-element-forward)))
  2880. ;; 2. Error when trying to swap nested elements.
  2881. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2882. (forward-line)
  2883. (should-error (org-drag-element-forward)))
  2884. ;; 3. Error when trying to swap a non-headline element and an
  2885. ;; headline.
  2886. (org-test-with-temp-text "Test.\n* Head 1"
  2887. (should-error (org-drag-element-forward)))
  2888. ;; 4. Otherwise, swap elements, preserving column and blank lines
  2889. ;; between elements.
  2890. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  2891. (search-forward "graph")
  2892. (org-drag-element-forward)
  2893. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  2894. (should (looking-at " 1")))
  2895. ;; 5. Preserve visibility of elements and their contents.
  2896. (org-test-with-temp-text "
  2897. #+BEGIN_CENTER
  2898. Text.
  2899. #+END_CENTER
  2900. - item 1
  2901. #+BEGIN_QUOTE
  2902. Text.
  2903. #+END_QUOTE"
  2904. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2905. (search-backward "#+BEGIN_CENTER")
  2906. (org-drag-element-forward)
  2907. (should
  2908. (equal
  2909. '((63 . 82) (26 . 48))
  2910. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2911. (overlays-in (point-min) (point-max)))))))
  2912. (ert-deftest test-org/next-block ()
  2913. "Test `org-next-block' specifications."
  2914. ;; Regular test.
  2915. (should
  2916. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2917. (org-next-block 1)
  2918. (looking-at "#\\+BEGIN_CENTER")))
  2919. ;; Ignore case.
  2920. (should
  2921. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  2922. (let ((case-fold-search nil))
  2923. (org-next-block 1)
  2924. (looking-at "#\\+begin_center"))))
  2925. ;; Ignore current line.
  2926. (should
  2927. (org-test-with-temp-text
  2928. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  2929. (org-next-block 1)
  2930. (looking-at "#\\+BEGIN_CENTER")))
  2931. ;; Throw an error when no block is found.
  2932. (should-error
  2933. (org-test-with-temp-text "Paragraph"
  2934. (org-next-block 1)))
  2935. ;; With an argument, skip many blocks at once.
  2936. (should
  2937. (org-test-with-temp-text
  2938. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  2939. (org-next-block 2)
  2940. (looking-at "#\\+BEGIN_QUOTE")))
  2941. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  2942. (should
  2943. (org-test-with-temp-text
  2944. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  2945. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  2946. (looking-at "#\\+BEGIN_QUOTE")))
  2947. ;; Optional argument is also case-insensitive.
  2948. (should
  2949. (org-test-with-temp-text
  2950. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  2951. (let ((case-fold-search nil))
  2952. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  2953. (looking-at "#\\+begin_quote")))))
  2954. (ert-deftest test-org/previous-block ()
  2955. "Test `org-previous-block' specifications."
  2956. ;; Regular test.
  2957. (should
  2958. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  2959. (org-previous-block 1)
  2960. (looking-at "#\\+BEGIN_CENTER")))
  2961. ;; Ignore case.
  2962. (should
  2963. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  2964. (let ((case-fold-search nil))
  2965. (org-previous-block 1)
  2966. (looking-at "#\\+begin_center"))))
  2967. ;; Ignore current line.
  2968. (should
  2969. (org-test-with-temp-text
  2970. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  2971. (org-previous-block 1)
  2972. (looking-at "#\\+BEGIN_QUOTE")))
  2973. ;; Throw an error when no block is found.
  2974. (should-error
  2975. (org-test-with-temp-text "Paragraph<point>"
  2976. (org-previous-block 1)))
  2977. ;; With an argument, skip many blocks at once.
  2978. (should
  2979. (org-test-with-temp-text
  2980. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  2981. (org-previous-block 2)
  2982. (looking-at "#\\+BEGIN_CENTER")))
  2983. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  2984. (should
  2985. (org-test-with-temp-text
  2986. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  2987. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  2988. (looking-at "#\\+BEGIN_QUOTE")))
  2989. ;; Optional argument is also case-insensitive.
  2990. (should
  2991. (org-test-with-temp-text
  2992. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  2993. (let ((case-fold-search nil))
  2994. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  2995. (looking-at "#\\+begin_quote")))))
  2996. ;;; Outline structure
  2997. (ert-deftest test-org/demote ()
  2998. "Test `org-demote' specifications."
  2999. ;; Add correct number of stars according to `org-odd-levels-only'.
  3000. (should
  3001. (= 2
  3002. (org-test-with-temp-text "* H"
  3003. (let ((org-odd-levels-only nil)) (org-demote))
  3004. (org-current-level))))
  3005. (should
  3006. (= 3
  3007. (org-test-with-temp-text "* H"
  3008. (let ((org-odd-levels-only t)) (org-demote))
  3009. (org-current-level))))
  3010. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3011. (should
  3012. (org-test-with-temp-text "* H :tag:"
  3013. (let ((org-tags-column 10)
  3014. (org-auto-align-tags t)
  3015. (org-odd-levels-only nil))
  3016. (org-demote))
  3017. (org-move-to-column 10)
  3018. (looking-at-p ":tag:$")))
  3019. (should-not
  3020. (org-test-with-temp-text "* H :tag:"
  3021. (let ((org-tags-column 10)
  3022. (org-auto-align-tags nil)
  3023. (org-odd-levels-only nil))
  3024. (org-demote))
  3025. (org-move-to-column 10)
  3026. (looking-at-p ":tag:$")))
  3027. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3028. ;; info and property drawers accordingly.
  3029. (should
  3030. (= 3
  3031. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3032. (let ((org-odd-levels-only nil)
  3033. (org-adapt-indentation t))
  3034. (org-demote))
  3035. (forward-line)
  3036. (org-get-indentation))))
  3037. (should
  3038. (= 3
  3039. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3040. (let ((org-odd-levels-only nil)
  3041. (org-adapt-indentation t))
  3042. (org-demote))
  3043. (forward-line)
  3044. (org-get-indentation))))
  3045. (should-not
  3046. (= 3
  3047. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3048. (let ((org-odd-levels-only nil)
  3049. (org-adapt-indentation nil))
  3050. (org-demote))
  3051. (forward-line)
  3052. (org-get-indentation))))
  3053. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3054. ;; section accordingly. Ignore, however, footnote definitions and
  3055. ;; inlinetasks boundaries.
  3056. (should
  3057. (= 3
  3058. (org-test-with-temp-text "* H\n Paragraph"
  3059. (let ((org-odd-levels-only nil)
  3060. (org-adapt-indentation t))
  3061. (org-demote))
  3062. (forward-line)
  3063. (org-get-indentation))))
  3064. (should
  3065. (= 2
  3066. (org-test-with-temp-text "* H\n Paragraph"
  3067. (let ((org-odd-levels-only nil)
  3068. (org-adapt-indentation nil))
  3069. (org-demote))
  3070. (forward-line)
  3071. (org-get-indentation))))
  3072. (should
  3073. (zerop
  3074. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  3075. (let ((org-odd-levels-only nil)
  3076. (org-adapt-indentation t))
  3077. (org-demote))
  3078. (goto-char (point-max))
  3079. (org-get-indentation))))
  3080. (should
  3081. (= 3
  3082. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  3083. (let ((org-odd-levels-only nil)
  3084. (org-adapt-indentation t))
  3085. (org-demote))
  3086. (goto-char (point-max))
  3087. (org-get-indentation))))
  3088. (when (featurep 'org-inlinetask)
  3089. (should
  3090. (zerop
  3091. (let ((org-inlinetask-min-level 5)
  3092. (org-adapt-indentation t))
  3093. (org-test-with-temp-text "* H\n***** I\n***** END"
  3094. (org-demote)
  3095. (forward-line)
  3096. (org-get-indentation))))))
  3097. (when (featurep 'org-inlinetask)
  3098. (should
  3099. (= 3
  3100. (let ((org-inlinetask-min-level 5)
  3101. (org-adapt-indentation t))
  3102. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  3103. (org-demote)
  3104. (forward-line 2)
  3105. (org-get-indentation))))))
  3106. ;; Ignore contents of source blocks or example blocks when
  3107. ;; indentation should be preserved (through
  3108. ;; `org-src-preserve-indentation' or "-i" flag).
  3109. (should-not
  3110. (zerop
  3111. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3112. (let ((org-adapt-indentation t)
  3113. (org-src-preserve-indentation nil))
  3114. (org-demote))
  3115. (forward-line 2)
  3116. (org-get-indentation))))
  3117. (should
  3118. (zerop
  3119. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3120. (let ((org-adapt-indentation t)
  3121. (org-src-preserve-indentation t))
  3122. (org-demote))
  3123. (forward-line 2)
  3124. (org-get-indentation))))
  3125. (should
  3126. (zerop
  3127. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3128. (let ((org-adapt-indentation t)
  3129. (org-src-preserve-indentation t))
  3130. (org-demote))
  3131. (forward-line 2)
  3132. (org-get-indentation))))
  3133. (should
  3134. (zerop
  3135. (org-test-with-temp-text
  3136. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  3137. (let ((org-adapt-indentation t)
  3138. (org-src-preserve-indentation nil))
  3139. (org-demote))
  3140. (forward-line 2)
  3141. (org-get-indentation)))))
  3142. (ert-deftest test-org/promote ()
  3143. "Test `org-promote' specifications."
  3144. ;; Return an error if headline is to be promoted to level 0, unless
  3145. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  3146. ;; headline becomes a comment.
  3147. (should-error
  3148. (org-test-with-temp-text "* H"
  3149. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  3150. (should
  3151. (equal "# H"
  3152. (org-test-with-temp-text "* H"
  3153. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  3154. (buffer-string))))
  3155. ;; Remove correct number of stars according to
  3156. ;; `org-odd-levels-only'.
  3157. (should
  3158. (= 2
  3159. (org-test-with-temp-text "*** H"
  3160. (let ((org-odd-levels-only nil)) (org-promote))
  3161. (org-current-level))))
  3162. (should
  3163. (= 1
  3164. (org-test-with-temp-text "*** H"
  3165. (let ((org-odd-levels-only t)) (org-promote))
  3166. (org-current-level))))
  3167. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3168. (should
  3169. (org-test-with-temp-text "** H :tag:"
  3170. (let ((org-tags-column 10)
  3171. (org-auto-align-tags t)
  3172. (org-odd-levels-only nil))
  3173. (org-promote))
  3174. (org-move-to-column 10)
  3175. (looking-at-p ":tag:$")))
  3176. (should-not
  3177. (org-test-with-temp-text "** H :tag:"
  3178. (let ((org-tags-column 10)
  3179. (org-auto-align-tags nil)
  3180. (org-odd-levels-only nil))
  3181. (org-promote))
  3182. (org-move-to-column 10)
  3183. (looking-at-p ":tag:$")))
  3184. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3185. ;; info and property drawers.
  3186. (should
  3187. (= 2
  3188. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3189. (let ((org-odd-levels-only nil)
  3190. (org-adapt-indentation t))
  3191. (org-promote))
  3192. (forward-line)
  3193. (org-get-indentation))))
  3194. (should
  3195. (= 2
  3196. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3197. (let ((org-odd-levels-only nil)
  3198. (org-adapt-indentation t))
  3199. (org-promote))
  3200. (forward-line)
  3201. (org-get-indentation))))
  3202. (should-not
  3203. (= 2
  3204. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3205. (let ((org-odd-levels-only nil)
  3206. (org-adapt-indentation nil))
  3207. (org-promote))
  3208. (forward-line)
  3209. (org-get-indentation))))
  3210. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3211. ;; section accordingly. Ignore, however, footnote definitions and
  3212. ;; inlinetasks boundaries.
  3213. (should
  3214. (= 2
  3215. (org-test-with-temp-text "** H\n Paragraph"
  3216. (let ((org-odd-levels-only nil)
  3217. (org-adapt-indentation t))
  3218. (org-promote))
  3219. (forward-line)
  3220. (org-get-indentation))))
  3221. (should-not
  3222. (= 2
  3223. (org-test-with-temp-text "** H\n Paragraph"
  3224. (let ((org-odd-levels-only nil)
  3225. (org-adapt-indentation nil))
  3226. (org-promote))
  3227. (forward-line)
  3228. (org-get-indentation))))
  3229. (should
  3230. (= 2
  3231. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  3232. (let ((org-odd-levels-only nil)
  3233. (org-adapt-indentation t))
  3234. (org-promote))
  3235. (forward-line)
  3236. (org-get-indentation))))
  3237. (when (featurep 'org-inlinetask)
  3238. (should
  3239. (zerop
  3240. (let ((org-inlinetask-min-level 5)
  3241. (org-adapt-indentation t))
  3242. (org-test-with-temp-text "** H\n***** I\n***** END"
  3243. (org-promote)
  3244. (forward-line)
  3245. (org-get-indentation))))))
  3246. (when (featurep 'org-inlinetask)
  3247. (should
  3248. (= 2
  3249. (let ((org-inlinetask-min-level 5)
  3250. (org-adapt-indentation t))
  3251. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  3252. (org-promote)
  3253. (forward-line 2)
  3254. (org-get-indentation))))))
  3255. ;; Give up shifting if it would break document's structure
  3256. ;; otherwise.
  3257. (should
  3258. (= 3
  3259. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  3260. (let ((org-odd-levels-only nil)
  3261. (org-adapt-indentation t))
  3262. (org-promote))
  3263. (forward-line)
  3264. (org-get-indentation))))
  3265. (should
  3266. (= 3
  3267. (org-test-with-temp-text "** H\n Paragraph\n * list."
  3268. (let ((org-odd-levels-only nil)
  3269. (org-adapt-indentation t))
  3270. (org-promote))
  3271. (forward-line)
  3272. (org-get-indentation))))
  3273. ;; Ignore contents of source blocks or example blocks when
  3274. ;; indentation should be preserved (through
  3275. ;; `org-src-preserve-indentation' or "-i" flag).
  3276. (should-not
  3277. (zerop
  3278. (org-test-with-temp-text
  3279. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3280. (let ((org-adapt-indentation t)
  3281. (org-src-preserve-indentation nil)
  3282. (org-odd-levels-only nil))
  3283. (org-promote))
  3284. (forward-line)
  3285. (org-get-indentation))))
  3286. (should
  3287. (zerop
  3288. (org-test-with-temp-text
  3289. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  3290. (let ((org-adapt-indentation t)
  3291. (org-src-preserve-indentation t)
  3292. (org-odd-levels-only nil))
  3293. (org-promote))
  3294. (forward-line)
  3295. (org-get-indentation))))
  3296. (should
  3297. (zerop
  3298. (org-test-with-temp-text
  3299. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3300. (let ((org-adapt-indentation t)
  3301. (org-src-preserve-indentation t)
  3302. (org-odd-levels-only nil))
  3303. (org-promote))
  3304. (forward-line)
  3305. (org-get-indentation))))
  3306. (should
  3307. (zerop
  3308. (org-test-with-temp-text
  3309. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  3310. (let ((org-adapt-indentation t)
  3311. (org-src-preserve-indentation nil)
  3312. (org-odd-levels-only nil))
  3313. (org-promote))
  3314. (forward-line)
  3315. (org-get-indentation)))))
  3316. ;;; Planning
  3317. (ert-deftest test-org/at-planning-p ()
  3318. "Test `org-at-planning-p' specifications."
  3319. ;; Regular test.
  3320. (should
  3321. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  3322. (org-at-planning-p)))
  3323. (should-not
  3324. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  3325. (org-at-planning-p)))
  3326. ;; Correctly find planning attached to inlinetasks.
  3327. (when (featurep 'org-inlinetask)
  3328. (should
  3329. (org-test-with-temp-text
  3330. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  3331. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3332. (should-not
  3333. (org-test-with-temp-text
  3334. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3335. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3336. (should-not
  3337. (org-test-with-temp-text
  3338. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3339. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3340. (should-not
  3341. (org-test-with-temp-text
  3342. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  3343. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  3344. (ert-deftest test-org/add-planning-info ()
  3345. "Test `org-add-planning-info'."
  3346. ;; Create deadline when `org-adapt-indentation' is non-nil.
  3347. (should
  3348. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3349. (org-test-with-temp-text "* H\nParagraph<point>"
  3350. (let ((org-adapt-indentation t))
  3351. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3352. (replace-regexp-in-string
  3353. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3354. nil nil 1))))
  3355. ;; Create deadline when `org-adapt-indentation' is nil.
  3356. (should
  3357. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3358. (org-test-with-temp-text "* H\nParagraph<point>"
  3359. (let ((org-adapt-indentation nil))
  3360. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3361. (replace-regexp-in-string
  3362. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3363. nil nil 1))))
  3364. ;; Update deadline when `org-adapt-indentation' is non-nil.
  3365. (should
  3366. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3367. (org-test-with-temp-text "\
  3368. * H
  3369. DEADLINE: <2015-06-24 Wed>
  3370. Paragraph<point>"
  3371. (let ((org-adapt-indentation t))
  3372. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3373. (replace-regexp-in-string
  3374. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3375. nil nil 1))))
  3376. ;; Update deadline when `org-adapt-indentation' is nil.
  3377. (should
  3378. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3379. (org-test-with-temp-text "\
  3380. * H
  3381. DEADLINE: <2015-06-24 Wed>
  3382. Paragraph<point>"
  3383. (let ((org-adapt-indentation nil))
  3384. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3385. (replace-regexp-in-string
  3386. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3387. nil nil 1))))
  3388. ;; Schedule when `org-adapt-indentation' is non-nil.
  3389. (should
  3390. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  3391. (org-test-with-temp-text "* H\nParagraph<point>"
  3392. (let ((org-adapt-indentation t))
  3393. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3394. (replace-regexp-in-string
  3395. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3396. nil nil 1))))
  3397. ;; Schedule when `org-adapt-indentation' is nil.
  3398. (should
  3399. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  3400. (org-test-with-temp-text "* H\nParagraph<point>"
  3401. (let ((org-adapt-indentation nil))
  3402. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3403. (replace-regexp-in-string
  3404. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3405. nil nil 1))))
  3406. ;; Add deadline when scheduled.
  3407. (should
  3408. (equal "\
  3409. * H
  3410. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  3411. Paragraph"
  3412. (org-test-with-temp-text "\
  3413. * H
  3414. SCHEDULED: <2015-06-24 Wed>
  3415. Paragraph<point>"
  3416. (let ((org-adapt-indentation t))
  3417. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3418. (replace-regexp-in-string
  3419. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3420. nil nil 1))))
  3421. ;; Remove middle entry.
  3422. (should
  3423. (equal "\
  3424. * H
  3425. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  3426. Paragraph"
  3427. (org-test-with-temp-text "\
  3428. * H
  3429. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3430. Paragraph<point>"
  3431. (let ((org-adapt-indentation t))
  3432. (org-add-planning-info nil nil 'deadline))
  3433. (replace-regexp-in-string
  3434. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3435. nil nil 1))))
  3436. ;; Remove last entry and then middle entry (order should not
  3437. ;; matter).
  3438. (should
  3439. (equal "\
  3440. * H
  3441. CLOSED: [2015-06-24]
  3442. Paragraph"
  3443. (org-test-with-temp-text "\
  3444. * H
  3445. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3446. Paragraph<point>"
  3447. (let ((org-adapt-indentation t))
  3448. (org-add-planning-info nil nil 'scheduled 'deadline))
  3449. (replace-regexp-in-string
  3450. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3451. nil nil 1))))
  3452. ;; Remove closed when `org-adapt-indentation' is non-nil.
  3453. (should
  3454. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3455. (org-test-with-temp-text "\
  3456. * H
  3457. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3458. Paragraph<point>"
  3459. (let ((org-adapt-indentation t))
  3460. (org-add-planning-info nil nil 'closed))
  3461. (replace-regexp-in-string
  3462. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3463. nil nil 1))))
  3464. (should
  3465. (equal "* H\n Paragraph"
  3466. (org-test-with-temp-text "\
  3467. * H
  3468. CLOSED: [2015-06-25 Thu]
  3469. Paragraph<point>"
  3470. (let ((org-adapt-indentation t))
  3471. (org-add-planning-info nil nil 'closed))
  3472. (replace-regexp-in-string
  3473. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3474. nil nil 1))))
  3475. ;; Remove closed when `org-adapt-indentation' is nil.
  3476. (should
  3477. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3478. (org-test-with-temp-text "\
  3479. * H
  3480. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3481. Paragraph<point>"
  3482. (let ((org-adapt-indentation nil))
  3483. (org-add-planning-info nil nil 'closed))
  3484. (replace-regexp-in-string
  3485. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3486. nil nil 1))))
  3487. (should
  3488. (equal "* H\nParagraph"
  3489. (org-test-with-temp-text "\
  3490. * H
  3491. CLOSED: [2015-06-25 Thu]
  3492. Paragraph<point>"
  3493. (let ((org-adapt-indentation nil))
  3494. (org-add-planning-info nil nil 'closed))
  3495. (replace-regexp-in-string
  3496. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3497. nil nil 1))))
  3498. ;; Remove closed entry and delete empty line.
  3499. (should
  3500. (equal "\
  3501. * H
  3502. Paragraph"
  3503. (org-test-with-temp-text "\
  3504. * H
  3505. CLOSED: [2015-06-24 Wed]
  3506. Paragraph<point>"
  3507. (let ((org-adapt-indentation t))
  3508. (org-add-planning-info nil nil 'closed))
  3509. (replace-regexp-in-string
  3510. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3511. nil nil 1))))
  3512. ;; Remove one entry and update another.
  3513. (should
  3514. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3515. (org-test-with-temp-text "\
  3516. * H
  3517. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  3518. Paragraph<point>"
  3519. (let ((org-adapt-indentation t))
  3520. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  3521. (replace-regexp-in-string
  3522. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3523. nil nil 1)))))
  3524. ;;; Property API
  3525. (ert-deftest test-org/buffer-property-keys ()
  3526. "Test `org-buffer-property-keys' specifications."
  3527. ;; Retrieve properties accross siblings.
  3528. (should
  3529. (equal '("A" "B")
  3530. (org-test-with-temp-text "
  3531. * H1
  3532. :PROPERTIES:
  3533. :A: 1
  3534. :END:
  3535. * H2
  3536. :PROPERTIES:
  3537. :B: 1
  3538. :END:"
  3539. (org-buffer-property-keys))))
  3540. ;; Retrieve properties accross children.
  3541. (should
  3542. (equal '("A" "B")
  3543. (org-test-with-temp-text "
  3544. * H1
  3545. :PROPERTIES:
  3546. :A: 1
  3547. :END:
  3548. ** H2
  3549. :PROPERTIES:
  3550. :B: 1
  3551. :END:"
  3552. (org-buffer-property-keys))))
  3553. ;; Retrieve muliple properties in the same drawer.
  3554. (should
  3555. (equal '("A" "B")
  3556. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3557. (org-buffer-property-keys))))
  3558. ;; Ignore extension symbol in property name.
  3559. (should
  3560. (equal '("A")
  3561. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3562. (org-buffer-property-keys))))
  3563. ;; With non-nil COLUMNS, extract property names from columns.
  3564. (should
  3565. (equal '("A" "B")
  3566. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  3567. (org-buffer-property-keys nil nil t))))
  3568. (should
  3569. (equal '("A" "B" "COLUMNS")
  3570. (org-test-with-temp-text
  3571. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  3572. (org-buffer-property-keys nil nil t))))
  3573. ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
  3574. (should
  3575. (equal '("A")
  3576. (org-test-with-temp-text
  3577. "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
  3578. (org-buffer-property-keys nil nil nil t)))))
  3579. (ert-deftest test-org/property-values ()
  3580. "Test `org-property-values' specifications."
  3581. ;; Regular test.
  3582. (should
  3583. (equal '("2" "1")
  3584. (org-test-with-temp-text
  3585. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  3586. (org-property-values "A"))))
  3587. ;; Ignore empty values.
  3588. (should-not
  3589. (org-test-with-temp-text
  3590. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  3591. (org-property-values "A")))
  3592. ;; Take into consideration extended values.
  3593. (should
  3594. (equal '("1 2")
  3595. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3596. (org-property-values "A")))))
  3597. (ert-deftest test-org/find-property ()
  3598. "Test `org-find-property' specifications."
  3599. ;; Regular test.
  3600. (should
  3601. (= 1
  3602. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  3603. (org-find-property "prop"))))
  3604. ;; Ignore false positives.
  3605. (should
  3606. (= 27
  3607. (org-test-with-temp-text
  3608. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  3609. (org-find-property "A"))))
  3610. ;; Return first entry found in buffer.
  3611. (should
  3612. (= 1
  3613. (org-test-with-temp-text
  3614. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3615. (org-find-property "A"))))
  3616. ;; Only search visible part of the buffer.
  3617. (should
  3618. (= 31
  3619. (org-test-with-temp-text
  3620. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3621. (org-narrow-to-subtree)
  3622. (org-find-property "A"))))
  3623. ;; With optional argument, only find entries with a specific value.
  3624. (should-not
  3625. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3626. (org-find-property "A" "2")))
  3627. (should
  3628. (= 31
  3629. (org-test-with-temp-text
  3630. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  3631. (org-find-property "A" "2"))))
  3632. ;; Use "nil" for explicit nil values.
  3633. (should
  3634. (= 31
  3635. (org-test-with-temp-text
  3636. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  3637. (org-find-property "A" "nil")))))
  3638. (ert-deftest test-org/entry-delete ()
  3639. "Test `org-entry-delete' specifications."
  3640. ;; Regular test.
  3641. (should
  3642. (string-match
  3643. " *:PROPERTIES:\n *:B: +2\n *:END:"
  3644. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3645. (org-entry-delete (point) "A")
  3646. (buffer-string))))
  3647. ;; Also remove accumulated properties.
  3648. (should-not
  3649. (string-match
  3650. ":A"
  3651. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  3652. (org-entry-delete (point) "A")
  3653. (buffer-string))))
  3654. ;; When last property is removed, remove the property drawer.
  3655. (should-not
  3656. (string-match
  3657. ":PROPERTIES:"
  3658. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  3659. (org-entry-delete (point) "A")
  3660. (buffer-string))))
  3661. ;; Return a non-nil value when some property was removed.
  3662. (should
  3663. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3664. (org-entry-delete (point) "A")))
  3665. (should-not
  3666. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3667. (org-entry-delete (point) "C"))))
  3668. (ert-deftest test-org/entry-get ()
  3669. "Test `org-entry-get' specifications."
  3670. ;; Regular test.
  3671. (should
  3672. (equal "1"
  3673. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3674. (org-entry-get (point) "A"))))
  3675. ;; Ignore case.
  3676. (should
  3677. (equal "1"
  3678. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3679. (org-entry-get (point) "a"))))
  3680. ;; Handle extended values, both before and after base value.
  3681. (should
  3682. (equal "1 2 3"
  3683. (org-test-with-temp-text
  3684. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3685. (org-entry-get (point) "A"))))
  3686. ;; Empty values are returned as the empty string.
  3687. (should
  3688. (equal ""
  3689. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  3690. (org-entry-get (point) "A"))))
  3691. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  3692. ;; otherwise, return nil.
  3693. (should-not
  3694. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3695. (org-entry-get (point) "A")))
  3696. (should
  3697. (equal "nil"
  3698. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3699. (org-entry-get (point) "A" nil t))))
  3700. ;; Return nil when no property is found, independently on the
  3701. ;; LITERAL-NIL argument.
  3702. (should-not
  3703. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3704. (org-entry-get (point) "B")))
  3705. (should-not
  3706. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3707. (org-entry-get (point) "B" nil t)))
  3708. ;; Handle inheritance, when allowed. Include extended values and
  3709. ;; possibly global values.
  3710. (should
  3711. (equal
  3712. "1"
  3713. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3714. (org-entry-get (point) "A" t))))
  3715. (should
  3716. (equal
  3717. "1"
  3718. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3719. (let ((org-use-property-inheritance t))
  3720. (org-entry-get (point) "A" 'selective)))))
  3721. (should-not
  3722. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3723. (let ((org-use-property-inheritance nil))
  3724. (org-entry-get (point) "A" 'selective))))
  3725. (should
  3726. (equal
  3727. "1 2"
  3728. (org-test-with-temp-text
  3729. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  3730. (org-entry-get (point-max) "A" t))))
  3731. (should
  3732. (equal "1"
  3733. (org-test-with-temp-text
  3734. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  3735. (org-mode-restart)
  3736. (org-entry-get (point-max) "A" t))))
  3737. (should
  3738. (equal "0 1"
  3739. (org-test-with-temp-text
  3740. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  3741. (org-mode-restart)
  3742. (org-entry-get (point-max) "A" t)))))
  3743. (ert-deftest test-org/entry-properties ()
  3744. "Test `org-entry-properties' specifications."
  3745. ;; Get "ITEM" property.
  3746. (should
  3747. (equal "H"
  3748. (org-test-with-temp-text "* TODO H"
  3749. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  3750. (should
  3751. (equal "H"
  3752. (org-test-with-temp-text "* TODO H"
  3753. (cdr (assoc "ITEM" (org-entry-properties))))))
  3754. ;; Get "TODO" property. TODO keywords are case sensitive.
  3755. (should
  3756. (equal "TODO"
  3757. (org-test-with-temp-text "* TODO H"
  3758. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  3759. (should
  3760. (equal "TODO"
  3761. (org-test-with-temp-text "* TODO H"
  3762. (cdr (assoc "TODO" (org-entry-properties))))))
  3763. (should-not
  3764. (org-test-with-temp-text "* H"
  3765. (assoc "TODO" (org-entry-properties nil "TODO"))))
  3766. (should-not
  3767. (org-test-with-temp-text "* todo H"
  3768. (assoc "TODO" (org-entry-properties nil "TODO"))))
  3769. ;; Get "PRIORITY" property.
  3770. (should
  3771. (equal "A"
  3772. (org-test-with-temp-text "* [#A] H"
  3773. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  3774. (should
  3775. (equal "A"
  3776. (org-test-with-temp-text "* [#A] H"
  3777. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  3778. (should
  3779. (equal (char-to-string org-default-priority)
  3780. (org-test-with-temp-text "* H"
  3781. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  3782. ;; Get "FILE" property.
  3783. (should
  3784. (org-test-with-temp-text-in-file "* H\nParagraph"
  3785. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  3786. (buffer-file-name))))
  3787. (should
  3788. (org-test-with-temp-text-in-file "* H\nParagraph"
  3789. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  3790. (buffer-file-name))))
  3791. (should-not
  3792. (org-test-with-temp-text "* H\nParagraph"
  3793. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  3794. ;; Get "TAGS" property.
  3795. (should
  3796. (equal ":tag1:tag2:"
  3797. (org-test-with-temp-text "* H :tag1:tag2:"
  3798. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  3799. (should
  3800. (equal ":tag1:tag2:"
  3801. (org-test-with-temp-text "* H :tag1:tag2:"
  3802. (cdr (assoc "TAGS" (org-entry-properties))))))
  3803. (should-not
  3804. (org-test-with-temp-text "* H"
  3805. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  3806. ;; Get "ALLTAGS" property.
  3807. (should
  3808. (equal ":tag1:tag2:"
  3809. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  3810. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  3811. (should
  3812. (equal ":tag1:tag2:"
  3813. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  3814. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  3815. (should-not
  3816. (org-test-with-temp-text "* H"
  3817. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  3818. ;; Get "BLOCKED" property.
  3819. (should
  3820. (equal "t"
  3821. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  3822. (let ((org-enforce-todo-dependencies t)
  3823. (org-blocker-hook
  3824. '(org-block-todo-from-children-or-siblings-or-parent)))
  3825. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  3826. (should
  3827. (equal ""
  3828. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  3829. (let ((org-enforce-todo-dependencies t)
  3830. (org-blocker-hook
  3831. '(org-block-todo-from-children-or-siblings-or-parent)))
  3832. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  3833. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  3834. (should
  3835. (equal
  3836. "[2012-03-29 thu.]"
  3837. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  3838. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  3839. (should
  3840. (equal
  3841. "[2012-03-29 thu.]"
  3842. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  3843. (cdr (assoc "CLOSED" (org-entry-properties))))))
  3844. (should-not
  3845. (org-test-with-temp-text "* H"
  3846. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  3847. (should
  3848. (equal
  3849. "<2014-03-04 tue.>"
  3850. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3851. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  3852. (should
  3853. (equal
  3854. "<2014-03-04 tue.>"
  3855. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3856. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  3857. (should-not
  3858. (org-test-with-temp-text "* H"
  3859. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  3860. (should
  3861. (equal
  3862. "<2014-03-04 tue.>"
  3863. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3864. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  3865. (should
  3866. (equal
  3867. "<2014-03-04 tue.>"
  3868. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3869. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  3870. (should-not
  3871. (org-test-with-temp-text "* H"
  3872. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  3873. ;; Get "CATEGORY"
  3874. (should
  3875. (equal "cat"
  3876. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  3877. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  3878. (should
  3879. (equal "cat"
  3880. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  3881. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3882. (should
  3883. (equal "cat"
  3884. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  3885. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3886. (should
  3887. (equal "cat2"
  3888. (org-test-with-temp-text
  3889. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  3890. "\n"
  3891. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  3892. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3893. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  3894. (should
  3895. (equal "<2012-03-29 thu.>"
  3896. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  3897. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  3898. (should
  3899. (equal "[2012-03-29 thu.]"
  3900. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  3901. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  3902. (should
  3903. (equal "<2012-03-29 thu.>"
  3904. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  3905. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  3906. (should
  3907. (equal "[2014-03-04 tue.]"
  3908. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  3909. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
  3910. ;; Get standard properties.
  3911. (should
  3912. (equal "1"
  3913. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3914. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3915. ;; Handle extended properties.
  3916. (should
  3917. (equal "1 2 3"
  3918. (org-test-with-temp-text
  3919. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3920. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3921. (should
  3922. (equal "1 2 3"
  3923. (org-test-with-temp-text
  3924. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  3925. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3926. ;; Ignore forbidden (special) properties.
  3927. (should-not
  3928. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  3929. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  3930. (ert-deftest test-org/entry-put ()
  3931. "Test `org-entry-put' specifications."
  3932. ;; Error when not a string or nil.
  3933. (should-error
  3934. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  3935. (org-entry-put 1 "test" 2)))
  3936. ;; Error when property name is invalid.
  3937. (should-error
  3938. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  3939. (org-entry-put 1 "no space" "value")))
  3940. (should-error
  3941. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  3942. (org-entry-put 1 "" "value")))
  3943. ;; Set "TODO" property.
  3944. (should
  3945. (string-match (regexp-quote " TODO H")
  3946. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  3947. (org-entry-put (point) "TODO" "TODO")
  3948. (buffer-string))))
  3949. (should
  3950. (string-match (regexp-quote "* H")
  3951. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  3952. (org-entry-put (point) "TODO" nil)
  3953. (buffer-string))))
  3954. ;; Set "PRIORITY" property.
  3955. (should
  3956. (equal "* [#A] H"
  3957. (org-test-with-temp-text "* [#B] H"
  3958. (org-entry-put (point) "PRIORITY" "A")
  3959. (buffer-string))))
  3960. (should
  3961. (equal "* H"
  3962. (org-test-with-temp-text "* [#B] H"
  3963. (org-entry-put (point) "PRIORITY" nil)
  3964. (buffer-string))))
  3965. ;; Set "SCHEDULED" property.
  3966. (should
  3967. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  3968. (org-test-with-temp-text "* H"
  3969. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  3970. (buffer-string))))
  3971. (should
  3972. (string= "* H\n"
  3973. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3974. (org-entry-put (point) "SCHEDULED" nil)
  3975. (buffer-string))))
  3976. (should
  3977. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  3978. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3979. (org-entry-put (point) "SCHEDULED" "earlier")
  3980. (buffer-string))))
  3981. (should
  3982. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  3983. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3984. (org-entry-put (point) "SCHEDULED" "later")
  3985. (buffer-string))))
  3986. ;; Set "DEADLINE" property.
  3987. (should
  3988. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  3989. (org-test-with-temp-text "* H"
  3990. (org-entry-put (point) "DEADLINE" "2014-03-04")
  3991. (buffer-string))))
  3992. (should
  3993. (string= "* H\n"
  3994. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3995. (org-entry-put (point) "DEADLINE" nil)
  3996. (buffer-string))))
  3997. (should
  3998. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  3999. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4000. (org-entry-put (point) "DEADLINE" "earlier")
  4001. (buffer-string))))
  4002. (should
  4003. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  4004. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4005. (org-entry-put (point) "DEADLINE" "later")
  4006. (buffer-string))))
  4007. ;; Set "CATEGORY" property
  4008. (should
  4009. (string-match "^ *:CATEGORY: cat"
  4010. (org-test-with-temp-text "* H"
  4011. (org-entry-put (point) "CATEGORY" "cat")
  4012. (buffer-string))))
  4013. ;; Regular properties, with or without pre-existing drawer.
  4014. (should
  4015. (string-match "^ *:A: +2$"
  4016. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4017. (org-entry-put (point) "A" "2")
  4018. (buffer-string))))
  4019. (should
  4020. (string-match "^ *:A: +1$"
  4021. (org-test-with-temp-text "* H"
  4022. (org-entry-put (point) "A" "1")
  4023. (buffer-string))))
  4024. ;; Special case: two consecutive headlines.
  4025. (should
  4026. (string-match "\\* A\n *:PROPERTIES:"
  4027. (org-test-with-temp-text "* A\n** B"
  4028. (org-entry-put (point) "A" "1")
  4029. (buffer-string)))))
  4030. ;;; Radio Targets
  4031. (ert-deftest test-org/update-radio-target-regexp ()
  4032. "Test `org-update-radio-target-regexp' specifications."
  4033. ;; Properly update cache with no previous radio target regexp.
  4034. (should
  4035. (eq 'link
  4036. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4037. (save-excursion (goto-char (point-max)) (org-element-context))
  4038. (insert "<<<")
  4039. (search-forward "o")
  4040. (insert ">>>")
  4041. (org-update-radio-target-regexp)
  4042. (goto-char (point-max))
  4043. (org-element-type (org-element-context)))))
  4044. ;; Properly update cache with previous radio target regexp.
  4045. (should
  4046. (eq 'link
  4047. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4048. (save-excursion (goto-char (point-max)) (org-element-context))
  4049. (insert "<<<")
  4050. (search-forward "o")
  4051. (insert ">>>")
  4052. (org-update-radio-target-regexp)
  4053. (search-backward "r")
  4054. (delete-char 5)
  4055. (insert "new")
  4056. (org-update-radio-target-regexp)
  4057. (goto-char (point-max))
  4058. (delete-region (line-beginning-position) (point))
  4059. (insert "new")
  4060. (org-element-type (org-element-context))))))
  4061. ;;; Sparse trees
  4062. (ert-deftest test-org/match-sparse-tree ()
  4063. "Test `org-match-sparse-tree' specifications."
  4064. ;; Match tags.
  4065. (should-not
  4066. (org-test-with-temp-text "* H\n** H1 :tag:"
  4067. (org-match-sparse-tree nil "tag")
  4068. (search-forward "H1")
  4069. (org-invisible-p2)))
  4070. (should
  4071. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  4072. (org-match-sparse-tree nil "tag")
  4073. (search-forward "H2")
  4074. (org-invisible-p2)))
  4075. ;; "-" operator for tags.
  4076. (should-not
  4077. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4078. (org-match-sparse-tree nil "tag1-tag2")
  4079. (search-forward "H1")
  4080. (org-invisible-p2)))
  4081. (should
  4082. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4083. (org-match-sparse-tree nil "tag1-tag2")
  4084. (search-forward "H2")
  4085. (org-invisible-p2)))
  4086. ;; "&" operator for tags.
  4087. (should
  4088. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4089. (org-match-sparse-tree nil "tag1&tag2")
  4090. (search-forward "H1")
  4091. (org-invisible-p2)))
  4092. (should-not
  4093. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4094. (org-match-sparse-tree nil "tag1&tag2")
  4095. (search-forward "H2")
  4096. (org-invisible-p2)))
  4097. ;; "|" operator for tags.
  4098. (should-not
  4099. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4100. (org-match-sparse-tree nil "tag1|tag2")
  4101. (search-forward "H1")
  4102. (org-invisible-p2)))
  4103. (should-not
  4104. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4105. (org-match-sparse-tree nil "tag1|tag2")
  4106. (search-forward "H2")
  4107. (org-invisible-p2)))
  4108. ;; Regexp match on tags.
  4109. (should-not
  4110. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4111. (org-match-sparse-tree nil "{^tag.*}")
  4112. (search-forward "H1")
  4113. (org-invisible-p2)))
  4114. (should
  4115. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4116. (org-match-sparse-tree nil "{^tag.*}")
  4117. (search-forward "H2")
  4118. (org-invisible-p2)))
  4119. ;; Match group tags.
  4120. (should-not
  4121. (org-test-with-temp-text
  4122. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4123. (org-match-sparse-tree nil "work")
  4124. (search-forward "H1")
  4125. (org-invisible-p2)))
  4126. (should-not
  4127. (org-test-with-temp-text
  4128. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4129. (org-match-sparse-tree nil "work")
  4130. (search-forward "H2")
  4131. (org-invisible-p2)))
  4132. ;; Match group tags with hard brackets.
  4133. (should-not
  4134. (org-test-with-temp-text
  4135. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4136. (org-match-sparse-tree nil "work")
  4137. (search-forward "H1")
  4138. (org-invisible-p2)))
  4139. (should-not
  4140. (org-test-with-temp-text
  4141. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4142. (org-match-sparse-tree nil "work")
  4143. (search-forward "H2")
  4144. (org-invisible-p2)))
  4145. ;; Match tags in hierarchies
  4146. (should-not
  4147. (org-test-with-temp-text
  4148. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  4149. #+TAGS: [ Lev_2 : Lev_3 ]\n
  4150. #+TAGS: { Lev_3 : Lev_4 }\n
  4151. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  4152. (org-match-sparse-tree nil "Lev_1")
  4153. (search-forward "H4")
  4154. (org-invisible-p2)))
  4155. ;; Match regular expressions in tags
  4156. (should-not
  4157. (org-test-with-temp-text
  4158. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  4159. (org-match-sparse-tree nil "Lev")
  4160. (search-forward "H1")
  4161. (org-invisible-p2)))
  4162. (should
  4163. (org-test-with-temp-text
  4164. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  4165. (org-match-sparse-tree nil "Lev")
  4166. (search-forward "H1")
  4167. (org-invisible-p2)))
  4168. ;; Match properties.
  4169. (should
  4170. (org-test-with-temp-text
  4171. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  4172. (org-match-sparse-tree nil "A=\"1\"")
  4173. (search-forward "H2")
  4174. (org-invisible-p2)))
  4175. (should-not
  4176. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4177. (org-match-sparse-tree nil "A=\"1\"")
  4178. (search-forward "H2")
  4179. (org-invisible-p2)))
  4180. ;; Case is not significant when matching properties.
  4181. (should-not
  4182. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4183. (org-match-sparse-tree nil "a=\"1\"")
  4184. (search-forward "H2")
  4185. (org-invisible-p2)))
  4186. (should-not
  4187. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  4188. (org-match-sparse-tree nil "A=\"1\"")
  4189. (search-forward "H2")
  4190. (org-invisible-p2)))
  4191. ;; Match special LEVEL property.
  4192. (should-not
  4193. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4194. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4195. (search-forward "H1")
  4196. (org-invisible-p2)))
  4197. (should
  4198. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4199. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4200. (search-forward "H2")
  4201. (org-invisible-p2)))
  4202. ;; Comparison operators when matching properties.
  4203. (should
  4204. (org-test-with-temp-text
  4205. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4206. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4207. (search-forward "H1")
  4208. (org-invisible-p2)))
  4209. (should-not
  4210. (org-test-with-temp-text
  4211. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4212. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4213. (search-forward "H2")
  4214. (org-invisible-p2)))
  4215. ;; Regexp match on properties values.
  4216. (should-not
  4217. (org-test-with-temp-text
  4218. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4219. (org-match-sparse-tree nil "A={f.*}")
  4220. (search-forward "H1")
  4221. (org-invisible-p2)))
  4222. (should
  4223. (org-test-with-temp-text
  4224. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4225. (org-match-sparse-tree nil "A={f.*}")
  4226. (search-forward "H2")
  4227. (org-invisible-p2)))
  4228. ;; With an optional argument, limit match to TODO entries.
  4229. (should-not
  4230. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4231. (org-match-sparse-tree t "tag")
  4232. (search-forward "H1")
  4233. (org-invisible-p2)))
  4234. (should
  4235. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4236. (org-match-sparse-tree t "tag")
  4237. (search-forward "H2")
  4238. (org-invisible-p2))))
  4239. (ert-deftest test-org/occur ()
  4240. "Test `org-occur' specifications."
  4241. ;; Count number of matches.
  4242. (should
  4243. (= 1
  4244. (org-test-with-temp-text "* H\nA\n* H2"
  4245. (org-occur "A"))))
  4246. (should
  4247. (= 2
  4248. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4249. (org-occur "A"))))
  4250. ;; Test CALLBACK optional argument.
  4251. (should
  4252. (= 0
  4253. (org-test-with-temp-text "* H\nA\n* H2"
  4254. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  4255. (should
  4256. (= 1
  4257. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4258. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  4259. ;; Case-fold searches according to `org-occur-case-fold-search'.
  4260. (should
  4261. (= 2
  4262. (org-test-with-temp-text "Aa"
  4263. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  4264. (should
  4265. (= 2
  4266. (org-test-with-temp-text "Aa"
  4267. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  4268. (should
  4269. (= 1
  4270. (org-test-with-temp-text "Aa"
  4271. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  4272. (should
  4273. (= 1
  4274. (org-test-with-temp-text "Aa"
  4275. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  4276. (should
  4277. (= 1
  4278. (org-test-with-temp-text "Aa"
  4279. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  4280. (should
  4281. (= 2
  4282. (org-test-with-temp-text "Aa"
  4283. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  4284. ;;; Tags
  4285. (ert-deftest test-org/tag-string-to-alist ()
  4286. "Test `org-tag-string-to-alist' specifications."
  4287. ;; Tag without selection key.
  4288. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  4289. ;; Tag with selection key.
  4290. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  4291. ;; Tag group.
  4292. (should
  4293. (equal
  4294. (org-tag-string-to-alist "[ group : t1 t2 ]")
  4295. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  4296. ;; Mutually exclusive tags.
  4297. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  4298. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  4299. (should
  4300. (equal
  4301. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  4302. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  4303. (ert-deftest test-org/tag-alist-to-string ()
  4304. "Test `org-tag-alist-to-string' specifications."
  4305. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  4306. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  4307. (should
  4308. (equal
  4309. (org-tag-alist-to-string
  4310. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  4311. "[ group : t1 t2 ]"))
  4312. (should
  4313. (equal (org-tag-alist-to-string
  4314. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  4315. "{ tag1 tag2 }"))
  4316. (should
  4317. (equal
  4318. (org-tag-alist-to-string
  4319. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  4320. "{ group : tag1 tag2 }")))
  4321. (ert-deftest test-org/tag-alist-to-groups ()
  4322. "Test `org-tag-alist-to-groups' specifications."
  4323. (should
  4324. (equal (org-tag-alist-to-groups
  4325. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  4326. '(("group" "t1" "t2"))))
  4327. (should
  4328. (equal
  4329. (org-tag-alist-to-groups
  4330. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  4331. '(("group" "t1" "t2"))))
  4332. (should-not
  4333. (org-tag-alist-to-groups
  4334. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  4335. (ert-deftest test-org/tag-align ()
  4336. "Test `org-align-tags-here' with different display width."
  4337. (should
  4338. ;; 12345678901234567890
  4339. (equal "* Test :abc:"
  4340. (org-test-with-temp-text "* Test :abc:"
  4341. (let ((org-tags-column -20)
  4342. (indent-tabs-mode nil))
  4343. (org-fix-tags-on-the-fly))
  4344. (buffer-string))))
  4345. (should
  4346. ;; 12345678901234567890
  4347. (equal "* Test :日本語:"
  4348. (org-test-with-temp-text "* Test :日本語:"
  4349. (let ((org-tags-column -20)
  4350. (indent-tabs-mode nil))
  4351. (org-fix-tags-on-the-fly))
  4352. (buffer-string)))))
  4353. (ert-deftest test-org/tags-at ()
  4354. (should
  4355. (equal '("foo" "bar")
  4356. (org-test-with-temp-text
  4357. "* T<point>est :foo:bar:"
  4358. (org-get-tags-at)))))
  4359. ;;; Timestamps API
  4360. (ert-deftest test-org/time-stamp ()
  4361. "Test `org-time-stamp' specifications."
  4362. ;; Insert chosen time stamp at point.
  4363. (should
  4364. (string-match
  4365. "Te<2014-03-04 .*?>xt"
  4366. (org-test-with-temp-text "Te<point>xt"
  4367. (cl-letf (((symbol-function 'org-read-date)
  4368. (lambda (&rest args)
  4369. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4370. (org-time-stamp nil)
  4371. (buffer-string)))))
  4372. ;; With a prefix argument, also insert time.
  4373. (should
  4374. (string-match
  4375. "Te<2014-03-04 .*? 00:41>xt"
  4376. (org-test-with-temp-text "Te<point>xt"
  4377. (cl-letf (((symbol-function 'org-read-date)
  4378. (lambda (&rest args)
  4379. (apply #'encode-time
  4380. (org-parse-time-string "2014-03-04 00:41")))))
  4381. (org-time-stamp '(4))
  4382. (buffer-string)))))
  4383. ;; With two universal prefix arguments, insert an active timestamp
  4384. ;; with the current time without prompting the user.
  4385. (should
  4386. (string-match
  4387. "Te<2014-03-04 .*? 00:41>xt"
  4388. (org-test-with-temp-text "Te<point>xt"
  4389. (cl-letf (((symbol-function 'current-time)
  4390. (lambda ()
  4391. (apply #'encode-time
  4392. (org-parse-time-string "2014-03-04 00:41")))))
  4393. (org-time-stamp '(16))
  4394. (buffer-string)))))
  4395. ;; When optional argument is non-nil, insert an inactive timestamp.
  4396. (should
  4397. (string-match
  4398. "Te\\[2014-03-04 .*?\\]xt"
  4399. (org-test-with-temp-text "Te<point>xt"
  4400. (cl-letf (((symbol-function 'org-read-date)
  4401. (lambda (&rest args)
  4402. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4403. (org-time-stamp nil t)
  4404. (buffer-string)))))
  4405. ;; When called from a timestamp, replace existing one.
  4406. (should
  4407. (string-match
  4408. "<2014-03-04 .*?>"
  4409. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  4410. (cl-letf (((symbol-function 'org-read-date)
  4411. (lambda (&rest args)
  4412. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4413. (org-time-stamp nil)
  4414. (buffer-string)))))
  4415. (should
  4416. (string-match
  4417. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  4418. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  4419. (cl-letf (((symbol-function 'org-read-date)
  4420. (lambda (&rest args)
  4421. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4422. (org-time-stamp nil)
  4423. (buffer-string)))))
  4424. ;; When replacing a timestamp, preserve repeater, if any.
  4425. (should
  4426. (string-match
  4427. "<2014-03-04 .*? \\+2y>"
  4428. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  4429. (cl-letf (((symbol-function 'org-read-date)
  4430. (lambda (&rest args)
  4431. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4432. (org-time-stamp nil)
  4433. (buffer-string)))))
  4434. ;; When called twice in a raw, build a date range.
  4435. (should
  4436. (string-match
  4437. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  4438. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  4439. (cl-letf (((symbol-function 'org-read-date)
  4440. (lambda (&rest args)
  4441. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4442. (let ((last-command 'org-time-stamp)
  4443. (this-command 'org-time-stamp))
  4444. (org-time-stamp nil))
  4445. (buffer-string))))))
  4446. (ert-deftest test-org/timestamp-has-time-p ()
  4447. "Test `org-timestamp-has-time-p' specifications."
  4448. ;; With time.
  4449. (should
  4450. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  4451. (org-timestamp-has-time-p (org-element-context))))
  4452. ;; Without time.
  4453. (should-not
  4454. (org-test-with-temp-text "<2012-03-29 Thu>"
  4455. (org-timestamp-has-time-p (org-element-context)))))
  4456. (ert-deftest test-org/timestamp-format ()
  4457. "Test `org-timestamp-format' specifications."
  4458. ;; Regular test.
  4459. (should
  4460. (equal
  4461. "2012-03-29 16:40"
  4462. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  4463. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  4464. ;; Range end.
  4465. (should
  4466. (equal
  4467. "2012-03-29"
  4468. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  4469. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  4470. (ert-deftest test-org/timestamp-split-range ()
  4471. "Test `org-timestamp-split-range' specifications."
  4472. ;; Extract range start (active).
  4473. (should
  4474. (equal '(2012 3 29)
  4475. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4476. (let ((ts (org-timestamp-split-range (org-element-context))))
  4477. (mapcar (lambda (p) (org-element-property p ts))
  4478. '(:year-end :month-end :day-end))))))
  4479. ;; Extract range start (inactive)
  4480. (should
  4481. (equal '(2012 3 29)
  4482. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  4483. (let ((ts (org-timestamp-split-range (org-element-context))))
  4484. (mapcar (lambda (p) (org-element-property p ts))
  4485. '(:year-end :month-end :day-end))))))
  4486. ;; Extract range end (active).
  4487. (should
  4488. (equal '(2012 3 30)
  4489. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4490. (let ((ts (org-timestamp-split-range
  4491. (org-element-context) t)))
  4492. (mapcar (lambda (p) (org-element-property p ts))
  4493. '(:year-end :month-end :day-end))))))
  4494. ;; Extract range end (inactive)
  4495. (should
  4496. (equal '(2012 3 30)
  4497. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  4498. (let ((ts (org-timestamp-split-range
  4499. (org-element-context) t)))
  4500. (mapcar (lambda (p) (org-element-property p ts))
  4501. '(:year-end :month-end :day-end))))))
  4502. ;; Return the timestamp if not a range.
  4503. (should
  4504. (org-test-with-temp-text "[2012-03-29 Thu]"
  4505. (let* ((ts-orig (org-element-context))
  4506. (ts-copy (org-timestamp-split-range ts-orig)))
  4507. (eq ts-orig ts-copy))))
  4508. (should
  4509. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  4510. (let* ((ts-orig (org-element-context))
  4511. (ts-copy (org-timestamp-split-range ts-orig)))
  4512. (eq ts-orig ts-copy)))))
  4513. (ert-deftest test-org/timestamp-translate ()
  4514. "Test `org-timestamp-translate' specifications."
  4515. ;; Translate whole date range.
  4516. (should
  4517. (equal "<29>--<30>"
  4518. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4519. (let ((org-display-custom-times t)
  4520. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4521. (org-timestamp-translate (org-element-context))))))
  4522. ;; Translate date range start.
  4523. (should
  4524. (equal "<29>"
  4525. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4526. (let ((org-display-custom-times t)
  4527. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4528. (org-timestamp-translate (org-element-context) 'start)))))
  4529. ;; Translate date range end.
  4530. (should
  4531. (equal "<30>"
  4532. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4533. (let ((org-display-custom-times t)
  4534. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4535. (org-timestamp-translate (org-element-context) 'end)))))
  4536. ;; Translate time range.
  4537. (should
  4538. (equal "<08>--<16>"
  4539. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  4540. (let ((org-display-custom-times t)
  4541. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  4542. (org-timestamp-translate (org-element-context))))))
  4543. ;; Translate non-range timestamp.
  4544. (should
  4545. (equal "<29>"
  4546. (org-test-with-temp-text "<2012-03-29 Thu>"
  4547. (let ((org-display-custom-times t)
  4548. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4549. (org-timestamp-translate (org-element-context))))))
  4550. ;; Do not change `diary' timestamps.
  4551. (should
  4552. (equal "<%%(org-float t 4 2)>"
  4553. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  4554. (let ((org-display-custom-times t)
  4555. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4556. (org-timestamp-translate (org-element-context)))))))
  4557. ;;; Visibility
  4558. (ert-deftest test-org/flag-drawer ()
  4559. "Test `org-flag-drawer' specifications."
  4560. ;; Hide drawer.
  4561. (should
  4562. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4563. (org-flag-drawer t)
  4564. (get-char-property (line-end-position) 'invisible)))
  4565. ;; Show drawer.
  4566. (should-not
  4567. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4568. (org-flag-drawer t)
  4569. (org-flag-drawer nil)
  4570. (get-char-property (line-end-position) 'invisible)))
  4571. ;; Test optional argument.
  4572. (should
  4573. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4574. (let ((drawer (save-excursion (search-forward ":D2")
  4575. (org-element-at-point))))
  4576. (org-flag-drawer t drawer)
  4577. (get-char-property (progn (search-forward ":D2") (line-end-position))
  4578. 'invisible))))
  4579. (should-not
  4580. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4581. (let ((drawer (save-excursion (search-forward ":D2")
  4582. (org-element-at-point))))
  4583. (org-flag-drawer t drawer)
  4584. (get-char-property (line-end-position) 'invisible))))
  4585. ;; Do not hide fake drawers.
  4586. (should-not
  4587. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  4588. (forward-line 1)
  4589. (org-flag-drawer t)
  4590. (get-char-property (line-end-position) 'invisible)))
  4591. ;; Do not hide incomplete drawers.
  4592. (should-not
  4593. (org-test-with-temp-text ":D:\nparagraph"
  4594. (forward-line 1)
  4595. (org-flag-drawer t)
  4596. (get-char-property (line-end-position) 'invisible)))
  4597. ;; Do not hide drawers when called from final blank lines.
  4598. (should-not
  4599. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  4600. (goto-char (point-max))
  4601. (org-flag-drawer t)
  4602. (goto-char (point-min))
  4603. (get-char-property (line-end-position) 'invisible)))
  4604. ;; Don't leave point in an invisible part of the buffer when hiding
  4605. ;; a drawer away.
  4606. (should-not
  4607. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4608. (goto-char (point-max))
  4609. (org-flag-drawer t)
  4610. (get-char-property (point) 'invisible))))
  4611. (ert-deftest test-org/hide-block-toggle ()
  4612. "Test `org-hide-block-toggle' specifications."
  4613. ;; Error when not at a block.
  4614. (should-error
  4615. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  4616. (org-hide-block-toggle 'off)
  4617. (get-char-property (line-end-position) 'invisible)))
  4618. ;; Hide block.
  4619. (should
  4620. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  4621. (org-hide-block-toggle)
  4622. (get-char-property (line-end-position) 'invisible)))
  4623. (should
  4624. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  4625. (org-hide-block-toggle)
  4626. (get-char-property (line-end-position) 'invisible)))
  4627. ;; Show block unconditionally when optional argument is `off'.
  4628. (should-not
  4629. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4630. (org-hide-block-toggle)
  4631. (org-hide-block-toggle 'off)
  4632. (get-char-property (line-end-position) 'invisible)))
  4633. (should-not
  4634. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4635. (org-hide-block-toggle 'off)
  4636. (get-char-property (line-end-position) 'invisible)))
  4637. ;; Hide block unconditionally when optional argument is non-nil.
  4638. (should
  4639. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4640. (org-hide-block-toggle t)
  4641. (get-char-property (line-end-position) 'invisible)))
  4642. (should
  4643. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4644. (org-hide-block-toggle)
  4645. (org-hide-block-toggle t)
  4646. (get-char-property (line-end-position) 'invisible)))
  4647. ;; Do not hide block when called from final blank lines.
  4648. (should-not
  4649. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  4650. (org-hide-block-toggle)
  4651. (goto-char (point-min))
  4652. (get-char-property (line-end-position) 'invisible)))
  4653. ;; Don't leave point in an invisible part of the buffer when hiding
  4654. ;; a block away.
  4655. (should-not
  4656. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  4657. (org-hide-block-toggle)
  4658. (get-char-property (point) 'invisible))))
  4659. (ert-deftest test-org/hide-block-toggle-maybe ()
  4660. "Test `org-hide-block-toggle-maybe' specifications."
  4661. (should
  4662. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  4663. (org-hide-block-toggle-maybe)))
  4664. (should-not
  4665. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  4666. (ert-deftest test-org/show-set-visibility ()
  4667. "Test `org-show-set-visibility' specifications."
  4668. ;; Do not throw an error before first heading.
  4669. (should
  4670. (org-test-with-temp-text "Preamble\n* Headline"
  4671. (org-show-set-visibility 'tree)
  4672. t))
  4673. ;; Test all visibility spans, both on headline and in entry.
  4674. (let ((list-visible-lines
  4675. (lambda (state headerp)
  4676. (org-test-with-temp-text "* Grandmother (0)
  4677. ** Uncle (1)
  4678. *** Heir (2)
  4679. ** Father (3)
  4680. Ancestor text (4)
  4681. *** Sister (5)
  4682. Sibling text (6)
  4683. *** Self (7)
  4684. Match (8)
  4685. **** First born (9)
  4686. Child text (10)
  4687. **** The other child (11)
  4688. *** Brother (12)
  4689. ** Aunt (13)
  4690. "
  4691. (org-cycle t)
  4692. (search-forward (if headerp "Self" "Match"))
  4693. (org-show-set-visibility state)
  4694. (goto-char (point-min))
  4695. (let (result (line 0))
  4696. (while (not (eobp))
  4697. (unless (org-invisible-p2) (push line result))
  4698. (incf line)
  4699. (forward-line))
  4700. (nreverse result))))))
  4701. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  4702. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  4703. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  4704. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  4705. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  4706. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  4707. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  4708. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  4709. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  4710. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  4711. (funcall list-visible-lines 'tree nil)))
  4712. (should (equal '(0 1 3 4 5 7 12 13)
  4713. (funcall list-visible-lines 'canonical t)))
  4714. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  4715. (funcall list-visible-lines 'canonical nil))))
  4716. ;; When point is hidden in a drawer or a block, make sure to make it
  4717. ;; visible.
  4718. (should-not
  4719. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  4720. (org-hide-block-toggle)
  4721. (search-forward "Text")
  4722. (org-show-set-visibility 'minimal)
  4723. (org-invisible-p2)))
  4724. (should-not
  4725. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  4726. (org-flag-drawer t)
  4727. (search-forward "Text")
  4728. (org-show-set-visibility 'minimal)
  4729. (org-invisible-p2)))
  4730. (should-not
  4731. (org-test-with-temp-text
  4732. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  4733. (org-flag-drawer t)
  4734. (forward-line -1)
  4735. (org-hide-block-toggle)
  4736. (search-forward "Text")
  4737. (org-show-set-visibility 'minimal)
  4738. (org-invisible-p2))))
  4739. (provide 'test-org)
  4740. ;;; test-org.el ends here