test-org.el 243 KB

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