test-org.el 219 KB

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