test-org.el 142 KB

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