test-org.el 172 KB

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