test-org.el 168 KB

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