test-org.el 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869
  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 |"
  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)) (org-return))
  856. (org-looking-at-p "<<target>>")))
  857. (should-not
  858. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  859. (let ((org-return-follows-link nil)) (org-return))
  860. (org-looking-at-p "<<target>>")))
  861. ;; However, do not open link when point is in a table.
  862. (should
  863. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  864. (let ((org-return-follows-link t)) (org-return))
  865. (org-looking-at-p "between")))
  866. ;; Special case: in a list, when indenting, do not break structure.
  867. (should
  868. (equal "- A\n B"
  869. (org-test-with-temp-text "- A <point>B"
  870. (org-return t)
  871. (buffer-string))))
  872. (should
  873. (equal "- A\n\n- B"
  874. (org-test-with-temp-text "- A\n<point>- B"
  875. (org-return t)
  876. (buffer-string))))
  877. ;; On tags part of a headline, add a newline below it instead of
  878. ;; breaking it.
  879. (should
  880. (equal "* H :tag:\n"
  881. (org-test-with-temp-text "* H :<point>tag:"
  882. (org-return)
  883. (buffer-string))))
  884. ;; Before headline text, add a newline below it instead of breaking
  885. ;; it.
  886. (should
  887. (equal "* TODO H :tag:\n"
  888. (org-test-with-temp-text "* <point>TODO H :tag:"
  889. (org-return)
  890. (buffer-string))))
  891. (should
  892. (equal "* TODO [#B] H :tag:\n"
  893. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  894. (org-return)
  895. (buffer-string))))
  896. ;; At headline text, break headline text but preserve tags.
  897. (should
  898. (equal "* TODO [#B] foo :tag:\nbar"
  899. (let (org-auto-align-tags)
  900. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  901. (org-return)
  902. (buffer-string)))))
  903. ;; At bol of headline insert newline.
  904. (should
  905. (equal "\n* h"
  906. (org-test-with-temp-text "<point>* h"
  907. (org-return)
  908. (buffer-string)))))
  909. (ert-deftest test-org/meta-return ()
  910. "Test M-RET (`org-meta-return') specifications."
  911. ;; In a table field insert a row above.
  912. (should
  913. (org-test-with-temp-text "| a |"
  914. (forward-char)
  915. (org-meta-return)
  916. (forward-line -1)
  917. (looking-at "| |$")))
  918. ;; In a paragraph change current line into a header.
  919. (should
  920. (org-test-with-temp-text "a"
  921. (org-meta-return)
  922. (beginning-of-line)
  923. (looking-at "\* a$")))
  924. ;; In an item insert an item, in this case above.
  925. (should
  926. (org-test-with-temp-text "- a"
  927. (org-meta-return)
  928. (beginning-of-line)
  929. (looking-at "- $")))
  930. ;; In a drawer and item insert an item, in this case above.
  931. (should
  932. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  933. (forward-line)
  934. (org-meta-return)
  935. (beginning-of-line)
  936. (looking-at "- $"))))
  937. (ert-deftest test-org/insert-heading ()
  938. "Test `org-insert-heading' specifications."
  939. ;; In an empty buffer, insert a new headline.
  940. (should
  941. (equal "* "
  942. (org-test-with-temp-text ""
  943. (org-insert-heading)
  944. (buffer-string))))
  945. ;; At the beginning of a line, turn it into a headline
  946. (should
  947. (equal "* P"
  948. (org-test-with-temp-text "<point>P"
  949. (org-insert-heading)
  950. (buffer-string))))
  951. ;; In the middle of a line, split the line if allowed, otherwise,
  952. ;; insert the headline at its end.
  953. (should
  954. (equal "Para\n* graph"
  955. (org-test-with-temp-text "Para<point>graph"
  956. (let ((org-M-RET-may-split-line '((default . t))))
  957. (org-insert-heading))
  958. (buffer-string))))
  959. (should
  960. (equal "Paragraph\n* "
  961. (org-test-with-temp-text "Para<point>graph"
  962. (let ((org-M-RET-may-split-line '((default . nil))))
  963. (org-insert-heading))
  964. (buffer-string))))
  965. ;; When on a list, insert an item instead, unless called with an
  966. ;; universal argument or if list is invisible. In this case, create
  967. ;; a new headline after contents.
  968. (should
  969. (equal "* H\n- item\n- "
  970. (org-test-with-temp-text "* H\n- item<point>"
  971. (let ((org-insert-heading-respect-content nil))
  972. (org-insert-heading))
  973. (buffer-string))))
  974. (should
  975. (equal "* H\n- item\n- item 2\n* "
  976. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  977. (let ((org-insert-heading-respect-content nil))
  978. (org-insert-heading '(4)))
  979. (buffer-string))))
  980. (should
  981. (equal "* H\n- item\n* "
  982. (org-test-with-temp-text "* H\n- item"
  983. (org-cycle)
  984. (goto-char (point-max))
  985. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  986. (buffer-string))))
  987. ;; When called with two universal arguments, insert a new headline
  988. ;; at the end of the grandparent subtree.
  989. (should
  990. (equal "* H1\n** H3\n- item\n** H2\n** "
  991. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  992. (let ((org-insert-heading-respect-content nil))
  993. (org-insert-heading '(16)))
  994. (buffer-string))))
  995. ;; When optional TOP-LEVEL argument is non-nil, always insert
  996. ;; a level 1 heading.
  997. (should
  998. (equal "* H1\n** H2\n* "
  999. (org-test-with-temp-text "* H1\n** H2<point>"
  1000. (org-insert-heading nil nil t)
  1001. (buffer-string))))
  1002. (should
  1003. (equal "* H1\n- item\n* "
  1004. (org-test-with-temp-text "* H1\n- item<point>"
  1005. (org-insert-heading nil nil t)
  1006. (buffer-string))))
  1007. ;; Corner case: correctly insert a headline after an empty one.
  1008. (should
  1009. (equal "* \n* "
  1010. (org-test-with-temp-text "* <point>"
  1011. (org-insert-heading)
  1012. (buffer-string)))))
  1013. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1014. "Test `org-insert-todo-heading-respect-content' specifications."
  1015. ;; Create a TODO heading.
  1016. (should
  1017. (org-test-with-temp-text "* H1\n Body"
  1018. (org-insert-todo-heading-respect-content)
  1019. (nth 2 (org-heading-components))))
  1020. ;; Add headline at the end of the first subtree
  1021. (should
  1022. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  1023. (search-forward "H1Body")
  1024. (org-insert-todo-heading-respect-content)
  1025. (and (eobp) (org-at-heading-p))))
  1026. ;; In a list, do not create a new item.
  1027. (should
  1028. (org-test-with-temp-text "* H\n- an item\n- another one"
  1029. (search-forward "an ")
  1030. (org-insert-todo-heading-respect-content)
  1031. (and (eobp) (org-at-heading-p)))))
  1032. ;;; Fixed-Width Areas
  1033. (ert-deftest test-org/toggle-fixed-width ()
  1034. "Test `org-toggle-fixed-width' specifications."
  1035. ;; No region: Toggle on fixed-width marker in paragraphs.
  1036. (should
  1037. (equal ": A"
  1038. (org-test-with-temp-text "A"
  1039. (org-toggle-fixed-width)
  1040. (buffer-string))))
  1041. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1042. (should
  1043. (equal "A"
  1044. (org-test-with-temp-text ": A"
  1045. (org-toggle-fixed-width)
  1046. (buffer-string))))
  1047. ;; No region: Toggle on marker in blank lines after elements or just
  1048. ;; after a headline.
  1049. (should
  1050. (equal "* H\n: "
  1051. (org-test-with-temp-text "* H\n"
  1052. (forward-line)
  1053. (org-toggle-fixed-width)
  1054. (buffer-string))))
  1055. (should
  1056. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1057. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1058. (goto-char (point-max))
  1059. (org-toggle-fixed-width)
  1060. (buffer-string))))
  1061. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1062. ;; headlines, clocks)
  1063. (should
  1064. (equal ": * Headline"
  1065. (org-test-with-temp-text "* Headline"
  1066. (org-toggle-fixed-width)
  1067. (buffer-string))))
  1068. (should
  1069. (equal ": #+KEYWORD: value"
  1070. (org-test-with-temp-text "#+KEYWORD: value"
  1071. (org-toggle-fixed-width)
  1072. (buffer-string))))
  1073. ;; No region: error in other situations.
  1074. (should-error
  1075. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1076. (forward-line)
  1077. (org-toggle-fixed-width)
  1078. (buffer-string)))
  1079. ;; No region: Indentation is preserved.
  1080. (should
  1081. (equal "- A\n : B"
  1082. (org-test-with-temp-text "- A\n B"
  1083. (forward-line)
  1084. (org-toggle-fixed-width)
  1085. (buffer-string))))
  1086. ;; Region: If it contains only fixed-width elements and blank lines,
  1087. ;; toggle off fixed-width markup.
  1088. (should
  1089. (equal "A\n\nB"
  1090. (org-test-with-temp-text ": A\n\n: B"
  1091. (transient-mark-mode 1)
  1092. (push-mark (point) t t)
  1093. (goto-char (point-max))
  1094. (org-toggle-fixed-width)
  1095. (buffer-string))))
  1096. ;; Region: If it contains anything else, toggle on fixed-width but
  1097. ;; not on fixed-width areas.
  1098. (should
  1099. (equal ": A\n: \n: B\n: \n: C"
  1100. (org-test-with-temp-text "A\n\n: B\n\nC"
  1101. (transient-mark-mode 1)
  1102. (push-mark (point) t t)
  1103. (goto-char (point-max))
  1104. (org-toggle-fixed-width)
  1105. (buffer-string))))
  1106. ;; Region: Ignore blank lines at its end, unless it contains only
  1107. ;; such lines.
  1108. (should
  1109. (equal ": A\n\n"
  1110. (org-test-with-temp-text "A\n\n"
  1111. (transient-mark-mode 1)
  1112. (push-mark (point) t t)
  1113. (goto-char (point-max))
  1114. (org-toggle-fixed-width)
  1115. (buffer-string))))
  1116. (should
  1117. (equal ": \n: \n"
  1118. (org-test-with-temp-text "\n\n"
  1119. (transient-mark-mode 1)
  1120. (push-mark (point) t t)
  1121. (goto-char (point-max))
  1122. (org-toggle-fixed-width)
  1123. (buffer-string)))))
  1124. ;;; Headline
  1125. (ert-deftest test-org/in-commented-heading-p ()
  1126. "Test `org-in-commented-heading-p' specifications."
  1127. ;; Commented headline.
  1128. (should
  1129. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1130. (goto-char (point-max))
  1131. (org-in-commented-heading-p)))
  1132. ;; Commented ancestor.
  1133. (should
  1134. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1135. (goto-char (point-max))
  1136. (org-in-commented-heading-p)))
  1137. ;; Comment keyword is case-sensitive.
  1138. (should-not
  1139. (org-test-with-temp-text "* Comment Headline\nBody"
  1140. (goto-char (point-max))
  1141. (org-in-commented-heading-p)))
  1142. ;; Keyword is standalone.
  1143. (should-not
  1144. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1145. (goto-char (point-max))
  1146. (org-in-commented-heading-p)))
  1147. ;; Optional argument.
  1148. (should-not
  1149. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1150. (goto-char (point-max))
  1151. (org-in-commented-heading-p t))))
  1152. ;;; Keywords
  1153. (ert-deftest test-org/set-regexps-and-options ()
  1154. "Test `org-set-regexps-and-options' specifications."
  1155. ;; TAGS keyword.
  1156. (should
  1157. (equal '(("A" . ?a) ("B") ("C"))
  1158. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1159. (org-mode-restart)
  1160. org-tag-alist)))
  1161. (should
  1162. (equal '(("A") (:newline) ("B"))
  1163. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1164. (org-mode-restart)
  1165. org-tag-alist)))
  1166. (should
  1167. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1168. (org-test-with-temp-text "#+TAGS: { A B } C"
  1169. (org-mode-restart)
  1170. org-tag-alist)))
  1171. (should
  1172. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1173. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1174. (org-mode-restart)
  1175. org-tag-alist)))
  1176. (should
  1177. (equal '(("A" "B" "C"))
  1178. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1179. (org-mode-restart)
  1180. org-tag-groups-alist)))
  1181. (should
  1182. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1183. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1184. (org-mode-restart)
  1185. org-tag-alist)))
  1186. (should
  1187. (equal '(("A" "B" "C"))
  1188. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1189. (org-mode-restart)
  1190. org-tag-groups-alist)))
  1191. ;; FILETAGS keyword.
  1192. (should
  1193. (equal '("A" "B" "C")
  1194. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1195. (org-mode-restart)
  1196. org-file-tags)))
  1197. ;; PROPERTY keyword. Property names are case-insensitive.
  1198. (should
  1199. (equal "foo=1"
  1200. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1201. (org-mode-restart)
  1202. (cdr (assoc "var" org-file-properties)))))
  1203. (should
  1204. (equal
  1205. "foo=1 bar=2"
  1206. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1207. (org-mode-restart)
  1208. (cdr (assoc "var" org-file-properties)))))
  1209. (should
  1210. (equal
  1211. "foo=1 bar=2"
  1212. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1213. (org-mode-restart)
  1214. (cdr (assoc "var" org-file-properties)))))
  1215. ;; ARCHIVE keyword.
  1216. (should
  1217. (equal "%s_done::"
  1218. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1219. (org-mode-restart)
  1220. org-archive-location)))
  1221. ;; CATEGORY keyword.
  1222. (should
  1223. (eq 'test
  1224. (org-test-with-temp-text "#+CATEGORY: test"
  1225. (org-mode-restart)
  1226. org-category)))
  1227. (should
  1228. (equal "test"
  1229. (org-test-with-temp-text "#+CATEGORY: test"
  1230. (org-mode-restart)
  1231. (cdr (assoc "CATEGORY" org-file-properties)))))
  1232. ;; COLUMNS keyword.
  1233. (should
  1234. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1235. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1236. (org-mode-restart)
  1237. org-columns-default-format)))
  1238. ;; CONSTANTS keyword. Constants names are case sensitive.
  1239. (should
  1240. (equal '("299792458." "3.14")
  1241. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1242. (org-mode-restart)
  1243. (mapcar
  1244. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1245. '("c" "pi")))))
  1246. (should
  1247. (equal "3.14"
  1248. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1249. (org-mode-restart)
  1250. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1251. (should
  1252. (equal "22/7"
  1253. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1254. (org-mode-restart)
  1255. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1256. ;; LINK keyword.
  1257. (should
  1258. (equal
  1259. '("url1" "url2")
  1260. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1261. (org-mode-restart)
  1262. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1263. '("a" "b")))))
  1264. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1265. (should
  1266. (equal
  1267. '(?X ?Z ?Y)
  1268. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1269. (org-mode-restart)
  1270. (list org-highest-priority org-lowest-priority org-default-priority))))
  1271. (should
  1272. (equal
  1273. '(?A ?C ?B)
  1274. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1275. (org-mode-restart)
  1276. (list org-highest-priority org-lowest-priority org-default-priority))))
  1277. ;; STARTUP keyword.
  1278. (should
  1279. (equal '(t t)
  1280. (org-test-with-temp-text "#+STARTUP: fold odd"
  1281. (org-mode-restart)
  1282. (list org-startup-folded org-odd-levels-only))))
  1283. ;; TODO keywords.
  1284. (should
  1285. (equal '(("A" "B") ("C"))
  1286. (org-test-with-temp-text "#+TODO: A B | C"
  1287. (org-mode-restart)
  1288. (list org-not-done-keywords org-done-keywords))))
  1289. (should
  1290. (equal '(("A" "C") ("B" "D"))
  1291. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1292. (org-mode-restart)
  1293. (list org-not-done-keywords org-done-keywords))))
  1294. (should
  1295. (equal '(("A" "B") ("C"))
  1296. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1297. (org-mode-restart)
  1298. (list org-not-done-keywords org-done-keywords))))
  1299. (should
  1300. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1301. (org-test-with-temp-text "#+TODO: A(a)"
  1302. (org-mode-restart)
  1303. org-todo-key-alist)))
  1304. (should
  1305. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1306. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1307. (org-mode-restart)
  1308. org-todo-log-states)))
  1309. ;; Enter SETUPFILE keyword.
  1310. (should
  1311. (equal "1"
  1312. (org-test-with-temp-text
  1313. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1314. (org-mode-restart)
  1315. (cdr (assoc "a" org-file-properties))))))
  1316. ;;; Links
  1317. ;;;; Coderefs
  1318. (ert-deftest test-org/coderef ()
  1319. "Test coderef links specifications."
  1320. (should
  1321. (org-test-with-temp-text "
  1322. #+BEGIN_SRC emacs-lisp
  1323. \(+ 1 1) (ref:sc)
  1324. #+END_SRC
  1325. \[[(sc)]]"
  1326. (goto-char (point-max))
  1327. (org-open-at-point)
  1328. (looking-at "(ref:sc)"))))
  1329. ;;;; Custom ID
  1330. (ert-deftest test-org/custom-id ()
  1331. "Test custom ID links specifications."
  1332. (should
  1333. (org-test-with-temp-text
  1334. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  1335. (goto-char (point-max))
  1336. (org-open-at-point)
  1337. (org-looking-at-p "\\* H1")))
  1338. ;; Ignore false positives.
  1339. (should-not
  1340. (org-test-with-temp-text
  1341. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
  1342. (goto-char (point-max))
  1343. (let (org-link-search-must-match-exact-headline) (org-open-at-point))
  1344. (org-looking-at-p "\\* H1"))))
  1345. ;;;; Fuzzy Links
  1346. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1347. ;; a named element (#+name: text) and to headlines (* Text).
  1348. (ert-deftest test-org/fuzzy-links ()
  1349. "Test fuzzy links specifications."
  1350. ;; Fuzzy link goes in priority to a matching target.
  1351. (should
  1352. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  1353. (goto-line 5)
  1354. (org-open-at-point)
  1355. (looking-at "<<Test>>")))
  1356. ;; Then fuzzy link points to an element with a given name.
  1357. (should
  1358. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  1359. (goto-line 5)
  1360. (org-open-at-point)
  1361. (looking-at "#\\+NAME: Test")))
  1362. ;; A target still lead to a matching headline otherwise.
  1363. (should
  1364. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  1365. (goto-line 4)
  1366. (org-open-at-point)
  1367. (looking-at "\\* Head2")))
  1368. ;; With a leading star in link, enforce heading match.
  1369. (should
  1370. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  1371. (goto-line 3)
  1372. (org-open-at-point)
  1373. (looking-at "\\* Test")))
  1374. ;; With a leading star in link, enforce exact heading match, even
  1375. ;; with `org-link-search-must-match-exact-headline' set to nil.
  1376. (should-error
  1377. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  1378. (let ((org-link-search-must-match-exact-headline nil))
  1379. (org-open-at-point))))
  1380. ;; Heading match should not care about spaces, cookies, todo
  1381. ;; keywords, priorities, and tags.
  1382. (should
  1383. (let ((first-line
  1384. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1385. (org-test-with-temp-text
  1386. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  1387. (let ((org-link-search-must-match-exact-headline nil)
  1388. (org-todo-regexp "TODO"))
  1389. (org-open-at-point))
  1390. (looking-at (regexp-quote first-line)))))
  1391. ;; Heading match should still be exact.
  1392. (should-error
  1393. (let ((first-line
  1394. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1395. (org-test-with-temp-text
  1396. (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
  1397. (let ((org-link-search-must-match-exact-headline nil)
  1398. (org-todo-regexp "TODO"))
  1399. (org-open-at-point)))))
  1400. ;; Heading match ignores COMMENT keyword.
  1401. (should
  1402. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  1403. (org-open-at-point)
  1404. (looking-at "\\* COMMENT Test")))
  1405. ;; Correctly un-hexify fuzzy links.
  1406. (should
  1407. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  1408. (goto-char (point-max))
  1409. (org-open-at-point)
  1410. (bobp))))
  1411. ;;;; Link Escaping
  1412. (ert-deftest test-org/org-link-escape-ascii-character ()
  1413. "Escape an ascii character."
  1414. (should
  1415. (string=
  1416. "%5B"
  1417. (org-link-escape "["))))
  1418. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  1419. "Escape an ascii control character."
  1420. (should
  1421. (string=
  1422. "%09"
  1423. (org-link-escape "\t"))))
  1424. (ert-deftest test-org/org-link-escape-multibyte-character ()
  1425. "Escape an unicode multibyte character."
  1426. (should
  1427. (string=
  1428. "%E2%82%AC"
  1429. (org-link-escape "€"))))
  1430. (ert-deftest test-org/org-link-escape-custom-table ()
  1431. "Escape string with custom character table."
  1432. (should
  1433. (string=
  1434. "Foo%3A%42ar%0A"
  1435. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  1436. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  1437. "Escape string with custom table merged with default table."
  1438. (should
  1439. (string=
  1440. "%5BF%6F%6F%3A%42ar%0A%5D"
  1441. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  1442. (ert-deftest test-org/org-link-unescape-ascii-character ()
  1443. "Unescape an ascii character."
  1444. (should
  1445. (string=
  1446. "["
  1447. (org-link-unescape "%5B"))))
  1448. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  1449. "Unescpae an ascii control character."
  1450. (should
  1451. (string=
  1452. "\n"
  1453. (org-link-unescape "%0A"))))
  1454. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  1455. "Unescape unicode multibyte character."
  1456. (should
  1457. (string=
  1458. "€"
  1459. (org-link-unescape "%E2%82%AC"))))
  1460. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  1461. "Unescape old style percent escaped character."
  1462. (should
  1463. (string=
  1464. "àâçèéêîôùû"
  1465. (decode-coding-string
  1466. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  1467. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  1468. "Escape and unescape a URL that includes an escaped char.
  1469. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  1470. (should
  1471. (string=
  1472. "http://some.host.com/form?&id=blah%2Bblah25"
  1473. (org-link-unescape
  1474. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  1475. (ert-deftest test-org/org-link-escape-chars-browser ()
  1476. "Test of the constant `org-link-escape-chars-browser'.
  1477. See there why this test is a candidate to be removed once Org
  1478. drops support for Emacs 24.1 and 24.2."
  1479. (should
  1480. (string=
  1481. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1482. "%22Release%208.2%22&idxname=emacs-orgmode")
  1483. (org-link-escape-browser ; Do not replace with `url-encode-url',
  1484. ; see docstring above.
  1485. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1486. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  1487. ;;;; Open at point
  1488. (ert-deftest test-org/open-at-point-in-keyword ()
  1489. "Does `org-open-at-point' open link in a keyword line?"
  1490. (should
  1491. (org-test-with-temp-text
  1492. "#+KEYWORD: <point>[[info:emacs#Top]]"
  1493. (org-open-at-point) t)))
  1494. (ert-deftest test-org/open-at-point-in-property ()
  1495. "Does `org-open-at-point' open link in property drawer?"
  1496. (should
  1497. (org-test-with-temp-text
  1498. "* Headline
  1499. :PROPERTIES:
  1500. :URL: <point>[[info:emacs#Top]]
  1501. :END:"
  1502. (org-open-at-point) t)))
  1503. (ert-deftest test-org/open-at-point-in-comment ()
  1504. "Does `org-open-at-point' open link in a commented line?"
  1505. (should
  1506. (org-test-with-temp-text
  1507. "# <point>[[info:emacs#Top]]"
  1508. (org-open-at-point) t)))
  1509. (ert-deftest test-org/open-at-point/info ()
  1510. "Test `org-open-at-point' on info links."
  1511. (should
  1512. (org-test-with-temp-text
  1513. "<point>[[info:emacs#Top]]"
  1514. (org-open-at-point)
  1515. (and (switch-to-buffer "*info*")
  1516. (prog1
  1517. (looking-at "\nThe Emacs Editor")
  1518. (kill-buffer))))))
  1519. (ert-deftest test-org/open-at-point/inline-image ()
  1520. "Test `org-open-at-point' on nested links."
  1521. (should
  1522. (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
  1523. (org-open-at-point)
  1524. (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
  1525. (kill-buffer "*info*")))))
  1526. ;;; Node Properties
  1527. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1528. "Ensure properties accumulate in subtree drawers."
  1529. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1530. (org-babel-next-src-block)
  1531. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1532. (ert-deftest test-org/custom-properties ()
  1533. "Test custom properties specifications."
  1534. ;; Standard test.
  1535. (should
  1536. (let ((org-custom-properties '("FOO")))
  1537. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1538. (org-toggle-custom-properties-visibility)
  1539. (org-invisible-p2))))
  1540. ;; Properties are case-insensitive.
  1541. (should
  1542. (let ((org-custom-properties '("FOO")))
  1543. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  1544. (org-toggle-custom-properties-visibility)
  1545. (org-invisible-p2))))
  1546. (should
  1547. (let ((org-custom-properties '("foo")))
  1548. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1549. (org-toggle-custom-properties-visibility)
  1550. (org-invisible-p2))))
  1551. ;; Multiple custom properties in the same drawer.
  1552. (should
  1553. (let ((org-custom-properties '("FOO" "BAR")))
  1554. (org-test-with-temp-text
  1555. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  1556. (org-toggle-custom-properties-visibility)
  1557. (and (org-invisible-p2)
  1558. (not (progn (forward-line) (org-invisible-p2)))
  1559. (progn (forward-line) (org-invisible-p2))))))
  1560. ;; Hide custom properties with an empty value.
  1561. (should
  1562. (let ((org-custom-properties '("FOO")))
  1563. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  1564. (org-toggle-custom-properties-visibility)
  1565. (org-invisible-p2))))
  1566. ;; Do not hide fake properties.
  1567. (should-not
  1568. (let ((org-custom-properties '("FOO")))
  1569. (org-test-with-temp-text ":FOO: val\n"
  1570. (org-toggle-custom-properties-visibility)
  1571. (org-invisible-p2))))
  1572. (should-not
  1573. (let ((org-custom-properties '("A")))
  1574. (org-test-with-temp-text
  1575. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  1576. (org-toggle-custom-properties-visibility)
  1577. (org-invisible-p2)))))
  1578. ;;; Mark Region
  1579. (ert-deftest test-org/mark-subtree ()
  1580. "Test `org-mark-subtree' specifications."
  1581. ;; Error when point is before first headline.
  1582. (should-error
  1583. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1584. (progn (transient-mark-mode 1)
  1585. (org-mark-subtree))))
  1586. ;; Without argument, mark current subtree.
  1587. (should
  1588. (equal
  1589. '(12 32)
  1590. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1591. (progn (transient-mark-mode 1)
  1592. (forward-line 2)
  1593. (org-mark-subtree)
  1594. (list (region-beginning) (region-end))))))
  1595. ;; With an argument, move ARG up.
  1596. (should
  1597. (equal
  1598. '(1 32)
  1599. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1600. (progn (transient-mark-mode 1)
  1601. (forward-line 2)
  1602. (org-mark-subtree 1)
  1603. (list (region-beginning) (region-end))))))
  1604. ;; Do not get fooled by inlinetasks.
  1605. (when (featurep 'org-inlinetask)
  1606. (should
  1607. (= 1
  1608. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1609. (progn (transient-mark-mode 1)
  1610. (forward-line 1)
  1611. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1612. (region-beginning)))))))
  1613. ;;; Navigation
  1614. (ert-deftest test-org/end-of-meta-data ()
  1615. "Test `org-end-of-meta-data' specifications."
  1616. ;; Skip planning line.
  1617. (should
  1618. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  1619. (org-end-of-meta-data)
  1620. (eobp)))
  1621. ;; Skip properties drawer.
  1622. (should
  1623. (org-test-with-temp-text
  1624. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  1625. (org-end-of-meta-data)
  1626. (eobp)))
  1627. ;; Skip both.
  1628. (should
  1629. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  1630. (org-end-of-meta-data)
  1631. (eobp)))
  1632. ;; Nothing to skip, go to first line.
  1633. (should
  1634. (org-test-with-temp-text "* Headline\nContents"
  1635. (org-end-of-meta-data)
  1636. (looking-at "Contents")))
  1637. ;; With option argument, skip empty lines, regular drawers and
  1638. ;; clocking lines.
  1639. (should
  1640. (org-test-with-temp-text "* Headline\n\nContents"
  1641. (org-end-of-meta-data t)
  1642. (looking-at "Contents")))
  1643. (should
  1644. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  1645. (org-end-of-meta-data t)
  1646. (looking-at "Contents")))
  1647. (should
  1648. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  1649. (org-end-of-meta-data t)
  1650. (looking-at "Contents")))
  1651. ;; Special case: do not skip incomplete drawers.
  1652. (should
  1653. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  1654. (org-end-of-meta-data t)
  1655. (looking-at ":LOGBOOK:")))
  1656. ;; Special case: Be careful about consecutive headlines.
  1657. (should-not
  1658. (org-test-with-temp-text "* H1\n*H2\nContents"
  1659. (org-end-of-meta-data t)
  1660. (looking-at "Contents"))))
  1661. (ert-deftest test-org/beginning-of-line ()
  1662. "Test `org-beginning-of-line' specifications."
  1663. ;; Standard test.
  1664. (should
  1665. (org-test-with-temp-text "Some text\nSome other text"
  1666. (progn (org-beginning-of-line) (bolp))))
  1667. ;; Standard test with `visual-line-mode'.
  1668. (should-not
  1669. (org-test-with-temp-text "A long line of text\nSome other text"
  1670. (progn (visual-line-mode)
  1671. (forward-char 2)
  1672. (dotimes (i 1000) (insert "very "))
  1673. (org-beginning-of-line)
  1674. (bolp))))
  1675. ;; At an headline with special movement.
  1676. (should
  1677. (org-test-with-temp-text "* TODO Headline"
  1678. (let ((org-special-ctrl-a/e t))
  1679. (org-end-of-line)
  1680. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1681. (progn (org-beginning-of-line) (bolp))
  1682. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1683. (ert-deftest test-org/end-of-line ()
  1684. "Test `org-end-of-line' specifications."
  1685. ;; Standard test.
  1686. (should
  1687. (org-test-with-temp-text "Some text\nSome other text"
  1688. (progn (org-end-of-line) (eolp))))
  1689. ;; Standard test with `visual-line-mode'.
  1690. (should-not
  1691. (org-test-with-temp-text "A long line of text\nSome other text"
  1692. (progn (visual-line-mode)
  1693. (forward-char 2)
  1694. (dotimes (i 1000) (insert "very "))
  1695. (goto-char (point-min))
  1696. (org-end-of-line)
  1697. (eolp))))
  1698. ;; At an headline with special movement.
  1699. (should
  1700. (org-test-with-temp-text "* Headline1 :tag:\n"
  1701. (let ((org-special-ctrl-a/e t))
  1702. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1703. (progn (org-end-of-line) (eolp))
  1704. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1705. ;; At an headline without special movement.
  1706. (should
  1707. (org-test-with-temp-text "* Headline2 :tag:\n"
  1708. (let ((org-special-ctrl-a/e nil))
  1709. (and (progn (org-end-of-line) (eolp))
  1710. (progn (org-end-of-line) (eolp))))))
  1711. ;; At an headline, with reversed movement.
  1712. (should
  1713. (org-test-with-temp-text "* Headline3 :tag:\n"
  1714. (let ((org-special-ctrl-a/e 'reversed)
  1715. (this-command last-command))
  1716. (and (progn (org-end-of-line) (eolp))
  1717. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1718. ;; At a block without hidden contents.
  1719. (should
  1720. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1721. (progn (org-end-of-line) (eolp))))
  1722. ;; At a block with hidden contents.
  1723. (should-not
  1724. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1725. (let ((org-special-ctrl-a/e t))
  1726. (org-hide-block-toggle)
  1727. (org-end-of-line)
  1728. (eobp)))))
  1729. (ert-deftest test-org/forward-sentence ()
  1730. "Test `org-forward-sentence' specifications."
  1731. ;; At the end of a table cell, move to the end of the next one.
  1732. (should
  1733. (org-test-with-temp-text "| a<point> | b |"
  1734. (org-forward-sentence)
  1735. (looking-at " |$")))
  1736. ;; Elsewhere in a cell, move to its end.
  1737. (should
  1738. (org-test-with-temp-text "| a<point>c | b |"
  1739. (org-forward-sentence)
  1740. (looking-at " | b |$")))
  1741. ;; Otherwise, simply call `forward-sentence'.
  1742. (should
  1743. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  1744. (org-forward-sentence)
  1745. (looking-at " Sentence 2.")))
  1746. (should
  1747. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  1748. (org-forward-sentence)
  1749. (org-forward-sentence)
  1750. (eobp)))
  1751. ;; At the end of an element, jump to the next one, without stopping
  1752. ;; on blank lines in-between.
  1753. (should
  1754. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  1755. (org-forward-sentence)
  1756. (eobp))))
  1757. (ert-deftest test-org/backward-sentence ()
  1758. "Test `org-backward-sentence' specifications."
  1759. ;; At the beginning of a table cell, move to the beginning of the
  1760. ;; previous one.
  1761. (should
  1762. (org-test-with-temp-text "| a | <point>b |"
  1763. (org-backward-sentence)
  1764. (looking-at "a | b |$")))
  1765. ;; Elsewhere in a cell, move to its beginning.
  1766. (should
  1767. (org-test-with-temp-text "| a | b<point>c |"
  1768. (org-backward-sentence)
  1769. (looking-at "bc |$")))
  1770. ;; Otherwise, simply call `backward-sentence'.
  1771. (should
  1772. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  1773. (org-backward-sentence)
  1774. (looking-at "Sentence 2.")))
  1775. (should
  1776. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  1777. (org-backward-sentence)
  1778. (org-backward-sentence)
  1779. (bobp)))
  1780. ;; Make sure to hit the beginning of a sentence on the same line as
  1781. ;; an item.
  1782. (should
  1783. (org-test-with-temp-text "- Line 1\n line <point>2."
  1784. (org-backward-sentence)
  1785. (looking-at "Line 1"))))
  1786. (ert-deftest test-org/forward-paragraph ()
  1787. "Test `org-forward-paragraph' specifications."
  1788. ;; At end of buffer, return an error.
  1789. (should-error
  1790. (org-test-with-temp-text "Paragraph"
  1791. (goto-char (point-max))
  1792. (org-forward-paragraph)))
  1793. ;; Standard test.
  1794. (should
  1795. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1796. (org-forward-paragraph)
  1797. (looking-at "P2")))
  1798. ;; Ignore depth.
  1799. (should
  1800. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1801. (org-forward-paragraph)
  1802. (looking-at "P1")))
  1803. ;; Do not enter elements with invisible contents.
  1804. (should
  1805. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1806. (org-hide-block-toggle)
  1807. (org-forward-paragraph)
  1808. (looking-at "P3")))
  1809. ;; On an affiliated keyword, jump to the beginning of the element.
  1810. (should
  1811. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1812. (org-forward-paragraph)
  1813. (looking-at "Para")))
  1814. ;; On an item or a footnote definition, move to the second element
  1815. ;; inside, if any.
  1816. (should
  1817. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1818. (org-forward-paragraph)
  1819. (looking-at " Paragraph")))
  1820. (should
  1821. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1822. (org-forward-paragraph)
  1823. (looking-at "Paragraph")))
  1824. ;; On an item, or a footnote definition, when the first line is
  1825. ;; empty, move to the first item.
  1826. (should
  1827. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1828. (org-forward-paragraph)
  1829. (looking-at " Paragraph")))
  1830. (should
  1831. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1832. (org-forward-paragraph)
  1833. (looking-at "Paragraph")))
  1834. ;; On a table (resp. a property drawer) do not move through table
  1835. ;; rows (resp. node properties).
  1836. (should
  1837. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1838. (org-forward-paragraph)
  1839. (looking-at "Paragraph")))
  1840. (should
  1841. (org-test-with-temp-text
  1842. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1843. (org-forward-paragraph)
  1844. (looking-at "Paragraph")))
  1845. ;; On a verse or source block, stop after blank lines.
  1846. (should
  1847. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1848. (org-forward-paragraph)
  1849. (looking-at "L2")))
  1850. (should
  1851. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1852. (org-forward-paragraph)
  1853. (looking-at "L2"))))
  1854. (ert-deftest test-org/backward-paragraph ()
  1855. "Test `org-backward-paragraph' specifications."
  1856. ;; Error at beginning of buffer.
  1857. (should-error
  1858. (org-test-with-temp-text "Paragraph"
  1859. (org-backward-paragraph)))
  1860. ;; Regular test.
  1861. (should
  1862. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1863. (goto-char (point-max))
  1864. (org-backward-paragraph)
  1865. (looking-at "P3")))
  1866. (should
  1867. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1868. (goto-char (point-max))
  1869. (beginning-of-line)
  1870. (org-backward-paragraph)
  1871. (looking-at "P2")))
  1872. ;; Ignore depth.
  1873. (should
  1874. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1875. (goto-char (point-max))
  1876. (beginning-of-line)
  1877. (org-backward-paragraph)
  1878. (looking-at "P2")))
  1879. ;; Ignore invisible elements.
  1880. (should
  1881. (org-test-with-temp-text "* H1\n P1\n* H2"
  1882. (org-cycle)
  1883. (goto-char (point-max))
  1884. (beginning-of-line)
  1885. (org-backward-paragraph)
  1886. (bobp)))
  1887. ;; On an affiliated keyword, jump to the first one.
  1888. (should
  1889. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1890. (search-forward "c2")
  1891. (org-backward-paragraph)
  1892. (looking-at "#\\+name")))
  1893. ;; On the second element in an item or a footnote definition, jump
  1894. ;; to item or the definition.
  1895. (should
  1896. (org-test-with-temp-text "- line1\n\n line2"
  1897. (goto-char (point-max))
  1898. (beginning-of-line)
  1899. (org-backward-paragraph)
  1900. (looking-at "- line1")))
  1901. (should
  1902. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1903. (goto-char (point-max))
  1904. (beginning-of-line)
  1905. (org-backward-paragraph)
  1906. (looking-at "\\[fn:1\\] line1")))
  1907. ;; On a table (resp. a property drawer), ignore table rows
  1908. ;; (resp. node properties).
  1909. (should
  1910. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1911. (goto-char (point-max))
  1912. (beginning-of-line)
  1913. (org-backward-paragraph)
  1914. (bobp)))
  1915. (should
  1916. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  1917. (org-backward-paragraph)
  1918. (looking-at ":PROPERTIES:")))
  1919. ;; On a source or verse block, stop before blank lines.
  1920. (should
  1921. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1922. (search-forward "L3")
  1923. (beginning-of-line)
  1924. (org-backward-paragraph)
  1925. (looking-at "L2")))
  1926. (should
  1927. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1928. (search-forward "L3")
  1929. (beginning-of-line)
  1930. (org-backward-paragraph)
  1931. (looking-at "L2"))))
  1932. (ert-deftest test-org/forward-element ()
  1933. "Test `org-forward-element' specifications."
  1934. ;; 1. At EOB: should error.
  1935. (org-test-with-temp-text "Some text\n"
  1936. (goto-char (point-max))
  1937. (should-error (org-forward-element)))
  1938. ;; 2. Standard move: expected to ignore blank lines.
  1939. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1940. (org-forward-element)
  1941. (should (looking-at (regexp-quote "Second paragraph."))))
  1942. ;; 3. Headline tests.
  1943. (org-test-with-temp-text "
  1944. * Head 1
  1945. ** Head 1.1
  1946. *** Head 1.1.1
  1947. ** Head 1.2"
  1948. ;; 3.1. At an headline beginning: move to next headline at the
  1949. ;; same level.
  1950. (goto-line 3)
  1951. (org-forward-element)
  1952. (should (looking-at (regexp-quote "** Head 1.2")))
  1953. ;; 3.2. At an headline beginning: move to parent headline if no
  1954. ;; headline at the same level.
  1955. (goto-line 3)
  1956. (org-forward-element)
  1957. (should (looking-at (regexp-quote "** Head 1.2"))))
  1958. ;; 4. Greater element tests.
  1959. (org-test-with-temp-text
  1960. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1961. ;; 4.1. At a greater element: expected to skip contents.
  1962. (org-forward-element)
  1963. (should (looking-at (regexp-quote "Outside.")))
  1964. ;; 4.2. At the end of greater element contents: expected to skip
  1965. ;; to the end of the greater element.
  1966. (goto-line 2)
  1967. (org-forward-element)
  1968. (should (looking-at (regexp-quote "Outside."))))
  1969. ;; 5. List tests.
  1970. (org-test-with-temp-text "
  1971. - item1
  1972. - sub1
  1973. - sub2
  1974. - sub3
  1975. Inner paragraph.
  1976. - item2
  1977. Outside."
  1978. ;; 5.1. At list top point: expected to move to the element after
  1979. ;; the list.
  1980. (goto-line 2)
  1981. (org-forward-element)
  1982. (should (looking-at (regexp-quote "Outside.")))
  1983. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1984. ;; beginning of line, move to next item.
  1985. (goto-line 2)
  1986. (forward-char)
  1987. (org-forward-element)
  1988. (should (looking-at "- item2"))
  1989. (goto-line 4)
  1990. (forward-char)
  1991. (org-forward-element)
  1992. (should (looking-at " - sub2"))
  1993. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1994. (goto-line 4)
  1995. (org-forward-element)
  1996. (should (looking-at (regexp-quote " Inner paragraph.")))
  1997. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1998. (goto-line 8)
  1999. (org-forward-element)
  2000. (should (looking-at (regexp-quote " Inner paragraph.")))
  2001. ;; 5.5. At an item: expected to move to next item, if any.
  2002. (goto-line 6)
  2003. (org-forward-element)
  2004. (should (looking-at " - sub3"))))
  2005. (ert-deftest test-org/backward-element ()
  2006. "Test `org-backward-element' specifications."
  2007. ;; 1. Should error at BOB.
  2008. (org-test-with-temp-text " \nParagraph."
  2009. (should-error (org-backward-element)))
  2010. ;; 2. Should move at BOB when called on the first element in buffer.
  2011. (should
  2012. (org-test-with-temp-text "\n#+TITLE: test"
  2013. (progn (forward-line)
  2014. (org-backward-element)
  2015. (bobp))))
  2016. ;; 3. Not at the beginning of an element: move at its beginning.
  2017. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2018. (goto-line 3)
  2019. (end-of-line)
  2020. (org-backward-element)
  2021. (should (looking-at (regexp-quote "Paragraph2."))))
  2022. ;; 4. Headline tests.
  2023. (org-test-with-temp-text "
  2024. * Head 1
  2025. ** Head 1.1
  2026. *** Head 1.1.1
  2027. ** Head 1.2"
  2028. ;; 4.1. At an headline beginning: move to previous headline at the
  2029. ;; same level.
  2030. (goto-line 5)
  2031. (org-backward-element)
  2032. (should (looking-at (regexp-quote "** Head 1.1")))
  2033. ;; 4.2. At an headline beginning: move to parent headline if no
  2034. ;; headline at the same level.
  2035. (goto-line 3)
  2036. (org-backward-element)
  2037. (should (looking-at (regexp-quote "* Head 1")))
  2038. ;; 4.3. At the first top-level headline: should error.
  2039. (goto-line 2)
  2040. (should-error (org-backward-element)))
  2041. ;; 5. At beginning of first element inside a greater element:
  2042. ;; expected to move to greater element's beginning.
  2043. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  2044. (goto-line 3)
  2045. (org-backward-element)
  2046. (should (looking-at "#\\+BEGIN_CENTER")))
  2047. ;; 6. At the beginning of the first element in a section: should
  2048. ;; move back to headline, if any.
  2049. (should
  2050. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  2051. (progn (goto-char (point-max))
  2052. (beginning-of-line)
  2053. (org-backward-element)
  2054. (org-at-heading-p))))
  2055. ;; 7. List tests.
  2056. (org-test-with-temp-text "
  2057. - item1
  2058. - sub1
  2059. - sub2
  2060. - sub3
  2061. Inner paragraph.
  2062. - item2
  2063. Outside."
  2064. ;; 7.1. At beginning of sub-list: expected to move to the
  2065. ;; paragraph before it.
  2066. (goto-line 4)
  2067. (org-backward-element)
  2068. (should (looking-at "item1"))
  2069. ;; 7.2. At an item in a list: expected to move at previous item.
  2070. (goto-line 8)
  2071. (org-backward-element)
  2072. (should (looking-at " - sub2"))
  2073. (goto-line 12)
  2074. (org-backward-element)
  2075. (should (looking-at "- item1"))
  2076. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  2077. ;; beginning.
  2078. (goto-line 10)
  2079. (org-backward-element)
  2080. (should (looking-at " - sub1"))
  2081. (goto-line 15)
  2082. (org-backward-element)
  2083. (should (looking-at "- item1"))
  2084. ;; 7.4. At blank-lines before list end: expected to move to top
  2085. ;; item.
  2086. (goto-line 14)
  2087. (org-backward-element)
  2088. (should (looking-at "- item1"))))
  2089. (ert-deftest test-org/up-element ()
  2090. "Test `org-up-element' specifications."
  2091. ;; 1. At BOB or with no surrounding element: should error.
  2092. (org-test-with-temp-text "Paragraph."
  2093. (should-error (org-up-element)))
  2094. (org-test-with-temp-text "* Head1\n* Head2"
  2095. (goto-line 2)
  2096. (should-error (org-up-element)))
  2097. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2098. (goto-line 3)
  2099. (should-error (org-up-element)))
  2100. ;; 2. At an headline: move to parent headline.
  2101. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  2102. (goto-line 3)
  2103. (org-up-element)
  2104. (should (looking-at "\\* Head1")))
  2105. ;; 3. Inside a greater element: move to greater element beginning.
  2106. (org-test-with-temp-text
  2107. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  2108. (goto-line 3)
  2109. (org-up-element)
  2110. (should (looking-at "#\\+BEGIN_CENTER")))
  2111. ;; 4. List tests.
  2112. (org-test-with-temp-text "* Top
  2113. - item1
  2114. - sub1
  2115. - sub2
  2116. Paragraph within sub2.
  2117. - item2"
  2118. ;; 4.1. Within an item: move to the item beginning.
  2119. (goto-line 8)
  2120. (org-up-element)
  2121. (should (looking-at " - sub2"))
  2122. ;; 4.2. At an item in a sub-list: move to parent item.
  2123. (goto-line 4)
  2124. (org-up-element)
  2125. (should (looking-at "- item1"))
  2126. ;; 4.3. At an item in top list: move to beginning of whole list.
  2127. (goto-line 10)
  2128. (org-up-element)
  2129. (should (looking-at "- item1"))
  2130. ;; 4.4. Special case. At very top point: should move to parent of
  2131. ;; list.
  2132. (goto-line 2)
  2133. (org-up-element)
  2134. (should (looking-at "\\* Top"))))
  2135. (ert-deftest test-org/down-element ()
  2136. "Test `org-down-element' specifications."
  2137. ;; Error when the element hasn't got a recursive type.
  2138. (org-test-with-temp-text "Paragraph."
  2139. (should-error (org-down-element)))
  2140. ;; Error when the element has no contents
  2141. (org-test-with-temp-text "* Headline"
  2142. (should-error (org-down-element)))
  2143. ;; When at a plain-list, move to first item.
  2144. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  2145. (goto-line 2)
  2146. (org-down-element)
  2147. (should (looking-at " - Item 1.1")))
  2148. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  2149. (org-down-element)
  2150. (should (looking-at " Item 1")))
  2151. ;; When at a table, move to first row
  2152. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  2153. (org-down-element)
  2154. (should (looking-at " a | b |")))
  2155. ;; Otherwise, move inside the greater element.
  2156. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  2157. (org-down-element)
  2158. (should (looking-at "Paragraph"))))
  2159. (ert-deftest test-org/drag-element-backward ()
  2160. "Test `org-drag-element-backward' specifications."
  2161. ;; Standard test.
  2162. (should
  2163. (equal
  2164. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  2165. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  2166. (org-drag-element-backward)
  2167. (buffer-string))))
  2168. (should
  2169. (equal
  2170. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  2171. (org-test-with-temp-text
  2172. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  2173. (org-drag-element-backward)
  2174. (buffer-string))))
  2175. ;; Preserve blank lines.
  2176. (should
  2177. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  2178. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  2179. (org-drag-element-backward)
  2180. (buffer-string))))
  2181. ;; Preserve column.
  2182. (should
  2183. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  2184. (org-drag-element-backward)
  2185. (org-looking-at-p "2")))
  2186. ;; Error when trying to move first element of buffer.
  2187. (should-error
  2188. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2189. (org-drag-element-backward)))
  2190. ;; Error when trying to swap nested elements.
  2191. (should-error
  2192. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2193. (forward-line)
  2194. (org-drag-element-backward)))
  2195. ;; Error when trying to swap an headline element and a non-headline
  2196. ;; element.
  2197. (should-error
  2198. (org-test-with-temp-text "Test.\n* Head 1"
  2199. (forward-line)
  2200. (org-drag-element-backward)))
  2201. ;; Preserve visibility of elements and their contents.
  2202. (should
  2203. (equal '((63 . 82) (26 . 48))
  2204. (org-test-with-temp-text "
  2205. #+BEGIN_CENTER
  2206. Text.
  2207. #+END_CENTER
  2208. - item 1
  2209. #+BEGIN_QUOTE
  2210. Text.
  2211. #+END_QUOTE"
  2212. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2213. (search-backward "- item 1")
  2214. (org-drag-element-backward)
  2215. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2216. (overlays-in (point-min) (point-max)))))))
  2217. (ert-deftest test-org/drag-element-forward ()
  2218. "Test `org-drag-element-forward' specifications."
  2219. ;; 1. Error when trying to move first element of buffer.
  2220. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2221. (goto-line 3)
  2222. (should-error (org-drag-element-forward)))
  2223. ;; 2. Error when trying to swap nested elements.
  2224. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2225. (forward-line)
  2226. (should-error (org-drag-element-forward)))
  2227. ;; 3. Error when trying to swap a non-headline element and an
  2228. ;; headline.
  2229. (org-test-with-temp-text "Test.\n* Head 1"
  2230. (should-error (org-drag-element-forward)))
  2231. ;; 4. Otherwise, swap elements, preserving column and blank lines
  2232. ;; between elements.
  2233. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  2234. (search-forward "graph")
  2235. (org-drag-element-forward)
  2236. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  2237. (should (looking-at " 1")))
  2238. ;; 5. Preserve visibility of elements and their contents.
  2239. (org-test-with-temp-text "
  2240. #+BEGIN_CENTER
  2241. Text.
  2242. #+END_CENTER
  2243. - item 1
  2244. #+BEGIN_QUOTE
  2245. Text.
  2246. #+END_QUOTE"
  2247. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2248. (search-backward "#+BEGIN_CENTER")
  2249. (org-drag-element-forward)
  2250. (should
  2251. (equal
  2252. '((63 . 82) (26 . 48))
  2253. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2254. (overlays-in (point-min) (point-max)))))))
  2255. (ert-deftest test-org/next-block ()
  2256. "Test `org-next-block' specifications."
  2257. ;; Regular test.
  2258. (should
  2259. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2260. (org-next-block 1)
  2261. (looking-at "#\\+BEGIN_CENTER")))
  2262. ;; Ignore case.
  2263. (should
  2264. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  2265. (let ((case-fold-search nil))
  2266. (org-next-block 1)
  2267. (looking-at "#\\+begin_center"))))
  2268. ;; Ignore current line.
  2269. (should
  2270. (org-test-with-temp-text
  2271. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  2272. (org-next-block 1)
  2273. (looking-at "#\\+BEGIN_CENTER")))
  2274. ;; Throw an error when no block is found.
  2275. (should-error
  2276. (org-test-with-temp-text "Paragraph"
  2277. (org-next-block 1)))
  2278. ;; With an argument, skip many blocks at once.
  2279. (should
  2280. (org-test-with-temp-text
  2281. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  2282. (org-next-block 2)
  2283. (looking-at "#\\+BEGIN_QUOTE")))
  2284. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  2285. (should
  2286. (org-test-with-temp-text
  2287. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  2288. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  2289. (looking-at "#\\+BEGIN_QUOTE")))
  2290. ;; Optional argument is also case-insensitive.
  2291. (should
  2292. (org-test-with-temp-text
  2293. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  2294. (let ((case-fold-search nil))
  2295. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  2296. (looking-at "#\\+begin_quote")))))
  2297. (ert-deftest test-org/previous-block ()
  2298. "Test `org-previous-block' specifications."
  2299. ;; Regular test.
  2300. (should
  2301. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  2302. (org-previous-block 1)
  2303. (looking-at "#\\+BEGIN_CENTER")))
  2304. ;; Ignore case.
  2305. (should
  2306. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  2307. (let ((case-fold-search nil))
  2308. (org-previous-block 1)
  2309. (looking-at "#\\+begin_center"))))
  2310. ;; Ignore current line.
  2311. (should
  2312. (org-test-with-temp-text
  2313. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  2314. (org-previous-block 1)
  2315. (looking-at "#\\+BEGIN_QUOTE")))
  2316. ;; Throw an error when no block is found.
  2317. (should-error
  2318. (org-test-with-temp-text "Paragraph<point>"
  2319. (org-previous-block 1)))
  2320. ;; With an argument, skip many blocks at once.
  2321. (should
  2322. (org-test-with-temp-text
  2323. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  2324. (org-previous-block 2)
  2325. (looking-at "#\\+BEGIN_CENTER")))
  2326. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  2327. (should
  2328. (org-test-with-temp-text
  2329. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  2330. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  2331. (looking-at "#\\+BEGIN_QUOTE")))
  2332. ;; Optional argument is also case-insensitive.
  2333. (should
  2334. (org-test-with-temp-text
  2335. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  2336. (let ((case-fold-search nil))
  2337. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  2338. (looking-at "#\\+begin_quote")))))
  2339. ;;; Outline structure
  2340. (ert-deftest test-org/demote ()
  2341. "Test `org-demote' specifications."
  2342. ;; Add correct number of stars according to `org-odd-levels-only'.
  2343. (should
  2344. (= 2
  2345. (org-test-with-temp-text "* H"
  2346. (let ((org-odd-levels-only nil)) (org-demote))
  2347. (org-current-level))))
  2348. (should
  2349. (= 3
  2350. (org-test-with-temp-text "* H"
  2351. (let ((org-odd-levels-only t)) (org-demote))
  2352. (org-current-level))))
  2353. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  2354. (should
  2355. (org-test-with-temp-text "* H :tag:"
  2356. (let ((org-tags-column 10)
  2357. (org-auto-align-tags t)
  2358. (org-odd-levels-only nil))
  2359. (org-demote))
  2360. (org-move-to-column 10)
  2361. (org-looking-at-p ":tag:$")))
  2362. (should-not
  2363. (org-test-with-temp-text "* H :tag:"
  2364. (let ((org-tags-column 10)
  2365. (org-auto-align-tags nil)
  2366. (org-odd-levels-only nil))
  2367. (org-demote))
  2368. (org-move-to-column 10)
  2369. (org-looking-at-p ":tag:$")))
  2370. ;; When `org-adapt-indentation' is non-nil, always indent planning
  2371. ;; info and property drawers accordingly.
  2372. (should
  2373. (= 3
  2374. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  2375. (let ((org-odd-levels-only nil)
  2376. (org-adapt-indentation t))
  2377. (org-demote))
  2378. (forward-line)
  2379. (org-get-indentation))))
  2380. (should
  2381. (= 3
  2382. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2383. (let ((org-odd-levels-only nil)
  2384. (org-adapt-indentation t))
  2385. (org-demote))
  2386. (forward-line)
  2387. (org-get-indentation))))
  2388. (should-not
  2389. (= 3
  2390. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  2391. (let ((org-odd-levels-only nil)
  2392. (org-adapt-indentation nil))
  2393. (org-demote))
  2394. (forward-line)
  2395. (org-get-indentation))))
  2396. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2397. ;; section accordingly. Ignore, however, footnote definitions and
  2398. ;; inlinetasks boundaries.
  2399. (should
  2400. (= 3
  2401. (org-test-with-temp-text "* H\n Paragraph"
  2402. (let ((org-odd-levels-only nil)
  2403. (org-adapt-indentation t))
  2404. (org-demote))
  2405. (forward-line)
  2406. (org-get-indentation))))
  2407. (should
  2408. (= 2
  2409. (org-test-with-temp-text "* H\n Paragraph"
  2410. (let ((org-odd-levels-only nil)
  2411. (org-adapt-indentation nil))
  2412. (org-demote))
  2413. (forward-line)
  2414. (org-get-indentation))))
  2415. (should
  2416. (zerop
  2417. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  2418. (let ((org-odd-levels-only nil)
  2419. (org-adapt-indentation t))
  2420. (org-demote))
  2421. (goto-char (point-max))
  2422. (org-get-indentation))))
  2423. (should
  2424. (= 3
  2425. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  2426. (let ((org-odd-levels-only nil)
  2427. (org-adapt-indentation t))
  2428. (org-demote))
  2429. (goto-char (point-max))
  2430. (org-get-indentation))))
  2431. (when (featurep 'org-inlinetask)
  2432. (should
  2433. (zerop
  2434. (let ((org-inlinetask-min-level 5)
  2435. (org-adapt-indentation t))
  2436. (org-test-with-temp-text "* H\n***** I\n***** END"
  2437. (org-demote)
  2438. (forward-line)
  2439. (org-get-indentation))))))
  2440. (when (featurep 'org-inlinetask)
  2441. (should
  2442. (= 3
  2443. (let ((org-inlinetask-min-level 5)
  2444. (org-adapt-indentation t))
  2445. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  2446. (org-demote)
  2447. (forward-line 2)
  2448. (org-get-indentation))))))
  2449. ;; Ignore contents of source blocks or example blocks when
  2450. ;; indentation should be preserved (through
  2451. ;; `org-src-preserve-indentation' or "-i" flag).
  2452. (should-not
  2453. (zerop
  2454. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2455. (let ((org-adapt-indentation t)
  2456. (org-src-preserve-indentation nil))
  2457. (org-demote))
  2458. (forward-line 2)
  2459. (org-get-indentation))))
  2460. (should
  2461. (zerop
  2462. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2463. (let ((org-adapt-indentation t)
  2464. (org-src-preserve-indentation t))
  2465. (org-demote))
  2466. (forward-line 2)
  2467. (org-get-indentation))))
  2468. (should
  2469. (zerop
  2470. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2471. (let ((org-adapt-indentation t)
  2472. (org-src-preserve-indentation t))
  2473. (org-demote))
  2474. (forward-line 2)
  2475. (org-get-indentation))))
  2476. (should
  2477. (zerop
  2478. (org-test-with-temp-text
  2479. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  2480. (let ((org-adapt-indentation t)
  2481. (org-src-preserve-indentation nil))
  2482. (org-demote))
  2483. (forward-line 2)
  2484. (org-get-indentation)))))
  2485. (ert-deftest test-org/promote ()
  2486. "Test `org-promote' specifications."
  2487. ;; Return an error if headline is to be promoted to level 0, unless
  2488. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  2489. ;; headline becomes a comment.
  2490. (should-error
  2491. (org-test-with-temp-text "* H"
  2492. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  2493. (should
  2494. (equal "# H"
  2495. (org-test-with-temp-text "* H"
  2496. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  2497. (buffer-string))))
  2498. ;; Remove correct number of stars according to
  2499. ;; `org-odd-levels-only'.
  2500. (should
  2501. (= 2
  2502. (org-test-with-temp-text "*** H"
  2503. (let ((org-odd-levels-only nil)) (org-promote))
  2504. (org-current-level))))
  2505. (should
  2506. (= 1
  2507. (org-test-with-temp-text "*** H"
  2508. (let ((org-odd-levels-only t)) (org-promote))
  2509. (org-current-level))))
  2510. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  2511. (should
  2512. (org-test-with-temp-text "** H :tag:"
  2513. (let ((org-tags-column 10)
  2514. (org-auto-align-tags t)
  2515. (org-odd-levels-only nil))
  2516. (org-promote))
  2517. (org-move-to-column 10)
  2518. (org-looking-at-p ":tag:$")))
  2519. (should-not
  2520. (org-test-with-temp-text "** H :tag:"
  2521. (let ((org-tags-column 10)
  2522. (org-auto-align-tags nil)
  2523. (org-odd-levels-only nil))
  2524. (org-promote))
  2525. (org-move-to-column 10)
  2526. (org-looking-at-p ":tag:$")))
  2527. ;; When `org-adapt-indentation' is non-nil, always indent planning
  2528. ;; info and property drawers.
  2529. (should
  2530. (= 2
  2531. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2532. (let ((org-odd-levels-only nil)
  2533. (org-adapt-indentation t))
  2534. (org-promote))
  2535. (forward-line)
  2536. (org-get-indentation))))
  2537. (should
  2538. (= 2
  2539. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2540. (let ((org-odd-levels-only nil)
  2541. (org-adapt-indentation t))
  2542. (org-promote))
  2543. (forward-line)
  2544. (org-get-indentation))))
  2545. (should-not
  2546. (= 2
  2547. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2548. (let ((org-odd-levels-only nil)
  2549. (org-adapt-indentation nil))
  2550. (org-promote))
  2551. (forward-line)
  2552. (org-get-indentation))))
  2553. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2554. ;; section accordingly. Ignore, however, footnote definitions and
  2555. ;; inlinetasks boundaries.
  2556. (should
  2557. (= 2
  2558. (org-test-with-temp-text "** H\n Paragraph"
  2559. (let ((org-odd-levels-only nil)
  2560. (org-adapt-indentation t))
  2561. (org-promote))
  2562. (forward-line)
  2563. (org-get-indentation))))
  2564. (should-not
  2565. (= 2
  2566. (org-test-with-temp-text "** H\n Paragraph"
  2567. (let ((org-odd-levels-only nil)
  2568. (org-adapt-indentation nil))
  2569. (org-promote))
  2570. (forward-line)
  2571. (org-get-indentation))))
  2572. (should
  2573. (= 2
  2574. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  2575. (let ((org-odd-levels-only nil)
  2576. (org-adapt-indentation t))
  2577. (org-promote))
  2578. (forward-line)
  2579. (org-get-indentation))))
  2580. (when (featurep 'org-inlinetask)
  2581. (should
  2582. (zerop
  2583. (let ((org-inlinetask-min-level 5)
  2584. (org-adapt-indentation t))
  2585. (org-test-with-temp-text "** H\n***** I\n***** END"
  2586. (org-promote)
  2587. (forward-line)
  2588. (org-get-indentation))))))
  2589. (when (featurep 'org-inlinetask)
  2590. (should
  2591. (= 2
  2592. (let ((org-inlinetask-min-level 5)
  2593. (org-adapt-indentation t))
  2594. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  2595. (org-promote)
  2596. (forward-line 2)
  2597. (org-get-indentation))))))
  2598. ;; Give up shifting if it would break document's structure
  2599. ;; otherwise.
  2600. (should
  2601. (= 3
  2602. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  2603. (let ((org-odd-levels-only nil)
  2604. (org-adapt-indentation t))
  2605. (org-promote))
  2606. (forward-line)
  2607. (org-get-indentation))))
  2608. (should
  2609. (= 3
  2610. (org-test-with-temp-text "** H\n Paragraph\n * list."
  2611. (let ((org-odd-levels-only nil)
  2612. (org-adapt-indentation t))
  2613. (org-promote))
  2614. (forward-line)
  2615. (org-get-indentation))))
  2616. ;; Ignore contents of source blocks or example blocks when
  2617. ;; indentation should be preserved (through
  2618. ;; `org-src-preserve-indentation' or "-i" flag).
  2619. (should-not
  2620. (zerop
  2621. (org-test-with-temp-text
  2622. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2623. (let ((org-adapt-indentation t)
  2624. (org-src-preserve-indentation nil)
  2625. (org-odd-levels-only nil))
  2626. (org-promote))
  2627. (forward-line)
  2628. (org-get-indentation))))
  2629. (should
  2630. (zerop
  2631. (org-test-with-temp-text
  2632. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  2633. (let ((org-adapt-indentation t)
  2634. (org-src-preserve-indentation t)
  2635. (org-odd-levels-only nil))
  2636. (org-promote))
  2637. (forward-line)
  2638. (org-get-indentation))))
  2639. (should
  2640. (zerop
  2641. (org-test-with-temp-text
  2642. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2643. (let ((org-adapt-indentation t)
  2644. (org-src-preserve-indentation t)
  2645. (org-odd-levels-only nil))
  2646. (org-promote))
  2647. (forward-line)
  2648. (org-get-indentation))))
  2649. (should
  2650. (zerop
  2651. (org-test-with-temp-text
  2652. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  2653. (let ((org-adapt-indentation t)
  2654. (org-src-preserve-indentation nil)
  2655. (org-odd-levels-only nil))
  2656. (org-promote))
  2657. (forward-line)
  2658. (org-get-indentation)))))
  2659. ;;; Planning
  2660. (ert-deftest test-org/at-planning-p ()
  2661. "Test `org-at-planning-p' specifications."
  2662. ;; Regular test.
  2663. (should
  2664. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  2665. (org-at-planning-p)))
  2666. (should-not
  2667. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  2668. (org-at-planning-p)))
  2669. ;; Correctly find planning attached to inlinetasks.
  2670. (when (featurep 'org-inlinetask)
  2671. (should
  2672. (org-test-with-temp-text
  2673. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  2674. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2675. (should-not
  2676. (org-test-with-temp-text
  2677. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  2678. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2679. (should-not
  2680. (org-test-with-temp-text
  2681. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  2682. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2683. (should-not
  2684. (org-test-with-temp-text
  2685. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  2686. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  2687. ;;; Property API
  2688. (ert-deftest test-org/buffer-property-keys ()
  2689. "Test `org-buffer-property-keys' specifications."
  2690. ;; Retrieve properties accross siblings.
  2691. (should
  2692. (equal '("A" "B")
  2693. (org-test-with-temp-text "
  2694. * H1
  2695. :PROPERTIES:
  2696. :A: 1
  2697. :END:
  2698. * H2
  2699. :PROPERTIES:
  2700. :B: 1
  2701. :END:"
  2702. (org-buffer-property-keys))))
  2703. ;; Retrieve properties accross children.
  2704. (should
  2705. (equal '("A" "B")
  2706. (org-test-with-temp-text "
  2707. * H1
  2708. :PROPERTIES:
  2709. :A: 1
  2710. :END:
  2711. ** H2
  2712. :PROPERTIES:
  2713. :B: 1
  2714. :END:"
  2715. (org-buffer-property-keys))))
  2716. ;; Retrieve muliple properties in the same drawer.
  2717. (should
  2718. (equal '("A" "B")
  2719. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2720. (org-buffer-property-keys))))
  2721. ;; Ignore extension symbol in property name.
  2722. (should
  2723. (equal '("A")
  2724. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  2725. (org-buffer-property-keys))))
  2726. ;; With non-nil COLUMNS, extract property names from columns.
  2727. (should
  2728. (equal '("A" "B")
  2729. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  2730. (org-buffer-property-keys nil nil t))))
  2731. (should
  2732. (equal '("A" "B" "COLUMNS")
  2733. (org-test-with-temp-text
  2734. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  2735. (org-buffer-property-keys nil nil t)))))
  2736. (ert-deftest test-org/property-values ()
  2737. "Test `org-property-values' specifications."
  2738. ;; Regular test.
  2739. (should
  2740. (equal '("2" "1")
  2741. (org-test-with-temp-text
  2742. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  2743. (org-property-values "A"))))
  2744. ;; Ignore empty values.
  2745. (should-not
  2746. (org-test-with-temp-text
  2747. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  2748. (org-property-values "A")))
  2749. ;; Take into consideration extended values.
  2750. (should
  2751. (equal '("1 2")
  2752. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  2753. (org-property-values "A")))))
  2754. (ert-deftest test-org/find-property ()
  2755. "Test `org-find-property' specifications."
  2756. ;; Regular test.
  2757. (should
  2758. (= 1
  2759. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  2760. (org-find-property "prop"))))
  2761. ;; Ignore false positives.
  2762. (should
  2763. (= 27
  2764. (org-test-with-temp-text
  2765. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  2766. (org-find-property "A"))))
  2767. ;; Return first entry found in buffer.
  2768. (should
  2769. (= 1
  2770. (org-test-with-temp-text
  2771. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  2772. (org-find-property "A"))))
  2773. ;; Only search visible part of the buffer.
  2774. (should
  2775. (= 31
  2776. (org-test-with-temp-text
  2777. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  2778. (org-narrow-to-subtree)
  2779. (org-find-property "A"))))
  2780. ;; With optional argument, only find entries with a specific value.
  2781. (should-not
  2782. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2783. (org-find-property "A" "2")))
  2784. (should
  2785. (= 31
  2786. (org-test-with-temp-text
  2787. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  2788. (org-find-property "A" "2"))))
  2789. ;; Use "nil" for explicit nil values.
  2790. (should
  2791. (= 31
  2792. (org-test-with-temp-text
  2793. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  2794. (org-find-property "A" "nil")))))
  2795. (ert-deftest test-org/entry-delete ()
  2796. "Test `org-entry-delete' specifications."
  2797. ;; Regular test.
  2798. (should
  2799. (string-match
  2800. " *:PROPERTIES:\n *:B: +2\n *:END:"
  2801. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2802. (org-entry-delete (point) "A")
  2803. (buffer-string))))
  2804. ;; Also remove accumulated properties.
  2805. (should-not
  2806. (string-match
  2807. ":A"
  2808. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  2809. (org-entry-delete (point) "A")
  2810. (buffer-string))))
  2811. ;; When last property is removed, remove the property drawer.
  2812. (should-not
  2813. (string-match
  2814. ":PROPERTIES:"
  2815. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  2816. (org-entry-delete (point) "A")
  2817. (buffer-string))))
  2818. ;; Return a non-nil value when some property was removed.
  2819. (should
  2820. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2821. (org-entry-delete (point) "A")))
  2822. (should-not
  2823. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2824. (org-entry-delete (point) "C"))))
  2825. (ert-deftest test-org/entry-get ()
  2826. "Test `org-entry-get' specifications."
  2827. ;; Regular test.
  2828. (should
  2829. (equal "1"
  2830. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2831. (org-entry-get (point) "A"))))
  2832. ;; Ignore case.
  2833. (should
  2834. (equal "1"
  2835. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2836. (org-entry-get (point) "a"))))
  2837. ;; Handle extended values, both before and after base value.
  2838. (should
  2839. (equal "1 2 3"
  2840. (org-test-with-temp-text
  2841. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2842. (org-entry-get (point) "A"))))
  2843. ;; Empty values are returned as the empty string.
  2844. (should
  2845. (equal ""
  2846. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  2847. (org-entry-get (point) "A"))))
  2848. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  2849. ;; otherwise, return nil.
  2850. (should-not
  2851. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2852. (org-entry-get (point) "A")))
  2853. (should
  2854. (equal "nil"
  2855. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2856. (org-entry-get (point) "A" nil t))))
  2857. ;; Return nil when no property is found, independently on the
  2858. ;; LITERAL-NIL argument.
  2859. (should-not
  2860. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2861. (org-entry-get (point) "B")))
  2862. (should-not
  2863. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2864. (org-entry-get (point) "B" nil t)))
  2865. ;; Handle inheritance, when allowed.
  2866. (should
  2867. (equal
  2868. "1"
  2869. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2870. (org-entry-get (point) "A" t))))
  2871. (should
  2872. (equal
  2873. "1"
  2874. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2875. (let ((org-use-property-inheritance t))
  2876. (org-entry-get (point) "A" 'selective)))))
  2877. (should-not
  2878. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2879. (let ((org-use-property-inheritance nil))
  2880. (org-entry-get (point) "A" 'selective)))))
  2881. (ert-deftest test-org/entry-properties ()
  2882. "Test `org-entry-properties' specifications."
  2883. ;; Get "ITEM" property.
  2884. (should
  2885. (equal "* H"
  2886. (org-test-with-temp-text "* TODO H"
  2887. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  2888. (should
  2889. (equal "* H"
  2890. (org-test-with-temp-text "* TODO H"
  2891. (cdr (assoc "ITEM" (org-entry-properties))))))
  2892. ;; Get "TODO" property.
  2893. (should
  2894. (equal "TODO"
  2895. (org-test-with-temp-text "* TODO H"
  2896. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  2897. (should
  2898. (equal "TODO"
  2899. (org-test-with-temp-text "* TODO H"
  2900. (cdr (assoc "TODO" (org-entry-properties))))))
  2901. (should-not
  2902. (org-test-with-temp-text "* H"
  2903. (assoc "TODO" (org-entry-properties nil "TODO"))))
  2904. ;; Get "PRIORITY" property.
  2905. (should
  2906. (equal "A"
  2907. (org-test-with-temp-text "* [#A] H"
  2908. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  2909. (should
  2910. (equal "A"
  2911. (org-test-with-temp-text "* [#A] H"
  2912. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  2913. (should-not
  2914. (org-test-with-temp-text "* H"
  2915. (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
  2916. ;; Get "FILE" property.
  2917. (should
  2918. (org-test-with-temp-text-in-file "* H\nParagraph"
  2919. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  2920. (buffer-file-name))))
  2921. (should
  2922. (org-test-with-temp-text-in-file "* H\nParagraph"
  2923. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  2924. (buffer-file-name))))
  2925. (should-not
  2926. (org-test-with-temp-text "* H\nParagraph"
  2927. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  2928. ;; Get "TAGS" property.
  2929. (should
  2930. (equal ":tag1:tag2:"
  2931. (org-test-with-temp-text "* H :tag1:tag2:"
  2932. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  2933. (should
  2934. (equal ":tag1:tag2:"
  2935. (org-test-with-temp-text "* H :tag1:tag2:"
  2936. (cdr (assoc "TAGS" (org-entry-properties))))))
  2937. (should-not
  2938. (org-test-with-temp-text "* H"
  2939. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  2940. ;; Get "ALLTAGS" property.
  2941. (should
  2942. (equal ":tag1:tag2:"
  2943. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  2944. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  2945. (should
  2946. (equal ":tag1:tag2:"
  2947. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  2948. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  2949. (should-not
  2950. (org-test-with-temp-text "* H"
  2951. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  2952. ;; Get "BLOCKED" property.
  2953. (should
  2954. (equal "t"
  2955. (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
  2956. (let ((org-enforce-todo-dependencies t)
  2957. (org-blocker-hook
  2958. '(org-block-todo-from-children-or-siblings-or-parent)))
  2959. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2960. (should
  2961. (equal "t"
  2962. (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
  2963. (let ((org-enforce-todo-dependencies t)
  2964. (org-blocker-hook
  2965. '(org-block-todo-from-children-or-siblings-or-parent)))
  2966. (cdr (assoc "BLOCKED" (org-entry-properties)))))))
  2967. (should
  2968. (equal ""
  2969. (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
  2970. (let ((org-enforce-todo-dependencies t))
  2971. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2972. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  2973. (should
  2974. (equal
  2975. "[2012-03-29 thu.]"
  2976. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  2977. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  2978. (should
  2979. (equal
  2980. "[2012-03-29 thu.]"
  2981. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  2982. (cdr (assoc "CLOSED" (org-entry-properties))))))
  2983. (should-not
  2984. (org-test-with-temp-text "* H"
  2985. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  2986. (should
  2987. (equal
  2988. "<2014-03-04 tue.>"
  2989. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2990. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  2991. (should
  2992. (equal
  2993. "<2014-03-04 tue.>"
  2994. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2995. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  2996. (should-not
  2997. (org-test-with-temp-text "* H"
  2998. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  2999. (should
  3000. (equal
  3001. "<2014-03-04 tue.>"
  3002. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3003. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  3004. (should
  3005. (equal
  3006. "<2014-03-04 tue.>"
  3007. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3008. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  3009. (should-not
  3010. (org-test-with-temp-text "* H"
  3011. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  3012. ;; Get "CATEGORY"
  3013. (should
  3014. (equal "cat"
  3015. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  3016. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  3017. (should
  3018. (equal "cat"
  3019. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  3020. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3021. (should
  3022. (equal "cat"
  3023. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  3024. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3025. (should
  3026. (equal "cat2"
  3027. (org-test-with-temp-text
  3028. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  3029. "\n"
  3030. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  3031. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3032. ;; Get standard properties.
  3033. (should
  3034. (equal "1"
  3035. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3036. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3037. ;; Handle extended properties.
  3038. (should
  3039. (equal "1 2 3"
  3040. (org-test-with-temp-text
  3041. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3042. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3043. (should
  3044. (equal "1 2 3"
  3045. (org-test-with-temp-text
  3046. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  3047. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3048. ;; Ignore forbidden (special) properties.
  3049. (should-not
  3050. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  3051. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  3052. (ert-deftest test-org/entry-put ()
  3053. "Test `org-entry-put' specifications."
  3054. ;; Error when not a string or nil.
  3055. (should-error
  3056. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  3057. (org-entry-put 1 "test" 2)))
  3058. ;; Set "TODO" property.
  3059. (should
  3060. (string-match (regexp-quote " TODO H")
  3061. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  3062. (org-entry-put (point) "TODO" "TODO")
  3063. (buffer-string))))
  3064. (should
  3065. (string-match (regexp-quote "* H")
  3066. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  3067. (org-entry-put (point) "TODO" nil)
  3068. (buffer-string))))
  3069. ;; Set "PRIORITY" property.
  3070. (should
  3071. (equal "* [#A] H"
  3072. (org-test-with-temp-text "* [#B] H"
  3073. (org-entry-put (point) "PRIORITY" "A")
  3074. (buffer-string))))
  3075. (should
  3076. (equal "* H"
  3077. (org-test-with-temp-text "* [#B] H"
  3078. (org-entry-put (point) "PRIORITY" nil)
  3079. (buffer-string))))
  3080. ;; Set "SCHEDULED" property.
  3081. (should
  3082. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  3083. (org-test-with-temp-text "* H"
  3084. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  3085. (buffer-string))))
  3086. (should
  3087. (string= "* H\n"
  3088. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3089. (org-entry-put (point) "SCHEDULED" nil)
  3090. (buffer-string))))
  3091. (should
  3092. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  3093. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3094. (org-entry-put (point) "SCHEDULED" "earlier")
  3095. (buffer-string))))
  3096. (should
  3097. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  3098. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3099. (org-entry-put (point) "SCHEDULED" "later")
  3100. (buffer-string))))
  3101. ;; Set "DEADLINE" property.
  3102. (should
  3103. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  3104. (org-test-with-temp-text "* H"
  3105. (org-entry-put (point) "DEADLINE" "2014-03-04")
  3106. (buffer-string))))
  3107. (should
  3108. (string= "* H\n"
  3109. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3110. (org-entry-put (point) "DEADLINE" nil)
  3111. (buffer-string))))
  3112. (should
  3113. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  3114. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3115. (org-entry-put (point) "DEADLINE" "earlier")
  3116. (buffer-string))))
  3117. (should
  3118. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  3119. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3120. (org-entry-put (point) "DEADLINE" "later")
  3121. (buffer-string))))
  3122. ;; Set "CATEGORY" property
  3123. (should
  3124. (string-match "^ *:CATEGORY: cat"
  3125. (org-test-with-temp-text "* H"
  3126. (org-entry-put (point) "CATEGORY" "cat")
  3127. (buffer-string))))
  3128. ;; Regular properties, with or without pre-existing drawer.
  3129. (should
  3130. (string-match "^ *:A: +2$"
  3131. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3132. (org-entry-put (point) "A" "2")
  3133. (buffer-string))))
  3134. (should
  3135. (string-match "^ *:A: +1$"
  3136. (org-test-with-temp-text "* H"
  3137. (org-entry-put (point) "A" "1")
  3138. (buffer-string))))
  3139. ;; Special case: two consecutive headlines.
  3140. (should
  3141. (string-match "\\* A\n *:PROPERTIES:"
  3142. (org-test-with-temp-text "* A\n** B"
  3143. (org-entry-put (point) "A" "1")
  3144. (buffer-string)))))
  3145. ;;; Radio Targets
  3146. (ert-deftest test-org/update-radio-target-regexp ()
  3147. "Test `org-update-radio-target-regexp' specifications."
  3148. ;; Properly update cache with no previous radio target regexp.
  3149. (should
  3150. (eq 'link
  3151. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  3152. (save-excursion (goto-char (point-max)) (org-element-context))
  3153. (insert "<<<")
  3154. (search-forward "o")
  3155. (insert ">>>")
  3156. (org-update-radio-target-regexp)
  3157. (goto-char (point-max))
  3158. (org-element-type (org-element-context)))))
  3159. ;; Properly update cache with previous radio target regexp.
  3160. (should
  3161. (eq 'link
  3162. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  3163. (save-excursion (goto-char (point-max)) (org-element-context))
  3164. (insert "<<<")
  3165. (search-forward "o")
  3166. (insert ">>>")
  3167. (org-update-radio-target-regexp)
  3168. (search-backward "r")
  3169. (delete-char 5)
  3170. (insert "new")
  3171. (org-update-radio-target-regexp)
  3172. (goto-char (point-max))
  3173. (delete-region (line-beginning-position) (point))
  3174. (insert "new")
  3175. (org-element-type (org-element-context))))))
  3176. ;;; Sparse trees
  3177. (ert-deftest test-org/match-sparse-tree ()
  3178. "Test `org-match-sparse-tree' specifications."
  3179. ;; Match tags.
  3180. (should-not
  3181. (org-test-with-temp-text "* H\n** H1 :tag:"
  3182. (org-match-sparse-tree nil "tag")
  3183. (search-forward "H1")
  3184. (org-invisible-p2)))
  3185. (should
  3186. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  3187. (org-match-sparse-tree nil "tag")
  3188. (search-forward "H2")
  3189. (org-invisible-p2)))
  3190. ;; "-" operator for tags.
  3191. (should-not
  3192. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3193. (org-match-sparse-tree nil "tag1-tag2")
  3194. (search-forward "H1")
  3195. (org-invisible-p2)))
  3196. (should
  3197. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3198. (org-match-sparse-tree nil "tag1-tag2")
  3199. (search-forward "H2")
  3200. (org-invisible-p2)))
  3201. ;; "&" operator for tags.
  3202. (should
  3203. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3204. (org-match-sparse-tree nil "tag1&tag2")
  3205. (search-forward "H1")
  3206. (org-invisible-p2)))
  3207. (should-not
  3208. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3209. (org-match-sparse-tree nil "tag1&tag2")
  3210. (search-forward "H2")
  3211. (org-invisible-p2)))
  3212. ;; "|" operator for tags.
  3213. (should-not
  3214. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3215. (org-match-sparse-tree nil "tag1|tag2")
  3216. (search-forward "H1")
  3217. (org-invisible-p2)))
  3218. (should-not
  3219. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3220. (org-match-sparse-tree nil "tag1|tag2")
  3221. (search-forward "H2")
  3222. (org-invisible-p2)))
  3223. ;; Regexp match on tags.
  3224. (should-not
  3225. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  3226. (org-match-sparse-tree nil "{^tag.*}")
  3227. (search-forward "H1")
  3228. (org-invisible-p2)))
  3229. (should
  3230. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  3231. (org-match-sparse-tree nil "{^tag.*}")
  3232. (search-forward "H2")
  3233. (org-invisible-p2)))
  3234. ;; Match group tags.
  3235. (should-not
  3236. (org-test-with-temp-text
  3237. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  3238. (org-match-sparse-tree nil "work")
  3239. (search-forward "H1")
  3240. (org-invisible-p2)))
  3241. (should-not
  3242. (org-test-with-temp-text
  3243. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  3244. (org-match-sparse-tree nil "work")
  3245. (search-forward "H2")
  3246. (org-invisible-p2)))
  3247. ;; Match group tags with hard brackets.
  3248. (should-not
  3249. (org-test-with-temp-text
  3250. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  3251. (org-match-sparse-tree nil "work")
  3252. (search-forward "H1")
  3253. (org-invisible-p2)))
  3254. (should-not
  3255. (org-test-with-temp-text
  3256. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  3257. (org-match-sparse-tree nil "work")
  3258. (search-forward "H2")
  3259. (org-invisible-p2)))
  3260. ;; Match tags in hierarchies
  3261. (should-not
  3262. (org-test-with-temp-text
  3263. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  3264. #+TAGS: [ Lev_2 : Lev_3 ]\n
  3265. #+TAGS: { Lev_3 : Lev_4 }\n
  3266. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  3267. (org-match-sparse-tree nil "Lev_1")
  3268. (search-forward "H4")
  3269. (org-invisible-p2)))
  3270. ;; Match regular expressions in tags
  3271. (should-not
  3272. (org-test-with-temp-text
  3273. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  3274. (org-match-sparse-tree nil "Lev")
  3275. (search-forward "H1")
  3276. (org-invisible-p2)))
  3277. (should
  3278. (org-test-with-temp-text
  3279. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  3280. (org-match-sparse-tree nil "Lev")
  3281. (search-forward "H1")
  3282. (org-invisible-p2)))
  3283. ;; Match properties.
  3284. (should
  3285. (org-test-with-temp-text
  3286. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  3287. (org-match-sparse-tree nil "A=\"1\"")
  3288. (search-forward "H2")
  3289. (org-invisible-p2)))
  3290. (should-not
  3291. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  3292. (org-match-sparse-tree nil "A=\"1\"")
  3293. (search-forward "H2")
  3294. (org-invisible-p2)))
  3295. ;; Case is not significant when matching properties.
  3296. (should-not
  3297. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  3298. (org-match-sparse-tree nil "a=\"1\"")
  3299. (search-forward "H2")
  3300. (org-invisible-p2)))
  3301. (should-not
  3302. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  3303. (org-match-sparse-tree nil "A=\"1\"")
  3304. (search-forward "H2")
  3305. (org-invisible-p2)))
  3306. ;; Match special LEVEL property.
  3307. (should-not
  3308. (org-test-with-temp-text "* H\n** H1\n*** H2"
  3309. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  3310. (search-forward "H1")
  3311. (org-invisible-p2)))
  3312. (should
  3313. (org-test-with-temp-text "* H\n** H1\n*** H2"
  3314. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  3315. (search-forward "H2")
  3316. (org-invisible-p2)))
  3317. ;; Comparison operators when matching properties.
  3318. (should
  3319. (org-test-with-temp-text
  3320. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  3321. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  3322. (search-forward "H1")
  3323. (org-invisible-p2)))
  3324. (should-not
  3325. (org-test-with-temp-text
  3326. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  3327. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  3328. (search-forward "H2")
  3329. (org-invisible-p2)))
  3330. ;; Regexp match on properties values.
  3331. (should-not
  3332. (org-test-with-temp-text
  3333. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  3334. (org-match-sparse-tree nil "A={f.*}")
  3335. (search-forward "H1")
  3336. (org-invisible-p2)))
  3337. (should
  3338. (org-test-with-temp-text
  3339. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  3340. (org-match-sparse-tree nil "A={f.*}")
  3341. (search-forward "H2")
  3342. (org-invisible-p2)))
  3343. ;; With an optional argument, limit match to TODO entries.
  3344. (should-not
  3345. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  3346. (org-match-sparse-tree t "tag")
  3347. (search-forward "H1")
  3348. (org-invisible-p2)))
  3349. (should
  3350. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  3351. (org-match-sparse-tree t "tag")
  3352. (search-forward "H2")
  3353. (org-invisible-p2))))
  3354. ;;; Timestamps API
  3355. (ert-deftest test-org/time-stamp ()
  3356. "Test `org-time-stamp' specifications."
  3357. ;; Insert chosen time stamp at point.
  3358. (should
  3359. (string-match
  3360. "Te<2014-03-04 .*?>xt"
  3361. (org-test-with-temp-text "Te<point>xt"
  3362. (flet ((org-read-date
  3363. (&rest args)
  3364. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3365. (org-time-stamp nil)
  3366. (buffer-string)))))
  3367. ;; With a prefix argument, also insert time.
  3368. (should
  3369. (string-match
  3370. "Te<2014-03-04 .*? 00:41>xt"
  3371. (org-test-with-temp-text "Te<point>xt"
  3372. (flet ((org-read-date
  3373. (&rest args)
  3374. (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
  3375. (org-time-stamp '(4))
  3376. (buffer-string)))))
  3377. ;; With two universal prefix arguments, insert an active timestamp
  3378. ;; with the current time without prompting the user.
  3379. (should
  3380. (string-match
  3381. "Te<2014-03-04 .*? 00:41>xt"
  3382. (org-test-with-temp-text "Te<point>xt"
  3383. (flet ((current-time
  3384. ()
  3385. (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
  3386. (org-time-stamp '(16))
  3387. (buffer-string)))))
  3388. ;; When optional argument is non-nil, insert an inactive timestamp.
  3389. (should
  3390. (string-match
  3391. "Te\\[2014-03-04 .*?\\]xt"
  3392. (org-test-with-temp-text "Te<point>xt"
  3393. (flet ((org-read-date
  3394. (&rest args)
  3395. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3396. (org-time-stamp nil t)
  3397. (buffer-string)))))
  3398. ;; When called from a timestamp, replace existing one.
  3399. (should
  3400. (string-match
  3401. "<2014-03-04 .*?>"
  3402. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  3403. (flet ((org-read-date
  3404. (&rest args)
  3405. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3406. (org-time-stamp nil)
  3407. (buffer-string)))))
  3408. (should
  3409. (string-match
  3410. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  3411. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  3412. (flet ((org-read-date
  3413. (&rest args)
  3414. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3415. (org-time-stamp nil)
  3416. (buffer-string)))))
  3417. ;; When replacing a timestamp, preserve repeater, if any.
  3418. (should
  3419. (string-match
  3420. "<2014-03-04 .*? \\+2y>"
  3421. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  3422. (flet ((org-read-date
  3423. (&rest args)
  3424. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3425. (org-time-stamp nil)
  3426. (buffer-string)))))
  3427. ;; When called twice in a raw, build a date range.
  3428. (should
  3429. (string-match
  3430. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  3431. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  3432. (flet ((org-read-date
  3433. (&rest args)
  3434. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3435. (let ((last-command 'org-time-stamp)
  3436. (this-command 'org-time-stamp))
  3437. (org-time-stamp nil))
  3438. (buffer-string))))))
  3439. (ert-deftest test-org/timestamp-has-time-p ()
  3440. "Test `org-timestamp-has-time-p' specifications."
  3441. ;; With time.
  3442. (should
  3443. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  3444. (org-timestamp-has-time-p (org-element-context))))
  3445. ;; Without time.
  3446. (should-not
  3447. (org-test-with-temp-text "<2012-03-29 Thu>"
  3448. (org-timestamp-has-time-p (org-element-context)))))
  3449. (ert-deftest test-org/timestamp-format ()
  3450. "Test `org-timestamp-format' specifications."
  3451. ;; Regular test.
  3452. (should
  3453. (equal
  3454. "2012-03-29 16:40"
  3455. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  3456. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  3457. ;; Range end.
  3458. (should
  3459. (equal
  3460. "2012-03-29"
  3461. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  3462. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  3463. (ert-deftest test-org/timestamp-split-range ()
  3464. "Test `org-timestamp-split-range' specifications."
  3465. ;; Extract range start (active).
  3466. (should
  3467. (equal '(2012 3 29)
  3468. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3469. (let ((ts (org-timestamp-split-range (org-element-context))))
  3470. (mapcar (lambda (p) (org-element-property p ts))
  3471. '(:year-end :month-end :day-end))))))
  3472. ;; Extract range start (inactive)
  3473. (should
  3474. (equal '(2012 3 29)
  3475. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  3476. (let ((ts (org-timestamp-split-range (org-element-context))))
  3477. (mapcar (lambda (p) (org-element-property p ts))
  3478. '(:year-end :month-end :day-end))))))
  3479. ;; Extract range end (active).
  3480. (should
  3481. (equal '(2012 3 30)
  3482. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3483. (let ((ts (org-timestamp-split-range
  3484. (org-element-context) t)))
  3485. (mapcar (lambda (p) (org-element-property p ts))
  3486. '(:year-end :month-end :day-end))))))
  3487. ;; Extract range end (inactive)
  3488. (should
  3489. (equal '(2012 3 30)
  3490. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  3491. (let ((ts (org-timestamp-split-range
  3492. (org-element-context) t)))
  3493. (mapcar (lambda (p) (org-element-property p ts))
  3494. '(:year-end :month-end :day-end))))))
  3495. ;; Return the timestamp if not a range.
  3496. (should
  3497. (org-test-with-temp-text "[2012-03-29 Thu]"
  3498. (let* ((ts-orig (org-element-context))
  3499. (ts-copy (org-timestamp-split-range ts-orig)))
  3500. (eq ts-orig ts-copy))))
  3501. (should
  3502. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  3503. (let* ((ts-orig (org-element-context))
  3504. (ts-copy (org-timestamp-split-range ts-orig)))
  3505. (eq ts-orig ts-copy)))))
  3506. (ert-deftest test-org/timestamp-translate ()
  3507. "Test `org-timestamp-translate' specifications."
  3508. ;; Translate whole date range.
  3509. (should
  3510. (equal "<29>--<30>"
  3511. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3512. (let ((org-display-custom-times t)
  3513. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3514. (org-timestamp-translate (org-element-context))))))
  3515. ;; Translate date range start.
  3516. (should
  3517. (equal "<29>"
  3518. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3519. (let ((org-display-custom-times t)
  3520. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3521. (org-timestamp-translate (org-element-context) 'start)))))
  3522. ;; Translate date range end.
  3523. (should
  3524. (equal "<30>"
  3525. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3526. (let ((org-display-custom-times t)
  3527. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3528. (org-timestamp-translate (org-element-context) 'end)))))
  3529. ;; Translate time range.
  3530. (should
  3531. (equal "<08>--<16>"
  3532. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  3533. (let ((org-display-custom-times t)
  3534. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  3535. (org-timestamp-translate (org-element-context))))))
  3536. ;; Translate non-range timestamp.
  3537. (should
  3538. (equal "<29>"
  3539. (org-test-with-temp-text "<2012-03-29 Thu>"
  3540. (let ((org-display-custom-times t)
  3541. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3542. (org-timestamp-translate (org-element-context))))))
  3543. ;; Do not change `diary' timestamps.
  3544. (should
  3545. (equal "<%%(org-float t 4 2)>"
  3546. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  3547. (let ((org-display-custom-times t)
  3548. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3549. (org-timestamp-translate (org-element-context)))))))
  3550. ;;; Visibility
  3551. (ert-deftest test-org/flag-drawer ()
  3552. "Test `org-flag-drawer' specifications."
  3553. ;; Hide drawer.
  3554. (should
  3555. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3556. (org-flag-drawer t)
  3557. (get-char-property (line-end-position) 'invisible)))
  3558. ;; Show drawer.
  3559. (should-not
  3560. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3561. (org-flag-drawer t)
  3562. (org-flag-drawer nil)
  3563. (get-char-property (line-end-position) 'invisible)))
  3564. ;; Test optional argument.
  3565. (should
  3566. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  3567. (let ((drawer (save-excursion (search-forward ":D2")
  3568. (org-element-at-point))))
  3569. (org-flag-drawer t drawer)
  3570. (get-char-property (progn (search-forward ":D2") (line-end-position))
  3571. 'invisible))))
  3572. (should-not
  3573. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  3574. (let ((drawer (save-excursion (search-forward ":D2")
  3575. (org-element-at-point))))
  3576. (org-flag-drawer t drawer)
  3577. (get-char-property (line-end-position) 'invisible))))
  3578. ;; Do not hide fake drawers.
  3579. (should-not
  3580. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  3581. (forward-line 1)
  3582. (org-flag-drawer t)
  3583. (get-char-property (line-end-position) 'invisible)))
  3584. ;; Do not hide incomplete drawers.
  3585. (should-not
  3586. (org-test-with-temp-text ":D:\nparagraph"
  3587. (forward-line 1)
  3588. (org-flag-drawer t)
  3589. (get-char-property (line-end-position) 'invisible)))
  3590. ;; Do not hide drawers when called from final blank lines.
  3591. (should-not
  3592. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  3593. (goto-char (point-max))
  3594. (org-flag-drawer t)
  3595. (goto-char (point-min))
  3596. (get-char-property (line-end-position) 'invisible)))
  3597. ;; Don't leave point in an invisible part of the buffer when hiding
  3598. ;; a drawer away.
  3599. (should-not
  3600. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3601. (goto-char (point-max))
  3602. (org-flag-drawer t)
  3603. (get-char-property (point) 'invisible))))
  3604. (ert-deftest test-org/hide-block-toggle ()
  3605. "Test `org-hide-block-toggle' specifications."
  3606. ;; Error when not at a block.
  3607. (should-error
  3608. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  3609. (org-hide-block-toggle 'off)
  3610. (get-char-property (line-end-position) 'invisible)))
  3611. ;; Hide block.
  3612. (should
  3613. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3614. (org-hide-block-toggle)
  3615. (get-char-property (line-end-position) 'invisible)))
  3616. (should
  3617. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  3618. (org-hide-block-toggle)
  3619. (get-char-property (line-end-position) 'invisible)))
  3620. ;; Show block unconditionally when optional argument is `off'.
  3621. (should-not
  3622. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3623. (org-hide-block-toggle)
  3624. (org-hide-block-toggle 'off)
  3625. (get-char-property (line-end-position) 'invisible)))
  3626. (should-not
  3627. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3628. (org-hide-block-toggle 'off)
  3629. (get-char-property (line-end-position) 'invisible)))
  3630. ;; Hide block unconditionally when optional argument is non-nil.
  3631. (should
  3632. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3633. (org-hide-block-toggle t)
  3634. (get-char-property (line-end-position) 'invisible)))
  3635. (should
  3636. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3637. (org-hide-block-toggle)
  3638. (org-hide-block-toggle t)
  3639. (get-char-property (line-end-position) 'invisible)))
  3640. ;; Do not hide block when called from final blank lines.
  3641. (should-not
  3642. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  3643. (org-hide-block-toggle)
  3644. (goto-char (point-min))
  3645. (get-char-property (line-end-position) 'invisible)))
  3646. ;; Don't leave point in an invisible part of the buffer when hiding
  3647. ;; a block away.
  3648. (should-not
  3649. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  3650. (org-hide-block-toggle)
  3651. (get-char-property (point) 'invisible))))
  3652. (ert-deftest test-org/hide-block-toggle-maybe ()
  3653. "Test `org-hide-block-toggle-maybe' specifications."
  3654. (should
  3655. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  3656. (org-hide-block-toggle-maybe)))
  3657. (should-not
  3658. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  3659. (ert-deftest test-org/show-set-visibility ()
  3660. "Test `org-show-set-visibility' specifications."
  3661. ;; Do not throw an error before first heading.
  3662. (should
  3663. (org-test-with-temp-text "Preamble\n* Headline"
  3664. (org-show-set-visibility 'tree)
  3665. t))
  3666. ;; Test all visibility spans, both on headline and in entry.
  3667. (let ((list-visible-lines
  3668. (lambda (state headerp)
  3669. (org-test-with-temp-text "* Grandmother (0)
  3670. ** Uncle (1)
  3671. *** Heir (2)
  3672. ** Father (3)
  3673. Ancestor text (4)
  3674. *** Sister (5)
  3675. Sibling text (6)
  3676. *** Self (7)
  3677. Match (8)
  3678. **** First born (9)
  3679. Child text (10)
  3680. **** The other child (11)
  3681. *** Brother (12)
  3682. ** Aunt (13)
  3683. "
  3684. (org-cycle t)
  3685. (search-forward (if headerp "Self" "Match"))
  3686. (org-show-set-visibility state)
  3687. (goto-char (point-min))
  3688. (let (result (line 0))
  3689. (while (not (eobp))
  3690. (unless (org-invisible-p2) (push line result))
  3691. (incf line)
  3692. (forward-line))
  3693. (nreverse result))))))
  3694. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  3695. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  3696. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  3697. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  3698. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  3699. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  3700. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  3701. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  3702. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  3703. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  3704. (funcall list-visible-lines 'tree nil)))
  3705. (should (equal '(0 1 3 4 5 7 12 13)
  3706. (funcall list-visible-lines 'canonical t)))
  3707. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  3708. (funcall list-visible-lines 'canonical nil)))))
  3709. (provide 'test-org)
  3710. ;;; test-org.el ends here