test-org.el 218 KB

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