test-org.el 229 KB

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