test-org.el 215 KB

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