test-org.el 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831
  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. (ert-deftest test-org/org-parse-time-string ()
  180. "Test `org-parse-time-string'."
  181. (should (equal (org-parse-time-string "2012-03-29 16:40")
  182. '(0 40 16 29 3 2012 nil nil nil)))
  183. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  184. '(0 40 16 29 3 2012 nil nil nil)))
  185. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  186. '(0 40 16 29 3 2012 nil nil nil)))
  187. (should (equal (org-parse-time-string "<2012-03-29>")
  188. '(0 0 0 29 3 2012 nil nil nil)))
  189. (should (equal (org-parse-time-string "<2012-03-29>" t)
  190. '(0 nil nil 29 3 2012 nil nil nil))))
  191. ;;; Drawers
  192. (ert-deftest test-org/insert-property-drawer ()
  193. "Test `org-insert-property-drawer' specifications."
  194. ;; Error before first headline.
  195. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  196. ;; Insert drawer right after headline if there is no planning line,
  197. ;; or after it otherwise.
  198. (should
  199. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  200. (org-test-with-temp-text "* H\nParagraph<point>"
  201. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  202. (buffer-string))))
  203. (should
  204. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  205. (org-test-with-temp-text
  206. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  207. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  208. (buffer-string))))
  209. ;; Indent inserted drawer.
  210. (should
  211. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  212. (org-test-with-temp-text "* H\nParagraph<point>"
  213. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  214. (buffer-string))))
  215. ;; Handle insertion at eob.
  216. (should
  217. (equal "* H\n:PROPERTIES:\n:END:\n"
  218. (org-test-with-temp-text "* H"
  219. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  220. (buffer-string))))
  221. ;; Skip inlinetasks before point.
  222. (when (featurep 'org-inlinetask)
  223. (should
  224. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  225. (org-test-with-temp-text
  226. "* H\n*************** I\n*************** END\nP<point>"
  227. (let ((org-adapt-indentation nil)
  228. (org-inlinetask-min-level 15))
  229. (org-insert-property-drawer))
  230. (buffer-string)))))
  231. ;; Correctly set drawer in an inlinetask.
  232. (when (featurep 'org-inlinetask)
  233. (should
  234. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  235. (org-test-with-temp-text
  236. "* H\n*************** I\nP<point>\n*************** END"
  237. (let ((org-adapt-indentation nil)
  238. (org-inlinetask-min-level 15))
  239. (org-insert-property-drawer))
  240. (buffer-string))))))
  241. ;;; Filling
  242. (ert-deftest test-org/fill-paragraph ()
  243. "Test `org-fill-paragraph' specifications."
  244. ;; At an Org table, align it.
  245. (should
  246. (equal "| a |\n"
  247. (org-test-with-temp-text "|a|"
  248. (org-fill-paragraph)
  249. (buffer-string))))
  250. (should
  251. (equal "#+name: table\n| a |\n"
  252. (org-test-with-temp-text "#+name: table\n| a |"
  253. (org-fill-paragraph)
  254. (buffer-string))))
  255. ;; At a paragraph, preserve line breaks.
  256. (org-test-with-temp-text "some \\\\\nlong\ntext"
  257. (let ((fill-column 20))
  258. (org-fill-paragraph)
  259. (should (equal (buffer-string) "some \\\\\nlong text"))))
  260. ;; Correctly fill a paragraph when point is at its very end.
  261. (should
  262. (equal "A B"
  263. (org-test-with-temp-text "A\nB"
  264. (let ((fill-column 20))
  265. (goto-char (point-max))
  266. (org-fill-paragraph)
  267. (buffer-string)))))
  268. ;; Correctly fill the last paragraph of a greater element.
  269. (should
  270. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  271. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  272. (let ((fill-column 8))
  273. (forward-line)
  274. (end-of-line)
  275. (org-fill-paragraph)
  276. (buffer-string)))))
  277. ;; Correctly fill an element in a narrowed buffer.
  278. (should
  279. (equal "01234\n6"
  280. (org-test-with-temp-text "01234 6789"
  281. (let ((fill-column 5))
  282. (narrow-to-region 1 8)
  283. (org-fill-paragraph)
  284. (buffer-string)))))
  285. ;; Handle `adaptive-fill-regexp' in paragraphs.
  286. (should
  287. (equal "> a b"
  288. (org-test-with-temp-text "> a\n> b"
  289. (let ((fill-column 5)
  290. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  291. (org-fill-paragraph)
  292. (buffer-string)))))
  293. ;; Special case: Fill first paragraph when point is at an item or
  294. ;; a plain-list or a footnote reference.
  295. (should
  296. (equal "- A B"
  297. (org-test-with-temp-text "- A\n B"
  298. (let ((fill-column 20))
  299. (org-fill-paragraph)
  300. (buffer-string)))))
  301. (should
  302. (equal "[fn:1] A B"
  303. (org-test-with-temp-text "[fn:1] A\nB"
  304. (let ((fill-column 20))
  305. (org-fill-paragraph)
  306. (buffer-string)))))
  307. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  308. (let ((fill-column 20))
  309. (org-fill-paragraph)
  310. (should (equal (buffer-string)
  311. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  312. ;; Fill contents of `comment-block' elements.
  313. (should
  314. (equal
  315. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  316. (let ((fill-column 20))
  317. (forward-line)
  318. (org-fill-paragraph)
  319. (buffer-string)))
  320. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  321. ;; Fill `comment' elements.
  322. (should
  323. (equal " # A B"
  324. (org-test-with-temp-text " # A\n # B"
  325. (let ((fill-column 20))
  326. (org-fill-paragraph)
  327. (buffer-string)))))
  328. ;; Do not mix consecutive comments when filling one of them.
  329. (should
  330. (equal "# A B\n\n# C"
  331. (org-test-with-temp-text "# A\n# B\n\n# C"
  332. (let ((fill-column 20))
  333. (org-fill-paragraph)
  334. (buffer-string)))))
  335. ;; Use commented empty lines as separators when filling comments.
  336. (should
  337. (equal "# A B\n#\n# C"
  338. (org-test-with-temp-text "# A\n# B\n#\n# C"
  339. (let ((fill-column 20))
  340. (org-fill-paragraph)
  341. (buffer-string)))))
  342. ;; Handle `adaptive-fill-regexp' in comments.
  343. (should
  344. (equal "# > a b"
  345. (org-test-with-temp-text "# > a\n# > b"
  346. (let ((fill-column 20)
  347. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  348. (org-fill-paragraph)
  349. (buffer-string)))))
  350. ;; Do nothing at affiliated keywords.
  351. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  352. (let ((fill-column 20))
  353. (org-fill-paragraph)
  354. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  355. ;; Do not move point after table when filling a table.
  356. (should-not
  357. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  358. (forward-char)
  359. (org-fill-paragraph)
  360. (eobp))))
  361. (ert-deftest test-org/auto-fill-function ()
  362. "Test auto-filling features."
  363. ;; Auto fill paragraph.
  364. (should
  365. (equal "12345\n7890"
  366. (org-test-with-temp-text "12345 7890"
  367. (let ((fill-column 5))
  368. (end-of-line)
  369. (org-auto-fill-function)
  370. (buffer-string)))))
  371. ;; Auto fill first paragraph in an item.
  372. (should
  373. (equal "- 12345\n 7890"
  374. (org-test-with-temp-text "- 12345 7890"
  375. (let ((fill-column 7))
  376. (end-of-line)
  377. (org-auto-fill-function)
  378. (buffer-string)))))
  379. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  380. (should
  381. (equal "> 12345\n 7890"
  382. (org-test-with-temp-text "> 12345 7890"
  383. (let ((fill-column 10)
  384. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  385. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  386. (end-of-line)
  387. (org-auto-fill-function)
  388. (buffer-string)))))
  389. (should
  390. (equal "> 12345\n> 12345\n> 7890"
  391. (org-test-with-temp-text "> 12345\n> 12345 7890"
  392. (let ((fill-column 10)
  393. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  394. (goto-char (point-max))
  395. (org-auto-fill-function)
  396. (buffer-string)))))
  397. (should-not
  398. (equal " 12345\n *12345\n *12345"
  399. (org-test-with-temp-text " 12345\n *12345 12345"
  400. (let ((fill-column 10)
  401. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  402. (goto-char (point-max))
  403. (org-auto-fill-function)
  404. (buffer-string)))))
  405. ;; Auto fill comments.
  406. (should
  407. (equal " # 12345\n # 7890"
  408. (org-test-with-temp-text " # 12345 7890"
  409. (let ((fill-column 10))
  410. (end-of-line)
  411. (org-auto-fill-function)
  412. (buffer-string)))))
  413. ;; A hash within a line isn't a comment.
  414. (should-not
  415. (equal "12345 # 7890\n# 1"
  416. (org-test-with-temp-text "12345 # 7890 1"
  417. (let ((fill-column 12))
  418. (end-of-line)
  419. (org-auto-fill-function)
  420. (buffer-string)))))
  421. ;; Correctly interpret empty prefix.
  422. (should-not
  423. (equal "# a\n# b\nRegular\n# paragraph"
  424. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  425. (let ((fill-column 12))
  426. (end-of-line 3)
  427. (org-auto-fill-function)
  428. (buffer-string)))))
  429. ;; Comment block: auto fill contents.
  430. (should
  431. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  432. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  433. (let ((fill-column 5))
  434. (forward-line)
  435. (end-of-line)
  436. (org-auto-fill-function)
  437. (buffer-string)))))
  438. (should
  439. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  440. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  441. (let ((fill-column 5))
  442. (forward-line)
  443. (end-of-line)
  444. (org-auto-fill-function)
  445. (buffer-string)))))
  446. ;; Do not fill if a new item could be created.
  447. (should-not
  448. (equal "12345\n- 90"
  449. (org-test-with-temp-text "12345 - 90"
  450. (let ((fill-column 5))
  451. (end-of-line)
  452. (org-auto-fill-function)
  453. (buffer-string)))))
  454. ;; Do not fill if a line break could be introduced.
  455. (should-not
  456. (equal "123\\\\\n7890"
  457. (org-test-with-temp-text "123\\\\ 7890"
  458. (let ((fill-column 6))
  459. (end-of-line)
  460. (org-auto-fill-function)
  461. (buffer-string)))))
  462. ;; Do not fill affiliated keywords.
  463. (should-not
  464. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  465. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  466. (let ((fill-column 20))
  467. (end-of-line)
  468. (org-auto-fill-function)
  469. (buffer-string))))))
  470. ;;; Indentation
  471. (ert-deftest test-org/indent-line ()
  472. "Test `org-indent-line' specifications."
  473. ;; Do not indent footnote definitions or headlines.
  474. (should
  475. (zerop
  476. (org-test-with-temp-text "* H"
  477. (org-indent-line)
  478. (org-get-indentation))))
  479. (should
  480. (zerop
  481. (org-test-with-temp-text "[fn:1] fn"
  482. (let ((org-adapt-indentation t)) (org-indent-line))
  483. (org-get-indentation))))
  484. ;; Do not indent before first headline.
  485. (should
  486. (zerop
  487. (org-test-with-temp-text ""
  488. (org-indent-line)
  489. (org-get-indentation))))
  490. ;; Indent according to headline level otherwise, unless
  491. ;; `org-adapt-indentation' is nil.
  492. (should
  493. (= 2
  494. (org-test-with-temp-text "* H\nA"
  495. (forward-line)
  496. (let ((org-adapt-indentation t)) (org-indent-line))
  497. (org-get-indentation))))
  498. (should
  499. (= 2
  500. (org-test-with-temp-text "* H\n\nA"
  501. (forward-line)
  502. (let ((org-adapt-indentation t)) (org-indent-line))
  503. (org-get-indentation))))
  504. (should
  505. (zerop
  506. (org-test-with-temp-text "* H\nA"
  507. (forward-line)
  508. (let ((org-adapt-indentation nil)) (org-indent-line))
  509. (org-get-indentation))))
  510. ;; Indenting preserves point position.
  511. (should
  512. (org-test-with-temp-text "* H\nAB"
  513. (forward-line)
  514. (forward-char)
  515. (let ((org-adapt-indentation t)) (org-indent-line))
  516. (looking-at "B")))
  517. ;; Do not change indentation at an item.
  518. (should
  519. (= 1
  520. (org-test-with-temp-text "* H\n - A"
  521. (forward-line)
  522. (let ((org-adapt-indentation t)) (org-indent-line))
  523. (org-get-indentation))))
  524. ;; On blank lines at the end of a list, indent like last element
  525. ;; within it if the line is still in the list. Otherwise, indent
  526. ;; like the whole list.
  527. (should
  528. (= 4
  529. (org-test-with-temp-text "* H\n- A\n - AA\n"
  530. (goto-char (point-max))
  531. (let ((org-adapt-indentation t)) (org-indent-line))
  532. (org-get-indentation))))
  533. (should
  534. (zerop
  535. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
  536. (goto-char (point-max))
  537. (let ((org-adapt-indentation t)) (org-indent-line))
  538. (org-get-indentation))))
  539. ;; Likewise, on a blank line at the end of a footnote definition,
  540. ;; indent at column 0 if line belongs to the definition. Otherwise,
  541. ;; indent like the definition itself.
  542. (should
  543. (zerop
  544. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  545. (goto-char (point-max))
  546. (let ((org-adapt-indentation t)) (org-indent-line))
  547. (org-get-indentation))))
  548. (should
  549. (zerop
  550. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  551. (goto-char (point-max))
  552. (let ((org-adapt-indentation t)) (org-indent-line))
  553. (org-get-indentation))))
  554. ;; After the end of the contents of a greater element, indent like
  555. ;; the beginning of the element.
  556. (should
  557. (= 1
  558. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  559. (forward-line 2)
  560. (org-indent-line)
  561. (org-get-indentation))))
  562. ;; At the first line of an element, indent like previous element's
  563. ;; first line, ignoring footnotes definitions and inline tasks, or
  564. ;; according to parent.
  565. (should
  566. (= 2
  567. (org-test-with-temp-text "A\n\n B\n\nC"
  568. (goto-char (point-max))
  569. (org-indent-line)
  570. (org-get-indentation))))
  571. (should
  572. (= 1
  573. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  574. (goto-char (point-max))
  575. (org-indent-line)
  576. (org-get-indentation))))
  577. (should
  578. (= 1
  579. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  580. (forward-line 1)
  581. (org-indent-line)
  582. (org-get-indentation))))
  583. ;; Within code part of a source block, use language major mode if
  584. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  585. ;; according to line above.
  586. (should
  587. (= 6
  588. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  589. (forward-line 2)
  590. (let ((org-src-tab-acts-natively t)
  591. (org-edit-src-content-indentation 0))
  592. (org-indent-line))
  593. (org-get-indentation))))
  594. (should
  595. (= 1
  596. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  597. (forward-line 2)
  598. (let ((org-src-tab-acts-natively nil)
  599. (org-edit-src-content-indentation 0))
  600. (org-indent-line))
  601. (org-get-indentation))))
  602. ;; Otherwise, indent like the first non-blank line above.
  603. (should
  604. (zerop
  605. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  606. (forward-line 3)
  607. (org-indent-line)
  608. (org-get-indentation))))
  609. ;; Align node properties according to `org-property-format'. Handle
  610. ;; nicely empty values.
  611. (should
  612. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  613. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  614. (let ((org-property-format "%-10s %s")) (org-indent-line))
  615. (buffer-string))))
  616. (should
  617. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  618. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  619. (let ((org-property-format "%-10s %s")) (org-indent-line))
  620. (buffer-string)))))
  621. (ert-deftest test-org/indent-region ()
  622. "Test `org-indent-region' specifications."
  623. ;; Indent paragraph.
  624. (should
  625. (equal "A\nB\nC"
  626. (org-test-with-temp-text " A\nB\n C"
  627. (org-indent-region (point-min) (point-max))
  628. (buffer-string))))
  629. ;; Indent greater elements along with their contents.
  630. (should
  631. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  632. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  633. (org-indent-region (point-min) (point-max))
  634. (buffer-string))))
  635. ;; Ignore contents of verse blocks and example blocks.
  636. (should
  637. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  638. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  639. (org-indent-region (point-min) (point-max))
  640. (buffer-string))))
  641. (should
  642. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  643. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  644. (org-indent-region (point-min) (point-max))
  645. (buffer-string))))
  646. ;; Indent according to mode if `org-src-tab-acts-natively' is
  647. ;; non-nil. Otherwise, do not indent code at all.
  648. (should
  649. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  650. (org-test-with-temp-text
  651. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  652. (let ((org-src-tab-acts-natively t)
  653. (org-edit-src-content-indentation 0))
  654. (org-indent-region (point-min) (point-max)))
  655. (buffer-string))))
  656. (should
  657. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  658. (org-test-with-temp-text
  659. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  660. (let ((org-src-tab-acts-natively nil)
  661. (org-edit-src-content-indentation 0))
  662. (org-indent-region (point-min) (point-max)))
  663. (buffer-string))))
  664. ;; Align node properties according to `org-property-format'. Handle
  665. ;; nicely empty values.
  666. (should
  667. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  668. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  669. (let ((org-property-format "%-10s %s")
  670. (org-adapt-indentation nil))
  671. (org-indent-region (point) (point-max)))
  672. (buffer-string))))
  673. (should
  674. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  675. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  676. (let ((org-property-format "%-10s %s")
  677. (org-adapt-indentation nil))
  678. (org-indent-region (point) (point-max)))
  679. (buffer-string))))
  680. ;; Indent plain lists.
  681. (should
  682. (equal "- A\n B\n - C\n\n D"
  683. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  684. (org-indent-region (point-min) (point-max))
  685. (buffer-string))))
  686. (should
  687. (equal "- A\n\n- B"
  688. (org-test-with-temp-text " - A\n\n - B"
  689. (org-indent-region (point-min) (point-max))
  690. (buffer-string))))
  691. ;; Indent footnote definitions.
  692. (should
  693. (equal "[fn:1] Definition\n\nDefinition"
  694. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  695. (org-indent-region (point-min) (point-max))
  696. (buffer-string))))
  697. ;; Special case: Start indenting on a blank line.
  698. (should
  699. (equal "\nParagraph"
  700. (org-test-with-temp-text "\n Paragraph"
  701. (org-indent-region (point-min) (point-max))
  702. (buffer-string)))))
  703. ;;; Editing
  704. ;;;; Insert elements
  705. (ert-deftest test-org/meta-return ()
  706. "Test M-RET (`org-meta-return')."
  707. ;; In a table field insert a row above.
  708. (should
  709. (org-test-with-temp-text "| a |"
  710. (forward-char)
  711. (org-meta-return)
  712. (forward-line -1)
  713. (looking-at "| |$")))
  714. ;; In a paragraph change current line into a header.
  715. (should
  716. (org-test-with-temp-text "a"
  717. (org-meta-return)
  718. (beginning-of-line)
  719. (looking-at "\* a$")))
  720. ;; In an item insert an item, in this case above.
  721. (should
  722. (org-test-with-temp-text "- a"
  723. (org-meta-return)
  724. (beginning-of-line)
  725. (looking-at "- $")))
  726. ;; In a drawer and item insert an item, in this case above.
  727. (should
  728. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  729. (forward-line)
  730. (org-meta-return)
  731. (beginning-of-line)
  732. (looking-at "- $"))))
  733. (ert-deftest test-org/insert-heading ()
  734. "Test `org-insert-heading' specifications."
  735. ;; FIXME: Test coverage is incomplete yet.
  736. ;;
  737. ;; In an empty buffer, insert a new headline.
  738. (should
  739. (equal "* "
  740. (org-test-with-temp-text ""
  741. (org-insert-heading)
  742. (buffer-string))))
  743. ;; At the beginning of a line, turn it into a headline
  744. (should
  745. (equal "* P"
  746. (org-test-with-temp-text "<point>P"
  747. (org-insert-heading)
  748. (buffer-string))))
  749. ;; In the middle of a line, split the line if allowed, otherwise,
  750. ;; insert the headline at its end.
  751. (should
  752. (equal "Para\n* graph"
  753. (org-test-with-temp-text "Para<point>graph"
  754. (let ((org-M-RET-may-split-line '((default . t))))
  755. (org-insert-heading))
  756. (buffer-string))))
  757. (should
  758. (equal "Paragraph\n* "
  759. (org-test-with-temp-text "Para<point>graph"
  760. (let ((org-M-RET-may-split-line '((default . nil))))
  761. (org-insert-heading))
  762. (buffer-string))))
  763. ;; Corner case: correctly insert a headline after an empty one.
  764. (should
  765. (equal "* \n* "
  766. (org-test-with-temp-text "* <point>"
  767. (org-insert-heading)
  768. (buffer-string)))))
  769. (ert-deftest test-org/insert-todo-heading-respect-content ()
  770. "Test `org-insert-todo-heading-respect-content' specifications."
  771. ;; Create a TODO heading.
  772. (should
  773. (org-test-with-temp-text "* H1\n Body"
  774. (org-insert-todo-heading-respect-content)
  775. (nth 2 (org-heading-components))))
  776. ;; Add headline at the end of the first subtree
  777. (should
  778. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  779. (search-forward "H1Body")
  780. (org-insert-todo-heading-respect-content)
  781. (and (eobp) (org-at-heading-p))))
  782. ;; In a list, do not create a new item.
  783. (should
  784. (org-test-with-temp-text "* H\n- an item\n- another one"
  785. (search-forward "an ")
  786. (org-insert-todo-heading-respect-content)
  787. (and (eobp) (org-at-heading-p)))))
  788. ;;; Fixed-Width Areas
  789. (ert-deftest test-org/toggle-fixed-width ()
  790. "Test `org-toggle-fixed-width' specifications."
  791. ;; No region: Toggle on fixed-width marker in paragraphs.
  792. (should
  793. (equal ": A"
  794. (org-test-with-temp-text "A"
  795. (org-toggle-fixed-width)
  796. (buffer-string))))
  797. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  798. (should
  799. (equal "A"
  800. (org-test-with-temp-text ": A"
  801. (org-toggle-fixed-width)
  802. (buffer-string))))
  803. ;; No region: Toggle on marker in blank lines after elements or just
  804. ;; after a headline.
  805. (should
  806. (equal "* H\n: "
  807. (org-test-with-temp-text "* H\n"
  808. (forward-line)
  809. (org-toggle-fixed-width)
  810. (buffer-string))))
  811. (should
  812. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  813. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  814. (goto-char (point-max))
  815. (org-toggle-fixed-width)
  816. (buffer-string))))
  817. ;; No region: Toggle on marker in front of one line elements (e.g.,
  818. ;; headlines, clocks)
  819. (should
  820. (equal ": * Headline"
  821. (org-test-with-temp-text "* Headline"
  822. (org-toggle-fixed-width)
  823. (buffer-string))))
  824. (should
  825. (equal ": #+KEYWORD: value"
  826. (org-test-with-temp-text "#+KEYWORD: value"
  827. (org-toggle-fixed-width)
  828. (buffer-string))))
  829. ;; No region: error in other situations.
  830. (should-error
  831. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  832. (forward-line)
  833. (org-toggle-fixed-width)
  834. (buffer-string)))
  835. ;; No region: Indentation is preserved.
  836. (should
  837. (equal "- A\n : B"
  838. (org-test-with-temp-text "- A\n B"
  839. (forward-line)
  840. (org-toggle-fixed-width)
  841. (buffer-string))))
  842. ;; Region: If it contains only fixed-width elements and blank lines,
  843. ;; toggle off fixed-width markup.
  844. (should
  845. (equal "A\n\nB"
  846. (org-test-with-temp-text ": A\n\n: B"
  847. (transient-mark-mode 1)
  848. (push-mark (point) t t)
  849. (goto-char (point-max))
  850. (org-toggle-fixed-width)
  851. (buffer-string))))
  852. ;; Region: If it contains anything else, toggle on fixed-width but
  853. ;; not on fixed-width areas.
  854. (should
  855. (equal ": A\n: \n: B\n: \n: C"
  856. (org-test-with-temp-text "A\n\n: B\n\nC"
  857. (transient-mark-mode 1)
  858. (push-mark (point) t t)
  859. (goto-char (point-max))
  860. (org-toggle-fixed-width)
  861. (buffer-string))))
  862. ;; Region: Ignore blank lines at its end, unless it contains only
  863. ;; such lines.
  864. (should
  865. (equal ": A\n\n"
  866. (org-test-with-temp-text "A\n\n"
  867. (transient-mark-mode 1)
  868. (push-mark (point) t t)
  869. (goto-char (point-max))
  870. (org-toggle-fixed-width)
  871. (buffer-string))))
  872. (should
  873. (equal ": \n: \n"
  874. (org-test-with-temp-text "\n\n"
  875. (transient-mark-mode 1)
  876. (push-mark (point) t t)
  877. (goto-char (point-max))
  878. (org-toggle-fixed-width)
  879. (buffer-string)))))
  880. ;;; Headline
  881. (ert-deftest test-org/in-commented-heading-p ()
  882. "Test `org-in-commented-heading-p' specifications."
  883. ;; Commented headline.
  884. (should
  885. (org-test-with-temp-text "* COMMENT Headline\nBody"
  886. (goto-char (point-max))
  887. (org-in-commented-heading-p)))
  888. ;; Commented ancestor.
  889. (should
  890. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  891. (goto-char (point-max))
  892. (org-in-commented-heading-p)))
  893. ;; Comment keyword is case-sensitive.
  894. (should-not
  895. (org-test-with-temp-text "* Comment Headline\nBody"
  896. (goto-char (point-max))
  897. (org-in-commented-heading-p)))
  898. ;; Keyword is standalone.
  899. (should-not
  900. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  901. (goto-char (point-max))
  902. (org-in-commented-heading-p)))
  903. ;; Optional argument.
  904. (should-not
  905. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  906. (goto-char (point-max))
  907. (org-in-commented-heading-p t))))
  908. ;;; Keywords
  909. (ert-deftest test-org/set-regexps-and-options ()
  910. "Test `org-set-regexps-and-options' specifications."
  911. ;; TAGS keyword.
  912. (should
  913. (equal '(("A" . ?a) ("B") ("C"))
  914. (org-test-with-temp-text "#+TAGS: A(a) B C"
  915. (org-mode-restart)
  916. org-tag-alist)))
  917. (should
  918. (equal '(("A") (:newline) ("B"))
  919. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  920. (org-mode-restart)
  921. org-tag-alist)))
  922. (should
  923. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  924. (org-test-with-temp-text "#+TAGS: { A B } C"
  925. (org-mode-restart)
  926. org-tag-alist)))
  927. (should
  928. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  929. (org-test-with-temp-text "#+TAGS: { A : B C }"
  930. (org-mode-restart)
  931. org-tag-alist)))
  932. (should
  933. (equal '(("A" "B" "C"))
  934. (org-test-with-temp-text "#+TAGS: { A : B C }"
  935. (org-mode-restart)
  936. org-tag-groups-alist)))
  937. ;; FILETAGS keyword.
  938. (should
  939. (equal '("A" "B" "C")
  940. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  941. (org-mode-restart)
  942. org-file-tags)))
  943. ;; PROPERTY keyword. Property names are case-insensitive.
  944. (should
  945. (equal "foo=1"
  946. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  947. (org-mode-restart)
  948. (cdr (assoc "var" org-file-properties)))))
  949. (should
  950. (equal
  951. "foo=1 bar=2"
  952. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  953. (org-mode-restart)
  954. (cdr (assoc "var" org-file-properties)))))
  955. (should
  956. (equal
  957. "foo=1 bar=2"
  958. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  959. (org-mode-restart)
  960. (cdr (assoc "var" org-file-properties)))))
  961. ;; ARCHIVE keyword.
  962. (should
  963. (equal "%s_done::"
  964. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  965. (org-mode-restart)
  966. org-archive-location)))
  967. ;; CATEGORY keyword.
  968. (should
  969. (eq 'test
  970. (org-test-with-temp-text "#+CATEGORY: test"
  971. (org-mode-restart)
  972. org-category)))
  973. (should
  974. (equal "test"
  975. (org-test-with-temp-text "#+CATEGORY: test"
  976. (org-mode-restart)
  977. (cdr (assoc "CATEGORY" org-file-properties)))))
  978. ;; COLUMNS keyword.
  979. (should
  980. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  981. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  982. (org-mode-restart)
  983. org-columns-default-format)))
  984. ;; CONSTANTS keyword. Constants names are case sensitive.
  985. (should
  986. (equal '("299792458." "3.14")
  987. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  988. (org-mode-restart)
  989. (mapcar
  990. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  991. '("c" "pi")))))
  992. (should
  993. (equal "3.14"
  994. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  995. (org-mode-restart)
  996. (cdr (assoc "pi" org-table-formula-constants-local)))))
  997. (should
  998. (equal "22/7"
  999. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1000. (org-mode-restart)
  1001. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1002. ;; LINK keyword.
  1003. (should
  1004. (equal
  1005. '("url1" "url2")
  1006. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1007. (org-mode-restart)
  1008. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1009. '("a" "b")))))
  1010. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1011. (should
  1012. (equal
  1013. '(?X ?Z ?Y)
  1014. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1015. (org-mode-restart)
  1016. (list org-highest-priority org-lowest-priority org-default-priority))))
  1017. (should
  1018. (equal
  1019. '(?A ?C ?B)
  1020. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1021. (org-mode-restart)
  1022. (list org-highest-priority org-lowest-priority org-default-priority))))
  1023. ;; STARTUP keyword.
  1024. (should
  1025. (equal '(t t)
  1026. (org-test-with-temp-text "#+STARTUP: fold odd"
  1027. (org-mode-restart)
  1028. (list org-startup-folded org-odd-levels-only))))
  1029. ;; TODO keywords.
  1030. (should
  1031. (equal '(("A" "B") ("C"))
  1032. (org-test-with-temp-text "#+TODO: A B | C"
  1033. (org-mode-restart)
  1034. (list org-not-done-keywords org-done-keywords))))
  1035. (should
  1036. (equal '(("A" "C") ("B" "D"))
  1037. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1038. (org-mode-restart)
  1039. (list org-not-done-keywords org-done-keywords))))
  1040. (should
  1041. (equal '(("A" "B") ("C"))
  1042. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1043. (org-mode-restart)
  1044. (list org-not-done-keywords org-done-keywords))))
  1045. (should
  1046. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1047. (org-test-with-temp-text "#+TODO: A(a)"
  1048. (org-mode-restart)
  1049. org-todo-key-alist)))
  1050. (should
  1051. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1052. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1053. (org-mode-restart)
  1054. org-todo-log-states)))
  1055. ;; Enter SETUPFILE keyword.
  1056. (should
  1057. (equal "1"
  1058. (org-test-with-temp-text
  1059. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1060. (org-mode-restart)
  1061. (cdr (assoc "a" org-file-properties))))))
  1062. ;;; Links
  1063. ;;;; Coderefs
  1064. (ert-deftest test-org/coderef ()
  1065. "Test coderef links specifications."
  1066. (should
  1067. (org-test-with-temp-text "
  1068. #+BEGIN_SRC emacs-lisp
  1069. \(+ 1 1) (ref:sc)
  1070. #+END_SRC
  1071. \[[(sc)]]"
  1072. (goto-char (point-max))
  1073. (org-open-at-point)
  1074. (looking-at "(ref:sc)"))))
  1075. ;;;; Custom ID
  1076. (ert-deftest test-org/custom-id ()
  1077. "Test custom ID links specifications."
  1078. (should
  1079. (org-test-with-temp-text
  1080. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  1081. (goto-char (point-max))
  1082. (org-open-at-point)
  1083. (org-looking-at-p "\\* H1"))))
  1084. ;;;; Fuzzy Links
  1085. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1086. ;; a named element (#+name: text) and to headlines (* Text).
  1087. (ert-deftest test-org/fuzzy-links ()
  1088. "Test fuzzy links specifications."
  1089. ;; Fuzzy link goes in priority to a matching target.
  1090. (should
  1091. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  1092. (goto-line 5)
  1093. (org-open-at-point)
  1094. (looking-at "<<Test>>")))
  1095. ;; Then fuzzy link points to an element with a given name.
  1096. (should
  1097. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  1098. (goto-line 5)
  1099. (org-open-at-point)
  1100. (looking-at "#\\+NAME: Test")))
  1101. ;; A target still lead to a matching headline otherwise.
  1102. (should
  1103. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  1104. (goto-line 4)
  1105. (org-open-at-point)
  1106. (looking-at "\\* Head2")))
  1107. ;; With a leading star in link, enforce heading match.
  1108. (should
  1109. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  1110. (goto-line 3)
  1111. (org-open-at-point)
  1112. (looking-at "\\* Test")))
  1113. ;; Correctly un-hexify fuzzy links.
  1114. (should
  1115. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  1116. (goto-char (point-max))
  1117. (org-open-at-point)
  1118. (bobp))))
  1119. ;;;; Link Escaping
  1120. (ert-deftest test-org/org-link-escape-ascii-character ()
  1121. "Escape an ascii character."
  1122. (should
  1123. (string=
  1124. "%5B"
  1125. (org-link-escape "["))))
  1126. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  1127. "Escape an ascii control character."
  1128. (should
  1129. (string=
  1130. "%09"
  1131. (org-link-escape "\t"))))
  1132. (ert-deftest test-org/org-link-escape-multibyte-character ()
  1133. "Escape an unicode multibyte character."
  1134. (should
  1135. (string=
  1136. "%E2%82%AC"
  1137. (org-link-escape "€"))))
  1138. (ert-deftest test-org/org-link-escape-custom-table ()
  1139. "Escape string with custom character table."
  1140. (should
  1141. (string=
  1142. "Foo%3A%42ar%0A"
  1143. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  1144. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  1145. "Escape string with custom table merged with default table."
  1146. (should
  1147. (string=
  1148. "%5BF%6F%6F%3A%42ar%0A%5D"
  1149. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  1150. (ert-deftest test-org/org-link-unescape-ascii-character ()
  1151. "Unescape an ascii character."
  1152. (should
  1153. (string=
  1154. "["
  1155. (org-link-unescape "%5B"))))
  1156. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  1157. "Unescpae an ascii control character."
  1158. (should
  1159. (string=
  1160. "\n"
  1161. (org-link-unescape "%0A"))))
  1162. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  1163. "Unescape unicode multibyte character."
  1164. (should
  1165. (string=
  1166. "€"
  1167. (org-link-unescape "%E2%82%AC"))))
  1168. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  1169. "Unescape old style percent escaped character."
  1170. (should
  1171. (string=
  1172. "àâçèéêîôùû"
  1173. (decode-coding-string
  1174. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  1175. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  1176. "Escape and unescape a URL that includes an escaped char.
  1177. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  1178. (should
  1179. (string=
  1180. "http://some.host.com/form?&id=blah%2Bblah25"
  1181. (org-link-unescape
  1182. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  1183. (ert-deftest test-org/org-link-escape-chars-browser ()
  1184. "Test of the constant `org-link-escape-chars-browser'.
  1185. See there why this test is a candidate to be removed once Org
  1186. drops support for Emacs 24.1 and 24.2."
  1187. (should
  1188. (string=
  1189. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1190. "%22Release%208.2%22&idxname=emacs-orgmode")
  1191. (org-link-escape-browser ; Do not replace with `url-encode-url',
  1192. ; see docstring above.
  1193. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1194. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  1195. ;;;; Open at point
  1196. (ert-deftest test-org/open-at-point-in-property ()
  1197. "Does `org-open-at-point' open link in property drawer?"
  1198. (should
  1199. (org-test-with-temp-text
  1200. "* Headline
  1201. :PROPERTIES:
  1202. :URL: <point>[[info:emacs#Top]]
  1203. :END:"
  1204. (org-open-at-point) t)))
  1205. (ert-deftest test-org/open-at-point-in-comment ()
  1206. "Does `org-open-at-point' open link in a commented line?"
  1207. (should
  1208. (org-test-with-temp-text
  1209. "# <point>[[info:emacs#Top]]"
  1210. (org-open-at-point) t)))
  1211. (ert-deftest test-org/open-at-point/info ()
  1212. "Test `org-open-at-point' on info links."
  1213. (should
  1214. (org-test-with-temp-text
  1215. "<point>[[info:emacs#Top]]"
  1216. (org-open-at-point)
  1217. (and (switch-to-buffer "*info*")
  1218. (prog1
  1219. (looking-at "\nThe Emacs Editor")
  1220. (kill-buffer))))))
  1221. ;;; Node Properties
  1222. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1223. "Ensure properties accumulate in subtree drawers."
  1224. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1225. (org-babel-next-src-block)
  1226. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1227. (ert-deftest test-org/custom-properties ()
  1228. "Test custom properties specifications."
  1229. ;; Standard test.
  1230. (should
  1231. (let ((org-custom-properties '("FOO")))
  1232. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1233. (org-toggle-custom-properties-visibility)
  1234. (org-invisible-p2))))
  1235. ;; Properties are case-insensitive.
  1236. (should
  1237. (let ((org-custom-properties '("FOO")))
  1238. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  1239. (org-toggle-custom-properties-visibility)
  1240. (org-invisible-p2))))
  1241. (should
  1242. (let ((org-custom-properties '("foo")))
  1243. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1244. (org-toggle-custom-properties-visibility)
  1245. (org-invisible-p2))))
  1246. ;; Multiple custom properties in the same drawer.
  1247. (should
  1248. (let ((org-custom-properties '("FOO" "BAR")))
  1249. (org-test-with-temp-text
  1250. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  1251. (org-toggle-custom-properties-visibility)
  1252. (and (org-invisible-p2)
  1253. (not (progn (forward-line) (org-invisible-p2)))
  1254. (progn (forward-line) (org-invisible-p2))))))
  1255. ;; Hide custom properties with an empty value.
  1256. (should
  1257. (let ((org-custom-properties '("FOO")))
  1258. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  1259. (org-toggle-custom-properties-visibility)
  1260. (org-invisible-p2))))
  1261. ;; Do not hide fake properties.
  1262. (should-not
  1263. (let ((org-custom-properties '("FOO")))
  1264. (org-test-with-temp-text ":FOO: val\n"
  1265. (org-toggle-custom-properties-visibility)
  1266. (org-invisible-p2))))
  1267. (should-not
  1268. (let ((org-custom-properties '("A")))
  1269. (org-test-with-temp-text
  1270. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  1271. (org-toggle-custom-properties-visibility)
  1272. (org-invisible-p2)))))
  1273. ;;; Mark Region
  1274. (ert-deftest test-org/mark-subtree ()
  1275. "Test `org-mark-subtree' specifications."
  1276. ;; Error when point is before first headline.
  1277. (should-error
  1278. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1279. (progn (transient-mark-mode 1)
  1280. (org-mark-subtree))))
  1281. ;; Without argument, mark current subtree.
  1282. (should
  1283. (equal
  1284. '(12 32)
  1285. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1286. (progn (transient-mark-mode 1)
  1287. (forward-line 2)
  1288. (org-mark-subtree)
  1289. (list (region-beginning) (region-end))))))
  1290. ;; With an argument, move ARG up.
  1291. (should
  1292. (equal
  1293. '(1 32)
  1294. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1295. (progn (transient-mark-mode 1)
  1296. (forward-line 2)
  1297. (org-mark-subtree 1)
  1298. (list (region-beginning) (region-end))))))
  1299. ;; Do not get fooled by inlinetasks.
  1300. (when (featurep 'org-inlinetask)
  1301. (should
  1302. (= 1
  1303. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1304. (progn (transient-mark-mode 1)
  1305. (forward-line 1)
  1306. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1307. (region-beginning)))))))
  1308. ;;; Navigation
  1309. (ert-deftest test-org/beginning-of-line ()
  1310. "Test `org-beginning-of-line' specifications."
  1311. ;; Standard test.
  1312. (should
  1313. (org-test-with-temp-text "Some text\nSome other text"
  1314. (progn (org-beginning-of-line) (bolp))))
  1315. ;; Standard test with `visual-line-mode'.
  1316. (should-not
  1317. (org-test-with-temp-text "A long line of text\nSome other text"
  1318. (progn (visual-line-mode)
  1319. (forward-char 2)
  1320. (dotimes (i 1000) (insert "very "))
  1321. (org-beginning-of-line)
  1322. (bolp))))
  1323. ;; At an headline with special movement.
  1324. (should
  1325. (org-test-with-temp-text "* TODO Headline"
  1326. (let ((org-special-ctrl-a/e t))
  1327. (org-end-of-line)
  1328. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1329. (progn (org-beginning-of-line) (bolp))
  1330. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1331. (ert-deftest test-org/end-of-line ()
  1332. "Test `org-end-of-line' specifications."
  1333. ;; Standard test.
  1334. (should
  1335. (org-test-with-temp-text "Some text\nSome other text"
  1336. (progn (org-end-of-line) (eolp))))
  1337. ;; Standard test with `visual-line-mode'.
  1338. (should-not
  1339. (org-test-with-temp-text "A long line of text\nSome other text"
  1340. (progn (visual-line-mode)
  1341. (forward-char 2)
  1342. (dotimes (i 1000) (insert "very "))
  1343. (goto-char (point-min))
  1344. (org-end-of-line)
  1345. (eolp))))
  1346. ;; At an headline with special movement.
  1347. (should
  1348. (org-test-with-temp-text "* Headline1 :tag:\n"
  1349. (let ((org-special-ctrl-a/e t))
  1350. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1351. (progn (org-end-of-line) (eolp))
  1352. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1353. ;; At an headline without special movement.
  1354. (should
  1355. (org-test-with-temp-text "* Headline2 :tag:\n"
  1356. (let ((org-special-ctrl-a/e nil))
  1357. (and (progn (org-end-of-line) (eolp))
  1358. (progn (org-end-of-line) (eolp))))))
  1359. ;; At an headline, with reversed movement.
  1360. (should
  1361. (org-test-with-temp-text "* Headline3 :tag:\n"
  1362. (let ((org-special-ctrl-a/e 'reversed)
  1363. (this-command last-command))
  1364. (and (progn (org-end-of-line) (eolp))
  1365. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1366. ;; At a block without hidden contents.
  1367. (should
  1368. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1369. (progn (org-end-of-line) (eolp))))
  1370. ;; At a block with hidden contents.
  1371. (should-not
  1372. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1373. (let ((org-special-ctrl-a/e t))
  1374. (org-hide-block-toggle)
  1375. (org-end-of-line)
  1376. (eobp)))))
  1377. (ert-deftest test-org/forward-paragraph ()
  1378. "Test `org-forward-paragraph' specifications."
  1379. ;; At end of buffer, return an error.
  1380. (should-error
  1381. (org-test-with-temp-text "Paragraph"
  1382. (goto-char (point-max))
  1383. (org-forward-paragraph)))
  1384. ;; Standard test.
  1385. (should
  1386. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1387. (org-forward-paragraph)
  1388. (looking-at "P2")))
  1389. ;; Ignore depth.
  1390. (should
  1391. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1392. (org-forward-paragraph)
  1393. (looking-at "P1")))
  1394. ;; Do not enter elements with invisible contents.
  1395. (should
  1396. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1397. (org-hide-block-toggle)
  1398. (org-forward-paragraph)
  1399. (looking-at "P3")))
  1400. ;; On an affiliated keyword, jump to the beginning of the element.
  1401. (should
  1402. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1403. (org-forward-paragraph)
  1404. (looking-at "Para")))
  1405. ;; On an item or a footnote definition, move to the second element
  1406. ;; inside, if any.
  1407. (should
  1408. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1409. (org-forward-paragraph)
  1410. (looking-at " Paragraph")))
  1411. (should
  1412. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1413. (org-forward-paragraph)
  1414. (looking-at "Paragraph")))
  1415. ;; On an item, or a footnote definition, when the first line is
  1416. ;; empty, move to the first item.
  1417. (should
  1418. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1419. (org-forward-paragraph)
  1420. (looking-at " Paragraph")))
  1421. (should
  1422. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1423. (org-forward-paragraph)
  1424. (looking-at "Paragraph")))
  1425. ;; On a table (resp. a property drawer) do not move through table
  1426. ;; rows (resp. node properties).
  1427. (should
  1428. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1429. (org-forward-paragraph)
  1430. (looking-at "Paragraph")))
  1431. (should
  1432. (org-test-with-temp-text
  1433. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1434. (org-forward-paragraph)
  1435. (looking-at "Paragraph")))
  1436. ;; On a verse or source block, stop after blank lines.
  1437. (should
  1438. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1439. (org-forward-paragraph)
  1440. (looking-at "L2")))
  1441. (should
  1442. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1443. (org-forward-paragraph)
  1444. (looking-at "L2"))))
  1445. (ert-deftest test-org/backward-paragraph ()
  1446. "Test `org-backward-paragraph' specifications."
  1447. ;; Error at beginning of buffer.
  1448. (should-error
  1449. (org-test-with-temp-text "Paragraph"
  1450. (org-backward-paragraph)))
  1451. ;; Regular test.
  1452. (should
  1453. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1454. (goto-char (point-max))
  1455. (org-backward-paragraph)
  1456. (looking-at "P3")))
  1457. (should
  1458. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1459. (goto-char (point-max))
  1460. (beginning-of-line)
  1461. (org-backward-paragraph)
  1462. (looking-at "P2")))
  1463. ;; Ignore depth.
  1464. (should
  1465. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1466. (goto-char (point-max))
  1467. (beginning-of-line)
  1468. (org-backward-paragraph)
  1469. (looking-at "P2")))
  1470. ;; Ignore invisible elements.
  1471. (should
  1472. (org-test-with-temp-text "* H1\n P1\n* H2"
  1473. (org-cycle)
  1474. (goto-char (point-max))
  1475. (beginning-of-line)
  1476. (org-backward-paragraph)
  1477. (bobp)))
  1478. ;; On an affiliated keyword, jump to the first one.
  1479. (should
  1480. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1481. (search-forward "c2")
  1482. (org-backward-paragraph)
  1483. (looking-at "#\\+name")))
  1484. ;; On the second element in an item or a footnote definition, jump
  1485. ;; to item or the definition.
  1486. (should
  1487. (org-test-with-temp-text "- line1\n\n line2"
  1488. (goto-char (point-max))
  1489. (beginning-of-line)
  1490. (org-backward-paragraph)
  1491. (looking-at "- line1")))
  1492. (should
  1493. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1494. (goto-char (point-max))
  1495. (beginning-of-line)
  1496. (org-backward-paragraph)
  1497. (looking-at "\\[fn:1\\] line1")))
  1498. ;; On a table (resp. a property drawer), ignore table rows
  1499. ;; (resp. node properties).
  1500. (should
  1501. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1502. (goto-char (point-max))
  1503. (beginning-of-line)
  1504. (org-backward-paragraph)
  1505. (bobp)))
  1506. (should
  1507. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  1508. (org-backward-paragraph)
  1509. (looking-at ":PROPERTIES:")))
  1510. ;; On a source or verse block, stop before blank lines.
  1511. (should
  1512. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1513. (search-forward "L3")
  1514. (beginning-of-line)
  1515. (org-backward-paragraph)
  1516. (looking-at "L2")))
  1517. (should
  1518. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1519. (search-forward "L3")
  1520. (beginning-of-line)
  1521. (org-backward-paragraph)
  1522. (looking-at "L2"))))
  1523. (ert-deftest test-org/forward-element ()
  1524. "Test `org-forward-element' specifications."
  1525. ;; 1. At EOB: should error.
  1526. (org-test-with-temp-text "Some text\n"
  1527. (goto-char (point-max))
  1528. (should-error (org-forward-element)))
  1529. ;; 2. Standard move: expected to ignore blank lines.
  1530. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1531. (org-forward-element)
  1532. (should (looking-at (regexp-quote "Second paragraph."))))
  1533. ;; 3. Headline tests.
  1534. (org-test-with-temp-text "
  1535. * Head 1
  1536. ** Head 1.1
  1537. *** Head 1.1.1
  1538. ** Head 1.2"
  1539. ;; 3.1. At an headline beginning: move to next headline at the
  1540. ;; same level.
  1541. (goto-line 3)
  1542. (org-forward-element)
  1543. (should (looking-at (regexp-quote "** Head 1.2")))
  1544. ;; 3.2. At an headline beginning: move to parent headline if no
  1545. ;; headline at the same level.
  1546. (goto-line 3)
  1547. (org-forward-element)
  1548. (should (looking-at (regexp-quote "** Head 1.2"))))
  1549. ;; 4. Greater element tests.
  1550. (org-test-with-temp-text
  1551. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1552. ;; 4.1. At a greater element: expected to skip contents.
  1553. (org-forward-element)
  1554. (should (looking-at (regexp-quote "Outside.")))
  1555. ;; 4.2. At the end of greater element contents: expected to skip
  1556. ;; to the end of the greater element.
  1557. (goto-line 2)
  1558. (org-forward-element)
  1559. (should (looking-at (regexp-quote "Outside."))))
  1560. ;; 5. List tests.
  1561. (org-test-with-temp-text "
  1562. - item1
  1563. - sub1
  1564. - sub2
  1565. - sub3
  1566. Inner paragraph.
  1567. - item2
  1568. Outside."
  1569. ;; 5.1. At list top point: expected to move to the element after
  1570. ;; the list.
  1571. (goto-line 2)
  1572. (org-forward-element)
  1573. (should (looking-at (regexp-quote "Outside.")))
  1574. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1575. ;; beginning of line, move to next item.
  1576. (goto-line 2)
  1577. (forward-char)
  1578. (org-forward-element)
  1579. (should (looking-at "- item2"))
  1580. (goto-line 4)
  1581. (forward-char)
  1582. (org-forward-element)
  1583. (should (looking-at " - sub2"))
  1584. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1585. (goto-line 4)
  1586. (org-forward-element)
  1587. (should (looking-at (regexp-quote " Inner paragraph.")))
  1588. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1589. (goto-line 8)
  1590. (org-forward-element)
  1591. (should (looking-at (regexp-quote " Inner paragraph.")))
  1592. ;; 5.5. At an item: expected to move to next item, if any.
  1593. (goto-line 6)
  1594. (org-forward-element)
  1595. (should (looking-at " - sub3"))))
  1596. (ert-deftest test-org/backward-element ()
  1597. "Test `org-backward-element' specifications."
  1598. ;; 1. Should error at BOB.
  1599. (org-test-with-temp-text " \nParagraph."
  1600. (should-error (org-backward-element)))
  1601. ;; 2. Should move at BOB when called on the first element in buffer.
  1602. (should
  1603. (org-test-with-temp-text "\n#+TITLE: test"
  1604. (progn (forward-line)
  1605. (org-backward-element)
  1606. (bobp))))
  1607. ;; 3. Not at the beginning of an element: move at its beginning.
  1608. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1609. (goto-line 3)
  1610. (end-of-line)
  1611. (org-backward-element)
  1612. (should (looking-at (regexp-quote "Paragraph2."))))
  1613. ;; 4. Headline tests.
  1614. (org-test-with-temp-text "
  1615. * Head 1
  1616. ** Head 1.1
  1617. *** Head 1.1.1
  1618. ** Head 1.2"
  1619. ;; 4.1. At an headline beginning: move to previous headline at the
  1620. ;; same level.
  1621. (goto-line 5)
  1622. (org-backward-element)
  1623. (should (looking-at (regexp-quote "** Head 1.1")))
  1624. ;; 4.2. At an headline beginning: move to parent headline if no
  1625. ;; headline at the same level.
  1626. (goto-line 3)
  1627. (org-backward-element)
  1628. (should (looking-at (regexp-quote "* Head 1")))
  1629. ;; 4.3. At the first top-level headline: should error.
  1630. (goto-line 2)
  1631. (should-error (org-backward-element)))
  1632. ;; 5. At beginning of first element inside a greater element:
  1633. ;; expected to move to greater element's beginning.
  1634. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1635. (goto-line 3)
  1636. (org-backward-element)
  1637. (should (looking-at "#\\+BEGIN_CENTER")))
  1638. ;; 6. At the beginning of the first element in a section: should
  1639. ;; move back to headline, if any.
  1640. (should
  1641. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1642. (progn (goto-char (point-max))
  1643. (beginning-of-line)
  1644. (org-backward-element)
  1645. (org-at-heading-p))))
  1646. ;; 7. List tests.
  1647. (org-test-with-temp-text "
  1648. - item1
  1649. - sub1
  1650. - sub2
  1651. - sub3
  1652. Inner paragraph.
  1653. - item2
  1654. Outside."
  1655. ;; 7.1. At beginning of sub-list: expected to move to the
  1656. ;; paragraph before it.
  1657. (goto-line 4)
  1658. (org-backward-element)
  1659. (should (looking-at "item1"))
  1660. ;; 7.2. At an item in a list: expected to move at previous item.
  1661. (goto-line 8)
  1662. (org-backward-element)
  1663. (should (looking-at " - sub2"))
  1664. (goto-line 12)
  1665. (org-backward-element)
  1666. (should (looking-at "- item1"))
  1667. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1668. ;; beginning.
  1669. (goto-line 10)
  1670. (org-backward-element)
  1671. (should (looking-at " - sub1"))
  1672. (goto-line 15)
  1673. (org-backward-element)
  1674. (should (looking-at "- item1"))
  1675. ;; 7.4. At blank-lines before list end: expected to move to top
  1676. ;; item.
  1677. (goto-line 14)
  1678. (org-backward-element)
  1679. (should (looking-at "- item1"))))
  1680. (ert-deftest test-org/up-element ()
  1681. "Test `org-up-element' specifications."
  1682. ;; 1. At BOB or with no surrounding element: should error.
  1683. (org-test-with-temp-text "Paragraph."
  1684. (should-error (org-up-element)))
  1685. (org-test-with-temp-text "* Head1\n* Head2"
  1686. (goto-line 2)
  1687. (should-error (org-up-element)))
  1688. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1689. (goto-line 3)
  1690. (should-error (org-up-element)))
  1691. ;; 2. At an headline: move to parent headline.
  1692. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1693. (goto-line 3)
  1694. (org-up-element)
  1695. (should (looking-at "\\* Head1")))
  1696. ;; 3. Inside a greater element: move to greater element beginning.
  1697. (org-test-with-temp-text
  1698. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1699. (goto-line 3)
  1700. (org-up-element)
  1701. (should (looking-at "#\\+BEGIN_CENTER")))
  1702. ;; 4. List tests.
  1703. (org-test-with-temp-text "* Top
  1704. - item1
  1705. - sub1
  1706. - sub2
  1707. Paragraph within sub2.
  1708. - item2"
  1709. ;; 4.1. Within an item: move to the item beginning.
  1710. (goto-line 8)
  1711. (org-up-element)
  1712. (should (looking-at " - sub2"))
  1713. ;; 4.2. At an item in a sub-list: move to parent item.
  1714. (goto-line 4)
  1715. (org-up-element)
  1716. (should (looking-at "- item1"))
  1717. ;; 4.3. At an item in top list: move to beginning of whole list.
  1718. (goto-line 10)
  1719. (org-up-element)
  1720. (should (looking-at "- item1"))
  1721. ;; 4.4. Special case. At very top point: should move to parent of
  1722. ;; list.
  1723. (goto-line 2)
  1724. (org-up-element)
  1725. (should (looking-at "\\* Top"))))
  1726. (ert-deftest test-org/down-element ()
  1727. "Test `org-down-element' specifications."
  1728. ;; Error when the element hasn't got a recursive type.
  1729. (org-test-with-temp-text "Paragraph."
  1730. (should-error (org-down-element)))
  1731. ;; Error when the element has no contents
  1732. (org-test-with-temp-text "* Headline"
  1733. (should-error (org-down-element)))
  1734. ;; When at a plain-list, move to first item.
  1735. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1736. (goto-line 2)
  1737. (org-down-element)
  1738. (should (looking-at " - Item 1.1")))
  1739. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1740. (org-down-element)
  1741. (should (looking-at " Item 1")))
  1742. ;; When at a table, move to first row
  1743. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1744. (org-down-element)
  1745. (should (looking-at " a | b |")))
  1746. ;; Otherwise, move inside the greater element.
  1747. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1748. (org-down-element)
  1749. (should (looking-at "Paragraph"))))
  1750. (ert-deftest test-org/drag-element-backward ()
  1751. "Test `org-drag-element-backward' specifications."
  1752. ;; Standard test.
  1753. (should
  1754. (equal
  1755. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  1756. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  1757. (org-drag-element-backward)
  1758. (buffer-string))))
  1759. (should
  1760. (equal
  1761. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  1762. (org-test-with-temp-text
  1763. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  1764. (org-drag-element-backward)
  1765. (buffer-string))))
  1766. ;; Preserve blank lines.
  1767. (should
  1768. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  1769. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  1770. (org-drag-element-backward)
  1771. (buffer-string))))
  1772. ;; Preserve column.
  1773. (should
  1774. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  1775. (org-drag-element-backward)
  1776. (org-looking-at-p "2")))
  1777. ;; Error when trying to move first element of buffer.
  1778. (should-error
  1779. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1780. (org-drag-element-backward)))
  1781. ;; Error when trying to swap nested elements.
  1782. (should-error
  1783. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1784. (forward-line)
  1785. (org-drag-element-backward)))
  1786. ;; Error when trying to swap an headline element and a non-headline
  1787. ;; element.
  1788. (should-error
  1789. (org-test-with-temp-text "Test.\n* Head 1"
  1790. (forward-line)
  1791. (org-drag-element-backward)))
  1792. ;; Preserve visibility of elements and their contents.
  1793. (should
  1794. (equal '((63 . 82) (26 . 48))
  1795. (org-test-with-temp-text "
  1796. #+BEGIN_CENTER
  1797. Text.
  1798. #+END_CENTER
  1799. - item 1
  1800. #+BEGIN_QUOTE
  1801. Text.
  1802. #+END_QUOTE"
  1803. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1804. (search-backward "- item 1")
  1805. (org-drag-element-backward)
  1806. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1807. (overlays-in (point-min) (point-max)))))))
  1808. (ert-deftest test-org/drag-element-forward ()
  1809. "Test `org-drag-element-forward' specifications."
  1810. ;; 1. Error when trying to move first element of buffer.
  1811. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1812. (goto-line 3)
  1813. (should-error (org-drag-element-forward)))
  1814. ;; 2. Error when trying to swap nested elements.
  1815. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1816. (forward-line)
  1817. (should-error (org-drag-element-forward)))
  1818. ;; 3. Error when trying to swap a non-headline element and an
  1819. ;; headline.
  1820. (org-test-with-temp-text "Test.\n* Head 1"
  1821. (should-error (org-drag-element-forward)))
  1822. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1823. ;; between elements.
  1824. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1825. (search-forward "graph")
  1826. (org-drag-element-forward)
  1827. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1828. (should (looking-at " 1")))
  1829. ;; 5. Preserve visibility of elements and their contents.
  1830. (org-test-with-temp-text "
  1831. #+BEGIN_CENTER
  1832. Text.
  1833. #+END_CENTER
  1834. - item 1
  1835. #+BEGIN_QUOTE
  1836. Text.
  1837. #+END_QUOTE"
  1838. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1839. (search-backward "#+BEGIN_CENTER")
  1840. (org-drag-element-forward)
  1841. (should
  1842. (equal
  1843. '((63 . 82) (26 . 48))
  1844. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1845. (overlays-in (point-min) (point-max)))))))
  1846. ;;; Outline structure
  1847. (ert-deftest test-org/demote ()
  1848. "Test `org-demote' specifications."
  1849. ;; Add correct number of stars according to `org-odd-levels-only'.
  1850. (should
  1851. (= 2
  1852. (org-test-with-temp-text "* H"
  1853. (let ((org-odd-levels-only nil)) (org-demote))
  1854. (org-current-level))))
  1855. (should
  1856. (= 3
  1857. (org-test-with-temp-text "* H"
  1858. (let ((org-odd-levels-only t)) (org-demote))
  1859. (org-current-level))))
  1860. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  1861. (should
  1862. (org-test-with-temp-text "* H :tag:"
  1863. (let ((org-tags-column 10)
  1864. (org-auto-align-tags t)
  1865. (org-odd-levels-only nil))
  1866. (org-demote))
  1867. (org-move-to-column 10)
  1868. (org-looking-at-p ":tag:$")))
  1869. (should-not
  1870. (org-test-with-temp-text "* H :tag:"
  1871. (let ((org-tags-column 10)
  1872. (org-auto-align-tags nil)
  1873. (org-odd-levels-only nil))
  1874. (org-demote))
  1875. (org-move-to-column 10)
  1876. (org-looking-at-p ":tag:$")))
  1877. ;; When `org-adapt-indentation' is non-nil, always indent planning
  1878. ;; info and property drawers accordingly.
  1879. (should
  1880. (= 3
  1881. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  1882. (let ((org-odd-levels-only nil)
  1883. (org-adapt-indentation t))
  1884. (org-demote))
  1885. (forward-line)
  1886. (org-get-indentation))))
  1887. (should
  1888. (= 3
  1889. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  1890. (let ((org-odd-levels-only nil)
  1891. (org-adapt-indentation t))
  1892. (org-demote))
  1893. (forward-line)
  1894. (org-get-indentation))))
  1895. (should-not
  1896. (= 3
  1897. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  1898. (let ((org-odd-levels-only nil)
  1899. (org-adapt-indentation nil))
  1900. (org-demote))
  1901. (forward-line)
  1902. (org-get-indentation))))
  1903. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  1904. ;; section accordingly. Ignore, however, footnote definitions and
  1905. ;; inlinetasks boundaries.
  1906. (should
  1907. (= 3
  1908. (org-test-with-temp-text "* H\n Paragraph"
  1909. (let ((org-odd-levels-only nil)
  1910. (org-adapt-indentation t))
  1911. (org-demote))
  1912. (forward-line)
  1913. (org-get-indentation))))
  1914. (should
  1915. (= 2
  1916. (org-test-with-temp-text "* H\n Paragraph"
  1917. (let ((org-odd-levels-only nil)
  1918. (org-adapt-indentation nil))
  1919. (org-demote))
  1920. (forward-line)
  1921. (org-get-indentation))))
  1922. (should
  1923. (zerop
  1924. (org-test-with-temp-text "* H\n[fn:1] Definition."
  1925. (let ((org-odd-levels-only nil)
  1926. (org-adapt-indentation t))
  1927. (org-demote))
  1928. (forward-line)
  1929. (org-get-indentation))))
  1930. (should
  1931. (= 3
  1932. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  1933. (let ((org-odd-levels-only nil)
  1934. (org-adapt-indentation t))
  1935. (org-demote))
  1936. (goto-char (point-max))
  1937. (org-get-indentation))))
  1938. (when (featurep 'org-inlinetask)
  1939. (should
  1940. (zerop
  1941. (let ((org-inlinetask-min-level 5)
  1942. (org-adapt-indentation t))
  1943. (org-test-with-temp-text "* H\n***** I\n***** END"
  1944. (org-demote)
  1945. (forward-line)
  1946. (org-get-indentation))))))
  1947. (when (featurep 'org-inlinetask)
  1948. (should
  1949. (= 3
  1950. (let ((org-inlinetask-min-level 5)
  1951. (org-adapt-indentation t))
  1952. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  1953. (org-demote)
  1954. (forward-line 2)
  1955. (org-get-indentation)))))))
  1956. (ert-deftest test-org/promote ()
  1957. "Test `org-promote' specifications."
  1958. ;; Return an error if headline is to be promoted to level 0, unless
  1959. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  1960. ;; headline becomes a comment.
  1961. (should-error
  1962. (org-test-with-temp-text "* H"
  1963. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  1964. (should
  1965. (equal "# H"
  1966. (org-test-with-temp-text "* H"
  1967. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  1968. (buffer-string))))
  1969. ;; Remove correct number of stars according to
  1970. ;; `org-odd-levels-only'.
  1971. (should
  1972. (= 2
  1973. (org-test-with-temp-text "*** H"
  1974. (let ((org-odd-levels-only nil)) (org-promote))
  1975. (org-current-level))))
  1976. (should
  1977. (= 1
  1978. (org-test-with-temp-text "*** H"
  1979. (let ((org-odd-levels-only t)) (org-promote))
  1980. (org-current-level))))
  1981. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  1982. (should
  1983. (org-test-with-temp-text "** H :tag:"
  1984. (let ((org-tags-column 10)
  1985. (org-auto-align-tags t)
  1986. (org-odd-levels-only nil))
  1987. (org-promote))
  1988. (org-move-to-column 10)
  1989. (org-looking-at-p ":tag:$")))
  1990. (should-not
  1991. (org-test-with-temp-text "** H :tag:"
  1992. (let ((org-tags-column 10)
  1993. (org-auto-align-tags nil)
  1994. (org-odd-levels-only nil))
  1995. (org-promote))
  1996. (org-move-to-column 10)
  1997. (org-looking-at-p ":tag:$")))
  1998. ;; When `org-adapt-indentation' is non-nil, always indent planning
  1999. ;; info and property drawers.
  2000. (should
  2001. (= 2
  2002. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2003. (let ((org-odd-levels-only nil)
  2004. (org-adapt-indentation t))
  2005. (org-promote))
  2006. (forward-line)
  2007. (org-get-indentation))))
  2008. (should
  2009. (= 2
  2010. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2011. (let ((org-odd-levels-only nil)
  2012. (org-adapt-indentation t))
  2013. (org-promote))
  2014. (forward-line)
  2015. (org-get-indentation))))
  2016. (should-not
  2017. (= 2
  2018. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2019. (let ((org-odd-levels-only nil)
  2020. (org-adapt-indentation nil))
  2021. (org-promote))
  2022. (forward-line)
  2023. (org-get-indentation))))
  2024. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2025. ;; section accordingly. Ignore, however, footnote definitions and
  2026. ;; inlinetasks boundaries.
  2027. (should
  2028. (= 2
  2029. (org-test-with-temp-text "** H\n Paragraph"
  2030. (let ((org-odd-levels-only nil)
  2031. (org-adapt-indentation t))
  2032. (org-promote))
  2033. (forward-line)
  2034. (org-get-indentation))))
  2035. (should-not
  2036. (= 2
  2037. (org-test-with-temp-text "** H\n Paragraph"
  2038. (let ((org-odd-levels-only nil)
  2039. (org-adapt-indentation nil))
  2040. (org-promote))
  2041. (forward-line)
  2042. (org-get-indentation))))
  2043. (should
  2044. (= 2
  2045. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] Definition."
  2046. (let ((org-odd-levels-only nil)
  2047. (org-adapt-indentation t))
  2048. (org-promote))
  2049. (forward-line)
  2050. (org-get-indentation))))
  2051. (when (featurep 'org-inlinetask)
  2052. (should
  2053. (zerop
  2054. (let ((org-inlinetask-min-level 5)
  2055. (org-adapt-indentation t))
  2056. (org-test-with-temp-text "** H\n***** I\n***** END"
  2057. (org-promote)
  2058. (forward-line)
  2059. (org-get-indentation))))))
  2060. (when (featurep 'org-inlinetask)
  2061. (should
  2062. (= 2
  2063. (let ((org-inlinetask-min-level 5)
  2064. (org-adapt-indentation t))
  2065. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  2066. (org-promote)
  2067. (forward-line 2)
  2068. (org-get-indentation))))))
  2069. ;; Give up shifting if it would break document's structure
  2070. ;; otherwise.
  2071. (should
  2072. (= 3
  2073. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  2074. (let ((org-odd-levels-only nil)
  2075. (org-adapt-indentation t))
  2076. (org-promote))
  2077. (forward-line)
  2078. (org-get-indentation))))
  2079. (should
  2080. (= 3
  2081. (org-test-with-temp-text "** H\n Paragraph\n * list."
  2082. (let ((org-odd-levels-only nil)
  2083. (org-adapt-indentation t))
  2084. (org-promote))
  2085. (forward-line)
  2086. (org-get-indentation)))))
  2087. ;;; Planning
  2088. (ert-deftest test-org/timestamp-has-time-p ()
  2089. "Test `org-timestamp-has-time-p' specifications."
  2090. ;; With time.
  2091. (should
  2092. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  2093. (org-timestamp-has-time-p (org-element-context))))
  2094. ;; Without time.
  2095. (should-not
  2096. (org-test-with-temp-text "<2012-03-29 Thu>"
  2097. (org-timestamp-has-time-p (org-element-context)))))
  2098. (ert-deftest test-org/timestamp-format ()
  2099. "Test `org-timestamp-format' specifications."
  2100. ;; Regular test.
  2101. (should
  2102. (equal
  2103. "2012-03-29 16:40"
  2104. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  2105. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  2106. ;; Range end.
  2107. (should
  2108. (equal
  2109. "2012-03-29"
  2110. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  2111. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  2112. (ert-deftest test-org/timestamp-split-range ()
  2113. "Test `org-timestamp-split-range' specifications."
  2114. ;; Extract range start (active).
  2115. (should
  2116. (equal '(2012 3 29)
  2117. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2118. (let ((ts (org-timestamp-split-range (org-element-context))))
  2119. (mapcar (lambda (p) (org-element-property p ts))
  2120. '(:year-end :month-end :day-end))))))
  2121. ;; Extract range start (inactive)
  2122. (should
  2123. (equal '(2012 3 29)
  2124. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  2125. (let ((ts (org-timestamp-split-range (org-element-context))))
  2126. (mapcar (lambda (p) (org-element-property p ts))
  2127. '(:year-end :month-end :day-end))))))
  2128. ;; Extract range end (active).
  2129. (should
  2130. (equal '(2012 3 30)
  2131. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2132. (let ((ts (org-timestamp-split-range
  2133. (org-element-context) t)))
  2134. (mapcar (lambda (p) (org-element-property p ts))
  2135. '(:year-end :month-end :day-end))))))
  2136. ;; Extract range end (inactive)
  2137. (should
  2138. (equal '(2012 3 30)
  2139. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  2140. (let ((ts (org-timestamp-split-range
  2141. (org-element-context) t)))
  2142. (mapcar (lambda (p) (org-element-property p ts))
  2143. '(:year-end :month-end :day-end))))))
  2144. ;; Return the timestamp if not a range.
  2145. (should
  2146. (org-test-with-temp-text "[2012-03-29 Thu]"
  2147. (let* ((ts-orig (org-element-context))
  2148. (ts-copy (org-timestamp-split-range ts-orig)))
  2149. (eq ts-orig ts-copy))))
  2150. (should
  2151. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  2152. (let* ((ts-orig (org-element-context))
  2153. (ts-copy (org-timestamp-split-range ts-orig)))
  2154. (eq ts-orig ts-copy))))
  2155. ;; Check that parent is the same when a range was split.
  2156. (should
  2157. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  2158. (let* ((ts-orig (org-element-context))
  2159. (ts-copy (org-timestamp-split-range ts-orig)))
  2160. (eq (org-element-property :parent ts-orig)
  2161. (org-element-property :parent ts-copy))))))
  2162. (ert-deftest test-org/timestamp-translate ()
  2163. "Test `org-timestamp-translate' specifications."
  2164. ;; Translate whole date range.
  2165. (should
  2166. (equal "<29>--<30>"
  2167. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2168. (let ((org-display-custom-times t)
  2169. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2170. (org-timestamp-translate (org-element-context))))))
  2171. ;; Translate date range start.
  2172. (should
  2173. (equal "<29>"
  2174. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2175. (let ((org-display-custom-times t)
  2176. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2177. (org-timestamp-translate (org-element-context) 'start)))))
  2178. ;; Translate date range end.
  2179. (should
  2180. (equal "<30>"
  2181. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  2182. (let ((org-display-custom-times t)
  2183. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2184. (org-timestamp-translate (org-element-context) 'end)))))
  2185. ;; Translate time range.
  2186. (should
  2187. (equal "<08>--<16>"
  2188. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  2189. (let ((org-display-custom-times t)
  2190. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  2191. (org-timestamp-translate (org-element-context))))))
  2192. ;; Translate non-range timestamp.
  2193. (should
  2194. (equal "<29>"
  2195. (org-test-with-temp-text "<2012-03-29 Thu>"
  2196. (let ((org-display-custom-times t)
  2197. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2198. (org-timestamp-translate (org-element-context))))))
  2199. ;; Do not change `diary' timestamps.
  2200. (should
  2201. (equal "<%%(org-float t 4 2)>"
  2202. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  2203. (let ((org-display-custom-times t)
  2204. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  2205. (org-timestamp-translate (org-element-context)))))))
  2206. ;;; Property API
  2207. (ert-deftest test-org/buffer-property-keys ()
  2208. "Test `org-buffer-property-keys' specifications."
  2209. ;; Retrieve properties accross siblings.
  2210. (should
  2211. (equal '("A" "B")
  2212. (org-test-with-temp-text "
  2213. * H1
  2214. :PROPERTIES:
  2215. :A: 1
  2216. :END:
  2217. * H2
  2218. :PROPERTIES:
  2219. :B: 1
  2220. :END:"
  2221. (org-buffer-property-keys))))
  2222. ;; Retrieve properties accross children.
  2223. (should
  2224. (equal '("A" "B")
  2225. (org-test-with-temp-text "
  2226. * H1
  2227. :PROPERTIES:
  2228. :A: 1
  2229. :END:
  2230. ** H2
  2231. :PROPERTIES:
  2232. :B: 1
  2233. :END:"
  2234. (org-buffer-property-keys))))
  2235. ;; Retrieve muliple properties in the same drawer.
  2236. (should
  2237. (equal '("A" "B")
  2238. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2239. (org-buffer-property-keys))))
  2240. ;; Ignore extension symbol in property name.
  2241. (should
  2242. (equal '("A")
  2243. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  2244. (org-buffer-property-keys))))
  2245. ;; With non-nil COLUMNS, extract property names from columns.
  2246. (should
  2247. (equal '("A" "B")
  2248. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  2249. (org-buffer-property-keys nil nil t))))
  2250. (should
  2251. (equal '("A" "B" "COLUMNS")
  2252. (org-test-with-temp-text
  2253. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  2254. (org-buffer-property-keys nil nil t)))))
  2255. (ert-deftest test-org/property-values ()
  2256. "Test `org-property-values' specifications."
  2257. ;; Regular test.
  2258. (should
  2259. (equal '("2" "1")
  2260. (org-test-with-temp-text
  2261. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  2262. (org-property-values "A"))))
  2263. ;; Ignore empty values.
  2264. (should-not
  2265. (org-test-with-temp-text
  2266. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  2267. (org-property-values "A")))
  2268. ;; Take into consideration extended values.
  2269. (should
  2270. (equal '("1 2")
  2271. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  2272. (org-property-values "A")))))
  2273. (ert-deftest test-org/entry-delete ()
  2274. "Test `org-entry-delete' specifications."
  2275. ;; Regular test.
  2276. (should
  2277. (string-match
  2278. " *:PROPERTIES:\n *:B: +2\n *:END:"
  2279. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2280. (org-entry-delete (point) "A")
  2281. (buffer-string))))
  2282. ;; When last property is removed, remove the property drawer.
  2283. (should-not
  2284. (string-match
  2285. ":PROPERTIES:"
  2286. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  2287. (org-entry-delete (point) "A")
  2288. (buffer-string)))))
  2289. (ert-deftest test-org/entry-get ()
  2290. "Test `org-entry-get' specifications."
  2291. ;; Regular test.
  2292. (should
  2293. (equal "1"
  2294. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2295. (org-entry-get (point) "A"))))
  2296. ;; Ignore case.
  2297. (should
  2298. (equal "1"
  2299. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2300. (org-entry-get (point) "a"))))
  2301. ;; Handle extended values, both before and after base value.
  2302. (should
  2303. (equal "1 2 3"
  2304. (org-test-with-temp-text
  2305. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2306. (org-entry-get (point) "A"))))
  2307. ;; Empty values are returned as the empty string.
  2308. (should
  2309. (equal ""
  2310. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  2311. (org-entry-get (point) "A"))))
  2312. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  2313. ;; otherwise, return nil.
  2314. (should-not
  2315. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2316. (org-entry-get (point) "A")))
  2317. (should
  2318. (equal "nil"
  2319. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2320. (org-entry-get (point) "A" nil t))))
  2321. ;; Return nil when no property is found, independently on the
  2322. ;; LITERAL-NIL argument.
  2323. (should-not
  2324. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2325. (org-entry-get (point) "B")))
  2326. (should-not
  2327. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2328. (org-entry-get (point) "B" nil t)))
  2329. ;; Handle inheritance, when allowed.
  2330. (should
  2331. (equal
  2332. "1"
  2333. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2334. (org-entry-get (point) "A" t))))
  2335. (should
  2336. (equal
  2337. "1"
  2338. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2339. (let ((org-use-property-inheritance t))
  2340. (org-entry-get (point) "A" 'selective)))))
  2341. (should-not
  2342. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2343. (let ((org-use-property-inheritance nil))
  2344. (org-entry-get (point) "A" 'selective)))))
  2345. (ert-deftest test-org/entry-properties ()
  2346. "Test `org-entry-properties' specifications."
  2347. ;; Get "ITEM" property.
  2348. (should
  2349. (equal "* H"
  2350. (org-test-with-temp-text "* TODO H"
  2351. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  2352. ;; Get "TODO" property.
  2353. (should
  2354. (equal "TODO"
  2355. (org-test-with-temp-text "* TODO H"
  2356. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  2357. (should-not
  2358. (org-test-with-temp-text "* H"
  2359. (assoc "TODO" (org-entry-properties nil "TODO"))))
  2360. ;; Get "PRIORITY" property.
  2361. (should
  2362. (equal "A"
  2363. (org-test-with-temp-text "* [#A] H"
  2364. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  2365. (should-not
  2366. (org-test-with-temp-text "* H"
  2367. (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
  2368. ;; Get "FILE" property.
  2369. (should
  2370. (org-test-with-temp-text-in-file "* H\nParagraph"
  2371. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  2372. (buffer-file-name))))
  2373. (should-not
  2374. (org-test-with-temp-text "* H\nParagraph"
  2375. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  2376. ;; Get "TAGS" property.
  2377. (should
  2378. (equal ":tag1:tag2:"
  2379. (org-test-with-temp-text "* H :tag1:tag2:"
  2380. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  2381. (should-not
  2382. (org-test-with-temp-text "* H"
  2383. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  2384. ;; Get "ALLTAGS" property.
  2385. (should
  2386. (equal ":tag1:tag2:"
  2387. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  2388. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  2389. (should-not
  2390. (org-test-with-temp-text "* H"
  2391. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  2392. ;; Get "BLOCKED" property.
  2393. (should
  2394. (equal "t"
  2395. (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
  2396. (let ((org-enforce-todo-dependencies t)
  2397. (org-blocker-hook
  2398. '(org-block-todo-from-children-or-siblings-or-parent)))
  2399. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2400. (should
  2401. (equal ""
  2402. (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
  2403. (let ((org-enforce-todo-dependencies t))
  2404. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2405. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  2406. (should
  2407. (equal
  2408. "[2012-03-29 thu.]"
  2409. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  2410. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  2411. (should-not
  2412. (org-test-with-temp-text "* H"
  2413. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  2414. (should
  2415. (equal
  2416. "<2014-03-04 tue.>"
  2417. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2418. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  2419. (should-not
  2420. (org-test-with-temp-text "* H"
  2421. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  2422. (should
  2423. (equal
  2424. "<2014-03-04 tue.>"
  2425. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2426. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  2427. (should-not
  2428. (org-test-with-temp-text "* H"
  2429. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  2430. ;; Get "CATEGORY"
  2431. (should
  2432. (equal "cat"
  2433. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  2434. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2435. (should
  2436. (equal "cat"
  2437. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  2438. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2439. ;; Get standard properties.
  2440. (should
  2441. (equal "1"
  2442. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2443. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2444. ;; Handle extended properties.
  2445. (should
  2446. (equal "1 2 3"
  2447. (org-test-with-temp-text
  2448. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2449. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2450. (should
  2451. (equal "1 2 3"
  2452. (org-test-with-temp-text
  2453. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  2454. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2455. ;; Ignore forbidden (special) properties.
  2456. (should-not
  2457. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  2458. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  2459. (ert-deftest test-org/entry-put ()
  2460. "Test `org-entry-put' specifications."
  2461. ;; Error when not a string or nil.
  2462. (should-error
  2463. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  2464. (org-entry-put 1 "test" 2)))
  2465. ;; Set "TODO" property.
  2466. (should
  2467. (string-match (regexp-quote " TODO H")
  2468. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2469. (org-entry-put (point) "TODO" "TODO")
  2470. (buffer-string))))
  2471. (should
  2472. (string-match (regexp-quote "* H")
  2473. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2474. (org-entry-put (point) "TODO" nil)
  2475. (buffer-string))))
  2476. ;; Set "PRIORITY" property.
  2477. (should
  2478. (equal "* [#A] H"
  2479. (org-test-with-temp-text "* [#B] H"
  2480. (org-entry-put (point) "PRIORITY" "A")
  2481. (buffer-string))))
  2482. (should
  2483. (equal "* H"
  2484. (org-test-with-temp-text "* [#B] H"
  2485. (org-entry-put (point) "PRIORITY" nil)
  2486. (buffer-string))))
  2487. ;; Set "SCHEDULED" property.
  2488. (should
  2489. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  2490. (org-test-with-temp-text "* H"
  2491. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  2492. (buffer-string))))
  2493. (should
  2494. (string= "* H\n"
  2495. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2496. (org-entry-put (point) "SCHEDULED" nil)
  2497. (buffer-string))))
  2498. (should
  2499. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  2500. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2501. (org-entry-put (point) "SCHEDULED" "earlier")
  2502. (buffer-string))))
  2503. (should
  2504. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  2505. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2506. (org-entry-put (point) "SCHEDULED" "later")
  2507. (buffer-string))))
  2508. ;; Set "DEADLINE" property.
  2509. (should
  2510. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  2511. (org-test-with-temp-text "* H"
  2512. (org-entry-put (point) "DEADLINE" "2014-03-04")
  2513. (buffer-string))))
  2514. (should
  2515. (string= "* H\n"
  2516. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2517. (org-entry-put (point) "DEADLINE" nil)
  2518. (buffer-string))))
  2519. (should
  2520. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  2521. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2522. (org-entry-put (point) "DEADLINE" "earlier")
  2523. (buffer-string))))
  2524. (should
  2525. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  2526. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2527. (org-entry-put (point) "DEADLINE" "later")
  2528. (buffer-string))))
  2529. ;; Regular properties, with or without pre-existing drawer.
  2530. (should
  2531. (string-match "^ *:A: +2$"
  2532. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2533. (org-entry-put (point) "A" "2")
  2534. (buffer-string))))
  2535. (should
  2536. (string-match "^ *:A: +1$"
  2537. (org-test-with-temp-text "* H"
  2538. (org-entry-put (point) "A" "1")
  2539. (buffer-string))))
  2540. ;; Special case: two consecutive headlines.
  2541. (should
  2542. (string-match "\\* A\n *:PROPERTIES:"
  2543. (org-test-with-temp-text "* A\n** B"
  2544. (org-entry-put (point) "A" "1")
  2545. (buffer-string)))))
  2546. ;;; Radio Targets
  2547. (ert-deftest test-org/update-radio-target-regexp ()
  2548. "Test `org-update-radio-target-regexp' specifications."
  2549. ;; Properly update cache with no previous radio target regexp.
  2550. (should
  2551. (eq 'link
  2552. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2553. (save-excursion (goto-char (point-max)) (org-element-context))
  2554. (insert "<<<")
  2555. (search-forward "o")
  2556. (insert ">>>")
  2557. (org-update-radio-target-regexp)
  2558. (goto-char (point-max))
  2559. (org-element-type (org-element-context)))))
  2560. ;; Properly update cache with previous radio target regexp.
  2561. (should
  2562. (eq 'link
  2563. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2564. (save-excursion (goto-char (point-max)) (org-element-context))
  2565. (insert "<<<")
  2566. (search-forward "o")
  2567. (insert ">>>")
  2568. (org-update-radio-target-regexp)
  2569. (search-backward "r")
  2570. (delete-char 5)
  2571. (insert "new")
  2572. (org-update-radio-target-regexp)
  2573. (goto-char (point-max))
  2574. (delete-region (line-beginning-position) (point))
  2575. (insert "new")
  2576. (org-element-type (org-element-context))))))
  2577. ;;; Visibility
  2578. (ert-deftest test-org/flag-drawer ()
  2579. "Test `org-flag-drawer' specifications."
  2580. ;; Hide drawer.
  2581. (should
  2582. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2583. (org-flag-drawer t)
  2584. (get-char-property (line-end-position) 'invisible)))
  2585. ;; Show drawer.
  2586. (should-not
  2587. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2588. (org-flag-drawer t)
  2589. (org-flag-drawer nil)
  2590. (get-char-property (line-end-position) 'invisible)))
  2591. ;; Test optional argument.
  2592. (should
  2593. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2594. (let ((drawer (save-excursion (search-forward ":D2")
  2595. (org-element-at-point))))
  2596. (org-flag-drawer t drawer)
  2597. (get-char-property (progn (search-forward ":D2") (line-end-position))
  2598. 'invisible))))
  2599. (should-not
  2600. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2601. (let ((drawer (save-excursion (search-forward ":D2")
  2602. (org-element-at-point))))
  2603. (org-flag-drawer t drawer)
  2604. (get-char-property (line-end-position) 'invisible))))
  2605. ;; Do not hide fake drawers.
  2606. (should-not
  2607. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  2608. (forward-line 1)
  2609. (org-flag-drawer t)
  2610. (get-char-property (line-end-position) 'invisible)))
  2611. ;; Do not hide incomplete drawers.
  2612. (should-not
  2613. (org-test-with-temp-text ":D:\nparagraph"
  2614. (forward-line 1)
  2615. (org-flag-drawer t)
  2616. (get-char-property (line-end-position) 'invisible)))
  2617. ;; Do not hide drawers when called from final blank lines.
  2618. (should-not
  2619. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  2620. (goto-char (point-max))
  2621. (org-flag-drawer t)
  2622. (goto-char (point-min))
  2623. (get-char-property (line-end-position) 'invisible)))
  2624. ;; Don't leave point in an invisible part of the buffer when hiding
  2625. ;; a drawer away.
  2626. (should-not
  2627. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2628. (goto-char (point-max))
  2629. (org-flag-drawer t)
  2630. (get-char-property (point) 'invisible))))
  2631. (ert-deftest test-org/hide-block-toggle ()
  2632. "Test `org-hide-block-toggle' specifications."
  2633. ;; Error when not at a block.
  2634. (should-error
  2635. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  2636. (org-hide-block-toggle 'off)
  2637. (get-char-property (line-end-position) 'invisible)))
  2638. ;; Hide block.
  2639. (should
  2640. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2641. (org-hide-block-toggle)
  2642. (get-char-property (line-end-position) 'invisible)))
  2643. (should
  2644. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  2645. (org-hide-block-toggle)
  2646. (get-char-property (line-end-position) 'invisible)))
  2647. ;; Show block unconditionally when optional argument is `off'.
  2648. (should-not
  2649. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2650. (org-hide-block-toggle)
  2651. (org-hide-block-toggle 'off)
  2652. (get-char-property (line-end-position) 'invisible)))
  2653. (should-not
  2654. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2655. (org-hide-block-toggle 'off)
  2656. (get-char-property (line-end-position) 'invisible)))
  2657. ;; Hide block unconditionally when optional argument is non-nil.
  2658. (should
  2659. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2660. (org-hide-block-toggle t)
  2661. (get-char-property (line-end-position) 'invisible)))
  2662. (should
  2663. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2664. (org-hide-block-toggle)
  2665. (org-hide-block-toggle t)
  2666. (get-char-property (line-end-position) 'invisible)))
  2667. ;; Do not hide block when called from final blank lines.
  2668. (should-not
  2669. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  2670. (org-hide-block-toggle)
  2671. (goto-char (point-min))
  2672. (get-char-property (line-end-position) 'invisible)))
  2673. ;; Don't leave point in an invisible part of the buffer when hiding
  2674. ;; a block away.
  2675. (should-not
  2676. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  2677. (org-hide-block-toggle)
  2678. (get-char-property (point) 'invisible))))
  2679. (ert-deftest test-org/hide-block-toggle-maybe ()
  2680. "Test `org-hide-block-toggle-maybe' specifications."
  2681. (should
  2682. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  2683. (org-hide-block-toggle-maybe)))
  2684. (should-not
  2685. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  2686. (provide 'test-org)
  2687. ;;; test-org.el ends here