test-org.el 246 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577
  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. (eval-and-compile (require 'cl-lib))
  18. ;;; Comments
  19. (ert-deftest test-org/toggle-comment ()
  20. "Test `org-toggle-comment' specifications."
  21. ;; Simple headline.
  22. (should
  23. (equal "* Test"
  24. (org-test-with-temp-text "* COMMENT Test"
  25. (org-toggle-comment)
  26. (buffer-string))))
  27. (should
  28. (equal "* COMMENT Test"
  29. (org-test-with-temp-text "* Test"
  30. (org-toggle-comment)
  31. (buffer-string))))
  32. ;; Headline with a regular keyword.
  33. (should
  34. (equal "* TODO Test"
  35. (org-test-with-temp-text "* TODO COMMENT Test"
  36. (org-toggle-comment)
  37. (buffer-string))))
  38. (should
  39. (equal "* TODO COMMENT Test"
  40. (org-test-with-temp-text "* TODO Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. ;; Empty headline.
  44. (should
  45. (equal "* "
  46. (org-test-with-temp-text "* COMMENT"
  47. (org-toggle-comment)
  48. (buffer-string))))
  49. (should
  50. (equal "* COMMENT"
  51. (org-test-with-temp-text "* "
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. ;; Headline with a single keyword.
  55. (should
  56. (equal "* TODO "
  57. (org-test-with-temp-text "* TODO COMMENT"
  58. (org-toggle-comment)
  59. (buffer-string))))
  60. (should
  61. (equal "* TODO COMMENT"
  62. (org-test-with-temp-text "* TODO"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. ;; Headline with a keyword, a priority cookie and contents.
  66. (should
  67. (equal "* TODO [#A] Headline"
  68. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  69. (org-toggle-comment)
  70. (buffer-string))))
  71. (should
  72. (equal "* TODO [#A] COMMENT Headline"
  73. (org-test-with-temp-text "* TODO [#A] Headline"
  74. (org-toggle-comment)
  75. (buffer-string)))))
  76. (ert-deftest test-org/comment-dwim ()
  77. "Test `comment-dwim' behaviour in an Org buffer."
  78. ;; No region selected, no comment on current line and line not
  79. ;; empty: insert comment on line above.
  80. (should
  81. (equal "# \nComment"
  82. (org-test-with-temp-text "Comment"
  83. (call-interactively #'org-comment-dwim)
  84. (buffer-string))))
  85. ;; No region selected, no comment on current line and line empty:
  86. ;; insert comment on this line.
  87. (should
  88. (equal "# \nParagraph"
  89. (org-test-with-temp-text "\nParagraph"
  90. (call-interactively #'org-comment-dwim)
  91. (buffer-string))))
  92. ;; No region selected, and a comment on this line: indent it.
  93. (should
  94. (equal "* Headline\n # Comment"
  95. (org-test-with-temp-text "* Headline\n# <point>Comment"
  96. (let ((org-adapt-indentation t))
  97. (call-interactively #'org-comment-dwim))
  98. (buffer-string))))
  99. ;; Also recognize single # at column 0 as comments.
  100. (should
  101. (equal "# Comment"
  102. (org-test-with-temp-text "# Comment"
  103. (call-interactively #'org-comment-dwim)
  104. (buffer-string))))
  105. ;; Region selected and only comments and blank lines within it:
  106. ;; un-comment all commented lines.
  107. (should
  108. (equal "Comment 1\n\nComment 2"
  109. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  110. (transient-mark-mode 1)
  111. (push-mark (point) t t)
  112. (goto-char (point-max))
  113. (call-interactively #'org-comment-dwim)
  114. (buffer-string))))
  115. ;; Region selected without comments: comment all lines if
  116. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  117. (should
  118. (equal "# Comment 1\n\n# Comment 2"
  119. (org-test-with-temp-text "Comment 1\n\nComment 2"
  120. (transient-mark-mode 1)
  121. (push-mark (point) t t)
  122. (goto-char (point-max))
  123. (let ((comment-empty-lines nil))
  124. (call-interactively #'org-comment-dwim))
  125. (buffer-string))))
  126. (should
  127. (equal "# Comment 1\n# \n# Comment 2"
  128. (org-test-with-temp-text "Comment 1\n\nComment 2"
  129. (transient-mark-mode 1)
  130. (push-mark (point) t t)
  131. (goto-char (point-max))
  132. (let ((comment-empty-lines t))
  133. (call-interactively #'org-comment-dwim))
  134. (buffer-string))))
  135. ;; In front of a keyword without region, insert a new comment.
  136. (should
  137. (equal "# \n#+KEYWORD: value"
  138. (org-test-with-temp-text "#+KEYWORD: value"
  139. (call-interactively #'org-comment-dwim)
  140. (buffer-string))))
  141. ;; Comment a heading
  142. (should
  143. (equal "* COMMENT Test"
  144. (org-test-with-temp-text "* Test"
  145. (call-interactively #'org-comment-dwim)
  146. (buffer-string))))
  147. ;; In a source block, use appropriate syntax.
  148. (should
  149. (equal " ;; "
  150. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n<point>\n#+END_SRC"
  151. (let ((org-edit-src-content-indentation 2))
  152. (call-interactively #'org-comment-dwim))
  153. (buffer-substring-no-properties (line-beginning-position)
  154. (point)))))
  155. (should
  156. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  157. (org-test-with-temp-text
  158. "#+BEGIN_SRC emacs-lisp\n<point>a\nb\n#+END_SRC"
  159. (transient-mark-mode 1)
  160. (push-mark (point) t t)
  161. (forward-line 2)
  162. (let ((org-edit-src-content-indentation 2))
  163. (call-interactively #'org-comment-dwim))
  164. (buffer-string)))))
  165. ;;; Date and time analysis
  166. (ert-deftest test-org/org-read-date ()
  167. "Test `org-read-date' specifications."
  168. ;; Parse ISO date with abbreviated year and month.
  169. (should (equal "2012-03-29 16:40"
  170. (let ((org-time-was-given t))
  171. (org-read-date t nil "12-3-29 16:40"))))
  172. ;; Parse Europeans dates.
  173. (should (equal "2012-03-29 16:40"
  174. (let ((org-time-was-given t))
  175. (org-read-date t nil "29.03.2012 16:40"))))
  176. ;; Parse Europeans dates without year.
  177. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  178. (let ((org-time-was-given t))
  179. (org-read-date t nil "29.03. 16:40"))))
  180. ;; Relative date applied to current time if there is single
  181. ;; plus/minus, or to default date when there are two of them.
  182. (should
  183. (equal
  184. "2015-03-04"
  185. (org-test-at-time "2014-03-04"
  186. (org-read-date
  187. t nil "+1y" nil
  188. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  189. (should
  190. (equal
  191. "2013-03-29"
  192. (org-test-at-time "2014-03-04"
  193. (org-read-date
  194. t nil "++1y" nil
  195. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  196. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  197. ;; dates (relatively to now) when incomplete. Otherwise, use
  198. ;; default date.
  199. (should
  200. (equal
  201. "2014-04-01"
  202. (org-test-at-time "2014-03-04"
  203. (let ((org-read-date-prefer-future t))
  204. (org-read-date t nil "1")))))
  205. (should
  206. (equal
  207. "2013-03-04"
  208. (org-test-at-time "2012-03-29"
  209. (let ((org-read-date-prefer-future t))
  210. (org-read-date t nil "3-4")))))
  211. (should
  212. (equal
  213. "2012-03-04"
  214. (org-test-at-time "2012-03-29"
  215. (let ((org-read-date-prefer-future nil))
  216. (org-read-date t nil "3-4")))))
  217. ;; When set to `org-read-date-prefer-future' is set to `time', read
  218. ;; day is moved to tomorrow if specified hour is before current
  219. ;; time. However, it only happens in no other part of the date is
  220. ;; specified.
  221. (should
  222. (equal
  223. "2012-03-30"
  224. (org-test-at-time "2012-03-29 16:40"
  225. (let ((org-read-date-prefer-future 'time))
  226. (org-read-date t nil "00:40" nil)))))
  227. (should-not
  228. (equal
  229. "2012-03-30"
  230. (org-test-at-time "2012-03-29 16:40"
  231. (let ((org-read-date-prefer-future 'time))
  232. (org-read-date t nil "29 00:40" nil)))))
  233. ;; Caveat: `org-read-date-prefer-future' always refers to current
  234. ;; time, not default time, when they differ.
  235. (should
  236. (equal
  237. "2014-04-01"
  238. (org-test-at-time "2014-03-04"
  239. (let ((org-read-date-prefer-future t))
  240. (org-read-date
  241. t nil "1" nil
  242. (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
  243. (should
  244. (equal
  245. "2014-03-25"
  246. (org-test-at-time "2014-03-04"
  247. (let ((org-read-date-prefer-future t))
  248. (org-read-date
  249. t nil "25" nil
  250. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
  251. (ert-deftest test-org/org-parse-time-string ()
  252. "Test `org-parse-time-string'."
  253. (should (equal (org-parse-time-string "2012-03-29 16:40")
  254. '(0 40 16 29 3 2012 nil nil nil)))
  255. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  256. '(0 40 16 29 3 2012 nil nil nil)))
  257. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  258. '(0 40 16 29 3 2012 nil nil nil)))
  259. (should (equal (org-parse-time-string "<2012-03-29>")
  260. '(0 0 0 29 3 2012 nil nil nil)))
  261. (should (equal (org-parse-time-string "<2012-03-29>" t)
  262. '(0 nil nil 29 3 2012 nil nil nil))))
  263. (ert-deftest test-org/closest-date ()
  264. "Test `org-closest-date' specifications."
  265. (require 'calendar)
  266. ;; Time stamps without a repeater are returned unchanged.
  267. (should
  268. (equal
  269. '(3 29 2012)
  270. (calendar-gregorian-from-absolute
  271. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  272. ;; Time stamps with a null repeater are returned unchanged.
  273. (should
  274. (equal
  275. '(3 29 2012)
  276. (calendar-gregorian-from-absolute
  277. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  278. ;; if PREFER is set to `past' always return a date before, or equal
  279. ;; to CURRENT.
  280. (should
  281. (equal
  282. '(3 1 2014)
  283. (calendar-gregorian-from-absolute
  284. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  285. (should
  286. (equal
  287. '(3 4 2014)
  288. (calendar-gregorian-from-absolute
  289. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  290. ;; if PREFER is set to `future' always return a date before, or equal
  291. ;; to CURRENT.
  292. (should
  293. (equal
  294. '(3 29 2014)
  295. (calendar-gregorian-from-absolute
  296. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  297. (should
  298. (equal
  299. '(3 4 2014)
  300. (calendar-gregorian-from-absolute
  301. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  302. ;; If PREFER is neither `past' nor `future', select closest date.
  303. (should
  304. (equal
  305. '(3 1 2014)
  306. (calendar-gregorian-from-absolute
  307. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  308. (should
  309. (equal
  310. '(5 4 2014)
  311. (calendar-gregorian-from-absolute
  312. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  313. ;; Test "day" repeater.
  314. (should
  315. (equal '(3 8 2014)
  316. (calendar-gregorian-from-absolute
  317. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  318. (should
  319. (equal '(3 10 2014)
  320. (calendar-gregorian-from-absolute
  321. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  322. ;; Test "month" repeater.
  323. (should
  324. (equal '(1 5 2015)
  325. (calendar-gregorian-from-absolute
  326. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  327. (should
  328. (equal '(3 29 2014)
  329. (calendar-gregorian-from-absolute
  330. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  331. ;; Test "year" repeater.
  332. (should
  333. (equal '(3 5 2014)
  334. (calendar-gregorian-from-absolute
  335. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  336. (should
  337. (equal '(3 29 2014)
  338. (calendar-gregorian-from-absolute
  339. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  340. (ert-deftest test-org/deadline-close-p ()
  341. "Test `org-deadline-close-p' specifications."
  342. (org-test-at-time "2016-06-03 Fri 01:43"
  343. ;; Timestamps are close if they are within `ndays' of lead time.
  344. (org-test-with-temp-text "* Heading"
  345. (should (org-deadline-close-p "2016-06-03 Fri" 0))
  346. (should (org-deadline-close-p "2016-06-02 Thu" 0))
  347. (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
  348. (should (org-deadline-close-p "2016-06-04 Sat" 1))
  349. (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
  350. ;; Read `ndays' from timestamp if argument not given.
  351. (org-test-with-temp-text "* H"
  352. (should (org-deadline-close-p "2016-06-04 Sat -1d"))
  353. (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
  354. (should (org-deadline-close-p "2016-06-10 Fri -1w"))
  355. (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
  356. ;; Prefer `ndays' argument over lead time in timestamp.
  357. (org-test-with-temp-text "* H"
  358. (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
  359. (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
  360. ;; Completed tasks are never close.
  361. (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
  362. (org-test-with-temp-text "* TODO Heading"
  363. (should (org-deadline-close-p "2016-06-03")))
  364. (org-test-with-temp-text "* DONE Heading"
  365. (should-not (org-deadline-close-p "2016-06-03"))))))
  366. ;;; Drawers
  367. (ert-deftest test-org/insert-property-drawer ()
  368. "Test `org-insert-property-drawer' specifications."
  369. ;; Error before first headline.
  370. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  371. ;; Insert drawer right after headline if there is no planning line,
  372. ;; or after it otherwise.
  373. (should
  374. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  375. (org-test-with-temp-text "* H\nParagraph<point>"
  376. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  377. (buffer-string))))
  378. (should
  379. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  380. (org-test-with-temp-text
  381. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  382. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  383. (buffer-string))))
  384. ;; Indent inserted drawer.
  385. (should
  386. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  387. (org-test-with-temp-text "* H\nParagraph<point>"
  388. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  389. (buffer-string))))
  390. ;; Handle insertion at eob.
  391. (should
  392. (equal "* H\n:PROPERTIES:\n:END:\n"
  393. (org-test-with-temp-text "* H"
  394. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  395. (buffer-string))))
  396. ;; Skip inlinetasks before point.
  397. (when (featurep 'org-inlinetask)
  398. (should
  399. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  400. (org-test-with-temp-text
  401. "* H\n*************** I\n*************** END\nP<point>"
  402. (let ((org-adapt-indentation nil)
  403. (org-inlinetask-min-level 15))
  404. (org-insert-property-drawer))
  405. (buffer-string)))))
  406. ;; Correctly set drawer in an inlinetask.
  407. (when (featurep 'org-inlinetask)
  408. (should
  409. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  410. (org-test-with-temp-text
  411. "* H\n*************** I\nP<point>\n*************** END"
  412. (let ((org-adapt-indentation nil)
  413. (org-inlinetask-min-level 15))
  414. (org-insert-property-drawer))
  415. (buffer-string))))))
  416. ;;; Filling
  417. (ert-deftest test-org/fill-element ()
  418. "Test `org-fill-element' specifications."
  419. ;; At an Org table, align it.
  420. (should
  421. (equal "| a |\n"
  422. (org-test-with-temp-text "|a|"
  423. (org-fill-element)
  424. (buffer-string))))
  425. (should
  426. (equal "#+name: table\n| a |\n"
  427. (org-test-with-temp-text "#+name: table\n| a |\n"
  428. (org-fill-element)
  429. (buffer-string))))
  430. ;; At a paragraph, preserve line breaks.
  431. (org-test-with-temp-text "some \\\\\nlong\ntext"
  432. (let ((fill-column 20))
  433. (org-fill-element)
  434. (should (equal (buffer-string) "some \\\\\nlong text"))))
  435. ;; Correctly fill a paragraph when point is at its very end.
  436. (should
  437. (equal "A B"
  438. (org-test-with-temp-text "A\nB"
  439. (let ((fill-column 20))
  440. (goto-char (point-max))
  441. (org-fill-element)
  442. (buffer-string)))))
  443. ;; Correctly fill the last paragraph of a greater element.
  444. (should
  445. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  446. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  447. (let ((fill-column 8))
  448. (forward-line)
  449. (end-of-line)
  450. (org-fill-element)
  451. (buffer-string)))))
  452. ;; Correctly fill an element in a narrowed buffer.
  453. (should
  454. (equal "01234\n6"
  455. (org-test-with-temp-text "01234 6789"
  456. (let ((fill-column 5))
  457. (narrow-to-region 1 8)
  458. (org-fill-element)
  459. (buffer-string)))))
  460. ;; Handle `adaptive-fill-regexp' in paragraphs.
  461. (should
  462. (equal "> a b"
  463. (org-test-with-temp-text "> a\n> b"
  464. (let ((fill-column 5)
  465. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  466. (org-fill-element)
  467. (buffer-string)))))
  468. ;; Special case: Fill first paragraph when point is at an item or
  469. ;; a plain-list or a footnote reference.
  470. (should
  471. (equal "- A B"
  472. (org-test-with-temp-text "- A\n B"
  473. (let ((fill-column 20))
  474. (org-fill-element)
  475. (buffer-string)))))
  476. (should
  477. (equal "[fn:1] A B"
  478. (org-test-with-temp-text "[fn:1] A\nB"
  479. (let ((fill-column 20))
  480. (org-fill-element)
  481. (buffer-string)))))
  482. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  483. (let ((fill-column 20))
  484. (org-fill-element)
  485. (should (equal (buffer-string)
  486. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  487. ;; Fill contents of `comment-block' elements.
  488. (should
  489. (equal
  490. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  491. (let ((fill-column 20))
  492. (forward-line)
  493. (org-fill-element)
  494. (buffer-string)))
  495. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  496. ;; Fill `comment' elements.
  497. (should
  498. (equal " # A B"
  499. (org-test-with-temp-text " # A\n # B"
  500. (let ((fill-column 20))
  501. (org-fill-element)
  502. (buffer-string)))))
  503. ;; Do not mix consecutive comments when filling one of them.
  504. (should
  505. (equal "# A B\n\n# C"
  506. (org-test-with-temp-text "# A\n# B\n\n# C"
  507. (let ((fill-column 20))
  508. (org-fill-element)
  509. (buffer-string)))))
  510. ;; Use commented empty lines as separators when filling comments.
  511. (should
  512. (equal "# A B\n#\n# C"
  513. (org-test-with-temp-text "# A\n# B\n#\n# C"
  514. (let ((fill-column 20))
  515. (org-fill-element)
  516. (buffer-string)))))
  517. ;; Handle `adaptive-fill-regexp' in comments.
  518. (should
  519. (equal "# > a b"
  520. (org-test-with-temp-text "# > a\n# > b"
  521. (let ((fill-column 20)
  522. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  523. (org-fill-element)
  524. (buffer-string)))))
  525. ;; Do nothing at affiliated keywords.
  526. (should
  527. (equal "#+NAME: para\nSome\ntext."
  528. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  529. (let ((fill-column 20))
  530. (org-fill-element)
  531. (buffer-string)))))
  532. ;; Do not move point after table when filling a table.
  533. (should-not
  534. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  535. (forward-char)
  536. (org-fill-element)
  537. (eobp)))
  538. ;; Do not fill "n" macro, with or without arguments, followed by
  539. ;; a dot or a closing parenthesis since it could be confused with
  540. ;; a numbered bullet.
  541. (should-not
  542. (equal "123456789\n{{{n}}}."
  543. (org-test-with-temp-text "123456789 {{{n}}}."
  544. (let ((fill-column 10))
  545. (org-fill-element)
  546. (buffer-string)))))
  547. (should-not
  548. (equal "123456789\n{{{n}}}\)"
  549. (org-test-with-temp-text "123456789 {{{n}}}\)"
  550. (let ((fill-column 10))
  551. (org-fill-element)
  552. (buffer-string)))))
  553. (should-not
  554. (equal "123456789\n{{{n()}}}."
  555. (org-test-with-temp-text "123456789 {{{n()}}}."
  556. (let ((fill-column 10))
  557. (org-fill-element)
  558. (buffer-string)))))
  559. (should-not
  560. (equal "123456789\n{{{n(counter)}}}."
  561. (org-test-with-temp-text "123456789 {{{n(counter)}}}."
  562. (let ((fill-column 10))
  563. (org-fill-element)
  564. (buffer-string))))))
  565. (ert-deftest test-org/auto-fill-function ()
  566. "Test auto-filling features."
  567. ;; Auto fill paragraph.
  568. (should
  569. (equal "12345\n7890"
  570. (org-test-with-temp-text "12345 7890"
  571. (let ((fill-column 5))
  572. (end-of-line)
  573. (org-auto-fill-function)
  574. (buffer-string)))))
  575. ;; Auto fill first paragraph in an item.
  576. (should
  577. (equal "- 12345\n 7890"
  578. (org-test-with-temp-text "- 12345 7890"
  579. (let ((fill-column 7))
  580. (end-of-line)
  581. (org-auto-fill-function)
  582. (buffer-string)))))
  583. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  584. (should
  585. (equal "> 12345\n 7890"
  586. (org-test-with-temp-text "> 12345 7890"
  587. (let ((fill-column 10)
  588. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  589. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  590. (end-of-line)
  591. (org-auto-fill-function)
  592. (buffer-string)))))
  593. (should
  594. (equal "> 12345\n> 12345\n> 7890"
  595. (org-test-with-temp-text "> 12345\n> 12345 7890"
  596. (let ((fill-column 10)
  597. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  598. (goto-char (point-max))
  599. (org-auto-fill-function)
  600. (buffer-string)))))
  601. (should-not
  602. (equal " 12345\n *12345\n *12345"
  603. (org-test-with-temp-text " 12345\n *12345 12345"
  604. (let ((fill-column 10)
  605. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  606. (goto-char (point-max))
  607. (org-auto-fill-function)
  608. (buffer-string)))))
  609. ;; Auto fill comments.
  610. (should
  611. (equal " # 12345\n # 7890"
  612. (org-test-with-temp-text " # 12345 7890"
  613. (let ((fill-column 10))
  614. (end-of-line)
  615. (org-auto-fill-function)
  616. (buffer-string)))))
  617. ;; A hash within a line isn't a comment.
  618. (should-not
  619. (equal "12345 # 7890\n# 1"
  620. (org-test-with-temp-text "12345 # 7890 1"
  621. (let ((fill-column 12))
  622. (end-of-line)
  623. (org-auto-fill-function)
  624. (buffer-string)))))
  625. ;; Correctly interpret empty prefix.
  626. (should-not
  627. (equal "# a\n# b\nRegular\n# paragraph"
  628. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  629. (let ((fill-column 12))
  630. (end-of-line 3)
  631. (org-auto-fill-function)
  632. (buffer-string)))))
  633. ;; Comment block: auto fill contents.
  634. (should
  635. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  636. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  637. (let ((fill-column 5))
  638. (forward-line)
  639. (end-of-line)
  640. (org-auto-fill-function)
  641. (buffer-string)))))
  642. (should
  643. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  644. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  645. (let ((fill-column 5))
  646. (forward-line)
  647. (end-of-line)
  648. (org-auto-fill-function)
  649. (buffer-string)))))
  650. ;; Do not fill if a new item could be created.
  651. (should-not
  652. (equal "12345\n- 90"
  653. (org-test-with-temp-text "12345 - 90"
  654. (let ((fill-column 5))
  655. (end-of-line)
  656. (org-auto-fill-function)
  657. (buffer-string)))))
  658. ;; Do not fill if a line break could be introduced.
  659. (should-not
  660. (equal "123\\\\\n7890"
  661. (org-test-with-temp-text "123\\\\ 7890"
  662. (let ((fill-column 6))
  663. (end-of-line)
  664. (org-auto-fill-function)
  665. (buffer-string)))))
  666. ;; Do not fill affiliated keywords.
  667. (should-not
  668. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  669. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  670. (let ((fill-column 20))
  671. (end-of-line)
  672. (org-auto-fill-function)
  673. (buffer-string))))))
  674. ;;; Indentation
  675. (ert-deftest test-org/indent-line ()
  676. "Test `org-indent-line' specifications."
  677. ;; Do not indent diary sexps, footnote definitions or headlines.
  678. (should
  679. (zerop
  680. (org-test-with-temp-text "%%(org-calendar-holiday)"
  681. (org-indent-line)
  682. (org-get-indentation))))
  683. (should
  684. (zerop
  685. (org-test-with-temp-text "[fn:1] fn"
  686. (let ((org-adapt-indentation t)) (org-indent-line))
  687. (org-get-indentation))))
  688. (should
  689. (zerop
  690. (org-test-with-temp-text "* H"
  691. (org-indent-line)
  692. (org-get-indentation))))
  693. ;; Do not indent before first headline.
  694. (should
  695. (zerop
  696. (org-test-with-temp-text ""
  697. (org-indent-line)
  698. (org-get-indentation))))
  699. ;; Indent according to headline level otherwise, unless
  700. ;; `org-adapt-indentation' is nil.
  701. (should
  702. (= 2
  703. (org-test-with-temp-text "* H\n<point>A"
  704. (let ((org-adapt-indentation t)) (org-indent-line))
  705. (org-get-indentation))))
  706. (should
  707. (= 2
  708. (org-test-with-temp-text "* H\n<point>\nA"
  709. (let ((org-adapt-indentation t)) (org-indent-line))
  710. (org-get-indentation))))
  711. (should
  712. (zerop
  713. (org-test-with-temp-text "* H\n<point>A"
  714. (let ((org-adapt-indentation nil)) (org-indent-line))
  715. (org-get-indentation))))
  716. ;; Indenting preserves point position.
  717. (should
  718. (org-test-with-temp-text "* H\nA<point>B"
  719. (let ((org-adapt-indentation t)) (org-indent-line))
  720. (looking-at "B")))
  721. ;; Do not change indentation at an item or a LaTeX environment.
  722. (should
  723. (= 1
  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. (= 1
  729. (org-test-with-temp-text
  730. "\\begin{equation}\n <point>1+1=2\n\\end{equation}"
  731. (org-indent-line)
  732. (org-get-indentation))))
  733. ;; On blank lines at the end of a list, indent like last element
  734. ;; within it if the line is still in the list. If the last element
  735. ;; is an item, indent like its contents. Otherwise, indent like the
  736. ;; whole list.
  737. (should
  738. (= 4
  739. (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
  740. (let ((org-adapt-indentation t)) (org-indent-line))
  741. (org-get-indentation))))
  742. (should
  743. (= 4
  744. (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
  745. (let ((org-adapt-indentation t)) (org-indent-line))
  746. (org-get-indentation))))
  747. (should
  748. (zerop
  749. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
  750. (let ((org-adapt-indentation t)) (org-indent-line))
  751. (org-get-indentation))))
  752. (should
  753. (= 4
  754. (org-test-with-temp-text "* H\n- A\n - \n<point>"
  755. (let ((org-adapt-indentation t)) (org-indent-line))
  756. (org-get-indentation))))
  757. (should
  758. (= 4
  759. (org-test-with-temp-text
  760. "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
  761. (let ((org-adapt-indentation t)) (org-indent-line))
  762. (org-get-indentation))))
  763. (should
  764. (= 2
  765. (org-test-with-temp-text "- A\n B\n\n<point>"
  766. (let ((org-adapt-indentation nil)) (org-indent-line))
  767. (org-get-indentation))))
  768. (should
  769. (= 2
  770. (org-test-with-temp-text
  771. "- A\n \begin{cases} 1 + 1\n \end{cases}\n\n<point>"
  772. (let ((org-adapt-indentation nil)) (org-indent-line))
  773. (org-get-indentation))))
  774. ;; Likewise, on a blank line at the end of a footnote definition,
  775. ;; indent at column 0 if line belongs to the definition. Otherwise,
  776. ;; indent like the definition itself.
  777. (should
  778. (zerop
  779. (org-test-with-temp-text "* H\n[fn:1] Definition\n<point>"
  780. (let ((org-adapt-indentation t)) (org-indent-line))
  781. (org-get-indentation))))
  782. (should
  783. (zerop
  784. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n<point>"
  785. (let ((org-adapt-indentation t)) (org-indent-line))
  786. (org-get-indentation))))
  787. ;; After the end of the contents of a greater element, indent like
  788. ;; the beginning of the element.
  789. (should
  790. (= 1
  791. (org-test-with-temp-text
  792. " #+BEGIN_CENTER\n Contents\n<point>#+END_CENTER"
  793. (org-indent-line)
  794. (org-get-indentation))))
  795. ;; On blank lines after a paragraph, indent like its last non-empty
  796. ;; line.
  797. (should
  798. (= 1
  799. (org-test-with-temp-text " Paragraph\n\n<point>"
  800. (org-indent-line)
  801. (org-get-indentation))))
  802. ;; At the first line of an element, indent like previous element's
  803. ;; first line, ignoring footnotes definitions and inline tasks, or
  804. ;; according to parent.
  805. (should
  806. (= 2
  807. (org-test-with-temp-text "A\n\n B\n\nC<point>"
  808. (org-indent-line)
  809. (org-get-indentation))))
  810. (should
  811. (= 1
  812. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC<point>"
  813. (org-indent-line)
  814. (org-get-indentation))))
  815. (should
  816. (= 1
  817. (org-test-with-temp-text
  818. " #+BEGIN_CENTER\n<point> Contents\n#+END_CENTER"
  819. (org-indent-line)
  820. (org-get-indentation))))
  821. ;; Within code part of a source block, use language major mode if
  822. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  823. ;; according to line above.
  824. (should
  825. (= 6
  826. (org-test-with-temp-text
  827. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  828. (let ((org-src-tab-acts-natively t)
  829. (org-edit-src-content-indentation 0))
  830. (org-indent-line))
  831. (org-get-indentation))))
  832. (should
  833. (= 1
  834. (org-test-with-temp-text
  835. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  836. (let ((org-src-tab-acts-natively nil)
  837. (org-edit-src-content-indentation 0))
  838. (org-indent-line))
  839. (org-get-indentation))))
  840. ;; Otherwise, indent like the first non-blank line above.
  841. (should
  842. (zerop
  843. (org-test-with-temp-text
  844. "#+BEGIN_CENTER\nline1\n\n<point> line2\n#+END_CENTER"
  845. (org-indent-line)
  846. (org-get-indentation))))
  847. ;; Align node properties according to `org-property-format'. Handle
  848. ;; nicely empty values.
  849. (should
  850. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  851. (org-test-with-temp-text
  852. "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  853. (let ((org-property-format "%-10s %s")) (org-indent-line))
  854. (buffer-string))))
  855. (should
  856. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  857. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  858. (let ((org-property-format "%-10s %s")) (org-indent-line))
  859. (buffer-string)))))
  860. (ert-deftest test-org/indent-region ()
  861. "Test `org-indent-region' specifications."
  862. ;; Indent paragraph.
  863. (should
  864. (equal "A\nB\nC"
  865. (org-test-with-temp-text " A\nB\n C"
  866. (org-indent-region (point-min) (point-max))
  867. (buffer-string))))
  868. ;; Indent greater elements along with their contents.
  869. (should
  870. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  871. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  872. (org-indent-region (point-min) (point-max))
  873. (buffer-string))))
  874. ;; Ignore contents of verse blocks. Only indent block delimiters.
  875. (should
  876. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  877. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  878. (org-indent-region (point-min) (point-max))
  879. (buffer-string))))
  880. (should
  881. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  882. (org-test-with-temp-text " #+BEGIN_VERSE\n A\n B\n #+END_VERSE"
  883. (org-indent-region (point-min) (point-max))
  884. (buffer-string))))
  885. ;; Indent example blocks as a single block, unless indentation
  886. ;; should be preserved. In this case only indent the block markers.
  887. (should
  888. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  889. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  890. (org-indent-region (point-min) (point-max))
  891. (buffer-string))))
  892. (should
  893. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  894. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  895. (org-indent-region (point-min) (point-max))
  896. (buffer-string))))
  897. (should
  898. (equal "#+BEGIN_EXAMPLE -i\n A\n B\n#+END_EXAMPLE"
  899. (org-test-with-temp-text
  900. " #+BEGIN_EXAMPLE -i\n A\n B\n #+END_EXAMPLE"
  901. (org-indent-region (point-min) (point-max))
  902. (buffer-string))))
  903. (should
  904. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  905. (org-test-with-temp-text
  906. " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  907. (let ((org-src-preserve-indentation t))
  908. (org-indent-region (point-min) (point-max)))
  909. (buffer-string))))
  910. ;; Treat export blocks as a whole.
  911. (should
  912. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  913. (org-test-with-temp-text "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  914. (org-indent-region (point-min) (point-max))
  915. (buffer-string))))
  916. (should
  917. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  918. (org-test-with-temp-text
  919. " #+BEGIN_EXPORT latex\n A\n B\n #+END_EXPORT"
  920. (org-indent-region (point-min) (point-max))
  921. (buffer-string))))
  922. ;; Indent according to mode if `org-src-tab-acts-natively' is
  923. ;; non-nil. Otherwise, do not indent code at all.
  924. (should
  925. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  926. (org-test-with-temp-text
  927. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  928. (let ((org-src-tab-acts-natively t)
  929. (org-edit-src-content-indentation 0))
  930. (org-indent-region (point-min) (point-max)))
  931. (buffer-string))))
  932. (should
  933. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  934. (org-test-with-temp-text
  935. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  936. (let ((org-src-tab-acts-natively nil)
  937. (org-edit-src-content-indentation 0))
  938. (org-indent-region (point-min) (point-max)))
  939. (buffer-string))))
  940. ;; Align node properties according to `org-property-format'. Handle
  941. ;; nicely empty values.
  942. (should
  943. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  944. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  945. (let ((org-property-format "%-10s %s")
  946. (org-adapt-indentation nil))
  947. (org-indent-region (point) (point-max)))
  948. (buffer-string))))
  949. (should
  950. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  951. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  952. (let ((org-property-format "%-10s %s")
  953. (org-adapt-indentation nil))
  954. (org-indent-region (point) (point-max)))
  955. (buffer-string))))
  956. ;; Indent plain lists.
  957. (should
  958. (equal "- A\n B\n - C\n\n D"
  959. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  960. (org-indent-region (point-min) (point-max))
  961. (buffer-string))))
  962. (should
  963. (equal "- A\n\n- B"
  964. (org-test-with-temp-text " - A\n\n - B"
  965. (org-indent-region (point-min) (point-max))
  966. (buffer-string))))
  967. ;; Indent footnote definitions.
  968. (should
  969. (equal "[fn:1] Definition\n\nDefinition"
  970. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  971. (org-indent-region (point-min) (point-max))
  972. (buffer-string))))
  973. ;; Special case: Start indenting on a blank line.
  974. (should
  975. (equal "\nParagraph"
  976. (org-test-with-temp-text "\n Paragraph"
  977. (org-indent-region (point-min) (point-max))
  978. (buffer-string)))))
  979. ;;; Editing
  980. (ert-deftest test-org/delete-indentation ()
  981. "Test `org-delete-indentation' specifications."
  982. ;; Regular test.
  983. (should (equal "foo bar"
  984. (org-test-with-temp-text
  985. "foo \n bar<point>"
  986. (org-delete-indentation)
  987. (buffer-string))))
  988. ;; With optional argument.
  989. (should (equal "foo bar"
  990. (org-test-with-temp-text
  991. "foo<point> \n bar"
  992. (org-delete-indentation t)
  993. (buffer-string))))
  994. ;; At headline text should be appended to the headline text.
  995. (should
  996. (equal"* foo bar :tag:"
  997. (let (org-auto-align-tags)
  998. (org-test-with-temp-text
  999. "* foo :tag:\n bar<point>"
  1000. (org-delete-indentation)
  1001. (buffer-string)))))
  1002. (should
  1003. (equal "* foo bar :tag:"
  1004. (let (org-auto-align-tags)
  1005. (org-test-with-temp-text
  1006. "* foo <point>:tag:\n bar"
  1007. (org-delete-indentation t)
  1008. (buffer-string))))))
  1009. (ert-deftest test-org/return ()
  1010. "Test `org-return' specifications."
  1011. ;; Regular test.
  1012. (should
  1013. (equal "Para\ngraph"
  1014. (org-test-with-temp-text "Para<point>graph"
  1015. (org-return)
  1016. (buffer-string))))
  1017. ;; With optional argument, indent line.
  1018. (should
  1019. (equal " Para\n graph"
  1020. (org-test-with-temp-text " Para<point>graph"
  1021. (org-return t)
  1022. (buffer-string))))
  1023. ;; On a table, call `org-table-next-row'.
  1024. (should
  1025. (org-test-with-temp-text "| <point>a |\n| b |"
  1026. (org-return)
  1027. (looking-at-p "b")))
  1028. ;; Open link or timestamp under point when `org-return-follows-link'
  1029. ;; is non-nil.
  1030. (should
  1031. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1032. (let ((org-return-follows-link t)
  1033. (org-link-search-must-match-exact-headline nil))
  1034. (org-return))
  1035. (looking-at-p "<<target>>")))
  1036. (should-not
  1037. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1038. (let ((org-return-follows-link nil)) (org-return))
  1039. (looking-at-p "<<target>>")))
  1040. (should
  1041. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  1042. (let ((org-return-follows-link t)) (org-return))
  1043. (looking-at-p "* b")))
  1044. (should
  1045. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  1046. (let ((org-return-follows-link t)
  1047. (org-link-search-must-match-exact-headline nil))
  1048. (org-return))
  1049. (looking-at-p "<<target>>")))
  1050. (should-not
  1051. (org-test-with-temp-text "Link [[target]]<point> <<target>>"
  1052. (let ((org-return-follows-link t)
  1053. (org-link-search-must-match-exact-headline nil))
  1054. (org-return))
  1055. (looking-at-p "<<target>>")))
  1056. ;; When `org-return-follows-link' is non-nil, tolerate links and
  1057. ;; timestamps in comments, node properties, etc.
  1058. (should
  1059. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1060. (let ((org-return-follows-link t)
  1061. (org-link-search-must-match-exact-headline nil))
  1062. (org-return))
  1063. (looking-at-p "<<target>>")))
  1064. (should-not
  1065. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1066. (let ((org-return-follows-link nil)) (org-return))
  1067. (looking-at-p "<<target>>")))
  1068. (should-not
  1069. (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
  1070. (let ((org-return-follows-link t)
  1071. (org-link-search-must-match-exact-headline nil))
  1072. (org-return))
  1073. (looking-at-p "<<target>>")))
  1074. ;; `org-return-follows-link' handle multi-line lines.
  1075. (should
  1076. (org-test-with-temp-text
  1077. "[[target][This is a very\n long description<point>]]\n <<target>>"
  1078. (let ((org-return-follows-link t)
  1079. (org-link-search-must-match-exact-headline nil))
  1080. (org-return))
  1081. (looking-at-p "<<target>>")))
  1082. (should-not
  1083. (org-test-with-temp-text
  1084. "[[target][This is a very\n long description]]<point>\n <<target>>"
  1085. (let ((org-return-follows-link t)
  1086. (org-link-search-must-match-exact-headline nil))
  1087. (org-return))
  1088. (looking-at-p "<<target>>")))
  1089. ;; However, do not open link when point is in a table.
  1090. (should
  1091. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1092. (let ((org-return-follows-link t)) (org-return))
  1093. (looking-at-p "between")))
  1094. ;; Special case: in a list, when indenting, do not break structure.
  1095. (should
  1096. (equal "- A\n B"
  1097. (org-test-with-temp-text "- A <point>B"
  1098. (org-return t)
  1099. (buffer-string))))
  1100. (should
  1101. (equal "- A\n\n- B"
  1102. (org-test-with-temp-text "- A\n<point>- B"
  1103. (org-return t)
  1104. (buffer-string))))
  1105. ;; On tags part of a headline, add a newline below it instead of
  1106. ;; breaking it.
  1107. (should
  1108. (equal "* H :tag:\n"
  1109. (org-test-with-temp-text "* H :<point>tag:"
  1110. (org-return)
  1111. (buffer-string))))
  1112. ;; Before headline text, add a newline below it instead of breaking
  1113. ;; it.
  1114. (should
  1115. (equal "* TODO H :tag:\n"
  1116. (org-test-with-temp-text "* <point>TODO H :tag:"
  1117. (org-return)
  1118. (buffer-string))))
  1119. (should
  1120. (equal "* TODO [#B] H :tag:\n"
  1121. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1122. (org-return)
  1123. (buffer-string))))
  1124. (should ;TODO are case-sensitive
  1125. (equal "* \nTodo"
  1126. (org-test-with-temp-text "* <point>Todo"
  1127. (org-return)
  1128. (buffer-string))))
  1129. ;; At headline text, break headline text but preserve tags.
  1130. (should
  1131. (equal "* TODO [#B] foo :tag:\nbar"
  1132. (let (org-auto-align-tags)
  1133. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1134. (org-return)
  1135. (buffer-string)))))
  1136. ;; At bol of headline insert newline.
  1137. (should
  1138. (equal "\n* h"
  1139. (org-test-with-temp-text "<point>* h"
  1140. (org-return)
  1141. (buffer-string))))
  1142. ;; Refuse to leave invalid headline in buffer.
  1143. (should
  1144. (equal "* h\n"
  1145. (org-test-with-temp-text "*<point> h"
  1146. (org-return)
  1147. (buffer-string))))
  1148. ;; Before first column or after last one in a table, split the
  1149. ;; table.
  1150. (should
  1151. (equal "| a |\n\n| b |"
  1152. (org-test-with-temp-text "| a |\n<point>| b |"
  1153. (org-return)
  1154. (buffer-string))))
  1155. (should
  1156. (equal "| a |\n\n| b |"
  1157. (org-test-with-temp-text "| a |<point>\n| b |"
  1158. (org-return)
  1159. (buffer-string))))
  1160. ;; Do not auto-fill on hitting <RET> inside a property drawer.
  1161. (should
  1162. (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:"
  1163. (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:"
  1164. (setq-local fill-column 10)
  1165. (auto-fill-mode 1)
  1166. (org-return)
  1167. (buffer-string)))))
  1168. (ert-deftest test-org/meta-return ()
  1169. "Test M-RET (`org-meta-return') specifications."
  1170. ;; In a table field insert a row above.
  1171. (should
  1172. (org-test-with-temp-text "| a |"
  1173. (forward-char)
  1174. (org-meta-return)
  1175. (forward-line -1)
  1176. (looking-at "| |$")))
  1177. ;; In a paragraph change current line into a header.
  1178. (should
  1179. (org-test-with-temp-text "a"
  1180. (org-meta-return)
  1181. (beginning-of-line)
  1182. (looking-at "\* a$")))
  1183. ;; In an item insert an item, in this case above.
  1184. (should
  1185. (org-test-with-temp-text "- a"
  1186. (org-meta-return)
  1187. (beginning-of-line)
  1188. (looking-at "- $")))
  1189. ;; In a drawer and item insert an item, in this case above.
  1190. (should
  1191. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1192. (forward-line)
  1193. (org-meta-return)
  1194. (beginning-of-line)
  1195. (looking-at "- $"))))
  1196. (ert-deftest test-org/insert-heading ()
  1197. "Test `org-insert-heading' specifications."
  1198. ;; In an empty buffer, insert a new headline.
  1199. (should
  1200. (equal "* "
  1201. (org-test-with-temp-text ""
  1202. (org-insert-heading)
  1203. (buffer-string))))
  1204. ;; At the beginning of a line, turn it into a headline.
  1205. (should
  1206. (equal "* P"
  1207. (org-test-with-temp-text "<point>P"
  1208. (org-insert-heading)
  1209. (buffer-string))))
  1210. ;; In the middle of a line, split the line if allowed, otherwise,
  1211. ;; insert the headline at its end.
  1212. (should
  1213. (equal "Para\n* graph"
  1214. (org-test-with-temp-text "Para<point>graph"
  1215. (let ((org-M-RET-may-split-line '((default . t))))
  1216. (org-insert-heading))
  1217. (buffer-string))))
  1218. (should
  1219. (equal "Paragraph\n* "
  1220. (org-test-with-temp-text "Para<point>graph"
  1221. (let ((org-M-RET-may-split-line '((default . nil))))
  1222. (org-insert-heading))
  1223. (buffer-string))))
  1224. ;; At the beginning of a headline, create one above.
  1225. (should
  1226. (equal "* \n* H"
  1227. (org-test-with-temp-text "* H"
  1228. (org-insert-heading)
  1229. (buffer-string))))
  1230. ;; In the middle of a headline, split it if allowed.
  1231. (should
  1232. (equal "* H\n* 1\n"
  1233. (org-test-with-temp-text "* H<point>1"
  1234. (let ((org-M-RET-may-split-line '((headline . t))))
  1235. (org-insert-heading))
  1236. (buffer-string))))
  1237. (should
  1238. (equal "* H1\n* \n"
  1239. (org-test-with-temp-text "* H<point>1"
  1240. (let ((org-M-RET-may-split-line '((headline . nil))))
  1241. (org-insert-heading))
  1242. (buffer-string))))
  1243. ;; However, splitting cannot happen on TODO keywords, priorities or
  1244. ;; tags.
  1245. (should
  1246. (equal "* TODO H1\n* \n"
  1247. (org-test-with-temp-text "* TO<point>DO H1"
  1248. (let ((org-M-RET-may-split-line '((headline . t))))
  1249. (org-insert-heading))
  1250. (buffer-string))))
  1251. (should
  1252. (equal "* [#A] H1\n* \n"
  1253. (org-test-with-temp-text "* [#<point>A] H1"
  1254. (let ((org-M-RET-may-split-line '((headline . t))))
  1255. (org-insert-heading))
  1256. (buffer-string))))
  1257. (should
  1258. (equal "* H1 :tag:\n* \n"
  1259. (org-test-with-temp-text "* H1 :ta<point>g:"
  1260. (let ((org-M-RET-may-split-line '((headline . t))))
  1261. (org-insert-heading))
  1262. (buffer-string))))
  1263. ;; New headline level depends on the level of the headline above.
  1264. (should
  1265. (equal "** H\n** P"
  1266. (org-test-with-temp-text "** H\n<point>P"
  1267. (org-insert-heading)
  1268. (buffer-string))))
  1269. (should
  1270. (equal "** H\nPara\n** graph"
  1271. (org-test-with-temp-text "** H\nPara<point>graph"
  1272. (let ((org-M-RET-may-split-line '((default . t))))
  1273. (org-insert-heading))
  1274. (buffer-string))))
  1275. (should
  1276. (equal "** \n** H"
  1277. (org-test-with-temp-text "** H"
  1278. (org-insert-heading)
  1279. (buffer-string))))
  1280. ;; When called with one universal argument, insert a new headline at
  1281. ;; the end of the current subtree, independently on the position of
  1282. ;; point.
  1283. (should
  1284. (equal
  1285. "* H1\n** H2\n* \n"
  1286. (org-test-with-temp-text "* H1\n** H2"
  1287. (let ((org-insert-heading-respect-content nil))
  1288. (org-insert-heading '(4)))
  1289. (buffer-string))))
  1290. (should
  1291. (equal
  1292. "* H1\n** H2\n* \n"
  1293. (org-test-with-temp-text "* H<point>1\n** H2"
  1294. (let ((org-insert-heading-respect-content nil))
  1295. (org-insert-heading '(4)))
  1296. (buffer-string))))
  1297. ;; When called with two universal arguments, insert a new headline
  1298. ;; at the end of the grandparent subtree.
  1299. (should
  1300. (equal "* H1\n** H3\n- item\n** H2\n** \n"
  1301. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1302. (let ((org-insert-heading-respect-content nil))
  1303. (org-insert-heading '(16)))
  1304. (buffer-string))))
  1305. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1306. ;; a level 1 heading.
  1307. (should
  1308. (equal "* H1\n** H2\n* \n"
  1309. (org-test-with-temp-text "* H1\n** H2<point>"
  1310. (org-insert-heading nil nil t)
  1311. (buffer-string))))
  1312. (should
  1313. (equal "* H1\n- item\n* "
  1314. (org-test-with-temp-text "* H1\n- item<point>"
  1315. (org-insert-heading nil nil t)
  1316. (buffer-string))))
  1317. ;; Obey `org-blank-before-new-entry'.
  1318. (should
  1319. (equal "* H1\n\n* \n"
  1320. (org-test-with-temp-text "* H1<point>"
  1321. (let ((org-blank-before-new-entry '((heading . t))))
  1322. (org-insert-heading))
  1323. (buffer-string))))
  1324. (should
  1325. (equal "* H1\n* \n"
  1326. (org-test-with-temp-text "* H1<point>"
  1327. (let ((org-blank-before-new-entry '((heading . nil))))
  1328. (org-insert-heading))
  1329. (buffer-string))))
  1330. (should
  1331. (equal "* H1\n* H2\n* \n"
  1332. (org-test-with-temp-text "* H1\n* H2<point>"
  1333. (let ((org-blank-before-new-entry '((heading . auto))))
  1334. (org-insert-heading))
  1335. (buffer-string))))
  1336. (should
  1337. (equal "* H1\n\n* H2\n\n* \n"
  1338. (org-test-with-temp-text "* H1\n\n* H2<point>"
  1339. (let ((org-blank-before-new-entry '((heading . auto))))
  1340. (org-insert-heading))
  1341. (buffer-string))))
  1342. ;; Corner case: correctly insert a headline after an empty one.
  1343. (should
  1344. (equal "* \n* \n"
  1345. (org-test-with-temp-text "* <point>"
  1346. (org-insert-heading)
  1347. (buffer-string))))
  1348. (should
  1349. (org-test-with-temp-text "* <point>\n"
  1350. (org-insert-heading)
  1351. (looking-at-p "\n\\'")))
  1352. ;; Do not insert spurious headlines when inserting a new headline.
  1353. (should
  1354. (equal "* H1\n* H2\n* \n"
  1355. (org-test-with-temp-text "* H1\n* H2<point>\n"
  1356. (org-insert-heading)
  1357. (buffer-string))))
  1358. ;; Preserve visibility at beginning of line. In particular, when
  1359. ;; removing spurious blank lines, do not visually merge heading with
  1360. ;; the line visible above.
  1361. (should-not
  1362. (org-test-with-temp-text "* H1<point>\nContents\n\n* H2\n"
  1363. (org-overview)
  1364. (let ((org-blank-before-new-entry '((heading . nil))))
  1365. (org-insert-heading '(4)))
  1366. (invisible-p (line-end-position 0))))
  1367. ;; Properly handle empty lines when forcing a headline below current
  1368. ;; one.
  1369. (should
  1370. (equal "* H1\n\n* H\n\n* \n"
  1371. (org-test-with-temp-text "* H1\n\n* H<point>"
  1372. (let ((org-blank-before-new-entry '((heading . t))))
  1373. (org-insert-heading '(4))
  1374. (buffer-string))))))
  1375. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1376. "Test `org-insert-todo-heading-respect-content' specifications."
  1377. ;; Create a TODO heading.
  1378. (should
  1379. (org-test-with-temp-text "* H1\n Body"
  1380. (org-insert-todo-heading-respect-content)
  1381. (nth 2 (org-heading-components))))
  1382. ;; Add headline at the end of the first subtree
  1383. (should
  1384. (equal
  1385. "* TODO \n"
  1386. (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
  1387. (org-insert-todo-heading-respect-content)
  1388. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1389. ;; In a list, do not create a new item.
  1390. (should
  1391. (equal
  1392. "* TODO \n"
  1393. (org-test-with-temp-text "* H\n- an item\n- another one"
  1394. (search-forward "an ")
  1395. (org-insert-todo-heading-respect-content)
  1396. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1397. ;; Use the same TODO keyword as current heading.
  1398. (should
  1399. (equal
  1400. "* TODO \n"
  1401. (org-test-with-temp-text "* TODO\n** WAITING\n"
  1402. (org-insert-todo-heading-respect-content)
  1403. (buffer-substring-no-properties (line-beginning-position) (point-max))))))
  1404. (ert-deftest test-org/clone-with-time-shift ()
  1405. "Test `org-clone-subtree-with-time-shift'."
  1406. ;; Raise an error before first heading.
  1407. (should-error
  1408. (org-test-with-temp-text ""
  1409. (org-clone-subtree-with-time-shift 1)))
  1410. ;; Raise an error on invalid number of clones.
  1411. (should-error
  1412. (org-test-with-temp-text "* Clone me"
  1413. (org-clone-subtree-with-time-shift -1)))
  1414. ;; Clone non-repeating once.
  1415. (should
  1416. (equal "\
  1417. * H1\n<2015-06-21>
  1418. * H1\n<2015-06-23>
  1419. "
  1420. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1421. (org-clone-subtree-with-time-shift 1 "+2d")
  1422. (replace-regexp-in-string
  1423. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1424. nil nil 1))))
  1425. ;; Clone repeating once.
  1426. (should
  1427. (equal "\
  1428. * H1\n<2015-06-21>
  1429. * H1\n<2015-06-23>
  1430. * H1\n<2015-06-25 +1w>
  1431. "
  1432. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1433. (org-clone-subtree-with-time-shift 1 "+2d")
  1434. (replace-regexp-in-string
  1435. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1436. nil nil 1))))
  1437. ;; Clone repeating once in backward.
  1438. (should
  1439. (equal "\
  1440. * H1\n<2015-06-21>
  1441. * H1\n<2015-06-19>
  1442. * H1\n<2015-06-17 +1w>
  1443. "
  1444. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1445. (org-clone-subtree-with-time-shift 1 "-2d")
  1446. (replace-regexp-in-string
  1447. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1448. nil nil 1))))
  1449. ;; Clone non-repeating zero times.
  1450. (should
  1451. (equal "\
  1452. * H1\n<2015-06-21>
  1453. "
  1454. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1455. (org-clone-subtree-with-time-shift 0 "+2d")
  1456. (replace-regexp-in-string
  1457. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1458. nil nil 1))))
  1459. ;; Clone repeating "zero" times.
  1460. (should
  1461. (equal "\
  1462. * H1\n<2015-06-21>
  1463. * H1\n<2015-06-23 +1w>
  1464. "
  1465. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1466. (org-clone-subtree-with-time-shift 0 "+2d")
  1467. (replace-regexp-in-string
  1468. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1469. nil nil 1))))
  1470. ;; Clone with blank SHIFT argument.
  1471. (should
  1472. (string-prefix-p
  1473. "* H <2012-03-29"
  1474. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1475. (org-clone-subtree-with-time-shift 1 "")
  1476. (buffer-substring-no-properties (line-beginning-position 2)
  1477. (line-end-position 2)))))
  1478. ;; Find time stamps before point. If SHIFT is not specified, ask
  1479. ;; for a time shift.
  1480. (should
  1481. (string-prefix-p
  1482. "* H <2012-03-30"
  1483. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1484. (org-clone-subtree-with-time-shift 1 "+1d")
  1485. (buffer-substring-no-properties (line-beginning-position 2)
  1486. (line-end-position 2)))))
  1487. (should
  1488. (string-prefix-p
  1489. "* H <2014-03-05"
  1490. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  1491. (cl-letf (((symbol-function 'read-from-minibuffer)
  1492. (lambda (&rest args) "+1d")))
  1493. (org-clone-subtree-with-time-shift 1))
  1494. (buffer-substring-no-properties (line-beginning-position 2)
  1495. (line-end-position 2))))))
  1496. ;;; Fixed-Width Areas
  1497. (ert-deftest test-org/toggle-fixed-width ()
  1498. "Test `org-toggle-fixed-width' specifications."
  1499. ;; No region: Toggle on fixed-width marker in paragraphs.
  1500. (should
  1501. (equal ": A"
  1502. (org-test-with-temp-text "A"
  1503. (org-toggle-fixed-width)
  1504. (buffer-string))))
  1505. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1506. (should
  1507. (equal "A"
  1508. (org-test-with-temp-text ": A"
  1509. (org-toggle-fixed-width)
  1510. (buffer-string))))
  1511. ;; No region: Toggle on marker in blank lines after elements or just
  1512. ;; after a headline.
  1513. (should
  1514. (equal "* H\n: "
  1515. (org-test-with-temp-text "* H\n"
  1516. (forward-line)
  1517. (org-toggle-fixed-width)
  1518. (buffer-string))))
  1519. (should
  1520. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1521. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1522. (goto-char (point-max))
  1523. (org-toggle-fixed-width)
  1524. (buffer-string))))
  1525. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1526. ;; headlines, clocks)
  1527. (should
  1528. (equal ": * Headline"
  1529. (org-test-with-temp-text "* Headline"
  1530. (org-toggle-fixed-width)
  1531. (buffer-string))))
  1532. (should
  1533. (equal ": #+KEYWORD: value"
  1534. (org-test-with-temp-text "#+KEYWORD: value"
  1535. (org-toggle-fixed-width)
  1536. (buffer-string))))
  1537. ;; No region: error in other situations.
  1538. (should-error
  1539. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1540. (forward-line)
  1541. (org-toggle-fixed-width)
  1542. (buffer-string)))
  1543. ;; No region: Indentation is preserved.
  1544. (should
  1545. (equal "- A\n : B"
  1546. (org-test-with-temp-text "- A\n B"
  1547. (forward-line)
  1548. (org-toggle-fixed-width)
  1549. (buffer-string))))
  1550. ;; Region: If it contains only fixed-width elements and blank lines,
  1551. ;; toggle off fixed-width markup.
  1552. (should
  1553. (equal "A\n\nB"
  1554. (org-test-with-temp-text ": A\n\n: B"
  1555. (transient-mark-mode 1)
  1556. (push-mark (point) t t)
  1557. (goto-char (point-max))
  1558. (org-toggle-fixed-width)
  1559. (buffer-string))))
  1560. ;; Region: If it contains anything else, toggle on fixed-width but
  1561. ;; not on fixed-width areas.
  1562. (should
  1563. (equal ": A\n: \n: B\n: \n: C"
  1564. (org-test-with-temp-text "A\n\n: B\n\nC"
  1565. (transient-mark-mode 1)
  1566. (push-mark (point) t t)
  1567. (goto-char (point-max))
  1568. (org-toggle-fixed-width)
  1569. (buffer-string))))
  1570. ;; Region: Ignore blank lines at its end, unless it contains only
  1571. ;; such lines.
  1572. (should
  1573. (equal ": A\n\n"
  1574. (org-test-with-temp-text "A\n\n"
  1575. (transient-mark-mode 1)
  1576. (push-mark (point) t t)
  1577. (goto-char (point-max))
  1578. (org-toggle-fixed-width)
  1579. (buffer-string))))
  1580. (should
  1581. (equal ": \n: \n"
  1582. (org-test-with-temp-text "\n\n"
  1583. (transient-mark-mode 1)
  1584. (push-mark (point) t t)
  1585. (goto-char (point-max))
  1586. (org-toggle-fixed-width)
  1587. (buffer-string)))))
  1588. (ert-deftest test-org/kill-line ()
  1589. "Test `org-kill-line' specifications."
  1590. ;; At the beginning of a line, kill whole line.
  1591. (should
  1592. (equal ""
  1593. (org-test-with-temp-text "abc"
  1594. (org-kill-line)
  1595. (buffer-string))))
  1596. ;; In the middle of a line, kill line until its end.
  1597. (should
  1598. (equal "a"
  1599. (org-test-with-temp-text "a<point>bc"
  1600. (org-kill-line)
  1601. (buffer-string))))
  1602. ;; Do not kill newline character.
  1603. (should
  1604. (equal "\n123"
  1605. (org-test-with-temp-text "abc\n123"
  1606. (org-kill-line)
  1607. (buffer-string))))
  1608. (should
  1609. (equal "a\n123"
  1610. (org-test-with-temp-text "a<point>bc\n123"
  1611. (org-kill-line)
  1612. (buffer-string))))
  1613. ;; When `org-special-ctrl-k' is non-nil and point is at a headline,
  1614. ;; kill until tags.
  1615. (should
  1616. (equal "* A :tag:"
  1617. (org-test-with-temp-text "* A<point>B :tag:"
  1618. (let ((org-special-ctrl-k t)
  1619. (org-tags-column 0))
  1620. (org-kill-line))
  1621. (buffer-string))))
  1622. ;; If point is on tags, only kill part left until the end of line.
  1623. (should
  1624. (equal "* A :tag:"
  1625. (org-test-with-temp-text "* A :tag:<point>tag2:"
  1626. (let ((org-special-ctrl-k t)
  1627. (org-tags-column 0))
  1628. (org-kill-line))
  1629. (buffer-string))))
  1630. ;; However, if point is at the beginning of the line, kill whole
  1631. ;; headline.
  1632. (should
  1633. (equal ""
  1634. (org-test-with-temp-text "* AB :tag:"
  1635. (let ((org-special-ctrl-k t)
  1636. (org-tags-column 0))
  1637. (org-kill-line))
  1638. (buffer-string))))
  1639. ;; When `org-ctrl-k-protect-subtree' is non-nil, and point is in
  1640. ;; invisible text, ask before removing it. When set to `error',
  1641. ;; throw an error.
  1642. (should-error
  1643. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  1644. (org-overview)
  1645. (let ((org-special-ctrl-k nil)
  1646. (org-ctrl-k-protect-subtree t))
  1647. (cl-letf (((symbol-function 'y-or-n-p) 'ignore))
  1648. (org-kill-line)))))
  1649. (should-error
  1650. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  1651. (org-overview)
  1652. (let ((org-special-ctrl-k nil)
  1653. (org-ctrl-k-protect-subtree 'error))
  1654. (org-kill-line)))))
  1655. ;;; Headline
  1656. (ert-deftest test-org/get-heading ()
  1657. "Test `org-get-heading' specifications."
  1658. ;; Return current heading, even if point is not on it.
  1659. (should
  1660. (equal "H"
  1661. (org-test-with-temp-text "* H"
  1662. (org-get-heading))))
  1663. (should
  1664. (equal "H"
  1665. (org-test-with-temp-text "* H\nText<point>"
  1666. (org-get-heading))))
  1667. ;; Without any optional argument, return TODO keyword, priority
  1668. ;; cookie, COMMENT keyword and tags.
  1669. (should
  1670. (equal "TODO H"
  1671. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1672. (org-get-heading))))
  1673. (should
  1674. (equal "[#A] H"
  1675. (org-test-with-temp-text "* [#A] H"
  1676. (org-get-heading))))
  1677. (should
  1678. (equal "COMMENT H"
  1679. (org-test-with-temp-text "* COMMENT H"
  1680. (org-get-heading))))
  1681. (should
  1682. (equal "H :tag:"
  1683. (org-test-with-temp-text "* H :tag:"
  1684. (org-get-heading))))
  1685. ;; With NO-TAGS argument, ignore tags.
  1686. (should
  1687. (equal "TODO H"
  1688. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1689. (org-get-heading t))))
  1690. (should
  1691. (equal "H"
  1692. (org-test-with-temp-text "* H :tag:"
  1693. (org-get-heading t))))
  1694. ;; With NO-TODO, ignore TODO keyword.
  1695. (should
  1696. (equal "H"
  1697. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1698. (org-get-heading nil t))))
  1699. (should
  1700. (equal "H :tag:"
  1701. (org-test-with-temp-text "* H :tag:"
  1702. (org-get-heading nil t))))
  1703. ;; TODO keywords are case-sensitive.
  1704. (should
  1705. (equal "Todo H"
  1706. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1707. (org-get-heading nil t))))
  1708. ;; With NO-PRIORITY, ignore priority.
  1709. (should
  1710. (equal "H"
  1711. (org-test-with-temp-text "* [#A] H"
  1712. (org-get-heading nil nil t))))
  1713. (should
  1714. (equal "H"
  1715. (org-test-with-temp-text "* H"
  1716. (org-get-heading nil nil t))))
  1717. (should
  1718. (equal "TODO H"
  1719. (org-test-with-temp-text "* TODO [#A] H"
  1720. (org-get-heading nil nil t))))
  1721. ;; With NO-COMMENT, ignore COMMENT keyword.
  1722. (should
  1723. (equal "H"
  1724. (org-test-with-temp-text "* COMMENT H"
  1725. (org-get-heading nil nil nil t))))
  1726. (should
  1727. (equal "H"
  1728. (org-test-with-temp-text "* H"
  1729. (org-get-heading nil nil nil t))))
  1730. (should
  1731. (equal "TODO [#A] H"
  1732. (org-test-with-temp-text "* TODO [#A] COMMENT H"
  1733. (org-get-heading nil nil nil t))))
  1734. ;; On an empty headline, return value is consistent.
  1735. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1736. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1737. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1738. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil t))))
  1739. (should
  1740. (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil nil t)))))
  1741. (ert-deftest test-org/in-commented-heading-p ()
  1742. "Test `org-in-commented-heading-p' specifications."
  1743. ;; Commented headline.
  1744. (should
  1745. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1746. (goto-char (point-max))
  1747. (org-in-commented-heading-p)))
  1748. ;; Commented ancestor.
  1749. (should
  1750. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1751. (goto-char (point-max))
  1752. (org-in-commented-heading-p)))
  1753. ;; Comment keyword is case-sensitive.
  1754. (should-not
  1755. (org-test-with-temp-text "* Comment Headline\nBody"
  1756. (goto-char (point-max))
  1757. (org-in-commented-heading-p)))
  1758. ;; Keyword is standalone.
  1759. (should-not
  1760. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1761. (goto-char (point-max))
  1762. (org-in-commented-heading-p)))
  1763. ;; Optional argument.
  1764. (should-not
  1765. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1766. (goto-char (point-max))
  1767. (org-in-commented-heading-p t))))
  1768. (ert-deftest test-org/entry-blocked-p ()
  1769. ;; Check other dependencies.
  1770. (should
  1771. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1772. (let ((org-enforce-todo-dependencies t)
  1773. (org-blocker-hook
  1774. '(org-block-todo-from-children-or-siblings-or-parent)))
  1775. (org-entry-blocked-p))))
  1776. (should-not
  1777. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1778. (let ((org-enforce-todo-dependencies t)
  1779. (org-blocker-hook
  1780. '(org-block-todo-from-children-or-siblings-or-parent)))
  1781. (org-entry-blocked-p))))
  1782. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1783. ;; blocked.
  1784. (should-not
  1785. (org-test-with-temp-text "* Blocked\n** TODO one"
  1786. (let ((org-enforce-todo-dependencies t)
  1787. (org-blocker-hook
  1788. '(org-block-todo-from-children-or-siblings-or-parent)))
  1789. (org-entry-blocked-p))))
  1790. (should-not
  1791. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1792. (let ((org-enforce-todo-dependencies t)
  1793. (org-blocker-hook
  1794. '(org-block-todo-from-children-or-siblings-or-parent)))
  1795. (org-entry-blocked-p))))
  1796. ;; Follow :ORDERED: specifications.
  1797. (should
  1798. (org-test-with-temp-text
  1799. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1800. (let ((org-enforce-todo-dependencies t)
  1801. (org-blocker-hook
  1802. '(org-block-todo-from-children-or-siblings-or-parent)))
  1803. (org-entry-blocked-p))))
  1804. (should-not
  1805. (org-test-with-temp-text
  1806. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1807. (let ((org-enforce-todo-dependencies t)
  1808. (org-blocker-hook
  1809. '(org-block-todo-from-children-or-siblings-or-parent)))
  1810. (org-entry-blocked-p)))))
  1811. (ert-deftest test-org/get-outline-path ()
  1812. "Test `org-get-outline-path' specifications."
  1813. ;; Top-level headlines have no outline path.
  1814. (should-not
  1815. (org-test-with-temp-text "* H"
  1816. (org-get-outline-path)))
  1817. ;; Otherwise, outline path is the path leading to the headline.
  1818. (should
  1819. (equal '("H")
  1820. (org-test-with-temp-text "* H\n** S<point>"
  1821. (org-get-outline-path))))
  1822. ;; Find path even when point is not on a headline.
  1823. (should
  1824. (equal '("H")
  1825. (org-test-with-temp-text "* H\n** S\nText<point>"
  1826. (org-get-outline-path))))
  1827. ;; TODO keywords, tags and statistics cookies are ignored.
  1828. (should
  1829. (equal '("H")
  1830. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1831. (org-get-outline-path))))
  1832. ;; Links are replaced with their description or their path.
  1833. (should
  1834. (equal '("Org")
  1835. (org-test-with-temp-text
  1836. "* [[https://orgmode.org][Org]]\n** S<point>"
  1837. (org-get-outline-path))))
  1838. (should
  1839. (equal '("https://orgmode.org")
  1840. (org-test-with-temp-text
  1841. "* [[https://orgmode.org]]\n** S<point>"
  1842. (org-get-outline-path))))
  1843. ;; When WITH-SELF is non-nil, include current heading.
  1844. (should
  1845. (equal '("H")
  1846. (org-test-with-temp-text "* H"
  1847. (org-get-outline-path t))))
  1848. (should
  1849. (equal '("H" "S")
  1850. (org-test-with-temp-text "* H\n** S\nText<point>"
  1851. (org-get-outline-path t))))
  1852. ;; Using cache is transparent to the user.
  1853. (should
  1854. (equal '("H")
  1855. (org-test-with-temp-text "* H\n** S<point>"
  1856. (setq org-outline-path-cache nil)
  1857. (org-get-outline-path nil t))))
  1858. ;; Do not corrupt cache when finding outline path in distant part of
  1859. ;; the buffer.
  1860. (should
  1861. (equal '("H2")
  1862. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1863. (setq org-outline-path-cache nil)
  1864. (org-get-outline-path nil t)
  1865. (search-forward "S2")
  1866. (org-get-outline-path nil t))))
  1867. ;; Do not choke on empty headlines.
  1868. (should
  1869. (org-test-with-temp-text "* H\n** <point>"
  1870. (org-get-outline-path)))
  1871. (should
  1872. (org-test-with-temp-text "* \n** H<point>"
  1873. (org-get-outline-path))))
  1874. (ert-deftest test-org/format-outline-path ()
  1875. "Test `org-format-outline-path' specifications."
  1876. (should
  1877. (string= (org-format-outline-path (list "one" "two" "three"))
  1878. "one/two/three"))
  1879. ;; Empty path.
  1880. (should
  1881. (string= (org-format-outline-path '())
  1882. ""))
  1883. (should
  1884. (string= (org-format-outline-path '(nil))
  1885. ""))
  1886. ;; Empty path and prefix.
  1887. (should
  1888. (string= (org-format-outline-path '() nil ">>")
  1889. ">>"))
  1890. ;; Trailing whitespace in headings.
  1891. (should
  1892. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1893. "one/tw o/three"))
  1894. ;; Non-default prefix and separators.
  1895. (should
  1896. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1897. ">>|one|two|three"))
  1898. ;; Truncate.
  1899. (should
  1900. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1901. "one/two/.."))
  1902. ;; Give a very narrow width.
  1903. (should
  1904. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1905. "on"))
  1906. ;; Give a prefix that extends beyond the width.
  1907. (should
  1908. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1909. ">>>>>>>>>>")
  1910. ">>>>>>>>..")))
  1911. (ert-deftest test-org/map-entries ()
  1912. "Test `org-map-entries' specifications."
  1913. ;; Full match.
  1914. (should
  1915. (equal '(1 11)
  1916. (org-test-with-temp-text "* Level 1\n** Level 2"
  1917. (org-map-entries #'point))))
  1918. ;; Level match.
  1919. (should
  1920. (equal '(1)
  1921. (org-test-with-temp-text "* Level 1\n** Level 2"
  1922. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1923. (should
  1924. (equal '(11)
  1925. (org-test-with-temp-text "* Level 1\n** Level 2"
  1926. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1927. ;; Tag match.
  1928. (should
  1929. (equal '(11)
  1930. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1931. (org-map-entries #'point "yes"))))
  1932. (should
  1933. (equal '(14)
  1934. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1935. (org-map-entries #'point "+yes-a"))))
  1936. (should
  1937. (equal '(11 23)
  1938. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1939. (org-map-entries #'point "{yes?}"))))
  1940. ;; Priority match.
  1941. (should
  1942. (equal '(1)
  1943. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1944. (org-map-entries #'point "PRIORITY=\"A\""))))
  1945. ;; Date match.
  1946. (should
  1947. (equal '(36)
  1948. (org-test-with-temp-text "
  1949. * H1
  1950. SCHEDULED: <2012-03-29 thu.>
  1951. * H2
  1952. SCHEDULED: <2014-03-04 tue.>"
  1953. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1954. (should
  1955. (equal '(2)
  1956. (org-test-with-temp-text "
  1957. * H1
  1958. SCHEDULED: <2012-03-29 thu.>
  1959. * H2
  1960. SCHEDULED: <2014-03-04 tue.>"
  1961. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1962. ;; Regular property match.
  1963. (should
  1964. (equal '(2)
  1965. (org-test-with-temp-text "
  1966. * H1
  1967. :PROPERTIES:
  1968. :TEST: 1
  1969. :END:
  1970. * H2
  1971. :PROPERTIES:
  1972. :TEST: 2
  1973. :END:"
  1974. (org-map-entries #'point "TEST=1"))))
  1975. ;; Multiple criteria.
  1976. (should
  1977. (equal '(23)
  1978. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1979. (let (org-odd-levels-only
  1980. (org-use-tag-inheritance nil))
  1981. (org-map-entries #'point "yes+LEVEL=1")))))
  1982. ;; "or" criteria.
  1983. (should
  1984. (equal '(12 24)
  1985. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1986. (let (org-odd-levels-only)
  1987. (org-map-entries #'point "LEVEL=2|no")))))
  1988. (should
  1989. (equal '(1 12)
  1990. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  1991. (let (org-odd-levels-only)
  1992. (org-map-entries #'point "yes|no")))))
  1993. ;; "and" criteria.
  1994. (should
  1995. (equal '(22)
  1996. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  1997. (let (org-odd-levels-only)
  1998. (org-map-entries #'point "yes&no"))))))
  1999. (ert-deftest test-org/edit-headline ()
  2000. "Test `org-edit-headline' specifications."
  2001. (should
  2002. (equal "* B"
  2003. (org-test-with-temp-text "* A"
  2004. (org-edit-headline "B")
  2005. (buffer-string))))
  2006. ;; Handle empty headings.
  2007. (should
  2008. (equal "* "
  2009. (org-test-with-temp-text "* A"
  2010. (org-edit-headline "")
  2011. (buffer-string))))
  2012. (should
  2013. (equal "* A"
  2014. (org-test-with-temp-text "* "
  2015. (org-edit-headline "A")
  2016. (buffer-string))))
  2017. ;; Handle TODO keywords and priority cookies.
  2018. (should
  2019. (equal "* TODO B"
  2020. (org-test-with-temp-text "* TODO A"
  2021. (org-edit-headline "B")
  2022. (buffer-string))))
  2023. (should
  2024. (equal "* [#A] B"
  2025. (org-test-with-temp-text "* [#A] A"
  2026. (org-edit-headline "B")
  2027. (buffer-string))))
  2028. (should
  2029. (equal "* TODO [#A] B"
  2030. (org-test-with-temp-text "* TODO [#A] A"
  2031. (org-edit-headline "B")
  2032. (buffer-string))))
  2033. ;; Handle tags.
  2034. (equal "* B :tag:"
  2035. (org-test-with-temp-text "* A :tag:"
  2036. (let ((org-tags-column 4)) (org-edit-headline "B"))
  2037. (buffer-string))))
  2038. ;;; Keywords
  2039. (ert-deftest test-org/set-regexps-and-options ()
  2040. "Test `org-set-regexps-and-options' specifications."
  2041. ;; TAGS keyword.
  2042. (should
  2043. (equal '(("A"))
  2044. (let ((org-tag-alist '(("A")))
  2045. (org-tag-persistent-alist nil))
  2046. (org-test-with-temp-text ""
  2047. (org-mode-restart)
  2048. org-current-tag-alist))))
  2049. (should
  2050. (equal '(("B"))
  2051. (let ((org-tag-alist '(("A")))
  2052. (org-tag-persistent-alist nil))
  2053. (org-test-with-temp-text "#+TAGS: B"
  2054. (org-mode-restart)
  2055. org-current-tag-alist))))
  2056. (should
  2057. (equal '(("C") ("B"))
  2058. (let ((org-tag-alist '(("A")))
  2059. (org-tag-persistent-alist '(("C"))))
  2060. (org-test-with-temp-text "#+TAGS: B"
  2061. (org-mode-restart)
  2062. org-current-tag-alist))))
  2063. (should
  2064. (equal '(("B"))
  2065. (let ((org-tag-alist '(("A")))
  2066. (org-tag-persistent-alist '(("C"))))
  2067. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  2068. (org-mode-restart)
  2069. org-current-tag-alist))))
  2070. (should
  2071. (equal '(("A" . ?a) ("B") ("C"))
  2072. (let ((org-tag-persistant-alist nil))
  2073. (org-test-with-temp-text "#+TAGS: A(a) B C"
  2074. (org-mode-restart)
  2075. org-current-tag-alist))))
  2076. (should
  2077. (equal '(("A") (:newline) ("B"))
  2078. (let ((org-tag-persistent-alist nil))
  2079. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  2080. (org-mode-restart)
  2081. org-current-tag-alist))))
  2082. (should
  2083. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  2084. (let ((org-tag-persistent-alist nil))
  2085. (org-test-with-temp-text "#+TAGS: { A B } C"
  2086. (org-mode-restart)
  2087. org-current-tag-alist))))
  2088. (should
  2089. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  2090. (let ((org-tag-persistent-alist nil))
  2091. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2092. (org-mode-restart)
  2093. org-current-tag-alist))))
  2094. (should
  2095. (equal '(("A" "B" "C"))
  2096. (let ((org-tag-persistent-alist nil))
  2097. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2098. (org-mode-restart)
  2099. org-tag-groups-alist))))
  2100. (should
  2101. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  2102. (let ((org-tag-persistent-alist nil))
  2103. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2104. (org-mode-restart)
  2105. org-current-tag-alist))))
  2106. (should
  2107. (equal '(("A" "B" "C"))
  2108. (let ((org-tag-persistent-alist nil))
  2109. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2110. (org-mode-restart)
  2111. org-tag-groups-alist))))
  2112. ;; FILETAGS keyword.
  2113. (should
  2114. (equal '("A" "B" "C")
  2115. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  2116. (org-mode-restart)
  2117. org-file-tags)))
  2118. ;; PROPERTY keyword. Property names are case-insensitive.
  2119. (should
  2120. (equal "foo=1"
  2121. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  2122. (org-mode-restart)
  2123. (cdr (assoc "var" org-file-properties)))))
  2124. (should
  2125. (equal
  2126. "foo=1 bar=2"
  2127. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  2128. (org-mode-restart)
  2129. (cdr (assoc "var" org-file-properties)))))
  2130. (should
  2131. (equal
  2132. "foo=1 bar=2"
  2133. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  2134. (org-mode-restart)
  2135. (cdr (assoc "var" org-file-properties)))))
  2136. ;; ARCHIVE keyword.
  2137. (should
  2138. (equal "%s_done::"
  2139. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  2140. (org-mode-restart)
  2141. org-archive-location)))
  2142. ;; CATEGORY keyword.
  2143. (should
  2144. (eq 'test
  2145. (org-test-with-temp-text "#+CATEGORY: test"
  2146. (org-mode-restart)
  2147. org-category)))
  2148. (should
  2149. (equal "test"
  2150. (org-test-with-temp-text "#+CATEGORY: test"
  2151. (org-mode-restart)
  2152. (cdr (assoc "CATEGORY" org-file-properties)))))
  2153. ;; COLUMNS keyword.
  2154. (should
  2155. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  2156. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  2157. (org-mode-restart)
  2158. org-columns-default-format)))
  2159. ;; CONSTANTS keyword. Constants names are case sensitive.
  2160. (should
  2161. (equal '("299792458." "3.14")
  2162. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  2163. (org-mode-restart)
  2164. (mapcar
  2165. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  2166. '("c" "pi")))))
  2167. (should
  2168. (equal "3.14"
  2169. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  2170. (org-mode-restart)
  2171. (cdr (assoc "pi" org-table-formula-constants-local)))))
  2172. (should
  2173. (equal "22/7"
  2174. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  2175. (org-mode-restart)
  2176. (cdr (assoc "PI" org-table-formula-constants-local)))))
  2177. ;; LINK keyword.
  2178. (should
  2179. (equal
  2180. '("url1" "url2")
  2181. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  2182. (org-mode-restart)
  2183. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  2184. '("a" "b")))))
  2185. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  2186. (should
  2187. (equal
  2188. '(?X ?Z ?Y)
  2189. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  2190. (org-mode-restart)
  2191. (list org-highest-priority org-lowest-priority org-default-priority))))
  2192. (should
  2193. (equal
  2194. '(?A ?C ?B)
  2195. (org-test-with-temp-text "#+PRIORITIES: X Z"
  2196. (org-mode-restart)
  2197. (list org-highest-priority org-lowest-priority org-default-priority))))
  2198. ;; STARTUP keyword.
  2199. (should
  2200. (equal '(t t)
  2201. (org-test-with-temp-text "#+STARTUP: fold odd"
  2202. (org-mode-restart)
  2203. (list org-startup-folded org-odd-levels-only))))
  2204. ;; TODO keywords.
  2205. (should
  2206. (equal '(("A" "B") ("C"))
  2207. (org-test-with-temp-text "#+TODO: A B | C"
  2208. (org-mode-restart)
  2209. (list org-not-done-keywords org-done-keywords))))
  2210. (should
  2211. (equal '(("A" "C") ("B" "D"))
  2212. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  2213. (org-mode-restart)
  2214. (list org-not-done-keywords org-done-keywords))))
  2215. (should
  2216. (equal '(("A" "B") ("C"))
  2217. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  2218. (org-mode-restart)
  2219. (list org-not-done-keywords org-done-keywords))))
  2220. (should
  2221. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  2222. (org-test-with-temp-text "#+TODO: A(a)"
  2223. (org-mode-restart)
  2224. org-todo-key-alist)))
  2225. (should
  2226. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  2227. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  2228. (org-mode-restart)
  2229. org-todo-log-states)))
  2230. ;; Enter SETUPFILE keyword.
  2231. (should
  2232. (equal "1"
  2233. (org-test-with-temp-text
  2234. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  2235. (org-mode-restart)
  2236. (cdr (assoc "a" org-file-properties))))))
  2237. ;;; Links
  2238. ;;;; Coderefs
  2239. (ert-deftest test-org/coderef ()
  2240. "Test coderef links specifications."
  2241. (should
  2242. (org-test-with-temp-text "
  2243. #+BEGIN_SRC emacs-lisp
  2244. \(+ 1 1) (ref:sc)
  2245. #+END_SRC
  2246. \[[(sc)<point>]]"
  2247. (org-open-at-point)
  2248. (looking-at "(ref:sc)")))
  2249. ;; Find coderef even with alternate label format.
  2250. (should
  2251. (org-test-with-temp-text "
  2252. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2253. \(+ 1 1) {ref:sc}
  2254. #+END_SRC
  2255. \[[(sc)<point>]]"
  2256. (org-open-at-point)
  2257. (looking-at "{ref:sc}"))))
  2258. ;;;; Custom ID
  2259. (ert-deftest test-org/custom-id ()
  2260. "Test custom ID links specifications."
  2261. (should
  2262. (org-test-with-temp-text
  2263. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2264. (org-open-at-point)
  2265. (looking-at-p "\\* H1")))
  2266. ;; Handle escape characters.
  2267. (should
  2268. (org-test-with-temp-text
  2269. "* H1\n:PROPERTIES:\n:CUSTOM_ID: [%]\n:END:\n* H2\n[[#%5B%25%5D<point>]]"
  2270. (org-open-at-point)
  2271. (looking-at-p "\\* H1")))
  2272. ;; Throw an error on false positives.
  2273. (should-error
  2274. (org-test-with-temp-text
  2275. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2276. (org-open-at-point)
  2277. (looking-at-p "\\* H1"))))
  2278. ;;;; Fuzzy Links
  2279. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2280. ;; a named element (#+name: text) and to headlines (* Text).
  2281. (ert-deftest test-org/fuzzy-links ()
  2282. "Test fuzzy links specifications."
  2283. ;; Fuzzy link goes in priority to a matching target.
  2284. (should
  2285. (org-test-with-temp-text
  2286. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2287. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2288. (looking-at "<<Test>>")))
  2289. ;; Then fuzzy link points to an element with a given name.
  2290. (should
  2291. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2292. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2293. (looking-at "#\\+NAME: Test")))
  2294. ;; A target still lead to a matching headline otherwise.
  2295. (should
  2296. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2297. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2298. (looking-at "\\* Head2")))
  2299. ;; With a leading star in link, enforce heading match.
  2300. (should
  2301. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2302. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2303. (looking-at "\\* Test")))
  2304. ;; With a leading star in link, enforce exact heading match, even
  2305. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2306. (should-error
  2307. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2308. (let ((org-link-search-must-match-exact-headline nil))
  2309. (org-open-at-point))))
  2310. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2311. (should
  2312. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2313. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2314. (looking-at "\\* Test")))
  2315. (should
  2316. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2317. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2318. (looking-at "\\* Test")))
  2319. ;; Heading match should not care about spaces, cookies, TODO
  2320. ;; keywords, priorities, and tags. However, TODO keywords are
  2321. ;; case-sensitive.
  2322. (should
  2323. (let ((first-line
  2324. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2325. (org-test-with-temp-text
  2326. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2327. (let ((org-link-search-must-match-exact-headline nil)
  2328. (org-todo-regexp "TODO"))
  2329. (org-open-at-point))
  2330. (looking-at (regexp-quote first-line)))))
  2331. (should-error
  2332. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2333. (let ((org-link-search-must-match-exact-headline nil)
  2334. (org-todo-regexp "TODO"))
  2335. (org-open-at-point))))
  2336. ;; Heading match should still be exact.
  2337. (should-error
  2338. (org-test-with-temp-text "
  2339. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2340. Foo Bar
  2341. <point>[[*Test 1]]"
  2342. (let ((org-link-search-must-match-exact-headline nil)
  2343. (org-todo-regexp "TODO"))
  2344. (org-open-at-point))))
  2345. (should
  2346. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2347. (let ((org-link-search-must-match-exact-headline nil)
  2348. (org-todo-regexp "TODO"))
  2349. (org-open-at-point))
  2350. (looking-at-p (regexp-quote "** Test 1 2"))))
  2351. ;; Heading match ignores COMMENT keyword.
  2352. (should
  2353. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2354. (org-open-at-point)
  2355. (looking-at "\\* COMMENT Test")))
  2356. (should
  2357. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2358. (org-open-at-point)
  2359. (looking-at "\\* TODO COMMENT Test")))
  2360. ;; Correctly un-hexify fuzzy links.
  2361. (should
  2362. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2363. (org-open-at-point)
  2364. (bobp)))
  2365. (should
  2366. (org-test-with-temp-text "* [1]\n[[*%5B1%5D<point>]]"
  2367. (org-open-at-point)
  2368. (bobp)))
  2369. ;; Match search strings containing newline characters, including
  2370. ;; blank lines.
  2371. (should
  2372. (org-test-with-temp-text-in-file "Paragraph\n\nline1\nline2\n\n"
  2373. (let ((file (buffer-file-name)))
  2374. (goto-char (point-max))
  2375. (insert (format "[[file:%s::line1 line2]]" file))
  2376. (beginning-of-line)
  2377. (let ((org-link-search-must-match-exact-headline nil))
  2378. (org-open-at-point 0))
  2379. (looking-at-p "line1"))))
  2380. (should
  2381. (org-test-with-temp-text-in-file "Paragraph\n\nline1\n\nline2\n\n"
  2382. (let ((file (buffer-file-name)))
  2383. (goto-char (point-max))
  2384. (insert (format "[[file:%s::line1 line2]]" file))
  2385. (beginning-of-line)
  2386. (let ((org-link-search-must-match-exact-headline nil))
  2387. (org-open-at-point 0))
  2388. (looking-at-p "line1")))))
  2389. ;;;; Link Escaping
  2390. (ert-deftest test-org/org-link-escape-ascii-character ()
  2391. "Escape an ascii character."
  2392. (should
  2393. (string=
  2394. "%5B"
  2395. (org-link-escape "["))))
  2396. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2397. "Escape an ascii control character."
  2398. (should
  2399. (string=
  2400. "%09"
  2401. (org-link-escape "\t"))))
  2402. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2403. "Escape an unicode multibyte character."
  2404. (should
  2405. (string=
  2406. "%E2%82%AC"
  2407. (org-link-escape "€"))))
  2408. (ert-deftest test-org/org-link-escape-custom-table ()
  2409. "Escape string with custom character table."
  2410. (should
  2411. (string=
  2412. "Foo%3A%42ar%0A"
  2413. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2414. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2415. "Escape string with custom table merged with default table."
  2416. (should
  2417. (string=
  2418. "%5BF%6F%6F%3A%42ar%0A%5D"
  2419. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2420. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2421. "Unescape an ascii character."
  2422. (should
  2423. (string=
  2424. "["
  2425. (org-link-unescape "%5B"))))
  2426. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2427. "Unescpae an ascii control character."
  2428. (should
  2429. (string=
  2430. "\n"
  2431. (org-link-unescape "%0A"))))
  2432. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2433. "Unescape unicode multibyte character."
  2434. (should
  2435. (string=
  2436. "€"
  2437. (org-link-unescape "%E2%82%AC"))))
  2438. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2439. "Unescape old style percent escaped character."
  2440. (should
  2441. (string=
  2442. "àâçèéêîôùû"
  2443. (decode-coding-string
  2444. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2445. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2446. "Escape and unescape a URL that includes an escaped char.
  2447. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2448. (should
  2449. (string=
  2450. "http://some.host.com/form?&id=blah%2Bblah25"
  2451. (org-link-unescape
  2452. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2453. ;;;; Open at point
  2454. (ert-deftest test-org/open-at-point/keyword ()
  2455. "Does `org-open-at-point' open link in a keyword line?"
  2456. (should
  2457. (org-test-with-temp-text
  2458. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2459. (org-open-at-point) t))
  2460. (should
  2461. (org-test-with-temp-text
  2462. "* H\n<<top>>\n#+KEYWORD: <point>[[top]]"
  2463. (org-open-at-point) t)))
  2464. (ert-deftest test-org/open-at-point/property ()
  2465. "Does `org-open-at-point' open link in property drawer?"
  2466. (should
  2467. (org-test-with-temp-text
  2468. "* Headline
  2469. :PROPERTIES:
  2470. :URL: <point>[[*Headline]]
  2471. :END:"
  2472. (org-open-at-point) t)))
  2473. (ert-deftest test-org/open-at-point/comment ()
  2474. "Does `org-open-at-point' open link in a commented line?"
  2475. (should
  2476. (org-test-with-temp-text
  2477. "<<top>>\n# <point>[[top]]"
  2478. (org-open-at-point) t))
  2479. (should
  2480. (org-test-with-temp-text
  2481. "* H\n<<top>>\n# <point>[[top]]"
  2482. (org-open-at-point) t)))
  2483. (ert-deftest test-org/open-at-point/inline-image ()
  2484. "Test `org-open-at-point' on nested links."
  2485. (should
  2486. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2487. (org-open-at-point)
  2488. (bobp))))
  2489. (ert-deftest test-org/open-at-point/radio-target ()
  2490. "Test `org-open-at-point' on radio targets."
  2491. (should
  2492. (org-test-with-temp-text "<<<target>>> <point>target"
  2493. (org-update-radio-target-regexp)
  2494. (org-open-at-point)
  2495. (eq (org-element-type (org-element-context)) 'radio-target))))
  2496. (ert-deftest test-org/open-at-point/tag ()
  2497. "Test `org-open-at-point' on tags."
  2498. (should
  2499. (org-test-with-temp-text "* H :<point>tag:"
  2500. (catch :result
  2501. (cl-letf (((symbol-function 'org-tags-view)
  2502. (lambda (&rest args) (throw :result t))))
  2503. (org-open-at-point)
  2504. nil))))
  2505. (should-not
  2506. (org-test-with-temp-text-in-file "* H<point> :tag:"
  2507. (catch :result
  2508. (cl-letf (((symbol-function 'org-tags-view)
  2509. (lambda (&rest args) (throw :result t))))
  2510. (org-open-at-point)
  2511. nil)))))
  2512. ;;;; Stored links
  2513. (ert-deftest test-org/store-link ()
  2514. "Test `org-store-link' specifications."
  2515. ;; On a headline, link to that headline. Use heading as the
  2516. ;; description of the link.
  2517. (should
  2518. (let (org-store-link-props org-stored-links)
  2519. (org-test-with-temp-text-in-file "* H1"
  2520. (let ((file (buffer-file-name)))
  2521. (equal (format "[[file:%s::*H1][H1]]" file)
  2522. (org-store-link nil))))))
  2523. ;; On a headline, remove any link from description.
  2524. (should
  2525. (let (org-store-link-props org-stored-links)
  2526. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2527. (let ((file (buffer-file-name)))
  2528. (equal (format "[[file:%s::*%s][d]]"
  2529. file
  2530. (org-link-escape "[[#l][d]]"))
  2531. (org-store-link nil))))))
  2532. (should
  2533. (let (org-store-link-props org-stored-links)
  2534. (org-test-with-temp-text-in-file "* [[l]]"
  2535. (let ((file (buffer-file-name)))
  2536. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2537. (org-store-link nil))))))
  2538. (should
  2539. (let (org-store-link-props org-stored-links)
  2540. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2541. (let ((file (buffer-file-name)))
  2542. (equal (format "[[file:%s::*%s][d1 d2]]"
  2543. file
  2544. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2545. (org-store-link nil))))))
  2546. ;; On a named element, link to that element.
  2547. (should
  2548. (let (org-store-link-props org-stored-links)
  2549. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2550. (let ((file (buffer-file-name)))
  2551. (equal (format "[[file:%s::foo][foo]]" file)
  2552. (org-store-link nil))))))
  2553. ;; Store link to Org buffer, with context.
  2554. (should
  2555. (let ((org-stored-links nil)
  2556. (org-id-link-to-org-use-id nil)
  2557. (org-context-in-file-links t))
  2558. (org-test-with-temp-text-in-file "* h1"
  2559. (let ((file (buffer-file-name)))
  2560. (equal (format "[[file:%s::*h1][h1]]" file)
  2561. (org-store-link nil))))))
  2562. ;; Store link to Org buffer, without context.
  2563. (should
  2564. (let ((org-stored-links nil)
  2565. (org-id-link-to-org-use-id nil)
  2566. (org-context-in-file-links nil))
  2567. (org-test-with-temp-text-in-file "* h1"
  2568. (let ((file (buffer-file-name)))
  2569. (equal (format "[[file:%s][file:%s]]" file file)
  2570. (org-store-link nil))))))
  2571. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  2572. (should
  2573. (let ((org-stored-links nil)
  2574. (org-id-link-to-org-use-id nil)
  2575. (org-context-in-file-links nil))
  2576. (org-test-with-temp-text-in-file "* h1"
  2577. (let ((file (buffer-file-name)))
  2578. (equal (format "[[file:%s::*h1][h1]]" file)
  2579. (org-store-link '(4)))))))
  2580. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  2581. ;; buffer.
  2582. (should
  2583. (let ((org-stored-links nil)
  2584. (org-id-link-to-org-use-id nil)
  2585. (org-context-in-file-links nil))
  2586. (org-test-with-temp-text-in-file "* h1"
  2587. (let ((file (buffer-file-name)))
  2588. (equal (format "[[file:%s][file:%s]]" file file)
  2589. (org-store-link '(16)))))))
  2590. ;; Store file link to non-Org buffer, with context.
  2591. (should
  2592. (let ((org-stored-links nil)
  2593. (org-context-in-file-links t))
  2594. (org-test-with-temp-text-in-file "one\n<point>two"
  2595. (fundamental-mode)
  2596. (let ((file (buffer-file-name)))
  2597. (equal (format "[[file:%s::one]]" file)
  2598. (org-store-link nil))))))
  2599. ;; Store file link to non-Org buffer, without context.
  2600. (should
  2601. (let ((org-stored-links nil)
  2602. (org-context-in-file-links nil))
  2603. (org-test-with-temp-text-in-file "one\n<point>two"
  2604. (fundamental-mode)
  2605. (let ((file (buffer-file-name)))
  2606. (equal (format "[[file:%s][file:%s]]" file file)
  2607. (org-store-link nil))))))
  2608. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  2609. ;; buffer.
  2610. (should
  2611. (let ((org-stored-links nil)
  2612. (org-context-in-file-links nil))
  2613. (org-test-with-temp-text-in-file "one\n<point>two"
  2614. (fundamental-mode)
  2615. (let ((file (buffer-file-name)))
  2616. (equal (format "[[file:%s::one]]" file)
  2617. (org-store-link '(4)))))))
  2618. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  2619. ;; non-Org buffer.
  2620. (should
  2621. (let ((org-stored-links nil)
  2622. (org-context-in-file-links nil))
  2623. (org-test-with-temp-text-in-file "one\n<point>two"
  2624. (fundamental-mode)
  2625. (let ((file (buffer-file-name)))
  2626. (equal (format "[[file:%s][file:%s]]" file file)
  2627. (org-store-link '(16))))))))
  2628. ;;; Node Properties
  2629. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2630. "Ensure properties accumulate in subtree drawers."
  2631. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2632. (org-babel-next-src-block)
  2633. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2634. (ert-deftest test-org/custom-properties ()
  2635. "Test custom properties specifications."
  2636. ;; Standard test.
  2637. (should
  2638. (let ((org-custom-properties '("FOO")))
  2639. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2640. (org-toggle-custom-properties-visibility)
  2641. (org-invisible-p2))))
  2642. ;; Properties are case-insensitive.
  2643. (should
  2644. (let ((org-custom-properties '("FOO")))
  2645. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2646. (org-toggle-custom-properties-visibility)
  2647. (org-invisible-p2))))
  2648. (should
  2649. (let ((org-custom-properties '("foo")))
  2650. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2651. (org-toggle-custom-properties-visibility)
  2652. (org-invisible-p2))))
  2653. ;; Multiple custom properties in the same drawer.
  2654. (should
  2655. (let ((org-custom-properties '("FOO" "BAR")))
  2656. (org-test-with-temp-text
  2657. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2658. (org-toggle-custom-properties-visibility)
  2659. (and (org-invisible-p2)
  2660. (not (progn (forward-line) (org-invisible-p2)))
  2661. (progn (forward-line) (org-invisible-p2))))))
  2662. ;; Hide custom properties with an empty value.
  2663. (should
  2664. (let ((org-custom-properties '("FOO")))
  2665. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2666. (org-toggle-custom-properties-visibility)
  2667. (org-invisible-p2))))
  2668. ;; Do not hide fake properties.
  2669. (should-not
  2670. (let ((org-custom-properties '("FOO")))
  2671. (org-test-with-temp-text ":FOO: val\n"
  2672. (org-toggle-custom-properties-visibility)
  2673. (org-invisible-p2))))
  2674. (should-not
  2675. (let ((org-custom-properties '("A")))
  2676. (org-test-with-temp-text
  2677. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2678. (org-toggle-custom-properties-visibility)
  2679. (org-invisible-p2)))))
  2680. ;;; Mark Region
  2681. (ert-deftest test-org/mark-subtree ()
  2682. "Test `org-mark-subtree' specifications."
  2683. ;; Error when point is before first headline.
  2684. (should-error
  2685. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2686. (progn (transient-mark-mode 1)
  2687. (org-mark-subtree))))
  2688. ;; Without argument, mark current subtree.
  2689. (should
  2690. (equal
  2691. '(12 32)
  2692. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2693. (progn (transient-mark-mode 1)
  2694. (forward-line 2)
  2695. (org-mark-subtree)
  2696. (list (region-beginning) (region-end))))))
  2697. ;; With an argument, move ARG up.
  2698. (should
  2699. (equal
  2700. '(1 32)
  2701. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2702. (progn (transient-mark-mode 1)
  2703. (forward-line 2)
  2704. (org-mark-subtree 1)
  2705. (list (region-beginning) (region-end))))))
  2706. ;; Do not get fooled by inlinetasks.
  2707. (when (featurep 'org-inlinetask)
  2708. (should
  2709. (= 1
  2710. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2711. (progn (transient-mark-mode 1)
  2712. (forward-line 1)
  2713. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2714. (region-beginning)))))))
  2715. ;;; Miscellaneous
  2716. (ert-deftest test-org/sort-entries ()
  2717. "Test `org-sort-entries'."
  2718. ;; Sort alphabetically.
  2719. (should
  2720. (equal "\n* abc\n* def\n* xyz\n"
  2721. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2722. (org-sort-entries nil ?a)
  2723. (buffer-string))))
  2724. (should
  2725. (equal "\n* xyz\n* def\n* abc\n"
  2726. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2727. (org-sort-entries nil ?A)
  2728. (buffer-string))))
  2729. (should
  2730. (equal "\n* \n* klm\n* xyz\n"
  2731. (org-test-with-temp-text "\n* xyz\n* \n* klm\n"
  2732. (org-sort-entries nil ?a)
  2733. (buffer-string))))
  2734. ;; Sort numerically.
  2735. (should
  2736. (equal "\n* 1\n* 2\n* 10\n"
  2737. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2738. (org-sort-entries nil ?n)
  2739. (buffer-string))))
  2740. (should
  2741. (equal "\n* 10\n* 2\n* 1\n"
  2742. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2743. (org-sort-entries nil ?N)
  2744. (buffer-string))))
  2745. (should
  2746. (equal "\n* \n* 1\n* 2\n"
  2747. (org-test-with-temp-text "\n* 1\n* \n* 2\n"
  2748. (org-sort-entries nil ?n)
  2749. (buffer-string))))
  2750. ;; Sort by custom function.
  2751. (should
  2752. (equal "\n* b\n* aa\n* ccc\n"
  2753. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2754. (org-sort-entries nil ?f
  2755. (lambda ()
  2756. (length (buffer-substring (point-at-bol)
  2757. (point-at-eol))))
  2758. #'<)
  2759. (buffer-string))))
  2760. (should
  2761. (equal "\n* ccc\n* aa\n* b\n"
  2762. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2763. (org-sort-entries nil ?F
  2764. (lambda ()
  2765. (length (buffer-substring (point-at-bol)
  2766. (point-at-eol))))
  2767. #'<)
  2768. (buffer-string))))
  2769. ;; Sort by TODO keyword.
  2770. (should
  2771. (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
  2772. (org-test-with-temp-text
  2773. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2774. (org-sort-entries nil ?o)
  2775. (buffer-string))))
  2776. (should
  2777. (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
  2778. (org-test-with-temp-text
  2779. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2780. (org-sort-entries nil ?O)
  2781. (buffer-string))))
  2782. ;; Sort by priority.
  2783. (should
  2784. (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
  2785. (org-test-with-temp-text
  2786. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2787. (org-sort-entries nil ?p)
  2788. (buffer-string))))
  2789. (should
  2790. (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
  2791. (org-test-with-temp-text
  2792. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2793. (org-sort-entries nil ?P)
  2794. (buffer-string))))
  2795. ;; Sort by creation time.
  2796. (should
  2797. (equal "
  2798. * h3
  2799. [2017-05-08 Mon]
  2800. * h2
  2801. [2017-05-09 Tue]
  2802. * h1
  2803. [2018-05-09 Wed]
  2804. "
  2805. (org-test-with-temp-text
  2806. "
  2807. * h1
  2808. [2018-05-09 Wed]
  2809. * h2
  2810. [2017-05-09 Tue]
  2811. * h3
  2812. [2017-05-08 Mon]
  2813. "
  2814. (org-sort-entries nil ?c)
  2815. (buffer-string))))
  2816. ;; Sort by scheduled date.
  2817. (should
  2818. (equal "
  2819. * TODO h4
  2820. SCHEDULED: <2017-05-06 Sat>
  2821. * TODO h3
  2822. SCHEDULED: <2017-05-08 Mon>
  2823. * TODO h2
  2824. DEADLINE: <2017-05-09 Tue>
  2825. * TODO h1
  2826. DEADLINE: <2017-05-07 Sun>
  2827. "
  2828. (org-test-with-temp-text
  2829. "
  2830. * TODO h2
  2831. DEADLINE: <2017-05-09 Tue>
  2832. * TODO h1
  2833. DEADLINE: <2017-05-07 Sun>
  2834. * TODO h3
  2835. SCHEDULED: <2017-05-08 Mon>
  2836. * TODO h4
  2837. SCHEDULED: <2017-05-06 Sat>
  2838. "
  2839. (org-sort-entries nil ?s)
  2840. (buffer-string))))
  2841. ;; Sort by deadline date.
  2842. (should
  2843. (equal "
  2844. * TODO h1
  2845. DEADLINE: <2017-05-07 Sun>
  2846. * TODO h2
  2847. DEADLINE: <2017-05-09 Tue>
  2848. * TODO h3
  2849. SCHEDULED: <2017-05-08 Mon>
  2850. * TODO h4
  2851. SCHEDULED: <2017-05-06 Sat>
  2852. "
  2853. (org-test-with-temp-text
  2854. "
  2855. * TODO h2
  2856. DEADLINE: <2017-05-09 Tue>
  2857. * TODO h1
  2858. DEADLINE: <2017-05-07 Sun>
  2859. * TODO h3
  2860. SCHEDULED: <2017-05-08 Mon>
  2861. * TODO h4
  2862. SCHEDULED: <2017-05-06 Sat>
  2863. "
  2864. (org-sort-entries nil ?d)
  2865. (buffer-string))))
  2866. ;; Sort by any date/time
  2867. (should
  2868. (equal "
  2869. * TODO h4
  2870. SCHEDULED: <2017-05-06 Sat>
  2871. * TODO h1
  2872. DEADLINE: <2017-05-07 Sun>
  2873. * TODO h3
  2874. SCHEDULED: <2017-05-08 Mon>
  2875. * TODO h2
  2876. DEADLINE: <2017-05-09 Tue>
  2877. "
  2878. (org-test-with-temp-text
  2879. "
  2880. * TODO h2
  2881. DEADLINE: <2017-05-09 Tue>
  2882. * TODO h1
  2883. DEADLINE: <2017-05-07 Sun>
  2884. * TODO h3
  2885. SCHEDULED: <2017-05-08 Mon>
  2886. * TODO h4
  2887. SCHEDULED: <2017-05-06 Sat>
  2888. "
  2889. (org-sort-entries nil ?t)
  2890. (buffer-string))))
  2891. ;; Sort by clocking time.
  2892. (should
  2893. (equal "
  2894. * clocked h2
  2895. :LOGBOOK:
  2896. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2897. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2898. :END:
  2899. * clocked h1
  2900. :LOGBOOK:
  2901. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2902. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2903. :END:
  2904. "
  2905. (org-test-with-temp-text
  2906. "
  2907. * clocked h1
  2908. :LOGBOOK:
  2909. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2910. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2911. :END:
  2912. * clocked h2
  2913. :LOGBOOK:
  2914. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2915. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2916. :END:
  2917. "
  2918. (org-sort-entries nil ?k)
  2919. (buffer-string))))
  2920. ;; Preserve file local variables when sorting.
  2921. (should
  2922. (equal "\n* A\n* B\n# Local Variables:\n# foo: t\n# End:\n"
  2923. (org-test-with-temp-text
  2924. "\n* B\n* A\n# Local Variables:\n# foo: t\n# End:"
  2925. (org-sort-entries nil ?a)
  2926. (buffer-string)))))
  2927. (ert-deftest test-org/string-collate-greaterp ()
  2928. "Test `org-string-collate-greaterp' specifications."
  2929. (should (org-string-collate-greaterp "def" "abc"))
  2930. (should-not (org-string-collate-greaterp "abc" "def")))
  2931. (ert-deftest test-org/file-contents ()
  2932. "Test `org-file-contents' specifications."
  2933. ;; Open files.
  2934. (should
  2935. (string= "#+BIND: variable value
  2936. #+DESCRIPTION: l2
  2937. #+LANGUAGE: en
  2938. #+SELECT_TAGS: b
  2939. #+TITLE: b
  2940. #+PROPERTY: a 1
  2941. " (org-file-contents (expand-file-name "setupfile3.org"
  2942. (concat org-test-dir "examples/")))))
  2943. ;; Throw error when trying to access an invalid file.
  2944. (should-error (org-file-contents "this-file-must-not-exist"))
  2945. ;; Try to access an invalid file, but do not throw an error.
  2946. (should
  2947. (progn (org-file-contents "this-file-must-not-exist" :noerror) t))
  2948. ;; Open URL.
  2949. (should
  2950. (string= "foo"
  2951. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2952. (unwind-protect
  2953. ;; Simulate successful retrieval of a URL.
  2954. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2955. (lambda (&rest_)
  2956. (with-current-buffer buffer
  2957. (insert "HTTP/1.1 200 OK\n\nfoo"))
  2958. buffer)))
  2959. (org-file-contents "http://some-valid-url"))
  2960. (kill-buffer buffer)))))
  2961. ;; Throw error when trying to access an invalid URL.
  2962. (should-error
  2963. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2964. (unwind-protect
  2965. ;; Simulate unsuccessful retrieval of a URL.
  2966. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2967. (lambda (&rest_)
  2968. (with-current-buffer buffer
  2969. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2970. buffer)))
  2971. (org-file-contents "http://this-url-must-not-exist"))
  2972. (kill-buffer buffer))))
  2973. ;; Try to access an invalid URL, but do not throw an error.
  2974. (should-error
  2975. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2976. (unwind-protect
  2977. ;; Simulate unsuccessful retrieval of a URL.
  2978. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2979. (lambda (&rest_)
  2980. (with-current-buffer buffer
  2981. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2982. buffer)))
  2983. (org-file-contents "http://this-url-must-not-exist"))
  2984. (kill-buffer buffer))))
  2985. (should
  2986. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2987. (unwind-protect
  2988. ;; Simulate unsuccessful retrieval of a URL.
  2989. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2990. (lambda (&rest_)
  2991. (with-current-buffer buffer
  2992. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2993. buffer)))
  2994. (org-file-contents "http://this-url-must-not-exist" :noerror))
  2995. (kill-buffer buffer))
  2996. t)))
  2997. ;;; Navigation
  2998. (ert-deftest test-org/forward-heading-same-level ()
  2999. "Test `org-forward-heading-same-level' specifications."
  3000. ;; Test navigation at top level, forward and backward.
  3001. (should
  3002. (equal "* H2"
  3003. (org-test-with-temp-text "* H1\n* H2"
  3004. (org-forward-heading-same-level 1)
  3005. (buffer-substring-no-properties (point) (line-end-position)))))
  3006. (should
  3007. (equal "* H1"
  3008. (org-test-with-temp-text "* H1\n<point>* H2"
  3009. (org-forward-heading-same-level -1)
  3010. (buffer-substring-no-properties (point) (line-end-position)))))
  3011. ;; Test navigation in a sub-tree, forward and backward.
  3012. (should
  3013. (equal "* H2"
  3014. (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
  3015. (org-forward-heading-same-level 1)
  3016. (buffer-substring-no-properties (point) (line-end-position)))))
  3017. (should
  3018. (equal "* H1"
  3019. (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
  3020. (org-forward-heading-same-level -1)
  3021. (buffer-substring-no-properties (point) (line-end-position)))))
  3022. ;; Stop at first or last sub-heading.
  3023. (should-not
  3024. (equal "* H2"
  3025. (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
  3026. (org-forward-heading-same-level 1)
  3027. (buffer-substring-no-properties (point) (line-end-position)))))
  3028. (should-not
  3029. (equal "* H2"
  3030. (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
  3031. (org-forward-heading-same-level -1)
  3032. (buffer-substring-no-properties (point) (line-end-position)))))
  3033. ;; Allow multiple moves.
  3034. (should
  3035. (equal "* H3"
  3036. (org-test-with-temp-text "* H1\n* H2\n* H3"
  3037. (org-forward-heading-same-level 2)
  3038. (buffer-substring-no-properties (point) (line-end-position)))))
  3039. (should
  3040. (equal "* H1"
  3041. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3042. (org-forward-heading-same-level -2)
  3043. (buffer-substring-no-properties (point) (line-end-position)))))
  3044. ;; Ignore spurious moves when first (or last) sibling is reached.
  3045. (should
  3046. (equal "** H3"
  3047. (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
  3048. (org-forward-heading-same-level 100)
  3049. (buffer-substring-no-properties (point) (line-end-position)))))
  3050. (should
  3051. (equal "** H1"
  3052. (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
  3053. (org-forward-heading-same-level -100)
  3054. (buffer-substring-no-properties (point) (line-end-position))))))
  3055. (ert-deftest test-org/end-of-meta-data ()
  3056. "Test `org-end-of-meta-data' specifications."
  3057. ;; Skip planning line.
  3058. (should
  3059. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  3060. (org-end-of-meta-data)
  3061. (eobp)))
  3062. ;; Skip properties drawer.
  3063. (should
  3064. (org-test-with-temp-text
  3065. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  3066. (org-end-of-meta-data)
  3067. (eobp)))
  3068. ;; Skip both.
  3069. (should
  3070. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  3071. (org-end-of-meta-data)
  3072. (eobp)))
  3073. ;; Nothing to skip, go to first line.
  3074. (should
  3075. (org-test-with-temp-text "* Headline\nContents"
  3076. (org-end-of-meta-data)
  3077. (looking-at "Contents")))
  3078. ;; With option argument, skip empty lines, regular drawers and
  3079. ;; clocking lines.
  3080. (should
  3081. (org-test-with-temp-text "* Headline\n\nContents"
  3082. (org-end-of-meta-data t)
  3083. (looking-at "Contents")))
  3084. (should
  3085. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  3086. (org-end-of-meta-data t)
  3087. (looking-at "Contents")))
  3088. (should
  3089. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  3090. (org-end-of-meta-data t)
  3091. (looking-at "Contents")))
  3092. ;; Special case: do not skip incomplete drawers.
  3093. (should
  3094. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  3095. (org-end-of-meta-data t)
  3096. (looking-at ":LOGBOOK:")))
  3097. ;; Special case: Be careful about consecutive headlines.
  3098. (should-not
  3099. (org-test-with-temp-text "* H1\n*H2\nContents"
  3100. (org-end-of-meta-data t)
  3101. (looking-at "Contents"))))
  3102. (ert-deftest test-org/beginning-of-line ()
  3103. "Test `org-beginning-of-line' specifications."
  3104. ;; Move to beginning of line. If current line in invisible, move to
  3105. ;; beginning of visible line instead.
  3106. (should
  3107. (org-test-with-temp-text "Some text\nSome other text<point>"
  3108. (org-beginning-of-line)
  3109. (bolp)))
  3110. (should
  3111. (org-test-with-temp-text "* H1\n** H2<point>"
  3112. (org-overview)
  3113. (org-beginning-of-line)
  3114. (= (line-beginning-position) 1)))
  3115. ;; With `visual-line-mode' active, move to beginning of visual line.
  3116. (should-not
  3117. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3118. (visual-line-mode)
  3119. (dotimes (i 1000) (insert "very "))
  3120. (org-beginning-of-line)
  3121. (bolp)))
  3122. ;; In a wide headline, with `visual-line-mode', prefer going to the
  3123. ;; beginning of a visual line than to the logical beginning of line,
  3124. ;; even if special movement is active.
  3125. (should-not
  3126. (org-test-with-temp-text "* A <point>long headline"
  3127. (visual-line-mode)
  3128. (dotimes (i 1000) (insert "very "))
  3129. (goto-char (point-max))
  3130. (org-beginning-of-line)
  3131. (bobp)))
  3132. (should-not
  3133. (org-test-with-temp-text "* A <point>long headline"
  3134. (visual-line-mode)
  3135. (dotimes (i 1000) (insert "very "))
  3136. (goto-char (point-max))
  3137. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  3138. (bobp)))
  3139. ;; At an headline with special movement, first move at beginning of
  3140. ;; title, then at the beginning of line, rinse, repeat.
  3141. (should
  3142. (org-test-with-temp-text "* TODO Headline<point>"
  3143. (let ((org-special-ctrl-a/e t))
  3144. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  3145. (progn (org-beginning-of-line) (bolp))
  3146. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3147. (should
  3148. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3149. (let ((org-special-ctrl-a/e t))
  3150. (org-beginning-of-line)
  3151. (looking-at "Headline"))))
  3152. (should
  3153. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3154. (let ((org-special-ctrl-a/e '(t . nil)))
  3155. (org-beginning-of-line)
  3156. (looking-at "Headline"))))
  3157. (should-not
  3158. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3159. (let ((org-special-ctrl-a/e '(nil . nil)))
  3160. (org-beginning-of-line)
  3161. (looking-at "Headline"))))
  3162. ;; At an headline with reversed movement, first move to beginning of
  3163. ;; line, then to the beginning of title.
  3164. (should
  3165. (org-test-with-temp-text "* TODO Headline<point>"
  3166. (let ((org-special-ctrl-a/e 'reversed)
  3167. (this-command last-command))
  3168. (and (progn (org-beginning-of-line) (bolp))
  3169. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3170. (should
  3171. (org-test-with-temp-text "* TODO Headline<point>"
  3172. (let ((org-special-ctrl-a/e '(reversed . nil))
  3173. (this-command last-command))
  3174. (and (progn (org-beginning-of-line) (bolp))
  3175. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3176. (should-not
  3177. (org-test-with-temp-text "* TODO Headline<point>"
  3178. (let ((org-special-ctrl-a/e '(t . nil))
  3179. (this-command last-command))
  3180. (and (progn (org-beginning-of-line) (bolp))
  3181. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3182. ;; At an item with special movement, first move after to beginning
  3183. ;; of title, then to the beginning of line, rinse, repeat.
  3184. (should
  3185. (org-test-with-temp-text "- [ ] Item<point>"
  3186. (let ((org-special-ctrl-a/e t))
  3187. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  3188. (progn (org-beginning-of-line) (bolp))
  3189. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3190. ;; At an item with reversed movement, first move to beginning of
  3191. ;; line, then to the beginning of title.
  3192. (should
  3193. (org-test-with-temp-text "- [X] Item<point>"
  3194. (let ((org-special-ctrl-a/e 'reversed)
  3195. (this-command last-command))
  3196. (and (progn (org-beginning-of-line) (bolp))
  3197. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3198. ;; Leave point before invisible characters at column 0.
  3199. (should
  3200. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3201. (let ((org-special-ctrl-a/e nil))
  3202. (org-beginning-of-line)
  3203. (bolp))))
  3204. (should
  3205. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3206. (let ((org-special-ctrl-a/e t))
  3207. (org-beginning-of-line)
  3208. (bolp))))
  3209. (should
  3210. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  3211. (visual-line-mode)
  3212. (org-beginning-of-line)
  3213. (bolp)))
  3214. ;; Special case: Do not error when the buffer contains only a single
  3215. ;; asterisk.
  3216. (should
  3217. (org-test-with-temp-text "*<point>"
  3218. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  3219. (should
  3220. (org-test-with-temp-text "*<point>"
  3221. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  3222. (ert-deftest test-org/end-of-line ()
  3223. "Test `org-end-of-line' specifications."
  3224. ;; Standard test.
  3225. (should
  3226. (org-test-with-temp-text "Some text\nSome other text"
  3227. (org-end-of-line)
  3228. (eolp)))
  3229. ;; With `visual-line-mode' active, move to end of visible line.
  3230. ;; However, never go past ellipsis.
  3231. (should-not
  3232. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3233. (visual-line-mode)
  3234. (dotimes (i 1000) (insert "very "))
  3235. (goto-char (point-min))
  3236. (org-end-of-line)
  3237. (eolp)))
  3238. (should-not
  3239. (org-test-with-temp-text "* A short headline\nSome contents"
  3240. (visual-line-mode)
  3241. (org-overview)
  3242. (org-end-of-line)
  3243. (eobp)))
  3244. ;; In a wide headline, with `visual-line-mode', prefer going to end
  3245. ;; of visible line if tags, or end of line, are farther.
  3246. (should-not
  3247. (org-test-with-temp-text "* A <point>long headline"
  3248. (visual-line-mode)
  3249. (dotimes (i 1000) (insert "very "))
  3250. (goto-char (point-min))
  3251. (org-end-of-line)
  3252. (eolp)))
  3253. (should-not
  3254. (org-test-with-temp-text "* A <point>long headline :tag:"
  3255. (visual-line-mode)
  3256. (dotimes (i 1000) (insert "very "))
  3257. (goto-char (point-min))
  3258. (org-end-of-line)
  3259. (eolp)))
  3260. ;; At an headline without special movement, go to end of line.
  3261. ;; However, never go past ellipsis.
  3262. (should
  3263. (org-test-with-temp-text "* Headline2b :tag:\n"
  3264. (let ((org-special-ctrl-a/e nil))
  3265. (and (progn (org-end-of-line) (eolp))
  3266. (progn (org-end-of-line) (eolp))))))
  3267. (should
  3268. (org-test-with-temp-text "* Headline2b :tag:\n"
  3269. (let ((org-special-ctrl-a/e '(t . nil)))
  3270. (and (progn (org-end-of-line) (eolp))
  3271. (progn (org-end-of-line) (eolp))))))
  3272. (should
  3273. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3274. (org-overview)
  3275. (let ((org-special-ctrl-a/e nil))
  3276. (org-end-of-line)
  3277. (= 1 (line-beginning-position)))))
  3278. ;; At an headline with special movement, first move before tags,
  3279. ;; then at the end of line, rinse, repeat. However, never go past
  3280. ;; ellipsis.
  3281. (should
  3282. (org-test-with-temp-text "* Headline1 :tag:\n"
  3283. (let ((org-special-ctrl-a/e t))
  3284. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3285. (progn (org-end-of-line) (eolp))
  3286. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3287. (should
  3288. (org-test-with-temp-text "* Headline1 :tag:\n"
  3289. (let ((org-special-ctrl-a/e '(nil . t)))
  3290. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3291. (progn (org-end-of-line) (eolp))
  3292. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3293. (should-not
  3294. (org-test-with-temp-text "* Headline1 :tag:\n"
  3295. (let ((org-special-ctrl-a/e '(nil . nil)))
  3296. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3297. (progn (org-end-of-line) (eolp))
  3298. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3299. (should
  3300. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3301. (org-overview)
  3302. (let ((org-special-ctrl-a/e t))
  3303. (org-end-of-line)
  3304. (org-end-of-line)
  3305. (= 1 (line-beginning-position)))))
  3306. ;; At an headline, with reversed movement, first go to end of line,
  3307. ;; then before tags. However, never go past ellipsis.
  3308. (should
  3309. (org-test-with-temp-text "* Headline3 :tag:\n"
  3310. (let ((org-special-ctrl-a/e 'reversed)
  3311. (this-command last-command))
  3312. (and (progn (org-end-of-line) (eolp))
  3313. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3314. (should
  3315. (org-test-with-temp-text "* Headline3 :tag:\n"
  3316. (let ((org-special-ctrl-a/e '(nil . reversed))
  3317. (this-command last-command))
  3318. (and (progn (org-end-of-line) (eolp))
  3319. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3320. (should-not
  3321. (org-test-with-temp-text "* Headline3 :tag:\n"
  3322. (let ((org-special-ctrl-a/e '(nil . t))
  3323. (this-command last-command))
  3324. (and (progn (org-end-of-line) (eolp))
  3325. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3326. (should
  3327. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3328. (org-overview)
  3329. (let ((org-special-ctrl-a/e 'reversed))
  3330. (org-end-of-line)
  3331. (= 1 (line-beginning-position)))))
  3332. ;; At a block without hidden contents.
  3333. (should
  3334. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3335. (progn (org-end-of-line) (eolp))))
  3336. ;; At a block with hidden contents.
  3337. (should-not
  3338. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3339. (let ((org-special-ctrl-a/e t))
  3340. (org-hide-block-toggle)
  3341. (org-end-of-line)
  3342. (eobp))))
  3343. ;; Get past invisible characters at the end of line.
  3344. (should
  3345. (org-test-with-temp-text "[[https://orgmode.org]]"
  3346. (org-end-of-line)
  3347. (eolp))))
  3348. (ert-deftest test-org/open-line ()
  3349. "Test `org-open-line' specifications."
  3350. ;; Call `open-line' outside of tables.
  3351. (should
  3352. (equal "\nText"
  3353. (org-test-with-temp-text "Text"
  3354. (org-open-line 1)
  3355. (buffer-string))))
  3356. ;; At a table, create a row above.
  3357. (should
  3358. (equal "\n| |\n| a |"
  3359. (org-test-with-temp-text "\n<point>| a |"
  3360. (org-open-line 1)
  3361. (buffer-string))))
  3362. ;; At the very first character of the buffer, also call `open-line'.
  3363. (should
  3364. (equal "\n| a |"
  3365. (org-test-with-temp-text "| a |"
  3366. (org-open-line 1)
  3367. (buffer-string))))
  3368. ;; Narrowing does not count.
  3369. (should
  3370. (equal "Text\n| |\n| a |"
  3371. (org-test-with-temp-text "Text\n<point>| a |"
  3372. (narrow-to-region (point) (point-max))
  3373. (org-open-line 1)
  3374. (widen)
  3375. (buffer-string)))))
  3376. (ert-deftest test-org/forward-sentence ()
  3377. "Test `org-forward-sentence' specifications."
  3378. ;; At the end of a table cell, move to the end of the next one.
  3379. (should
  3380. (org-test-with-temp-text "| a<point> | b |"
  3381. (org-forward-sentence)
  3382. (looking-at " |$")))
  3383. ;; Elsewhere in a cell, move to its end.
  3384. (should
  3385. (org-test-with-temp-text "| a<point>c | b |"
  3386. (org-forward-sentence)
  3387. (looking-at " | b |$")))
  3388. ;; Otherwise, simply call `forward-sentence'.
  3389. (should
  3390. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3391. (org-forward-sentence)
  3392. (looking-at " Sentence 2.")))
  3393. (should
  3394. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3395. (org-forward-sentence)
  3396. (org-forward-sentence)
  3397. (eobp)))
  3398. ;; At the end of an element, jump to the next one, without stopping
  3399. ;; on blank lines in-between.
  3400. (should
  3401. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  3402. (org-forward-sentence)
  3403. (eobp)))
  3404. ;; Headlines are considered to be sentences by themselves, even if
  3405. ;; they do not end with a full stop.
  3406. (should
  3407. (equal
  3408. "* Headline"
  3409. (org-test-with-temp-text "* <point>Headline\nSentence."
  3410. (org-forward-sentence)
  3411. (buffer-substring-no-properties (line-beginning-position) (point)))))
  3412. (should
  3413. (org-test-with-temp-text "* Headline<point>\nSentence."
  3414. (org-forward-sentence)
  3415. (eobp)))
  3416. (should
  3417. (org-test-with-temp-text "Sentence.<point>\n\n* Headline\n\nSentence 2."
  3418. (org-forward-sentence)
  3419. (and (org-at-heading-p) (eolp)))))
  3420. (ert-deftest test-org/backward-sentence ()
  3421. "Test `org-backward-sentence' specifications."
  3422. ;; At the beginning of a table cell, move to the beginning of the
  3423. ;; previous one.
  3424. (should
  3425. (org-test-with-temp-text "| a | <point>b |"
  3426. (org-backward-sentence)
  3427. (looking-at "a | b |$")))
  3428. ;; Elsewhere in a cell, move to its beginning.
  3429. (should
  3430. (org-test-with-temp-text "| a | b<point>c |"
  3431. (org-backward-sentence)
  3432. (looking-at "bc |$")))
  3433. ;; Otherwise, simply call `backward-sentence'.
  3434. (should
  3435. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3436. (org-backward-sentence)
  3437. (looking-at "Sentence 2.")))
  3438. (should
  3439. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3440. (org-backward-sentence)
  3441. (org-backward-sentence)
  3442. (bobp)))
  3443. ;; Make sure to hit the beginning of a sentence on the same line as
  3444. ;; an item.
  3445. (should
  3446. (org-test-with-temp-text "- Line 1\n line <point>2."
  3447. (org-backward-sentence)
  3448. (looking-at "Line 1"))))
  3449. (ert-deftest test-org/forward-paragraph ()
  3450. "Test `org-forward-paragraph' specifications."
  3451. ;; At end of buffer, do not return an error.
  3452. (should
  3453. (org-test-with-temp-text "Paragraph"
  3454. (goto-char (point-max))
  3455. (org-forward-paragraph)
  3456. t))
  3457. ;; Standard test.
  3458. (should
  3459. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3460. (org-forward-paragraph)
  3461. (looking-at "P2")))
  3462. ;; Ignore depth.
  3463. (should
  3464. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  3465. (org-forward-paragraph)
  3466. (looking-at "P1")))
  3467. ;; Do not enter elements with invisible contents.
  3468. (should
  3469. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  3470. (org-hide-block-toggle)
  3471. (org-forward-paragraph)
  3472. (looking-at "P3")))
  3473. ;; On an affiliated keyword, jump to the beginning of the element.
  3474. (should
  3475. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  3476. (org-forward-paragraph)
  3477. (looking-at "Para")))
  3478. ;; On an item or a footnote definition, move to the second element
  3479. ;; inside, if any.
  3480. (should
  3481. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  3482. (org-forward-paragraph)
  3483. (looking-at " Paragraph")))
  3484. (should
  3485. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  3486. (org-forward-paragraph)
  3487. (looking-at "Paragraph")))
  3488. ;; On an item, or a footnote definition, when the first line is
  3489. ;; empty, move to the first item.
  3490. (should
  3491. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  3492. (org-forward-paragraph)
  3493. (looking-at " Paragraph")))
  3494. (should
  3495. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  3496. (org-forward-paragraph)
  3497. (looking-at "Paragraph")))
  3498. ;; On a table (resp. a property drawer) do not move through table
  3499. ;; rows (resp. node properties).
  3500. (should
  3501. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  3502. (org-forward-paragraph)
  3503. (looking-at "Paragraph")))
  3504. (should
  3505. (org-test-with-temp-text
  3506. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  3507. (org-forward-paragraph)
  3508. (looking-at "Paragraph")))
  3509. ;; On a verse or source block, stop after blank lines.
  3510. (should
  3511. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  3512. (org-forward-paragraph)
  3513. (looking-at "L2")))
  3514. (should
  3515. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  3516. (org-forward-paragraph)
  3517. (looking-at "L2"))))
  3518. (ert-deftest test-org/backward-paragraph ()
  3519. "Test `org-backward-paragraph' specifications."
  3520. ;; Do not error at beginning of buffer.
  3521. (should
  3522. (org-test-with-temp-text "Paragraph"
  3523. (org-backward-paragraph)
  3524. t))
  3525. ;; Regular test.
  3526. (should
  3527. (org-test-with-temp-text "P1\n\nP2\n\nP3<point>"
  3528. (org-backward-paragraph)
  3529. (looking-at "P3")))
  3530. (should
  3531. (org-test-with-temp-text "P1\n\nP2\n\n<point>P3"
  3532. (org-backward-paragraph)
  3533. (looking-at-p "P2")))
  3534. ;; Ignore depth.
  3535. (should
  3536. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\n<point>P3"
  3537. (org-backward-paragraph)
  3538. (looking-at-p "P2")))
  3539. ;; Ignore invisible elements.
  3540. (should
  3541. (org-test-with-temp-text "* H1\n P1\n* H2"
  3542. (org-cycle)
  3543. (goto-char (point-max))
  3544. (beginning-of-line)
  3545. (org-backward-paragraph)
  3546. (bobp)))
  3547. ;; On an affiliated keyword, jump to the first one.
  3548. (should
  3549. (org-test-with-temp-text
  3550. "P1\n#+name: n\n#+caption: c1\n#+caption: <point>c2\nP2"
  3551. (org-backward-paragraph)
  3552. (looking-at-p "#\\+name")))
  3553. ;; On the second element in an item or a footnote definition, jump
  3554. ;; to item or the definition.
  3555. (should
  3556. (org-test-with-temp-text "- line1\n\n<point> line2"
  3557. (org-backward-paragraph)
  3558. (looking-at-p "- line1")))
  3559. (should
  3560. (org-test-with-temp-text "[fn:1] line1\n\n<point> line2"
  3561. (org-backward-paragraph)
  3562. (looking-at-p "\\[fn:1\\] line1")))
  3563. ;; On a table (resp. a property drawer), ignore table rows
  3564. ;; (resp. node properties).
  3565. (should
  3566. (org-test-with-temp-text "| a | b |\n| c | d |\n<point>P1"
  3567. (org-backward-paragraph)
  3568. (bobp)))
  3569. (should
  3570. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  3571. (org-backward-paragraph)
  3572. (looking-at-p ":PROPERTIES:")))
  3573. ;; On a comment, example, src and verse blocks, stop before blank
  3574. ;; lines.
  3575. (should
  3576. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\n<point>L3\n#+END_VERSE"
  3577. (org-backward-paragraph)
  3578. (looking-at-p "L2")))
  3579. (should
  3580. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\n<point>L3#+END_SRC"
  3581. (org-backward-paragraph)
  3582. (looking-at-p "L2")))
  3583. ;; In comment, example, export, src and verse blocks, stop below
  3584. ;; opening line when called from within the block.
  3585. (should
  3586. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\nL2<point>\n#+END_VERSE"
  3587. (org-backward-paragraph)
  3588. (looking-at-p "L1")))
  3589. (should
  3590. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nL1\nL2<point>\n#+END_EXAMPLE"
  3591. (org-backward-paragraph)
  3592. (looking-at-p "L1")))
  3593. ;; When called from the opening line itself, however, move to
  3594. ;; beginning of block.
  3595. (should
  3596. (org-test-with-temp-text "#+BEGIN_<point>EXAMPLE\nL1\n#+END_EXAMPLE"
  3597. (org-backward-paragraph)
  3598. (bobp)))
  3599. ;; Pathological case: on an empty heading, move to its beginning.
  3600. (should
  3601. (org-test-with-temp-text "* <point>H"
  3602. (org-backward-paragraph)
  3603. (bobp))))
  3604. (ert-deftest test-org/forward-element ()
  3605. "Test `org-forward-element' specifications."
  3606. ;; 1. At EOB: should error.
  3607. (org-test-with-temp-text "Some text\n"
  3608. (goto-char (point-max))
  3609. (should-error (org-forward-element)))
  3610. ;; 2. Standard move: expected to ignore blank lines.
  3611. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  3612. (org-forward-element)
  3613. (should (looking-at (regexp-quote "Second paragraph."))))
  3614. ;; 3. Headline tests.
  3615. (org-test-with-temp-text "
  3616. * Head 1
  3617. ** Head 1.1
  3618. *** Head 1.1.1
  3619. ** Head 1.2"
  3620. ;; 3.1. At an headline beginning: move to next headline at the
  3621. ;; same level.
  3622. (goto-line 3)
  3623. (org-forward-element)
  3624. (should (looking-at (regexp-quote "** Head 1.2")))
  3625. ;; 3.2. At an headline beginning: move to parent headline if no
  3626. ;; headline at the same level.
  3627. (goto-line 3)
  3628. (org-forward-element)
  3629. (should (looking-at (regexp-quote "** Head 1.2"))))
  3630. ;; 4. Greater element tests.
  3631. (org-test-with-temp-text
  3632. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  3633. ;; 4.1. At a greater element: expected to skip contents.
  3634. (org-forward-element)
  3635. (should (looking-at (regexp-quote "Outside.")))
  3636. ;; 4.2. At the end of greater element contents: expected to skip
  3637. ;; to the end of the greater element.
  3638. (goto-line 2)
  3639. (org-forward-element)
  3640. (should (looking-at (regexp-quote "Outside."))))
  3641. ;; 5. List tests.
  3642. (org-test-with-temp-text "
  3643. - item1
  3644. - sub1
  3645. - sub2
  3646. - sub3
  3647. Inner paragraph.
  3648. - item2
  3649. Outside."
  3650. ;; 5.1. At list top point: expected to move to the element after
  3651. ;; the list.
  3652. (goto-line 2)
  3653. (org-forward-element)
  3654. (should (looking-at (regexp-quote "Outside.")))
  3655. ;; 5.2. Special case: at the first line of a sub-list, but not at
  3656. ;; beginning of line, move to next item.
  3657. (goto-line 2)
  3658. (forward-char)
  3659. (org-forward-element)
  3660. (should (looking-at "- item2"))
  3661. (goto-line 4)
  3662. (forward-char)
  3663. (org-forward-element)
  3664. (should (looking-at " - sub2"))
  3665. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  3666. (goto-line 4)
  3667. (org-forward-element)
  3668. (should (looking-at (regexp-quote " Inner paragraph.")))
  3669. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  3670. (goto-line 8)
  3671. (org-forward-element)
  3672. (should (looking-at (regexp-quote " Inner paragraph.")))
  3673. ;; 5.5. At an item: expected to move to next item, if any.
  3674. (goto-line 6)
  3675. (org-forward-element)
  3676. (should (looking-at " - sub3"))))
  3677. (ert-deftest test-org/backward-element ()
  3678. "Test `org-backward-element' specifications."
  3679. ;; 1. Should error at BOB.
  3680. (org-test-with-temp-text " \nParagraph."
  3681. (should-error (org-backward-element)))
  3682. ;; 2. Should move at BOB when called on the first element in buffer.
  3683. (should
  3684. (org-test-with-temp-text "\n#+TITLE: test"
  3685. (progn (forward-line)
  3686. (org-backward-element)
  3687. (bobp))))
  3688. ;; 3. Not at the beginning of an element: move at its beginning.
  3689. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3690. (goto-line 3)
  3691. (end-of-line)
  3692. (org-backward-element)
  3693. (should (looking-at (regexp-quote "Paragraph2."))))
  3694. ;; 4. Headline tests.
  3695. (org-test-with-temp-text "
  3696. * Head 1
  3697. ** Head 1.1
  3698. *** Head 1.1.1
  3699. ** Head 1.2"
  3700. ;; 4.1. At an headline beginning: move to previous headline at the
  3701. ;; same level.
  3702. (goto-line 5)
  3703. (org-backward-element)
  3704. (should (looking-at (regexp-quote "** Head 1.1")))
  3705. ;; 4.2. At an headline beginning: move to parent headline if no
  3706. ;; headline at the same level.
  3707. (goto-line 3)
  3708. (org-backward-element)
  3709. (should (looking-at (regexp-quote "* Head 1")))
  3710. ;; 4.3. At the first top-level headline: should error.
  3711. (goto-line 2)
  3712. (should-error (org-backward-element)))
  3713. ;; 5. At beginning of first element inside a greater element:
  3714. ;; expected to move to greater element's beginning.
  3715. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  3716. (goto-line 3)
  3717. (org-backward-element)
  3718. (should (looking-at "#\\+BEGIN_CENTER")))
  3719. ;; 6. At the beginning of the first element in a section: should
  3720. ;; move back to headline, if any.
  3721. (should
  3722. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  3723. (progn (goto-char (point-max))
  3724. (beginning-of-line)
  3725. (org-backward-element)
  3726. (org-at-heading-p))))
  3727. ;; 7. List tests.
  3728. (org-test-with-temp-text "
  3729. - item1
  3730. - sub1
  3731. - sub2
  3732. - sub3
  3733. Inner paragraph.
  3734. - item2
  3735. Outside."
  3736. ;; 7.1. At beginning of sub-list: expected to move to the
  3737. ;; paragraph before it.
  3738. (goto-line 4)
  3739. (org-backward-element)
  3740. (should (looking-at "item1"))
  3741. ;; 7.2. At an item in a list: expected to move at previous item.
  3742. (goto-line 8)
  3743. (org-backward-element)
  3744. (should (looking-at " - sub2"))
  3745. (goto-line 12)
  3746. (org-backward-element)
  3747. (should (looking-at "- item1"))
  3748. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3749. ;; beginning.
  3750. (goto-line 10)
  3751. (org-backward-element)
  3752. (should (looking-at " - sub1"))
  3753. (goto-line 15)
  3754. (org-backward-element)
  3755. (should (looking-at "- item1"))
  3756. ;; 7.4. At blank-lines before list end: expected to move to top
  3757. ;; item.
  3758. (goto-line 14)
  3759. (org-backward-element)
  3760. (should (looking-at "- item1"))))
  3761. (ert-deftest test-org/up-element ()
  3762. "Test `org-up-element' specifications."
  3763. ;; 1. At BOB or with no surrounding element: should error.
  3764. (org-test-with-temp-text "Paragraph."
  3765. (should-error (org-up-element)))
  3766. (org-test-with-temp-text "* Head1\n* Head2"
  3767. (goto-line 2)
  3768. (should-error (org-up-element)))
  3769. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3770. (goto-line 3)
  3771. (should-error (org-up-element)))
  3772. ;; 2. At an headline: move to parent headline.
  3773. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3774. (goto-line 3)
  3775. (org-up-element)
  3776. (should (looking-at "\\* Head1")))
  3777. ;; 3. Inside a greater element: move to greater element beginning.
  3778. (org-test-with-temp-text
  3779. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3780. (goto-line 3)
  3781. (org-up-element)
  3782. (should (looking-at "#\\+BEGIN_CENTER")))
  3783. ;; 4. List tests.
  3784. (org-test-with-temp-text "* Top
  3785. - item1
  3786. - sub1
  3787. - sub2
  3788. Paragraph within sub2.
  3789. - item2"
  3790. ;; 4.1. Within an item: move to the item beginning.
  3791. (goto-line 8)
  3792. (org-up-element)
  3793. (should (looking-at " - sub2"))
  3794. ;; 4.2. At an item in a sub-list: move to parent item.
  3795. (goto-line 4)
  3796. (org-up-element)
  3797. (should (looking-at "- item1"))
  3798. ;; 4.3. At an item in top list: move to beginning of whole list.
  3799. (goto-line 10)
  3800. (org-up-element)
  3801. (should (looking-at "- item1"))
  3802. ;; 4.4. Special case. At very top point: should move to parent of
  3803. ;; list.
  3804. (goto-line 2)
  3805. (org-up-element)
  3806. (should (looking-at "\\* Top"))))
  3807. (ert-deftest test-org/down-element ()
  3808. "Test `org-down-element' specifications."
  3809. ;; Error when the element hasn't got a recursive type.
  3810. (org-test-with-temp-text "Paragraph."
  3811. (should-error (org-down-element)))
  3812. ;; Error when the element has no contents
  3813. (org-test-with-temp-text "* Headline"
  3814. (should-error (org-down-element)))
  3815. ;; When at a plain-list, move to first item.
  3816. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3817. (goto-line 2)
  3818. (org-down-element)
  3819. (should (looking-at " - Item 1.1")))
  3820. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3821. (org-down-element)
  3822. (should (looking-at " Item 1")))
  3823. ;; When at a table, move to first row
  3824. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3825. (org-down-element)
  3826. (should (looking-at " a | b |")))
  3827. ;; Otherwise, move inside the greater element.
  3828. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3829. (org-down-element)
  3830. (should (looking-at "Paragraph"))))
  3831. (ert-deftest test-org/drag-element-backward ()
  3832. "Test `org-drag-element-backward' specifications."
  3833. ;; Standard test.
  3834. (should
  3835. (equal
  3836. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3837. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3838. (org-drag-element-backward)
  3839. (buffer-string))))
  3840. (should
  3841. (equal
  3842. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3843. (org-test-with-temp-text
  3844. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3845. (org-drag-element-backward)
  3846. (buffer-string))))
  3847. ;; Preserve blank lines.
  3848. (should
  3849. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3850. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3851. (org-drag-element-backward)
  3852. (buffer-string))))
  3853. ;; Preserve column.
  3854. (should
  3855. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3856. (org-drag-element-backward)
  3857. (looking-at-p "2")))
  3858. ;; Error when trying to move first element of buffer.
  3859. (should-error
  3860. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3861. (org-drag-element-backward))
  3862. :type 'user-error)
  3863. ;; Error when trying to swap nested elements.
  3864. (should-error
  3865. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  3866. (org-drag-element-backward))
  3867. :type 'user-error)
  3868. ;; Error when trying to swap an headline element and a non-headline
  3869. ;; element.
  3870. (should-error
  3871. (org-test-with-temp-text "Test.\n<point>* Head 1"
  3872. (org-drag-element-backward))
  3873. :type 'error)
  3874. ;; Error when called before first element.
  3875. (should-error
  3876. (org-test-with-temp-text "\n<point>"
  3877. (org-drag-element-backward))
  3878. :type 'user-error)
  3879. ;; Preserve visibility of elements and their contents.
  3880. (should
  3881. (equal '((63 . 82) (26 . 48))
  3882. (org-test-with-temp-text "
  3883. #+BEGIN_CENTER
  3884. Text.
  3885. #+END_CENTER
  3886. - item 1
  3887. #+BEGIN_QUOTE
  3888. Text.
  3889. #+END_QUOTE"
  3890. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3891. (search-backward "- item 1")
  3892. (org-drag-element-backward)
  3893. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3894. (overlays-in (point-min) (point-max))))))
  3895. ;; Pathological case: handle call with point in blank lines right
  3896. ;; after a headline.
  3897. (should
  3898. (equal "* H2\n\n* H1\nText\n"
  3899. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  3900. (org-drag-element-backward)
  3901. (buffer-string)))))
  3902. (ert-deftest test-org/drag-element-forward ()
  3903. "Test `org-drag-element-forward' specifications."
  3904. ;; 1. Error when trying to move first element of buffer.
  3905. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3906. (goto-line 3)
  3907. (should-error (org-drag-element-forward)))
  3908. ;; 2. Error when trying to swap nested elements.
  3909. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3910. (forward-line)
  3911. (should-error (org-drag-element-forward)))
  3912. ;; 3. Error when trying to swap a non-headline element and an
  3913. ;; headline.
  3914. (org-test-with-temp-text "Test.\n* Head 1"
  3915. (should-error (org-drag-element-forward)))
  3916. ;; 4. Error when called before first element.
  3917. (should-error
  3918. (org-test-with-temp-text "\n"
  3919. (forward-line)
  3920. (org-drag-element-backward))
  3921. :type 'user-error)
  3922. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3923. ;; between elements.
  3924. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3925. (search-forward "graph")
  3926. (org-drag-element-forward)
  3927. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3928. (should (looking-at " 1")))
  3929. ;; 5. Preserve visibility of elements and their contents.
  3930. (org-test-with-temp-text "
  3931. #+BEGIN_CENTER
  3932. Text.
  3933. #+END_CENTER
  3934. - item 1
  3935. #+BEGIN_QUOTE
  3936. Text.
  3937. #+END_QUOTE"
  3938. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3939. (search-backward "#+BEGIN_CENTER")
  3940. (org-drag-element-forward)
  3941. (should
  3942. (equal
  3943. '((63 . 82) (26 . 48))
  3944. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3945. (overlays-in (point-min) (point-max)))))))
  3946. (ert-deftest test-org/next-block ()
  3947. "Test `org-next-block' specifications."
  3948. ;; Regular test.
  3949. (should
  3950. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3951. (org-next-block 1)
  3952. (looking-at "#\\+BEGIN_CENTER")))
  3953. ;; Ignore case.
  3954. (should
  3955. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3956. (let ((case-fold-search nil))
  3957. (org-next-block 1)
  3958. (looking-at "#\\+begin_center"))))
  3959. ;; Ignore current line.
  3960. (should
  3961. (org-test-with-temp-text
  3962. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3963. (org-next-block 1)
  3964. (looking-at "#\\+BEGIN_CENTER")))
  3965. ;; Throw an error when no block is found.
  3966. (should-error
  3967. (org-test-with-temp-text "Paragraph"
  3968. (org-next-block 1)))
  3969. ;; With an argument, skip many blocks at once.
  3970. (should
  3971. (org-test-with-temp-text
  3972. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3973. (org-next-block 2)
  3974. (looking-at "#\\+BEGIN_QUOTE")))
  3975. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3976. (should
  3977. (org-test-with-temp-text
  3978. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3979. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3980. (looking-at "#\\+BEGIN_QUOTE")))
  3981. ;; Optional argument is also case-insensitive.
  3982. (should
  3983. (org-test-with-temp-text
  3984. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3985. (let ((case-fold-search nil))
  3986. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3987. (looking-at "#\\+begin_quote")))))
  3988. (ert-deftest test-org/insert-structure-template ()
  3989. "Test `org-insert-structure-template'."
  3990. ;; Test in empty buffer.
  3991. (should
  3992. (string= "#+begin_foo\n#+end_foo\n"
  3993. (org-test-with-temp-text ""
  3994. (org-insert-structure-template "foo")
  3995. (buffer-string))))
  3996. ;; Test with multiple lines in buffer.
  3997. (should
  3998. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  3999. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4000. (transient-mark-mode 1)
  4001. (org-mark-element)
  4002. (org-insert-structure-template "foo")
  4003. (buffer-string))))
  4004. ;; Mark only the current line.
  4005. (should
  4006. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4007. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4008. (transient-mark-mode 1)
  4009. (set-mark (point-min))
  4010. (end-of-line)
  4011. (org-insert-structure-template "foo")
  4012. (buffer-string))))
  4013. ;; Middle of paragraph.
  4014. (should
  4015. (string= "p1\n#+begin_foo\np2\n#+end_foo\np3"
  4016. (org-test-with-temp-text "p1\n<point>p2\np3"
  4017. (set-mark (line-beginning-position))
  4018. (end-of-line)
  4019. (activate-mark)
  4020. (org-insert-structure-template "foo")
  4021. (buffer-string))))
  4022. ;; Test with text in buffer, no region, no final newline.
  4023. (should
  4024. (string= "#+begin_foo\nI'm a paragraph.\n#+end_foo\n"
  4025. (org-test-with-temp-text "I'm a paragraph."
  4026. (org-mark-element)
  4027. (org-insert-structure-template "foo")
  4028. (buffer-string))))
  4029. ;; Test with text in buffer and region set.
  4030. (should
  4031. (string= "#+begin_foo\nI'm a paragraph\n\nI'm a second paragrah\n#+end_foo\n"
  4032. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragrah"
  4033. (set-mark (point))
  4034. (goto-char (point-max))
  4035. (org-insert-structure-template "foo")
  4036. (buffer-string))))
  4037. ;; Test with example escaping.
  4038. (should
  4039. (string= "#+begin_example\n,* Heading\n#+end_example\n"
  4040. (org-test-with-temp-text "* Heading"
  4041. (org-mark-element)
  4042. (org-insert-structure-template "example")
  4043. (buffer-string))))
  4044. ;; Test with indentation.
  4045. (should
  4046. (string= " #+begin_foo\n This is a paragraph\n #+end_foo\n"
  4047. (org-test-with-temp-text " This is a paragraph"
  4048. (org-mark-element)
  4049. (org-insert-structure-template "foo")
  4050. (buffer-string))))
  4051. (should
  4052. (string= " #+begin_foo\n Line 1\n Line2\n #+end_foo\n"
  4053. (org-test-with-temp-text " Line 1\n Line2"
  4054. (org-mark-element)
  4055. (org-insert-structure-template "foo")
  4056. (buffer-string))))
  4057. ;; Test point location.
  4058. (should
  4059. (string= "#+begin_foo\n"
  4060. (org-test-with-temp-text ""
  4061. (org-insert-structure-template "foo")
  4062. (buffer-substring (point-min) (point)))))
  4063. (should
  4064. (string= "#+begin_src "
  4065. (org-test-with-temp-text ""
  4066. (org-insert-structure-template "src")
  4067. (buffer-substring (point-min) (point))))))
  4068. (ert-deftest test-org/previous-block ()
  4069. "Test `org-previous-block' specifications."
  4070. ;; Regular test.
  4071. (should
  4072. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  4073. (org-previous-block 1)
  4074. (looking-at "#\\+BEGIN_CENTER")))
  4075. ;; Ignore case.
  4076. (should
  4077. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  4078. (let ((case-fold-search nil))
  4079. (org-previous-block 1)
  4080. (looking-at "#\\+begin_center"))))
  4081. ;; Ignore current line.
  4082. (should
  4083. (org-test-with-temp-text
  4084. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  4085. (org-previous-block 1)
  4086. (looking-at "#\\+BEGIN_QUOTE")))
  4087. ;; Throw an error when no block is found.
  4088. (should-error
  4089. (org-test-with-temp-text "Paragraph<point>"
  4090. (org-previous-block 1)))
  4091. ;; With an argument, skip many blocks at once.
  4092. (should
  4093. (org-test-with-temp-text
  4094. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4095. (org-previous-block 2)
  4096. (looking-at "#\\+BEGIN_CENTER")))
  4097. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  4098. (should
  4099. (org-test-with-temp-text
  4100. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4101. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4102. (looking-at "#\\+BEGIN_QUOTE")))
  4103. ;; Optional argument is also case-insensitive.
  4104. (should
  4105. (org-test-with-temp-text
  4106. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  4107. (let ((case-fold-search nil))
  4108. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4109. (looking-at "#\\+begin_quote")))))
  4110. ;;; Outline structure
  4111. (ert-deftest test-org/demote ()
  4112. "Test `org-demote' specifications."
  4113. ;; Add correct number of stars according to `org-odd-levels-only'.
  4114. (should
  4115. (= 2
  4116. (org-test-with-temp-text "* H"
  4117. (let ((org-odd-levels-only nil)) (org-demote))
  4118. (org-current-level))))
  4119. (should
  4120. (= 3
  4121. (org-test-with-temp-text "* H"
  4122. (let ((org-odd-levels-only t)) (org-demote))
  4123. (org-current-level))))
  4124. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4125. (should
  4126. (org-test-with-temp-text "* H :tag:"
  4127. (let ((org-tags-column 10)
  4128. (org-auto-align-tags t)
  4129. (org-odd-levels-only nil))
  4130. (org-demote))
  4131. (org-move-to-column 10)
  4132. (looking-at-p ":tag:$")))
  4133. (should-not
  4134. (org-test-with-temp-text "* H :tag:"
  4135. (let ((org-tags-column 10)
  4136. (org-auto-align-tags nil)
  4137. (org-odd-levels-only nil))
  4138. (org-demote))
  4139. (org-move-to-column 10)
  4140. (looking-at-p ":tag:$")))
  4141. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4142. ;; info and property drawers accordingly.
  4143. (should
  4144. (= 3
  4145. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4146. (let ((org-odd-levels-only nil)
  4147. (org-adapt-indentation t))
  4148. (org-demote))
  4149. (forward-line)
  4150. (org-get-indentation))))
  4151. (should
  4152. (= 3
  4153. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4154. (let ((org-odd-levels-only nil)
  4155. (org-adapt-indentation t))
  4156. (org-demote))
  4157. (forward-line)
  4158. (org-get-indentation))))
  4159. (should-not
  4160. (= 3
  4161. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4162. (let ((org-odd-levels-only nil)
  4163. (org-adapt-indentation nil))
  4164. (org-demote))
  4165. (forward-line)
  4166. (org-get-indentation))))
  4167. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4168. ;; section accordingly. Ignore, however, footnote definitions and
  4169. ;; inlinetasks boundaries.
  4170. (should
  4171. (= 3
  4172. (org-test-with-temp-text "* H\n Paragraph"
  4173. (let ((org-odd-levels-only nil)
  4174. (org-adapt-indentation t))
  4175. (org-demote))
  4176. (forward-line)
  4177. (org-get-indentation))))
  4178. (should
  4179. (= 2
  4180. (org-test-with-temp-text "* H\n Paragraph"
  4181. (let ((org-odd-levels-only nil)
  4182. (org-adapt-indentation nil))
  4183. (org-demote))
  4184. (forward-line)
  4185. (org-get-indentation))))
  4186. (should
  4187. (zerop
  4188. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  4189. (let ((org-odd-levels-only nil)
  4190. (org-adapt-indentation t))
  4191. (org-demote))
  4192. (goto-char (point-max))
  4193. (org-get-indentation))))
  4194. (should
  4195. (= 3
  4196. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  4197. (let ((org-odd-levels-only nil)
  4198. (org-adapt-indentation t))
  4199. (org-demote))
  4200. (goto-char (point-max))
  4201. (org-get-indentation))))
  4202. (when (featurep 'org-inlinetask)
  4203. (should
  4204. (zerop
  4205. (let ((org-inlinetask-min-level 5)
  4206. (org-adapt-indentation t))
  4207. (org-test-with-temp-text "* H\n***** I\n***** END"
  4208. (org-demote)
  4209. (forward-line)
  4210. (org-get-indentation))))))
  4211. (when (featurep 'org-inlinetask)
  4212. (should
  4213. (= 3
  4214. (let ((org-inlinetask-min-level 5)
  4215. (org-adapt-indentation t))
  4216. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  4217. (org-demote)
  4218. (forward-line 2)
  4219. (org-get-indentation))))))
  4220. ;; Ignore contents of source blocks or example blocks when
  4221. ;; indentation should be preserved (through
  4222. ;; `org-src-preserve-indentation' or "-i" flag).
  4223. (should-not
  4224. (zerop
  4225. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4226. (let ((org-adapt-indentation t)
  4227. (org-src-preserve-indentation nil))
  4228. (org-demote))
  4229. (forward-line 2)
  4230. (org-get-indentation))))
  4231. (should
  4232. (zerop
  4233. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  4234. (let ((org-adapt-indentation t)
  4235. (org-src-preserve-indentation t))
  4236. (org-demote))
  4237. (forward-line 2)
  4238. (org-get-indentation))))
  4239. (should
  4240. (zerop
  4241. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4242. (let ((org-adapt-indentation t)
  4243. (org-src-preserve-indentation t))
  4244. (org-demote))
  4245. (forward-line 2)
  4246. (org-get-indentation))))
  4247. (should
  4248. (zerop
  4249. (org-test-with-temp-text
  4250. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  4251. (let ((org-adapt-indentation t)
  4252. (org-src-preserve-indentation nil))
  4253. (org-demote))
  4254. (forward-line 2)
  4255. (org-get-indentation)))))
  4256. (ert-deftest test-org/promote ()
  4257. "Test `org-promote' specifications."
  4258. ;; Return an error if headline is to be promoted to level 0, unless
  4259. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  4260. ;; headline becomes a comment.
  4261. (should-error
  4262. (org-test-with-temp-text "* H"
  4263. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  4264. (should
  4265. (equal "# H"
  4266. (org-test-with-temp-text "* H"
  4267. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  4268. (buffer-string))))
  4269. ;; Remove correct number of stars according to
  4270. ;; `org-odd-levels-only'.
  4271. (should
  4272. (= 2
  4273. (org-test-with-temp-text "*** H"
  4274. (let ((org-odd-levels-only nil)) (org-promote))
  4275. (org-current-level))))
  4276. (should
  4277. (= 1
  4278. (org-test-with-temp-text "*** H"
  4279. (let ((org-odd-levels-only t)) (org-promote))
  4280. (org-current-level))))
  4281. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4282. (should
  4283. (org-test-with-temp-text "** H :tag:"
  4284. (let ((org-tags-column 10)
  4285. (org-auto-align-tags t)
  4286. (org-odd-levels-only nil))
  4287. (org-promote))
  4288. (org-move-to-column 10)
  4289. (looking-at-p ":tag:$")))
  4290. (should-not
  4291. (org-test-with-temp-text "** H :tag:"
  4292. (let ((org-tags-column 10)
  4293. (org-auto-align-tags nil)
  4294. (org-odd-levels-only nil))
  4295. (org-promote))
  4296. (org-move-to-column 10)
  4297. (looking-at-p ":tag:$")))
  4298. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4299. ;; info and property drawers.
  4300. (should
  4301. (= 2
  4302. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4303. (let ((org-odd-levels-only nil)
  4304. (org-adapt-indentation t))
  4305. (org-promote))
  4306. (forward-line)
  4307. (org-get-indentation))))
  4308. (should
  4309. (= 2
  4310. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4311. (let ((org-odd-levels-only nil)
  4312. (org-adapt-indentation t))
  4313. (org-promote))
  4314. (forward-line)
  4315. (org-get-indentation))))
  4316. (should-not
  4317. (= 2
  4318. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4319. (let ((org-odd-levels-only nil)
  4320. (org-adapt-indentation nil))
  4321. (org-promote))
  4322. (forward-line)
  4323. (org-get-indentation))))
  4324. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4325. ;; section accordingly. Ignore, however, footnote definitions and
  4326. ;; inlinetasks boundaries.
  4327. (should
  4328. (= 2
  4329. (org-test-with-temp-text "** H\n Paragraph"
  4330. (let ((org-odd-levels-only nil)
  4331. (org-adapt-indentation t))
  4332. (org-promote))
  4333. (forward-line)
  4334. (org-get-indentation))))
  4335. (should-not
  4336. (= 2
  4337. (org-test-with-temp-text "** H\n Paragraph"
  4338. (let ((org-odd-levels-only nil)
  4339. (org-adapt-indentation nil))
  4340. (org-promote))
  4341. (forward-line)
  4342. (org-get-indentation))))
  4343. (should
  4344. (= 2
  4345. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  4346. (let ((org-odd-levels-only nil)
  4347. (org-adapt-indentation t))
  4348. (org-promote))
  4349. (forward-line)
  4350. (org-get-indentation))))
  4351. (when (featurep 'org-inlinetask)
  4352. (should
  4353. (zerop
  4354. (let ((org-inlinetask-min-level 5)
  4355. (org-adapt-indentation t))
  4356. (org-test-with-temp-text "** H\n***** I\n***** END"
  4357. (org-promote)
  4358. (forward-line)
  4359. (org-get-indentation))))))
  4360. (when (featurep 'org-inlinetask)
  4361. (should
  4362. (= 2
  4363. (let ((org-inlinetask-min-level 5)
  4364. (org-adapt-indentation t))
  4365. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  4366. (org-promote)
  4367. (forward-line 2)
  4368. (org-get-indentation))))))
  4369. ;; Give up shifting if it would break document's structure
  4370. ;; otherwise.
  4371. (should
  4372. (= 3
  4373. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  4374. (let ((org-odd-levels-only nil)
  4375. (org-adapt-indentation t))
  4376. (org-promote))
  4377. (forward-line)
  4378. (org-get-indentation))))
  4379. (should
  4380. (= 3
  4381. (org-test-with-temp-text "** H\n Paragraph\n * list."
  4382. (let ((org-odd-levels-only nil)
  4383. (org-adapt-indentation t))
  4384. (org-promote))
  4385. (forward-line)
  4386. (org-get-indentation))))
  4387. ;; Ignore contents of source blocks or example blocks when
  4388. ;; indentation should be preserved (through
  4389. ;; `org-src-preserve-indentation' or "-i" flag).
  4390. (should-not
  4391. (zerop
  4392. (org-test-with-temp-text
  4393. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4394. (let ((org-adapt-indentation t)
  4395. (org-src-preserve-indentation nil)
  4396. (org-odd-levels-only nil))
  4397. (org-promote))
  4398. (forward-line)
  4399. (org-get-indentation))))
  4400. (should
  4401. (zerop
  4402. (org-test-with-temp-text
  4403. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  4404. (let ((org-adapt-indentation t)
  4405. (org-src-preserve-indentation t)
  4406. (org-odd-levels-only nil))
  4407. (org-promote))
  4408. (forward-line)
  4409. (org-get-indentation))))
  4410. (should
  4411. (zerop
  4412. (org-test-with-temp-text
  4413. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4414. (let ((org-adapt-indentation t)
  4415. (org-src-preserve-indentation t)
  4416. (org-odd-levels-only nil))
  4417. (org-promote))
  4418. (forward-line)
  4419. (org-get-indentation))))
  4420. (should
  4421. (zerop
  4422. (org-test-with-temp-text
  4423. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  4424. (let ((org-adapt-indentation t)
  4425. (org-src-preserve-indentation nil)
  4426. (org-odd-levels-only nil))
  4427. (org-promote))
  4428. (forward-line)
  4429. (org-get-indentation)))))
  4430. (ert-deftest test-org/org-get-valid-level ()
  4431. "Test function `org-get-valid-level' specifications."
  4432. (let ((org-odd-levels-only nil))
  4433. (should (equal 1 (org-get-valid-level 0 0)))
  4434. (should (equal 1 (org-get-valid-level 0 1)))
  4435. (should (equal 2 (org-get-valid-level 0 2)))
  4436. (should (equal 3 (org-get-valid-level 0 3)))
  4437. (should (equal 1 (org-get-valid-level 1 0)))
  4438. (should (equal 2 (org-get-valid-level 1 1)))
  4439. (should (equal 23 (org-get-valid-level 1 22)))
  4440. (should (equal 1 (org-get-valid-level 1 -1)))
  4441. (should (equal 1 (org-get-valid-level 2 -1))))
  4442. (let ((org-odd-levels-only t))
  4443. (should (equal 1 (org-get-valid-level 0 0)))
  4444. (should (equal 1 (org-get-valid-level 0 1)))
  4445. (should (equal 3 (org-get-valid-level 0 2)))
  4446. (should (equal 5 (org-get-valid-level 0 3)))
  4447. (should (equal 1 (org-get-valid-level 1 0)))
  4448. (should (equal 3 (org-get-valid-level 1 1)))
  4449. (should (equal 3 (org-get-valid-level 2 1)))
  4450. (should (equal 5 (org-get-valid-level 3 1)))
  4451. (should (equal 5 (org-get-valid-level 4 1)))
  4452. (should (equal 43 (org-get-valid-level 1 21)))
  4453. (should (equal 1 (org-get-valid-level 1 -1)))
  4454. (should (equal 1 (org-get-valid-level 2 -1)))
  4455. (should (equal 1 (org-get-valid-level 3 -1)))
  4456. (should (equal 3 (org-get-valid-level 4 -1)))
  4457. (should (equal 3 (org-get-valid-level 5 -1)))))
  4458. ;;; Planning
  4459. (ert-deftest test-org/at-planning-p ()
  4460. "Test `org-at-planning-p' specifications."
  4461. ;; Regular test.
  4462. (should
  4463. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  4464. (org-at-planning-p)))
  4465. (should-not
  4466. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  4467. (org-at-planning-p)))
  4468. ;; Correctly find planning attached to inlinetasks.
  4469. (when (featurep 'org-inlinetask)
  4470. (should
  4471. (org-test-with-temp-text
  4472. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  4473. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4474. (should-not
  4475. (org-test-with-temp-text
  4476. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4477. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4478. (should-not
  4479. (org-test-with-temp-text
  4480. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4481. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4482. (should-not
  4483. (org-test-with-temp-text
  4484. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  4485. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  4486. (ert-deftest test-org/add-planning-info ()
  4487. "Test `org-add-planning-info'."
  4488. ;; Create deadline when `org-adapt-indentation' is non-nil.
  4489. (should
  4490. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4491. (org-test-with-temp-text "* H\nParagraph<point>"
  4492. (let ((org-adapt-indentation t))
  4493. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4494. (replace-regexp-in-string
  4495. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4496. nil nil 1))))
  4497. ;; Create deadline when `org-adapt-indentation' is nil.
  4498. (should
  4499. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4500. (org-test-with-temp-text "* H\nParagraph<point>"
  4501. (let ((org-adapt-indentation nil))
  4502. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4503. (replace-regexp-in-string
  4504. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4505. nil nil 1))))
  4506. ;; Update deadline when `org-adapt-indentation' is non-nil.
  4507. (should
  4508. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4509. (org-test-with-temp-text "\
  4510. * H
  4511. DEADLINE: <2015-06-24 Wed>
  4512. Paragraph<point>"
  4513. (let ((org-adapt-indentation t))
  4514. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4515. (replace-regexp-in-string
  4516. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4517. nil nil 1))))
  4518. ;; Update deadline when `org-adapt-indentation' is nil.
  4519. (should
  4520. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4521. (org-test-with-temp-text "\
  4522. * H
  4523. DEADLINE: <2015-06-24 Wed>
  4524. Paragraph<point>"
  4525. (let ((org-adapt-indentation nil))
  4526. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4527. (replace-regexp-in-string
  4528. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4529. nil nil 1))))
  4530. ;; Schedule when `org-adapt-indentation' is non-nil.
  4531. (should
  4532. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  4533. (org-test-with-temp-text "* H\nParagraph<point>"
  4534. (let ((org-adapt-indentation t))
  4535. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4536. (replace-regexp-in-string
  4537. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4538. nil nil 1))))
  4539. ;; Schedule when `org-adapt-indentation' is nil.
  4540. (should
  4541. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  4542. (org-test-with-temp-text "* H\nParagraph<point>"
  4543. (let ((org-adapt-indentation nil))
  4544. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4545. (replace-regexp-in-string
  4546. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4547. nil nil 1))))
  4548. ;; Add deadline when scheduled.
  4549. (should
  4550. (equal "\
  4551. * H
  4552. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  4553. Paragraph"
  4554. (org-test-with-temp-text "\
  4555. * H
  4556. SCHEDULED: <2015-06-24 Wed>
  4557. Paragraph<point>"
  4558. (let ((org-adapt-indentation t))
  4559. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4560. (replace-regexp-in-string
  4561. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4562. nil nil 1))))
  4563. ;; Remove middle entry.
  4564. (should
  4565. (equal "\
  4566. * H
  4567. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  4568. Paragraph"
  4569. (org-test-with-temp-text "\
  4570. * H
  4571. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4572. Paragraph<point>"
  4573. (let ((org-adapt-indentation t))
  4574. (org-add-planning-info nil nil 'deadline))
  4575. (replace-regexp-in-string
  4576. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4577. nil nil 1))))
  4578. ;; Remove last entry and then middle entry (order should not
  4579. ;; matter).
  4580. (should
  4581. (equal "\
  4582. * H
  4583. CLOSED: [2015-06-24]
  4584. Paragraph"
  4585. (org-test-with-temp-text "\
  4586. * H
  4587. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4588. Paragraph<point>"
  4589. (let ((org-adapt-indentation t))
  4590. (org-add-planning-info nil nil 'scheduled 'deadline))
  4591. (replace-regexp-in-string
  4592. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4593. nil nil 1))))
  4594. ;; Remove closed when `org-adapt-indentation' is non-nil.
  4595. (should
  4596. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4597. (org-test-with-temp-text "\
  4598. * H
  4599. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4600. Paragraph<point>"
  4601. (let ((org-adapt-indentation t))
  4602. (org-add-planning-info nil nil 'closed))
  4603. (replace-regexp-in-string
  4604. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4605. nil nil 1))))
  4606. (should
  4607. (equal "* H\n Paragraph"
  4608. (org-test-with-temp-text "\
  4609. * H
  4610. CLOSED: [2015-06-25 Thu]
  4611. Paragraph<point>"
  4612. (let ((org-adapt-indentation t))
  4613. (org-add-planning-info nil nil 'closed))
  4614. (replace-regexp-in-string
  4615. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4616. nil nil 1))))
  4617. ;; Remove closed when `org-adapt-indentation' is nil.
  4618. (should
  4619. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4620. (org-test-with-temp-text "\
  4621. * H
  4622. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4623. Paragraph<point>"
  4624. (let ((org-adapt-indentation nil))
  4625. (org-add-planning-info nil nil 'closed))
  4626. (replace-regexp-in-string
  4627. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4628. nil nil 1))))
  4629. (should
  4630. (equal "* H\nParagraph"
  4631. (org-test-with-temp-text "\
  4632. * H
  4633. CLOSED: [2015-06-25 Thu]
  4634. Paragraph<point>"
  4635. (let ((org-adapt-indentation nil))
  4636. (org-add-planning-info nil nil 'closed))
  4637. (replace-regexp-in-string
  4638. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4639. nil nil 1))))
  4640. ;; Remove closed entry and delete empty line.
  4641. (should
  4642. (equal "\
  4643. * H
  4644. Paragraph"
  4645. (org-test-with-temp-text "\
  4646. * H
  4647. CLOSED: [2015-06-24 Wed]
  4648. Paragraph<point>"
  4649. (let ((org-adapt-indentation t))
  4650. (org-add-planning-info nil nil 'closed))
  4651. (replace-regexp-in-string
  4652. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4653. nil nil 1))))
  4654. ;; Remove one entry and update another.
  4655. (should
  4656. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4657. (org-test-with-temp-text "\
  4658. * H
  4659. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  4660. Paragraph<point>"
  4661. (let ((org-adapt-indentation t))
  4662. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  4663. (replace-regexp-in-string
  4664. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4665. nil nil 1)))))
  4666. (ert-deftest test-org/deadline ()
  4667. "Test `org-deadline' specifications."
  4668. ;; Insert a new value or replace existing one.
  4669. (should
  4670. (equal "* H\nDEADLINE: <2012-03-29>\n"
  4671. (org-test-with-temp-text "* H"
  4672. (let ((org-adapt-indentation nil)
  4673. (org-last-inserted-timestamp nil))
  4674. (org-deadline nil "<2012-03-29 Tue>"))
  4675. (replace-regexp-in-string
  4676. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4677. nil nil 1))))
  4678. (should
  4679. (equal "* H\nDEADLINE: <2014-03-04>"
  4680. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4681. (let ((org-adapt-indentation nil)
  4682. (org-last-inserted-timestamp nil))
  4683. (org-deadline nil "<2014-03-04 Thu>"))
  4684. (replace-regexp-in-string
  4685. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4686. nil nil 1))))
  4687. ;; Accept delta time, e.g., "+2d".
  4688. (should
  4689. (equal "* H\nDEADLINE: <2015-03-04>\n"
  4690. (org-test-at-time "2014-03-04"
  4691. (org-test-with-temp-text "* H"
  4692. (let ((org-adapt-indentation nil)
  4693. (org-last-inserted-timestamp nil))
  4694. (org-deadline nil "+1y"))
  4695. (replace-regexp-in-string
  4696. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4697. ;; Preserve repeater.
  4698. (should
  4699. (equal "* H\nDEADLINE: <2012-03-29 +2y>\n"
  4700. (org-test-with-temp-text "* H"
  4701. (let ((org-adapt-indentation nil)
  4702. (org-last-inserted-timestamp nil))
  4703. (org-deadline nil "<2012-03-29 Tue +2y>"))
  4704. (replace-regexp-in-string
  4705. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4706. ;; Remove CLOSED keyword, if any.
  4707. (should
  4708. (equal "* H\nDEADLINE: <2012-03-29>"
  4709. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4710. (let ((org-adapt-indentation nil)
  4711. (org-last-inserted-timestamp nil))
  4712. (org-deadline nil "<2012-03-29 Tue>"))
  4713. (replace-regexp-in-string
  4714. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4715. ;; With C-u argument, remove DEADLINE keyword.
  4716. (should
  4717. (equal "* H\n"
  4718. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4719. (let ((org-adapt-indentation nil)
  4720. (org-last-inserted-timestamp nil))
  4721. (org-deadline '(4)))
  4722. (buffer-string))))
  4723. (should
  4724. (equal "* H"
  4725. (org-test-with-temp-text "* H"
  4726. (let ((org-adapt-indentation nil)
  4727. (org-last-inserted-timestamp nil))
  4728. (org-deadline '(4)))
  4729. (buffer-string))))
  4730. ;; With C-u C-u argument, prompt for a delay cookie.
  4731. (should
  4732. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  4733. (cl-letf (((symbol-function 'org-read-date)
  4734. (lambda (&rest args)
  4735. (apply #'encode-time
  4736. (org-parse-time-string "2014-03-04")))))
  4737. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4738. (let ((org-adapt-indentation nil)
  4739. (org-last-inserted-timestamp nil))
  4740. (org-deadline '(16)))
  4741. (buffer-string)))))
  4742. (should-error
  4743. (cl-letf (((symbol-function 'org-read-date)
  4744. (lambda (&rest args)
  4745. (apply #'encode-time
  4746. (org-parse-time-string "2014-03-04")))))
  4747. (org-test-with-temp-text "* H"
  4748. (let ((org-adapt-indentation nil)
  4749. (org-last-inserted-timestamp nil))
  4750. (org-deadline '(16)))
  4751. (buffer-string))))
  4752. ;; When a region is active and
  4753. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4754. ;; same value in all headlines in region.
  4755. (should
  4756. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4757. (org-test-with-temp-text "* H1\n* H2"
  4758. (let ((org-adapt-indentation nil)
  4759. (org-last-inserted-timestamp nil)
  4760. (org-loop-over-headlines-in-active-region t))
  4761. (transient-mark-mode 1)
  4762. (push-mark (point) t t)
  4763. (goto-char (point-max))
  4764. (org-deadline nil "2012-03-29"))
  4765. (replace-regexp-in-string
  4766. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4767. (should-not
  4768. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4769. (org-test-with-temp-text "* H1\n* H2"
  4770. (let ((org-adapt-indentation nil)
  4771. (org-last-inserted-timestamp nil)
  4772. (org-loop-over-headlines-in-active-region nil))
  4773. (transient-mark-mode 1)
  4774. (push-mark (point) t t)
  4775. (goto-char (point-max))
  4776. (org-deadline nil "2012-03-29"))
  4777. (replace-regexp-in-string
  4778. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4779. (ert-deftest test-org/schedule ()
  4780. "Test `org-schedule' specifications."
  4781. ;; Insert a new value or replace existing one.
  4782. (should
  4783. (equal "* H\nSCHEDULED: <2012-03-29>\n"
  4784. (org-test-with-temp-text "* H"
  4785. (let ((org-adapt-indentation nil)
  4786. (org-last-inserted-timestamp nil))
  4787. (org-schedule nil "<2012-03-29 Tue>"))
  4788. (replace-regexp-in-string
  4789. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4790. nil nil 1))))
  4791. (should
  4792. (equal "* H\nSCHEDULED: <2014-03-04>"
  4793. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4794. (let ((org-adapt-indentation nil)
  4795. (org-last-inserted-timestamp nil))
  4796. (org-schedule nil "<2014-03-04 Thu>"))
  4797. (replace-regexp-in-string
  4798. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4799. nil nil 1))))
  4800. ;; Accept delta time, e.g., "+2d".
  4801. (should
  4802. (equal "* H\nSCHEDULED: <2015-03-04>\n"
  4803. (org-test-at-time "2014-03-04"
  4804. (org-test-with-temp-text "* H"
  4805. (let ((org-adapt-indentation nil)
  4806. (org-last-inserted-timestamp nil))
  4807. (org-schedule nil "+1y"))
  4808. (replace-regexp-in-string
  4809. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4810. ;; Preserve repeater.
  4811. (should
  4812. (equal "* H\nSCHEDULED: <2012-03-29 +2y>\n"
  4813. (org-test-with-temp-text "* H"
  4814. (let ((org-adapt-indentation nil)
  4815. (org-last-inserted-timestamp nil))
  4816. (org-schedule nil "<2012-03-29 Tue +2y>"))
  4817. (replace-regexp-in-string
  4818. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4819. ;; Remove CLOSED keyword, if any.
  4820. (should
  4821. (equal "* H\nSCHEDULED: <2012-03-29>"
  4822. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4823. (let ((org-adapt-indentation nil)
  4824. (org-last-inserted-timestamp nil))
  4825. (org-schedule nil "<2012-03-29 Tue>"))
  4826. (replace-regexp-in-string
  4827. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4828. ;; With C-u argument, remove SCHEDULED keyword.
  4829. (should
  4830. (equal "* H\n"
  4831. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4832. (let ((org-adapt-indentation nil)
  4833. (org-last-inserted-timestamp nil))
  4834. (org-schedule '(4)))
  4835. (buffer-string))))
  4836. (should
  4837. (equal "* H"
  4838. (org-test-with-temp-text "* H"
  4839. (let ((org-adapt-indentation nil)
  4840. (org-last-inserted-timestamp nil))
  4841. (org-schedule '(4)))
  4842. (buffer-string))))
  4843. ;; With C-u C-u argument, prompt for a delay cookie.
  4844. (should
  4845. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  4846. (cl-letf (((symbol-function 'org-read-date)
  4847. (lambda (&rest args)
  4848. (apply #'encode-time
  4849. (org-parse-time-string "2014-03-04")))))
  4850. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4851. (let ((org-adapt-indentation nil)
  4852. (org-last-inserted-timestamp nil))
  4853. (org-schedule '(16)))
  4854. (buffer-string)))))
  4855. (should-error
  4856. (cl-letf (((symbol-function 'org-read-date)
  4857. (lambda (&rest args)
  4858. (apply #'encode-time
  4859. (org-parse-time-string "2014-03-04")))))
  4860. (org-test-with-temp-text "* H"
  4861. (let ((org-adapt-indentation nil)
  4862. (org-last-inserted-timestamp nil))
  4863. (org-schedule '(16)))
  4864. (buffer-string))))
  4865. ;; When a region is active and
  4866. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4867. ;; same value in all headlines in region.
  4868. (should
  4869. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4870. (org-test-with-temp-text "* H1\n* H2"
  4871. (let ((org-adapt-indentation nil)
  4872. (org-last-inserted-timestamp nil)
  4873. (org-loop-over-headlines-in-active-region t))
  4874. (transient-mark-mode 1)
  4875. (push-mark (point) t t)
  4876. (goto-char (point-max))
  4877. (org-schedule nil "2012-03-29"))
  4878. (replace-regexp-in-string
  4879. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4880. (should-not
  4881. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4882. (org-test-with-temp-text "* H1\n* H2"
  4883. (let ((org-adapt-indentation nil)
  4884. (org-last-inserted-timestamp nil)
  4885. (org-loop-over-headlines-in-active-region nil))
  4886. (transient-mark-mode 1)
  4887. (push-mark (point) t t)
  4888. (goto-char (point-max))
  4889. (org-schedule nil "2012-03-29"))
  4890. (replace-regexp-in-string
  4891. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4892. (should
  4893. ;; check if a repeater survives re-scheduling.
  4894. (string-match-p
  4895. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  4896. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  4897. (let ((org-adapt-indentation nil)
  4898. (org-last-inserted-timestamp nil))
  4899. (org-schedule nil "2017-02-01"))
  4900. (buffer-string)))))
  4901. ;;; Property API
  4902. (ert-deftest test-org/buffer-property-keys ()
  4903. "Test `org-buffer-property-keys' specifications."
  4904. ;; Retrieve properties accross siblings.
  4905. (should
  4906. (equal '("A" "B")
  4907. (org-test-with-temp-text "
  4908. * H1
  4909. :PROPERTIES:
  4910. :A: 1
  4911. :END:
  4912. * H2
  4913. :PROPERTIES:
  4914. :B: 1
  4915. :END:"
  4916. (org-buffer-property-keys))))
  4917. ;; Retrieve properties accross children.
  4918. (should
  4919. (equal '("A" "B")
  4920. (org-test-with-temp-text "
  4921. * H1
  4922. :PROPERTIES:
  4923. :A: 1
  4924. :END:
  4925. ** H2
  4926. :PROPERTIES:
  4927. :B: 1
  4928. :END:"
  4929. (org-buffer-property-keys))))
  4930. ;; Retrieve muliple properties in the same drawer.
  4931. (should
  4932. (equal '("A" "B")
  4933. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4934. (org-buffer-property-keys))))
  4935. ;; Ignore extension symbol in property name.
  4936. (should
  4937. (equal '("A")
  4938. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4939. (org-buffer-property-keys))))
  4940. ;; With non-nil COLUMNS, extract property names from columns.
  4941. (should
  4942. (equal '("A" "B")
  4943. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  4944. (org-buffer-property-keys nil nil t))))
  4945. (should
  4946. (equal '("A" "B" "COLUMNS")
  4947. (org-test-with-temp-text
  4948. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  4949. (org-buffer-property-keys nil nil t))))
  4950. ;; In COLUMNS, ignore title and summary-type.
  4951. (should
  4952. (equal '("A")
  4953. (org-test-with-temp-text "#+COLUMNS: %A(Foo)"
  4954. (org-buffer-property-keys nil nil t))))
  4955. (should
  4956. (equal '("A")
  4957. (org-test-with-temp-text "#+COLUMNS: %A{Foo}"
  4958. (org-buffer-property-keys nil nil t))))
  4959. (should
  4960. (equal '("A")
  4961. (org-test-with-temp-text "#+COLUMNS: %A(Foo){Bar}"
  4962. (org-buffer-property-keys nil nil t)))))
  4963. (ert-deftest test-org/property-values ()
  4964. "Test `org-property-values' specifications."
  4965. ;; Regular test.
  4966. (should
  4967. (equal '("2" "1")
  4968. (org-test-with-temp-text
  4969. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  4970. (org-property-values "A"))))
  4971. ;; Ignore empty values.
  4972. (should-not
  4973. (org-test-with-temp-text
  4974. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  4975. (org-property-values "A")))
  4976. ;; Take into consideration extended values.
  4977. (should
  4978. (equal '("1 2")
  4979. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4980. (org-property-values "A")))))
  4981. (ert-deftest test-org/find-property ()
  4982. "Test `org-find-property' specifications."
  4983. ;; Regular test.
  4984. (should
  4985. (= 1
  4986. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  4987. (org-find-property "prop"))))
  4988. ;; Ignore false positives.
  4989. (should
  4990. (= 27
  4991. (org-test-with-temp-text
  4992. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  4993. (org-find-property "A"))))
  4994. ;; Return first entry found in buffer.
  4995. (should
  4996. (= 1
  4997. (org-test-with-temp-text
  4998. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4999. (org-find-property "A"))))
  5000. ;; Only search visible part of the buffer.
  5001. (should
  5002. (= 31
  5003. (org-test-with-temp-text
  5004. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5005. (org-narrow-to-subtree)
  5006. (org-find-property "A"))))
  5007. ;; With optional argument, only find entries with a specific value.
  5008. (should-not
  5009. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5010. (org-find-property "A" "2")))
  5011. (should
  5012. (= 31
  5013. (org-test-with-temp-text
  5014. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  5015. (org-find-property "A" "2"))))
  5016. ;; Use "nil" for explicit nil values.
  5017. (should
  5018. (= 31
  5019. (org-test-with-temp-text
  5020. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  5021. (org-find-property "A" "nil")))))
  5022. (ert-deftest test-org/entry-delete ()
  5023. "Test `org-entry-delete' specifications."
  5024. ;; Regular test.
  5025. (should
  5026. (string-match
  5027. " *:PROPERTIES:\n *:B: +2\n *:END:"
  5028. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5029. (org-entry-delete (point) "A")
  5030. (buffer-string))))
  5031. ;; Also remove accumulated properties.
  5032. (should-not
  5033. (string-match
  5034. ":A"
  5035. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  5036. (org-entry-delete (point) "A")
  5037. (buffer-string))))
  5038. ;; When last property is removed, remove the property drawer.
  5039. (should-not
  5040. (string-match
  5041. ":PROPERTIES:"
  5042. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  5043. (org-entry-delete (point) "A")
  5044. (buffer-string))))
  5045. ;; Return a non-nil value when some property was removed.
  5046. (should
  5047. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5048. (org-entry-delete (point) "A")))
  5049. (should-not
  5050. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5051. (org-entry-delete (point) "C")))
  5052. ;; Special properties cannot be located in a drawer. Allow to
  5053. ;; remove them anyway, in case of user error.
  5054. (should
  5055. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  5056. (org-entry-delete (point) "SCHEDULED"))))
  5057. (ert-deftest test-org/entry-get ()
  5058. "Test `org-entry-get' specifications."
  5059. ;; Regular test.
  5060. (should
  5061. (equal "1"
  5062. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5063. (org-entry-get (point) "A"))))
  5064. ;; Ignore case.
  5065. (should
  5066. (equal "1"
  5067. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5068. (org-entry-get (point) "a"))))
  5069. ;; Handle extended values, both before and after base value.
  5070. (should
  5071. (equal "1 2 3"
  5072. (org-test-with-temp-text
  5073. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5074. (org-entry-get (point) "A"))))
  5075. ;; Empty values are returned as the empty string.
  5076. (should
  5077. (equal ""
  5078. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  5079. (org-entry-get (point) "A"))))
  5080. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  5081. ;; otherwise, return nil.
  5082. (should-not
  5083. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5084. (org-entry-get (point) "A")))
  5085. (should
  5086. (equal "nil"
  5087. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5088. (org-entry-get (point) "A" nil t))))
  5089. ;; Return nil when no property is found, independently on the
  5090. ;; LITERAL-NIL argument.
  5091. (should-not
  5092. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5093. (org-entry-get (point) "B")))
  5094. (should-not
  5095. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5096. (org-entry-get (point) "B" nil t)))
  5097. ;; Handle inheritance, when allowed. Include extended values and
  5098. ;; possibly global values.
  5099. (should
  5100. (equal
  5101. "1"
  5102. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5103. (org-entry-get (point) "A" t))))
  5104. (should
  5105. (equal
  5106. "1"
  5107. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5108. (let ((org-use-property-inheritance t))
  5109. (org-entry-get (point) "A" 'selective)))))
  5110. (should-not
  5111. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5112. (let ((org-use-property-inheritance nil))
  5113. (org-entry-get (point) "A" 'selective))))
  5114. (should
  5115. (equal
  5116. "1 2"
  5117. (org-test-with-temp-text
  5118. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  5119. (org-entry-get (point-max) "A" t))))
  5120. (should
  5121. (equal "1"
  5122. (org-test-with-temp-text
  5123. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  5124. (org-mode-restart)
  5125. (org-entry-get (point-max) "A" t))))
  5126. (should
  5127. (equal "0 1"
  5128. (org-test-with-temp-text
  5129. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5130. (org-mode-restart)
  5131. (org-entry-get (point-max) "A" t)))))
  5132. (ert-deftest test-org/entry-properties ()
  5133. "Test `org-entry-properties' specifications."
  5134. ;; Get "ITEM" property.
  5135. (should
  5136. (equal "H"
  5137. (org-test-with-temp-text "* TODO H"
  5138. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  5139. (should
  5140. (equal "H"
  5141. (org-test-with-temp-text "* TODO H"
  5142. (cdr (assoc "ITEM" (org-entry-properties))))))
  5143. ;; Get "TODO" property. TODO keywords are case sensitive.
  5144. (should
  5145. (equal "TODO"
  5146. (org-test-with-temp-text "* TODO H"
  5147. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  5148. (should
  5149. (equal "TODO"
  5150. (org-test-with-temp-text "* TODO H"
  5151. (cdr (assoc "TODO" (org-entry-properties))))))
  5152. (should-not
  5153. (org-test-with-temp-text "* H"
  5154. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5155. (should-not
  5156. (org-test-with-temp-text "* todo H"
  5157. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5158. ;; Get "PRIORITY" property.
  5159. (should
  5160. (equal "A"
  5161. (org-test-with-temp-text "* [#A] H"
  5162. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5163. (should
  5164. (equal "A"
  5165. (org-test-with-temp-text "* [#A] H"
  5166. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  5167. (should
  5168. (equal (char-to-string org-default-priority)
  5169. (org-test-with-temp-text "* H"
  5170. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5171. ;; Get "FILE" property.
  5172. (should
  5173. (org-test-with-temp-text-in-file "* H\nParagraph"
  5174. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  5175. (buffer-file-name))))
  5176. (should
  5177. (org-test-with-temp-text-in-file "* H\nParagraph"
  5178. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  5179. (buffer-file-name))))
  5180. (should-not
  5181. (org-test-with-temp-text "* H\nParagraph"
  5182. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  5183. ;; Get "TAGS" property.
  5184. (should
  5185. (equal ":tag1:tag2:"
  5186. (org-test-with-temp-text "* H :tag1:tag2:"
  5187. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  5188. (should
  5189. (equal ":tag1:tag2:"
  5190. (org-test-with-temp-text "* H :tag1:tag2:"
  5191. (cdr (assoc "TAGS" (org-entry-properties))))))
  5192. (should-not
  5193. (org-test-with-temp-text "* H"
  5194. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  5195. ;; Get "ALLTAGS" property.
  5196. (should
  5197. (equal ":tag1:tag2:"
  5198. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5199. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  5200. (should
  5201. (equal ":tag1:tag2:"
  5202. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5203. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  5204. (should-not
  5205. (org-test-with-temp-text "* H"
  5206. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  5207. ;; Get "BLOCKED" property.
  5208. (should
  5209. (equal "t"
  5210. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  5211. (let ((org-enforce-todo-dependencies t)
  5212. (org-blocker-hook
  5213. '(org-block-todo-from-children-or-siblings-or-parent)))
  5214. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5215. (should
  5216. (equal ""
  5217. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  5218. (let ((org-enforce-todo-dependencies t)
  5219. (org-blocker-hook
  5220. '(org-block-todo-from-children-or-siblings-or-parent)))
  5221. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5222. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  5223. (should
  5224. (equal
  5225. "[2012-03-29 thu.]"
  5226. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5227. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  5228. (should
  5229. (equal
  5230. "[2012-03-29 thu.]"
  5231. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5232. (cdr (assoc "CLOSED" (org-entry-properties))))))
  5233. (should-not
  5234. (org-test-with-temp-text "* H"
  5235. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  5236. (should
  5237. (equal
  5238. "<2014-03-04 tue.>"
  5239. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5240. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  5241. (should
  5242. (equal
  5243. "<2014-03-04 tue.>"
  5244. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5245. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  5246. (should-not
  5247. (org-test-with-temp-text "* H"
  5248. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  5249. (should
  5250. (equal
  5251. "<2014-03-04 tue.>"
  5252. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5253. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  5254. (should
  5255. (equal
  5256. "<2014-03-04 tue.>"
  5257. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5258. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  5259. (should-not
  5260. (org-test-with-temp-text "* H"
  5261. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  5262. ;; Get "CATEGORY"
  5263. (should
  5264. (equal "cat"
  5265. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5266. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  5267. (should
  5268. (equal "cat"
  5269. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5270. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5271. (should
  5272. (equal "cat"
  5273. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  5274. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5275. (should
  5276. (equal "cat2"
  5277. (org-test-with-temp-text
  5278. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  5279. "\n"
  5280. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  5281. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5282. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  5283. (should
  5284. (equal "<2012-03-29 thu.>"
  5285. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  5286. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5287. (should
  5288. (equal "[2012-03-29 thu.]"
  5289. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  5290. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  5291. (should
  5292. (equal "<2012-03-29 thu.>"
  5293. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  5294. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  5295. (should
  5296. (equal "[2014-03-04 tue.]"
  5297. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  5298. (cdr (assoc "TIMESTAMP_IA"
  5299. (org-entry-properties nil "TIMESTAMP_IA"))))))
  5300. (should-not
  5301. (equal "<2012-03-29 thu.>"
  5302. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  5303. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5304. ;; Get standard properties.
  5305. (should
  5306. (equal "1"
  5307. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5308. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5309. ;; Handle extended properties.
  5310. (should
  5311. (equal "1 2 3"
  5312. (org-test-with-temp-text
  5313. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5314. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5315. (should
  5316. (equal "1 2 3"
  5317. (org-test-with-temp-text
  5318. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  5319. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5320. ;; Ignore forbidden (special) properties.
  5321. (should-not
  5322. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  5323. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  5324. (ert-deftest test-org/entry-put ()
  5325. "Test `org-entry-put' specifications."
  5326. ;; Error when not a string or nil.
  5327. (should-error
  5328. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5329. (org-entry-put 1 "test" 2)))
  5330. ;; Error when property name is invalid.
  5331. (should-error
  5332. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5333. (org-entry-put 1 "no space" "value")))
  5334. (should-error
  5335. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5336. (org-entry-put 1 "" "value")))
  5337. ;; Set "TODO" property.
  5338. (should
  5339. (string-match (regexp-quote " TODO H")
  5340. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5341. (org-entry-put (point) "TODO" "TODO")
  5342. (buffer-string))))
  5343. (should
  5344. (string-match (regexp-quote "* H")
  5345. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5346. (org-entry-put (point) "TODO" nil)
  5347. (buffer-string))))
  5348. ;; Set "PRIORITY" property.
  5349. (should
  5350. (equal "* [#A] H"
  5351. (org-test-with-temp-text "* [#B] H"
  5352. (org-entry-put (point) "PRIORITY" "A")
  5353. (buffer-string))))
  5354. (should
  5355. (equal "* H"
  5356. (org-test-with-temp-text "* [#B] H"
  5357. (org-entry-put (point) "PRIORITY" nil)
  5358. (buffer-string))))
  5359. ;; Set "SCHEDULED" property.
  5360. (should
  5361. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  5362. (org-test-with-temp-text "* H"
  5363. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  5364. (buffer-string))))
  5365. (should
  5366. (string= "* H\n"
  5367. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5368. (org-entry-put (point) "SCHEDULED" nil)
  5369. (buffer-string))))
  5370. (should
  5371. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  5372. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5373. (org-entry-put (point) "SCHEDULED" "earlier")
  5374. (buffer-string))))
  5375. (should
  5376. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  5377. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5378. (org-entry-put (point) "SCHEDULED" "later")
  5379. (buffer-string))))
  5380. ;; Set "DEADLINE" property.
  5381. (should
  5382. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  5383. (org-test-with-temp-text "* H"
  5384. (org-entry-put (point) "DEADLINE" "2014-03-04")
  5385. (buffer-string))))
  5386. (should
  5387. (string= "* H\n"
  5388. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5389. (org-entry-put (point) "DEADLINE" nil)
  5390. (buffer-string))))
  5391. (should
  5392. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  5393. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5394. (org-entry-put (point) "DEADLINE" "earlier")
  5395. (buffer-string))))
  5396. (should
  5397. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  5398. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5399. (org-entry-put (point) "DEADLINE" "later")
  5400. (buffer-string))))
  5401. ;; Set "CATEGORY" property
  5402. (should
  5403. (string-match "^ *:CATEGORY: cat"
  5404. (org-test-with-temp-text "* H"
  5405. (org-entry-put (point) "CATEGORY" "cat")
  5406. (buffer-string))))
  5407. ;; Regular properties, with or without pre-existing drawer.
  5408. (should
  5409. (string-match "^ *:A: +2$"
  5410. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5411. (org-entry-put (point) "A" "2")
  5412. (buffer-string))))
  5413. (should
  5414. (string-match "^ *:A: +1$"
  5415. (org-test-with-temp-text "* H"
  5416. (org-entry-put (point) "A" "1")
  5417. (buffer-string))))
  5418. ;; Special case: two consecutive headlines.
  5419. (should
  5420. (string-match "\\* A\n *:PROPERTIES:"
  5421. (org-test-with-temp-text "* A\n** B"
  5422. (org-entry-put (point) "A" "1")
  5423. (buffer-string)))))
  5424. (ert-deftest test-org/refresh-properties ()
  5425. "Test `org-refresh-properties' specifications."
  5426. (should
  5427. (equal "1"
  5428. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5429. (org-refresh-properties "A" 'org-test)
  5430. (get-text-property (point) 'org-test))))
  5431. (should-not
  5432. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5433. (org-refresh-properties "B" 'org-test)
  5434. (get-text-property (point) 'org-test)))
  5435. ;; Handle properties only defined with extension syntax, i.e.,
  5436. ;; "PROPERTY+".
  5437. (should
  5438. (equal "1"
  5439. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5440. (org-refresh-properties "A" 'org-test)
  5441. (get-text-property (point) 'org-test))))
  5442. ;; When property is inherited, add text property to the whole
  5443. ;; sub-tree.
  5444. (should
  5445. (equal "1"
  5446. (org-test-with-temp-text
  5447. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  5448. (let ((org-use-property-inheritance t))
  5449. (org-refresh-properties "A" 'org-test))
  5450. (get-text-property (point) 'org-test))))
  5451. ;; When property is inherited, use global value across the whole
  5452. ;; buffer. However local values have precedence.
  5453. (should-not
  5454. (equal "1"
  5455. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5456. (org-mode-restart)
  5457. (let ((org-use-property-inheritance nil))
  5458. (org-refresh-properties "A" 'org-test))
  5459. (get-text-property (point) 'org-test))))
  5460. (should
  5461. (equal "1"
  5462. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5463. (org-mode-restart)
  5464. (let ((org-use-property-inheritance t))
  5465. (org-refresh-properties "A" 'org-test))
  5466. (get-text-property (point) 'org-test))))
  5467. (should
  5468. (equal "2"
  5469. (org-test-with-temp-text
  5470. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  5471. (org-mode-restart)
  5472. (let ((org-use-property-inheritance t))
  5473. (org-refresh-properties "A" 'org-test))
  5474. (get-text-property (point) 'org-test)))))
  5475. ;;; Radio Targets
  5476. (ert-deftest test-org/update-radio-target-regexp ()
  5477. "Test `org-update-radio-target-regexp' specifications."
  5478. ;; Properly update cache with no previous radio target regexp.
  5479. (should
  5480. (eq 'link
  5481. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5482. (save-excursion (goto-char (point-max)) (org-element-context))
  5483. (insert "<<<")
  5484. (search-forward "o")
  5485. (insert ">>>")
  5486. (org-update-radio-target-regexp)
  5487. (goto-char (point-max))
  5488. (org-element-type (org-element-context)))))
  5489. ;; Properly update cache with previous radio target regexp.
  5490. (should
  5491. (eq 'link
  5492. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5493. (save-excursion (goto-char (point-max)) (org-element-context))
  5494. (insert "<<<")
  5495. (search-forward "o")
  5496. (insert ">>>")
  5497. (org-update-radio-target-regexp)
  5498. (search-backward "r")
  5499. (delete-char 5)
  5500. (insert "new")
  5501. (org-update-radio-target-regexp)
  5502. (goto-char (point-max))
  5503. (delete-region (line-beginning-position) (point))
  5504. (insert "new")
  5505. (org-element-type (org-element-context))))))
  5506. ;;; Refile
  5507. (ert-deftest test-org/refile-get-targets ()
  5508. "Test `org-refile-get-targets' specifications."
  5509. ;; :maxlevel includes all headings above specified value.
  5510. (should
  5511. (equal '("H1" "H2" "H3")
  5512. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5513. (let ((org-refile-use-outline-path nil)
  5514. (org-refile-targets `((nil :maxlevel . 3))))
  5515. (mapcar #'car (org-refile-get-targets))))))
  5516. (should
  5517. (equal '("H1" "H2")
  5518. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5519. (let ((org-refile-use-outline-path nil)
  5520. (org-refile-targets `((nil :maxlevel . 2))))
  5521. (mapcar #'car (org-refile-get-targets))))))
  5522. ;; :level limits targets to headlines with the specified level.
  5523. (should
  5524. (equal '("H2")
  5525. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5526. (let ((org-refile-use-outline-path nil)
  5527. (org-refile-targets `((nil :level . 2))))
  5528. (mapcar #'car (org-refile-get-targets))))))
  5529. ;; :tag limits targets to headlines with specified tag.
  5530. (should
  5531. (equal '("H1")
  5532. (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
  5533. (let ((org-refile-use-outline-path nil)
  5534. (org-refile-targets `((nil :tag . "foo"))))
  5535. (mapcar #'car (org-refile-get-targets))))))
  5536. ;; :todo limits targets to headlines with specified TODO keyword.
  5537. (should
  5538. (equal '("H2")
  5539. (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
  5540. (let ((org-refile-use-outline-path nil)
  5541. (org-refile-targets `((nil :todo . "TODO"))))
  5542. (mapcar #'car (org-refile-get-targets))))))
  5543. ;; :regexp filters targets matching provided regexp.
  5544. (should
  5545. (equal '("F2" "F3")
  5546. (org-test-with-temp-text "* H1\n** F2\n*** F3"
  5547. (let ((org-refile-use-outline-path nil)
  5548. (org-refile-targets `((nil :regexp . "F"))))
  5549. (mapcar #'car (org-refile-get-targets))))))
  5550. ;; A nil `org-refile-targets' includes only top level headlines in
  5551. ;; current buffer.
  5552. (should
  5553. (equal '("H1" "H2")
  5554. (org-test-with-temp-text "* H1\n** S1\n* H2"
  5555. (let ((org-refile-use-outline-path nil)
  5556. (org-refile-targets nil))
  5557. (mapcar #'car (org-refile-get-targets))))))
  5558. ;; Return value is the union of the targets according to all the
  5559. ;; defined rules. However, prevent duplicates.
  5560. (should
  5561. (equal '("F2" "F3" "H1")
  5562. (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
  5563. (let ((org-refile-use-outline-path nil)
  5564. (org-refile-targets `((nil :regexp . "F")
  5565. (nil :todo . "TODO"))))
  5566. (mapcar #'car (org-refile-get-targets))))))
  5567. (should
  5568. (equal '("F2" "F3" "H1")
  5569. (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
  5570. (let ((org-refile-use-outline-path nil)
  5571. (org-refile-targets `((nil :regexp . "F")
  5572. (nil :todo . "TODO"))))
  5573. (mapcar #'car (org-refile-get-targets))))))
  5574. ;; When `org-refile-use-outline-path' is non-nil, provide targets as
  5575. ;; paths.
  5576. (should
  5577. (equal '("H1" "H1/H2" "H1/H2/H3")
  5578. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5579. (let ((org-refile-use-outline-path t)
  5580. (org-refile-targets `((nil :maxlevel . 3))))
  5581. (mapcar #'car (org-refile-get-targets))))))
  5582. ;; When providing targets as paths, escape forward slashes in
  5583. ;; headings with backslashes.
  5584. (should
  5585. (equal '("H1\\/foo")
  5586. (org-test-with-temp-text "* H1/foo"
  5587. (let ((org-refile-use-outline-path t)
  5588. (org-refile-targets `((nil :maxlevel . 1))))
  5589. (mapcar #'car (org-refile-get-targets))))))
  5590. ;; When `org-refile-use-outline-path' is `file', include file name
  5591. ;; without directory in targets.
  5592. (should
  5593. (org-test-with-temp-text-in-file "* H1"
  5594. (let* ((filename (buffer-file-name))
  5595. (org-refile-use-outline-path 'file)
  5596. (org-refile-targets `(((,filename) :level . 1))))
  5597. (member (file-name-nondirectory filename)
  5598. (mapcar #'car (org-refile-get-targets))))))
  5599. ;; When `org-refile-use-outline-path' is `full-file-path', include
  5600. ;; full file name.
  5601. (should
  5602. (org-test-with-temp-text-in-file "* H1"
  5603. (let* ((filename (file-truename (buffer-file-name)))
  5604. (org-refile-use-outline-path 'full-file-path)
  5605. (org-refile-targets `(((,filename) :level . 1))))
  5606. (member filename (mapcar #'car (org-refile-get-targets))))))
  5607. ;; When `org-refile-use-outline-path' is `buffer-name', include
  5608. ;; buffer name.
  5609. (should
  5610. (org-test-with-temp-text "* H1"
  5611. (let* ((org-refile-use-outline-path 'buffer-name)
  5612. (org-refile-targets `((nil :level . 1))))
  5613. (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
  5614. ;;; Sparse trees
  5615. (ert-deftest test-org/match-sparse-tree ()
  5616. "Test `org-match-sparse-tree' specifications."
  5617. ;; Match tags.
  5618. (should-not
  5619. (org-test-with-temp-text "* H\n** H1 :tag:"
  5620. (org-match-sparse-tree nil "tag")
  5621. (search-forward "H1")
  5622. (org-invisible-p2)))
  5623. (should
  5624. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  5625. (org-match-sparse-tree nil "tag")
  5626. (search-forward "H2")
  5627. (org-invisible-p2)))
  5628. ;; "-" operator for tags.
  5629. (should-not
  5630. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5631. (org-match-sparse-tree nil "tag1-tag2")
  5632. (search-forward "H1")
  5633. (org-invisible-p2)))
  5634. (should
  5635. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5636. (org-match-sparse-tree nil "tag1-tag2")
  5637. (search-forward "H2")
  5638. (org-invisible-p2)))
  5639. ;; "&" operator for tags.
  5640. (should
  5641. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5642. (org-match-sparse-tree nil "tag1&tag2")
  5643. (search-forward "H1")
  5644. (org-invisible-p2)))
  5645. (should-not
  5646. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5647. (org-match-sparse-tree nil "tag1&tag2")
  5648. (search-forward "H2")
  5649. (org-invisible-p2)))
  5650. ;; "|" operator for tags.
  5651. (should-not
  5652. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5653. (org-match-sparse-tree nil "tag1|tag2")
  5654. (search-forward "H1")
  5655. (org-invisible-p2)))
  5656. (should-not
  5657. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5658. (org-match-sparse-tree nil "tag1|tag2")
  5659. (search-forward "H2")
  5660. (org-invisible-p2)))
  5661. ;; Regexp match on tags.
  5662. (should-not
  5663. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5664. (org-match-sparse-tree nil "{^tag.*}")
  5665. (search-forward "H1")
  5666. (org-invisible-p2)))
  5667. (should
  5668. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5669. (org-match-sparse-tree nil "{^tag.*}")
  5670. (search-forward "H2")
  5671. (org-invisible-p2)))
  5672. ;; Match group tags.
  5673. (should-not
  5674. (org-test-with-temp-text
  5675. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5676. (org-match-sparse-tree nil "work")
  5677. (search-forward "H1")
  5678. (org-invisible-p2)))
  5679. (should-not
  5680. (org-test-with-temp-text
  5681. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5682. (org-match-sparse-tree nil "work")
  5683. (search-forward "H2")
  5684. (org-invisible-p2)))
  5685. ;; Match group tags with hard brackets.
  5686. (should-not
  5687. (org-test-with-temp-text
  5688. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5689. (org-match-sparse-tree nil "work")
  5690. (search-forward "H1")
  5691. (org-invisible-p2)))
  5692. (should-not
  5693. (org-test-with-temp-text
  5694. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5695. (org-match-sparse-tree nil "work")
  5696. (search-forward "H2")
  5697. (org-invisible-p2)))
  5698. ;; Match tags in hierarchies
  5699. (should-not
  5700. (org-test-with-temp-text
  5701. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5702. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5703. #+TAGS: { Lev_3 : Lev_4 }\n
  5704. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5705. (org-match-sparse-tree nil "Lev_1")
  5706. (search-forward "H4")
  5707. (org-invisible-p2)))
  5708. (should-not
  5709. (org-test-with-temp-text
  5710. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5711. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5712. #+TAGS: { Lev_3 : Lev_4 }\n
  5713. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5714. (org-match-sparse-tree nil "Lev_1+Lev_3")
  5715. (search-forward "H4")
  5716. (org-invisible-p2)))
  5717. ;; Match regular expressions in tags
  5718. (should-not
  5719. (org-test-with-temp-text
  5720. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  5721. (org-match-sparse-tree nil "Lev")
  5722. (search-forward "H1")
  5723. (org-invisible-p2)))
  5724. (should
  5725. (org-test-with-temp-text
  5726. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  5727. (org-match-sparse-tree nil "Lev")
  5728. (search-forward "H1")
  5729. (org-invisible-p2)))
  5730. ;; Match properties.
  5731. (should
  5732. (org-test-with-temp-text
  5733. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  5734. (org-match-sparse-tree nil "A=\"1\"")
  5735. (search-forward "H2")
  5736. (org-invisible-p2)))
  5737. (should-not
  5738. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5739. (org-match-sparse-tree nil "A=\"1\"")
  5740. (search-forward "H2")
  5741. (org-invisible-p2)))
  5742. ;; Case is not significant when matching properties.
  5743. (should-not
  5744. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5745. (org-match-sparse-tree nil "a=\"1\"")
  5746. (search-forward "H2")
  5747. (org-invisible-p2)))
  5748. (should-not
  5749. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  5750. (org-match-sparse-tree nil "A=\"1\"")
  5751. (search-forward "H2")
  5752. (org-invisible-p2)))
  5753. ;; Match special LEVEL property.
  5754. (should-not
  5755. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5756. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5757. (search-forward "H1")
  5758. (org-invisible-p2)))
  5759. (should
  5760. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5761. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5762. (search-forward "H2")
  5763. (org-invisible-p2)))
  5764. ;; Comparison operators when matching properties.
  5765. (should
  5766. (org-test-with-temp-text
  5767. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5768. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5769. (search-forward "H1")
  5770. (org-invisible-p2)))
  5771. (should-not
  5772. (org-test-with-temp-text
  5773. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5774. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5775. (search-forward "H2")
  5776. (org-invisible-p2)))
  5777. ;; Regexp match on properties values.
  5778. (should-not
  5779. (org-test-with-temp-text
  5780. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5781. (org-match-sparse-tree nil "A={f.*}")
  5782. (search-forward "H1")
  5783. (org-invisible-p2)))
  5784. (should
  5785. (org-test-with-temp-text
  5786. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5787. (org-match-sparse-tree nil "A={f.*}")
  5788. (search-forward "H2")
  5789. (org-invisible-p2)))
  5790. ;; With an optional argument, limit match to TODO entries.
  5791. (should-not
  5792. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5793. (org-match-sparse-tree t "tag")
  5794. (search-forward "H1")
  5795. (org-invisible-p2)))
  5796. (should
  5797. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5798. (org-match-sparse-tree t "tag")
  5799. (search-forward "H2")
  5800. (org-invisible-p2))))
  5801. (ert-deftest test-org/occur ()
  5802. "Test `org-occur' specifications."
  5803. ;; Count number of matches.
  5804. (should
  5805. (= 1
  5806. (org-test-with-temp-text "* H\nA\n* H2"
  5807. (org-occur "A"))))
  5808. (should
  5809. (= 2
  5810. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5811. (org-occur "A"))))
  5812. ;; Test CALLBACK optional argument.
  5813. (should
  5814. (= 0
  5815. (org-test-with-temp-text "* H\nA\n* H2"
  5816. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5817. (should
  5818. (= 1
  5819. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5820. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5821. ;; Case-fold searches according to `org-occur-case-fold-search'.
  5822. (should
  5823. (= 2
  5824. (org-test-with-temp-text "Aa"
  5825. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  5826. (should
  5827. (= 2
  5828. (org-test-with-temp-text "Aa"
  5829. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  5830. (should
  5831. (= 1
  5832. (org-test-with-temp-text "Aa"
  5833. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  5834. (should
  5835. (= 1
  5836. (org-test-with-temp-text "Aa"
  5837. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  5838. (should
  5839. (= 1
  5840. (org-test-with-temp-text "Aa"
  5841. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  5842. (should
  5843. (= 2
  5844. (org-test-with-temp-text "Aa"
  5845. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  5846. ;;; Tags
  5847. (ert-deftest test-org/tag-string-to-alist ()
  5848. "Test `org-tag-string-to-alist' specifications."
  5849. ;; Tag without selection key.
  5850. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  5851. ;; Tag with selection key.
  5852. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  5853. ;; Tag group.
  5854. (should
  5855. (equal
  5856. (org-tag-string-to-alist "[ group : t1 t2 ]")
  5857. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  5858. ;; Mutually exclusive tags.
  5859. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  5860. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  5861. (should
  5862. (equal
  5863. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  5864. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  5865. (ert-deftest test-org/tag-alist-to-string ()
  5866. "Test `org-tag-alist-to-string' specifications."
  5867. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  5868. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  5869. (should
  5870. (equal
  5871. (org-tag-alist-to-string
  5872. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5873. "[ group : t1 t2 ]"))
  5874. (should
  5875. (equal (org-tag-alist-to-string
  5876. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  5877. "{ tag1 tag2 }"))
  5878. (should
  5879. (equal
  5880. (org-tag-alist-to-string
  5881. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  5882. "{ group : tag1 tag2 }")))
  5883. (ert-deftest test-org/tag-alist-to-groups ()
  5884. "Test `org-tag-alist-to-groups' specifications."
  5885. (should
  5886. (equal (org-tag-alist-to-groups
  5887. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  5888. '(("group" "t1" "t2"))))
  5889. (should
  5890. (equal
  5891. (org-tag-alist-to-groups
  5892. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5893. '(("group" "t1" "t2"))))
  5894. (should-not
  5895. (org-tag-alist-to-groups
  5896. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  5897. (ert-deftest test-org/tag-align ()
  5898. "Test tags alignment."
  5899. ;; Test aligning tags with different display width.
  5900. (should
  5901. ;; 12345678901234567890
  5902. (equal "* Test :abc:"
  5903. (org-test-with-temp-text "* Test :abc:"
  5904. (let ((org-tags-column -20)
  5905. (indent-tabs-mode nil))
  5906. (org-fix-tags-on-the-fly))
  5907. (buffer-string))))
  5908. (should
  5909. ;; 12345678901234567890
  5910. (equal "* Test :日本語:"
  5911. (org-test-with-temp-text "* Test :日本語:"
  5912. (let ((org-tags-column -20)
  5913. (indent-tabs-mode nil))
  5914. (org-fix-tags-on-the-fly))
  5915. (buffer-string))))
  5916. ;; Make sure aligning tags do not skip invisible text.
  5917. (should
  5918. (equal "* [[linkx]] :tag:"
  5919. (org-test-with-temp-text "* [[link<point>]] :tag:"
  5920. (let ((org-tags-column 0))
  5921. (org-fix-tags-on-the-fly)
  5922. (insert "x")
  5923. (buffer-string)))))
  5924. ;; Aligning tags preserve position.
  5925. (should
  5926. (= 6 (org-test-with-temp-text "* 345 <point> :tag:"
  5927. (let ((org-tags-column 78)
  5928. (indent-tabs-mode nil))
  5929. (org-fix-tags-on-the-fly))
  5930. (current-column))))
  5931. ;; Aligning all tags in visible buffer.
  5932. (should
  5933. ;; 12345678901234567890
  5934. (equal (concat "* Level 1 :abc:\n"
  5935. "** Level 2 :def:")
  5936. (org-test-with-temp-text (concat "* Level 1 :abc:\n"
  5937. "** Level 2 :def:")
  5938. (let ((org-tags-column -20)
  5939. (indent-tabs-mode nil))
  5940. ;; (org-align-tags :all) must work even when the point
  5941. ;; is at the end of the buffer.
  5942. (goto-char (point-max))
  5943. (org-align-tags :all))
  5944. (buffer-string)))))
  5945. (ert-deftest test-org/get-tags ()
  5946. "Test `org-get-tags' specifications."
  5947. ;; Standard test.
  5948. (should
  5949. (equal '("foo")
  5950. (org-test-with-temp-text "* Test :foo:" (org-get-tags))))
  5951. (should
  5952. (equal '("foo" "bar")
  5953. (org-test-with-temp-text "* Test :foo:bar:" (org-get-tags))))
  5954. ;; Return nil when there is no tag.
  5955. (should-not
  5956. (org-test-with-temp-text "* Test" (org-get-tags)))
  5957. ;; Tags are inherited from parent headlines.
  5958. (should
  5959. (equal '("tag")
  5960. (let ((org-use-tag-inheritance t))
  5961. (org-test-with-temp-text "* H0 :foo:\n* H1 :tag:\n<point>** H2"
  5962. (org-get-tags)))))
  5963. ;; Tags are inherited from `org-file-tags'.
  5964. (should
  5965. (equal '("tag")
  5966. (org-test-with-temp-text "* H1"
  5967. (let ((org-file-tags '("tag"))
  5968. (org-use-tag-inheritance t))
  5969. (org-get-tags)))))
  5970. ;; Only inherited tags have the `inherited' text property.
  5971. (should
  5972. (get-text-property 0 'inherited
  5973. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5974. (let ((org-use-tag-inheritance t))
  5975. (assoc-string "foo" (org-get-tags))))))
  5976. (should-not
  5977. (get-text-property 0 'inherited
  5978. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5979. (let ((org-use-tag-inheritance t))
  5980. (assoc-string "bar" (org-get-tags))))))
  5981. ;; Obey to `org-use-tag-inheritance'.
  5982. (should-not
  5983. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5984. (let ((org-use-tag-inheritance nil))
  5985. (assoc-string "foo" (org-get-tags)))))
  5986. (should-not
  5987. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5988. (let ((org-use-tag-inheritance nil)
  5989. (org-file-tags '("foo")))
  5990. (assoc-string "foo" (org-get-tags)))))
  5991. (should-not
  5992. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5993. (let ((org-use-tag-inheritance '("bar")))
  5994. (assoc-string "foo" (org-get-tags)))))
  5995. (should
  5996. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5997. (let ((org-use-tag-inheritance '("bar")))
  5998. (assoc-string "bar" (org-get-tags)))))
  5999. (should-not
  6000. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6001. (let ((org-use-tag-inheritance "b.*"))
  6002. (assoc-string "foo" (org-get-tags)))))
  6003. (should
  6004. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6005. (let ((org-use-tag-inheritance "b.*"))
  6006. (assoc-string "bar" (org-get-tags)))))
  6007. ;; When optional argument LOCAL is non-nil, ignore tag inheritance.
  6008. (should
  6009. (equal '("baz")
  6010. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6011. (let ((org-use-tag-inheritance t))
  6012. (org-get-tags nil t)))))
  6013. ;; When optional argument POS is non-nil, get tags there instead.
  6014. (should
  6015. (equal '("foo")
  6016. (org-test-with-temp-text "* H1 :foo:\n* <point>H2 :bar:"
  6017. (org-get-tags 1))))
  6018. ;; Make sure tags excluded from inheritance are returned if local
  6019. (should
  6020. (equal '("foo")
  6021. (org-test-with-temp-text "* Test :foo:"
  6022. (let ((org-use-tag-inheritance t)
  6023. (org-tags-exclude-from-inheritance '("foo")))
  6024. (org-get-tags)))))
  6025. ;; Test the collection of tags from #+filetags and parent tags.
  6026. (should
  6027. (equal '("a" "b" "c" "d")
  6028. (org-test-with-temp-text (concat "#+filetags: a\n"
  6029. "* Level 1 :b:\n"
  6030. "** Level 2 :c:\n"
  6031. "*** Level 3 :d:\n"
  6032. "<point>")
  6033. (let ((org-use-tag-inheritance t))
  6034. (org-mode-restart) ;So that `org-file-tags' get populated from #+filetags
  6035. (org-get-tags)))))
  6036. ;; Pathological case: tagged headline with an empty body.
  6037. (should (org-test-with-temp-text "* :tag:" (org-get-tags))))
  6038. (ert-deftest test-org/set-tags ()
  6039. "Test `org-set-tags' specifications."
  6040. ;; Throw an error on invalid data.
  6041. (should-error
  6042. (org-test-with-temp-text "* H"
  6043. (org-set-tags 'foo)))
  6044. ;; `nil', an empty, and a blank string remove all tags.
  6045. (should
  6046. (equal "* H"
  6047. (org-test-with-temp-text "* H :tag1:tag2:"
  6048. (org-set-tags nil)
  6049. (buffer-string))))
  6050. (should
  6051. (equal "* H"
  6052. (org-test-with-temp-text "* H :tag1:tag2:"
  6053. (org-set-tags "")
  6054. (buffer-string))))
  6055. (should
  6056. (equal "* H"
  6057. (org-test-with-temp-text "* H :tag1:tag2:"
  6058. (org-set-tags " ")
  6059. (buffer-string))))
  6060. ;; If there's nothing to remove, just bail out.
  6061. (should
  6062. (equal "* H"
  6063. (org-test-with-temp-text "* H"
  6064. (org-set-tags nil)
  6065. (buffer-string))))
  6066. (should
  6067. (equal "* "
  6068. (org-test-with-temp-text "* "
  6069. (org-set-tags nil)
  6070. (buffer-string))))
  6071. ;; If DATA is a tag string, set current tags to it, even if it means
  6072. ;; replacing old tags.
  6073. (should
  6074. (equal "* H :tag0:"
  6075. (org-test-with-temp-text "* H :tag1:tag2:"
  6076. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  6077. (buffer-string))))
  6078. (should
  6079. (equal "* H :tag0:"
  6080. (org-test-with-temp-text "* H"
  6081. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  6082. (buffer-string))))
  6083. ;; If DATA is a list, set tags to this list, even if it means
  6084. ;; replacing old tags.
  6085. (should
  6086. (equal "* H :tag0:"
  6087. (org-test-with-temp-text "* H :tag1:tag2:"
  6088. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6089. (buffer-string))))
  6090. (should
  6091. (equal "* H :tag0:"
  6092. (org-test-with-temp-text "* H"
  6093. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6094. (buffer-string))))
  6095. ;; When set, apply `org-tags-sort-function'.
  6096. (should
  6097. (equal "* H :a:b:"
  6098. (org-test-with-temp-text "* H"
  6099. (let ((org-tags-column 1)
  6100. (org-tags-sort-function #'string<))
  6101. (org-set-tags '("b" "a"))
  6102. (buffer-string)))))
  6103. ;; When new tags are identical to the previous ones, still align.
  6104. (should
  6105. (equal "* H :foo:"
  6106. (org-test-with-temp-text "* H :foo:"
  6107. (let ((org-tags-column 1))
  6108. (org-set-tags '("foo"))
  6109. (buffer-string)))))
  6110. ;; When tags have been changed, run `org-after-tags-change-hook'.
  6111. (should
  6112. (catch :return
  6113. (org-test-with-temp-text "* H :foo:"
  6114. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6115. (org-set-tags '("bar"))
  6116. nil))))
  6117. (should-not
  6118. (catch :return
  6119. (org-test-with-temp-text "* H :foo:"
  6120. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6121. (org-set-tags '("foo"))
  6122. nil))))
  6123. ;; Special case: handle empty headlines.
  6124. (should
  6125. (equal "* :tag0:"
  6126. (org-test-with-temp-text "* "
  6127. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6128. (buffer-string))))
  6129. ;; Modify buffer only when a tag change happens or alignment is
  6130. ;; done.
  6131. (should-not
  6132. (org-test-with-temp-text "* H :foo:"
  6133. (set-buffer-modified-p nil)
  6134. (let ((org-tags-column 1)) (org-set-tags '("foo")))
  6135. (buffer-modified-p)))
  6136. (should
  6137. (org-test-with-temp-text "* H :foo:"
  6138. (set-buffer-modified-p nil)
  6139. (let ((org-tags-column 10)) (org-set-tags '("foo")))
  6140. (buffer-modified-p)))
  6141. (should
  6142. (org-test-with-temp-text "* H :foo:"
  6143. (set-buffer-modified-p nil)
  6144. (let ((org-tags-column 10)) (org-set-tags '("bar")))
  6145. (buffer-modified-p)))
  6146. ;; Pathological case: when setting tags of a folded headline, do not
  6147. ;; let new tags being sucked into invisibility.
  6148. (should-not
  6149. (org-test-with-temp-text "* H1\nContent\n* H2\n\n Other Content"
  6150. ;; Show only headlines
  6151. (org-content)
  6152. ;; Set NEXT tag on current entry
  6153. (org-set-tags ":NEXT:")
  6154. ;; Move point to that NEXT tag
  6155. (search-forward "NEXT") (backward-word)
  6156. ;; And it should be visible (i.e. no overlays)
  6157. (overlays-at (point)))))
  6158. (ert-deftest test-org/set-tags-command ()
  6159. "Test `org-set-tags-command' specifications"
  6160. ;; Set tags at current headline.
  6161. (should
  6162. (equal "* H1 :foo:"
  6163. (org-test-with-temp-text "* H1"
  6164. (cl-letf (((symbol-function 'completing-read)
  6165. (lambda (&rest args) ":foo:")))
  6166. (let ((org-use-fast-tag-selection nil)
  6167. (org-tags-column 1))
  6168. (org-set-tags-command)))
  6169. (buffer-string))))
  6170. ;; Preserve position when called from the section below.
  6171. (should
  6172. (equal "* H1 :foo:\nContents"
  6173. (org-test-with-temp-text "* H1\n<point>Contents"
  6174. (cl-letf (((symbol-function 'completing-read)
  6175. (lambda (&rest args) ":foo:")))
  6176. (let ((org-use-fast-tag-selection nil)
  6177. (org-tags-column 1))
  6178. (org-set-tags-command)))
  6179. (buffer-string))))
  6180. (should-not
  6181. (equal "* H1 :foo:\nContents2"
  6182. (org-test-with-temp-text "* H1\n<point>Contents2"
  6183. (cl-letf (((symbol-function 'completing-read)
  6184. (lambda (&rest args) ":foo:")))
  6185. (let ((org-use-fast-tag-selection nil)
  6186. (org-tags-column 1))
  6187. (org-set-tags-command)))
  6188. (org-at-heading-p))))
  6189. ;; Strip all forbidden characters from user-entered tags.
  6190. (should
  6191. (equal "* H1 :foo:"
  6192. (org-test-with-temp-text "* H1"
  6193. (cl-letf (((symbol-function 'completing-read)
  6194. (lambda (&rest args) ": foo *:")))
  6195. (let ((org-use-fast-tag-selection nil)
  6196. (org-tags-column 1))
  6197. (org-set-tags-command)))
  6198. (buffer-string))))
  6199. ;; When a region is active and
  6200. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  6201. ;; same value in all headlines in region.
  6202. (should
  6203. (equal "* H1 :foo:\nContents\n* H2 :foo:"
  6204. (org-test-with-temp-text "* H1\nContents\n* H2"
  6205. (cl-letf (((symbol-function 'completing-read)
  6206. (lambda (&rest args) ":foo:")))
  6207. (let ((org-use-fast-tag-selection nil)
  6208. (org-loop-over-headlines-in-active-region t)
  6209. (org-tags-column 1))
  6210. (transient-mark-mode 1)
  6211. (push-mark (point) t t)
  6212. (goto-char (point-max))
  6213. (org-set-tags-command)))
  6214. (buffer-string))))
  6215. (should
  6216. (equal "* H1\nContents\n* H2 :foo:"
  6217. (org-test-with-temp-text "* H1\nContents\n* H2"
  6218. (cl-letf (((symbol-function 'completing-read)
  6219. (lambda (&rest args) ":foo:")))
  6220. (let ((org-use-fast-tag-selection nil)
  6221. (org-loop-over-headlines-in-active-region nil)
  6222. (org-tags-column 1))
  6223. (transient-mark-mode 1)
  6224. (push-mark (point) t t)
  6225. (goto-char (point-max))
  6226. (org-set-tags-command)))
  6227. (buffer-string))))
  6228. ;; With a C-u prefix argument, align all tags in the buffer.
  6229. (should
  6230. (equal "* H1 :foo:\n* H2 :bar:"
  6231. (org-test-with-temp-text "* H1 :foo:\n* H2 :bar:"
  6232. (let ((org-tags-column 1)) (org-set-tags-command '(4)))
  6233. (buffer-string)))))
  6234. (ert-deftest test-org/toggle-tag ()
  6235. "Test `org-toggle-tag' specifications."
  6236. ;; Insert missing tag.
  6237. (should
  6238. (equal "* H :tag:"
  6239. (org-test-with-temp-text "* H"
  6240. (let ((org-tags-column 1)) (org-toggle-tag "tag"))
  6241. (buffer-string))))
  6242. (should
  6243. (equal "* H :tag1:tag2:"
  6244. (org-test-with-temp-text "* H :tag1:"
  6245. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6246. (buffer-string))))
  6247. ;; Remove existing tag.
  6248. (should
  6249. (equal "* H"
  6250. (org-test-with-temp-text "* H :tag:"
  6251. (org-toggle-tag "tag")
  6252. (buffer-string))))
  6253. (should
  6254. (equal "* H :tag1:"
  6255. (org-test-with-temp-text "* H :tag1:tag2:"
  6256. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6257. (buffer-string))))
  6258. (should
  6259. (equal "* H :tag2:"
  6260. (org-test-with-temp-text "* H :tag1:tag2:"
  6261. (let ((org-tags-column 1)) (org-toggle-tag "tag1"))
  6262. (buffer-string))))
  6263. ;; With optional argument ONOFF set to `on', try to insert the tag,
  6264. ;; even if its already there.
  6265. (should
  6266. (equal "* H :tag:"
  6267. (org-test-with-temp-text "* H"
  6268. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6269. (buffer-string))))
  6270. (should
  6271. (equal "* H :tag:"
  6272. (org-test-with-temp-text "* H :tag:"
  6273. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6274. (buffer-string))))
  6275. ;; With optional argument ONOFF set to `off', try to remove the tag,
  6276. ;; even if its not there.
  6277. (should
  6278. (equal "* H"
  6279. (org-test-with-temp-text "* H :tag:"
  6280. (org-toggle-tag "tag" 'off)
  6281. (buffer-string))))
  6282. (should
  6283. (equal "* H :tag:"
  6284. (org-test-with-temp-text "* H :tag:"
  6285. (let ((org-tags-column 1)) (org-toggle-tag "foo" 'off))
  6286. (buffer-string))))
  6287. ;; Special case: Handle properly tag inheritance. In particular, do
  6288. ;; not set inherited tags.
  6289. (should
  6290. (equal "* H1 :tag:\n** H2 :tag2:tag:"
  6291. (org-test-with-temp-text "* H1 :tag:\n** <point>H2 :tag2:"
  6292. (let ((org-use-tag-inheritance t)
  6293. (org-tags-column 1))
  6294. (org-toggle-tag "tag"))
  6295. (buffer-string))))
  6296. (should
  6297. (equal "* H1 :tag1:tag2:\n** H2 :foo:"
  6298. (org-test-with-temp-text "* H1 :tag1:tag2:\n** <point>H2"
  6299. (let ((org-use-tag-inheritance t)
  6300. (org-tags-column 1))
  6301. (org-toggle-tag "foo"))
  6302. (buffer-string)))))
  6303. (ert-deftest test-org/tags-expand ()
  6304. "Test `org-tags-expand' specifications."
  6305. ;; Expand tag groups as a regexp enclosed withing curly brackets.
  6306. (should
  6307. (equal "{\\<[ABC]\\>}"
  6308. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6309. (org-mode-restart)
  6310. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  6311. (should
  6312. (equal "{\\<\\(?:Aa\\|Bb\\|Cc\\)\\>}"
  6313. (org-test-with-temp-text "#+TAGS: [ Aa : Bb Cc ]"
  6314. (org-mode-restart)
  6315. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "Aa")))))
  6316. ;; Preserve operator before the regexp.
  6317. (should
  6318. (equal "+{\\<[ABC]\\>}"
  6319. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6320. (org-mode-restart)
  6321. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "+A")))))
  6322. (should
  6323. (equal "-{\\<[ABC]\\>}"
  6324. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6325. (org-mode-restart)
  6326. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "-A")))))
  6327. ;; Handle "|" syntax.
  6328. (should
  6329. (equal "{\\<[ABC]\\>}|D"
  6330. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6331. (org-mode-restart)
  6332. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|D")))))
  6333. ;; Handle nested groups.
  6334. (should
  6335. (equal "{\\<[A-D]\\>}"
  6336. (org-test-with-temp-text "#+TAGS: [ A : B C ]\n#+TAGS: [ B : D ]"
  6337. (org-mode-restart)
  6338. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  6339. ;; Expand multiple occurrences of the same group.
  6340. (should
  6341. (equal "{\\<[ABC]\\>}|{\\<[ABC]\\>}"
  6342. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6343. (org-mode-restart)
  6344. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|A")))))
  6345. ;; Preserve regexp matches.
  6346. (should
  6347. (equal "{A+}"
  6348. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6349. (org-mode-restart)
  6350. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "{A+}"))))))
  6351. ;;; TODO keywords
  6352. (ert-deftest test-org/auto-repeat-maybe ()
  6353. "Test `org-auto-repeat-maybe' specifications."
  6354. ;; Do not auto repeat when there is no valid time stamp with
  6355. ;; a repeater in the entry.
  6356. (should-not
  6357. (string-prefix-p
  6358. "* TODO H"
  6359. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6360. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  6361. (org-todo "DONE")
  6362. (buffer-string)))))
  6363. (should-not
  6364. (string-prefix-p
  6365. "* TODO H"
  6366. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6367. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  6368. (org-todo "DONE")
  6369. (buffer-string)))))
  6370. ;; When switching to DONE state, switch back to first TODO keyword
  6371. ;; in sequence, or the same keyword if they have different types.
  6372. (should
  6373. (string-prefix-p
  6374. "* TODO H"
  6375. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6376. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6377. (org-todo "DONE")
  6378. (buffer-string)))))
  6379. (should
  6380. (string-prefix-p
  6381. "* KWD1 H"
  6382. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6383. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6384. (org-todo "DONE")
  6385. (buffer-string)))))
  6386. (should
  6387. (string-prefix-p
  6388. "* KWD2 H"
  6389. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  6390. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6391. (org-todo "DONE")
  6392. (buffer-string)))))
  6393. ;; If there was no TODO keyword in the first place, do not insert
  6394. ;; any either.
  6395. (should
  6396. (string-prefix-p
  6397. "* H"
  6398. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6399. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  6400. (org-todo "DONE")
  6401. (buffer-string)))))
  6402. ;; Revert to REPEAT_TO_STATE, if set.
  6403. (should
  6404. (string-prefix-p
  6405. "* KWD2 H"
  6406. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6407. (org-test-with-temp-text
  6408. "* KWD2 H
  6409. :PROPERTIES:
  6410. :REPEAT_TO_STATE: KWD2
  6411. :END:
  6412. <2012-03-29 Thu +2y>"
  6413. (org-todo "DONE")
  6414. (buffer-string)))))
  6415. ;; When switching to DONE state, update base date. If there are
  6416. ;; multiple repeated time stamps, update them all.
  6417. (should
  6418. (string-match-p
  6419. "<2014-03-29 .* \\+2y>"
  6420. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6421. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6422. (org-todo "DONE")
  6423. (buffer-string)))))
  6424. (should
  6425. (string-match-p
  6426. "<2015-03-04 .* \\+1y>"
  6427. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6428. (org-test-with-temp-text
  6429. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  6430. (org-todo "DONE")
  6431. (buffer-string)))))
  6432. ;; Throw an error if repeater unit is the hour and no time is
  6433. ;; provided in the time-stamp.
  6434. (should-error
  6435. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6436. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  6437. (org-todo "DONE")
  6438. (buffer-string))))
  6439. ;; Do not repeat inactive time stamps with a repeater.
  6440. (should-not
  6441. (string-match-p
  6442. "\\[2014-03-29 .* \\+2y\\]"
  6443. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6444. (org-test-with-temp-text
  6445. "* TODO H\n[2012-03-29 Thu. +2y]"
  6446. (org-todo "DONE")
  6447. (buffer-string)))))
  6448. ;; Do not repeat commented time stamps.
  6449. (should-not
  6450. (string-prefix-p
  6451. "<2015-03-04 .* \\+1y>"
  6452. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6453. (org-test-with-temp-text
  6454. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  6455. (org-todo "DONE")
  6456. (buffer-string)))))
  6457. (should-not
  6458. (string-prefix-p
  6459. "<2015-03-04 .* \\+1y>"
  6460. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6461. (org-test-with-temp-text
  6462. "* TODO H
  6463. <2012-03-29 Thu. +2y>
  6464. #+BEGIN_EXAMPLE
  6465. <2014-03-04 Tue +1y>
  6466. #+END_EXAMPLE"
  6467. (org-todo "DONE")
  6468. (buffer-string)))))
  6469. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  6470. ;; entry, record time of last repeat.
  6471. (should-not
  6472. (string-match-p
  6473. ":LAST_REPEAT:"
  6474. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6475. (org-log-repeat nil))
  6476. (cl-letf (((symbol-function 'org-add-log-setup)
  6477. (lambda (&rest args) nil)))
  6478. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6479. (org-todo "DONE")
  6480. (buffer-string))))))
  6481. (should
  6482. (string-match-p
  6483. ":LAST_REPEAT:"
  6484. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6485. (org-log-repeat t))
  6486. (cl-letf (((symbol-function 'org-add-log-setup)
  6487. (lambda (&rest args) nil)))
  6488. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6489. (org-todo "DONE")
  6490. (buffer-string))))))
  6491. (should
  6492. (string-match-p
  6493. ":LAST_REPEAT:"
  6494. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6495. (cl-letf (((symbol-function 'org-add-log-setup)
  6496. (lambda (&rest args) nil)))
  6497. (org-test-with-temp-text
  6498. "* TODO H\n<2012-03-29 Thu +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  6499. (org-todo "DONE")
  6500. (buffer-string))))))
  6501. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  6502. ;; the entry as it is no longer relevant.
  6503. (should-not
  6504. (string-match-p
  6505. "^SCHEDULED:"
  6506. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6507. (org-test-with-temp-text
  6508. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  6509. (org-todo "DONE")
  6510. (buffer-string)))))
  6511. ;; Properly advance repeater even when a clock entry is specified
  6512. ;; and `org-log-repeat' is nil.
  6513. (should
  6514. (string-match-p
  6515. "SCHEDULED: <2014-03-29"
  6516. (let ((org-log-repeat nil)
  6517. (org-todo-keywords '((sequence "TODO" "DONE"))))
  6518. (org-test-with-temp-text
  6519. "* TODO H
  6520. SCHEDULED: <2012-03-29 Thu +2y>
  6521. CLOCK: [2012-03-29 Thu 10:00]--[2012-03-29 Thu 16:40] => 6:40"
  6522. (org-todo "DONE")
  6523. (buffer-string))))))
  6524. ;;; Timestamps API
  6525. (ert-deftest test-org/at-timestamp-p ()
  6526. "Test `org-at-timestamp-p' specifications."
  6527. (should
  6528. (org-test-with-temp-text "<2012-03-29 Thu>"
  6529. (org-at-timestamp-p)))
  6530. (should-not
  6531. (org-test-with-temp-text "2012-03-29 Thu"
  6532. (org-at-timestamp-p)))
  6533. ;; Test return values.
  6534. (should
  6535. (eq 'bracket
  6536. (org-test-with-temp-text "<2012-03-29 Thu>"
  6537. (org-at-timestamp-p))))
  6538. (should
  6539. (eq 'year
  6540. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  6541. (org-at-timestamp-p))))
  6542. (should
  6543. (eq 'month
  6544. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  6545. (org-at-timestamp-p))))
  6546. (should
  6547. (eq 'day
  6548. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  6549. (org-at-timestamp-p))))
  6550. (should
  6551. (eq 'day
  6552. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  6553. (org-at-timestamp-p))))
  6554. (should
  6555. (wholenump
  6556. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  6557. (org-at-timestamp-p))))
  6558. (should
  6559. (eq 'bracket
  6560. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  6561. (org-at-timestamp-p))))
  6562. (should
  6563. (eq 'after
  6564. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  6565. (org-at-timestamp-p))))
  6566. ;; Test `inactive' optional argument.
  6567. (should
  6568. (org-test-with-temp-text "[2012-03-29 Thu]"
  6569. (org-at-timestamp-p 'inactive)))
  6570. (should-not
  6571. (org-test-with-temp-text "[2012-03-29 Thu]"
  6572. (org-at-timestamp-p)))
  6573. ;; When optional argument is `agenda', recognize time-stamps in
  6574. ;; planning info line, property drawers and clocks.
  6575. (should
  6576. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6577. (org-at-timestamp-p 'agenda)))
  6578. (should-not
  6579. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6580. (org-at-timestamp-p)))
  6581. (should
  6582. (org-test-with-temp-text
  6583. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6584. (org-at-timestamp-p 'agenda)))
  6585. (should-not
  6586. (org-test-with-temp-text
  6587. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6588. (org-at-timestamp-p)))
  6589. (should
  6590. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6591. (let ((org-agenda-include-inactive-timestamps t))
  6592. (org-at-timestamp-p 'agenda))))
  6593. (should-not
  6594. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6595. (let ((org-agenda-include-inactive-timestamps t))
  6596. (org-at-timestamp-p))))
  6597. (should-not
  6598. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6599. (let ((org-agenda-include-inactive-timestamps t))
  6600. (org-at-timestamp-p 'inactive))))
  6601. ;; When optional argument is `lax', match any part of the document
  6602. ;; with Org timestamp syntax.
  6603. (should
  6604. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6605. (org-at-timestamp-p 'lax)))
  6606. (should-not
  6607. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6608. (org-at-timestamp-p)))
  6609. (should
  6610. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6611. (org-at-timestamp-p 'lax)))
  6612. (should-not
  6613. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6614. (org-at-timestamp-p)))
  6615. (should
  6616. (org-test-with-temp-text
  6617. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6618. (org-at-timestamp-p 'lax)))
  6619. (should-not
  6620. (org-test-with-temp-text
  6621. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6622. (org-at-timestamp-p)))
  6623. ;; Optional argument `lax' also matches inactive timestamps.
  6624. (should
  6625. (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
  6626. (org-at-timestamp-p 'lax))))
  6627. (ert-deftest test-org/time-stamp ()
  6628. "Test `org-time-stamp' specifications."
  6629. ;; Insert chosen time stamp at point.
  6630. (should
  6631. (string-match
  6632. "Te<2014-03-04 .*?>xt"
  6633. (org-test-with-temp-text "Te<point>xt"
  6634. (cl-letf (((symbol-function 'org-read-date)
  6635. (lambda (&rest args)
  6636. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6637. (org-time-stamp nil)
  6638. (buffer-string)))))
  6639. ;; With a prefix argument, also insert time.
  6640. (should
  6641. (string-match
  6642. "Te<2014-03-04 .*? 00:41>xt"
  6643. (org-test-with-temp-text "Te<point>xt"
  6644. (cl-letf (((symbol-function 'org-read-date)
  6645. (lambda (&rest args)
  6646. (apply #'encode-time
  6647. (org-parse-time-string "2014-03-04 00:41")))))
  6648. (org-time-stamp '(4))
  6649. (buffer-string)))))
  6650. ;; With two universal prefix arguments, insert an active timestamp
  6651. ;; with the current time without prompting the user.
  6652. (should
  6653. (string-match
  6654. "Te<2014-03-04 .*? 00:41>xt"
  6655. (org-test-with-temp-text "Te<point>xt"
  6656. (org-test-at-time "2014-03-04 00:41"
  6657. (org-time-stamp '(16))
  6658. (buffer-string)))))
  6659. ;; When optional argument is non-nil, insert an inactive timestamp.
  6660. (should
  6661. (string-match
  6662. "Te\\[2014-03-04 .*?\\]xt"
  6663. (org-test-with-temp-text "Te<point>xt"
  6664. (cl-letf (((symbol-function 'org-read-date)
  6665. (lambda (&rest args)
  6666. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6667. (org-time-stamp nil t)
  6668. (buffer-string)))))
  6669. ;; When called from a timestamp, replace existing one.
  6670. (should
  6671. (string-match
  6672. "<2014-03-04 .*?>"
  6673. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  6674. (cl-letf (((symbol-function 'org-read-date)
  6675. (lambda (&rest args)
  6676. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6677. (org-time-stamp nil)
  6678. (buffer-string)))))
  6679. (should
  6680. (string-match
  6681. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  6682. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  6683. (cl-letf (((symbol-function 'org-read-date)
  6684. (lambda (&rest args)
  6685. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6686. (org-time-stamp nil)
  6687. (buffer-string)))))
  6688. ;; When replacing a timestamp, preserve repeater, if any.
  6689. (should
  6690. (string-match
  6691. "<2014-03-04 .*? \\+2y>"
  6692. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  6693. (cl-letf (((symbol-function 'org-read-date)
  6694. (lambda (&rest args)
  6695. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6696. (org-time-stamp nil)
  6697. (buffer-string)))))
  6698. ;; When called twice in a raw, build a date range.
  6699. (should
  6700. (string-match
  6701. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  6702. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  6703. (cl-letf (((symbol-function 'org-read-date)
  6704. (lambda (&rest args)
  6705. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6706. (let ((last-command 'org-time-stamp)
  6707. (this-command 'org-time-stamp))
  6708. (org-time-stamp nil))
  6709. (buffer-string))))))
  6710. (ert-deftest test-org/timestamp-has-time-p ()
  6711. "Test `org-timestamp-has-time-p' specifications."
  6712. ;; With time.
  6713. (should
  6714. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6715. (org-timestamp-has-time-p (org-element-context))))
  6716. ;; Without time.
  6717. (should-not
  6718. (org-test-with-temp-text "<2012-03-29 Thu>"
  6719. (org-timestamp-has-time-p (org-element-context)))))
  6720. (ert-deftest test-org/get-repeat ()
  6721. "Test `org-get-repeat' specifications."
  6722. (should
  6723. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  6724. (org-get-repeat)))
  6725. (should-not
  6726. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  6727. (org-get-repeat)))
  6728. ;; Return proper repeat string.
  6729. (should
  6730. (equal "+2y"
  6731. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  6732. (org-get-repeat))))
  6733. ;; Prevent false positive (commented or verbatim time stamps)
  6734. (should-not
  6735. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  6736. (org-get-repeat)))
  6737. (should-not
  6738. (org-test-with-temp-text
  6739. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  6740. (org-get-repeat)))
  6741. ;; Return nil when called before first heading.
  6742. (should-not
  6743. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  6744. (org-get-repeat)))
  6745. ;; When called with an optional argument, extract repeater from that
  6746. ;; string instead.
  6747. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  6748. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  6749. (ert-deftest test-org/timestamp-format ()
  6750. "Test `org-timestamp-format' specifications."
  6751. ;; Regular test.
  6752. (should
  6753. (equal
  6754. "2012-03-29 16:40"
  6755. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6756. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  6757. ;; Range end.
  6758. (should
  6759. (equal
  6760. "2012-03-29"
  6761. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  6762. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  6763. (ert-deftest test-org/timestamp-split-range ()
  6764. "Test `org-timestamp-split-range' specifications."
  6765. ;; Extract range start (active).
  6766. (should
  6767. (equal '(2012 3 29)
  6768. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6769. (let ((ts (org-timestamp-split-range (org-element-context))))
  6770. (mapcar (lambda (p) (org-element-property p ts))
  6771. '(:year-end :month-end :day-end))))))
  6772. ;; Extract range start (inactive)
  6773. (should
  6774. (equal '(2012 3 29)
  6775. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6776. (let ((ts (org-timestamp-split-range (org-element-context))))
  6777. (mapcar (lambda (p) (org-element-property p ts))
  6778. '(:year-end :month-end :day-end))))))
  6779. ;; Extract range end (active).
  6780. (should
  6781. (equal '(2012 3 30)
  6782. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6783. (let ((ts (org-timestamp-split-range
  6784. (org-element-context) t)))
  6785. (mapcar (lambda (p) (org-element-property p ts))
  6786. '(:year-end :month-end :day-end))))))
  6787. ;; Extract range end (inactive)
  6788. (should
  6789. (equal '(2012 3 30)
  6790. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6791. (let ((ts (org-timestamp-split-range
  6792. (org-element-context) t)))
  6793. (mapcar (lambda (p) (org-element-property p ts))
  6794. '(:year-end :month-end :day-end))))))
  6795. ;; Return the timestamp if not a range.
  6796. (should
  6797. (org-test-with-temp-text "[2012-03-29 Thu]"
  6798. (let* ((ts-orig (org-element-context))
  6799. (ts-copy (org-timestamp-split-range ts-orig)))
  6800. (eq ts-orig ts-copy))))
  6801. (should
  6802. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6803. (let* ((ts-orig (org-element-context))
  6804. (ts-copy (org-timestamp-split-range ts-orig)))
  6805. (eq ts-orig ts-copy)))))
  6806. (ert-deftest test-org/timestamp-translate ()
  6807. "Test `org-timestamp-translate' specifications."
  6808. ;; Translate whole date range.
  6809. (should
  6810. (equal "<29>--<30>"
  6811. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6812. (let ((org-display-custom-times t)
  6813. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6814. (org-timestamp-translate (org-element-context))))))
  6815. ;; Translate date range start.
  6816. (should
  6817. (equal "<29>"
  6818. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6819. (let ((org-display-custom-times t)
  6820. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6821. (org-timestamp-translate (org-element-context) 'start)))))
  6822. ;; Translate date range end.
  6823. (should
  6824. (equal "<30>"
  6825. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6826. (let ((org-display-custom-times t)
  6827. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6828. (org-timestamp-translate (org-element-context) 'end)))))
  6829. ;; Translate time range.
  6830. (should
  6831. (equal "<08>--<16>"
  6832. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  6833. (let ((org-display-custom-times t)
  6834. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  6835. (org-timestamp-translate (org-element-context))))))
  6836. ;; Translate non-range timestamp.
  6837. (should
  6838. (equal "<29>"
  6839. (org-test-with-temp-text "<2012-03-29 Thu>"
  6840. (let ((org-display-custom-times t)
  6841. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6842. (org-timestamp-translate (org-element-context))))))
  6843. ;; Do not change `diary' timestamps.
  6844. (should
  6845. (equal "<%%(org-float t 4 2)>"
  6846. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6847. (let ((org-display-custom-times t)
  6848. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6849. (org-timestamp-translate (org-element-context)))))))
  6850. (ert-deftest test-org/timestamp-from-string ()
  6851. "Test `org-timestamp-from-string' specifications."
  6852. ;; Return nil if argument is not a valid Org timestamp.
  6853. (should-not (org-timestamp-from-string ""))
  6854. (should-not (org-timestamp-from-string nil))
  6855. (should-not (org-timestamp-from-string "<2012-03-29"))
  6856. ;; Otherwise, return a valid Org timestamp object.
  6857. (should
  6858. (equal "<2012-03-29 Thu>"
  6859. (let ((system-time-locale "en_US"))
  6860. (org-element-interpret-data
  6861. (org-timestamp-from-string "<2012-03-29 Thu>")))))
  6862. (should
  6863. (equal "[2014-03-04 Tue]"
  6864. (let ((system-time-locale "en_US"))
  6865. (org-element-interpret-data
  6866. (org-timestamp-from-string "[2014-03-04 Tue]"))))))
  6867. (ert-deftest test-org/timestamp-from-time ()
  6868. "Test `org-timestamp-from-time' specifications."
  6869. ;; Standard test.
  6870. (should
  6871. (equal "<2012-03-29 Thu>"
  6872. (let ((system-time-locale "en_US"))
  6873. (org-element-interpret-data
  6874. (org-timestamp-from-time
  6875. (apply #'encode-time
  6876. (org-parse-time-string "<2012-03-29 Thu 16:40>")))))))
  6877. ;; When optional argument WITH-TIME is non-nil, provide time
  6878. ;; information.
  6879. (should
  6880. (equal "<2012-03-29 Thu 16:40>"
  6881. (let ((system-time-locale "en_US"))
  6882. (org-element-interpret-data
  6883. (org-timestamp-from-time
  6884. (apply #'encode-time
  6885. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  6886. t)))))
  6887. ;; When optional argument INACTIVE is non-nil, return an inactive
  6888. ;; timestamp.
  6889. (should
  6890. (equal "[2012-03-29 Thu]"
  6891. (let ((system-time-locale "en_US"))
  6892. (org-element-interpret-data
  6893. (org-timestamp-from-time
  6894. (apply #'encode-time
  6895. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  6896. nil t))))))
  6897. (ert-deftest test-org/timestamp-to-time ()
  6898. "Test `org-timestamp-to-time' specifications."
  6899. (should
  6900. (equal "2014-03-04"
  6901. (format-time-string
  6902. "%Y-%m-%d"
  6903. (org-timestamp-to-time
  6904. (org-timestamp-from-string "<2014-03-04 Tue>")))))
  6905. (should
  6906. (equal "2014-03-04"
  6907. (format-time-string
  6908. "%Y-%m-%d"
  6909. (org-timestamp-to-time
  6910. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  6911. (should
  6912. (equal "2012-03-29 08:30"
  6913. (format-time-string
  6914. "%Y-%m-%d %H:%M"
  6915. (org-timestamp-to-time
  6916. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")))))
  6917. (should
  6918. (equal "2012-03-29"
  6919. (format-time-string
  6920. "%Y-%m-%d"
  6921. (org-timestamp-to-time
  6922. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")))))
  6923. (should
  6924. (equal "2012-03-29"
  6925. (format-time-string
  6926. "%Y-%m-%d"
  6927. (org-timestamp-to-time
  6928. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")))))
  6929. ;; When optional argument END is non-nil, use end of date range or
  6930. ;; time range.
  6931. (should
  6932. (equal "2012-03-29 16:40"
  6933. (format-time-string
  6934. "%Y-%m-%d %H:%M"
  6935. (org-timestamp-to-time
  6936. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")
  6937. t))))
  6938. (should
  6939. (equal "2014-03-04"
  6940. (format-time-string
  6941. "%Y-%m-%d"
  6942. (org-timestamp-to-time
  6943. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")
  6944. t))))
  6945. (should
  6946. (equal "2014-03-04"
  6947. (format-time-string
  6948. "%Y-%m-%d"
  6949. (org-timestamp-to-time
  6950. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")
  6951. t)))))
  6952. ;;; Visibility
  6953. (ert-deftest test-org/flag-drawer ()
  6954. "Test `org-flag-drawer' specifications."
  6955. ;; Hide drawer.
  6956. (should
  6957. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6958. (org-flag-drawer t)
  6959. (get-char-property (line-end-position) 'invisible)))
  6960. ;; Show drawer.
  6961. (should-not
  6962. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6963. (org-flag-drawer t)
  6964. (org-flag-drawer nil)
  6965. (get-char-property (line-end-position) 'invisible)))
  6966. ;; Test optional argument.
  6967. (should
  6968. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6969. (let ((drawer (save-excursion (search-forward ":D2")
  6970. (org-element-at-point))))
  6971. (org-flag-drawer t drawer)
  6972. (get-char-property (progn (search-forward ":D2") (line-end-position))
  6973. 'invisible))))
  6974. (should-not
  6975. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6976. (let ((drawer (save-excursion (search-forward ":D2")
  6977. (org-element-at-point))))
  6978. (org-flag-drawer t drawer)
  6979. (get-char-property (line-end-position) 'invisible))))
  6980. ;; Do not hide fake drawers.
  6981. (should-not
  6982. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  6983. (forward-line 1)
  6984. (org-flag-drawer t)
  6985. (get-char-property (line-end-position) 'invisible)))
  6986. ;; Do not hide incomplete drawers.
  6987. (should-not
  6988. (org-test-with-temp-text ":D:\nparagraph"
  6989. (forward-line 1)
  6990. (org-flag-drawer t)
  6991. (get-char-property (line-end-position) 'invisible)))
  6992. ;; Do not hide drawers when called from final blank lines.
  6993. (should-not
  6994. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  6995. (goto-char (point-max))
  6996. (org-flag-drawer t)
  6997. (goto-char (point-min))
  6998. (get-char-property (line-end-position) 'invisible)))
  6999. ;; Don't leave point in an invisible part of the buffer when hiding
  7000. ;; a drawer away.
  7001. (should-not
  7002. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  7003. (goto-char (point-max))
  7004. (org-flag-drawer t)
  7005. (get-char-property (point) 'invisible))))
  7006. (ert-deftest test-org/hide-block-toggle ()
  7007. "Test `org-hide-block-toggle' specifications."
  7008. ;; Error when not at a block.
  7009. (should-error
  7010. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  7011. (org-hide-block-toggle 'off)
  7012. (get-char-property (line-end-position) 'invisible)))
  7013. ;; Hide block.
  7014. (should
  7015. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  7016. (org-hide-block-toggle)
  7017. (get-char-property (line-end-position) 'invisible)))
  7018. (should
  7019. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  7020. (org-hide-block-toggle)
  7021. (get-char-property (line-end-position) 'invisible)))
  7022. ;; Show block unconditionally when optional argument is `off'.
  7023. (should-not
  7024. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7025. (org-hide-block-toggle)
  7026. (org-hide-block-toggle 'off)
  7027. (get-char-property (line-end-position) 'invisible)))
  7028. (should-not
  7029. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7030. (org-hide-block-toggle 'off)
  7031. (get-char-property (line-end-position) 'invisible)))
  7032. ;; Hide block unconditionally when optional argument is non-nil.
  7033. (should
  7034. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7035. (org-hide-block-toggle t)
  7036. (get-char-property (line-end-position) 'invisible)))
  7037. (should
  7038. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7039. (org-hide-block-toggle)
  7040. (org-hide-block-toggle t)
  7041. (get-char-property (line-end-position) 'invisible)))
  7042. ;; Do not hide block when called from final blank lines.
  7043. (should-not
  7044. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  7045. (org-hide-block-toggle)
  7046. (goto-char (point-min))
  7047. (get-char-property (line-end-position) 'invisible)))
  7048. ;; Don't leave point in an invisible part of the buffer when hiding
  7049. ;; a block away.
  7050. (should-not
  7051. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  7052. (org-hide-block-toggle)
  7053. (get-char-property (point) 'invisible))))
  7054. (ert-deftest test-org/hide-block-toggle-maybe ()
  7055. "Test `org-hide-block-toggle-maybe' specifications."
  7056. (should
  7057. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  7058. (org-hide-block-toggle-maybe)))
  7059. (should-not
  7060. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  7061. (ert-deftest test-org/show-set-visibility ()
  7062. "Test `org-show-set-visibility' specifications."
  7063. ;; Do not throw an error before first heading.
  7064. (should
  7065. (org-test-with-temp-text "Preamble\n* Headline"
  7066. (org-show-set-visibility 'tree)
  7067. t))
  7068. ;; Test all visibility spans, both on headline and in entry.
  7069. (let ((list-visible-lines
  7070. (lambda (state headerp)
  7071. (org-test-with-temp-text "* Grandmother (0)
  7072. ** Uncle (1)
  7073. *** Heir (2)
  7074. ** Father (3)
  7075. Ancestor text (4)
  7076. *** Sister (5)
  7077. Sibling text (6)
  7078. *** Self (7)
  7079. Match (8)
  7080. **** First born (9)
  7081. Child text (10)
  7082. **** The other child (11)
  7083. *** Brother (12)
  7084. ** Aunt (13)
  7085. "
  7086. (org-cycle t)
  7087. (search-forward (if headerp "Self" "Match"))
  7088. (org-show-set-visibility state)
  7089. (goto-char (point-min))
  7090. (let (result (line 0))
  7091. (while (not (eobp))
  7092. (unless (org-invisible-p2) (push line result))
  7093. (cl-incf line)
  7094. (forward-line))
  7095. (nreverse result))))))
  7096. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  7097. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  7098. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  7099. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  7100. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  7101. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  7102. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  7103. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  7104. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  7105. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  7106. (funcall list-visible-lines 'tree nil)))
  7107. (should (equal '(0 1 3 4 5 7 12 13)
  7108. (funcall list-visible-lines 'canonical t)))
  7109. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  7110. (funcall list-visible-lines 'canonical nil))))
  7111. ;; When point is hidden in a drawer or a block, make sure to make it
  7112. ;; visible.
  7113. (should-not
  7114. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  7115. (org-hide-block-toggle)
  7116. (search-forward "Text")
  7117. (org-show-set-visibility 'minimal)
  7118. (org-invisible-p2)))
  7119. (should-not
  7120. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  7121. (org-flag-drawer t)
  7122. (search-forward "Text")
  7123. (org-show-set-visibility 'minimal)
  7124. (org-invisible-p2)))
  7125. (should-not
  7126. (org-test-with-temp-text
  7127. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  7128. (org-flag-drawer t)
  7129. (forward-line -1)
  7130. (org-hide-block-toggle)
  7131. (search-forward "Text")
  7132. (org-show-set-visibility 'minimal)
  7133. (org-invisible-p2))))
  7134. (defun test-org/copy-visible ()
  7135. "Test `org-copy-visible' specifications."
  7136. (should
  7137. (equal "Foo"
  7138. (org-test-with-temp-text "Foo"
  7139. (let ((kill-ring nil))
  7140. (org-copy-visible (point-min) (point-max))
  7141. (current-kill 0 t)))))
  7142. ;; Skip invisible characters by text property.
  7143. (should
  7144. (equal "Foo"
  7145. (org-test-with-temp-text #("F<hidden>oo" 1 7 (invisible t))
  7146. (let ((kill-ring nil))
  7147. (org-copy-visible (point-min) (point-max))
  7148. (current-kill 0 t)))))
  7149. ;; Skip invisible characters by overlay.
  7150. (should
  7151. (equal "Foo"
  7152. (org-test-with-temp-text "F<hidden>oo"
  7153. (let ((o (make-overlay 2 10)))
  7154. (overlay-put o 'invisible t))
  7155. (let ((kill-ring nil))
  7156. (org-copy-visible (point-min) (point-max))
  7157. (current-kill 0 t)))))
  7158. ;; Handle invisible characters at the beginning and the end of the
  7159. ;; buffer.
  7160. (should
  7161. (equal "Foo"
  7162. (org-test-with-temp-text #("<hidden>Foo" 0 8 (invisible t))
  7163. (let ((kill-ring nil))
  7164. (org-copy-visible (point-min) (point-max))
  7165. (current-kill 0 t)))))
  7166. (should
  7167. (equal "Foo"
  7168. (org-test-with-temp-text #("Foo<hidden>" 3 11 (invisible t))
  7169. (let ((kill-ring nil))
  7170. (org-copy-visible (point-min) (point-max))
  7171. (current-kill 0 t)))))
  7172. ;; Handle multiple visible parts.
  7173. (should
  7174. (equal "abc"
  7175. (org-test-with-temp-text
  7176. #("aXbXc" 1 2 (invisible t) 3 4 (invisible t))
  7177. (let ((kill-ring nil))
  7178. (org-copy-visible (point-min) (point-max))
  7179. (current-kill 0 t))))))
  7180. (ert-deftest test-org/set-visibility-according-to-property ()
  7181. "Test `org-set-visibility-according-to-property' specifications."
  7182. ;; "folded" state.
  7183. (should
  7184. (org-test-with-temp-text
  7185. "
  7186. * a
  7187. :PROPERTIES:
  7188. :VISIBILITY: folded
  7189. :END:
  7190. ** <point>b"
  7191. (org-set-visibility-according-to-property)
  7192. (invisible-p (point))))
  7193. ;; "children" state.
  7194. (should
  7195. (org-test-with-temp-text
  7196. "
  7197. * a
  7198. :PROPERTIES:
  7199. :VISIBILITY: children
  7200. :END:
  7201. ** b
  7202. <point>Contents
  7203. ** c"
  7204. (org-set-visibility-according-to-property)
  7205. (invisible-p (point))))
  7206. (should
  7207. (org-test-with-temp-text
  7208. "
  7209. * a
  7210. :PROPERTIES:
  7211. :VISIBILITY: children
  7212. :END:
  7213. ** b
  7214. Contents
  7215. *** <point>c"
  7216. (org-set-visibility-according-to-property)
  7217. (invisible-p (point))))
  7218. ;; "content" state.
  7219. (should
  7220. (org-test-with-temp-text
  7221. "
  7222. * a
  7223. :PROPERTIES:
  7224. :VISIBILITY: content
  7225. :END:
  7226. ** b
  7227. <point>Contents
  7228. *** c"
  7229. (org-set-visibility-according-to-property)
  7230. (invisible-p (point))))
  7231. (should
  7232. (org-test-with-temp-text
  7233. "
  7234. * a
  7235. :PROPERTIES:
  7236. :VISIBILITY: content
  7237. :END:
  7238. ** b
  7239. Contents
  7240. *** <point>c"
  7241. (org-set-visibility-according-to-property)
  7242. (not (invisible-p (point)))))
  7243. ;; "showall" state.
  7244. (should
  7245. (org-test-with-temp-text
  7246. "
  7247. * a
  7248. :PROPERTIES:
  7249. :VISIBILITY: showall
  7250. :END:
  7251. ** b
  7252. <point>Contents
  7253. *** c"
  7254. (org-set-visibility-according-to-property)
  7255. (not (invisible-p (point)))))
  7256. (should
  7257. (org-test-with-temp-text
  7258. "
  7259. * a
  7260. :PROPERTIES:
  7261. :VISIBILITY: showall
  7262. :END:
  7263. ** b
  7264. Contents
  7265. *** <point>c"
  7266. (org-set-visibility-according-to-property)
  7267. (not (invisible-p (point)))))
  7268. ;; When VISIBILITY properties are nested, ignore inner ones.
  7269. (should
  7270. (org-test-with-temp-text
  7271. "
  7272. * A
  7273. :PROPERTIES:
  7274. :VISIBILITY: folded
  7275. :END:
  7276. ** <point>B
  7277. :PROPERTIES:
  7278. :VISIBILITY: folded
  7279. :END:"
  7280. (org-set-visibility-according-to-property)
  7281. (invisible-p (point)))))
  7282. ;;; Yank and Kill
  7283. (ert-deftest test-org/paste-subtree ()
  7284. "Test `org-paste-subtree' specifications."
  7285. ;; Return an error if text to yank is not a set of subtrees.
  7286. (should-error (org-paste-subtree nil "Text"))
  7287. ;; Adjust level according to current one.
  7288. (should
  7289. (equal "* H\n* Text\n"
  7290. (org-test-with-temp-text "* H\n<point>"
  7291. (org-paste-subtree nil "* Text")
  7292. (buffer-string))))
  7293. (should
  7294. (equal "* H1\n** H2\n** Text\n"
  7295. (org-test-with-temp-text "* H1\n** H2\n<point>"
  7296. (org-paste-subtree nil "* Text")
  7297. (buffer-string))))
  7298. ;; When not on a heading, move to next heading before yanking.
  7299. (should
  7300. (equal "* H1\nParagraph\n* Text\n* H2"
  7301. (org-test-with-temp-text "* H1\n<point>Paragraph\n* H2"
  7302. (org-paste-subtree nil "* Text")
  7303. (buffer-string))))
  7304. ;; If point is between two headings, use the deepest level.
  7305. (should
  7306. (equal "* H1\n\n* Text\n* H2"
  7307. (org-test-with-temp-text "* H1\n<point>\n* H2"
  7308. (org-paste-subtree nil "* Text")
  7309. (buffer-string))))
  7310. (should
  7311. (equal "** H1\n\n** Text\n* H2"
  7312. (org-test-with-temp-text "** H1\n<point>\n* H2"
  7313. (org-paste-subtree nil "* Text")
  7314. (buffer-string))))
  7315. (should
  7316. (equal "* H1\n\n** Text\n** H2"
  7317. (org-test-with-temp-text "* H1\n<point>\n** H2"
  7318. (org-paste-subtree nil "* Text")
  7319. (buffer-string))))
  7320. ;; When on an empty heading, after the stars, deduce the new level
  7321. ;; from the number of stars.
  7322. (should
  7323. (equal "*** Text\n"
  7324. (org-test-with-temp-text "*** <point>"
  7325. (org-paste-subtree nil "* Text")
  7326. (buffer-string))))
  7327. ;; Optional argument LEVEL forces a level for the subtree.
  7328. (should
  7329. (equal "* H\n*** Text\n"
  7330. (org-test-with-temp-text "* H<point>"
  7331. (org-paste-subtree 3 "* Text")
  7332. (buffer-string)))))
  7333. (ert-deftest test-org/cut-and-paste-subtree ()
  7334. "Test `org-cut-subtree' and `org-paste-subtree'."
  7335. (should
  7336. (equal
  7337. "* Two
  7338. two
  7339. * One
  7340. "
  7341. (org-test-with-temp-text
  7342. "* One
  7343. <point>* Two
  7344. two
  7345. "
  7346. (call-interactively #'org-cut-subtree)
  7347. (goto-char (point-min))
  7348. (call-interactively #'org-paste-subtree)
  7349. (buffer-string))))
  7350. (should
  7351. (equal
  7352. "* One
  7353. * Two
  7354. "
  7355. (org-test-with-temp-text
  7356. "* One
  7357. <point>* Two
  7358. "
  7359. (call-interactively #'org-cut-subtree)
  7360. (backward-char)
  7361. (call-interactively #'org-paste-subtree)
  7362. (buffer-string)))))
  7363. (provide 'test-org)
  7364. ;;; test-org.el ends here