test-org.el 220 KB

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