test-org.el 215 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579
  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. ;; Template test file for Org tests
  16. ;;; Code:
  17. ;;; Comments
  18. (ert-deftest test-org/toggle-comment ()
  19. "Test `org-toggle-comment' specifications."
  20. ;; Simple headline.
  21. (should
  22. (equal "* Test"
  23. (org-test-with-temp-text "* COMMENT Test"
  24. (org-toggle-comment)
  25. (buffer-string))))
  26. (should
  27. (equal "* COMMENT Test"
  28. (org-test-with-temp-text "* Test"
  29. (org-toggle-comment)
  30. (buffer-string))))
  31. ;; Headline with a regular keyword.
  32. (should
  33. (equal "* TODO Test"
  34. (org-test-with-temp-text "* TODO COMMENT Test"
  35. (org-toggle-comment)
  36. (buffer-string))))
  37. (should
  38. (equal "* TODO COMMENT Test"
  39. (org-test-with-temp-text "* TODO Test"
  40. (org-toggle-comment)
  41. (buffer-string))))
  42. ;; Empty headline.
  43. (should
  44. (equal "* "
  45. (org-test-with-temp-text "* COMMENT"
  46. (org-toggle-comment)
  47. (buffer-string))))
  48. (should
  49. (equal "* COMMENT"
  50. (org-test-with-temp-text "* "
  51. (org-toggle-comment)
  52. (buffer-string))))
  53. ;; Headline with a single keyword.
  54. (should
  55. (equal "* TODO "
  56. (org-test-with-temp-text "* TODO COMMENT"
  57. (org-toggle-comment)
  58. (buffer-string))))
  59. (should
  60. (equal "* TODO COMMENT"
  61. (org-test-with-temp-text "* TODO"
  62. (org-toggle-comment)
  63. (buffer-string))))
  64. ;; Headline with a keyword, a priority cookie and contents.
  65. (should
  66. (equal "* TODO [#A] Headline"
  67. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  68. (org-toggle-comment)
  69. (buffer-string))))
  70. (should
  71. (equal "* TODO [#A] COMMENT Headline"
  72. (org-test-with-temp-text "* TODO [#A] Headline"
  73. (org-toggle-comment)
  74. (buffer-string)))))
  75. (ert-deftest test-org/comment-dwim ()
  76. "Test `comment-dwim' behaviour in an Org buffer."
  77. ;; No region selected, no comment on current line and line not
  78. ;; empty: insert comment on line above.
  79. (should
  80. (equal "# \nComment"
  81. (org-test-with-temp-text "Comment"
  82. (progn (call-interactively 'comment-dwim)
  83. (buffer-string)))))
  84. ;; No region selected, no comment on current line and line empty:
  85. ;; insert comment on this line.
  86. (should
  87. (equal "# \nParagraph"
  88. (org-test-with-temp-text "\nParagraph"
  89. (progn (call-interactively 'comment-dwim)
  90. (buffer-string)))))
  91. ;; No region selected, and a comment on this line: indent it.
  92. (should
  93. (equal "* Headline\n # Comment"
  94. (org-test-with-temp-text "* Headline\n# Comment"
  95. (progn (forward-line)
  96. (let ((org-adapt-indentation t))
  97. (call-interactively 'comment-dwim))
  98. (buffer-string)))))
  99. ;; Also recognize single # at column 0 as comments.
  100. (should
  101. (equal "# Comment"
  102. (org-test-with-temp-text "# Comment"
  103. (progn (forward-line)
  104. (call-interactively 'comment-dwim)
  105. (buffer-string)))))
  106. ;; Region selected and only comments and blank lines within it:
  107. ;; un-comment all commented lines.
  108. (should
  109. (equal "Comment 1\n\nComment 2"
  110. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  111. (progn
  112. (transient-mark-mode 1)
  113. (push-mark (point) t t)
  114. (goto-char (point-max))
  115. (call-interactively 'comment-dwim)
  116. (buffer-string)))))
  117. ;; Region selected without comments: comment all lines if
  118. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  119. (should
  120. (equal "# Comment 1\n\n# Comment 2"
  121. (org-test-with-temp-text "Comment 1\n\nComment 2"
  122. (progn
  123. (transient-mark-mode 1)
  124. (push-mark (point) t t)
  125. (goto-char (point-max))
  126. (let ((comment-empty-lines nil))
  127. (call-interactively 'comment-dwim))
  128. (buffer-string)))))
  129. (should
  130. (equal "# Comment 1\n# \n# Comment 2"
  131. (org-test-with-temp-text "Comment 1\n\nComment 2"
  132. (progn
  133. (transient-mark-mode 1)
  134. (push-mark (point) t t)
  135. (goto-char (point-max))
  136. (let ((comment-empty-lines t))
  137. (call-interactively 'comment-dwim))
  138. (buffer-string)))))
  139. ;; In front of a keyword without region, insert a new comment.
  140. (should
  141. (equal "# \n#+KEYWORD: value"
  142. (org-test-with-temp-text "#+KEYWORD: value"
  143. (progn (call-interactively 'comment-dwim)
  144. (buffer-string)))))
  145. ;; In a source block, use appropriate syntax.
  146. (should
  147. (equal " ;; "
  148. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
  149. (forward-line)
  150. (let ((org-edit-src-content-indentation 2))
  151. (call-interactively 'comment-dwim))
  152. (buffer-substring-no-properties (line-beginning-position) (point)))))
  153. (should
  154. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  155. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
  156. (forward-line)
  157. (transient-mark-mode 1)
  158. (push-mark (point) t t)
  159. (forward-line 2)
  160. (let ((org-edit-src-content-indentation 2))
  161. (call-interactively 'comment-dwim))
  162. (buffer-string)))))
  163. ;;; Date and time analysis
  164. (ert-deftest test-org/org-read-date ()
  165. "Test `org-read-date' specifications."
  166. ;; Parse ISO date with abbreviated year and month.
  167. (should (equal "2012-03-29 16:40"
  168. (let ((org-time-was-given t))
  169. (org-read-date t nil "12-3-29 16:40"))))
  170. ;; Parse Europeans dates.
  171. (should (equal "2012-03-29 16:40"
  172. (let ((org-time-was-given t))
  173. (org-read-date t nil "29.03.2012 16:40"))))
  174. ;; Parse Europeans dates without year.
  175. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  176. (let ((org-time-was-given t))
  177. (org-read-date t nil "29.03. 16:40"))))
  178. ;; Relative date applied to current time if there is single
  179. ;; plus/minus, or to default date when there are two of them.
  180. (should
  181. (equal
  182. "2015-03-04"
  183. (cl-letf (((symbol-function 'current-time)
  184. (lambda ()
  185. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  186. (org-read-date
  187. t nil "+1y" nil
  188. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  189. (should
  190. (equal
  191. "2013-03-29"
  192. (cl-letf (((symbol-function 'current-time)
  193. (lambda ()
  194. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  195. (org-read-date
  196. t nil "++1y" nil
  197. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  198. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  199. ;; dates (relatively to now) when incomplete. Otherwise, use
  200. ;; default date.
  201. (should
  202. (equal
  203. "2014-04-01"
  204. (cl-letf (((symbol-function 'current-time)
  205. (lambda ()
  206. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  207. (let ((org-read-date-prefer-future t))
  208. (org-read-date t nil "1")))))
  209. (should
  210. (equal
  211. "2013-03-04"
  212. (cl-letf (((symbol-function 'current-time)
  213. (lambda ()
  214. (apply #'encode-time (org-parse-time-string "2012-03-29")))))
  215. (let ((org-read-date-prefer-future t))
  216. (org-read-date t nil "3-4")))))
  217. (should
  218. (equal
  219. "2012-03-04"
  220. (cl-letf (((symbol-function 'current-time)
  221. (lambda ()
  222. (apply #'encode-time (org-parse-time-string "2012-03-29")))))
  223. (let ((org-read-date-prefer-future nil))
  224. (org-read-date t nil "3-4")))))
  225. ;; When set to `org-read-date-prefer-future' is set to `time', read
  226. ;; day is moved to tomorrow if specified hour is before current
  227. ;; time. However, it only happens in no other part of the date is
  228. ;; specified.
  229. (should
  230. (equal
  231. "2012-03-30"
  232. (cl-letf (((symbol-function 'current-time)
  233. (lambda ()
  234. (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
  235. (let ((org-read-date-prefer-future 'time))
  236. (org-read-date t nil "00:40" nil)))))
  237. (should-not
  238. (equal
  239. "2012-03-30"
  240. (cl-letf (((symbol-function 'current-time)
  241. (lambda ()
  242. (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
  243. (let ((org-read-date-prefer-future 'time))
  244. (org-read-date t nil "29 00:40" nil)))))
  245. ;; Caveat: `org-read-date-prefer-future' always refers to current
  246. ;; time, not default time, when they differ.
  247. (should
  248. (equal
  249. "2014-04-01"
  250. (cl-letf (((symbol-function 'current-time)
  251. (lambda ()
  252. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  253. (let ((org-read-date-prefer-future t))
  254. (org-read-date
  255. t nil "1" nil
  256. (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
  257. (should
  258. (equal
  259. "2014-03-25"
  260. (cl-letf (((symbol-function 'current-time)
  261. (lambda ()
  262. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  263. (let ((org-read-date-prefer-future t))
  264. (org-read-date
  265. t nil "25" nil
  266. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
  267. (ert-deftest test-org/org-parse-time-string ()
  268. "Test `org-parse-time-string'."
  269. (should (equal (org-parse-time-string "2012-03-29 16:40")
  270. '(0 40 16 29 3 2012 nil nil nil)))
  271. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  272. '(0 40 16 29 3 2012 nil nil nil)))
  273. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  274. '(0 40 16 29 3 2012 nil nil nil)))
  275. (should (equal (org-parse-time-string "<2012-03-29>")
  276. '(0 0 0 29 3 2012 nil nil nil)))
  277. (should (equal (org-parse-time-string "<2012-03-29>" t)
  278. '(0 nil nil 29 3 2012 nil nil nil))))
  279. (ert-deftest test-org/closest-date ()
  280. "Test `org-closest-date' specifications."
  281. (require 'calendar)
  282. ;; Time stamps without a repeater are returned unchanged.
  283. (should
  284. (equal
  285. '(3 29 2012)
  286. (calendar-gregorian-from-absolute
  287. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  288. ;; Time stamps with a null repeater are returned unchanged.
  289. (should
  290. (equal
  291. '(3 29 2012)
  292. (calendar-gregorian-from-absolute
  293. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  294. ;; if PREFER is set to `past' always return a date before, or equal
  295. ;; to CURRENT.
  296. (should
  297. (equal
  298. '(3 1 2014)
  299. (calendar-gregorian-from-absolute
  300. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  301. (should
  302. (equal
  303. '(3 4 2014)
  304. (calendar-gregorian-from-absolute
  305. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  306. ;; if PREFER is set to `future' always return a date before, or equal
  307. ;; to CURRENT.
  308. (should
  309. (equal
  310. '(3 29 2014)
  311. (calendar-gregorian-from-absolute
  312. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  313. (should
  314. (equal
  315. '(3 4 2014)
  316. (calendar-gregorian-from-absolute
  317. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  318. ;; If PREFER is neither `past' nor `future', select closest date.
  319. (should
  320. (equal
  321. '(3 1 2014)
  322. (calendar-gregorian-from-absolute
  323. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  324. (should
  325. (equal
  326. '(5 4 2014)
  327. (calendar-gregorian-from-absolute
  328. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  329. ;; Test "day" repeater.
  330. (should
  331. (equal '(3 8 2014)
  332. (calendar-gregorian-from-absolute
  333. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  334. (should
  335. (equal '(3 10 2014)
  336. (calendar-gregorian-from-absolute
  337. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  338. ;; Test "month" repeater.
  339. (should
  340. (equal '(1 5 2015)
  341. (calendar-gregorian-from-absolute
  342. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  343. (should
  344. (equal '(3 29 2014)
  345. (calendar-gregorian-from-absolute
  346. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  347. ;; Test "year" repeater.
  348. (should
  349. (equal '(3 5 2014)
  350. (calendar-gregorian-from-absolute
  351. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  352. (should
  353. (equal '(3 29 2014)
  354. (calendar-gregorian-from-absolute
  355. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  356. (ert-deftest test-org/deadline-close-p ()
  357. "Test `org-deadline-close-p' specifications."
  358. ;; Pretend that the current time is 2016-06-03 Fri 01:43
  359. (cl-letf (((symbol-function 'current-time)
  360. (lambda () '(22353 6425 905205 644000))))
  361. ;; Timestamps are close if they are within `ndays' of lead time.
  362. (org-test-with-temp-text "* Heading"
  363. (should (org-deadline-close-p "2016-06-03 Fri" 0))
  364. (should (org-deadline-close-p "2016-06-02 Thu" 0))
  365. (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
  366. (should (org-deadline-close-p "2016-06-04 Sat" 1))
  367. (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
  368. ;; Read `ndays' from timestamp if argument not given.
  369. (org-test-with-temp-text "* H"
  370. (should (org-deadline-close-p "2016-06-04 Sat -1d"))
  371. (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
  372. (should (org-deadline-close-p "2016-06-10 Fri -1w"))
  373. (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
  374. ;; Prefer `ndays' argument over lead time in timestamp.
  375. (org-test-with-temp-text "* H"
  376. (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
  377. (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
  378. ;; Completed tasks are never close.
  379. (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
  380. (org-test-with-temp-text "* TODO Heading"
  381. (should (org-deadline-close-p "2016-06-03")))
  382. (org-test-with-temp-text "* DONE Heading"
  383. (should-not (org-deadline-close-p "2016-06-03"))))))
  384. ;;; Drawers
  385. (ert-deftest test-org/insert-property-drawer ()
  386. "Test `org-insert-property-drawer' specifications."
  387. ;; Error before first headline.
  388. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  389. ;; Insert drawer right after headline if there is no planning line,
  390. ;; or after it otherwise.
  391. (should
  392. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  393. (org-test-with-temp-text "* H\nParagraph<point>"
  394. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  395. (buffer-string))))
  396. (should
  397. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  398. (org-test-with-temp-text
  399. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  400. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  401. (buffer-string))))
  402. ;; Indent inserted drawer.
  403. (should
  404. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  405. (org-test-with-temp-text "* H\nParagraph<point>"
  406. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  407. (buffer-string))))
  408. ;; Handle insertion at eob.
  409. (should
  410. (equal "* H\n:PROPERTIES:\n:END:\n"
  411. (org-test-with-temp-text "* H"
  412. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  413. (buffer-string))))
  414. ;; Skip inlinetasks before point.
  415. (when (featurep 'org-inlinetask)
  416. (should
  417. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  418. (org-test-with-temp-text
  419. "* H\n*************** I\n*************** END\nP<point>"
  420. (let ((org-adapt-indentation nil)
  421. (org-inlinetask-min-level 15))
  422. (org-insert-property-drawer))
  423. (buffer-string)))))
  424. ;; Correctly set drawer in an inlinetask.
  425. (when (featurep 'org-inlinetask)
  426. (should
  427. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  428. (org-test-with-temp-text
  429. "* H\n*************** I\nP<point>\n*************** END"
  430. (let ((org-adapt-indentation nil)
  431. (org-inlinetask-min-level 15))
  432. (org-insert-property-drawer))
  433. (buffer-string))))))
  434. ;;; Filling
  435. (ert-deftest test-org/fill-element ()
  436. "Test `org-fill-element' specifications."
  437. ;; At an Org table, align it.
  438. (should
  439. (equal "| a |\n"
  440. (org-test-with-temp-text "|a|"
  441. (org-fill-element)
  442. (buffer-string))))
  443. (should
  444. (equal "#+name: table\n| a |\n"
  445. (org-test-with-temp-text "#+name: table\n| a |\n"
  446. (org-fill-element)
  447. (buffer-string))))
  448. ;; At a paragraph, preserve line breaks.
  449. (org-test-with-temp-text "some \\\\\nlong\ntext"
  450. (let ((fill-column 20))
  451. (org-fill-element)
  452. (should (equal (buffer-string) "some \\\\\nlong text"))))
  453. ;; Correctly fill a paragraph when point is at its very end.
  454. (should
  455. (equal "A B"
  456. (org-test-with-temp-text "A\nB"
  457. (let ((fill-column 20))
  458. (goto-char (point-max))
  459. (org-fill-element)
  460. (buffer-string)))))
  461. ;; Correctly fill the last paragraph of a greater element.
  462. (should
  463. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  464. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  465. (let ((fill-column 8))
  466. (forward-line)
  467. (end-of-line)
  468. (org-fill-element)
  469. (buffer-string)))))
  470. ;; Correctly fill an element in a narrowed buffer.
  471. (should
  472. (equal "01234\n6"
  473. (org-test-with-temp-text "01234 6789"
  474. (let ((fill-column 5))
  475. (narrow-to-region 1 8)
  476. (org-fill-element)
  477. (buffer-string)))))
  478. ;; Handle `adaptive-fill-regexp' in paragraphs.
  479. (should
  480. (equal "> a b"
  481. (org-test-with-temp-text "> a\n> b"
  482. (let ((fill-column 5)
  483. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  484. (org-fill-element)
  485. (buffer-string)))))
  486. ;; Special case: Fill first paragraph when point is at an item or
  487. ;; a plain-list or a footnote reference.
  488. (should
  489. (equal "- A B"
  490. (org-test-with-temp-text "- A\n B"
  491. (let ((fill-column 20))
  492. (org-fill-element)
  493. (buffer-string)))))
  494. (should
  495. (equal "[fn:1] A B"
  496. (org-test-with-temp-text "[fn:1] A\nB"
  497. (let ((fill-column 20))
  498. (org-fill-element)
  499. (buffer-string)))))
  500. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  501. (let ((fill-column 20))
  502. (org-fill-element)
  503. (should (equal (buffer-string)
  504. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  505. ;; Fill contents of `comment-block' elements.
  506. (should
  507. (equal
  508. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  509. (let ((fill-column 20))
  510. (forward-line)
  511. (org-fill-element)
  512. (buffer-string)))
  513. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  514. ;; Fill `comment' elements.
  515. (should
  516. (equal " # A B"
  517. (org-test-with-temp-text " # A\n # B"
  518. (let ((fill-column 20))
  519. (org-fill-element)
  520. (buffer-string)))))
  521. ;; Do not mix consecutive comments when filling one of them.
  522. (should
  523. (equal "# A B\n\n# C"
  524. (org-test-with-temp-text "# A\n# B\n\n# C"
  525. (let ((fill-column 20))
  526. (org-fill-element)
  527. (buffer-string)))))
  528. ;; Use commented empty lines as separators when filling comments.
  529. (should
  530. (equal "# A B\n#\n# C"
  531. (org-test-with-temp-text "# A\n# B\n#\n# C"
  532. (let ((fill-column 20))
  533. (org-fill-element)
  534. (buffer-string)))))
  535. ;; Handle `adaptive-fill-regexp' in comments.
  536. (should
  537. (equal "# > a b"
  538. (org-test-with-temp-text "# > a\n# > b"
  539. (let ((fill-column 20)
  540. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  541. (org-fill-element)
  542. (buffer-string)))))
  543. ;; Do nothing at affiliated keywords.
  544. (should
  545. (equal "#+NAME: para\nSome\ntext."
  546. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  547. (let ((fill-column 20))
  548. (org-fill-element)
  549. (buffer-string)))))
  550. ;; Do not move point after table when filling a table.
  551. (should-not
  552. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  553. (forward-char)
  554. (org-fill-element)
  555. (eobp)))
  556. ;; Do not fill "n" macro, with or without arguments, followed by
  557. ;; a dot or a closing parenthesis since it could be confused with
  558. ;; a numbered bullet.
  559. (should-not
  560. (equal "123456789\n{{{n}}}."
  561. (org-test-with-temp-text "123456789 {{{n}}}."
  562. (let ((fill-column 10))
  563. (org-fill-element)
  564. (buffer-string)))))
  565. (should-not
  566. (equal "123456789\n{{{n}}}\)"
  567. (org-test-with-temp-text "123456789 {{{n}}}\)"
  568. (let ((fill-column 10))
  569. (org-fill-element)
  570. (buffer-string)))))
  571. (should-not
  572. (equal "123456789\n{{{n()}}}."
  573. (org-test-with-temp-text "123456789 {{{n()}}}."
  574. (let ((fill-column 10))
  575. (org-fill-element)
  576. (buffer-string)))))
  577. (should-not
  578. (equal "123456789\n{{{n(counter)}}}."
  579. (org-test-with-temp-text "123456789 {{{n(counter)}}}."
  580. (let ((fill-column 10))
  581. (org-fill-element)
  582. (buffer-string))))))
  583. (ert-deftest test-org/auto-fill-function ()
  584. "Test auto-filling features."
  585. ;; Auto fill paragraph.
  586. (should
  587. (equal "12345\n7890"
  588. (org-test-with-temp-text "12345 7890"
  589. (let ((fill-column 5))
  590. (end-of-line)
  591. (org-auto-fill-function)
  592. (buffer-string)))))
  593. ;; Auto fill first paragraph in an item.
  594. (should
  595. (equal "- 12345\n 7890"
  596. (org-test-with-temp-text "- 12345 7890"
  597. (let ((fill-column 7))
  598. (end-of-line)
  599. (org-auto-fill-function)
  600. (buffer-string)))))
  601. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  602. (should
  603. (equal "> 12345\n 7890"
  604. (org-test-with-temp-text "> 12345 7890"
  605. (let ((fill-column 10)
  606. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  607. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  608. (end-of-line)
  609. (org-auto-fill-function)
  610. (buffer-string)))))
  611. (should
  612. (equal "> 12345\n> 12345\n> 7890"
  613. (org-test-with-temp-text "> 12345\n> 12345 7890"
  614. (let ((fill-column 10)
  615. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  616. (goto-char (point-max))
  617. (org-auto-fill-function)
  618. (buffer-string)))))
  619. (should-not
  620. (equal " 12345\n *12345\n *12345"
  621. (org-test-with-temp-text " 12345\n *12345 12345"
  622. (let ((fill-column 10)
  623. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  624. (goto-char (point-max))
  625. (org-auto-fill-function)
  626. (buffer-string)))))
  627. ;; Auto fill comments.
  628. (should
  629. (equal " # 12345\n # 7890"
  630. (org-test-with-temp-text " # 12345 7890"
  631. (let ((fill-column 10))
  632. (end-of-line)
  633. (org-auto-fill-function)
  634. (buffer-string)))))
  635. ;; A hash within a line isn't a comment.
  636. (should-not
  637. (equal "12345 # 7890\n# 1"
  638. (org-test-with-temp-text "12345 # 7890 1"
  639. (let ((fill-column 12))
  640. (end-of-line)
  641. (org-auto-fill-function)
  642. (buffer-string)))))
  643. ;; Correctly interpret empty prefix.
  644. (should-not
  645. (equal "# a\n# b\nRegular\n# paragraph"
  646. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  647. (let ((fill-column 12))
  648. (end-of-line 3)
  649. (org-auto-fill-function)
  650. (buffer-string)))))
  651. ;; Comment block: auto fill contents.
  652. (should
  653. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  654. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  655. (let ((fill-column 5))
  656. (forward-line)
  657. (end-of-line)
  658. (org-auto-fill-function)
  659. (buffer-string)))))
  660. (should
  661. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  662. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  663. (let ((fill-column 5))
  664. (forward-line)
  665. (end-of-line)
  666. (org-auto-fill-function)
  667. (buffer-string)))))
  668. ;; Do not fill if a new item could be created.
  669. (should-not
  670. (equal "12345\n- 90"
  671. (org-test-with-temp-text "12345 - 90"
  672. (let ((fill-column 5))
  673. (end-of-line)
  674. (org-auto-fill-function)
  675. (buffer-string)))))
  676. ;; Do not fill if a line break could be introduced.
  677. (should-not
  678. (equal "123\\\\\n7890"
  679. (org-test-with-temp-text "123\\\\ 7890"
  680. (let ((fill-column 6))
  681. (end-of-line)
  682. (org-auto-fill-function)
  683. (buffer-string)))))
  684. ;; Do not fill affiliated keywords.
  685. (should-not
  686. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  687. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  688. (let ((fill-column 20))
  689. (end-of-line)
  690. (org-auto-fill-function)
  691. (buffer-string))))))
  692. ;;; Indentation
  693. (ert-deftest test-org/indent-line ()
  694. "Test `org-indent-line' specifications."
  695. ;; Do not indent diary sexps, footnote definitions or headlines.
  696. (should
  697. (zerop
  698. (org-test-with-temp-text "%%(org-calendar-holiday)"
  699. (org-indent-line)
  700. (org-get-indentation))))
  701. (should
  702. (zerop
  703. (org-test-with-temp-text "[fn:1] fn"
  704. (let ((org-adapt-indentation t)) (org-indent-line))
  705. (org-get-indentation))))
  706. (should
  707. (zerop
  708. (org-test-with-temp-text "* H"
  709. (org-indent-line)
  710. (org-get-indentation))))
  711. ;; Do not indent before first headline.
  712. (should
  713. (zerop
  714. (org-test-with-temp-text ""
  715. (org-indent-line)
  716. (org-get-indentation))))
  717. ;; Indent according to headline level otherwise, unless
  718. ;; `org-adapt-indentation' is nil.
  719. (should
  720. (= 2
  721. (org-test-with-temp-text "* H\n<point>A"
  722. (let ((org-adapt-indentation t)) (org-indent-line))
  723. (org-get-indentation))))
  724. (should
  725. (= 2
  726. (org-test-with-temp-text "* H\n<point>\nA"
  727. (let ((org-adapt-indentation t)) (org-indent-line))
  728. (org-get-indentation))))
  729. (should
  730. (zerop
  731. (org-test-with-temp-text "* H\n<point>A"
  732. (let ((org-adapt-indentation nil)) (org-indent-line))
  733. (org-get-indentation))))
  734. ;; Indenting preserves point position.
  735. (should
  736. (org-test-with-temp-text "* H\nA<point>B"
  737. (let ((org-adapt-indentation t)) (org-indent-line))
  738. (looking-at "B")))
  739. ;; Do not change indentation at an item or a LaTeX environment.
  740. (should
  741. (= 1
  742. (org-test-with-temp-text "* H\n<point> - A"
  743. (let ((org-adapt-indentation t)) (org-indent-line))
  744. (org-get-indentation))))
  745. (should
  746. (= 1
  747. (org-test-with-temp-text
  748. "\\begin{equation}\n <point>1+1=2\n\\end{equation}"
  749. (org-indent-line)
  750. (org-get-indentation))))
  751. ;; On blank lines at the end of a list, indent like last element
  752. ;; within it if the line is still in the list. If the last element
  753. ;; is an item, indent like its contents. Otherwise, indent like the
  754. ;; whole list.
  755. (should
  756. (= 4
  757. (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
  758. (let ((org-adapt-indentation t)) (org-indent-line))
  759. (org-get-indentation))))
  760. (should
  761. (= 4
  762. (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
  763. (let ((org-adapt-indentation t)) (org-indent-line))
  764. (org-get-indentation))))
  765. (should
  766. (zerop
  767. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
  768. (let ((org-adapt-indentation t)) (org-indent-line))
  769. (org-get-indentation))))
  770. (should
  771. (= 4
  772. (org-test-with-temp-text "* H\n- A\n - \n<point>"
  773. (let ((org-adapt-indentation t)) (org-indent-line))
  774. (org-get-indentation))))
  775. (should
  776. (= 4
  777. (org-test-with-temp-text
  778. "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
  779. (let ((org-adapt-indentation t)) (org-indent-line))
  780. (org-get-indentation))))
  781. (should
  782. (= 2
  783. (org-test-with-temp-text "- A\n B\n\n<point>"
  784. (let ((org-adapt-indentation nil)) (org-indent-line))
  785. (org-get-indentation))))
  786. (should
  787. (= 2
  788. (org-test-with-temp-text
  789. "- A\n \begin{cases} 1 + 1\n \end{cases}\n\n<point>"
  790. (let ((org-adapt-indentation nil)) (org-indent-line))
  791. (org-get-indentation))))
  792. ;; Likewise, on a blank line at the end of a footnote definition,
  793. ;; indent at column 0 if line belongs to the definition. Otherwise,
  794. ;; indent like the definition itself.
  795. (should
  796. (zerop
  797. (org-test-with-temp-text "* H\n[fn:1] Definition\n<point>"
  798. (let ((org-adapt-indentation t)) (org-indent-line))
  799. (org-get-indentation))))
  800. (should
  801. (zerop
  802. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n<point>"
  803. (let ((org-adapt-indentation t)) (org-indent-line))
  804. (org-get-indentation))))
  805. ;; After the end of the contents of a greater element, indent like
  806. ;; the beginning of the element.
  807. (should
  808. (= 1
  809. (org-test-with-temp-text
  810. " #+BEGIN_CENTER\n Contents\n<point>#+END_CENTER"
  811. (org-indent-line)
  812. (org-get-indentation))))
  813. ;; On blank lines after a paragraph, indent like its last non-empty
  814. ;; line.
  815. (should
  816. (= 1
  817. (org-test-with-temp-text " Paragraph\n\n<point>"
  818. (org-indent-line)
  819. (org-get-indentation))))
  820. ;; At the first line of an element, indent like previous element's
  821. ;; first line, ignoring footnotes definitions and inline tasks, or
  822. ;; according to parent.
  823. (should
  824. (= 2
  825. (org-test-with-temp-text "A\n\n B\n\nC<point>"
  826. (org-indent-line)
  827. (org-get-indentation))))
  828. (should
  829. (= 1
  830. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC<point>"
  831. (org-indent-line)
  832. (org-get-indentation))))
  833. (should
  834. (= 1
  835. (org-test-with-temp-text
  836. " #+BEGIN_CENTER\n<point> Contents\n#+END_CENTER"
  837. (org-indent-line)
  838. (org-get-indentation))))
  839. ;; Within code part of a source block, use language major mode if
  840. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  841. ;; according to line above.
  842. (should
  843. (= 6
  844. (org-test-with-temp-text
  845. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  846. (let ((org-src-tab-acts-natively t)
  847. (org-edit-src-content-indentation 0))
  848. (org-indent-line))
  849. (org-get-indentation))))
  850. (should
  851. (= 1
  852. (org-test-with-temp-text
  853. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  854. (let ((org-src-tab-acts-natively nil)
  855. (org-edit-src-content-indentation 0))
  856. (org-indent-line))
  857. (org-get-indentation))))
  858. ;; Otherwise, indent like the first non-blank line above.
  859. (should
  860. (zerop
  861. (org-test-with-temp-text
  862. "#+BEGIN_CENTER\nline1\n\n<point> line2\n#+END_CENTER"
  863. (org-indent-line)
  864. (org-get-indentation))))
  865. ;; Align node properties according to `org-property-format'. Handle
  866. ;; nicely empty values.
  867. (should
  868. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  869. (org-test-with-temp-text
  870. "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  871. (let ((org-property-format "%-10s %s")) (org-indent-line))
  872. (buffer-string))))
  873. (should
  874. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  875. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  876. (let ((org-property-format "%-10s %s")) (org-indent-line))
  877. (buffer-string)))))
  878. (ert-deftest test-org/indent-region ()
  879. "Test `org-indent-region' specifications."
  880. ;; Indent paragraph.
  881. (should
  882. (equal "A\nB\nC"
  883. (org-test-with-temp-text " A\nB\n C"
  884. (org-indent-region (point-min) (point-max))
  885. (buffer-string))))
  886. ;; Indent greater elements along with their contents.
  887. (should
  888. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  889. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  890. (org-indent-region (point-min) (point-max))
  891. (buffer-string))))
  892. ;; Ignore contents of verse blocks. Only indent block delimiters.
  893. (should
  894. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  895. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  896. (org-indent-region (point-min) (point-max))
  897. (buffer-string))))
  898. (should
  899. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  900. (org-test-with-temp-text " #+BEGIN_VERSE\n A\n B\n #+END_VERSE"
  901. (org-indent-region (point-min) (point-max))
  902. (buffer-string))))
  903. ;; Indent example blocks as a single block, unless indentation
  904. ;; should be preserved. In this case only indent the block markers.
  905. (should
  906. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  907. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  908. (org-indent-region (point-min) (point-max))
  909. (buffer-string))))
  910. (should
  911. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  912. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  913. (org-indent-region (point-min) (point-max))
  914. (buffer-string))))
  915. (should
  916. (equal "#+BEGIN_EXAMPLE -i\n A\n B\n#+END_EXAMPLE"
  917. (org-test-with-temp-text
  918. " #+BEGIN_EXAMPLE -i\n A\n B\n #+END_EXAMPLE"
  919. (org-indent-region (point-min) (point-max))
  920. (buffer-string))))
  921. (should
  922. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  923. (org-test-with-temp-text
  924. " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  925. (let ((org-src-preserve-indentation t))
  926. (org-indent-region (point-min) (point-max)))
  927. (buffer-string))))
  928. ;; Treat export blocks as a whole.
  929. (should
  930. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  931. (org-test-with-temp-text "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  932. (org-indent-region (point-min) (point-max))
  933. (buffer-string))))
  934. (should
  935. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  936. (org-test-with-temp-text
  937. " #+BEGIN_EXPORT latex\n A\n B\n #+END_EXPORT"
  938. (org-indent-region (point-min) (point-max))
  939. (buffer-string))))
  940. ;; Indent according to mode if `org-src-tab-acts-natively' is
  941. ;; non-nil. Otherwise, do not indent code at all.
  942. (should
  943. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  944. (org-test-with-temp-text
  945. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  946. (let ((org-src-tab-acts-natively t)
  947. (org-edit-src-content-indentation 0))
  948. (org-indent-region (point-min) (point-max)))
  949. (buffer-string))))
  950. (should
  951. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  952. (org-test-with-temp-text
  953. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  954. (let ((org-src-tab-acts-natively nil)
  955. (org-edit-src-content-indentation 0))
  956. (org-indent-region (point-min) (point-max)))
  957. (buffer-string))))
  958. ;; Align node properties according to `org-property-format'. Handle
  959. ;; nicely empty values.
  960. (should
  961. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  962. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  963. (let ((org-property-format "%-10s %s")
  964. (org-adapt-indentation nil))
  965. (org-indent-region (point) (point-max)))
  966. (buffer-string))))
  967. (should
  968. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  969. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  970. (let ((org-property-format "%-10s %s")
  971. (org-adapt-indentation nil))
  972. (org-indent-region (point) (point-max)))
  973. (buffer-string))))
  974. ;; Indent plain lists.
  975. (should
  976. (equal "- A\n B\n - C\n\n D"
  977. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  978. (org-indent-region (point-min) (point-max))
  979. (buffer-string))))
  980. (should
  981. (equal "- A\n\n- B"
  982. (org-test-with-temp-text " - A\n\n - B"
  983. (org-indent-region (point-min) (point-max))
  984. (buffer-string))))
  985. ;; Indent footnote definitions.
  986. (should
  987. (equal "[fn:1] Definition\n\nDefinition"
  988. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  989. (org-indent-region (point-min) (point-max))
  990. (buffer-string))))
  991. ;; Special case: Start indenting on a blank line.
  992. (should
  993. (equal "\nParagraph"
  994. (org-test-with-temp-text "\n Paragraph"
  995. (org-indent-region (point-min) (point-max))
  996. (buffer-string)))))
  997. ;;; Editing
  998. (ert-deftest test-org/delete-indentation ()
  999. "Test `org-delete-indentation' specifications."
  1000. ;; Regular test.
  1001. (should (equal "foo bar"
  1002. (org-test-with-temp-text
  1003. "foo \n bar<point>"
  1004. (org-delete-indentation)
  1005. (buffer-string))))
  1006. ;; With optional argument.
  1007. (should (equal "foo bar"
  1008. (org-test-with-temp-text
  1009. "foo<point> \n bar"
  1010. (org-delete-indentation t)
  1011. (buffer-string))))
  1012. ;; At headline text should be appended to the headline text.
  1013. (should
  1014. (equal"* foo bar :tag:"
  1015. (let (org-auto-align-tags)
  1016. (org-test-with-temp-text
  1017. "* foo :tag:\n bar<point>"
  1018. (org-delete-indentation)
  1019. (buffer-string)))))
  1020. (should
  1021. (equal "* foo bar :tag:"
  1022. (let (org-auto-align-tags)
  1023. (org-test-with-temp-text
  1024. "* foo <point>:tag:\n bar"
  1025. (org-delete-indentation t)
  1026. (buffer-string))))))
  1027. (ert-deftest test-org/return ()
  1028. "Test `org-return' specifications."
  1029. ;; Regular test.
  1030. (should
  1031. (equal "Para\ngraph"
  1032. (org-test-with-temp-text "Para<point>graph"
  1033. (org-return)
  1034. (buffer-string))))
  1035. ;; With optional argument, indent line.
  1036. (should
  1037. (equal " Para\n graph"
  1038. (org-test-with-temp-text " Para<point>graph"
  1039. (org-return t)
  1040. (buffer-string))))
  1041. ;; On a table, call `org-table-next-row'.
  1042. (should
  1043. (org-test-with-temp-text "| <point>a |\n| b |"
  1044. (org-return)
  1045. (looking-at-p "b")))
  1046. ;; Open link or timestamp under point when `org-return-follows-link'
  1047. ;; is non-nil.
  1048. (should
  1049. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1050. (let ((org-return-follows-link t)
  1051. (org-link-search-must-match-exact-headline nil))
  1052. (org-return))
  1053. (looking-at-p "<<target>>")))
  1054. (should-not
  1055. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1056. (let ((org-return-follows-link nil)) (org-return))
  1057. (looking-at-p "<<target>>")))
  1058. (should
  1059. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  1060. (let ((org-return-follows-link t)) (org-return))
  1061. (looking-at-p "* b")))
  1062. (should
  1063. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  1064. (let ((org-return-follows-link t)
  1065. (org-link-search-must-match-exact-headline nil))
  1066. (org-return))
  1067. (looking-at-p "<<target>>")))
  1068. (should-not
  1069. (org-test-with-temp-text "Link [[target]]<point> <<target>>"
  1070. (let ((org-return-follows-link t)
  1071. (org-link-search-must-match-exact-headline nil))
  1072. (org-return))
  1073. (looking-at-p "<<target>>")))
  1074. ;; When `org-return-follows-link' is non-nil, tolerate links and
  1075. ;; timestamps in comments, node properties, etc.
  1076. (should
  1077. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1078. (let ((org-return-follows-link t)
  1079. (org-link-search-must-match-exact-headline nil))
  1080. (org-return))
  1081. (looking-at-p "<<target>>")))
  1082. (should-not
  1083. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1084. (let ((org-return-follows-link nil)) (org-return))
  1085. (looking-at-p "<<target>>")))
  1086. (should-not
  1087. (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
  1088. (let ((org-return-follows-link t)
  1089. (org-link-search-must-match-exact-headline nil))
  1090. (org-return))
  1091. (looking-at-p "<<target>>")))
  1092. ;; However, do not open link when point is in a table.
  1093. (should
  1094. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1095. (let ((org-return-follows-link t)) (org-return))
  1096. (looking-at-p "between")))
  1097. ;; Special case: in a list, when indenting, do not break structure.
  1098. (should
  1099. (equal "- A\n B"
  1100. (org-test-with-temp-text "- A <point>B"
  1101. (org-return t)
  1102. (buffer-string))))
  1103. (should
  1104. (equal "- A\n\n- B"
  1105. (org-test-with-temp-text "- A\n<point>- B"
  1106. (org-return t)
  1107. (buffer-string))))
  1108. ;; On tags part of a headline, add a newline below it instead of
  1109. ;; breaking it.
  1110. (should
  1111. (equal "* H :tag:\n"
  1112. (org-test-with-temp-text "* H :<point>tag:"
  1113. (org-return)
  1114. (buffer-string))))
  1115. ;; Before headline text, add a newline below it instead of breaking
  1116. ;; it.
  1117. (should
  1118. (equal "* TODO H :tag:\n"
  1119. (org-test-with-temp-text "* <point>TODO H :tag:"
  1120. (org-return)
  1121. (buffer-string))))
  1122. (should
  1123. (equal "* TODO [#B] H :tag:\n"
  1124. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1125. (org-return)
  1126. (buffer-string))))
  1127. (should ;TODO are case-sensitive
  1128. (equal "* \nTodo"
  1129. (org-test-with-temp-text "* <point>Todo"
  1130. (org-return)
  1131. (buffer-string))))
  1132. ;; At headline text, break headline text but preserve tags.
  1133. (should
  1134. (equal "* TODO [#B] foo :tag:\nbar"
  1135. (let (org-auto-align-tags)
  1136. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1137. (org-return)
  1138. (buffer-string)))))
  1139. ;; At bol of headline insert newline.
  1140. (should
  1141. (equal "\n* h"
  1142. (org-test-with-temp-text "<point>* h"
  1143. (org-return)
  1144. (buffer-string))))
  1145. ;; Refuse to leave invalid headline in buffer.
  1146. (should
  1147. (equal "* h\n"
  1148. (org-test-with-temp-text "*<point> h"
  1149. (org-return)
  1150. (buffer-string)))))
  1151. (ert-deftest test-org/meta-return ()
  1152. "Test M-RET (`org-meta-return') specifications."
  1153. ;; In a table field insert a row above.
  1154. (should
  1155. (org-test-with-temp-text "| a |"
  1156. (forward-char)
  1157. (org-meta-return)
  1158. (forward-line -1)
  1159. (looking-at "| |$")))
  1160. ;; In a paragraph change current line into a header.
  1161. (should
  1162. (org-test-with-temp-text "a"
  1163. (org-meta-return)
  1164. (beginning-of-line)
  1165. (looking-at "\* a$")))
  1166. ;; In an item insert an item, in this case above.
  1167. (should
  1168. (org-test-with-temp-text "- a"
  1169. (org-meta-return)
  1170. (beginning-of-line)
  1171. (looking-at "- $")))
  1172. ;; In a drawer and item insert an item, in this case above.
  1173. (should
  1174. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1175. (forward-line)
  1176. (org-meta-return)
  1177. (beginning-of-line)
  1178. (looking-at "- $"))))
  1179. (ert-deftest test-org/insert-heading ()
  1180. "Test `org-insert-heading' specifications."
  1181. ;; In an empty buffer, insert a new headline.
  1182. (should
  1183. (equal "* "
  1184. (org-test-with-temp-text ""
  1185. (org-insert-heading)
  1186. (buffer-string))))
  1187. ;; At the beginning of a line, turn it into a headline.
  1188. (should
  1189. (equal "* P"
  1190. (org-test-with-temp-text "<point>P"
  1191. (org-insert-heading)
  1192. (buffer-string))))
  1193. ;; In the middle of a line, split the line if allowed, otherwise,
  1194. ;; insert the headline at its end.
  1195. (should
  1196. (equal "Para\n* graph"
  1197. (org-test-with-temp-text "Para<point>graph"
  1198. (let ((org-M-RET-may-split-line '((default . t))))
  1199. (org-insert-heading))
  1200. (buffer-string))))
  1201. (should
  1202. (equal "Paragraph\n* "
  1203. (org-test-with-temp-text "Para<point>graph"
  1204. (let ((org-M-RET-may-split-line '((default . nil))))
  1205. (org-insert-heading))
  1206. (buffer-string))))
  1207. ;; At the beginning of a headline, create one above.
  1208. (should
  1209. (equal "* \n* H"
  1210. (org-test-with-temp-text "* H"
  1211. (org-insert-heading)
  1212. (buffer-string))))
  1213. ;; In the middle of a headline, split it if allowed.
  1214. (should
  1215. (equal "* H\n* 1\n"
  1216. (org-test-with-temp-text "* H<point>1"
  1217. (let ((org-M-RET-may-split-line '((headline . t))))
  1218. (org-insert-heading))
  1219. (buffer-string))))
  1220. (should
  1221. (equal "* H1\n* \n"
  1222. (org-test-with-temp-text "* H<point>1"
  1223. (let ((org-M-RET-may-split-line '((headline . nil))))
  1224. (org-insert-heading))
  1225. (buffer-string))))
  1226. ;; However, splitting cannot happen on TODO keywords, priorities or
  1227. ;; tags.
  1228. (should
  1229. (equal "* TODO H1\n* \n"
  1230. (org-test-with-temp-text "* TO<point>DO H1"
  1231. (let ((org-M-RET-may-split-line '((headline . t))))
  1232. (org-insert-heading))
  1233. (buffer-string))))
  1234. (should
  1235. (equal "* [#A] H1\n* \n"
  1236. (org-test-with-temp-text "* [#<point>A] H1"
  1237. (let ((org-M-RET-may-split-line '((headline . t))))
  1238. (org-insert-heading))
  1239. (buffer-string))))
  1240. (should
  1241. (equal "* H1 :tag:\n* \n"
  1242. (org-test-with-temp-text "* H1 :ta<point>g:"
  1243. (let ((org-M-RET-may-split-line '((headline . t))))
  1244. (org-insert-heading))
  1245. (buffer-string))))
  1246. ;; New headline level depends on the level of the headline above.
  1247. (should
  1248. (equal "** H\n** P"
  1249. (org-test-with-temp-text "** H\n<point>P"
  1250. (org-insert-heading)
  1251. (buffer-string))))
  1252. (should
  1253. (equal "** H\nPara\n** graph"
  1254. (org-test-with-temp-text "** H\nPara<point>graph"
  1255. (let ((org-M-RET-may-split-line '((default . t))))
  1256. (org-insert-heading))
  1257. (buffer-string))))
  1258. (should
  1259. (equal "** \n** H"
  1260. (org-test-with-temp-text "** H"
  1261. (org-insert-heading)
  1262. (buffer-string))))
  1263. ;; When called with one universal argument, insert a new headline at
  1264. ;; the end of the current subtree, independently on the position of
  1265. ;; point.
  1266. (should
  1267. (equal
  1268. "* H1\n** H2\n* \n"
  1269. (org-test-with-temp-text "* H1\n** H2"
  1270. (let ((org-insert-heading-respect-content nil))
  1271. (org-insert-heading '(4)))
  1272. (buffer-string))))
  1273. (should
  1274. (equal
  1275. "* H1\n** H2\n* \n"
  1276. (org-test-with-temp-text "* H<point>1\n** H2"
  1277. (let ((org-insert-heading-respect-content nil))
  1278. (org-insert-heading '(4)))
  1279. (buffer-string))))
  1280. ;; When called with two universal arguments, insert a new headline
  1281. ;; at the end of the grandparent subtree.
  1282. (should
  1283. (equal "* H1\n** H3\n- item\n** H2\n** \n"
  1284. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1285. (let ((org-insert-heading-respect-content nil))
  1286. (org-insert-heading '(16)))
  1287. (buffer-string))))
  1288. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1289. ;; a level 1 heading.
  1290. (should
  1291. (equal "* H1\n** H2\n* \n"
  1292. (org-test-with-temp-text "* H1\n** H2<point>"
  1293. (org-insert-heading nil nil t)
  1294. (buffer-string))))
  1295. (should
  1296. (equal "* H1\n- item\n* "
  1297. (org-test-with-temp-text "* H1\n- item<point>"
  1298. (org-insert-heading nil nil t)
  1299. (buffer-string))))
  1300. ;; Obey `org-blank-before-new-entry'.
  1301. (should
  1302. (equal "* H1\n\n* \n"
  1303. (org-test-with-temp-text "* H1<point>"
  1304. (let ((org-blank-before-new-entry '((heading . t))))
  1305. (org-insert-heading))
  1306. (buffer-string))))
  1307. (should
  1308. (equal "* H1\n* \n"
  1309. (org-test-with-temp-text "* H1<point>"
  1310. (let ((org-blank-before-new-entry '((heading . nil))))
  1311. (org-insert-heading))
  1312. (buffer-string))))
  1313. (should
  1314. (equal "* H1\n* H2\n* \n"
  1315. (org-test-with-temp-text "* H1\n* H2<point>"
  1316. (let ((org-blank-before-new-entry '((heading . auto))))
  1317. (org-insert-heading))
  1318. (buffer-string))))
  1319. (should
  1320. (equal "* H1\n\n* H2\n\n* \n"
  1321. (org-test-with-temp-text "* H1\n\n* H2<point>"
  1322. (let ((org-blank-before-new-entry '((heading . auto))))
  1323. (org-insert-heading))
  1324. (buffer-string))))
  1325. ;; Corner case: correctly insert a headline after an empty one.
  1326. (should
  1327. (equal "* \n* \n"
  1328. (org-test-with-temp-text "* <point>"
  1329. (org-insert-heading)
  1330. (buffer-string))))
  1331. (should
  1332. (org-test-with-temp-text "* <point>\n"
  1333. (org-insert-heading)
  1334. (looking-at-p "\n\\'")))
  1335. ;; Do not insert spurious headlines when inserting a new headline.
  1336. (should
  1337. (equal "* H1\n* H2\n* \n"
  1338. (org-test-with-temp-text "* H1\n* H2<point>\n"
  1339. (org-insert-heading)
  1340. (buffer-string))))
  1341. ;; Preserve visibility at beginning of line. In particular, when
  1342. ;; removing spurious blank lines, do not visually merge heading with
  1343. ;; the line visible above.
  1344. (should-not
  1345. (org-test-with-temp-text "* H1<point>\nContents\n\n* H2\n"
  1346. (org-overview)
  1347. (let ((org-blank-before-new-entry '((heading . nil))))
  1348. (org-insert-heading '(4)))
  1349. (invisible-p (line-end-position 0))))
  1350. ;; Properly handle empty lines when forcing a headline below current
  1351. ;; one.
  1352. (should
  1353. (equal "* H1\n\n* H\n\n* \n"
  1354. (org-test-with-temp-text "* H1\n\n* H<point>"
  1355. (let ((org-blank-before-new-entry '((heading . t))))
  1356. (org-insert-heading '(4))
  1357. (buffer-string))))))
  1358. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1359. "Test `org-insert-todo-heading-respect-content' specifications."
  1360. ;; Create a TODO heading.
  1361. (should
  1362. (org-test-with-temp-text "* H1\n Body"
  1363. (org-insert-todo-heading-respect-content)
  1364. (nth 2 (org-heading-components))))
  1365. ;; Add headline at the end of the first subtree
  1366. (should
  1367. (equal
  1368. "* TODO \n"
  1369. (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
  1370. (org-insert-todo-heading-respect-content)
  1371. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1372. ;; In a list, do not create a new item.
  1373. (should
  1374. (equal
  1375. "* TODO \n"
  1376. (org-test-with-temp-text "* H\n- an item\n- another one"
  1377. (search-forward "an ")
  1378. (org-insert-todo-heading-respect-content)
  1379. (buffer-substring-no-properties (line-beginning-position) (point-max))))))
  1380. (ert-deftest test-org/clone-with-time-shift ()
  1381. "Test `org-clone-subtree-with-time-shift'."
  1382. ;; Raise an error before first heading.
  1383. (should-error
  1384. (org-test-with-temp-text ""
  1385. (org-clone-subtree-with-time-shift 1)))
  1386. ;; Raise an error on invalid number of clones.
  1387. (should-error
  1388. (org-test-with-temp-text "* Clone me"
  1389. (org-clone-subtree-with-time-shift -1)))
  1390. ;; Clone non-repeating once.
  1391. (should
  1392. (equal "\
  1393. * H1\n<2015-06-21>
  1394. * H1\n<2015-06-23>
  1395. "
  1396. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1397. (org-clone-subtree-with-time-shift 1 "+2d")
  1398. (replace-regexp-in-string
  1399. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1400. nil nil 1))))
  1401. ;; Clone repeating once.
  1402. (should
  1403. (equal "\
  1404. * H1\n<2015-06-21>
  1405. * H1\n<2015-06-23>
  1406. * H1\n<2015-06-25 +1w>
  1407. "
  1408. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1409. (org-clone-subtree-with-time-shift 1 "+2d")
  1410. (replace-regexp-in-string
  1411. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1412. nil nil 1))))
  1413. ;; Clone non-repeating zero times.
  1414. (should
  1415. (equal "\
  1416. * H1\n<2015-06-21>
  1417. "
  1418. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1419. (org-clone-subtree-with-time-shift 0 "+2d")
  1420. (replace-regexp-in-string
  1421. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1422. nil nil 1))))
  1423. ;; Clone repeating "zero" times.
  1424. (should
  1425. (equal "\
  1426. * H1\n<2015-06-21>
  1427. * H1\n<2015-06-23 +1w>
  1428. "
  1429. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1430. (org-clone-subtree-with-time-shift 0 "+2d")
  1431. (replace-regexp-in-string
  1432. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1433. nil nil 1))))
  1434. ;; Clone with blank SHIFT argument.
  1435. (should
  1436. (string-prefix-p
  1437. "* H <2012-03-29"
  1438. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1439. (org-clone-subtree-with-time-shift 1 "")
  1440. (buffer-substring-no-properties (line-beginning-position 2)
  1441. (line-end-position 2)))))
  1442. ;; Find time stamps before point. If SHIFT is not specified, ask
  1443. ;; for a time shift.
  1444. (should
  1445. (string-prefix-p
  1446. "* H <2012-03-30"
  1447. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1448. (org-clone-subtree-with-time-shift 1 "+1d")
  1449. (buffer-substring-no-properties (line-beginning-position 2)
  1450. (line-end-position 2)))))
  1451. (should
  1452. (string-prefix-p
  1453. "* H <2014-03-05"
  1454. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  1455. (cl-letf (((symbol-function 'read-from-minibuffer)
  1456. (lambda (&rest args) "+1d")))
  1457. (org-clone-subtree-with-time-shift 1))
  1458. (buffer-substring-no-properties (line-beginning-position 2)
  1459. (line-end-position 2))))))
  1460. ;;; Fixed-Width Areas
  1461. (ert-deftest test-org/toggle-fixed-width ()
  1462. "Test `org-toggle-fixed-width' specifications."
  1463. ;; No region: Toggle on fixed-width marker in paragraphs.
  1464. (should
  1465. (equal ": A"
  1466. (org-test-with-temp-text "A"
  1467. (org-toggle-fixed-width)
  1468. (buffer-string))))
  1469. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1470. (should
  1471. (equal "A"
  1472. (org-test-with-temp-text ": A"
  1473. (org-toggle-fixed-width)
  1474. (buffer-string))))
  1475. ;; No region: Toggle on marker in blank lines after elements or just
  1476. ;; after a headline.
  1477. (should
  1478. (equal "* H\n: "
  1479. (org-test-with-temp-text "* H\n"
  1480. (forward-line)
  1481. (org-toggle-fixed-width)
  1482. (buffer-string))))
  1483. (should
  1484. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1485. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1486. (goto-char (point-max))
  1487. (org-toggle-fixed-width)
  1488. (buffer-string))))
  1489. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1490. ;; headlines, clocks)
  1491. (should
  1492. (equal ": * Headline"
  1493. (org-test-with-temp-text "* Headline"
  1494. (org-toggle-fixed-width)
  1495. (buffer-string))))
  1496. (should
  1497. (equal ": #+KEYWORD: value"
  1498. (org-test-with-temp-text "#+KEYWORD: value"
  1499. (org-toggle-fixed-width)
  1500. (buffer-string))))
  1501. ;; No region: error in other situations.
  1502. (should-error
  1503. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1504. (forward-line)
  1505. (org-toggle-fixed-width)
  1506. (buffer-string)))
  1507. ;; No region: Indentation is preserved.
  1508. (should
  1509. (equal "- A\n : B"
  1510. (org-test-with-temp-text "- A\n B"
  1511. (forward-line)
  1512. (org-toggle-fixed-width)
  1513. (buffer-string))))
  1514. ;; Region: If it contains only fixed-width elements and blank lines,
  1515. ;; toggle off fixed-width markup.
  1516. (should
  1517. (equal "A\n\nB"
  1518. (org-test-with-temp-text ": A\n\n: B"
  1519. (transient-mark-mode 1)
  1520. (push-mark (point) t t)
  1521. (goto-char (point-max))
  1522. (org-toggle-fixed-width)
  1523. (buffer-string))))
  1524. ;; Region: If it contains anything else, toggle on fixed-width but
  1525. ;; not on fixed-width areas.
  1526. (should
  1527. (equal ": A\n: \n: B\n: \n: C"
  1528. (org-test-with-temp-text "A\n\n: B\n\nC"
  1529. (transient-mark-mode 1)
  1530. (push-mark (point) t t)
  1531. (goto-char (point-max))
  1532. (org-toggle-fixed-width)
  1533. (buffer-string))))
  1534. ;; Region: Ignore blank lines at its end, unless it contains only
  1535. ;; such lines.
  1536. (should
  1537. (equal ": A\n\n"
  1538. (org-test-with-temp-text "A\n\n"
  1539. (transient-mark-mode 1)
  1540. (push-mark (point) t t)
  1541. (goto-char (point-max))
  1542. (org-toggle-fixed-width)
  1543. (buffer-string))))
  1544. (should
  1545. (equal ": \n: \n"
  1546. (org-test-with-temp-text "\n\n"
  1547. (transient-mark-mode 1)
  1548. (push-mark (point) t t)
  1549. (goto-char (point-max))
  1550. (org-toggle-fixed-width)
  1551. (buffer-string)))))
  1552. ;;; Headline
  1553. (ert-deftest test-org/get-heading ()
  1554. "Test `org-get-heading' specifications."
  1555. ;; Return current heading, even if point is not on it.
  1556. (should
  1557. (equal "H"
  1558. (org-test-with-temp-text "* H"
  1559. (org-get-heading))))
  1560. (should
  1561. (equal "H"
  1562. (org-test-with-temp-text "* H\nText<point>"
  1563. (org-get-heading))))
  1564. ;; Without any optional argument, return TODO keyword, priority
  1565. ;; cookie, COMMENT keyword and tags.
  1566. (should
  1567. (equal "TODO H"
  1568. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1569. (org-get-heading))))
  1570. (should
  1571. (equal "[#A] H"
  1572. (org-test-with-temp-text "* [#A] H"
  1573. (org-get-heading))))
  1574. (should
  1575. (equal "COMMENT H"
  1576. (org-test-with-temp-text "* COMMENT H"
  1577. (org-get-heading))))
  1578. (should
  1579. (equal "H :tag:"
  1580. (org-test-with-temp-text "* H :tag:"
  1581. (org-get-heading))))
  1582. ;; With NO-TAGS argument, ignore tags.
  1583. (should
  1584. (equal "TODO H"
  1585. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1586. (org-get-heading t))))
  1587. (should
  1588. (equal "H"
  1589. (org-test-with-temp-text "* H :tag:"
  1590. (org-get-heading t))))
  1591. ;; With NO-TODO, ignore TODO keyword.
  1592. (should
  1593. (equal "H"
  1594. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1595. (org-get-heading nil t))))
  1596. (should
  1597. (equal "H :tag:"
  1598. (org-test-with-temp-text "* H :tag:"
  1599. (org-get-heading nil t))))
  1600. ;; TODO keywords are case-sensitive.
  1601. (should
  1602. (equal "Todo H"
  1603. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1604. (org-get-heading nil t))))
  1605. ;; With NO-PRIORITY, ignore priority.
  1606. (should
  1607. (equal "H"
  1608. (org-test-with-temp-text "* [#A] H"
  1609. (org-get-heading nil nil t))))
  1610. (should
  1611. (equal "H"
  1612. (org-test-with-temp-text "* H"
  1613. (org-get-heading nil nil t))))
  1614. (should
  1615. (equal "TODO H"
  1616. (org-test-with-temp-text "* TODO [#A] H"
  1617. (org-get-heading nil nil t))))
  1618. ;; With NO-COMMENT, ignore COMMENT keyword.
  1619. (should
  1620. (equal "H"
  1621. (org-test-with-temp-text "* COMMENT H"
  1622. (org-get-heading nil nil nil t))))
  1623. (should
  1624. (equal "H"
  1625. (org-test-with-temp-text "* H"
  1626. (org-get-heading nil nil nil t))))
  1627. (should
  1628. (equal "TODO [#A] H"
  1629. (org-test-with-temp-text "* TODO [#A] COMMENT H"
  1630. (org-get-heading nil nil nil t))))
  1631. ;; On an empty headline, return value is consistent.
  1632. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1633. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1634. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1635. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil t))))
  1636. (should
  1637. (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil nil t)))))
  1638. (ert-deftest test-org/in-commented-heading-p ()
  1639. "Test `org-in-commented-heading-p' specifications."
  1640. ;; Commented headline.
  1641. (should
  1642. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1643. (goto-char (point-max))
  1644. (org-in-commented-heading-p)))
  1645. ;; Commented ancestor.
  1646. (should
  1647. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1648. (goto-char (point-max))
  1649. (org-in-commented-heading-p)))
  1650. ;; Comment keyword is case-sensitive.
  1651. (should-not
  1652. (org-test-with-temp-text "* Comment Headline\nBody"
  1653. (goto-char (point-max))
  1654. (org-in-commented-heading-p)))
  1655. ;; Keyword is standalone.
  1656. (should-not
  1657. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1658. (goto-char (point-max))
  1659. (org-in-commented-heading-p)))
  1660. ;; Optional argument.
  1661. (should-not
  1662. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1663. (goto-char (point-max))
  1664. (org-in-commented-heading-p t))))
  1665. (ert-deftest test-org/entry-blocked-p ()
  1666. ;; Check other dependencies.
  1667. (should
  1668. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1669. (let ((org-enforce-todo-dependencies t)
  1670. (org-blocker-hook
  1671. '(org-block-todo-from-children-or-siblings-or-parent)))
  1672. (org-entry-blocked-p))))
  1673. (should-not
  1674. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1675. (let ((org-enforce-todo-dependencies t)
  1676. (org-blocker-hook
  1677. '(org-block-todo-from-children-or-siblings-or-parent)))
  1678. (org-entry-blocked-p))))
  1679. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1680. ;; blocked.
  1681. (should-not
  1682. (org-test-with-temp-text "* Blocked\n** TODO one"
  1683. (let ((org-enforce-todo-dependencies t)
  1684. (org-blocker-hook
  1685. '(org-block-todo-from-children-or-siblings-or-parent)))
  1686. (org-entry-blocked-p))))
  1687. (should-not
  1688. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1689. (let ((org-enforce-todo-dependencies t)
  1690. (org-blocker-hook
  1691. '(org-block-todo-from-children-or-siblings-or-parent)))
  1692. (org-entry-blocked-p))))
  1693. ;; Follow :ORDERED: specifications.
  1694. (should
  1695. (org-test-with-temp-text
  1696. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1697. (let ((org-enforce-todo-dependencies t)
  1698. (org-blocker-hook
  1699. '(org-block-todo-from-children-or-siblings-or-parent)))
  1700. (org-entry-blocked-p))))
  1701. (should-not
  1702. (org-test-with-temp-text
  1703. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1704. (let ((org-enforce-todo-dependencies t)
  1705. (org-blocker-hook
  1706. '(org-block-todo-from-children-or-siblings-or-parent)))
  1707. (org-entry-blocked-p)))))
  1708. (ert-deftest test-org/get-outline-path ()
  1709. "Test `org-get-outline-path' specifications."
  1710. ;; Top-level headlines have no outline path.
  1711. (should-not
  1712. (org-test-with-temp-text "* H"
  1713. (org-get-outline-path)))
  1714. ;; Otherwise, outline path is the path leading to the headline.
  1715. (should
  1716. (equal '("H")
  1717. (org-test-with-temp-text "* H\n** S<point>"
  1718. (org-get-outline-path))))
  1719. ;; Find path even when point is not on a headline.
  1720. (should
  1721. (equal '("H")
  1722. (org-test-with-temp-text "* H\n** S\nText<point>"
  1723. (org-get-outline-path))))
  1724. ;; TODO keywords, tags and statistics cookies are ignored.
  1725. (should
  1726. (equal '("H")
  1727. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1728. (org-get-outline-path))))
  1729. ;; Links are replaced with their description or their path.
  1730. (should
  1731. (equal '("Org")
  1732. (org-test-with-temp-text
  1733. "* [[http://orgmode.org][Org]]\n** S<point>"
  1734. (org-get-outline-path))))
  1735. (should
  1736. (equal '("http://orgmode.org")
  1737. (org-test-with-temp-text
  1738. "* [[http://orgmode.org]]\n** S<point>"
  1739. (org-get-outline-path))))
  1740. ;; When WITH-SELF is non-nil, include current heading.
  1741. (should
  1742. (equal '("H")
  1743. (org-test-with-temp-text "* H"
  1744. (org-get-outline-path t))))
  1745. (should
  1746. (equal '("H" "S")
  1747. (org-test-with-temp-text "* H\n** S\nText<point>"
  1748. (org-get-outline-path t))))
  1749. ;; Using cache is transparent to the user.
  1750. (should
  1751. (equal '("H")
  1752. (org-test-with-temp-text "* H\n** S<point>"
  1753. (setq org-outline-path-cache nil)
  1754. (org-get-outline-path nil t))))
  1755. ;; Do not corrupt cache when finding outline path in distant part of
  1756. ;; the buffer.
  1757. (should
  1758. (equal '("H2")
  1759. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1760. (setq org-outline-path-cache nil)
  1761. (org-get-outline-path nil t)
  1762. (search-forward "S2")
  1763. (org-get-outline-path nil t))))
  1764. ;; Do not choke on empty headlines.
  1765. (should
  1766. (org-test-with-temp-text "* H\n** <point>"
  1767. (org-get-outline-path)))
  1768. (should
  1769. (org-test-with-temp-text "* \n** H<point>"
  1770. (org-get-outline-path))))
  1771. (ert-deftest test-org/format-outline-path ()
  1772. "Test `org-format-outline-path' specifications."
  1773. (should
  1774. (string= (org-format-outline-path (list "one" "two" "three"))
  1775. "one/two/three"))
  1776. ;; Empty path.
  1777. (should
  1778. (string= (org-format-outline-path '())
  1779. ""))
  1780. (should
  1781. (string= (org-format-outline-path '(nil))
  1782. ""))
  1783. ;; Empty path and prefix.
  1784. (should
  1785. (string= (org-format-outline-path '() nil ">>")
  1786. ">>"))
  1787. ;; Trailing whitespace in headings.
  1788. (should
  1789. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1790. "one/tw o/three"))
  1791. ;; Non-default prefix and separators.
  1792. (should
  1793. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1794. ">>|one|two|three"))
  1795. ;; Truncate.
  1796. (should
  1797. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1798. "one/two/.."))
  1799. ;; Give a very narrow width.
  1800. (should
  1801. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1802. "on"))
  1803. ;; Give a prefix that extends beyond the width.
  1804. (should
  1805. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1806. ">>>>>>>>>>")
  1807. ">>>>>>>>..")))
  1808. (ert-deftest test-org/map-entries ()
  1809. "Test `org-map-entries' specifications."
  1810. ;; Full match.
  1811. (should
  1812. (equal '(1 11)
  1813. (org-test-with-temp-text "* Level 1\n** Level 2"
  1814. (org-map-entries #'point))))
  1815. ;; Level match.
  1816. (should
  1817. (equal '(1)
  1818. (org-test-with-temp-text "* Level 1\n** Level 2"
  1819. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1820. (should
  1821. (equal '(11)
  1822. (org-test-with-temp-text "* Level 1\n** Level 2"
  1823. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1824. ;; Tag match.
  1825. (should
  1826. (equal '(11)
  1827. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1828. (org-map-entries #'point "yes"))))
  1829. (should
  1830. (equal '(14)
  1831. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1832. (org-map-entries #'point "+yes-a"))))
  1833. (should
  1834. (equal '(11 23)
  1835. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1836. (org-map-entries #'point "{yes?}"))))
  1837. ;; Priority match.
  1838. (should
  1839. (equal '(1)
  1840. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1841. (org-map-entries #'point "PRIORITY=\"A\""))))
  1842. ;; Date match.
  1843. (should
  1844. (equal '(36)
  1845. (org-test-with-temp-text "
  1846. * H1
  1847. SCHEDULED: <2012-03-29 thu.>
  1848. * H2
  1849. SCHEDULED: <2014-03-04 tue.>"
  1850. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1851. (should
  1852. (equal '(2)
  1853. (org-test-with-temp-text "
  1854. * H1
  1855. SCHEDULED: <2012-03-29 thu.>
  1856. * H2
  1857. SCHEDULED: <2014-03-04 tue.>"
  1858. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1859. ;; Regular property match.
  1860. (should
  1861. (equal '(2)
  1862. (org-test-with-temp-text "
  1863. * H1
  1864. :PROPERTIES:
  1865. :TEST: 1
  1866. :END:
  1867. * H2
  1868. :PROPERTIES:
  1869. :TEST: 2
  1870. :END:"
  1871. (org-map-entries #'point "TEST=1"))))
  1872. ;; Multiple criteria.
  1873. (should
  1874. (equal '(23)
  1875. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1876. (let (org-odd-levels-only
  1877. (org-use-tag-inheritance nil))
  1878. (org-map-entries #'point "yes+LEVEL=1")))))
  1879. ;; "or" criteria.
  1880. (should
  1881. (equal '(12 24)
  1882. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1883. (let (org-odd-levels-only)
  1884. (org-map-entries #'point "LEVEL=2|no")))))
  1885. (should
  1886. (equal '(1 12)
  1887. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  1888. (let (org-odd-levels-only)
  1889. (org-map-entries #'point "yes|no")))))
  1890. ;; "and" criteria.
  1891. (should
  1892. (equal '(22)
  1893. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  1894. (let (org-odd-levels-only)
  1895. (org-map-entries #'point "yes&no"))))))
  1896. (ert-deftest test-org/edit-headline ()
  1897. "Test `org-edit-headline' specifications."
  1898. (should
  1899. (equal "* B"
  1900. (org-test-with-temp-text "* A"
  1901. (org-edit-headline "B")
  1902. (buffer-string))))
  1903. ;; Handle empty headings.
  1904. (should
  1905. (equal "* "
  1906. (org-test-with-temp-text "* A"
  1907. (org-edit-headline "")
  1908. (buffer-string))))
  1909. (should
  1910. (equal "* A"
  1911. (org-test-with-temp-text "* "
  1912. (org-edit-headline "A")
  1913. (buffer-string))))
  1914. ;; Handle TODO keywords and priority cookies.
  1915. (should
  1916. (equal "* TODO B"
  1917. (org-test-with-temp-text "* TODO A"
  1918. (org-edit-headline "B")
  1919. (buffer-string))))
  1920. (should
  1921. (equal "* [#A] B"
  1922. (org-test-with-temp-text "* [#A] A"
  1923. (org-edit-headline "B")
  1924. (buffer-string))))
  1925. (should
  1926. (equal "* TODO [#A] B"
  1927. (org-test-with-temp-text "* TODO [#A] A"
  1928. (org-edit-headline "B")
  1929. (buffer-string))))
  1930. ;; Handle tags.
  1931. (equal "* B :tag:"
  1932. (org-test-with-temp-text "* A :tag:"
  1933. (let ((org-tags-column 4)) (org-edit-headline "B"))
  1934. (buffer-string))))
  1935. ;;; Keywords
  1936. (ert-deftest test-org/set-regexps-and-options ()
  1937. "Test `org-set-regexps-and-options' specifications."
  1938. ;; TAGS keyword.
  1939. (should
  1940. (equal '(("A"))
  1941. (let ((org-tag-alist '(("A")))
  1942. (org-tag-persistent-alist nil))
  1943. (org-test-with-temp-text ""
  1944. (org-mode-restart)
  1945. org-current-tag-alist))))
  1946. (should
  1947. (equal '(("B"))
  1948. (let ((org-tag-alist '(("A")))
  1949. (org-tag-persistent-alist nil))
  1950. (org-test-with-temp-text "#+TAGS: B"
  1951. (org-mode-restart)
  1952. org-current-tag-alist))))
  1953. (should
  1954. (equal '(("C") ("B"))
  1955. (let ((org-tag-alist '(("A")))
  1956. (org-tag-persistent-alist '(("C"))))
  1957. (org-test-with-temp-text "#+TAGS: B"
  1958. (org-mode-restart)
  1959. org-current-tag-alist))))
  1960. (should
  1961. (equal '(("B"))
  1962. (let ((org-tag-alist '(("A")))
  1963. (org-tag-persistent-alist '(("C"))))
  1964. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  1965. (org-mode-restart)
  1966. org-current-tag-alist))))
  1967. (should
  1968. (equal '(("A" . ?a) ("B") ("C"))
  1969. (let ((org-tag-persistant-alist nil))
  1970. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1971. (org-mode-restart)
  1972. org-current-tag-alist))))
  1973. (should
  1974. (equal '(("A") (:newline) ("B"))
  1975. (let ((org-tag-persistent-alist nil))
  1976. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1977. (org-mode-restart)
  1978. org-current-tag-alist))))
  1979. (should
  1980. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1981. (let ((org-tag-persistent-alist nil))
  1982. (org-test-with-temp-text "#+TAGS: { A B } C"
  1983. (org-mode-restart)
  1984. org-current-tag-alist))))
  1985. (should
  1986. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1987. (let ((org-tag-persistent-alist nil))
  1988. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1989. (org-mode-restart)
  1990. org-current-tag-alist))))
  1991. (should
  1992. (equal '(("A" "B" "C"))
  1993. (let ((org-tag-persistent-alist nil))
  1994. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1995. (org-mode-restart)
  1996. org-tag-groups-alist))))
  1997. (should
  1998. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1999. (let ((org-tag-persistent-alist nil))
  2000. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2001. (org-mode-restart)
  2002. org-current-tag-alist))))
  2003. (should
  2004. (equal '(("A" "B" "C"))
  2005. (let ((org-tag-persistent-alist nil))
  2006. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2007. (org-mode-restart)
  2008. org-tag-groups-alist))))
  2009. ;; FILETAGS keyword.
  2010. (should
  2011. (equal '("A" "B" "C")
  2012. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  2013. (org-mode-restart)
  2014. org-file-tags)))
  2015. ;; PROPERTY keyword. Property names are case-insensitive.
  2016. (should
  2017. (equal "foo=1"
  2018. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  2019. (org-mode-restart)
  2020. (cdr (assoc "var" org-file-properties)))))
  2021. (should
  2022. (equal
  2023. "foo=1 bar=2"
  2024. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  2025. (org-mode-restart)
  2026. (cdr (assoc "var" org-file-properties)))))
  2027. (should
  2028. (equal
  2029. "foo=1 bar=2"
  2030. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  2031. (org-mode-restart)
  2032. (cdr (assoc "var" org-file-properties)))))
  2033. ;; ARCHIVE keyword.
  2034. (should
  2035. (equal "%s_done::"
  2036. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  2037. (org-mode-restart)
  2038. org-archive-location)))
  2039. ;; CATEGORY keyword.
  2040. (should
  2041. (eq 'test
  2042. (org-test-with-temp-text "#+CATEGORY: test"
  2043. (org-mode-restart)
  2044. org-category)))
  2045. (should
  2046. (equal "test"
  2047. (org-test-with-temp-text "#+CATEGORY: test"
  2048. (org-mode-restart)
  2049. (cdr (assoc "CATEGORY" org-file-properties)))))
  2050. ;; COLUMNS keyword.
  2051. (should
  2052. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  2053. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  2054. (org-mode-restart)
  2055. org-columns-default-format)))
  2056. ;; CONSTANTS keyword. Constants names are case sensitive.
  2057. (should
  2058. (equal '("299792458." "3.14")
  2059. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  2060. (org-mode-restart)
  2061. (mapcar
  2062. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  2063. '("c" "pi")))))
  2064. (should
  2065. (equal "3.14"
  2066. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  2067. (org-mode-restart)
  2068. (cdr (assoc "pi" org-table-formula-constants-local)))))
  2069. (should
  2070. (equal "22/7"
  2071. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  2072. (org-mode-restart)
  2073. (cdr (assoc "PI" org-table-formula-constants-local)))))
  2074. ;; LINK keyword.
  2075. (should
  2076. (equal
  2077. '("url1" "url2")
  2078. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  2079. (org-mode-restart)
  2080. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  2081. '("a" "b")))))
  2082. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  2083. (should
  2084. (equal
  2085. '(?X ?Z ?Y)
  2086. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  2087. (org-mode-restart)
  2088. (list org-highest-priority org-lowest-priority org-default-priority))))
  2089. (should
  2090. (equal
  2091. '(?A ?C ?B)
  2092. (org-test-with-temp-text "#+PRIORITIES: X Z"
  2093. (org-mode-restart)
  2094. (list org-highest-priority org-lowest-priority org-default-priority))))
  2095. ;; STARTUP keyword.
  2096. (should
  2097. (equal '(t t)
  2098. (org-test-with-temp-text "#+STARTUP: fold odd"
  2099. (org-mode-restart)
  2100. (list org-startup-folded org-odd-levels-only))))
  2101. ;; TODO keywords.
  2102. (should
  2103. (equal '(("A" "B") ("C"))
  2104. (org-test-with-temp-text "#+TODO: A B | C"
  2105. (org-mode-restart)
  2106. (list org-not-done-keywords org-done-keywords))))
  2107. (should
  2108. (equal '(("A" "C") ("B" "D"))
  2109. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  2110. (org-mode-restart)
  2111. (list org-not-done-keywords org-done-keywords))))
  2112. (should
  2113. (equal '(("A" "B") ("C"))
  2114. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  2115. (org-mode-restart)
  2116. (list org-not-done-keywords org-done-keywords))))
  2117. (should
  2118. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  2119. (org-test-with-temp-text "#+TODO: A(a)"
  2120. (org-mode-restart)
  2121. org-todo-key-alist)))
  2122. (should
  2123. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  2124. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  2125. (org-mode-restart)
  2126. org-todo-log-states)))
  2127. ;; Enter SETUPFILE keyword.
  2128. (should
  2129. (equal "1"
  2130. (org-test-with-temp-text
  2131. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  2132. (org-mode-restart)
  2133. (cdr (assoc "a" org-file-properties))))))
  2134. ;;; Links
  2135. ;;;; Coderefs
  2136. (ert-deftest test-org/coderef ()
  2137. "Test coderef links specifications."
  2138. (should
  2139. (org-test-with-temp-text "
  2140. #+BEGIN_SRC emacs-lisp
  2141. \(+ 1 1) (ref:sc)
  2142. #+END_SRC
  2143. \[[(sc)<point>]]"
  2144. (org-open-at-point)
  2145. (looking-at "(ref:sc)")))
  2146. ;; Find coderef even with alternate label format.
  2147. (should
  2148. (org-test-with-temp-text "
  2149. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2150. \(+ 1 1) {ref:sc}
  2151. #+END_SRC
  2152. \[[(sc)<point>]]"
  2153. (org-open-at-point)
  2154. (looking-at "{ref:sc}"))))
  2155. ;;;; Custom ID
  2156. (ert-deftest test-org/custom-id ()
  2157. "Test custom ID links specifications."
  2158. (should
  2159. (org-test-with-temp-text
  2160. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2161. (org-open-at-point)
  2162. (looking-at-p "\\* H1")))
  2163. ;; Throw an error on false positives.
  2164. (should-error
  2165. (org-test-with-temp-text
  2166. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2167. (org-open-at-point)
  2168. (looking-at-p "\\* H1"))))
  2169. ;;;; Fuzzy Links
  2170. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2171. ;; a named element (#+name: text) and to headlines (* Text).
  2172. (ert-deftest test-org/fuzzy-links ()
  2173. "Test fuzzy links specifications."
  2174. ;; Fuzzy link goes in priority to a matching target.
  2175. (should
  2176. (org-test-with-temp-text
  2177. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2178. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2179. (looking-at "<<Test>>")))
  2180. ;; Then fuzzy link points to an element with a given name.
  2181. (should
  2182. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2183. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2184. (looking-at "#\\+NAME: Test")))
  2185. ;; A target still lead to a matching headline otherwise.
  2186. (should
  2187. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2188. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2189. (looking-at "\\* Head2")))
  2190. ;; With a leading star in link, enforce heading match.
  2191. (should
  2192. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2193. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2194. (looking-at "\\* Test")))
  2195. ;; With a leading star in link, enforce exact heading match, even
  2196. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2197. (should-error
  2198. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2199. (let ((org-link-search-must-match-exact-headline nil))
  2200. (org-open-at-point))))
  2201. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2202. (should
  2203. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2204. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2205. (looking-at "\\* Test")))
  2206. (should
  2207. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2208. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2209. (looking-at "\\* Test")))
  2210. ;; Heading match should not care about spaces, cookies, TODO
  2211. ;; keywords, priorities, and tags. However, TODO keywords are
  2212. ;; case-sensitive.
  2213. (should
  2214. (let ((first-line
  2215. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2216. (org-test-with-temp-text
  2217. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2218. (let ((org-link-search-must-match-exact-headline nil)
  2219. (org-todo-regexp "TODO"))
  2220. (org-open-at-point))
  2221. (looking-at (regexp-quote first-line)))))
  2222. (should-error
  2223. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2224. (let ((org-link-search-must-match-exact-headline nil)
  2225. (org-todo-regexp "TODO"))
  2226. (org-open-at-point))))
  2227. ;; Heading match should still be exact.
  2228. (should-error
  2229. (org-test-with-temp-text "
  2230. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2231. Foo Bar
  2232. <point>[[*Test 1]]"
  2233. (let ((org-link-search-must-match-exact-headline nil)
  2234. (org-todo-regexp "TODO"))
  2235. (org-open-at-point))))
  2236. (should
  2237. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2238. (let ((org-link-search-must-match-exact-headline nil)
  2239. (org-todo-regexp "TODO"))
  2240. (org-open-at-point))
  2241. (looking-at-p (regexp-quote "** Test 1 2"))))
  2242. ;; Heading match ignores COMMENT keyword.
  2243. (should
  2244. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2245. (org-open-at-point)
  2246. (looking-at "\\* COMMENT Test")))
  2247. (should
  2248. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2249. (org-open-at-point)
  2250. (looking-at "\\* TODO COMMENT Test")))
  2251. ;; Correctly un-hexify fuzzy links.
  2252. (should
  2253. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2254. (org-open-at-point)
  2255. (bobp)))
  2256. (should
  2257. (org-test-with-temp-text "* [1]\n[[*%5B1%5D<point>]]"
  2258. (org-open-at-point)
  2259. (bobp)))
  2260. ;; Match search strings containing newline characters, including
  2261. ;; blank lines.
  2262. (should
  2263. (org-test-with-temp-text-in-file "Paragraph\n\nline1\nline2\n\n"
  2264. (let ((file (buffer-file-name)))
  2265. (goto-char (point-max))
  2266. (insert (format "[[file:%s::line1 line2]]" file))
  2267. (beginning-of-line)
  2268. (let ((org-link-search-must-match-exact-headline nil))
  2269. (org-open-at-point 0))
  2270. (looking-at-p "line1"))))
  2271. (should
  2272. (org-test-with-temp-text-in-file "Paragraph\n\nline1\n\nline2\n\n"
  2273. (let ((file (buffer-file-name)))
  2274. (goto-char (point-max))
  2275. (insert (format "[[file:%s::line1 line2]]" file))
  2276. (beginning-of-line)
  2277. (let ((org-link-search-must-match-exact-headline nil))
  2278. (org-open-at-point 0))
  2279. (looking-at-p "line1")))))
  2280. ;;;; Link Escaping
  2281. (ert-deftest test-org/org-link-escape-ascii-character ()
  2282. "Escape an ascii character."
  2283. (should
  2284. (string=
  2285. "%5B"
  2286. (org-link-escape "["))))
  2287. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2288. "Escape an ascii control character."
  2289. (should
  2290. (string=
  2291. "%09"
  2292. (org-link-escape "\t"))))
  2293. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2294. "Escape an unicode multibyte character."
  2295. (should
  2296. (string=
  2297. "%E2%82%AC"
  2298. (org-link-escape "€"))))
  2299. (ert-deftest test-org/org-link-escape-custom-table ()
  2300. "Escape string with custom character table."
  2301. (should
  2302. (string=
  2303. "Foo%3A%42ar%0A"
  2304. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2305. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2306. "Escape string with custom table merged with default table."
  2307. (should
  2308. (string=
  2309. "%5BF%6F%6F%3A%42ar%0A%5D"
  2310. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2311. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2312. "Unescape an ascii character."
  2313. (should
  2314. (string=
  2315. "["
  2316. (org-link-unescape "%5B"))))
  2317. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2318. "Unescpae an ascii control character."
  2319. (should
  2320. (string=
  2321. "\n"
  2322. (org-link-unescape "%0A"))))
  2323. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2324. "Unescape unicode multibyte character."
  2325. (should
  2326. (string=
  2327. "€"
  2328. (org-link-unescape "%E2%82%AC"))))
  2329. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2330. "Unescape old style percent escaped character."
  2331. (should
  2332. (string=
  2333. "àâçèéêîôùû"
  2334. (decode-coding-string
  2335. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2336. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2337. "Escape and unescape a URL that includes an escaped char.
  2338. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2339. (should
  2340. (string=
  2341. "http://some.host.com/form?&id=blah%2Bblah25"
  2342. (org-link-unescape
  2343. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2344. ;;;; Open at point
  2345. (ert-deftest test-org/open-at-point/keyword ()
  2346. "Does `org-open-at-point' open link in a keyword line?"
  2347. (should
  2348. (org-test-with-temp-text
  2349. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2350. (org-open-at-point) t))
  2351. (should
  2352. (org-test-with-temp-text
  2353. "* H\n<<top>>\n#+KEYWORD: <point>[[top]]"
  2354. (org-open-at-point) t)))
  2355. (ert-deftest test-org/open-at-point/property ()
  2356. "Does `org-open-at-point' open link in property drawer?"
  2357. (should
  2358. (org-test-with-temp-text
  2359. "* Headline
  2360. :PROPERTIES:
  2361. :URL: <point>[[*Headline]]
  2362. :END:"
  2363. (org-open-at-point) t)))
  2364. (ert-deftest test-org/open-at-point/comment ()
  2365. "Does `org-open-at-point' open link in a commented line?"
  2366. (should
  2367. (org-test-with-temp-text
  2368. "<<top>>\n# <point>[[top]]"
  2369. (org-open-at-point) t))
  2370. (should
  2371. (org-test-with-temp-text
  2372. "* H\n<<top>>\n# <point>[[top]]"
  2373. (org-open-at-point) t)))
  2374. (ert-deftest test-org/open-at-point/inline-image ()
  2375. "Test `org-open-at-point' on nested links."
  2376. (should
  2377. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2378. (org-open-at-point)
  2379. (bobp))))
  2380. (ert-deftest test-org/open-at-point/radio-target ()
  2381. "Test `org-open-at-point' on radio targets."
  2382. (should
  2383. (org-test-with-temp-text "<<<target>>> <point>target"
  2384. (org-update-radio-target-regexp)
  2385. (org-open-at-point)
  2386. (eq (org-element-type (org-element-context)) 'radio-target))))
  2387. ;;;; Stored links
  2388. (ert-deftest test-org/store-link ()
  2389. "Test `org-store-link' specifications."
  2390. ;; On a headline, link to that headline. Use heading as the
  2391. ;; description of the link.
  2392. (should
  2393. (let (org-store-link-props org-stored-links)
  2394. (org-test-with-temp-text-in-file "* H1"
  2395. (let ((file (buffer-file-name)))
  2396. (equal (format "[[file:%s::*H1][H1]]" file)
  2397. (org-store-link nil))))))
  2398. ;; On a headline, remove any link from description.
  2399. (should
  2400. (let (org-store-link-props org-stored-links)
  2401. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2402. (let ((file (buffer-file-name)))
  2403. (equal (format "[[file:%s::*%s][d]]"
  2404. file
  2405. (org-link-escape "[[#l][d]]"))
  2406. (org-store-link nil))))))
  2407. (should
  2408. (let (org-store-link-props org-stored-links)
  2409. (org-test-with-temp-text-in-file "* [[l]]"
  2410. (let ((file (buffer-file-name)))
  2411. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2412. (org-store-link nil))))))
  2413. (should
  2414. (let (org-store-link-props org-stored-links)
  2415. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2416. (let ((file (buffer-file-name)))
  2417. (equal (format "[[file:%s::*%s][d1 d2]]"
  2418. file
  2419. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2420. (org-store-link nil))))))
  2421. ;; On a named element, link to that element.
  2422. (should
  2423. (let (org-store-link-props org-stored-links)
  2424. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2425. (let ((file (buffer-file-name)))
  2426. (equal (format "[[file:%s::foo][foo]]" file)
  2427. (org-store-link nil))))))
  2428. ;; Store link to Org buffer, with context.
  2429. (should
  2430. (let ((org-stored-links nil)
  2431. (org-id-link-to-org-use-id nil)
  2432. (org-context-in-file-links t))
  2433. (org-test-with-temp-text-in-file "* h1"
  2434. (let ((file (buffer-file-name)))
  2435. (equal (format "[[file:%s::*h1][h1]]" file)
  2436. (org-store-link nil))))))
  2437. ;; Store link to Org buffer, without context.
  2438. (should
  2439. (let ((org-stored-links nil)
  2440. (org-id-link-to-org-use-id nil)
  2441. (org-context-in-file-links nil))
  2442. (org-test-with-temp-text-in-file "* h1"
  2443. (let ((file (buffer-file-name)))
  2444. (equal (format "[[file:%s][file:%s]]" file file)
  2445. (org-store-link nil))))))
  2446. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  2447. (should
  2448. (let ((org-stored-links nil)
  2449. (org-id-link-to-org-use-id nil)
  2450. (org-context-in-file-links nil))
  2451. (org-test-with-temp-text-in-file "* h1"
  2452. (let ((file (buffer-file-name)))
  2453. (equal (format "[[file:%s::*h1][h1]]" file)
  2454. (org-store-link '(4)))))))
  2455. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  2456. ;; buffer.
  2457. (should
  2458. (let ((org-stored-links nil)
  2459. (org-id-link-to-org-use-id nil)
  2460. (org-context-in-file-links nil))
  2461. (org-test-with-temp-text-in-file "* h1"
  2462. (let ((file (buffer-file-name)))
  2463. (equal (format "[[file:%s][file:%s]]" file file)
  2464. (org-store-link '(16)))))))
  2465. ;; Store file link to non-Org buffer, with context.
  2466. (should
  2467. (let ((org-stored-links nil)
  2468. (org-context-in-file-links t))
  2469. (org-test-with-temp-text-in-file "one\n<point>two"
  2470. (fundamental-mode)
  2471. (let ((file (buffer-file-name)))
  2472. (equal (format "[[file:%s::one]]" file)
  2473. (org-store-link nil))))))
  2474. ;; Store file link to non-Org buffer, without context.
  2475. (should
  2476. (let ((org-stored-links nil)
  2477. (org-context-in-file-links nil))
  2478. (org-test-with-temp-text-in-file "one\n<point>two"
  2479. (fundamental-mode)
  2480. (let ((file (buffer-file-name)))
  2481. (equal (format "[[file:%s][file:%s]]" file file)
  2482. (org-store-link nil))))))
  2483. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  2484. ;; buffer.
  2485. (should
  2486. (let ((org-stored-links nil)
  2487. (org-context-in-file-links nil))
  2488. (org-test-with-temp-text-in-file "one\n<point>two"
  2489. (fundamental-mode)
  2490. (let ((file (buffer-file-name)))
  2491. (equal (format "[[file:%s::one]]" file)
  2492. (org-store-link '(4)))))))
  2493. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  2494. ;; non-Org buffer.
  2495. (should
  2496. (let ((org-stored-links nil)
  2497. (org-context-in-file-links nil))
  2498. (org-test-with-temp-text-in-file "one\n<point>two"
  2499. (fundamental-mode)
  2500. (let ((file (buffer-file-name)))
  2501. (equal (format "[[file:%s][file:%s]]" file file)
  2502. (org-store-link '(16))))))))
  2503. ;;; Node Properties
  2504. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2505. "Ensure properties accumulate in subtree drawers."
  2506. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2507. (org-babel-next-src-block)
  2508. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2509. (ert-deftest test-org/custom-properties ()
  2510. "Test custom properties specifications."
  2511. ;; Standard test.
  2512. (should
  2513. (let ((org-custom-properties '("FOO")))
  2514. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2515. (org-toggle-custom-properties-visibility)
  2516. (org-invisible-p2))))
  2517. ;; Properties are case-insensitive.
  2518. (should
  2519. (let ((org-custom-properties '("FOO")))
  2520. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2521. (org-toggle-custom-properties-visibility)
  2522. (org-invisible-p2))))
  2523. (should
  2524. (let ((org-custom-properties '("foo")))
  2525. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2526. (org-toggle-custom-properties-visibility)
  2527. (org-invisible-p2))))
  2528. ;; Multiple custom properties in the same drawer.
  2529. (should
  2530. (let ((org-custom-properties '("FOO" "BAR")))
  2531. (org-test-with-temp-text
  2532. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2533. (org-toggle-custom-properties-visibility)
  2534. (and (org-invisible-p2)
  2535. (not (progn (forward-line) (org-invisible-p2)))
  2536. (progn (forward-line) (org-invisible-p2))))))
  2537. ;; Hide custom properties with an empty value.
  2538. (should
  2539. (let ((org-custom-properties '("FOO")))
  2540. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2541. (org-toggle-custom-properties-visibility)
  2542. (org-invisible-p2))))
  2543. ;; Do not hide fake properties.
  2544. (should-not
  2545. (let ((org-custom-properties '("FOO")))
  2546. (org-test-with-temp-text ":FOO: val\n"
  2547. (org-toggle-custom-properties-visibility)
  2548. (org-invisible-p2))))
  2549. (should-not
  2550. (let ((org-custom-properties '("A")))
  2551. (org-test-with-temp-text
  2552. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2553. (org-toggle-custom-properties-visibility)
  2554. (org-invisible-p2)))))
  2555. ;;; Mark Region
  2556. (ert-deftest test-org/mark-subtree ()
  2557. "Test `org-mark-subtree' specifications."
  2558. ;; Error when point is before first headline.
  2559. (should-error
  2560. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2561. (progn (transient-mark-mode 1)
  2562. (org-mark-subtree))))
  2563. ;; Without argument, mark current subtree.
  2564. (should
  2565. (equal
  2566. '(12 32)
  2567. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2568. (progn (transient-mark-mode 1)
  2569. (forward-line 2)
  2570. (org-mark-subtree)
  2571. (list (region-beginning) (region-end))))))
  2572. ;; With an argument, move ARG up.
  2573. (should
  2574. (equal
  2575. '(1 32)
  2576. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2577. (progn (transient-mark-mode 1)
  2578. (forward-line 2)
  2579. (org-mark-subtree 1)
  2580. (list (region-beginning) (region-end))))))
  2581. ;; Do not get fooled by inlinetasks.
  2582. (when (featurep 'org-inlinetask)
  2583. (should
  2584. (= 1
  2585. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2586. (progn (transient-mark-mode 1)
  2587. (forward-line 1)
  2588. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2589. (region-beginning)))))))
  2590. ;;; Miscellaneous
  2591. (ert-deftest test-org/in-regexp ()
  2592. "Test `org-in-regexp' specifications."
  2593. ;; Standard tests.
  2594. (should
  2595. (org-test-with-temp-text "xx ab<point>c xx"
  2596. (org-in-regexp "abc")))
  2597. (should-not
  2598. (org-test-with-temp-text "xx abc <point>xx"
  2599. (org-in-regexp "abc")))
  2600. ;; Return non-nil even with multiple matching regexps in the same
  2601. ;; line.
  2602. (should
  2603. (org-test-with-temp-text "abc xx ab<point>c xx"
  2604. (org-in-regexp "abc")))
  2605. ;; With optional argument NLINES, check extra lines around point.
  2606. (should-not
  2607. (org-test-with-temp-text "A\nB<point>\nC"
  2608. (org-in-regexp "A\nB\nC")))
  2609. (should
  2610. (org-test-with-temp-text "A\nB<point>\nC"
  2611. (org-in-regexp "A\nB\nC" 1)))
  2612. (should-not
  2613. (org-test-with-temp-text "A\nB\nC<point>"
  2614. (org-in-regexp "A\nB\nC" 1)))
  2615. ;; When optional argument VISUALLY is non-nil, return nil if at
  2616. ;; regexp boundaries.
  2617. (should
  2618. (org-test-with-temp-text "xx abc<point> xx"
  2619. (org-in-regexp "abc")))
  2620. (should-not
  2621. (org-test-with-temp-text "xx abc<point> xx"
  2622. (org-in-regexp "abc" nil t))))
  2623. (ert-deftest test-org/sort-entries ()
  2624. "Test `org-sort-entries'."
  2625. ;; Sort alphabetically.
  2626. (should
  2627. (equal "\n* abc\n* def\n* xyz\n"
  2628. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2629. (org-sort-entries nil ?a)
  2630. (buffer-string))))
  2631. (should
  2632. (equal "\n* xyz\n* def\n* abc\n"
  2633. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2634. (org-sort-entries nil ?A)
  2635. (buffer-string))))
  2636. ;; Sort numerically.
  2637. (should
  2638. (equal "\n* 1\n* 2\n* 10\n"
  2639. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2640. (org-sort-entries nil ?n)
  2641. (buffer-string))))
  2642. (should
  2643. (equal "\n* 10\n* 2\n* 1\n"
  2644. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2645. (org-sort-entries nil ?N)
  2646. (buffer-string))))
  2647. ;; Sort by custom function.
  2648. (should
  2649. (equal "\n* b\n* aa\n* ccc\n"
  2650. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2651. (org-sort-entries nil ?f
  2652. (lambda ()
  2653. (length (buffer-substring (point-at-bol)
  2654. (point-at-eol))))
  2655. #'<)
  2656. (buffer-string))))
  2657. (should
  2658. (equal "\n* ccc\n* aa\n* b\n"
  2659. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2660. (org-sort-entries nil ?F
  2661. (lambda ()
  2662. (length (buffer-substring (point-at-bol)
  2663. (point-at-eol))))
  2664. #'<)
  2665. (buffer-string))))
  2666. ;; Sort by TODO keyword.
  2667. (should
  2668. (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
  2669. (org-test-with-temp-text
  2670. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2671. (org-sort-entries nil ?o)
  2672. (buffer-string))))
  2673. (should
  2674. (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
  2675. (org-test-with-temp-text
  2676. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2677. (org-sort-entries nil ?O)
  2678. (buffer-string))))
  2679. ;; Sort by priority.
  2680. (should
  2681. (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
  2682. (org-test-with-temp-text
  2683. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2684. (org-sort-entries nil ?p)
  2685. (buffer-string))))
  2686. (should
  2687. (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
  2688. (org-test-with-temp-text
  2689. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2690. (org-sort-entries nil ?P)
  2691. (buffer-string))))
  2692. ;; Sort by creation time.
  2693. (should
  2694. (equal "
  2695. * h3
  2696. [2017-05-08 Mon]
  2697. * h2
  2698. [2017-05-09 Tue]
  2699. * h1
  2700. [2018-05-09 Wed]
  2701. "
  2702. (org-test-with-temp-text
  2703. "
  2704. * h1
  2705. [2018-05-09 Wed]
  2706. * h2
  2707. [2017-05-09 Tue]
  2708. * h3
  2709. [2017-05-08 Mon]
  2710. "
  2711. (org-sort-entries nil ?c)
  2712. (buffer-string))))
  2713. ;; Sort by scheduled date.
  2714. (should
  2715. (equal "
  2716. * TODO h4
  2717. SCHEDULED: <2017-05-06 Sat>
  2718. * TODO h3
  2719. SCHEDULED: <2017-05-08 Mon>
  2720. * TODO h2
  2721. DEADLINE: <2017-05-09 Tue>
  2722. * TODO h1
  2723. DEADLINE: <2017-05-07 Sun>
  2724. "
  2725. (org-test-with-temp-text
  2726. "
  2727. * TODO h2
  2728. DEADLINE: <2017-05-09 Tue>
  2729. * TODO h1
  2730. DEADLINE: <2017-05-07 Sun>
  2731. * TODO h3
  2732. SCHEDULED: <2017-05-08 Mon>
  2733. * TODO h4
  2734. SCHEDULED: <2017-05-06 Sat>
  2735. "
  2736. (org-sort-entries nil ?s)
  2737. (buffer-string))))
  2738. ;; Sort by deadline date.
  2739. (should
  2740. (equal "
  2741. * TODO h1
  2742. DEADLINE: <2017-05-07 Sun>
  2743. * TODO h2
  2744. DEADLINE: <2017-05-09 Tue>
  2745. * TODO h3
  2746. SCHEDULED: <2017-05-08 Mon>
  2747. * TODO h4
  2748. SCHEDULED: <2017-05-06 Sat>
  2749. "
  2750. (org-test-with-temp-text
  2751. "
  2752. * TODO h2
  2753. DEADLINE: <2017-05-09 Tue>
  2754. * TODO h1
  2755. DEADLINE: <2017-05-07 Sun>
  2756. * TODO h3
  2757. SCHEDULED: <2017-05-08 Mon>
  2758. * TODO h4
  2759. SCHEDULED: <2017-05-06 Sat>
  2760. "
  2761. (org-sort-entries nil ?d)
  2762. (buffer-string))))
  2763. ;; Sort by any date/time
  2764. (should
  2765. (equal "
  2766. * TODO h4
  2767. SCHEDULED: <2017-05-06 Sat>
  2768. * TODO h1
  2769. DEADLINE: <2017-05-07 Sun>
  2770. * TODO h3
  2771. SCHEDULED: <2017-05-08 Mon>
  2772. * TODO h2
  2773. DEADLINE: <2017-05-09 Tue>
  2774. "
  2775. (org-test-with-temp-text
  2776. "
  2777. * TODO h2
  2778. DEADLINE: <2017-05-09 Tue>
  2779. * TODO h1
  2780. DEADLINE: <2017-05-07 Sun>
  2781. * TODO h3
  2782. SCHEDULED: <2017-05-08 Mon>
  2783. * TODO h4
  2784. SCHEDULED: <2017-05-06 Sat>
  2785. "
  2786. (org-sort-entries nil ?t)
  2787. (buffer-string))))
  2788. ;; Sort by clocking time.
  2789. (should
  2790. (equal "
  2791. * clocked h2
  2792. :LOGBOOK:
  2793. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2794. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2795. :END:
  2796. * clocked h1
  2797. :LOGBOOK:
  2798. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2799. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2800. :END:
  2801. "
  2802. (org-test-with-temp-text
  2803. "
  2804. * clocked h1
  2805. :LOGBOOK:
  2806. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2807. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2808. :END:
  2809. * clocked h2
  2810. :LOGBOOK:
  2811. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2812. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2813. :END:
  2814. "
  2815. (org-sort-entries nil ?k)
  2816. (buffer-string)))))
  2817. ;;; Navigation
  2818. (ert-deftest test-org/forward-heading-same-level ()
  2819. "Test `org-forward-heading-same-level' specifications."
  2820. ;; Test navigation at top level, forward and backward.
  2821. (should
  2822. (equal "* H2"
  2823. (org-test-with-temp-text "* H1\n* H2"
  2824. (org-forward-heading-same-level 1)
  2825. (buffer-substring-no-properties (point) (line-end-position)))))
  2826. (should
  2827. (equal "* H1"
  2828. (org-test-with-temp-text "* H1\n<point>* H2"
  2829. (org-forward-heading-same-level -1)
  2830. (buffer-substring-no-properties (point) (line-end-position)))))
  2831. ;; Test navigation in a sub-tree, forward and backward.
  2832. (should
  2833. (equal "* H2"
  2834. (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
  2835. (org-forward-heading-same-level 1)
  2836. (buffer-substring-no-properties (point) (line-end-position)))))
  2837. (should
  2838. (equal "* H1"
  2839. (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
  2840. (org-forward-heading-same-level -1)
  2841. (buffer-substring-no-properties (point) (line-end-position)))))
  2842. ;; Stop at first or last sub-heading.
  2843. (should-not
  2844. (equal "* H2"
  2845. (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
  2846. (org-forward-heading-same-level 1)
  2847. (buffer-substring-no-properties (point) (line-end-position)))))
  2848. (should-not
  2849. (equal "* H2"
  2850. (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
  2851. (org-forward-heading-same-level -1)
  2852. (buffer-substring-no-properties (point) (line-end-position)))))
  2853. ;; Allow multiple moves.
  2854. (should
  2855. (equal "* H3"
  2856. (org-test-with-temp-text "* H1\n* H2\n* H3"
  2857. (org-forward-heading-same-level 2)
  2858. (buffer-substring-no-properties (point) (line-end-position)))))
  2859. (should
  2860. (equal "* H1"
  2861. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  2862. (org-forward-heading-same-level -2)
  2863. (buffer-substring-no-properties (point) (line-end-position)))))
  2864. ;; Ignore spurious moves when first (or last) sibling is reached.
  2865. (should
  2866. (equal "** H3"
  2867. (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
  2868. (org-forward-heading-same-level 100)
  2869. (buffer-substring-no-properties (point) (line-end-position)))))
  2870. (should
  2871. (equal "** H1"
  2872. (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
  2873. (org-forward-heading-same-level -100)
  2874. (buffer-substring-no-properties (point) (line-end-position))))))
  2875. (ert-deftest test-org/end-of-meta-data ()
  2876. "Test `org-end-of-meta-data' specifications."
  2877. ;; Skip planning line.
  2878. (should
  2879. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  2880. (org-end-of-meta-data)
  2881. (eobp)))
  2882. ;; Skip properties drawer.
  2883. (should
  2884. (org-test-with-temp-text
  2885. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  2886. (org-end-of-meta-data)
  2887. (eobp)))
  2888. ;; Skip both.
  2889. (should
  2890. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  2891. (org-end-of-meta-data)
  2892. (eobp)))
  2893. ;; Nothing to skip, go to first line.
  2894. (should
  2895. (org-test-with-temp-text "* Headline\nContents"
  2896. (org-end-of-meta-data)
  2897. (looking-at "Contents")))
  2898. ;; With option argument, skip empty lines, regular drawers and
  2899. ;; clocking lines.
  2900. (should
  2901. (org-test-with-temp-text "* Headline\n\nContents"
  2902. (org-end-of-meta-data t)
  2903. (looking-at "Contents")))
  2904. (should
  2905. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  2906. (org-end-of-meta-data t)
  2907. (looking-at "Contents")))
  2908. (should
  2909. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  2910. (org-end-of-meta-data t)
  2911. (looking-at "Contents")))
  2912. ;; Special case: do not skip incomplete drawers.
  2913. (should
  2914. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  2915. (org-end-of-meta-data t)
  2916. (looking-at ":LOGBOOK:")))
  2917. ;; Special case: Be careful about consecutive headlines.
  2918. (should-not
  2919. (org-test-with-temp-text "* H1\n*H2\nContents"
  2920. (org-end-of-meta-data t)
  2921. (looking-at "Contents"))))
  2922. (ert-deftest test-org/beginning-of-line ()
  2923. "Test `org-beginning-of-line' specifications."
  2924. ;; Move to beginning of line. If current line in invisible, move to
  2925. ;; beginning of visible line instead.
  2926. (should
  2927. (org-test-with-temp-text "Some text\nSome other text<point>"
  2928. (org-beginning-of-line)
  2929. (bolp)))
  2930. (should
  2931. (org-test-with-temp-text "* H1\n** H2<point>"
  2932. (org-overview)
  2933. (org-beginning-of-line)
  2934. (= (line-beginning-position) 1)))
  2935. ;; With `visual-line-mode' active, move to beginning of visual line.
  2936. (should-not
  2937. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2938. (visual-line-mode)
  2939. (dotimes (i 1000) (insert "very "))
  2940. (org-beginning-of-line)
  2941. (bolp)))
  2942. ;; In a wide headline, with `visual-line-mode', prefer going to the
  2943. ;; beginning of a visual line than to the logical beginning of line,
  2944. ;; even if special movement is active.
  2945. (should-not
  2946. (org-test-with-temp-text "* A <point>long headline"
  2947. (visual-line-mode)
  2948. (dotimes (i 1000) (insert "very "))
  2949. (goto-char (point-max))
  2950. (org-beginning-of-line)
  2951. (bobp)))
  2952. (should-not
  2953. (org-test-with-temp-text "* A <point>long headline"
  2954. (visual-line-mode)
  2955. (dotimes (i 1000) (insert "very "))
  2956. (goto-char (point-max))
  2957. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  2958. (bobp)))
  2959. ;; At an headline with special movement, first move at beginning of
  2960. ;; title, then at the beginning of line, rinse, repeat.
  2961. (should
  2962. (org-test-with-temp-text "* TODO Headline<point>"
  2963. (let ((org-special-ctrl-a/e t))
  2964. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  2965. (progn (org-beginning-of-line) (bolp))
  2966. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2967. (should
  2968. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2969. (let ((org-special-ctrl-a/e t))
  2970. (org-beginning-of-line)
  2971. (looking-at "Headline"))))
  2972. (should
  2973. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2974. (let ((org-special-ctrl-a/e '(t . nil)))
  2975. (org-beginning-of-line)
  2976. (looking-at "Headline"))))
  2977. (should-not
  2978. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2979. (let ((org-special-ctrl-a/e '(nil . nil)))
  2980. (org-beginning-of-line)
  2981. (looking-at "Headline"))))
  2982. ;; At an headline with reversed movement, first move to beginning of
  2983. ;; line, then to the beginning of title.
  2984. (should
  2985. (org-test-with-temp-text "* TODO Headline<point>"
  2986. (let ((org-special-ctrl-a/e 'reversed)
  2987. (this-command last-command))
  2988. (and (progn (org-beginning-of-line) (bolp))
  2989. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2990. (should
  2991. (org-test-with-temp-text "* TODO Headline<point>"
  2992. (let ((org-special-ctrl-a/e '(reversed . nil))
  2993. (this-command last-command))
  2994. (and (progn (org-beginning-of-line) (bolp))
  2995. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2996. (should-not
  2997. (org-test-with-temp-text "* TODO Headline<point>"
  2998. (let ((org-special-ctrl-a/e '(t . nil))
  2999. (this-command last-command))
  3000. (and (progn (org-beginning-of-line) (bolp))
  3001. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3002. ;; At an item with special movement, first move after to beginning
  3003. ;; of title, then to the beginning of line, rinse, repeat.
  3004. (should
  3005. (org-test-with-temp-text "- [ ] Item<point>"
  3006. (let ((org-special-ctrl-a/e t))
  3007. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  3008. (progn (org-beginning-of-line) (bolp))
  3009. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3010. ;; At an item with reversed movement, first move to beginning of
  3011. ;; line, then to the beginning of title.
  3012. (should
  3013. (org-test-with-temp-text "- [X] Item<point>"
  3014. (let ((org-special-ctrl-a/e 'reversed)
  3015. (this-command last-command))
  3016. (and (progn (org-beginning-of-line) (bolp))
  3017. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3018. ;; Leave point before invisible characters at column 0.
  3019. (should
  3020. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  3021. (let ((org-special-ctrl-a/e nil))
  3022. (org-beginning-of-line)
  3023. (bolp))))
  3024. (should
  3025. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  3026. (let ((org-special-ctrl-a/e t))
  3027. (org-beginning-of-line)
  3028. (bolp))))
  3029. (should
  3030. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  3031. (visual-line-mode)
  3032. (org-beginning-of-line)
  3033. (bolp)))
  3034. ;; Special case: Do not error when the buffer contains only a single
  3035. ;; asterisk.
  3036. (should
  3037. (org-test-with-temp-text "*<point>"
  3038. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  3039. (should
  3040. (org-test-with-temp-text "*<point>"
  3041. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  3042. (ert-deftest test-org/end-of-line ()
  3043. "Test `org-end-of-line' specifications."
  3044. ;; Standard test.
  3045. (should
  3046. (org-test-with-temp-text "Some text\nSome other text"
  3047. (org-end-of-line)
  3048. (eolp)))
  3049. ;; With `visual-line-mode' active, move to end of visible line.
  3050. ;; However, never go past ellipsis.
  3051. (should-not
  3052. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3053. (visual-line-mode)
  3054. (dotimes (i 1000) (insert "very "))
  3055. (goto-char (point-min))
  3056. (org-end-of-line)
  3057. (eolp)))
  3058. (should-not
  3059. (org-test-with-temp-text "* A short headline\nSome contents"
  3060. (visual-line-mode)
  3061. (org-overview)
  3062. (org-end-of-line)
  3063. (eobp)))
  3064. ;; In a wide headline, with `visual-line-mode', prefer going to end
  3065. ;; of visible line if tags, or end of line, are farther.
  3066. (should-not
  3067. (org-test-with-temp-text "* A <point>long headline"
  3068. (visual-line-mode)
  3069. (dotimes (i 1000) (insert "very "))
  3070. (goto-char (point-min))
  3071. (org-end-of-line)
  3072. (eolp)))
  3073. (should-not
  3074. (org-test-with-temp-text "* A <point>long headline :tag:"
  3075. (visual-line-mode)
  3076. (dotimes (i 1000) (insert "very "))
  3077. (goto-char (point-min))
  3078. (org-end-of-line)
  3079. (eolp)))
  3080. ;; At an headline without special movement, go to end of line.
  3081. ;; However, never go past ellipsis.
  3082. (should
  3083. (org-test-with-temp-text "* Headline2b :tag:\n"
  3084. (let ((org-special-ctrl-a/e nil))
  3085. (and (progn (org-end-of-line) (eolp))
  3086. (progn (org-end-of-line) (eolp))))))
  3087. (should
  3088. (org-test-with-temp-text "* Headline2b :tag:\n"
  3089. (let ((org-special-ctrl-a/e '(t . nil)))
  3090. (and (progn (org-end-of-line) (eolp))
  3091. (progn (org-end-of-line) (eolp))))))
  3092. (should
  3093. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3094. (org-overview)
  3095. (let ((org-special-ctrl-a/e nil))
  3096. (org-end-of-line)
  3097. (= 1 (line-beginning-position)))))
  3098. ;; At an headline with special movement, first move before tags,
  3099. ;; then at the end of line, rinse, repeat. However, never go past
  3100. ;; ellipsis.
  3101. (should
  3102. (org-test-with-temp-text "* Headline1 :tag:\n"
  3103. (let ((org-special-ctrl-a/e t))
  3104. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3105. (progn (org-end-of-line) (eolp))
  3106. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3107. (should
  3108. (org-test-with-temp-text "* Headline1 :tag:\n"
  3109. (let ((org-special-ctrl-a/e '(nil . t)))
  3110. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3111. (progn (org-end-of-line) (eolp))
  3112. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3113. (should-not
  3114. (org-test-with-temp-text "* Headline1 :tag:\n"
  3115. (let ((org-special-ctrl-a/e '(nil . nil)))
  3116. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3117. (progn (org-end-of-line) (eolp))
  3118. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3119. (should
  3120. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3121. (org-overview)
  3122. (let ((org-special-ctrl-a/e t))
  3123. (org-end-of-line)
  3124. (org-end-of-line)
  3125. (= 1 (line-beginning-position)))))
  3126. ;; At an headline, with reversed movement, first go to end of line,
  3127. ;; then before tags. However, never go past ellipsis.
  3128. (should
  3129. (org-test-with-temp-text "* Headline3 :tag:\n"
  3130. (let ((org-special-ctrl-a/e 'reversed)
  3131. (this-command last-command))
  3132. (and (progn (org-end-of-line) (eolp))
  3133. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3134. (should
  3135. (org-test-with-temp-text "* Headline3 :tag:\n"
  3136. (let ((org-special-ctrl-a/e '(nil . reversed))
  3137. (this-command last-command))
  3138. (and (progn (org-end-of-line) (eolp))
  3139. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3140. (should-not
  3141. (org-test-with-temp-text "* Headline3 :tag:\n"
  3142. (let ((org-special-ctrl-a/e '(nil . t))
  3143. (this-command last-command))
  3144. (and (progn (org-end-of-line) (eolp))
  3145. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3146. (should
  3147. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3148. (org-overview)
  3149. (let ((org-special-ctrl-a/e 'reversed))
  3150. (org-end-of-line)
  3151. (= 1 (line-beginning-position)))))
  3152. ;; At a block without hidden contents.
  3153. (should
  3154. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3155. (progn (org-end-of-line) (eolp))))
  3156. ;; At a block with hidden contents.
  3157. (should-not
  3158. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3159. (let ((org-special-ctrl-a/e t))
  3160. (org-hide-block-toggle)
  3161. (org-end-of-line)
  3162. (eobp))))
  3163. ;; Get past invisible characters at the end of line.
  3164. (should
  3165. (org-test-with-temp-text "[[http://orgmode.org]]"
  3166. (org-end-of-line)
  3167. (eolp))))
  3168. (ert-deftest test-org/open-line ()
  3169. "Test `org-open-line' specifications."
  3170. ;; Call `open-line' outside of tables.
  3171. (should
  3172. (equal "\nText"
  3173. (org-test-with-temp-text "Text"
  3174. (org-open-line 1)
  3175. (buffer-string))))
  3176. ;; At a table, create a row above.
  3177. (should
  3178. (equal "\n| |\n| a |"
  3179. (org-test-with-temp-text "\n<point>| a |"
  3180. (org-open-line 1)
  3181. (buffer-string))))
  3182. ;; At the very first character of the buffer, also call `open-line'.
  3183. (should
  3184. (equal "\n| a |"
  3185. (org-test-with-temp-text "| a |"
  3186. (org-open-line 1)
  3187. (buffer-string))))
  3188. ;; Narrowing does not count.
  3189. (should
  3190. (equal "Text\n| |\n| a |"
  3191. (org-test-with-temp-text "Text\n<point>| a |"
  3192. (narrow-to-region (point) (point-max))
  3193. (org-open-line 1)
  3194. (widen)
  3195. (buffer-string)))))
  3196. (ert-deftest test-org/forward-sentence ()
  3197. "Test `org-forward-sentence' specifications."
  3198. ;; At the end of a table cell, move to the end of the next one.
  3199. (should
  3200. (org-test-with-temp-text "| a<point> | b |"
  3201. (org-forward-sentence)
  3202. (looking-at " |$")))
  3203. ;; Elsewhere in a cell, move to its end.
  3204. (should
  3205. (org-test-with-temp-text "| a<point>c | b |"
  3206. (org-forward-sentence)
  3207. (looking-at " | b |$")))
  3208. ;; Otherwise, simply call `forward-sentence'.
  3209. (should
  3210. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3211. (org-forward-sentence)
  3212. (looking-at " Sentence 2.")))
  3213. (should
  3214. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3215. (org-forward-sentence)
  3216. (org-forward-sentence)
  3217. (eobp)))
  3218. ;; At the end of an element, jump to the next one, without stopping
  3219. ;; on blank lines in-between.
  3220. (should
  3221. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  3222. (org-forward-sentence)
  3223. (eobp))))
  3224. (ert-deftest test-org/backward-sentence ()
  3225. "Test `org-backward-sentence' specifications."
  3226. ;; At the beginning of a table cell, move to the beginning of the
  3227. ;; previous one.
  3228. (should
  3229. (org-test-with-temp-text "| a | <point>b |"
  3230. (org-backward-sentence)
  3231. (looking-at "a | b |$")))
  3232. ;; Elsewhere in a cell, move to its beginning.
  3233. (should
  3234. (org-test-with-temp-text "| a | b<point>c |"
  3235. (org-backward-sentence)
  3236. (looking-at "bc |$")))
  3237. ;; Otherwise, simply call `backward-sentence'.
  3238. (should
  3239. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3240. (org-backward-sentence)
  3241. (looking-at "Sentence 2.")))
  3242. (should
  3243. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3244. (org-backward-sentence)
  3245. (org-backward-sentence)
  3246. (bobp)))
  3247. ;; Make sure to hit the beginning of a sentence on the same line as
  3248. ;; an item.
  3249. (should
  3250. (org-test-with-temp-text "- Line 1\n line <point>2."
  3251. (org-backward-sentence)
  3252. (looking-at "Line 1"))))
  3253. (ert-deftest test-org/forward-paragraph ()
  3254. "Test `org-forward-paragraph' specifications."
  3255. ;; At end of buffer, return an error.
  3256. (should-error
  3257. (org-test-with-temp-text "Paragraph"
  3258. (goto-char (point-max))
  3259. (org-forward-paragraph)))
  3260. ;; Standard test.
  3261. (should
  3262. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3263. (org-forward-paragraph)
  3264. (looking-at "P2")))
  3265. ;; Ignore depth.
  3266. (should
  3267. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  3268. (org-forward-paragraph)
  3269. (looking-at "P1")))
  3270. ;; Do not enter elements with invisible contents.
  3271. (should
  3272. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  3273. (org-hide-block-toggle)
  3274. (org-forward-paragraph)
  3275. (looking-at "P3")))
  3276. ;; On an affiliated keyword, jump to the beginning of the element.
  3277. (should
  3278. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  3279. (org-forward-paragraph)
  3280. (looking-at "Para")))
  3281. ;; On an item or a footnote definition, move to the second element
  3282. ;; inside, if any.
  3283. (should
  3284. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  3285. (org-forward-paragraph)
  3286. (looking-at " Paragraph")))
  3287. (should
  3288. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  3289. (org-forward-paragraph)
  3290. (looking-at "Paragraph")))
  3291. ;; On an item, or a footnote definition, when the first line is
  3292. ;; empty, move to the first item.
  3293. (should
  3294. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  3295. (org-forward-paragraph)
  3296. (looking-at " Paragraph")))
  3297. (should
  3298. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  3299. (org-forward-paragraph)
  3300. (looking-at "Paragraph")))
  3301. ;; On a table (resp. a property drawer) do not move through table
  3302. ;; rows (resp. node properties).
  3303. (should
  3304. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  3305. (org-forward-paragraph)
  3306. (looking-at "Paragraph")))
  3307. (should
  3308. (org-test-with-temp-text
  3309. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  3310. (org-forward-paragraph)
  3311. (looking-at "Paragraph")))
  3312. ;; On a verse or source block, stop after blank lines.
  3313. (should
  3314. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  3315. (org-forward-paragraph)
  3316. (looking-at "L2")))
  3317. (should
  3318. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  3319. (org-forward-paragraph)
  3320. (looking-at "L2"))))
  3321. (ert-deftest test-org/backward-paragraph ()
  3322. "Test `org-backward-paragraph' specifications."
  3323. ;; Error at beginning of buffer.
  3324. (should-error
  3325. (org-test-with-temp-text "Paragraph"
  3326. (org-backward-paragraph)))
  3327. ;; Regular test.
  3328. (should
  3329. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3330. (goto-char (point-max))
  3331. (org-backward-paragraph)
  3332. (looking-at "P3")))
  3333. (should
  3334. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3335. (goto-char (point-max))
  3336. (beginning-of-line)
  3337. (org-backward-paragraph)
  3338. (looking-at "P2")))
  3339. ;; Ignore depth.
  3340. (should
  3341. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  3342. (goto-char (point-max))
  3343. (beginning-of-line)
  3344. (org-backward-paragraph)
  3345. (looking-at "P2")))
  3346. ;; Ignore invisible elements.
  3347. (should
  3348. (org-test-with-temp-text "* H1\n P1\n* H2"
  3349. (org-cycle)
  3350. (goto-char (point-max))
  3351. (beginning-of-line)
  3352. (org-backward-paragraph)
  3353. (bobp)))
  3354. ;; On an affiliated keyword, jump to the first one.
  3355. (should
  3356. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  3357. (search-forward "c2")
  3358. (org-backward-paragraph)
  3359. (looking-at "#\\+name")))
  3360. ;; On the second element in an item or a footnote definition, jump
  3361. ;; to item or the definition.
  3362. (should
  3363. (org-test-with-temp-text "- line1\n\n line2"
  3364. (goto-char (point-max))
  3365. (beginning-of-line)
  3366. (org-backward-paragraph)
  3367. (looking-at "- line1")))
  3368. (should
  3369. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  3370. (goto-char (point-max))
  3371. (beginning-of-line)
  3372. (org-backward-paragraph)
  3373. (looking-at "\\[fn:1\\] line1")))
  3374. ;; On a table (resp. a property drawer), ignore table rows
  3375. ;; (resp. node properties).
  3376. (should
  3377. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  3378. (goto-char (point-max))
  3379. (beginning-of-line)
  3380. (org-backward-paragraph)
  3381. (bobp)))
  3382. (should
  3383. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  3384. (org-backward-paragraph)
  3385. (looking-at ":PROPERTIES:")))
  3386. ;; On a source or verse block, stop before blank lines.
  3387. (should
  3388. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  3389. (search-forward "L3")
  3390. (beginning-of-line)
  3391. (org-backward-paragraph)
  3392. (looking-at "L2")))
  3393. (should
  3394. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  3395. (search-forward "L3")
  3396. (beginning-of-line)
  3397. (org-backward-paragraph)
  3398. (looking-at "L2"))))
  3399. (ert-deftest test-org/forward-element ()
  3400. "Test `org-forward-element' specifications."
  3401. ;; 1. At EOB: should error.
  3402. (org-test-with-temp-text "Some text\n"
  3403. (goto-char (point-max))
  3404. (should-error (org-forward-element)))
  3405. ;; 2. Standard move: expected to ignore blank lines.
  3406. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  3407. (org-forward-element)
  3408. (should (looking-at (regexp-quote "Second paragraph."))))
  3409. ;; 3. Headline tests.
  3410. (org-test-with-temp-text "
  3411. * Head 1
  3412. ** Head 1.1
  3413. *** Head 1.1.1
  3414. ** Head 1.2"
  3415. ;; 3.1. At an headline beginning: move to next headline at the
  3416. ;; same level.
  3417. (goto-line 3)
  3418. (org-forward-element)
  3419. (should (looking-at (regexp-quote "** Head 1.2")))
  3420. ;; 3.2. At an headline beginning: move to parent headline if no
  3421. ;; headline at the same level.
  3422. (goto-line 3)
  3423. (org-forward-element)
  3424. (should (looking-at (regexp-quote "** Head 1.2"))))
  3425. ;; 4. Greater element tests.
  3426. (org-test-with-temp-text
  3427. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  3428. ;; 4.1. At a greater element: expected to skip contents.
  3429. (org-forward-element)
  3430. (should (looking-at (regexp-quote "Outside.")))
  3431. ;; 4.2. At the end of greater element contents: expected to skip
  3432. ;; to the end of the greater element.
  3433. (goto-line 2)
  3434. (org-forward-element)
  3435. (should (looking-at (regexp-quote "Outside."))))
  3436. ;; 5. List tests.
  3437. (org-test-with-temp-text "
  3438. - item1
  3439. - sub1
  3440. - sub2
  3441. - sub3
  3442. Inner paragraph.
  3443. - item2
  3444. Outside."
  3445. ;; 5.1. At list top point: expected to move to the element after
  3446. ;; the list.
  3447. (goto-line 2)
  3448. (org-forward-element)
  3449. (should (looking-at (regexp-quote "Outside.")))
  3450. ;; 5.2. Special case: at the first line of a sub-list, but not at
  3451. ;; beginning of line, move to next item.
  3452. (goto-line 2)
  3453. (forward-char)
  3454. (org-forward-element)
  3455. (should (looking-at "- item2"))
  3456. (goto-line 4)
  3457. (forward-char)
  3458. (org-forward-element)
  3459. (should (looking-at " - sub2"))
  3460. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  3461. (goto-line 4)
  3462. (org-forward-element)
  3463. (should (looking-at (regexp-quote " Inner paragraph.")))
  3464. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  3465. (goto-line 8)
  3466. (org-forward-element)
  3467. (should (looking-at (regexp-quote " Inner paragraph.")))
  3468. ;; 5.5. At an item: expected to move to next item, if any.
  3469. (goto-line 6)
  3470. (org-forward-element)
  3471. (should (looking-at " - sub3"))))
  3472. (ert-deftest test-org/backward-element ()
  3473. "Test `org-backward-element' specifications."
  3474. ;; 1. Should error at BOB.
  3475. (org-test-with-temp-text " \nParagraph."
  3476. (should-error (org-backward-element)))
  3477. ;; 2. Should move at BOB when called on the first element in buffer.
  3478. (should
  3479. (org-test-with-temp-text "\n#+TITLE: test"
  3480. (progn (forward-line)
  3481. (org-backward-element)
  3482. (bobp))))
  3483. ;; 3. Not at the beginning of an element: move at its beginning.
  3484. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3485. (goto-line 3)
  3486. (end-of-line)
  3487. (org-backward-element)
  3488. (should (looking-at (regexp-quote "Paragraph2."))))
  3489. ;; 4. Headline tests.
  3490. (org-test-with-temp-text "
  3491. * Head 1
  3492. ** Head 1.1
  3493. *** Head 1.1.1
  3494. ** Head 1.2"
  3495. ;; 4.1. At an headline beginning: move to previous headline at the
  3496. ;; same level.
  3497. (goto-line 5)
  3498. (org-backward-element)
  3499. (should (looking-at (regexp-quote "** Head 1.1")))
  3500. ;; 4.2. At an headline beginning: move to parent headline if no
  3501. ;; headline at the same level.
  3502. (goto-line 3)
  3503. (org-backward-element)
  3504. (should (looking-at (regexp-quote "* Head 1")))
  3505. ;; 4.3. At the first top-level headline: should error.
  3506. (goto-line 2)
  3507. (should-error (org-backward-element)))
  3508. ;; 5. At beginning of first element inside a greater element:
  3509. ;; expected to move to greater element's beginning.
  3510. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  3511. (goto-line 3)
  3512. (org-backward-element)
  3513. (should (looking-at "#\\+BEGIN_CENTER")))
  3514. ;; 6. At the beginning of the first element in a section: should
  3515. ;; move back to headline, if any.
  3516. (should
  3517. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  3518. (progn (goto-char (point-max))
  3519. (beginning-of-line)
  3520. (org-backward-element)
  3521. (org-at-heading-p))))
  3522. ;; 7. List tests.
  3523. (org-test-with-temp-text "
  3524. - item1
  3525. - sub1
  3526. - sub2
  3527. - sub3
  3528. Inner paragraph.
  3529. - item2
  3530. Outside."
  3531. ;; 7.1. At beginning of sub-list: expected to move to the
  3532. ;; paragraph before it.
  3533. (goto-line 4)
  3534. (org-backward-element)
  3535. (should (looking-at "item1"))
  3536. ;; 7.2. At an item in a list: expected to move at previous item.
  3537. (goto-line 8)
  3538. (org-backward-element)
  3539. (should (looking-at " - sub2"))
  3540. (goto-line 12)
  3541. (org-backward-element)
  3542. (should (looking-at "- item1"))
  3543. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3544. ;; beginning.
  3545. (goto-line 10)
  3546. (org-backward-element)
  3547. (should (looking-at " - sub1"))
  3548. (goto-line 15)
  3549. (org-backward-element)
  3550. (should (looking-at "- item1"))
  3551. ;; 7.4. At blank-lines before list end: expected to move to top
  3552. ;; item.
  3553. (goto-line 14)
  3554. (org-backward-element)
  3555. (should (looking-at "- item1"))))
  3556. (ert-deftest test-org/up-element ()
  3557. "Test `org-up-element' specifications."
  3558. ;; 1. At BOB or with no surrounding element: should error.
  3559. (org-test-with-temp-text "Paragraph."
  3560. (should-error (org-up-element)))
  3561. (org-test-with-temp-text "* Head1\n* Head2"
  3562. (goto-line 2)
  3563. (should-error (org-up-element)))
  3564. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3565. (goto-line 3)
  3566. (should-error (org-up-element)))
  3567. ;; 2. At an headline: move to parent headline.
  3568. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3569. (goto-line 3)
  3570. (org-up-element)
  3571. (should (looking-at "\\* Head1")))
  3572. ;; 3. Inside a greater element: move to greater element beginning.
  3573. (org-test-with-temp-text
  3574. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3575. (goto-line 3)
  3576. (org-up-element)
  3577. (should (looking-at "#\\+BEGIN_CENTER")))
  3578. ;; 4. List tests.
  3579. (org-test-with-temp-text "* Top
  3580. - item1
  3581. - sub1
  3582. - sub2
  3583. Paragraph within sub2.
  3584. - item2"
  3585. ;; 4.1. Within an item: move to the item beginning.
  3586. (goto-line 8)
  3587. (org-up-element)
  3588. (should (looking-at " - sub2"))
  3589. ;; 4.2. At an item in a sub-list: move to parent item.
  3590. (goto-line 4)
  3591. (org-up-element)
  3592. (should (looking-at "- item1"))
  3593. ;; 4.3. At an item in top list: move to beginning of whole list.
  3594. (goto-line 10)
  3595. (org-up-element)
  3596. (should (looking-at "- item1"))
  3597. ;; 4.4. Special case. At very top point: should move to parent of
  3598. ;; list.
  3599. (goto-line 2)
  3600. (org-up-element)
  3601. (should (looking-at "\\* Top"))))
  3602. (ert-deftest test-org/down-element ()
  3603. "Test `org-down-element' specifications."
  3604. ;; Error when the element hasn't got a recursive type.
  3605. (org-test-with-temp-text "Paragraph."
  3606. (should-error (org-down-element)))
  3607. ;; Error when the element has no contents
  3608. (org-test-with-temp-text "* Headline"
  3609. (should-error (org-down-element)))
  3610. ;; When at a plain-list, move to first item.
  3611. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3612. (goto-line 2)
  3613. (org-down-element)
  3614. (should (looking-at " - Item 1.1")))
  3615. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3616. (org-down-element)
  3617. (should (looking-at " Item 1")))
  3618. ;; When at a table, move to first row
  3619. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3620. (org-down-element)
  3621. (should (looking-at " a | b |")))
  3622. ;; Otherwise, move inside the greater element.
  3623. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3624. (org-down-element)
  3625. (should (looking-at "Paragraph"))))
  3626. (ert-deftest test-org/drag-element-backward ()
  3627. "Test `org-drag-element-backward' specifications."
  3628. ;; Standard test.
  3629. (should
  3630. (equal
  3631. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3632. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3633. (org-drag-element-backward)
  3634. (buffer-string))))
  3635. (should
  3636. (equal
  3637. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3638. (org-test-with-temp-text
  3639. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3640. (org-drag-element-backward)
  3641. (buffer-string))))
  3642. ;; Preserve blank lines.
  3643. (should
  3644. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3645. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3646. (org-drag-element-backward)
  3647. (buffer-string))))
  3648. ;; Preserve column.
  3649. (should
  3650. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3651. (org-drag-element-backward)
  3652. (looking-at-p "2")))
  3653. ;; Error when trying to move first element of buffer.
  3654. (should-error
  3655. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3656. (org-drag-element-backward))
  3657. :type 'user-error)
  3658. ;; Error when trying to swap nested elements.
  3659. (should-error
  3660. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  3661. (org-drag-element-backward))
  3662. :type 'user-error)
  3663. ;; Error when trying to swap an headline element and a non-headline
  3664. ;; element.
  3665. (should-error
  3666. (org-test-with-temp-text "Test.\n<point>* Head 1"
  3667. (org-drag-element-backward))
  3668. :type 'error)
  3669. ;; Error when called before first element.
  3670. (should-error
  3671. (org-test-with-temp-text "\n<point>"
  3672. (org-drag-element-backward))
  3673. :type 'user-error)
  3674. ;; Preserve visibility of elements and their contents.
  3675. (should
  3676. (equal '((63 . 82) (26 . 48))
  3677. (org-test-with-temp-text "
  3678. #+BEGIN_CENTER
  3679. Text.
  3680. #+END_CENTER
  3681. - item 1
  3682. #+BEGIN_QUOTE
  3683. Text.
  3684. #+END_QUOTE"
  3685. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3686. (search-backward "- item 1")
  3687. (org-drag-element-backward)
  3688. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3689. (overlays-in (point-min) (point-max))))))
  3690. ;; Pathological case: handle call with point in blank lines right
  3691. ;; after a headline.
  3692. (should
  3693. (equal "* H2\n* H1\nText\n\n"
  3694. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  3695. (org-drag-element-backward)
  3696. (buffer-string)))))
  3697. (ert-deftest test-org/drag-element-forward ()
  3698. "Test `org-drag-element-forward' specifications."
  3699. ;; 1. Error when trying to move first element of buffer.
  3700. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3701. (goto-line 3)
  3702. (should-error (org-drag-element-forward)))
  3703. ;; 2. Error when trying to swap nested elements.
  3704. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3705. (forward-line)
  3706. (should-error (org-drag-element-forward)))
  3707. ;; 3. Error when trying to swap a non-headline element and an
  3708. ;; headline.
  3709. (org-test-with-temp-text "Test.\n* Head 1"
  3710. (should-error (org-drag-element-forward)))
  3711. ;; 4. Error when called before first element.
  3712. (should-error
  3713. (org-test-with-temp-text "\n"
  3714. (forward-line)
  3715. (org-drag-element-backward))
  3716. :type 'user-error)
  3717. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3718. ;; between elements.
  3719. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3720. (search-forward "graph")
  3721. (org-drag-element-forward)
  3722. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3723. (should (looking-at " 1")))
  3724. ;; 5. Preserve visibility of elements and their contents.
  3725. (org-test-with-temp-text "
  3726. #+BEGIN_CENTER
  3727. Text.
  3728. #+END_CENTER
  3729. - item 1
  3730. #+BEGIN_QUOTE
  3731. Text.
  3732. #+END_QUOTE"
  3733. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3734. (search-backward "#+BEGIN_CENTER")
  3735. (org-drag-element-forward)
  3736. (should
  3737. (equal
  3738. '((63 . 82) (26 . 48))
  3739. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3740. (overlays-in (point-min) (point-max)))))))
  3741. (ert-deftest test-org/next-block ()
  3742. "Test `org-next-block' specifications."
  3743. ;; Regular test.
  3744. (should
  3745. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3746. (org-next-block 1)
  3747. (looking-at "#\\+BEGIN_CENTER")))
  3748. ;; Ignore case.
  3749. (should
  3750. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3751. (let ((case-fold-search nil))
  3752. (org-next-block 1)
  3753. (looking-at "#\\+begin_center"))))
  3754. ;; Ignore current line.
  3755. (should
  3756. (org-test-with-temp-text
  3757. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3758. (org-next-block 1)
  3759. (looking-at "#\\+BEGIN_CENTER")))
  3760. ;; Throw an error when no block is found.
  3761. (should-error
  3762. (org-test-with-temp-text "Paragraph"
  3763. (org-next-block 1)))
  3764. ;; With an argument, skip many blocks at once.
  3765. (should
  3766. (org-test-with-temp-text
  3767. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3768. (org-next-block 2)
  3769. (looking-at "#\\+BEGIN_QUOTE")))
  3770. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3771. (should
  3772. (org-test-with-temp-text
  3773. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3774. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3775. (looking-at "#\\+BEGIN_QUOTE")))
  3776. ;; Optional argument is also case-insensitive.
  3777. (should
  3778. (org-test-with-temp-text
  3779. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3780. (let ((case-fold-search nil))
  3781. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3782. (looking-at "#\\+begin_quote")))))
  3783. (ert-deftest test-org/previous-block ()
  3784. "Test `org-previous-block' specifications."
  3785. ;; Regular test.
  3786. (should
  3787. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  3788. (org-previous-block 1)
  3789. (looking-at "#\\+BEGIN_CENTER")))
  3790. ;; Ignore case.
  3791. (should
  3792. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  3793. (let ((case-fold-search nil))
  3794. (org-previous-block 1)
  3795. (looking-at "#\\+begin_center"))))
  3796. ;; Ignore current line.
  3797. (should
  3798. (org-test-with-temp-text
  3799. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  3800. (org-previous-block 1)
  3801. (looking-at "#\\+BEGIN_QUOTE")))
  3802. ;; Throw an error when no block is found.
  3803. (should-error
  3804. (org-test-with-temp-text "Paragraph<point>"
  3805. (org-previous-block 1)))
  3806. ;; With an argument, skip many blocks at once.
  3807. (should
  3808. (org-test-with-temp-text
  3809. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3810. (org-previous-block 2)
  3811. (looking-at "#\\+BEGIN_CENTER")))
  3812. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3813. (should
  3814. (org-test-with-temp-text
  3815. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3816. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3817. (looking-at "#\\+BEGIN_QUOTE")))
  3818. ;; Optional argument is also case-insensitive.
  3819. (should
  3820. (org-test-with-temp-text
  3821. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  3822. (let ((case-fold-search nil))
  3823. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3824. (looking-at "#\\+begin_quote")))))
  3825. ;;; Outline structure
  3826. (ert-deftest test-org/demote ()
  3827. "Test `org-demote' specifications."
  3828. ;; Add correct number of stars according to `org-odd-levels-only'.
  3829. (should
  3830. (= 2
  3831. (org-test-with-temp-text "* H"
  3832. (let ((org-odd-levels-only nil)) (org-demote))
  3833. (org-current-level))))
  3834. (should
  3835. (= 3
  3836. (org-test-with-temp-text "* H"
  3837. (let ((org-odd-levels-only t)) (org-demote))
  3838. (org-current-level))))
  3839. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3840. (should
  3841. (org-test-with-temp-text "* H :tag:"
  3842. (let ((org-tags-column 10)
  3843. (org-auto-align-tags t)
  3844. (org-odd-levels-only nil))
  3845. (org-demote))
  3846. (org-move-to-column 10)
  3847. (looking-at-p ":tag:$")))
  3848. (should-not
  3849. (org-test-with-temp-text "* H :tag:"
  3850. (let ((org-tags-column 10)
  3851. (org-auto-align-tags nil)
  3852. (org-odd-levels-only nil))
  3853. (org-demote))
  3854. (org-move-to-column 10)
  3855. (looking-at-p ":tag:$")))
  3856. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3857. ;; info and property drawers accordingly.
  3858. (should
  3859. (= 3
  3860. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3861. (let ((org-odd-levels-only nil)
  3862. (org-adapt-indentation t))
  3863. (org-demote))
  3864. (forward-line)
  3865. (org-get-indentation))))
  3866. (should
  3867. (= 3
  3868. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3869. (let ((org-odd-levels-only nil)
  3870. (org-adapt-indentation t))
  3871. (org-demote))
  3872. (forward-line)
  3873. (org-get-indentation))))
  3874. (should-not
  3875. (= 3
  3876. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3877. (let ((org-odd-levels-only nil)
  3878. (org-adapt-indentation nil))
  3879. (org-demote))
  3880. (forward-line)
  3881. (org-get-indentation))))
  3882. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3883. ;; section accordingly. Ignore, however, footnote definitions and
  3884. ;; inlinetasks boundaries.
  3885. (should
  3886. (= 3
  3887. (org-test-with-temp-text "* H\n Paragraph"
  3888. (let ((org-odd-levels-only nil)
  3889. (org-adapt-indentation t))
  3890. (org-demote))
  3891. (forward-line)
  3892. (org-get-indentation))))
  3893. (should
  3894. (= 2
  3895. (org-test-with-temp-text "* H\n Paragraph"
  3896. (let ((org-odd-levels-only nil)
  3897. (org-adapt-indentation nil))
  3898. (org-demote))
  3899. (forward-line)
  3900. (org-get-indentation))))
  3901. (should
  3902. (zerop
  3903. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  3904. (let ((org-odd-levels-only nil)
  3905. (org-adapt-indentation t))
  3906. (org-demote))
  3907. (goto-char (point-max))
  3908. (org-get-indentation))))
  3909. (should
  3910. (= 3
  3911. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  3912. (let ((org-odd-levels-only nil)
  3913. (org-adapt-indentation t))
  3914. (org-demote))
  3915. (goto-char (point-max))
  3916. (org-get-indentation))))
  3917. (when (featurep 'org-inlinetask)
  3918. (should
  3919. (zerop
  3920. (let ((org-inlinetask-min-level 5)
  3921. (org-adapt-indentation t))
  3922. (org-test-with-temp-text "* H\n***** I\n***** END"
  3923. (org-demote)
  3924. (forward-line)
  3925. (org-get-indentation))))))
  3926. (when (featurep 'org-inlinetask)
  3927. (should
  3928. (= 3
  3929. (let ((org-inlinetask-min-level 5)
  3930. (org-adapt-indentation t))
  3931. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  3932. (org-demote)
  3933. (forward-line 2)
  3934. (org-get-indentation))))))
  3935. ;; Ignore contents of source blocks or example blocks when
  3936. ;; indentation should be preserved (through
  3937. ;; `org-src-preserve-indentation' or "-i" flag).
  3938. (should-not
  3939. (zerop
  3940. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3941. (let ((org-adapt-indentation t)
  3942. (org-src-preserve-indentation nil))
  3943. (org-demote))
  3944. (forward-line 2)
  3945. (org-get-indentation))))
  3946. (should
  3947. (zerop
  3948. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3949. (let ((org-adapt-indentation t)
  3950. (org-src-preserve-indentation t))
  3951. (org-demote))
  3952. (forward-line 2)
  3953. (org-get-indentation))))
  3954. (should
  3955. (zerop
  3956. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3957. (let ((org-adapt-indentation t)
  3958. (org-src-preserve-indentation t))
  3959. (org-demote))
  3960. (forward-line 2)
  3961. (org-get-indentation))))
  3962. (should
  3963. (zerop
  3964. (org-test-with-temp-text
  3965. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  3966. (let ((org-adapt-indentation t)
  3967. (org-src-preserve-indentation nil))
  3968. (org-demote))
  3969. (forward-line 2)
  3970. (org-get-indentation)))))
  3971. (ert-deftest test-org/promote ()
  3972. "Test `org-promote' specifications."
  3973. ;; Return an error if headline is to be promoted to level 0, unless
  3974. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  3975. ;; headline becomes a comment.
  3976. (should-error
  3977. (org-test-with-temp-text "* H"
  3978. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  3979. (should
  3980. (equal "# H"
  3981. (org-test-with-temp-text "* H"
  3982. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  3983. (buffer-string))))
  3984. ;; Remove correct number of stars according to
  3985. ;; `org-odd-levels-only'.
  3986. (should
  3987. (= 2
  3988. (org-test-with-temp-text "*** H"
  3989. (let ((org-odd-levels-only nil)) (org-promote))
  3990. (org-current-level))))
  3991. (should
  3992. (= 1
  3993. (org-test-with-temp-text "*** H"
  3994. (let ((org-odd-levels-only t)) (org-promote))
  3995. (org-current-level))))
  3996. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3997. (should
  3998. (org-test-with-temp-text "** H :tag:"
  3999. (let ((org-tags-column 10)
  4000. (org-auto-align-tags t)
  4001. (org-odd-levels-only nil))
  4002. (org-promote))
  4003. (org-move-to-column 10)
  4004. (looking-at-p ":tag:$")))
  4005. (should-not
  4006. (org-test-with-temp-text "** H :tag:"
  4007. (let ((org-tags-column 10)
  4008. (org-auto-align-tags nil)
  4009. (org-odd-levels-only nil))
  4010. (org-promote))
  4011. (org-move-to-column 10)
  4012. (looking-at-p ":tag:$")))
  4013. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4014. ;; info and property drawers.
  4015. (should
  4016. (= 2
  4017. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4018. (let ((org-odd-levels-only nil)
  4019. (org-adapt-indentation t))
  4020. (org-promote))
  4021. (forward-line)
  4022. (org-get-indentation))))
  4023. (should
  4024. (= 2
  4025. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4026. (let ((org-odd-levels-only nil)
  4027. (org-adapt-indentation t))
  4028. (org-promote))
  4029. (forward-line)
  4030. (org-get-indentation))))
  4031. (should-not
  4032. (= 2
  4033. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4034. (let ((org-odd-levels-only nil)
  4035. (org-adapt-indentation nil))
  4036. (org-promote))
  4037. (forward-line)
  4038. (org-get-indentation))))
  4039. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4040. ;; section accordingly. Ignore, however, footnote definitions and
  4041. ;; inlinetasks boundaries.
  4042. (should
  4043. (= 2
  4044. (org-test-with-temp-text "** H\n Paragraph"
  4045. (let ((org-odd-levels-only nil)
  4046. (org-adapt-indentation t))
  4047. (org-promote))
  4048. (forward-line)
  4049. (org-get-indentation))))
  4050. (should-not
  4051. (= 2
  4052. (org-test-with-temp-text "** H\n Paragraph"
  4053. (let ((org-odd-levels-only nil)
  4054. (org-adapt-indentation nil))
  4055. (org-promote))
  4056. (forward-line)
  4057. (org-get-indentation))))
  4058. (should
  4059. (= 2
  4060. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  4061. (let ((org-odd-levels-only nil)
  4062. (org-adapt-indentation t))
  4063. (org-promote))
  4064. (forward-line)
  4065. (org-get-indentation))))
  4066. (when (featurep 'org-inlinetask)
  4067. (should
  4068. (zerop
  4069. (let ((org-inlinetask-min-level 5)
  4070. (org-adapt-indentation t))
  4071. (org-test-with-temp-text "** H\n***** I\n***** END"
  4072. (org-promote)
  4073. (forward-line)
  4074. (org-get-indentation))))))
  4075. (when (featurep 'org-inlinetask)
  4076. (should
  4077. (= 2
  4078. (let ((org-inlinetask-min-level 5)
  4079. (org-adapt-indentation t))
  4080. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  4081. (org-promote)
  4082. (forward-line 2)
  4083. (org-get-indentation))))))
  4084. ;; Give up shifting if it would break document's structure
  4085. ;; otherwise.
  4086. (should
  4087. (= 3
  4088. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  4089. (let ((org-odd-levels-only nil)
  4090. (org-adapt-indentation t))
  4091. (org-promote))
  4092. (forward-line)
  4093. (org-get-indentation))))
  4094. (should
  4095. (= 3
  4096. (org-test-with-temp-text "** H\n Paragraph\n * list."
  4097. (let ((org-odd-levels-only nil)
  4098. (org-adapt-indentation t))
  4099. (org-promote))
  4100. (forward-line)
  4101. (org-get-indentation))))
  4102. ;; Ignore contents of source blocks or example blocks when
  4103. ;; indentation should be preserved (through
  4104. ;; `org-src-preserve-indentation' or "-i" flag).
  4105. (should-not
  4106. (zerop
  4107. (org-test-with-temp-text
  4108. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4109. (let ((org-adapt-indentation t)
  4110. (org-src-preserve-indentation nil)
  4111. (org-odd-levels-only nil))
  4112. (org-promote))
  4113. (forward-line)
  4114. (org-get-indentation))))
  4115. (should
  4116. (zerop
  4117. (org-test-with-temp-text
  4118. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  4119. (let ((org-adapt-indentation t)
  4120. (org-src-preserve-indentation t)
  4121. (org-odd-levels-only nil))
  4122. (org-promote))
  4123. (forward-line)
  4124. (org-get-indentation))))
  4125. (should
  4126. (zerop
  4127. (org-test-with-temp-text
  4128. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4129. (let ((org-adapt-indentation t)
  4130. (org-src-preserve-indentation t)
  4131. (org-odd-levels-only nil))
  4132. (org-promote))
  4133. (forward-line)
  4134. (org-get-indentation))))
  4135. (should
  4136. (zerop
  4137. (org-test-with-temp-text
  4138. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  4139. (let ((org-adapt-indentation t)
  4140. (org-src-preserve-indentation nil)
  4141. (org-odd-levels-only nil))
  4142. (org-promote))
  4143. (forward-line)
  4144. (org-get-indentation)))))
  4145. (ert-deftest test-org/org-get-valid-level ()
  4146. "Test function `org-get-valid-level' specifications."
  4147. (let ((org-odd-levels-only nil))
  4148. (should (equal 1 (org-get-valid-level 0 0)))
  4149. (should (equal 1 (org-get-valid-level 0 1)))
  4150. (should (equal 2 (org-get-valid-level 0 2)))
  4151. (should (equal 3 (org-get-valid-level 0 3)))
  4152. (should (equal 1 (org-get-valid-level 1 0)))
  4153. (should (equal 2 (org-get-valid-level 1 1)))
  4154. (should (equal 23 (org-get-valid-level 1 22)))
  4155. (should (equal 1 (org-get-valid-level 1 -1)))
  4156. (should (equal 1 (org-get-valid-level 2 -1))))
  4157. (let ((org-odd-levels-only t))
  4158. (should (equal 1 (org-get-valid-level 0 0)))
  4159. (should (equal 1 (org-get-valid-level 0 1)))
  4160. (should (equal 3 (org-get-valid-level 0 2)))
  4161. (should (equal 5 (org-get-valid-level 0 3)))
  4162. (should (equal 1 (org-get-valid-level 1 0)))
  4163. (should (equal 3 (org-get-valid-level 1 1)))
  4164. (should (equal 3 (org-get-valid-level 2 1)))
  4165. (should (equal 5 (org-get-valid-level 3 1)))
  4166. (should (equal 5 (org-get-valid-level 4 1)))
  4167. (should (equal 43 (org-get-valid-level 1 21)))
  4168. (should (equal 1 (org-get-valid-level 1 -1)))
  4169. (should (equal 1 (org-get-valid-level 2 -1)))
  4170. (should (equal 1 (org-get-valid-level 3 -1)))
  4171. (should (equal 3 (org-get-valid-level 4 -1)))
  4172. (should (equal 3 (org-get-valid-level 5 -1)))))
  4173. ;;; Planning
  4174. (ert-deftest test-org/at-planning-p ()
  4175. "Test `org-at-planning-p' specifications."
  4176. ;; Regular test.
  4177. (should
  4178. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  4179. (org-at-planning-p)))
  4180. (should-not
  4181. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  4182. (org-at-planning-p)))
  4183. ;; Correctly find planning attached to inlinetasks.
  4184. (when (featurep 'org-inlinetask)
  4185. (should
  4186. (org-test-with-temp-text
  4187. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  4188. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4189. (should-not
  4190. (org-test-with-temp-text
  4191. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4192. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4193. (should-not
  4194. (org-test-with-temp-text
  4195. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4196. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4197. (should-not
  4198. (org-test-with-temp-text
  4199. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  4200. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  4201. (ert-deftest test-org/add-planning-info ()
  4202. "Test `org-add-planning-info'."
  4203. ;; Create deadline when `org-adapt-indentation' is non-nil.
  4204. (should
  4205. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4206. (org-test-with-temp-text "* H\nParagraph<point>"
  4207. (let ((org-adapt-indentation t))
  4208. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4209. (replace-regexp-in-string
  4210. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4211. nil nil 1))))
  4212. ;; Create deadline when `org-adapt-indentation' is nil.
  4213. (should
  4214. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4215. (org-test-with-temp-text "* H\nParagraph<point>"
  4216. (let ((org-adapt-indentation nil))
  4217. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4218. (replace-regexp-in-string
  4219. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4220. nil nil 1))))
  4221. ;; Update deadline when `org-adapt-indentation' is non-nil.
  4222. (should
  4223. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4224. (org-test-with-temp-text "\
  4225. * H
  4226. DEADLINE: <2015-06-24 Wed>
  4227. Paragraph<point>"
  4228. (let ((org-adapt-indentation t))
  4229. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4230. (replace-regexp-in-string
  4231. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4232. nil nil 1))))
  4233. ;; Update deadline when `org-adapt-indentation' is nil.
  4234. (should
  4235. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4236. (org-test-with-temp-text "\
  4237. * H
  4238. DEADLINE: <2015-06-24 Wed>
  4239. Paragraph<point>"
  4240. (let ((org-adapt-indentation nil))
  4241. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4242. (replace-regexp-in-string
  4243. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4244. nil nil 1))))
  4245. ;; Schedule when `org-adapt-indentation' is non-nil.
  4246. (should
  4247. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  4248. (org-test-with-temp-text "* H\nParagraph<point>"
  4249. (let ((org-adapt-indentation t))
  4250. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4251. (replace-regexp-in-string
  4252. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4253. nil nil 1))))
  4254. ;; Schedule when `org-adapt-indentation' is nil.
  4255. (should
  4256. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  4257. (org-test-with-temp-text "* H\nParagraph<point>"
  4258. (let ((org-adapt-indentation nil))
  4259. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4260. (replace-regexp-in-string
  4261. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4262. nil nil 1))))
  4263. ;; Add deadline when scheduled.
  4264. (should
  4265. (equal "\
  4266. * H
  4267. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  4268. Paragraph"
  4269. (org-test-with-temp-text "\
  4270. * H
  4271. SCHEDULED: <2015-06-24 Wed>
  4272. Paragraph<point>"
  4273. (let ((org-adapt-indentation t))
  4274. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4275. (replace-regexp-in-string
  4276. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4277. nil nil 1))))
  4278. ;; Remove middle entry.
  4279. (should
  4280. (equal "\
  4281. * H
  4282. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  4283. Paragraph"
  4284. (org-test-with-temp-text "\
  4285. * H
  4286. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4287. Paragraph<point>"
  4288. (let ((org-adapt-indentation t))
  4289. (org-add-planning-info nil nil 'deadline))
  4290. (replace-regexp-in-string
  4291. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4292. nil nil 1))))
  4293. ;; Remove last entry and then middle entry (order should not
  4294. ;; matter).
  4295. (should
  4296. (equal "\
  4297. * H
  4298. CLOSED: [2015-06-24]
  4299. Paragraph"
  4300. (org-test-with-temp-text "\
  4301. * H
  4302. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4303. Paragraph<point>"
  4304. (let ((org-adapt-indentation t))
  4305. (org-add-planning-info nil nil 'scheduled 'deadline))
  4306. (replace-regexp-in-string
  4307. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4308. nil nil 1))))
  4309. ;; Remove closed when `org-adapt-indentation' is non-nil.
  4310. (should
  4311. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4312. (org-test-with-temp-text "\
  4313. * H
  4314. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4315. Paragraph<point>"
  4316. (let ((org-adapt-indentation t))
  4317. (org-add-planning-info nil nil 'closed))
  4318. (replace-regexp-in-string
  4319. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4320. nil nil 1))))
  4321. (should
  4322. (equal "* H\n Paragraph"
  4323. (org-test-with-temp-text "\
  4324. * H
  4325. CLOSED: [2015-06-25 Thu]
  4326. Paragraph<point>"
  4327. (let ((org-adapt-indentation t))
  4328. (org-add-planning-info nil nil 'closed))
  4329. (replace-regexp-in-string
  4330. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4331. nil nil 1))))
  4332. ;; Remove closed when `org-adapt-indentation' is nil.
  4333. (should
  4334. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4335. (org-test-with-temp-text "\
  4336. * H
  4337. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4338. Paragraph<point>"
  4339. (let ((org-adapt-indentation nil))
  4340. (org-add-planning-info nil nil 'closed))
  4341. (replace-regexp-in-string
  4342. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4343. nil nil 1))))
  4344. (should
  4345. (equal "* H\nParagraph"
  4346. (org-test-with-temp-text "\
  4347. * H
  4348. CLOSED: [2015-06-25 Thu]
  4349. Paragraph<point>"
  4350. (let ((org-adapt-indentation nil))
  4351. (org-add-planning-info nil nil 'closed))
  4352. (replace-regexp-in-string
  4353. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4354. nil nil 1))))
  4355. ;; Remove closed entry and delete empty line.
  4356. (should
  4357. (equal "\
  4358. * H
  4359. Paragraph"
  4360. (org-test-with-temp-text "\
  4361. * H
  4362. CLOSED: [2015-06-24 Wed]
  4363. Paragraph<point>"
  4364. (let ((org-adapt-indentation t))
  4365. (org-add-planning-info nil nil 'closed))
  4366. (replace-regexp-in-string
  4367. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4368. nil nil 1))))
  4369. ;; Remove one entry and update another.
  4370. (should
  4371. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4372. (org-test-with-temp-text "\
  4373. * H
  4374. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  4375. Paragraph<point>"
  4376. (let ((org-adapt-indentation t))
  4377. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  4378. (replace-regexp-in-string
  4379. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4380. nil nil 1)))))
  4381. (ert-deftest test-org/deadline ()
  4382. "Test `org-deadline' specifications."
  4383. ;; Insert a new value or replace existing one.
  4384. (should
  4385. (equal "* H\nDEADLINE: <2012-03-29>\n"
  4386. (org-test-with-temp-text "* H"
  4387. (let ((org-adapt-indentation nil)
  4388. (org-last-inserted-timestamp nil))
  4389. (org-deadline nil "<2012-03-29 Tue>"))
  4390. (replace-regexp-in-string
  4391. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4392. nil nil 1))))
  4393. (should
  4394. (equal "* H\nDEADLINE: <2014-03-04>"
  4395. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4396. (let ((org-adapt-indentation nil)
  4397. (org-last-inserted-timestamp nil))
  4398. (org-deadline nil "<2014-03-04 Thu>"))
  4399. (replace-regexp-in-string
  4400. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4401. nil nil 1))))
  4402. ;; Accept delta time, e.g., "+2d".
  4403. (should
  4404. (equal "* H\nDEADLINE: <2015-03-04>\n"
  4405. (cl-letf (((symbol-function 'current-time)
  4406. (lambda (&rest args)
  4407. (apply #'encode-time
  4408. (org-parse-time-string "2014-03-04")))))
  4409. (org-test-with-temp-text "* H"
  4410. (let ((org-adapt-indentation nil)
  4411. (org-last-inserted-timestamp nil))
  4412. (org-deadline nil "+1y"))
  4413. (replace-regexp-in-string
  4414. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4415. ;; Preserve repeater.
  4416. (should
  4417. (equal "* H\nDEADLINE: <2012-03-29 +2y>\n"
  4418. (org-test-with-temp-text "* H"
  4419. (let ((org-adapt-indentation nil)
  4420. (org-last-inserted-timestamp nil))
  4421. (org-deadline nil "<2012-03-29 Tue +2y>"))
  4422. (replace-regexp-in-string
  4423. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4424. ;; Remove CLOSED keyword, if any.
  4425. (should
  4426. (equal "* H\nDEADLINE: <2012-03-29>"
  4427. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4428. (let ((org-adapt-indentation nil)
  4429. (org-last-inserted-timestamp nil))
  4430. (org-deadline nil "<2012-03-29 Tue>"))
  4431. (replace-regexp-in-string
  4432. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4433. ;; With C-u argument, remove DEADLINE keyword.
  4434. (should
  4435. (equal "* H\n"
  4436. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4437. (let ((org-adapt-indentation nil)
  4438. (org-last-inserted-timestamp nil))
  4439. (org-deadline '(4)))
  4440. (buffer-string))))
  4441. (should
  4442. (equal "* H"
  4443. (org-test-with-temp-text "* H"
  4444. (let ((org-adapt-indentation nil)
  4445. (org-last-inserted-timestamp nil))
  4446. (org-deadline '(4)))
  4447. (buffer-string))))
  4448. ;; With C-u C-u argument, prompt for a delay cookie.
  4449. (should
  4450. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  4451. (cl-letf (((symbol-function 'org-read-date)
  4452. (lambda (&rest args)
  4453. (apply #'encode-time
  4454. (org-parse-time-string "2014-03-04")))))
  4455. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4456. (let ((org-adapt-indentation nil)
  4457. (org-last-inserted-timestamp nil))
  4458. (org-deadline '(16)))
  4459. (buffer-string)))))
  4460. (should-error
  4461. (cl-letf (((symbol-function 'org-read-date)
  4462. (lambda (&rest args)
  4463. (apply #'encode-time
  4464. (org-parse-time-string "2014-03-04")))))
  4465. (org-test-with-temp-text "* H"
  4466. (let ((org-adapt-indentation nil)
  4467. (org-last-inserted-timestamp nil))
  4468. (org-deadline '(16)))
  4469. (buffer-string))))
  4470. ;; When a region is active and
  4471. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4472. ;; same value in all headlines in region.
  4473. (should
  4474. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4475. (org-test-with-temp-text "* H1\n* H2"
  4476. (let ((org-adapt-indentation nil)
  4477. (org-last-inserted-timestamp nil)
  4478. (org-loop-over-headlines-in-active-region t))
  4479. (transient-mark-mode 1)
  4480. (push-mark (point) t t)
  4481. (goto-char (point-max))
  4482. (org-deadline nil "2012-03-29"))
  4483. (replace-regexp-in-string
  4484. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4485. (should-not
  4486. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4487. (org-test-with-temp-text "* H1\n* H2"
  4488. (let ((org-adapt-indentation nil)
  4489. (org-last-inserted-timestamp nil)
  4490. (org-loop-over-headlines-in-active-region nil))
  4491. (transient-mark-mode 1)
  4492. (push-mark (point) t t)
  4493. (goto-char (point-max))
  4494. (org-deadline nil "2012-03-29"))
  4495. (replace-regexp-in-string
  4496. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4497. (ert-deftest test-org/schedule ()
  4498. "Test `org-schedule' specifications."
  4499. ;; Insert a new value or replace existing one.
  4500. (should
  4501. (equal "* H\nSCHEDULED: <2012-03-29>\n"
  4502. (org-test-with-temp-text "* H"
  4503. (let ((org-adapt-indentation nil)
  4504. (org-last-inserted-timestamp nil))
  4505. (org-schedule nil "<2012-03-29 Tue>"))
  4506. (replace-regexp-in-string
  4507. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4508. nil nil 1))))
  4509. (should
  4510. (equal "* H\nSCHEDULED: <2014-03-04>"
  4511. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4512. (let ((org-adapt-indentation nil)
  4513. (org-last-inserted-timestamp nil))
  4514. (org-schedule nil "<2014-03-04 Thu>"))
  4515. (replace-regexp-in-string
  4516. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4517. nil nil 1))))
  4518. ;; Accept delta time, e.g., "+2d".
  4519. (should
  4520. (equal "* H\nSCHEDULED: <2015-03-04>\n"
  4521. (cl-letf (((symbol-function 'current-time)
  4522. (lambda (&rest args)
  4523. (apply #'encode-time
  4524. (org-parse-time-string "2014-03-04")))))
  4525. (org-test-with-temp-text "* H"
  4526. (let ((org-adapt-indentation nil)
  4527. (org-last-inserted-timestamp nil))
  4528. (org-schedule nil "+1y"))
  4529. (replace-regexp-in-string
  4530. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4531. ;; Preserve repeater.
  4532. (should
  4533. (equal "* H\nSCHEDULED: <2012-03-29 +2y>\n"
  4534. (org-test-with-temp-text "* H"
  4535. (let ((org-adapt-indentation nil)
  4536. (org-last-inserted-timestamp nil))
  4537. (org-schedule nil "<2012-03-29 Tue +2y>"))
  4538. (replace-regexp-in-string
  4539. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4540. ;; Remove CLOSED keyword, if any.
  4541. (should
  4542. (equal "* H\nSCHEDULED: <2012-03-29>"
  4543. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4544. (let ((org-adapt-indentation nil)
  4545. (org-last-inserted-timestamp nil))
  4546. (org-schedule nil "<2012-03-29 Tue>"))
  4547. (replace-regexp-in-string
  4548. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4549. ;; With C-u argument, remove SCHEDULED keyword.
  4550. (should
  4551. (equal "* H\n"
  4552. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4553. (let ((org-adapt-indentation nil)
  4554. (org-last-inserted-timestamp nil))
  4555. (org-schedule '(4)))
  4556. (buffer-string))))
  4557. (should
  4558. (equal "* H"
  4559. (org-test-with-temp-text "* H"
  4560. (let ((org-adapt-indentation nil)
  4561. (org-last-inserted-timestamp nil))
  4562. (org-schedule '(4)))
  4563. (buffer-string))))
  4564. ;; With C-u C-u argument, prompt for a delay cookie.
  4565. (should
  4566. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  4567. (cl-letf (((symbol-function 'org-read-date)
  4568. (lambda (&rest args)
  4569. (apply #'encode-time
  4570. (org-parse-time-string "2014-03-04")))))
  4571. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4572. (let ((org-adapt-indentation nil)
  4573. (org-last-inserted-timestamp nil))
  4574. (org-schedule '(16)))
  4575. (buffer-string)))))
  4576. (should-error
  4577. (cl-letf (((symbol-function 'org-read-date)
  4578. (lambda (&rest args)
  4579. (apply #'encode-time
  4580. (org-parse-time-string "2014-03-04")))))
  4581. (org-test-with-temp-text "* H"
  4582. (let ((org-adapt-indentation nil)
  4583. (org-last-inserted-timestamp nil))
  4584. (org-schedule '(16)))
  4585. (buffer-string))))
  4586. ;; When a region is active and
  4587. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4588. ;; same value in all headlines in region.
  4589. (should
  4590. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4591. (org-test-with-temp-text "* H1\n* H2"
  4592. (let ((org-adapt-indentation nil)
  4593. (org-last-inserted-timestamp nil)
  4594. (org-loop-over-headlines-in-active-region t))
  4595. (transient-mark-mode 1)
  4596. (push-mark (point) t t)
  4597. (goto-char (point-max))
  4598. (org-schedule nil "2012-03-29"))
  4599. (replace-regexp-in-string
  4600. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4601. (should-not
  4602. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4603. (org-test-with-temp-text "* H1\n* H2"
  4604. (let ((org-adapt-indentation nil)
  4605. (org-last-inserted-timestamp nil)
  4606. (org-loop-over-headlines-in-active-region nil))
  4607. (transient-mark-mode 1)
  4608. (push-mark (point) t t)
  4609. (goto-char (point-max))
  4610. (org-schedule nil "2012-03-29"))
  4611. (replace-regexp-in-string
  4612. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4613. (should
  4614. ;; check if a repeater survives re-scheduling.
  4615. (string-match-p
  4616. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  4617. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  4618. (let ((org-adapt-indentation nil)
  4619. (org-last-inserted-timestamp nil))
  4620. (org-schedule nil "2017-02-01"))
  4621. (buffer-string)))))
  4622. ;;; Property API
  4623. (ert-deftest test-org/buffer-property-keys ()
  4624. "Test `org-buffer-property-keys' specifications."
  4625. ;; Retrieve properties accross siblings.
  4626. (should
  4627. (equal '("A" "B")
  4628. (org-test-with-temp-text "
  4629. * H1
  4630. :PROPERTIES:
  4631. :A: 1
  4632. :END:
  4633. * H2
  4634. :PROPERTIES:
  4635. :B: 1
  4636. :END:"
  4637. (org-buffer-property-keys))))
  4638. ;; Retrieve properties accross children.
  4639. (should
  4640. (equal '("A" "B")
  4641. (org-test-with-temp-text "
  4642. * H1
  4643. :PROPERTIES:
  4644. :A: 1
  4645. :END:
  4646. ** H2
  4647. :PROPERTIES:
  4648. :B: 1
  4649. :END:"
  4650. (org-buffer-property-keys))))
  4651. ;; Retrieve muliple properties in the same drawer.
  4652. (should
  4653. (equal '("A" "B")
  4654. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4655. (org-buffer-property-keys))))
  4656. ;; Ignore extension symbol in property name.
  4657. (should
  4658. (equal '("A")
  4659. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4660. (org-buffer-property-keys))))
  4661. ;; With non-nil COLUMNS, extract property names from columns.
  4662. (should
  4663. (equal '("A" "B")
  4664. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  4665. (org-buffer-property-keys nil nil t))))
  4666. (should
  4667. (equal '("A" "B" "COLUMNS")
  4668. (org-test-with-temp-text
  4669. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  4670. (org-buffer-property-keys nil nil t))))
  4671. ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
  4672. (should
  4673. (equal '("A")
  4674. (org-test-with-temp-text
  4675. "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
  4676. (org-buffer-property-keys nil nil nil t)))))
  4677. (ert-deftest test-org/property-values ()
  4678. "Test `org-property-values' specifications."
  4679. ;; Regular test.
  4680. (should
  4681. (equal '("2" "1")
  4682. (org-test-with-temp-text
  4683. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  4684. (org-property-values "A"))))
  4685. ;; Ignore empty values.
  4686. (should-not
  4687. (org-test-with-temp-text
  4688. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  4689. (org-property-values "A")))
  4690. ;; Take into consideration extended values.
  4691. (should
  4692. (equal '("1 2")
  4693. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4694. (org-property-values "A")))))
  4695. (ert-deftest test-org/find-property ()
  4696. "Test `org-find-property' specifications."
  4697. ;; Regular test.
  4698. (should
  4699. (= 1
  4700. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  4701. (org-find-property "prop"))))
  4702. ;; Ignore false positives.
  4703. (should
  4704. (= 27
  4705. (org-test-with-temp-text
  4706. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  4707. (org-find-property "A"))))
  4708. ;; Return first entry found in buffer.
  4709. (should
  4710. (= 1
  4711. (org-test-with-temp-text
  4712. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4713. (org-find-property "A"))))
  4714. ;; Only search visible part of the buffer.
  4715. (should
  4716. (= 31
  4717. (org-test-with-temp-text
  4718. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4719. (org-narrow-to-subtree)
  4720. (org-find-property "A"))))
  4721. ;; With optional argument, only find entries with a specific value.
  4722. (should-not
  4723. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4724. (org-find-property "A" "2")))
  4725. (should
  4726. (= 31
  4727. (org-test-with-temp-text
  4728. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  4729. (org-find-property "A" "2"))))
  4730. ;; Use "nil" for explicit nil values.
  4731. (should
  4732. (= 31
  4733. (org-test-with-temp-text
  4734. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  4735. (org-find-property "A" "nil")))))
  4736. (ert-deftest test-org/entry-delete ()
  4737. "Test `org-entry-delete' specifications."
  4738. ;; Regular test.
  4739. (should
  4740. (string-match
  4741. " *:PROPERTIES:\n *:B: +2\n *:END:"
  4742. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4743. (org-entry-delete (point) "A")
  4744. (buffer-string))))
  4745. ;; Also remove accumulated properties.
  4746. (should-not
  4747. (string-match
  4748. ":A"
  4749. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  4750. (org-entry-delete (point) "A")
  4751. (buffer-string))))
  4752. ;; When last property is removed, remove the property drawer.
  4753. (should-not
  4754. (string-match
  4755. ":PROPERTIES:"
  4756. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  4757. (org-entry-delete (point) "A")
  4758. (buffer-string))))
  4759. ;; Return a non-nil value when some property was removed.
  4760. (should
  4761. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4762. (org-entry-delete (point) "A")))
  4763. (should-not
  4764. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4765. (org-entry-delete (point) "C")))
  4766. ;; Special properties cannot be located in a drawer. Allow to
  4767. ;; remove them anyway, in case of user error.
  4768. (should
  4769. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  4770. (org-entry-delete (point) "SCHEDULED"))))
  4771. (ert-deftest test-org/entry-get ()
  4772. "Test `org-entry-get' specifications."
  4773. ;; Regular test.
  4774. (should
  4775. (equal "1"
  4776. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4777. (org-entry-get (point) "A"))))
  4778. ;; Ignore case.
  4779. (should
  4780. (equal "1"
  4781. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4782. (org-entry-get (point) "a"))))
  4783. ;; Handle extended values, both before and after base value.
  4784. (should
  4785. (equal "1 2 3"
  4786. (org-test-with-temp-text
  4787. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  4788. (org-entry-get (point) "A"))))
  4789. ;; Empty values are returned as the empty string.
  4790. (should
  4791. (equal ""
  4792. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  4793. (org-entry-get (point) "A"))))
  4794. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  4795. ;; otherwise, return nil.
  4796. (should-not
  4797. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  4798. (org-entry-get (point) "A")))
  4799. (should
  4800. (equal "nil"
  4801. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  4802. (org-entry-get (point) "A" nil t))))
  4803. ;; Return nil when no property is found, independently on the
  4804. ;; LITERAL-NIL argument.
  4805. (should-not
  4806. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4807. (org-entry-get (point) "B")))
  4808. (should-not
  4809. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4810. (org-entry-get (point) "B" nil t)))
  4811. ;; Handle inheritance, when allowed. Include extended values and
  4812. ;; possibly global values.
  4813. (should
  4814. (equal
  4815. "1"
  4816. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4817. (org-entry-get (point) "A" t))))
  4818. (should
  4819. (equal
  4820. "1"
  4821. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4822. (let ((org-use-property-inheritance t))
  4823. (org-entry-get (point) "A" 'selective)))))
  4824. (should-not
  4825. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4826. (let ((org-use-property-inheritance nil))
  4827. (org-entry-get (point) "A" 'selective))))
  4828. (should
  4829. (equal
  4830. "1 2"
  4831. (org-test-with-temp-text
  4832. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  4833. (org-entry-get (point-max) "A" t))))
  4834. (should
  4835. (equal "1"
  4836. (org-test-with-temp-text
  4837. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  4838. (org-mode-restart)
  4839. (org-entry-get (point-max) "A" t))))
  4840. (should
  4841. (equal "0 1"
  4842. (org-test-with-temp-text
  4843. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  4844. (org-mode-restart)
  4845. (org-entry-get (point-max) "A" t)))))
  4846. (ert-deftest test-org/entry-properties ()
  4847. "Test `org-entry-properties' specifications."
  4848. ;; Get "ITEM" property.
  4849. (should
  4850. (equal "H"
  4851. (org-test-with-temp-text "* TODO H"
  4852. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  4853. (should
  4854. (equal "H"
  4855. (org-test-with-temp-text "* TODO H"
  4856. (cdr (assoc "ITEM" (org-entry-properties))))))
  4857. ;; Get "TODO" property. TODO keywords are case sensitive.
  4858. (should
  4859. (equal "TODO"
  4860. (org-test-with-temp-text "* TODO H"
  4861. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  4862. (should
  4863. (equal "TODO"
  4864. (org-test-with-temp-text "* TODO H"
  4865. (cdr (assoc "TODO" (org-entry-properties))))))
  4866. (should-not
  4867. (org-test-with-temp-text "* H"
  4868. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4869. (should-not
  4870. (org-test-with-temp-text "* todo H"
  4871. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4872. ;; Get "PRIORITY" property.
  4873. (should
  4874. (equal "A"
  4875. (org-test-with-temp-text "* [#A] H"
  4876. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4877. (should
  4878. (equal "A"
  4879. (org-test-with-temp-text "* [#A] H"
  4880. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  4881. (should
  4882. (equal (char-to-string org-default-priority)
  4883. (org-test-with-temp-text "* H"
  4884. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4885. ;; Get "FILE" property.
  4886. (should
  4887. (org-test-with-temp-text-in-file "* H\nParagraph"
  4888. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  4889. (buffer-file-name))))
  4890. (should
  4891. (org-test-with-temp-text-in-file "* H\nParagraph"
  4892. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  4893. (buffer-file-name))))
  4894. (should-not
  4895. (org-test-with-temp-text "* H\nParagraph"
  4896. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  4897. ;; Get "TAGS" property.
  4898. (should
  4899. (equal ":tag1:tag2:"
  4900. (org-test-with-temp-text "* H :tag1:tag2:"
  4901. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  4902. (should
  4903. (equal ":tag1:tag2:"
  4904. (org-test-with-temp-text "* H :tag1:tag2:"
  4905. (cdr (assoc "TAGS" (org-entry-properties))))))
  4906. (should-not
  4907. (org-test-with-temp-text "* H"
  4908. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  4909. ;; Get "ALLTAGS" property.
  4910. (should
  4911. (equal ":tag1:tag2:"
  4912. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4913. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  4914. (should
  4915. (equal ":tag1:tag2:"
  4916. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4917. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  4918. (should-not
  4919. (org-test-with-temp-text "* H"
  4920. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  4921. ;; Get "BLOCKED" property.
  4922. (should
  4923. (equal "t"
  4924. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  4925. (let ((org-enforce-todo-dependencies t)
  4926. (org-blocker-hook
  4927. '(org-block-todo-from-children-or-siblings-or-parent)))
  4928. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4929. (should
  4930. (equal ""
  4931. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  4932. (let ((org-enforce-todo-dependencies t)
  4933. (org-blocker-hook
  4934. '(org-block-todo-from-children-or-siblings-or-parent)))
  4935. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4936. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  4937. (should
  4938. (equal
  4939. "[2012-03-29 thu.]"
  4940. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4941. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  4942. (should
  4943. (equal
  4944. "[2012-03-29 thu.]"
  4945. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4946. (cdr (assoc "CLOSED" (org-entry-properties))))))
  4947. (should-not
  4948. (org-test-with-temp-text "* H"
  4949. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  4950. (should
  4951. (equal
  4952. "<2014-03-04 tue.>"
  4953. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4954. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  4955. (should
  4956. (equal
  4957. "<2014-03-04 tue.>"
  4958. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4959. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  4960. (should-not
  4961. (org-test-with-temp-text "* H"
  4962. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  4963. (should
  4964. (equal
  4965. "<2014-03-04 tue.>"
  4966. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4967. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  4968. (should
  4969. (equal
  4970. "<2014-03-04 tue.>"
  4971. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4972. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  4973. (should-not
  4974. (org-test-with-temp-text "* H"
  4975. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  4976. ;; Get "CATEGORY"
  4977. (should
  4978. (equal "cat"
  4979. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4980. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  4981. (should
  4982. (equal "cat"
  4983. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4984. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4985. (should
  4986. (equal "cat"
  4987. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  4988. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4989. (should
  4990. (equal "cat2"
  4991. (org-test-with-temp-text
  4992. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  4993. "\n"
  4994. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  4995. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4996. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  4997. (should
  4998. (equal "<2012-03-29 thu.>"
  4999. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  5000. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5001. (should
  5002. (equal "[2012-03-29 thu.]"
  5003. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  5004. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  5005. (should
  5006. (equal "<2012-03-29 thu.>"
  5007. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  5008. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  5009. (should
  5010. (equal "[2014-03-04 tue.]"
  5011. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  5012. (cdr (assoc "TIMESTAMP_IA"
  5013. (org-entry-properties nil "TIMESTAMP_IA"))))))
  5014. (should-not
  5015. (equal "<2012-03-29 thu.>"
  5016. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  5017. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5018. ;; Get standard properties.
  5019. (should
  5020. (equal "1"
  5021. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5022. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5023. ;; Handle extended properties.
  5024. (should
  5025. (equal "1 2 3"
  5026. (org-test-with-temp-text
  5027. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5028. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5029. (should
  5030. (equal "1 2 3"
  5031. (org-test-with-temp-text
  5032. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  5033. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5034. ;; Ignore forbidden (special) properties.
  5035. (should-not
  5036. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  5037. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  5038. (ert-deftest test-org/entry-put ()
  5039. "Test `org-entry-put' specifications."
  5040. ;; Error when not a string or nil.
  5041. (should-error
  5042. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5043. (org-entry-put 1 "test" 2)))
  5044. ;; Error when property name is invalid.
  5045. (should-error
  5046. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5047. (org-entry-put 1 "no space" "value")))
  5048. (should-error
  5049. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5050. (org-entry-put 1 "" "value")))
  5051. ;; Set "TODO" property.
  5052. (should
  5053. (string-match (regexp-quote " TODO H")
  5054. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5055. (org-entry-put (point) "TODO" "TODO")
  5056. (buffer-string))))
  5057. (should
  5058. (string-match (regexp-quote "* H")
  5059. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5060. (org-entry-put (point) "TODO" nil)
  5061. (buffer-string))))
  5062. ;; Set "PRIORITY" property.
  5063. (should
  5064. (equal "* [#A] H"
  5065. (org-test-with-temp-text "* [#B] H"
  5066. (org-entry-put (point) "PRIORITY" "A")
  5067. (buffer-string))))
  5068. (should
  5069. (equal "* H"
  5070. (org-test-with-temp-text "* [#B] H"
  5071. (org-entry-put (point) "PRIORITY" nil)
  5072. (buffer-string))))
  5073. ;; Set "SCHEDULED" property.
  5074. (should
  5075. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  5076. (org-test-with-temp-text "* H"
  5077. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  5078. (buffer-string))))
  5079. (should
  5080. (string= "* H\n"
  5081. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5082. (org-entry-put (point) "SCHEDULED" nil)
  5083. (buffer-string))))
  5084. (should
  5085. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  5086. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5087. (org-entry-put (point) "SCHEDULED" "earlier")
  5088. (buffer-string))))
  5089. (should
  5090. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  5091. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5092. (org-entry-put (point) "SCHEDULED" "later")
  5093. (buffer-string))))
  5094. ;; Set "DEADLINE" property.
  5095. (should
  5096. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  5097. (org-test-with-temp-text "* H"
  5098. (org-entry-put (point) "DEADLINE" "2014-03-04")
  5099. (buffer-string))))
  5100. (should
  5101. (string= "* H\n"
  5102. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5103. (org-entry-put (point) "DEADLINE" nil)
  5104. (buffer-string))))
  5105. (should
  5106. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  5107. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5108. (org-entry-put (point) "DEADLINE" "earlier")
  5109. (buffer-string))))
  5110. (should
  5111. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  5112. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5113. (org-entry-put (point) "DEADLINE" "later")
  5114. (buffer-string))))
  5115. ;; Set "CATEGORY" property
  5116. (should
  5117. (string-match "^ *:CATEGORY: cat"
  5118. (org-test-with-temp-text "* H"
  5119. (org-entry-put (point) "CATEGORY" "cat")
  5120. (buffer-string))))
  5121. ;; Regular properties, with or without pre-existing drawer.
  5122. (should
  5123. (string-match "^ *:A: +2$"
  5124. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5125. (org-entry-put (point) "A" "2")
  5126. (buffer-string))))
  5127. (should
  5128. (string-match "^ *:A: +1$"
  5129. (org-test-with-temp-text "* H"
  5130. (org-entry-put (point) "A" "1")
  5131. (buffer-string))))
  5132. ;; Special case: two consecutive headlines.
  5133. (should
  5134. (string-match "\\* A\n *:PROPERTIES:"
  5135. (org-test-with-temp-text "* A\n** B"
  5136. (org-entry-put (point) "A" "1")
  5137. (buffer-string)))))
  5138. (ert-deftest test-org/refresh-properties ()
  5139. "Test `org-refresh-properties' specifications."
  5140. (should
  5141. (equal "1"
  5142. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5143. (org-refresh-properties "A" 'org-test)
  5144. (get-text-property (point) 'org-test))))
  5145. (should-not
  5146. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5147. (org-refresh-properties "B" 'org-test)
  5148. (get-text-property (point) 'org-test)))
  5149. ;; Handle properties only defined with extension syntax, i.e.,
  5150. ;; "PROPERTY+".
  5151. (should
  5152. (equal "1"
  5153. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5154. (org-refresh-properties "A" 'org-test)
  5155. (get-text-property (point) 'org-test))))
  5156. ;; When property is inherited, add text property to the whole
  5157. ;; sub-tree.
  5158. (should
  5159. (equal "1"
  5160. (org-test-with-temp-text
  5161. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  5162. (let ((org-use-property-inheritance t))
  5163. (org-refresh-properties "A" 'org-test))
  5164. (get-text-property (point) 'org-test))))
  5165. ;; When property is inherited, use global value across the whole
  5166. ;; buffer. However local values have precedence.
  5167. (should-not
  5168. (equal "1"
  5169. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5170. (org-mode-restart)
  5171. (let ((org-use-property-inheritance nil))
  5172. (org-refresh-properties "A" 'org-test))
  5173. (get-text-property (point) 'org-test))))
  5174. (should
  5175. (equal "1"
  5176. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5177. (org-mode-restart)
  5178. (let ((org-use-property-inheritance t))
  5179. (org-refresh-properties "A" 'org-test))
  5180. (get-text-property (point) 'org-test))))
  5181. (should
  5182. (equal "2"
  5183. (org-test-with-temp-text
  5184. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  5185. (org-mode-restart)
  5186. (let ((org-use-property-inheritance t))
  5187. (org-refresh-properties "A" 'org-test))
  5188. (get-text-property (point) 'org-test)))))
  5189. ;;; Radio Targets
  5190. (ert-deftest test-org/update-radio-target-regexp ()
  5191. "Test `org-update-radio-target-regexp' specifications."
  5192. ;; Properly update cache with no previous radio target regexp.
  5193. (should
  5194. (eq 'link
  5195. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5196. (save-excursion (goto-char (point-max)) (org-element-context))
  5197. (insert "<<<")
  5198. (search-forward "o")
  5199. (insert ">>>")
  5200. (org-update-radio-target-regexp)
  5201. (goto-char (point-max))
  5202. (org-element-type (org-element-context)))))
  5203. ;; Properly update cache with previous radio target regexp.
  5204. (should
  5205. (eq 'link
  5206. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5207. (save-excursion (goto-char (point-max)) (org-element-context))
  5208. (insert "<<<")
  5209. (search-forward "o")
  5210. (insert ">>>")
  5211. (org-update-radio-target-regexp)
  5212. (search-backward "r")
  5213. (delete-char 5)
  5214. (insert "new")
  5215. (org-update-radio-target-regexp)
  5216. (goto-char (point-max))
  5217. (delete-region (line-beginning-position) (point))
  5218. (insert "new")
  5219. (org-element-type (org-element-context))))))
  5220. ;;; Refile
  5221. (ert-deftest test-org/refile-get-targets ()
  5222. "Test `org-refile-get-targets' specifications."
  5223. ;; :maxlevel includes all headings above specified value.
  5224. (should
  5225. (equal '("H1" "H2" "H3")
  5226. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5227. (let ((org-refile-use-outline-path nil)
  5228. (org-refile-targets `((nil :maxlevel . 3))))
  5229. (mapcar #'car (org-refile-get-targets))))))
  5230. (should
  5231. (equal '("H1" "H2")
  5232. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5233. (let ((org-refile-use-outline-path nil)
  5234. (org-refile-targets `((nil :maxlevel . 2))))
  5235. (mapcar #'car (org-refile-get-targets))))))
  5236. ;; :level limits targets to headlines with the specified level.
  5237. (should
  5238. (equal '("H2")
  5239. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5240. (let ((org-refile-use-outline-path nil)
  5241. (org-refile-targets `((nil :level . 2))))
  5242. (mapcar #'car (org-refile-get-targets))))))
  5243. ;; :tag limits targets to headlines with specified tag.
  5244. (should
  5245. (equal '("H1")
  5246. (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
  5247. (let ((org-refile-use-outline-path nil)
  5248. (org-refile-targets `((nil :tag . "foo"))))
  5249. (mapcar #'car (org-refile-get-targets))))))
  5250. ;; :todo limits targets to headlines with specified TODO keyword.
  5251. (should
  5252. (equal '("H2")
  5253. (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
  5254. (let ((org-refile-use-outline-path nil)
  5255. (org-refile-targets `((nil :todo . "TODO"))))
  5256. (mapcar #'car (org-refile-get-targets))))))
  5257. ;; :regexp filters targets matching provided regexp.
  5258. (should
  5259. (equal '("F2" "F3")
  5260. (org-test-with-temp-text "* H1\n** F2\n*** F3"
  5261. (let ((org-refile-use-outline-path nil)
  5262. (org-refile-targets `((nil :regexp . "F"))))
  5263. (mapcar #'car (org-refile-get-targets))))))
  5264. ;; A nil `org-refile-targets' includes only top level headlines in
  5265. ;; current buffer.
  5266. (should
  5267. (equal '("H1" "H2")
  5268. (org-test-with-temp-text "* H1\n** S1\n* H2"
  5269. (let ((org-refile-use-outline-path nil)
  5270. (org-refile-targets nil))
  5271. (mapcar #'car (org-refile-get-targets))))))
  5272. ;; Return value is the union of the targets according to all the
  5273. ;; defined rules. However, prevent duplicates.
  5274. (should
  5275. (equal '("F2" "F3" "H1")
  5276. (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
  5277. (let ((org-refile-use-outline-path nil)
  5278. (org-refile-targets `((nil :regexp . "F")
  5279. (nil :todo . "TODO"))))
  5280. (mapcar #'car (org-refile-get-targets))))))
  5281. (should
  5282. (equal '("F2" "F3" "H1")
  5283. (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
  5284. (let ((org-refile-use-outline-path nil)
  5285. (org-refile-targets `((nil :regexp . "F")
  5286. (nil :todo . "TODO"))))
  5287. (mapcar #'car (org-refile-get-targets))))))
  5288. ;; When `org-refile-use-outline-path' is non-nil, provide targets as
  5289. ;; paths.
  5290. (should
  5291. (equal '("H1" "H1/H2" "H1/H2/H3")
  5292. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5293. (let ((org-refile-use-outline-path t)
  5294. (org-refile-targets `((nil :maxlevel . 3))))
  5295. (mapcar #'car (org-refile-get-targets))))))
  5296. ;; When providing targets as paths, escape forward slashes in
  5297. ;; headings with backslashes.
  5298. (should
  5299. (equal '("H1\\/foo")
  5300. (org-test-with-temp-text "* H1/foo"
  5301. (let ((org-refile-use-outline-path t)
  5302. (org-refile-targets `((nil :maxlevel . 1))))
  5303. (mapcar #'car (org-refile-get-targets))))))
  5304. ;; When `org-refile-use-outline-path' is `file', include file name
  5305. ;; without directory in targets.
  5306. (should
  5307. (org-test-with-temp-text-in-file "* H1"
  5308. (let* ((filename (buffer-file-name))
  5309. (org-refile-use-outline-path 'file)
  5310. (org-refile-targets `(((,filename) :level . 1))))
  5311. (member (file-name-nondirectory filename)
  5312. (mapcar #'car (org-refile-get-targets))))))
  5313. ;; When `org-refile-use-outline-path' is `full-file-path', include
  5314. ;; full file name.
  5315. (should
  5316. (org-test-with-temp-text-in-file "* H1"
  5317. (let* ((filename (buffer-file-name))
  5318. (org-refile-use-outline-path 'full-file-path)
  5319. (org-refile-targets `(((,filename) :level . 1))))
  5320. (member filename (mapcar #'car (org-refile-get-targets))))))
  5321. ;; When `org-refile-use-outline-path' is `buffer-name', include
  5322. ;; buffer name.
  5323. (should
  5324. (org-test-with-temp-text "* H1"
  5325. (let* ((org-refile-use-outline-path 'buffer-name)
  5326. (org-refile-targets `((nil :level . 1))))
  5327. (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
  5328. ;;; Sparse trees
  5329. (ert-deftest test-org/match-sparse-tree ()
  5330. "Test `org-match-sparse-tree' specifications."
  5331. ;; Match tags.
  5332. (should-not
  5333. (org-test-with-temp-text "* H\n** H1 :tag:"
  5334. (org-match-sparse-tree nil "tag")
  5335. (search-forward "H1")
  5336. (org-invisible-p2)))
  5337. (should
  5338. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  5339. (org-match-sparse-tree nil "tag")
  5340. (search-forward "H2")
  5341. (org-invisible-p2)))
  5342. ;; "-" operator for tags.
  5343. (should-not
  5344. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5345. (org-match-sparse-tree nil "tag1-tag2")
  5346. (search-forward "H1")
  5347. (org-invisible-p2)))
  5348. (should
  5349. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5350. (org-match-sparse-tree nil "tag1-tag2")
  5351. (search-forward "H2")
  5352. (org-invisible-p2)))
  5353. ;; "&" operator for tags.
  5354. (should
  5355. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5356. (org-match-sparse-tree nil "tag1&tag2")
  5357. (search-forward "H1")
  5358. (org-invisible-p2)))
  5359. (should-not
  5360. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5361. (org-match-sparse-tree nil "tag1&tag2")
  5362. (search-forward "H2")
  5363. (org-invisible-p2)))
  5364. ;; "|" operator for tags.
  5365. (should-not
  5366. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5367. (org-match-sparse-tree nil "tag1|tag2")
  5368. (search-forward "H1")
  5369. (org-invisible-p2)))
  5370. (should-not
  5371. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5372. (org-match-sparse-tree nil "tag1|tag2")
  5373. (search-forward "H2")
  5374. (org-invisible-p2)))
  5375. ;; Regexp match on tags.
  5376. (should-not
  5377. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5378. (org-match-sparse-tree nil "{^tag.*}")
  5379. (search-forward "H1")
  5380. (org-invisible-p2)))
  5381. (should
  5382. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5383. (org-match-sparse-tree nil "{^tag.*}")
  5384. (search-forward "H2")
  5385. (org-invisible-p2)))
  5386. ;; Match group tags.
  5387. (should-not
  5388. (org-test-with-temp-text
  5389. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5390. (org-match-sparse-tree nil "work")
  5391. (search-forward "H1")
  5392. (org-invisible-p2)))
  5393. (should-not
  5394. (org-test-with-temp-text
  5395. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5396. (org-match-sparse-tree nil "work")
  5397. (search-forward "H2")
  5398. (org-invisible-p2)))
  5399. ;; Match group tags with hard brackets.
  5400. (should-not
  5401. (org-test-with-temp-text
  5402. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5403. (org-match-sparse-tree nil "work")
  5404. (search-forward "H1")
  5405. (org-invisible-p2)))
  5406. (should-not
  5407. (org-test-with-temp-text
  5408. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5409. (org-match-sparse-tree nil "work")
  5410. (search-forward "H2")
  5411. (org-invisible-p2)))
  5412. ;; Match tags in hierarchies
  5413. (should-not
  5414. (org-test-with-temp-text
  5415. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5416. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5417. #+TAGS: { Lev_3 : Lev_4 }\n
  5418. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5419. (org-match-sparse-tree nil "Lev_1")
  5420. (search-forward "H4")
  5421. (org-invisible-p2)))
  5422. ;; Match regular expressions in tags
  5423. (should-not
  5424. (org-test-with-temp-text
  5425. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  5426. (org-match-sparse-tree nil "Lev")
  5427. (search-forward "H1")
  5428. (org-invisible-p2)))
  5429. (should
  5430. (org-test-with-temp-text
  5431. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  5432. (org-match-sparse-tree nil "Lev")
  5433. (search-forward "H1")
  5434. (org-invisible-p2)))
  5435. ;; Match properties.
  5436. (should
  5437. (org-test-with-temp-text
  5438. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  5439. (org-match-sparse-tree nil "A=\"1\"")
  5440. (search-forward "H2")
  5441. (org-invisible-p2)))
  5442. (should-not
  5443. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5444. (org-match-sparse-tree nil "A=\"1\"")
  5445. (search-forward "H2")
  5446. (org-invisible-p2)))
  5447. ;; Case is not significant when matching properties.
  5448. (should-not
  5449. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5450. (org-match-sparse-tree nil "a=\"1\"")
  5451. (search-forward "H2")
  5452. (org-invisible-p2)))
  5453. (should-not
  5454. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  5455. (org-match-sparse-tree nil "A=\"1\"")
  5456. (search-forward "H2")
  5457. (org-invisible-p2)))
  5458. ;; Match special LEVEL property.
  5459. (should-not
  5460. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5461. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5462. (search-forward "H1")
  5463. (org-invisible-p2)))
  5464. (should
  5465. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5466. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5467. (search-forward "H2")
  5468. (org-invisible-p2)))
  5469. ;; Comparison operators when matching properties.
  5470. (should
  5471. (org-test-with-temp-text
  5472. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5473. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5474. (search-forward "H1")
  5475. (org-invisible-p2)))
  5476. (should-not
  5477. (org-test-with-temp-text
  5478. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5479. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5480. (search-forward "H2")
  5481. (org-invisible-p2)))
  5482. ;; Regexp match on properties values.
  5483. (should-not
  5484. (org-test-with-temp-text
  5485. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5486. (org-match-sparse-tree nil "A={f.*}")
  5487. (search-forward "H1")
  5488. (org-invisible-p2)))
  5489. (should
  5490. (org-test-with-temp-text
  5491. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5492. (org-match-sparse-tree nil "A={f.*}")
  5493. (search-forward "H2")
  5494. (org-invisible-p2)))
  5495. ;; With an optional argument, limit match to TODO entries.
  5496. (should-not
  5497. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5498. (org-match-sparse-tree t "tag")
  5499. (search-forward "H1")
  5500. (org-invisible-p2)))
  5501. (should
  5502. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5503. (org-match-sparse-tree t "tag")
  5504. (search-forward "H2")
  5505. (org-invisible-p2))))
  5506. (ert-deftest test-org/occur ()
  5507. "Test `org-occur' specifications."
  5508. ;; Count number of matches.
  5509. (should
  5510. (= 1
  5511. (org-test-with-temp-text "* H\nA\n* H2"
  5512. (org-occur "A"))))
  5513. (should
  5514. (= 2
  5515. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5516. (org-occur "A"))))
  5517. ;; Test CALLBACK optional argument.
  5518. (should
  5519. (= 0
  5520. (org-test-with-temp-text "* H\nA\n* H2"
  5521. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5522. (should
  5523. (= 1
  5524. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5525. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5526. ;; Case-fold searches according to `org-occur-case-fold-search'.
  5527. (should
  5528. (= 2
  5529. (org-test-with-temp-text "Aa"
  5530. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  5531. (should
  5532. (= 2
  5533. (org-test-with-temp-text "Aa"
  5534. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  5535. (should
  5536. (= 1
  5537. (org-test-with-temp-text "Aa"
  5538. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  5539. (should
  5540. (= 1
  5541. (org-test-with-temp-text "Aa"
  5542. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  5543. (should
  5544. (= 1
  5545. (org-test-with-temp-text "Aa"
  5546. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  5547. (should
  5548. (= 2
  5549. (org-test-with-temp-text "Aa"
  5550. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  5551. ;;; Tags
  5552. (ert-deftest test-org/tag-string-to-alist ()
  5553. "Test `org-tag-string-to-alist' specifications."
  5554. ;; Tag without selection key.
  5555. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  5556. ;; Tag with selection key.
  5557. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  5558. ;; Tag group.
  5559. (should
  5560. (equal
  5561. (org-tag-string-to-alist "[ group : t1 t2 ]")
  5562. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  5563. ;; Mutually exclusive tags.
  5564. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  5565. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  5566. (should
  5567. (equal
  5568. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  5569. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  5570. (ert-deftest test-org/tag-alist-to-string ()
  5571. "Test `org-tag-alist-to-string' specifications."
  5572. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  5573. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  5574. (should
  5575. (equal
  5576. (org-tag-alist-to-string
  5577. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5578. "[ group : t1 t2 ]"))
  5579. (should
  5580. (equal (org-tag-alist-to-string
  5581. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  5582. "{ tag1 tag2 }"))
  5583. (should
  5584. (equal
  5585. (org-tag-alist-to-string
  5586. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  5587. "{ group : tag1 tag2 }")))
  5588. (ert-deftest test-org/tag-alist-to-groups ()
  5589. "Test `org-tag-alist-to-groups' specifications."
  5590. (should
  5591. (equal (org-tag-alist-to-groups
  5592. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  5593. '(("group" "t1" "t2"))))
  5594. (should
  5595. (equal
  5596. (org-tag-alist-to-groups
  5597. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5598. '(("group" "t1" "t2"))))
  5599. (should-not
  5600. (org-tag-alist-to-groups
  5601. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  5602. (ert-deftest test-org/tag-align ()
  5603. "Test tags alignment."
  5604. ;; Test aligning tags with different display width.
  5605. (should
  5606. ;; 12345678901234567890
  5607. (equal "* Test :abc:"
  5608. (org-test-with-temp-text "* Test :abc:"
  5609. (let ((org-tags-column -20)
  5610. (indent-tabs-mode nil))
  5611. (org-fix-tags-on-the-fly))
  5612. (buffer-string))))
  5613. (should
  5614. ;; 12345678901234567890
  5615. (equal "* Test :日本語:"
  5616. (org-test-with-temp-text "* Test :日本語:"
  5617. (let ((org-tags-column -20)
  5618. (indent-tabs-mode nil))
  5619. (org-fix-tags-on-the-fly))
  5620. (buffer-string))))
  5621. ;; Make sure aligning tags do not skip invisible text.
  5622. (should
  5623. (equal "* [[linkx]] :tag:"
  5624. (org-test-with-temp-text "* [[link<point>]] :tag:"
  5625. (let ((org-tags-column 0))
  5626. (org-fix-tags-on-the-fly)
  5627. (insert "x")
  5628. (buffer-string))))))
  5629. (ert-deftest test-org/tags-at ()
  5630. (should
  5631. (equal '("foo" "bar")
  5632. (org-test-with-temp-text
  5633. "* T<point>est :foo:bar:"
  5634. (org-get-tags-at)))))
  5635. ;;; TODO keywords
  5636. (ert-deftest test-org/auto-repeat-maybe ()
  5637. "Test `org-auto-repeat-maybe' specifications."
  5638. ;; Do not auto repeat when there is no valid time stamp with
  5639. ;; a repeater in the entry.
  5640. (should-not
  5641. (string-prefix-p
  5642. "* TODO H"
  5643. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5644. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  5645. (org-todo "DONE")
  5646. (buffer-string)))))
  5647. (should-not
  5648. (string-prefix-p
  5649. "* TODO H"
  5650. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5651. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  5652. (org-todo "DONE")
  5653. (buffer-string)))))
  5654. ;; When switching to DONE state, switch back to first TODO keyword
  5655. ;; in sequence, or the same keyword if they have different types.
  5656. (should
  5657. (string-prefix-p
  5658. "* TODO H"
  5659. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5660. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  5661. (org-todo "DONE")
  5662. (buffer-string)))))
  5663. (should
  5664. (string-prefix-p
  5665. "* KWD1 H"
  5666. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  5667. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  5668. (org-todo "DONE")
  5669. (buffer-string)))))
  5670. (should
  5671. (string-prefix-p
  5672. "* KWD2 H"
  5673. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  5674. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  5675. (org-todo "DONE")
  5676. (buffer-string)))))
  5677. ;; If there was no TODO keyword in the first place, do not insert
  5678. ;; any either.
  5679. (should
  5680. (string-prefix-p
  5681. "* H"
  5682. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5683. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  5684. (org-todo "DONE")
  5685. (buffer-string)))))
  5686. ;; Revert to REPEAT_TO_STATE, if set.
  5687. (should
  5688. (string-prefix-p
  5689. "* KWD2 H"
  5690. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  5691. (org-test-with-temp-text
  5692. "* KWD2 H
  5693. :PROPERTIES:
  5694. :REPEAT_TO_STATE: KWD2
  5695. :END:
  5696. <2012-03-29 Thu +2y>"
  5697. (org-todo "DONE")
  5698. (buffer-string)))))
  5699. ;; When switching to DONE state, update base date. If there are
  5700. ;; multiple repeated time stamps, update them all.
  5701. (should
  5702. (string-match-p
  5703. "<2014-03-29 .* \\+2y>"
  5704. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5705. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  5706. (org-todo "DONE")
  5707. (buffer-string)))))
  5708. (should
  5709. (string-match-p
  5710. "<2015-03-04 .* \\+1y>"
  5711. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5712. (org-test-with-temp-text
  5713. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  5714. (org-todo "DONE")
  5715. (buffer-string)))))
  5716. ;; Throw an error if repeater unit is the hour and no time is
  5717. ;; provided in the time-stamp.
  5718. (should-error
  5719. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5720. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  5721. (org-todo "DONE")
  5722. (buffer-string))))
  5723. ;; Do not repeat commented time stamps.
  5724. (should-not
  5725. (string-prefix-p
  5726. "<2015-03-04 .* \\+1y>"
  5727. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5728. (org-test-with-temp-text
  5729. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  5730. (org-todo "DONE")
  5731. (buffer-string)))))
  5732. (should-not
  5733. (string-prefix-p
  5734. "<2015-03-04 .* \\+1y>"
  5735. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5736. (org-test-with-temp-text
  5737. "* TODO H
  5738. <2012-03-29 Thu. +2y>
  5739. #+BEGIN_EXAMPLE
  5740. <2014-03-04 Tue +1y>
  5741. #+END_EXAMPLE"
  5742. (org-todo "DONE")
  5743. (buffer-string)))))
  5744. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  5745. ;; entry, record time of last repeat.
  5746. (should-not
  5747. (string-match-p
  5748. ":LAST_REPEAT:"
  5749. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  5750. (org-log-repeat nil))
  5751. (cl-letf (((symbol-function 'org-add-log-setup)
  5752. (lambda (&rest args) nil)))
  5753. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  5754. (org-todo "DONE")
  5755. (buffer-string))))))
  5756. (should
  5757. (string-match-p
  5758. ":LAST_REPEAT:"
  5759. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  5760. (org-log-repeat t))
  5761. (cl-letf (((symbol-function 'org-add-log-setup)
  5762. (lambda (&rest args) nil)))
  5763. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  5764. (org-todo "DONE")
  5765. (buffer-string))))))
  5766. (should
  5767. (string-match-p
  5768. ":LAST_REPEAT:"
  5769. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5770. (cl-letf (((symbol-function 'org-add-log-setup)
  5771. (lambda (&rest args) nil)))
  5772. (org-test-with-temp-text
  5773. "* TODO H\n<2012-03-29 Thu. +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  5774. (org-todo "DONE")
  5775. (buffer-string))))))
  5776. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  5777. ;; the entry as it is no longer relevant.
  5778. (should-not
  5779. (string-match-p
  5780. "^SCHEDULED:"
  5781. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5782. (org-test-with-temp-text
  5783. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  5784. (org-todo "DONE")
  5785. (buffer-string))))))
  5786. ;;; Timestamps API
  5787. (ert-deftest test-org/at-timestamp-p ()
  5788. "Test `org-at-timestamp-p' specifications."
  5789. (should
  5790. (org-test-with-temp-text "<2012-03-29 Thu>"
  5791. (org-at-timestamp-p)))
  5792. (should-not
  5793. (org-test-with-temp-text "2012-03-29 Thu"
  5794. (org-at-timestamp-p)))
  5795. ;; Test return values.
  5796. (should
  5797. (eq 'bracket
  5798. (org-test-with-temp-text "<2012-03-29 Thu>"
  5799. (org-at-timestamp-p))))
  5800. (should
  5801. (eq 'year
  5802. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  5803. (org-at-timestamp-p))))
  5804. (should
  5805. (eq 'month
  5806. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  5807. (org-at-timestamp-p))))
  5808. (should
  5809. (eq 'day
  5810. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  5811. (org-at-timestamp-p))))
  5812. (should
  5813. (eq 'day
  5814. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  5815. (org-at-timestamp-p))))
  5816. (should
  5817. (wholenump
  5818. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  5819. (org-at-timestamp-p))))
  5820. (should
  5821. (eq 'bracket
  5822. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  5823. (org-at-timestamp-p))))
  5824. (should
  5825. (eq 'after
  5826. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  5827. (org-at-timestamp-p))))
  5828. ;; Test `inactive' optional argument.
  5829. (should
  5830. (org-test-with-temp-text "[2012-03-29 Thu]"
  5831. (org-at-timestamp-p 'inactive)))
  5832. (should-not
  5833. (org-test-with-temp-text "[2012-03-29 Thu]"
  5834. (org-at-timestamp-p)))
  5835. ;; When optional argument is `agenda', recognize time-stamps in
  5836. ;; planning info line, property drawers and clocks.
  5837. (should
  5838. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  5839. (org-at-timestamp-p 'agenda)))
  5840. (should-not
  5841. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  5842. (org-at-timestamp-p)))
  5843. (should
  5844. (org-test-with-temp-text
  5845. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  5846. (org-at-timestamp-p 'agenda)))
  5847. (should-not
  5848. (org-test-with-temp-text
  5849. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  5850. (org-at-timestamp-p)))
  5851. (should
  5852. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  5853. (let ((org-agenda-include-inactive-timestamps t))
  5854. (org-at-timestamp-p 'agenda))))
  5855. (should-not
  5856. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  5857. (let ((org-agenda-include-inactive-timestamps t))
  5858. (org-at-timestamp-p))))
  5859. (should-not
  5860. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  5861. (let ((org-agenda-include-inactive-timestamps t))
  5862. (org-at-timestamp-p 'inactive))))
  5863. ;; When optional argument is `lax', match any part of the document
  5864. ;; with Org timestamp syntax.
  5865. (should
  5866. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  5867. (org-at-timestamp-p 'lax)))
  5868. (should-not
  5869. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  5870. (org-at-timestamp-p)))
  5871. (should
  5872. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  5873. (org-at-timestamp-p 'lax)))
  5874. (should-not
  5875. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  5876. (org-at-timestamp-p)))
  5877. (should
  5878. (org-test-with-temp-text
  5879. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  5880. (org-at-timestamp-p 'lax)))
  5881. (should-not
  5882. (org-test-with-temp-text
  5883. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  5884. (org-at-timestamp-p)))
  5885. ;; Optional argument `lax' also matches inactive timestamps.
  5886. (should
  5887. (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
  5888. (org-at-timestamp-p 'lax))))
  5889. (ert-deftest test-org/time-stamp ()
  5890. "Test `org-time-stamp' specifications."
  5891. ;; Insert chosen time stamp at point.
  5892. (should
  5893. (string-match
  5894. "Te<2014-03-04 .*?>xt"
  5895. (org-test-with-temp-text "Te<point>xt"
  5896. (cl-letf (((symbol-function 'org-read-date)
  5897. (lambda (&rest args)
  5898. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5899. (org-time-stamp nil)
  5900. (buffer-string)))))
  5901. ;; With a prefix argument, also insert time.
  5902. (should
  5903. (string-match
  5904. "Te<2014-03-04 .*? 00:41>xt"
  5905. (org-test-with-temp-text "Te<point>xt"
  5906. (cl-letf (((symbol-function 'org-read-date)
  5907. (lambda (&rest args)
  5908. (apply #'encode-time
  5909. (org-parse-time-string "2014-03-04 00:41")))))
  5910. (org-time-stamp '(4))
  5911. (buffer-string)))))
  5912. ;; With two universal prefix arguments, insert an active timestamp
  5913. ;; with the current time without prompting the user.
  5914. (should
  5915. (string-match
  5916. "Te<2014-03-04 .*? 00:41>xt"
  5917. (org-test-with-temp-text "Te<point>xt"
  5918. (cl-letf (((symbol-function 'current-time)
  5919. (lambda ()
  5920. (apply #'encode-time
  5921. (org-parse-time-string "2014-03-04 00:41")))))
  5922. (org-time-stamp '(16))
  5923. (buffer-string)))))
  5924. ;; When optional argument is non-nil, insert an inactive timestamp.
  5925. (should
  5926. (string-match
  5927. "Te\\[2014-03-04 .*?\\]xt"
  5928. (org-test-with-temp-text "Te<point>xt"
  5929. (cl-letf (((symbol-function 'org-read-date)
  5930. (lambda (&rest args)
  5931. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5932. (org-time-stamp nil t)
  5933. (buffer-string)))))
  5934. ;; When called from a timestamp, replace existing one.
  5935. (should
  5936. (string-match
  5937. "<2014-03-04 .*?>"
  5938. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  5939. (cl-letf (((symbol-function 'org-read-date)
  5940. (lambda (&rest args)
  5941. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5942. (org-time-stamp nil)
  5943. (buffer-string)))))
  5944. (should
  5945. (string-match
  5946. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  5947. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  5948. (cl-letf (((symbol-function 'org-read-date)
  5949. (lambda (&rest args)
  5950. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5951. (org-time-stamp nil)
  5952. (buffer-string)))))
  5953. ;; When replacing a timestamp, preserve repeater, if any.
  5954. (should
  5955. (string-match
  5956. "<2014-03-04 .*? \\+2y>"
  5957. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  5958. (cl-letf (((symbol-function 'org-read-date)
  5959. (lambda (&rest args)
  5960. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5961. (org-time-stamp nil)
  5962. (buffer-string)))))
  5963. ;; When called twice in a raw, build a date range.
  5964. (should
  5965. (string-match
  5966. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  5967. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  5968. (cl-letf (((symbol-function 'org-read-date)
  5969. (lambda (&rest args)
  5970. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5971. (let ((last-command 'org-time-stamp)
  5972. (this-command 'org-time-stamp))
  5973. (org-time-stamp nil))
  5974. (buffer-string))))))
  5975. (ert-deftest test-org/timestamp-has-time-p ()
  5976. "Test `org-timestamp-has-time-p' specifications."
  5977. ;; With time.
  5978. (should
  5979. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  5980. (org-timestamp-has-time-p (org-element-context))))
  5981. ;; Without time.
  5982. (should-not
  5983. (org-test-with-temp-text "<2012-03-29 Thu>"
  5984. (org-timestamp-has-time-p (org-element-context)))))
  5985. (ert-deftest test-org/get-repeat ()
  5986. "Test `org-get-repeat' specifications."
  5987. (should
  5988. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  5989. (org-get-repeat)))
  5990. (should-not
  5991. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  5992. (org-get-repeat)))
  5993. ;; Return proper repeat string.
  5994. (should
  5995. (equal "+2y"
  5996. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  5997. (org-get-repeat))))
  5998. ;; Prevent false positive (commented or verbatim time stamps)
  5999. (should-not
  6000. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  6001. (org-get-repeat)))
  6002. (should-not
  6003. (org-test-with-temp-text
  6004. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  6005. (org-get-repeat)))
  6006. ;; Return nil when called before first heading.
  6007. (should-not
  6008. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  6009. (org-get-repeat)))
  6010. ;; When called with an optional argument, extract repeater from that
  6011. ;; string instead.
  6012. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  6013. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  6014. (ert-deftest test-org/timestamp-format ()
  6015. "Test `org-timestamp-format' specifications."
  6016. ;; Regular test.
  6017. (should
  6018. (equal
  6019. "2012-03-29 16:40"
  6020. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6021. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  6022. ;; Range end.
  6023. (should
  6024. (equal
  6025. "2012-03-29"
  6026. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  6027. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  6028. (ert-deftest test-org/timestamp-split-range ()
  6029. "Test `org-timestamp-split-range' specifications."
  6030. ;; Extract range start (active).
  6031. (should
  6032. (equal '(2012 3 29)
  6033. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6034. (let ((ts (org-timestamp-split-range (org-element-context))))
  6035. (mapcar (lambda (p) (org-element-property p ts))
  6036. '(:year-end :month-end :day-end))))))
  6037. ;; Extract range start (inactive)
  6038. (should
  6039. (equal '(2012 3 29)
  6040. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6041. (let ((ts (org-timestamp-split-range (org-element-context))))
  6042. (mapcar (lambda (p) (org-element-property p ts))
  6043. '(:year-end :month-end :day-end))))))
  6044. ;; Extract range end (active).
  6045. (should
  6046. (equal '(2012 3 30)
  6047. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6048. (let ((ts (org-timestamp-split-range
  6049. (org-element-context) t)))
  6050. (mapcar (lambda (p) (org-element-property p ts))
  6051. '(:year-end :month-end :day-end))))))
  6052. ;; Extract range end (inactive)
  6053. (should
  6054. (equal '(2012 3 30)
  6055. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6056. (let ((ts (org-timestamp-split-range
  6057. (org-element-context) t)))
  6058. (mapcar (lambda (p) (org-element-property p ts))
  6059. '(:year-end :month-end :day-end))))))
  6060. ;; Return the timestamp if not a range.
  6061. (should
  6062. (org-test-with-temp-text "[2012-03-29 Thu]"
  6063. (let* ((ts-orig (org-element-context))
  6064. (ts-copy (org-timestamp-split-range ts-orig)))
  6065. (eq ts-orig ts-copy))))
  6066. (should
  6067. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6068. (let* ((ts-orig (org-element-context))
  6069. (ts-copy (org-timestamp-split-range ts-orig)))
  6070. (eq ts-orig ts-copy)))))
  6071. (ert-deftest test-org/timestamp-translate ()
  6072. "Test `org-timestamp-translate' specifications."
  6073. ;; Translate whole date range.
  6074. (should
  6075. (equal "<29>--<30>"
  6076. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6077. (let ((org-display-custom-times t)
  6078. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6079. (org-timestamp-translate (org-element-context))))))
  6080. ;; Translate date range start.
  6081. (should
  6082. (equal "<29>"
  6083. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6084. (let ((org-display-custom-times t)
  6085. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6086. (org-timestamp-translate (org-element-context) 'start)))))
  6087. ;; Translate date range end.
  6088. (should
  6089. (equal "<30>"
  6090. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6091. (let ((org-display-custom-times t)
  6092. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6093. (org-timestamp-translate (org-element-context) 'end)))))
  6094. ;; Translate time range.
  6095. (should
  6096. (equal "<08>--<16>"
  6097. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  6098. (let ((org-display-custom-times t)
  6099. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  6100. (org-timestamp-translate (org-element-context))))))
  6101. ;; Translate non-range timestamp.
  6102. (should
  6103. (equal "<29>"
  6104. (org-test-with-temp-text "<2012-03-29 Thu>"
  6105. (let ((org-display-custom-times t)
  6106. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6107. (org-timestamp-translate (org-element-context))))))
  6108. ;; Do not change `diary' timestamps.
  6109. (should
  6110. (equal "<%%(org-float t 4 2)>"
  6111. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6112. (let ((org-display-custom-times t)
  6113. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6114. (org-timestamp-translate (org-element-context)))))))
  6115. ;;; Visibility
  6116. (ert-deftest test-org/flag-drawer ()
  6117. "Test `org-flag-drawer' specifications."
  6118. ;; Hide drawer.
  6119. (should
  6120. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6121. (org-flag-drawer t)
  6122. (get-char-property (line-end-position) 'invisible)))
  6123. ;; Show drawer.
  6124. (should-not
  6125. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6126. (org-flag-drawer t)
  6127. (org-flag-drawer nil)
  6128. (get-char-property (line-end-position) 'invisible)))
  6129. ;; Test optional argument.
  6130. (should
  6131. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6132. (let ((drawer (save-excursion (search-forward ":D2")
  6133. (org-element-at-point))))
  6134. (org-flag-drawer t drawer)
  6135. (get-char-property (progn (search-forward ":D2") (line-end-position))
  6136. 'invisible))))
  6137. (should-not
  6138. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6139. (let ((drawer (save-excursion (search-forward ":D2")
  6140. (org-element-at-point))))
  6141. (org-flag-drawer t drawer)
  6142. (get-char-property (line-end-position) 'invisible))))
  6143. ;; Do not hide fake drawers.
  6144. (should-not
  6145. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  6146. (forward-line 1)
  6147. (org-flag-drawer t)
  6148. (get-char-property (line-end-position) 'invisible)))
  6149. ;; Do not hide incomplete drawers.
  6150. (should-not
  6151. (org-test-with-temp-text ":D:\nparagraph"
  6152. (forward-line 1)
  6153. (org-flag-drawer t)
  6154. (get-char-property (line-end-position) 'invisible)))
  6155. ;; Do not hide drawers when called from final blank lines.
  6156. (should-not
  6157. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  6158. (goto-char (point-max))
  6159. (org-flag-drawer t)
  6160. (goto-char (point-min))
  6161. (get-char-property (line-end-position) 'invisible)))
  6162. ;; Don't leave point in an invisible part of the buffer when hiding
  6163. ;; a drawer away.
  6164. (should-not
  6165. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6166. (goto-char (point-max))
  6167. (org-flag-drawer t)
  6168. (get-char-property (point) 'invisible))))
  6169. (ert-deftest test-org/hide-block-toggle ()
  6170. "Test `org-hide-block-toggle' specifications."
  6171. ;; Error when not at a block.
  6172. (should-error
  6173. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  6174. (org-hide-block-toggle 'off)
  6175. (get-char-property (line-end-position) 'invisible)))
  6176. ;; Hide block.
  6177. (should
  6178. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  6179. (org-hide-block-toggle)
  6180. (get-char-property (line-end-position) 'invisible)))
  6181. (should
  6182. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  6183. (org-hide-block-toggle)
  6184. (get-char-property (line-end-position) 'invisible)))
  6185. ;; Show block unconditionally when optional argument is `off'.
  6186. (should-not
  6187. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6188. (org-hide-block-toggle)
  6189. (org-hide-block-toggle 'off)
  6190. (get-char-property (line-end-position) 'invisible)))
  6191. (should-not
  6192. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6193. (org-hide-block-toggle 'off)
  6194. (get-char-property (line-end-position) 'invisible)))
  6195. ;; Hide block unconditionally when optional argument is non-nil.
  6196. (should
  6197. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6198. (org-hide-block-toggle t)
  6199. (get-char-property (line-end-position) 'invisible)))
  6200. (should
  6201. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6202. (org-hide-block-toggle)
  6203. (org-hide-block-toggle t)
  6204. (get-char-property (line-end-position) 'invisible)))
  6205. ;; Do not hide block when called from final blank lines.
  6206. (should-not
  6207. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  6208. (org-hide-block-toggle)
  6209. (goto-char (point-min))
  6210. (get-char-property (line-end-position) 'invisible)))
  6211. ;; Don't leave point in an invisible part of the buffer when hiding
  6212. ;; a block away.
  6213. (should-not
  6214. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  6215. (org-hide-block-toggle)
  6216. (get-char-property (point) 'invisible))))
  6217. (ert-deftest test-org/hide-block-toggle-maybe ()
  6218. "Test `org-hide-block-toggle-maybe' specifications."
  6219. (should
  6220. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  6221. (org-hide-block-toggle-maybe)))
  6222. (should-not
  6223. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  6224. (ert-deftest test-org/set-tags ()
  6225. "Test `org-set-tags' specifications."
  6226. ;; Tags set via fast-tag-selection should be visible afterwards
  6227. (should
  6228. (let ((org-tag-alist '(("NEXT" . ?n)))
  6229. (org-fast-tag-selection-single-key t))
  6230. (cl-letf (((symbol-function 'read-char-exclusive) (lambda () ?n))
  6231. ((symbol-function 'window-width) (lambda (&rest args) 100)))
  6232. (org-test-with-temp-text "<point>* Headline\nAnd its content\n* And another headline\n\nWith some content"
  6233. ;; Show only headlines
  6234. (org-content)
  6235. ;; Set NEXT tag on current entry
  6236. (org-set-tags nil nil)
  6237. ;; Move point to that NEXT tag
  6238. (search-forward "NEXT") (backward-word)
  6239. ;; And it should be visible (i.e. no overlays)
  6240. (not (overlays-at (point))))))))
  6241. (ert-deftest test-org/show-set-visibility ()
  6242. "Test `org-show-set-visibility' specifications."
  6243. ;; Do not throw an error before first heading.
  6244. (should
  6245. (org-test-with-temp-text "Preamble\n* Headline"
  6246. (org-show-set-visibility 'tree)
  6247. t))
  6248. ;; Test all visibility spans, both on headline and in entry.
  6249. (let ((list-visible-lines
  6250. (lambda (state headerp)
  6251. (org-test-with-temp-text "* Grandmother (0)
  6252. ** Uncle (1)
  6253. *** Heir (2)
  6254. ** Father (3)
  6255. Ancestor text (4)
  6256. *** Sister (5)
  6257. Sibling text (6)
  6258. *** Self (7)
  6259. Match (8)
  6260. **** First born (9)
  6261. Child text (10)
  6262. **** The other child (11)
  6263. *** Brother (12)
  6264. ** Aunt (13)
  6265. "
  6266. (org-cycle t)
  6267. (search-forward (if headerp "Self" "Match"))
  6268. (org-show-set-visibility state)
  6269. (goto-char (point-min))
  6270. (let (result (line 0))
  6271. (while (not (eobp))
  6272. (unless (org-invisible-p2) (push line result))
  6273. (incf line)
  6274. (forward-line))
  6275. (nreverse result))))))
  6276. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  6277. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  6278. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  6279. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  6280. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  6281. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  6282. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  6283. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  6284. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  6285. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  6286. (funcall list-visible-lines 'tree nil)))
  6287. (should (equal '(0 1 3 4 5 7 12 13)
  6288. (funcall list-visible-lines 'canonical t)))
  6289. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  6290. (funcall list-visible-lines 'canonical nil))))
  6291. ;; When point is hidden in a drawer or a block, make sure to make it
  6292. ;; visible.
  6293. (should-not
  6294. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  6295. (org-hide-block-toggle)
  6296. (search-forward "Text")
  6297. (org-show-set-visibility 'minimal)
  6298. (org-invisible-p2)))
  6299. (should-not
  6300. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  6301. (org-flag-drawer t)
  6302. (search-forward "Text")
  6303. (org-show-set-visibility 'minimal)
  6304. (org-invisible-p2)))
  6305. (should-not
  6306. (org-test-with-temp-text
  6307. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  6308. (org-flag-drawer t)
  6309. (forward-line -1)
  6310. (org-hide-block-toggle)
  6311. (search-forward "Text")
  6312. (org-show-set-visibility 'minimal)
  6313. (org-invisible-p2))))
  6314. (ert-deftest test-org/file-contents ()
  6315. "Test `org-file-contents' specifications."
  6316. ;; Open files.
  6317. (should
  6318. (string= "#+BIND: variable value
  6319. #+DESCRIPTION: l2
  6320. #+LANGUAGE: en
  6321. #+SELECT_TAGS: b
  6322. #+TITLE: b
  6323. #+PROPERTY: a 1
  6324. " (org-file-contents (expand-file-name "setupfile3.org"
  6325. (concat org-test-dir "examples/")))))
  6326. ;; Throw error when trying to access an invalid file.
  6327. (should-error (org-file-contents "this-file-must-not-exist"))
  6328. ;; Try to access an invalid file, but do not throw an error.
  6329. (should
  6330. (progn (org-file-contents "this-file-must-not-exist" :noerror) t))
  6331. ;; Open URL.
  6332. (should
  6333. (string= "foo"
  6334. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  6335. (unwind-protect
  6336. ;; Simulate successful retrieval of a URL.
  6337. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  6338. (lambda (&rest_)
  6339. (with-current-buffer buffer
  6340. (insert "HTTP/1.1 200 OK\n\nfoo"))
  6341. buffer)))
  6342. (org-file-contents "http://some-valid-url"))
  6343. (kill-buffer buffer)))))
  6344. ;; Throw error when trying to access an invalid URL.
  6345. (should-error
  6346. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  6347. (unwind-protect
  6348. ;; Simulate unsuccessful retrieval of a URL.
  6349. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  6350. (lambda (&rest_)
  6351. (with-current-buffer buffer
  6352. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  6353. buffer)))
  6354. (org-file-contents "http://this-url-must-not-exist"))
  6355. (kill-buffer buffer))))
  6356. ;; Try to access an invalid URL, but do not throw an error.
  6357. (should-error
  6358. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  6359. (unwind-protect
  6360. ;; Simulate unsuccessful retrieval of a URL.
  6361. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  6362. (lambda (&rest_)
  6363. (with-current-buffer buffer
  6364. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  6365. buffer)))
  6366. (org-file-contents "http://this-url-must-not-exist"))
  6367. (kill-buffer buffer))))
  6368. (should
  6369. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  6370. (unwind-protect
  6371. ;; Simulate unsuccessful retrieval of a URL.
  6372. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  6373. (lambda (&rest_)
  6374. (with-current-buffer buffer
  6375. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  6376. buffer)))
  6377. (org-file-contents "http://this-url-must-not-exist" :noerror))
  6378. (kill-buffer buffer))
  6379. t)))
  6380. (provide 'test-org)
  6381. ;;; test-org.el ends here