test-org.el 187 KB

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