test-org.el 168 KB

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