test-org.el 136 KB

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