test-org.el 225 KB

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