test-org.el 246 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581
  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. (eval-and-compile (require 'cl-lib))
  18. ;;; Comments
  19. (ert-deftest test-org/toggle-comment ()
  20. "Test `org-toggle-comment' specifications."
  21. ;; Simple headline.
  22. (should
  23. (equal "* Test"
  24. (org-test-with-temp-text "* COMMENT Test"
  25. (org-toggle-comment)
  26. (buffer-string))))
  27. (should
  28. (equal "* COMMENT Test"
  29. (org-test-with-temp-text "* Test"
  30. (org-toggle-comment)
  31. (buffer-string))))
  32. ;; Headline with a regular keyword.
  33. (should
  34. (equal "* TODO Test"
  35. (org-test-with-temp-text "* TODO COMMENT Test"
  36. (org-toggle-comment)
  37. (buffer-string))))
  38. (should
  39. (equal "* TODO COMMENT Test"
  40. (org-test-with-temp-text "* TODO Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. ;; Empty headline.
  44. (should
  45. (equal "* "
  46. (org-test-with-temp-text "* COMMENT"
  47. (org-toggle-comment)
  48. (buffer-string))))
  49. (should
  50. (equal "* COMMENT"
  51. (org-test-with-temp-text "* "
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. ;; Headline with a single keyword.
  55. (should
  56. (equal "* TODO "
  57. (org-test-with-temp-text "* TODO COMMENT"
  58. (org-toggle-comment)
  59. (buffer-string))))
  60. (should
  61. (equal "* TODO COMMENT"
  62. (org-test-with-temp-text "* TODO"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. ;; Headline with a keyword, a priority cookie and contents.
  66. (should
  67. (equal "* TODO [#A] Headline"
  68. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  69. (org-toggle-comment)
  70. (buffer-string))))
  71. (should
  72. (equal "* TODO [#A] COMMENT Headline"
  73. (org-test-with-temp-text "* TODO [#A] Headline"
  74. (org-toggle-comment)
  75. (buffer-string)))))
  76. (ert-deftest test-org/comment-dwim ()
  77. "Test `comment-dwim' behaviour in an Org buffer."
  78. ;; No region selected, no comment on current line and line not
  79. ;; empty: insert comment on line above.
  80. (should
  81. (equal "# \nComment"
  82. (org-test-with-temp-text "Comment"
  83. (call-interactively #'org-comment-dwim)
  84. (buffer-string))))
  85. ;; No region selected, no comment on current line and line empty:
  86. ;; insert comment on this line.
  87. (should
  88. (equal "# \nParagraph"
  89. (org-test-with-temp-text "\nParagraph"
  90. (call-interactively #'org-comment-dwim)
  91. (buffer-string))))
  92. ;; No region selected, and a comment on this line: indent it.
  93. (should
  94. (equal "* Headline\n # Comment"
  95. (org-test-with-temp-text "* Headline\n# <point>Comment"
  96. (let ((org-adapt-indentation t))
  97. (call-interactively #'org-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. (call-interactively #'org-comment-dwim)
  104. (buffer-string))))
  105. ;; Region selected and only comments and blank lines within it:
  106. ;; un-comment all commented lines.
  107. (should
  108. (equal "Comment 1\n\nComment 2"
  109. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  110. (transient-mark-mode 1)
  111. (push-mark (point) t t)
  112. (goto-char (point-max))
  113. (call-interactively #'org-comment-dwim)
  114. (buffer-string))))
  115. ;; Region selected without comments: comment all lines if
  116. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  117. (should
  118. (equal "# Comment 1\n\n# Comment 2"
  119. (org-test-with-temp-text "Comment 1\n\nComment 2"
  120. (transient-mark-mode 1)
  121. (push-mark (point) t t)
  122. (goto-char (point-max))
  123. (let ((comment-empty-lines nil))
  124. (call-interactively #'org-comment-dwim))
  125. (buffer-string))))
  126. (should
  127. (equal "# Comment 1\n# \n# Comment 2"
  128. (org-test-with-temp-text "Comment 1\n\nComment 2"
  129. (transient-mark-mode 1)
  130. (push-mark (point) t t)
  131. (goto-char (point-max))
  132. (let ((comment-empty-lines t))
  133. (call-interactively #'org-comment-dwim))
  134. (buffer-string))))
  135. ;; In front of a keyword without region, insert a new comment.
  136. (should
  137. (equal "# \n#+KEYWORD: value"
  138. (org-test-with-temp-text "#+KEYWORD: value"
  139. (call-interactively #'org-comment-dwim)
  140. (buffer-string))))
  141. ;; Comment a heading
  142. (should
  143. (equal "* COMMENT Test"
  144. (org-test-with-temp-text "* Test"
  145. (call-interactively #'org-comment-dwim)
  146. (buffer-string))))
  147. ;; In a source block, use appropriate syntax.
  148. (should
  149. (equal " ;; "
  150. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n<point>\n#+END_SRC"
  151. (let ((org-edit-src-content-indentation 2))
  152. (call-interactively #'org-comment-dwim))
  153. (buffer-substring-no-properties (line-beginning-position)
  154. (point)))))
  155. (should
  156. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  157. (org-test-with-temp-text
  158. "#+BEGIN_SRC emacs-lisp\n<point>a\nb\n#+END_SRC"
  159. (transient-mark-mode 1)
  160. (push-mark (point) t t)
  161. (forward-line 2)
  162. (let ((org-edit-src-content-indentation 2))
  163. (call-interactively #'org-comment-dwim))
  164. (buffer-string)))))
  165. ;;; Date and time analysis
  166. (ert-deftest test-org/org-read-date ()
  167. "Test `org-read-date' specifications."
  168. ;; Parse ISO date with abbreviated year and month.
  169. (should (equal "2012-03-29 16:40"
  170. (let ((org-time-was-given t))
  171. (org-read-date t nil "12-3-29 16:40"))))
  172. ;; Parse Europeans dates.
  173. (should (equal "2012-03-29 16:40"
  174. (let ((org-time-was-given t))
  175. (org-read-date t nil "29.03.2012 16:40"))))
  176. ;; Parse Europeans dates without year.
  177. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  178. (let ((org-time-was-given t))
  179. (org-read-date t nil "29.03. 16:40"))))
  180. ;; Relative date applied to current time if there is single
  181. ;; plus/minus, or to default date when there are two of them.
  182. (should
  183. (equal
  184. "2015-03-04"
  185. (cl-letf (((symbol-function 'current-time)
  186. (lambda ()
  187. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  188. (org-read-date
  189. t nil "+1y" nil
  190. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  191. (should
  192. (equal
  193. "2013-03-29"
  194. (cl-letf (((symbol-function 'current-time)
  195. (lambda ()
  196. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  197. (org-read-date
  198. t nil "++1y" nil
  199. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  200. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  201. ;; dates (relatively to now) when incomplete. Otherwise, use
  202. ;; default date.
  203. (should
  204. (equal
  205. "2014-04-01"
  206. (cl-letf (((symbol-function 'current-time)
  207. (lambda ()
  208. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  209. (let ((org-read-date-prefer-future t))
  210. (org-read-date t nil "1")))))
  211. (should
  212. (equal
  213. "2013-03-04"
  214. (cl-letf (((symbol-function 'current-time)
  215. (lambda ()
  216. (apply #'encode-time (org-parse-time-string "2012-03-29")))))
  217. (let ((org-read-date-prefer-future t))
  218. (org-read-date t nil "3-4")))))
  219. (should
  220. (equal
  221. "2012-03-04"
  222. (cl-letf (((symbol-function 'current-time)
  223. (lambda ()
  224. (apply #'encode-time (org-parse-time-string "2012-03-29")))))
  225. (let ((org-read-date-prefer-future nil))
  226. (org-read-date t nil "3-4")))))
  227. ;; When set to `org-read-date-prefer-future' is set to `time', read
  228. ;; day is moved to tomorrow if specified hour is before current
  229. ;; time. However, it only happens in no other part of the date is
  230. ;; specified.
  231. (should
  232. (equal
  233. "2012-03-30"
  234. (cl-letf (((symbol-function 'current-time)
  235. (lambda ()
  236. (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
  237. (let ((org-read-date-prefer-future 'time))
  238. (org-read-date t nil "00:40" nil)))))
  239. (should-not
  240. (equal
  241. "2012-03-30"
  242. (cl-letf (((symbol-function 'current-time)
  243. (lambda ()
  244. (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
  245. (let ((org-read-date-prefer-future 'time))
  246. (org-read-date t nil "29 00:40" nil)))))
  247. ;; Caveat: `org-read-date-prefer-future' always refers to current
  248. ;; time, not default time, when they differ.
  249. (should
  250. (equal
  251. "2014-04-01"
  252. (cl-letf (((symbol-function 'current-time)
  253. (lambda ()
  254. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  255. (let ((org-read-date-prefer-future t))
  256. (org-read-date
  257. t nil "1" nil
  258. (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
  259. (should
  260. (equal
  261. "2014-03-25"
  262. (cl-letf (((symbol-function 'current-time)
  263. (lambda ()
  264. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  265. (let ((org-read-date-prefer-future t))
  266. (org-read-date
  267. t nil "25" nil
  268. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
  269. (ert-deftest test-org/org-parse-time-string ()
  270. "Test `org-parse-time-string'."
  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 16:40>")
  276. '(0 40 16 29 3 2012 nil nil nil)))
  277. (should (equal (org-parse-time-string "<2012-03-29>")
  278. '(0 0 0 29 3 2012 nil nil nil)))
  279. (should (equal (org-parse-time-string "<2012-03-29>" t)
  280. '(0 nil nil 29 3 2012 nil nil nil))))
  281. (ert-deftest test-org/closest-date ()
  282. "Test `org-closest-date' specifications."
  283. (require 'calendar)
  284. ;; Time stamps without a repeater are returned unchanged.
  285. (should
  286. (equal
  287. '(3 29 2012)
  288. (calendar-gregorian-from-absolute
  289. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  290. ;; Time stamps with a null repeater are returned unchanged.
  291. (should
  292. (equal
  293. '(3 29 2012)
  294. (calendar-gregorian-from-absolute
  295. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  296. ;; if PREFER is set to `past' always return a date before, or equal
  297. ;; to CURRENT.
  298. (should
  299. (equal
  300. '(3 1 2014)
  301. (calendar-gregorian-from-absolute
  302. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  303. (should
  304. (equal
  305. '(3 4 2014)
  306. (calendar-gregorian-from-absolute
  307. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  308. ;; if PREFER is set to `future' always return a date before, or equal
  309. ;; to CURRENT.
  310. (should
  311. (equal
  312. '(3 29 2014)
  313. (calendar-gregorian-from-absolute
  314. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  315. (should
  316. (equal
  317. '(3 4 2014)
  318. (calendar-gregorian-from-absolute
  319. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  320. ;; If PREFER is neither `past' nor `future', select closest date.
  321. (should
  322. (equal
  323. '(3 1 2014)
  324. (calendar-gregorian-from-absolute
  325. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  326. (should
  327. (equal
  328. '(5 4 2014)
  329. (calendar-gregorian-from-absolute
  330. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  331. ;; Test "day" repeater.
  332. (should
  333. (equal '(3 8 2014)
  334. (calendar-gregorian-from-absolute
  335. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  336. (should
  337. (equal '(3 10 2014)
  338. (calendar-gregorian-from-absolute
  339. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  340. ;; Test "month" repeater.
  341. (should
  342. (equal '(1 5 2015)
  343. (calendar-gregorian-from-absolute
  344. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  345. (should
  346. (equal '(3 29 2014)
  347. (calendar-gregorian-from-absolute
  348. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  349. ;; Test "year" repeater.
  350. (should
  351. (equal '(3 5 2014)
  352. (calendar-gregorian-from-absolute
  353. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  354. (should
  355. (equal '(3 29 2014)
  356. (calendar-gregorian-from-absolute
  357. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  358. (ert-deftest test-org/deadline-close-p ()
  359. "Test `org-deadline-close-p' specifications."
  360. ;; Pretend that the current time is 2016-06-03 Fri 01:43
  361. (cl-letf (((symbol-function 'current-time)
  362. (lambda ()
  363. (apply #'encode-time
  364. (org-parse-time-string "2016-06-03 Fri 01:43")))))
  365. ;; Timestamps are close if they are within `ndays' of lead time.
  366. (org-test-with-temp-text "* Heading"
  367. (should (org-deadline-close-p "2016-06-03 Fri" 0))
  368. (should (org-deadline-close-p "2016-06-02 Thu" 0))
  369. (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
  370. (should (org-deadline-close-p "2016-06-04 Sat" 1))
  371. (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
  372. ;; Read `ndays' from timestamp if argument not given.
  373. (org-test-with-temp-text "* H"
  374. (should (org-deadline-close-p "2016-06-04 Sat -1d"))
  375. (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
  376. (should (org-deadline-close-p "2016-06-10 Fri -1w"))
  377. (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
  378. ;; Prefer `ndays' argument over lead time in timestamp.
  379. (org-test-with-temp-text "* H"
  380. (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
  381. (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
  382. ;; Completed tasks are never close.
  383. (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
  384. (org-test-with-temp-text "* TODO Heading"
  385. (should (org-deadline-close-p "2016-06-03")))
  386. (org-test-with-temp-text "* DONE Heading"
  387. (should-not (org-deadline-close-p "2016-06-03"))))))
  388. ;;; Drawers
  389. (ert-deftest test-org/insert-property-drawer ()
  390. "Test `org-insert-property-drawer' specifications."
  391. ;; Error before first headline.
  392. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  393. ;; Insert drawer right after headline if there is no planning line,
  394. ;; or after it otherwise.
  395. (should
  396. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  397. (org-test-with-temp-text "* H\nParagraph<point>"
  398. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  399. (buffer-string))))
  400. (should
  401. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  402. (org-test-with-temp-text
  403. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  404. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  405. (buffer-string))))
  406. ;; Indent inserted drawer.
  407. (should
  408. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  409. (org-test-with-temp-text "* H\nParagraph<point>"
  410. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  411. (buffer-string))))
  412. ;; Handle insertion at eob.
  413. (should
  414. (equal "* H\n:PROPERTIES:\n:END:\n"
  415. (org-test-with-temp-text "* H"
  416. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  417. (buffer-string))))
  418. ;; Skip inlinetasks before point.
  419. (when (featurep 'org-inlinetask)
  420. (should
  421. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  422. (org-test-with-temp-text
  423. "* H\n*************** I\n*************** END\nP<point>"
  424. (let ((org-adapt-indentation nil)
  425. (org-inlinetask-min-level 15))
  426. (org-insert-property-drawer))
  427. (buffer-string)))))
  428. ;; Correctly set drawer in an inlinetask.
  429. (when (featurep 'org-inlinetask)
  430. (should
  431. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  432. (org-test-with-temp-text
  433. "* H\n*************** I\nP<point>\n*************** END"
  434. (let ((org-adapt-indentation nil)
  435. (org-inlinetask-min-level 15))
  436. (org-insert-property-drawer))
  437. (buffer-string))))))
  438. ;;; Filling
  439. (ert-deftest test-org/fill-element ()
  440. "Test `org-fill-element' specifications."
  441. ;; At an Org table, align it.
  442. (should
  443. (equal "| a |\n"
  444. (org-test-with-temp-text "|a|"
  445. (org-fill-element)
  446. (buffer-string))))
  447. (should
  448. (equal "#+name: table\n| a |\n"
  449. (org-test-with-temp-text "#+name: table\n| a |\n"
  450. (org-fill-element)
  451. (buffer-string))))
  452. ;; At a paragraph, preserve line breaks.
  453. (org-test-with-temp-text "some \\\\\nlong\ntext"
  454. (let ((fill-column 20))
  455. (org-fill-element)
  456. (should (equal (buffer-string) "some \\\\\nlong text"))))
  457. ;; Correctly fill a paragraph when point is at its very end.
  458. (should
  459. (equal "A B"
  460. (org-test-with-temp-text "A\nB"
  461. (let ((fill-column 20))
  462. (goto-char (point-max))
  463. (org-fill-element)
  464. (buffer-string)))))
  465. ;; Correctly fill the last paragraph of a greater element.
  466. (should
  467. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  468. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  469. (let ((fill-column 8))
  470. (forward-line)
  471. (end-of-line)
  472. (org-fill-element)
  473. (buffer-string)))))
  474. ;; Correctly fill an element in a narrowed buffer.
  475. (should
  476. (equal "01234\n6"
  477. (org-test-with-temp-text "01234 6789"
  478. (let ((fill-column 5))
  479. (narrow-to-region 1 8)
  480. (org-fill-element)
  481. (buffer-string)))))
  482. ;; Handle `adaptive-fill-regexp' in paragraphs.
  483. (should
  484. (equal "> a b"
  485. (org-test-with-temp-text "> a\n> b"
  486. (let ((fill-column 5)
  487. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  488. (org-fill-element)
  489. (buffer-string)))))
  490. ;; Special case: Fill first paragraph when point is at an item or
  491. ;; a plain-list or a footnote reference.
  492. (should
  493. (equal "- A B"
  494. (org-test-with-temp-text "- A\n B"
  495. (let ((fill-column 20))
  496. (org-fill-element)
  497. (buffer-string)))))
  498. (should
  499. (equal "[fn:1] A B"
  500. (org-test-with-temp-text "[fn:1] A\nB"
  501. (let ((fill-column 20))
  502. (org-fill-element)
  503. (buffer-string)))))
  504. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  505. (let ((fill-column 20))
  506. (org-fill-element)
  507. (should (equal (buffer-string)
  508. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  509. ;; Fill contents of `comment-block' elements.
  510. (should
  511. (equal
  512. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  513. (let ((fill-column 20))
  514. (forward-line)
  515. (org-fill-element)
  516. (buffer-string)))
  517. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  518. ;; Fill `comment' elements.
  519. (should
  520. (equal " # A B"
  521. (org-test-with-temp-text " # A\n # B"
  522. (let ((fill-column 20))
  523. (org-fill-element)
  524. (buffer-string)))))
  525. ;; Do not mix consecutive comments when filling one of them.
  526. (should
  527. (equal "# A B\n\n# C"
  528. (org-test-with-temp-text "# A\n# B\n\n# C"
  529. (let ((fill-column 20))
  530. (org-fill-element)
  531. (buffer-string)))))
  532. ;; Use commented empty lines as separators when filling comments.
  533. (should
  534. (equal "# A B\n#\n# C"
  535. (org-test-with-temp-text "# A\n# B\n#\n# C"
  536. (let ((fill-column 20))
  537. (org-fill-element)
  538. (buffer-string)))))
  539. ;; Handle `adaptive-fill-regexp' in comments.
  540. (should
  541. (equal "# > a b"
  542. (org-test-with-temp-text "# > a\n# > b"
  543. (let ((fill-column 20)
  544. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  545. (org-fill-element)
  546. (buffer-string)))))
  547. ;; Do nothing at affiliated keywords.
  548. (should
  549. (equal "#+NAME: para\nSome\ntext."
  550. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  551. (let ((fill-column 20))
  552. (org-fill-element)
  553. (buffer-string)))))
  554. ;; Do not move point after table when filling a table.
  555. (should-not
  556. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  557. (forward-char)
  558. (org-fill-element)
  559. (eobp)))
  560. ;; Do not fill "n" macro, with or without arguments, followed by
  561. ;; a dot or a closing parenthesis since it could be confused with
  562. ;; a numbered bullet.
  563. (should-not
  564. (equal "123456789\n{{{n}}}."
  565. (org-test-with-temp-text "123456789 {{{n}}}."
  566. (let ((fill-column 10))
  567. (org-fill-element)
  568. (buffer-string)))))
  569. (should-not
  570. (equal "123456789\n{{{n}}}\)"
  571. (org-test-with-temp-text "123456789 {{{n}}}\)"
  572. (let ((fill-column 10))
  573. (org-fill-element)
  574. (buffer-string)))))
  575. (should-not
  576. (equal "123456789\n{{{n()}}}."
  577. (org-test-with-temp-text "123456789 {{{n()}}}."
  578. (let ((fill-column 10))
  579. (org-fill-element)
  580. (buffer-string)))))
  581. (should-not
  582. (equal "123456789\n{{{n(counter)}}}."
  583. (org-test-with-temp-text "123456789 {{{n(counter)}}}."
  584. (let ((fill-column 10))
  585. (org-fill-element)
  586. (buffer-string))))))
  587. (ert-deftest test-org/auto-fill-function ()
  588. "Test auto-filling features."
  589. ;; Auto fill paragraph.
  590. (should
  591. (equal "12345\n7890"
  592. (org-test-with-temp-text "12345 7890"
  593. (let ((fill-column 5))
  594. (end-of-line)
  595. (org-auto-fill-function)
  596. (buffer-string)))))
  597. ;; Auto fill first paragraph in an item.
  598. (should
  599. (equal "- 12345\n 7890"
  600. (org-test-with-temp-text "- 12345 7890"
  601. (let ((fill-column 7))
  602. (end-of-line)
  603. (org-auto-fill-function)
  604. (buffer-string)))))
  605. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  606. (should
  607. (equal "> 12345\n 7890"
  608. (org-test-with-temp-text "> 12345 7890"
  609. (let ((fill-column 10)
  610. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  611. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  612. (end-of-line)
  613. (org-auto-fill-function)
  614. (buffer-string)))))
  615. (should
  616. (equal "> 12345\n> 12345\n> 7890"
  617. (org-test-with-temp-text "> 12345\n> 12345 7890"
  618. (let ((fill-column 10)
  619. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  620. (goto-char (point-max))
  621. (org-auto-fill-function)
  622. (buffer-string)))))
  623. (should-not
  624. (equal " 12345\n *12345\n *12345"
  625. (org-test-with-temp-text " 12345\n *12345 12345"
  626. (let ((fill-column 10)
  627. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  628. (goto-char (point-max))
  629. (org-auto-fill-function)
  630. (buffer-string)))))
  631. ;; Auto fill comments.
  632. (should
  633. (equal " # 12345\n # 7890"
  634. (org-test-with-temp-text " # 12345 7890"
  635. (let ((fill-column 10))
  636. (end-of-line)
  637. (org-auto-fill-function)
  638. (buffer-string)))))
  639. ;; A hash within a line isn't a comment.
  640. (should-not
  641. (equal "12345 # 7890\n# 1"
  642. (org-test-with-temp-text "12345 # 7890 1"
  643. (let ((fill-column 12))
  644. (end-of-line)
  645. (org-auto-fill-function)
  646. (buffer-string)))))
  647. ;; Correctly interpret empty prefix.
  648. (should-not
  649. (equal "# a\n# b\nRegular\n# paragraph"
  650. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  651. (let ((fill-column 12))
  652. (end-of-line 3)
  653. (org-auto-fill-function)
  654. (buffer-string)))))
  655. ;; Comment block: auto fill contents.
  656. (should
  657. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  658. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  659. (let ((fill-column 5))
  660. (forward-line)
  661. (end-of-line)
  662. (org-auto-fill-function)
  663. (buffer-string)))))
  664. (should
  665. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  666. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  667. (let ((fill-column 5))
  668. (forward-line)
  669. (end-of-line)
  670. (org-auto-fill-function)
  671. (buffer-string)))))
  672. ;; Do not fill if a new item could be created.
  673. (should-not
  674. (equal "12345\n- 90"
  675. (org-test-with-temp-text "12345 - 90"
  676. (let ((fill-column 5))
  677. (end-of-line)
  678. (org-auto-fill-function)
  679. (buffer-string)))))
  680. ;; Do not fill if a line break could be introduced.
  681. (should-not
  682. (equal "123\\\\\n7890"
  683. (org-test-with-temp-text "123\\\\ 7890"
  684. (let ((fill-column 6))
  685. (end-of-line)
  686. (org-auto-fill-function)
  687. (buffer-string)))))
  688. ;; Do not fill affiliated keywords.
  689. (should-not
  690. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  691. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  692. (let ((fill-column 20))
  693. (end-of-line)
  694. (org-auto-fill-function)
  695. (buffer-string))))))
  696. ;;; Indentation
  697. (ert-deftest test-org/indent-line ()
  698. "Test `org-indent-line' specifications."
  699. ;; Do not indent diary sexps, footnote definitions or headlines.
  700. (should
  701. (zerop
  702. (org-test-with-temp-text "%%(org-calendar-holiday)"
  703. (org-indent-line)
  704. (org-get-indentation))))
  705. (should
  706. (zerop
  707. (org-test-with-temp-text "[fn:1] fn"
  708. (let ((org-adapt-indentation t)) (org-indent-line))
  709. (org-get-indentation))))
  710. (should
  711. (zerop
  712. (org-test-with-temp-text "* H"
  713. (org-indent-line)
  714. (org-get-indentation))))
  715. ;; Do not indent before first headline.
  716. (should
  717. (zerop
  718. (org-test-with-temp-text ""
  719. (org-indent-line)
  720. (org-get-indentation))))
  721. ;; Indent according to headline level otherwise, unless
  722. ;; `org-adapt-indentation' is nil.
  723. (should
  724. (= 2
  725. (org-test-with-temp-text "* H\n<point>A"
  726. (let ((org-adapt-indentation t)) (org-indent-line))
  727. (org-get-indentation))))
  728. (should
  729. (= 2
  730. (org-test-with-temp-text "* H\n<point>\nA"
  731. (let ((org-adapt-indentation t)) (org-indent-line))
  732. (org-get-indentation))))
  733. (should
  734. (zerop
  735. (org-test-with-temp-text "* H\n<point>A"
  736. (let ((org-adapt-indentation nil)) (org-indent-line))
  737. (org-get-indentation))))
  738. ;; Indenting preserves point position.
  739. (should
  740. (org-test-with-temp-text "* H\nA<point>B"
  741. (let ((org-adapt-indentation t)) (org-indent-line))
  742. (looking-at "B")))
  743. ;; Do not change indentation at an item or a LaTeX environment.
  744. (should
  745. (= 1
  746. (org-test-with-temp-text "* H\n<point> - A"
  747. (let ((org-adapt-indentation t)) (org-indent-line))
  748. (org-get-indentation))))
  749. (should
  750. (= 1
  751. (org-test-with-temp-text
  752. "\\begin{equation}\n <point>1+1=2\n\\end{equation}"
  753. (org-indent-line)
  754. (org-get-indentation))))
  755. ;; On blank lines at the end of a list, indent like last element
  756. ;; within it if the line is still in the list. If the last element
  757. ;; is an item, indent like its contents. Otherwise, indent like the
  758. ;; whole list.
  759. (should
  760. (= 4
  761. (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
  762. (let ((org-adapt-indentation t)) (org-indent-line))
  763. (org-get-indentation))))
  764. (should
  765. (= 4
  766. (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
  767. (let ((org-adapt-indentation t)) (org-indent-line))
  768. (org-get-indentation))))
  769. (should
  770. (zerop
  771. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
  772. (let ((org-adapt-indentation t)) (org-indent-line))
  773. (org-get-indentation))))
  774. (should
  775. (= 4
  776. (org-test-with-temp-text "* H\n- A\n - \n<point>"
  777. (let ((org-adapt-indentation t)) (org-indent-line))
  778. (org-get-indentation))))
  779. (should
  780. (= 4
  781. (org-test-with-temp-text
  782. "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
  783. (let ((org-adapt-indentation t)) (org-indent-line))
  784. (org-get-indentation))))
  785. (should
  786. (= 2
  787. (org-test-with-temp-text "- A\n B\n\n<point>"
  788. (let ((org-adapt-indentation nil)) (org-indent-line))
  789. (org-get-indentation))))
  790. (should
  791. (= 2
  792. (org-test-with-temp-text
  793. "- A\n \begin{cases} 1 + 1\n \end{cases}\n\n<point>"
  794. (let ((org-adapt-indentation nil)) (org-indent-line))
  795. (org-get-indentation))))
  796. ;; Likewise, on a blank line at the end of a footnote definition,
  797. ;; indent at column 0 if line belongs to the definition. Otherwise,
  798. ;; indent like the definition itself.
  799. (should
  800. (zerop
  801. (org-test-with-temp-text "* H\n[fn:1] Definition\n<point>"
  802. (let ((org-adapt-indentation t)) (org-indent-line))
  803. (org-get-indentation))))
  804. (should
  805. (zerop
  806. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n<point>"
  807. (let ((org-adapt-indentation t)) (org-indent-line))
  808. (org-get-indentation))))
  809. ;; After the end of the contents of a greater element, indent like
  810. ;; the beginning of the element.
  811. (should
  812. (= 1
  813. (org-test-with-temp-text
  814. " #+BEGIN_CENTER\n Contents\n<point>#+END_CENTER"
  815. (org-indent-line)
  816. (org-get-indentation))))
  817. ;; On blank lines after a paragraph, indent like its last non-empty
  818. ;; line.
  819. (should
  820. (= 1
  821. (org-test-with-temp-text " Paragraph\n\n<point>"
  822. (org-indent-line)
  823. (org-get-indentation))))
  824. ;; At the first line of an element, indent like previous element's
  825. ;; first line, ignoring footnotes definitions and inline tasks, or
  826. ;; according to parent.
  827. (should
  828. (= 2
  829. (org-test-with-temp-text "A\n\n B\n\nC<point>"
  830. (org-indent-line)
  831. (org-get-indentation))))
  832. (should
  833. (= 1
  834. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC<point>"
  835. (org-indent-line)
  836. (org-get-indentation))))
  837. (should
  838. (= 1
  839. (org-test-with-temp-text
  840. " #+BEGIN_CENTER\n<point> Contents\n#+END_CENTER"
  841. (org-indent-line)
  842. (org-get-indentation))))
  843. ;; Within code part of a source block, use language major mode if
  844. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  845. ;; according to line above.
  846. (should
  847. (= 6
  848. (org-test-with-temp-text
  849. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  850. (let ((org-src-tab-acts-natively t)
  851. (org-edit-src-content-indentation 0))
  852. (org-indent-line))
  853. (org-get-indentation))))
  854. (should
  855. (= 1
  856. (org-test-with-temp-text
  857. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  858. (let ((org-src-tab-acts-natively nil)
  859. (org-edit-src-content-indentation 0))
  860. (org-indent-line))
  861. (org-get-indentation))))
  862. ;; Otherwise, indent like the first non-blank line above.
  863. (should
  864. (zerop
  865. (org-test-with-temp-text
  866. "#+BEGIN_CENTER\nline1\n\n<point> line2\n#+END_CENTER"
  867. (org-indent-line)
  868. (org-get-indentation))))
  869. ;; Align node properties according to `org-property-format'. Handle
  870. ;; nicely empty values.
  871. (should
  872. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  873. (org-test-with-temp-text
  874. "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  875. (let ((org-property-format "%-10s %s")) (org-indent-line))
  876. (buffer-string))))
  877. (should
  878. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  879. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  880. (let ((org-property-format "%-10s %s")) (org-indent-line))
  881. (buffer-string)))))
  882. (ert-deftest test-org/indent-region ()
  883. "Test `org-indent-region' specifications."
  884. ;; Indent paragraph.
  885. (should
  886. (equal "A\nB\nC"
  887. (org-test-with-temp-text " A\nB\n C"
  888. (org-indent-region (point-min) (point-max))
  889. (buffer-string))))
  890. ;; Indent greater elements along with their contents.
  891. (should
  892. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  893. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  894. (org-indent-region (point-min) (point-max))
  895. (buffer-string))))
  896. ;; Ignore contents of verse blocks. Only indent block delimiters.
  897. (should
  898. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  899. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  900. (org-indent-region (point-min) (point-max))
  901. (buffer-string))))
  902. (should
  903. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  904. (org-test-with-temp-text " #+BEGIN_VERSE\n A\n B\n #+END_VERSE"
  905. (org-indent-region (point-min) (point-max))
  906. (buffer-string))))
  907. ;; Indent example blocks as a single block, unless indentation
  908. ;; should be preserved. In this case only indent the block markers.
  909. (should
  910. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  911. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  912. (org-indent-region (point-min) (point-max))
  913. (buffer-string))))
  914. (should
  915. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  916. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  917. (org-indent-region (point-min) (point-max))
  918. (buffer-string))))
  919. (should
  920. (equal "#+BEGIN_EXAMPLE -i\n A\n B\n#+END_EXAMPLE"
  921. (org-test-with-temp-text
  922. " #+BEGIN_EXAMPLE -i\n A\n B\n #+END_EXAMPLE"
  923. (org-indent-region (point-min) (point-max))
  924. (buffer-string))))
  925. (should
  926. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  927. (org-test-with-temp-text
  928. " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  929. (let ((org-src-preserve-indentation t))
  930. (org-indent-region (point-min) (point-max)))
  931. (buffer-string))))
  932. ;; Treat export blocks as a whole.
  933. (should
  934. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  935. (org-test-with-temp-text "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  936. (org-indent-region (point-min) (point-max))
  937. (buffer-string))))
  938. (should
  939. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  940. (org-test-with-temp-text
  941. " #+BEGIN_EXPORT latex\n A\n B\n #+END_EXPORT"
  942. (org-indent-region (point-min) (point-max))
  943. (buffer-string))))
  944. ;; Indent according to mode if `org-src-tab-acts-natively' is
  945. ;; non-nil. Otherwise, do not indent code at all.
  946. (should
  947. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  948. (org-test-with-temp-text
  949. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  950. (let ((org-src-tab-acts-natively t)
  951. (org-edit-src-content-indentation 0))
  952. (org-indent-region (point-min) (point-max)))
  953. (buffer-string))))
  954. (should
  955. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  956. (org-test-with-temp-text
  957. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  958. (let ((org-src-tab-acts-natively nil)
  959. (org-edit-src-content-indentation 0))
  960. (org-indent-region (point-min) (point-max)))
  961. (buffer-string))))
  962. ;; Align node properties according to `org-property-format'. Handle
  963. ;; nicely empty values.
  964. (should
  965. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  966. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  967. (let ((org-property-format "%-10s %s")
  968. (org-adapt-indentation nil))
  969. (org-indent-region (point) (point-max)))
  970. (buffer-string))))
  971. (should
  972. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  973. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  974. (let ((org-property-format "%-10s %s")
  975. (org-adapt-indentation nil))
  976. (org-indent-region (point) (point-max)))
  977. (buffer-string))))
  978. ;; Indent plain lists.
  979. (should
  980. (equal "- A\n B\n - C\n\n D"
  981. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  982. (org-indent-region (point-min) (point-max))
  983. (buffer-string))))
  984. (should
  985. (equal "- A\n\n- B"
  986. (org-test-with-temp-text " - A\n\n - B"
  987. (org-indent-region (point-min) (point-max))
  988. (buffer-string))))
  989. ;; Indent footnote definitions.
  990. (should
  991. (equal "[fn:1] Definition\n\nDefinition"
  992. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  993. (org-indent-region (point-min) (point-max))
  994. (buffer-string))))
  995. ;; Special case: Start indenting on a blank line.
  996. (should
  997. (equal "\nParagraph"
  998. (org-test-with-temp-text "\n Paragraph"
  999. (org-indent-region (point-min) (point-max))
  1000. (buffer-string)))))
  1001. ;;; Editing
  1002. (ert-deftest test-org/delete-indentation ()
  1003. "Test `org-delete-indentation' specifications."
  1004. ;; Regular test.
  1005. (should (equal "foo bar"
  1006. (org-test-with-temp-text
  1007. "foo \n bar<point>"
  1008. (org-delete-indentation)
  1009. (buffer-string))))
  1010. ;; With optional argument.
  1011. (should (equal "foo bar"
  1012. (org-test-with-temp-text
  1013. "foo<point> \n bar"
  1014. (org-delete-indentation t)
  1015. (buffer-string))))
  1016. ;; At headline text should be appended to the headline text.
  1017. (should
  1018. (equal"* foo bar :tag:"
  1019. (let (org-auto-align-tags)
  1020. (org-test-with-temp-text
  1021. "* foo :tag:\n bar<point>"
  1022. (org-delete-indentation)
  1023. (buffer-string)))))
  1024. (should
  1025. (equal "* foo bar :tag:"
  1026. (let (org-auto-align-tags)
  1027. (org-test-with-temp-text
  1028. "* foo <point>:tag:\n bar"
  1029. (org-delete-indentation t)
  1030. (buffer-string))))))
  1031. (ert-deftest test-org/return ()
  1032. "Test `org-return' specifications."
  1033. ;; Regular test.
  1034. (should
  1035. (equal "Para\ngraph"
  1036. (org-test-with-temp-text "Para<point>graph"
  1037. (org-return)
  1038. (buffer-string))))
  1039. ;; With optional argument, indent line.
  1040. (should
  1041. (equal " Para\n graph"
  1042. (org-test-with-temp-text " Para<point>graph"
  1043. (org-return t)
  1044. (buffer-string))))
  1045. ;; On a table, call `org-table-next-row'.
  1046. (should
  1047. (org-test-with-temp-text "| <point>a |\n| b |"
  1048. (org-return)
  1049. (looking-at-p "b")))
  1050. ;; Open link or timestamp under point when `org-return-follows-link'
  1051. ;; is non-nil.
  1052. (should
  1053. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1054. (let ((org-return-follows-link t)
  1055. (org-link-search-must-match-exact-headline nil))
  1056. (org-return))
  1057. (looking-at-p "<<target>>")))
  1058. (should-not
  1059. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1060. (let ((org-return-follows-link nil)) (org-return))
  1061. (looking-at-p "<<target>>")))
  1062. (should
  1063. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  1064. (let ((org-return-follows-link t)) (org-return))
  1065. (looking-at-p "* b")))
  1066. (should
  1067. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  1068. (let ((org-return-follows-link t)
  1069. (org-link-search-must-match-exact-headline nil))
  1070. (org-return))
  1071. (looking-at-p "<<target>>")))
  1072. (should-not
  1073. (org-test-with-temp-text "Link [[target]]<point> <<target>>"
  1074. (let ((org-return-follows-link t)
  1075. (org-link-search-must-match-exact-headline nil))
  1076. (org-return))
  1077. (looking-at-p "<<target>>")))
  1078. ;; When `org-return-follows-link' is non-nil, tolerate links and
  1079. ;; timestamps in comments, node properties, etc.
  1080. (should
  1081. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1082. (let ((org-return-follows-link t)
  1083. (org-link-search-must-match-exact-headline nil))
  1084. (org-return))
  1085. (looking-at-p "<<target>>")))
  1086. (should-not
  1087. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1088. (let ((org-return-follows-link nil)) (org-return))
  1089. (looking-at-p "<<target>>")))
  1090. (should-not
  1091. (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
  1092. (let ((org-return-follows-link t)
  1093. (org-link-search-must-match-exact-headline nil))
  1094. (org-return))
  1095. (looking-at-p "<<target>>")))
  1096. ;; `org-return-follows-link' handle multi-line lines.
  1097. (should
  1098. (org-test-with-temp-text
  1099. "[[target][This is a very\n long description<point>]]\n <<target>>"
  1100. (let ((org-return-follows-link t)
  1101. (org-link-search-must-match-exact-headline nil))
  1102. (org-return))
  1103. (looking-at-p "<<target>>")))
  1104. (should-not
  1105. (org-test-with-temp-text
  1106. "[[target][This is a very\n long description]]<point>\n <<target>>"
  1107. (let ((org-return-follows-link t)
  1108. (org-link-search-must-match-exact-headline nil))
  1109. (org-return))
  1110. (looking-at-p "<<target>>")))
  1111. ;; However, do not open link when point is in a table.
  1112. (should
  1113. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1114. (let ((org-return-follows-link t)) (org-return))
  1115. (looking-at-p "between")))
  1116. ;; Special case: in a list, when indenting, do not break structure.
  1117. (should
  1118. (equal "- A\n B"
  1119. (org-test-with-temp-text "- A <point>B"
  1120. (org-return t)
  1121. (buffer-string))))
  1122. (should
  1123. (equal "- A\n\n- B"
  1124. (org-test-with-temp-text "- A\n<point>- B"
  1125. (org-return t)
  1126. (buffer-string))))
  1127. ;; On tags part of a headline, add a newline below it instead of
  1128. ;; breaking it.
  1129. (should
  1130. (equal "* H :tag:\n"
  1131. (org-test-with-temp-text "* H :<point>tag:"
  1132. (org-return)
  1133. (buffer-string))))
  1134. ;; Before headline text, add a newline below it instead of breaking
  1135. ;; it.
  1136. (should
  1137. (equal "* TODO H :tag:\n"
  1138. (org-test-with-temp-text "* <point>TODO H :tag:"
  1139. (org-return)
  1140. (buffer-string))))
  1141. (should
  1142. (equal "* TODO [#B] H :tag:\n"
  1143. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1144. (org-return)
  1145. (buffer-string))))
  1146. (should ;TODO are case-sensitive
  1147. (equal "* \nTodo"
  1148. (org-test-with-temp-text "* <point>Todo"
  1149. (org-return)
  1150. (buffer-string))))
  1151. ;; At headline text, break headline text but preserve tags.
  1152. (should
  1153. (equal "* TODO [#B] foo :tag:\nbar"
  1154. (let (org-auto-align-tags)
  1155. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1156. (org-return)
  1157. (buffer-string)))))
  1158. ;; At bol of headline insert newline.
  1159. (should
  1160. (equal "\n* h"
  1161. (org-test-with-temp-text "<point>* h"
  1162. (org-return)
  1163. (buffer-string))))
  1164. ;; Refuse to leave invalid headline in buffer.
  1165. (should
  1166. (equal "* h\n"
  1167. (org-test-with-temp-text "*<point> h"
  1168. (org-return)
  1169. (buffer-string))))
  1170. ;; Before first column or after last one in a table, split the
  1171. ;; table.
  1172. (should
  1173. (equal "| a |\n\n| b |"
  1174. (org-test-with-temp-text "| a |\n<point>| b |"
  1175. (org-return)
  1176. (buffer-string))))
  1177. (should
  1178. (equal "| a |\n\n| b |"
  1179. (org-test-with-temp-text "| a |<point>\n| b |"
  1180. (org-return)
  1181. (buffer-string))))
  1182. ;; Do not auto-fill on hitting <RET> inside a property drawer.
  1183. (should
  1184. (equal "* Heading\n:PROPERTIES:\n:SOME_PROP: This is a very long property value that goes beyond the fill-column. But this is inside a property drawer, so the auto-filling should be disabled.\n\n:END:"
  1185. (org-test-with-temp-text "* Heading\n:PROPERTIES:\n:SOME_PROP: This is a very long property value that goes beyond the fill-column. But this is inside a property drawer, so the auto-filling should be disabled.<point>\n:END:"
  1186. (setq-local fill-column 10)
  1187. (auto-fill-mode 1)
  1188. (org-return)
  1189. (buffer-string)))))
  1190. (ert-deftest test-org/meta-return ()
  1191. "Test M-RET (`org-meta-return') specifications."
  1192. ;; In a table field insert a row above.
  1193. (should
  1194. (org-test-with-temp-text "| a |"
  1195. (forward-char)
  1196. (org-meta-return)
  1197. (forward-line -1)
  1198. (looking-at "| |$")))
  1199. ;; In a paragraph change current line into a header.
  1200. (should
  1201. (org-test-with-temp-text "a"
  1202. (org-meta-return)
  1203. (beginning-of-line)
  1204. (looking-at "\* a$")))
  1205. ;; In an item insert an item, in this case above.
  1206. (should
  1207. (org-test-with-temp-text "- a"
  1208. (org-meta-return)
  1209. (beginning-of-line)
  1210. (looking-at "- $")))
  1211. ;; In a drawer and item insert an item, in this case above.
  1212. (should
  1213. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1214. (forward-line)
  1215. (org-meta-return)
  1216. (beginning-of-line)
  1217. (looking-at "- $"))))
  1218. (ert-deftest test-org/insert-heading ()
  1219. "Test `org-insert-heading' specifications."
  1220. ;; In an empty buffer, insert a new headline.
  1221. (should
  1222. (equal "* "
  1223. (org-test-with-temp-text ""
  1224. (org-insert-heading)
  1225. (buffer-string))))
  1226. ;; At the beginning of a line, turn it into a headline.
  1227. (should
  1228. (equal "* P"
  1229. (org-test-with-temp-text "<point>P"
  1230. (org-insert-heading)
  1231. (buffer-string))))
  1232. ;; In the middle of a line, split the line if allowed, otherwise,
  1233. ;; insert the headline at its end.
  1234. (should
  1235. (equal "Para\n* graph"
  1236. (org-test-with-temp-text "Para<point>graph"
  1237. (let ((org-M-RET-may-split-line '((default . t))))
  1238. (org-insert-heading))
  1239. (buffer-string))))
  1240. (should
  1241. (equal "Paragraph\n* "
  1242. (org-test-with-temp-text "Para<point>graph"
  1243. (let ((org-M-RET-may-split-line '((default . nil))))
  1244. (org-insert-heading))
  1245. (buffer-string))))
  1246. ;; At the beginning of a headline, create one above.
  1247. (should
  1248. (equal "* \n* H"
  1249. (org-test-with-temp-text "* H"
  1250. (org-insert-heading)
  1251. (buffer-string))))
  1252. ;; In the middle of a headline, split it if allowed.
  1253. (should
  1254. (equal "* H\n* 1\n"
  1255. (org-test-with-temp-text "* H<point>1"
  1256. (let ((org-M-RET-may-split-line '((headline . t))))
  1257. (org-insert-heading))
  1258. (buffer-string))))
  1259. (should
  1260. (equal "* H1\n* \n"
  1261. (org-test-with-temp-text "* H<point>1"
  1262. (let ((org-M-RET-may-split-line '((headline . nil))))
  1263. (org-insert-heading))
  1264. (buffer-string))))
  1265. ;; However, splitting cannot happen on TODO keywords, priorities or
  1266. ;; tags.
  1267. (should
  1268. (equal "* TODO H1\n* \n"
  1269. (org-test-with-temp-text "* TO<point>DO H1"
  1270. (let ((org-M-RET-may-split-line '((headline . t))))
  1271. (org-insert-heading))
  1272. (buffer-string))))
  1273. (should
  1274. (equal "* [#A] H1\n* \n"
  1275. (org-test-with-temp-text "* [#<point>A] H1"
  1276. (let ((org-M-RET-may-split-line '((headline . t))))
  1277. (org-insert-heading))
  1278. (buffer-string))))
  1279. (should
  1280. (equal "* H1 :tag:\n* \n"
  1281. (org-test-with-temp-text "* H1 :ta<point>g:"
  1282. (let ((org-M-RET-may-split-line '((headline . t))))
  1283. (org-insert-heading))
  1284. (buffer-string))))
  1285. ;; New headline level depends on the level of the headline above.
  1286. (should
  1287. (equal "** H\n** P"
  1288. (org-test-with-temp-text "** H\n<point>P"
  1289. (org-insert-heading)
  1290. (buffer-string))))
  1291. (should
  1292. (equal "** H\nPara\n** graph"
  1293. (org-test-with-temp-text "** H\nPara<point>graph"
  1294. (let ((org-M-RET-may-split-line '((default . t))))
  1295. (org-insert-heading))
  1296. (buffer-string))))
  1297. (should
  1298. (equal "** \n** H"
  1299. (org-test-with-temp-text "** H"
  1300. (org-insert-heading)
  1301. (buffer-string))))
  1302. ;; When called with one universal argument, insert a new headline at
  1303. ;; the end of the current subtree, independently on the position of
  1304. ;; point.
  1305. (should
  1306. (equal
  1307. "* H1\n** H2\n* \n"
  1308. (org-test-with-temp-text "* H1\n** H2"
  1309. (let ((org-insert-heading-respect-content nil))
  1310. (org-insert-heading '(4)))
  1311. (buffer-string))))
  1312. (should
  1313. (equal
  1314. "* H1\n** H2\n* \n"
  1315. (org-test-with-temp-text "* H<point>1\n** H2"
  1316. (let ((org-insert-heading-respect-content nil))
  1317. (org-insert-heading '(4)))
  1318. (buffer-string))))
  1319. ;; When called with two universal arguments, insert a new headline
  1320. ;; at the end of the grandparent subtree.
  1321. (should
  1322. (equal "* H1\n** H3\n- item\n** H2\n** \n"
  1323. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1324. (let ((org-insert-heading-respect-content nil))
  1325. (org-insert-heading '(16)))
  1326. (buffer-string))))
  1327. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1328. ;; a level 1 heading.
  1329. (should
  1330. (equal "* H1\n** H2\n* \n"
  1331. (org-test-with-temp-text "* H1\n** H2<point>"
  1332. (org-insert-heading nil nil t)
  1333. (buffer-string))))
  1334. (should
  1335. (equal "* H1\n- item\n* "
  1336. (org-test-with-temp-text "* H1\n- item<point>"
  1337. (org-insert-heading nil nil t)
  1338. (buffer-string))))
  1339. ;; Obey `org-blank-before-new-entry'.
  1340. (should
  1341. (equal "* H1\n\n* \n"
  1342. (org-test-with-temp-text "* H1<point>"
  1343. (let ((org-blank-before-new-entry '((heading . t))))
  1344. (org-insert-heading))
  1345. (buffer-string))))
  1346. (should
  1347. (equal "* H1\n* \n"
  1348. (org-test-with-temp-text "* H1<point>"
  1349. (let ((org-blank-before-new-entry '((heading . nil))))
  1350. (org-insert-heading))
  1351. (buffer-string))))
  1352. (should
  1353. (equal "* H1\n* H2\n* \n"
  1354. (org-test-with-temp-text "* H1\n* H2<point>"
  1355. (let ((org-blank-before-new-entry '((heading . auto))))
  1356. (org-insert-heading))
  1357. (buffer-string))))
  1358. (should
  1359. (equal "* H1\n\n* H2\n\n* \n"
  1360. (org-test-with-temp-text "* H1\n\n* H2<point>"
  1361. (let ((org-blank-before-new-entry '((heading . auto))))
  1362. (org-insert-heading))
  1363. (buffer-string))))
  1364. ;; Corner case: correctly insert a headline after an empty one.
  1365. (should
  1366. (equal "* \n* \n"
  1367. (org-test-with-temp-text "* <point>"
  1368. (org-insert-heading)
  1369. (buffer-string))))
  1370. (should
  1371. (org-test-with-temp-text "* <point>\n"
  1372. (org-insert-heading)
  1373. (looking-at-p "\n\\'")))
  1374. ;; Do not insert spurious headlines when inserting a new headline.
  1375. (should
  1376. (equal "* H1\n* H2\n* \n"
  1377. (org-test-with-temp-text "* H1\n* H2<point>\n"
  1378. (org-insert-heading)
  1379. (buffer-string))))
  1380. ;; Preserve visibility at beginning of line. In particular, when
  1381. ;; removing spurious blank lines, do not visually merge heading with
  1382. ;; the line visible above.
  1383. (should-not
  1384. (org-test-with-temp-text "* H1<point>\nContents\n\n* H2\n"
  1385. (org-overview)
  1386. (let ((org-blank-before-new-entry '((heading . nil))))
  1387. (org-insert-heading '(4)))
  1388. (invisible-p (line-end-position 0))))
  1389. ;; Properly handle empty lines when forcing a headline below current
  1390. ;; one.
  1391. (should
  1392. (equal "* H1\n\n* H\n\n* \n"
  1393. (org-test-with-temp-text "* H1\n\n* H<point>"
  1394. (let ((org-blank-before-new-entry '((heading . t))))
  1395. (org-insert-heading '(4))
  1396. (buffer-string))))))
  1397. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1398. "Test `org-insert-todo-heading-respect-content' specifications."
  1399. ;; Create a TODO heading.
  1400. (should
  1401. (org-test-with-temp-text "* H1\n Body"
  1402. (org-insert-todo-heading-respect-content)
  1403. (nth 2 (org-heading-components))))
  1404. ;; Add headline at the end of the first subtree
  1405. (should
  1406. (equal
  1407. "* TODO \n"
  1408. (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
  1409. (org-insert-todo-heading-respect-content)
  1410. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1411. ;; In a list, do not create a new item.
  1412. (should
  1413. (equal
  1414. "* TODO \n"
  1415. (org-test-with-temp-text "* H\n- an item\n- another one"
  1416. (search-forward "an ")
  1417. (org-insert-todo-heading-respect-content)
  1418. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1419. ;; Use the same TODO keyword as current heading.
  1420. (should
  1421. (equal
  1422. "* TODO \n"
  1423. (org-test-with-temp-text "* TODO\n** WAITING\n"
  1424. (org-insert-todo-heading-respect-content)
  1425. (buffer-substring-no-properties (line-beginning-position) (point-max))))))
  1426. (ert-deftest test-org/clone-with-time-shift ()
  1427. "Test `org-clone-subtree-with-time-shift'."
  1428. ;; Raise an error before first heading.
  1429. (should-error
  1430. (org-test-with-temp-text ""
  1431. (org-clone-subtree-with-time-shift 1)))
  1432. ;; Raise an error on invalid number of clones.
  1433. (should-error
  1434. (org-test-with-temp-text "* Clone me"
  1435. (org-clone-subtree-with-time-shift -1)))
  1436. ;; Clone non-repeating once.
  1437. (should
  1438. (equal "\
  1439. * H1\n<2015-06-21>
  1440. * H1\n<2015-06-23>
  1441. "
  1442. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1443. (org-clone-subtree-with-time-shift 1 "+2d")
  1444. (replace-regexp-in-string
  1445. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1446. nil nil 1))))
  1447. ;; Clone repeating once.
  1448. (should
  1449. (equal "\
  1450. * H1\n<2015-06-21>
  1451. * H1\n<2015-06-23>
  1452. * H1\n<2015-06-25 +1w>
  1453. "
  1454. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1455. (org-clone-subtree-with-time-shift 1 "+2d")
  1456. (replace-regexp-in-string
  1457. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1458. nil nil 1))))
  1459. ;; Clone non-repeating zero times.
  1460. (should
  1461. (equal "\
  1462. * H1\n<2015-06-21>
  1463. "
  1464. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1465. (org-clone-subtree-with-time-shift 0 "+2d")
  1466. (replace-regexp-in-string
  1467. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1468. nil nil 1))))
  1469. ;; Clone repeating "zero" times.
  1470. (should
  1471. (equal "\
  1472. * H1\n<2015-06-21>
  1473. * H1\n<2015-06-23 +1w>
  1474. "
  1475. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1476. (org-clone-subtree-with-time-shift 0 "+2d")
  1477. (replace-regexp-in-string
  1478. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1479. nil nil 1))))
  1480. ;; Clone with blank SHIFT argument.
  1481. (should
  1482. (string-prefix-p
  1483. "* H <2012-03-29"
  1484. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1485. (org-clone-subtree-with-time-shift 1 "")
  1486. (buffer-substring-no-properties (line-beginning-position 2)
  1487. (line-end-position 2)))))
  1488. ;; Find time stamps before point. If SHIFT is not specified, ask
  1489. ;; for a time shift.
  1490. (should
  1491. (string-prefix-p
  1492. "* H <2012-03-30"
  1493. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1494. (org-clone-subtree-with-time-shift 1 "+1d")
  1495. (buffer-substring-no-properties (line-beginning-position 2)
  1496. (line-end-position 2)))))
  1497. (should
  1498. (string-prefix-p
  1499. "* H <2014-03-05"
  1500. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  1501. (cl-letf (((symbol-function 'read-from-minibuffer)
  1502. (lambda (&rest args) "+1d")))
  1503. (org-clone-subtree-with-time-shift 1))
  1504. (buffer-substring-no-properties (line-beginning-position 2)
  1505. (line-end-position 2))))))
  1506. ;;; Fixed-Width Areas
  1507. (ert-deftest test-org/toggle-fixed-width ()
  1508. "Test `org-toggle-fixed-width' specifications."
  1509. ;; No region: Toggle on fixed-width marker in paragraphs.
  1510. (should
  1511. (equal ": A"
  1512. (org-test-with-temp-text "A"
  1513. (org-toggle-fixed-width)
  1514. (buffer-string))))
  1515. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1516. (should
  1517. (equal "A"
  1518. (org-test-with-temp-text ": A"
  1519. (org-toggle-fixed-width)
  1520. (buffer-string))))
  1521. ;; No region: Toggle on marker in blank lines after elements or just
  1522. ;; after a headline.
  1523. (should
  1524. (equal "* H\n: "
  1525. (org-test-with-temp-text "* H\n"
  1526. (forward-line)
  1527. (org-toggle-fixed-width)
  1528. (buffer-string))))
  1529. (should
  1530. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1531. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1532. (goto-char (point-max))
  1533. (org-toggle-fixed-width)
  1534. (buffer-string))))
  1535. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1536. ;; headlines, clocks)
  1537. (should
  1538. (equal ": * Headline"
  1539. (org-test-with-temp-text "* Headline"
  1540. (org-toggle-fixed-width)
  1541. (buffer-string))))
  1542. (should
  1543. (equal ": #+KEYWORD: value"
  1544. (org-test-with-temp-text "#+KEYWORD: value"
  1545. (org-toggle-fixed-width)
  1546. (buffer-string))))
  1547. ;; No region: error in other situations.
  1548. (should-error
  1549. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1550. (forward-line)
  1551. (org-toggle-fixed-width)
  1552. (buffer-string)))
  1553. ;; No region: Indentation is preserved.
  1554. (should
  1555. (equal "- A\n : B"
  1556. (org-test-with-temp-text "- A\n B"
  1557. (forward-line)
  1558. (org-toggle-fixed-width)
  1559. (buffer-string))))
  1560. ;; Region: If it contains only fixed-width elements and blank lines,
  1561. ;; toggle off fixed-width markup.
  1562. (should
  1563. (equal "A\n\nB"
  1564. (org-test-with-temp-text ": A\n\n: B"
  1565. (transient-mark-mode 1)
  1566. (push-mark (point) t t)
  1567. (goto-char (point-max))
  1568. (org-toggle-fixed-width)
  1569. (buffer-string))))
  1570. ;; Region: If it contains anything else, toggle on fixed-width but
  1571. ;; not on fixed-width areas.
  1572. (should
  1573. (equal ": A\n: \n: B\n: \n: C"
  1574. (org-test-with-temp-text "A\n\n: B\n\nC"
  1575. (transient-mark-mode 1)
  1576. (push-mark (point) t t)
  1577. (goto-char (point-max))
  1578. (org-toggle-fixed-width)
  1579. (buffer-string))))
  1580. ;; Region: Ignore blank lines at its end, unless it contains only
  1581. ;; such lines.
  1582. (should
  1583. (equal ": A\n\n"
  1584. (org-test-with-temp-text "A\n\n"
  1585. (transient-mark-mode 1)
  1586. (push-mark (point) t t)
  1587. (goto-char (point-max))
  1588. (org-toggle-fixed-width)
  1589. (buffer-string))))
  1590. (should
  1591. (equal ": \n: \n"
  1592. (org-test-with-temp-text "\n\n"
  1593. (transient-mark-mode 1)
  1594. (push-mark (point) t t)
  1595. (goto-char (point-max))
  1596. (org-toggle-fixed-width)
  1597. (buffer-string)))))
  1598. (ert-deftest test-org/kill-line ()
  1599. "Test `org-kill-line' specifications."
  1600. ;; At the beginning of a line, kill whole line.
  1601. (should
  1602. (equal ""
  1603. (org-test-with-temp-text "abc"
  1604. (org-kill-line)
  1605. (buffer-string))))
  1606. ;; In the middle of a line, kill line until its end.
  1607. (should
  1608. (equal "a"
  1609. (org-test-with-temp-text "a<point>bc"
  1610. (org-kill-line)
  1611. (buffer-string))))
  1612. ;; Do not kill newline character.
  1613. (should
  1614. (equal "\n123"
  1615. (org-test-with-temp-text "abc\n123"
  1616. (org-kill-line)
  1617. (buffer-string))))
  1618. (should
  1619. (equal "a\n123"
  1620. (org-test-with-temp-text "a<point>bc\n123"
  1621. (org-kill-line)
  1622. (buffer-string))))
  1623. ;; When `org-special-ctrl-k' is non-nil and point is at a headline,
  1624. ;; kill until tags.
  1625. (should
  1626. (equal "* A :tag:"
  1627. (org-test-with-temp-text "* A<point>B :tag:"
  1628. (let ((org-special-ctrl-k t)
  1629. (org-tags-column 0))
  1630. (org-kill-line))
  1631. (buffer-string))))
  1632. ;; If point is on tags, only kill part left until the end of line.
  1633. (should
  1634. (equal "* A :tag:"
  1635. (org-test-with-temp-text "* A :tag:<point>tag2:"
  1636. (let ((org-special-ctrl-k t)
  1637. (org-tags-column 0))
  1638. (org-kill-line))
  1639. (buffer-string))))
  1640. ;; However, if point is at the beginning of the line, kill whole
  1641. ;; headline.
  1642. (should
  1643. (equal ""
  1644. (org-test-with-temp-text "* AB :tag:"
  1645. (let ((org-special-ctrl-k t)
  1646. (org-tags-column 0))
  1647. (org-kill-line))
  1648. (buffer-string))))
  1649. ;; When `org-ctrl-k-protect-subtree' is non-nil, and point is in
  1650. ;; invisible text, ask before removing it. When set to `error',
  1651. ;; throw an error.
  1652. (should-error
  1653. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  1654. (org-overview)
  1655. (let ((org-special-ctrl-k nil)
  1656. (org-ctrl-k-protect-subtree t))
  1657. (cl-letf (((symbol-function 'y-or-n-p) 'ignore))
  1658. (org-kill-line)))))
  1659. (should-error
  1660. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  1661. (org-overview)
  1662. (let ((org-special-ctrl-k nil)
  1663. (org-ctrl-k-protect-subtree 'error))
  1664. (org-kill-line)))))
  1665. ;;; Headline
  1666. (ert-deftest test-org/get-heading ()
  1667. "Test `org-get-heading' specifications."
  1668. ;; Return current heading, even if point is not on it.
  1669. (should
  1670. (equal "H"
  1671. (org-test-with-temp-text "* H"
  1672. (org-get-heading))))
  1673. (should
  1674. (equal "H"
  1675. (org-test-with-temp-text "* H\nText<point>"
  1676. (org-get-heading))))
  1677. ;; Without any optional argument, return TODO keyword, priority
  1678. ;; cookie, COMMENT keyword and tags.
  1679. (should
  1680. (equal "TODO H"
  1681. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1682. (org-get-heading))))
  1683. (should
  1684. (equal "[#A] H"
  1685. (org-test-with-temp-text "* [#A] H"
  1686. (org-get-heading))))
  1687. (should
  1688. (equal "COMMENT H"
  1689. (org-test-with-temp-text "* COMMENT H"
  1690. (org-get-heading))))
  1691. (should
  1692. (equal "H :tag:"
  1693. (org-test-with-temp-text "* H :tag:"
  1694. (org-get-heading))))
  1695. ;; With NO-TAGS argument, ignore tags.
  1696. (should
  1697. (equal "TODO H"
  1698. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1699. (org-get-heading t))))
  1700. (should
  1701. (equal "H"
  1702. (org-test-with-temp-text "* H :tag:"
  1703. (org-get-heading t))))
  1704. ;; With NO-TODO, ignore TODO keyword.
  1705. (should
  1706. (equal "H"
  1707. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1708. (org-get-heading nil t))))
  1709. (should
  1710. (equal "H :tag:"
  1711. (org-test-with-temp-text "* H :tag:"
  1712. (org-get-heading nil t))))
  1713. ;; TODO keywords are case-sensitive.
  1714. (should
  1715. (equal "Todo H"
  1716. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1717. (org-get-heading nil t))))
  1718. ;; With NO-PRIORITY, ignore priority.
  1719. (should
  1720. (equal "H"
  1721. (org-test-with-temp-text "* [#A] H"
  1722. (org-get-heading nil nil t))))
  1723. (should
  1724. (equal "H"
  1725. (org-test-with-temp-text "* H"
  1726. (org-get-heading nil nil t))))
  1727. (should
  1728. (equal "TODO H"
  1729. (org-test-with-temp-text "* TODO [#A] H"
  1730. (org-get-heading nil nil t))))
  1731. ;; With NO-COMMENT, ignore COMMENT keyword.
  1732. (should
  1733. (equal "H"
  1734. (org-test-with-temp-text "* COMMENT H"
  1735. (org-get-heading nil nil nil t))))
  1736. (should
  1737. (equal "H"
  1738. (org-test-with-temp-text "* H"
  1739. (org-get-heading nil nil nil t))))
  1740. (should
  1741. (equal "TODO [#A] H"
  1742. (org-test-with-temp-text "* TODO [#A] COMMENT H"
  1743. (org-get-heading nil nil nil t))))
  1744. ;; On an empty headline, return value is consistent.
  1745. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1746. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1747. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1748. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil t))))
  1749. (should
  1750. (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil nil t)))))
  1751. (ert-deftest test-org/in-commented-heading-p ()
  1752. "Test `org-in-commented-heading-p' specifications."
  1753. ;; Commented headline.
  1754. (should
  1755. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1756. (goto-char (point-max))
  1757. (org-in-commented-heading-p)))
  1758. ;; Commented ancestor.
  1759. (should
  1760. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1761. (goto-char (point-max))
  1762. (org-in-commented-heading-p)))
  1763. ;; Comment keyword is case-sensitive.
  1764. (should-not
  1765. (org-test-with-temp-text "* Comment Headline\nBody"
  1766. (goto-char (point-max))
  1767. (org-in-commented-heading-p)))
  1768. ;; Keyword is standalone.
  1769. (should-not
  1770. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1771. (goto-char (point-max))
  1772. (org-in-commented-heading-p)))
  1773. ;; Optional argument.
  1774. (should-not
  1775. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1776. (goto-char (point-max))
  1777. (org-in-commented-heading-p t))))
  1778. (ert-deftest test-org/entry-blocked-p ()
  1779. ;; Check other dependencies.
  1780. (should
  1781. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1782. (let ((org-enforce-todo-dependencies t)
  1783. (org-blocker-hook
  1784. '(org-block-todo-from-children-or-siblings-or-parent)))
  1785. (org-entry-blocked-p))))
  1786. (should-not
  1787. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1788. (let ((org-enforce-todo-dependencies t)
  1789. (org-blocker-hook
  1790. '(org-block-todo-from-children-or-siblings-or-parent)))
  1791. (org-entry-blocked-p))))
  1792. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1793. ;; blocked.
  1794. (should-not
  1795. (org-test-with-temp-text "* Blocked\n** TODO one"
  1796. (let ((org-enforce-todo-dependencies t)
  1797. (org-blocker-hook
  1798. '(org-block-todo-from-children-or-siblings-or-parent)))
  1799. (org-entry-blocked-p))))
  1800. (should-not
  1801. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1802. (let ((org-enforce-todo-dependencies t)
  1803. (org-blocker-hook
  1804. '(org-block-todo-from-children-or-siblings-or-parent)))
  1805. (org-entry-blocked-p))))
  1806. ;; Follow :ORDERED: specifications.
  1807. (should
  1808. (org-test-with-temp-text
  1809. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1810. (let ((org-enforce-todo-dependencies t)
  1811. (org-blocker-hook
  1812. '(org-block-todo-from-children-or-siblings-or-parent)))
  1813. (org-entry-blocked-p))))
  1814. (should-not
  1815. (org-test-with-temp-text
  1816. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1817. (let ((org-enforce-todo-dependencies t)
  1818. (org-blocker-hook
  1819. '(org-block-todo-from-children-or-siblings-or-parent)))
  1820. (org-entry-blocked-p)))))
  1821. (ert-deftest test-org/get-outline-path ()
  1822. "Test `org-get-outline-path' specifications."
  1823. ;; Top-level headlines have no outline path.
  1824. (should-not
  1825. (org-test-with-temp-text "* H"
  1826. (org-get-outline-path)))
  1827. ;; Otherwise, outline path is the path leading to the headline.
  1828. (should
  1829. (equal '("H")
  1830. (org-test-with-temp-text "* H\n** S<point>"
  1831. (org-get-outline-path))))
  1832. ;; Find path even when point is not on a headline.
  1833. (should
  1834. (equal '("H")
  1835. (org-test-with-temp-text "* H\n** S\nText<point>"
  1836. (org-get-outline-path))))
  1837. ;; TODO keywords, tags and statistics cookies are ignored.
  1838. (should
  1839. (equal '("H")
  1840. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1841. (org-get-outline-path))))
  1842. ;; Links are replaced with their description or their path.
  1843. (should
  1844. (equal '("Org")
  1845. (org-test-with-temp-text
  1846. "* [[https://orgmode.org][Org]]\n** S<point>"
  1847. (org-get-outline-path))))
  1848. (should
  1849. (equal '("https://orgmode.org")
  1850. (org-test-with-temp-text
  1851. "* [[https://orgmode.org]]\n** S<point>"
  1852. (org-get-outline-path))))
  1853. ;; When WITH-SELF is non-nil, include current heading.
  1854. (should
  1855. (equal '("H")
  1856. (org-test-with-temp-text "* H"
  1857. (org-get-outline-path t))))
  1858. (should
  1859. (equal '("H" "S")
  1860. (org-test-with-temp-text "* H\n** S\nText<point>"
  1861. (org-get-outline-path t))))
  1862. ;; Using cache is transparent to the user.
  1863. (should
  1864. (equal '("H")
  1865. (org-test-with-temp-text "* H\n** S<point>"
  1866. (setq org-outline-path-cache nil)
  1867. (org-get-outline-path nil t))))
  1868. ;; Do not corrupt cache when finding outline path in distant part of
  1869. ;; the buffer.
  1870. (should
  1871. (equal '("H2")
  1872. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1873. (setq org-outline-path-cache nil)
  1874. (org-get-outline-path nil t)
  1875. (search-forward "S2")
  1876. (org-get-outline-path nil t))))
  1877. ;; Do not choke on empty headlines.
  1878. (should
  1879. (org-test-with-temp-text "* H\n** <point>"
  1880. (org-get-outline-path)))
  1881. (should
  1882. (org-test-with-temp-text "* \n** H<point>"
  1883. (org-get-outline-path))))
  1884. (ert-deftest test-org/format-outline-path ()
  1885. "Test `org-format-outline-path' specifications."
  1886. (should
  1887. (string= (org-format-outline-path (list "one" "two" "three"))
  1888. "one/two/three"))
  1889. ;; Empty path.
  1890. (should
  1891. (string= (org-format-outline-path '())
  1892. ""))
  1893. (should
  1894. (string= (org-format-outline-path '(nil))
  1895. ""))
  1896. ;; Empty path and prefix.
  1897. (should
  1898. (string= (org-format-outline-path '() nil ">>")
  1899. ">>"))
  1900. ;; Trailing whitespace in headings.
  1901. (should
  1902. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1903. "one/tw o/three"))
  1904. ;; Non-default prefix and separators.
  1905. (should
  1906. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1907. ">>|one|two|three"))
  1908. ;; Truncate.
  1909. (should
  1910. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1911. "one/two/.."))
  1912. ;; Give a very narrow width.
  1913. (should
  1914. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1915. "on"))
  1916. ;; Give a prefix that extends beyond the width.
  1917. (should
  1918. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1919. ">>>>>>>>>>")
  1920. ">>>>>>>>..")))
  1921. (ert-deftest test-org/map-entries ()
  1922. "Test `org-map-entries' specifications."
  1923. ;; Full match.
  1924. (should
  1925. (equal '(1 11)
  1926. (org-test-with-temp-text "* Level 1\n** Level 2"
  1927. (org-map-entries #'point))))
  1928. ;; Level match.
  1929. (should
  1930. (equal '(1)
  1931. (org-test-with-temp-text "* Level 1\n** Level 2"
  1932. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1933. (should
  1934. (equal '(11)
  1935. (org-test-with-temp-text "* Level 1\n** Level 2"
  1936. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1937. ;; Tag match.
  1938. (should
  1939. (equal '(11)
  1940. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1941. (org-map-entries #'point "yes"))))
  1942. (should
  1943. (equal '(14)
  1944. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1945. (org-map-entries #'point "+yes-a"))))
  1946. (should
  1947. (equal '(11 23)
  1948. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1949. (org-map-entries #'point "{yes?}"))))
  1950. ;; Priority match.
  1951. (should
  1952. (equal '(1)
  1953. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1954. (org-map-entries #'point "PRIORITY=\"A\""))))
  1955. ;; Date match.
  1956. (should
  1957. (equal '(36)
  1958. (org-test-with-temp-text "
  1959. * H1
  1960. SCHEDULED: <2012-03-29 thu.>
  1961. * H2
  1962. SCHEDULED: <2014-03-04 tue.>"
  1963. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1964. (should
  1965. (equal '(2)
  1966. (org-test-with-temp-text "
  1967. * H1
  1968. SCHEDULED: <2012-03-29 thu.>
  1969. * H2
  1970. SCHEDULED: <2014-03-04 tue.>"
  1971. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1972. ;; Regular property match.
  1973. (should
  1974. (equal '(2)
  1975. (org-test-with-temp-text "
  1976. * H1
  1977. :PROPERTIES:
  1978. :TEST: 1
  1979. :END:
  1980. * H2
  1981. :PROPERTIES:
  1982. :TEST: 2
  1983. :END:"
  1984. (org-map-entries #'point "TEST=1"))))
  1985. ;; Multiple criteria.
  1986. (should
  1987. (equal '(23)
  1988. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1989. (let (org-odd-levels-only
  1990. (org-use-tag-inheritance nil))
  1991. (org-map-entries #'point "yes+LEVEL=1")))))
  1992. ;; "or" criteria.
  1993. (should
  1994. (equal '(12 24)
  1995. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1996. (let (org-odd-levels-only)
  1997. (org-map-entries #'point "LEVEL=2|no")))))
  1998. (should
  1999. (equal '(1 12)
  2000. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  2001. (let (org-odd-levels-only)
  2002. (org-map-entries #'point "yes|no")))))
  2003. ;; "and" criteria.
  2004. (should
  2005. (equal '(22)
  2006. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  2007. (let (org-odd-levels-only)
  2008. (org-map-entries #'point "yes&no"))))))
  2009. (ert-deftest test-org/edit-headline ()
  2010. "Test `org-edit-headline' specifications."
  2011. (should
  2012. (equal "* B"
  2013. (org-test-with-temp-text "* A"
  2014. (org-edit-headline "B")
  2015. (buffer-string))))
  2016. ;; Handle empty headings.
  2017. (should
  2018. (equal "* "
  2019. (org-test-with-temp-text "* A"
  2020. (org-edit-headline "")
  2021. (buffer-string))))
  2022. (should
  2023. (equal "* A"
  2024. (org-test-with-temp-text "* "
  2025. (org-edit-headline "A")
  2026. (buffer-string))))
  2027. ;; Handle TODO keywords and priority cookies.
  2028. (should
  2029. (equal "* TODO B"
  2030. (org-test-with-temp-text "* TODO A"
  2031. (org-edit-headline "B")
  2032. (buffer-string))))
  2033. (should
  2034. (equal "* [#A] B"
  2035. (org-test-with-temp-text "* [#A] A"
  2036. (org-edit-headline "B")
  2037. (buffer-string))))
  2038. (should
  2039. (equal "* TODO [#A] B"
  2040. (org-test-with-temp-text "* TODO [#A] A"
  2041. (org-edit-headline "B")
  2042. (buffer-string))))
  2043. ;; Handle tags.
  2044. (equal "* B :tag:"
  2045. (org-test-with-temp-text "* A :tag:"
  2046. (let ((org-tags-column 4)) (org-edit-headline "B"))
  2047. (buffer-string))))
  2048. ;;; Keywords
  2049. (ert-deftest test-org/set-regexps-and-options ()
  2050. "Test `org-set-regexps-and-options' specifications."
  2051. ;; TAGS keyword.
  2052. (should
  2053. (equal '(("A"))
  2054. (let ((org-tag-alist '(("A")))
  2055. (org-tag-persistent-alist nil))
  2056. (org-test-with-temp-text ""
  2057. (org-mode-restart)
  2058. org-current-tag-alist))))
  2059. (should
  2060. (equal '(("B"))
  2061. (let ((org-tag-alist '(("A")))
  2062. (org-tag-persistent-alist nil))
  2063. (org-test-with-temp-text "#+TAGS: B"
  2064. (org-mode-restart)
  2065. org-current-tag-alist))))
  2066. (should
  2067. (equal '(("C") ("B"))
  2068. (let ((org-tag-alist '(("A")))
  2069. (org-tag-persistent-alist '(("C"))))
  2070. (org-test-with-temp-text "#+TAGS: B"
  2071. (org-mode-restart)
  2072. org-current-tag-alist))))
  2073. (should
  2074. (equal '(("B"))
  2075. (let ((org-tag-alist '(("A")))
  2076. (org-tag-persistent-alist '(("C"))))
  2077. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  2078. (org-mode-restart)
  2079. org-current-tag-alist))))
  2080. (should
  2081. (equal '(("A" . ?a) ("B") ("C"))
  2082. (let ((org-tag-persistant-alist nil))
  2083. (org-test-with-temp-text "#+TAGS: A(a) B C"
  2084. (org-mode-restart)
  2085. org-current-tag-alist))))
  2086. (should
  2087. (equal '(("A") (:newline) ("B"))
  2088. (let ((org-tag-persistent-alist nil))
  2089. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  2090. (org-mode-restart)
  2091. org-current-tag-alist))))
  2092. (should
  2093. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  2094. (let ((org-tag-persistent-alist nil))
  2095. (org-test-with-temp-text "#+TAGS: { A B } C"
  2096. (org-mode-restart)
  2097. org-current-tag-alist))))
  2098. (should
  2099. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  2100. (let ((org-tag-persistent-alist nil))
  2101. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2102. (org-mode-restart)
  2103. org-current-tag-alist))))
  2104. (should
  2105. (equal '(("A" "B" "C"))
  2106. (let ((org-tag-persistent-alist nil))
  2107. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2108. (org-mode-restart)
  2109. org-tag-groups-alist))))
  2110. (should
  2111. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  2112. (let ((org-tag-persistent-alist nil))
  2113. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2114. (org-mode-restart)
  2115. org-current-tag-alist))))
  2116. (should
  2117. (equal '(("A" "B" "C"))
  2118. (let ((org-tag-persistent-alist nil))
  2119. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2120. (org-mode-restart)
  2121. org-tag-groups-alist))))
  2122. ;; FILETAGS keyword.
  2123. (should
  2124. (equal '("A" "B" "C")
  2125. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  2126. (org-mode-restart)
  2127. org-file-tags)))
  2128. ;; PROPERTY keyword. Property names are case-insensitive.
  2129. (should
  2130. (equal "foo=1"
  2131. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  2132. (org-mode-restart)
  2133. (cdr (assoc "var" org-file-properties)))))
  2134. (should
  2135. (equal
  2136. "foo=1 bar=2"
  2137. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  2138. (org-mode-restart)
  2139. (cdr (assoc "var" org-file-properties)))))
  2140. (should
  2141. (equal
  2142. "foo=1 bar=2"
  2143. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  2144. (org-mode-restart)
  2145. (cdr (assoc "var" org-file-properties)))))
  2146. ;; ARCHIVE keyword.
  2147. (should
  2148. (equal "%s_done::"
  2149. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  2150. (org-mode-restart)
  2151. org-archive-location)))
  2152. ;; CATEGORY keyword.
  2153. (should
  2154. (eq 'test
  2155. (org-test-with-temp-text "#+CATEGORY: test"
  2156. (org-mode-restart)
  2157. org-category)))
  2158. (should
  2159. (equal "test"
  2160. (org-test-with-temp-text "#+CATEGORY: test"
  2161. (org-mode-restart)
  2162. (cdr (assoc "CATEGORY" org-file-properties)))))
  2163. ;; COLUMNS keyword.
  2164. (should
  2165. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  2166. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  2167. (org-mode-restart)
  2168. org-columns-default-format)))
  2169. ;; CONSTANTS keyword. Constants names are case sensitive.
  2170. (should
  2171. (equal '("299792458." "3.14")
  2172. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  2173. (org-mode-restart)
  2174. (mapcar
  2175. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  2176. '("c" "pi")))))
  2177. (should
  2178. (equal "3.14"
  2179. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  2180. (org-mode-restart)
  2181. (cdr (assoc "pi" org-table-formula-constants-local)))))
  2182. (should
  2183. (equal "22/7"
  2184. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  2185. (org-mode-restart)
  2186. (cdr (assoc "PI" org-table-formula-constants-local)))))
  2187. ;; LINK keyword.
  2188. (should
  2189. (equal
  2190. '("url1" "url2")
  2191. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  2192. (org-mode-restart)
  2193. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  2194. '("a" "b")))))
  2195. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  2196. (should
  2197. (equal
  2198. '(?X ?Z ?Y)
  2199. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  2200. (org-mode-restart)
  2201. (list org-highest-priority org-lowest-priority org-default-priority))))
  2202. (should
  2203. (equal
  2204. '(?A ?C ?B)
  2205. (org-test-with-temp-text "#+PRIORITIES: X Z"
  2206. (org-mode-restart)
  2207. (list org-highest-priority org-lowest-priority org-default-priority))))
  2208. ;; STARTUP keyword.
  2209. (should
  2210. (equal '(t t)
  2211. (org-test-with-temp-text "#+STARTUP: fold odd"
  2212. (org-mode-restart)
  2213. (list org-startup-folded org-odd-levels-only))))
  2214. ;; TODO keywords.
  2215. (should
  2216. (equal '(("A" "B") ("C"))
  2217. (org-test-with-temp-text "#+TODO: A B | C"
  2218. (org-mode-restart)
  2219. (list org-not-done-keywords org-done-keywords))))
  2220. (should
  2221. (equal '(("A" "C") ("B" "D"))
  2222. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  2223. (org-mode-restart)
  2224. (list org-not-done-keywords org-done-keywords))))
  2225. (should
  2226. (equal '(("A" "B") ("C"))
  2227. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  2228. (org-mode-restart)
  2229. (list org-not-done-keywords org-done-keywords))))
  2230. (should
  2231. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  2232. (org-test-with-temp-text "#+TODO: A(a)"
  2233. (org-mode-restart)
  2234. org-todo-key-alist)))
  2235. (should
  2236. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  2237. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  2238. (org-mode-restart)
  2239. org-todo-log-states)))
  2240. ;; Enter SETUPFILE keyword.
  2241. (should
  2242. (equal "1"
  2243. (org-test-with-temp-text
  2244. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  2245. (org-mode-restart)
  2246. (cdr (assoc "a" org-file-properties))))))
  2247. ;;; Links
  2248. ;;;; Coderefs
  2249. (ert-deftest test-org/coderef ()
  2250. "Test coderef links specifications."
  2251. (should
  2252. (org-test-with-temp-text "
  2253. #+BEGIN_SRC emacs-lisp
  2254. \(+ 1 1) (ref:sc)
  2255. #+END_SRC
  2256. \[[(sc)<point>]]"
  2257. (org-open-at-point)
  2258. (looking-at "(ref:sc)")))
  2259. ;; Find coderef even with alternate label format.
  2260. (should
  2261. (org-test-with-temp-text "
  2262. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2263. \(+ 1 1) {ref:sc}
  2264. #+END_SRC
  2265. \[[(sc)<point>]]"
  2266. (org-open-at-point)
  2267. (looking-at "{ref:sc}"))))
  2268. ;;;; Custom ID
  2269. (ert-deftest test-org/custom-id ()
  2270. "Test custom ID links specifications."
  2271. (should
  2272. (org-test-with-temp-text
  2273. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2274. (org-open-at-point)
  2275. (looking-at-p "\\* H1")))
  2276. ;; Handle escape characters.
  2277. (should
  2278. (org-test-with-temp-text
  2279. "* H1\n:PROPERTIES:\n:CUSTOM_ID: [%]\n:END:\n* H2\n[[#%5B%25%5D<point>]]"
  2280. (org-open-at-point)
  2281. (looking-at-p "\\* H1")))
  2282. ;; Throw an error on false positives.
  2283. (should-error
  2284. (org-test-with-temp-text
  2285. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2286. (org-open-at-point)
  2287. (looking-at-p "\\* H1"))))
  2288. ;;;; Fuzzy Links
  2289. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2290. ;; a named element (#+name: text) and to headlines (* Text).
  2291. (ert-deftest test-org/fuzzy-links ()
  2292. "Test fuzzy links specifications."
  2293. ;; Fuzzy link goes in priority to a matching target.
  2294. (should
  2295. (org-test-with-temp-text
  2296. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2297. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2298. (looking-at "<<Test>>")))
  2299. ;; Then fuzzy link points to an element with a given name.
  2300. (should
  2301. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2302. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2303. (looking-at "#\\+NAME: Test")))
  2304. ;; A target still lead to a matching headline otherwise.
  2305. (should
  2306. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2307. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2308. (looking-at "\\* Head2")))
  2309. ;; With a leading star in link, enforce heading match.
  2310. (should
  2311. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2312. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2313. (looking-at "\\* Test")))
  2314. ;; With a leading star in link, enforce exact heading match, even
  2315. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2316. (should-error
  2317. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2318. (let ((org-link-search-must-match-exact-headline nil))
  2319. (org-open-at-point))))
  2320. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2321. (should
  2322. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2323. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2324. (looking-at "\\* Test")))
  2325. (should
  2326. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2327. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2328. (looking-at "\\* Test")))
  2329. ;; Heading match should not care about spaces, cookies, TODO
  2330. ;; keywords, priorities, and tags. However, TODO keywords are
  2331. ;; case-sensitive.
  2332. (should
  2333. (let ((first-line
  2334. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2335. (org-test-with-temp-text
  2336. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2337. (let ((org-link-search-must-match-exact-headline nil)
  2338. (org-todo-regexp "TODO"))
  2339. (org-open-at-point))
  2340. (looking-at (regexp-quote first-line)))))
  2341. (should-error
  2342. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2343. (let ((org-link-search-must-match-exact-headline nil)
  2344. (org-todo-regexp "TODO"))
  2345. (org-open-at-point))))
  2346. ;; Heading match should still be exact.
  2347. (should-error
  2348. (org-test-with-temp-text "
  2349. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2350. Foo Bar
  2351. <point>[[*Test 1]]"
  2352. (let ((org-link-search-must-match-exact-headline nil)
  2353. (org-todo-regexp "TODO"))
  2354. (org-open-at-point))))
  2355. (should
  2356. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2357. (let ((org-link-search-must-match-exact-headline nil)
  2358. (org-todo-regexp "TODO"))
  2359. (org-open-at-point))
  2360. (looking-at-p (regexp-quote "** Test 1 2"))))
  2361. ;; Heading match ignores COMMENT keyword.
  2362. (should
  2363. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2364. (org-open-at-point)
  2365. (looking-at "\\* COMMENT Test")))
  2366. (should
  2367. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2368. (org-open-at-point)
  2369. (looking-at "\\* TODO COMMENT Test")))
  2370. ;; Correctly un-hexify fuzzy links.
  2371. (should
  2372. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2373. (org-open-at-point)
  2374. (bobp)))
  2375. (should
  2376. (org-test-with-temp-text "* [1]\n[[*%5B1%5D<point>]]"
  2377. (org-open-at-point)
  2378. (bobp)))
  2379. ;; Match search strings containing newline characters, including
  2380. ;; blank lines.
  2381. (should
  2382. (org-test-with-temp-text-in-file "Paragraph\n\nline1\nline2\n\n"
  2383. (let ((file (buffer-file-name)))
  2384. (goto-char (point-max))
  2385. (insert (format "[[file:%s::line1 line2]]" file))
  2386. (beginning-of-line)
  2387. (let ((org-link-search-must-match-exact-headline nil))
  2388. (org-open-at-point 0))
  2389. (looking-at-p "line1"))))
  2390. (should
  2391. (org-test-with-temp-text-in-file "Paragraph\n\nline1\n\nline2\n\n"
  2392. (let ((file (buffer-file-name)))
  2393. (goto-char (point-max))
  2394. (insert (format "[[file:%s::line1 line2]]" file))
  2395. (beginning-of-line)
  2396. (let ((org-link-search-must-match-exact-headline nil))
  2397. (org-open-at-point 0))
  2398. (looking-at-p "line1")))))
  2399. ;;;; Link Escaping
  2400. (ert-deftest test-org/org-link-escape-ascii-character ()
  2401. "Escape an ascii character."
  2402. (should
  2403. (string=
  2404. "%5B"
  2405. (org-link-escape "["))))
  2406. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2407. "Escape an ascii control character."
  2408. (should
  2409. (string=
  2410. "%09"
  2411. (org-link-escape "\t"))))
  2412. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2413. "Escape an unicode multibyte character."
  2414. (should
  2415. (string=
  2416. "%E2%82%AC"
  2417. (org-link-escape "€"))))
  2418. (ert-deftest test-org/org-link-escape-custom-table ()
  2419. "Escape string with custom character table."
  2420. (should
  2421. (string=
  2422. "Foo%3A%42ar%0A"
  2423. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2424. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2425. "Escape string with custom table merged with default table."
  2426. (should
  2427. (string=
  2428. "%5BF%6F%6F%3A%42ar%0A%5D"
  2429. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2430. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2431. "Unescape an ascii character."
  2432. (should
  2433. (string=
  2434. "["
  2435. (org-link-unescape "%5B"))))
  2436. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2437. "Unescpae an ascii control character."
  2438. (should
  2439. (string=
  2440. "\n"
  2441. (org-link-unescape "%0A"))))
  2442. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2443. "Unescape unicode multibyte character."
  2444. (should
  2445. (string=
  2446. "€"
  2447. (org-link-unescape "%E2%82%AC"))))
  2448. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2449. "Unescape old style percent escaped character."
  2450. (should
  2451. (string=
  2452. "àâçèéêîôùû"
  2453. (decode-coding-string
  2454. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2455. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2456. "Escape and unescape a URL that includes an escaped char.
  2457. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2458. (should
  2459. (string=
  2460. "http://some.host.com/form?&id=blah%2Bblah25"
  2461. (org-link-unescape
  2462. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2463. ;;;; Open at point
  2464. (ert-deftest test-org/open-at-point/keyword ()
  2465. "Does `org-open-at-point' open link in a keyword line?"
  2466. (should
  2467. (org-test-with-temp-text
  2468. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2469. (org-open-at-point) t))
  2470. (should
  2471. (org-test-with-temp-text
  2472. "* H\n<<top>>\n#+KEYWORD: <point>[[top]]"
  2473. (org-open-at-point) t)))
  2474. (ert-deftest test-org/open-at-point/property ()
  2475. "Does `org-open-at-point' open link in property drawer?"
  2476. (should
  2477. (org-test-with-temp-text
  2478. "* Headline
  2479. :PROPERTIES:
  2480. :URL: <point>[[*Headline]]
  2481. :END:"
  2482. (org-open-at-point) t)))
  2483. (ert-deftest test-org/open-at-point/comment ()
  2484. "Does `org-open-at-point' open link in a commented line?"
  2485. (should
  2486. (org-test-with-temp-text
  2487. "<<top>>\n# <point>[[top]]"
  2488. (org-open-at-point) t))
  2489. (should
  2490. (org-test-with-temp-text
  2491. "* H\n<<top>>\n# <point>[[top]]"
  2492. (org-open-at-point) t)))
  2493. (ert-deftest test-org/open-at-point/inline-image ()
  2494. "Test `org-open-at-point' on nested links."
  2495. (should
  2496. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2497. (org-open-at-point)
  2498. (bobp))))
  2499. (ert-deftest test-org/open-at-point/radio-target ()
  2500. "Test `org-open-at-point' on radio targets."
  2501. (should
  2502. (org-test-with-temp-text "<<<target>>> <point>target"
  2503. (org-update-radio-target-regexp)
  2504. (org-open-at-point)
  2505. (eq (org-element-type (org-element-context)) 'radio-target))))
  2506. (ert-deftest test-org/open-at-point/tag ()
  2507. "Test `org-open-at-point' on tags."
  2508. (should
  2509. (org-test-with-temp-text "* H :<point>tag:"
  2510. (catch :result
  2511. (cl-letf (((symbol-function 'org-tags-view)
  2512. (lambda (&rest args) (throw :result t))))
  2513. (org-open-at-point)
  2514. nil))))
  2515. (should-not
  2516. (org-test-with-temp-text-in-file "* H<point> :tag:"
  2517. (catch :result
  2518. (cl-letf (((symbol-function 'org-tags-view)
  2519. (lambda (&rest args) (throw :result t))))
  2520. (org-open-at-point)
  2521. nil)))))
  2522. ;;;; Stored links
  2523. (ert-deftest test-org/store-link ()
  2524. "Test `org-store-link' specifications."
  2525. ;; On a headline, link to that headline. Use heading as the
  2526. ;; description of the link.
  2527. (should
  2528. (let (org-store-link-props org-stored-links)
  2529. (org-test-with-temp-text-in-file "* H1"
  2530. (let ((file (buffer-file-name)))
  2531. (equal (format "[[file:%s::*H1][H1]]" file)
  2532. (org-store-link nil))))))
  2533. ;; On a headline, remove any link from description.
  2534. (should
  2535. (let (org-store-link-props org-stored-links)
  2536. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2537. (let ((file (buffer-file-name)))
  2538. (equal (format "[[file:%s::*%s][d]]"
  2539. file
  2540. (org-link-escape "[[#l][d]]"))
  2541. (org-store-link nil))))))
  2542. (should
  2543. (let (org-store-link-props org-stored-links)
  2544. (org-test-with-temp-text-in-file "* [[l]]"
  2545. (let ((file (buffer-file-name)))
  2546. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2547. (org-store-link nil))))))
  2548. (should
  2549. (let (org-store-link-props org-stored-links)
  2550. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2551. (let ((file (buffer-file-name)))
  2552. (equal (format "[[file:%s::*%s][d1 d2]]"
  2553. file
  2554. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2555. (org-store-link nil))))))
  2556. ;; On a named element, link to that element.
  2557. (should
  2558. (let (org-store-link-props org-stored-links)
  2559. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2560. (let ((file (buffer-file-name)))
  2561. (equal (format "[[file:%s::foo][foo]]" file)
  2562. (org-store-link nil))))))
  2563. ;; Store link to Org buffer, with context.
  2564. (should
  2565. (let ((org-stored-links nil)
  2566. (org-id-link-to-org-use-id nil)
  2567. (org-context-in-file-links t))
  2568. (org-test-with-temp-text-in-file "* h1"
  2569. (let ((file (buffer-file-name)))
  2570. (equal (format "[[file:%s::*h1][h1]]" file)
  2571. (org-store-link nil))))))
  2572. ;; Store link to Org buffer, without context.
  2573. (should
  2574. (let ((org-stored-links nil)
  2575. (org-id-link-to-org-use-id nil)
  2576. (org-context-in-file-links nil))
  2577. (org-test-with-temp-text-in-file "* h1"
  2578. (let ((file (buffer-file-name)))
  2579. (equal (format "[[file:%s][file:%s]]" file file)
  2580. (org-store-link nil))))))
  2581. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  2582. (should
  2583. (let ((org-stored-links nil)
  2584. (org-id-link-to-org-use-id nil)
  2585. (org-context-in-file-links nil))
  2586. (org-test-with-temp-text-in-file "* h1"
  2587. (let ((file (buffer-file-name)))
  2588. (equal (format "[[file:%s::*h1][h1]]" file)
  2589. (org-store-link '(4)))))))
  2590. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  2591. ;; buffer.
  2592. (should
  2593. (let ((org-stored-links nil)
  2594. (org-id-link-to-org-use-id nil)
  2595. (org-context-in-file-links nil))
  2596. (org-test-with-temp-text-in-file "* h1"
  2597. (let ((file (buffer-file-name)))
  2598. (equal (format "[[file:%s][file:%s]]" file file)
  2599. (org-store-link '(16)))))))
  2600. ;; Store file link to non-Org buffer, with context.
  2601. (should
  2602. (let ((org-stored-links nil)
  2603. (org-context-in-file-links t))
  2604. (org-test-with-temp-text-in-file "one\n<point>two"
  2605. (fundamental-mode)
  2606. (let ((file (buffer-file-name)))
  2607. (equal (format "[[file:%s::one]]" file)
  2608. (org-store-link nil))))))
  2609. ;; Store file link to non-Org buffer, without context.
  2610. (should
  2611. (let ((org-stored-links nil)
  2612. (org-context-in-file-links nil))
  2613. (org-test-with-temp-text-in-file "one\n<point>two"
  2614. (fundamental-mode)
  2615. (let ((file (buffer-file-name)))
  2616. (equal (format "[[file:%s][file:%s]]" file file)
  2617. (org-store-link nil))))))
  2618. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  2619. ;; buffer.
  2620. (should
  2621. (let ((org-stored-links nil)
  2622. (org-context-in-file-links nil))
  2623. (org-test-with-temp-text-in-file "one\n<point>two"
  2624. (fundamental-mode)
  2625. (let ((file (buffer-file-name)))
  2626. (equal (format "[[file:%s::one]]" file)
  2627. (org-store-link '(4)))))))
  2628. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  2629. ;; non-Org buffer.
  2630. (should
  2631. (let ((org-stored-links nil)
  2632. (org-context-in-file-links nil))
  2633. (org-test-with-temp-text-in-file "one\n<point>two"
  2634. (fundamental-mode)
  2635. (let ((file (buffer-file-name)))
  2636. (equal (format "[[file:%s][file:%s]]" file file)
  2637. (org-store-link '(16))))))))
  2638. ;;; Node Properties
  2639. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2640. "Ensure properties accumulate in subtree drawers."
  2641. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2642. (org-babel-next-src-block)
  2643. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2644. (ert-deftest test-org/custom-properties ()
  2645. "Test custom properties specifications."
  2646. ;; Standard test.
  2647. (should
  2648. (let ((org-custom-properties '("FOO")))
  2649. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2650. (org-toggle-custom-properties-visibility)
  2651. (org-invisible-p2))))
  2652. ;; Properties are case-insensitive.
  2653. (should
  2654. (let ((org-custom-properties '("FOO")))
  2655. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2656. (org-toggle-custom-properties-visibility)
  2657. (org-invisible-p2))))
  2658. (should
  2659. (let ((org-custom-properties '("foo")))
  2660. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2661. (org-toggle-custom-properties-visibility)
  2662. (org-invisible-p2))))
  2663. ;; Multiple custom properties in the same drawer.
  2664. (should
  2665. (let ((org-custom-properties '("FOO" "BAR")))
  2666. (org-test-with-temp-text
  2667. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2668. (org-toggle-custom-properties-visibility)
  2669. (and (org-invisible-p2)
  2670. (not (progn (forward-line) (org-invisible-p2)))
  2671. (progn (forward-line) (org-invisible-p2))))))
  2672. ;; Hide custom properties with an empty value.
  2673. (should
  2674. (let ((org-custom-properties '("FOO")))
  2675. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2676. (org-toggle-custom-properties-visibility)
  2677. (org-invisible-p2))))
  2678. ;; Do not hide fake properties.
  2679. (should-not
  2680. (let ((org-custom-properties '("FOO")))
  2681. (org-test-with-temp-text ":FOO: val\n"
  2682. (org-toggle-custom-properties-visibility)
  2683. (org-invisible-p2))))
  2684. (should-not
  2685. (let ((org-custom-properties '("A")))
  2686. (org-test-with-temp-text
  2687. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2688. (org-toggle-custom-properties-visibility)
  2689. (org-invisible-p2)))))
  2690. ;;; Mark Region
  2691. (ert-deftest test-org/mark-subtree ()
  2692. "Test `org-mark-subtree' specifications."
  2693. ;; Error when point is before first headline.
  2694. (should-error
  2695. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2696. (progn (transient-mark-mode 1)
  2697. (org-mark-subtree))))
  2698. ;; Without argument, mark current subtree.
  2699. (should
  2700. (equal
  2701. '(12 32)
  2702. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2703. (progn (transient-mark-mode 1)
  2704. (forward-line 2)
  2705. (org-mark-subtree)
  2706. (list (region-beginning) (region-end))))))
  2707. ;; With an argument, move ARG up.
  2708. (should
  2709. (equal
  2710. '(1 32)
  2711. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2712. (progn (transient-mark-mode 1)
  2713. (forward-line 2)
  2714. (org-mark-subtree 1)
  2715. (list (region-beginning) (region-end))))))
  2716. ;; Do not get fooled by inlinetasks.
  2717. (when (featurep 'org-inlinetask)
  2718. (should
  2719. (= 1
  2720. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2721. (progn (transient-mark-mode 1)
  2722. (forward-line 1)
  2723. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2724. (region-beginning)))))))
  2725. ;;; Miscellaneous
  2726. (ert-deftest test-org/sort-entries ()
  2727. "Test `org-sort-entries'."
  2728. ;; Sort alphabetically.
  2729. (should
  2730. (equal "\n* abc\n* def\n* xyz\n"
  2731. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2732. (org-sort-entries nil ?a)
  2733. (buffer-string))))
  2734. (should
  2735. (equal "\n* xyz\n* def\n* abc\n"
  2736. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2737. (org-sort-entries nil ?A)
  2738. (buffer-string))))
  2739. (should
  2740. (equal "\n* \n* klm\n* xyz\n"
  2741. (org-test-with-temp-text "\n* xyz\n* \n* klm\n"
  2742. (org-sort-entries nil ?a)
  2743. (buffer-string))))
  2744. ;; Sort numerically.
  2745. (should
  2746. (equal "\n* 1\n* 2\n* 10\n"
  2747. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2748. (org-sort-entries nil ?n)
  2749. (buffer-string))))
  2750. (should
  2751. (equal "\n* 10\n* 2\n* 1\n"
  2752. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2753. (org-sort-entries nil ?N)
  2754. (buffer-string))))
  2755. (should
  2756. (equal "\n* \n* 1\n* 2\n"
  2757. (org-test-with-temp-text "\n* 1\n* \n* 2\n"
  2758. (org-sort-entries nil ?n)
  2759. (buffer-string))))
  2760. ;; Sort by custom function.
  2761. (should
  2762. (equal "\n* b\n* aa\n* ccc\n"
  2763. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2764. (org-sort-entries nil ?f
  2765. (lambda ()
  2766. (length (buffer-substring (point-at-bol)
  2767. (point-at-eol))))
  2768. #'<)
  2769. (buffer-string))))
  2770. (should
  2771. (equal "\n* ccc\n* aa\n* b\n"
  2772. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2773. (org-sort-entries nil ?F
  2774. (lambda ()
  2775. (length (buffer-substring (point-at-bol)
  2776. (point-at-eol))))
  2777. #'<)
  2778. (buffer-string))))
  2779. ;; Sort by TODO keyword.
  2780. (should
  2781. (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
  2782. (org-test-with-temp-text
  2783. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2784. (org-sort-entries nil ?o)
  2785. (buffer-string))))
  2786. (should
  2787. (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
  2788. (org-test-with-temp-text
  2789. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2790. (org-sort-entries nil ?O)
  2791. (buffer-string))))
  2792. ;; Sort by priority.
  2793. (should
  2794. (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
  2795. (org-test-with-temp-text
  2796. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2797. (org-sort-entries nil ?p)
  2798. (buffer-string))))
  2799. (should
  2800. (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
  2801. (org-test-with-temp-text
  2802. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2803. (org-sort-entries nil ?P)
  2804. (buffer-string))))
  2805. ;; Sort by creation time.
  2806. (should
  2807. (equal "
  2808. * h3
  2809. [2017-05-08 Mon]
  2810. * h2
  2811. [2017-05-09 Tue]
  2812. * h1
  2813. [2018-05-09 Wed]
  2814. "
  2815. (org-test-with-temp-text
  2816. "
  2817. * h1
  2818. [2018-05-09 Wed]
  2819. * h2
  2820. [2017-05-09 Tue]
  2821. * h3
  2822. [2017-05-08 Mon]
  2823. "
  2824. (org-sort-entries nil ?c)
  2825. (buffer-string))))
  2826. ;; Sort by scheduled date.
  2827. (should
  2828. (equal "
  2829. * TODO h4
  2830. SCHEDULED: <2017-05-06 Sat>
  2831. * TODO h3
  2832. SCHEDULED: <2017-05-08 Mon>
  2833. * TODO h2
  2834. DEADLINE: <2017-05-09 Tue>
  2835. * TODO h1
  2836. DEADLINE: <2017-05-07 Sun>
  2837. "
  2838. (org-test-with-temp-text
  2839. "
  2840. * TODO h2
  2841. DEADLINE: <2017-05-09 Tue>
  2842. * TODO h1
  2843. DEADLINE: <2017-05-07 Sun>
  2844. * TODO h3
  2845. SCHEDULED: <2017-05-08 Mon>
  2846. * TODO h4
  2847. SCHEDULED: <2017-05-06 Sat>
  2848. "
  2849. (org-sort-entries nil ?s)
  2850. (buffer-string))))
  2851. ;; Sort by deadline date.
  2852. (should
  2853. (equal "
  2854. * TODO h1
  2855. DEADLINE: <2017-05-07 Sun>
  2856. * TODO h2
  2857. DEADLINE: <2017-05-09 Tue>
  2858. * TODO h3
  2859. SCHEDULED: <2017-05-08 Mon>
  2860. * TODO h4
  2861. SCHEDULED: <2017-05-06 Sat>
  2862. "
  2863. (org-test-with-temp-text
  2864. "
  2865. * TODO h2
  2866. DEADLINE: <2017-05-09 Tue>
  2867. * TODO h1
  2868. DEADLINE: <2017-05-07 Sun>
  2869. * TODO h3
  2870. SCHEDULED: <2017-05-08 Mon>
  2871. * TODO h4
  2872. SCHEDULED: <2017-05-06 Sat>
  2873. "
  2874. (org-sort-entries nil ?d)
  2875. (buffer-string))))
  2876. ;; Sort by any date/time
  2877. (should
  2878. (equal "
  2879. * TODO h4
  2880. SCHEDULED: <2017-05-06 Sat>
  2881. * TODO h1
  2882. DEADLINE: <2017-05-07 Sun>
  2883. * TODO h3
  2884. SCHEDULED: <2017-05-08 Mon>
  2885. * TODO h2
  2886. DEADLINE: <2017-05-09 Tue>
  2887. "
  2888. (org-test-with-temp-text
  2889. "
  2890. * TODO h2
  2891. DEADLINE: <2017-05-09 Tue>
  2892. * TODO h1
  2893. DEADLINE: <2017-05-07 Sun>
  2894. * TODO h3
  2895. SCHEDULED: <2017-05-08 Mon>
  2896. * TODO h4
  2897. SCHEDULED: <2017-05-06 Sat>
  2898. "
  2899. (org-sort-entries nil ?t)
  2900. (buffer-string))))
  2901. ;; Sort by clocking time.
  2902. (should
  2903. (equal "
  2904. * clocked h2
  2905. :LOGBOOK:
  2906. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2907. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2908. :END:
  2909. * clocked h1
  2910. :LOGBOOK:
  2911. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2912. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2913. :END:
  2914. "
  2915. (org-test-with-temp-text
  2916. "
  2917. * clocked h1
  2918. :LOGBOOK:
  2919. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2920. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2921. :END:
  2922. * clocked h2
  2923. :LOGBOOK:
  2924. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2925. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2926. :END:
  2927. "
  2928. (org-sort-entries nil ?k)
  2929. (buffer-string))))
  2930. ;; Preserve file local variables when sorting.
  2931. (should
  2932. (equal "\n* A\n* B\n# Local Variables:\n# foo: t\n# End:\n"
  2933. (org-test-with-temp-text
  2934. "\n* B\n* A\n# Local Variables:\n# foo: t\n# End:"
  2935. (org-sort-entries nil ?a)
  2936. (buffer-string)))))
  2937. (ert-deftest test-org/string-collate-greaterp ()
  2938. "Test `org-string-collate-greaterp' specifications."
  2939. (should (org-string-collate-greaterp "def" "abc"))
  2940. (should-not (org-string-collate-greaterp "abc" "def")))
  2941. (ert-deftest test-org/file-contents ()
  2942. "Test `org-file-contents' specifications."
  2943. ;; Open files.
  2944. (should
  2945. (string= "#+BIND: variable value
  2946. #+DESCRIPTION: l2
  2947. #+LANGUAGE: en
  2948. #+SELECT_TAGS: b
  2949. #+TITLE: b
  2950. #+PROPERTY: a 1
  2951. " (org-file-contents (expand-file-name "setupfile3.org"
  2952. (concat org-test-dir "examples/")))))
  2953. ;; Throw error when trying to access an invalid file.
  2954. (should-error (org-file-contents "this-file-must-not-exist"))
  2955. ;; Try to access an invalid file, but do not throw an error.
  2956. (should
  2957. (progn (org-file-contents "this-file-must-not-exist" :noerror) t))
  2958. ;; Open URL.
  2959. (should
  2960. (string= "foo"
  2961. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2962. (unwind-protect
  2963. ;; Simulate successful retrieval of a URL.
  2964. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2965. (lambda (&rest_)
  2966. (with-current-buffer buffer
  2967. (insert "HTTP/1.1 200 OK\n\nfoo"))
  2968. buffer)))
  2969. (org-file-contents "http://some-valid-url"))
  2970. (kill-buffer buffer)))))
  2971. ;; Throw error when trying to access an invalid URL.
  2972. (should-error
  2973. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2974. (unwind-protect
  2975. ;; Simulate unsuccessful retrieval of a URL.
  2976. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2977. (lambda (&rest_)
  2978. (with-current-buffer buffer
  2979. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2980. buffer)))
  2981. (org-file-contents "http://this-url-must-not-exist"))
  2982. (kill-buffer buffer))))
  2983. ;; Try to access an invalid URL, but do not throw an error.
  2984. (should-error
  2985. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2986. (unwind-protect
  2987. ;; Simulate unsuccessful retrieval of a URL.
  2988. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2989. (lambda (&rest_)
  2990. (with-current-buffer buffer
  2991. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2992. buffer)))
  2993. (org-file-contents "http://this-url-must-not-exist"))
  2994. (kill-buffer buffer))))
  2995. (should
  2996. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2997. (unwind-protect
  2998. ;; Simulate unsuccessful retrieval of a URL.
  2999. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3000. (lambda (&rest_)
  3001. (with-current-buffer buffer
  3002. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  3003. buffer)))
  3004. (org-file-contents "http://this-url-must-not-exist" :noerror))
  3005. (kill-buffer buffer))
  3006. t)))
  3007. ;;; Navigation
  3008. (ert-deftest test-org/forward-heading-same-level ()
  3009. "Test `org-forward-heading-same-level' specifications."
  3010. ;; Test navigation at top level, forward and backward.
  3011. (should
  3012. (equal "* H2"
  3013. (org-test-with-temp-text "* H1\n* H2"
  3014. (org-forward-heading-same-level 1)
  3015. (buffer-substring-no-properties (point) (line-end-position)))))
  3016. (should
  3017. (equal "* H1"
  3018. (org-test-with-temp-text "* H1\n<point>* H2"
  3019. (org-forward-heading-same-level -1)
  3020. (buffer-substring-no-properties (point) (line-end-position)))))
  3021. ;; Test navigation in a sub-tree, forward and backward.
  3022. (should
  3023. (equal "* H2"
  3024. (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
  3025. (org-forward-heading-same-level 1)
  3026. (buffer-substring-no-properties (point) (line-end-position)))))
  3027. (should
  3028. (equal "* H1"
  3029. (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
  3030. (org-forward-heading-same-level -1)
  3031. (buffer-substring-no-properties (point) (line-end-position)))))
  3032. ;; Stop at first or last sub-heading.
  3033. (should-not
  3034. (equal "* H2"
  3035. (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
  3036. (org-forward-heading-same-level 1)
  3037. (buffer-substring-no-properties (point) (line-end-position)))))
  3038. (should-not
  3039. (equal "* H2"
  3040. (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
  3041. (org-forward-heading-same-level -1)
  3042. (buffer-substring-no-properties (point) (line-end-position)))))
  3043. ;; Allow multiple moves.
  3044. (should
  3045. (equal "* H3"
  3046. (org-test-with-temp-text "* H1\n* H2\n* H3"
  3047. (org-forward-heading-same-level 2)
  3048. (buffer-substring-no-properties (point) (line-end-position)))))
  3049. (should
  3050. (equal "* H1"
  3051. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3052. (org-forward-heading-same-level -2)
  3053. (buffer-substring-no-properties (point) (line-end-position)))))
  3054. ;; Ignore spurious moves when first (or last) sibling is reached.
  3055. (should
  3056. (equal "** H3"
  3057. (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
  3058. (org-forward-heading-same-level 100)
  3059. (buffer-substring-no-properties (point) (line-end-position)))))
  3060. (should
  3061. (equal "** H1"
  3062. (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
  3063. (org-forward-heading-same-level -100)
  3064. (buffer-substring-no-properties (point) (line-end-position))))))
  3065. (ert-deftest test-org/end-of-meta-data ()
  3066. "Test `org-end-of-meta-data' specifications."
  3067. ;; Skip planning line.
  3068. (should
  3069. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  3070. (org-end-of-meta-data)
  3071. (eobp)))
  3072. ;; Skip properties drawer.
  3073. (should
  3074. (org-test-with-temp-text
  3075. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  3076. (org-end-of-meta-data)
  3077. (eobp)))
  3078. ;; Skip both.
  3079. (should
  3080. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  3081. (org-end-of-meta-data)
  3082. (eobp)))
  3083. ;; Nothing to skip, go to first line.
  3084. (should
  3085. (org-test-with-temp-text "* Headline\nContents"
  3086. (org-end-of-meta-data)
  3087. (looking-at "Contents")))
  3088. ;; With option argument, skip empty lines, regular drawers and
  3089. ;; clocking lines.
  3090. (should
  3091. (org-test-with-temp-text "* Headline\n\nContents"
  3092. (org-end-of-meta-data t)
  3093. (looking-at "Contents")))
  3094. (should
  3095. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  3096. (org-end-of-meta-data t)
  3097. (looking-at "Contents")))
  3098. (should
  3099. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  3100. (org-end-of-meta-data t)
  3101. (looking-at "Contents")))
  3102. ;; Special case: do not skip incomplete drawers.
  3103. (should
  3104. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  3105. (org-end-of-meta-data t)
  3106. (looking-at ":LOGBOOK:")))
  3107. ;; Special case: Be careful about consecutive headlines.
  3108. (should-not
  3109. (org-test-with-temp-text "* H1\n*H2\nContents"
  3110. (org-end-of-meta-data t)
  3111. (looking-at "Contents"))))
  3112. (ert-deftest test-org/beginning-of-line ()
  3113. "Test `org-beginning-of-line' specifications."
  3114. ;; Move to beginning of line. If current line in invisible, move to
  3115. ;; beginning of visible line instead.
  3116. (should
  3117. (org-test-with-temp-text "Some text\nSome other text<point>"
  3118. (org-beginning-of-line)
  3119. (bolp)))
  3120. (should
  3121. (org-test-with-temp-text "* H1\n** H2<point>"
  3122. (org-overview)
  3123. (org-beginning-of-line)
  3124. (= (line-beginning-position) 1)))
  3125. ;; With `visual-line-mode' active, move to beginning of visual line.
  3126. (should-not
  3127. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3128. (visual-line-mode)
  3129. (dotimes (i 1000) (insert "very "))
  3130. (org-beginning-of-line)
  3131. (bolp)))
  3132. ;; In a wide headline, with `visual-line-mode', prefer going to the
  3133. ;; beginning of a visual line than to the logical beginning of line,
  3134. ;; even if special movement is active.
  3135. (should-not
  3136. (org-test-with-temp-text "* A <point>long headline"
  3137. (visual-line-mode)
  3138. (dotimes (i 1000) (insert "very "))
  3139. (goto-char (point-max))
  3140. (org-beginning-of-line)
  3141. (bobp)))
  3142. (should-not
  3143. (org-test-with-temp-text "* A <point>long headline"
  3144. (visual-line-mode)
  3145. (dotimes (i 1000) (insert "very "))
  3146. (goto-char (point-max))
  3147. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  3148. (bobp)))
  3149. ;; At an headline with special movement, first move at beginning of
  3150. ;; title, then at the beginning of line, rinse, repeat.
  3151. (should
  3152. (org-test-with-temp-text "* TODO Headline<point>"
  3153. (let ((org-special-ctrl-a/e t))
  3154. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  3155. (progn (org-beginning-of-line) (bolp))
  3156. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3157. (should
  3158. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3159. (let ((org-special-ctrl-a/e t))
  3160. (org-beginning-of-line)
  3161. (looking-at "Headline"))))
  3162. (should
  3163. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3164. (let ((org-special-ctrl-a/e '(t . nil)))
  3165. (org-beginning-of-line)
  3166. (looking-at "Headline"))))
  3167. (should-not
  3168. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3169. (let ((org-special-ctrl-a/e '(nil . nil)))
  3170. (org-beginning-of-line)
  3171. (looking-at "Headline"))))
  3172. ;; At an headline with reversed movement, first move to beginning of
  3173. ;; line, then to the beginning of title.
  3174. (should
  3175. (org-test-with-temp-text "* TODO Headline<point>"
  3176. (let ((org-special-ctrl-a/e 'reversed)
  3177. (this-command last-command))
  3178. (and (progn (org-beginning-of-line) (bolp))
  3179. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3180. (should
  3181. (org-test-with-temp-text "* TODO Headline<point>"
  3182. (let ((org-special-ctrl-a/e '(reversed . nil))
  3183. (this-command last-command))
  3184. (and (progn (org-beginning-of-line) (bolp))
  3185. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3186. (should-not
  3187. (org-test-with-temp-text "* TODO Headline<point>"
  3188. (let ((org-special-ctrl-a/e '(t . nil))
  3189. (this-command last-command))
  3190. (and (progn (org-beginning-of-line) (bolp))
  3191. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3192. ;; At an item with special movement, first move after to beginning
  3193. ;; of title, then to the beginning of line, rinse, repeat.
  3194. (should
  3195. (org-test-with-temp-text "- [ ] Item<point>"
  3196. (let ((org-special-ctrl-a/e t))
  3197. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  3198. (progn (org-beginning-of-line) (bolp))
  3199. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3200. ;; At an item with reversed movement, first move to beginning of
  3201. ;; line, then to the beginning of title.
  3202. (should
  3203. (org-test-with-temp-text "- [X] Item<point>"
  3204. (let ((org-special-ctrl-a/e 'reversed)
  3205. (this-command last-command))
  3206. (and (progn (org-beginning-of-line) (bolp))
  3207. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3208. ;; Leave point before invisible characters at column 0.
  3209. (should
  3210. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3211. (let ((org-special-ctrl-a/e nil))
  3212. (org-beginning-of-line)
  3213. (bolp))))
  3214. (should
  3215. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3216. (let ((org-special-ctrl-a/e t))
  3217. (org-beginning-of-line)
  3218. (bolp))))
  3219. (should
  3220. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  3221. (visual-line-mode)
  3222. (org-beginning-of-line)
  3223. (bolp)))
  3224. ;; Special case: Do not error when the buffer contains only a single
  3225. ;; asterisk.
  3226. (should
  3227. (org-test-with-temp-text "*<point>"
  3228. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  3229. (should
  3230. (org-test-with-temp-text "*<point>"
  3231. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  3232. (ert-deftest test-org/end-of-line ()
  3233. "Test `org-end-of-line' specifications."
  3234. ;; Standard test.
  3235. (should
  3236. (org-test-with-temp-text "Some text\nSome other text"
  3237. (org-end-of-line)
  3238. (eolp)))
  3239. ;; With `visual-line-mode' active, move to end of visible line.
  3240. ;; However, never go past ellipsis.
  3241. (should-not
  3242. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3243. (visual-line-mode)
  3244. (dotimes (i 1000) (insert "very "))
  3245. (goto-char (point-min))
  3246. (org-end-of-line)
  3247. (eolp)))
  3248. (should-not
  3249. (org-test-with-temp-text "* A short headline\nSome contents"
  3250. (visual-line-mode)
  3251. (org-overview)
  3252. (org-end-of-line)
  3253. (eobp)))
  3254. ;; In a wide headline, with `visual-line-mode', prefer going to end
  3255. ;; of visible line if tags, or end of line, are farther.
  3256. (should-not
  3257. (org-test-with-temp-text "* A <point>long headline"
  3258. (visual-line-mode)
  3259. (dotimes (i 1000) (insert "very "))
  3260. (goto-char (point-min))
  3261. (org-end-of-line)
  3262. (eolp)))
  3263. (should-not
  3264. (org-test-with-temp-text "* A <point>long headline :tag:"
  3265. (visual-line-mode)
  3266. (dotimes (i 1000) (insert "very "))
  3267. (goto-char (point-min))
  3268. (org-end-of-line)
  3269. (eolp)))
  3270. ;; At an headline without special movement, go to end of line.
  3271. ;; However, never go past ellipsis.
  3272. (should
  3273. (org-test-with-temp-text "* Headline2b :tag:\n"
  3274. (let ((org-special-ctrl-a/e nil))
  3275. (and (progn (org-end-of-line) (eolp))
  3276. (progn (org-end-of-line) (eolp))))))
  3277. (should
  3278. (org-test-with-temp-text "* Headline2b :tag:\n"
  3279. (let ((org-special-ctrl-a/e '(t . nil)))
  3280. (and (progn (org-end-of-line) (eolp))
  3281. (progn (org-end-of-line) (eolp))))))
  3282. (should
  3283. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3284. (org-overview)
  3285. (let ((org-special-ctrl-a/e nil))
  3286. (org-end-of-line)
  3287. (= 1 (line-beginning-position)))))
  3288. ;; At an headline with special movement, first move before tags,
  3289. ;; then at the end of line, rinse, repeat. However, never go past
  3290. ;; ellipsis.
  3291. (should
  3292. (org-test-with-temp-text "* Headline1 :tag:\n"
  3293. (let ((org-special-ctrl-a/e t))
  3294. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3295. (progn (org-end-of-line) (eolp))
  3296. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3297. (should
  3298. (org-test-with-temp-text "* Headline1 :tag:\n"
  3299. (let ((org-special-ctrl-a/e '(nil . t)))
  3300. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3301. (progn (org-end-of-line) (eolp))
  3302. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3303. (should-not
  3304. (org-test-with-temp-text "* Headline1 :tag:\n"
  3305. (let ((org-special-ctrl-a/e '(nil . nil)))
  3306. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3307. (progn (org-end-of-line) (eolp))
  3308. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3309. (should
  3310. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3311. (org-overview)
  3312. (let ((org-special-ctrl-a/e t))
  3313. (org-end-of-line)
  3314. (org-end-of-line)
  3315. (= 1 (line-beginning-position)))))
  3316. ;; At an headline, with reversed movement, first go to end of line,
  3317. ;; then before tags. However, never go past ellipsis.
  3318. (should
  3319. (org-test-with-temp-text "* Headline3 :tag:\n"
  3320. (let ((org-special-ctrl-a/e 'reversed)
  3321. (this-command last-command))
  3322. (and (progn (org-end-of-line) (eolp))
  3323. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3324. (should
  3325. (org-test-with-temp-text "* Headline3 :tag:\n"
  3326. (let ((org-special-ctrl-a/e '(nil . reversed))
  3327. (this-command last-command))
  3328. (and (progn (org-end-of-line) (eolp))
  3329. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3330. (should-not
  3331. (org-test-with-temp-text "* Headline3 :tag:\n"
  3332. (let ((org-special-ctrl-a/e '(nil . t))
  3333. (this-command last-command))
  3334. (and (progn (org-end-of-line) (eolp))
  3335. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3336. (should
  3337. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3338. (org-overview)
  3339. (let ((org-special-ctrl-a/e 'reversed))
  3340. (org-end-of-line)
  3341. (= 1 (line-beginning-position)))))
  3342. ;; At a block without hidden contents.
  3343. (should
  3344. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3345. (progn (org-end-of-line) (eolp))))
  3346. ;; At a block with hidden contents.
  3347. (should-not
  3348. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3349. (let ((org-special-ctrl-a/e t))
  3350. (org-hide-block-toggle)
  3351. (org-end-of-line)
  3352. (eobp))))
  3353. ;; Get past invisible characters at the end of line.
  3354. (should
  3355. (org-test-with-temp-text "[[https://orgmode.org]]"
  3356. (org-end-of-line)
  3357. (eolp))))
  3358. (ert-deftest test-org/open-line ()
  3359. "Test `org-open-line' specifications."
  3360. ;; Call `open-line' outside of tables.
  3361. (should
  3362. (equal "\nText"
  3363. (org-test-with-temp-text "Text"
  3364. (org-open-line 1)
  3365. (buffer-string))))
  3366. ;; At a table, create a row above.
  3367. (should
  3368. (equal "\n| |\n| a |"
  3369. (org-test-with-temp-text "\n<point>| a |"
  3370. (org-open-line 1)
  3371. (buffer-string))))
  3372. ;; At the very first character of the buffer, also call `open-line'.
  3373. (should
  3374. (equal "\n| a |"
  3375. (org-test-with-temp-text "| a |"
  3376. (org-open-line 1)
  3377. (buffer-string))))
  3378. ;; Narrowing does not count.
  3379. (should
  3380. (equal "Text\n| |\n| a |"
  3381. (org-test-with-temp-text "Text\n<point>| a |"
  3382. (narrow-to-region (point) (point-max))
  3383. (org-open-line 1)
  3384. (widen)
  3385. (buffer-string)))))
  3386. (ert-deftest test-org/forward-sentence ()
  3387. "Test `org-forward-sentence' specifications."
  3388. ;; At the end of a table cell, move to the end of the next one.
  3389. (should
  3390. (org-test-with-temp-text "| a<point> | b |"
  3391. (org-forward-sentence)
  3392. (looking-at " |$")))
  3393. ;; Elsewhere in a cell, move to its end.
  3394. (should
  3395. (org-test-with-temp-text "| a<point>c | b |"
  3396. (org-forward-sentence)
  3397. (looking-at " | b |$")))
  3398. ;; Otherwise, simply call `forward-sentence'.
  3399. (should
  3400. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3401. (org-forward-sentence)
  3402. (looking-at " Sentence 2.")))
  3403. (should
  3404. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3405. (org-forward-sentence)
  3406. (org-forward-sentence)
  3407. (eobp)))
  3408. ;; At the end of an element, jump to the next one, without stopping
  3409. ;; on blank lines in-between.
  3410. (should
  3411. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  3412. (org-forward-sentence)
  3413. (eobp)))
  3414. ;; Headlines are considered to be sentences by themselves, even if
  3415. ;; they do not end with a full stop.
  3416. (should
  3417. (equal
  3418. "* Headline"
  3419. (org-test-with-temp-text "* <point>Headline\nSentence."
  3420. (org-forward-sentence)
  3421. (buffer-substring-no-properties (line-beginning-position) (point)))))
  3422. (should
  3423. (org-test-with-temp-text "* Headline<point>\nSentence."
  3424. (org-forward-sentence)
  3425. (eobp)))
  3426. (should
  3427. (org-test-with-temp-text "Sentence.<point>\n\n* Headline\n\nSentence 2."
  3428. (org-forward-sentence)
  3429. (and (org-at-heading-p) (eolp)))))
  3430. (ert-deftest test-org/backward-sentence ()
  3431. "Test `org-backward-sentence' specifications."
  3432. ;; At the beginning of a table cell, move to the beginning of the
  3433. ;; previous one.
  3434. (should
  3435. (org-test-with-temp-text "| a | <point>b |"
  3436. (org-backward-sentence)
  3437. (looking-at "a | b |$")))
  3438. ;; Elsewhere in a cell, move to its beginning.
  3439. (should
  3440. (org-test-with-temp-text "| a | b<point>c |"
  3441. (org-backward-sentence)
  3442. (looking-at "bc |$")))
  3443. ;; Otherwise, simply call `backward-sentence'.
  3444. (should
  3445. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3446. (org-backward-sentence)
  3447. (looking-at "Sentence 2.")))
  3448. (should
  3449. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3450. (org-backward-sentence)
  3451. (org-backward-sentence)
  3452. (bobp)))
  3453. ;; Make sure to hit the beginning of a sentence on the same line as
  3454. ;; an item.
  3455. (should
  3456. (org-test-with-temp-text "- Line 1\n line <point>2."
  3457. (org-backward-sentence)
  3458. (looking-at "Line 1"))))
  3459. (ert-deftest test-org/forward-paragraph ()
  3460. "Test `org-forward-paragraph' specifications."
  3461. ;; At end of buffer, do not return an error.
  3462. (should
  3463. (org-test-with-temp-text "Paragraph"
  3464. (goto-char (point-max))
  3465. (org-forward-paragraph)
  3466. t))
  3467. ;; Standard test.
  3468. (should
  3469. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3470. (org-forward-paragraph)
  3471. (looking-at "P2")))
  3472. ;; Ignore depth.
  3473. (should
  3474. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  3475. (org-forward-paragraph)
  3476. (looking-at "P1")))
  3477. ;; Do not enter elements with invisible contents.
  3478. (should
  3479. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  3480. (org-hide-block-toggle)
  3481. (org-forward-paragraph)
  3482. (looking-at "P3")))
  3483. ;; On an affiliated keyword, jump to the beginning of the element.
  3484. (should
  3485. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  3486. (org-forward-paragraph)
  3487. (looking-at "Para")))
  3488. ;; On an item or a footnote definition, move to the second element
  3489. ;; inside, if any.
  3490. (should
  3491. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  3492. (org-forward-paragraph)
  3493. (looking-at " Paragraph")))
  3494. (should
  3495. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  3496. (org-forward-paragraph)
  3497. (looking-at "Paragraph")))
  3498. ;; On an item, or a footnote definition, when the first line is
  3499. ;; empty, move to the first item.
  3500. (should
  3501. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  3502. (org-forward-paragraph)
  3503. (looking-at " Paragraph")))
  3504. (should
  3505. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  3506. (org-forward-paragraph)
  3507. (looking-at "Paragraph")))
  3508. ;; On a table (resp. a property drawer) do not move through table
  3509. ;; rows (resp. node properties).
  3510. (should
  3511. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  3512. (org-forward-paragraph)
  3513. (looking-at "Paragraph")))
  3514. (should
  3515. (org-test-with-temp-text
  3516. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  3517. (org-forward-paragraph)
  3518. (looking-at "Paragraph")))
  3519. ;; On a verse or source block, stop after blank lines.
  3520. (should
  3521. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  3522. (org-forward-paragraph)
  3523. (looking-at "L2")))
  3524. (should
  3525. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  3526. (org-forward-paragraph)
  3527. (looking-at "L2"))))
  3528. (ert-deftest test-org/backward-paragraph ()
  3529. "Test `org-backward-paragraph' specifications."
  3530. ;; Do not error at beginning of buffer.
  3531. (should
  3532. (org-test-with-temp-text "Paragraph"
  3533. (org-backward-paragraph)
  3534. t))
  3535. ;; Regular test.
  3536. (should
  3537. (org-test-with-temp-text "P1\n\nP2\n\nP3<point>"
  3538. (org-backward-paragraph)
  3539. (looking-at "P3")))
  3540. (should
  3541. (org-test-with-temp-text "P1\n\nP2\n\n<point>P3"
  3542. (org-backward-paragraph)
  3543. (looking-at-p "P2")))
  3544. ;; Ignore depth.
  3545. (should
  3546. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\n<point>P3"
  3547. (org-backward-paragraph)
  3548. (looking-at-p "P2")))
  3549. ;; Ignore invisible elements.
  3550. (should
  3551. (org-test-with-temp-text "* H1\n P1\n* H2"
  3552. (org-cycle)
  3553. (goto-char (point-max))
  3554. (beginning-of-line)
  3555. (org-backward-paragraph)
  3556. (bobp)))
  3557. ;; On an affiliated keyword, jump to the first one.
  3558. (should
  3559. (org-test-with-temp-text
  3560. "P1\n#+name: n\n#+caption: c1\n#+caption: <point>c2\nP2"
  3561. (org-backward-paragraph)
  3562. (looking-at-p "#\\+name")))
  3563. ;; On the second element in an item or a footnote definition, jump
  3564. ;; to item or the definition.
  3565. (should
  3566. (org-test-with-temp-text "- line1\n\n<point> line2"
  3567. (org-backward-paragraph)
  3568. (looking-at-p "- line1")))
  3569. (should
  3570. (org-test-with-temp-text "[fn:1] line1\n\n<point> line2"
  3571. (org-backward-paragraph)
  3572. (looking-at-p "\\[fn:1\\] line1")))
  3573. ;; On a table (resp. a property drawer), ignore table rows
  3574. ;; (resp. node properties).
  3575. (should
  3576. (org-test-with-temp-text "| a | b |\n| c | d |\n<point>P1"
  3577. (org-backward-paragraph)
  3578. (bobp)))
  3579. (should
  3580. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  3581. (org-backward-paragraph)
  3582. (looking-at-p ":PROPERTIES:")))
  3583. ;; On a comment, example, src and verse blocks, stop before blank
  3584. ;; lines.
  3585. (should
  3586. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\n<point>L3\n#+END_VERSE"
  3587. (org-backward-paragraph)
  3588. (looking-at-p "L2")))
  3589. (should
  3590. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\n<point>L3#+END_SRC"
  3591. (org-backward-paragraph)
  3592. (looking-at-p "L2")))
  3593. ;; In comment, example, export, src and verse blocks, stop below
  3594. ;; opening line when called from within the block.
  3595. (should
  3596. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\nL2<point>\n#+END_VERSE"
  3597. (org-backward-paragraph)
  3598. (looking-at-p "L1")))
  3599. (should
  3600. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nL1\nL2<point>\n#+END_EXAMPLE"
  3601. (org-backward-paragraph)
  3602. (looking-at-p "L1")))
  3603. ;; When called from the opening line itself, however, move to
  3604. ;; beginning of block.
  3605. (should
  3606. (org-test-with-temp-text "#+BEGIN_<point>EXAMPLE\nL1\n#+END_EXAMPLE"
  3607. (org-backward-paragraph)
  3608. (bobp)))
  3609. ;; Pathological case: on an empty heading, move to its beginning.
  3610. (should
  3611. (org-test-with-temp-text "* <point>H"
  3612. (org-backward-paragraph)
  3613. (bobp))))
  3614. (ert-deftest test-org/forward-element ()
  3615. "Test `org-forward-element' specifications."
  3616. ;; 1. At EOB: should error.
  3617. (org-test-with-temp-text "Some text\n"
  3618. (goto-char (point-max))
  3619. (should-error (org-forward-element)))
  3620. ;; 2. Standard move: expected to ignore blank lines.
  3621. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  3622. (org-forward-element)
  3623. (should (looking-at (regexp-quote "Second paragraph."))))
  3624. ;; 3. Headline tests.
  3625. (org-test-with-temp-text "
  3626. * Head 1
  3627. ** Head 1.1
  3628. *** Head 1.1.1
  3629. ** Head 1.2"
  3630. ;; 3.1. At an headline beginning: move to next headline at the
  3631. ;; same level.
  3632. (goto-line 3)
  3633. (org-forward-element)
  3634. (should (looking-at (regexp-quote "** Head 1.2")))
  3635. ;; 3.2. At an headline beginning: move to parent headline if no
  3636. ;; headline at the same level.
  3637. (goto-line 3)
  3638. (org-forward-element)
  3639. (should (looking-at (regexp-quote "** Head 1.2"))))
  3640. ;; 4. Greater element tests.
  3641. (org-test-with-temp-text
  3642. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  3643. ;; 4.1. At a greater element: expected to skip contents.
  3644. (org-forward-element)
  3645. (should (looking-at (regexp-quote "Outside.")))
  3646. ;; 4.2. At the end of greater element contents: expected to skip
  3647. ;; to the end of the greater element.
  3648. (goto-line 2)
  3649. (org-forward-element)
  3650. (should (looking-at (regexp-quote "Outside."))))
  3651. ;; 5. List tests.
  3652. (org-test-with-temp-text "
  3653. - item1
  3654. - sub1
  3655. - sub2
  3656. - sub3
  3657. Inner paragraph.
  3658. - item2
  3659. Outside."
  3660. ;; 5.1. At list top point: expected to move to the element after
  3661. ;; the list.
  3662. (goto-line 2)
  3663. (org-forward-element)
  3664. (should (looking-at (regexp-quote "Outside.")))
  3665. ;; 5.2. Special case: at the first line of a sub-list, but not at
  3666. ;; beginning of line, move to next item.
  3667. (goto-line 2)
  3668. (forward-char)
  3669. (org-forward-element)
  3670. (should (looking-at "- item2"))
  3671. (goto-line 4)
  3672. (forward-char)
  3673. (org-forward-element)
  3674. (should (looking-at " - sub2"))
  3675. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  3676. (goto-line 4)
  3677. (org-forward-element)
  3678. (should (looking-at (regexp-quote " Inner paragraph.")))
  3679. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  3680. (goto-line 8)
  3681. (org-forward-element)
  3682. (should (looking-at (regexp-quote " Inner paragraph.")))
  3683. ;; 5.5. At an item: expected to move to next item, if any.
  3684. (goto-line 6)
  3685. (org-forward-element)
  3686. (should (looking-at " - sub3"))))
  3687. (ert-deftest test-org/backward-element ()
  3688. "Test `org-backward-element' specifications."
  3689. ;; 1. Should error at BOB.
  3690. (org-test-with-temp-text " \nParagraph."
  3691. (should-error (org-backward-element)))
  3692. ;; 2. Should move at BOB when called on the first element in buffer.
  3693. (should
  3694. (org-test-with-temp-text "\n#+TITLE: test"
  3695. (progn (forward-line)
  3696. (org-backward-element)
  3697. (bobp))))
  3698. ;; 3. Not at the beginning of an element: move at its beginning.
  3699. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3700. (goto-line 3)
  3701. (end-of-line)
  3702. (org-backward-element)
  3703. (should (looking-at (regexp-quote "Paragraph2."))))
  3704. ;; 4. Headline tests.
  3705. (org-test-with-temp-text "
  3706. * Head 1
  3707. ** Head 1.1
  3708. *** Head 1.1.1
  3709. ** Head 1.2"
  3710. ;; 4.1. At an headline beginning: move to previous headline at the
  3711. ;; same level.
  3712. (goto-line 5)
  3713. (org-backward-element)
  3714. (should (looking-at (regexp-quote "** Head 1.1")))
  3715. ;; 4.2. At an headline beginning: move to parent headline if no
  3716. ;; headline at the same level.
  3717. (goto-line 3)
  3718. (org-backward-element)
  3719. (should (looking-at (regexp-quote "* Head 1")))
  3720. ;; 4.3. At the first top-level headline: should error.
  3721. (goto-line 2)
  3722. (should-error (org-backward-element)))
  3723. ;; 5. At beginning of first element inside a greater element:
  3724. ;; expected to move to greater element's beginning.
  3725. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  3726. (goto-line 3)
  3727. (org-backward-element)
  3728. (should (looking-at "#\\+BEGIN_CENTER")))
  3729. ;; 6. At the beginning of the first element in a section: should
  3730. ;; move back to headline, if any.
  3731. (should
  3732. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  3733. (progn (goto-char (point-max))
  3734. (beginning-of-line)
  3735. (org-backward-element)
  3736. (org-at-heading-p))))
  3737. ;; 7. List tests.
  3738. (org-test-with-temp-text "
  3739. - item1
  3740. - sub1
  3741. - sub2
  3742. - sub3
  3743. Inner paragraph.
  3744. - item2
  3745. Outside."
  3746. ;; 7.1. At beginning of sub-list: expected to move to the
  3747. ;; paragraph before it.
  3748. (goto-line 4)
  3749. (org-backward-element)
  3750. (should (looking-at "item1"))
  3751. ;; 7.2. At an item in a list: expected to move at previous item.
  3752. (goto-line 8)
  3753. (org-backward-element)
  3754. (should (looking-at " - sub2"))
  3755. (goto-line 12)
  3756. (org-backward-element)
  3757. (should (looking-at "- item1"))
  3758. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3759. ;; beginning.
  3760. (goto-line 10)
  3761. (org-backward-element)
  3762. (should (looking-at " - sub1"))
  3763. (goto-line 15)
  3764. (org-backward-element)
  3765. (should (looking-at "- item1"))
  3766. ;; 7.4. At blank-lines before list end: expected to move to top
  3767. ;; item.
  3768. (goto-line 14)
  3769. (org-backward-element)
  3770. (should (looking-at "- item1"))))
  3771. (ert-deftest test-org/up-element ()
  3772. "Test `org-up-element' specifications."
  3773. ;; 1. At BOB or with no surrounding element: should error.
  3774. (org-test-with-temp-text "Paragraph."
  3775. (should-error (org-up-element)))
  3776. (org-test-with-temp-text "* Head1\n* Head2"
  3777. (goto-line 2)
  3778. (should-error (org-up-element)))
  3779. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3780. (goto-line 3)
  3781. (should-error (org-up-element)))
  3782. ;; 2. At an headline: move to parent headline.
  3783. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3784. (goto-line 3)
  3785. (org-up-element)
  3786. (should (looking-at "\\* Head1")))
  3787. ;; 3. Inside a greater element: move to greater element beginning.
  3788. (org-test-with-temp-text
  3789. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3790. (goto-line 3)
  3791. (org-up-element)
  3792. (should (looking-at "#\\+BEGIN_CENTER")))
  3793. ;; 4. List tests.
  3794. (org-test-with-temp-text "* Top
  3795. - item1
  3796. - sub1
  3797. - sub2
  3798. Paragraph within sub2.
  3799. - item2"
  3800. ;; 4.1. Within an item: move to the item beginning.
  3801. (goto-line 8)
  3802. (org-up-element)
  3803. (should (looking-at " - sub2"))
  3804. ;; 4.2. At an item in a sub-list: move to parent item.
  3805. (goto-line 4)
  3806. (org-up-element)
  3807. (should (looking-at "- item1"))
  3808. ;; 4.3. At an item in top list: move to beginning of whole list.
  3809. (goto-line 10)
  3810. (org-up-element)
  3811. (should (looking-at "- item1"))
  3812. ;; 4.4. Special case. At very top point: should move to parent of
  3813. ;; list.
  3814. (goto-line 2)
  3815. (org-up-element)
  3816. (should (looking-at "\\* Top"))))
  3817. (ert-deftest test-org/down-element ()
  3818. "Test `org-down-element' specifications."
  3819. ;; Error when the element hasn't got a recursive type.
  3820. (org-test-with-temp-text "Paragraph."
  3821. (should-error (org-down-element)))
  3822. ;; Error when the element has no contents
  3823. (org-test-with-temp-text "* Headline"
  3824. (should-error (org-down-element)))
  3825. ;; When at a plain-list, move to first item.
  3826. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3827. (goto-line 2)
  3828. (org-down-element)
  3829. (should (looking-at " - Item 1.1")))
  3830. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3831. (org-down-element)
  3832. (should (looking-at " Item 1")))
  3833. ;; When at a table, move to first row
  3834. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3835. (org-down-element)
  3836. (should (looking-at " a | b |")))
  3837. ;; Otherwise, move inside the greater element.
  3838. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3839. (org-down-element)
  3840. (should (looking-at "Paragraph"))))
  3841. (ert-deftest test-org/drag-element-backward ()
  3842. "Test `org-drag-element-backward' specifications."
  3843. ;; Standard test.
  3844. (should
  3845. (equal
  3846. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3847. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3848. (org-drag-element-backward)
  3849. (buffer-string))))
  3850. (should
  3851. (equal
  3852. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3853. (org-test-with-temp-text
  3854. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3855. (org-drag-element-backward)
  3856. (buffer-string))))
  3857. ;; Preserve blank lines.
  3858. (should
  3859. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3860. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3861. (org-drag-element-backward)
  3862. (buffer-string))))
  3863. ;; Preserve column.
  3864. (should
  3865. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3866. (org-drag-element-backward)
  3867. (looking-at-p "2")))
  3868. ;; Error when trying to move first element of buffer.
  3869. (should-error
  3870. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3871. (org-drag-element-backward))
  3872. :type 'user-error)
  3873. ;; Error when trying to swap nested elements.
  3874. (should-error
  3875. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  3876. (org-drag-element-backward))
  3877. :type 'user-error)
  3878. ;; Error when trying to swap an headline element and a non-headline
  3879. ;; element.
  3880. (should-error
  3881. (org-test-with-temp-text "Test.\n<point>* Head 1"
  3882. (org-drag-element-backward))
  3883. :type 'error)
  3884. ;; Error when called before first element.
  3885. (should-error
  3886. (org-test-with-temp-text "\n<point>"
  3887. (org-drag-element-backward))
  3888. :type 'user-error)
  3889. ;; Preserve visibility of elements and their contents.
  3890. (should
  3891. (equal '((63 . 82) (26 . 48))
  3892. (org-test-with-temp-text "
  3893. #+BEGIN_CENTER
  3894. Text.
  3895. #+END_CENTER
  3896. - item 1
  3897. #+BEGIN_QUOTE
  3898. Text.
  3899. #+END_QUOTE"
  3900. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3901. (search-backward "- item 1")
  3902. (org-drag-element-backward)
  3903. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3904. (overlays-in (point-min) (point-max))))))
  3905. ;; Pathological case: handle call with point in blank lines right
  3906. ;; after a headline.
  3907. (should
  3908. (equal "* H2\n\n* H1\nText\n"
  3909. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  3910. (org-drag-element-backward)
  3911. (buffer-string)))))
  3912. (ert-deftest test-org/drag-element-forward ()
  3913. "Test `org-drag-element-forward' specifications."
  3914. ;; 1. Error when trying to move first element of buffer.
  3915. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3916. (goto-line 3)
  3917. (should-error (org-drag-element-forward)))
  3918. ;; 2. Error when trying to swap nested elements.
  3919. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3920. (forward-line)
  3921. (should-error (org-drag-element-forward)))
  3922. ;; 3. Error when trying to swap a non-headline element and an
  3923. ;; headline.
  3924. (org-test-with-temp-text "Test.\n* Head 1"
  3925. (should-error (org-drag-element-forward)))
  3926. ;; 4. Error when called before first element.
  3927. (should-error
  3928. (org-test-with-temp-text "\n"
  3929. (forward-line)
  3930. (org-drag-element-backward))
  3931. :type 'user-error)
  3932. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3933. ;; between elements.
  3934. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3935. (search-forward "graph")
  3936. (org-drag-element-forward)
  3937. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3938. (should (looking-at " 1")))
  3939. ;; 5. Preserve visibility of elements and their contents.
  3940. (org-test-with-temp-text "
  3941. #+BEGIN_CENTER
  3942. Text.
  3943. #+END_CENTER
  3944. - item 1
  3945. #+BEGIN_QUOTE
  3946. Text.
  3947. #+END_QUOTE"
  3948. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3949. (search-backward "#+BEGIN_CENTER")
  3950. (org-drag-element-forward)
  3951. (should
  3952. (equal
  3953. '((63 . 82) (26 . 48))
  3954. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3955. (overlays-in (point-min) (point-max)))))))
  3956. (ert-deftest test-org/next-block ()
  3957. "Test `org-next-block' specifications."
  3958. ;; Regular test.
  3959. (should
  3960. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3961. (org-next-block 1)
  3962. (looking-at "#\\+BEGIN_CENTER")))
  3963. ;; Ignore case.
  3964. (should
  3965. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3966. (let ((case-fold-search nil))
  3967. (org-next-block 1)
  3968. (looking-at "#\\+begin_center"))))
  3969. ;; Ignore current line.
  3970. (should
  3971. (org-test-with-temp-text
  3972. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3973. (org-next-block 1)
  3974. (looking-at "#\\+BEGIN_CENTER")))
  3975. ;; Throw an error when no block is found.
  3976. (should-error
  3977. (org-test-with-temp-text "Paragraph"
  3978. (org-next-block 1)))
  3979. ;; With an argument, skip many blocks at once.
  3980. (should
  3981. (org-test-with-temp-text
  3982. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3983. (org-next-block 2)
  3984. (looking-at "#\\+BEGIN_QUOTE")))
  3985. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3986. (should
  3987. (org-test-with-temp-text
  3988. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3989. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3990. (looking-at "#\\+BEGIN_QUOTE")))
  3991. ;; Optional argument is also case-insensitive.
  3992. (should
  3993. (org-test-with-temp-text
  3994. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3995. (let ((case-fold-search nil))
  3996. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3997. (looking-at "#\\+begin_quote")))))
  3998. (ert-deftest test-org/insert-structure-template ()
  3999. "Test `org-insert-structure-template'."
  4000. ;; Test in empty buffer.
  4001. (should
  4002. (string= "#+begin_foo\n#+end_foo\n"
  4003. (org-test-with-temp-text ""
  4004. (org-insert-structure-template "foo")
  4005. (buffer-string))))
  4006. ;; Test with multiple lines in buffer.
  4007. (should
  4008. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4009. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4010. (transient-mark-mode 1)
  4011. (org-mark-element)
  4012. (org-insert-structure-template "foo")
  4013. (buffer-string))))
  4014. ;; Mark only the current line.
  4015. (should
  4016. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4017. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4018. (transient-mark-mode 1)
  4019. (set-mark (point-min))
  4020. (end-of-line)
  4021. (org-insert-structure-template "foo")
  4022. (buffer-string))))
  4023. ;; Middle of paragraph.
  4024. (should
  4025. (string= "p1\n#+begin_foo\np2\n#+end_foo\np3"
  4026. (org-test-with-temp-text "p1\n<point>p2\np3"
  4027. (set-mark (line-beginning-position))
  4028. (end-of-line)
  4029. (activate-mark)
  4030. (org-insert-structure-template "foo")
  4031. (buffer-string))))
  4032. ;; Test with text in buffer, no region, no final newline.
  4033. (should
  4034. (string= "#+begin_foo\nI'm a paragraph.\n#+end_foo\n"
  4035. (org-test-with-temp-text "I'm a paragraph."
  4036. (org-mark-element)
  4037. (org-insert-structure-template "foo")
  4038. (buffer-string))))
  4039. ;; Test with text in buffer and region set.
  4040. (should
  4041. (string= "#+begin_foo\nI'm a paragraph\n\nI'm a second paragrah\n#+end_foo\n"
  4042. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragrah"
  4043. (set-mark (point))
  4044. (goto-char (point-max))
  4045. (org-insert-structure-template "foo")
  4046. (buffer-string))))
  4047. ;; Test with example escaping.
  4048. (should
  4049. (string= "#+begin_example\n,* Heading\n#+end_example\n"
  4050. (org-test-with-temp-text "* Heading"
  4051. (org-mark-element)
  4052. (org-insert-structure-template "example")
  4053. (buffer-string))))
  4054. ;; Test with indentation.
  4055. (should
  4056. (string= " #+begin_foo\n This is a paragraph\n #+end_foo\n"
  4057. (org-test-with-temp-text " This is a paragraph"
  4058. (org-mark-element)
  4059. (org-insert-structure-template "foo")
  4060. (buffer-string))))
  4061. (should
  4062. (string= " #+begin_foo\n Line 1\n Line2\n #+end_foo\n"
  4063. (org-test-with-temp-text " Line 1\n Line2"
  4064. (org-mark-element)
  4065. (org-insert-structure-template "foo")
  4066. (buffer-string))))
  4067. ;; Test point location.
  4068. (should
  4069. (string= "#+begin_foo\n"
  4070. (org-test-with-temp-text ""
  4071. (org-insert-structure-template "foo")
  4072. (buffer-substring (point-min) (point)))))
  4073. (should
  4074. (string= "#+begin_src "
  4075. (org-test-with-temp-text ""
  4076. (org-insert-structure-template "src")
  4077. (buffer-substring (point-min) (point))))))
  4078. (ert-deftest test-org/previous-block ()
  4079. "Test `org-previous-block' specifications."
  4080. ;; Regular test.
  4081. (should
  4082. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  4083. (org-previous-block 1)
  4084. (looking-at "#\\+BEGIN_CENTER")))
  4085. ;; Ignore case.
  4086. (should
  4087. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  4088. (let ((case-fold-search nil))
  4089. (org-previous-block 1)
  4090. (looking-at "#\\+begin_center"))))
  4091. ;; Ignore current line.
  4092. (should
  4093. (org-test-with-temp-text
  4094. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  4095. (org-previous-block 1)
  4096. (looking-at "#\\+BEGIN_QUOTE")))
  4097. ;; Throw an error when no block is found.
  4098. (should-error
  4099. (org-test-with-temp-text "Paragraph<point>"
  4100. (org-previous-block 1)))
  4101. ;; With an argument, skip many blocks at once.
  4102. (should
  4103. (org-test-with-temp-text
  4104. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4105. (org-previous-block 2)
  4106. (looking-at "#\\+BEGIN_CENTER")))
  4107. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  4108. (should
  4109. (org-test-with-temp-text
  4110. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4111. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4112. (looking-at "#\\+BEGIN_QUOTE")))
  4113. ;; Optional argument is also case-insensitive.
  4114. (should
  4115. (org-test-with-temp-text
  4116. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  4117. (let ((case-fold-search nil))
  4118. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4119. (looking-at "#\\+begin_quote")))))
  4120. ;;; Outline structure
  4121. (ert-deftest test-org/demote ()
  4122. "Test `org-demote' specifications."
  4123. ;; Add correct number of stars according to `org-odd-levels-only'.
  4124. (should
  4125. (= 2
  4126. (org-test-with-temp-text "* H"
  4127. (let ((org-odd-levels-only nil)) (org-demote))
  4128. (org-current-level))))
  4129. (should
  4130. (= 3
  4131. (org-test-with-temp-text "* H"
  4132. (let ((org-odd-levels-only t)) (org-demote))
  4133. (org-current-level))))
  4134. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4135. (should
  4136. (org-test-with-temp-text "* H :tag:"
  4137. (let ((org-tags-column 10)
  4138. (org-auto-align-tags t)
  4139. (org-odd-levels-only nil))
  4140. (org-demote))
  4141. (org-move-to-column 10)
  4142. (looking-at-p ":tag:$")))
  4143. (should-not
  4144. (org-test-with-temp-text "* H :tag:"
  4145. (let ((org-tags-column 10)
  4146. (org-auto-align-tags nil)
  4147. (org-odd-levels-only nil))
  4148. (org-demote))
  4149. (org-move-to-column 10)
  4150. (looking-at-p ":tag:$")))
  4151. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4152. ;; info and property drawers accordingly.
  4153. (should
  4154. (= 3
  4155. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4156. (let ((org-odd-levels-only nil)
  4157. (org-adapt-indentation t))
  4158. (org-demote))
  4159. (forward-line)
  4160. (org-get-indentation))))
  4161. (should
  4162. (= 3
  4163. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4164. (let ((org-odd-levels-only nil)
  4165. (org-adapt-indentation t))
  4166. (org-demote))
  4167. (forward-line)
  4168. (org-get-indentation))))
  4169. (should-not
  4170. (= 3
  4171. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4172. (let ((org-odd-levels-only nil)
  4173. (org-adapt-indentation nil))
  4174. (org-demote))
  4175. (forward-line)
  4176. (org-get-indentation))))
  4177. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4178. ;; section accordingly. Ignore, however, footnote definitions and
  4179. ;; inlinetasks boundaries.
  4180. (should
  4181. (= 3
  4182. (org-test-with-temp-text "* H\n Paragraph"
  4183. (let ((org-odd-levels-only nil)
  4184. (org-adapt-indentation t))
  4185. (org-demote))
  4186. (forward-line)
  4187. (org-get-indentation))))
  4188. (should
  4189. (= 2
  4190. (org-test-with-temp-text "* H\n Paragraph"
  4191. (let ((org-odd-levels-only nil)
  4192. (org-adapt-indentation nil))
  4193. (org-demote))
  4194. (forward-line)
  4195. (org-get-indentation))))
  4196. (should
  4197. (zerop
  4198. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  4199. (let ((org-odd-levels-only nil)
  4200. (org-adapt-indentation t))
  4201. (org-demote))
  4202. (goto-char (point-max))
  4203. (org-get-indentation))))
  4204. (should
  4205. (= 3
  4206. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  4207. (let ((org-odd-levels-only nil)
  4208. (org-adapt-indentation t))
  4209. (org-demote))
  4210. (goto-char (point-max))
  4211. (org-get-indentation))))
  4212. (when (featurep 'org-inlinetask)
  4213. (should
  4214. (zerop
  4215. (let ((org-inlinetask-min-level 5)
  4216. (org-adapt-indentation t))
  4217. (org-test-with-temp-text "* H\n***** I\n***** END"
  4218. (org-demote)
  4219. (forward-line)
  4220. (org-get-indentation))))))
  4221. (when (featurep 'org-inlinetask)
  4222. (should
  4223. (= 3
  4224. (let ((org-inlinetask-min-level 5)
  4225. (org-adapt-indentation t))
  4226. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  4227. (org-demote)
  4228. (forward-line 2)
  4229. (org-get-indentation))))))
  4230. ;; Ignore contents of source blocks or example blocks when
  4231. ;; indentation should be preserved (through
  4232. ;; `org-src-preserve-indentation' or "-i" flag).
  4233. (should-not
  4234. (zerop
  4235. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4236. (let ((org-adapt-indentation t)
  4237. (org-src-preserve-indentation nil))
  4238. (org-demote))
  4239. (forward-line 2)
  4240. (org-get-indentation))))
  4241. (should
  4242. (zerop
  4243. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  4244. (let ((org-adapt-indentation t)
  4245. (org-src-preserve-indentation t))
  4246. (org-demote))
  4247. (forward-line 2)
  4248. (org-get-indentation))))
  4249. (should
  4250. (zerop
  4251. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4252. (let ((org-adapt-indentation t)
  4253. (org-src-preserve-indentation t))
  4254. (org-demote))
  4255. (forward-line 2)
  4256. (org-get-indentation))))
  4257. (should
  4258. (zerop
  4259. (org-test-with-temp-text
  4260. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  4261. (let ((org-adapt-indentation t)
  4262. (org-src-preserve-indentation nil))
  4263. (org-demote))
  4264. (forward-line 2)
  4265. (org-get-indentation)))))
  4266. (ert-deftest test-org/promote ()
  4267. "Test `org-promote' specifications."
  4268. ;; Return an error if headline is to be promoted to level 0, unless
  4269. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  4270. ;; headline becomes a comment.
  4271. (should-error
  4272. (org-test-with-temp-text "* H"
  4273. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  4274. (should
  4275. (equal "# H"
  4276. (org-test-with-temp-text "* H"
  4277. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  4278. (buffer-string))))
  4279. ;; Remove correct number of stars according to
  4280. ;; `org-odd-levels-only'.
  4281. (should
  4282. (= 2
  4283. (org-test-with-temp-text "*** H"
  4284. (let ((org-odd-levels-only nil)) (org-promote))
  4285. (org-current-level))))
  4286. (should
  4287. (= 1
  4288. (org-test-with-temp-text "*** H"
  4289. (let ((org-odd-levels-only t)) (org-promote))
  4290. (org-current-level))))
  4291. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4292. (should
  4293. (org-test-with-temp-text "** H :tag:"
  4294. (let ((org-tags-column 10)
  4295. (org-auto-align-tags t)
  4296. (org-odd-levels-only nil))
  4297. (org-promote))
  4298. (org-move-to-column 10)
  4299. (looking-at-p ":tag:$")))
  4300. (should-not
  4301. (org-test-with-temp-text "** H :tag:"
  4302. (let ((org-tags-column 10)
  4303. (org-auto-align-tags nil)
  4304. (org-odd-levels-only nil))
  4305. (org-promote))
  4306. (org-move-to-column 10)
  4307. (looking-at-p ":tag:$")))
  4308. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4309. ;; info and property drawers.
  4310. (should
  4311. (= 2
  4312. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4313. (let ((org-odd-levels-only nil)
  4314. (org-adapt-indentation t))
  4315. (org-promote))
  4316. (forward-line)
  4317. (org-get-indentation))))
  4318. (should
  4319. (= 2
  4320. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4321. (let ((org-odd-levels-only nil)
  4322. (org-adapt-indentation t))
  4323. (org-promote))
  4324. (forward-line)
  4325. (org-get-indentation))))
  4326. (should-not
  4327. (= 2
  4328. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4329. (let ((org-odd-levels-only nil)
  4330. (org-adapt-indentation nil))
  4331. (org-promote))
  4332. (forward-line)
  4333. (org-get-indentation))))
  4334. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4335. ;; section accordingly. Ignore, however, footnote definitions and
  4336. ;; inlinetasks boundaries.
  4337. (should
  4338. (= 2
  4339. (org-test-with-temp-text "** H\n Paragraph"
  4340. (let ((org-odd-levels-only nil)
  4341. (org-adapt-indentation t))
  4342. (org-promote))
  4343. (forward-line)
  4344. (org-get-indentation))))
  4345. (should-not
  4346. (= 2
  4347. (org-test-with-temp-text "** H\n Paragraph"
  4348. (let ((org-odd-levels-only nil)
  4349. (org-adapt-indentation nil))
  4350. (org-promote))
  4351. (forward-line)
  4352. (org-get-indentation))))
  4353. (should
  4354. (= 2
  4355. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  4356. (let ((org-odd-levels-only nil)
  4357. (org-adapt-indentation t))
  4358. (org-promote))
  4359. (forward-line)
  4360. (org-get-indentation))))
  4361. (when (featurep 'org-inlinetask)
  4362. (should
  4363. (zerop
  4364. (let ((org-inlinetask-min-level 5)
  4365. (org-adapt-indentation t))
  4366. (org-test-with-temp-text "** H\n***** I\n***** END"
  4367. (org-promote)
  4368. (forward-line)
  4369. (org-get-indentation))))))
  4370. (when (featurep 'org-inlinetask)
  4371. (should
  4372. (= 2
  4373. (let ((org-inlinetask-min-level 5)
  4374. (org-adapt-indentation t))
  4375. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  4376. (org-promote)
  4377. (forward-line 2)
  4378. (org-get-indentation))))))
  4379. ;; Give up shifting if it would break document's structure
  4380. ;; otherwise.
  4381. (should
  4382. (= 3
  4383. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  4384. (let ((org-odd-levels-only nil)
  4385. (org-adapt-indentation t))
  4386. (org-promote))
  4387. (forward-line)
  4388. (org-get-indentation))))
  4389. (should
  4390. (= 3
  4391. (org-test-with-temp-text "** H\n Paragraph\n * list."
  4392. (let ((org-odd-levels-only nil)
  4393. (org-adapt-indentation t))
  4394. (org-promote))
  4395. (forward-line)
  4396. (org-get-indentation))))
  4397. ;; Ignore contents of source blocks or example blocks when
  4398. ;; indentation should be preserved (through
  4399. ;; `org-src-preserve-indentation' or "-i" flag).
  4400. (should-not
  4401. (zerop
  4402. (org-test-with-temp-text
  4403. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4404. (let ((org-adapt-indentation t)
  4405. (org-src-preserve-indentation nil)
  4406. (org-odd-levels-only nil))
  4407. (org-promote))
  4408. (forward-line)
  4409. (org-get-indentation))))
  4410. (should
  4411. (zerop
  4412. (org-test-with-temp-text
  4413. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  4414. (let ((org-adapt-indentation t)
  4415. (org-src-preserve-indentation t)
  4416. (org-odd-levels-only nil))
  4417. (org-promote))
  4418. (forward-line)
  4419. (org-get-indentation))))
  4420. (should
  4421. (zerop
  4422. (org-test-with-temp-text
  4423. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4424. (let ((org-adapt-indentation t)
  4425. (org-src-preserve-indentation t)
  4426. (org-odd-levels-only nil))
  4427. (org-promote))
  4428. (forward-line)
  4429. (org-get-indentation))))
  4430. (should
  4431. (zerop
  4432. (org-test-with-temp-text
  4433. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  4434. (let ((org-adapt-indentation t)
  4435. (org-src-preserve-indentation nil)
  4436. (org-odd-levels-only nil))
  4437. (org-promote))
  4438. (forward-line)
  4439. (org-get-indentation)))))
  4440. (ert-deftest test-org/org-get-valid-level ()
  4441. "Test function `org-get-valid-level' specifications."
  4442. (let ((org-odd-levels-only nil))
  4443. (should (equal 1 (org-get-valid-level 0 0)))
  4444. (should (equal 1 (org-get-valid-level 0 1)))
  4445. (should (equal 2 (org-get-valid-level 0 2)))
  4446. (should (equal 3 (org-get-valid-level 0 3)))
  4447. (should (equal 1 (org-get-valid-level 1 0)))
  4448. (should (equal 2 (org-get-valid-level 1 1)))
  4449. (should (equal 23 (org-get-valid-level 1 22)))
  4450. (should (equal 1 (org-get-valid-level 1 -1)))
  4451. (should (equal 1 (org-get-valid-level 2 -1))))
  4452. (let ((org-odd-levels-only t))
  4453. (should (equal 1 (org-get-valid-level 0 0)))
  4454. (should (equal 1 (org-get-valid-level 0 1)))
  4455. (should (equal 3 (org-get-valid-level 0 2)))
  4456. (should (equal 5 (org-get-valid-level 0 3)))
  4457. (should (equal 1 (org-get-valid-level 1 0)))
  4458. (should (equal 3 (org-get-valid-level 1 1)))
  4459. (should (equal 3 (org-get-valid-level 2 1)))
  4460. (should (equal 5 (org-get-valid-level 3 1)))
  4461. (should (equal 5 (org-get-valid-level 4 1)))
  4462. (should (equal 43 (org-get-valid-level 1 21)))
  4463. (should (equal 1 (org-get-valid-level 1 -1)))
  4464. (should (equal 1 (org-get-valid-level 2 -1)))
  4465. (should (equal 1 (org-get-valid-level 3 -1)))
  4466. (should (equal 3 (org-get-valid-level 4 -1)))
  4467. (should (equal 3 (org-get-valid-level 5 -1)))))
  4468. ;;; Planning
  4469. (ert-deftest test-org/at-planning-p ()
  4470. "Test `org-at-planning-p' specifications."
  4471. ;; Regular test.
  4472. (should
  4473. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  4474. (org-at-planning-p)))
  4475. (should-not
  4476. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  4477. (org-at-planning-p)))
  4478. ;; Correctly find planning attached to inlinetasks.
  4479. (when (featurep 'org-inlinetask)
  4480. (should
  4481. (org-test-with-temp-text
  4482. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  4483. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4484. (should-not
  4485. (org-test-with-temp-text
  4486. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4487. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4488. (should-not
  4489. (org-test-with-temp-text
  4490. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4491. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4492. (should-not
  4493. (org-test-with-temp-text
  4494. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  4495. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  4496. (ert-deftest test-org/add-planning-info ()
  4497. "Test `org-add-planning-info'."
  4498. ;; Create deadline when `org-adapt-indentation' is non-nil.
  4499. (should
  4500. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4501. (org-test-with-temp-text "* H\nParagraph<point>"
  4502. (let ((org-adapt-indentation t))
  4503. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4504. (replace-regexp-in-string
  4505. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4506. nil nil 1))))
  4507. ;; Create deadline when `org-adapt-indentation' is nil.
  4508. (should
  4509. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4510. (org-test-with-temp-text "* H\nParagraph<point>"
  4511. (let ((org-adapt-indentation nil))
  4512. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4513. (replace-regexp-in-string
  4514. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4515. nil nil 1))))
  4516. ;; Update deadline when `org-adapt-indentation' is non-nil.
  4517. (should
  4518. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4519. (org-test-with-temp-text "\
  4520. * H
  4521. DEADLINE: <2015-06-24 Wed>
  4522. Paragraph<point>"
  4523. (let ((org-adapt-indentation t))
  4524. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4525. (replace-regexp-in-string
  4526. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4527. nil nil 1))))
  4528. ;; Update deadline when `org-adapt-indentation' is nil.
  4529. (should
  4530. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4531. (org-test-with-temp-text "\
  4532. * H
  4533. DEADLINE: <2015-06-24 Wed>
  4534. Paragraph<point>"
  4535. (let ((org-adapt-indentation nil))
  4536. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4537. (replace-regexp-in-string
  4538. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4539. nil nil 1))))
  4540. ;; Schedule when `org-adapt-indentation' is non-nil.
  4541. (should
  4542. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  4543. (org-test-with-temp-text "* H\nParagraph<point>"
  4544. (let ((org-adapt-indentation t))
  4545. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4546. (replace-regexp-in-string
  4547. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4548. nil nil 1))))
  4549. ;; Schedule when `org-adapt-indentation' is nil.
  4550. (should
  4551. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  4552. (org-test-with-temp-text "* H\nParagraph<point>"
  4553. (let ((org-adapt-indentation nil))
  4554. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4555. (replace-regexp-in-string
  4556. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4557. nil nil 1))))
  4558. ;; Add deadline when scheduled.
  4559. (should
  4560. (equal "\
  4561. * H
  4562. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  4563. Paragraph"
  4564. (org-test-with-temp-text "\
  4565. * H
  4566. SCHEDULED: <2015-06-24 Wed>
  4567. Paragraph<point>"
  4568. (let ((org-adapt-indentation t))
  4569. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4570. (replace-regexp-in-string
  4571. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4572. nil nil 1))))
  4573. ;; Remove middle entry.
  4574. (should
  4575. (equal "\
  4576. * H
  4577. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  4578. Paragraph"
  4579. (org-test-with-temp-text "\
  4580. * H
  4581. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4582. Paragraph<point>"
  4583. (let ((org-adapt-indentation t))
  4584. (org-add-planning-info nil nil 'deadline))
  4585. (replace-regexp-in-string
  4586. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4587. nil nil 1))))
  4588. ;; Remove last entry and then middle entry (order should not
  4589. ;; matter).
  4590. (should
  4591. (equal "\
  4592. * H
  4593. CLOSED: [2015-06-24]
  4594. Paragraph"
  4595. (org-test-with-temp-text "\
  4596. * H
  4597. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4598. Paragraph<point>"
  4599. (let ((org-adapt-indentation t))
  4600. (org-add-planning-info nil nil 'scheduled 'deadline))
  4601. (replace-regexp-in-string
  4602. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4603. nil nil 1))))
  4604. ;; Remove closed when `org-adapt-indentation' is non-nil.
  4605. (should
  4606. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4607. (org-test-with-temp-text "\
  4608. * H
  4609. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4610. Paragraph<point>"
  4611. (let ((org-adapt-indentation t))
  4612. (org-add-planning-info nil nil 'closed))
  4613. (replace-regexp-in-string
  4614. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4615. nil nil 1))))
  4616. (should
  4617. (equal "* H\n Paragraph"
  4618. (org-test-with-temp-text "\
  4619. * H
  4620. CLOSED: [2015-06-25 Thu]
  4621. Paragraph<point>"
  4622. (let ((org-adapt-indentation t))
  4623. (org-add-planning-info nil nil 'closed))
  4624. (replace-regexp-in-string
  4625. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4626. nil nil 1))))
  4627. ;; Remove closed when `org-adapt-indentation' is nil.
  4628. (should
  4629. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4630. (org-test-with-temp-text "\
  4631. * H
  4632. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4633. Paragraph<point>"
  4634. (let ((org-adapt-indentation nil))
  4635. (org-add-planning-info nil nil 'closed))
  4636. (replace-regexp-in-string
  4637. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4638. nil nil 1))))
  4639. (should
  4640. (equal "* H\nParagraph"
  4641. (org-test-with-temp-text "\
  4642. * H
  4643. CLOSED: [2015-06-25 Thu]
  4644. Paragraph<point>"
  4645. (let ((org-adapt-indentation nil))
  4646. (org-add-planning-info nil nil 'closed))
  4647. (replace-regexp-in-string
  4648. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4649. nil nil 1))))
  4650. ;; Remove closed entry and delete empty line.
  4651. (should
  4652. (equal "\
  4653. * H
  4654. Paragraph"
  4655. (org-test-with-temp-text "\
  4656. * H
  4657. CLOSED: [2015-06-24 Wed]
  4658. Paragraph<point>"
  4659. (let ((org-adapt-indentation t))
  4660. (org-add-planning-info nil nil 'closed))
  4661. (replace-regexp-in-string
  4662. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4663. nil nil 1))))
  4664. ;; Remove one entry and update another.
  4665. (should
  4666. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4667. (org-test-with-temp-text "\
  4668. * H
  4669. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  4670. Paragraph<point>"
  4671. (let ((org-adapt-indentation t))
  4672. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  4673. (replace-regexp-in-string
  4674. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4675. nil nil 1)))))
  4676. (ert-deftest test-org/deadline ()
  4677. "Test `org-deadline' specifications."
  4678. ;; Insert a new value or replace existing one.
  4679. (should
  4680. (equal "* H\nDEADLINE: <2012-03-29>\n"
  4681. (org-test-with-temp-text "* H"
  4682. (let ((org-adapt-indentation nil)
  4683. (org-last-inserted-timestamp nil))
  4684. (org-deadline nil "<2012-03-29 Tue>"))
  4685. (replace-regexp-in-string
  4686. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4687. nil nil 1))))
  4688. (should
  4689. (equal "* H\nDEADLINE: <2014-03-04>"
  4690. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4691. (let ((org-adapt-indentation nil)
  4692. (org-last-inserted-timestamp nil))
  4693. (org-deadline nil "<2014-03-04 Thu>"))
  4694. (replace-regexp-in-string
  4695. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4696. nil nil 1))))
  4697. ;; Accept delta time, e.g., "+2d".
  4698. (should
  4699. (equal "* H\nDEADLINE: <2015-03-04>\n"
  4700. (cl-letf (((symbol-function 'current-time)
  4701. (lambda (&rest args)
  4702. (apply #'encode-time
  4703. (org-parse-time-string "2014-03-04")))))
  4704. (org-test-with-temp-text "* H"
  4705. (let ((org-adapt-indentation nil)
  4706. (org-last-inserted-timestamp nil))
  4707. (org-deadline nil "+1y"))
  4708. (replace-regexp-in-string
  4709. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4710. ;; Preserve repeater.
  4711. (should
  4712. (equal "* H\nDEADLINE: <2012-03-29 +2y>\n"
  4713. (org-test-with-temp-text "* H"
  4714. (let ((org-adapt-indentation nil)
  4715. (org-last-inserted-timestamp nil))
  4716. (org-deadline nil "<2012-03-29 Tue +2y>"))
  4717. (replace-regexp-in-string
  4718. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4719. ;; Remove CLOSED keyword, if any.
  4720. (should
  4721. (equal "* H\nDEADLINE: <2012-03-29>"
  4722. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4723. (let ((org-adapt-indentation nil)
  4724. (org-last-inserted-timestamp nil))
  4725. (org-deadline nil "<2012-03-29 Tue>"))
  4726. (replace-regexp-in-string
  4727. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4728. ;; With C-u argument, remove DEADLINE keyword.
  4729. (should
  4730. (equal "* H\n"
  4731. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4732. (let ((org-adapt-indentation nil)
  4733. (org-last-inserted-timestamp nil))
  4734. (org-deadline '(4)))
  4735. (buffer-string))))
  4736. (should
  4737. (equal "* H"
  4738. (org-test-with-temp-text "* H"
  4739. (let ((org-adapt-indentation nil)
  4740. (org-last-inserted-timestamp nil))
  4741. (org-deadline '(4)))
  4742. (buffer-string))))
  4743. ;; With C-u C-u argument, prompt for a delay cookie.
  4744. (should
  4745. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  4746. (cl-letf (((symbol-function 'org-read-date)
  4747. (lambda (&rest args)
  4748. (apply #'encode-time
  4749. (org-parse-time-string "2014-03-04")))))
  4750. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4751. (let ((org-adapt-indentation nil)
  4752. (org-last-inserted-timestamp nil))
  4753. (org-deadline '(16)))
  4754. (buffer-string)))))
  4755. (should-error
  4756. (cl-letf (((symbol-function 'org-read-date)
  4757. (lambda (&rest args)
  4758. (apply #'encode-time
  4759. (org-parse-time-string "2014-03-04")))))
  4760. (org-test-with-temp-text "* H"
  4761. (let ((org-adapt-indentation nil)
  4762. (org-last-inserted-timestamp nil))
  4763. (org-deadline '(16)))
  4764. (buffer-string))))
  4765. ;; When a region is active and
  4766. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4767. ;; same value in all headlines in region.
  4768. (should
  4769. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4770. (org-test-with-temp-text "* H1\n* H2"
  4771. (let ((org-adapt-indentation nil)
  4772. (org-last-inserted-timestamp nil)
  4773. (org-loop-over-headlines-in-active-region t))
  4774. (transient-mark-mode 1)
  4775. (push-mark (point) t t)
  4776. (goto-char (point-max))
  4777. (org-deadline nil "2012-03-29"))
  4778. (replace-regexp-in-string
  4779. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4780. (should-not
  4781. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4782. (org-test-with-temp-text "* H1\n* H2"
  4783. (let ((org-adapt-indentation nil)
  4784. (org-last-inserted-timestamp nil)
  4785. (org-loop-over-headlines-in-active-region nil))
  4786. (transient-mark-mode 1)
  4787. (push-mark (point) t t)
  4788. (goto-char (point-max))
  4789. (org-deadline nil "2012-03-29"))
  4790. (replace-regexp-in-string
  4791. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4792. (ert-deftest test-org/schedule ()
  4793. "Test `org-schedule' specifications."
  4794. ;; Insert a new value or replace existing one.
  4795. (should
  4796. (equal "* H\nSCHEDULED: <2012-03-29>\n"
  4797. (org-test-with-temp-text "* H"
  4798. (let ((org-adapt-indentation nil)
  4799. (org-last-inserted-timestamp nil))
  4800. (org-schedule nil "<2012-03-29 Tue>"))
  4801. (replace-regexp-in-string
  4802. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4803. nil nil 1))))
  4804. (should
  4805. (equal "* H\nSCHEDULED: <2014-03-04>"
  4806. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4807. (let ((org-adapt-indentation nil)
  4808. (org-last-inserted-timestamp nil))
  4809. (org-schedule nil "<2014-03-04 Thu>"))
  4810. (replace-regexp-in-string
  4811. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4812. nil nil 1))))
  4813. ;; Accept delta time, e.g., "+2d".
  4814. (should
  4815. (equal "* H\nSCHEDULED: <2015-03-04>\n"
  4816. (cl-letf (((symbol-function 'current-time)
  4817. (lambda (&rest args)
  4818. (apply #'encode-time
  4819. (org-parse-time-string "2014-03-04")))))
  4820. (org-test-with-temp-text "* H"
  4821. (let ((org-adapt-indentation nil)
  4822. (org-last-inserted-timestamp nil))
  4823. (org-schedule nil "+1y"))
  4824. (replace-regexp-in-string
  4825. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4826. ;; Preserve repeater.
  4827. (should
  4828. (equal "* H\nSCHEDULED: <2012-03-29 +2y>\n"
  4829. (org-test-with-temp-text "* H"
  4830. (let ((org-adapt-indentation nil)
  4831. (org-last-inserted-timestamp nil))
  4832. (org-schedule nil "<2012-03-29 Tue +2y>"))
  4833. (replace-regexp-in-string
  4834. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4835. ;; Remove CLOSED keyword, if any.
  4836. (should
  4837. (equal "* H\nSCHEDULED: <2012-03-29>"
  4838. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4839. (let ((org-adapt-indentation nil)
  4840. (org-last-inserted-timestamp nil))
  4841. (org-schedule nil "<2012-03-29 Tue>"))
  4842. (replace-regexp-in-string
  4843. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4844. ;; With C-u argument, remove SCHEDULED keyword.
  4845. (should
  4846. (equal "* H\n"
  4847. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4848. (let ((org-adapt-indentation nil)
  4849. (org-last-inserted-timestamp nil))
  4850. (org-schedule '(4)))
  4851. (buffer-string))))
  4852. (should
  4853. (equal "* H"
  4854. (org-test-with-temp-text "* H"
  4855. (let ((org-adapt-indentation nil)
  4856. (org-last-inserted-timestamp nil))
  4857. (org-schedule '(4)))
  4858. (buffer-string))))
  4859. ;; With C-u C-u argument, prompt for a delay cookie.
  4860. (should
  4861. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  4862. (cl-letf (((symbol-function 'org-read-date)
  4863. (lambda (&rest args)
  4864. (apply #'encode-time
  4865. (org-parse-time-string "2014-03-04")))))
  4866. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4867. (let ((org-adapt-indentation nil)
  4868. (org-last-inserted-timestamp nil))
  4869. (org-schedule '(16)))
  4870. (buffer-string)))))
  4871. (should-error
  4872. (cl-letf (((symbol-function 'org-read-date)
  4873. (lambda (&rest args)
  4874. (apply #'encode-time
  4875. (org-parse-time-string "2014-03-04")))))
  4876. (org-test-with-temp-text "* H"
  4877. (let ((org-adapt-indentation nil)
  4878. (org-last-inserted-timestamp nil))
  4879. (org-schedule '(16)))
  4880. (buffer-string))))
  4881. ;; When a region is active and
  4882. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4883. ;; same value in all headlines in region.
  4884. (should
  4885. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4886. (org-test-with-temp-text "* H1\n* H2"
  4887. (let ((org-adapt-indentation nil)
  4888. (org-last-inserted-timestamp nil)
  4889. (org-loop-over-headlines-in-active-region t))
  4890. (transient-mark-mode 1)
  4891. (push-mark (point) t t)
  4892. (goto-char (point-max))
  4893. (org-schedule nil "2012-03-29"))
  4894. (replace-regexp-in-string
  4895. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4896. (should-not
  4897. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4898. (org-test-with-temp-text "* H1\n* H2"
  4899. (let ((org-adapt-indentation nil)
  4900. (org-last-inserted-timestamp nil)
  4901. (org-loop-over-headlines-in-active-region nil))
  4902. (transient-mark-mode 1)
  4903. (push-mark (point) t t)
  4904. (goto-char (point-max))
  4905. (org-schedule nil "2012-03-29"))
  4906. (replace-regexp-in-string
  4907. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4908. (should
  4909. ;; check if a repeater survives re-scheduling.
  4910. (string-match-p
  4911. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  4912. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  4913. (let ((org-adapt-indentation nil)
  4914. (org-last-inserted-timestamp nil))
  4915. (org-schedule nil "2017-02-01"))
  4916. (buffer-string)))))
  4917. ;;; Property API
  4918. (ert-deftest test-org/buffer-property-keys ()
  4919. "Test `org-buffer-property-keys' specifications."
  4920. ;; Retrieve properties accross siblings.
  4921. (should
  4922. (equal '("A" "B")
  4923. (org-test-with-temp-text "
  4924. * H1
  4925. :PROPERTIES:
  4926. :A: 1
  4927. :END:
  4928. * H2
  4929. :PROPERTIES:
  4930. :B: 1
  4931. :END:"
  4932. (org-buffer-property-keys))))
  4933. ;; Retrieve properties accross children.
  4934. (should
  4935. (equal '("A" "B")
  4936. (org-test-with-temp-text "
  4937. * H1
  4938. :PROPERTIES:
  4939. :A: 1
  4940. :END:
  4941. ** H2
  4942. :PROPERTIES:
  4943. :B: 1
  4944. :END:"
  4945. (org-buffer-property-keys))))
  4946. ;; Retrieve muliple properties in the same drawer.
  4947. (should
  4948. (equal '("A" "B")
  4949. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4950. (org-buffer-property-keys))))
  4951. ;; Ignore extension symbol in property name.
  4952. (should
  4953. (equal '("A")
  4954. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4955. (org-buffer-property-keys))))
  4956. ;; With non-nil COLUMNS, extract property names from columns.
  4957. (should
  4958. (equal '("A" "B")
  4959. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  4960. (org-buffer-property-keys nil nil t))))
  4961. (should
  4962. (equal '("A" "B" "COLUMNS")
  4963. (org-test-with-temp-text
  4964. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  4965. (org-buffer-property-keys nil nil t))))
  4966. ;; In COLUMNS, ignore title and summary-type.
  4967. (should
  4968. (equal '("A")
  4969. (org-test-with-temp-text "#+COLUMNS: %A(Foo)"
  4970. (org-buffer-property-keys nil nil t))))
  4971. (should
  4972. (equal '("A")
  4973. (org-test-with-temp-text "#+COLUMNS: %A{Foo}"
  4974. (org-buffer-property-keys nil nil t))))
  4975. (should
  4976. (equal '("A")
  4977. (org-test-with-temp-text "#+COLUMNS: %A(Foo){Bar}"
  4978. (org-buffer-property-keys nil nil t)))))
  4979. (ert-deftest test-org/property-values ()
  4980. "Test `org-property-values' specifications."
  4981. ;; Regular test.
  4982. (should
  4983. (equal '("2" "1")
  4984. (org-test-with-temp-text
  4985. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  4986. (org-property-values "A"))))
  4987. ;; Ignore empty values.
  4988. (should-not
  4989. (org-test-with-temp-text
  4990. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  4991. (org-property-values "A")))
  4992. ;; Take into consideration extended values.
  4993. (should
  4994. (equal '("1 2")
  4995. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4996. (org-property-values "A")))))
  4997. (ert-deftest test-org/find-property ()
  4998. "Test `org-find-property' specifications."
  4999. ;; Regular test.
  5000. (should
  5001. (= 1
  5002. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  5003. (org-find-property "prop"))))
  5004. ;; Ignore false positives.
  5005. (should
  5006. (= 27
  5007. (org-test-with-temp-text
  5008. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  5009. (org-find-property "A"))))
  5010. ;; Return first entry found in buffer.
  5011. (should
  5012. (= 1
  5013. (org-test-with-temp-text
  5014. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5015. (org-find-property "A"))))
  5016. ;; Only search visible part of the buffer.
  5017. (should
  5018. (= 31
  5019. (org-test-with-temp-text
  5020. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5021. (org-narrow-to-subtree)
  5022. (org-find-property "A"))))
  5023. ;; With optional argument, only find entries with a specific value.
  5024. (should-not
  5025. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5026. (org-find-property "A" "2")))
  5027. (should
  5028. (= 31
  5029. (org-test-with-temp-text
  5030. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  5031. (org-find-property "A" "2"))))
  5032. ;; Use "nil" for explicit nil values.
  5033. (should
  5034. (= 31
  5035. (org-test-with-temp-text
  5036. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  5037. (org-find-property "A" "nil")))))
  5038. (ert-deftest test-org/entry-delete ()
  5039. "Test `org-entry-delete' specifications."
  5040. ;; Regular test.
  5041. (should
  5042. (string-match
  5043. " *:PROPERTIES:\n *:B: +2\n *:END:"
  5044. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5045. (org-entry-delete (point) "A")
  5046. (buffer-string))))
  5047. ;; Also remove accumulated properties.
  5048. (should-not
  5049. (string-match
  5050. ":A"
  5051. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  5052. (org-entry-delete (point) "A")
  5053. (buffer-string))))
  5054. ;; When last property is removed, remove the property drawer.
  5055. (should-not
  5056. (string-match
  5057. ":PROPERTIES:"
  5058. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  5059. (org-entry-delete (point) "A")
  5060. (buffer-string))))
  5061. ;; Return a non-nil value when some property was removed.
  5062. (should
  5063. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5064. (org-entry-delete (point) "A")))
  5065. (should-not
  5066. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5067. (org-entry-delete (point) "C")))
  5068. ;; Special properties cannot be located in a drawer. Allow to
  5069. ;; remove them anyway, in case of user error.
  5070. (should
  5071. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  5072. (org-entry-delete (point) "SCHEDULED"))))
  5073. (ert-deftest test-org/entry-get ()
  5074. "Test `org-entry-get' specifications."
  5075. ;; Regular test.
  5076. (should
  5077. (equal "1"
  5078. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5079. (org-entry-get (point) "A"))))
  5080. ;; Ignore case.
  5081. (should
  5082. (equal "1"
  5083. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5084. (org-entry-get (point) "a"))))
  5085. ;; Handle extended values, both before and after base value.
  5086. (should
  5087. (equal "1 2 3"
  5088. (org-test-with-temp-text
  5089. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5090. (org-entry-get (point) "A"))))
  5091. ;; Empty values are returned as the empty string.
  5092. (should
  5093. (equal ""
  5094. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  5095. (org-entry-get (point) "A"))))
  5096. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  5097. ;; otherwise, return nil.
  5098. (should-not
  5099. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5100. (org-entry-get (point) "A")))
  5101. (should
  5102. (equal "nil"
  5103. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5104. (org-entry-get (point) "A" nil t))))
  5105. ;; Return nil when no property is found, independently on the
  5106. ;; LITERAL-NIL argument.
  5107. (should-not
  5108. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5109. (org-entry-get (point) "B")))
  5110. (should-not
  5111. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5112. (org-entry-get (point) "B" nil t)))
  5113. ;; Handle inheritance, when allowed. Include extended values and
  5114. ;; possibly global values.
  5115. (should
  5116. (equal
  5117. "1"
  5118. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5119. (org-entry-get (point) "A" t))))
  5120. (should
  5121. (equal
  5122. "1"
  5123. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5124. (let ((org-use-property-inheritance t))
  5125. (org-entry-get (point) "A" 'selective)))))
  5126. (should-not
  5127. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5128. (let ((org-use-property-inheritance nil))
  5129. (org-entry-get (point) "A" 'selective))))
  5130. (should
  5131. (equal
  5132. "1 2"
  5133. (org-test-with-temp-text
  5134. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  5135. (org-entry-get (point-max) "A" t))))
  5136. (should
  5137. (equal "1"
  5138. (org-test-with-temp-text
  5139. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  5140. (org-mode-restart)
  5141. (org-entry-get (point-max) "A" t))))
  5142. (should
  5143. (equal "0 1"
  5144. (org-test-with-temp-text
  5145. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5146. (org-mode-restart)
  5147. (org-entry-get (point-max) "A" t)))))
  5148. (ert-deftest test-org/entry-properties ()
  5149. "Test `org-entry-properties' specifications."
  5150. ;; Get "ITEM" property.
  5151. (should
  5152. (equal "H"
  5153. (org-test-with-temp-text "* TODO H"
  5154. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  5155. (should
  5156. (equal "H"
  5157. (org-test-with-temp-text "* TODO H"
  5158. (cdr (assoc "ITEM" (org-entry-properties))))))
  5159. ;; Get "TODO" property. TODO keywords are case sensitive.
  5160. (should
  5161. (equal "TODO"
  5162. (org-test-with-temp-text "* TODO H"
  5163. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  5164. (should
  5165. (equal "TODO"
  5166. (org-test-with-temp-text "* TODO H"
  5167. (cdr (assoc "TODO" (org-entry-properties))))))
  5168. (should-not
  5169. (org-test-with-temp-text "* H"
  5170. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5171. (should-not
  5172. (org-test-with-temp-text "* todo H"
  5173. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5174. ;; Get "PRIORITY" property.
  5175. (should
  5176. (equal "A"
  5177. (org-test-with-temp-text "* [#A] H"
  5178. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5179. (should
  5180. (equal "A"
  5181. (org-test-with-temp-text "* [#A] H"
  5182. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  5183. (should
  5184. (equal (char-to-string org-default-priority)
  5185. (org-test-with-temp-text "* H"
  5186. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5187. ;; Get "FILE" property.
  5188. (should
  5189. (org-test-with-temp-text-in-file "* H\nParagraph"
  5190. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  5191. (buffer-file-name))))
  5192. (should
  5193. (org-test-with-temp-text-in-file "* H\nParagraph"
  5194. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  5195. (buffer-file-name))))
  5196. (should-not
  5197. (org-test-with-temp-text "* H\nParagraph"
  5198. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  5199. ;; Get "TAGS" property.
  5200. (should
  5201. (equal ":tag1:tag2:"
  5202. (org-test-with-temp-text "* H :tag1:tag2:"
  5203. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  5204. (should
  5205. (equal ":tag1:tag2:"
  5206. (org-test-with-temp-text "* H :tag1:tag2:"
  5207. (cdr (assoc "TAGS" (org-entry-properties))))))
  5208. (should-not
  5209. (org-test-with-temp-text "* H"
  5210. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  5211. ;; Get "ALLTAGS" property.
  5212. (should
  5213. (equal ":tag1:tag2:"
  5214. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5215. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  5216. (should
  5217. (equal ":tag1:tag2:"
  5218. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5219. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  5220. (should-not
  5221. (org-test-with-temp-text "* H"
  5222. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  5223. ;; Get "BLOCKED" property.
  5224. (should
  5225. (equal "t"
  5226. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  5227. (let ((org-enforce-todo-dependencies t)
  5228. (org-blocker-hook
  5229. '(org-block-todo-from-children-or-siblings-or-parent)))
  5230. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5231. (should
  5232. (equal ""
  5233. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  5234. (let ((org-enforce-todo-dependencies t)
  5235. (org-blocker-hook
  5236. '(org-block-todo-from-children-or-siblings-or-parent)))
  5237. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5238. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  5239. (should
  5240. (equal
  5241. "[2012-03-29 thu.]"
  5242. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5243. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  5244. (should
  5245. (equal
  5246. "[2012-03-29 thu.]"
  5247. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5248. (cdr (assoc "CLOSED" (org-entry-properties))))))
  5249. (should-not
  5250. (org-test-with-temp-text "* H"
  5251. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  5252. (should
  5253. (equal
  5254. "<2014-03-04 tue.>"
  5255. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5256. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  5257. (should
  5258. (equal
  5259. "<2014-03-04 tue.>"
  5260. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5261. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  5262. (should-not
  5263. (org-test-with-temp-text "* H"
  5264. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  5265. (should
  5266. (equal
  5267. "<2014-03-04 tue.>"
  5268. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5269. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  5270. (should
  5271. (equal
  5272. "<2014-03-04 tue.>"
  5273. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5274. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  5275. (should-not
  5276. (org-test-with-temp-text "* H"
  5277. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  5278. ;; Get "CATEGORY"
  5279. (should
  5280. (equal "cat"
  5281. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5282. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  5283. (should
  5284. (equal "cat"
  5285. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5286. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5287. (should
  5288. (equal "cat"
  5289. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  5290. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5291. (should
  5292. (equal "cat2"
  5293. (org-test-with-temp-text
  5294. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  5295. "\n"
  5296. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  5297. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5298. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  5299. (should
  5300. (equal "<2012-03-29 thu.>"
  5301. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  5302. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5303. (should
  5304. (equal "[2012-03-29 thu.]"
  5305. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  5306. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  5307. (should
  5308. (equal "<2012-03-29 thu.>"
  5309. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  5310. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  5311. (should
  5312. (equal "[2014-03-04 tue.]"
  5313. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  5314. (cdr (assoc "TIMESTAMP_IA"
  5315. (org-entry-properties nil "TIMESTAMP_IA"))))))
  5316. (should-not
  5317. (equal "<2012-03-29 thu.>"
  5318. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  5319. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5320. ;; Get standard properties.
  5321. (should
  5322. (equal "1"
  5323. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5324. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5325. ;; Handle extended properties.
  5326. (should
  5327. (equal "1 2 3"
  5328. (org-test-with-temp-text
  5329. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5330. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5331. (should
  5332. (equal "1 2 3"
  5333. (org-test-with-temp-text
  5334. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  5335. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5336. ;; Ignore forbidden (special) properties.
  5337. (should-not
  5338. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  5339. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  5340. (ert-deftest test-org/entry-put ()
  5341. "Test `org-entry-put' specifications."
  5342. ;; Error when not a string or nil.
  5343. (should-error
  5344. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5345. (org-entry-put 1 "test" 2)))
  5346. ;; Error when property name is invalid.
  5347. (should-error
  5348. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5349. (org-entry-put 1 "no space" "value")))
  5350. (should-error
  5351. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5352. (org-entry-put 1 "" "value")))
  5353. ;; Set "TODO" property.
  5354. (should
  5355. (string-match (regexp-quote " TODO H")
  5356. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5357. (org-entry-put (point) "TODO" "TODO")
  5358. (buffer-string))))
  5359. (should
  5360. (string-match (regexp-quote "* H")
  5361. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5362. (org-entry-put (point) "TODO" nil)
  5363. (buffer-string))))
  5364. ;; Set "PRIORITY" property.
  5365. (should
  5366. (equal "* [#A] H"
  5367. (org-test-with-temp-text "* [#B] H"
  5368. (org-entry-put (point) "PRIORITY" "A")
  5369. (buffer-string))))
  5370. (should
  5371. (equal "* H"
  5372. (org-test-with-temp-text "* [#B] H"
  5373. (org-entry-put (point) "PRIORITY" nil)
  5374. (buffer-string))))
  5375. ;; Set "SCHEDULED" property.
  5376. (should
  5377. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  5378. (org-test-with-temp-text "* H"
  5379. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  5380. (buffer-string))))
  5381. (should
  5382. (string= "* H\n"
  5383. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5384. (org-entry-put (point) "SCHEDULED" nil)
  5385. (buffer-string))))
  5386. (should
  5387. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  5388. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5389. (org-entry-put (point) "SCHEDULED" "earlier")
  5390. (buffer-string))))
  5391. (should
  5392. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  5393. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5394. (org-entry-put (point) "SCHEDULED" "later")
  5395. (buffer-string))))
  5396. ;; Set "DEADLINE" property.
  5397. (should
  5398. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  5399. (org-test-with-temp-text "* H"
  5400. (org-entry-put (point) "DEADLINE" "2014-03-04")
  5401. (buffer-string))))
  5402. (should
  5403. (string= "* H\n"
  5404. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5405. (org-entry-put (point) "DEADLINE" nil)
  5406. (buffer-string))))
  5407. (should
  5408. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  5409. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5410. (org-entry-put (point) "DEADLINE" "earlier")
  5411. (buffer-string))))
  5412. (should
  5413. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  5414. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5415. (org-entry-put (point) "DEADLINE" "later")
  5416. (buffer-string))))
  5417. ;; Set "CATEGORY" property
  5418. (should
  5419. (string-match "^ *:CATEGORY: cat"
  5420. (org-test-with-temp-text "* H"
  5421. (org-entry-put (point) "CATEGORY" "cat")
  5422. (buffer-string))))
  5423. ;; Regular properties, with or without pre-existing drawer.
  5424. (should
  5425. (string-match "^ *:A: +2$"
  5426. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5427. (org-entry-put (point) "A" "2")
  5428. (buffer-string))))
  5429. (should
  5430. (string-match "^ *:A: +1$"
  5431. (org-test-with-temp-text "* H"
  5432. (org-entry-put (point) "A" "1")
  5433. (buffer-string))))
  5434. ;; Special case: two consecutive headlines.
  5435. (should
  5436. (string-match "\\* A\n *:PROPERTIES:"
  5437. (org-test-with-temp-text "* A\n** B"
  5438. (org-entry-put (point) "A" "1")
  5439. (buffer-string)))))
  5440. (ert-deftest test-org/refresh-properties ()
  5441. "Test `org-refresh-properties' specifications."
  5442. (should
  5443. (equal "1"
  5444. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5445. (org-refresh-properties "A" 'org-test)
  5446. (get-text-property (point) 'org-test))))
  5447. (should-not
  5448. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5449. (org-refresh-properties "B" 'org-test)
  5450. (get-text-property (point) 'org-test)))
  5451. ;; Handle properties only defined with extension syntax, i.e.,
  5452. ;; "PROPERTY+".
  5453. (should
  5454. (equal "1"
  5455. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5456. (org-refresh-properties "A" 'org-test)
  5457. (get-text-property (point) 'org-test))))
  5458. ;; When property is inherited, add text property to the whole
  5459. ;; sub-tree.
  5460. (should
  5461. (equal "1"
  5462. (org-test-with-temp-text
  5463. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  5464. (let ((org-use-property-inheritance t))
  5465. (org-refresh-properties "A" 'org-test))
  5466. (get-text-property (point) 'org-test))))
  5467. ;; When property is inherited, use global value across the whole
  5468. ;; buffer. However local values have precedence.
  5469. (should-not
  5470. (equal "1"
  5471. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5472. (org-mode-restart)
  5473. (let ((org-use-property-inheritance nil))
  5474. (org-refresh-properties "A" 'org-test))
  5475. (get-text-property (point) 'org-test))))
  5476. (should
  5477. (equal "1"
  5478. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5479. (org-mode-restart)
  5480. (let ((org-use-property-inheritance t))
  5481. (org-refresh-properties "A" 'org-test))
  5482. (get-text-property (point) 'org-test))))
  5483. (should
  5484. (equal "2"
  5485. (org-test-with-temp-text
  5486. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  5487. (org-mode-restart)
  5488. (let ((org-use-property-inheritance t))
  5489. (org-refresh-properties "A" 'org-test))
  5490. (get-text-property (point) 'org-test)))))
  5491. ;;; Radio Targets
  5492. (ert-deftest test-org/update-radio-target-regexp ()
  5493. "Test `org-update-radio-target-regexp' specifications."
  5494. ;; Properly update cache with no previous radio target regexp.
  5495. (should
  5496. (eq 'link
  5497. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5498. (save-excursion (goto-char (point-max)) (org-element-context))
  5499. (insert "<<<")
  5500. (search-forward "o")
  5501. (insert ">>>")
  5502. (org-update-radio-target-regexp)
  5503. (goto-char (point-max))
  5504. (org-element-type (org-element-context)))))
  5505. ;; Properly update cache with previous radio target regexp.
  5506. (should
  5507. (eq 'link
  5508. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5509. (save-excursion (goto-char (point-max)) (org-element-context))
  5510. (insert "<<<")
  5511. (search-forward "o")
  5512. (insert ">>>")
  5513. (org-update-radio-target-regexp)
  5514. (search-backward "r")
  5515. (delete-char 5)
  5516. (insert "new")
  5517. (org-update-radio-target-regexp)
  5518. (goto-char (point-max))
  5519. (delete-region (line-beginning-position) (point))
  5520. (insert "new")
  5521. (org-element-type (org-element-context))))))
  5522. ;;; Refile
  5523. (ert-deftest test-org/refile-get-targets ()
  5524. "Test `org-refile-get-targets' specifications."
  5525. ;; :maxlevel includes all headings above specified value.
  5526. (should
  5527. (equal '("H1" "H2" "H3")
  5528. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5529. (let ((org-refile-use-outline-path nil)
  5530. (org-refile-targets `((nil :maxlevel . 3))))
  5531. (mapcar #'car (org-refile-get-targets))))))
  5532. (should
  5533. (equal '("H1" "H2")
  5534. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5535. (let ((org-refile-use-outline-path nil)
  5536. (org-refile-targets `((nil :maxlevel . 2))))
  5537. (mapcar #'car (org-refile-get-targets))))))
  5538. ;; :level limits targets to headlines with the specified level.
  5539. (should
  5540. (equal '("H2")
  5541. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5542. (let ((org-refile-use-outline-path nil)
  5543. (org-refile-targets `((nil :level . 2))))
  5544. (mapcar #'car (org-refile-get-targets))))))
  5545. ;; :tag limits targets to headlines with specified tag.
  5546. (should
  5547. (equal '("H1")
  5548. (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
  5549. (let ((org-refile-use-outline-path nil)
  5550. (org-refile-targets `((nil :tag . "foo"))))
  5551. (mapcar #'car (org-refile-get-targets))))))
  5552. ;; :todo limits targets to headlines with specified TODO keyword.
  5553. (should
  5554. (equal '("H2")
  5555. (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
  5556. (let ((org-refile-use-outline-path nil)
  5557. (org-refile-targets `((nil :todo . "TODO"))))
  5558. (mapcar #'car (org-refile-get-targets))))))
  5559. ;; :regexp filters targets matching provided regexp.
  5560. (should
  5561. (equal '("F2" "F3")
  5562. (org-test-with-temp-text "* H1\n** F2\n*** F3"
  5563. (let ((org-refile-use-outline-path nil)
  5564. (org-refile-targets `((nil :regexp . "F"))))
  5565. (mapcar #'car (org-refile-get-targets))))))
  5566. ;; A nil `org-refile-targets' includes only top level headlines in
  5567. ;; current buffer.
  5568. (should
  5569. (equal '("H1" "H2")
  5570. (org-test-with-temp-text "* H1\n** S1\n* H2"
  5571. (let ((org-refile-use-outline-path nil)
  5572. (org-refile-targets nil))
  5573. (mapcar #'car (org-refile-get-targets))))))
  5574. ;; Return value is the union of the targets according to all the
  5575. ;; defined rules. However, prevent duplicates.
  5576. (should
  5577. (equal '("F2" "F3" "H1")
  5578. (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
  5579. (let ((org-refile-use-outline-path nil)
  5580. (org-refile-targets `((nil :regexp . "F")
  5581. (nil :todo . "TODO"))))
  5582. (mapcar #'car (org-refile-get-targets))))))
  5583. (should
  5584. (equal '("F2" "F3" "H1")
  5585. (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
  5586. (let ((org-refile-use-outline-path nil)
  5587. (org-refile-targets `((nil :regexp . "F")
  5588. (nil :todo . "TODO"))))
  5589. (mapcar #'car (org-refile-get-targets))))))
  5590. ;; When `org-refile-use-outline-path' is non-nil, provide targets as
  5591. ;; paths.
  5592. (should
  5593. (equal '("H1" "H1/H2" "H1/H2/H3")
  5594. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5595. (let ((org-refile-use-outline-path t)
  5596. (org-refile-targets `((nil :maxlevel . 3))))
  5597. (mapcar #'car (org-refile-get-targets))))))
  5598. ;; When providing targets as paths, escape forward slashes in
  5599. ;; headings with backslashes.
  5600. (should
  5601. (equal '("H1\\/foo")
  5602. (org-test-with-temp-text "* H1/foo"
  5603. (let ((org-refile-use-outline-path t)
  5604. (org-refile-targets `((nil :maxlevel . 1))))
  5605. (mapcar #'car (org-refile-get-targets))))))
  5606. ;; When `org-refile-use-outline-path' is `file', include file name
  5607. ;; without directory in targets.
  5608. (should
  5609. (org-test-with-temp-text-in-file "* H1"
  5610. (let* ((filename (buffer-file-name))
  5611. (org-refile-use-outline-path 'file)
  5612. (org-refile-targets `(((,filename) :level . 1))))
  5613. (member (file-name-nondirectory filename)
  5614. (mapcar #'car (org-refile-get-targets))))))
  5615. ;; When `org-refile-use-outline-path' is `full-file-path', include
  5616. ;; full file name.
  5617. (should
  5618. (org-test-with-temp-text-in-file "* H1"
  5619. (let* ((filename (file-truename (buffer-file-name)))
  5620. (org-refile-use-outline-path 'full-file-path)
  5621. (org-refile-targets `(((,filename) :level . 1))))
  5622. (member filename (mapcar #'car (org-refile-get-targets))))))
  5623. ;; When `org-refile-use-outline-path' is `buffer-name', include
  5624. ;; buffer name.
  5625. (should
  5626. (org-test-with-temp-text "* H1"
  5627. (let* ((org-refile-use-outline-path 'buffer-name)
  5628. (org-refile-targets `((nil :level . 1))))
  5629. (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
  5630. ;;; Sparse trees
  5631. (ert-deftest test-org/match-sparse-tree ()
  5632. "Test `org-match-sparse-tree' specifications."
  5633. ;; Match tags.
  5634. (should-not
  5635. (org-test-with-temp-text "* H\n** H1 :tag:"
  5636. (org-match-sparse-tree nil "tag")
  5637. (search-forward "H1")
  5638. (org-invisible-p2)))
  5639. (should
  5640. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  5641. (org-match-sparse-tree nil "tag")
  5642. (search-forward "H2")
  5643. (org-invisible-p2)))
  5644. ;; "-" operator for tags.
  5645. (should-not
  5646. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5647. (org-match-sparse-tree nil "tag1-tag2")
  5648. (search-forward "H1")
  5649. (org-invisible-p2)))
  5650. (should
  5651. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5652. (org-match-sparse-tree nil "tag1-tag2")
  5653. (search-forward "H2")
  5654. (org-invisible-p2)))
  5655. ;; "&" operator for tags.
  5656. (should
  5657. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5658. (org-match-sparse-tree nil "tag1&tag2")
  5659. (search-forward "H1")
  5660. (org-invisible-p2)))
  5661. (should-not
  5662. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5663. (org-match-sparse-tree nil "tag1&tag2")
  5664. (search-forward "H2")
  5665. (org-invisible-p2)))
  5666. ;; "|" operator for tags.
  5667. (should-not
  5668. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5669. (org-match-sparse-tree nil "tag1|tag2")
  5670. (search-forward "H1")
  5671. (org-invisible-p2)))
  5672. (should-not
  5673. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5674. (org-match-sparse-tree nil "tag1|tag2")
  5675. (search-forward "H2")
  5676. (org-invisible-p2)))
  5677. ;; Regexp match on tags.
  5678. (should-not
  5679. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5680. (org-match-sparse-tree nil "{^tag.*}")
  5681. (search-forward "H1")
  5682. (org-invisible-p2)))
  5683. (should
  5684. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5685. (org-match-sparse-tree nil "{^tag.*}")
  5686. (search-forward "H2")
  5687. (org-invisible-p2)))
  5688. ;; Match group tags.
  5689. (should-not
  5690. (org-test-with-temp-text
  5691. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5692. (org-match-sparse-tree nil "work")
  5693. (search-forward "H1")
  5694. (org-invisible-p2)))
  5695. (should-not
  5696. (org-test-with-temp-text
  5697. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5698. (org-match-sparse-tree nil "work")
  5699. (search-forward "H2")
  5700. (org-invisible-p2)))
  5701. ;; Match group tags with hard brackets.
  5702. (should-not
  5703. (org-test-with-temp-text
  5704. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5705. (org-match-sparse-tree nil "work")
  5706. (search-forward "H1")
  5707. (org-invisible-p2)))
  5708. (should-not
  5709. (org-test-with-temp-text
  5710. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5711. (org-match-sparse-tree nil "work")
  5712. (search-forward "H2")
  5713. (org-invisible-p2)))
  5714. ;; Match tags in hierarchies
  5715. (should-not
  5716. (org-test-with-temp-text
  5717. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5718. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5719. #+TAGS: { Lev_3 : Lev_4 }\n
  5720. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5721. (org-match-sparse-tree nil "Lev_1")
  5722. (search-forward "H4")
  5723. (org-invisible-p2)))
  5724. (should-not
  5725. (org-test-with-temp-text
  5726. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5727. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5728. #+TAGS: { Lev_3 : Lev_4 }\n
  5729. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5730. (org-match-sparse-tree nil "Lev_1+Lev_3")
  5731. (search-forward "H4")
  5732. (org-invisible-p2)))
  5733. ;; Match regular expressions in tags
  5734. (should-not
  5735. (org-test-with-temp-text
  5736. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  5737. (org-match-sparse-tree nil "Lev")
  5738. (search-forward "H1")
  5739. (org-invisible-p2)))
  5740. (should
  5741. (org-test-with-temp-text
  5742. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  5743. (org-match-sparse-tree nil "Lev")
  5744. (search-forward "H1")
  5745. (org-invisible-p2)))
  5746. ;; Match properties.
  5747. (should
  5748. (org-test-with-temp-text
  5749. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  5750. (org-match-sparse-tree nil "A=\"1\"")
  5751. (search-forward "H2")
  5752. (org-invisible-p2)))
  5753. (should-not
  5754. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5755. (org-match-sparse-tree nil "A=\"1\"")
  5756. (search-forward "H2")
  5757. (org-invisible-p2)))
  5758. ;; Case is not significant when matching properties.
  5759. (should-not
  5760. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5761. (org-match-sparse-tree nil "a=\"1\"")
  5762. (search-forward "H2")
  5763. (org-invisible-p2)))
  5764. (should-not
  5765. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  5766. (org-match-sparse-tree nil "A=\"1\"")
  5767. (search-forward "H2")
  5768. (org-invisible-p2)))
  5769. ;; Match special LEVEL property.
  5770. (should-not
  5771. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5772. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5773. (search-forward "H1")
  5774. (org-invisible-p2)))
  5775. (should
  5776. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5777. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5778. (search-forward "H2")
  5779. (org-invisible-p2)))
  5780. ;; Comparison operators when matching properties.
  5781. (should
  5782. (org-test-with-temp-text
  5783. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5784. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5785. (search-forward "H1")
  5786. (org-invisible-p2)))
  5787. (should-not
  5788. (org-test-with-temp-text
  5789. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5790. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5791. (search-forward "H2")
  5792. (org-invisible-p2)))
  5793. ;; Regexp match on properties values.
  5794. (should-not
  5795. (org-test-with-temp-text
  5796. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5797. (org-match-sparse-tree nil "A={f.*}")
  5798. (search-forward "H1")
  5799. (org-invisible-p2)))
  5800. (should
  5801. (org-test-with-temp-text
  5802. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5803. (org-match-sparse-tree nil "A={f.*}")
  5804. (search-forward "H2")
  5805. (org-invisible-p2)))
  5806. ;; With an optional argument, limit match to TODO entries.
  5807. (should-not
  5808. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5809. (org-match-sparse-tree t "tag")
  5810. (search-forward "H1")
  5811. (org-invisible-p2)))
  5812. (should
  5813. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5814. (org-match-sparse-tree t "tag")
  5815. (search-forward "H2")
  5816. (org-invisible-p2))))
  5817. (ert-deftest test-org/occur ()
  5818. "Test `org-occur' specifications."
  5819. ;; Count number of matches.
  5820. (should
  5821. (= 1
  5822. (org-test-with-temp-text "* H\nA\n* H2"
  5823. (org-occur "A"))))
  5824. (should
  5825. (= 2
  5826. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5827. (org-occur "A"))))
  5828. ;; Test CALLBACK optional argument.
  5829. (should
  5830. (= 0
  5831. (org-test-with-temp-text "* H\nA\n* H2"
  5832. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5833. (should
  5834. (= 1
  5835. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5836. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5837. ;; Case-fold searches according to `org-occur-case-fold-search'.
  5838. (should
  5839. (= 2
  5840. (org-test-with-temp-text "Aa"
  5841. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  5842. (should
  5843. (= 2
  5844. (org-test-with-temp-text "Aa"
  5845. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  5846. (should
  5847. (= 1
  5848. (org-test-with-temp-text "Aa"
  5849. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  5850. (should
  5851. (= 1
  5852. (org-test-with-temp-text "Aa"
  5853. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  5854. (should
  5855. (= 1
  5856. (org-test-with-temp-text "Aa"
  5857. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  5858. (should
  5859. (= 2
  5860. (org-test-with-temp-text "Aa"
  5861. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  5862. ;;; Tags
  5863. (ert-deftest test-org/tag-string-to-alist ()
  5864. "Test `org-tag-string-to-alist' specifications."
  5865. ;; Tag without selection key.
  5866. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  5867. ;; Tag with selection key.
  5868. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  5869. ;; Tag group.
  5870. (should
  5871. (equal
  5872. (org-tag-string-to-alist "[ group : t1 t2 ]")
  5873. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  5874. ;; Mutually exclusive tags.
  5875. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  5876. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  5877. (should
  5878. (equal
  5879. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  5880. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  5881. (ert-deftest test-org/tag-alist-to-string ()
  5882. "Test `org-tag-alist-to-string' specifications."
  5883. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  5884. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  5885. (should
  5886. (equal
  5887. (org-tag-alist-to-string
  5888. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5889. "[ group : t1 t2 ]"))
  5890. (should
  5891. (equal (org-tag-alist-to-string
  5892. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  5893. "{ tag1 tag2 }"))
  5894. (should
  5895. (equal
  5896. (org-tag-alist-to-string
  5897. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  5898. "{ group : tag1 tag2 }")))
  5899. (ert-deftest test-org/tag-alist-to-groups ()
  5900. "Test `org-tag-alist-to-groups' specifications."
  5901. (should
  5902. (equal (org-tag-alist-to-groups
  5903. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  5904. '(("group" "t1" "t2"))))
  5905. (should
  5906. (equal
  5907. (org-tag-alist-to-groups
  5908. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5909. '(("group" "t1" "t2"))))
  5910. (should-not
  5911. (org-tag-alist-to-groups
  5912. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  5913. (ert-deftest test-org/tag-align ()
  5914. "Test tags alignment."
  5915. ;; Test aligning tags with different display width.
  5916. (should
  5917. ;; 12345678901234567890
  5918. (equal "* Test :abc:"
  5919. (org-test-with-temp-text "* Test :abc:"
  5920. (let ((org-tags-column -20)
  5921. (indent-tabs-mode nil))
  5922. (org-fix-tags-on-the-fly))
  5923. (buffer-string))))
  5924. (should
  5925. ;; 12345678901234567890
  5926. (equal "* Test :日本語:"
  5927. (org-test-with-temp-text "* Test :日本語:"
  5928. (let ((org-tags-column -20)
  5929. (indent-tabs-mode nil))
  5930. (org-fix-tags-on-the-fly))
  5931. (buffer-string))))
  5932. ;; Make sure aligning tags do not skip invisible text.
  5933. (should
  5934. (equal "* [[linkx]] :tag:"
  5935. (org-test-with-temp-text "* [[link<point>]] :tag:"
  5936. (let ((org-tags-column 0))
  5937. (org-fix-tags-on-the-fly)
  5938. (insert "x")
  5939. (buffer-string)))))
  5940. ;; Aligning tags preserve position.
  5941. (should
  5942. (= 6 (org-test-with-temp-text "* 345 <point> :tag:"
  5943. (let ((org-tags-column 78)
  5944. (indent-tabs-mode nil))
  5945. (org-fix-tags-on-the-fly))
  5946. (current-column))))
  5947. ;; Aligning all tags in visible buffer.
  5948. (should
  5949. ;; 12345678901234567890
  5950. (equal (concat "* Level 1 :abc:\n"
  5951. "** Level 2 :def:")
  5952. (org-test-with-temp-text (concat "* Level 1 :abc:\n"
  5953. "** Level 2 :def:")
  5954. (let ((org-tags-column -20)
  5955. (indent-tabs-mode nil))
  5956. ;; (org-align-tags :all) must work even when the point
  5957. ;; is at the end of the buffer.
  5958. (goto-char (point-max))
  5959. (org-align-tags :all))
  5960. (buffer-string)))))
  5961. (ert-deftest test-org/get-tags ()
  5962. "Test `org-get-tags' specifications."
  5963. ;; Standard test.
  5964. (should
  5965. (equal '("foo")
  5966. (org-test-with-temp-text "* Test :foo:" (org-get-tags))))
  5967. (should
  5968. (equal '("foo" "bar")
  5969. (org-test-with-temp-text "* Test :foo:bar:" (org-get-tags))))
  5970. ;; Return nil when there is no tag.
  5971. (should-not
  5972. (org-test-with-temp-text "* Test" (org-get-tags)))
  5973. ;; Tags are inherited from parent headlines.
  5974. (should
  5975. (equal '("tag")
  5976. (let ((org-use-tag-inheritance t))
  5977. (org-test-with-temp-text "* H0 :foo:\n* H1 :tag:\n<point>** H2"
  5978. (org-get-tags)))))
  5979. ;; Tags are inherited from `org-file-tags'.
  5980. (should
  5981. (equal '("tag")
  5982. (org-test-with-temp-text "* H1"
  5983. (let ((org-file-tags '("tag"))
  5984. (org-use-tag-inheritance t))
  5985. (org-get-tags)))))
  5986. ;; Only inherited tags have the `inherited' text property.
  5987. (should
  5988. (get-text-property 0 'inherited
  5989. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5990. (let ((org-use-tag-inheritance t))
  5991. (assoc-string "foo" (org-get-tags))))))
  5992. (should-not
  5993. (get-text-property 0 'inherited
  5994. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5995. (let ((org-use-tag-inheritance t))
  5996. (assoc-string "bar" (org-get-tags))))))
  5997. ;; Obey to `org-use-tag-inheritance'.
  5998. (should-not
  5999. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  6000. (let ((org-use-tag-inheritance nil))
  6001. (assoc-string "foo" (org-get-tags)))))
  6002. (should-not
  6003. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  6004. (let ((org-use-tag-inheritance nil)
  6005. (org-file-tags '("foo")))
  6006. (assoc-string "foo" (org-get-tags)))))
  6007. (should-not
  6008. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6009. (let ((org-use-tag-inheritance '("bar")))
  6010. (assoc-string "foo" (org-get-tags)))))
  6011. (should
  6012. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6013. (let ((org-use-tag-inheritance '("bar")))
  6014. (assoc-string "bar" (org-get-tags)))))
  6015. (should-not
  6016. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6017. (let ((org-use-tag-inheritance "b.*"))
  6018. (assoc-string "foo" (org-get-tags)))))
  6019. (should
  6020. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6021. (let ((org-use-tag-inheritance "b.*"))
  6022. (assoc-string "bar" (org-get-tags)))))
  6023. ;; When optional argument LOCAL is non-nil, ignore tag inheritance.
  6024. (should
  6025. (equal '("baz")
  6026. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6027. (let ((org-use-tag-inheritance t))
  6028. (org-get-tags nil t)))))
  6029. ;; When optional argument POS is non-nil, get tags there instead.
  6030. (should
  6031. (equal '("foo")
  6032. (org-test-with-temp-text "* H1 :foo:\n* <point>H2 :bar:"
  6033. (org-get-tags 1))))
  6034. ;; Make sure tags excluded from inheritance are returned if local
  6035. (should
  6036. (equal '("foo")
  6037. (org-test-with-temp-text "* Test :foo:"
  6038. (let ((org-use-tag-inheritance t)
  6039. (org-tags-exclude-from-inheritance '("foo")))
  6040. (org-get-tags)))))
  6041. ;; Test the collection of tags from #+filetags and parent tags.
  6042. (should
  6043. (equal '("a" "b" "c" "d")
  6044. (org-test-with-temp-text (concat "#+filetags: a\n"
  6045. "* Level 1 :b:\n"
  6046. "** Level 2 :c:\n"
  6047. "*** Level 3 :d:\n"
  6048. "<point>")
  6049. (let ((org-use-tag-inheritance t))
  6050. (org-mode-restart) ;So that `org-file-tags' get populated from #+filetags
  6051. (org-get-tags)))))
  6052. ;; Pathological case: tagged headline with an empty body.
  6053. (should (org-test-with-temp-text "* :tag:" (org-get-tags))))
  6054. (ert-deftest test-org/set-tags ()
  6055. "Test `org-set-tags' specifications."
  6056. ;; Throw an error on invalid data.
  6057. (should-error
  6058. (org-test-with-temp-text "* H"
  6059. (org-set-tags 'foo)))
  6060. ;; `nil', an empty, and a blank string remove all tags.
  6061. (should
  6062. (equal "* H"
  6063. (org-test-with-temp-text "* H :tag1:tag2:"
  6064. (org-set-tags nil)
  6065. (buffer-string))))
  6066. (should
  6067. (equal "* H"
  6068. (org-test-with-temp-text "* H :tag1:tag2:"
  6069. (org-set-tags "")
  6070. (buffer-string))))
  6071. (should
  6072. (equal "* H"
  6073. (org-test-with-temp-text "* H :tag1:tag2:"
  6074. (org-set-tags " ")
  6075. (buffer-string))))
  6076. ;; If there's nothing to remove, just bail out.
  6077. (should
  6078. (equal "* H"
  6079. (org-test-with-temp-text "* H"
  6080. (org-set-tags nil)
  6081. (buffer-string))))
  6082. (should
  6083. (equal "* "
  6084. (org-test-with-temp-text "* "
  6085. (org-set-tags nil)
  6086. (buffer-string))))
  6087. ;; If DATA is a tag string, set current tags to it, even if it means
  6088. ;; replacing old tags.
  6089. (should
  6090. (equal "* H :tag0:"
  6091. (org-test-with-temp-text "* H :tag1:tag2:"
  6092. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  6093. (buffer-string))))
  6094. (should
  6095. (equal "* H :tag0:"
  6096. (org-test-with-temp-text "* H"
  6097. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  6098. (buffer-string))))
  6099. ;; If DATA is a list, set tags to this list, even if it means
  6100. ;; replacing old tags.
  6101. (should
  6102. (equal "* H :tag0:"
  6103. (org-test-with-temp-text "* H :tag1:tag2:"
  6104. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6105. (buffer-string))))
  6106. (should
  6107. (equal "* H :tag0:"
  6108. (org-test-with-temp-text "* H"
  6109. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6110. (buffer-string))))
  6111. ;; When set, apply `org-tags-sort-function'.
  6112. (should
  6113. (equal "* H :a:b:"
  6114. (org-test-with-temp-text "* H"
  6115. (let ((org-tags-column 1)
  6116. (org-tags-sort-function #'string<))
  6117. (org-set-tags '("b" "a"))
  6118. (buffer-string)))))
  6119. ;; When new tags are identical to the previous ones, still align.
  6120. (should
  6121. (equal "* H :foo:"
  6122. (org-test-with-temp-text "* H :foo:"
  6123. (let ((org-tags-column 1))
  6124. (org-set-tags '("foo"))
  6125. (buffer-string)))))
  6126. ;; When tags have been changed, run `org-after-tags-change-hook'.
  6127. (should
  6128. (catch :return
  6129. (org-test-with-temp-text "* H :foo:"
  6130. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6131. (org-set-tags '("bar"))
  6132. nil))))
  6133. (should-not
  6134. (catch :return
  6135. (org-test-with-temp-text "* H :foo:"
  6136. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6137. (org-set-tags '("foo"))
  6138. nil))))
  6139. ;; Special case: handle empty headlines.
  6140. (should
  6141. (equal "* :tag0:"
  6142. (org-test-with-temp-text "* "
  6143. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6144. (buffer-string))))
  6145. ;; Modify buffer only when a tag change happens or alignment is
  6146. ;; done.
  6147. (should-not
  6148. (org-test-with-temp-text "* H :foo:"
  6149. (set-buffer-modified-p nil)
  6150. (let ((org-tags-column 1)) (org-set-tags '("foo")))
  6151. (buffer-modified-p)))
  6152. (should
  6153. (org-test-with-temp-text "* H :foo:"
  6154. (set-buffer-modified-p nil)
  6155. (let ((org-tags-column 10)) (org-set-tags '("foo")))
  6156. (buffer-modified-p)))
  6157. (should
  6158. (org-test-with-temp-text "* H :foo:"
  6159. (set-buffer-modified-p nil)
  6160. (let ((org-tags-column 10)) (org-set-tags '("bar")))
  6161. (buffer-modified-p)))
  6162. ;; Pathological case: when setting tags of a folded headline, do not
  6163. ;; let new tags being sucked into invisibility.
  6164. (should-not
  6165. (org-test-with-temp-text "* H1\nContent\n* H2\n\n Other Content"
  6166. ;; Show only headlines
  6167. (org-content)
  6168. ;; Set NEXT tag on current entry
  6169. (org-set-tags ":NEXT:")
  6170. ;; Move point to that NEXT tag
  6171. (search-forward "NEXT") (backward-word)
  6172. ;; And it should be visible (i.e. no overlays)
  6173. (overlays-at (point)))))
  6174. (ert-deftest test-org/set-tags-command ()
  6175. "Test `org-set-tags-command' specifications"
  6176. ;; Set tags at current headline.
  6177. (should
  6178. (equal "* H1 :foo:"
  6179. (org-test-with-temp-text "* H1"
  6180. (cl-letf (((symbol-function 'completing-read)
  6181. (lambda (&rest args) ":foo:")))
  6182. (let ((org-use-fast-tag-selection nil)
  6183. (org-tags-column 1))
  6184. (org-set-tags-command)))
  6185. (buffer-string))))
  6186. ;; Preserve position when called from the section below.
  6187. (should
  6188. (equal "* H1 :foo:\nContents"
  6189. (org-test-with-temp-text "* H1\n<point>Contents"
  6190. (cl-letf (((symbol-function 'completing-read)
  6191. (lambda (&rest args) ":foo:")))
  6192. (let ((org-use-fast-tag-selection nil)
  6193. (org-tags-column 1))
  6194. (org-set-tags-command)))
  6195. (buffer-string))))
  6196. (should-not
  6197. (equal "* H1 :foo:\nContents2"
  6198. (org-test-with-temp-text "* H1\n<point>Contents2"
  6199. (cl-letf (((symbol-function 'completing-read)
  6200. (lambda (&rest args) ":foo:")))
  6201. (let ((org-use-fast-tag-selection nil)
  6202. (org-tags-column 1))
  6203. (org-set-tags-command)))
  6204. (org-at-heading-p))))
  6205. ;; Strip all forbidden characters from user-entered tags.
  6206. (should
  6207. (equal "* H1 :foo:"
  6208. (org-test-with-temp-text "* H1"
  6209. (cl-letf (((symbol-function 'completing-read)
  6210. (lambda (&rest args) ": foo *:")))
  6211. (let ((org-use-fast-tag-selection nil)
  6212. (org-tags-column 1))
  6213. (org-set-tags-command)))
  6214. (buffer-string))))
  6215. ;; When a region is active and
  6216. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  6217. ;; same value in all headlines in region.
  6218. (should
  6219. (equal "* H1 :foo:\nContents\n* H2 :foo:"
  6220. (org-test-with-temp-text "* H1\nContents\n* H2"
  6221. (cl-letf (((symbol-function 'completing-read)
  6222. (lambda (&rest args) ":foo:")))
  6223. (let ((org-use-fast-tag-selection nil)
  6224. (org-loop-over-headlines-in-active-region t)
  6225. (org-tags-column 1))
  6226. (transient-mark-mode 1)
  6227. (push-mark (point) t t)
  6228. (goto-char (point-max))
  6229. (org-set-tags-command)))
  6230. (buffer-string))))
  6231. (should
  6232. (equal "* H1\nContents\n* H2 :foo:"
  6233. (org-test-with-temp-text "* H1\nContents\n* H2"
  6234. (cl-letf (((symbol-function 'completing-read)
  6235. (lambda (&rest args) ":foo:")))
  6236. (let ((org-use-fast-tag-selection nil)
  6237. (org-loop-over-headlines-in-active-region nil)
  6238. (org-tags-column 1))
  6239. (transient-mark-mode 1)
  6240. (push-mark (point) t t)
  6241. (goto-char (point-max))
  6242. (org-set-tags-command)))
  6243. (buffer-string))))
  6244. ;; With a non-nil prefix argument, align all tags in the buffer.
  6245. (should
  6246. (equal "* H1 :foo:\n* H2 :bar:"
  6247. (org-test-with-temp-text "* H1 :foo:\n* H2 :bar:"
  6248. (let ((org-tags-column 1)) (org-set-tags-command t))
  6249. (buffer-string)))))
  6250. (ert-deftest test-org/toggle-tag ()
  6251. "Test `org-toggle-tag' specifications."
  6252. ;; Insert missing tag.
  6253. (should
  6254. (equal "* H :tag:"
  6255. (org-test-with-temp-text "* H"
  6256. (let ((org-tags-column 1)) (org-toggle-tag "tag"))
  6257. (buffer-string))))
  6258. (should
  6259. (equal "* H :tag1:tag2:"
  6260. (org-test-with-temp-text "* H :tag1:"
  6261. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6262. (buffer-string))))
  6263. ;; Remove existing tag.
  6264. (should
  6265. (equal "* H"
  6266. (org-test-with-temp-text "* H :tag:"
  6267. (org-toggle-tag "tag")
  6268. (buffer-string))))
  6269. (should
  6270. (equal "* H :tag1:"
  6271. (org-test-with-temp-text "* H :tag1:tag2:"
  6272. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6273. (buffer-string))))
  6274. (should
  6275. (equal "* H :tag2:"
  6276. (org-test-with-temp-text "* H :tag1:tag2:"
  6277. (let ((org-tags-column 1)) (org-toggle-tag "tag1"))
  6278. (buffer-string))))
  6279. ;; With optional argument ONOFF set to `on', try to insert the tag,
  6280. ;; even if its already there.
  6281. (should
  6282. (equal "* H :tag:"
  6283. (org-test-with-temp-text "* H"
  6284. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6285. (buffer-string))))
  6286. (should
  6287. (equal "* H :tag:"
  6288. (org-test-with-temp-text "* H :tag:"
  6289. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6290. (buffer-string))))
  6291. ;; With optional argument ONOFF set to `off', try to remove the tag,
  6292. ;; even if its not there.
  6293. (should
  6294. (equal "* H"
  6295. (org-test-with-temp-text "* H :tag:"
  6296. (org-toggle-tag "tag" 'off)
  6297. (buffer-string))))
  6298. (should
  6299. (equal "* H :tag:"
  6300. (org-test-with-temp-text "* H :tag:"
  6301. (let ((org-tags-column 1)) (org-toggle-tag "foo" 'off))
  6302. (buffer-string))))
  6303. ;; Special case: Handle properly tag inheritance. In particular, do
  6304. ;; not set inherited tags.
  6305. (should
  6306. (equal "* H1 :tag:\n** H2 :tag2:tag:"
  6307. (org-test-with-temp-text "* H1 :tag:\n** <point>H2 :tag2:"
  6308. (let ((org-use-tag-inheritance t)
  6309. (org-tags-column 1))
  6310. (org-toggle-tag "tag"))
  6311. (buffer-string))))
  6312. (should
  6313. (equal "* H1 :tag1:tag2:\n** H2 :foo:"
  6314. (org-test-with-temp-text "* H1 :tag1:tag2:\n** <point>H2"
  6315. (let ((org-use-tag-inheritance t)
  6316. (org-tags-column 1))
  6317. (org-toggle-tag "foo"))
  6318. (buffer-string)))))
  6319. (ert-deftest test-org/tags-expand ()
  6320. "Test `org-tags-expand' specifications."
  6321. ;; Expand tag groups as a regexp enclosed withing curly brackets.
  6322. (should
  6323. (equal "{\\<[ABC]\\>}"
  6324. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6325. (org-mode-restart)
  6326. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  6327. (should
  6328. (equal "{\\<\\(?:Aa\\|Bb\\|Cc\\)\\>}"
  6329. (org-test-with-temp-text "#+TAGS: [ Aa : Bb Cc ]"
  6330. (org-mode-restart)
  6331. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "Aa")))))
  6332. ;; Preserve operator before the regexp.
  6333. (should
  6334. (equal "+{\\<[ABC]\\>}"
  6335. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6336. (org-mode-restart)
  6337. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "+A")))))
  6338. (should
  6339. (equal "-{\\<[ABC]\\>}"
  6340. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6341. (org-mode-restart)
  6342. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "-A")))))
  6343. ;; Handle "|" syntax.
  6344. (should
  6345. (equal "{\\<[ABC]\\>}|D"
  6346. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6347. (org-mode-restart)
  6348. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|D")))))
  6349. ;; Handle nested groups.
  6350. (should
  6351. (equal "{\\<[A-D]\\>}"
  6352. (org-test-with-temp-text "#+TAGS: [ A : B C ]\n#+TAGS: [ B : D ]"
  6353. (org-mode-restart)
  6354. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  6355. ;; Expand multiple occurrences of the same group.
  6356. (should
  6357. (equal "{\\<[ABC]\\>}|{\\<[ABC]\\>}"
  6358. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6359. (org-mode-restart)
  6360. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|A")))))
  6361. ;; Preserve regexp matches.
  6362. (should
  6363. (equal "{A+}"
  6364. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6365. (org-mode-restart)
  6366. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "{A+}"))))))
  6367. ;;; TODO keywords
  6368. (ert-deftest test-org/auto-repeat-maybe ()
  6369. "Test `org-auto-repeat-maybe' specifications."
  6370. ;; Do not auto repeat when there is no valid time stamp with
  6371. ;; a repeater in the entry.
  6372. (should-not
  6373. (string-prefix-p
  6374. "* TODO H"
  6375. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6376. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  6377. (org-todo "DONE")
  6378. (buffer-string)))))
  6379. (should-not
  6380. (string-prefix-p
  6381. "* TODO H"
  6382. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6383. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  6384. (org-todo "DONE")
  6385. (buffer-string)))))
  6386. ;; When switching to DONE state, switch back to first TODO keyword
  6387. ;; in sequence, or the same keyword if they have different types.
  6388. (should
  6389. (string-prefix-p
  6390. "* TODO H"
  6391. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6392. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6393. (org-todo "DONE")
  6394. (buffer-string)))))
  6395. (should
  6396. (string-prefix-p
  6397. "* KWD1 H"
  6398. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6399. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6400. (org-todo "DONE")
  6401. (buffer-string)))))
  6402. (should
  6403. (string-prefix-p
  6404. "* KWD2 H"
  6405. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  6406. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6407. (org-todo "DONE")
  6408. (buffer-string)))))
  6409. ;; If there was no TODO keyword in the first place, do not insert
  6410. ;; any either.
  6411. (should
  6412. (string-prefix-p
  6413. "* H"
  6414. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6415. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  6416. (org-todo "DONE")
  6417. (buffer-string)))))
  6418. ;; Revert to REPEAT_TO_STATE, if set.
  6419. (should
  6420. (string-prefix-p
  6421. "* KWD2 H"
  6422. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6423. (org-test-with-temp-text
  6424. "* KWD2 H
  6425. :PROPERTIES:
  6426. :REPEAT_TO_STATE: KWD2
  6427. :END:
  6428. <2012-03-29 Thu +2y>"
  6429. (org-todo "DONE")
  6430. (buffer-string)))))
  6431. ;; When switching to DONE state, update base date. If there are
  6432. ;; multiple repeated time stamps, update them all.
  6433. (should
  6434. (string-match-p
  6435. "<2014-03-29 .* \\+2y>"
  6436. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6437. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6438. (org-todo "DONE")
  6439. (buffer-string)))))
  6440. (should
  6441. (string-match-p
  6442. "<2015-03-04 .* \\+1y>"
  6443. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6444. (org-test-with-temp-text
  6445. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  6446. (org-todo "DONE")
  6447. (buffer-string)))))
  6448. ;; Throw an error if repeater unit is the hour and no time is
  6449. ;; provided in the time-stamp.
  6450. (should-error
  6451. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6452. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  6453. (org-todo "DONE")
  6454. (buffer-string))))
  6455. ;; Do not repeat inactive time stamps with a repeater.
  6456. (should-not
  6457. (string-match-p
  6458. "\\[2014-03-29 .* \\+2y\\]"
  6459. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6460. (org-test-with-temp-text
  6461. "* TODO H\n[2012-03-29 Thu. +2y]"
  6462. (org-todo "DONE")
  6463. (buffer-string)))))
  6464. ;; Do not repeat commented time stamps.
  6465. (should-not
  6466. (string-prefix-p
  6467. "<2015-03-04 .* \\+1y>"
  6468. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6469. (org-test-with-temp-text
  6470. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  6471. (org-todo "DONE")
  6472. (buffer-string)))))
  6473. (should-not
  6474. (string-prefix-p
  6475. "<2015-03-04 .* \\+1y>"
  6476. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6477. (org-test-with-temp-text
  6478. "* TODO H
  6479. <2012-03-29 Thu. +2y>
  6480. #+BEGIN_EXAMPLE
  6481. <2014-03-04 Tue +1y>
  6482. #+END_EXAMPLE"
  6483. (org-todo "DONE")
  6484. (buffer-string)))))
  6485. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  6486. ;; entry, record time of last repeat.
  6487. (should-not
  6488. (string-match-p
  6489. ":LAST_REPEAT:"
  6490. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6491. (org-log-repeat nil))
  6492. (cl-letf (((symbol-function 'org-add-log-setup)
  6493. (lambda (&rest args) nil)))
  6494. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6495. (org-todo "DONE")
  6496. (buffer-string))))))
  6497. (should
  6498. (string-match-p
  6499. ":LAST_REPEAT:"
  6500. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6501. (org-log-repeat t))
  6502. (cl-letf (((symbol-function 'org-add-log-setup)
  6503. (lambda (&rest args) nil)))
  6504. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6505. (org-todo "DONE")
  6506. (buffer-string))))))
  6507. (should
  6508. (string-match-p
  6509. ":LAST_REPEAT:"
  6510. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6511. (cl-letf (((symbol-function 'org-add-log-setup)
  6512. (lambda (&rest args) nil)))
  6513. (org-test-with-temp-text
  6514. "* TODO H\n<2012-03-29 Thu +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  6515. (org-todo "DONE")
  6516. (buffer-string))))))
  6517. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  6518. ;; the entry as it is no longer relevant.
  6519. (should-not
  6520. (string-match-p
  6521. "^SCHEDULED:"
  6522. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6523. (org-test-with-temp-text
  6524. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  6525. (org-todo "DONE")
  6526. (buffer-string)))))
  6527. ;; Properly advance repeater even when a clock entry is specified
  6528. ;; and `org-log-repeat' is nil.
  6529. (should
  6530. (string-match-p
  6531. "SCHEDULED: <2014-03-29"
  6532. (let ((org-log-repeat nil)
  6533. (org-todo-keywords '((sequence "TODO" "DONE"))))
  6534. (org-test-with-temp-text
  6535. "* TODO H
  6536. SCHEDULED: <2012-03-29 Thu +2y>
  6537. CLOCK: [2012-03-29 Thu 10:00]--[2012-03-29 Thu 16:40] => 6:40"
  6538. (org-todo "DONE")
  6539. (buffer-string))))))
  6540. ;;; Timestamps API
  6541. (ert-deftest test-org/at-timestamp-p ()
  6542. "Test `org-at-timestamp-p' specifications."
  6543. (should
  6544. (org-test-with-temp-text "<2012-03-29 Thu>"
  6545. (org-at-timestamp-p)))
  6546. (should-not
  6547. (org-test-with-temp-text "2012-03-29 Thu"
  6548. (org-at-timestamp-p)))
  6549. ;; Test return values.
  6550. (should
  6551. (eq 'bracket
  6552. (org-test-with-temp-text "<2012-03-29 Thu>"
  6553. (org-at-timestamp-p))))
  6554. (should
  6555. (eq 'year
  6556. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  6557. (org-at-timestamp-p))))
  6558. (should
  6559. (eq 'month
  6560. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  6561. (org-at-timestamp-p))))
  6562. (should
  6563. (eq 'day
  6564. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  6565. (org-at-timestamp-p))))
  6566. (should
  6567. (eq 'day
  6568. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  6569. (org-at-timestamp-p))))
  6570. (should
  6571. (wholenump
  6572. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  6573. (org-at-timestamp-p))))
  6574. (should
  6575. (eq 'bracket
  6576. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  6577. (org-at-timestamp-p))))
  6578. (should
  6579. (eq 'after
  6580. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  6581. (org-at-timestamp-p))))
  6582. ;; Test `inactive' optional argument.
  6583. (should
  6584. (org-test-with-temp-text "[2012-03-29 Thu]"
  6585. (org-at-timestamp-p 'inactive)))
  6586. (should-not
  6587. (org-test-with-temp-text "[2012-03-29 Thu]"
  6588. (org-at-timestamp-p)))
  6589. ;; When optional argument is `agenda', recognize time-stamps in
  6590. ;; planning info line, property drawers and clocks.
  6591. (should
  6592. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6593. (org-at-timestamp-p 'agenda)))
  6594. (should-not
  6595. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6596. (org-at-timestamp-p)))
  6597. (should
  6598. (org-test-with-temp-text
  6599. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6600. (org-at-timestamp-p 'agenda)))
  6601. (should-not
  6602. (org-test-with-temp-text
  6603. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6604. (org-at-timestamp-p)))
  6605. (should
  6606. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6607. (let ((org-agenda-include-inactive-timestamps t))
  6608. (org-at-timestamp-p 'agenda))))
  6609. (should-not
  6610. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6611. (let ((org-agenda-include-inactive-timestamps t))
  6612. (org-at-timestamp-p))))
  6613. (should-not
  6614. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6615. (let ((org-agenda-include-inactive-timestamps t))
  6616. (org-at-timestamp-p 'inactive))))
  6617. ;; When optional argument is `lax', match any part of the document
  6618. ;; with Org timestamp syntax.
  6619. (should
  6620. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6621. (org-at-timestamp-p 'lax)))
  6622. (should-not
  6623. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6624. (org-at-timestamp-p)))
  6625. (should
  6626. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6627. (org-at-timestamp-p 'lax)))
  6628. (should-not
  6629. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6630. (org-at-timestamp-p)))
  6631. (should
  6632. (org-test-with-temp-text
  6633. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6634. (org-at-timestamp-p 'lax)))
  6635. (should-not
  6636. (org-test-with-temp-text
  6637. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6638. (org-at-timestamp-p)))
  6639. ;; Optional argument `lax' also matches inactive timestamps.
  6640. (should
  6641. (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
  6642. (org-at-timestamp-p 'lax))))
  6643. (ert-deftest test-org/time-stamp ()
  6644. "Test `org-time-stamp' specifications."
  6645. ;; Insert chosen time stamp at point.
  6646. (should
  6647. (string-match
  6648. "Te<2014-03-04 .*?>xt"
  6649. (org-test-with-temp-text "Te<point>xt"
  6650. (cl-letf (((symbol-function 'org-read-date)
  6651. (lambda (&rest args)
  6652. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6653. (org-time-stamp nil)
  6654. (buffer-string)))))
  6655. ;; With a prefix argument, also insert time.
  6656. (should
  6657. (string-match
  6658. "Te<2014-03-04 .*? 00:41>xt"
  6659. (org-test-with-temp-text "Te<point>xt"
  6660. (cl-letf (((symbol-function 'org-read-date)
  6661. (lambda (&rest args)
  6662. (apply #'encode-time
  6663. (org-parse-time-string "2014-03-04 00:41")))))
  6664. (org-time-stamp '(4))
  6665. (buffer-string)))))
  6666. ;; With two universal prefix arguments, insert an active timestamp
  6667. ;; with the current time without prompting the user.
  6668. (should
  6669. (string-match
  6670. "Te<2014-03-04 .*? 00:41>xt"
  6671. (org-test-with-temp-text "Te<point>xt"
  6672. (cl-letf (((symbol-function 'current-time)
  6673. (lambda ()
  6674. (apply #'encode-time
  6675. (org-parse-time-string "2014-03-04 00:41")))))
  6676. (org-time-stamp '(16))
  6677. (buffer-string)))))
  6678. ;; When optional argument is non-nil, insert an inactive timestamp.
  6679. (should
  6680. (string-match
  6681. "Te\\[2014-03-04 .*?\\]xt"
  6682. (org-test-with-temp-text "Te<point>xt"
  6683. (cl-letf (((symbol-function 'org-read-date)
  6684. (lambda (&rest args)
  6685. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6686. (org-time-stamp nil t)
  6687. (buffer-string)))))
  6688. ;; When called from a timestamp, replace existing one.
  6689. (should
  6690. (string-match
  6691. "<2014-03-04 .*?>"
  6692. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  6693. (cl-letf (((symbol-function 'org-read-date)
  6694. (lambda (&rest args)
  6695. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6696. (org-time-stamp nil)
  6697. (buffer-string)))))
  6698. (should
  6699. (string-match
  6700. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  6701. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  6702. (cl-letf (((symbol-function 'org-read-date)
  6703. (lambda (&rest args)
  6704. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6705. (org-time-stamp nil)
  6706. (buffer-string)))))
  6707. ;; When replacing a timestamp, preserve repeater, if any.
  6708. (should
  6709. (string-match
  6710. "<2014-03-04 .*? \\+2y>"
  6711. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  6712. (cl-letf (((symbol-function 'org-read-date)
  6713. (lambda (&rest args)
  6714. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6715. (org-time-stamp nil)
  6716. (buffer-string)))))
  6717. ;; When called twice in a raw, build a date range.
  6718. (should
  6719. (string-match
  6720. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  6721. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  6722. (cl-letf (((symbol-function 'org-read-date)
  6723. (lambda (&rest args)
  6724. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6725. (let ((last-command 'org-time-stamp)
  6726. (this-command 'org-time-stamp))
  6727. (org-time-stamp nil))
  6728. (buffer-string))))))
  6729. (ert-deftest test-org/timestamp-has-time-p ()
  6730. "Test `org-timestamp-has-time-p' specifications."
  6731. ;; With time.
  6732. (should
  6733. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6734. (org-timestamp-has-time-p (org-element-context))))
  6735. ;; Without time.
  6736. (should-not
  6737. (org-test-with-temp-text "<2012-03-29 Thu>"
  6738. (org-timestamp-has-time-p (org-element-context)))))
  6739. (ert-deftest test-org/get-repeat ()
  6740. "Test `org-get-repeat' specifications."
  6741. (should
  6742. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  6743. (org-get-repeat)))
  6744. (should-not
  6745. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  6746. (org-get-repeat)))
  6747. ;; Return proper repeat string.
  6748. (should
  6749. (equal "+2y"
  6750. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  6751. (org-get-repeat))))
  6752. ;; Prevent false positive (commented or verbatim time stamps)
  6753. (should-not
  6754. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  6755. (org-get-repeat)))
  6756. (should-not
  6757. (org-test-with-temp-text
  6758. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  6759. (org-get-repeat)))
  6760. ;; Return nil when called before first heading.
  6761. (should-not
  6762. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  6763. (org-get-repeat)))
  6764. ;; When called with an optional argument, extract repeater from that
  6765. ;; string instead.
  6766. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  6767. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  6768. (ert-deftest test-org/timestamp-format ()
  6769. "Test `org-timestamp-format' specifications."
  6770. ;; Regular test.
  6771. (should
  6772. (equal
  6773. "2012-03-29 16:40"
  6774. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6775. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  6776. ;; Range end.
  6777. (should
  6778. (equal
  6779. "2012-03-29"
  6780. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  6781. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  6782. (ert-deftest test-org/timestamp-split-range ()
  6783. "Test `org-timestamp-split-range' specifications."
  6784. ;; Extract range start (active).
  6785. (should
  6786. (equal '(2012 3 29)
  6787. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6788. (let ((ts (org-timestamp-split-range (org-element-context))))
  6789. (mapcar (lambda (p) (org-element-property p ts))
  6790. '(:year-end :month-end :day-end))))))
  6791. ;; Extract range start (inactive)
  6792. (should
  6793. (equal '(2012 3 29)
  6794. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6795. (let ((ts (org-timestamp-split-range (org-element-context))))
  6796. (mapcar (lambda (p) (org-element-property p ts))
  6797. '(:year-end :month-end :day-end))))))
  6798. ;; Extract range end (active).
  6799. (should
  6800. (equal '(2012 3 30)
  6801. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6802. (let ((ts (org-timestamp-split-range
  6803. (org-element-context) t)))
  6804. (mapcar (lambda (p) (org-element-property p ts))
  6805. '(:year-end :month-end :day-end))))))
  6806. ;; Extract range end (inactive)
  6807. (should
  6808. (equal '(2012 3 30)
  6809. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6810. (let ((ts (org-timestamp-split-range
  6811. (org-element-context) t)))
  6812. (mapcar (lambda (p) (org-element-property p ts))
  6813. '(:year-end :month-end :day-end))))))
  6814. ;; Return the timestamp if not a range.
  6815. (should
  6816. (org-test-with-temp-text "[2012-03-29 Thu]"
  6817. (let* ((ts-orig (org-element-context))
  6818. (ts-copy (org-timestamp-split-range ts-orig)))
  6819. (eq ts-orig ts-copy))))
  6820. (should
  6821. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6822. (let* ((ts-orig (org-element-context))
  6823. (ts-copy (org-timestamp-split-range ts-orig)))
  6824. (eq ts-orig ts-copy)))))
  6825. (ert-deftest test-org/timestamp-translate ()
  6826. "Test `org-timestamp-translate' specifications."
  6827. ;; Translate whole date range.
  6828. (should
  6829. (equal "<29>--<30>"
  6830. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6831. (let ((org-display-custom-times t)
  6832. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6833. (org-timestamp-translate (org-element-context))))))
  6834. ;; Translate date range start.
  6835. (should
  6836. (equal "<29>"
  6837. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6838. (let ((org-display-custom-times t)
  6839. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6840. (org-timestamp-translate (org-element-context) 'start)))))
  6841. ;; Translate date range end.
  6842. (should
  6843. (equal "<30>"
  6844. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6845. (let ((org-display-custom-times t)
  6846. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6847. (org-timestamp-translate (org-element-context) 'end)))))
  6848. ;; Translate time range.
  6849. (should
  6850. (equal "<08>--<16>"
  6851. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  6852. (let ((org-display-custom-times t)
  6853. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  6854. (org-timestamp-translate (org-element-context))))))
  6855. ;; Translate non-range timestamp.
  6856. (should
  6857. (equal "<29>"
  6858. (org-test-with-temp-text "<2012-03-29 Thu>"
  6859. (let ((org-display-custom-times t)
  6860. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6861. (org-timestamp-translate (org-element-context))))))
  6862. ;; Do not change `diary' timestamps.
  6863. (should
  6864. (equal "<%%(org-float t 4 2)>"
  6865. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6866. (let ((org-display-custom-times t)
  6867. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6868. (org-timestamp-translate (org-element-context)))))))
  6869. (ert-deftest test-org/timestamp-from-string ()
  6870. "Test `org-timestamp-from-string' specifications."
  6871. ;; Return nil if argument is not a valid Org timestamp.
  6872. (should-not (org-timestamp-from-string ""))
  6873. (should-not (org-timestamp-from-string nil))
  6874. (should-not (org-timestamp-from-string "<2012-03-29"))
  6875. ;; Otherwise, return a valid Org timestamp object.
  6876. (should
  6877. (equal "<2012-03-29 Thu>"
  6878. (let ((system-time-locale "en_US"))
  6879. (org-element-interpret-data
  6880. (org-timestamp-from-string "<2012-03-29 Thu>")))))
  6881. (should
  6882. (equal "[2014-03-04 Tue]"
  6883. (let ((system-time-locale "en_US"))
  6884. (org-element-interpret-data
  6885. (org-timestamp-from-string "[2014-03-04 Tue]"))))))
  6886. (ert-deftest test-org/timestamp-from-time ()
  6887. "Test `org-timestamp-from-time' specifications."
  6888. ;; Standard test.
  6889. (should
  6890. (equal "<2012-03-29 Thu>"
  6891. (let ((system-time-locale "en_US"))
  6892. (org-element-interpret-data
  6893. (org-timestamp-from-time
  6894. (apply #'encode-time
  6895. (org-parse-time-string "<2012-03-29 Thu 16:40>")))))))
  6896. ;; When optional argument WITH-TIME is non-nil, provide time
  6897. ;; information.
  6898. (should
  6899. (equal "<2012-03-29 Thu 16:40>"
  6900. (let ((system-time-locale "en_US"))
  6901. (org-element-interpret-data
  6902. (org-timestamp-from-time
  6903. (apply #'encode-time
  6904. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  6905. t)))))
  6906. ;; When optional argument INACTIVE is non-nil, return an inactive
  6907. ;; timestamp.
  6908. (should
  6909. (equal "[2012-03-29 Thu]"
  6910. (let ((system-time-locale "en_US"))
  6911. (org-element-interpret-data
  6912. (org-timestamp-from-time
  6913. (apply #'encode-time
  6914. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  6915. nil t))))))
  6916. (ert-deftest test-org/timestamp-to-time ()
  6917. "Test `org-timestamp-to-time' specifications."
  6918. (should
  6919. (equal "2014-03-04"
  6920. (format-time-string
  6921. "%Y-%m-%d"
  6922. (org-timestamp-to-time
  6923. (org-timestamp-from-string "<2014-03-04 Tue>")))))
  6924. (should
  6925. (equal "2014-03-04"
  6926. (format-time-string
  6927. "%Y-%m-%d"
  6928. (org-timestamp-to-time
  6929. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  6930. (should
  6931. (equal "2012-03-29 08:30"
  6932. (format-time-string
  6933. "%Y-%m-%d %H:%M"
  6934. (org-timestamp-to-time
  6935. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")))))
  6936. (should
  6937. (equal "2012-03-29"
  6938. (format-time-string
  6939. "%Y-%m-%d"
  6940. (org-timestamp-to-time
  6941. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")))))
  6942. (should
  6943. (equal "2012-03-29"
  6944. (format-time-string
  6945. "%Y-%m-%d"
  6946. (org-timestamp-to-time
  6947. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")))))
  6948. ;; When optional argument END is non-nil, use end of date range or
  6949. ;; time range.
  6950. (should
  6951. (equal "2012-03-29 16:40"
  6952. (format-time-string
  6953. "%Y-%m-%d %H:%M"
  6954. (org-timestamp-to-time
  6955. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")
  6956. t))))
  6957. (should
  6958. (equal "2014-03-04"
  6959. (format-time-string
  6960. "%Y-%m-%d"
  6961. (org-timestamp-to-time
  6962. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")
  6963. t))))
  6964. (should
  6965. (equal "2014-03-04"
  6966. (format-time-string
  6967. "%Y-%m-%d"
  6968. (org-timestamp-to-time
  6969. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")
  6970. t)))))
  6971. ;;; Visibility
  6972. (ert-deftest test-org/flag-drawer ()
  6973. "Test `org-flag-drawer' specifications."
  6974. ;; Hide drawer.
  6975. (should
  6976. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6977. (org-flag-drawer t)
  6978. (get-char-property (line-end-position) 'invisible)))
  6979. ;; Show drawer.
  6980. (should-not
  6981. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6982. (org-flag-drawer t)
  6983. (org-flag-drawer nil)
  6984. (get-char-property (line-end-position) 'invisible)))
  6985. ;; Test optional argument.
  6986. (should
  6987. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6988. (let ((drawer (save-excursion (search-forward ":D2")
  6989. (org-element-at-point))))
  6990. (org-flag-drawer t drawer)
  6991. (get-char-property (progn (search-forward ":D2") (line-end-position))
  6992. 'invisible))))
  6993. (should-not
  6994. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6995. (let ((drawer (save-excursion (search-forward ":D2")
  6996. (org-element-at-point))))
  6997. (org-flag-drawer t drawer)
  6998. (get-char-property (line-end-position) 'invisible))))
  6999. ;; Do not hide fake drawers.
  7000. (should-not
  7001. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  7002. (forward-line 1)
  7003. (org-flag-drawer t)
  7004. (get-char-property (line-end-position) 'invisible)))
  7005. ;; Do not hide incomplete drawers.
  7006. (should-not
  7007. (org-test-with-temp-text ":D:\nparagraph"
  7008. (forward-line 1)
  7009. (org-flag-drawer t)
  7010. (get-char-property (line-end-position) 'invisible)))
  7011. ;; Do not hide drawers when called from final blank lines.
  7012. (should-not
  7013. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  7014. (goto-char (point-max))
  7015. (org-flag-drawer t)
  7016. (goto-char (point-min))
  7017. (get-char-property (line-end-position) 'invisible)))
  7018. ;; Don't leave point in an invisible part of the buffer when hiding
  7019. ;; a drawer away.
  7020. (should-not
  7021. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  7022. (goto-char (point-max))
  7023. (org-flag-drawer t)
  7024. (get-char-property (point) 'invisible))))
  7025. (ert-deftest test-org/hide-block-toggle ()
  7026. "Test `org-hide-block-toggle' specifications."
  7027. ;; Error when not at a block.
  7028. (should-error
  7029. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  7030. (org-hide-block-toggle 'off)
  7031. (get-char-property (line-end-position) 'invisible)))
  7032. ;; Hide block.
  7033. (should
  7034. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  7035. (org-hide-block-toggle)
  7036. (get-char-property (line-end-position) 'invisible)))
  7037. (should
  7038. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  7039. (org-hide-block-toggle)
  7040. (get-char-property (line-end-position) 'invisible)))
  7041. ;; Show block unconditionally when optional argument is `off'.
  7042. (should-not
  7043. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7044. (org-hide-block-toggle)
  7045. (org-hide-block-toggle 'off)
  7046. (get-char-property (line-end-position) 'invisible)))
  7047. (should-not
  7048. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7049. (org-hide-block-toggle 'off)
  7050. (get-char-property (line-end-position) 'invisible)))
  7051. ;; Hide block unconditionally when optional argument is non-nil.
  7052. (should
  7053. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7054. (org-hide-block-toggle t)
  7055. (get-char-property (line-end-position) 'invisible)))
  7056. (should
  7057. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7058. (org-hide-block-toggle)
  7059. (org-hide-block-toggle t)
  7060. (get-char-property (line-end-position) 'invisible)))
  7061. ;; Do not hide block when called from final blank lines.
  7062. (should-not
  7063. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  7064. (org-hide-block-toggle)
  7065. (goto-char (point-min))
  7066. (get-char-property (line-end-position) 'invisible)))
  7067. ;; Don't leave point in an invisible part of the buffer when hiding
  7068. ;; a block away.
  7069. (should-not
  7070. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  7071. (org-hide-block-toggle)
  7072. (get-char-property (point) 'invisible))))
  7073. (ert-deftest test-org/hide-block-toggle-maybe ()
  7074. "Test `org-hide-block-toggle-maybe' specifications."
  7075. (should
  7076. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  7077. (org-hide-block-toggle-maybe)))
  7078. (should-not
  7079. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  7080. (ert-deftest test-org/show-set-visibility ()
  7081. "Test `org-show-set-visibility' specifications."
  7082. ;; Do not throw an error before first heading.
  7083. (should
  7084. (org-test-with-temp-text "Preamble\n* Headline"
  7085. (org-show-set-visibility 'tree)
  7086. t))
  7087. ;; Test all visibility spans, both on headline and in entry.
  7088. (let ((list-visible-lines
  7089. (lambda (state headerp)
  7090. (org-test-with-temp-text "* Grandmother (0)
  7091. ** Uncle (1)
  7092. *** Heir (2)
  7093. ** Father (3)
  7094. Ancestor text (4)
  7095. *** Sister (5)
  7096. Sibling text (6)
  7097. *** Self (7)
  7098. Match (8)
  7099. **** First born (9)
  7100. Child text (10)
  7101. **** The other child (11)
  7102. *** Brother (12)
  7103. ** Aunt (13)
  7104. "
  7105. (org-cycle t)
  7106. (search-forward (if headerp "Self" "Match"))
  7107. (org-show-set-visibility state)
  7108. (goto-char (point-min))
  7109. (let (result (line 0))
  7110. (while (not (eobp))
  7111. (unless (org-invisible-p2) (push line result))
  7112. (cl-incf line)
  7113. (forward-line))
  7114. (nreverse result))))))
  7115. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  7116. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  7117. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  7118. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  7119. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  7120. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  7121. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  7122. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  7123. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  7124. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  7125. (funcall list-visible-lines 'tree nil)))
  7126. (should (equal '(0 1 3 4 5 7 12 13)
  7127. (funcall list-visible-lines 'canonical t)))
  7128. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  7129. (funcall list-visible-lines 'canonical nil))))
  7130. ;; When point is hidden in a drawer or a block, make sure to make it
  7131. ;; visible.
  7132. (should-not
  7133. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  7134. (org-hide-block-toggle)
  7135. (search-forward "Text")
  7136. (org-show-set-visibility 'minimal)
  7137. (org-invisible-p2)))
  7138. (should-not
  7139. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  7140. (org-flag-drawer t)
  7141. (search-forward "Text")
  7142. (org-show-set-visibility 'minimal)
  7143. (org-invisible-p2)))
  7144. (should-not
  7145. (org-test-with-temp-text
  7146. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  7147. (org-flag-drawer t)
  7148. (forward-line -1)
  7149. (org-hide-block-toggle)
  7150. (search-forward "Text")
  7151. (org-show-set-visibility 'minimal)
  7152. (org-invisible-p2))))
  7153. (defun test-org/copy-visible ()
  7154. "Test `org-copy-visible' specifications."
  7155. (should
  7156. (equal "Foo"
  7157. (org-test-with-temp-text "Foo"
  7158. (let ((kill-ring nil))
  7159. (org-copy-visible (point-min) (point-max))
  7160. (current-kill 0 t)))))
  7161. ;; Skip invisible characters by text property.
  7162. (should
  7163. (equal "Foo"
  7164. (org-test-with-temp-text #("F<hidden>oo" 1 7 (invisible t))
  7165. (let ((kill-ring nil))
  7166. (org-copy-visible (point-min) (point-max))
  7167. (current-kill 0 t)))))
  7168. ;; Skip invisible characters by overlay.
  7169. (should
  7170. (equal "Foo"
  7171. (org-test-with-temp-text "F<hidden>oo"
  7172. (let ((o (make-overlay 2 10)))
  7173. (overlay-put o 'invisible t))
  7174. (let ((kill-ring nil))
  7175. (org-copy-visible (point-min) (point-max))
  7176. (current-kill 0 t)))))
  7177. ;; Handle invisible characters at the beginning and the end of the
  7178. ;; buffer.
  7179. (should
  7180. (equal "Foo"
  7181. (org-test-with-temp-text #("<hidden>Foo" 0 8 (invisible t))
  7182. (let ((kill-ring nil))
  7183. (org-copy-visible (point-min) (point-max))
  7184. (current-kill 0 t)))))
  7185. (should
  7186. (equal "Foo"
  7187. (org-test-with-temp-text #("Foo<hidden>" 3 11 (invisible t))
  7188. (let ((kill-ring nil))
  7189. (org-copy-visible (point-min) (point-max))
  7190. (current-kill 0 t)))))
  7191. ;; Handle multiple visible parts.
  7192. (should
  7193. (equal "abc"
  7194. (org-test-with-temp-text
  7195. #("aXbXc" 1 2 (invisible t) 3 4 (invisible t))
  7196. (let ((kill-ring nil))
  7197. (org-copy-visible (point-min) (point-max))
  7198. (current-kill 0 t))))))
  7199. (ert-deftest test-org/set-visibility-according-to-property ()
  7200. "Test `org-set-visibility-according-to-property' specifications."
  7201. ;; "folded" state.
  7202. (should
  7203. (org-test-with-temp-text
  7204. "
  7205. * a
  7206. :PROPERTIES:
  7207. :VISIBILITY: folded
  7208. :END:
  7209. ** <point>b"
  7210. (org-set-visibility-according-to-property)
  7211. (invisible-p (point))))
  7212. ;; "children" state.
  7213. (should
  7214. (org-test-with-temp-text
  7215. "
  7216. * a
  7217. :PROPERTIES:
  7218. :VISIBILITY: children
  7219. :END:
  7220. ** b
  7221. <point>Contents
  7222. ** c"
  7223. (org-set-visibility-according-to-property)
  7224. (invisible-p (point))))
  7225. (should
  7226. (org-test-with-temp-text
  7227. "
  7228. * a
  7229. :PROPERTIES:
  7230. :VISIBILITY: children
  7231. :END:
  7232. ** b
  7233. Contents
  7234. *** <point>c"
  7235. (org-set-visibility-according-to-property)
  7236. (invisible-p (point))))
  7237. ;; "content" state.
  7238. (should
  7239. (org-test-with-temp-text
  7240. "
  7241. * a
  7242. :PROPERTIES:
  7243. :VISIBILITY: content
  7244. :END:
  7245. ** b
  7246. <point>Contents
  7247. *** c"
  7248. (org-set-visibility-according-to-property)
  7249. (invisible-p (point))))
  7250. (should
  7251. (org-test-with-temp-text
  7252. "
  7253. * a
  7254. :PROPERTIES:
  7255. :VISIBILITY: content
  7256. :END:
  7257. ** b
  7258. Contents
  7259. *** <point>c"
  7260. (org-set-visibility-according-to-property)
  7261. (not (invisible-p (point)))))
  7262. ;; "showall" state.
  7263. (should
  7264. (org-test-with-temp-text
  7265. "
  7266. * a
  7267. :PROPERTIES:
  7268. :VISIBILITY: showall
  7269. :END:
  7270. ** b
  7271. <point>Contents
  7272. *** c"
  7273. (org-set-visibility-according-to-property)
  7274. (not (invisible-p (point)))))
  7275. (should
  7276. (org-test-with-temp-text
  7277. "
  7278. * a
  7279. :PROPERTIES:
  7280. :VISIBILITY: showall
  7281. :END:
  7282. ** b
  7283. Contents
  7284. *** <point>c"
  7285. (org-set-visibility-according-to-property)
  7286. (not (invisible-p (point))))))
  7287. ;;; Yank and Kill
  7288. (ert-deftest test-org/paste-subtree ()
  7289. "Test `org-paste-subtree' specifications."
  7290. ;; Return an error if text to yank is not a set of subtrees.
  7291. (should-error (org-paste-subtree nil "Text"))
  7292. ;; Adjust level according to current one.
  7293. (should
  7294. (equal "* H\n* Text\n"
  7295. (org-test-with-temp-text "* H\n<point>"
  7296. (org-paste-subtree nil "* Text")
  7297. (buffer-string))))
  7298. (should
  7299. (equal "* H1\n** H2\n** Text\n"
  7300. (org-test-with-temp-text "* H1\n** H2\n<point>"
  7301. (org-paste-subtree nil "* Text")
  7302. (buffer-string))))
  7303. ;; When not on a heading, move to next heading before yanking.
  7304. (should
  7305. (equal "* H1\nParagraph\n* Text\n* H2"
  7306. (org-test-with-temp-text "* H1\n<point>Paragraph\n* H2"
  7307. (org-paste-subtree nil "* Text")
  7308. (buffer-string))))
  7309. ;; If point is between two headings, use the deepest level.
  7310. (should
  7311. (equal "* H1\n\n* Text\n* H2"
  7312. (org-test-with-temp-text "* H1\n<point>\n* H2"
  7313. (org-paste-subtree nil "* Text")
  7314. (buffer-string))))
  7315. (should
  7316. (equal "** H1\n\n** Text\n* H2"
  7317. (org-test-with-temp-text "** H1\n<point>\n* H2"
  7318. (org-paste-subtree nil "* Text")
  7319. (buffer-string))))
  7320. (should
  7321. (equal "* H1\n\n** Text\n** H2"
  7322. (org-test-with-temp-text "* H1\n<point>\n** H2"
  7323. (org-paste-subtree nil "* Text")
  7324. (buffer-string))))
  7325. ;; When on an empty heading, after the stars, deduce the new level
  7326. ;; from the number of stars.
  7327. (should
  7328. (equal "*** Text\n"
  7329. (org-test-with-temp-text "*** <point>"
  7330. (org-paste-subtree nil "* Text")
  7331. (buffer-string))))
  7332. ;; Optional argument LEVEL forces a level for the subtree.
  7333. (should
  7334. (equal "* H\n*** Text\n"
  7335. (org-test-with-temp-text "* H<point>"
  7336. (org-paste-subtree 3 "* Text")
  7337. (buffer-string)))))
  7338. (ert-deftest test-org/cut-and-paste-subtree ()
  7339. "Test `org-cut-subtree' and `org-paste-subtree'."
  7340. (should
  7341. (equal
  7342. "* Two
  7343. two
  7344. * One
  7345. "
  7346. (org-test-with-temp-text
  7347. "* One
  7348. <point>* Two
  7349. two
  7350. "
  7351. (call-interactively #'org-cut-subtree)
  7352. (goto-char (point-min))
  7353. (call-interactively #'org-paste-subtree)
  7354. (buffer-string))))
  7355. (should
  7356. (equal
  7357. "* One
  7358. * Two
  7359. "
  7360. (org-test-with-temp-text
  7361. "* One
  7362. <point>* Two
  7363. "
  7364. (call-interactively #'org-cut-subtree)
  7365. (backward-char)
  7366. (call-interactively #'org-paste-subtree)
  7367. (buffer-string)))))
  7368. (provide 'test-org)
  7369. ;;; test-org.el ends here