test-org.el 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  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. ;;; Filling
  192. (ert-deftest test-org/fill-paragraph ()
  193. "Test `org-fill-paragraph' specifications."
  194. ;; At an Org table, align it.
  195. (should
  196. (equal "| a |\n"
  197. (org-test-with-temp-text "|a|"
  198. (org-fill-paragraph)
  199. (buffer-string))))
  200. (should
  201. (equal "#+name: table\n| a |\n"
  202. (org-test-with-temp-text "#+name: table\n| a |"
  203. (org-fill-paragraph)
  204. (buffer-string))))
  205. ;; At a paragraph, preserve line breaks.
  206. (org-test-with-temp-text "some \\\\\nlong\ntext"
  207. (let ((fill-column 20))
  208. (org-fill-paragraph)
  209. (should (equal (buffer-string) "some \\\\\nlong text"))))
  210. ;; Correctly fill a paragraph when point is at its very end.
  211. (should
  212. (equal "A B"
  213. (org-test-with-temp-text "A\nB"
  214. (let ((fill-column 20))
  215. (goto-char (point-max))
  216. (org-fill-paragraph)
  217. (buffer-string)))))
  218. ;; Correctly fill the last paragraph of a greater element.
  219. (should
  220. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  221. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  222. (let ((fill-column 8))
  223. (forward-line)
  224. (end-of-line)
  225. (org-fill-paragraph)
  226. (buffer-string)))))
  227. ;; Correctly fill an element in a narrowed buffer.
  228. (should
  229. (equal "01234\n6"
  230. (org-test-with-temp-text "01234 6789"
  231. (let ((fill-column 5))
  232. (narrow-to-region 1 8)
  233. (org-fill-paragraph)
  234. (buffer-string)))))
  235. ;; Handle `adaptive-fill-regexp' in paragraphs.
  236. (should
  237. (equal "> a b"
  238. (org-test-with-temp-text "> a\n> b"
  239. (let ((fill-column 5)
  240. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  241. (org-fill-paragraph)
  242. (buffer-string)))))
  243. ;; Special case: Fill first paragraph when point is at an item or
  244. ;; a plain-list or a footnote reference.
  245. (should
  246. (equal "- A B"
  247. (org-test-with-temp-text "- A\n B"
  248. (let ((fill-column 20))
  249. (org-fill-paragraph)
  250. (buffer-string)))))
  251. (should
  252. (equal "[fn:1] A B"
  253. (org-test-with-temp-text "[fn:1] A\nB"
  254. (let ((fill-column 20))
  255. (org-fill-paragraph)
  256. (buffer-string)))))
  257. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  258. (let ((fill-column 20))
  259. (org-fill-paragraph)
  260. (should (equal (buffer-string)
  261. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  262. ;; Fill contents of `comment-block' elements.
  263. (should
  264. (equal
  265. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  266. (let ((fill-column 20))
  267. (forward-line)
  268. (org-fill-paragraph)
  269. (buffer-string)))
  270. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  271. ;; Fill `comment' elements.
  272. (should
  273. (equal " # A B"
  274. (org-test-with-temp-text " # A\n # B"
  275. (let ((fill-column 20))
  276. (org-fill-paragraph)
  277. (buffer-string)))))
  278. ;; Do not mix consecutive comments when filling one of them.
  279. (should
  280. (equal "# A B\n\n# C"
  281. (org-test-with-temp-text "# A\n# B\n\n# C"
  282. (let ((fill-column 20))
  283. (org-fill-paragraph)
  284. (buffer-string)))))
  285. ;; Use commented empty lines as separators when filling comments.
  286. (should
  287. (equal "# A B\n#\n# C"
  288. (org-test-with-temp-text "# A\n# B\n#\n# C"
  289. (let ((fill-column 20))
  290. (org-fill-paragraph)
  291. (buffer-string)))))
  292. ;; Handle `adaptive-fill-regexp' in comments.
  293. (should
  294. (equal "# > a b"
  295. (org-test-with-temp-text "# > a\n# > b"
  296. (let ((fill-column 20)
  297. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  298. (org-fill-paragraph)
  299. (buffer-string)))))
  300. ;; Do nothing at affiliated keywords.
  301. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  302. (let ((fill-column 20))
  303. (org-fill-paragraph)
  304. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  305. ;; Do not move point after table when filling a table.
  306. (should-not
  307. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  308. (forward-char)
  309. (org-fill-paragraph)
  310. (eobp))))
  311. (ert-deftest test-org/auto-fill-function ()
  312. "Test auto-filling features."
  313. ;; Auto fill paragraph.
  314. (should
  315. (equal "12345\n7890"
  316. (org-test-with-temp-text "12345 7890"
  317. (let ((fill-column 5))
  318. (end-of-line)
  319. (org-auto-fill-function)
  320. (buffer-string)))))
  321. ;; Auto fill first paragraph in an item.
  322. (should
  323. (equal "- 12345\n 7890"
  324. (org-test-with-temp-text "- 12345 7890"
  325. (let ((fill-column 7))
  326. (end-of-line)
  327. (org-auto-fill-function)
  328. (buffer-string)))))
  329. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  330. (should
  331. (equal "> 12345\n 7890"
  332. (org-test-with-temp-text "> 12345 7890"
  333. (let ((fill-column 10)
  334. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  335. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  336. (end-of-line)
  337. (org-auto-fill-function)
  338. (buffer-string)))))
  339. (should
  340. (equal "> 12345\n> 12345\n> 7890"
  341. (org-test-with-temp-text "> 12345\n> 12345 7890"
  342. (let ((fill-column 10)
  343. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  344. (goto-char (point-max))
  345. (org-auto-fill-function)
  346. (buffer-string)))))
  347. (should-not
  348. (equal " 12345\n *12345\n *12345"
  349. (org-test-with-temp-text " 12345\n *12345 12345"
  350. (let ((fill-column 10)
  351. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  352. (goto-char (point-max))
  353. (org-auto-fill-function)
  354. (buffer-string)))))
  355. ;; Auto fill comments.
  356. (should
  357. (equal " # 12345\n # 7890"
  358. (org-test-with-temp-text " # 12345 7890"
  359. (let ((fill-column 10))
  360. (end-of-line)
  361. (org-auto-fill-function)
  362. (buffer-string)))))
  363. ;; A hash within a line isn't a comment.
  364. (should-not
  365. (equal "12345 # 7890\n# 1"
  366. (org-test-with-temp-text "12345 # 7890 1"
  367. (let ((fill-column 12))
  368. (end-of-line)
  369. (org-auto-fill-function)
  370. (buffer-string)))))
  371. ;; Correctly interpret empty prefix.
  372. (should-not
  373. (equal "# a\n# b\nRegular\n# paragraph"
  374. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  375. (let ((fill-column 12))
  376. (end-of-line 3)
  377. (org-auto-fill-function)
  378. (buffer-string)))))
  379. ;; Comment block: auto fill contents.
  380. (should
  381. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  382. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  383. (let ((fill-column 5))
  384. (forward-line)
  385. (end-of-line)
  386. (org-auto-fill-function)
  387. (buffer-string)))))
  388. (should
  389. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  390. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  391. (let ((fill-column 5))
  392. (forward-line)
  393. (end-of-line)
  394. (org-auto-fill-function)
  395. (buffer-string)))))
  396. ;; Do not fill if a new item could be created.
  397. (should-not
  398. (equal "12345\n- 90"
  399. (org-test-with-temp-text "12345 - 90"
  400. (let ((fill-column 5))
  401. (end-of-line)
  402. (org-auto-fill-function)
  403. (buffer-string)))))
  404. ;; Do not fill if a line break could be introduced.
  405. (should-not
  406. (equal "123\\\\\n7890"
  407. (org-test-with-temp-text "123\\\\ 7890"
  408. (let ((fill-column 6))
  409. (end-of-line)
  410. (org-auto-fill-function)
  411. (buffer-string)))))
  412. ;; Do not fill affiliated keywords.
  413. (should-not
  414. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  415. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  416. (let ((fill-column 20))
  417. (end-of-line)
  418. (org-auto-fill-function)
  419. (buffer-string))))))
  420. ;;; Indentation
  421. (ert-deftest test-org/indent-line ()
  422. "Test `org-indent-line' specifications."
  423. ;; Do not indent footnote definitions or headlines.
  424. (should
  425. (zerop
  426. (org-test-with-temp-text "* H"
  427. (org-indent-line)
  428. (org-get-indentation))))
  429. (should
  430. (zerop
  431. (org-test-with-temp-text "[fn:1] fn"
  432. (let ((org-adapt-indentation t)) (org-indent-line))
  433. (org-get-indentation))))
  434. ;; Do not indent before first headline.
  435. (should
  436. (zerop
  437. (org-test-with-temp-text ""
  438. (org-indent-line)
  439. (org-get-indentation))))
  440. ;; Indent according to headline level otherwise, unless
  441. ;; `org-adapt-indentation' is nil.
  442. (should
  443. (= 2
  444. (org-test-with-temp-text "* H\nA"
  445. (forward-line)
  446. (let ((org-adapt-indentation t)) (org-indent-line))
  447. (org-get-indentation))))
  448. (should
  449. (= 2
  450. (org-test-with-temp-text "* H\n\nA"
  451. (forward-line)
  452. (let ((org-adapt-indentation t)) (org-indent-line))
  453. (org-get-indentation))))
  454. (should
  455. (zerop
  456. (org-test-with-temp-text "* H\nA"
  457. (forward-line)
  458. (let ((org-adapt-indentation nil)) (org-indent-line))
  459. (org-get-indentation))))
  460. ;; Indenting preserves point position.
  461. (should
  462. (org-test-with-temp-text "* H\nAB"
  463. (forward-line)
  464. (forward-char)
  465. (let ((org-adapt-indentation t)) (org-indent-line))
  466. (looking-at "B")))
  467. ;; Do not change indentation at an item.
  468. (should
  469. (= 1
  470. (org-test-with-temp-text "* H\n - A"
  471. (forward-line)
  472. (let ((org-adapt-indentation t)) (org-indent-line))
  473. (org-get-indentation))))
  474. ;; On blank lines at the end of a list, indent like last element
  475. ;; within it if the line is still in the list. Otherwise, indent
  476. ;; like the whole list.
  477. (should
  478. (= 4
  479. (org-test-with-temp-text "* H\n- A\n - AA\n"
  480. (goto-char (point-max))
  481. (let ((org-adapt-indentation t)) (org-indent-line))
  482. (org-get-indentation))))
  483. (should
  484. (zerop
  485. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
  486. (goto-char (point-max))
  487. (let ((org-adapt-indentation t)) (org-indent-line))
  488. (org-get-indentation))))
  489. ;; Likewise, on a blank line at the end of a footnote definition,
  490. ;; indent at column 0 if line belongs to the definition. Otherwise,
  491. ;; indent like the definition itself.
  492. (should
  493. (zerop
  494. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  495. (goto-char (point-max))
  496. (let ((org-adapt-indentation t)) (org-indent-line))
  497. (org-get-indentation))))
  498. (should
  499. (zerop
  500. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  501. (goto-char (point-max))
  502. (let ((org-adapt-indentation t)) (org-indent-line))
  503. (org-get-indentation))))
  504. ;; After the end of the contents of a greater element, indent like
  505. ;; the beginning of the element.
  506. (should
  507. (= 1
  508. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  509. (forward-line 2)
  510. (org-indent-line)
  511. (org-get-indentation))))
  512. ;; At the first line of an element, indent like previous element's
  513. ;; first line, ignoring footnotes definitions and inline tasks, or
  514. ;; according to parent.
  515. (should
  516. (= 2
  517. (org-test-with-temp-text "A\n\n B\n\nC"
  518. (goto-char (point-max))
  519. (org-indent-line)
  520. (org-get-indentation))))
  521. (should
  522. (= 1
  523. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  524. (goto-char (point-max))
  525. (org-indent-line)
  526. (org-get-indentation))))
  527. (should
  528. (= 1
  529. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  530. (forward-line 1)
  531. (org-indent-line)
  532. (org-get-indentation))))
  533. ;; Within code part of a source block, use language major mode if
  534. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  535. ;; according to line above.
  536. (should
  537. (= 6
  538. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  539. (forward-line 2)
  540. (let ((org-src-tab-acts-natively t)
  541. (org-edit-src-content-indentation 0))
  542. (org-indent-line))
  543. (org-get-indentation))))
  544. (should
  545. (= 1
  546. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  547. (forward-line 2)
  548. (let ((org-src-tab-acts-natively nil)
  549. (org-edit-src-content-indentation 0))
  550. (org-indent-line))
  551. (org-get-indentation))))
  552. ;; Otherwise, indent like the first non-blank line above.
  553. (should
  554. (zerop
  555. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  556. (forward-line 3)
  557. (org-indent-line)
  558. (org-get-indentation))))
  559. ;; Align node properties according to `org-property-format'. Handle
  560. ;; nicely empty values.
  561. (should
  562. (equal ":PROPERTIES:\n:key: value\n:END:"
  563. (org-test-with-temp-text ":PROPERTIES:\n:key: value\n:END:"
  564. (forward-line)
  565. (let ((org-property-format "%-10s %s"))
  566. (org-indent-line)
  567. (buffer-string)))))
  568. (should
  569. (equal ":PROPERTIES:\n:key:\n:END:"
  570. (org-test-with-temp-text ":PROPERTIES:\n:key:\n:END:"
  571. (forward-line)
  572. (let ((org-property-format "%-10s %s"))
  573. (org-indent-line)
  574. (buffer-string))))))
  575. (ert-deftest test-org/indent-region ()
  576. "Test `org-indent-region' specifications."
  577. ;; Indent paragraph.
  578. (should
  579. (equal "A\nB\nC"
  580. (org-test-with-temp-text " A\nB\n C"
  581. (org-indent-region (point-min) (point-max))
  582. (buffer-string))))
  583. ;; Indent greater elements along with their contents.
  584. (should
  585. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  586. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  587. (org-indent-region (point-min) (point-max))
  588. (buffer-string))))
  589. ;; Ignore contents of verse blocks and example blocks.
  590. (should
  591. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  592. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  593. (org-indent-region (point-min) (point-max))
  594. (buffer-string))))
  595. (should
  596. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  597. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  598. (org-indent-region (point-min) (point-max))
  599. (buffer-string))))
  600. ;; Indent according to mode if `org-src-tab-acts-natively' is
  601. ;; non-nil. Otherwise, do not indent code at all.
  602. (should
  603. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  604. (org-test-with-temp-text
  605. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  606. (let ((org-src-tab-acts-natively t)
  607. (org-edit-src-content-indentation 0))
  608. (org-indent-region (point-min) (point-max)))
  609. (buffer-string))))
  610. (should
  611. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  612. (org-test-with-temp-text
  613. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  614. (let ((org-src-tab-acts-natively nil)
  615. (org-edit-src-content-indentation 0))
  616. (org-indent-region (point-min) (point-max)))
  617. (buffer-string))))
  618. ;; Align node properties according to `org-property-format'. Handle
  619. ;; nicely empty values.
  620. (should
  621. (equal ":PROPERTIES:\n:key: value\n:END:"
  622. (org-test-with-temp-text ":PROPERTIES:\n:key: value\n:END:"
  623. (let ((org-property-format "%-10s %s"))
  624. (org-indent-region (point-min) (point-max)))
  625. (buffer-string))))
  626. (should
  627. (equal ":PROPERTIES:\n:key:\n:END:"
  628. (org-test-with-temp-text ":PROPERTIES:\n:key:\n:END:"
  629. (let ((org-property-format "%-10s %s"))
  630. (org-indent-region (point-min) (point-max)))
  631. (buffer-string))))
  632. ;; Special case: plain lists and footnote definitions.
  633. (should
  634. (equal "- A\n B\n - C\n\n D"
  635. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  636. (org-indent-region (point-min) (point-max))
  637. (buffer-string))))
  638. (should
  639. (equal "[fn:1] Definition\n\nDefinition"
  640. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  641. (org-indent-region (point-min) (point-max))
  642. (buffer-string)))))
  643. ;;; Editing
  644. ;;;; Insert elements
  645. (ert-deftest test-org/meta-return ()
  646. "Test M-RET (`org-meta-return')."
  647. ;; In a table field insert a row above.
  648. (should
  649. (org-test-with-temp-text "| a |"
  650. (forward-char)
  651. (org-meta-return)
  652. (forward-line -1)
  653. (looking-at "| |$")))
  654. ;; In a paragraph change current line into a header.
  655. (should
  656. (org-test-with-temp-text "a"
  657. (org-meta-return)
  658. (beginning-of-line)
  659. (looking-at "\* a$")))
  660. ;; In an item insert an item, in this case above.
  661. (should
  662. (org-test-with-temp-text "- a"
  663. (org-meta-return)
  664. (beginning-of-line)
  665. (looking-at "- $")))
  666. ;; In a drawer and paragraph insert an empty line, in this case above.
  667. (should
  668. (org-test-with-temp-text ":MYDRAWER:\na\n:END:"
  669. (forward-line)
  670. (org-meta-return)
  671. (forward-line -1)
  672. (looking-at "$")))
  673. ;; In a drawer and item insert an item, in this case above.
  674. (should
  675. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  676. (forward-line)
  677. (org-meta-return)
  678. (beginning-of-line)
  679. (looking-at "- $"))))
  680. (ert-deftest test-org/insert-todo-heading-respect-content ()
  681. "Test `org-insert-todo-heading-respect-content' specifications."
  682. ;; Create a TODO heading.
  683. (should
  684. (org-test-with-temp-text "* H1\n Body"
  685. (org-insert-todo-heading-respect-content)
  686. (nth 2 (org-heading-components))))
  687. ;; Add headline at the end of the first subtree
  688. (should
  689. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  690. (search-forward "H1Body")
  691. (org-insert-todo-heading-respect-content)
  692. (and (eobp) (org-at-heading-p))))
  693. ;; In a list, do not create a new item.
  694. (should
  695. (org-test-with-temp-text "* H\n- an item\n- another one"
  696. (search-forward "an ")
  697. (org-insert-todo-heading-respect-content)
  698. (and (eobp) (org-at-heading-p)))))
  699. ;;; Fixed-Width Areas
  700. (ert-deftest test-org/toggle-fixed-width ()
  701. "Test `org-toggle-fixed-width' specifications."
  702. ;; No region: Toggle on fixed-width marker in paragraphs.
  703. (should
  704. (equal ": A"
  705. (org-test-with-temp-text "A"
  706. (org-toggle-fixed-width)
  707. (buffer-string))))
  708. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  709. (should
  710. (equal "A"
  711. (org-test-with-temp-text ": A"
  712. (org-toggle-fixed-width)
  713. (buffer-string))))
  714. ;; No region: Toggle on marker in blank lines after elements or just
  715. ;; after a headline.
  716. (should
  717. (equal "* H\n: "
  718. (org-test-with-temp-text "* H\n"
  719. (forward-line)
  720. (org-toggle-fixed-width)
  721. (buffer-string))))
  722. (should
  723. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  724. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  725. (goto-char (point-max))
  726. (org-toggle-fixed-width)
  727. (buffer-string))))
  728. ;; No region: Toggle on marker in front of one line elements (e.g.,
  729. ;; headlines, clocks)
  730. (should
  731. (equal ": * Headline"
  732. (org-test-with-temp-text "* Headline"
  733. (org-toggle-fixed-width)
  734. (buffer-string))))
  735. (should
  736. (equal ": #+KEYWORD: value"
  737. (org-test-with-temp-text "#+KEYWORD: value"
  738. (org-toggle-fixed-width)
  739. (buffer-string))))
  740. ;; No region: error in other situations.
  741. (should-error
  742. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  743. (forward-line)
  744. (org-toggle-fixed-width)
  745. (buffer-string)))
  746. ;; No region: Indentation is preserved.
  747. (should
  748. (equal "- A\n : B"
  749. (org-test-with-temp-text "- A\n B"
  750. (forward-line)
  751. (org-toggle-fixed-width)
  752. (buffer-string))))
  753. ;; Region: If it contains only fixed-width elements and blank lines,
  754. ;; toggle off fixed-width markup.
  755. (should
  756. (equal "A\n\nB"
  757. (org-test-with-temp-text ": A\n\n: B"
  758. (transient-mark-mode 1)
  759. (push-mark (point) t t)
  760. (goto-char (point-max))
  761. (org-toggle-fixed-width)
  762. (buffer-string))))
  763. ;; Region: If it contains anything else, toggle on fixed-width but
  764. ;; not on fixed-width areas.
  765. (should
  766. (equal ": A\n: \n: B\n: \n: C"
  767. (org-test-with-temp-text "A\n\n: B\n\nC"
  768. (transient-mark-mode 1)
  769. (push-mark (point) t t)
  770. (goto-char (point-max))
  771. (org-toggle-fixed-width)
  772. (buffer-string))))
  773. ;; Region: Ignore blank lines at its end, unless it contains only
  774. ;; such lines.
  775. (should
  776. (equal ": A\n\n"
  777. (org-test-with-temp-text "A\n\n"
  778. (transient-mark-mode 1)
  779. (push-mark (point) t t)
  780. (goto-char (point-max))
  781. (org-toggle-fixed-width)
  782. (buffer-string))))
  783. (should
  784. (equal ": \n: \n"
  785. (org-test-with-temp-text "\n\n"
  786. (transient-mark-mode 1)
  787. (push-mark (point) t t)
  788. (goto-char (point-max))
  789. (org-toggle-fixed-width)
  790. (buffer-string)))))
  791. ;;; Headline
  792. (ert-deftest test-org/in-commented-heading-p ()
  793. "Test `org-in-commented-heading-p' specifications."
  794. ;; Commented headline.
  795. (should
  796. (org-test-with-temp-text "* COMMENT Headline\nBody"
  797. (goto-char (point-max))
  798. (org-in-commented-heading-p)))
  799. ;; Commented ancestor.
  800. (should
  801. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  802. (goto-char (point-max))
  803. (org-in-commented-heading-p)))
  804. ;; Comment keyword is case-sensitive.
  805. (should-not
  806. (org-test-with-temp-text "* Comment Headline\nBody"
  807. (goto-char (point-max))
  808. (org-in-commented-heading-p)))
  809. ;; Keyword is standalone.
  810. (should-not
  811. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  812. (goto-char (point-max))
  813. (org-in-commented-heading-p)))
  814. ;; Optional argument.
  815. (should-not
  816. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  817. (goto-char (point-max))
  818. (org-in-commented-heading-p t))))
  819. ;;; Links
  820. ;;;; Coderefs
  821. (ert-deftest test-org/coderef ()
  822. "Test coderef links specifications."
  823. (should
  824. (org-test-with-temp-text "
  825. #+BEGIN_SRC emacs-lisp
  826. \(+ 1 1) (ref:sc)
  827. #+END_SRC
  828. \[[(sc)]]"
  829. (goto-char (point-max))
  830. (org-open-at-point)
  831. (looking-at "(ref:sc)"))))
  832. ;;;; Custom ID
  833. (ert-deftest test-org/custom-id ()
  834. "Test custom ID links specifications."
  835. (should
  836. (org-test-with-temp-text
  837. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  838. (goto-char (point-max))
  839. (org-open-at-point)
  840. (org-looking-at-p "\\* H1"))))
  841. ;;;; Fuzzy Links
  842. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  843. ;; a named element (#+name: text) and to headlines (* Text).
  844. (ert-deftest test-org/fuzzy-links ()
  845. "Test fuzzy links specifications."
  846. ;; Fuzzy link goes in priority to a matching target.
  847. (should
  848. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  849. (goto-line 5)
  850. (org-open-at-point)
  851. (looking-at "<<Test>>")))
  852. ;; Then fuzzy link points to an element with a given name.
  853. (should
  854. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  855. (goto-line 5)
  856. (org-open-at-point)
  857. (looking-at "#\\+NAME: Test")))
  858. ;; A target still lead to a matching headline otherwise.
  859. (should
  860. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  861. (goto-line 4)
  862. (org-open-at-point)
  863. (looking-at "\\* Head2")))
  864. ;; With a leading star in link, enforce heading match.
  865. (should
  866. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  867. (goto-line 3)
  868. (org-open-at-point)
  869. (looking-at "\\* Test")))
  870. ;; Correctly un-hexify fuzzy links.
  871. (should
  872. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  873. (goto-char (point-max))
  874. (org-open-at-point)
  875. (bobp))))
  876. ;;;; Link Escaping
  877. (ert-deftest test-org/org-link-escape-ascii-character ()
  878. "Escape an ascii character."
  879. (should
  880. (string=
  881. "%5B"
  882. (org-link-escape "["))))
  883. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  884. "Escape an ascii control character."
  885. (should
  886. (string=
  887. "%09"
  888. (org-link-escape "\t"))))
  889. (ert-deftest test-org/org-link-escape-multibyte-character ()
  890. "Escape an unicode multibyte character."
  891. (should
  892. (string=
  893. "%E2%82%AC"
  894. (org-link-escape "€"))))
  895. (ert-deftest test-org/org-link-escape-custom-table ()
  896. "Escape string with custom character table."
  897. (should
  898. (string=
  899. "Foo%3A%42ar%0A"
  900. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  901. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  902. "Escape string with custom table merged with default table."
  903. (should
  904. (string=
  905. "%5BF%6F%6F%3A%42ar%0A%5D"
  906. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  907. (ert-deftest test-org/org-link-unescape-ascii-character ()
  908. "Unescape an ascii character."
  909. (should
  910. (string=
  911. "["
  912. (org-link-unescape "%5B"))))
  913. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  914. "Unescpae an ascii control character."
  915. (should
  916. (string=
  917. "\n"
  918. (org-link-unescape "%0A"))))
  919. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  920. "Unescape unicode multibyte character."
  921. (should
  922. (string=
  923. "€"
  924. (org-link-unescape "%E2%82%AC"))))
  925. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  926. "Unescape old style percent escaped character."
  927. (should
  928. (string=
  929. "àâçèéêîôùû"
  930. (decode-coding-string
  931. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  932. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  933. "Escape and unescape a URL that includes an escaped char.
  934. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  935. (should
  936. (string=
  937. "http://some.host.com/form?&id=blah%2Bblah25"
  938. (org-link-unescape
  939. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  940. (ert-deftest test-org/org-link-escape-chars-browser ()
  941. "Test of the constant `org-link-escape-chars-browser'.
  942. See there why this test is a candidate to be removed once Org
  943. drops support for Emacs 24.1 and 24.2."
  944. (should
  945. (string=
  946. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  947. "%22Release%208.2%22&idxname=emacs-orgmode")
  948. (org-link-escape-browser ; Do not replace with `url-encode-url',
  949. ; see docstring above.
  950. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  951. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  952. ;;;; Open at point
  953. (ert-deftest test-org/open-at-point ()
  954. "Test `org-open-at-point' with link being a heading property."
  955. (org-test-with-temp-text
  956. "* Headline
  957. :PROPERTIES:
  958. :URL: <point>[[info:org#Top]]
  959. :END:"
  960. (org-open-at-point)))
  961. ;;; Node Properties
  962. (ert-deftest test-org/accumulated-properties-in-drawers ()
  963. "Ensure properties accumulate in subtree drawers."
  964. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  965. (org-babel-next-src-block)
  966. (should (equal '(2 1) (org-babel-execute-src-block)))))
  967. ;;; Mark Region
  968. (ert-deftest test-org/mark-subtree ()
  969. "Test `org-mark-subtree' specifications."
  970. ;; Error when point is before first headline.
  971. (should-error
  972. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  973. (progn (transient-mark-mode 1)
  974. (org-mark-subtree))))
  975. ;; Without argument, mark current subtree.
  976. (should
  977. (equal
  978. '(12 32)
  979. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  980. (progn (transient-mark-mode 1)
  981. (forward-line 2)
  982. (org-mark-subtree)
  983. (list (region-beginning) (region-end))))))
  984. ;; With an argument, move ARG up.
  985. (should
  986. (equal
  987. '(1 32)
  988. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  989. (progn (transient-mark-mode 1)
  990. (forward-line 2)
  991. (org-mark-subtree 1)
  992. (list (region-beginning) (region-end))))))
  993. ;; Do not get fooled by inlinetasks.
  994. (when (featurep 'org-inlinetask)
  995. (should
  996. (= 1
  997. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  998. (progn (transient-mark-mode 1)
  999. (forward-line 1)
  1000. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1001. (region-beginning)))))))
  1002. ;;; Navigation
  1003. (ert-deftest test-org/beginning-of-line ()
  1004. "Test `org-beginning-of-line' specifications."
  1005. ;; Standard test.
  1006. (should
  1007. (org-test-with-temp-text "Some text\nSome other text"
  1008. (progn (org-beginning-of-line) (bolp))))
  1009. ;; Standard test with `visual-line-mode'.
  1010. (should-not
  1011. (org-test-with-temp-text "A long line of text\nSome other text"
  1012. (progn (visual-line-mode)
  1013. (forward-char 2)
  1014. (dotimes (i 1000) (insert "very "))
  1015. (org-beginning-of-line)
  1016. (bolp))))
  1017. ;; At an headline with special movement.
  1018. (should
  1019. (org-test-with-temp-text "* TODO Headline"
  1020. (let ((org-special-ctrl-a/e t))
  1021. (org-end-of-line)
  1022. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1023. (progn (org-beginning-of-line) (bolp))
  1024. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1025. (ert-deftest test-org/end-of-line ()
  1026. "Test `org-end-of-line' specifications."
  1027. ;; Standard test.
  1028. (should
  1029. (org-test-with-temp-text "Some text\nSome other text"
  1030. (progn (org-end-of-line) (eolp))))
  1031. ;; Standard test with `visual-line-mode'.
  1032. (should-not
  1033. (org-test-with-temp-text "A long line of text\nSome other text"
  1034. (progn (visual-line-mode)
  1035. (forward-char 2)
  1036. (dotimes (i 1000) (insert "very "))
  1037. (goto-char (point-min))
  1038. (org-end-of-line)
  1039. (eolp))))
  1040. ;; At an headline with special movement.
  1041. (should
  1042. (org-test-with-temp-text "* Headline1 :tag:\n"
  1043. (let ((org-special-ctrl-a/e t))
  1044. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1045. (progn (org-end-of-line) (eolp))
  1046. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1047. ;; At an headline without special movement.
  1048. (should
  1049. (org-test-with-temp-text "* Headline2 :tag:\n"
  1050. (let ((org-special-ctrl-a/e nil))
  1051. (and (progn (org-end-of-line) (eolp))
  1052. (progn (org-end-of-line) (eolp))))))
  1053. ;; At an headline, with reversed movement.
  1054. (should
  1055. (org-test-with-temp-text "* Headline3 :tag:\n"
  1056. (let ((org-special-ctrl-a/e 'reversed)
  1057. (this-command last-command))
  1058. (and (progn (org-end-of-line) (eolp))
  1059. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1060. ;; At a block without hidden contents.
  1061. (should
  1062. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1063. (progn (org-end-of-line) (eolp))))
  1064. ;; At a block with hidden contents.
  1065. (should-not
  1066. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1067. (let ((org-special-ctrl-a/e t))
  1068. (org-hide-block-toggle)
  1069. (org-end-of-line)
  1070. (eobp)))))
  1071. (ert-deftest test-org/forward-paragraph ()
  1072. "Test `org-forward-paragraph' specifications."
  1073. ;; At end of buffer, return an error.
  1074. (should-error
  1075. (org-test-with-temp-text "Paragraph"
  1076. (goto-char (point-max))
  1077. (org-forward-paragraph)))
  1078. ;; Standard test.
  1079. (should
  1080. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1081. (org-forward-paragraph)
  1082. (looking-at "P2")))
  1083. ;; Ignore depth.
  1084. (should
  1085. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1086. (org-forward-paragraph)
  1087. (looking-at "P1")))
  1088. ;; Do not enter elements with invisible contents.
  1089. (should
  1090. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1091. (org-hide-block-toggle)
  1092. (org-forward-paragraph)
  1093. (looking-at "P3")))
  1094. ;; On an affiliated keyword, jump to the beginning of the element.
  1095. (should
  1096. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1097. (org-forward-paragraph)
  1098. (looking-at "Para")))
  1099. ;; On an item or a footnote definition, move to the second element
  1100. ;; inside, if any.
  1101. (should
  1102. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1103. (org-forward-paragraph)
  1104. (looking-at " Paragraph")))
  1105. (should
  1106. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1107. (org-forward-paragraph)
  1108. (looking-at "Paragraph")))
  1109. ;; On an item, or a footnote definition, when the first line is
  1110. ;; empty, move to the first item.
  1111. (should
  1112. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1113. (org-forward-paragraph)
  1114. (looking-at " Paragraph")))
  1115. (should
  1116. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1117. (org-forward-paragraph)
  1118. (looking-at "Paragraph")))
  1119. ;; On a table (resp. a property drawer) do not move through table
  1120. ;; rows (resp. node properties).
  1121. (should
  1122. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1123. (org-forward-paragraph)
  1124. (looking-at "Paragraph")))
  1125. (should
  1126. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1127. (org-forward-paragraph)
  1128. (looking-at "Paragraph")))
  1129. ;; On a verse or source block, stop after blank lines.
  1130. (should
  1131. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1132. (org-forward-paragraph)
  1133. (looking-at "L2")))
  1134. (should
  1135. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1136. (org-forward-paragraph)
  1137. (looking-at "L2"))))
  1138. (ert-deftest test-org/backward-paragraph ()
  1139. "Test `org-backward-paragraph' specifications."
  1140. ;; Error at beginning of buffer.
  1141. (should-error
  1142. (org-test-with-temp-text "Paragraph"
  1143. (org-backward-paragraph)))
  1144. ;; Regular test.
  1145. (should
  1146. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1147. (goto-char (point-max))
  1148. (org-backward-paragraph)
  1149. (looking-at "P3")))
  1150. (should
  1151. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1152. (goto-char (point-max))
  1153. (beginning-of-line)
  1154. (org-backward-paragraph)
  1155. (looking-at "P2")))
  1156. ;; Ignore depth.
  1157. (should
  1158. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1159. (goto-char (point-max))
  1160. (beginning-of-line)
  1161. (org-backward-paragraph)
  1162. (looking-at "P2")))
  1163. ;; Ignore invisible elements.
  1164. (should
  1165. (org-test-with-temp-text "* H1\n P1\n* H2"
  1166. (org-cycle)
  1167. (goto-char (point-max))
  1168. (beginning-of-line)
  1169. (org-backward-paragraph)
  1170. (bobp)))
  1171. ;; On an affiliated keyword, jump to the first one.
  1172. (should
  1173. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1174. (search-forward "c2")
  1175. (org-backward-paragraph)
  1176. (looking-at "#\\+name")))
  1177. ;; On the second element in an item or a footnote definition, jump
  1178. ;; to item or the definition.
  1179. (should
  1180. (org-test-with-temp-text "- line1\n\n line2"
  1181. (goto-char (point-max))
  1182. (beginning-of-line)
  1183. (org-backward-paragraph)
  1184. (looking-at "- line1")))
  1185. (should
  1186. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1187. (goto-char (point-max))
  1188. (beginning-of-line)
  1189. (org-backward-paragraph)
  1190. (looking-at "\\[fn:1\\] line1")))
  1191. ;; On a table (resp. a property drawer), ignore table rows
  1192. ;; (resp. node properties).
  1193. (should
  1194. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1195. (goto-char (point-max))
  1196. (beginning-of-line)
  1197. (org-backward-paragraph)
  1198. (bobp)))
  1199. (should
  1200. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
  1201. (goto-char (point-max))
  1202. (beginning-of-line)
  1203. (org-backward-paragraph)
  1204. (bobp)))
  1205. ;; On a source or verse block, stop before blank lines.
  1206. (should
  1207. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1208. (search-forward "L3")
  1209. (beginning-of-line)
  1210. (org-backward-paragraph)
  1211. (looking-at "L2")))
  1212. (should
  1213. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1214. (search-forward "L3")
  1215. (beginning-of-line)
  1216. (org-backward-paragraph)
  1217. (looking-at "L2"))))
  1218. (ert-deftest test-org/forward-element ()
  1219. "Test `org-forward-element' specifications."
  1220. ;; 1. At EOB: should error.
  1221. (org-test-with-temp-text "Some text\n"
  1222. (goto-char (point-max))
  1223. (should-error (org-forward-element)))
  1224. ;; 2. Standard move: expected to ignore blank lines.
  1225. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1226. (org-forward-element)
  1227. (should (looking-at (regexp-quote "Second paragraph."))))
  1228. ;; 3. Headline tests.
  1229. (org-test-with-temp-text "
  1230. * Head 1
  1231. ** Head 1.1
  1232. *** Head 1.1.1
  1233. ** Head 1.2"
  1234. ;; 3.1. At an headline beginning: move to next headline at the
  1235. ;; same level.
  1236. (goto-line 3)
  1237. (org-forward-element)
  1238. (should (looking-at (regexp-quote "** Head 1.2")))
  1239. ;; 3.2. At an headline beginning: move to parent headline if no
  1240. ;; headline at the same level.
  1241. (goto-line 3)
  1242. (org-forward-element)
  1243. (should (looking-at (regexp-quote "** Head 1.2"))))
  1244. ;; 4. Greater element tests.
  1245. (org-test-with-temp-text
  1246. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1247. ;; 4.1. At a greater element: expected to skip contents.
  1248. (org-forward-element)
  1249. (should (looking-at (regexp-quote "Outside.")))
  1250. ;; 4.2. At the end of greater element contents: expected to skip
  1251. ;; to the end of the greater element.
  1252. (goto-line 2)
  1253. (org-forward-element)
  1254. (should (looking-at (regexp-quote "Outside."))))
  1255. ;; 5. List tests.
  1256. (org-test-with-temp-text "
  1257. - item1
  1258. - sub1
  1259. - sub2
  1260. - sub3
  1261. Inner paragraph.
  1262. - item2
  1263. Outside."
  1264. ;; 5.1. At list top point: expected to move to the element after
  1265. ;; the list.
  1266. (goto-line 2)
  1267. (org-forward-element)
  1268. (should (looking-at (regexp-quote "Outside.")))
  1269. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1270. ;; beginning of line, move to next item.
  1271. (goto-line 2)
  1272. (forward-char)
  1273. (org-forward-element)
  1274. (should (looking-at "- item2"))
  1275. (goto-line 4)
  1276. (forward-char)
  1277. (org-forward-element)
  1278. (should (looking-at " - sub2"))
  1279. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1280. (goto-line 4)
  1281. (org-forward-element)
  1282. (should (looking-at (regexp-quote " Inner paragraph.")))
  1283. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1284. (goto-line 8)
  1285. (org-forward-element)
  1286. (should (looking-at (regexp-quote " Inner paragraph.")))
  1287. ;; 5.5. At an item: expected to move to next item, if any.
  1288. (goto-line 6)
  1289. (org-forward-element)
  1290. (should (looking-at " - sub3"))))
  1291. (ert-deftest test-org/backward-element ()
  1292. "Test `org-backward-element' specifications."
  1293. ;; 1. Should error at BOB.
  1294. (org-test-with-temp-text " \nParagraph."
  1295. (should-error (org-backward-element)))
  1296. ;; 2. Should move at BOB when called on the first element in buffer.
  1297. (should
  1298. (org-test-with-temp-text "\n#+TITLE: test"
  1299. (progn (forward-line)
  1300. (org-backward-element)
  1301. (bobp))))
  1302. ;; 3. Not at the beginning of an element: move at its beginning.
  1303. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1304. (goto-line 3)
  1305. (end-of-line)
  1306. (org-backward-element)
  1307. (should (looking-at (regexp-quote "Paragraph2."))))
  1308. ;; 4. Headline tests.
  1309. (org-test-with-temp-text "
  1310. * Head 1
  1311. ** Head 1.1
  1312. *** Head 1.1.1
  1313. ** Head 1.2"
  1314. ;; 4.1. At an headline beginning: move to previous headline at the
  1315. ;; same level.
  1316. (goto-line 5)
  1317. (org-backward-element)
  1318. (should (looking-at (regexp-quote "** Head 1.1")))
  1319. ;; 4.2. At an headline beginning: move to parent headline if no
  1320. ;; headline at the same level.
  1321. (goto-line 3)
  1322. (org-backward-element)
  1323. (should (looking-at (regexp-quote "* Head 1")))
  1324. ;; 4.3. At the first top-level headline: should error.
  1325. (goto-line 2)
  1326. (should-error (org-backward-element)))
  1327. ;; 5. At beginning of first element inside a greater element:
  1328. ;; expected to move to greater element's beginning.
  1329. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1330. (goto-line 3)
  1331. (org-backward-element)
  1332. (should (looking-at "#\\+BEGIN_CENTER")))
  1333. ;; 6. At the beginning of the first element in a section: should
  1334. ;; move back to headline, if any.
  1335. (should
  1336. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1337. (progn (goto-char (point-max))
  1338. (beginning-of-line)
  1339. (org-backward-element)
  1340. (org-at-heading-p))))
  1341. ;; 7. List tests.
  1342. (org-test-with-temp-text "
  1343. - item1
  1344. - sub1
  1345. - sub2
  1346. - sub3
  1347. Inner paragraph.
  1348. - item2
  1349. Outside."
  1350. ;; 7.1. At beginning of sub-list: expected to move to the
  1351. ;; paragraph before it.
  1352. (goto-line 4)
  1353. (org-backward-element)
  1354. (should (looking-at "item1"))
  1355. ;; 7.2. At an item in a list: expected to move at previous item.
  1356. (goto-line 8)
  1357. (org-backward-element)
  1358. (should (looking-at " - sub2"))
  1359. (goto-line 12)
  1360. (org-backward-element)
  1361. (should (looking-at "- item1"))
  1362. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1363. ;; beginning.
  1364. (goto-line 10)
  1365. (org-backward-element)
  1366. (should (looking-at " - sub1"))
  1367. (goto-line 15)
  1368. (org-backward-element)
  1369. (should (looking-at "- item1"))
  1370. ;; 7.4. At blank-lines before list end: expected to move to top
  1371. ;; item.
  1372. (goto-line 14)
  1373. (org-backward-element)
  1374. (should (looking-at "- item1"))))
  1375. (ert-deftest test-org/up-element ()
  1376. "Test `org-up-element' specifications."
  1377. ;; 1. At BOB or with no surrounding element: should error.
  1378. (org-test-with-temp-text "Paragraph."
  1379. (should-error (org-up-element)))
  1380. (org-test-with-temp-text "* Head1\n* Head2"
  1381. (goto-line 2)
  1382. (should-error (org-up-element)))
  1383. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1384. (goto-line 3)
  1385. (should-error (org-up-element)))
  1386. ;; 2. At an headline: move to parent headline.
  1387. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1388. (goto-line 3)
  1389. (org-up-element)
  1390. (should (looking-at "\\* Head1")))
  1391. ;; 3. Inside a greater element: move to greater element beginning.
  1392. (org-test-with-temp-text
  1393. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1394. (goto-line 3)
  1395. (org-up-element)
  1396. (should (looking-at "#\\+BEGIN_CENTER")))
  1397. ;; 4. List tests.
  1398. (org-test-with-temp-text "* Top
  1399. - item1
  1400. - sub1
  1401. - sub2
  1402. Paragraph within sub2.
  1403. - item2"
  1404. ;; 4.1. Within an item: move to the item beginning.
  1405. (goto-line 8)
  1406. (org-up-element)
  1407. (should (looking-at " - sub2"))
  1408. ;; 4.2. At an item in a sub-list: move to parent item.
  1409. (goto-line 4)
  1410. (org-up-element)
  1411. (should (looking-at "- item1"))
  1412. ;; 4.3. At an item in top list: move to beginning of whole list.
  1413. (goto-line 10)
  1414. (org-up-element)
  1415. (should (looking-at "- item1"))
  1416. ;; 4.4. Special case. At very top point: should move to parent of
  1417. ;; list.
  1418. (goto-line 2)
  1419. (org-up-element)
  1420. (should (looking-at "\\* Top"))))
  1421. (ert-deftest test-org/down-element ()
  1422. "Test `org-down-element' specifications."
  1423. ;; Error when the element hasn't got a recursive type.
  1424. (org-test-with-temp-text "Paragraph."
  1425. (should-error (org-down-element)))
  1426. ;; Error when the element has no contents
  1427. (org-test-with-temp-text "* Headline"
  1428. (should-error (org-down-element)))
  1429. ;; When at a plain-list, move to first item.
  1430. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1431. (goto-line 2)
  1432. (org-down-element)
  1433. (should (looking-at " - Item 1.1")))
  1434. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1435. (org-down-element)
  1436. (should (looking-at " Item 1")))
  1437. ;; When at a table, move to first row
  1438. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1439. (org-down-element)
  1440. (should (looking-at " a | b |")))
  1441. ;; Otherwise, move inside the greater element.
  1442. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1443. (org-down-element)
  1444. (should (looking-at "Paragraph"))))
  1445. (ert-deftest test-org/drag-element-backward ()
  1446. "Test `org-drag-element-backward' specifications."
  1447. ;; 1. Error when trying to move first element of buffer.
  1448. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1449. (should-error (org-drag-element-backward)))
  1450. ;; 2. Error when trying to swap nested elements.
  1451. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1452. (forward-line)
  1453. (should-error (org-drag-element-backward)))
  1454. ;; 3. Error when trying to swap an headline element and
  1455. ;; a non-headline element.
  1456. (org-test-with-temp-text "Test.\n* Head 1"
  1457. (forward-line)
  1458. (should-error (org-drag-element-backward)))
  1459. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1460. ;; between elements.
  1461. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  1462. (search-forward "graph")
  1463. (org-drag-element-backward)
  1464. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  1465. (should (looking-at " 2")))
  1466. ;; 5. Preserve visibility of elements and their contents.
  1467. (org-test-with-temp-text "
  1468. #+BEGIN_CENTER
  1469. Text.
  1470. #+END_CENTER
  1471. - item 1
  1472. #+BEGIN_QUOTE
  1473. Text.
  1474. #+END_QUOTE"
  1475. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1476. (search-backward "- item 1")
  1477. (org-drag-element-backward)
  1478. (should
  1479. (equal
  1480. '((63 . 82) (26 . 48))
  1481. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1482. (overlays-in (point-min) (point-max)))))))
  1483. (ert-deftest test-org/drag-element-forward ()
  1484. "Test `org-drag-element-forward' specifications."
  1485. ;; 1. Error when trying to move first element of buffer.
  1486. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1487. (goto-line 3)
  1488. (should-error (org-drag-element-forward)))
  1489. ;; 2. Error when trying to swap nested elements.
  1490. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1491. (forward-line)
  1492. (should-error (org-drag-element-forward)))
  1493. ;; 3. Error when trying to swap a non-headline element and an
  1494. ;; headline.
  1495. (org-test-with-temp-text "Test.\n* Head 1"
  1496. (should-error (org-drag-element-forward)))
  1497. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1498. ;; between elements.
  1499. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1500. (search-forward "graph")
  1501. (org-drag-element-forward)
  1502. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1503. (should (looking-at " 1")))
  1504. ;; 5. Preserve visibility of elements and their contents.
  1505. (org-test-with-temp-text "
  1506. #+BEGIN_CENTER
  1507. Text.
  1508. #+END_CENTER
  1509. - item 1
  1510. #+BEGIN_QUOTE
  1511. Text.
  1512. #+END_QUOTE"
  1513. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1514. (search-backward "#+BEGIN_CENTER")
  1515. (org-drag-element-forward)
  1516. (should
  1517. (equal
  1518. '((63 . 82) (26 . 48))
  1519. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1520. (overlays-in (point-min) (point-max)))))))
  1521. ;;; Planning
  1522. (ert-deftest test-org/timestamp-has-time-p ()
  1523. "Test `org-timestamp-has-time-p' specifications."
  1524. ;; With time.
  1525. (should
  1526. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1527. (org-timestamp-has-time-p (org-element-context))))
  1528. ;; Without time.
  1529. (should-not
  1530. (org-test-with-temp-text "<2012-03-29 Thu>"
  1531. (org-timestamp-has-time-p (org-element-context)))))
  1532. (ert-deftest test-org/timestamp-format ()
  1533. "Test `org-timestamp-format' specifications."
  1534. ;; Regular test.
  1535. (should
  1536. (equal
  1537. "2012-03-29 16:40"
  1538. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1539. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1540. ;; Range end.
  1541. (should
  1542. (equal
  1543. "2012-03-29"
  1544. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1545. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1546. (ert-deftest test-org/timestamp-split-range ()
  1547. "Test `org-timestamp-split-range' specifications."
  1548. ;; Extract range start (active).
  1549. (should
  1550. (equal '(2012 3 29)
  1551. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1552. (let ((ts (org-timestamp-split-range (org-element-context))))
  1553. (mapcar (lambda (p) (org-element-property p ts))
  1554. '(:year-end :month-end :day-end))))))
  1555. ;; Extract range start (inactive)
  1556. (should
  1557. (equal '(2012 3 29)
  1558. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1559. (let ((ts (org-timestamp-split-range (org-element-context))))
  1560. (mapcar (lambda (p) (org-element-property p ts))
  1561. '(:year-end :month-end :day-end))))))
  1562. ;; Extract range end (active).
  1563. (should
  1564. (equal '(2012 3 30)
  1565. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1566. (let ((ts (org-timestamp-split-range
  1567. (org-element-context) t)))
  1568. (mapcar (lambda (p) (org-element-property p ts))
  1569. '(:year-end :month-end :day-end))))))
  1570. ;; Extract range end (inactive)
  1571. (should
  1572. (equal '(2012 3 30)
  1573. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1574. (let ((ts (org-timestamp-split-range
  1575. (org-element-context) t)))
  1576. (mapcar (lambda (p) (org-element-property p ts))
  1577. '(:year-end :month-end :day-end))))))
  1578. ;; Return the timestamp if not a range.
  1579. (should
  1580. (org-test-with-temp-text "[2012-03-29 Thu]"
  1581. (let* ((ts-orig (org-element-context))
  1582. (ts-copy (org-timestamp-split-range ts-orig)))
  1583. (eq ts-orig ts-copy))))
  1584. (should
  1585. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1586. (let* ((ts-orig (org-element-context))
  1587. (ts-copy (org-timestamp-split-range ts-orig)))
  1588. (eq ts-orig ts-copy))))
  1589. ;; Check that parent is the same when a range was split.
  1590. (should
  1591. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1592. (let* ((ts-orig (org-element-context))
  1593. (ts-copy (org-timestamp-split-range ts-orig)))
  1594. (eq (org-element-property :parent ts-orig)
  1595. (org-element-property :parent ts-copy))))))
  1596. (ert-deftest test-org/timestamp-translate ()
  1597. "Test `org-timestamp-translate' specifications."
  1598. ;; Translate whole date range.
  1599. (should
  1600. (equal "<29>--<30>"
  1601. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1602. (let ((org-display-custom-times t)
  1603. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1604. (org-timestamp-translate (org-element-context))))))
  1605. ;; Translate date range start.
  1606. (should
  1607. (equal "<29>"
  1608. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1609. (let ((org-display-custom-times t)
  1610. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1611. (org-timestamp-translate (org-element-context) 'start)))))
  1612. ;; Translate date range end.
  1613. (should
  1614. (equal "<30>"
  1615. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1616. (let ((org-display-custom-times t)
  1617. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1618. (org-timestamp-translate (org-element-context) 'end)))))
  1619. ;; Translate time range.
  1620. (should
  1621. (equal "<08>--<16>"
  1622. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1623. (let ((org-display-custom-times t)
  1624. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1625. (org-timestamp-translate (org-element-context))))))
  1626. ;; Translate non-range timestamp.
  1627. (should
  1628. (equal "<29>"
  1629. (org-test-with-temp-text "<2012-03-29 Thu>"
  1630. (let ((org-display-custom-times t)
  1631. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1632. (org-timestamp-translate (org-element-context))))))
  1633. ;; Do not change `diary' timestamps.
  1634. (should
  1635. (equal "<%%(org-float t 4 2)>"
  1636. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1637. (let ((org-display-custom-times t)
  1638. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1639. (org-timestamp-translate (org-element-context)))))))
  1640. ;;; Radio Targets
  1641. (ert-deftest test-org/update-radio-target-regexp ()
  1642. "Test `org-update-radio-target-regexp' specifications."
  1643. ;; Properly update cache with no previous radio target regexp.
  1644. (should
  1645. (eq 'link
  1646. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  1647. (save-excursion (goto-char (point-max)) (org-element-context))
  1648. (insert "<<<")
  1649. (search-forward "o")
  1650. (insert ">>>")
  1651. (org-update-radio-target-regexp)
  1652. (goto-char (point-max))
  1653. (org-element-type (org-element-context)))))
  1654. ;; Properly update cache with previous radio target regexp.
  1655. (should
  1656. (eq 'link
  1657. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  1658. (save-excursion (goto-char (point-max)) (org-element-context))
  1659. (insert "<<<")
  1660. (search-forward "o")
  1661. (insert ">>>")
  1662. (org-update-radio-target-regexp)
  1663. (search-backward "r")
  1664. (delete-char 5)
  1665. (insert "new")
  1666. (org-update-radio-target-regexp)
  1667. (goto-char (point-max))
  1668. (delete-region (line-beginning-position) (point))
  1669. (insert "new")
  1670. (org-element-type (org-element-context))))))
  1671. ;;; Visibility
  1672. (ert-deftest test-org/flag-drawer ()
  1673. "Test `org-flag-drawer' specifications."
  1674. ;; Hide drawer.
  1675. (should
  1676. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1677. (org-flag-drawer t)
  1678. (get-char-property (line-end-position) 'invisible)))
  1679. ;; Show drawer.
  1680. (should-not
  1681. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1682. (org-flag-drawer t)
  1683. (org-flag-drawer nil)
  1684. (get-char-property (line-end-position) 'invisible)))
  1685. ;; Test optional argument.
  1686. (should
  1687. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1688. (let ((drawer (save-excursion (search-forward ":D2")
  1689. (org-element-at-point))))
  1690. (org-flag-drawer t drawer)
  1691. (get-char-property (progn (search-forward ":D2") (line-end-position))
  1692. 'invisible))))
  1693. (should-not
  1694. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1695. (let ((drawer (save-excursion (search-forward ":D2")
  1696. (org-element-at-point))))
  1697. (org-flag-drawer t drawer)
  1698. (get-char-property (line-end-position) 'invisible))))
  1699. ;; Do not hide fake drawers.
  1700. (should-not
  1701. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  1702. (forward-line 1)
  1703. (org-flag-drawer t)
  1704. (get-char-property (line-end-position) 'invisible)))
  1705. ;; Do not hide incomplete drawers.
  1706. (should-not
  1707. (org-test-with-temp-text ":D:\nparagraph"
  1708. (forward-line 1)
  1709. (org-flag-drawer t)
  1710. (get-char-property (line-end-position) 'invisible)))
  1711. ;; Do not hide drawers when called from final blank lines.
  1712. (should-not
  1713. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  1714. (goto-char (point-max))
  1715. (org-flag-drawer t)
  1716. (goto-char (point-min))
  1717. (get-char-property (line-end-position) 'invisible)))
  1718. ;; Don't leave point in an invisible part of the buffer when hiding
  1719. ;; a drawer away.
  1720. (should-not
  1721. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1722. (goto-char (point-max))
  1723. (org-flag-drawer t)
  1724. (get-char-property (point) 'invisible))))
  1725. (provide 'test-org)
  1726. ;;; test-org.el ends here