test-org.el 164 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073
  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. ;; At the beginning of a headline, create one above.
  1135. (should
  1136. (equal "* \n* H"
  1137. (org-test-with-temp-text "* H"
  1138. (org-insert-heading)
  1139. (buffer-string))))
  1140. ;; In the middle of a headline, split it if allowed.
  1141. (should
  1142. (equal "* H\n* 1"
  1143. (org-test-with-temp-text "* H<point>1"
  1144. (let ((org-M-RET-may-split-line '((headline . t))))
  1145. (org-insert-heading))
  1146. (buffer-string))))
  1147. (should
  1148. (equal "* H1\n* "
  1149. (org-test-with-temp-text "* H<point>1"
  1150. (let ((org-M-RET-may-split-line '((headline . nil))))
  1151. (org-insert-heading))
  1152. (buffer-string))))
  1153. ;; However, splitting cannot happen on TODO keywords, priorities or
  1154. ;; tags.
  1155. (should
  1156. (equal "* TODO H1\n* "
  1157. (org-test-with-temp-text "* TO<point>DO H1"
  1158. (let ((org-M-RET-may-split-line '((headline . t))))
  1159. (org-insert-heading))
  1160. (buffer-string))))
  1161. (should
  1162. (equal "* [#A] H1\n* "
  1163. (org-test-with-temp-text "* [#<point>A] H1"
  1164. (let ((org-M-RET-may-split-line '((headline . t))))
  1165. (org-insert-heading))
  1166. (buffer-string))))
  1167. (should
  1168. (equal "* H1 :tag:\n* "
  1169. (org-test-with-temp-text "* H1 :ta<point>g:"
  1170. (let ((org-M-RET-may-split-line '((headline . t))))
  1171. (org-insert-heading))
  1172. (buffer-string))))
  1173. ;; When on a list, insert an item instead, unless called with an
  1174. ;; universal argument or if list is invisible. In this case, create
  1175. ;; a new headline after contents.
  1176. (should
  1177. (equal "* H\n- item\n- "
  1178. (org-test-with-temp-text "* H\n- item<point>"
  1179. (let ((org-insert-heading-respect-content nil))
  1180. (org-insert-heading))
  1181. (buffer-string))))
  1182. (should
  1183. (equal "* H\n- item\n- item 2\n* "
  1184. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  1185. (let ((org-insert-heading-respect-content nil))
  1186. (org-insert-heading '(4)))
  1187. (buffer-string))))
  1188. (should
  1189. (equal "* H\n- item\n* "
  1190. (org-test-with-temp-text "* H\n- item"
  1191. (org-cycle)
  1192. (goto-char (point-max))
  1193. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1194. (buffer-string))))
  1195. ;; Preserve list visibility when inserting an item.
  1196. (should
  1197. (equal
  1198. '(outline outline)
  1199. (org-test-with-temp-text "- A\n - B\n- C\n - D"
  1200. (let ((org-cycle-include-plain-lists t))
  1201. (org-cycle)
  1202. (forward-line 2)
  1203. (org-cycle)
  1204. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1205. (list (get-char-property (line-beginning-position 0) 'invisible)
  1206. (get-char-property (line-end-position 2) 'invisible))))))
  1207. ;; When called with one universal argument, insert a new headline at
  1208. ;; the end of the current subtree, independently on the position of
  1209. ;; point.
  1210. (should
  1211. (equal
  1212. "* H1\n** H2\n* "
  1213. (org-test-with-temp-text "* H1\n** H2"
  1214. (let ((org-insert-heading-respect-content nil))
  1215. (org-insert-heading '(4)))
  1216. (buffer-string))))
  1217. (should
  1218. (equal
  1219. "* H1\n** H2\n* "
  1220. (org-test-with-temp-text "* H<point>1\n** H2"
  1221. (let ((org-insert-heading-respect-content nil))
  1222. (org-insert-heading '(4)))
  1223. (buffer-string))))
  1224. ;; When called with two universal arguments, insert a new headline
  1225. ;; at the end of the grandparent subtree.
  1226. (should
  1227. (equal "* H1\n** H3\n- item\n** H2\n** "
  1228. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1229. (let ((org-insert-heading-respect-content nil))
  1230. (org-insert-heading '(16)))
  1231. (buffer-string))))
  1232. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1233. ;; a level 1 heading.
  1234. (should
  1235. (equal "* H1\n** H2\n* "
  1236. (org-test-with-temp-text "* H1\n** H2<point>"
  1237. (org-insert-heading nil nil t)
  1238. (buffer-string))))
  1239. (should
  1240. (equal "* H1\n- item\n* "
  1241. (org-test-with-temp-text "* H1\n- item<point>"
  1242. (org-insert-heading nil nil t)
  1243. (buffer-string))))
  1244. ;; Corner case: correctly insert a headline after an empty one.
  1245. (should
  1246. (equal "* \n* "
  1247. (org-test-with-temp-text "* <point>"
  1248. (org-insert-heading)
  1249. (buffer-string)))))
  1250. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1251. "Test `org-insert-todo-heading-respect-content' specifications."
  1252. ;; Create a TODO heading.
  1253. (should
  1254. (org-test-with-temp-text "* H1\n Body"
  1255. (org-insert-todo-heading-respect-content)
  1256. (nth 2 (org-heading-components))))
  1257. ;; Add headline at the end of the first subtree
  1258. (should
  1259. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  1260. (search-forward "H1Body")
  1261. (org-insert-todo-heading-respect-content)
  1262. (and (eobp) (org-at-heading-p))))
  1263. ;; In a list, do not create a new item.
  1264. (should
  1265. (org-test-with-temp-text "* H\n- an item\n- another one"
  1266. (search-forward "an ")
  1267. (org-insert-todo-heading-respect-content)
  1268. (and (eobp) (org-at-heading-p)))))
  1269. (ert-deftest test-org/clone-with-time-shift ()
  1270. "Test `org-clone-subtree-with-time-shift'."
  1271. ;; Clone non-repeating once.
  1272. (should
  1273. (equal "\
  1274. * H1\n<2015-06-21>
  1275. * H1\n<2015-06-23>
  1276. "
  1277. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1278. (org-clone-subtree-with-time-shift 1 "+2d")
  1279. (replace-regexp-in-string
  1280. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1281. nil nil 1))))
  1282. ;; Clone repeating once.
  1283. (should
  1284. (equal "\
  1285. * H1\n<2015-06-21>
  1286. * H1\n<2015-06-23>
  1287. * H1\n<2015-06-25 +1w>
  1288. "
  1289. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1290. (org-clone-subtree-with-time-shift 1 "+2d")
  1291. (replace-regexp-in-string
  1292. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1293. nil nil 1))))
  1294. ;; Clone non-repeating zero times.
  1295. (should
  1296. (equal "\
  1297. * H1\n<2015-06-21>
  1298. "
  1299. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1300. (org-clone-subtree-with-time-shift 0 "+2d")
  1301. (replace-regexp-in-string
  1302. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1303. nil nil 1))))
  1304. ;; Clone repeating "zero" times.
  1305. (should
  1306. (equal "\
  1307. * H1\n<2015-06-21>
  1308. * H1\n<2015-06-23 +1w>
  1309. "
  1310. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1311. (org-clone-subtree-with-time-shift 0 "+2d")
  1312. (replace-regexp-in-string
  1313. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1314. nil nil 1)))))
  1315. ;;; Fixed-Width Areas
  1316. (ert-deftest test-org/toggle-fixed-width ()
  1317. "Test `org-toggle-fixed-width' specifications."
  1318. ;; No region: Toggle on fixed-width marker in paragraphs.
  1319. (should
  1320. (equal ": A"
  1321. (org-test-with-temp-text "A"
  1322. (org-toggle-fixed-width)
  1323. (buffer-string))))
  1324. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1325. (should
  1326. (equal "A"
  1327. (org-test-with-temp-text ": A"
  1328. (org-toggle-fixed-width)
  1329. (buffer-string))))
  1330. ;; No region: Toggle on marker in blank lines after elements or just
  1331. ;; after a headline.
  1332. (should
  1333. (equal "* H\n: "
  1334. (org-test-with-temp-text "* H\n"
  1335. (forward-line)
  1336. (org-toggle-fixed-width)
  1337. (buffer-string))))
  1338. (should
  1339. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1340. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1341. (goto-char (point-max))
  1342. (org-toggle-fixed-width)
  1343. (buffer-string))))
  1344. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1345. ;; headlines, clocks)
  1346. (should
  1347. (equal ": * Headline"
  1348. (org-test-with-temp-text "* Headline"
  1349. (org-toggle-fixed-width)
  1350. (buffer-string))))
  1351. (should
  1352. (equal ": #+KEYWORD: value"
  1353. (org-test-with-temp-text "#+KEYWORD: value"
  1354. (org-toggle-fixed-width)
  1355. (buffer-string))))
  1356. ;; No region: error in other situations.
  1357. (should-error
  1358. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1359. (forward-line)
  1360. (org-toggle-fixed-width)
  1361. (buffer-string)))
  1362. ;; No region: Indentation is preserved.
  1363. (should
  1364. (equal "- A\n : B"
  1365. (org-test-with-temp-text "- A\n B"
  1366. (forward-line)
  1367. (org-toggle-fixed-width)
  1368. (buffer-string))))
  1369. ;; Region: If it contains only fixed-width elements and blank lines,
  1370. ;; toggle off fixed-width markup.
  1371. (should
  1372. (equal "A\n\nB"
  1373. (org-test-with-temp-text ": A\n\n: B"
  1374. (transient-mark-mode 1)
  1375. (push-mark (point) t t)
  1376. (goto-char (point-max))
  1377. (org-toggle-fixed-width)
  1378. (buffer-string))))
  1379. ;; Region: If it contains anything else, toggle on fixed-width but
  1380. ;; not on fixed-width areas.
  1381. (should
  1382. (equal ": A\n: \n: B\n: \n: C"
  1383. (org-test-with-temp-text "A\n\n: B\n\nC"
  1384. (transient-mark-mode 1)
  1385. (push-mark (point) t t)
  1386. (goto-char (point-max))
  1387. (org-toggle-fixed-width)
  1388. (buffer-string))))
  1389. ;; Region: Ignore blank lines at its end, unless it contains only
  1390. ;; such lines.
  1391. (should
  1392. (equal ": A\n\n"
  1393. (org-test-with-temp-text "A\n\n"
  1394. (transient-mark-mode 1)
  1395. (push-mark (point) t t)
  1396. (goto-char (point-max))
  1397. (org-toggle-fixed-width)
  1398. (buffer-string))))
  1399. (should
  1400. (equal ": \n: \n"
  1401. (org-test-with-temp-text "\n\n"
  1402. (transient-mark-mode 1)
  1403. (push-mark (point) t t)
  1404. (goto-char (point-max))
  1405. (org-toggle-fixed-width)
  1406. (buffer-string)))))
  1407. ;;; Headline
  1408. (ert-deftest test-org/get-heading ()
  1409. "Test `org-get-heading' specifications."
  1410. ;; Return current heading, even if point is not on it.
  1411. (should
  1412. (equal "H"
  1413. (org-test-with-temp-text "* H"
  1414. (org-get-heading))))
  1415. (should
  1416. (equal "H"
  1417. (org-test-with-temp-text "* H\nText<point>"
  1418. (org-get-heading))))
  1419. ;; Without any optional argument, return TODO keywords and tags.
  1420. (should
  1421. (equal "TODO H"
  1422. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1423. (org-get-heading))))
  1424. (should
  1425. (equal "H :tag:"
  1426. (org-test-with-temp-text "* H :tag:"
  1427. (org-get-heading))))
  1428. ;; With NO-TAGS argument, ignore tags.
  1429. (should
  1430. (equal "TODO H"
  1431. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1432. (org-get-heading t))))
  1433. (should
  1434. (equal "H"
  1435. (org-test-with-temp-text "* H :tag:"
  1436. (org-get-heading t))))
  1437. ;; With NO-TODO, ignore TODO keyword.
  1438. (should
  1439. (equal "H"
  1440. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1441. (org-get-heading nil t))))
  1442. (should
  1443. (equal "H :tag:"
  1444. (org-test-with-temp-text "* H :tag:"
  1445. (org-get-heading nil t))))
  1446. ;; TODO keywords are case-sensitive.
  1447. (should
  1448. (equal "Todo H"
  1449. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1450. (org-get-heading nil t))))
  1451. ;; On an empty headline, return value is consistent.
  1452. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1453. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1454. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1455. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t t)))))
  1456. (ert-deftest test-org/in-commented-heading-p ()
  1457. "Test `org-in-commented-heading-p' specifications."
  1458. ;; Commented headline.
  1459. (should
  1460. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1461. (goto-char (point-max))
  1462. (org-in-commented-heading-p)))
  1463. ;; Commented ancestor.
  1464. (should
  1465. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1466. (goto-char (point-max))
  1467. (org-in-commented-heading-p)))
  1468. ;; Comment keyword is case-sensitive.
  1469. (should-not
  1470. (org-test-with-temp-text "* Comment Headline\nBody"
  1471. (goto-char (point-max))
  1472. (org-in-commented-heading-p)))
  1473. ;; Keyword is standalone.
  1474. (should-not
  1475. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1476. (goto-char (point-max))
  1477. (org-in-commented-heading-p)))
  1478. ;; Optional argument.
  1479. (should-not
  1480. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1481. (goto-char (point-max))
  1482. (org-in-commented-heading-p t))))
  1483. (ert-deftest test-org/entry-blocked-p ()
  1484. ;; Check other dependencies.
  1485. (should
  1486. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1487. (let ((org-enforce-todo-dependencies t)
  1488. (org-blocker-hook
  1489. '(org-block-todo-from-children-or-siblings-or-parent)))
  1490. (org-entry-blocked-p))))
  1491. (should-not
  1492. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1493. (let ((org-enforce-todo-dependencies t)
  1494. (org-blocker-hook
  1495. '(org-block-todo-from-children-or-siblings-or-parent)))
  1496. (org-entry-blocked-p))))
  1497. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1498. ;; blocked.
  1499. (should-not
  1500. (org-test-with-temp-text "* Blocked\n** TODO one"
  1501. (let ((org-enforce-todo-dependencies t)
  1502. (org-blocker-hook
  1503. '(org-block-todo-from-children-or-siblings-or-parent)))
  1504. (org-entry-blocked-p))))
  1505. (should-not
  1506. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1507. (let ((org-enforce-todo-dependencies t)
  1508. (org-blocker-hook
  1509. '(org-block-todo-from-children-or-siblings-or-parent)))
  1510. (org-entry-blocked-p))))
  1511. ;; Follow :ORDERED: specifications.
  1512. (should
  1513. (org-test-with-temp-text
  1514. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1515. (let ((org-enforce-todo-dependencies t)
  1516. (org-blocker-hook
  1517. '(org-block-todo-from-children-or-siblings-or-parent)))
  1518. (org-entry-blocked-p))))
  1519. (should-not
  1520. (org-test-with-temp-text
  1521. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1522. (let ((org-enforce-todo-dependencies t)
  1523. (org-blocker-hook
  1524. '(org-block-todo-from-children-or-siblings-or-parent)))
  1525. (org-entry-blocked-p)))))
  1526. (ert-deftest test-org/get-outline-path ()
  1527. "Test `org-get-outline-path' specifications."
  1528. ;; Top-level headlines have no outline path.
  1529. (should-not
  1530. (org-test-with-temp-text "* H"
  1531. (org-get-outline-path)))
  1532. ;; Otherwise, outline path is the path leading to the headline.
  1533. (should
  1534. (equal '("H")
  1535. (org-test-with-temp-text "* H\n** S<point>"
  1536. (org-get-outline-path))))
  1537. ;; Find path even when point is not on a headline.
  1538. (should
  1539. (equal '("H")
  1540. (org-test-with-temp-text "* H\n** S\nText<point>"
  1541. (org-get-outline-path))))
  1542. ;; TODO keywords, tags and statistics cookies are ignored.
  1543. (should
  1544. (equal '("H")
  1545. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1546. (org-get-outline-path))))
  1547. ;; Links are replaced with their description or their path.
  1548. (should
  1549. (equal '("Org")
  1550. (org-test-with-temp-text
  1551. "* [[http://orgmode.org][Org]]\n** S<point>"
  1552. (org-get-outline-path))))
  1553. (should
  1554. (equal '("http://orgmode.org")
  1555. (org-test-with-temp-text
  1556. "* [[http://orgmode.org]]\n** S<point>"
  1557. (org-get-outline-path))))
  1558. ;; When WITH-SELF is non-nil, include current heading.
  1559. (should
  1560. (equal '("H")
  1561. (org-test-with-temp-text "* H"
  1562. (org-get-outline-path t))))
  1563. (should
  1564. (equal '("H" "S")
  1565. (org-test-with-temp-text "* H\n** S\nText<point>"
  1566. (org-get-outline-path t))))
  1567. ;; Using cache is transparent to the user.
  1568. (should
  1569. (equal '("H")
  1570. (org-test-with-temp-text "* H\n** S<point>"
  1571. (setq org-outline-path-cache nil)
  1572. (org-get-outline-path nil t))))
  1573. ;; Do not corrupt cache when finding outline path in distant part of
  1574. ;; the buffer.
  1575. (should
  1576. (equal '("H2")
  1577. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1578. (setq org-outline-path-cache nil)
  1579. (org-get-outline-path nil t)
  1580. (search-forward "S2")
  1581. (org-get-outline-path nil t))))
  1582. ;; Do not choke on empty headlines.
  1583. (should
  1584. (org-test-with-temp-text "* H\n** <point>"
  1585. (org-get-outline-path)))
  1586. (should
  1587. (org-test-with-temp-text "* \n** H<point>"
  1588. (org-get-outline-path))))
  1589. (ert-deftest test-org/format-outline-path ()
  1590. "Test `org-format-outline-path' specifications."
  1591. (should
  1592. (string= (org-format-outline-path (list "one" "two" "three"))
  1593. "one/two/three"))
  1594. ;; Empty path.
  1595. (should
  1596. (string= (org-format-outline-path '())
  1597. ""))
  1598. (should
  1599. (string= (org-format-outline-path '(nil))
  1600. ""))
  1601. ;; Empty path and prefix.
  1602. (should
  1603. (string= (org-format-outline-path '() nil ">>")
  1604. ">>"))
  1605. ;; Trailing whitespace in headings.
  1606. (should
  1607. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1608. "one/tw o/three"))
  1609. ;; Non-default prefix and separators.
  1610. (should
  1611. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1612. ">>|one|two|three"))
  1613. ;; Truncate.
  1614. (should
  1615. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1616. "one/two/.."))
  1617. ;; Give a very narrow width.
  1618. (should
  1619. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1620. "on"))
  1621. ;; Give a prefix that extends beyond the width.
  1622. (should
  1623. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1624. ">>>>>>>>>>")
  1625. ">>>>>>>>..")))
  1626. (ert-deftest test-org/map-entries ()
  1627. "Test `org-map-entries' specifications."
  1628. ;; Full match.
  1629. (should
  1630. (equal '(1 11)
  1631. (org-test-with-temp-text "* Level 1\n** Level 2"
  1632. (org-map-entries #'point))))
  1633. ;; Level match.
  1634. (should
  1635. (equal '(1)
  1636. (org-test-with-temp-text "* Level 1\n** Level 2"
  1637. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1638. (should
  1639. (equal '(11)
  1640. (org-test-with-temp-text "* Level 1\n** Level 2"
  1641. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1642. ;; Tag match.
  1643. (should
  1644. (equal '(11)
  1645. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1646. (org-map-entries #'point "yes"))))
  1647. (should
  1648. (equal '(14)
  1649. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1650. (org-map-entries #'point "+yes-a"))))
  1651. (should
  1652. (equal '(11 23)
  1653. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1654. (org-map-entries #'point "{yes?}"))))
  1655. ;; Priority match.
  1656. (should
  1657. (equal '(1)
  1658. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1659. (org-map-entries #'point "PRIORITY=\"A\""))))
  1660. ;; Date match.
  1661. (should
  1662. (equal '(36)
  1663. (org-test-with-temp-text "
  1664. * H1
  1665. SCHEDULED: <2012-03-29 thu.>
  1666. * H2
  1667. SCHEDULED: <2014-03-04 tue.>"
  1668. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1669. (should
  1670. (equal '(2)
  1671. (org-test-with-temp-text "
  1672. * H1
  1673. SCHEDULED: <2012-03-29 thu.>
  1674. * H2
  1675. SCHEDULED: <2014-03-04 tue.>"
  1676. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1677. ;; Regular property match.
  1678. (should
  1679. (equal '(2)
  1680. (org-test-with-temp-text "
  1681. * H1
  1682. :PROPERTIES:
  1683. :TEST: 1
  1684. :END:
  1685. * H2
  1686. :PROPERTIES:
  1687. :TEST: 2
  1688. :END:"
  1689. (org-map-entries #'point "TEST=1"))))
  1690. ;; Multiple criteria.
  1691. (should
  1692. (equal '(23)
  1693. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1694. (let (org-odd-levels-only
  1695. (org-use-tag-inheritance nil))
  1696. (org-map-entries #'point "yes+LEVEL=1")))))
  1697. ;; "or" criteria.
  1698. (should
  1699. (equal '(12 24)
  1700. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1701. (let (org-odd-levels-only)
  1702. (org-map-entries #'point "LEVEL=2|no")))))
  1703. (should
  1704. (equal '(1 12)
  1705. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  1706. (let (org-odd-levels-only)
  1707. (org-map-entries #'point "yes|no")))))
  1708. ;; "and" criteria.
  1709. (should
  1710. (equal '(22)
  1711. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  1712. (let (org-odd-levels-only)
  1713. (org-map-entries #'point "yes&no"))))))
  1714. (ert-deftest test-org/edit-headline ()
  1715. "Test `org-edit-headline' specifications."
  1716. (should
  1717. (equal "* B"
  1718. (org-test-with-temp-text "* A"
  1719. (org-edit-headline "B")
  1720. (buffer-string))))
  1721. ;; Handle empty headings.
  1722. (should
  1723. (equal "* "
  1724. (org-test-with-temp-text "* A"
  1725. (org-edit-headline "")
  1726. (buffer-string))))
  1727. (should
  1728. (equal "* A"
  1729. (org-test-with-temp-text "* "
  1730. (org-edit-headline "A")
  1731. (buffer-string))))
  1732. ;; Handle TODO keywords and priority cookies.
  1733. (should
  1734. (equal "* TODO B"
  1735. (org-test-with-temp-text "* TODO A"
  1736. (org-edit-headline "B")
  1737. (buffer-string))))
  1738. (should
  1739. (equal "* [#A] B"
  1740. (org-test-with-temp-text "* [#A] A"
  1741. (org-edit-headline "B")
  1742. (buffer-string))))
  1743. (should
  1744. (equal "* TODO [#A] B"
  1745. (org-test-with-temp-text "* TODO [#A] A"
  1746. (org-edit-headline "B")
  1747. (buffer-string))))
  1748. ;; Handle tags.
  1749. (equal "* B :tag:"
  1750. (org-test-with-temp-text "* A :tag:"
  1751. (let ((org-tags-column 4)) (org-edit-headline "B"))
  1752. (buffer-string))))
  1753. ;;; Keywords
  1754. (ert-deftest test-org/set-regexps-and-options ()
  1755. "Test `org-set-regexps-and-options' specifications."
  1756. ;; TAGS keyword.
  1757. (should
  1758. (equal '(("A"))
  1759. (let ((org-tag-alist '(("A")))
  1760. (org-tag-persistent-alist nil))
  1761. (org-test-with-temp-text ""
  1762. (org-mode-restart)
  1763. org-current-tag-alist))))
  1764. (should
  1765. (equal '(("B"))
  1766. (let ((org-tag-alist '(("A")))
  1767. (org-tag-persistent-alist nil))
  1768. (org-test-with-temp-text "#+TAGS: B"
  1769. (org-mode-restart)
  1770. org-current-tag-alist))))
  1771. (should
  1772. (equal '(("C") ("B"))
  1773. (let ((org-tag-alist '(("A")))
  1774. (org-tag-persistent-alist '(("C"))))
  1775. (org-test-with-temp-text "#+TAGS: B"
  1776. (org-mode-restart)
  1777. org-current-tag-alist))))
  1778. (should
  1779. (equal '(("B"))
  1780. (let ((org-tag-alist '(("A")))
  1781. (org-tag-persistent-alist '(("C"))))
  1782. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  1783. (org-mode-restart)
  1784. org-current-tag-alist))))
  1785. (should
  1786. (equal '(("A" . ?a) ("B") ("C"))
  1787. (let ((org-tag-persistant-alist nil))
  1788. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1789. (org-mode-restart)
  1790. org-current-tag-alist))))
  1791. (should
  1792. (equal '(("A") (:newline) ("B"))
  1793. (let ((org-tag-persistent-alist nil))
  1794. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1795. (org-mode-restart)
  1796. org-current-tag-alist))))
  1797. (should
  1798. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1799. (let ((org-tag-persistent-alist nil))
  1800. (org-test-with-temp-text "#+TAGS: { A B } C"
  1801. (org-mode-restart)
  1802. org-current-tag-alist))))
  1803. (should
  1804. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1805. (let ((org-tag-persistent-alist nil))
  1806. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1807. (org-mode-restart)
  1808. org-current-tag-alist))))
  1809. (should
  1810. (equal '(("A" "B" "C"))
  1811. (let ((org-tag-persistent-alist nil))
  1812. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1813. (org-mode-restart)
  1814. org-tag-groups-alist))))
  1815. (should
  1816. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1817. (let ((org-tag-persistent-alist nil))
  1818. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1819. (org-mode-restart)
  1820. org-current-tag-alist))))
  1821. (should
  1822. (equal '(("A" "B" "C"))
  1823. (let ((org-tag-persistent-alist nil))
  1824. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1825. (org-mode-restart)
  1826. org-tag-groups-alist))))
  1827. ;; FILETAGS keyword.
  1828. (should
  1829. (equal '("A" "B" "C")
  1830. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1831. (org-mode-restart)
  1832. org-file-tags)))
  1833. ;; PROPERTY keyword. Property names are case-insensitive.
  1834. (should
  1835. (equal "foo=1"
  1836. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1837. (org-mode-restart)
  1838. (cdr (assoc "var" org-file-properties)))))
  1839. (should
  1840. (equal
  1841. "foo=1 bar=2"
  1842. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1843. (org-mode-restart)
  1844. (cdr (assoc "var" org-file-properties)))))
  1845. (should
  1846. (equal
  1847. "foo=1 bar=2"
  1848. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1849. (org-mode-restart)
  1850. (cdr (assoc "var" org-file-properties)))))
  1851. ;; ARCHIVE keyword.
  1852. (should
  1853. (equal "%s_done::"
  1854. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1855. (org-mode-restart)
  1856. org-archive-location)))
  1857. ;; CATEGORY keyword.
  1858. (should
  1859. (eq 'test
  1860. (org-test-with-temp-text "#+CATEGORY: test"
  1861. (org-mode-restart)
  1862. org-category)))
  1863. (should
  1864. (equal "test"
  1865. (org-test-with-temp-text "#+CATEGORY: test"
  1866. (org-mode-restart)
  1867. (cdr (assoc "CATEGORY" org-file-properties)))))
  1868. ;; COLUMNS keyword.
  1869. (should
  1870. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1871. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1872. (org-mode-restart)
  1873. org-columns-default-format)))
  1874. ;; CONSTANTS keyword. Constants names are case sensitive.
  1875. (should
  1876. (equal '("299792458." "3.14")
  1877. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1878. (org-mode-restart)
  1879. (mapcar
  1880. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1881. '("c" "pi")))))
  1882. (should
  1883. (equal "3.14"
  1884. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1885. (org-mode-restart)
  1886. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1887. (should
  1888. (equal "22/7"
  1889. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1890. (org-mode-restart)
  1891. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1892. ;; LINK keyword.
  1893. (should
  1894. (equal
  1895. '("url1" "url2")
  1896. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1897. (org-mode-restart)
  1898. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1899. '("a" "b")))))
  1900. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1901. (should
  1902. (equal
  1903. '(?X ?Z ?Y)
  1904. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1905. (org-mode-restart)
  1906. (list org-highest-priority org-lowest-priority org-default-priority))))
  1907. (should
  1908. (equal
  1909. '(?A ?C ?B)
  1910. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1911. (org-mode-restart)
  1912. (list org-highest-priority org-lowest-priority org-default-priority))))
  1913. ;; STARTUP keyword.
  1914. (should
  1915. (equal '(t t)
  1916. (org-test-with-temp-text "#+STARTUP: fold odd"
  1917. (org-mode-restart)
  1918. (list org-startup-folded org-odd-levels-only))))
  1919. ;; TODO keywords.
  1920. (should
  1921. (equal '(("A" "B") ("C"))
  1922. (org-test-with-temp-text "#+TODO: A B | C"
  1923. (org-mode-restart)
  1924. (list org-not-done-keywords org-done-keywords))))
  1925. (should
  1926. (equal '(("A" "C") ("B" "D"))
  1927. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1928. (org-mode-restart)
  1929. (list org-not-done-keywords org-done-keywords))))
  1930. (should
  1931. (equal '(("A" "B") ("C"))
  1932. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1933. (org-mode-restart)
  1934. (list org-not-done-keywords org-done-keywords))))
  1935. (should
  1936. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1937. (org-test-with-temp-text "#+TODO: A(a)"
  1938. (org-mode-restart)
  1939. org-todo-key-alist)))
  1940. (should
  1941. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1942. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1943. (org-mode-restart)
  1944. org-todo-log-states)))
  1945. ;; Enter SETUPFILE keyword.
  1946. (should
  1947. (equal "1"
  1948. (org-test-with-temp-text
  1949. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1950. (org-mode-restart)
  1951. (cdr (assoc "a" org-file-properties))))))
  1952. ;;; Links
  1953. ;;;; Coderefs
  1954. (ert-deftest test-org/coderef ()
  1955. "Test coderef links specifications."
  1956. (should
  1957. (org-test-with-temp-text "
  1958. #+BEGIN_SRC emacs-lisp
  1959. \(+ 1 1) (ref:sc)
  1960. #+END_SRC
  1961. \[[(sc)<point>]]"
  1962. (org-open-at-point)
  1963. (looking-at "(ref:sc)")))
  1964. ;; Find coderef even with alternate label format.
  1965. (should
  1966. (org-test-with-temp-text "
  1967. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  1968. \(+ 1 1) {ref:sc}
  1969. #+END_SRC
  1970. \[[(sc)<point>]]"
  1971. (org-open-at-point)
  1972. (looking-at "{ref:sc}"))))
  1973. ;;;; Custom ID
  1974. (ert-deftest test-org/custom-id ()
  1975. "Test custom ID links specifications."
  1976. (should
  1977. (org-test-with-temp-text
  1978. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  1979. (org-open-at-point)
  1980. (looking-at-p "\\* H1")))
  1981. ;; Throw an error on false positives.
  1982. (should-error
  1983. (org-test-with-temp-text
  1984. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  1985. (org-open-at-point)
  1986. (looking-at-p "\\* H1"))))
  1987. ;;;; Fuzzy Links
  1988. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1989. ;; a named element (#+name: text) and to headlines (* Text).
  1990. (ert-deftest test-org/fuzzy-links ()
  1991. "Test fuzzy links specifications."
  1992. ;; Fuzzy link goes in priority to a matching target.
  1993. (should
  1994. (org-test-with-temp-text
  1995. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  1996. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1997. (looking-at "<<Test>>")))
  1998. ;; Then fuzzy link points to an element with a given name.
  1999. (should
  2000. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2001. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2002. (looking-at "#\\+NAME: Test")))
  2003. ;; A target still lead to a matching headline otherwise.
  2004. (should
  2005. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2006. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2007. (looking-at "\\* Head2")))
  2008. ;; With a leading star in link, enforce heading match.
  2009. (should
  2010. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2011. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2012. (looking-at "\\* Test")))
  2013. ;; With a leading star in link, enforce exact heading match, even
  2014. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2015. (should-error
  2016. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2017. (let ((org-link-search-must-match-exact-headline nil))
  2018. (org-open-at-point))))
  2019. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2020. (should
  2021. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2022. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2023. (looking-at "\\* Test")))
  2024. (should
  2025. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2026. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2027. (looking-at "\\* Test")))
  2028. ;; Heading match should not care about spaces, cookies, TODO
  2029. ;; keywords, priorities, and tags.
  2030. (should
  2031. (let ((first-line
  2032. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2033. (org-test-with-temp-text
  2034. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2035. (let ((org-link-search-must-match-exact-headline nil)
  2036. (org-todo-regexp "TODO"))
  2037. (org-open-at-point))
  2038. (looking-at (regexp-quote first-line)))))
  2039. ;; Heading match should still be exact.
  2040. (should-error
  2041. (let ((first-line
  2042. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2043. (org-test-with-temp-text
  2044. (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
  2045. (let ((org-link-search-must-match-exact-headline nil)
  2046. (org-todo-regexp "TODO"))
  2047. (org-open-at-point)))))
  2048. ;; Heading match ignores COMMENT keyword.
  2049. (should
  2050. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2051. (org-open-at-point)
  2052. (looking-at "\\* COMMENT Test")))
  2053. ;; Correctly un-hexify fuzzy links.
  2054. (should
  2055. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2056. (org-open-at-point)
  2057. (bobp))))
  2058. ;;;; Link Escaping
  2059. (ert-deftest test-org/org-link-escape-ascii-character ()
  2060. "Escape an ascii character."
  2061. (should
  2062. (string=
  2063. "%5B"
  2064. (org-link-escape "["))))
  2065. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2066. "Escape an ascii control character."
  2067. (should
  2068. (string=
  2069. "%09"
  2070. (org-link-escape "\t"))))
  2071. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2072. "Escape an unicode multibyte character."
  2073. (should
  2074. (string=
  2075. "%E2%82%AC"
  2076. (org-link-escape "€"))))
  2077. (ert-deftest test-org/org-link-escape-custom-table ()
  2078. "Escape string with custom character table."
  2079. (should
  2080. (string=
  2081. "Foo%3A%42ar%0A"
  2082. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2083. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2084. "Escape string with custom table merged with default table."
  2085. (should
  2086. (string=
  2087. "%5BF%6F%6F%3A%42ar%0A%5D"
  2088. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2089. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2090. "Unescape an ascii character."
  2091. (should
  2092. (string=
  2093. "["
  2094. (org-link-unescape "%5B"))))
  2095. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2096. "Unescpae an ascii control character."
  2097. (should
  2098. (string=
  2099. "\n"
  2100. (org-link-unescape "%0A"))))
  2101. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2102. "Unescape unicode multibyte character."
  2103. (should
  2104. (string=
  2105. "€"
  2106. (org-link-unescape "%E2%82%AC"))))
  2107. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2108. "Unescape old style percent escaped character."
  2109. (should
  2110. (string=
  2111. "àâçèéêîôùû"
  2112. (decode-coding-string
  2113. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2114. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2115. "Escape and unescape a URL that includes an escaped char.
  2116. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2117. (should
  2118. (string=
  2119. "http://some.host.com/form?&id=blah%2Bblah25"
  2120. (org-link-unescape
  2121. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2122. ;;;; Open at point
  2123. (ert-deftest test-org/open-at-point-in-keyword ()
  2124. "Does `org-open-at-point' open link in a keyword line?"
  2125. (should
  2126. (org-test-with-temp-text
  2127. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2128. (org-open-at-point) t)))
  2129. (ert-deftest test-org/open-at-point-in-property ()
  2130. "Does `org-open-at-point' open link in property drawer?"
  2131. (should
  2132. (org-test-with-temp-text
  2133. "* Headline
  2134. :PROPERTIES:
  2135. :URL: <point>[[*Headline]]
  2136. :END:"
  2137. (org-open-at-point) t)))
  2138. (ert-deftest test-org/open-at-point-in-comment ()
  2139. "Does `org-open-at-point' open link in a commented line?"
  2140. (should
  2141. (org-test-with-temp-text
  2142. "<<top>>\n# <point>[[top]]"
  2143. (org-open-at-point) t)))
  2144. (ert-deftest test-org/open-at-point/inline-image ()
  2145. "Test `org-open-at-point' on nested links."
  2146. (should
  2147. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2148. (org-open-at-point)
  2149. (bobp))))
  2150. (ert-deftest test-org/open-at-point/radio-target ()
  2151. "Test `org-open-at-point' on radio targets."
  2152. (should
  2153. (org-test-with-temp-text "<<<target>>> <point>target"
  2154. (org-update-radio-target-regexp)
  2155. (org-open-at-point)
  2156. (eq (org-element-type (org-element-context)) 'radio-target))))
  2157. ;;;; Stored links
  2158. (ert-deftest test-org/store-link ()
  2159. "Test `org-store-link' specifications."
  2160. ;; On a headline, link to that headline. Use heading as the
  2161. ;; description of the link.
  2162. (should
  2163. (let (org-store-link-props org-stored-links)
  2164. (org-test-with-temp-text-in-file "* H1"
  2165. (let ((file (buffer-file-name)))
  2166. (equal (format "[[file:%s::*H1][H1]]" file)
  2167. (org-store-link nil))))))
  2168. ;; On a headline, remove any link from description.
  2169. (should
  2170. (let (org-store-link-props org-stored-links)
  2171. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2172. (let ((file (buffer-file-name)))
  2173. (equal (format "[[file:%s::*%s][d]]"
  2174. file
  2175. (org-link-escape "[[#l][d]]"))
  2176. (org-store-link nil))))))
  2177. (should
  2178. (let (org-store-link-props org-stored-links)
  2179. (org-test-with-temp-text-in-file "* [[l]]"
  2180. (let ((file (buffer-file-name)))
  2181. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2182. (org-store-link nil))))))
  2183. (should
  2184. (let (org-store-link-props org-stored-links)
  2185. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2186. (let ((file (buffer-file-name)))
  2187. (equal (format "[[file:%s::*%s][d1 d2]]"
  2188. file
  2189. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2190. (org-store-link nil))))))
  2191. ;; On a named element, link to that element.
  2192. (should
  2193. (let (org-store-link-props org-stored-links)
  2194. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2195. (let ((file (buffer-file-name)))
  2196. (equal (format "[[file:%s::foo][foo]]" file)
  2197. (org-store-link nil)))))))
  2198. ;;; Node Properties
  2199. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2200. "Ensure properties accumulate in subtree drawers."
  2201. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2202. (org-babel-next-src-block)
  2203. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2204. (ert-deftest test-org/custom-properties ()
  2205. "Test custom properties specifications."
  2206. ;; Standard test.
  2207. (should
  2208. (let ((org-custom-properties '("FOO")))
  2209. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2210. (org-toggle-custom-properties-visibility)
  2211. (org-invisible-p2))))
  2212. ;; Properties are case-insensitive.
  2213. (should
  2214. (let ((org-custom-properties '("FOO")))
  2215. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2216. (org-toggle-custom-properties-visibility)
  2217. (org-invisible-p2))))
  2218. (should
  2219. (let ((org-custom-properties '("foo")))
  2220. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2221. (org-toggle-custom-properties-visibility)
  2222. (org-invisible-p2))))
  2223. ;; Multiple custom properties in the same drawer.
  2224. (should
  2225. (let ((org-custom-properties '("FOO" "BAR")))
  2226. (org-test-with-temp-text
  2227. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2228. (org-toggle-custom-properties-visibility)
  2229. (and (org-invisible-p2)
  2230. (not (progn (forward-line) (org-invisible-p2)))
  2231. (progn (forward-line) (org-invisible-p2))))))
  2232. ;; Hide custom properties with an empty value.
  2233. (should
  2234. (let ((org-custom-properties '("FOO")))
  2235. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2236. (org-toggle-custom-properties-visibility)
  2237. (org-invisible-p2))))
  2238. ;; Do not hide fake properties.
  2239. (should-not
  2240. (let ((org-custom-properties '("FOO")))
  2241. (org-test-with-temp-text ":FOO: val\n"
  2242. (org-toggle-custom-properties-visibility)
  2243. (org-invisible-p2))))
  2244. (should-not
  2245. (let ((org-custom-properties '("A")))
  2246. (org-test-with-temp-text
  2247. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2248. (org-toggle-custom-properties-visibility)
  2249. (org-invisible-p2)))))
  2250. ;;; Mark Region
  2251. (ert-deftest test-org/mark-subtree ()
  2252. "Test `org-mark-subtree' specifications."
  2253. ;; Error when point is before first headline.
  2254. (should-error
  2255. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2256. (progn (transient-mark-mode 1)
  2257. (org-mark-subtree))))
  2258. ;; Without argument, mark current subtree.
  2259. (should
  2260. (equal
  2261. '(12 32)
  2262. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2263. (progn (transient-mark-mode 1)
  2264. (forward-line 2)
  2265. (org-mark-subtree)
  2266. (list (region-beginning) (region-end))))))
  2267. ;; With an argument, move ARG up.
  2268. (should
  2269. (equal
  2270. '(1 32)
  2271. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2272. (progn (transient-mark-mode 1)
  2273. (forward-line 2)
  2274. (org-mark-subtree 1)
  2275. (list (region-beginning) (region-end))))))
  2276. ;; Do not get fooled by inlinetasks.
  2277. (when (featurep 'org-inlinetask)
  2278. (should
  2279. (= 1
  2280. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2281. (progn (transient-mark-mode 1)
  2282. (forward-line 1)
  2283. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2284. (region-beginning)))))))
  2285. ;;; Miscellaneous
  2286. (ert-deftest test-org/in-regexp ()
  2287. "Test `org-in-regexp' specifications."
  2288. ;; Standard tests.
  2289. (should
  2290. (org-test-with-temp-text "xx ab<point>c xx"
  2291. (org-in-regexp "abc")))
  2292. (should-not
  2293. (org-test-with-temp-text "xx abc <point>xx"
  2294. (org-in-regexp "abc")))
  2295. ;; Return non-nil even with multiple matching regexps in the same
  2296. ;; line.
  2297. (should
  2298. (org-test-with-temp-text "abc xx ab<point>c xx"
  2299. (org-in-regexp "abc")))
  2300. ;; With optional argument NLINES, check extra lines around point.
  2301. (should-not
  2302. (org-test-with-temp-text "A\nB<point>\nC"
  2303. (org-in-regexp "A\nB\nC")))
  2304. (should
  2305. (org-test-with-temp-text "A\nB<point>\nC"
  2306. (org-in-regexp "A\nB\nC" 1)))
  2307. (should-not
  2308. (org-test-with-temp-text "A\nB\nC<point>"
  2309. (org-in-regexp "A\nB\nC" 1)))
  2310. ;; When optional argument VISUALLY is non-nil, return nil if at
  2311. ;; regexp boundaries.
  2312. (should
  2313. (org-test-with-temp-text "xx abc<point> xx"
  2314. (org-in-regexp "abc")))
  2315. (should-not
  2316. (org-test-with-temp-text "xx abc<point> xx"
  2317. (org-in-regexp "abc" nil t))))
  2318. ;;; Navigation
  2319. (ert-deftest test-org/end-of-meta-data ()
  2320. "Test `org-end-of-meta-data' specifications."
  2321. ;; Skip planning line.
  2322. (should
  2323. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  2324. (org-end-of-meta-data)
  2325. (eobp)))
  2326. ;; Skip properties drawer.
  2327. (should
  2328. (org-test-with-temp-text
  2329. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  2330. (org-end-of-meta-data)
  2331. (eobp)))
  2332. ;; Skip both.
  2333. (should
  2334. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  2335. (org-end-of-meta-data)
  2336. (eobp)))
  2337. ;; Nothing to skip, go to first line.
  2338. (should
  2339. (org-test-with-temp-text "* Headline\nContents"
  2340. (org-end-of-meta-data)
  2341. (looking-at "Contents")))
  2342. ;; With option argument, skip empty lines, regular drawers and
  2343. ;; clocking lines.
  2344. (should
  2345. (org-test-with-temp-text "* Headline\n\nContents"
  2346. (org-end-of-meta-data t)
  2347. (looking-at "Contents")))
  2348. (should
  2349. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  2350. (org-end-of-meta-data t)
  2351. (looking-at "Contents")))
  2352. (should
  2353. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  2354. (org-end-of-meta-data t)
  2355. (looking-at "Contents")))
  2356. ;; Special case: do not skip incomplete drawers.
  2357. (should
  2358. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  2359. (org-end-of-meta-data t)
  2360. (looking-at ":LOGBOOK:")))
  2361. ;; Special case: Be careful about consecutive headlines.
  2362. (should-not
  2363. (org-test-with-temp-text "* H1\n*H2\nContents"
  2364. (org-end-of-meta-data t)
  2365. (looking-at "Contents"))))
  2366. (ert-deftest test-org/beginning-of-line ()
  2367. "Test `org-beginning-of-line' specifications."
  2368. ;; Standard test.
  2369. (should
  2370. (org-test-with-temp-text "Some text\nSome other text<point>"
  2371. (progn (org-beginning-of-line) (bolp))))
  2372. ;; Standard test with `visual-line-mode'.
  2373. (should-not
  2374. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2375. (progn (visual-line-mode)
  2376. (dotimes (i 1000) (insert "very "))
  2377. (org-beginning-of-line)
  2378. (bolp))))
  2379. ;; At an headline with special movement.
  2380. (should
  2381. (org-test-with-temp-text "* TODO Headline<point>"
  2382. (let ((org-special-ctrl-a/e t))
  2383. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  2384. (progn (org-beginning-of-line) (bolp))
  2385. (progn (org-beginning-of-line) (looking-at "Headline"))))))
  2386. ;; Leave point before invisible characters at column 0.
  2387. (should
  2388. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  2389. (let ((org-special-ctrl-a/e nil))
  2390. (org-beginning-of-line)
  2391. (bolp))))
  2392. (should
  2393. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  2394. (let ((org-special-ctrl-a/e t))
  2395. (org-beginning-of-line)
  2396. (bolp))))
  2397. ;; Special case: Do not error when the buffer contains only a single
  2398. ;; asterisk.
  2399. (should
  2400. (org-test-with-temp-text "*<point>"
  2401. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  2402. (should
  2403. (org-test-with-temp-text "*<point>"
  2404. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  2405. (ert-deftest test-org/end-of-line ()
  2406. "Test `org-end-of-line' specifications."
  2407. ;; Standard test.
  2408. (should
  2409. (org-test-with-temp-text "Some text\nSome other text"
  2410. (progn (org-end-of-line) (eolp))))
  2411. ;; With `visual-line-mode' active, move to end of visible line.
  2412. ;; However, never go past ellipsis.
  2413. (should-not
  2414. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2415. (visual-line-mode)
  2416. (dotimes (i 1000) (insert "very "))
  2417. (goto-char (point-min))
  2418. (org-end-of-line)
  2419. (eolp)))
  2420. (should-not
  2421. (org-test-with-temp-text "* A short headline\nSome contents"
  2422. (visual-line-mode)
  2423. (org-overview)
  2424. (org-end-of-line)
  2425. (eobp)))
  2426. ;; In a wide headline, with `visual-line-mode', prefer going to end
  2427. ;; of visible line if tags, or end of line, are farther.
  2428. (should-not
  2429. (org-test-with-temp-text "* A <point>long headline"
  2430. (visual-line-mode)
  2431. (dotimes (i 1000) (insert "very "))
  2432. (goto-char (point-min))
  2433. (org-end-of-line)
  2434. (eolp)))
  2435. (should-not
  2436. (org-test-with-temp-text "* A <point>long headline :tag:"
  2437. (visual-line-mode)
  2438. (dotimes (i 1000) (insert "very "))
  2439. (goto-char (point-min))
  2440. (org-end-of-line)
  2441. (eolp)))
  2442. ;; At an headline without special movement, go to end of line.
  2443. ;; However, never go past ellipsis.
  2444. (should
  2445. (org-test-with-temp-text "* Headline2b :tag:\n"
  2446. (let ((org-special-ctrl-a/e nil))
  2447. (and (progn (org-end-of-line) (eolp))
  2448. (progn (org-end-of-line) (eolp))))))
  2449. (should
  2450. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2451. (org-overview)
  2452. (let ((org-special-ctrl-a/e nil))
  2453. (org-end-of-line)
  2454. (= 1 (line-beginning-position)))))
  2455. ;; At an headline with special movement, first move before tags,
  2456. ;; then at the end of line, rinse, repeat. However, never go past
  2457. ;; ellipsis.
  2458. (should
  2459. (org-test-with-temp-text "* Headline1 :tag:\n"
  2460. (let ((org-special-ctrl-a/e t))
  2461. (and (progn (org-end-of-line) (looking-at " :tag:"))
  2462. (progn (org-end-of-line) (eolp))
  2463. (progn (org-end-of-line) (looking-at " :tag:"))))))
  2464. (should
  2465. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2466. (org-overview)
  2467. (let ((org-special-ctrl-a/e t))
  2468. (org-end-of-line)
  2469. (org-end-of-line)
  2470. (= 1 (line-beginning-position)))))
  2471. ;; At an headline, with reversed movement, first go to end of line,
  2472. ;; then before tags. However, never go past ellipsis.
  2473. (should
  2474. (org-test-with-temp-text "* Headline3 :tag:\n"
  2475. (let ((org-special-ctrl-a/e 'reversed)
  2476. (this-command last-command))
  2477. (and (progn (org-end-of-line) (eolp))
  2478. (progn (org-end-of-line) (looking-at " :tag:"))))))
  2479. (should
  2480. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2481. (org-overview)
  2482. (let ((org-special-ctrl-a/e 'reversed))
  2483. (org-end-of-line)
  2484. (= 1 (line-beginning-position)))))
  2485. ;; At a block without hidden contents.
  2486. (should
  2487. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2488. (progn (org-end-of-line) (eolp))))
  2489. ;; At a block with hidden contents.
  2490. (should-not
  2491. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2492. (let ((org-special-ctrl-a/e t))
  2493. (org-hide-block-toggle)
  2494. (org-end-of-line)
  2495. (eobp))))
  2496. ;; Get past invisible characters at the end of line.
  2497. (should
  2498. (org-test-with-temp-text "[[http://orgmode.org]]"
  2499. (org-end-of-line)
  2500. (eolp))))
  2501. (ert-deftest test-org/open-line ()
  2502. "Test `org-open-line' specifications."
  2503. ;; Call `open-line' outside of tables.
  2504. (should
  2505. (equal "\nText"
  2506. (org-test-with-temp-text "Text"
  2507. (org-open-line 1)
  2508. (buffer-string))))
  2509. ;; At a table, create a row above.
  2510. (should
  2511. (equal "\n| |\n| a |"
  2512. (org-test-with-temp-text "\n<point>| a |"
  2513. (org-open-line 1)
  2514. (buffer-string))))
  2515. ;; At the very first character of the buffer, also call `open-line'.
  2516. (should
  2517. (equal "\n| a |"
  2518. (org-test-with-temp-text "| a |"
  2519. (org-open-line 1)
  2520. (buffer-string))))
  2521. ;; Narrowing does not count.
  2522. (should
  2523. (equal "Text\n| |\n| a |"
  2524. (org-test-with-temp-text "Text\n<point>| a |"
  2525. (narrow-to-region (point) (point-max))
  2526. (org-open-line 1)
  2527. (widen)
  2528. (buffer-string)))))
  2529. (ert-deftest test-org/forward-sentence ()
  2530. "Test `org-forward-sentence' specifications."
  2531. ;; At the end of a table cell, move to the end of the next one.
  2532. (should
  2533. (org-test-with-temp-text "| a<point> | b |"
  2534. (org-forward-sentence)
  2535. (looking-at " |$")))
  2536. ;; Elsewhere in a cell, move to its end.
  2537. (should
  2538. (org-test-with-temp-text "| a<point>c | b |"
  2539. (org-forward-sentence)
  2540. (looking-at " | b |$")))
  2541. ;; Otherwise, simply call `forward-sentence'.
  2542. (should
  2543. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2544. (org-forward-sentence)
  2545. (looking-at " Sentence 2.")))
  2546. (should
  2547. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2548. (org-forward-sentence)
  2549. (org-forward-sentence)
  2550. (eobp)))
  2551. ;; At the end of an element, jump to the next one, without stopping
  2552. ;; on blank lines in-between.
  2553. (should
  2554. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  2555. (org-forward-sentence)
  2556. (eobp))))
  2557. (ert-deftest test-org/backward-sentence ()
  2558. "Test `org-backward-sentence' specifications."
  2559. ;; At the beginning of a table cell, move to the beginning of the
  2560. ;; previous one.
  2561. (should
  2562. (org-test-with-temp-text "| a | <point>b |"
  2563. (org-backward-sentence)
  2564. (looking-at "a | b |$")))
  2565. ;; Elsewhere in a cell, move to its beginning.
  2566. (should
  2567. (org-test-with-temp-text "| a | b<point>c |"
  2568. (org-backward-sentence)
  2569. (looking-at "bc |$")))
  2570. ;; Otherwise, simply call `backward-sentence'.
  2571. (should
  2572. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2573. (org-backward-sentence)
  2574. (looking-at "Sentence 2.")))
  2575. (should
  2576. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2577. (org-backward-sentence)
  2578. (org-backward-sentence)
  2579. (bobp)))
  2580. ;; Make sure to hit the beginning of a sentence on the same line as
  2581. ;; an item.
  2582. (should
  2583. (org-test-with-temp-text "- Line 1\n line <point>2."
  2584. (org-backward-sentence)
  2585. (looking-at "Line 1"))))
  2586. (ert-deftest test-org/forward-paragraph ()
  2587. "Test `org-forward-paragraph' specifications."
  2588. ;; At end of buffer, return an error.
  2589. (should-error
  2590. (org-test-with-temp-text "Paragraph"
  2591. (goto-char (point-max))
  2592. (org-forward-paragraph)))
  2593. ;; Standard test.
  2594. (should
  2595. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2596. (org-forward-paragraph)
  2597. (looking-at "P2")))
  2598. ;; Ignore depth.
  2599. (should
  2600. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  2601. (org-forward-paragraph)
  2602. (looking-at "P1")))
  2603. ;; Do not enter elements with invisible contents.
  2604. (should
  2605. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  2606. (org-hide-block-toggle)
  2607. (org-forward-paragraph)
  2608. (looking-at "P3")))
  2609. ;; On an affiliated keyword, jump to the beginning of the element.
  2610. (should
  2611. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  2612. (org-forward-paragraph)
  2613. (looking-at "Para")))
  2614. ;; On an item or a footnote definition, move to the second element
  2615. ;; inside, if any.
  2616. (should
  2617. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  2618. (org-forward-paragraph)
  2619. (looking-at " Paragraph")))
  2620. (should
  2621. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  2622. (org-forward-paragraph)
  2623. (looking-at "Paragraph")))
  2624. ;; On an item, or a footnote definition, when the first line is
  2625. ;; empty, move to the first item.
  2626. (should
  2627. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  2628. (org-forward-paragraph)
  2629. (looking-at " Paragraph")))
  2630. (should
  2631. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  2632. (org-forward-paragraph)
  2633. (looking-at "Paragraph")))
  2634. ;; On a table (resp. a property drawer) do not move through table
  2635. ;; rows (resp. node properties).
  2636. (should
  2637. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  2638. (org-forward-paragraph)
  2639. (looking-at "Paragraph")))
  2640. (should
  2641. (org-test-with-temp-text
  2642. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  2643. (org-forward-paragraph)
  2644. (looking-at "Paragraph")))
  2645. ;; On a verse or source block, stop after blank lines.
  2646. (should
  2647. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  2648. (org-forward-paragraph)
  2649. (looking-at "L2")))
  2650. (should
  2651. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  2652. (org-forward-paragraph)
  2653. (looking-at "L2"))))
  2654. (ert-deftest test-org/backward-paragraph ()
  2655. "Test `org-backward-paragraph' specifications."
  2656. ;; Error at beginning of buffer.
  2657. (should-error
  2658. (org-test-with-temp-text "Paragraph"
  2659. (org-backward-paragraph)))
  2660. ;; Regular test.
  2661. (should
  2662. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2663. (goto-char (point-max))
  2664. (org-backward-paragraph)
  2665. (looking-at "P3")))
  2666. (should
  2667. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2668. (goto-char (point-max))
  2669. (beginning-of-line)
  2670. (org-backward-paragraph)
  2671. (looking-at "P2")))
  2672. ;; Ignore depth.
  2673. (should
  2674. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  2675. (goto-char (point-max))
  2676. (beginning-of-line)
  2677. (org-backward-paragraph)
  2678. (looking-at "P2")))
  2679. ;; Ignore invisible elements.
  2680. (should
  2681. (org-test-with-temp-text "* H1\n P1\n* H2"
  2682. (org-cycle)
  2683. (goto-char (point-max))
  2684. (beginning-of-line)
  2685. (org-backward-paragraph)
  2686. (bobp)))
  2687. ;; On an affiliated keyword, jump to the first one.
  2688. (should
  2689. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  2690. (search-forward "c2")
  2691. (org-backward-paragraph)
  2692. (looking-at "#\\+name")))
  2693. ;; On the second element in an item or a footnote definition, jump
  2694. ;; to item or the definition.
  2695. (should
  2696. (org-test-with-temp-text "- line1\n\n line2"
  2697. (goto-char (point-max))
  2698. (beginning-of-line)
  2699. (org-backward-paragraph)
  2700. (looking-at "- line1")))
  2701. (should
  2702. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  2703. (goto-char (point-max))
  2704. (beginning-of-line)
  2705. (org-backward-paragraph)
  2706. (looking-at "\\[fn:1\\] line1")))
  2707. ;; On a table (resp. a property drawer), ignore table rows
  2708. ;; (resp. node properties).
  2709. (should
  2710. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  2711. (goto-char (point-max))
  2712. (beginning-of-line)
  2713. (org-backward-paragraph)
  2714. (bobp)))
  2715. (should
  2716. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  2717. (org-backward-paragraph)
  2718. (looking-at ":PROPERTIES:")))
  2719. ;; On a source or verse block, stop before blank lines.
  2720. (should
  2721. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  2722. (search-forward "L3")
  2723. (beginning-of-line)
  2724. (org-backward-paragraph)
  2725. (looking-at "L2")))
  2726. (should
  2727. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  2728. (search-forward "L3")
  2729. (beginning-of-line)
  2730. (org-backward-paragraph)
  2731. (looking-at "L2"))))
  2732. (ert-deftest test-org/forward-element ()
  2733. "Test `org-forward-element' specifications."
  2734. ;; 1. At EOB: should error.
  2735. (org-test-with-temp-text "Some text\n"
  2736. (goto-char (point-max))
  2737. (should-error (org-forward-element)))
  2738. ;; 2. Standard move: expected to ignore blank lines.
  2739. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  2740. (org-forward-element)
  2741. (should (looking-at (regexp-quote "Second paragraph."))))
  2742. ;; 3. Headline tests.
  2743. (org-test-with-temp-text "
  2744. * Head 1
  2745. ** Head 1.1
  2746. *** Head 1.1.1
  2747. ** Head 1.2"
  2748. ;; 3.1. At an headline beginning: move to next headline at the
  2749. ;; same level.
  2750. (goto-line 3)
  2751. (org-forward-element)
  2752. (should (looking-at (regexp-quote "** Head 1.2")))
  2753. ;; 3.2. At an headline beginning: move to parent headline if no
  2754. ;; headline at the same level.
  2755. (goto-line 3)
  2756. (org-forward-element)
  2757. (should (looking-at (regexp-quote "** Head 1.2"))))
  2758. ;; 4. Greater element tests.
  2759. (org-test-with-temp-text
  2760. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  2761. ;; 4.1. At a greater element: expected to skip contents.
  2762. (org-forward-element)
  2763. (should (looking-at (regexp-quote "Outside.")))
  2764. ;; 4.2. At the end of greater element contents: expected to skip
  2765. ;; to the end of the greater element.
  2766. (goto-line 2)
  2767. (org-forward-element)
  2768. (should (looking-at (regexp-quote "Outside."))))
  2769. ;; 5. List tests.
  2770. (org-test-with-temp-text "
  2771. - item1
  2772. - sub1
  2773. - sub2
  2774. - sub3
  2775. Inner paragraph.
  2776. - item2
  2777. Outside."
  2778. ;; 5.1. At list top point: expected to move to the element after
  2779. ;; the list.
  2780. (goto-line 2)
  2781. (org-forward-element)
  2782. (should (looking-at (regexp-quote "Outside.")))
  2783. ;; 5.2. Special case: at the first line of a sub-list, but not at
  2784. ;; beginning of line, move to next item.
  2785. (goto-line 2)
  2786. (forward-char)
  2787. (org-forward-element)
  2788. (should (looking-at "- item2"))
  2789. (goto-line 4)
  2790. (forward-char)
  2791. (org-forward-element)
  2792. (should (looking-at " - sub2"))
  2793. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  2794. (goto-line 4)
  2795. (org-forward-element)
  2796. (should (looking-at (regexp-quote " Inner paragraph.")))
  2797. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  2798. (goto-line 8)
  2799. (org-forward-element)
  2800. (should (looking-at (regexp-quote " Inner paragraph.")))
  2801. ;; 5.5. At an item: expected to move to next item, if any.
  2802. (goto-line 6)
  2803. (org-forward-element)
  2804. (should (looking-at " - sub3"))))
  2805. (ert-deftest test-org/backward-element ()
  2806. "Test `org-backward-element' specifications."
  2807. ;; 1. Should error at BOB.
  2808. (org-test-with-temp-text " \nParagraph."
  2809. (should-error (org-backward-element)))
  2810. ;; 2. Should move at BOB when called on the first element in buffer.
  2811. (should
  2812. (org-test-with-temp-text "\n#+TITLE: test"
  2813. (progn (forward-line)
  2814. (org-backward-element)
  2815. (bobp))))
  2816. ;; 3. Not at the beginning of an element: move at its beginning.
  2817. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2818. (goto-line 3)
  2819. (end-of-line)
  2820. (org-backward-element)
  2821. (should (looking-at (regexp-quote "Paragraph2."))))
  2822. ;; 4. Headline tests.
  2823. (org-test-with-temp-text "
  2824. * Head 1
  2825. ** Head 1.1
  2826. *** Head 1.1.1
  2827. ** Head 1.2"
  2828. ;; 4.1. At an headline beginning: move to previous headline at the
  2829. ;; same level.
  2830. (goto-line 5)
  2831. (org-backward-element)
  2832. (should (looking-at (regexp-quote "** Head 1.1")))
  2833. ;; 4.2. At an headline beginning: move to parent headline if no
  2834. ;; headline at the same level.
  2835. (goto-line 3)
  2836. (org-backward-element)
  2837. (should (looking-at (regexp-quote "* Head 1")))
  2838. ;; 4.3. At the first top-level headline: should error.
  2839. (goto-line 2)
  2840. (should-error (org-backward-element)))
  2841. ;; 5. At beginning of first element inside a greater element:
  2842. ;; expected to move to greater element's beginning.
  2843. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  2844. (goto-line 3)
  2845. (org-backward-element)
  2846. (should (looking-at "#\\+BEGIN_CENTER")))
  2847. ;; 6. At the beginning of the first element in a section: should
  2848. ;; move back to headline, if any.
  2849. (should
  2850. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  2851. (progn (goto-char (point-max))
  2852. (beginning-of-line)
  2853. (org-backward-element)
  2854. (org-at-heading-p))))
  2855. ;; 7. List tests.
  2856. (org-test-with-temp-text "
  2857. - item1
  2858. - sub1
  2859. - sub2
  2860. - sub3
  2861. Inner paragraph.
  2862. - item2
  2863. Outside."
  2864. ;; 7.1. At beginning of sub-list: expected to move to the
  2865. ;; paragraph before it.
  2866. (goto-line 4)
  2867. (org-backward-element)
  2868. (should (looking-at "item1"))
  2869. ;; 7.2. At an item in a list: expected to move at previous item.
  2870. (goto-line 8)
  2871. (org-backward-element)
  2872. (should (looking-at " - sub2"))
  2873. (goto-line 12)
  2874. (org-backward-element)
  2875. (should (looking-at "- item1"))
  2876. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  2877. ;; beginning.
  2878. (goto-line 10)
  2879. (org-backward-element)
  2880. (should (looking-at " - sub1"))
  2881. (goto-line 15)
  2882. (org-backward-element)
  2883. (should (looking-at "- item1"))
  2884. ;; 7.4. At blank-lines before list end: expected to move to top
  2885. ;; item.
  2886. (goto-line 14)
  2887. (org-backward-element)
  2888. (should (looking-at "- item1"))))
  2889. (ert-deftest test-org/up-element ()
  2890. "Test `org-up-element' specifications."
  2891. ;; 1. At BOB or with no surrounding element: should error.
  2892. (org-test-with-temp-text "Paragraph."
  2893. (should-error (org-up-element)))
  2894. (org-test-with-temp-text "* Head1\n* Head2"
  2895. (goto-line 2)
  2896. (should-error (org-up-element)))
  2897. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2898. (goto-line 3)
  2899. (should-error (org-up-element)))
  2900. ;; 2. At an headline: move to parent headline.
  2901. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  2902. (goto-line 3)
  2903. (org-up-element)
  2904. (should (looking-at "\\* Head1")))
  2905. ;; 3. Inside a greater element: move to greater element beginning.
  2906. (org-test-with-temp-text
  2907. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  2908. (goto-line 3)
  2909. (org-up-element)
  2910. (should (looking-at "#\\+BEGIN_CENTER")))
  2911. ;; 4. List tests.
  2912. (org-test-with-temp-text "* Top
  2913. - item1
  2914. - sub1
  2915. - sub2
  2916. Paragraph within sub2.
  2917. - item2"
  2918. ;; 4.1. Within an item: move to the item beginning.
  2919. (goto-line 8)
  2920. (org-up-element)
  2921. (should (looking-at " - sub2"))
  2922. ;; 4.2. At an item in a sub-list: move to parent item.
  2923. (goto-line 4)
  2924. (org-up-element)
  2925. (should (looking-at "- item1"))
  2926. ;; 4.3. At an item in top list: move to beginning of whole list.
  2927. (goto-line 10)
  2928. (org-up-element)
  2929. (should (looking-at "- item1"))
  2930. ;; 4.4. Special case. At very top point: should move to parent of
  2931. ;; list.
  2932. (goto-line 2)
  2933. (org-up-element)
  2934. (should (looking-at "\\* Top"))))
  2935. (ert-deftest test-org/down-element ()
  2936. "Test `org-down-element' specifications."
  2937. ;; Error when the element hasn't got a recursive type.
  2938. (org-test-with-temp-text "Paragraph."
  2939. (should-error (org-down-element)))
  2940. ;; Error when the element has no contents
  2941. (org-test-with-temp-text "* Headline"
  2942. (should-error (org-down-element)))
  2943. ;; When at a plain-list, move to first item.
  2944. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  2945. (goto-line 2)
  2946. (org-down-element)
  2947. (should (looking-at " - Item 1.1")))
  2948. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  2949. (org-down-element)
  2950. (should (looking-at " Item 1")))
  2951. ;; When at a table, move to first row
  2952. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  2953. (org-down-element)
  2954. (should (looking-at " a | b |")))
  2955. ;; Otherwise, move inside the greater element.
  2956. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  2957. (org-down-element)
  2958. (should (looking-at "Paragraph"))))
  2959. (ert-deftest test-org/drag-element-backward ()
  2960. "Test `org-drag-element-backward' specifications."
  2961. ;; Standard test.
  2962. (should
  2963. (equal
  2964. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  2965. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  2966. (org-drag-element-backward)
  2967. (buffer-string))))
  2968. (should
  2969. (equal
  2970. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  2971. (org-test-with-temp-text
  2972. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  2973. (org-drag-element-backward)
  2974. (buffer-string))))
  2975. ;; Preserve blank lines.
  2976. (should
  2977. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  2978. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  2979. (org-drag-element-backward)
  2980. (buffer-string))))
  2981. ;; Preserve column.
  2982. (should
  2983. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  2984. (org-drag-element-backward)
  2985. (looking-at-p "2")))
  2986. ;; Error when trying to move first element of buffer.
  2987. (should-error
  2988. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2989. (org-drag-element-backward)))
  2990. ;; Error when trying to swap nested elements.
  2991. (should-error
  2992. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2993. (forward-line)
  2994. (org-drag-element-backward)))
  2995. ;; Error when trying to swap an headline element and a non-headline
  2996. ;; element.
  2997. (should-error
  2998. (org-test-with-temp-text "Test.\n* Head 1"
  2999. (forward-line)
  3000. (org-drag-element-backward)))
  3001. ;; Preserve visibility of elements and their contents.
  3002. (should
  3003. (equal '((63 . 82) (26 . 48))
  3004. (org-test-with-temp-text "
  3005. #+BEGIN_CENTER
  3006. Text.
  3007. #+END_CENTER
  3008. - item 1
  3009. #+BEGIN_QUOTE
  3010. Text.
  3011. #+END_QUOTE"
  3012. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3013. (search-backward "- item 1")
  3014. (org-drag-element-backward)
  3015. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3016. (overlays-in (point-min) (point-max)))))))
  3017. (ert-deftest test-org/drag-element-forward ()
  3018. "Test `org-drag-element-forward' specifications."
  3019. ;; 1. Error when trying to move first element of buffer.
  3020. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3021. (goto-line 3)
  3022. (should-error (org-drag-element-forward)))
  3023. ;; 2. Error when trying to swap nested elements.
  3024. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3025. (forward-line)
  3026. (should-error (org-drag-element-forward)))
  3027. ;; 3. Error when trying to swap a non-headline element and an
  3028. ;; headline.
  3029. (org-test-with-temp-text "Test.\n* Head 1"
  3030. (should-error (org-drag-element-forward)))
  3031. ;; 4. Otherwise, swap elements, preserving column and blank lines
  3032. ;; between elements.
  3033. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3034. (search-forward "graph")
  3035. (org-drag-element-forward)
  3036. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3037. (should (looking-at " 1")))
  3038. ;; 5. Preserve visibility of elements and their contents.
  3039. (org-test-with-temp-text "
  3040. #+BEGIN_CENTER
  3041. Text.
  3042. #+END_CENTER
  3043. - item 1
  3044. #+BEGIN_QUOTE
  3045. Text.
  3046. #+END_QUOTE"
  3047. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3048. (search-backward "#+BEGIN_CENTER")
  3049. (org-drag-element-forward)
  3050. (should
  3051. (equal
  3052. '((63 . 82) (26 . 48))
  3053. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3054. (overlays-in (point-min) (point-max)))))))
  3055. (ert-deftest test-org/next-block ()
  3056. "Test `org-next-block' specifications."
  3057. ;; Regular test.
  3058. (should
  3059. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3060. (org-next-block 1)
  3061. (looking-at "#\\+BEGIN_CENTER")))
  3062. ;; Ignore case.
  3063. (should
  3064. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3065. (let ((case-fold-search nil))
  3066. (org-next-block 1)
  3067. (looking-at "#\\+begin_center"))))
  3068. ;; Ignore current line.
  3069. (should
  3070. (org-test-with-temp-text
  3071. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3072. (org-next-block 1)
  3073. (looking-at "#\\+BEGIN_CENTER")))
  3074. ;; Throw an error when no block is found.
  3075. (should-error
  3076. (org-test-with-temp-text "Paragraph"
  3077. (org-next-block 1)))
  3078. ;; With an argument, skip many blocks at once.
  3079. (should
  3080. (org-test-with-temp-text
  3081. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3082. (org-next-block 2)
  3083. (looking-at "#\\+BEGIN_QUOTE")))
  3084. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3085. (should
  3086. (org-test-with-temp-text
  3087. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3088. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3089. (looking-at "#\\+BEGIN_QUOTE")))
  3090. ;; Optional argument is also case-insensitive.
  3091. (should
  3092. (org-test-with-temp-text
  3093. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3094. (let ((case-fold-search nil))
  3095. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3096. (looking-at "#\\+begin_quote")))))
  3097. (ert-deftest test-org/previous-block ()
  3098. "Test `org-previous-block' specifications."
  3099. ;; Regular test.
  3100. (should
  3101. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  3102. (org-previous-block 1)
  3103. (looking-at "#\\+BEGIN_CENTER")))
  3104. ;; Ignore case.
  3105. (should
  3106. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  3107. (let ((case-fold-search nil))
  3108. (org-previous-block 1)
  3109. (looking-at "#\\+begin_center"))))
  3110. ;; Ignore current line.
  3111. (should
  3112. (org-test-with-temp-text
  3113. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  3114. (org-previous-block 1)
  3115. (looking-at "#\\+BEGIN_QUOTE")))
  3116. ;; Throw an error when no block is found.
  3117. (should-error
  3118. (org-test-with-temp-text "Paragraph<point>"
  3119. (org-previous-block 1)))
  3120. ;; With an argument, skip many blocks at once.
  3121. (should
  3122. (org-test-with-temp-text
  3123. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3124. (org-previous-block 2)
  3125. (looking-at "#\\+BEGIN_CENTER")))
  3126. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3127. (should
  3128. (org-test-with-temp-text
  3129. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3130. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3131. (looking-at "#\\+BEGIN_QUOTE")))
  3132. ;; Optional argument is also case-insensitive.
  3133. (should
  3134. (org-test-with-temp-text
  3135. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  3136. (let ((case-fold-search nil))
  3137. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3138. (looking-at "#\\+begin_quote")))))
  3139. ;;; Outline structure
  3140. (ert-deftest test-org/demote ()
  3141. "Test `org-demote' specifications."
  3142. ;; Add correct number of stars according to `org-odd-levels-only'.
  3143. (should
  3144. (= 2
  3145. (org-test-with-temp-text "* H"
  3146. (let ((org-odd-levels-only nil)) (org-demote))
  3147. (org-current-level))))
  3148. (should
  3149. (= 3
  3150. (org-test-with-temp-text "* H"
  3151. (let ((org-odd-levels-only t)) (org-demote))
  3152. (org-current-level))))
  3153. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3154. (should
  3155. (org-test-with-temp-text "* H :tag:"
  3156. (let ((org-tags-column 10)
  3157. (org-auto-align-tags t)
  3158. (org-odd-levels-only nil))
  3159. (org-demote))
  3160. (org-move-to-column 10)
  3161. (looking-at-p ":tag:$")))
  3162. (should-not
  3163. (org-test-with-temp-text "* H :tag:"
  3164. (let ((org-tags-column 10)
  3165. (org-auto-align-tags nil)
  3166. (org-odd-levels-only nil))
  3167. (org-demote))
  3168. (org-move-to-column 10)
  3169. (looking-at-p ":tag:$")))
  3170. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3171. ;; info and property drawers accordingly.
  3172. (should
  3173. (= 3
  3174. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3175. (let ((org-odd-levels-only nil)
  3176. (org-adapt-indentation t))
  3177. (org-demote))
  3178. (forward-line)
  3179. (org-get-indentation))))
  3180. (should
  3181. (= 3
  3182. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3183. (let ((org-odd-levels-only nil)
  3184. (org-adapt-indentation t))
  3185. (org-demote))
  3186. (forward-line)
  3187. (org-get-indentation))))
  3188. (should-not
  3189. (= 3
  3190. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3191. (let ((org-odd-levels-only nil)
  3192. (org-adapt-indentation nil))
  3193. (org-demote))
  3194. (forward-line)
  3195. (org-get-indentation))))
  3196. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3197. ;; section accordingly. Ignore, however, footnote definitions and
  3198. ;; inlinetasks boundaries.
  3199. (should
  3200. (= 3
  3201. (org-test-with-temp-text "* H\n Paragraph"
  3202. (let ((org-odd-levels-only nil)
  3203. (org-adapt-indentation t))
  3204. (org-demote))
  3205. (forward-line)
  3206. (org-get-indentation))))
  3207. (should
  3208. (= 2
  3209. (org-test-with-temp-text "* H\n Paragraph"
  3210. (let ((org-odd-levels-only nil)
  3211. (org-adapt-indentation nil))
  3212. (org-demote))
  3213. (forward-line)
  3214. (org-get-indentation))))
  3215. (should
  3216. (zerop
  3217. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  3218. (let ((org-odd-levels-only nil)
  3219. (org-adapt-indentation t))
  3220. (org-demote))
  3221. (goto-char (point-max))
  3222. (org-get-indentation))))
  3223. (should
  3224. (= 3
  3225. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  3226. (let ((org-odd-levels-only nil)
  3227. (org-adapt-indentation t))
  3228. (org-demote))
  3229. (goto-char (point-max))
  3230. (org-get-indentation))))
  3231. (when (featurep 'org-inlinetask)
  3232. (should
  3233. (zerop
  3234. (let ((org-inlinetask-min-level 5)
  3235. (org-adapt-indentation t))
  3236. (org-test-with-temp-text "* H\n***** I\n***** END"
  3237. (org-demote)
  3238. (forward-line)
  3239. (org-get-indentation))))))
  3240. (when (featurep 'org-inlinetask)
  3241. (should
  3242. (= 3
  3243. (let ((org-inlinetask-min-level 5)
  3244. (org-adapt-indentation t))
  3245. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  3246. (org-demote)
  3247. (forward-line 2)
  3248. (org-get-indentation))))))
  3249. ;; Ignore contents of source blocks or example blocks when
  3250. ;; indentation should be preserved (through
  3251. ;; `org-src-preserve-indentation' or "-i" flag).
  3252. (should-not
  3253. (zerop
  3254. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3255. (let ((org-adapt-indentation t)
  3256. (org-src-preserve-indentation nil))
  3257. (org-demote))
  3258. (forward-line 2)
  3259. (org-get-indentation))))
  3260. (should
  3261. (zerop
  3262. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3263. (let ((org-adapt-indentation t)
  3264. (org-src-preserve-indentation t))
  3265. (org-demote))
  3266. (forward-line 2)
  3267. (org-get-indentation))))
  3268. (should
  3269. (zerop
  3270. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3271. (let ((org-adapt-indentation t)
  3272. (org-src-preserve-indentation t))
  3273. (org-demote))
  3274. (forward-line 2)
  3275. (org-get-indentation))))
  3276. (should
  3277. (zerop
  3278. (org-test-with-temp-text
  3279. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  3280. (let ((org-adapt-indentation t)
  3281. (org-src-preserve-indentation nil))
  3282. (org-demote))
  3283. (forward-line 2)
  3284. (org-get-indentation)))))
  3285. (ert-deftest test-org/promote ()
  3286. "Test `org-promote' specifications."
  3287. ;; Return an error if headline is to be promoted to level 0, unless
  3288. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  3289. ;; headline becomes a comment.
  3290. (should-error
  3291. (org-test-with-temp-text "* H"
  3292. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  3293. (should
  3294. (equal "# H"
  3295. (org-test-with-temp-text "* H"
  3296. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  3297. (buffer-string))))
  3298. ;; Remove correct number of stars according to
  3299. ;; `org-odd-levels-only'.
  3300. (should
  3301. (= 2
  3302. (org-test-with-temp-text "*** H"
  3303. (let ((org-odd-levels-only nil)) (org-promote))
  3304. (org-current-level))))
  3305. (should
  3306. (= 1
  3307. (org-test-with-temp-text "*** H"
  3308. (let ((org-odd-levels-only t)) (org-promote))
  3309. (org-current-level))))
  3310. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3311. (should
  3312. (org-test-with-temp-text "** H :tag:"
  3313. (let ((org-tags-column 10)
  3314. (org-auto-align-tags t)
  3315. (org-odd-levels-only nil))
  3316. (org-promote))
  3317. (org-move-to-column 10)
  3318. (looking-at-p ":tag:$")))
  3319. (should-not
  3320. (org-test-with-temp-text "** H :tag:"
  3321. (let ((org-tags-column 10)
  3322. (org-auto-align-tags nil)
  3323. (org-odd-levels-only nil))
  3324. (org-promote))
  3325. (org-move-to-column 10)
  3326. (looking-at-p ":tag:$")))
  3327. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3328. ;; info and property drawers.
  3329. (should
  3330. (= 2
  3331. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3332. (let ((org-odd-levels-only nil)
  3333. (org-adapt-indentation t))
  3334. (org-promote))
  3335. (forward-line)
  3336. (org-get-indentation))))
  3337. (should
  3338. (= 2
  3339. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3340. (let ((org-odd-levels-only nil)
  3341. (org-adapt-indentation t))
  3342. (org-promote))
  3343. (forward-line)
  3344. (org-get-indentation))))
  3345. (should-not
  3346. (= 2
  3347. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3348. (let ((org-odd-levels-only nil)
  3349. (org-adapt-indentation nil))
  3350. (org-promote))
  3351. (forward-line)
  3352. (org-get-indentation))))
  3353. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3354. ;; section accordingly. Ignore, however, footnote definitions and
  3355. ;; inlinetasks boundaries.
  3356. (should
  3357. (= 2
  3358. (org-test-with-temp-text "** H\n Paragraph"
  3359. (let ((org-odd-levels-only nil)
  3360. (org-adapt-indentation t))
  3361. (org-promote))
  3362. (forward-line)
  3363. (org-get-indentation))))
  3364. (should-not
  3365. (= 2
  3366. (org-test-with-temp-text "** H\n Paragraph"
  3367. (let ((org-odd-levels-only nil)
  3368. (org-adapt-indentation nil))
  3369. (org-promote))
  3370. (forward-line)
  3371. (org-get-indentation))))
  3372. (should
  3373. (= 2
  3374. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  3375. (let ((org-odd-levels-only nil)
  3376. (org-adapt-indentation t))
  3377. (org-promote))
  3378. (forward-line)
  3379. (org-get-indentation))))
  3380. (when (featurep 'org-inlinetask)
  3381. (should
  3382. (zerop
  3383. (let ((org-inlinetask-min-level 5)
  3384. (org-adapt-indentation t))
  3385. (org-test-with-temp-text "** H\n***** I\n***** END"
  3386. (org-promote)
  3387. (forward-line)
  3388. (org-get-indentation))))))
  3389. (when (featurep 'org-inlinetask)
  3390. (should
  3391. (= 2
  3392. (let ((org-inlinetask-min-level 5)
  3393. (org-adapt-indentation t))
  3394. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  3395. (org-promote)
  3396. (forward-line 2)
  3397. (org-get-indentation))))))
  3398. ;; Give up shifting if it would break document's structure
  3399. ;; otherwise.
  3400. (should
  3401. (= 3
  3402. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  3403. (let ((org-odd-levels-only nil)
  3404. (org-adapt-indentation t))
  3405. (org-promote))
  3406. (forward-line)
  3407. (org-get-indentation))))
  3408. (should
  3409. (= 3
  3410. (org-test-with-temp-text "** H\n Paragraph\n * list."
  3411. (let ((org-odd-levels-only nil)
  3412. (org-adapt-indentation t))
  3413. (org-promote))
  3414. (forward-line)
  3415. (org-get-indentation))))
  3416. ;; Ignore contents of source blocks or example blocks when
  3417. ;; indentation should be preserved (through
  3418. ;; `org-src-preserve-indentation' or "-i" flag).
  3419. (should-not
  3420. (zerop
  3421. (org-test-with-temp-text
  3422. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3423. (let ((org-adapt-indentation t)
  3424. (org-src-preserve-indentation nil)
  3425. (org-odd-levels-only nil))
  3426. (org-promote))
  3427. (forward-line)
  3428. (org-get-indentation))))
  3429. (should
  3430. (zerop
  3431. (org-test-with-temp-text
  3432. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  3433. (let ((org-adapt-indentation t)
  3434. (org-src-preserve-indentation t)
  3435. (org-odd-levels-only nil))
  3436. (org-promote))
  3437. (forward-line)
  3438. (org-get-indentation))))
  3439. (should
  3440. (zerop
  3441. (org-test-with-temp-text
  3442. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3443. (let ((org-adapt-indentation t)
  3444. (org-src-preserve-indentation t)
  3445. (org-odd-levels-only nil))
  3446. (org-promote))
  3447. (forward-line)
  3448. (org-get-indentation))))
  3449. (should
  3450. (zerop
  3451. (org-test-with-temp-text
  3452. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  3453. (let ((org-adapt-indentation t)
  3454. (org-src-preserve-indentation nil)
  3455. (org-odd-levels-only nil))
  3456. (org-promote))
  3457. (forward-line)
  3458. (org-get-indentation)))))
  3459. ;;; Planning
  3460. (ert-deftest test-org/at-planning-p ()
  3461. "Test `org-at-planning-p' specifications."
  3462. ;; Regular test.
  3463. (should
  3464. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  3465. (org-at-planning-p)))
  3466. (should-not
  3467. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  3468. (org-at-planning-p)))
  3469. ;; Correctly find planning attached to inlinetasks.
  3470. (when (featurep 'org-inlinetask)
  3471. (should
  3472. (org-test-with-temp-text
  3473. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  3474. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3475. (should-not
  3476. (org-test-with-temp-text
  3477. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3478. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3479. (should-not
  3480. (org-test-with-temp-text
  3481. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3482. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3483. (should-not
  3484. (org-test-with-temp-text
  3485. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  3486. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  3487. (ert-deftest test-org/add-planning-info ()
  3488. "Test `org-add-planning-info'."
  3489. ;; Create deadline when `org-adapt-indentation' is non-nil.
  3490. (should
  3491. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3492. (org-test-with-temp-text "* H\nParagraph<point>"
  3493. (let ((org-adapt-indentation t))
  3494. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3495. (replace-regexp-in-string
  3496. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3497. nil nil 1))))
  3498. ;; Create deadline when `org-adapt-indentation' is nil.
  3499. (should
  3500. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3501. (org-test-with-temp-text "* H\nParagraph<point>"
  3502. (let ((org-adapt-indentation nil))
  3503. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3504. (replace-regexp-in-string
  3505. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3506. nil nil 1))))
  3507. ;; Update deadline when `org-adapt-indentation' is non-nil.
  3508. (should
  3509. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3510. (org-test-with-temp-text "\
  3511. * H
  3512. DEADLINE: <2015-06-24 Wed>
  3513. Paragraph<point>"
  3514. (let ((org-adapt-indentation t))
  3515. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3516. (replace-regexp-in-string
  3517. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3518. nil nil 1))))
  3519. ;; Update deadline when `org-adapt-indentation' is nil.
  3520. (should
  3521. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3522. (org-test-with-temp-text "\
  3523. * H
  3524. DEADLINE: <2015-06-24 Wed>
  3525. Paragraph<point>"
  3526. (let ((org-adapt-indentation nil))
  3527. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3528. (replace-regexp-in-string
  3529. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3530. nil nil 1))))
  3531. ;; Schedule when `org-adapt-indentation' is non-nil.
  3532. (should
  3533. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  3534. (org-test-with-temp-text "* H\nParagraph<point>"
  3535. (let ((org-adapt-indentation t))
  3536. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3537. (replace-regexp-in-string
  3538. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3539. nil nil 1))))
  3540. ;; Schedule when `org-adapt-indentation' is nil.
  3541. (should
  3542. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  3543. (org-test-with-temp-text "* H\nParagraph<point>"
  3544. (let ((org-adapt-indentation nil))
  3545. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3546. (replace-regexp-in-string
  3547. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3548. nil nil 1))))
  3549. ;; Add deadline when scheduled.
  3550. (should
  3551. (equal "\
  3552. * H
  3553. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  3554. Paragraph"
  3555. (org-test-with-temp-text "\
  3556. * H
  3557. SCHEDULED: <2015-06-24 Wed>
  3558. Paragraph<point>"
  3559. (let ((org-adapt-indentation t))
  3560. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3561. (replace-regexp-in-string
  3562. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3563. nil nil 1))))
  3564. ;; Remove middle entry.
  3565. (should
  3566. (equal "\
  3567. * H
  3568. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  3569. Paragraph"
  3570. (org-test-with-temp-text "\
  3571. * H
  3572. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3573. Paragraph<point>"
  3574. (let ((org-adapt-indentation t))
  3575. (org-add-planning-info nil nil 'deadline))
  3576. (replace-regexp-in-string
  3577. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3578. nil nil 1))))
  3579. ;; Remove last entry and then middle entry (order should not
  3580. ;; matter).
  3581. (should
  3582. (equal "\
  3583. * H
  3584. CLOSED: [2015-06-24]
  3585. Paragraph"
  3586. (org-test-with-temp-text "\
  3587. * H
  3588. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3589. Paragraph<point>"
  3590. (let ((org-adapt-indentation t))
  3591. (org-add-planning-info nil nil 'scheduled 'deadline))
  3592. (replace-regexp-in-string
  3593. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3594. nil nil 1))))
  3595. ;; Remove closed when `org-adapt-indentation' is non-nil.
  3596. (should
  3597. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3598. (org-test-with-temp-text "\
  3599. * H
  3600. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3601. Paragraph<point>"
  3602. (let ((org-adapt-indentation t))
  3603. (org-add-planning-info nil nil 'closed))
  3604. (replace-regexp-in-string
  3605. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3606. nil nil 1))))
  3607. (should
  3608. (equal "* H\n Paragraph"
  3609. (org-test-with-temp-text "\
  3610. * H
  3611. CLOSED: [2015-06-25 Thu]
  3612. Paragraph<point>"
  3613. (let ((org-adapt-indentation t))
  3614. (org-add-planning-info nil nil 'closed))
  3615. (replace-regexp-in-string
  3616. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3617. nil nil 1))))
  3618. ;; Remove closed when `org-adapt-indentation' is nil.
  3619. (should
  3620. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3621. (org-test-with-temp-text "\
  3622. * H
  3623. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3624. Paragraph<point>"
  3625. (let ((org-adapt-indentation nil))
  3626. (org-add-planning-info nil nil 'closed))
  3627. (replace-regexp-in-string
  3628. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3629. nil nil 1))))
  3630. (should
  3631. (equal "* H\nParagraph"
  3632. (org-test-with-temp-text "\
  3633. * H
  3634. CLOSED: [2015-06-25 Thu]
  3635. Paragraph<point>"
  3636. (let ((org-adapt-indentation nil))
  3637. (org-add-planning-info nil nil 'closed))
  3638. (replace-regexp-in-string
  3639. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3640. nil nil 1))))
  3641. ;; Remove closed entry and delete empty line.
  3642. (should
  3643. (equal "\
  3644. * H
  3645. Paragraph"
  3646. (org-test-with-temp-text "\
  3647. * H
  3648. CLOSED: [2015-06-24 Wed]
  3649. Paragraph<point>"
  3650. (let ((org-adapt-indentation t))
  3651. (org-add-planning-info nil nil 'closed))
  3652. (replace-regexp-in-string
  3653. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3654. nil nil 1))))
  3655. ;; Remove one entry and update another.
  3656. (should
  3657. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3658. (org-test-with-temp-text "\
  3659. * H
  3660. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  3661. Paragraph<point>"
  3662. (let ((org-adapt-indentation t))
  3663. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  3664. (replace-regexp-in-string
  3665. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3666. nil nil 1)))))
  3667. ;;; Property API
  3668. (ert-deftest test-org/buffer-property-keys ()
  3669. "Test `org-buffer-property-keys' specifications."
  3670. ;; Retrieve properties accross siblings.
  3671. (should
  3672. (equal '("A" "B")
  3673. (org-test-with-temp-text "
  3674. * H1
  3675. :PROPERTIES:
  3676. :A: 1
  3677. :END:
  3678. * H2
  3679. :PROPERTIES:
  3680. :B: 1
  3681. :END:"
  3682. (org-buffer-property-keys))))
  3683. ;; Retrieve properties accross children.
  3684. (should
  3685. (equal '("A" "B")
  3686. (org-test-with-temp-text "
  3687. * H1
  3688. :PROPERTIES:
  3689. :A: 1
  3690. :END:
  3691. ** H2
  3692. :PROPERTIES:
  3693. :B: 1
  3694. :END:"
  3695. (org-buffer-property-keys))))
  3696. ;; Retrieve muliple properties in the same drawer.
  3697. (should
  3698. (equal '("A" "B")
  3699. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3700. (org-buffer-property-keys))))
  3701. ;; Ignore extension symbol in property name.
  3702. (should
  3703. (equal '("A")
  3704. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3705. (org-buffer-property-keys))))
  3706. ;; With non-nil COLUMNS, extract property names from columns.
  3707. (should
  3708. (equal '("A" "B")
  3709. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  3710. (org-buffer-property-keys nil nil t))))
  3711. (should
  3712. (equal '("A" "B" "COLUMNS")
  3713. (org-test-with-temp-text
  3714. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  3715. (org-buffer-property-keys nil nil t))))
  3716. ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
  3717. (should
  3718. (equal '("A")
  3719. (org-test-with-temp-text
  3720. "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
  3721. (org-buffer-property-keys nil nil nil t)))))
  3722. (ert-deftest test-org/property-values ()
  3723. "Test `org-property-values' specifications."
  3724. ;; Regular test.
  3725. (should
  3726. (equal '("2" "1")
  3727. (org-test-with-temp-text
  3728. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  3729. (org-property-values "A"))))
  3730. ;; Ignore empty values.
  3731. (should-not
  3732. (org-test-with-temp-text
  3733. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  3734. (org-property-values "A")))
  3735. ;; Take into consideration extended values.
  3736. (should
  3737. (equal '("1 2")
  3738. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3739. (org-property-values "A")))))
  3740. (ert-deftest test-org/find-property ()
  3741. "Test `org-find-property' specifications."
  3742. ;; Regular test.
  3743. (should
  3744. (= 1
  3745. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  3746. (org-find-property "prop"))))
  3747. ;; Ignore false positives.
  3748. (should
  3749. (= 27
  3750. (org-test-with-temp-text
  3751. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  3752. (org-find-property "A"))))
  3753. ;; Return first entry found in buffer.
  3754. (should
  3755. (= 1
  3756. (org-test-with-temp-text
  3757. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3758. (org-find-property "A"))))
  3759. ;; Only search visible part of the buffer.
  3760. (should
  3761. (= 31
  3762. (org-test-with-temp-text
  3763. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3764. (org-narrow-to-subtree)
  3765. (org-find-property "A"))))
  3766. ;; With optional argument, only find entries with a specific value.
  3767. (should-not
  3768. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3769. (org-find-property "A" "2")))
  3770. (should
  3771. (= 31
  3772. (org-test-with-temp-text
  3773. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  3774. (org-find-property "A" "2"))))
  3775. ;; Use "nil" for explicit nil values.
  3776. (should
  3777. (= 31
  3778. (org-test-with-temp-text
  3779. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  3780. (org-find-property "A" "nil")))))
  3781. (ert-deftest test-org/entry-delete ()
  3782. "Test `org-entry-delete' specifications."
  3783. ;; Regular test.
  3784. (should
  3785. (string-match
  3786. " *:PROPERTIES:\n *:B: +2\n *:END:"
  3787. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3788. (org-entry-delete (point) "A")
  3789. (buffer-string))))
  3790. ;; Also remove accumulated properties.
  3791. (should-not
  3792. (string-match
  3793. ":A"
  3794. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  3795. (org-entry-delete (point) "A")
  3796. (buffer-string))))
  3797. ;; When last property is removed, remove the property drawer.
  3798. (should-not
  3799. (string-match
  3800. ":PROPERTIES:"
  3801. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  3802. (org-entry-delete (point) "A")
  3803. (buffer-string))))
  3804. ;; Return a non-nil value when some property was removed.
  3805. (should
  3806. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3807. (org-entry-delete (point) "A")))
  3808. (should-not
  3809. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3810. (org-entry-delete (point) "C"))))
  3811. (ert-deftest test-org/entry-get ()
  3812. "Test `org-entry-get' specifications."
  3813. ;; Regular test.
  3814. (should
  3815. (equal "1"
  3816. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3817. (org-entry-get (point) "A"))))
  3818. ;; Ignore case.
  3819. (should
  3820. (equal "1"
  3821. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3822. (org-entry-get (point) "a"))))
  3823. ;; Handle extended values, both before and after base value.
  3824. (should
  3825. (equal "1 2 3"
  3826. (org-test-with-temp-text
  3827. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3828. (org-entry-get (point) "A"))))
  3829. ;; Empty values are returned as the empty string.
  3830. (should
  3831. (equal ""
  3832. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  3833. (org-entry-get (point) "A"))))
  3834. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  3835. ;; otherwise, return nil.
  3836. (should-not
  3837. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3838. (org-entry-get (point) "A")))
  3839. (should
  3840. (equal "nil"
  3841. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3842. (org-entry-get (point) "A" nil t))))
  3843. ;; Return nil when no property is found, independently on the
  3844. ;; LITERAL-NIL argument.
  3845. (should-not
  3846. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3847. (org-entry-get (point) "B")))
  3848. (should-not
  3849. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3850. (org-entry-get (point) "B" nil t)))
  3851. ;; Handle inheritance, when allowed. Include extended values and
  3852. ;; possibly global values.
  3853. (should
  3854. (equal
  3855. "1"
  3856. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3857. (org-entry-get (point) "A" t))))
  3858. (should
  3859. (equal
  3860. "1"
  3861. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3862. (let ((org-use-property-inheritance t))
  3863. (org-entry-get (point) "A" 'selective)))))
  3864. (should-not
  3865. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3866. (let ((org-use-property-inheritance nil))
  3867. (org-entry-get (point) "A" 'selective))))
  3868. (should
  3869. (equal
  3870. "1 2"
  3871. (org-test-with-temp-text
  3872. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  3873. (org-entry-get (point-max) "A" t))))
  3874. (should
  3875. (equal "1"
  3876. (org-test-with-temp-text
  3877. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  3878. (org-mode-restart)
  3879. (org-entry-get (point-max) "A" t))))
  3880. (should
  3881. (equal "0 1"
  3882. (org-test-with-temp-text
  3883. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  3884. (org-mode-restart)
  3885. (org-entry-get (point-max) "A" t)))))
  3886. (ert-deftest test-org/entry-properties ()
  3887. "Test `org-entry-properties' specifications."
  3888. ;; Get "ITEM" property.
  3889. (should
  3890. (equal "H"
  3891. (org-test-with-temp-text "* TODO H"
  3892. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  3893. (should
  3894. (equal "H"
  3895. (org-test-with-temp-text "* TODO H"
  3896. (cdr (assoc "ITEM" (org-entry-properties))))))
  3897. ;; Get "TODO" property. TODO keywords are case sensitive.
  3898. (should
  3899. (equal "TODO"
  3900. (org-test-with-temp-text "* TODO H"
  3901. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  3902. (should
  3903. (equal "TODO"
  3904. (org-test-with-temp-text "* TODO H"
  3905. (cdr (assoc "TODO" (org-entry-properties))))))
  3906. (should-not
  3907. (org-test-with-temp-text "* H"
  3908. (assoc "TODO" (org-entry-properties nil "TODO"))))
  3909. (should-not
  3910. (org-test-with-temp-text "* todo H"
  3911. (assoc "TODO" (org-entry-properties nil "TODO"))))
  3912. ;; Get "PRIORITY" property.
  3913. (should
  3914. (equal "A"
  3915. (org-test-with-temp-text "* [#A] H"
  3916. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  3917. (should
  3918. (equal "A"
  3919. (org-test-with-temp-text "* [#A] H"
  3920. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  3921. (should
  3922. (equal (char-to-string org-default-priority)
  3923. (org-test-with-temp-text "* H"
  3924. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  3925. ;; Get "FILE" property.
  3926. (should
  3927. (org-test-with-temp-text-in-file "* H\nParagraph"
  3928. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  3929. (buffer-file-name))))
  3930. (should
  3931. (org-test-with-temp-text-in-file "* H\nParagraph"
  3932. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  3933. (buffer-file-name))))
  3934. (should-not
  3935. (org-test-with-temp-text "* H\nParagraph"
  3936. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  3937. ;; Get "TAGS" property.
  3938. (should
  3939. (equal ":tag1:tag2:"
  3940. (org-test-with-temp-text "* H :tag1:tag2:"
  3941. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  3942. (should
  3943. (equal ":tag1:tag2:"
  3944. (org-test-with-temp-text "* H :tag1:tag2:"
  3945. (cdr (assoc "TAGS" (org-entry-properties))))))
  3946. (should-not
  3947. (org-test-with-temp-text "* H"
  3948. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  3949. ;; Get "ALLTAGS" property.
  3950. (should
  3951. (equal ":tag1:tag2:"
  3952. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  3953. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  3954. (should
  3955. (equal ":tag1:tag2:"
  3956. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  3957. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  3958. (should-not
  3959. (org-test-with-temp-text "* H"
  3960. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  3961. ;; Get "BLOCKED" property.
  3962. (should
  3963. (equal "t"
  3964. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  3965. (let ((org-enforce-todo-dependencies t)
  3966. (org-blocker-hook
  3967. '(org-block-todo-from-children-or-siblings-or-parent)))
  3968. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  3969. (should
  3970. (equal ""
  3971. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  3972. (let ((org-enforce-todo-dependencies t)
  3973. (org-blocker-hook
  3974. '(org-block-todo-from-children-or-siblings-or-parent)))
  3975. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  3976. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  3977. (should
  3978. (equal
  3979. "[2012-03-29 thu.]"
  3980. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  3981. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  3982. (should
  3983. (equal
  3984. "[2012-03-29 thu.]"
  3985. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  3986. (cdr (assoc "CLOSED" (org-entry-properties))))))
  3987. (should-not
  3988. (org-test-with-temp-text "* H"
  3989. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  3990. (should
  3991. (equal
  3992. "<2014-03-04 tue.>"
  3993. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3994. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  3995. (should
  3996. (equal
  3997. "<2014-03-04 tue.>"
  3998. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3999. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  4000. (should-not
  4001. (org-test-with-temp-text "* H"
  4002. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  4003. (should
  4004. (equal
  4005. "<2014-03-04 tue.>"
  4006. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4007. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  4008. (should
  4009. (equal
  4010. "<2014-03-04 tue.>"
  4011. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4012. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  4013. (should-not
  4014. (org-test-with-temp-text "* H"
  4015. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  4016. ;; Get "CATEGORY"
  4017. (should
  4018. (equal "cat"
  4019. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4020. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  4021. (should
  4022. (equal "cat"
  4023. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4024. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4025. (should
  4026. (equal "cat"
  4027. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  4028. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4029. (should
  4030. (equal "cat2"
  4031. (org-test-with-temp-text
  4032. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  4033. "\n"
  4034. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  4035. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4036. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  4037. (should
  4038. (equal "<2012-03-29 thu.>"
  4039. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  4040. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  4041. (should
  4042. (equal "[2012-03-29 thu.]"
  4043. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  4044. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  4045. (should
  4046. (equal "<2012-03-29 thu.>"
  4047. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  4048. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  4049. (should
  4050. (equal "[2014-03-04 tue.]"
  4051. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  4052. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
  4053. ;; Get standard properties.
  4054. (should
  4055. (equal "1"
  4056. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4057. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4058. ;; Handle extended properties.
  4059. (should
  4060. (equal "1 2 3"
  4061. (org-test-with-temp-text
  4062. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  4063. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4064. (should
  4065. (equal "1 2 3"
  4066. (org-test-with-temp-text
  4067. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  4068. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4069. ;; Ignore forbidden (special) properties.
  4070. (should-not
  4071. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  4072. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  4073. (ert-deftest test-org/entry-put ()
  4074. "Test `org-entry-put' specifications."
  4075. ;; Error when not a string or nil.
  4076. (should-error
  4077. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4078. (org-entry-put 1 "test" 2)))
  4079. ;; Error when property name is invalid.
  4080. (should-error
  4081. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4082. (org-entry-put 1 "no space" "value")))
  4083. (should-error
  4084. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4085. (org-entry-put 1 "" "value")))
  4086. ;; Set "TODO" property.
  4087. (should
  4088. (string-match (regexp-quote " TODO H")
  4089. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  4090. (org-entry-put (point) "TODO" "TODO")
  4091. (buffer-string))))
  4092. (should
  4093. (string-match (regexp-quote "* H")
  4094. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  4095. (org-entry-put (point) "TODO" nil)
  4096. (buffer-string))))
  4097. ;; Set "PRIORITY" property.
  4098. (should
  4099. (equal "* [#A] H"
  4100. (org-test-with-temp-text "* [#B] H"
  4101. (org-entry-put (point) "PRIORITY" "A")
  4102. (buffer-string))))
  4103. (should
  4104. (equal "* H"
  4105. (org-test-with-temp-text "* [#B] H"
  4106. (org-entry-put (point) "PRIORITY" nil)
  4107. (buffer-string))))
  4108. ;; Set "SCHEDULED" property.
  4109. (should
  4110. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  4111. (org-test-with-temp-text "* H"
  4112. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  4113. (buffer-string))))
  4114. (should
  4115. (string= "* H\n"
  4116. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4117. (org-entry-put (point) "SCHEDULED" nil)
  4118. (buffer-string))))
  4119. (should
  4120. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  4121. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4122. (org-entry-put (point) "SCHEDULED" "earlier")
  4123. (buffer-string))))
  4124. (should
  4125. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  4126. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4127. (org-entry-put (point) "SCHEDULED" "later")
  4128. (buffer-string))))
  4129. ;; Set "DEADLINE" property.
  4130. (should
  4131. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  4132. (org-test-with-temp-text "* H"
  4133. (org-entry-put (point) "DEADLINE" "2014-03-04")
  4134. (buffer-string))))
  4135. (should
  4136. (string= "* H\n"
  4137. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4138. (org-entry-put (point) "DEADLINE" nil)
  4139. (buffer-string))))
  4140. (should
  4141. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  4142. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4143. (org-entry-put (point) "DEADLINE" "earlier")
  4144. (buffer-string))))
  4145. (should
  4146. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  4147. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4148. (org-entry-put (point) "DEADLINE" "later")
  4149. (buffer-string))))
  4150. ;; Set "CATEGORY" property
  4151. (should
  4152. (string-match "^ *:CATEGORY: cat"
  4153. (org-test-with-temp-text "* H"
  4154. (org-entry-put (point) "CATEGORY" "cat")
  4155. (buffer-string))))
  4156. ;; Regular properties, with or without pre-existing drawer.
  4157. (should
  4158. (string-match "^ *:A: +2$"
  4159. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4160. (org-entry-put (point) "A" "2")
  4161. (buffer-string))))
  4162. (should
  4163. (string-match "^ *:A: +1$"
  4164. (org-test-with-temp-text "* H"
  4165. (org-entry-put (point) "A" "1")
  4166. (buffer-string))))
  4167. ;; Special case: two consecutive headlines.
  4168. (should
  4169. (string-match "\\* A\n *:PROPERTIES:"
  4170. (org-test-with-temp-text "* A\n** B"
  4171. (org-entry-put (point) "A" "1")
  4172. (buffer-string)))))
  4173. ;;; Radio Targets
  4174. (ert-deftest test-org/update-radio-target-regexp ()
  4175. "Test `org-update-radio-target-regexp' specifications."
  4176. ;; Properly update cache with no previous radio target regexp.
  4177. (should
  4178. (eq 'link
  4179. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4180. (save-excursion (goto-char (point-max)) (org-element-context))
  4181. (insert "<<<")
  4182. (search-forward "o")
  4183. (insert ">>>")
  4184. (org-update-radio-target-regexp)
  4185. (goto-char (point-max))
  4186. (org-element-type (org-element-context)))))
  4187. ;; Properly update cache with previous radio target regexp.
  4188. (should
  4189. (eq 'link
  4190. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4191. (save-excursion (goto-char (point-max)) (org-element-context))
  4192. (insert "<<<")
  4193. (search-forward "o")
  4194. (insert ">>>")
  4195. (org-update-radio-target-regexp)
  4196. (search-backward "r")
  4197. (delete-char 5)
  4198. (insert "new")
  4199. (org-update-radio-target-regexp)
  4200. (goto-char (point-max))
  4201. (delete-region (line-beginning-position) (point))
  4202. (insert "new")
  4203. (org-element-type (org-element-context))))))
  4204. ;;; Sparse trees
  4205. (ert-deftest test-org/match-sparse-tree ()
  4206. "Test `org-match-sparse-tree' specifications."
  4207. ;; Match tags.
  4208. (should-not
  4209. (org-test-with-temp-text "* H\n** H1 :tag:"
  4210. (org-match-sparse-tree nil "tag")
  4211. (search-forward "H1")
  4212. (org-invisible-p2)))
  4213. (should
  4214. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  4215. (org-match-sparse-tree nil "tag")
  4216. (search-forward "H2")
  4217. (org-invisible-p2)))
  4218. ;; "-" operator for tags.
  4219. (should-not
  4220. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4221. (org-match-sparse-tree nil "tag1-tag2")
  4222. (search-forward "H1")
  4223. (org-invisible-p2)))
  4224. (should
  4225. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4226. (org-match-sparse-tree nil "tag1-tag2")
  4227. (search-forward "H2")
  4228. (org-invisible-p2)))
  4229. ;; "&" operator for tags.
  4230. (should
  4231. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4232. (org-match-sparse-tree nil "tag1&tag2")
  4233. (search-forward "H1")
  4234. (org-invisible-p2)))
  4235. (should-not
  4236. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4237. (org-match-sparse-tree nil "tag1&tag2")
  4238. (search-forward "H2")
  4239. (org-invisible-p2)))
  4240. ;; "|" operator for tags.
  4241. (should-not
  4242. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4243. (org-match-sparse-tree nil "tag1|tag2")
  4244. (search-forward "H1")
  4245. (org-invisible-p2)))
  4246. (should-not
  4247. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4248. (org-match-sparse-tree nil "tag1|tag2")
  4249. (search-forward "H2")
  4250. (org-invisible-p2)))
  4251. ;; Regexp match on tags.
  4252. (should-not
  4253. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4254. (org-match-sparse-tree nil "{^tag.*}")
  4255. (search-forward "H1")
  4256. (org-invisible-p2)))
  4257. (should
  4258. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4259. (org-match-sparse-tree nil "{^tag.*}")
  4260. (search-forward "H2")
  4261. (org-invisible-p2)))
  4262. ;; Match group tags.
  4263. (should-not
  4264. (org-test-with-temp-text
  4265. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4266. (org-match-sparse-tree nil "work")
  4267. (search-forward "H1")
  4268. (org-invisible-p2)))
  4269. (should-not
  4270. (org-test-with-temp-text
  4271. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4272. (org-match-sparse-tree nil "work")
  4273. (search-forward "H2")
  4274. (org-invisible-p2)))
  4275. ;; Match group tags with hard brackets.
  4276. (should-not
  4277. (org-test-with-temp-text
  4278. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4279. (org-match-sparse-tree nil "work")
  4280. (search-forward "H1")
  4281. (org-invisible-p2)))
  4282. (should-not
  4283. (org-test-with-temp-text
  4284. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4285. (org-match-sparse-tree nil "work")
  4286. (search-forward "H2")
  4287. (org-invisible-p2)))
  4288. ;; Match tags in hierarchies
  4289. (should-not
  4290. (org-test-with-temp-text
  4291. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  4292. #+TAGS: [ Lev_2 : Lev_3 ]\n
  4293. #+TAGS: { Lev_3 : Lev_4 }\n
  4294. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  4295. (org-match-sparse-tree nil "Lev_1")
  4296. (search-forward "H4")
  4297. (org-invisible-p2)))
  4298. ;; Match regular expressions in tags
  4299. (should-not
  4300. (org-test-with-temp-text
  4301. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  4302. (org-match-sparse-tree nil "Lev")
  4303. (search-forward "H1")
  4304. (org-invisible-p2)))
  4305. (should
  4306. (org-test-with-temp-text
  4307. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  4308. (org-match-sparse-tree nil "Lev")
  4309. (search-forward "H1")
  4310. (org-invisible-p2)))
  4311. ;; Match properties.
  4312. (should
  4313. (org-test-with-temp-text
  4314. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  4315. (org-match-sparse-tree nil "A=\"1\"")
  4316. (search-forward "H2")
  4317. (org-invisible-p2)))
  4318. (should-not
  4319. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4320. (org-match-sparse-tree nil "A=\"1\"")
  4321. (search-forward "H2")
  4322. (org-invisible-p2)))
  4323. ;; Case is not significant when matching properties.
  4324. (should-not
  4325. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4326. (org-match-sparse-tree nil "a=\"1\"")
  4327. (search-forward "H2")
  4328. (org-invisible-p2)))
  4329. (should-not
  4330. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  4331. (org-match-sparse-tree nil "A=\"1\"")
  4332. (search-forward "H2")
  4333. (org-invisible-p2)))
  4334. ;; Match special LEVEL property.
  4335. (should-not
  4336. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4337. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4338. (search-forward "H1")
  4339. (org-invisible-p2)))
  4340. (should
  4341. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4342. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4343. (search-forward "H2")
  4344. (org-invisible-p2)))
  4345. ;; Comparison operators when matching properties.
  4346. (should
  4347. (org-test-with-temp-text
  4348. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4349. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4350. (search-forward "H1")
  4351. (org-invisible-p2)))
  4352. (should-not
  4353. (org-test-with-temp-text
  4354. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4355. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4356. (search-forward "H2")
  4357. (org-invisible-p2)))
  4358. ;; Regexp match on properties values.
  4359. (should-not
  4360. (org-test-with-temp-text
  4361. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4362. (org-match-sparse-tree nil "A={f.*}")
  4363. (search-forward "H1")
  4364. (org-invisible-p2)))
  4365. (should
  4366. (org-test-with-temp-text
  4367. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4368. (org-match-sparse-tree nil "A={f.*}")
  4369. (search-forward "H2")
  4370. (org-invisible-p2)))
  4371. ;; With an optional argument, limit match to TODO entries.
  4372. (should-not
  4373. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4374. (org-match-sparse-tree t "tag")
  4375. (search-forward "H1")
  4376. (org-invisible-p2)))
  4377. (should
  4378. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4379. (org-match-sparse-tree t "tag")
  4380. (search-forward "H2")
  4381. (org-invisible-p2))))
  4382. (ert-deftest test-org/occur ()
  4383. "Test `org-occur' specifications."
  4384. ;; Count number of matches.
  4385. (should
  4386. (= 1
  4387. (org-test-with-temp-text "* H\nA\n* H2"
  4388. (org-occur "A"))))
  4389. (should
  4390. (= 2
  4391. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4392. (org-occur "A"))))
  4393. ;; Test CALLBACK optional argument.
  4394. (should
  4395. (= 0
  4396. (org-test-with-temp-text "* H\nA\n* H2"
  4397. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  4398. (should
  4399. (= 1
  4400. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4401. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  4402. ;; Case-fold searches according to `org-occur-case-fold-search'.
  4403. (should
  4404. (= 2
  4405. (org-test-with-temp-text "Aa"
  4406. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  4407. (should
  4408. (= 2
  4409. (org-test-with-temp-text "Aa"
  4410. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  4411. (should
  4412. (= 1
  4413. (org-test-with-temp-text "Aa"
  4414. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  4415. (should
  4416. (= 1
  4417. (org-test-with-temp-text "Aa"
  4418. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  4419. (should
  4420. (= 1
  4421. (org-test-with-temp-text "Aa"
  4422. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  4423. (should
  4424. (= 2
  4425. (org-test-with-temp-text "Aa"
  4426. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  4427. ;;; Tags
  4428. (ert-deftest test-org/tag-string-to-alist ()
  4429. "Test `org-tag-string-to-alist' specifications."
  4430. ;; Tag without selection key.
  4431. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  4432. ;; Tag with selection key.
  4433. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  4434. ;; Tag group.
  4435. (should
  4436. (equal
  4437. (org-tag-string-to-alist "[ group : t1 t2 ]")
  4438. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  4439. ;; Mutually exclusive tags.
  4440. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  4441. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  4442. (should
  4443. (equal
  4444. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  4445. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  4446. (ert-deftest test-org/tag-alist-to-string ()
  4447. "Test `org-tag-alist-to-string' specifications."
  4448. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  4449. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  4450. (should
  4451. (equal
  4452. (org-tag-alist-to-string
  4453. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  4454. "[ group : t1 t2 ]"))
  4455. (should
  4456. (equal (org-tag-alist-to-string
  4457. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  4458. "{ tag1 tag2 }"))
  4459. (should
  4460. (equal
  4461. (org-tag-alist-to-string
  4462. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  4463. "{ group : tag1 tag2 }")))
  4464. (ert-deftest test-org/tag-alist-to-groups ()
  4465. "Test `org-tag-alist-to-groups' specifications."
  4466. (should
  4467. (equal (org-tag-alist-to-groups
  4468. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  4469. '(("group" "t1" "t2"))))
  4470. (should
  4471. (equal
  4472. (org-tag-alist-to-groups
  4473. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  4474. '(("group" "t1" "t2"))))
  4475. (should-not
  4476. (org-tag-alist-to-groups
  4477. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  4478. (ert-deftest test-org/tag-align ()
  4479. "Test tags alignment."
  4480. ;; Test aligning tags with different display width.
  4481. (should
  4482. ;; 12345678901234567890
  4483. (equal "* Test :abc:"
  4484. (org-test-with-temp-text "* Test :abc:"
  4485. (let ((org-tags-column -20)
  4486. (indent-tabs-mode nil))
  4487. (org-fix-tags-on-the-fly))
  4488. (buffer-string))))
  4489. (should
  4490. ;; 12345678901234567890
  4491. (equal "* Test :日本語:"
  4492. (org-test-with-temp-text "* Test :日本語:"
  4493. (let ((org-tags-column -20)
  4494. (indent-tabs-mode nil))
  4495. (org-fix-tags-on-the-fly))
  4496. (buffer-string))))
  4497. ;; Make sure aligning tags do not skip invisible text.
  4498. (should
  4499. (equal "* [[linkx]] :tag:"
  4500. (org-test-with-temp-text "* [[link<point>]] :tag:"
  4501. (let ((org-tags-column 0))
  4502. (org-fix-tags-on-the-fly)
  4503. (insert "x")
  4504. (buffer-string))))))
  4505. (ert-deftest test-org/tags-at ()
  4506. (should
  4507. (equal '("foo" "bar")
  4508. (org-test-with-temp-text
  4509. "* T<point>est :foo:bar:"
  4510. (org-get-tags-at)))))
  4511. ;;; Timestamps API
  4512. (ert-deftest test-org/time-stamp ()
  4513. "Test `org-time-stamp' specifications."
  4514. ;; Insert chosen time stamp at point.
  4515. (should
  4516. (string-match
  4517. "Te<2014-03-04 .*?>xt"
  4518. (org-test-with-temp-text "Te<point>xt"
  4519. (cl-letf (((symbol-function 'org-read-date)
  4520. (lambda (&rest args)
  4521. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4522. (org-time-stamp nil)
  4523. (buffer-string)))))
  4524. ;; With a prefix argument, also insert time.
  4525. (should
  4526. (string-match
  4527. "Te<2014-03-04 .*? 00:41>xt"
  4528. (org-test-with-temp-text "Te<point>xt"
  4529. (cl-letf (((symbol-function 'org-read-date)
  4530. (lambda (&rest args)
  4531. (apply #'encode-time
  4532. (org-parse-time-string "2014-03-04 00:41")))))
  4533. (org-time-stamp '(4))
  4534. (buffer-string)))))
  4535. ;; With two universal prefix arguments, insert an active timestamp
  4536. ;; with the current time without prompting the user.
  4537. (should
  4538. (string-match
  4539. "Te<2014-03-04 .*? 00:41>xt"
  4540. (org-test-with-temp-text "Te<point>xt"
  4541. (cl-letf (((symbol-function 'current-time)
  4542. (lambda ()
  4543. (apply #'encode-time
  4544. (org-parse-time-string "2014-03-04 00:41")))))
  4545. (org-time-stamp '(16))
  4546. (buffer-string)))))
  4547. ;; When optional argument is non-nil, insert an inactive timestamp.
  4548. (should
  4549. (string-match
  4550. "Te\\[2014-03-04 .*?\\]xt"
  4551. (org-test-with-temp-text "Te<point>xt"
  4552. (cl-letf (((symbol-function 'org-read-date)
  4553. (lambda (&rest args)
  4554. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4555. (org-time-stamp nil t)
  4556. (buffer-string)))))
  4557. ;; When called from a timestamp, replace existing one.
  4558. (should
  4559. (string-match
  4560. "<2014-03-04 .*?>"
  4561. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  4562. (cl-letf (((symbol-function 'org-read-date)
  4563. (lambda (&rest args)
  4564. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4565. (org-time-stamp nil)
  4566. (buffer-string)))))
  4567. (should
  4568. (string-match
  4569. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  4570. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  4571. (cl-letf (((symbol-function 'org-read-date)
  4572. (lambda (&rest args)
  4573. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4574. (org-time-stamp nil)
  4575. (buffer-string)))))
  4576. ;; When replacing a timestamp, preserve repeater, if any.
  4577. (should
  4578. (string-match
  4579. "<2014-03-04 .*? \\+2y>"
  4580. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  4581. (cl-letf (((symbol-function 'org-read-date)
  4582. (lambda (&rest args)
  4583. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4584. (org-time-stamp nil)
  4585. (buffer-string)))))
  4586. ;; When called twice in a raw, build a date range.
  4587. (should
  4588. (string-match
  4589. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  4590. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  4591. (cl-letf (((symbol-function 'org-read-date)
  4592. (lambda (&rest args)
  4593. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4594. (let ((last-command 'org-time-stamp)
  4595. (this-command 'org-time-stamp))
  4596. (org-time-stamp nil))
  4597. (buffer-string))))))
  4598. (ert-deftest test-org/timestamp-has-time-p ()
  4599. "Test `org-timestamp-has-time-p' specifications."
  4600. ;; With time.
  4601. (should
  4602. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  4603. (org-timestamp-has-time-p (org-element-context))))
  4604. ;; Without time.
  4605. (should-not
  4606. (org-test-with-temp-text "<2012-03-29 Thu>"
  4607. (org-timestamp-has-time-p (org-element-context)))))
  4608. (ert-deftest test-org/timestamp-format ()
  4609. "Test `org-timestamp-format' specifications."
  4610. ;; Regular test.
  4611. (should
  4612. (equal
  4613. "2012-03-29 16:40"
  4614. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  4615. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  4616. ;; Range end.
  4617. (should
  4618. (equal
  4619. "2012-03-29"
  4620. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  4621. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  4622. (ert-deftest test-org/timestamp-split-range ()
  4623. "Test `org-timestamp-split-range' specifications."
  4624. ;; Extract range start (active).
  4625. (should
  4626. (equal '(2012 3 29)
  4627. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4628. (let ((ts (org-timestamp-split-range (org-element-context))))
  4629. (mapcar (lambda (p) (org-element-property p ts))
  4630. '(:year-end :month-end :day-end))))))
  4631. ;; Extract range start (inactive)
  4632. (should
  4633. (equal '(2012 3 29)
  4634. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  4635. (let ((ts (org-timestamp-split-range (org-element-context))))
  4636. (mapcar (lambda (p) (org-element-property p ts))
  4637. '(:year-end :month-end :day-end))))))
  4638. ;; Extract range end (active).
  4639. (should
  4640. (equal '(2012 3 30)
  4641. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4642. (let ((ts (org-timestamp-split-range
  4643. (org-element-context) t)))
  4644. (mapcar (lambda (p) (org-element-property p ts))
  4645. '(:year-end :month-end :day-end))))))
  4646. ;; Extract range end (inactive)
  4647. (should
  4648. (equal '(2012 3 30)
  4649. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  4650. (let ((ts (org-timestamp-split-range
  4651. (org-element-context) t)))
  4652. (mapcar (lambda (p) (org-element-property p ts))
  4653. '(:year-end :month-end :day-end))))))
  4654. ;; Return the timestamp if not a range.
  4655. (should
  4656. (org-test-with-temp-text "[2012-03-29 Thu]"
  4657. (let* ((ts-orig (org-element-context))
  4658. (ts-copy (org-timestamp-split-range ts-orig)))
  4659. (eq ts-orig ts-copy))))
  4660. (should
  4661. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  4662. (let* ((ts-orig (org-element-context))
  4663. (ts-copy (org-timestamp-split-range ts-orig)))
  4664. (eq ts-orig ts-copy)))))
  4665. (ert-deftest test-org/timestamp-translate ()
  4666. "Test `org-timestamp-translate' specifications."
  4667. ;; Translate whole date range.
  4668. (should
  4669. (equal "<29>--<30>"
  4670. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4671. (let ((org-display-custom-times t)
  4672. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4673. (org-timestamp-translate (org-element-context))))))
  4674. ;; Translate date range start.
  4675. (should
  4676. (equal "<29>"
  4677. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4678. (let ((org-display-custom-times t)
  4679. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4680. (org-timestamp-translate (org-element-context) 'start)))))
  4681. ;; Translate date range end.
  4682. (should
  4683. (equal "<30>"
  4684. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4685. (let ((org-display-custom-times t)
  4686. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4687. (org-timestamp-translate (org-element-context) 'end)))))
  4688. ;; Translate time range.
  4689. (should
  4690. (equal "<08>--<16>"
  4691. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  4692. (let ((org-display-custom-times t)
  4693. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  4694. (org-timestamp-translate (org-element-context))))))
  4695. ;; Translate non-range timestamp.
  4696. (should
  4697. (equal "<29>"
  4698. (org-test-with-temp-text "<2012-03-29 Thu>"
  4699. (let ((org-display-custom-times t)
  4700. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4701. (org-timestamp-translate (org-element-context))))))
  4702. ;; Do not change `diary' timestamps.
  4703. (should
  4704. (equal "<%%(org-float t 4 2)>"
  4705. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  4706. (let ((org-display-custom-times t)
  4707. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4708. (org-timestamp-translate (org-element-context)))))))
  4709. ;;; Visibility
  4710. (ert-deftest test-org/flag-drawer ()
  4711. "Test `org-flag-drawer' specifications."
  4712. ;; Hide drawer.
  4713. (should
  4714. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4715. (org-flag-drawer t)
  4716. (get-char-property (line-end-position) 'invisible)))
  4717. ;; Show drawer.
  4718. (should-not
  4719. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4720. (org-flag-drawer t)
  4721. (org-flag-drawer nil)
  4722. (get-char-property (line-end-position) 'invisible)))
  4723. ;; Test optional argument.
  4724. (should
  4725. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4726. (let ((drawer (save-excursion (search-forward ":D2")
  4727. (org-element-at-point))))
  4728. (org-flag-drawer t drawer)
  4729. (get-char-property (progn (search-forward ":D2") (line-end-position))
  4730. 'invisible))))
  4731. (should-not
  4732. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4733. (let ((drawer (save-excursion (search-forward ":D2")
  4734. (org-element-at-point))))
  4735. (org-flag-drawer t drawer)
  4736. (get-char-property (line-end-position) 'invisible))))
  4737. ;; Do not hide fake drawers.
  4738. (should-not
  4739. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  4740. (forward-line 1)
  4741. (org-flag-drawer t)
  4742. (get-char-property (line-end-position) 'invisible)))
  4743. ;; Do not hide incomplete drawers.
  4744. (should-not
  4745. (org-test-with-temp-text ":D:\nparagraph"
  4746. (forward-line 1)
  4747. (org-flag-drawer t)
  4748. (get-char-property (line-end-position) 'invisible)))
  4749. ;; Do not hide drawers when called from final blank lines.
  4750. (should-not
  4751. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  4752. (goto-char (point-max))
  4753. (org-flag-drawer t)
  4754. (goto-char (point-min))
  4755. (get-char-property (line-end-position) 'invisible)))
  4756. ;; Don't leave point in an invisible part of the buffer when hiding
  4757. ;; a drawer away.
  4758. (should-not
  4759. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4760. (goto-char (point-max))
  4761. (org-flag-drawer t)
  4762. (get-char-property (point) 'invisible))))
  4763. (ert-deftest test-org/hide-block-toggle ()
  4764. "Test `org-hide-block-toggle' specifications."
  4765. ;; Error when not at a block.
  4766. (should-error
  4767. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  4768. (org-hide-block-toggle 'off)
  4769. (get-char-property (line-end-position) 'invisible)))
  4770. ;; Hide block.
  4771. (should
  4772. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  4773. (org-hide-block-toggle)
  4774. (get-char-property (line-end-position) 'invisible)))
  4775. (should
  4776. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  4777. (org-hide-block-toggle)
  4778. (get-char-property (line-end-position) 'invisible)))
  4779. ;; Show block unconditionally when optional argument is `off'.
  4780. (should-not
  4781. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4782. (org-hide-block-toggle)
  4783. (org-hide-block-toggle 'off)
  4784. (get-char-property (line-end-position) 'invisible)))
  4785. (should-not
  4786. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4787. (org-hide-block-toggle 'off)
  4788. (get-char-property (line-end-position) 'invisible)))
  4789. ;; Hide block unconditionally when optional argument is non-nil.
  4790. (should
  4791. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4792. (org-hide-block-toggle t)
  4793. (get-char-property (line-end-position) 'invisible)))
  4794. (should
  4795. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4796. (org-hide-block-toggle)
  4797. (org-hide-block-toggle t)
  4798. (get-char-property (line-end-position) 'invisible)))
  4799. ;; Do not hide block when called from final blank lines.
  4800. (should-not
  4801. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  4802. (org-hide-block-toggle)
  4803. (goto-char (point-min))
  4804. (get-char-property (line-end-position) 'invisible)))
  4805. ;; Don't leave point in an invisible part of the buffer when hiding
  4806. ;; a block away.
  4807. (should-not
  4808. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  4809. (org-hide-block-toggle)
  4810. (get-char-property (point) 'invisible))))
  4811. (ert-deftest test-org/hide-block-toggle-maybe ()
  4812. "Test `org-hide-block-toggle-maybe' specifications."
  4813. (should
  4814. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  4815. (org-hide-block-toggle-maybe)))
  4816. (should-not
  4817. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  4818. (ert-deftest test-org/show-set-visibility ()
  4819. "Test `org-show-set-visibility' specifications."
  4820. ;; Do not throw an error before first heading.
  4821. (should
  4822. (org-test-with-temp-text "Preamble\n* Headline"
  4823. (org-show-set-visibility 'tree)
  4824. t))
  4825. ;; Test all visibility spans, both on headline and in entry.
  4826. (let ((list-visible-lines
  4827. (lambda (state headerp)
  4828. (org-test-with-temp-text "* Grandmother (0)
  4829. ** Uncle (1)
  4830. *** Heir (2)
  4831. ** Father (3)
  4832. Ancestor text (4)
  4833. *** Sister (5)
  4834. Sibling text (6)
  4835. *** Self (7)
  4836. Match (8)
  4837. **** First born (9)
  4838. Child text (10)
  4839. **** The other child (11)
  4840. *** Brother (12)
  4841. ** Aunt (13)
  4842. "
  4843. (org-cycle t)
  4844. (search-forward (if headerp "Self" "Match"))
  4845. (org-show-set-visibility state)
  4846. (goto-char (point-min))
  4847. (let (result (line 0))
  4848. (while (not (eobp))
  4849. (unless (org-invisible-p2) (push line result))
  4850. (incf line)
  4851. (forward-line))
  4852. (nreverse result))))))
  4853. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  4854. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  4855. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  4856. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  4857. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  4858. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  4859. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  4860. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  4861. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  4862. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  4863. (funcall list-visible-lines 'tree nil)))
  4864. (should (equal '(0 1 3 4 5 7 12 13)
  4865. (funcall list-visible-lines 'canonical t)))
  4866. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  4867. (funcall list-visible-lines 'canonical nil))))
  4868. ;; When point is hidden in a drawer or a block, make sure to make it
  4869. ;; visible.
  4870. (should-not
  4871. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  4872. (org-hide-block-toggle)
  4873. (search-forward "Text")
  4874. (org-show-set-visibility 'minimal)
  4875. (org-invisible-p2)))
  4876. (should-not
  4877. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  4878. (org-flag-drawer t)
  4879. (search-forward "Text")
  4880. (org-show-set-visibility 'minimal)
  4881. (org-invisible-p2)))
  4882. (should-not
  4883. (org-test-with-temp-text
  4884. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  4885. (org-flag-drawer t)
  4886. (forward-line -1)
  4887. (org-hide-block-toggle)
  4888. (search-forward "Text")
  4889. (org-show-set-visibility 'minimal)
  4890. (org-invisible-p2))))
  4891. (provide 'test-org)
  4892. ;;; test-org.el ends here