test-org.el 148 KB

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