test-org.el 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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/comment-dwim ()
  20. "Test `comment-dwim' behaviour in an Org buffer."
  21. ;; No region selected, no comment on current line and line not
  22. ;; empty: insert comment on line above.
  23. (should
  24. (equal "# \nComment"
  25. (org-test-with-temp-text "Comment"
  26. (progn (call-interactively 'comment-dwim)
  27. (buffer-string)))))
  28. ;; No region selected, no comment on current line and line empty:
  29. ;; insert comment on this line.
  30. (should
  31. (equal "# \nParagraph"
  32. (org-test-with-temp-text "\nParagraph"
  33. (progn (call-interactively 'comment-dwim)
  34. (buffer-string)))))
  35. ;; No region selected, and a comment on this line: indent it.
  36. (should
  37. (equal "* Headline\n # Comment"
  38. (org-test-with-temp-text "* Headline\n# Comment"
  39. (progn (forward-line)
  40. (let ((org-adapt-indentation t))
  41. (call-interactively 'comment-dwim))
  42. (buffer-string)))))
  43. ;; Also recognize single # at column 0 as comments.
  44. (should
  45. (equal "# Comment"
  46. (org-test-with-temp-text "# Comment"
  47. (progn (forward-line)
  48. (call-interactively 'comment-dwim)
  49. (buffer-string)))))
  50. ;; Region selected and only comments and blank lines within it:
  51. ;; un-comment all commented lines.
  52. (should
  53. (equal "Comment 1\n\nComment 2"
  54. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  55. (progn
  56. (transient-mark-mode 1)
  57. (push-mark (point) t t)
  58. (goto-char (point-max))
  59. (call-interactively 'comment-dwim)
  60. (buffer-string)))))
  61. ;; Region selected without comments: comment all lines if
  62. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  63. (should
  64. (equal "# Comment 1\n\n# Comment 2"
  65. (org-test-with-temp-text "Comment 1\n\nComment 2"
  66. (progn
  67. (transient-mark-mode 1)
  68. (push-mark (point) t t)
  69. (goto-char (point-max))
  70. (let ((comment-empty-lines nil))
  71. (call-interactively 'comment-dwim))
  72. (buffer-string)))))
  73. (should
  74. (equal "# Comment 1\n# \n# Comment 2"
  75. (org-test-with-temp-text "Comment 1\n\nComment 2"
  76. (progn
  77. (transient-mark-mode 1)
  78. (push-mark (point) t t)
  79. (goto-char (point-max))
  80. (let ((comment-empty-lines t))
  81. (call-interactively 'comment-dwim))
  82. (buffer-string)))))
  83. ;; In front of a keyword without region, insert a new comment.
  84. (should
  85. (equal "# \n#+KEYWORD: value"
  86. (org-test-with-temp-text "#+KEYWORD: value"
  87. (progn (call-interactively 'comment-dwim)
  88. (buffer-string))))))
  89. ;;; Date and time analysis
  90. (ert-deftest test-org/org-read-date ()
  91. "Test `org-read-date' specifications."
  92. ;; Parse ISO date with abbreviated year and month.
  93. (should (equal "2012-03-29 16:40"
  94. (let ((org-time-was-given t))
  95. (org-read-date t nil "12-3-29 16:40"))))
  96. ;; Parse Europeans dates.
  97. (should (equal "2012-03-29 16:40"
  98. (let ((org-time-was-given t))
  99. (org-read-date t nil "29.03.2012 16:40"))))
  100. ;; Parse Europeans dates without year.
  101. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  102. (let ((org-time-was-given t))
  103. (org-read-date t nil "29.03. 16:40")))))
  104. (ert-deftest test-org/org-parse-time-string ()
  105. "Test `org-parse-time-string'."
  106. (should (equal (org-parse-time-string "2012-03-29 16:40")
  107. '(0 40 16 29 3 2012 nil nil nil)))
  108. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  109. '(0 40 16 29 3 2012 nil nil nil)))
  110. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  111. '(0 40 16 29 3 2012 nil nil nil)))
  112. (should (equal (org-parse-time-string "<2012-03-29>")
  113. '(0 0 0 29 3 2012 nil nil nil)))
  114. (should (equal (org-parse-time-string "<2012-03-29>" t)
  115. '(0 nil nil 29 3 2012 nil nil nil))))
  116. ;;; Filling
  117. (ert-deftest test-org/fill-paragraph ()
  118. "Test `org-fill-paragraph' specifications."
  119. ;; At an Org table, align it.
  120. (should
  121. (equal "| a |\n"
  122. (org-test-with-temp-text "|a|"
  123. (org-fill-paragraph)
  124. (buffer-string))))
  125. (should
  126. (equal "#+name: table\n| a |\n"
  127. (org-test-with-temp-text "#+name: table\n| a |"
  128. (org-fill-paragraph)
  129. (buffer-string))))
  130. ;; At a paragraph, preserve line breaks.
  131. (org-test-with-temp-text "some \\\\\nlong\ntext"
  132. (let ((fill-column 20))
  133. (org-fill-paragraph)
  134. (should (equal (buffer-string) "some \\\\\nlong text"))))
  135. ;; Correctly fill a paragraph when point is at its very end.
  136. (should
  137. (equal "A B"
  138. (org-test-with-temp-text "A\nB"
  139. (let ((fill-column 20))
  140. (goto-char (point-max))
  141. (org-fill-paragraph)
  142. (buffer-string)))))
  143. ;; Correctly fill the last paragraph of a greater element.
  144. (should
  145. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  146. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  147. (let ((fill-column 8))
  148. (forward-line)
  149. (end-of-line)
  150. (org-fill-paragraph)
  151. (buffer-string)))))
  152. ;; Correctly fill an element in a narrowed buffer.
  153. (should
  154. (equal "01234\n6"
  155. (org-test-with-temp-text "01234 6789"
  156. (let ((fill-column 5))
  157. (narrow-to-region 1 8)
  158. (org-fill-paragraph)
  159. (buffer-string)))))
  160. ;; Handle `adaptive-fill-regexp' in paragraphs.
  161. (should
  162. (equal "> a b"
  163. (org-test-with-temp-text "> a\n> b"
  164. (let ((fill-column 5)
  165. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  166. (org-fill-paragraph)
  167. (buffer-string)))))
  168. ;; Special case: Fill first paragraph when point is at an item or
  169. ;; a plain-list or a footnote reference.
  170. (should
  171. (equal "- A B"
  172. (org-test-with-temp-text "- A\n B"
  173. (let ((fill-column 20))
  174. (org-fill-paragraph)
  175. (buffer-string)))))
  176. (should
  177. (equal "[fn:1] A B"
  178. (org-test-with-temp-text "[fn:1] A\nB"
  179. (let ((fill-column 20))
  180. (org-fill-paragraph)
  181. (buffer-string)))))
  182. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  183. (let ((fill-column 20))
  184. (org-fill-paragraph)
  185. (should (equal (buffer-string)
  186. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  187. ;; Fill contents of `comment-block' elements.
  188. (should
  189. (equal
  190. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  191. (let ((fill-column 20))
  192. (forward-line)
  193. (org-fill-paragraph)
  194. (buffer-string)))
  195. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  196. ;; Fill `comment' elements.
  197. (should
  198. (equal " # A B"
  199. (org-test-with-temp-text " # A\n # B"
  200. (let ((fill-column 20))
  201. (org-fill-paragraph)
  202. (buffer-string)))))
  203. ;; Do not mix consecutive comments when filling one of them.
  204. (should
  205. (equal "# A B\n\n# C"
  206. (org-test-with-temp-text "# A\n# B\n\n# C"
  207. (let ((fill-column 20))
  208. (org-fill-paragraph)
  209. (buffer-string)))))
  210. ;; Use commented empty lines as separators when filling comments.
  211. (should
  212. (equal "# A B\n#\n# C"
  213. (org-test-with-temp-text "# A\n# B\n#\n# C"
  214. (let ((fill-column 20))
  215. (org-fill-paragraph)
  216. (buffer-string)))))
  217. ;; Handle `adaptive-fill-regexp' in comments.
  218. (should
  219. (equal "# > a b"
  220. (org-test-with-temp-text "# > a\n# > b"
  221. (let ((fill-column 20)
  222. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  223. (org-fill-paragraph)
  224. (buffer-string)))))
  225. ;; Do nothing at affiliated keywords.
  226. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  227. (let ((fill-column 20))
  228. (org-fill-paragraph)
  229. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  230. ;; Do not move point after table when filling a table.
  231. (should-not
  232. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  233. (forward-char)
  234. (org-fill-paragraph)
  235. (eobp))))
  236. (ert-deftest test-org/auto-fill-function ()
  237. "Test auto-filling features."
  238. ;; Auto fill paragraph.
  239. (should
  240. (equal "12345\n7890"
  241. (org-test-with-temp-text "12345 7890"
  242. (let ((fill-column 5))
  243. (end-of-line)
  244. (org-auto-fill-function)
  245. (buffer-string)))))
  246. ;; Auto fill first paragraph in an item.
  247. (should
  248. (equal "- 12345\n 7890"
  249. (org-test-with-temp-text "- 12345 7890"
  250. (let ((fill-column 7))
  251. (end-of-line)
  252. (org-auto-fill-function)
  253. (buffer-string)))))
  254. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  255. (should
  256. (equal "> 12345\n 7890"
  257. (org-test-with-temp-text "> 12345 7890"
  258. (let ((fill-column 10)
  259. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  260. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  261. (end-of-line)
  262. (org-auto-fill-function)
  263. (buffer-string)))))
  264. (should
  265. (equal "> 12345\n> 12345\n> 7890"
  266. (org-test-with-temp-text "> 12345\n> 12345 7890"
  267. (let ((fill-column 10)
  268. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  269. (goto-char (point-max))
  270. (org-auto-fill-function)
  271. (buffer-string)))))
  272. (should-not
  273. (equal " 12345\n *12345\n *12345"
  274. (org-test-with-temp-text " 12345\n *12345 12345"
  275. (let ((fill-column 10)
  276. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  277. (goto-char (point-max))
  278. (org-auto-fill-function)
  279. (buffer-string)))))
  280. ;; Auto fill comments.
  281. (should
  282. (equal " # 12345\n # 7890"
  283. (org-test-with-temp-text " # 12345 7890"
  284. (let ((fill-column 10))
  285. (end-of-line)
  286. (org-auto-fill-function)
  287. (buffer-string)))))
  288. ;; A hash within a line isn't a comment.
  289. (should-not
  290. (equal "12345 # 7890\n# 1"
  291. (org-test-with-temp-text "12345 # 7890 1"
  292. (let ((fill-column 12))
  293. (end-of-line)
  294. (org-auto-fill-function)
  295. (buffer-string)))))
  296. ;; Correctly interpret empty prefix.
  297. (should-not
  298. (equal "# a\n# b\nRegular\n# paragraph"
  299. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  300. (let ((fill-column 12))
  301. (end-of-line 3)
  302. (org-auto-fill-function)
  303. (buffer-string)))))
  304. ;; Comment block: auto fill contents.
  305. (should
  306. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  307. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  308. (let ((fill-column 5))
  309. (forward-line)
  310. (end-of-line)
  311. (org-auto-fill-function)
  312. (buffer-string)))))
  313. (should
  314. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  315. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  316. (let ((fill-column 5))
  317. (forward-line)
  318. (end-of-line)
  319. (org-auto-fill-function)
  320. (buffer-string)))))
  321. ;; Do not fill if a new item could be created.
  322. (should-not
  323. (equal "12345\n- 90"
  324. (org-test-with-temp-text "12345 - 90"
  325. (let ((fill-column 5))
  326. (end-of-line)
  327. (org-auto-fill-function)
  328. (buffer-string)))))
  329. ;; Do not fill if a line break could be introduced.
  330. (should-not
  331. (equal "123\\\\\n7890"
  332. (org-test-with-temp-text "123\\\\ 7890"
  333. (let ((fill-column 6))
  334. (end-of-line)
  335. (org-auto-fill-function)
  336. (buffer-string)))))
  337. ;; Do not fill affiliated keywords.
  338. (should-not
  339. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  340. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  341. (let ((fill-column 20))
  342. (end-of-line)
  343. (org-auto-fill-function)
  344. (buffer-string))))))
  345. ;;; Editing
  346. ;;;; Insert elements
  347. (ert-deftest test-org/meta-return ()
  348. "Test M-RET (`org-meta-return')."
  349. ;; In a table field insert a row above.
  350. (should
  351. (org-test-with-temp-text "| a |"
  352. (forward-char)
  353. (org-meta-return)
  354. (forward-line -1)
  355. (looking-at "| |$")))
  356. ;; In a paragraph change current line into a header.
  357. (should
  358. (org-test-with-temp-text "a"
  359. (org-meta-return)
  360. (beginning-of-line)
  361. (looking-at "\* a$")))
  362. ;; In an item insert an item, in this case above.
  363. (should
  364. (org-test-with-temp-text "- a"
  365. (org-meta-return)
  366. (beginning-of-line)
  367. (looking-at "- $")))
  368. ;; In a drawer and paragraph insert an empty line, in this case above.
  369. (should
  370. (let ((org-drawers '("MYDRAWER")))
  371. (org-test-with-temp-text ":MYDRAWER:\na\n:END:"
  372. (forward-line)
  373. (org-meta-return)
  374. (forward-line -1)
  375. (looking-at "$"))))
  376. ;; In a drawer and item insert an item, in this case above.
  377. (should
  378. (let ((org-drawers '("MYDRAWER")))
  379. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  380. (forward-line)
  381. (org-meta-return)
  382. (beginning-of-line)
  383. (looking-at "- $")))))
  384. ;;; Links
  385. ;;;; Fuzzy Links
  386. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  387. ;; a named element (#+name: text) and to headlines (* Text).
  388. (ert-deftest test-org/fuzzy-links ()
  389. "Test fuzzy links specifications."
  390. ;; 1. Fuzzy link goes in priority to a matching target.
  391. (should
  392. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  393. (goto-line 5)
  394. (org-open-at-point)
  395. (looking-at "<<Test>>")))
  396. ;; 2. Then fuzzy link points to an element with a given name.
  397. (should
  398. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  399. (goto-line 5)
  400. (org-open-at-point)
  401. (looking-at "#\\+NAME: Test")))
  402. ;; 3. A target still lead to a matching headline otherwise.
  403. (should
  404. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  405. (goto-line 4)
  406. (org-open-at-point)
  407. (looking-at "\\* Head2")))
  408. ;; 4. With a leading star in link, enforce heading match.
  409. (should
  410. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  411. (goto-line 3)
  412. (org-open-at-point)
  413. (looking-at "\\* Test"))))
  414. ;;;; Link Escaping
  415. (ert-deftest test-org/org-link-escape-ascii-character ()
  416. "Escape an ascii character."
  417. (should
  418. (string=
  419. "%5B"
  420. (org-link-escape "["))))
  421. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  422. "Escape an ascii control character."
  423. (should
  424. (string=
  425. "%09"
  426. (org-link-escape "\t"))))
  427. (ert-deftest test-org/org-link-escape-multibyte-character ()
  428. "Escape an unicode multibyte character."
  429. (should
  430. (string=
  431. "%E2%82%AC"
  432. (org-link-escape "€"))))
  433. (ert-deftest test-org/org-link-escape-custom-table ()
  434. "Escape string with custom character table."
  435. (should
  436. (string=
  437. "Foo%3A%42ar%0A"
  438. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  439. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  440. "Escape string with custom table merged with default table."
  441. (should
  442. (string=
  443. "%5BF%6F%6F%3A%42ar%0A%5D"
  444. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  445. (ert-deftest test-org/org-link-unescape-ascii-character ()
  446. "Unescape an ascii character."
  447. (should
  448. (string=
  449. "["
  450. (org-link-unescape "%5B"))))
  451. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  452. "Unescpae an ascii control character."
  453. (should
  454. (string=
  455. "\n"
  456. (org-link-unescape "%0A"))))
  457. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  458. "Unescape unicode multibyte character."
  459. (should
  460. (string=
  461. "€"
  462. (org-link-unescape "%E2%82%AC"))))
  463. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  464. "Unescape old style percent escaped character."
  465. (should
  466. (string=
  467. "àâçèéêîôùû"
  468. (decode-coding-string
  469. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  470. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  471. "Escape and unescape a URL that includes an escaped char.
  472. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  473. (should
  474. (string=
  475. "http://some.host.com/form?&id=blah%2Bblah25"
  476. (org-link-unescape
  477. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  478. (ert-deftest test-org/org-link-escape-chars-browser ()
  479. "Escape a URL to pass to `browse-url'."
  480. (should
  481. (string=
  482. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  483. "%22Release%208.2%22&idxname=emacs-orgmode")
  484. (org-link-escape-browser
  485. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  486. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  487. ;;; Node Properties
  488. (ert-deftest test-org/accumulated-properties-in-drawers ()
  489. "Ensure properties accumulate in subtree drawers."
  490. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  491. (org-babel-next-src-block)
  492. (should (equal '(2 1) (org-babel-execute-src-block)))))
  493. ;;; Mark Region
  494. (ert-deftest test-org/mark-subtree ()
  495. "Test `org-mark-subtree' specifications."
  496. ;; Error when point is before first headline.
  497. (should-error
  498. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  499. (progn (transient-mark-mode 1)
  500. (org-mark-subtree))))
  501. ;; Without argument, mark current subtree.
  502. (should
  503. (equal
  504. '(12 32)
  505. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  506. (progn (transient-mark-mode 1)
  507. (forward-line 2)
  508. (org-mark-subtree)
  509. (list (region-beginning) (region-end))))))
  510. ;; With an argument, move ARG up.
  511. (should
  512. (equal
  513. '(1 32)
  514. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  515. (progn (transient-mark-mode 1)
  516. (forward-line 2)
  517. (org-mark-subtree 1)
  518. (list (region-beginning) (region-end))))))
  519. ;; Do not get fooled by inlinetasks.
  520. (when (featurep 'org-inlinetask)
  521. (should
  522. (= 1
  523. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  524. (progn (transient-mark-mode 1)
  525. (forward-line 1)
  526. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  527. (region-beginning)))))))
  528. ;;; Navigation
  529. (ert-deftest test-org/beginning-of-line ()
  530. "Test `org-beginning-of-line' specifications."
  531. ;; Standard test.
  532. (should
  533. (org-test-with-temp-text "Some text\nSome other text"
  534. (progn (org-beginning-of-line) (bolp))))
  535. ;; Standard test with `visual-line-mode'.
  536. (should-not
  537. (org-test-with-temp-text "A long line of text\nSome other text"
  538. (progn (visual-line-mode)
  539. (forward-char 2)
  540. (dotimes (i 1000) (insert "very "))
  541. (org-beginning-of-line)
  542. (bolp))))
  543. ;; At an headline with special movement.
  544. (should
  545. (org-test-with-temp-text "* TODO Headline"
  546. (let ((org-special-ctrl-a/e t))
  547. (org-end-of-line)
  548. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  549. (progn (org-beginning-of-line) (bolp))
  550. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  551. (ert-deftest test-org/end-of-line ()
  552. "Test `org-end-of-line' specifications."
  553. ;; Standard test.
  554. (should
  555. (org-test-with-temp-text "Some text\nSome other text"
  556. (progn (org-end-of-line) (eolp))))
  557. ;; Standard test with `visual-line-mode'.
  558. (should-not
  559. (org-test-with-temp-text "A long line of text\nSome other text"
  560. (progn (visual-line-mode)
  561. (forward-char 2)
  562. (dotimes (i 1000) (insert "very "))
  563. (goto-char (point-min))
  564. (org-end-of-line)
  565. (eolp))))
  566. ;; At an headline with special movement.
  567. (should
  568. (org-test-with-temp-text "* Headline1 :tag:\n"
  569. (let ((org-special-ctrl-a/e t))
  570. (and (progn (org-end-of-line) (looking-at " :tag:"))
  571. (progn (org-end-of-line) (eolp))
  572. (progn (org-end-of-line) (looking-at " :tag:"))))))
  573. ;; At an headline without special movement.
  574. (should
  575. (org-test-with-temp-text "* Headline2 :tag:\n"
  576. (let ((org-special-ctrl-a/e nil))
  577. (and (progn (org-end-of-line) (eolp))
  578. (progn (org-end-of-line) (eolp))))))
  579. ;; At an headline, with reversed movement.
  580. (should
  581. (org-test-with-temp-text "* Headline3 :tag:\n"
  582. (let ((org-special-ctrl-a/e 'reversed)
  583. (this-command last-command))
  584. (and (progn (org-end-of-line) (eolp))
  585. (progn (org-end-of-line) (looking-at " :tag:"))))))
  586. ;; At a block without hidden contents.
  587. (should
  588. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  589. (progn (org-end-of-line) (eolp))))
  590. ;; At a block with hidden contents.
  591. (should-not
  592. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  593. (let ((org-special-ctrl-a/e t))
  594. (org-hide-block-toggle)
  595. (org-end-of-line)
  596. (eobp)))))
  597. (ert-deftest test-org/forward-paragraph ()
  598. "Test `org-forward-paragraph' specifications."
  599. ;; At end of buffer, return an error.
  600. (should-error
  601. (org-test-with-temp-text "Paragraph"
  602. (goto-char (point-max))
  603. (org-forward-paragraph)))
  604. ;; Standard test.
  605. (should
  606. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  607. (org-forward-paragraph)
  608. (looking-at "P2")))
  609. ;; Ignore depth.
  610. (should
  611. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  612. (org-forward-paragraph)
  613. (looking-at "P1")))
  614. ;; Do not enter elements with invisible contents.
  615. (should
  616. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  617. (org-hide-block-toggle)
  618. (org-forward-paragraph)
  619. (looking-at "P3")))
  620. ;; On an affiliated keyword, jump to the beginning of the element.
  621. (should
  622. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  623. (org-forward-paragraph)
  624. (looking-at "Para")))
  625. ;; On an item or a footnote definition, move to the second element
  626. ;; inside, if any.
  627. (should
  628. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  629. (org-forward-paragraph)
  630. (looking-at " Paragraph")))
  631. (should
  632. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  633. (org-forward-paragraph)
  634. (looking-at "Paragraph")))
  635. ;; On an item, or a footnote definition, when the first line is
  636. ;; empty, move to the first item.
  637. (should
  638. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  639. (org-forward-paragraph)
  640. (looking-at " Paragraph")))
  641. (should
  642. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  643. (org-forward-paragraph)
  644. (looking-at "Paragraph")))
  645. ;; On a table (resp. a property drawer) do not move through table
  646. ;; rows (resp. node properties).
  647. (should
  648. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  649. (org-forward-paragraph)
  650. (looking-at "Paragraph")))
  651. (should
  652. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
  653. (org-forward-paragraph)
  654. (looking-at "Paragraph")))
  655. ;; On a verse or source block, stop after blank lines.
  656. (should
  657. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  658. (org-forward-paragraph)
  659. (looking-at "L2")))
  660. (should
  661. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  662. (org-forward-paragraph)
  663. (looking-at "L2"))))
  664. (ert-deftest test-org/backward-paragraph ()
  665. "Test `org-backward-paragraph' specifications."
  666. ;; Error at beginning of buffer.
  667. (should-error
  668. (org-test-with-temp-text "Paragraph"
  669. (org-backward-paragraph)))
  670. ;; Regular test.
  671. (should
  672. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  673. (goto-char (point-max))
  674. (org-backward-paragraph)
  675. (looking-at "P3")))
  676. (should
  677. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  678. (goto-char (point-max))
  679. (beginning-of-line)
  680. (org-backward-paragraph)
  681. (looking-at "P2")))
  682. ;; Ignore depth.
  683. (should
  684. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  685. (goto-char (point-max))
  686. (beginning-of-line)
  687. (org-backward-paragraph)
  688. (looking-at "P2")))
  689. ;; Ignore invisible elements.
  690. (should
  691. (org-test-with-temp-text "* H1\n P1\n* H2"
  692. (org-cycle)
  693. (goto-char (point-max))
  694. (beginning-of-line)
  695. (org-backward-paragraph)
  696. (bobp)))
  697. ;; On an affiliated keyword, jump to the first one.
  698. (should
  699. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  700. (search-forward "c2")
  701. (org-backward-paragraph)
  702. (looking-at "#\\+name")))
  703. ;; On the second element in an item or a footnote definition, jump
  704. ;; to item or the definition.
  705. (should
  706. (org-test-with-temp-text "- line1\n\n line2"
  707. (goto-char (point-max))
  708. (beginning-of-line)
  709. (org-backward-paragraph)
  710. (looking-at "- line1")))
  711. (should
  712. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  713. (goto-char (point-max))
  714. (beginning-of-line)
  715. (org-backward-paragraph)
  716. (looking-at "\\[fn:1\\] line1")))
  717. ;; On a table (resp. a property drawer), ignore table rows
  718. ;; (resp. node properties).
  719. (should
  720. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  721. (goto-char (point-max))
  722. (beginning-of-line)
  723. (org-backward-paragraph)
  724. (bobp)))
  725. (should
  726. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
  727. (goto-char (point-max))
  728. (beginning-of-line)
  729. (org-backward-paragraph)
  730. (bobp)))
  731. ;; On a source or verse block, stop before blank lines.
  732. (should
  733. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  734. (search-forward "L3")
  735. (beginning-of-line)
  736. (org-backward-paragraph)
  737. (looking-at "L2")))
  738. (should
  739. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  740. (search-forward "L3")
  741. (beginning-of-line)
  742. (org-backward-paragraph)
  743. (looking-at "L2"))))
  744. (ert-deftest test-org/forward-element ()
  745. "Test `org-forward-element' specifications."
  746. ;; 1. At EOB: should error.
  747. (org-test-with-temp-text "Some text\n"
  748. (goto-char (point-max))
  749. (should-error (org-forward-element)))
  750. ;; 2. Standard move: expected to ignore blank lines.
  751. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  752. (org-forward-element)
  753. (should (looking-at (regexp-quote "Second paragraph."))))
  754. ;; 3. Headline tests.
  755. (org-test-with-temp-text "
  756. * Head 1
  757. ** Head 1.1
  758. *** Head 1.1.1
  759. ** Head 1.2"
  760. ;; 3.1. At an headline beginning: move to next headline at the
  761. ;; same level.
  762. (goto-line 3)
  763. (org-forward-element)
  764. (should (looking-at (regexp-quote "** Head 1.2")))
  765. ;; 3.2. At an headline beginning: move to parent headline if no
  766. ;; headline at the same level.
  767. (goto-line 3)
  768. (org-forward-element)
  769. (should (looking-at (regexp-quote "** Head 1.2"))))
  770. ;; 4. Greater element tests.
  771. (org-test-with-temp-text
  772. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  773. ;; 4.1. At a greater element: expected to skip contents.
  774. (org-forward-element)
  775. (should (looking-at (regexp-quote "Outside.")))
  776. ;; 4.2. At the end of greater element contents: expected to skip
  777. ;; to the end of the greater element.
  778. (goto-line 2)
  779. (org-forward-element)
  780. (should (looking-at (regexp-quote "Outside."))))
  781. ;; 5. List tests.
  782. (org-test-with-temp-text "
  783. - item1
  784. - sub1
  785. - sub2
  786. - sub3
  787. Inner paragraph.
  788. - item2
  789. Outside."
  790. ;; 5.1. At list top point: expected to move to the element after
  791. ;; the list.
  792. (goto-line 2)
  793. (org-forward-element)
  794. (should (looking-at (regexp-quote "Outside.")))
  795. ;; 5.2. Special case: at the first line of a sub-list, but not at
  796. ;; beginning of line, move to next item.
  797. (goto-line 2)
  798. (forward-char)
  799. (org-forward-element)
  800. (should (looking-at "- item2"))
  801. (goto-line 4)
  802. (forward-char)
  803. (org-forward-element)
  804. (should (looking-at " - sub2"))
  805. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  806. (goto-line 4)
  807. (org-forward-element)
  808. (should (looking-at (regexp-quote " Inner paragraph.")))
  809. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  810. (goto-line 8)
  811. (org-forward-element)
  812. (should (looking-at (regexp-quote " Inner paragraph.")))
  813. ;; 5.5. At an item: expected to move to next item, if any.
  814. (goto-line 6)
  815. (org-forward-element)
  816. (should (looking-at " - sub3"))))
  817. (ert-deftest test-org/backward-element ()
  818. "Test `org-backward-element' specifications."
  819. ;; 1. Should error at BOB.
  820. (org-test-with-temp-text " \nParagraph."
  821. (should-error (org-backward-element)))
  822. ;; 2. Should move at BOB when called on the first element in buffer.
  823. (should
  824. (org-test-with-temp-text "\n#+TITLE: test"
  825. (progn (forward-line)
  826. (org-backward-element)
  827. (bobp))))
  828. ;; 3. Not at the beginning of an element: move at its beginning.
  829. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  830. (goto-line 3)
  831. (end-of-line)
  832. (org-backward-element)
  833. (should (looking-at (regexp-quote "Paragraph2."))))
  834. ;; 4. Headline tests.
  835. (org-test-with-temp-text "
  836. * Head 1
  837. ** Head 1.1
  838. *** Head 1.1.1
  839. ** Head 1.2"
  840. ;; 4.1. At an headline beginning: move to previous headline at the
  841. ;; same level.
  842. (goto-line 5)
  843. (org-backward-element)
  844. (should (looking-at (regexp-quote "** Head 1.1")))
  845. ;; 4.2. At an headline beginning: move to parent headline if no
  846. ;; headline at the same level.
  847. (goto-line 3)
  848. (org-backward-element)
  849. (should (looking-at (regexp-quote "* Head 1")))
  850. ;; 4.3. At the first top-level headline: should error.
  851. (goto-line 2)
  852. (should-error (org-backward-element)))
  853. ;; 5. At beginning of first element inside a greater element:
  854. ;; expected to move to greater element's beginning.
  855. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  856. (goto-line 3)
  857. (org-backward-element)
  858. (should (looking-at "#\\+BEGIN_CENTER")))
  859. ;; 6. At the beginning of the first element in a section: should
  860. ;; move back to headline, if any.
  861. (should
  862. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  863. (progn (goto-char (point-max))
  864. (beginning-of-line)
  865. (org-backward-element)
  866. (org-at-heading-p))))
  867. ;; 7. List tests.
  868. (org-test-with-temp-text "
  869. - item1
  870. - sub1
  871. - sub2
  872. - sub3
  873. Inner paragraph.
  874. - item2
  875. Outside."
  876. ;; 7.1. At beginning of sub-list: expected to move to the
  877. ;; paragraph before it.
  878. (goto-line 4)
  879. (org-backward-element)
  880. (should (looking-at "item1"))
  881. ;; 7.2. At an item in a list: expected to move at previous item.
  882. (goto-line 8)
  883. (org-backward-element)
  884. (should (looking-at " - sub2"))
  885. (goto-line 12)
  886. (org-backward-element)
  887. (should (looking-at "- item1"))
  888. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  889. ;; beginning.
  890. (goto-line 10)
  891. (org-backward-element)
  892. (should (looking-at " - sub1"))
  893. (goto-line 15)
  894. (org-backward-element)
  895. (should (looking-at "- item1"))
  896. ;; 7.4. At blank-lines before list end: expected to move to top
  897. ;; item.
  898. (goto-line 14)
  899. (org-backward-element)
  900. (should (looking-at "- item1"))))
  901. (ert-deftest test-org/up-element ()
  902. "Test `org-up-element' specifications."
  903. ;; 1. At BOB or with no surrounding element: should error.
  904. (org-test-with-temp-text "Paragraph."
  905. (should-error (org-up-element)))
  906. (org-test-with-temp-text "* Head1\n* Head2"
  907. (goto-line 2)
  908. (should-error (org-up-element)))
  909. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  910. (goto-line 3)
  911. (should-error (org-up-element)))
  912. ;; 2. At an headline: move to parent headline.
  913. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  914. (goto-line 3)
  915. (org-up-element)
  916. (should (looking-at "\\* Head1")))
  917. ;; 3. Inside a greater element: move to greater element beginning.
  918. (org-test-with-temp-text
  919. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  920. (goto-line 3)
  921. (org-up-element)
  922. (should (looking-at "#\\+BEGIN_CENTER")))
  923. ;; 4. List tests.
  924. (org-test-with-temp-text "* Top
  925. - item1
  926. - sub1
  927. - sub2
  928. Paragraph within sub2.
  929. - item2"
  930. ;; 4.1. Within an item: move to the item beginning.
  931. (goto-line 8)
  932. (org-up-element)
  933. (should (looking-at " - sub2"))
  934. ;; 4.2. At an item in a sub-list: move to parent item.
  935. (goto-line 4)
  936. (org-up-element)
  937. (should (looking-at "- item1"))
  938. ;; 4.3. At an item in top list: move to beginning of whole list.
  939. (goto-line 10)
  940. (org-up-element)
  941. (should (looking-at "- item1"))
  942. ;; 4.4. Special case. At very top point: should move to parent of
  943. ;; list.
  944. (goto-line 2)
  945. (org-up-element)
  946. (should (looking-at "\\* Top"))))
  947. (ert-deftest test-org/down-element ()
  948. "Test `org-down-element' specifications."
  949. ;; Error when the element hasn't got a recursive type.
  950. (org-test-with-temp-text "Paragraph."
  951. (should-error (org-down-element)))
  952. ;; Error when the element has no contents
  953. (org-test-with-temp-text "* Headline"
  954. (should-error (org-down-element)))
  955. ;; When at a plain-list, move to first item.
  956. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  957. (goto-line 2)
  958. (org-down-element)
  959. (should (looking-at " - Item 1.1")))
  960. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  961. (org-down-element)
  962. (should (looking-at " Item 1")))
  963. ;; When at a table, move to first row
  964. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  965. (org-down-element)
  966. (should (looking-at " a | b |")))
  967. ;; Otherwise, move inside the greater element.
  968. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  969. (org-down-element)
  970. (should (looking-at "Paragraph"))))
  971. (ert-deftest test-org/drag-element-backward ()
  972. "Test `org-drag-element-backward' specifications."
  973. ;; 1. Error when trying to move first element of buffer.
  974. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  975. (should-error (org-drag-element-backward)))
  976. ;; 2. Error when trying to swap nested elements.
  977. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  978. (forward-line)
  979. (should-error (org-drag-element-backward)))
  980. ;; 3. Error when trying to swap an headline element and
  981. ;; a non-headline element.
  982. (org-test-with-temp-text "Test.\n* Head 1"
  983. (forward-line)
  984. (should-error (org-drag-element-backward)))
  985. ;; 4. Otherwise, swap elements, preserving column and blank lines
  986. ;; between elements.
  987. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  988. (search-forward "graph")
  989. (org-drag-element-backward)
  990. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  991. (should (looking-at " 2")))
  992. ;; 5. Preserve visibility of elements and their contents.
  993. (org-test-with-temp-text "
  994. #+BEGIN_CENTER
  995. Text.
  996. #+END_CENTER
  997. - item 1
  998. #+BEGIN_QUOTE
  999. Text.
  1000. #+END_QUOTE"
  1001. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1002. (search-backward "- item 1")
  1003. (org-drag-element-backward)
  1004. (should
  1005. (equal
  1006. '((63 . 82) (26 . 48))
  1007. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1008. (overlays-in (point-min) (point-max)))))))
  1009. (ert-deftest test-org/drag-element-forward ()
  1010. "Test `org-drag-element-forward' specifications."
  1011. ;; 1. Error when trying to move first element of buffer.
  1012. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1013. (goto-line 3)
  1014. (should-error (org-drag-element-forward)))
  1015. ;; 2. Error when trying to swap nested elements.
  1016. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1017. (forward-line)
  1018. (should-error (org-drag-element-forward)))
  1019. ;; 3. Error when trying to swap a non-headline element and an
  1020. ;; headline.
  1021. (org-test-with-temp-text "Test.\n* Head 1"
  1022. (should-error (org-drag-element-forward)))
  1023. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1024. ;; between elements.
  1025. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1026. (search-forward "graph")
  1027. (org-drag-element-forward)
  1028. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1029. (should (looking-at " 1")))
  1030. ;; 5. Preserve visibility of elements and their contents.
  1031. (org-test-with-temp-text "
  1032. #+BEGIN_CENTER
  1033. Text.
  1034. #+END_CENTER
  1035. - item 1
  1036. #+BEGIN_QUOTE
  1037. Text.
  1038. #+END_QUOTE"
  1039. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1040. (search-backward "#+BEGIN_CENTER")
  1041. (org-drag-element-forward)
  1042. (should
  1043. (equal
  1044. '((63 . 82) (26 . 48))
  1045. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1046. (overlays-in (point-min) (point-max)))))))
  1047. ;;; Planning
  1048. (ert-deftest test-org/timestamp-has-time-p ()
  1049. "Test `org-timestamp-has-time-p' specifications."
  1050. ;; With time.
  1051. (should
  1052. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1053. (org-timestamp-has-time-p (org-element-context))))
  1054. ;; Without time.
  1055. (should-not
  1056. (org-test-with-temp-text "<2012-03-29 Thu>"
  1057. (org-timestamp-has-time-p (org-element-context)))))
  1058. (ert-deftest test-org/timestamp-format ()
  1059. "Test `org-timestamp-format' specifications."
  1060. ;; Regular test.
  1061. (should
  1062. (equal
  1063. "2012-03-29 16:40"
  1064. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1065. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1066. ;; Range end.
  1067. (should
  1068. (equal
  1069. "2012-03-29"
  1070. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1071. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1072. (ert-deftest test-org/timestamp-split-range ()
  1073. "Test `org-timestamp-split-range' specifications."
  1074. ;; Extract range start (active).
  1075. (should
  1076. (equal '(2012 3 29)
  1077. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1078. (let ((ts (org-timestamp-split-range (org-element-context))))
  1079. (mapcar (lambda (p) (org-element-property p ts))
  1080. '(:year-end :month-end :day-end))))))
  1081. ;; Extract range start (inactive)
  1082. (should
  1083. (equal '(2012 3 29)
  1084. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1085. (let ((ts (org-timestamp-split-range (org-element-context))))
  1086. (mapcar (lambda (p) (org-element-property p ts))
  1087. '(:year-end :month-end :day-end))))))
  1088. ;; Extract range end (active).
  1089. (should
  1090. (equal '(2012 3 30)
  1091. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1092. (let ((ts (org-timestamp-split-range
  1093. (org-element-context) t)))
  1094. (mapcar (lambda (p) (org-element-property p ts))
  1095. '(:year-end :month-end :day-end))))))
  1096. ;; Extract range end (inactive)
  1097. (should
  1098. (equal '(2012 3 30)
  1099. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1100. (let ((ts (org-timestamp-split-range
  1101. (org-element-context) t)))
  1102. (mapcar (lambda (p) (org-element-property p ts))
  1103. '(:year-end :month-end :day-end))))))
  1104. ;; Return the timestamp if not a range.
  1105. (should
  1106. (org-test-with-temp-text "[2012-03-29 Thu]"
  1107. (let* ((ts-orig (org-element-context))
  1108. (ts-copy (org-timestamp-split-range ts-orig)))
  1109. (eq ts-orig ts-copy))))
  1110. (should
  1111. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1112. (let* ((ts-orig (org-element-context))
  1113. (ts-copy (org-timestamp-split-range ts-orig)))
  1114. (eq ts-orig ts-copy))))
  1115. ;; Check that parent is the same when a range was split.
  1116. (should
  1117. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1118. (let* ((ts-orig (org-element-context))
  1119. (ts-copy (org-timestamp-split-range ts-orig)))
  1120. (eq (org-element-property :parent ts-orig)
  1121. (org-element-property :parent ts-copy))))))
  1122. (ert-deftest test-org/timestamp-translate ()
  1123. "Test `org-timestamp-translate' specifications."
  1124. ;; Translate whole date range.
  1125. (should
  1126. (equal "<29>--<30>"
  1127. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1128. (let ((org-display-custom-times t)
  1129. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1130. (org-timestamp-translate (org-element-context))))))
  1131. ;; Translate date range start.
  1132. (should
  1133. (equal "<29>"
  1134. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1135. (let ((org-display-custom-times t)
  1136. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1137. (org-timestamp-translate (org-element-context) 'start)))))
  1138. ;; Translate date range end.
  1139. (should
  1140. (equal "<30>"
  1141. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1142. (let ((org-display-custom-times t)
  1143. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1144. (org-timestamp-translate (org-element-context) 'end)))))
  1145. ;; Translate time range.
  1146. (should
  1147. (equal "<08>--<16>"
  1148. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1149. (let ((org-display-custom-times t)
  1150. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1151. (org-timestamp-translate (org-element-context))))))
  1152. ;; Translate non-range timestamp.
  1153. (should
  1154. (equal "<29>"
  1155. (org-test-with-temp-text "<2012-03-29 Thu>"
  1156. (let ((org-display-custom-times t)
  1157. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1158. (org-timestamp-translate (org-element-context))))))
  1159. ;; Do not change `diary' timestamps.
  1160. (should
  1161. (equal "<%%(org-float t 4 2)>"
  1162. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1163. (let ((org-display-custom-times t)
  1164. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1165. (org-timestamp-translate (org-element-context)))))))
  1166. ;;; Targets and Radio Targets
  1167. (ert-deftest test-org/all-targets ()
  1168. "Test `org-all-targets' specifications."
  1169. ;; Without an argument.
  1170. (should
  1171. (equal '("radio-target" "target")
  1172. (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
  1173. (org-all-targets))))
  1174. (should
  1175. (equal '("radio-target")
  1176. (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
  1177. ;; With argument.
  1178. (should
  1179. (equal '("radio-target")
  1180. (org-test-with-temp-text "<<target>> <<<radio-target>>>"
  1181. (org-all-targets t)))))
  1182. (provide 'test-org)
  1183. ;;; test-org.el ends here