test-org.el 225 KB

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