test-org.el 194 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942
  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. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1300. "Test `org-insert-todo-heading-respect-content' specifications."
  1301. ;; Create a TODO heading.
  1302. (should
  1303. (org-test-with-temp-text "* H1\n Body"
  1304. (org-insert-todo-heading-respect-content)
  1305. (nth 2 (org-heading-components))))
  1306. ;; Add headline at the end of the first subtree
  1307. (should
  1308. (equal
  1309. "* TODO \n"
  1310. (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
  1311. (org-insert-todo-heading-respect-content)
  1312. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1313. ;; In a list, do not create a new item.
  1314. (should
  1315. (equal
  1316. "* TODO \n"
  1317. (org-test-with-temp-text "* H\n- an item\n- another one"
  1318. (search-forward "an ")
  1319. (org-insert-todo-heading-respect-content)
  1320. (buffer-substring-no-properties (line-beginning-position) (point-max))))))
  1321. (ert-deftest test-org/clone-with-time-shift ()
  1322. "Test `org-clone-subtree-with-time-shift'."
  1323. ;; Raise an error before first heading.
  1324. (should-error
  1325. (org-test-with-temp-text ""
  1326. (org-clone-subtree-with-time-shift 1)))
  1327. ;; Raise an error on invalid number of clones.
  1328. (should-error
  1329. (org-test-with-temp-text "* Clone me"
  1330. (org-clone-subtree-with-time-shift -1)))
  1331. ;; Clone non-repeating once.
  1332. (should
  1333. (equal "\
  1334. * H1\n<2015-06-21>
  1335. * H1\n<2015-06-23>
  1336. "
  1337. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1338. (org-clone-subtree-with-time-shift 1 "+2d")
  1339. (replace-regexp-in-string
  1340. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1341. nil nil 1))))
  1342. ;; Clone repeating once.
  1343. (should
  1344. (equal "\
  1345. * H1\n<2015-06-21>
  1346. * H1\n<2015-06-23>
  1347. * H1\n<2015-06-25 +1w>
  1348. "
  1349. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1350. (org-clone-subtree-with-time-shift 1 "+2d")
  1351. (replace-regexp-in-string
  1352. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1353. nil nil 1))))
  1354. ;; Clone non-repeating zero times.
  1355. (should
  1356. (equal "\
  1357. * H1\n<2015-06-21>
  1358. "
  1359. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1360. (org-clone-subtree-with-time-shift 0 "+2d")
  1361. (replace-regexp-in-string
  1362. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1363. nil nil 1))))
  1364. ;; Clone repeating "zero" times.
  1365. (should
  1366. (equal "\
  1367. * H1\n<2015-06-21>
  1368. * H1\n<2015-06-23 +1w>
  1369. "
  1370. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1371. (org-clone-subtree-with-time-shift 0 "+2d")
  1372. (replace-regexp-in-string
  1373. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1374. nil nil 1))))
  1375. ;; Clone with blank SHIFT argument.
  1376. (should
  1377. (string-prefix-p
  1378. "* H <2012-03-29"
  1379. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1380. (org-clone-subtree-with-time-shift 1 "")
  1381. (buffer-substring-no-properties (line-beginning-position 2)
  1382. (line-end-position 2)))))
  1383. ;; Find time stamps before point. If SHIFT is not specified, ask
  1384. ;; for a time shift.
  1385. (should
  1386. (string-prefix-p
  1387. "* H <2012-03-30"
  1388. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1389. (org-clone-subtree-with-time-shift 1 "+1d")
  1390. (buffer-substring-no-properties (line-beginning-position 2)
  1391. (line-end-position 2)))))
  1392. (should
  1393. (string-prefix-p
  1394. "* H <2014-03-05"
  1395. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  1396. (cl-letf (((symbol-function 'read-from-minibuffer)
  1397. (lambda (&rest args) "+1d")))
  1398. (org-clone-subtree-with-time-shift 1))
  1399. (buffer-substring-no-properties (line-beginning-position 2)
  1400. (line-end-position 2))))))
  1401. ;;; Fixed-Width Areas
  1402. (ert-deftest test-org/toggle-fixed-width ()
  1403. "Test `org-toggle-fixed-width' specifications."
  1404. ;; No region: Toggle on fixed-width marker in paragraphs.
  1405. (should
  1406. (equal ": A"
  1407. (org-test-with-temp-text "A"
  1408. (org-toggle-fixed-width)
  1409. (buffer-string))))
  1410. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1411. (should
  1412. (equal "A"
  1413. (org-test-with-temp-text ": A"
  1414. (org-toggle-fixed-width)
  1415. (buffer-string))))
  1416. ;; No region: Toggle on marker in blank lines after elements or just
  1417. ;; after a headline.
  1418. (should
  1419. (equal "* H\n: "
  1420. (org-test-with-temp-text "* H\n"
  1421. (forward-line)
  1422. (org-toggle-fixed-width)
  1423. (buffer-string))))
  1424. (should
  1425. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1426. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1427. (goto-char (point-max))
  1428. (org-toggle-fixed-width)
  1429. (buffer-string))))
  1430. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1431. ;; headlines, clocks)
  1432. (should
  1433. (equal ": * Headline"
  1434. (org-test-with-temp-text "* Headline"
  1435. (org-toggle-fixed-width)
  1436. (buffer-string))))
  1437. (should
  1438. (equal ": #+KEYWORD: value"
  1439. (org-test-with-temp-text "#+KEYWORD: value"
  1440. (org-toggle-fixed-width)
  1441. (buffer-string))))
  1442. ;; No region: error in other situations.
  1443. (should-error
  1444. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1445. (forward-line)
  1446. (org-toggle-fixed-width)
  1447. (buffer-string)))
  1448. ;; No region: Indentation is preserved.
  1449. (should
  1450. (equal "- A\n : B"
  1451. (org-test-with-temp-text "- A\n B"
  1452. (forward-line)
  1453. (org-toggle-fixed-width)
  1454. (buffer-string))))
  1455. ;; Region: If it contains only fixed-width elements and blank lines,
  1456. ;; toggle off fixed-width markup.
  1457. (should
  1458. (equal "A\n\nB"
  1459. (org-test-with-temp-text ": A\n\n: B"
  1460. (transient-mark-mode 1)
  1461. (push-mark (point) t t)
  1462. (goto-char (point-max))
  1463. (org-toggle-fixed-width)
  1464. (buffer-string))))
  1465. ;; Region: If it contains anything else, toggle on fixed-width but
  1466. ;; not on fixed-width areas.
  1467. (should
  1468. (equal ": A\n: \n: B\n: \n: C"
  1469. (org-test-with-temp-text "A\n\n: B\n\nC"
  1470. (transient-mark-mode 1)
  1471. (push-mark (point) t t)
  1472. (goto-char (point-max))
  1473. (org-toggle-fixed-width)
  1474. (buffer-string))))
  1475. ;; Region: Ignore blank lines at its end, unless it contains only
  1476. ;; such lines.
  1477. (should
  1478. (equal ": A\n\n"
  1479. (org-test-with-temp-text "A\n\n"
  1480. (transient-mark-mode 1)
  1481. (push-mark (point) t t)
  1482. (goto-char (point-max))
  1483. (org-toggle-fixed-width)
  1484. (buffer-string))))
  1485. (should
  1486. (equal ": \n: \n"
  1487. (org-test-with-temp-text "\n\n"
  1488. (transient-mark-mode 1)
  1489. (push-mark (point) t t)
  1490. (goto-char (point-max))
  1491. (org-toggle-fixed-width)
  1492. (buffer-string)))))
  1493. ;;; Headline
  1494. (ert-deftest test-org/get-heading ()
  1495. "Test `org-get-heading' specifications."
  1496. ;; Return current heading, even if point is not on it.
  1497. (should
  1498. (equal "H"
  1499. (org-test-with-temp-text "* H"
  1500. (org-get-heading))))
  1501. (should
  1502. (equal "H"
  1503. (org-test-with-temp-text "* H\nText<point>"
  1504. (org-get-heading))))
  1505. ;; Without any optional argument, return TODO keyword, priority
  1506. ;; cookie, COMMENT keyword and tags.
  1507. (should
  1508. (equal "TODO H"
  1509. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1510. (org-get-heading))))
  1511. (should
  1512. (equal "[#A] H"
  1513. (org-test-with-temp-text "* [#A] H"
  1514. (org-get-heading))))
  1515. (should
  1516. (equal "COMMENT H"
  1517. (org-test-with-temp-text "* COMMENT H"
  1518. (org-get-heading))))
  1519. (should
  1520. (equal "H :tag:"
  1521. (org-test-with-temp-text "* H :tag:"
  1522. (org-get-heading))))
  1523. ;; With NO-TAGS argument, ignore tags.
  1524. (should
  1525. (equal "TODO H"
  1526. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1527. (org-get-heading t))))
  1528. (should
  1529. (equal "H"
  1530. (org-test-with-temp-text "* H :tag:"
  1531. (org-get-heading t))))
  1532. ;; With NO-TODO, ignore TODO keyword.
  1533. (should
  1534. (equal "H"
  1535. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1536. (org-get-heading nil t))))
  1537. (should
  1538. (equal "H :tag:"
  1539. (org-test-with-temp-text "* H :tag:"
  1540. (org-get-heading nil t))))
  1541. ;; TODO keywords are case-sensitive.
  1542. (should
  1543. (equal "Todo H"
  1544. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1545. (org-get-heading nil t))))
  1546. ;; With NO-PRIORITY, ignore priority.
  1547. (should
  1548. (equal "H"
  1549. (org-test-with-temp-text "* [#A] H"
  1550. (org-get-heading nil nil t))))
  1551. (should
  1552. (equal "H"
  1553. (org-test-with-temp-text "* H"
  1554. (org-get-heading nil nil t))))
  1555. (should
  1556. (equal "TODO H"
  1557. (org-test-with-temp-text "* TODO [#A] H"
  1558. (org-get-heading nil nil t))))
  1559. ;; With NO-COMMENT, ignore COMMENT keyword.
  1560. (should
  1561. (equal "H"
  1562. (org-test-with-temp-text "* COMMENT H"
  1563. (org-get-heading nil nil nil t))))
  1564. (should
  1565. (equal "H"
  1566. (org-test-with-temp-text "* H"
  1567. (org-get-heading nil nil nil t))))
  1568. (should
  1569. (equal "TODO [#A] H"
  1570. (org-test-with-temp-text "* TODO [#A] COMMENT H"
  1571. (org-get-heading nil nil nil t))))
  1572. ;; On an empty headline, return value is consistent.
  1573. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1574. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1575. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1576. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil t))))
  1577. (should
  1578. (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil nil t)))))
  1579. (ert-deftest test-org/in-commented-heading-p ()
  1580. "Test `org-in-commented-heading-p' specifications."
  1581. ;; Commented headline.
  1582. (should
  1583. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1584. (goto-char (point-max))
  1585. (org-in-commented-heading-p)))
  1586. ;; Commented ancestor.
  1587. (should
  1588. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1589. (goto-char (point-max))
  1590. (org-in-commented-heading-p)))
  1591. ;; Comment keyword is case-sensitive.
  1592. (should-not
  1593. (org-test-with-temp-text "* Comment Headline\nBody"
  1594. (goto-char (point-max))
  1595. (org-in-commented-heading-p)))
  1596. ;; Keyword is standalone.
  1597. (should-not
  1598. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1599. (goto-char (point-max))
  1600. (org-in-commented-heading-p)))
  1601. ;; Optional argument.
  1602. (should-not
  1603. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1604. (goto-char (point-max))
  1605. (org-in-commented-heading-p t))))
  1606. (ert-deftest test-org/entry-blocked-p ()
  1607. ;; Check other dependencies.
  1608. (should
  1609. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1610. (let ((org-enforce-todo-dependencies t)
  1611. (org-blocker-hook
  1612. '(org-block-todo-from-children-or-siblings-or-parent)))
  1613. (org-entry-blocked-p))))
  1614. (should-not
  1615. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1616. (let ((org-enforce-todo-dependencies t)
  1617. (org-blocker-hook
  1618. '(org-block-todo-from-children-or-siblings-or-parent)))
  1619. (org-entry-blocked-p))))
  1620. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1621. ;; blocked.
  1622. (should-not
  1623. (org-test-with-temp-text "* Blocked\n** TODO one"
  1624. (let ((org-enforce-todo-dependencies t)
  1625. (org-blocker-hook
  1626. '(org-block-todo-from-children-or-siblings-or-parent)))
  1627. (org-entry-blocked-p))))
  1628. (should-not
  1629. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1630. (let ((org-enforce-todo-dependencies t)
  1631. (org-blocker-hook
  1632. '(org-block-todo-from-children-or-siblings-or-parent)))
  1633. (org-entry-blocked-p))))
  1634. ;; Follow :ORDERED: specifications.
  1635. (should
  1636. (org-test-with-temp-text
  1637. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1638. (let ((org-enforce-todo-dependencies t)
  1639. (org-blocker-hook
  1640. '(org-block-todo-from-children-or-siblings-or-parent)))
  1641. (org-entry-blocked-p))))
  1642. (should-not
  1643. (org-test-with-temp-text
  1644. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1645. (let ((org-enforce-todo-dependencies t)
  1646. (org-blocker-hook
  1647. '(org-block-todo-from-children-or-siblings-or-parent)))
  1648. (org-entry-blocked-p)))))
  1649. (ert-deftest test-org/get-outline-path ()
  1650. "Test `org-get-outline-path' specifications."
  1651. ;; Top-level headlines have no outline path.
  1652. (should-not
  1653. (org-test-with-temp-text "* H"
  1654. (org-get-outline-path)))
  1655. ;; Otherwise, outline path is the path leading to the headline.
  1656. (should
  1657. (equal '("H")
  1658. (org-test-with-temp-text "* H\n** S<point>"
  1659. (org-get-outline-path))))
  1660. ;; Find path even when point is not on a headline.
  1661. (should
  1662. (equal '("H")
  1663. (org-test-with-temp-text "* H\n** S\nText<point>"
  1664. (org-get-outline-path))))
  1665. ;; TODO keywords, tags and statistics cookies are ignored.
  1666. (should
  1667. (equal '("H")
  1668. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1669. (org-get-outline-path))))
  1670. ;; Links are replaced with their description or their path.
  1671. (should
  1672. (equal '("Org")
  1673. (org-test-with-temp-text
  1674. "* [[http://orgmode.org][Org]]\n** S<point>"
  1675. (org-get-outline-path))))
  1676. (should
  1677. (equal '("http://orgmode.org")
  1678. (org-test-with-temp-text
  1679. "* [[http://orgmode.org]]\n** S<point>"
  1680. (org-get-outline-path))))
  1681. ;; When WITH-SELF is non-nil, include current heading.
  1682. (should
  1683. (equal '("H")
  1684. (org-test-with-temp-text "* H"
  1685. (org-get-outline-path t))))
  1686. (should
  1687. (equal '("H" "S")
  1688. (org-test-with-temp-text "* H\n** S\nText<point>"
  1689. (org-get-outline-path t))))
  1690. ;; Using cache is transparent to the user.
  1691. (should
  1692. (equal '("H")
  1693. (org-test-with-temp-text "* H\n** S<point>"
  1694. (setq org-outline-path-cache nil)
  1695. (org-get-outline-path nil t))))
  1696. ;; Do not corrupt cache when finding outline path in distant part of
  1697. ;; the buffer.
  1698. (should
  1699. (equal '("H2")
  1700. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1701. (setq org-outline-path-cache nil)
  1702. (org-get-outline-path nil t)
  1703. (search-forward "S2")
  1704. (org-get-outline-path nil t))))
  1705. ;; Do not choke on empty headlines.
  1706. (should
  1707. (org-test-with-temp-text "* H\n** <point>"
  1708. (org-get-outline-path)))
  1709. (should
  1710. (org-test-with-temp-text "* \n** H<point>"
  1711. (org-get-outline-path))))
  1712. (ert-deftest test-org/format-outline-path ()
  1713. "Test `org-format-outline-path' specifications."
  1714. (should
  1715. (string= (org-format-outline-path (list "one" "two" "three"))
  1716. "one/two/three"))
  1717. ;; Empty path.
  1718. (should
  1719. (string= (org-format-outline-path '())
  1720. ""))
  1721. (should
  1722. (string= (org-format-outline-path '(nil))
  1723. ""))
  1724. ;; Empty path and prefix.
  1725. (should
  1726. (string= (org-format-outline-path '() nil ">>")
  1727. ">>"))
  1728. ;; Trailing whitespace in headings.
  1729. (should
  1730. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1731. "one/tw o/three"))
  1732. ;; Non-default prefix and separators.
  1733. (should
  1734. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1735. ">>|one|two|three"))
  1736. ;; Truncate.
  1737. (should
  1738. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1739. "one/two/.."))
  1740. ;; Give a very narrow width.
  1741. (should
  1742. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1743. "on"))
  1744. ;; Give a prefix that extends beyond the width.
  1745. (should
  1746. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1747. ">>>>>>>>>>")
  1748. ">>>>>>>>..")))
  1749. (ert-deftest test-org/map-entries ()
  1750. "Test `org-map-entries' specifications."
  1751. ;; Full match.
  1752. (should
  1753. (equal '(1 11)
  1754. (org-test-with-temp-text "* Level 1\n** Level 2"
  1755. (org-map-entries #'point))))
  1756. ;; Level match.
  1757. (should
  1758. (equal '(1)
  1759. (org-test-with-temp-text "* Level 1\n** Level 2"
  1760. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1761. (should
  1762. (equal '(11)
  1763. (org-test-with-temp-text "* Level 1\n** Level 2"
  1764. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1765. ;; Tag match.
  1766. (should
  1767. (equal '(11)
  1768. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1769. (org-map-entries #'point "yes"))))
  1770. (should
  1771. (equal '(14)
  1772. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1773. (org-map-entries #'point "+yes-a"))))
  1774. (should
  1775. (equal '(11 23)
  1776. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1777. (org-map-entries #'point "{yes?}"))))
  1778. ;; Priority match.
  1779. (should
  1780. (equal '(1)
  1781. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1782. (org-map-entries #'point "PRIORITY=\"A\""))))
  1783. ;; Date match.
  1784. (should
  1785. (equal '(36)
  1786. (org-test-with-temp-text "
  1787. * H1
  1788. SCHEDULED: <2012-03-29 thu.>
  1789. * H2
  1790. SCHEDULED: <2014-03-04 tue.>"
  1791. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1792. (should
  1793. (equal '(2)
  1794. (org-test-with-temp-text "
  1795. * H1
  1796. SCHEDULED: <2012-03-29 thu.>
  1797. * H2
  1798. SCHEDULED: <2014-03-04 tue.>"
  1799. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1800. ;; Regular property match.
  1801. (should
  1802. (equal '(2)
  1803. (org-test-with-temp-text "
  1804. * H1
  1805. :PROPERTIES:
  1806. :TEST: 1
  1807. :END:
  1808. * H2
  1809. :PROPERTIES:
  1810. :TEST: 2
  1811. :END:"
  1812. (org-map-entries #'point "TEST=1"))))
  1813. ;; Multiple criteria.
  1814. (should
  1815. (equal '(23)
  1816. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1817. (let (org-odd-levels-only
  1818. (org-use-tag-inheritance nil))
  1819. (org-map-entries #'point "yes+LEVEL=1")))))
  1820. ;; "or" criteria.
  1821. (should
  1822. (equal '(12 24)
  1823. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1824. (let (org-odd-levels-only)
  1825. (org-map-entries #'point "LEVEL=2|no")))))
  1826. (should
  1827. (equal '(1 12)
  1828. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  1829. (let (org-odd-levels-only)
  1830. (org-map-entries #'point "yes|no")))))
  1831. ;; "and" criteria.
  1832. (should
  1833. (equal '(22)
  1834. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  1835. (let (org-odd-levels-only)
  1836. (org-map-entries #'point "yes&no"))))))
  1837. (ert-deftest test-org/edit-headline ()
  1838. "Test `org-edit-headline' specifications."
  1839. (should
  1840. (equal "* B"
  1841. (org-test-with-temp-text "* A"
  1842. (org-edit-headline "B")
  1843. (buffer-string))))
  1844. ;; Handle empty headings.
  1845. (should
  1846. (equal "* "
  1847. (org-test-with-temp-text "* A"
  1848. (org-edit-headline "")
  1849. (buffer-string))))
  1850. (should
  1851. (equal "* A"
  1852. (org-test-with-temp-text "* "
  1853. (org-edit-headline "A")
  1854. (buffer-string))))
  1855. ;; Handle TODO keywords and priority cookies.
  1856. (should
  1857. (equal "* TODO B"
  1858. (org-test-with-temp-text "* TODO A"
  1859. (org-edit-headline "B")
  1860. (buffer-string))))
  1861. (should
  1862. (equal "* [#A] B"
  1863. (org-test-with-temp-text "* [#A] A"
  1864. (org-edit-headline "B")
  1865. (buffer-string))))
  1866. (should
  1867. (equal "* TODO [#A] B"
  1868. (org-test-with-temp-text "* TODO [#A] A"
  1869. (org-edit-headline "B")
  1870. (buffer-string))))
  1871. ;; Handle tags.
  1872. (equal "* B :tag:"
  1873. (org-test-with-temp-text "* A :tag:"
  1874. (let ((org-tags-column 4)) (org-edit-headline "B"))
  1875. (buffer-string))))
  1876. ;;; Keywords
  1877. (ert-deftest test-org/set-regexps-and-options ()
  1878. "Test `org-set-regexps-and-options' specifications."
  1879. ;; TAGS keyword.
  1880. (should
  1881. (equal '(("A"))
  1882. (let ((org-tag-alist '(("A")))
  1883. (org-tag-persistent-alist nil))
  1884. (org-test-with-temp-text ""
  1885. (org-mode-restart)
  1886. org-current-tag-alist))))
  1887. (should
  1888. (equal '(("B"))
  1889. (let ((org-tag-alist '(("A")))
  1890. (org-tag-persistent-alist nil))
  1891. (org-test-with-temp-text "#+TAGS: B"
  1892. (org-mode-restart)
  1893. org-current-tag-alist))))
  1894. (should
  1895. (equal '(("C") ("B"))
  1896. (let ((org-tag-alist '(("A")))
  1897. (org-tag-persistent-alist '(("C"))))
  1898. (org-test-with-temp-text "#+TAGS: B"
  1899. (org-mode-restart)
  1900. org-current-tag-alist))))
  1901. (should
  1902. (equal '(("B"))
  1903. (let ((org-tag-alist '(("A")))
  1904. (org-tag-persistent-alist '(("C"))))
  1905. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  1906. (org-mode-restart)
  1907. org-current-tag-alist))))
  1908. (should
  1909. (equal '(("A" . ?a) ("B") ("C"))
  1910. (let ((org-tag-persistant-alist nil))
  1911. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1912. (org-mode-restart)
  1913. org-current-tag-alist))))
  1914. (should
  1915. (equal '(("A") (:newline) ("B"))
  1916. (let ((org-tag-persistent-alist nil))
  1917. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1918. (org-mode-restart)
  1919. org-current-tag-alist))))
  1920. (should
  1921. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1922. (let ((org-tag-persistent-alist nil))
  1923. (org-test-with-temp-text "#+TAGS: { A B } C"
  1924. (org-mode-restart)
  1925. org-current-tag-alist))))
  1926. (should
  1927. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1928. (let ((org-tag-persistent-alist nil))
  1929. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1930. (org-mode-restart)
  1931. org-current-tag-alist))))
  1932. (should
  1933. (equal '(("A" "B" "C"))
  1934. (let ((org-tag-persistent-alist nil))
  1935. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1936. (org-mode-restart)
  1937. org-tag-groups-alist))))
  1938. (should
  1939. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1940. (let ((org-tag-persistent-alist nil))
  1941. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1942. (org-mode-restart)
  1943. org-current-tag-alist))))
  1944. (should
  1945. (equal '(("A" "B" "C"))
  1946. (let ((org-tag-persistent-alist nil))
  1947. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1948. (org-mode-restart)
  1949. org-tag-groups-alist))))
  1950. ;; FILETAGS keyword.
  1951. (should
  1952. (equal '("A" "B" "C")
  1953. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1954. (org-mode-restart)
  1955. org-file-tags)))
  1956. ;; PROPERTY keyword. Property names are case-insensitive.
  1957. (should
  1958. (equal "foo=1"
  1959. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1960. (org-mode-restart)
  1961. (cdr (assoc "var" org-file-properties)))))
  1962. (should
  1963. (equal
  1964. "foo=1 bar=2"
  1965. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1966. (org-mode-restart)
  1967. (cdr (assoc "var" org-file-properties)))))
  1968. (should
  1969. (equal
  1970. "foo=1 bar=2"
  1971. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1972. (org-mode-restart)
  1973. (cdr (assoc "var" org-file-properties)))))
  1974. ;; ARCHIVE keyword.
  1975. (should
  1976. (equal "%s_done::"
  1977. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1978. (org-mode-restart)
  1979. org-archive-location)))
  1980. ;; CATEGORY keyword.
  1981. (should
  1982. (eq 'test
  1983. (org-test-with-temp-text "#+CATEGORY: test"
  1984. (org-mode-restart)
  1985. org-category)))
  1986. (should
  1987. (equal "test"
  1988. (org-test-with-temp-text "#+CATEGORY: test"
  1989. (org-mode-restart)
  1990. (cdr (assoc "CATEGORY" org-file-properties)))))
  1991. ;; COLUMNS keyword.
  1992. (should
  1993. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1994. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1995. (org-mode-restart)
  1996. org-columns-default-format)))
  1997. ;; CONSTANTS keyword. Constants names are case sensitive.
  1998. (should
  1999. (equal '("299792458." "3.14")
  2000. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  2001. (org-mode-restart)
  2002. (mapcar
  2003. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  2004. '("c" "pi")))))
  2005. (should
  2006. (equal "3.14"
  2007. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  2008. (org-mode-restart)
  2009. (cdr (assoc "pi" org-table-formula-constants-local)))))
  2010. (should
  2011. (equal "22/7"
  2012. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  2013. (org-mode-restart)
  2014. (cdr (assoc "PI" org-table-formula-constants-local)))))
  2015. ;; LINK keyword.
  2016. (should
  2017. (equal
  2018. '("url1" "url2")
  2019. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  2020. (org-mode-restart)
  2021. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  2022. '("a" "b")))))
  2023. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  2024. (should
  2025. (equal
  2026. '(?X ?Z ?Y)
  2027. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  2028. (org-mode-restart)
  2029. (list org-highest-priority org-lowest-priority org-default-priority))))
  2030. (should
  2031. (equal
  2032. '(?A ?C ?B)
  2033. (org-test-with-temp-text "#+PRIORITIES: X Z"
  2034. (org-mode-restart)
  2035. (list org-highest-priority org-lowest-priority org-default-priority))))
  2036. ;; STARTUP keyword.
  2037. (should
  2038. (equal '(t t)
  2039. (org-test-with-temp-text "#+STARTUP: fold odd"
  2040. (org-mode-restart)
  2041. (list org-startup-folded org-odd-levels-only))))
  2042. ;; TODO keywords.
  2043. (should
  2044. (equal '(("A" "B") ("C"))
  2045. (org-test-with-temp-text "#+TODO: A B | C"
  2046. (org-mode-restart)
  2047. (list org-not-done-keywords org-done-keywords))))
  2048. (should
  2049. (equal '(("A" "C") ("B" "D"))
  2050. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  2051. (org-mode-restart)
  2052. (list org-not-done-keywords org-done-keywords))))
  2053. (should
  2054. (equal '(("A" "B") ("C"))
  2055. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  2056. (org-mode-restart)
  2057. (list org-not-done-keywords org-done-keywords))))
  2058. (should
  2059. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  2060. (org-test-with-temp-text "#+TODO: A(a)"
  2061. (org-mode-restart)
  2062. org-todo-key-alist)))
  2063. (should
  2064. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  2065. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  2066. (org-mode-restart)
  2067. org-todo-log-states)))
  2068. ;; Enter SETUPFILE keyword.
  2069. (should
  2070. (equal "1"
  2071. (org-test-with-temp-text
  2072. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  2073. (org-mode-restart)
  2074. (cdr (assoc "a" org-file-properties))))))
  2075. ;;; Links
  2076. ;;;; Coderefs
  2077. (ert-deftest test-org/coderef ()
  2078. "Test coderef links specifications."
  2079. (should
  2080. (org-test-with-temp-text "
  2081. #+BEGIN_SRC emacs-lisp
  2082. \(+ 1 1) (ref:sc)
  2083. #+END_SRC
  2084. \[[(sc)<point>]]"
  2085. (org-open-at-point)
  2086. (looking-at "(ref:sc)")))
  2087. ;; Find coderef even with alternate label format.
  2088. (should
  2089. (org-test-with-temp-text "
  2090. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2091. \(+ 1 1) {ref:sc}
  2092. #+END_SRC
  2093. \[[(sc)<point>]]"
  2094. (org-open-at-point)
  2095. (looking-at "{ref:sc}"))))
  2096. ;;;; Custom ID
  2097. (ert-deftest test-org/custom-id ()
  2098. "Test custom ID links specifications."
  2099. (should
  2100. (org-test-with-temp-text
  2101. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2102. (org-open-at-point)
  2103. (looking-at-p "\\* H1")))
  2104. ;; Throw an error on false positives.
  2105. (should-error
  2106. (org-test-with-temp-text
  2107. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2108. (org-open-at-point)
  2109. (looking-at-p "\\* H1"))))
  2110. ;;;; Fuzzy Links
  2111. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2112. ;; a named element (#+name: text) and to headlines (* Text).
  2113. (ert-deftest test-org/fuzzy-links ()
  2114. "Test fuzzy links specifications."
  2115. ;; Fuzzy link goes in priority to a matching target.
  2116. (should
  2117. (org-test-with-temp-text
  2118. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2119. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2120. (looking-at "<<Test>>")))
  2121. ;; Then fuzzy link points to an element with a given name.
  2122. (should
  2123. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2124. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2125. (looking-at "#\\+NAME: Test")))
  2126. ;; A target still lead to a matching headline otherwise.
  2127. (should
  2128. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2129. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2130. (looking-at "\\* Head2")))
  2131. ;; With a leading star in link, enforce heading match.
  2132. (should
  2133. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2134. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2135. (looking-at "\\* Test")))
  2136. ;; With a leading star in link, enforce exact heading match, even
  2137. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2138. (should-error
  2139. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2140. (let ((org-link-search-must-match-exact-headline nil))
  2141. (org-open-at-point))))
  2142. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2143. (should
  2144. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2145. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2146. (looking-at "\\* Test")))
  2147. (should
  2148. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2149. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2150. (looking-at "\\* Test")))
  2151. ;; Heading match should not care about spaces, cookies, TODO
  2152. ;; keywords, priorities, and tags. However, TODO keywords are
  2153. ;; case-sensitive.
  2154. (should
  2155. (let ((first-line
  2156. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2157. (org-test-with-temp-text
  2158. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2159. (let ((org-link-search-must-match-exact-headline nil)
  2160. (org-todo-regexp "TODO"))
  2161. (org-open-at-point))
  2162. (looking-at (regexp-quote first-line)))))
  2163. (should-error
  2164. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2165. (let ((org-link-search-must-match-exact-headline nil)
  2166. (org-todo-regexp "TODO"))
  2167. (org-open-at-point))))
  2168. ;; Heading match should still be exact.
  2169. (should-error
  2170. (org-test-with-temp-text "
  2171. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2172. Foo Bar
  2173. <point>[[*Test 1]]"
  2174. (let ((org-link-search-must-match-exact-headline nil)
  2175. (org-todo-regexp "TODO"))
  2176. (org-open-at-point))))
  2177. (should
  2178. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2179. (let ((org-link-search-must-match-exact-headline nil)
  2180. (org-todo-regexp "TODO"))
  2181. (org-open-at-point))
  2182. (looking-at-p (regexp-quote "** Test 1 2"))))
  2183. ;; Heading match ignores COMMENT keyword.
  2184. (should
  2185. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2186. (org-open-at-point)
  2187. (looking-at "\\* COMMENT Test")))
  2188. (should
  2189. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2190. (org-open-at-point)
  2191. (looking-at "\\* TODO COMMENT Test")))
  2192. ;; Correctly un-hexify fuzzy links.
  2193. (should
  2194. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2195. (org-open-at-point)
  2196. (bobp)))
  2197. (should
  2198. (org-test-with-temp-text "* [1]\n[[*%5B1%5D<point>]]"
  2199. (org-open-at-point)
  2200. (bobp))))
  2201. ;;;; Link Escaping
  2202. (ert-deftest test-org/org-link-escape-ascii-character ()
  2203. "Escape an ascii character."
  2204. (should
  2205. (string=
  2206. "%5B"
  2207. (org-link-escape "["))))
  2208. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2209. "Escape an ascii control character."
  2210. (should
  2211. (string=
  2212. "%09"
  2213. (org-link-escape "\t"))))
  2214. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2215. "Escape an unicode multibyte character."
  2216. (should
  2217. (string=
  2218. "%E2%82%AC"
  2219. (org-link-escape "€"))))
  2220. (ert-deftest test-org/org-link-escape-custom-table ()
  2221. "Escape string with custom character table."
  2222. (should
  2223. (string=
  2224. "Foo%3A%42ar%0A"
  2225. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2226. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2227. "Escape string with custom table merged with default table."
  2228. (should
  2229. (string=
  2230. "%5BF%6F%6F%3A%42ar%0A%5D"
  2231. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2232. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2233. "Unescape an ascii character."
  2234. (should
  2235. (string=
  2236. "["
  2237. (org-link-unescape "%5B"))))
  2238. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2239. "Unescpae an ascii control character."
  2240. (should
  2241. (string=
  2242. "\n"
  2243. (org-link-unescape "%0A"))))
  2244. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2245. "Unescape unicode multibyte character."
  2246. (should
  2247. (string=
  2248. "€"
  2249. (org-link-unescape "%E2%82%AC"))))
  2250. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2251. "Unescape old style percent escaped character."
  2252. (should
  2253. (string=
  2254. "àâçèéêîôùû"
  2255. (decode-coding-string
  2256. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2257. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2258. "Escape and unescape a URL that includes an escaped char.
  2259. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2260. (should
  2261. (string=
  2262. "http://some.host.com/form?&id=blah%2Bblah25"
  2263. (org-link-unescape
  2264. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2265. ;;;; Open at point
  2266. (ert-deftest test-org/open-at-point-in-keyword ()
  2267. "Does `org-open-at-point' open link in a keyword line?"
  2268. (should
  2269. (org-test-with-temp-text
  2270. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2271. (org-open-at-point) t)))
  2272. (ert-deftest test-org/open-at-point-in-property ()
  2273. "Does `org-open-at-point' open link in property drawer?"
  2274. (should
  2275. (org-test-with-temp-text
  2276. "* Headline
  2277. :PROPERTIES:
  2278. :URL: <point>[[*Headline]]
  2279. :END:"
  2280. (org-open-at-point) t)))
  2281. (ert-deftest test-org/open-at-point-in-comment ()
  2282. "Does `org-open-at-point' open link in a commented line?"
  2283. (should
  2284. (org-test-with-temp-text
  2285. "<<top>>\n# <point>[[top]]"
  2286. (org-open-at-point) t)))
  2287. (ert-deftest test-org/open-at-point/inline-image ()
  2288. "Test `org-open-at-point' on nested links."
  2289. (should
  2290. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2291. (org-open-at-point)
  2292. (bobp))))
  2293. (ert-deftest test-org/open-at-point/radio-target ()
  2294. "Test `org-open-at-point' on radio targets."
  2295. (should
  2296. (org-test-with-temp-text "<<<target>>> <point>target"
  2297. (org-update-radio-target-regexp)
  2298. (org-open-at-point)
  2299. (eq (org-element-type (org-element-context)) 'radio-target))))
  2300. ;;;; Stored links
  2301. (ert-deftest test-org/store-link ()
  2302. "Test `org-store-link' specifications."
  2303. ;; On a headline, link to that headline. Use heading as the
  2304. ;; description of the link.
  2305. (should
  2306. (let (org-store-link-props org-stored-links)
  2307. (org-test-with-temp-text-in-file "* H1"
  2308. (let ((file (buffer-file-name)))
  2309. (equal (format "[[file:%s::*H1][H1]]" file)
  2310. (org-store-link nil))))))
  2311. ;; On a headline, remove any link from description.
  2312. (should
  2313. (let (org-store-link-props org-stored-links)
  2314. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2315. (let ((file (buffer-file-name)))
  2316. (equal (format "[[file:%s::*%s][d]]"
  2317. file
  2318. (org-link-escape "[[#l][d]]"))
  2319. (org-store-link nil))))))
  2320. (should
  2321. (let (org-store-link-props org-stored-links)
  2322. (org-test-with-temp-text-in-file "* [[l]]"
  2323. (let ((file (buffer-file-name)))
  2324. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2325. (org-store-link nil))))))
  2326. (should
  2327. (let (org-store-link-props org-stored-links)
  2328. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2329. (let ((file (buffer-file-name)))
  2330. (equal (format "[[file:%s::*%s][d1 d2]]"
  2331. file
  2332. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2333. (org-store-link nil))))))
  2334. ;; On a named element, link to that element.
  2335. (should
  2336. (let (org-store-link-props org-stored-links)
  2337. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2338. (let ((file (buffer-file-name)))
  2339. (equal (format "[[file:%s::foo][foo]]" file)
  2340. (org-store-link nil)))))))
  2341. ;;; Node Properties
  2342. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2343. "Ensure properties accumulate in subtree drawers."
  2344. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2345. (org-babel-next-src-block)
  2346. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2347. (ert-deftest test-org/custom-properties ()
  2348. "Test custom properties specifications."
  2349. ;; Standard test.
  2350. (should
  2351. (let ((org-custom-properties '("FOO")))
  2352. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2353. (org-toggle-custom-properties-visibility)
  2354. (org-invisible-p2))))
  2355. ;; Properties are case-insensitive.
  2356. (should
  2357. (let ((org-custom-properties '("FOO")))
  2358. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2359. (org-toggle-custom-properties-visibility)
  2360. (org-invisible-p2))))
  2361. (should
  2362. (let ((org-custom-properties '("foo")))
  2363. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2364. (org-toggle-custom-properties-visibility)
  2365. (org-invisible-p2))))
  2366. ;; Multiple custom properties in the same drawer.
  2367. (should
  2368. (let ((org-custom-properties '("FOO" "BAR")))
  2369. (org-test-with-temp-text
  2370. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2371. (org-toggle-custom-properties-visibility)
  2372. (and (org-invisible-p2)
  2373. (not (progn (forward-line) (org-invisible-p2)))
  2374. (progn (forward-line) (org-invisible-p2))))))
  2375. ;; Hide custom properties with an empty value.
  2376. (should
  2377. (let ((org-custom-properties '("FOO")))
  2378. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2379. (org-toggle-custom-properties-visibility)
  2380. (org-invisible-p2))))
  2381. ;; Do not hide fake properties.
  2382. (should-not
  2383. (let ((org-custom-properties '("FOO")))
  2384. (org-test-with-temp-text ":FOO: val\n"
  2385. (org-toggle-custom-properties-visibility)
  2386. (org-invisible-p2))))
  2387. (should-not
  2388. (let ((org-custom-properties '("A")))
  2389. (org-test-with-temp-text
  2390. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2391. (org-toggle-custom-properties-visibility)
  2392. (org-invisible-p2)))))
  2393. ;;; Mark Region
  2394. (ert-deftest test-org/mark-subtree ()
  2395. "Test `org-mark-subtree' specifications."
  2396. ;; Error when point is before first headline.
  2397. (should-error
  2398. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2399. (progn (transient-mark-mode 1)
  2400. (org-mark-subtree))))
  2401. ;; Without argument, mark current subtree.
  2402. (should
  2403. (equal
  2404. '(12 32)
  2405. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2406. (progn (transient-mark-mode 1)
  2407. (forward-line 2)
  2408. (org-mark-subtree)
  2409. (list (region-beginning) (region-end))))))
  2410. ;; With an argument, move ARG up.
  2411. (should
  2412. (equal
  2413. '(1 32)
  2414. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2415. (progn (transient-mark-mode 1)
  2416. (forward-line 2)
  2417. (org-mark-subtree 1)
  2418. (list (region-beginning) (region-end))))))
  2419. ;; Do not get fooled by inlinetasks.
  2420. (when (featurep 'org-inlinetask)
  2421. (should
  2422. (= 1
  2423. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2424. (progn (transient-mark-mode 1)
  2425. (forward-line 1)
  2426. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2427. (region-beginning)))))))
  2428. ;;; Miscellaneous
  2429. (ert-deftest test-org/in-regexp ()
  2430. "Test `org-in-regexp' specifications."
  2431. ;; Standard tests.
  2432. (should
  2433. (org-test-with-temp-text "xx ab<point>c xx"
  2434. (org-in-regexp "abc")))
  2435. (should-not
  2436. (org-test-with-temp-text "xx abc <point>xx"
  2437. (org-in-regexp "abc")))
  2438. ;; Return non-nil even with multiple matching regexps in the same
  2439. ;; line.
  2440. (should
  2441. (org-test-with-temp-text "abc xx ab<point>c xx"
  2442. (org-in-regexp "abc")))
  2443. ;; With optional argument NLINES, check extra lines around point.
  2444. (should-not
  2445. (org-test-with-temp-text "A\nB<point>\nC"
  2446. (org-in-regexp "A\nB\nC")))
  2447. (should
  2448. (org-test-with-temp-text "A\nB<point>\nC"
  2449. (org-in-regexp "A\nB\nC" 1)))
  2450. (should-not
  2451. (org-test-with-temp-text "A\nB\nC<point>"
  2452. (org-in-regexp "A\nB\nC" 1)))
  2453. ;; When optional argument VISUALLY is non-nil, return nil if at
  2454. ;; regexp boundaries.
  2455. (should
  2456. (org-test-with-temp-text "xx abc<point> xx"
  2457. (org-in-regexp "abc")))
  2458. (should-not
  2459. (org-test-with-temp-text "xx abc<point> xx"
  2460. (org-in-regexp "abc" nil t))))
  2461. ;;; Navigation
  2462. (ert-deftest test-org/end-of-meta-data ()
  2463. "Test `org-end-of-meta-data' specifications."
  2464. ;; Skip planning line.
  2465. (should
  2466. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  2467. (org-end-of-meta-data)
  2468. (eobp)))
  2469. ;; Skip properties drawer.
  2470. (should
  2471. (org-test-with-temp-text
  2472. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  2473. (org-end-of-meta-data)
  2474. (eobp)))
  2475. ;; Skip both.
  2476. (should
  2477. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  2478. (org-end-of-meta-data)
  2479. (eobp)))
  2480. ;; Nothing to skip, go to first line.
  2481. (should
  2482. (org-test-with-temp-text "* Headline\nContents"
  2483. (org-end-of-meta-data)
  2484. (looking-at "Contents")))
  2485. ;; With option argument, skip empty lines, regular drawers and
  2486. ;; clocking lines.
  2487. (should
  2488. (org-test-with-temp-text "* Headline\n\nContents"
  2489. (org-end-of-meta-data t)
  2490. (looking-at "Contents")))
  2491. (should
  2492. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  2493. (org-end-of-meta-data t)
  2494. (looking-at "Contents")))
  2495. (should
  2496. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  2497. (org-end-of-meta-data t)
  2498. (looking-at "Contents")))
  2499. ;; Special case: do not skip incomplete drawers.
  2500. (should
  2501. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  2502. (org-end-of-meta-data t)
  2503. (looking-at ":LOGBOOK:")))
  2504. ;; Special case: Be careful about consecutive headlines.
  2505. (should-not
  2506. (org-test-with-temp-text "* H1\n*H2\nContents"
  2507. (org-end-of-meta-data t)
  2508. (looking-at "Contents"))))
  2509. (ert-deftest test-org/beginning-of-line ()
  2510. "Test `org-beginning-of-line' specifications."
  2511. ;; Move to beginning of line. If current line in invisible, move to
  2512. ;; beginning of visible line instead.
  2513. (should
  2514. (org-test-with-temp-text "Some text\nSome other text<point>"
  2515. (org-beginning-of-line)
  2516. (bolp)))
  2517. (should
  2518. (org-test-with-temp-text "* H1\n** H2<point>"
  2519. (org-overview)
  2520. (org-beginning-of-line)
  2521. (= (line-beginning-position) 1)))
  2522. ;; With `visual-line-mode' active, move to beginning of visual line.
  2523. (should-not
  2524. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2525. (visual-line-mode)
  2526. (dotimes (i 1000) (insert "very "))
  2527. (org-beginning-of-line)
  2528. (bolp)))
  2529. ;; In a wide headline, with `visual-line-mode', prefer going to the
  2530. ;; beginning of a visual line than to the logical beginning of line,
  2531. ;; even if special movement is active.
  2532. (should-not
  2533. (org-test-with-temp-text "* A <point>long headline"
  2534. (visual-line-mode)
  2535. (dotimes (i 1000) (insert "very "))
  2536. (goto-char (point-max))
  2537. (org-beginning-of-line)
  2538. (bobp)))
  2539. (should-not
  2540. (org-test-with-temp-text "* A <point>long headline"
  2541. (visual-line-mode)
  2542. (dotimes (i 1000) (insert "very "))
  2543. (goto-char (point-max))
  2544. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  2545. (bobp)))
  2546. ;; At an headline with special movement, first move at beginning of
  2547. ;; title, then at the beginning of line, rinse, repeat.
  2548. (should
  2549. (org-test-with-temp-text "* TODO Headline<point>"
  2550. (let ((org-special-ctrl-a/e t))
  2551. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  2552. (progn (org-beginning-of-line) (bolp))
  2553. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2554. (should
  2555. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2556. (let ((org-special-ctrl-a/e t))
  2557. (org-beginning-of-line)
  2558. (looking-at "Headline"))))
  2559. (should
  2560. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2561. (let ((org-special-ctrl-a/e '(t . nil)))
  2562. (org-beginning-of-line)
  2563. (looking-at "Headline"))))
  2564. (should-not
  2565. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2566. (let ((org-special-ctrl-a/e '(nil . nil)))
  2567. (org-beginning-of-line)
  2568. (looking-at "Headline"))))
  2569. ;; At an headline with reversed movement, first move to beginning of
  2570. ;; line, then to the beginning of title.
  2571. (should
  2572. (org-test-with-temp-text "* TODO Headline<point>"
  2573. (let ((org-special-ctrl-a/e 'reversed)
  2574. (this-command last-command))
  2575. (and (progn (org-beginning-of-line) (bolp))
  2576. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2577. (should
  2578. (org-test-with-temp-text "* TODO Headline<point>"
  2579. (let ((org-special-ctrl-a/e '(reversed . nil))
  2580. (this-command last-command))
  2581. (and (progn (org-beginning-of-line) (bolp))
  2582. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2583. (should-not
  2584. (org-test-with-temp-text "* TODO Headline<point>"
  2585. (let ((org-special-ctrl-a/e '(t . nil))
  2586. (this-command last-command))
  2587. (and (progn (org-beginning-of-line) (bolp))
  2588. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2589. ;; At an item with special movement, first move after to beginning
  2590. ;; of title, then to the beginning of line, rinse, repeat.
  2591. (should
  2592. (org-test-with-temp-text "- [ ] Item<point>"
  2593. (let ((org-special-ctrl-a/e t))
  2594. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  2595. (progn (org-beginning-of-line) (bolp))
  2596. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  2597. ;; At an item with reversed movement, first move to beginning of
  2598. ;; line, then to the beginning of title.
  2599. (should
  2600. (org-test-with-temp-text "- [X] Item<point>"
  2601. (let ((org-special-ctrl-a/e 'reversed)
  2602. (this-command last-command))
  2603. (and (progn (org-beginning-of-line) (bolp))
  2604. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  2605. ;; Leave point before invisible characters at column 0.
  2606. (should
  2607. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  2608. (let ((org-special-ctrl-a/e nil))
  2609. (org-beginning-of-line)
  2610. (bolp))))
  2611. (should
  2612. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  2613. (let ((org-special-ctrl-a/e t))
  2614. (org-beginning-of-line)
  2615. (bolp))))
  2616. (should
  2617. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  2618. (visual-line-mode)
  2619. (org-beginning-of-line)
  2620. (bolp)))
  2621. ;; Special case: Do not error when the buffer contains only a single
  2622. ;; asterisk.
  2623. (should
  2624. (org-test-with-temp-text "*<point>"
  2625. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  2626. (should
  2627. (org-test-with-temp-text "*<point>"
  2628. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  2629. (ert-deftest test-org/end-of-line ()
  2630. "Test `org-end-of-line' specifications."
  2631. ;; Standard test.
  2632. (should
  2633. (org-test-with-temp-text "Some text\nSome other text"
  2634. (org-end-of-line)
  2635. (eolp)))
  2636. ;; With `visual-line-mode' active, move to end of visible line.
  2637. ;; However, never go past ellipsis.
  2638. (should-not
  2639. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2640. (visual-line-mode)
  2641. (dotimes (i 1000) (insert "very "))
  2642. (goto-char (point-min))
  2643. (org-end-of-line)
  2644. (eolp)))
  2645. (should-not
  2646. (org-test-with-temp-text "* A short headline\nSome contents"
  2647. (visual-line-mode)
  2648. (org-overview)
  2649. (org-end-of-line)
  2650. (eobp)))
  2651. ;; In a wide headline, with `visual-line-mode', prefer going to end
  2652. ;; of visible line if tags, or end of line, are farther.
  2653. (should-not
  2654. (org-test-with-temp-text "* A <point>long headline"
  2655. (visual-line-mode)
  2656. (dotimes (i 1000) (insert "very "))
  2657. (goto-char (point-min))
  2658. (org-end-of-line)
  2659. (eolp)))
  2660. (should-not
  2661. (org-test-with-temp-text "* A <point>long headline :tag:"
  2662. (visual-line-mode)
  2663. (dotimes (i 1000) (insert "very "))
  2664. (goto-char (point-min))
  2665. (org-end-of-line)
  2666. (eolp)))
  2667. ;; At an headline without special movement, go to end of line.
  2668. ;; However, never go past ellipsis.
  2669. (should
  2670. (org-test-with-temp-text "* Headline2b :tag:\n"
  2671. (let ((org-special-ctrl-a/e nil))
  2672. (and (progn (org-end-of-line) (eolp))
  2673. (progn (org-end-of-line) (eolp))))))
  2674. (should
  2675. (org-test-with-temp-text "* Headline2b :tag:\n"
  2676. (let ((org-special-ctrl-a/e '(t . nil)))
  2677. (and (progn (org-end-of-line) (eolp))
  2678. (progn (org-end-of-line) (eolp))))))
  2679. (should
  2680. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2681. (org-overview)
  2682. (let ((org-special-ctrl-a/e nil))
  2683. (org-end-of-line)
  2684. (= 1 (line-beginning-position)))))
  2685. ;; At an headline with special movement, first move before tags,
  2686. ;; then at the end of line, rinse, repeat. However, never go past
  2687. ;; ellipsis.
  2688. (should
  2689. (org-test-with-temp-text "* Headline1 :tag:\n"
  2690. (let ((org-special-ctrl-a/e t))
  2691. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  2692. (progn (org-end-of-line) (eolp))
  2693. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2694. (should
  2695. (org-test-with-temp-text "* Headline1 :tag:\n"
  2696. (let ((org-special-ctrl-a/e '(nil . t)))
  2697. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  2698. (progn (org-end-of-line) (eolp))
  2699. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2700. (should-not
  2701. (org-test-with-temp-text "* Headline1 :tag:\n"
  2702. (let ((org-special-ctrl-a/e '(nil . nil)))
  2703. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  2704. (progn (org-end-of-line) (eolp))
  2705. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2706. (should
  2707. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2708. (org-overview)
  2709. (let ((org-special-ctrl-a/e t))
  2710. (org-end-of-line)
  2711. (org-end-of-line)
  2712. (= 1 (line-beginning-position)))))
  2713. ;; At an headline, with reversed movement, first go to end of line,
  2714. ;; then before tags. However, never go past ellipsis.
  2715. (should
  2716. (org-test-with-temp-text "* Headline3 :tag:\n"
  2717. (let ((org-special-ctrl-a/e 'reversed)
  2718. (this-command last-command))
  2719. (and (progn (org-end-of-line) (eolp))
  2720. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2721. (should
  2722. (org-test-with-temp-text "* Headline3 :tag:\n"
  2723. (let ((org-special-ctrl-a/e '(nil . reversed))
  2724. (this-command last-command))
  2725. (and (progn (org-end-of-line) (eolp))
  2726. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2727. (should-not
  2728. (org-test-with-temp-text "* Headline3 :tag:\n"
  2729. (let ((org-special-ctrl-a/e '(nil . t))
  2730. (this-command last-command))
  2731. (and (progn (org-end-of-line) (eolp))
  2732. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2733. (should
  2734. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2735. (org-overview)
  2736. (let ((org-special-ctrl-a/e 'reversed))
  2737. (org-end-of-line)
  2738. (= 1 (line-beginning-position)))))
  2739. ;; At a block without hidden contents.
  2740. (should
  2741. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2742. (progn (org-end-of-line) (eolp))))
  2743. ;; At a block with hidden contents.
  2744. (should-not
  2745. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2746. (let ((org-special-ctrl-a/e t))
  2747. (org-hide-block-toggle)
  2748. (org-end-of-line)
  2749. (eobp))))
  2750. ;; Get past invisible characters at the end of line.
  2751. (should
  2752. (org-test-with-temp-text "[[http://orgmode.org]]"
  2753. (org-end-of-line)
  2754. (eolp))))
  2755. (ert-deftest test-org/open-line ()
  2756. "Test `org-open-line' specifications."
  2757. ;; Call `open-line' outside of tables.
  2758. (should
  2759. (equal "\nText"
  2760. (org-test-with-temp-text "Text"
  2761. (org-open-line 1)
  2762. (buffer-string))))
  2763. ;; At a table, create a row above.
  2764. (should
  2765. (equal "\n| |\n| a |"
  2766. (org-test-with-temp-text "\n<point>| a |"
  2767. (org-open-line 1)
  2768. (buffer-string))))
  2769. ;; At the very first character of the buffer, also call `open-line'.
  2770. (should
  2771. (equal "\n| a |"
  2772. (org-test-with-temp-text "| a |"
  2773. (org-open-line 1)
  2774. (buffer-string))))
  2775. ;; Narrowing does not count.
  2776. (should
  2777. (equal "Text\n| |\n| a |"
  2778. (org-test-with-temp-text "Text\n<point>| a |"
  2779. (narrow-to-region (point) (point-max))
  2780. (org-open-line 1)
  2781. (widen)
  2782. (buffer-string)))))
  2783. (ert-deftest test-org/forward-sentence ()
  2784. "Test `org-forward-sentence' specifications."
  2785. ;; At the end of a table cell, move to the end of the next one.
  2786. (should
  2787. (org-test-with-temp-text "| a<point> | b |"
  2788. (org-forward-sentence)
  2789. (looking-at " |$")))
  2790. ;; Elsewhere in a cell, move to its end.
  2791. (should
  2792. (org-test-with-temp-text "| a<point>c | b |"
  2793. (org-forward-sentence)
  2794. (looking-at " | b |$")))
  2795. ;; Otherwise, simply call `forward-sentence'.
  2796. (should
  2797. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2798. (org-forward-sentence)
  2799. (looking-at " Sentence 2.")))
  2800. (should
  2801. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2802. (org-forward-sentence)
  2803. (org-forward-sentence)
  2804. (eobp)))
  2805. ;; At the end of an element, jump to the next one, without stopping
  2806. ;; on blank lines in-between.
  2807. (should
  2808. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  2809. (org-forward-sentence)
  2810. (eobp))))
  2811. (ert-deftest test-org/backward-sentence ()
  2812. "Test `org-backward-sentence' specifications."
  2813. ;; At the beginning of a table cell, move to the beginning of the
  2814. ;; previous one.
  2815. (should
  2816. (org-test-with-temp-text "| a | <point>b |"
  2817. (org-backward-sentence)
  2818. (looking-at "a | b |$")))
  2819. ;; Elsewhere in a cell, move to its beginning.
  2820. (should
  2821. (org-test-with-temp-text "| a | b<point>c |"
  2822. (org-backward-sentence)
  2823. (looking-at "bc |$")))
  2824. ;; Otherwise, simply call `backward-sentence'.
  2825. (should
  2826. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2827. (org-backward-sentence)
  2828. (looking-at "Sentence 2.")))
  2829. (should
  2830. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2831. (org-backward-sentence)
  2832. (org-backward-sentence)
  2833. (bobp)))
  2834. ;; Make sure to hit the beginning of a sentence on the same line as
  2835. ;; an item.
  2836. (should
  2837. (org-test-with-temp-text "- Line 1\n line <point>2."
  2838. (org-backward-sentence)
  2839. (looking-at "Line 1"))))
  2840. (ert-deftest test-org/forward-paragraph ()
  2841. "Test `org-forward-paragraph' specifications."
  2842. ;; At end of buffer, return an error.
  2843. (should-error
  2844. (org-test-with-temp-text "Paragraph"
  2845. (goto-char (point-max))
  2846. (org-forward-paragraph)))
  2847. ;; Standard test.
  2848. (should
  2849. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2850. (org-forward-paragraph)
  2851. (looking-at "P2")))
  2852. ;; Ignore depth.
  2853. (should
  2854. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  2855. (org-forward-paragraph)
  2856. (looking-at "P1")))
  2857. ;; Do not enter elements with invisible contents.
  2858. (should
  2859. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  2860. (org-hide-block-toggle)
  2861. (org-forward-paragraph)
  2862. (looking-at "P3")))
  2863. ;; On an affiliated keyword, jump to the beginning of the element.
  2864. (should
  2865. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  2866. (org-forward-paragraph)
  2867. (looking-at "Para")))
  2868. ;; On an item or a footnote definition, move to the second element
  2869. ;; inside, if any.
  2870. (should
  2871. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  2872. (org-forward-paragraph)
  2873. (looking-at " Paragraph")))
  2874. (should
  2875. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  2876. (org-forward-paragraph)
  2877. (looking-at "Paragraph")))
  2878. ;; On an item, or a footnote definition, when the first line is
  2879. ;; empty, move to the first item.
  2880. (should
  2881. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  2882. (org-forward-paragraph)
  2883. (looking-at " Paragraph")))
  2884. (should
  2885. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  2886. (org-forward-paragraph)
  2887. (looking-at "Paragraph")))
  2888. ;; On a table (resp. a property drawer) do not move through table
  2889. ;; rows (resp. node properties).
  2890. (should
  2891. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  2892. (org-forward-paragraph)
  2893. (looking-at "Paragraph")))
  2894. (should
  2895. (org-test-with-temp-text
  2896. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  2897. (org-forward-paragraph)
  2898. (looking-at "Paragraph")))
  2899. ;; On a verse or source block, stop after blank lines.
  2900. (should
  2901. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  2902. (org-forward-paragraph)
  2903. (looking-at "L2")))
  2904. (should
  2905. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  2906. (org-forward-paragraph)
  2907. (looking-at "L2"))))
  2908. (ert-deftest test-org/backward-paragraph ()
  2909. "Test `org-backward-paragraph' specifications."
  2910. ;; Error at beginning of buffer.
  2911. (should-error
  2912. (org-test-with-temp-text "Paragraph"
  2913. (org-backward-paragraph)))
  2914. ;; Regular test.
  2915. (should
  2916. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2917. (goto-char (point-max))
  2918. (org-backward-paragraph)
  2919. (looking-at "P3")))
  2920. (should
  2921. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2922. (goto-char (point-max))
  2923. (beginning-of-line)
  2924. (org-backward-paragraph)
  2925. (looking-at "P2")))
  2926. ;; Ignore depth.
  2927. (should
  2928. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  2929. (goto-char (point-max))
  2930. (beginning-of-line)
  2931. (org-backward-paragraph)
  2932. (looking-at "P2")))
  2933. ;; Ignore invisible elements.
  2934. (should
  2935. (org-test-with-temp-text "* H1\n P1\n* H2"
  2936. (org-cycle)
  2937. (goto-char (point-max))
  2938. (beginning-of-line)
  2939. (org-backward-paragraph)
  2940. (bobp)))
  2941. ;; On an affiliated keyword, jump to the first one.
  2942. (should
  2943. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  2944. (search-forward "c2")
  2945. (org-backward-paragraph)
  2946. (looking-at "#\\+name")))
  2947. ;; On the second element in an item or a footnote definition, jump
  2948. ;; to item or the definition.
  2949. (should
  2950. (org-test-with-temp-text "- line1\n\n line2"
  2951. (goto-char (point-max))
  2952. (beginning-of-line)
  2953. (org-backward-paragraph)
  2954. (looking-at "- line1")))
  2955. (should
  2956. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  2957. (goto-char (point-max))
  2958. (beginning-of-line)
  2959. (org-backward-paragraph)
  2960. (looking-at "\\[fn:1\\] line1")))
  2961. ;; On a table (resp. a property drawer), ignore table rows
  2962. ;; (resp. node properties).
  2963. (should
  2964. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  2965. (goto-char (point-max))
  2966. (beginning-of-line)
  2967. (org-backward-paragraph)
  2968. (bobp)))
  2969. (should
  2970. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  2971. (org-backward-paragraph)
  2972. (looking-at ":PROPERTIES:")))
  2973. ;; On a source or verse block, stop before blank lines.
  2974. (should
  2975. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  2976. (search-forward "L3")
  2977. (beginning-of-line)
  2978. (org-backward-paragraph)
  2979. (looking-at "L2")))
  2980. (should
  2981. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  2982. (search-forward "L3")
  2983. (beginning-of-line)
  2984. (org-backward-paragraph)
  2985. (looking-at "L2"))))
  2986. (ert-deftest test-org/forward-element ()
  2987. "Test `org-forward-element' specifications."
  2988. ;; 1. At EOB: should error.
  2989. (org-test-with-temp-text "Some text\n"
  2990. (goto-char (point-max))
  2991. (should-error (org-forward-element)))
  2992. ;; 2. Standard move: expected to ignore blank lines.
  2993. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  2994. (org-forward-element)
  2995. (should (looking-at (regexp-quote "Second paragraph."))))
  2996. ;; 3. Headline tests.
  2997. (org-test-with-temp-text "
  2998. * Head 1
  2999. ** Head 1.1
  3000. *** Head 1.1.1
  3001. ** Head 1.2"
  3002. ;; 3.1. At an headline beginning: move to next headline at the
  3003. ;; same level.
  3004. (goto-line 3)
  3005. (org-forward-element)
  3006. (should (looking-at (regexp-quote "** Head 1.2")))
  3007. ;; 3.2. At an headline beginning: move to parent headline if no
  3008. ;; headline at the same level.
  3009. (goto-line 3)
  3010. (org-forward-element)
  3011. (should (looking-at (regexp-quote "** Head 1.2"))))
  3012. ;; 4. Greater element tests.
  3013. (org-test-with-temp-text
  3014. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  3015. ;; 4.1. At a greater element: expected to skip contents.
  3016. (org-forward-element)
  3017. (should (looking-at (regexp-quote "Outside.")))
  3018. ;; 4.2. At the end of greater element contents: expected to skip
  3019. ;; to the end of the greater element.
  3020. (goto-line 2)
  3021. (org-forward-element)
  3022. (should (looking-at (regexp-quote "Outside."))))
  3023. ;; 5. List tests.
  3024. (org-test-with-temp-text "
  3025. - item1
  3026. - sub1
  3027. - sub2
  3028. - sub3
  3029. Inner paragraph.
  3030. - item2
  3031. Outside."
  3032. ;; 5.1. At list top point: expected to move to the element after
  3033. ;; the list.
  3034. (goto-line 2)
  3035. (org-forward-element)
  3036. (should (looking-at (regexp-quote "Outside.")))
  3037. ;; 5.2. Special case: at the first line of a sub-list, but not at
  3038. ;; beginning of line, move to next item.
  3039. (goto-line 2)
  3040. (forward-char)
  3041. (org-forward-element)
  3042. (should (looking-at "- item2"))
  3043. (goto-line 4)
  3044. (forward-char)
  3045. (org-forward-element)
  3046. (should (looking-at " - sub2"))
  3047. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  3048. (goto-line 4)
  3049. (org-forward-element)
  3050. (should (looking-at (regexp-quote " Inner paragraph.")))
  3051. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  3052. (goto-line 8)
  3053. (org-forward-element)
  3054. (should (looking-at (regexp-quote " Inner paragraph.")))
  3055. ;; 5.5. At an item: expected to move to next item, if any.
  3056. (goto-line 6)
  3057. (org-forward-element)
  3058. (should (looking-at " - sub3"))))
  3059. (ert-deftest test-org/backward-element ()
  3060. "Test `org-backward-element' specifications."
  3061. ;; 1. Should error at BOB.
  3062. (org-test-with-temp-text " \nParagraph."
  3063. (should-error (org-backward-element)))
  3064. ;; 2. Should move at BOB when called on the first element in buffer.
  3065. (should
  3066. (org-test-with-temp-text "\n#+TITLE: test"
  3067. (progn (forward-line)
  3068. (org-backward-element)
  3069. (bobp))))
  3070. ;; 3. Not at the beginning of an element: move at its beginning.
  3071. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3072. (goto-line 3)
  3073. (end-of-line)
  3074. (org-backward-element)
  3075. (should (looking-at (regexp-quote "Paragraph2."))))
  3076. ;; 4. Headline tests.
  3077. (org-test-with-temp-text "
  3078. * Head 1
  3079. ** Head 1.1
  3080. *** Head 1.1.1
  3081. ** Head 1.2"
  3082. ;; 4.1. At an headline beginning: move to previous headline at the
  3083. ;; same level.
  3084. (goto-line 5)
  3085. (org-backward-element)
  3086. (should (looking-at (regexp-quote "** Head 1.1")))
  3087. ;; 4.2. At an headline beginning: move to parent headline if no
  3088. ;; headline at the same level.
  3089. (goto-line 3)
  3090. (org-backward-element)
  3091. (should (looking-at (regexp-quote "* Head 1")))
  3092. ;; 4.3. At the first top-level headline: should error.
  3093. (goto-line 2)
  3094. (should-error (org-backward-element)))
  3095. ;; 5. At beginning of first element inside a greater element:
  3096. ;; expected to move to greater element's beginning.
  3097. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  3098. (goto-line 3)
  3099. (org-backward-element)
  3100. (should (looking-at "#\\+BEGIN_CENTER")))
  3101. ;; 6. At the beginning of the first element in a section: should
  3102. ;; move back to headline, if any.
  3103. (should
  3104. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  3105. (progn (goto-char (point-max))
  3106. (beginning-of-line)
  3107. (org-backward-element)
  3108. (org-at-heading-p))))
  3109. ;; 7. List tests.
  3110. (org-test-with-temp-text "
  3111. - item1
  3112. - sub1
  3113. - sub2
  3114. - sub3
  3115. Inner paragraph.
  3116. - item2
  3117. Outside."
  3118. ;; 7.1. At beginning of sub-list: expected to move to the
  3119. ;; paragraph before it.
  3120. (goto-line 4)
  3121. (org-backward-element)
  3122. (should (looking-at "item1"))
  3123. ;; 7.2. At an item in a list: expected to move at previous item.
  3124. (goto-line 8)
  3125. (org-backward-element)
  3126. (should (looking-at " - sub2"))
  3127. (goto-line 12)
  3128. (org-backward-element)
  3129. (should (looking-at "- item1"))
  3130. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3131. ;; beginning.
  3132. (goto-line 10)
  3133. (org-backward-element)
  3134. (should (looking-at " - sub1"))
  3135. (goto-line 15)
  3136. (org-backward-element)
  3137. (should (looking-at "- item1"))
  3138. ;; 7.4. At blank-lines before list end: expected to move to top
  3139. ;; item.
  3140. (goto-line 14)
  3141. (org-backward-element)
  3142. (should (looking-at "- item1"))))
  3143. (ert-deftest test-org/up-element ()
  3144. "Test `org-up-element' specifications."
  3145. ;; 1. At BOB or with no surrounding element: should error.
  3146. (org-test-with-temp-text "Paragraph."
  3147. (should-error (org-up-element)))
  3148. (org-test-with-temp-text "* Head1\n* Head2"
  3149. (goto-line 2)
  3150. (should-error (org-up-element)))
  3151. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3152. (goto-line 3)
  3153. (should-error (org-up-element)))
  3154. ;; 2. At an headline: move to parent headline.
  3155. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3156. (goto-line 3)
  3157. (org-up-element)
  3158. (should (looking-at "\\* Head1")))
  3159. ;; 3. Inside a greater element: move to greater element beginning.
  3160. (org-test-with-temp-text
  3161. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3162. (goto-line 3)
  3163. (org-up-element)
  3164. (should (looking-at "#\\+BEGIN_CENTER")))
  3165. ;; 4. List tests.
  3166. (org-test-with-temp-text "* Top
  3167. - item1
  3168. - sub1
  3169. - sub2
  3170. Paragraph within sub2.
  3171. - item2"
  3172. ;; 4.1. Within an item: move to the item beginning.
  3173. (goto-line 8)
  3174. (org-up-element)
  3175. (should (looking-at " - sub2"))
  3176. ;; 4.2. At an item in a sub-list: move to parent item.
  3177. (goto-line 4)
  3178. (org-up-element)
  3179. (should (looking-at "- item1"))
  3180. ;; 4.3. At an item in top list: move to beginning of whole list.
  3181. (goto-line 10)
  3182. (org-up-element)
  3183. (should (looking-at "- item1"))
  3184. ;; 4.4. Special case. At very top point: should move to parent of
  3185. ;; list.
  3186. (goto-line 2)
  3187. (org-up-element)
  3188. (should (looking-at "\\* Top"))))
  3189. (ert-deftest test-org/down-element ()
  3190. "Test `org-down-element' specifications."
  3191. ;; Error when the element hasn't got a recursive type.
  3192. (org-test-with-temp-text "Paragraph."
  3193. (should-error (org-down-element)))
  3194. ;; Error when the element has no contents
  3195. (org-test-with-temp-text "* Headline"
  3196. (should-error (org-down-element)))
  3197. ;; When at a plain-list, move to first item.
  3198. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3199. (goto-line 2)
  3200. (org-down-element)
  3201. (should (looking-at " - Item 1.1")))
  3202. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3203. (org-down-element)
  3204. (should (looking-at " Item 1")))
  3205. ;; When at a table, move to first row
  3206. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3207. (org-down-element)
  3208. (should (looking-at " a | b |")))
  3209. ;; Otherwise, move inside the greater element.
  3210. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3211. (org-down-element)
  3212. (should (looking-at "Paragraph"))))
  3213. (ert-deftest test-org/drag-element-backward ()
  3214. "Test `org-drag-element-backward' specifications."
  3215. ;; Standard test.
  3216. (should
  3217. (equal
  3218. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3219. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3220. (org-drag-element-backward)
  3221. (buffer-string))))
  3222. (should
  3223. (equal
  3224. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3225. (org-test-with-temp-text
  3226. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3227. (org-drag-element-backward)
  3228. (buffer-string))))
  3229. ;; Preserve blank lines.
  3230. (should
  3231. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3232. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3233. (org-drag-element-backward)
  3234. (buffer-string))))
  3235. ;; Preserve column.
  3236. (should
  3237. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3238. (org-drag-element-backward)
  3239. (looking-at-p "2")))
  3240. ;; Error when trying to move first element of buffer.
  3241. (should-error
  3242. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3243. (org-drag-element-backward))
  3244. :type 'user-error)
  3245. ;; Error when trying to swap nested elements.
  3246. (should-error
  3247. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  3248. (org-drag-element-backward))
  3249. :type 'user-error)
  3250. ;; Error when trying to swap an headline element and a non-headline
  3251. ;; element.
  3252. (should-error
  3253. (org-test-with-temp-text "Test.\n<point>* Head 1"
  3254. (org-drag-element-backward))
  3255. :type 'error)
  3256. ;; Error when called before first element.
  3257. (should-error
  3258. (org-test-with-temp-text "\n<point>"
  3259. (org-drag-element-backward))
  3260. :type 'user-error)
  3261. ;; Preserve visibility of elements and their contents.
  3262. (should
  3263. (equal '((63 . 82) (26 . 48))
  3264. (org-test-with-temp-text "
  3265. #+BEGIN_CENTER
  3266. Text.
  3267. #+END_CENTER
  3268. - item 1
  3269. #+BEGIN_QUOTE
  3270. Text.
  3271. #+END_QUOTE"
  3272. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3273. (search-backward "- item 1")
  3274. (org-drag-element-backward)
  3275. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3276. (overlays-in (point-min) (point-max))))))
  3277. ;; Pathological case: handle call with point in blank lines right
  3278. ;; after a headline.
  3279. (should
  3280. (equal "* H2\n* H1\nText\n\n"
  3281. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  3282. (org-drag-element-backward)
  3283. (buffer-string)))))
  3284. (ert-deftest test-org/drag-element-forward ()
  3285. "Test `org-drag-element-forward' specifications."
  3286. ;; 1. Error when trying to move first element of buffer.
  3287. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3288. (goto-line 3)
  3289. (should-error (org-drag-element-forward)))
  3290. ;; 2. Error when trying to swap nested elements.
  3291. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3292. (forward-line)
  3293. (should-error (org-drag-element-forward)))
  3294. ;; 3. Error when trying to swap a non-headline element and an
  3295. ;; headline.
  3296. (org-test-with-temp-text "Test.\n* Head 1"
  3297. (should-error (org-drag-element-forward)))
  3298. ;; 4. Error when called before first element.
  3299. (should-error
  3300. (org-test-with-temp-text "\n"
  3301. (forward-line)
  3302. (org-drag-element-backward))
  3303. :type 'user-error)
  3304. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3305. ;; between elements.
  3306. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3307. (search-forward "graph")
  3308. (org-drag-element-forward)
  3309. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3310. (should (looking-at " 1")))
  3311. ;; 5. Preserve visibility of elements and their contents.
  3312. (org-test-with-temp-text "
  3313. #+BEGIN_CENTER
  3314. Text.
  3315. #+END_CENTER
  3316. - item 1
  3317. #+BEGIN_QUOTE
  3318. Text.
  3319. #+END_QUOTE"
  3320. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3321. (search-backward "#+BEGIN_CENTER")
  3322. (org-drag-element-forward)
  3323. (should
  3324. (equal
  3325. '((63 . 82) (26 . 48))
  3326. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3327. (overlays-in (point-min) (point-max)))))))
  3328. (ert-deftest test-org/next-block ()
  3329. "Test `org-next-block' specifications."
  3330. ;; Regular test.
  3331. (should
  3332. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3333. (org-next-block 1)
  3334. (looking-at "#\\+BEGIN_CENTER")))
  3335. ;; Ignore case.
  3336. (should
  3337. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3338. (let ((case-fold-search nil))
  3339. (org-next-block 1)
  3340. (looking-at "#\\+begin_center"))))
  3341. ;; Ignore current line.
  3342. (should
  3343. (org-test-with-temp-text
  3344. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3345. (org-next-block 1)
  3346. (looking-at "#\\+BEGIN_CENTER")))
  3347. ;; Throw an error when no block is found.
  3348. (should-error
  3349. (org-test-with-temp-text "Paragraph"
  3350. (org-next-block 1)))
  3351. ;; With an argument, skip many blocks at once.
  3352. (should
  3353. (org-test-with-temp-text
  3354. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3355. (org-next-block 2)
  3356. (looking-at "#\\+BEGIN_QUOTE")))
  3357. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3358. (should
  3359. (org-test-with-temp-text
  3360. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3361. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3362. (looking-at "#\\+BEGIN_QUOTE")))
  3363. ;; Optional argument is also case-insensitive.
  3364. (should
  3365. (org-test-with-temp-text
  3366. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3367. (let ((case-fold-search nil))
  3368. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3369. (looking-at "#\\+begin_quote")))))
  3370. (ert-deftest test-org/previous-block ()
  3371. "Test `org-previous-block' specifications."
  3372. ;; Regular test.
  3373. (should
  3374. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  3375. (org-previous-block 1)
  3376. (looking-at "#\\+BEGIN_CENTER")))
  3377. ;; Ignore case.
  3378. (should
  3379. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  3380. (let ((case-fold-search nil))
  3381. (org-previous-block 1)
  3382. (looking-at "#\\+begin_center"))))
  3383. ;; Ignore current line.
  3384. (should
  3385. (org-test-with-temp-text
  3386. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  3387. (org-previous-block 1)
  3388. (looking-at "#\\+BEGIN_QUOTE")))
  3389. ;; Throw an error when no block is found.
  3390. (should-error
  3391. (org-test-with-temp-text "Paragraph<point>"
  3392. (org-previous-block 1)))
  3393. ;; With an argument, skip many blocks at once.
  3394. (should
  3395. (org-test-with-temp-text
  3396. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3397. (org-previous-block 2)
  3398. (looking-at "#\\+BEGIN_CENTER")))
  3399. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3400. (should
  3401. (org-test-with-temp-text
  3402. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3403. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3404. (looking-at "#\\+BEGIN_QUOTE")))
  3405. ;; Optional argument is also case-insensitive.
  3406. (should
  3407. (org-test-with-temp-text
  3408. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  3409. (let ((case-fold-search nil))
  3410. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3411. (looking-at "#\\+begin_quote")))))
  3412. ;;; Outline structure
  3413. (ert-deftest test-org/demote ()
  3414. "Test `org-demote' specifications."
  3415. ;; Add correct number of stars according to `org-odd-levels-only'.
  3416. (should
  3417. (= 2
  3418. (org-test-with-temp-text "* H"
  3419. (let ((org-odd-levels-only nil)) (org-demote))
  3420. (org-current-level))))
  3421. (should
  3422. (= 3
  3423. (org-test-with-temp-text "* H"
  3424. (let ((org-odd-levels-only t)) (org-demote))
  3425. (org-current-level))))
  3426. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3427. (should
  3428. (org-test-with-temp-text "* H :tag:"
  3429. (let ((org-tags-column 10)
  3430. (org-auto-align-tags t)
  3431. (org-odd-levels-only nil))
  3432. (org-demote))
  3433. (org-move-to-column 10)
  3434. (looking-at-p ":tag:$")))
  3435. (should-not
  3436. (org-test-with-temp-text "* H :tag:"
  3437. (let ((org-tags-column 10)
  3438. (org-auto-align-tags nil)
  3439. (org-odd-levels-only nil))
  3440. (org-demote))
  3441. (org-move-to-column 10)
  3442. (looking-at-p ":tag:$")))
  3443. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3444. ;; info and property drawers accordingly.
  3445. (should
  3446. (= 3
  3447. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3448. (let ((org-odd-levels-only nil)
  3449. (org-adapt-indentation t))
  3450. (org-demote))
  3451. (forward-line)
  3452. (org-get-indentation))))
  3453. (should
  3454. (= 3
  3455. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3456. (let ((org-odd-levels-only nil)
  3457. (org-adapt-indentation t))
  3458. (org-demote))
  3459. (forward-line)
  3460. (org-get-indentation))))
  3461. (should-not
  3462. (= 3
  3463. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3464. (let ((org-odd-levels-only nil)
  3465. (org-adapt-indentation nil))
  3466. (org-demote))
  3467. (forward-line)
  3468. (org-get-indentation))))
  3469. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3470. ;; section accordingly. Ignore, however, footnote definitions and
  3471. ;; inlinetasks boundaries.
  3472. (should
  3473. (= 3
  3474. (org-test-with-temp-text "* H\n Paragraph"
  3475. (let ((org-odd-levels-only nil)
  3476. (org-adapt-indentation t))
  3477. (org-demote))
  3478. (forward-line)
  3479. (org-get-indentation))))
  3480. (should
  3481. (= 2
  3482. (org-test-with-temp-text "* H\n Paragraph"
  3483. (let ((org-odd-levels-only nil)
  3484. (org-adapt-indentation nil))
  3485. (org-demote))
  3486. (forward-line)
  3487. (org-get-indentation))))
  3488. (should
  3489. (zerop
  3490. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  3491. (let ((org-odd-levels-only nil)
  3492. (org-adapt-indentation t))
  3493. (org-demote))
  3494. (goto-char (point-max))
  3495. (org-get-indentation))))
  3496. (should
  3497. (= 3
  3498. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  3499. (let ((org-odd-levels-only nil)
  3500. (org-adapt-indentation t))
  3501. (org-demote))
  3502. (goto-char (point-max))
  3503. (org-get-indentation))))
  3504. (when (featurep 'org-inlinetask)
  3505. (should
  3506. (zerop
  3507. (let ((org-inlinetask-min-level 5)
  3508. (org-adapt-indentation t))
  3509. (org-test-with-temp-text "* H\n***** I\n***** END"
  3510. (org-demote)
  3511. (forward-line)
  3512. (org-get-indentation))))))
  3513. (when (featurep 'org-inlinetask)
  3514. (should
  3515. (= 3
  3516. (let ((org-inlinetask-min-level 5)
  3517. (org-adapt-indentation t))
  3518. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  3519. (org-demote)
  3520. (forward-line 2)
  3521. (org-get-indentation))))))
  3522. ;; Ignore contents of source blocks or example blocks when
  3523. ;; indentation should be preserved (through
  3524. ;; `org-src-preserve-indentation' or "-i" flag).
  3525. (should-not
  3526. (zerop
  3527. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3528. (let ((org-adapt-indentation t)
  3529. (org-src-preserve-indentation nil))
  3530. (org-demote))
  3531. (forward-line 2)
  3532. (org-get-indentation))))
  3533. (should
  3534. (zerop
  3535. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3536. (let ((org-adapt-indentation t)
  3537. (org-src-preserve-indentation t))
  3538. (org-demote))
  3539. (forward-line 2)
  3540. (org-get-indentation))))
  3541. (should
  3542. (zerop
  3543. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3544. (let ((org-adapt-indentation t)
  3545. (org-src-preserve-indentation t))
  3546. (org-demote))
  3547. (forward-line 2)
  3548. (org-get-indentation))))
  3549. (should
  3550. (zerop
  3551. (org-test-with-temp-text
  3552. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  3553. (let ((org-adapt-indentation t)
  3554. (org-src-preserve-indentation nil))
  3555. (org-demote))
  3556. (forward-line 2)
  3557. (org-get-indentation)))))
  3558. (ert-deftest test-org/promote ()
  3559. "Test `org-promote' specifications."
  3560. ;; Return an error if headline is to be promoted to level 0, unless
  3561. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  3562. ;; headline becomes a comment.
  3563. (should-error
  3564. (org-test-with-temp-text "* H"
  3565. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  3566. (should
  3567. (equal "# H"
  3568. (org-test-with-temp-text "* H"
  3569. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  3570. (buffer-string))))
  3571. ;; Remove correct number of stars according to
  3572. ;; `org-odd-levels-only'.
  3573. (should
  3574. (= 2
  3575. (org-test-with-temp-text "*** H"
  3576. (let ((org-odd-levels-only nil)) (org-promote))
  3577. (org-current-level))))
  3578. (should
  3579. (= 1
  3580. (org-test-with-temp-text "*** H"
  3581. (let ((org-odd-levels-only t)) (org-promote))
  3582. (org-current-level))))
  3583. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3584. (should
  3585. (org-test-with-temp-text "** H :tag:"
  3586. (let ((org-tags-column 10)
  3587. (org-auto-align-tags t)
  3588. (org-odd-levels-only nil))
  3589. (org-promote))
  3590. (org-move-to-column 10)
  3591. (looking-at-p ":tag:$")))
  3592. (should-not
  3593. (org-test-with-temp-text "** H :tag:"
  3594. (let ((org-tags-column 10)
  3595. (org-auto-align-tags nil)
  3596. (org-odd-levels-only nil))
  3597. (org-promote))
  3598. (org-move-to-column 10)
  3599. (looking-at-p ":tag:$")))
  3600. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3601. ;; info and property drawers.
  3602. (should
  3603. (= 2
  3604. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3605. (let ((org-odd-levels-only nil)
  3606. (org-adapt-indentation t))
  3607. (org-promote))
  3608. (forward-line)
  3609. (org-get-indentation))))
  3610. (should
  3611. (= 2
  3612. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3613. (let ((org-odd-levels-only nil)
  3614. (org-adapt-indentation t))
  3615. (org-promote))
  3616. (forward-line)
  3617. (org-get-indentation))))
  3618. (should-not
  3619. (= 2
  3620. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3621. (let ((org-odd-levels-only nil)
  3622. (org-adapt-indentation nil))
  3623. (org-promote))
  3624. (forward-line)
  3625. (org-get-indentation))))
  3626. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3627. ;; section accordingly. Ignore, however, footnote definitions and
  3628. ;; inlinetasks boundaries.
  3629. (should
  3630. (= 2
  3631. (org-test-with-temp-text "** H\n Paragraph"
  3632. (let ((org-odd-levels-only nil)
  3633. (org-adapt-indentation t))
  3634. (org-promote))
  3635. (forward-line)
  3636. (org-get-indentation))))
  3637. (should-not
  3638. (= 2
  3639. (org-test-with-temp-text "** H\n Paragraph"
  3640. (let ((org-odd-levels-only nil)
  3641. (org-adapt-indentation nil))
  3642. (org-promote))
  3643. (forward-line)
  3644. (org-get-indentation))))
  3645. (should
  3646. (= 2
  3647. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  3648. (let ((org-odd-levels-only nil)
  3649. (org-adapt-indentation t))
  3650. (org-promote))
  3651. (forward-line)
  3652. (org-get-indentation))))
  3653. (when (featurep 'org-inlinetask)
  3654. (should
  3655. (zerop
  3656. (let ((org-inlinetask-min-level 5)
  3657. (org-adapt-indentation t))
  3658. (org-test-with-temp-text "** H\n***** I\n***** END"
  3659. (org-promote)
  3660. (forward-line)
  3661. (org-get-indentation))))))
  3662. (when (featurep 'org-inlinetask)
  3663. (should
  3664. (= 2
  3665. (let ((org-inlinetask-min-level 5)
  3666. (org-adapt-indentation t))
  3667. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  3668. (org-promote)
  3669. (forward-line 2)
  3670. (org-get-indentation))))))
  3671. ;; Give up shifting if it would break document's structure
  3672. ;; otherwise.
  3673. (should
  3674. (= 3
  3675. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  3676. (let ((org-odd-levels-only nil)
  3677. (org-adapt-indentation t))
  3678. (org-promote))
  3679. (forward-line)
  3680. (org-get-indentation))))
  3681. (should
  3682. (= 3
  3683. (org-test-with-temp-text "** H\n Paragraph\n * list."
  3684. (let ((org-odd-levels-only nil)
  3685. (org-adapt-indentation t))
  3686. (org-promote))
  3687. (forward-line)
  3688. (org-get-indentation))))
  3689. ;; Ignore contents of source blocks or example blocks when
  3690. ;; indentation should be preserved (through
  3691. ;; `org-src-preserve-indentation' or "-i" flag).
  3692. (should-not
  3693. (zerop
  3694. (org-test-with-temp-text
  3695. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3696. (let ((org-adapt-indentation t)
  3697. (org-src-preserve-indentation nil)
  3698. (org-odd-levels-only nil))
  3699. (org-promote))
  3700. (forward-line)
  3701. (org-get-indentation))))
  3702. (should
  3703. (zerop
  3704. (org-test-with-temp-text
  3705. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  3706. (let ((org-adapt-indentation t)
  3707. (org-src-preserve-indentation t)
  3708. (org-odd-levels-only nil))
  3709. (org-promote))
  3710. (forward-line)
  3711. (org-get-indentation))))
  3712. (should
  3713. (zerop
  3714. (org-test-with-temp-text
  3715. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3716. (let ((org-adapt-indentation t)
  3717. (org-src-preserve-indentation t)
  3718. (org-odd-levels-only nil))
  3719. (org-promote))
  3720. (forward-line)
  3721. (org-get-indentation))))
  3722. (should
  3723. (zerop
  3724. (org-test-with-temp-text
  3725. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  3726. (let ((org-adapt-indentation t)
  3727. (org-src-preserve-indentation nil)
  3728. (org-odd-levels-only nil))
  3729. (org-promote))
  3730. (forward-line)
  3731. (org-get-indentation)))))
  3732. (ert-deftest test-org/org-get-valid-level ()
  3733. "Test function `org-get-valid-level' specifications."
  3734. (let ((org-odd-levels-only nil))
  3735. (should (equal 1 (org-get-valid-level 0 0)))
  3736. (should (equal 1 (org-get-valid-level 0 1)))
  3737. (should (equal 2 (org-get-valid-level 0 2)))
  3738. (should (equal 3 (org-get-valid-level 0 3)))
  3739. (should (equal 1 (org-get-valid-level 1 0)))
  3740. (should (equal 2 (org-get-valid-level 1 1)))
  3741. (should (equal 23 (org-get-valid-level 1 22)))
  3742. (should (equal 1 (org-get-valid-level 1 -1)))
  3743. (should (equal 1 (org-get-valid-level 2 -1))))
  3744. (let ((org-odd-levels-only t))
  3745. (should (equal 1 (org-get-valid-level 0 0)))
  3746. (should (equal 1 (org-get-valid-level 0 1)))
  3747. (should (equal 3 (org-get-valid-level 0 2)))
  3748. (should (equal 5 (org-get-valid-level 0 3)))
  3749. (should (equal 1 (org-get-valid-level 1 0)))
  3750. (should (equal 3 (org-get-valid-level 1 1)))
  3751. (should (equal 3 (org-get-valid-level 2 1)))
  3752. (should (equal 5 (org-get-valid-level 3 1)))
  3753. (should (equal 5 (org-get-valid-level 4 1)))
  3754. (should (equal 43 (org-get-valid-level 1 21)))
  3755. (should (equal 1 (org-get-valid-level 1 -1)))
  3756. (should (equal 1 (org-get-valid-level 2 -1)))
  3757. (should (equal 1 (org-get-valid-level 3 -1)))
  3758. (should (equal 3 (org-get-valid-level 4 -1)))
  3759. (should (equal 3 (org-get-valid-level 5 -1)))))
  3760. ;;; Planning
  3761. (ert-deftest test-org/at-planning-p ()
  3762. "Test `org-at-planning-p' specifications."
  3763. ;; Regular test.
  3764. (should
  3765. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  3766. (org-at-planning-p)))
  3767. (should-not
  3768. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  3769. (org-at-planning-p)))
  3770. ;; Correctly find planning attached to inlinetasks.
  3771. (when (featurep 'org-inlinetask)
  3772. (should
  3773. (org-test-with-temp-text
  3774. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  3775. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3776. (should-not
  3777. (org-test-with-temp-text
  3778. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3779. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3780. (should-not
  3781. (org-test-with-temp-text
  3782. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3783. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3784. (should-not
  3785. (org-test-with-temp-text
  3786. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  3787. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  3788. (ert-deftest test-org/add-planning-info ()
  3789. "Test `org-add-planning-info'."
  3790. ;; Create deadline when `org-adapt-indentation' is non-nil.
  3791. (should
  3792. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3793. (org-test-with-temp-text "* H\nParagraph<point>"
  3794. (let ((org-adapt-indentation t))
  3795. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3796. (replace-regexp-in-string
  3797. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3798. nil nil 1))))
  3799. ;; Create deadline when `org-adapt-indentation' is nil.
  3800. (should
  3801. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3802. (org-test-with-temp-text "* H\nParagraph<point>"
  3803. (let ((org-adapt-indentation nil))
  3804. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3805. (replace-regexp-in-string
  3806. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3807. nil nil 1))))
  3808. ;; Update deadline when `org-adapt-indentation' is non-nil.
  3809. (should
  3810. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3811. (org-test-with-temp-text "\
  3812. * H
  3813. DEADLINE: <2015-06-24 Wed>
  3814. Paragraph<point>"
  3815. (let ((org-adapt-indentation t))
  3816. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3817. (replace-regexp-in-string
  3818. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3819. nil nil 1))))
  3820. ;; Update deadline when `org-adapt-indentation' is nil.
  3821. (should
  3822. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3823. (org-test-with-temp-text "\
  3824. * H
  3825. DEADLINE: <2015-06-24 Wed>
  3826. Paragraph<point>"
  3827. (let ((org-adapt-indentation nil))
  3828. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3829. (replace-regexp-in-string
  3830. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3831. nil nil 1))))
  3832. ;; Schedule when `org-adapt-indentation' is non-nil.
  3833. (should
  3834. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  3835. (org-test-with-temp-text "* H\nParagraph<point>"
  3836. (let ((org-adapt-indentation t))
  3837. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3838. (replace-regexp-in-string
  3839. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3840. nil nil 1))))
  3841. ;; Schedule when `org-adapt-indentation' is nil.
  3842. (should
  3843. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  3844. (org-test-with-temp-text "* H\nParagraph<point>"
  3845. (let ((org-adapt-indentation nil))
  3846. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3847. (replace-regexp-in-string
  3848. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3849. nil nil 1))))
  3850. ;; Add deadline when scheduled.
  3851. (should
  3852. (equal "\
  3853. * H
  3854. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  3855. Paragraph"
  3856. (org-test-with-temp-text "\
  3857. * H
  3858. SCHEDULED: <2015-06-24 Wed>
  3859. Paragraph<point>"
  3860. (let ((org-adapt-indentation t))
  3861. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3862. (replace-regexp-in-string
  3863. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3864. nil nil 1))))
  3865. ;; Remove middle entry.
  3866. (should
  3867. (equal "\
  3868. * H
  3869. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  3870. Paragraph"
  3871. (org-test-with-temp-text "\
  3872. * H
  3873. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3874. Paragraph<point>"
  3875. (let ((org-adapt-indentation t))
  3876. (org-add-planning-info nil nil 'deadline))
  3877. (replace-regexp-in-string
  3878. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3879. nil nil 1))))
  3880. ;; Remove last entry and then middle entry (order should not
  3881. ;; matter).
  3882. (should
  3883. (equal "\
  3884. * H
  3885. CLOSED: [2015-06-24]
  3886. Paragraph"
  3887. (org-test-with-temp-text "\
  3888. * H
  3889. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3890. Paragraph<point>"
  3891. (let ((org-adapt-indentation t))
  3892. (org-add-planning-info nil nil 'scheduled 'deadline))
  3893. (replace-regexp-in-string
  3894. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3895. nil nil 1))))
  3896. ;; Remove closed when `org-adapt-indentation' is non-nil.
  3897. (should
  3898. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3899. (org-test-with-temp-text "\
  3900. * H
  3901. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3902. Paragraph<point>"
  3903. (let ((org-adapt-indentation t))
  3904. (org-add-planning-info nil nil 'closed))
  3905. (replace-regexp-in-string
  3906. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3907. nil nil 1))))
  3908. (should
  3909. (equal "* H\n Paragraph"
  3910. (org-test-with-temp-text "\
  3911. * H
  3912. CLOSED: [2015-06-25 Thu]
  3913. Paragraph<point>"
  3914. (let ((org-adapt-indentation t))
  3915. (org-add-planning-info nil nil 'closed))
  3916. (replace-regexp-in-string
  3917. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3918. nil nil 1))))
  3919. ;; Remove closed when `org-adapt-indentation' is nil.
  3920. (should
  3921. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3922. (org-test-with-temp-text "\
  3923. * H
  3924. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3925. Paragraph<point>"
  3926. (let ((org-adapt-indentation nil))
  3927. (org-add-planning-info nil nil 'closed))
  3928. (replace-regexp-in-string
  3929. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3930. nil nil 1))))
  3931. (should
  3932. (equal "* H\nParagraph"
  3933. (org-test-with-temp-text "\
  3934. * H
  3935. CLOSED: [2015-06-25 Thu]
  3936. Paragraph<point>"
  3937. (let ((org-adapt-indentation nil))
  3938. (org-add-planning-info nil nil 'closed))
  3939. (replace-regexp-in-string
  3940. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3941. nil nil 1))))
  3942. ;; Remove closed entry and delete empty line.
  3943. (should
  3944. (equal "\
  3945. * H
  3946. Paragraph"
  3947. (org-test-with-temp-text "\
  3948. * H
  3949. CLOSED: [2015-06-24 Wed]
  3950. Paragraph<point>"
  3951. (let ((org-adapt-indentation t))
  3952. (org-add-planning-info nil nil 'closed))
  3953. (replace-regexp-in-string
  3954. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3955. nil nil 1))))
  3956. ;; Remove one entry and update another.
  3957. (should
  3958. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3959. (org-test-with-temp-text "\
  3960. * H
  3961. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  3962. Paragraph<point>"
  3963. (let ((org-adapt-indentation t))
  3964. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  3965. (replace-regexp-in-string
  3966. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3967. nil nil 1)))))
  3968. (ert-deftest test-org/deadline ()
  3969. "Test `org-deadline' specifications."
  3970. ;; Insert a new value or replace existing one.
  3971. (should
  3972. (equal "* H\nDEADLINE: <2012-03-29>\n"
  3973. (org-test-with-temp-text "* H"
  3974. (let ((org-adapt-indentation nil)
  3975. (org-last-inserted-timestamp nil))
  3976. (org-deadline nil "<2012-03-29 Tue>"))
  3977. (replace-regexp-in-string
  3978. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3979. nil nil 1))))
  3980. (should
  3981. (equal "* H\nDEADLINE: <2014-03-04>"
  3982. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  3983. (let ((org-adapt-indentation nil)
  3984. (org-last-inserted-timestamp nil))
  3985. (org-deadline nil "<2014-03-04 Thu>"))
  3986. (replace-regexp-in-string
  3987. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3988. nil nil 1))))
  3989. ;; Accept delta time, e.g., "+2d".
  3990. (should
  3991. (equal "* H\nDEADLINE: <2015-03-04>\n"
  3992. (cl-letf (((symbol-function 'current-time)
  3993. (lambda (&rest args)
  3994. (apply #'encode-time
  3995. (org-parse-time-string "2014-03-04")))))
  3996. (org-test-with-temp-text "* H"
  3997. (let ((org-adapt-indentation nil)
  3998. (org-last-inserted-timestamp nil))
  3999. (org-deadline nil "+1y"))
  4000. (replace-regexp-in-string
  4001. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4002. ;; Preserve repeater.
  4003. (should
  4004. (equal "* H\nDEADLINE: <2012-03-29 +2y>\n"
  4005. (org-test-with-temp-text "* H"
  4006. (let ((org-adapt-indentation nil)
  4007. (org-last-inserted-timestamp nil))
  4008. (org-deadline nil "<2012-03-29 Tue +2y>"))
  4009. (replace-regexp-in-string
  4010. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4011. ;; Remove CLOSED keyword, if any.
  4012. (should
  4013. (equal "* H\nDEADLINE: <2012-03-29>"
  4014. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4015. (let ((org-adapt-indentation nil)
  4016. (org-last-inserted-timestamp nil))
  4017. (org-deadline nil "<2012-03-29 Tue>"))
  4018. (replace-regexp-in-string
  4019. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4020. ;; With C-u argument, remove DEADLINE keyword.
  4021. (should
  4022. (equal "* H\n"
  4023. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4024. (let ((org-adapt-indentation nil)
  4025. (org-last-inserted-timestamp nil))
  4026. (org-deadline '(4)))
  4027. (buffer-string))))
  4028. (should
  4029. (equal "* H"
  4030. (org-test-with-temp-text "* H"
  4031. (let ((org-adapt-indentation nil)
  4032. (org-last-inserted-timestamp nil))
  4033. (org-deadline '(4)))
  4034. (buffer-string))))
  4035. ;; With C-u C-u argument, prompt for a delay cookie.
  4036. (should
  4037. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  4038. (cl-letf (((symbol-function 'org-read-date)
  4039. (lambda (&rest args)
  4040. (apply #'encode-time
  4041. (org-parse-time-string "2014-03-04")))))
  4042. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4043. (let ((org-adapt-indentation nil)
  4044. (org-last-inserted-timestamp nil))
  4045. (org-deadline '(16)))
  4046. (buffer-string)))))
  4047. (should-error
  4048. (cl-letf (((symbol-function 'org-read-date)
  4049. (lambda (&rest args)
  4050. (apply #'encode-time
  4051. (org-parse-time-string "2014-03-04")))))
  4052. (org-test-with-temp-text "* H"
  4053. (let ((org-adapt-indentation nil)
  4054. (org-last-inserted-timestamp nil))
  4055. (org-deadline '(16)))
  4056. (buffer-string))))
  4057. ;; When a region is active and
  4058. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4059. ;; same value in all headlines in region.
  4060. (should
  4061. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4062. (org-test-with-temp-text "* H1\n* H2"
  4063. (let ((org-adapt-indentation nil)
  4064. (org-last-inserted-timestamp nil)
  4065. (org-loop-over-headlines-in-active-region t))
  4066. (transient-mark-mode 1)
  4067. (push-mark (point) t t)
  4068. (goto-char (point-max))
  4069. (org-deadline nil "2012-03-29"))
  4070. (replace-regexp-in-string
  4071. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4072. (should-not
  4073. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4074. (org-test-with-temp-text "* H1\n* H2"
  4075. (let ((org-adapt-indentation nil)
  4076. (org-last-inserted-timestamp nil)
  4077. (org-loop-over-headlines-in-active-region nil))
  4078. (transient-mark-mode 1)
  4079. (push-mark (point) t t)
  4080. (goto-char (point-max))
  4081. (org-deadline nil "2012-03-29"))
  4082. (replace-regexp-in-string
  4083. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4084. (ert-deftest test-org/schedule ()
  4085. "Test `org-schedule' specifications."
  4086. ;; Insert a new value or replace existing one.
  4087. (should
  4088. (equal "* H\nSCHEDULED: <2012-03-29>\n"
  4089. (org-test-with-temp-text "* H"
  4090. (let ((org-adapt-indentation nil)
  4091. (org-last-inserted-timestamp nil))
  4092. (org-schedule nil "<2012-03-29 Tue>"))
  4093. (replace-regexp-in-string
  4094. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4095. nil nil 1))))
  4096. (should
  4097. (equal "* H\nSCHEDULED: <2014-03-04>"
  4098. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4099. (let ((org-adapt-indentation nil)
  4100. (org-last-inserted-timestamp nil))
  4101. (org-schedule nil "<2014-03-04 Thu>"))
  4102. (replace-regexp-in-string
  4103. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4104. nil nil 1))))
  4105. ;; Accept delta time, e.g., "+2d".
  4106. (should
  4107. (equal "* H\nSCHEDULED: <2015-03-04>\n"
  4108. (cl-letf (((symbol-function 'current-time)
  4109. (lambda (&rest args)
  4110. (apply #'encode-time
  4111. (org-parse-time-string "2014-03-04")))))
  4112. (org-test-with-temp-text "* H"
  4113. (let ((org-adapt-indentation nil)
  4114. (org-last-inserted-timestamp nil))
  4115. (org-schedule nil "+1y"))
  4116. (replace-regexp-in-string
  4117. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4118. ;; Preserve repeater.
  4119. (should
  4120. (equal "* H\nSCHEDULED: <2012-03-29 +2y>\n"
  4121. (org-test-with-temp-text "* H"
  4122. (let ((org-adapt-indentation nil)
  4123. (org-last-inserted-timestamp nil))
  4124. (org-schedule nil "<2012-03-29 Tue +2y>"))
  4125. (replace-regexp-in-string
  4126. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4127. ;; Remove CLOSED keyword, if any.
  4128. (should
  4129. (equal "* H\nSCHEDULED: <2012-03-29>"
  4130. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4131. (let ((org-adapt-indentation nil)
  4132. (org-last-inserted-timestamp nil))
  4133. (org-schedule nil "<2012-03-29 Tue>"))
  4134. (replace-regexp-in-string
  4135. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4136. ;; With C-u argument, remove SCHEDULED keyword.
  4137. (should
  4138. (equal "* H\n"
  4139. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4140. (let ((org-adapt-indentation nil)
  4141. (org-last-inserted-timestamp nil))
  4142. (org-schedule '(4)))
  4143. (buffer-string))))
  4144. (should
  4145. (equal "* H"
  4146. (org-test-with-temp-text "* H"
  4147. (let ((org-adapt-indentation nil)
  4148. (org-last-inserted-timestamp nil))
  4149. (org-schedule '(4)))
  4150. (buffer-string))))
  4151. ;; With C-u C-u argument, prompt for a delay cookie.
  4152. (should
  4153. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  4154. (cl-letf (((symbol-function 'org-read-date)
  4155. (lambda (&rest args)
  4156. (apply #'encode-time
  4157. (org-parse-time-string "2014-03-04")))))
  4158. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4159. (let ((org-adapt-indentation nil)
  4160. (org-last-inserted-timestamp nil))
  4161. (org-schedule '(16)))
  4162. (buffer-string)))))
  4163. (should-error
  4164. (cl-letf (((symbol-function 'org-read-date)
  4165. (lambda (&rest args)
  4166. (apply #'encode-time
  4167. (org-parse-time-string "2014-03-04")))))
  4168. (org-test-with-temp-text "* H"
  4169. (let ((org-adapt-indentation nil)
  4170. (org-last-inserted-timestamp nil))
  4171. (org-schedule '(16)))
  4172. (buffer-string))))
  4173. ;; When a region is active and
  4174. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4175. ;; same value in all headlines in region.
  4176. (should
  4177. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4178. (org-test-with-temp-text "* H1\n* H2"
  4179. (let ((org-adapt-indentation nil)
  4180. (org-last-inserted-timestamp nil)
  4181. (org-loop-over-headlines-in-active-region t))
  4182. (transient-mark-mode 1)
  4183. (push-mark (point) t t)
  4184. (goto-char (point-max))
  4185. (org-schedule nil "2012-03-29"))
  4186. (replace-regexp-in-string
  4187. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4188. (should-not
  4189. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4190. (org-test-with-temp-text "* H1\n* H2"
  4191. (let ((org-adapt-indentation nil)
  4192. (org-last-inserted-timestamp nil)
  4193. (org-loop-over-headlines-in-active-region nil))
  4194. (transient-mark-mode 1)
  4195. (push-mark (point) t t)
  4196. (goto-char (point-max))
  4197. (org-schedule nil "2012-03-29"))
  4198. (replace-regexp-in-string
  4199. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4200. (should
  4201. ;; check if a repeater survives re-scheduling.
  4202. (string-match-p
  4203. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  4204. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  4205. (let ((org-adapt-indentation nil)
  4206. (org-last-inserted-timestamp nil))
  4207. (org-schedule nil "2017-02-01"))
  4208. (buffer-string)))))
  4209. ;;; Property API
  4210. (ert-deftest test-org/buffer-property-keys ()
  4211. "Test `org-buffer-property-keys' specifications."
  4212. ;; Retrieve properties accross siblings.
  4213. (should
  4214. (equal '("A" "B")
  4215. (org-test-with-temp-text "
  4216. * H1
  4217. :PROPERTIES:
  4218. :A: 1
  4219. :END:
  4220. * H2
  4221. :PROPERTIES:
  4222. :B: 1
  4223. :END:"
  4224. (org-buffer-property-keys))))
  4225. ;; Retrieve properties accross children.
  4226. (should
  4227. (equal '("A" "B")
  4228. (org-test-with-temp-text "
  4229. * H1
  4230. :PROPERTIES:
  4231. :A: 1
  4232. :END:
  4233. ** H2
  4234. :PROPERTIES:
  4235. :B: 1
  4236. :END:"
  4237. (org-buffer-property-keys))))
  4238. ;; Retrieve muliple properties in the same drawer.
  4239. (should
  4240. (equal '("A" "B")
  4241. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4242. (org-buffer-property-keys))))
  4243. ;; Ignore extension symbol in property name.
  4244. (should
  4245. (equal '("A")
  4246. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4247. (org-buffer-property-keys))))
  4248. ;; With non-nil COLUMNS, extract property names from columns.
  4249. (should
  4250. (equal '("A" "B")
  4251. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  4252. (org-buffer-property-keys nil nil t))))
  4253. (should
  4254. (equal '("A" "B" "COLUMNS")
  4255. (org-test-with-temp-text
  4256. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  4257. (org-buffer-property-keys nil nil t))))
  4258. ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
  4259. (should
  4260. (equal '("A")
  4261. (org-test-with-temp-text
  4262. "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
  4263. (org-buffer-property-keys nil nil nil t)))))
  4264. (ert-deftest test-org/property-values ()
  4265. "Test `org-property-values' specifications."
  4266. ;; Regular test.
  4267. (should
  4268. (equal '("2" "1")
  4269. (org-test-with-temp-text
  4270. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  4271. (org-property-values "A"))))
  4272. ;; Ignore empty values.
  4273. (should-not
  4274. (org-test-with-temp-text
  4275. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  4276. (org-property-values "A")))
  4277. ;; Take into consideration extended values.
  4278. (should
  4279. (equal '("1 2")
  4280. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4281. (org-property-values "A")))))
  4282. (ert-deftest test-org/find-property ()
  4283. "Test `org-find-property' specifications."
  4284. ;; Regular test.
  4285. (should
  4286. (= 1
  4287. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  4288. (org-find-property "prop"))))
  4289. ;; Ignore false positives.
  4290. (should
  4291. (= 27
  4292. (org-test-with-temp-text
  4293. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  4294. (org-find-property "A"))))
  4295. ;; Return first entry found in buffer.
  4296. (should
  4297. (= 1
  4298. (org-test-with-temp-text
  4299. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4300. (org-find-property "A"))))
  4301. ;; Only search visible part of the buffer.
  4302. (should
  4303. (= 31
  4304. (org-test-with-temp-text
  4305. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4306. (org-narrow-to-subtree)
  4307. (org-find-property "A"))))
  4308. ;; With optional argument, only find entries with a specific value.
  4309. (should-not
  4310. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4311. (org-find-property "A" "2")))
  4312. (should
  4313. (= 31
  4314. (org-test-with-temp-text
  4315. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  4316. (org-find-property "A" "2"))))
  4317. ;; Use "nil" for explicit nil values.
  4318. (should
  4319. (= 31
  4320. (org-test-with-temp-text
  4321. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  4322. (org-find-property "A" "nil")))))
  4323. (ert-deftest test-org/entry-delete ()
  4324. "Test `org-entry-delete' specifications."
  4325. ;; Regular test.
  4326. (should
  4327. (string-match
  4328. " *:PROPERTIES:\n *:B: +2\n *:END:"
  4329. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4330. (org-entry-delete (point) "A")
  4331. (buffer-string))))
  4332. ;; Also remove accumulated properties.
  4333. (should-not
  4334. (string-match
  4335. ":A"
  4336. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  4337. (org-entry-delete (point) "A")
  4338. (buffer-string))))
  4339. ;; When last property is removed, remove the property drawer.
  4340. (should-not
  4341. (string-match
  4342. ":PROPERTIES:"
  4343. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  4344. (org-entry-delete (point) "A")
  4345. (buffer-string))))
  4346. ;; Return a non-nil value when some property was removed.
  4347. (should
  4348. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4349. (org-entry-delete (point) "A")))
  4350. (should-not
  4351. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4352. (org-entry-delete (point) "C")))
  4353. ;; Special properties cannot be located in a drawer. Allow to
  4354. ;; remove them anyway, in case of user error.
  4355. (should
  4356. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  4357. (org-entry-delete (point) "SCHEDULED"))))
  4358. (ert-deftest test-org/entry-get ()
  4359. "Test `org-entry-get' specifications."
  4360. ;; Regular test.
  4361. (should
  4362. (equal "1"
  4363. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4364. (org-entry-get (point) "A"))))
  4365. ;; Ignore case.
  4366. (should
  4367. (equal "1"
  4368. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4369. (org-entry-get (point) "a"))))
  4370. ;; Handle extended values, both before and after base value.
  4371. (should
  4372. (equal "1 2 3"
  4373. (org-test-with-temp-text
  4374. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  4375. (org-entry-get (point) "A"))))
  4376. ;; Empty values are returned as the empty string.
  4377. (should
  4378. (equal ""
  4379. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  4380. (org-entry-get (point) "A"))))
  4381. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  4382. ;; otherwise, return nil.
  4383. (should-not
  4384. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  4385. (org-entry-get (point) "A")))
  4386. (should
  4387. (equal "nil"
  4388. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  4389. (org-entry-get (point) "A" nil t))))
  4390. ;; Return nil when no property is found, independently on the
  4391. ;; LITERAL-NIL argument.
  4392. (should-not
  4393. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4394. (org-entry-get (point) "B")))
  4395. (should-not
  4396. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4397. (org-entry-get (point) "B" nil t)))
  4398. ;; Handle inheritance, when allowed. Include extended values and
  4399. ;; possibly global values.
  4400. (should
  4401. (equal
  4402. "1"
  4403. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4404. (org-entry-get (point) "A" t))))
  4405. (should
  4406. (equal
  4407. "1"
  4408. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4409. (let ((org-use-property-inheritance t))
  4410. (org-entry-get (point) "A" 'selective)))))
  4411. (should-not
  4412. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4413. (let ((org-use-property-inheritance nil))
  4414. (org-entry-get (point) "A" 'selective))))
  4415. (should
  4416. (equal
  4417. "1 2"
  4418. (org-test-with-temp-text
  4419. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  4420. (org-entry-get (point-max) "A" t))))
  4421. (should
  4422. (equal "1"
  4423. (org-test-with-temp-text
  4424. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  4425. (org-mode-restart)
  4426. (org-entry-get (point-max) "A" t))))
  4427. (should
  4428. (equal "0 1"
  4429. (org-test-with-temp-text
  4430. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  4431. (org-mode-restart)
  4432. (org-entry-get (point-max) "A" t)))))
  4433. (ert-deftest test-org/entry-properties ()
  4434. "Test `org-entry-properties' specifications."
  4435. ;; Get "ITEM" property.
  4436. (should
  4437. (equal "H"
  4438. (org-test-with-temp-text "* TODO H"
  4439. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  4440. (should
  4441. (equal "H"
  4442. (org-test-with-temp-text "* TODO H"
  4443. (cdr (assoc "ITEM" (org-entry-properties))))))
  4444. ;; Get "TODO" property. TODO keywords are case sensitive.
  4445. (should
  4446. (equal "TODO"
  4447. (org-test-with-temp-text "* TODO H"
  4448. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  4449. (should
  4450. (equal "TODO"
  4451. (org-test-with-temp-text "* TODO H"
  4452. (cdr (assoc "TODO" (org-entry-properties))))))
  4453. (should-not
  4454. (org-test-with-temp-text "* H"
  4455. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4456. (should-not
  4457. (org-test-with-temp-text "* todo H"
  4458. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4459. ;; Get "PRIORITY" property.
  4460. (should
  4461. (equal "A"
  4462. (org-test-with-temp-text "* [#A] H"
  4463. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4464. (should
  4465. (equal "A"
  4466. (org-test-with-temp-text "* [#A] H"
  4467. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  4468. (should
  4469. (equal (char-to-string org-default-priority)
  4470. (org-test-with-temp-text "* H"
  4471. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4472. ;; Get "FILE" property.
  4473. (should
  4474. (org-test-with-temp-text-in-file "* H\nParagraph"
  4475. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  4476. (buffer-file-name))))
  4477. (should
  4478. (org-test-with-temp-text-in-file "* H\nParagraph"
  4479. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  4480. (buffer-file-name))))
  4481. (should-not
  4482. (org-test-with-temp-text "* H\nParagraph"
  4483. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  4484. ;; Get "TAGS" property.
  4485. (should
  4486. (equal ":tag1:tag2:"
  4487. (org-test-with-temp-text "* H :tag1:tag2:"
  4488. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  4489. (should
  4490. (equal ":tag1:tag2:"
  4491. (org-test-with-temp-text "* H :tag1:tag2:"
  4492. (cdr (assoc "TAGS" (org-entry-properties))))))
  4493. (should-not
  4494. (org-test-with-temp-text "* H"
  4495. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  4496. ;; Get "ALLTAGS" property.
  4497. (should
  4498. (equal ":tag1:tag2:"
  4499. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4500. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  4501. (should
  4502. (equal ":tag1:tag2:"
  4503. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4504. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  4505. (should-not
  4506. (org-test-with-temp-text "* H"
  4507. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  4508. ;; Get "BLOCKED" property.
  4509. (should
  4510. (equal "t"
  4511. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  4512. (let ((org-enforce-todo-dependencies t)
  4513. (org-blocker-hook
  4514. '(org-block-todo-from-children-or-siblings-or-parent)))
  4515. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4516. (should
  4517. (equal ""
  4518. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  4519. (let ((org-enforce-todo-dependencies t)
  4520. (org-blocker-hook
  4521. '(org-block-todo-from-children-or-siblings-or-parent)))
  4522. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4523. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  4524. (should
  4525. (equal
  4526. "[2012-03-29 thu.]"
  4527. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4528. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  4529. (should
  4530. (equal
  4531. "[2012-03-29 thu.]"
  4532. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4533. (cdr (assoc "CLOSED" (org-entry-properties))))))
  4534. (should-not
  4535. (org-test-with-temp-text "* H"
  4536. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  4537. (should
  4538. (equal
  4539. "<2014-03-04 tue.>"
  4540. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4541. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  4542. (should
  4543. (equal
  4544. "<2014-03-04 tue.>"
  4545. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4546. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  4547. (should-not
  4548. (org-test-with-temp-text "* H"
  4549. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  4550. (should
  4551. (equal
  4552. "<2014-03-04 tue.>"
  4553. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4554. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  4555. (should
  4556. (equal
  4557. "<2014-03-04 tue.>"
  4558. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4559. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  4560. (should-not
  4561. (org-test-with-temp-text "* H"
  4562. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  4563. ;; Get "CATEGORY"
  4564. (should
  4565. (equal "cat"
  4566. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4567. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  4568. (should
  4569. (equal "cat"
  4570. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4571. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4572. (should
  4573. (equal "cat"
  4574. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  4575. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4576. (should
  4577. (equal "cat2"
  4578. (org-test-with-temp-text
  4579. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  4580. "\n"
  4581. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  4582. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4583. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  4584. (should
  4585. (equal "<2012-03-29 thu.>"
  4586. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  4587. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  4588. (should
  4589. (equal "[2012-03-29 thu.]"
  4590. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  4591. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  4592. (should
  4593. (equal "<2012-03-29 thu.>"
  4594. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  4595. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  4596. (should
  4597. (equal "[2014-03-04 tue.]"
  4598. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  4599. (cdr (assoc "TIMESTAMP_IA"
  4600. (org-entry-properties nil "TIMESTAMP_IA"))))))
  4601. (should-not
  4602. (equal "<2012-03-29 thu.>"
  4603. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  4604. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  4605. ;; Get standard properties.
  4606. (should
  4607. (equal "1"
  4608. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4609. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4610. ;; Handle extended properties.
  4611. (should
  4612. (equal "1 2 3"
  4613. (org-test-with-temp-text
  4614. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  4615. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4616. (should
  4617. (equal "1 2 3"
  4618. (org-test-with-temp-text
  4619. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  4620. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4621. ;; Ignore forbidden (special) properties.
  4622. (should-not
  4623. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  4624. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  4625. (ert-deftest test-org/entry-put ()
  4626. "Test `org-entry-put' specifications."
  4627. ;; Error when not a string or nil.
  4628. (should-error
  4629. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4630. (org-entry-put 1 "test" 2)))
  4631. ;; Error when property name is invalid.
  4632. (should-error
  4633. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4634. (org-entry-put 1 "no space" "value")))
  4635. (should-error
  4636. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4637. (org-entry-put 1 "" "value")))
  4638. ;; Set "TODO" property.
  4639. (should
  4640. (string-match (regexp-quote " TODO H")
  4641. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  4642. (org-entry-put (point) "TODO" "TODO")
  4643. (buffer-string))))
  4644. (should
  4645. (string-match (regexp-quote "* H")
  4646. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  4647. (org-entry-put (point) "TODO" nil)
  4648. (buffer-string))))
  4649. ;; Set "PRIORITY" property.
  4650. (should
  4651. (equal "* [#A] H"
  4652. (org-test-with-temp-text "* [#B] H"
  4653. (org-entry-put (point) "PRIORITY" "A")
  4654. (buffer-string))))
  4655. (should
  4656. (equal "* H"
  4657. (org-test-with-temp-text "* [#B] H"
  4658. (org-entry-put (point) "PRIORITY" nil)
  4659. (buffer-string))))
  4660. ;; Set "SCHEDULED" property.
  4661. (should
  4662. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  4663. (org-test-with-temp-text "* H"
  4664. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  4665. (buffer-string))))
  4666. (should
  4667. (string= "* H\n"
  4668. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4669. (org-entry-put (point) "SCHEDULED" nil)
  4670. (buffer-string))))
  4671. (should
  4672. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  4673. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4674. (org-entry-put (point) "SCHEDULED" "earlier")
  4675. (buffer-string))))
  4676. (should
  4677. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  4678. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4679. (org-entry-put (point) "SCHEDULED" "later")
  4680. (buffer-string))))
  4681. ;; Set "DEADLINE" property.
  4682. (should
  4683. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  4684. (org-test-with-temp-text "* H"
  4685. (org-entry-put (point) "DEADLINE" "2014-03-04")
  4686. (buffer-string))))
  4687. (should
  4688. (string= "* H\n"
  4689. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4690. (org-entry-put (point) "DEADLINE" nil)
  4691. (buffer-string))))
  4692. (should
  4693. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  4694. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4695. (org-entry-put (point) "DEADLINE" "earlier")
  4696. (buffer-string))))
  4697. (should
  4698. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  4699. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4700. (org-entry-put (point) "DEADLINE" "later")
  4701. (buffer-string))))
  4702. ;; Set "CATEGORY" property
  4703. (should
  4704. (string-match "^ *:CATEGORY: cat"
  4705. (org-test-with-temp-text "* H"
  4706. (org-entry-put (point) "CATEGORY" "cat")
  4707. (buffer-string))))
  4708. ;; Regular properties, with or without pre-existing drawer.
  4709. (should
  4710. (string-match "^ *:A: +2$"
  4711. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4712. (org-entry-put (point) "A" "2")
  4713. (buffer-string))))
  4714. (should
  4715. (string-match "^ *:A: +1$"
  4716. (org-test-with-temp-text "* H"
  4717. (org-entry-put (point) "A" "1")
  4718. (buffer-string))))
  4719. ;; Special case: two consecutive headlines.
  4720. (should
  4721. (string-match "\\* A\n *:PROPERTIES:"
  4722. (org-test-with-temp-text "* A\n** B"
  4723. (org-entry-put (point) "A" "1")
  4724. (buffer-string)))))
  4725. (ert-deftest test-org/refresh-properties ()
  4726. "Test `org-refresh-properties' specifications."
  4727. (should
  4728. (equal "1"
  4729. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4730. (org-refresh-properties "A" 'org-test)
  4731. (get-text-property (point) 'org-test))))
  4732. (should-not
  4733. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4734. (org-refresh-properties "B" 'org-test)
  4735. (get-text-property (point) 'org-test)))
  4736. ;; Handle properties only defined with extension syntax, i.e.,
  4737. ;; "PROPERTY+".
  4738. (should
  4739. (equal "1"
  4740. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  4741. (org-refresh-properties "A" 'org-test)
  4742. (get-text-property (point) 'org-test))))
  4743. ;; When property is inherited, add text property to the whole
  4744. ;; sub-tree.
  4745. (should
  4746. (equal "1"
  4747. (org-test-with-temp-text
  4748. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  4749. (let ((org-use-property-inheritance t))
  4750. (org-refresh-properties "A" 'org-test))
  4751. (get-text-property (point) 'org-test))))
  4752. ;; When property is inherited, use global value across the whole
  4753. ;; buffer. However local values have precedence.
  4754. (should-not
  4755. (equal "1"
  4756. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  4757. (org-mode-restart)
  4758. (let ((org-use-property-inheritance nil))
  4759. (org-refresh-properties "A" 'org-test))
  4760. (get-text-property (point) 'org-test))))
  4761. (should
  4762. (equal "1"
  4763. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  4764. (org-mode-restart)
  4765. (let ((org-use-property-inheritance t))
  4766. (org-refresh-properties "A" 'org-test))
  4767. (get-text-property (point) 'org-test))))
  4768. (should
  4769. (equal "2"
  4770. (org-test-with-temp-text
  4771. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  4772. (org-mode-restart)
  4773. (let ((org-use-property-inheritance t))
  4774. (org-refresh-properties "A" 'org-test))
  4775. (get-text-property (point) 'org-test)))))
  4776. ;;; Radio Targets
  4777. (ert-deftest test-org/update-radio-target-regexp ()
  4778. "Test `org-update-radio-target-regexp' specifications."
  4779. ;; Properly update cache with no previous radio target regexp.
  4780. (should
  4781. (eq 'link
  4782. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4783. (save-excursion (goto-char (point-max)) (org-element-context))
  4784. (insert "<<<")
  4785. (search-forward "o")
  4786. (insert ">>>")
  4787. (org-update-radio-target-regexp)
  4788. (goto-char (point-max))
  4789. (org-element-type (org-element-context)))))
  4790. ;; Properly update cache with previous radio target regexp.
  4791. (should
  4792. (eq 'link
  4793. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4794. (save-excursion (goto-char (point-max)) (org-element-context))
  4795. (insert "<<<")
  4796. (search-forward "o")
  4797. (insert ">>>")
  4798. (org-update-radio-target-regexp)
  4799. (search-backward "r")
  4800. (delete-char 5)
  4801. (insert "new")
  4802. (org-update-radio-target-regexp)
  4803. (goto-char (point-max))
  4804. (delete-region (line-beginning-position) (point))
  4805. (insert "new")
  4806. (org-element-type (org-element-context))))))
  4807. ;;; Sparse trees
  4808. (ert-deftest test-org/match-sparse-tree ()
  4809. "Test `org-match-sparse-tree' specifications."
  4810. ;; Match tags.
  4811. (should-not
  4812. (org-test-with-temp-text "* H\n** H1 :tag:"
  4813. (org-match-sparse-tree nil "tag")
  4814. (search-forward "H1")
  4815. (org-invisible-p2)))
  4816. (should
  4817. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  4818. (org-match-sparse-tree nil "tag")
  4819. (search-forward "H2")
  4820. (org-invisible-p2)))
  4821. ;; "-" operator for tags.
  4822. (should-not
  4823. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4824. (org-match-sparse-tree nil "tag1-tag2")
  4825. (search-forward "H1")
  4826. (org-invisible-p2)))
  4827. (should
  4828. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4829. (org-match-sparse-tree nil "tag1-tag2")
  4830. (search-forward "H2")
  4831. (org-invisible-p2)))
  4832. ;; "&" operator for tags.
  4833. (should
  4834. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4835. (org-match-sparse-tree nil "tag1&tag2")
  4836. (search-forward "H1")
  4837. (org-invisible-p2)))
  4838. (should-not
  4839. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4840. (org-match-sparse-tree nil "tag1&tag2")
  4841. (search-forward "H2")
  4842. (org-invisible-p2)))
  4843. ;; "|" operator for tags.
  4844. (should-not
  4845. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4846. (org-match-sparse-tree nil "tag1|tag2")
  4847. (search-forward "H1")
  4848. (org-invisible-p2)))
  4849. (should-not
  4850. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4851. (org-match-sparse-tree nil "tag1|tag2")
  4852. (search-forward "H2")
  4853. (org-invisible-p2)))
  4854. ;; Regexp match on tags.
  4855. (should-not
  4856. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4857. (org-match-sparse-tree nil "{^tag.*}")
  4858. (search-forward "H1")
  4859. (org-invisible-p2)))
  4860. (should
  4861. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4862. (org-match-sparse-tree nil "{^tag.*}")
  4863. (search-forward "H2")
  4864. (org-invisible-p2)))
  4865. ;; Match group tags.
  4866. (should-not
  4867. (org-test-with-temp-text
  4868. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4869. (org-match-sparse-tree nil "work")
  4870. (search-forward "H1")
  4871. (org-invisible-p2)))
  4872. (should-not
  4873. (org-test-with-temp-text
  4874. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4875. (org-match-sparse-tree nil "work")
  4876. (search-forward "H2")
  4877. (org-invisible-p2)))
  4878. ;; Match group tags with hard brackets.
  4879. (should-not
  4880. (org-test-with-temp-text
  4881. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4882. (org-match-sparse-tree nil "work")
  4883. (search-forward "H1")
  4884. (org-invisible-p2)))
  4885. (should-not
  4886. (org-test-with-temp-text
  4887. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4888. (org-match-sparse-tree nil "work")
  4889. (search-forward "H2")
  4890. (org-invisible-p2)))
  4891. ;; Match tags in hierarchies
  4892. (should-not
  4893. (org-test-with-temp-text
  4894. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  4895. #+TAGS: [ Lev_2 : Lev_3 ]\n
  4896. #+TAGS: { Lev_3 : Lev_4 }\n
  4897. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  4898. (org-match-sparse-tree nil "Lev_1")
  4899. (search-forward "H4")
  4900. (org-invisible-p2)))
  4901. ;; Match regular expressions in tags
  4902. (should-not
  4903. (org-test-with-temp-text
  4904. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  4905. (org-match-sparse-tree nil "Lev")
  4906. (search-forward "H1")
  4907. (org-invisible-p2)))
  4908. (should
  4909. (org-test-with-temp-text
  4910. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  4911. (org-match-sparse-tree nil "Lev")
  4912. (search-forward "H1")
  4913. (org-invisible-p2)))
  4914. ;; Match properties.
  4915. (should
  4916. (org-test-with-temp-text
  4917. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  4918. (org-match-sparse-tree nil "A=\"1\"")
  4919. (search-forward "H2")
  4920. (org-invisible-p2)))
  4921. (should-not
  4922. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4923. (org-match-sparse-tree nil "A=\"1\"")
  4924. (search-forward "H2")
  4925. (org-invisible-p2)))
  4926. ;; Case is not significant when matching properties.
  4927. (should-not
  4928. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4929. (org-match-sparse-tree nil "a=\"1\"")
  4930. (search-forward "H2")
  4931. (org-invisible-p2)))
  4932. (should-not
  4933. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  4934. (org-match-sparse-tree nil "A=\"1\"")
  4935. (search-forward "H2")
  4936. (org-invisible-p2)))
  4937. ;; Match special LEVEL property.
  4938. (should-not
  4939. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4940. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4941. (search-forward "H1")
  4942. (org-invisible-p2)))
  4943. (should
  4944. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4945. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4946. (search-forward "H2")
  4947. (org-invisible-p2)))
  4948. ;; Comparison operators when matching properties.
  4949. (should
  4950. (org-test-with-temp-text
  4951. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4952. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4953. (search-forward "H1")
  4954. (org-invisible-p2)))
  4955. (should-not
  4956. (org-test-with-temp-text
  4957. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4958. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4959. (search-forward "H2")
  4960. (org-invisible-p2)))
  4961. ;; Regexp match on properties values.
  4962. (should-not
  4963. (org-test-with-temp-text
  4964. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4965. (org-match-sparse-tree nil "A={f.*}")
  4966. (search-forward "H1")
  4967. (org-invisible-p2)))
  4968. (should
  4969. (org-test-with-temp-text
  4970. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4971. (org-match-sparse-tree nil "A={f.*}")
  4972. (search-forward "H2")
  4973. (org-invisible-p2)))
  4974. ;; With an optional argument, limit match to TODO entries.
  4975. (should-not
  4976. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4977. (org-match-sparse-tree t "tag")
  4978. (search-forward "H1")
  4979. (org-invisible-p2)))
  4980. (should
  4981. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4982. (org-match-sparse-tree t "tag")
  4983. (search-forward "H2")
  4984. (org-invisible-p2))))
  4985. (ert-deftest test-org/occur ()
  4986. "Test `org-occur' specifications."
  4987. ;; Count number of matches.
  4988. (should
  4989. (= 1
  4990. (org-test-with-temp-text "* H\nA\n* H2"
  4991. (org-occur "A"))))
  4992. (should
  4993. (= 2
  4994. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4995. (org-occur "A"))))
  4996. ;; Test CALLBACK optional argument.
  4997. (should
  4998. (= 0
  4999. (org-test-with-temp-text "* H\nA\n* H2"
  5000. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5001. (should
  5002. (= 1
  5003. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5004. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5005. ;; Case-fold searches according to `org-occur-case-fold-search'.
  5006. (should
  5007. (= 2
  5008. (org-test-with-temp-text "Aa"
  5009. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  5010. (should
  5011. (= 2
  5012. (org-test-with-temp-text "Aa"
  5013. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  5014. (should
  5015. (= 1
  5016. (org-test-with-temp-text "Aa"
  5017. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  5018. (should
  5019. (= 1
  5020. (org-test-with-temp-text "Aa"
  5021. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  5022. (should
  5023. (= 1
  5024. (org-test-with-temp-text "Aa"
  5025. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  5026. (should
  5027. (= 2
  5028. (org-test-with-temp-text "Aa"
  5029. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  5030. ;;; Tags
  5031. (ert-deftest test-org/tag-string-to-alist ()
  5032. "Test `org-tag-string-to-alist' specifications."
  5033. ;; Tag without selection key.
  5034. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  5035. ;; Tag with selection key.
  5036. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  5037. ;; Tag group.
  5038. (should
  5039. (equal
  5040. (org-tag-string-to-alist "[ group : t1 t2 ]")
  5041. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  5042. ;; Mutually exclusive tags.
  5043. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  5044. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  5045. (should
  5046. (equal
  5047. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  5048. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  5049. (ert-deftest test-org/tag-alist-to-string ()
  5050. "Test `org-tag-alist-to-string' specifications."
  5051. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  5052. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  5053. (should
  5054. (equal
  5055. (org-tag-alist-to-string
  5056. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5057. "[ group : t1 t2 ]"))
  5058. (should
  5059. (equal (org-tag-alist-to-string
  5060. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  5061. "{ tag1 tag2 }"))
  5062. (should
  5063. (equal
  5064. (org-tag-alist-to-string
  5065. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  5066. "{ group : tag1 tag2 }")))
  5067. (ert-deftest test-org/tag-alist-to-groups ()
  5068. "Test `org-tag-alist-to-groups' specifications."
  5069. (should
  5070. (equal (org-tag-alist-to-groups
  5071. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  5072. '(("group" "t1" "t2"))))
  5073. (should
  5074. (equal
  5075. (org-tag-alist-to-groups
  5076. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5077. '(("group" "t1" "t2"))))
  5078. (should-not
  5079. (org-tag-alist-to-groups
  5080. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  5081. (ert-deftest test-org/tag-align ()
  5082. "Test tags alignment."
  5083. ;; Test aligning tags with different display width.
  5084. (should
  5085. ;; 12345678901234567890
  5086. (equal "* Test :abc:"
  5087. (org-test-with-temp-text "* Test :abc:"
  5088. (let ((org-tags-column -20)
  5089. (indent-tabs-mode nil))
  5090. (org-fix-tags-on-the-fly))
  5091. (buffer-string))))
  5092. (should
  5093. ;; 12345678901234567890
  5094. (equal "* Test :日本語:"
  5095. (org-test-with-temp-text "* Test :日本語:"
  5096. (let ((org-tags-column -20)
  5097. (indent-tabs-mode nil))
  5098. (org-fix-tags-on-the-fly))
  5099. (buffer-string))))
  5100. ;; Make sure aligning tags do not skip invisible text.
  5101. (should
  5102. (equal "* [[linkx]] :tag:"
  5103. (org-test-with-temp-text "* [[link<point>]] :tag:"
  5104. (let ((org-tags-column 0))
  5105. (org-fix-tags-on-the-fly)
  5106. (insert "x")
  5107. (buffer-string))))))
  5108. (ert-deftest test-org/tags-at ()
  5109. (should
  5110. (equal '("foo" "bar")
  5111. (org-test-with-temp-text
  5112. "* T<point>est :foo:bar:"
  5113. (org-get-tags-at)))))
  5114. ;;; TODO keywords
  5115. (ert-deftest test-org/auto-repeat-maybe ()
  5116. "Test `org-auto-repeat-maybe' specifications."
  5117. ;; Do not auto repeat when there is no valid time stamp with
  5118. ;; a repeater in the entry.
  5119. (should-not
  5120. (string-prefix-p
  5121. "* TODO H"
  5122. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5123. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  5124. (org-todo "DONE")
  5125. (buffer-string)))))
  5126. (should-not
  5127. (string-prefix-p
  5128. "* TODO H"
  5129. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5130. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  5131. (org-todo "DONE")
  5132. (buffer-string)))))
  5133. ;; When switching to DONE state, switch back to first TODO keyword
  5134. ;; in sequence, or the same keyword if they have different types.
  5135. (should
  5136. (string-prefix-p
  5137. "* TODO H"
  5138. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5139. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  5140. (org-todo "DONE")
  5141. (buffer-string)))))
  5142. (should
  5143. (string-prefix-p
  5144. "* KWD1 H"
  5145. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  5146. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  5147. (org-todo "DONE")
  5148. (buffer-string)))))
  5149. (should
  5150. (string-prefix-p
  5151. "* KWD2 H"
  5152. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  5153. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  5154. (org-todo "DONE")
  5155. (buffer-string)))))
  5156. ;; If there was no TODO keyword in the first place, do not insert
  5157. ;; any either.
  5158. (should
  5159. (string-prefix-p
  5160. "* H"
  5161. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5162. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  5163. (org-todo "DONE")
  5164. (buffer-string)))))
  5165. ;; Revert to REPEAT_TO_STATE, if set.
  5166. (should
  5167. (string-prefix-p
  5168. "* KWD2 H"
  5169. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  5170. (org-test-with-temp-text
  5171. "* KWD2 H
  5172. :PROPERTIES:
  5173. :REPEAT_TO_STATE: KWD2
  5174. :END:
  5175. <2012-03-29 Thu +2y>"
  5176. (org-todo "DONE")
  5177. (buffer-string)))))
  5178. ;; When switching to DONE state, update base date. If there are
  5179. ;; multiple repeated time stamps, update them all.
  5180. (should
  5181. (string-match-p
  5182. "<2014-03-29 .* \\+2y>"
  5183. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5184. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  5185. (org-todo "DONE")
  5186. (buffer-string)))))
  5187. (should
  5188. (string-match-p
  5189. "<2015-03-04 .* \\+1y>"
  5190. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5191. (org-test-with-temp-text
  5192. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  5193. (org-todo "DONE")
  5194. (buffer-string)))))
  5195. ;; Throw an error if repeater unit is the hour and no time is
  5196. ;; provided in the time-stamp.
  5197. (should-error
  5198. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5199. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  5200. (org-todo "DONE")
  5201. (buffer-string))))
  5202. ;; Do not repeat commented time stamps.
  5203. (should-not
  5204. (string-prefix-p
  5205. "<2015-03-04 .* \\+1y>"
  5206. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5207. (org-test-with-temp-text
  5208. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  5209. (org-todo "DONE")
  5210. (buffer-string)))))
  5211. (should-not
  5212. (string-prefix-p
  5213. "<2015-03-04 .* \\+1y>"
  5214. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5215. (org-test-with-temp-text
  5216. "* TODO H
  5217. <2012-03-29 Thu. +2y>
  5218. #+BEGIN_EXAMPLE
  5219. <2014-03-04 Tue +1y>
  5220. #+END_EXAMPLE"
  5221. (org-todo "DONE")
  5222. (buffer-string)))))
  5223. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  5224. ;; entry, record time of last repeat.
  5225. (should-not
  5226. (string-match-p
  5227. ":LAST_REPEAT:"
  5228. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  5229. (org-log-repeat nil))
  5230. (cl-letf (((symbol-function 'org-add-log-setup)
  5231. (lambda (&rest args) nil)))
  5232. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  5233. (org-todo "DONE")
  5234. (buffer-string))))))
  5235. (should
  5236. (string-match-p
  5237. ":LAST_REPEAT:"
  5238. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  5239. (org-log-repeat t))
  5240. (cl-letf (((symbol-function 'org-add-log-setup)
  5241. (lambda (&rest args) nil)))
  5242. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  5243. (org-todo "DONE")
  5244. (buffer-string))))))
  5245. (should
  5246. (string-match-p
  5247. ":LAST_REPEAT:"
  5248. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5249. (cl-letf (((symbol-function 'org-add-log-setup)
  5250. (lambda (&rest args) nil)))
  5251. (org-test-with-temp-text
  5252. "* TODO H\n<2012-03-29 Thu. +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  5253. (org-todo "DONE")
  5254. (buffer-string))))))
  5255. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  5256. ;; the entry as it is no longer relevant.
  5257. (should-not
  5258. (string-match-p
  5259. "^SCHEDULED:"
  5260. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5261. (org-test-with-temp-text
  5262. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  5263. (org-todo "DONE")
  5264. (buffer-string))))))
  5265. ;;; Timestamps API
  5266. (ert-deftest test-org/at-timestamp-p ()
  5267. "Test `org-at-timestamp-p' specifications."
  5268. (should
  5269. (org-test-with-temp-text "<2012-03-29 Thu>"
  5270. (org-at-timestamp-p)))
  5271. (should-not
  5272. (org-test-with-temp-text "2012-03-29 Thu"
  5273. (org-at-timestamp-p)))
  5274. ;; Test return values.
  5275. (should
  5276. (eq 'bracket
  5277. (org-test-with-temp-text "<2012-03-29 Thu>"
  5278. (org-at-timestamp-p))))
  5279. (should
  5280. (eq 'year
  5281. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  5282. (org-at-timestamp-p))))
  5283. (should
  5284. (eq 'month
  5285. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  5286. (org-at-timestamp-p))))
  5287. (should
  5288. (eq 'day
  5289. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  5290. (org-at-timestamp-p))))
  5291. (should
  5292. (eq 'day
  5293. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  5294. (org-at-timestamp-p))))
  5295. (should
  5296. (wholenump
  5297. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  5298. (org-at-timestamp-p))))
  5299. (should
  5300. (eq 'bracket
  5301. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  5302. (org-at-timestamp-p))))
  5303. (should
  5304. (eq 'after
  5305. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  5306. (org-at-timestamp-p))))
  5307. ;; Test optional argument.
  5308. (should
  5309. (org-test-with-temp-text "[2012-03-29 Thu]"
  5310. (org-at-timestamp-p t)))
  5311. (should-not
  5312. (org-test-with-temp-text "[2012-03-29 Thu]"
  5313. (org-at-timestamp-p)))
  5314. ;; Unlike `org-element-context', recognize time-stamps in planning
  5315. ;; info line, property drawers and clocks.
  5316. (should
  5317. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  5318. (org-at-timestamp-p)))
  5319. (should
  5320. (org-test-with-temp-text
  5321. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  5322. (org-at-timestamp-p)))
  5323. (should
  5324. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  5325. (org-at-timestamp-p t))))
  5326. (ert-deftest test-org/time-stamp ()
  5327. "Test `org-time-stamp' specifications."
  5328. ;; Insert chosen time stamp at point.
  5329. (should
  5330. (string-match
  5331. "Te<2014-03-04 .*?>xt"
  5332. (org-test-with-temp-text "Te<point>xt"
  5333. (cl-letf (((symbol-function 'org-read-date)
  5334. (lambda (&rest args)
  5335. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5336. (org-time-stamp nil)
  5337. (buffer-string)))))
  5338. ;; With a prefix argument, also insert time.
  5339. (should
  5340. (string-match
  5341. "Te<2014-03-04 .*? 00:41>xt"
  5342. (org-test-with-temp-text "Te<point>xt"
  5343. (cl-letf (((symbol-function 'org-read-date)
  5344. (lambda (&rest args)
  5345. (apply #'encode-time
  5346. (org-parse-time-string "2014-03-04 00:41")))))
  5347. (org-time-stamp '(4))
  5348. (buffer-string)))))
  5349. ;; With two universal prefix arguments, insert an active timestamp
  5350. ;; with the current time without prompting the user.
  5351. (should
  5352. (string-match
  5353. "Te<2014-03-04 .*? 00:41>xt"
  5354. (org-test-with-temp-text "Te<point>xt"
  5355. (cl-letf (((symbol-function 'current-time)
  5356. (lambda ()
  5357. (apply #'encode-time
  5358. (org-parse-time-string "2014-03-04 00:41")))))
  5359. (org-time-stamp '(16))
  5360. (buffer-string)))))
  5361. ;; When optional argument is non-nil, insert an inactive timestamp.
  5362. (should
  5363. (string-match
  5364. "Te\\[2014-03-04 .*?\\]xt"
  5365. (org-test-with-temp-text "Te<point>xt"
  5366. (cl-letf (((symbol-function 'org-read-date)
  5367. (lambda (&rest args)
  5368. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5369. (org-time-stamp nil t)
  5370. (buffer-string)))))
  5371. ;; When called from a timestamp, replace existing one.
  5372. (should
  5373. (string-match
  5374. "<2014-03-04 .*?>"
  5375. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  5376. (cl-letf (((symbol-function 'org-read-date)
  5377. (lambda (&rest args)
  5378. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5379. (org-time-stamp nil)
  5380. (buffer-string)))))
  5381. (should
  5382. (string-match
  5383. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  5384. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  5385. (cl-letf (((symbol-function 'org-read-date)
  5386. (lambda (&rest args)
  5387. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5388. (org-time-stamp nil)
  5389. (buffer-string)))))
  5390. ;; When replacing a timestamp, preserve repeater, if any.
  5391. (should
  5392. (string-match
  5393. "<2014-03-04 .*? \\+2y>"
  5394. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  5395. (cl-letf (((symbol-function 'org-read-date)
  5396. (lambda (&rest args)
  5397. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5398. (org-time-stamp nil)
  5399. (buffer-string)))))
  5400. ;; When called twice in a raw, build a date range.
  5401. (should
  5402. (string-match
  5403. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  5404. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  5405. (cl-letf (((symbol-function 'org-read-date)
  5406. (lambda (&rest args)
  5407. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5408. (let ((last-command 'org-time-stamp)
  5409. (this-command 'org-time-stamp))
  5410. (org-time-stamp nil))
  5411. (buffer-string))))))
  5412. (ert-deftest test-org/timestamp-has-time-p ()
  5413. "Test `org-timestamp-has-time-p' specifications."
  5414. ;; With time.
  5415. (should
  5416. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  5417. (org-timestamp-has-time-p (org-element-context))))
  5418. ;; Without time.
  5419. (should-not
  5420. (org-test-with-temp-text "<2012-03-29 Thu>"
  5421. (org-timestamp-has-time-p (org-element-context)))))
  5422. (ert-deftest test-org/get-repeat ()
  5423. "Test `org-get-repeat' specifications."
  5424. (should
  5425. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  5426. (org-get-repeat)))
  5427. (should-not
  5428. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  5429. (org-get-repeat)))
  5430. ;; Return proper repeat string.
  5431. (should
  5432. (equal "+2y"
  5433. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  5434. (org-get-repeat))))
  5435. ;; Prevent false positive (commented or verbatim time stamps)
  5436. (should-not
  5437. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  5438. (org-get-repeat)))
  5439. (should-not
  5440. (org-test-with-temp-text
  5441. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  5442. (org-get-repeat)))
  5443. ;; Return nil when called before first heading.
  5444. (should-not
  5445. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  5446. (org-get-repeat)))
  5447. ;; When called with an optional argument, extract repeater from that
  5448. ;; string instead.
  5449. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  5450. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  5451. (ert-deftest test-org/timestamp-format ()
  5452. "Test `org-timestamp-format' specifications."
  5453. ;; Regular test.
  5454. (should
  5455. (equal
  5456. "2012-03-29 16:40"
  5457. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  5458. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  5459. ;; Range end.
  5460. (should
  5461. (equal
  5462. "2012-03-29"
  5463. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  5464. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  5465. (ert-deftest test-org/timestamp-split-range ()
  5466. "Test `org-timestamp-split-range' specifications."
  5467. ;; Extract range start (active).
  5468. (should
  5469. (equal '(2012 3 29)
  5470. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  5471. (let ((ts (org-timestamp-split-range (org-element-context))))
  5472. (mapcar (lambda (p) (org-element-property p ts))
  5473. '(:year-end :month-end :day-end))))))
  5474. ;; Extract range start (inactive)
  5475. (should
  5476. (equal '(2012 3 29)
  5477. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  5478. (let ((ts (org-timestamp-split-range (org-element-context))))
  5479. (mapcar (lambda (p) (org-element-property p ts))
  5480. '(:year-end :month-end :day-end))))))
  5481. ;; Extract range end (active).
  5482. (should
  5483. (equal '(2012 3 30)
  5484. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  5485. (let ((ts (org-timestamp-split-range
  5486. (org-element-context) t)))
  5487. (mapcar (lambda (p) (org-element-property p ts))
  5488. '(:year-end :month-end :day-end))))))
  5489. ;; Extract range end (inactive)
  5490. (should
  5491. (equal '(2012 3 30)
  5492. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  5493. (let ((ts (org-timestamp-split-range
  5494. (org-element-context) t)))
  5495. (mapcar (lambda (p) (org-element-property p ts))
  5496. '(:year-end :month-end :day-end))))))
  5497. ;; Return the timestamp if not a range.
  5498. (should
  5499. (org-test-with-temp-text "[2012-03-29 Thu]"
  5500. (let* ((ts-orig (org-element-context))
  5501. (ts-copy (org-timestamp-split-range ts-orig)))
  5502. (eq ts-orig ts-copy))))
  5503. (should
  5504. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  5505. (let* ((ts-orig (org-element-context))
  5506. (ts-copy (org-timestamp-split-range ts-orig)))
  5507. (eq ts-orig ts-copy)))))
  5508. (ert-deftest test-org/timestamp-translate ()
  5509. "Test `org-timestamp-translate' specifications."
  5510. ;; Translate whole date range.
  5511. (should
  5512. (equal "<29>--<30>"
  5513. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  5514. (let ((org-display-custom-times t)
  5515. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  5516. (org-timestamp-translate (org-element-context))))))
  5517. ;; Translate date range start.
  5518. (should
  5519. (equal "<29>"
  5520. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  5521. (let ((org-display-custom-times t)
  5522. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  5523. (org-timestamp-translate (org-element-context) 'start)))))
  5524. ;; Translate date range end.
  5525. (should
  5526. (equal "<30>"
  5527. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  5528. (let ((org-display-custom-times t)
  5529. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  5530. (org-timestamp-translate (org-element-context) 'end)))))
  5531. ;; Translate time range.
  5532. (should
  5533. (equal "<08>--<16>"
  5534. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  5535. (let ((org-display-custom-times t)
  5536. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  5537. (org-timestamp-translate (org-element-context))))))
  5538. ;; Translate non-range timestamp.
  5539. (should
  5540. (equal "<29>"
  5541. (org-test-with-temp-text "<2012-03-29 Thu>"
  5542. (let ((org-display-custom-times t)
  5543. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  5544. (org-timestamp-translate (org-element-context))))))
  5545. ;; Do not change `diary' timestamps.
  5546. (should
  5547. (equal "<%%(org-float t 4 2)>"
  5548. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  5549. (let ((org-display-custom-times t)
  5550. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  5551. (org-timestamp-translate (org-element-context)))))))
  5552. ;;; Visibility
  5553. (ert-deftest test-org/flag-drawer ()
  5554. "Test `org-flag-drawer' specifications."
  5555. ;; Hide drawer.
  5556. (should
  5557. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  5558. (org-flag-drawer t)
  5559. (get-char-property (line-end-position) 'invisible)))
  5560. ;; Show drawer.
  5561. (should-not
  5562. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  5563. (org-flag-drawer t)
  5564. (org-flag-drawer nil)
  5565. (get-char-property (line-end-position) 'invisible)))
  5566. ;; Test optional argument.
  5567. (should
  5568. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  5569. (let ((drawer (save-excursion (search-forward ":D2")
  5570. (org-element-at-point))))
  5571. (org-flag-drawer t drawer)
  5572. (get-char-property (progn (search-forward ":D2") (line-end-position))
  5573. 'invisible))))
  5574. (should-not
  5575. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  5576. (let ((drawer (save-excursion (search-forward ":D2")
  5577. (org-element-at-point))))
  5578. (org-flag-drawer t drawer)
  5579. (get-char-property (line-end-position) 'invisible))))
  5580. ;; Do not hide fake drawers.
  5581. (should-not
  5582. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  5583. (forward-line 1)
  5584. (org-flag-drawer t)
  5585. (get-char-property (line-end-position) 'invisible)))
  5586. ;; Do not hide incomplete drawers.
  5587. (should-not
  5588. (org-test-with-temp-text ":D:\nparagraph"
  5589. (forward-line 1)
  5590. (org-flag-drawer t)
  5591. (get-char-property (line-end-position) 'invisible)))
  5592. ;; Do not hide drawers when called from final blank lines.
  5593. (should-not
  5594. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  5595. (goto-char (point-max))
  5596. (org-flag-drawer t)
  5597. (goto-char (point-min))
  5598. (get-char-property (line-end-position) 'invisible)))
  5599. ;; Don't leave point in an invisible part of the buffer when hiding
  5600. ;; a drawer away.
  5601. (should-not
  5602. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  5603. (goto-char (point-max))
  5604. (org-flag-drawer t)
  5605. (get-char-property (point) 'invisible))))
  5606. (ert-deftest test-org/hide-block-toggle ()
  5607. "Test `org-hide-block-toggle' specifications."
  5608. ;; Error when not at a block.
  5609. (should-error
  5610. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  5611. (org-hide-block-toggle 'off)
  5612. (get-char-property (line-end-position) 'invisible)))
  5613. ;; Hide block.
  5614. (should
  5615. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  5616. (org-hide-block-toggle)
  5617. (get-char-property (line-end-position) 'invisible)))
  5618. (should
  5619. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  5620. (org-hide-block-toggle)
  5621. (get-char-property (line-end-position) 'invisible)))
  5622. ;; Show block unconditionally when optional argument is `off'.
  5623. (should-not
  5624. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5625. (org-hide-block-toggle)
  5626. (org-hide-block-toggle 'off)
  5627. (get-char-property (line-end-position) 'invisible)))
  5628. (should-not
  5629. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5630. (org-hide-block-toggle 'off)
  5631. (get-char-property (line-end-position) 'invisible)))
  5632. ;; Hide block unconditionally when optional argument is non-nil.
  5633. (should
  5634. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5635. (org-hide-block-toggle t)
  5636. (get-char-property (line-end-position) 'invisible)))
  5637. (should
  5638. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5639. (org-hide-block-toggle)
  5640. (org-hide-block-toggle t)
  5641. (get-char-property (line-end-position) 'invisible)))
  5642. ;; Do not hide block when called from final blank lines.
  5643. (should-not
  5644. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  5645. (org-hide-block-toggle)
  5646. (goto-char (point-min))
  5647. (get-char-property (line-end-position) 'invisible)))
  5648. ;; Don't leave point in an invisible part of the buffer when hiding
  5649. ;; a block away.
  5650. (should-not
  5651. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  5652. (org-hide-block-toggle)
  5653. (get-char-property (point) 'invisible))))
  5654. (ert-deftest test-org/hide-block-toggle-maybe ()
  5655. "Test `org-hide-block-toggle-maybe' specifications."
  5656. (should
  5657. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  5658. (org-hide-block-toggle-maybe)))
  5659. (should-not
  5660. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  5661. (ert-deftest test-org/set-tags ()
  5662. "Test `org-set-tags' specifications."
  5663. ;; Tags set via fast-tag-selection should be visible afterwards
  5664. (should
  5665. (let ((org-tag-alist '(("NEXT" . ?n)))
  5666. (org-fast-tag-selection-single-key t))
  5667. (cl-letf (((symbol-function 'read-char-exclusive) (lambda () ?n))
  5668. ((symbol-function 'window-width) (lambda (&rest args) 100)))
  5669. (org-test-with-temp-text "<point>* Headline\nAnd its content\n* And another headline\n\nWith some content"
  5670. ;; Show only headlines
  5671. (org-content)
  5672. ;; Set NEXT tag on current entry
  5673. (org-set-tags nil nil)
  5674. ;; Move point to that NEXT tag
  5675. (search-forward "NEXT") (backward-word)
  5676. ;; And it should be visible (i.e. no overlays)
  5677. (not (overlays-at (point))))))))
  5678. (ert-deftest test-org/show-set-visibility ()
  5679. "Test `org-show-set-visibility' specifications."
  5680. ;; Do not throw an error before first heading.
  5681. (should
  5682. (org-test-with-temp-text "Preamble\n* Headline"
  5683. (org-show-set-visibility 'tree)
  5684. t))
  5685. ;; Test all visibility spans, both on headline and in entry.
  5686. (let ((list-visible-lines
  5687. (lambda (state headerp)
  5688. (org-test-with-temp-text "* Grandmother (0)
  5689. ** Uncle (1)
  5690. *** Heir (2)
  5691. ** Father (3)
  5692. Ancestor text (4)
  5693. *** Sister (5)
  5694. Sibling text (6)
  5695. *** Self (7)
  5696. Match (8)
  5697. **** First born (9)
  5698. Child text (10)
  5699. **** The other child (11)
  5700. *** Brother (12)
  5701. ** Aunt (13)
  5702. "
  5703. (org-cycle t)
  5704. (search-forward (if headerp "Self" "Match"))
  5705. (org-show-set-visibility state)
  5706. (goto-char (point-min))
  5707. (let (result (line 0))
  5708. (while (not (eobp))
  5709. (unless (org-invisible-p2) (push line result))
  5710. (incf line)
  5711. (forward-line))
  5712. (nreverse result))))))
  5713. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  5714. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  5715. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  5716. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  5717. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  5718. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  5719. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  5720. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  5721. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  5722. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  5723. (funcall list-visible-lines 'tree nil)))
  5724. (should (equal '(0 1 3 4 5 7 12 13)
  5725. (funcall list-visible-lines 'canonical t)))
  5726. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  5727. (funcall list-visible-lines 'canonical nil))))
  5728. ;; When point is hidden in a drawer or a block, make sure to make it
  5729. ;; visible.
  5730. (should-not
  5731. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  5732. (org-hide-block-toggle)
  5733. (search-forward "Text")
  5734. (org-show-set-visibility 'minimal)
  5735. (org-invisible-p2)))
  5736. (should-not
  5737. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  5738. (org-flag-drawer t)
  5739. (search-forward "Text")
  5740. (org-show-set-visibility 'minimal)
  5741. (org-invisible-p2)))
  5742. (should-not
  5743. (org-test-with-temp-text
  5744. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  5745. (org-flag-drawer t)
  5746. (forward-line -1)
  5747. (org-hide-block-toggle)
  5748. (search-forward "Text")
  5749. (org-show-set-visibility 'minimal)
  5750. (org-invisible-p2))))
  5751. (provide 'test-org)
  5752. ;;; test-org.el ends here