test-org.el 196 KB

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