test-org.el 217 KB

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