test-org.el 152 KB

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