test-org.el 225 KB

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