test-org.el 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534
  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. ;;; Mark Region
  1228. (ert-deftest test-org/mark-subtree ()
  1229. "Test `org-mark-subtree' specifications."
  1230. ;; Error when point is before first headline.
  1231. (should-error
  1232. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1233. (progn (transient-mark-mode 1)
  1234. (org-mark-subtree))))
  1235. ;; Without argument, mark current subtree.
  1236. (should
  1237. (equal
  1238. '(12 32)
  1239. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1240. (progn (transient-mark-mode 1)
  1241. (forward-line 2)
  1242. (org-mark-subtree)
  1243. (list (region-beginning) (region-end))))))
  1244. ;; With an argument, move ARG up.
  1245. (should
  1246. (equal
  1247. '(1 32)
  1248. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1249. (progn (transient-mark-mode 1)
  1250. (forward-line 2)
  1251. (org-mark-subtree 1)
  1252. (list (region-beginning) (region-end))))))
  1253. ;; Do not get fooled by inlinetasks.
  1254. (when (featurep 'org-inlinetask)
  1255. (should
  1256. (= 1
  1257. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1258. (progn (transient-mark-mode 1)
  1259. (forward-line 1)
  1260. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1261. (region-beginning)))))))
  1262. ;;; Navigation
  1263. (ert-deftest test-org/beginning-of-line ()
  1264. "Test `org-beginning-of-line' specifications."
  1265. ;; Standard test.
  1266. (should
  1267. (org-test-with-temp-text "Some text\nSome other text"
  1268. (progn (org-beginning-of-line) (bolp))))
  1269. ;; Standard test with `visual-line-mode'.
  1270. (should-not
  1271. (org-test-with-temp-text "A long line of text\nSome other text"
  1272. (progn (visual-line-mode)
  1273. (forward-char 2)
  1274. (dotimes (i 1000) (insert "very "))
  1275. (org-beginning-of-line)
  1276. (bolp))))
  1277. ;; At an headline with special movement.
  1278. (should
  1279. (org-test-with-temp-text "* TODO Headline"
  1280. (let ((org-special-ctrl-a/e t))
  1281. (org-end-of-line)
  1282. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1283. (progn (org-beginning-of-line) (bolp))
  1284. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1285. (ert-deftest test-org/end-of-line ()
  1286. "Test `org-end-of-line' specifications."
  1287. ;; Standard test.
  1288. (should
  1289. (org-test-with-temp-text "Some text\nSome other text"
  1290. (progn (org-end-of-line) (eolp))))
  1291. ;; Standard test with `visual-line-mode'.
  1292. (should-not
  1293. (org-test-with-temp-text "A long line of text\nSome other text"
  1294. (progn (visual-line-mode)
  1295. (forward-char 2)
  1296. (dotimes (i 1000) (insert "very "))
  1297. (goto-char (point-min))
  1298. (org-end-of-line)
  1299. (eolp))))
  1300. ;; At an headline with special movement.
  1301. (should
  1302. (org-test-with-temp-text "* Headline1 :tag:\n"
  1303. (let ((org-special-ctrl-a/e t))
  1304. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1305. (progn (org-end-of-line) (eolp))
  1306. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1307. ;; At an headline without special movement.
  1308. (should
  1309. (org-test-with-temp-text "* Headline2 :tag:\n"
  1310. (let ((org-special-ctrl-a/e nil))
  1311. (and (progn (org-end-of-line) (eolp))
  1312. (progn (org-end-of-line) (eolp))))))
  1313. ;; At an headline, with reversed movement.
  1314. (should
  1315. (org-test-with-temp-text "* Headline3 :tag:\n"
  1316. (let ((org-special-ctrl-a/e 'reversed)
  1317. (this-command last-command))
  1318. (and (progn (org-end-of-line) (eolp))
  1319. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1320. ;; At a block without hidden contents.
  1321. (should
  1322. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1323. (progn (org-end-of-line) (eolp))))
  1324. ;; At a block with hidden contents.
  1325. (should-not
  1326. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1327. (let ((org-special-ctrl-a/e t))
  1328. (org-hide-block-toggle)
  1329. (org-end-of-line)
  1330. (eobp)))))
  1331. (ert-deftest test-org/forward-paragraph ()
  1332. "Test `org-forward-paragraph' specifications."
  1333. ;; At end of buffer, return an error.
  1334. (should-error
  1335. (org-test-with-temp-text "Paragraph"
  1336. (goto-char (point-max))
  1337. (org-forward-paragraph)))
  1338. ;; Standard test.
  1339. (should
  1340. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1341. (org-forward-paragraph)
  1342. (looking-at "P2")))
  1343. ;; Ignore depth.
  1344. (should
  1345. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1346. (org-forward-paragraph)
  1347. (looking-at "P1")))
  1348. ;; Do not enter elements with invisible contents.
  1349. (should
  1350. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1351. (org-hide-block-toggle)
  1352. (org-forward-paragraph)
  1353. (looking-at "P3")))
  1354. ;; On an affiliated keyword, jump to the beginning of the element.
  1355. (should
  1356. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1357. (org-forward-paragraph)
  1358. (looking-at "Para")))
  1359. ;; On an item or a footnote definition, move to the second element
  1360. ;; inside, if any.
  1361. (should
  1362. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1363. (org-forward-paragraph)
  1364. (looking-at " Paragraph")))
  1365. (should
  1366. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1367. (org-forward-paragraph)
  1368. (looking-at "Paragraph")))
  1369. ;; On an item, or a footnote definition, when the first line is
  1370. ;; empty, move to the first item.
  1371. (should
  1372. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1373. (org-forward-paragraph)
  1374. (looking-at " Paragraph")))
  1375. (should
  1376. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1377. (org-forward-paragraph)
  1378. (looking-at "Paragraph")))
  1379. ;; On a table (resp. a property drawer) do not move through table
  1380. ;; rows (resp. node properties).
  1381. (should
  1382. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1383. (org-forward-paragraph)
  1384. (looking-at "Paragraph")))
  1385. (should
  1386. (org-test-with-temp-text
  1387. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1388. (org-forward-paragraph)
  1389. (looking-at "Paragraph")))
  1390. ;; On a verse or source block, stop after blank lines.
  1391. (should
  1392. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1393. (org-forward-paragraph)
  1394. (looking-at "L2")))
  1395. (should
  1396. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1397. (org-forward-paragraph)
  1398. (looking-at "L2"))))
  1399. (ert-deftest test-org/backward-paragraph ()
  1400. "Test `org-backward-paragraph' specifications."
  1401. ;; Error at beginning of buffer.
  1402. (should-error
  1403. (org-test-with-temp-text "Paragraph"
  1404. (org-backward-paragraph)))
  1405. ;; Regular test.
  1406. (should
  1407. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1408. (goto-char (point-max))
  1409. (org-backward-paragraph)
  1410. (looking-at "P3")))
  1411. (should
  1412. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1413. (goto-char (point-max))
  1414. (beginning-of-line)
  1415. (org-backward-paragraph)
  1416. (looking-at "P2")))
  1417. ;; Ignore depth.
  1418. (should
  1419. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1420. (goto-char (point-max))
  1421. (beginning-of-line)
  1422. (org-backward-paragraph)
  1423. (looking-at "P2")))
  1424. ;; Ignore invisible elements.
  1425. (should
  1426. (org-test-with-temp-text "* H1\n P1\n* H2"
  1427. (org-cycle)
  1428. (goto-char (point-max))
  1429. (beginning-of-line)
  1430. (org-backward-paragraph)
  1431. (bobp)))
  1432. ;; On an affiliated keyword, jump to the first one.
  1433. (should
  1434. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1435. (search-forward "c2")
  1436. (org-backward-paragraph)
  1437. (looking-at "#\\+name")))
  1438. ;; On the second element in an item or a footnote definition, jump
  1439. ;; to item or the definition.
  1440. (should
  1441. (org-test-with-temp-text "- line1\n\n line2"
  1442. (goto-char (point-max))
  1443. (beginning-of-line)
  1444. (org-backward-paragraph)
  1445. (looking-at "- line1")))
  1446. (should
  1447. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1448. (goto-char (point-max))
  1449. (beginning-of-line)
  1450. (org-backward-paragraph)
  1451. (looking-at "\\[fn:1\\] line1")))
  1452. ;; On a table (resp. a property drawer), ignore table rows
  1453. ;; (resp. node properties).
  1454. (should
  1455. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1456. (goto-char (point-max))
  1457. (beginning-of-line)
  1458. (org-backward-paragraph)
  1459. (bobp)))
  1460. (should
  1461. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  1462. (org-backward-paragraph)
  1463. (looking-at ":PROPERTIES:")))
  1464. ;; On a source or verse block, stop before blank lines.
  1465. (should
  1466. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1467. (search-forward "L3")
  1468. (beginning-of-line)
  1469. (org-backward-paragraph)
  1470. (looking-at "L2")))
  1471. (should
  1472. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1473. (search-forward "L3")
  1474. (beginning-of-line)
  1475. (org-backward-paragraph)
  1476. (looking-at "L2"))))
  1477. (ert-deftest test-org/forward-element ()
  1478. "Test `org-forward-element' specifications."
  1479. ;; 1. At EOB: should error.
  1480. (org-test-with-temp-text "Some text\n"
  1481. (goto-char (point-max))
  1482. (should-error (org-forward-element)))
  1483. ;; 2. Standard move: expected to ignore blank lines.
  1484. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1485. (org-forward-element)
  1486. (should (looking-at (regexp-quote "Second paragraph."))))
  1487. ;; 3. Headline tests.
  1488. (org-test-with-temp-text "
  1489. * Head 1
  1490. ** Head 1.1
  1491. *** Head 1.1.1
  1492. ** Head 1.2"
  1493. ;; 3.1. At an headline beginning: move to next headline at the
  1494. ;; same level.
  1495. (goto-line 3)
  1496. (org-forward-element)
  1497. (should (looking-at (regexp-quote "** Head 1.2")))
  1498. ;; 3.2. At an headline beginning: move to parent headline if no
  1499. ;; headline at the same level.
  1500. (goto-line 3)
  1501. (org-forward-element)
  1502. (should (looking-at (regexp-quote "** Head 1.2"))))
  1503. ;; 4. Greater element tests.
  1504. (org-test-with-temp-text
  1505. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1506. ;; 4.1. At a greater element: expected to skip contents.
  1507. (org-forward-element)
  1508. (should (looking-at (regexp-quote "Outside.")))
  1509. ;; 4.2. At the end of greater element contents: expected to skip
  1510. ;; to the end of the greater element.
  1511. (goto-line 2)
  1512. (org-forward-element)
  1513. (should (looking-at (regexp-quote "Outside."))))
  1514. ;; 5. List tests.
  1515. (org-test-with-temp-text "
  1516. - item1
  1517. - sub1
  1518. - sub2
  1519. - sub3
  1520. Inner paragraph.
  1521. - item2
  1522. Outside."
  1523. ;; 5.1. At list top point: expected to move to the element after
  1524. ;; the list.
  1525. (goto-line 2)
  1526. (org-forward-element)
  1527. (should (looking-at (regexp-quote "Outside.")))
  1528. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1529. ;; beginning of line, move to next item.
  1530. (goto-line 2)
  1531. (forward-char)
  1532. (org-forward-element)
  1533. (should (looking-at "- item2"))
  1534. (goto-line 4)
  1535. (forward-char)
  1536. (org-forward-element)
  1537. (should (looking-at " - sub2"))
  1538. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1539. (goto-line 4)
  1540. (org-forward-element)
  1541. (should (looking-at (regexp-quote " Inner paragraph.")))
  1542. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1543. (goto-line 8)
  1544. (org-forward-element)
  1545. (should (looking-at (regexp-quote " Inner paragraph.")))
  1546. ;; 5.5. At an item: expected to move to next item, if any.
  1547. (goto-line 6)
  1548. (org-forward-element)
  1549. (should (looking-at " - sub3"))))
  1550. (ert-deftest test-org/backward-element ()
  1551. "Test `org-backward-element' specifications."
  1552. ;; 1. Should error at BOB.
  1553. (org-test-with-temp-text " \nParagraph."
  1554. (should-error (org-backward-element)))
  1555. ;; 2. Should move at BOB when called on the first element in buffer.
  1556. (should
  1557. (org-test-with-temp-text "\n#+TITLE: test"
  1558. (progn (forward-line)
  1559. (org-backward-element)
  1560. (bobp))))
  1561. ;; 3. Not at the beginning of an element: move at its beginning.
  1562. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1563. (goto-line 3)
  1564. (end-of-line)
  1565. (org-backward-element)
  1566. (should (looking-at (regexp-quote "Paragraph2."))))
  1567. ;; 4. Headline tests.
  1568. (org-test-with-temp-text "
  1569. * Head 1
  1570. ** Head 1.1
  1571. *** Head 1.1.1
  1572. ** Head 1.2"
  1573. ;; 4.1. At an headline beginning: move to previous headline at the
  1574. ;; same level.
  1575. (goto-line 5)
  1576. (org-backward-element)
  1577. (should (looking-at (regexp-quote "** Head 1.1")))
  1578. ;; 4.2. At an headline beginning: move to parent headline if no
  1579. ;; headline at the same level.
  1580. (goto-line 3)
  1581. (org-backward-element)
  1582. (should (looking-at (regexp-quote "* Head 1")))
  1583. ;; 4.3. At the first top-level headline: should error.
  1584. (goto-line 2)
  1585. (should-error (org-backward-element)))
  1586. ;; 5. At beginning of first element inside a greater element:
  1587. ;; expected to move to greater element's beginning.
  1588. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1589. (goto-line 3)
  1590. (org-backward-element)
  1591. (should (looking-at "#\\+BEGIN_CENTER")))
  1592. ;; 6. At the beginning of the first element in a section: should
  1593. ;; move back to headline, if any.
  1594. (should
  1595. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1596. (progn (goto-char (point-max))
  1597. (beginning-of-line)
  1598. (org-backward-element)
  1599. (org-at-heading-p))))
  1600. ;; 7. List tests.
  1601. (org-test-with-temp-text "
  1602. - item1
  1603. - sub1
  1604. - sub2
  1605. - sub3
  1606. Inner paragraph.
  1607. - item2
  1608. Outside."
  1609. ;; 7.1. At beginning of sub-list: expected to move to the
  1610. ;; paragraph before it.
  1611. (goto-line 4)
  1612. (org-backward-element)
  1613. (should (looking-at "item1"))
  1614. ;; 7.2. At an item in a list: expected to move at previous item.
  1615. (goto-line 8)
  1616. (org-backward-element)
  1617. (should (looking-at " - sub2"))
  1618. (goto-line 12)
  1619. (org-backward-element)
  1620. (should (looking-at "- item1"))
  1621. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1622. ;; beginning.
  1623. (goto-line 10)
  1624. (org-backward-element)
  1625. (should (looking-at " - sub1"))
  1626. (goto-line 15)
  1627. (org-backward-element)
  1628. (should (looking-at "- item1"))
  1629. ;; 7.4. At blank-lines before list end: expected to move to top
  1630. ;; item.
  1631. (goto-line 14)
  1632. (org-backward-element)
  1633. (should (looking-at "- item1"))))
  1634. (ert-deftest test-org/up-element ()
  1635. "Test `org-up-element' specifications."
  1636. ;; 1. At BOB or with no surrounding element: should error.
  1637. (org-test-with-temp-text "Paragraph."
  1638. (should-error (org-up-element)))
  1639. (org-test-with-temp-text "* Head1\n* Head2"
  1640. (goto-line 2)
  1641. (should-error (org-up-element)))
  1642. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1643. (goto-line 3)
  1644. (should-error (org-up-element)))
  1645. ;; 2. At an headline: move to parent headline.
  1646. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1647. (goto-line 3)
  1648. (org-up-element)
  1649. (should (looking-at "\\* Head1")))
  1650. ;; 3. Inside a greater element: move to greater element beginning.
  1651. (org-test-with-temp-text
  1652. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1653. (goto-line 3)
  1654. (org-up-element)
  1655. (should (looking-at "#\\+BEGIN_CENTER")))
  1656. ;; 4. List tests.
  1657. (org-test-with-temp-text "* Top
  1658. - item1
  1659. - sub1
  1660. - sub2
  1661. Paragraph within sub2.
  1662. - item2"
  1663. ;; 4.1. Within an item: move to the item beginning.
  1664. (goto-line 8)
  1665. (org-up-element)
  1666. (should (looking-at " - sub2"))
  1667. ;; 4.2. At an item in a sub-list: move to parent item.
  1668. (goto-line 4)
  1669. (org-up-element)
  1670. (should (looking-at "- item1"))
  1671. ;; 4.3. At an item in top list: move to beginning of whole list.
  1672. (goto-line 10)
  1673. (org-up-element)
  1674. (should (looking-at "- item1"))
  1675. ;; 4.4. Special case. At very top point: should move to parent of
  1676. ;; list.
  1677. (goto-line 2)
  1678. (org-up-element)
  1679. (should (looking-at "\\* Top"))))
  1680. (ert-deftest test-org/down-element ()
  1681. "Test `org-down-element' specifications."
  1682. ;; Error when the element hasn't got a recursive type.
  1683. (org-test-with-temp-text "Paragraph."
  1684. (should-error (org-down-element)))
  1685. ;; Error when the element has no contents
  1686. (org-test-with-temp-text "* Headline"
  1687. (should-error (org-down-element)))
  1688. ;; When at a plain-list, move to first item.
  1689. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1690. (goto-line 2)
  1691. (org-down-element)
  1692. (should (looking-at " - Item 1.1")))
  1693. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1694. (org-down-element)
  1695. (should (looking-at " Item 1")))
  1696. ;; When at a table, move to first row
  1697. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1698. (org-down-element)
  1699. (should (looking-at " a | b |")))
  1700. ;; Otherwise, move inside the greater element.
  1701. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1702. (org-down-element)
  1703. (should (looking-at "Paragraph"))))
  1704. (ert-deftest test-org/drag-element-backward ()
  1705. "Test `org-drag-element-backward' specifications."
  1706. ;; Standard test.
  1707. (should
  1708. (equal
  1709. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  1710. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  1711. (org-drag-element-backward)
  1712. (buffer-string))))
  1713. (should
  1714. (equal
  1715. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  1716. (org-test-with-temp-text
  1717. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  1718. (org-drag-element-backward)
  1719. (buffer-string))))
  1720. ;; Preserve blank lines.
  1721. (should
  1722. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  1723. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  1724. (org-drag-element-backward)
  1725. (buffer-string))))
  1726. ;; Preserve column.
  1727. (should
  1728. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  1729. (org-drag-element-backward)
  1730. (org-looking-at-p "2")))
  1731. ;; Error when trying to move first element of buffer.
  1732. (should-error
  1733. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1734. (org-drag-element-backward)))
  1735. ;; Error when trying to swap nested elements.
  1736. (should-error
  1737. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1738. (forward-line)
  1739. (org-drag-element-backward)))
  1740. ;; Error when trying to swap an headline element and a non-headline
  1741. ;; element.
  1742. (should-error
  1743. (org-test-with-temp-text "Test.\n* Head 1"
  1744. (forward-line)
  1745. (org-drag-element-backward)))
  1746. ;; Preserve visibility of elements and their contents.
  1747. (should
  1748. (equal '((63 . 82) (26 . 48))
  1749. (org-test-with-temp-text "
  1750. #+BEGIN_CENTER
  1751. Text.
  1752. #+END_CENTER
  1753. - item 1
  1754. #+BEGIN_QUOTE
  1755. Text.
  1756. #+END_QUOTE"
  1757. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1758. (search-backward "- item 1")
  1759. (org-drag-element-backward)
  1760. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1761. (overlays-in (point-min) (point-max)))))))
  1762. (ert-deftest test-org/drag-element-forward ()
  1763. "Test `org-drag-element-forward' specifications."
  1764. ;; 1. Error when trying to move first element of buffer.
  1765. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1766. (goto-line 3)
  1767. (should-error (org-drag-element-forward)))
  1768. ;; 2. Error when trying to swap nested elements.
  1769. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1770. (forward-line)
  1771. (should-error (org-drag-element-forward)))
  1772. ;; 3. Error when trying to swap a non-headline element and an
  1773. ;; headline.
  1774. (org-test-with-temp-text "Test.\n* Head 1"
  1775. (should-error (org-drag-element-forward)))
  1776. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1777. ;; between elements.
  1778. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1779. (search-forward "graph")
  1780. (org-drag-element-forward)
  1781. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1782. (should (looking-at " 1")))
  1783. ;; 5. Preserve visibility of elements and their contents.
  1784. (org-test-with-temp-text "
  1785. #+BEGIN_CENTER
  1786. Text.
  1787. #+END_CENTER
  1788. - item 1
  1789. #+BEGIN_QUOTE
  1790. Text.
  1791. #+END_QUOTE"
  1792. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1793. (search-backward "#+BEGIN_CENTER")
  1794. (org-drag-element-forward)
  1795. (should
  1796. (equal
  1797. '((63 . 82) (26 . 48))
  1798. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1799. (overlays-in (point-min) (point-max)))))))
  1800. ;;; Planning
  1801. (ert-deftest test-org/timestamp-has-time-p ()
  1802. "Test `org-timestamp-has-time-p' specifications."
  1803. ;; With time.
  1804. (should
  1805. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1806. (org-timestamp-has-time-p (org-element-context))))
  1807. ;; Without time.
  1808. (should-not
  1809. (org-test-with-temp-text "<2012-03-29 Thu>"
  1810. (org-timestamp-has-time-p (org-element-context)))))
  1811. (ert-deftest test-org/timestamp-format ()
  1812. "Test `org-timestamp-format' specifications."
  1813. ;; Regular test.
  1814. (should
  1815. (equal
  1816. "2012-03-29 16:40"
  1817. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1818. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1819. ;; Range end.
  1820. (should
  1821. (equal
  1822. "2012-03-29"
  1823. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1824. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1825. (ert-deftest test-org/timestamp-split-range ()
  1826. "Test `org-timestamp-split-range' specifications."
  1827. ;; Extract range start (active).
  1828. (should
  1829. (equal '(2012 3 29)
  1830. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1831. (let ((ts (org-timestamp-split-range (org-element-context))))
  1832. (mapcar (lambda (p) (org-element-property p ts))
  1833. '(:year-end :month-end :day-end))))))
  1834. ;; Extract range start (inactive)
  1835. (should
  1836. (equal '(2012 3 29)
  1837. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1838. (let ((ts (org-timestamp-split-range (org-element-context))))
  1839. (mapcar (lambda (p) (org-element-property p ts))
  1840. '(:year-end :month-end :day-end))))))
  1841. ;; Extract range end (active).
  1842. (should
  1843. (equal '(2012 3 30)
  1844. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1845. (let ((ts (org-timestamp-split-range
  1846. (org-element-context) t)))
  1847. (mapcar (lambda (p) (org-element-property p ts))
  1848. '(:year-end :month-end :day-end))))))
  1849. ;; Extract range end (inactive)
  1850. (should
  1851. (equal '(2012 3 30)
  1852. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1853. (let ((ts (org-timestamp-split-range
  1854. (org-element-context) t)))
  1855. (mapcar (lambda (p) (org-element-property p ts))
  1856. '(:year-end :month-end :day-end))))))
  1857. ;; Return the timestamp if not a range.
  1858. (should
  1859. (org-test-with-temp-text "[2012-03-29 Thu]"
  1860. (let* ((ts-orig (org-element-context))
  1861. (ts-copy (org-timestamp-split-range ts-orig)))
  1862. (eq ts-orig ts-copy))))
  1863. (should
  1864. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1865. (let* ((ts-orig (org-element-context))
  1866. (ts-copy (org-timestamp-split-range ts-orig)))
  1867. (eq ts-orig ts-copy))))
  1868. ;; Check that parent is the same when a range was split.
  1869. (should
  1870. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1871. (let* ((ts-orig (org-element-context))
  1872. (ts-copy (org-timestamp-split-range ts-orig)))
  1873. (eq (org-element-property :parent ts-orig)
  1874. (org-element-property :parent ts-copy))))))
  1875. (ert-deftest test-org/timestamp-translate ()
  1876. "Test `org-timestamp-translate' specifications."
  1877. ;; Translate whole date range.
  1878. (should
  1879. (equal "<29>--<30>"
  1880. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1881. (let ((org-display-custom-times t)
  1882. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1883. (org-timestamp-translate (org-element-context))))))
  1884. ;; Translate date range start.
  1885. (should
  1886. (equal "<29>"
  1887. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1888. (let ((org-display-custom-times t)
  1889. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1890. (org-timestamp-translate (org-element-context) 'start)))))
  1891. ;; Translate date range end.
  1892. (should
  1893. (equal "<30>"
  1894. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1895. (let ((org-display-custom-times t)
  1896. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1897. (org-timestamp-translate (org-element-context) 'end)))))
  1898. ;; Translate time range.
  1899. (should
  1900. (equal "<08>--<16>"
  1901. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1902. (let ((org-display-custom-times t)
  1903. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1904. (org-timestamp-translate (org-element-context))))))
  1905. ;; Translate non-range timestamp.
  1906. (should
  1907. (equal "<29>"
  1908. (org-test-with-temp-text "<2012-03-29 Thu>"
  1909. (let ((org-display-custom-times t)
  1910. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1911. (org-timestamp-translate (org-element-context))))))
  1912. ;; Do not change `diary' timestamps.
  1913. (should
  1914. (equal "<%%(org-float t 4 2)>"
  1915. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1916. (let ((org-display-custom-times t)
  1917. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1918. (org-timestamp-translate (org-element-context)))))))
  1919. ;;; Property API
  1920. (ert-deftest test-org/buffer-property-keys ()
  1921. "Test `org-buffer-property-keys' specifications."
  1922. ;; Retrieve properties accross siblings.
  1923. (should
  1924. (equal '("A" "B")
  1925. (org-test-with-temp-text "
  1926. * H1
  1927. :PROPERTIES:
  1928. :A: 1
  1929. :END:
  1930. * H2
  1931. :PROPERTIES:
  1932. :B: 1
  1933. :END:"
  1934. (org-buffer-property-keys))))
  1935. ;; Retrieve properties accross children.
  1936. (should
  1937. (equal '("A" "B")
  1938. (org-test-with-temp-text "
  1939. * H1
  1940. :PROPERTIES:
  1941. :A: 1
  1942. :END:
  1943. ** H2
  1944. :PROPERTIES:
  1945. :B: 1
  1946. :END:"
  1947. (org-buffer-property-keys))))
  1948. ;; Retrieve muliple properties in the same drawer.
  1949. (should
  1950. (equal '("A" "B")
  1951. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  1952. (org-buffer-property-keys))))
  1953. ;; Ignore extension symbol in property name.
  1954. (should
  1955. (equal '("A")
  1956. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  1957. (org-buffer-property-keys))))
  1958. ;; With non-nil COLUMNS, extract property names from columns.
  1959. (should
  1960. (equal '("A" "B")
  1961. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  1962. (org-buffer-property-keys nil nil t))))
  1963. (should
  1964. (equal '("A" "B" "COLUMNS")
  1965. (org-test-with-temp-text
  1966. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  1967. (org-buffer-property-keys nil nil t)))))
  1968. (ert-deftest test-org/property-values ()
  1969. "Test `org-property-values' specifications."
  1970. ;; Regular test.
  1971. (should
  1972. (equal '("2" "1")
  1973. (org-test-with-temp-text
  1974. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  1975. (org-property-values "A"))))
  1976. ;; Ignore empty values.
  1977. (should-not
  1978. (org-test-with-temp-text
  1979. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  1980. (org-property-values "A")))
  1981. ;; Take into consideration extended values.
  1982. (should
  1983. (equal '("1 2")
  1984. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  1985. (org-property-values "A")))))
  1986. (ert-deftest test-org/entry-delete ()
  1987. "Test `org-entry-delete' specifications."
  1988. ;; Regular test.
  1989. (should
  1990. (string-match
  1991. " *:PROPERTIES:\n *:B: +2\n *:END:"
  1992. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  1993. (org-entry-delete (point) "A")
  1994. (buffer-string))))
  1995. ;; When last property is removed, remove the property drawer.
  1996. (should-not
  1997. (string-match
  1998. ":PROPERTIES:"
  1999. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  2000. (org-entry-delete (point) "A")
  2001. (buffer-string)))))
  2002. (ert-deftest test-org/entry-get ()
  2003. "Test `org-entry-get' specifications."
  2004. ;; Regular test.
  2005. (should
  2006. (equal "1"
  2007. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2008. (org-entry-get (point) "A"))))
  2009. ;; Ignore case.
  2010. (should
  2011. (equal "1"
  2012. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2013. (org-entry-get (point) "a"))))
  2014. ;; Handle extended values, both before and after base value.
  2015. (should
  2016. (equal "1 2 3"
  2017. (org-test-with-temp-text
  2018. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2019. (org-entry-get (point) "A"))))
  2020. ;; Empty values are returned as the empty string.
  2021. (should
  2022. (equal ""
  2023. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  2024. (org-entry-get (point) "A"))))
  2025. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  2026. ;; otherwise, return nil.
  2027. (should-not
  2028. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2029. (org-entry-get (point) "A")))
  2030. (should
  2031. (equal "nil"
  2032. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2033. (org-entry-get (point) "A" nil t))))
  2034. ;; Return nil when no property is found, independently on the
  2035. ;; LITERAL-NIL argument.
  2036. (should-not
  2037. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2038. (org-entry-get (point) "B")))
  2039. (should-not
  2040. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2041. (org-entry-get (point) "B" nil t)))
  2042. ;; Handle inheritance, when allowed.
  2043. (should
  2044. (equal
  2045. "1"
  2046. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2047. (org-entry-get (point) "A" t))))
  2048. (should
  2049. (equal
  2050. "1"
  2051. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2052. (let ((org-use-property-inheritance t))
  2053. (org-entry-get (point) "A" 'selective)))))
  2054. (should-not
  2055. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2056. (let ((org-use-property-inheritance nil))
  2057. (org-entry-get (point) "A" 'selective)))))
  2058. (ert-deftest test-org/entry-properties ()
  2059. "Test `org-entry-properties' specifications."
  2060. ;; Get "TODO" property.
  2061. (should
  2062. (equal "TODO"
  2063. (org-test-with-temp-text "* TODO H"
  2064. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  2065. (should-not
  2066. (org-test-with-temp-text "* H"
  2067. (assoc "TODO" (org-entry-properties nil "TODO"))))
  2068. ;; Get "PRIORITY" property.
  2069. (should
  2070. (equal "A"
  2071. (org-test-with-temp-text "* [#A] H"
  2072. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  2073. (should-not
  2074. (org-test-with-temp-text "* H"
  2075. (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
  2076. ;; Get "FILE" property.
  2077. (should
  2078. (org-test-with-temp-text-in-file "* H\nParagraph"
  2079. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  2080. (buffer-file-name))))
  2081. (should-not
  2082. (org-test-with-temp-text "* H\nParagraph"
  2083. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  2084. ;; Get "TAGS" property.
  2085. (should
  2086. (equal ":tag1:tag2:"
  2087. (org-test-with-temp-text "* H :tag1:tag2:"
  2088. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  2089. (should-not
  2090. (org-test-with-temp-text "* H"
  2091. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  2092. ;; Get "ALLTAGS" property.
  2093. (should
  2094. (equal ":tag1:tag2:"
  2095. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  2096. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  2097. (should-not
  2098. (org-test-with-temp-text "* H"
  2099. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  2100. ;; Get "BLOCKED" property.
  2101. (should
  2102. (equal "t"
  2103. (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
  2104. (let ((org-enforce-todo-dependencies t)
  2105. (org-blocker-hook
  2106. '(org-block-todo-from-children-or-siblings-or-parent)))
  2107. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2108. (should
  2109. (equal ""
  2110. (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
  2111. (let ((org-enforce-todo-dependencies t))
  2112. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2113. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  2114. (should
  2115. (equal
  2116. "[2012-03-29 thu.]"
  2117. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  2118. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  2119. (should-not
  2120. (org-test-with-temp-text "* H"
  2121. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  2122. (should
  2123. (equal
  2124. "<2014-03-04 tue.>"
  2125. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2126. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  2127. (should-not
  2128. (org-test-with-temp-text "* H"
  2129. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  2130. (should
  2131. (equal
  2132. "<2014-03-04 tue.>"
  2133. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2134. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  2135. (should-not
  2136. (org-test-with-temp-text "* H"
  2137. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  2138. ;; Get "CATEGORY"
  2139. (should
  2140. (equal "cat"
  2141. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  2142. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2143. (should
  2144. (equal "cat"
  2145. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  2146. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2147. ;; Get standard properties.
  2148. (should
  2149. (equal "1"
  2150. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2151. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2152. ;; Handle extended properties.
  2153. (should
  2154. (equal "1 2 3"
  2155. (org-test-with-temp-text
  2156. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2157. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2158. (should
  2159. (equal "1 2 3"
  2160. (org-test-with-temp-text
  2161. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  2162. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2163. ;; Ignore forbidden (special) properties.
  2164. (should-not
  2165. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  2166. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  2167. (ert-deftest test-org/entry-put ()
  2168. "Test `org-entry-put' specifications."
  2169. ;; Error when not a string or nil.
  2170. (should-error
  2171. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  2172. (org-entry-put 1 "test" 2)))
  2173. ;; Set "TODO" property.
  2174. (should
  2175. (string-match (regexp-quote " TODO H")
  2176. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2177. (org-entry-put (point) "TODO" "TODO")
  2178. (buffer-string))))
  2179. (should
  2180. (string-match (regexp-quote "* H")
  2181. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2182. (org-entry-put (point) "TODO" nil)
  2183. (buffer-string))))
  2184. ;; Set "PRIORITY" property.
  2185. (should
  2186. (equal "* [#A] H"
  2187. (org-test-with-temp-text "* [#B] H"
  2188. (org-entry-put (point) "PRIORITY" "A")
  2189. (buffer-string))))
  2190. (should
  2191. (equal "* H"
  2192. (org-test-with-temp-text "* [#B] H"
  2193. (org-entry-put (point) "PRIORITY" nil)
  2194. (buffer-string))))
  2195. ;; Set "SCHEDULED" property.
  2196. (should
  2197. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  2198. (org-test-with-temp-text "* H"
  2199. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  2200. (buffer-string))))
  2201. (should
  2202. (string= "* H\n"
  2203. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2204. (org-entry-put (point) "SCHEDULED" nil)
  2205. (buffer-string))))
  2206. (should
  2207. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  2208. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2209. (org-entry-put (point) "SCHEDULED" "earlier")
  2210. (buffer-string))))
  2211. (should
  2212. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  2213. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2214. (org-entry-put (point) "SCHEDULED" "later")
  2215. (buffer-string))))
  2216. ;; Set "DEADLINE" property.
  2217. (should
  2218. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  2219. (org-test-with-temp-text "* H"
  2220. (org-entry-put (point) "DEADLINE" "2014-03-04")
  2221. (buffer-string))))
  2222. (should
  2223. (string= "* H\n"
  2224. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2225. (org-entry-put (point) "DEADLINE" nil)
  2226. (buffer-string))))
  2227. (should
  2228. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  2229. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2230. (org-entry-put (point) "DEADLINE" "earlier")
  2231. (buffer-string))))
  2232. (should
  2233. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  2234. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2235. (org-entry-put (point) "DEADLINE" "later")
  2236. (buffer-string))))
  2237. ;; Regular properties, with or without pre-existing drawer.
  2238. (should
  2239. (string-match "^ *:A: +2$"
  2240. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2241. (org-entry-put (point) "A" "2")
  2242. (buffer-string))))
  2243. (should
  2244. (string-match "^ *:A: +1$"
  2245. (org-test-with-temp-text "* H"
  2246. (org-entry-put (point) "A" "1")
  2247. (buffer-string))))
  2248. ;; Special case: two consecutive headlines.
  2249. (should
  2250. (string-match "\\* A\n *:PROPERTIES:"
  2251. (org-test-with-temp-text "* A\n** B"
  2252. (org-entry-put (point) "A" "1")
  2253. (buffer-string)))))
  2254. ;;; Radio Targets
  2255. (ert-deftest test-org/update-radio-target-regexp ()
  2256. "Test `org-update-radio-target-regexp' specifications."
  2257. ;; Properly update cache with no previous radio target regexp.
  2258. (should
  2259. (eq 'link
  2260. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2261. (save-excursion (goto-char (point-max)) (org-element-context))
  2262. (insert "<<<")
  2263. (search-forward "o")
  2264. (insert ">>>")
  2265. (org-update-radio-target-regexp)
  2266. (goto-char (point-max))
  2267. (org-element-type (org-element-context)))))
  2268. ;; Properly update cache with previous radio target regexp.
  2269. (should
  2270. (eq 'link
  2271. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2272. (save-excursion (goto-char (point-max)) (org-element-context))
  2273. (insert "<<<")
  2274. (search-forward "o")
  2275. (insert ">>>")
  2276. (org-update-radio-target-regexp)
  2277. (search-backward "r")
  2278. (delete-char 5)
  2279. (insert "new")
  2280. (org-update-radio-target-regexp)
  2281. (goto-char (point-max))
  2282. (delete-region (line-beginning-position) (point))
  2283. (insert "new")
  2284. (org-element-type (org-element-context))))))
  2285. ;;; Visibility
  2286. (ert-deftest test-org/flag-drawer ()
  2287. "Test `org-flag-drawer' specifications."
  2288. ;; Hide drawer.
  2289. (should
  2290. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2291. (org-flag-drawer t)
  2292. (get-char-property (line-end-position) 'invisible)))
  2293. ;; Show drawer.
  2294. (should-not
  2295. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2296. (org-flag-drawer t)
  2297. (org-flag-drawer nil)
  2298. (get-char-property (line-end-position) 'invisible)))
  2299. ;; Test optional argument.
  2300. (should
  2301. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2302. (let ((drawer (save-excursion (search-forward ":D2")
  2303. (org-element-at-point))))
  2304. (org-flag-drawer t drawer)
  2305. (get-char-property (progn (search-forward ":D2") (line-end-position))
  2306. 'invisible))))
  2307. (should-not
  2308. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2309. (let ((drawer (save-excursion (search-forward ":D2")
  2310. (org-element-at-point))))
  2311. (org-flag-drawer t drawer)
  2312. (get-char-property (line-end-position) 'invisible))))
  2313. ;; Do not hide fake drawers.
  2314. (should-not
  2315. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  2316. (forward-line 1)
  2317. (org-flag-drawer t)
  2318. (get-char-property (line-end-position) 'invisible)))
  2319. ;; Do not hide incomplete drawers.
  2320. (should-not
  2321. (org-test-with-temp-text ":D:\nparagraph"
  2322. (forward-line 1)
  2323. (org-flag-drawer t)
  2324. (get-char-property (line-end-position) 'invisible)))
  2325. ;; Do not hide drawers when called from final blank lines.
  2326. (should-not
  2327. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  2328. (goto-char (point-max))
  2329. (org-flag-drawer t)
  2330. (goto-char (point-min))
  2331. (get-char-property (line-end-position) 'invisible)))
  2332. ;; Don't leave point in an invisible part of the buffer when hiding
  2333. ;; a drawer away.
  2334. (should-not
  2335. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2336. (goto-char (point-max))
  2337. (org-flag-drawer t)
  2338. (get-char-property (point) 'invisible))))
  2339. (ert-deftest test-org/hide-block-toggle ()
  2340. "Test `org-hide-block-toggle' specifications."
  2341. ;; Error when not at a block.
  2342. (should-error
  2343. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  2344. (org-hide-block-toggle 'off)
  2345. (get-char-property (line-end-position) 'invisible)))
  2346. ;; Hide block.
  2347. (should
  2348. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2349. (org-hide-block-toggle)
  2350. (get-char-property (line-end-position) 'invisible)))
  2351. (should
  2352. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  2353. (org-hide-block-toggle)
  2354. (get-char-property (line-end-position) 'invisible)))
  2355. ;; Show block unconditionally when optional argument is `off'.
  2356. (should-not
  2357. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2358. (org-hide-block-toggle)
  2359. (org-hide-block-toggle 'off)
  2360. (get-char-property (line-end-position) 'invisible)))
  2361. (should-not
  2362. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2363. (org-hide-block-toggle 'off)
  2364. (get-char-property (line-end-position) 'invisible)))
  2365. ;; Hide block unconditionally when optional argument is non-nil.
  2366. (should
  2367. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2368. (org-hide-block-toggle t)
  2369. (get-char-property (line-end-position) 'invisible)))
  2370. (should
  2371. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2372. (org-hide-block-toggle)
  2373. (org-hide-block-toggle t)
  2374. (get-char-property (line-end-position) 'invisible)))
  2375. ;; Do not hide block when called from final blank lines.
  2376. (should-not
  2377. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  2378. (org-hide-block-toggle)
  2379. (goto-char (point-min))
  2380. (get-char-property (line-end-position) 'invisible)))
  2381. ;; Don't leave point in an invisible part of the buffer when hiding
  2382. ;; a block away.
  2383. (should-not
  2384. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  2385. (org-hide-block-toggle)
  2386. (get-char-property (point) 'invisible))))
  2387. (ert-deftest test-org/hide-block-toggle-maybe ()
  2388. "Test `org-hide-block-toggle-maybe' specifications."
  2389. (should
  2390. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  2391. (org-hide-block-toggle-maybe)))
  2392. (should-not
  2393. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  2394. (provide 'test-org)
  2395. ;;; test-org.el ends here