test-org.el 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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 non-blank lines.
  62. (should
  63. (equal "# Comment 1\n\n# Comment 2"
  64. (org-test-with-temp-text "Comment 1\n\nComment 2"
  65. (progn
  66. (transient-mark-mode 1)
  67. (push-mark (point) t t)
  68. (goto-char (point-max))
  69. (call-interactively 'comment-dwim)
  70. (buffer-string)))))
  71. ;; In front of a keyword without region, insert a new comment.
  72. (should
  73. (equal "# \n#+KEYWORD: value"
  74. (org-test-with-temp-text "#+KEYWORD: value"
  75. (progn (call-interactively 'comment-dwim)
  76. (buffer-string))))))
  77. ;;; Date and time analysis
  78. (ert-deftest test-org/org-read-date ()
  79. "Test `org-read-date' specifications."
  80. ;; Parse ISO date with abbreviated year and month.
  81. (should (equal "2012-03-29 16:40"
  82. (let ((org-time-was-given t))
  83. (org-read-date t nil "12-3-29 16:40"))))
  84. ;; Parse Europeans dates.
  85. (should (equal "2012-03-29 16:40"
  86. (let ((org-time-was-given t))
  87. (org-read-date t nil "29.03.2012 16:40"))))
  88. ;; Parse Europeans dates without year.
  89. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  90. (let ((org-time-was-given t))
  91. (org-read-date t nil "29.03. 16:40")))))
  92. (ert-deftest test-org/org-parse-time-string ()
  93. "Test `org-parse-time-string'."
  94. (should (equal (org-parse-time-string "2012-03-29 16:40")
  95. '(0 40 16 29 3 2012 nil nil nil)))
  96. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  97. '(0 40 16 29 3 2012 nil nil nil)))
  98. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  99. '(0 40 16 29 3 2012 nil nil nil)))
  100. (should (equal (org-parse-time-string "<2012-03-29>")
  101. '(0 0 0 29 3 2012 nil nil nil)))
  102. (should (equal (org-parse-time-string "<2012-03-29>" t)
  103. '(0 nil nil 29 3 2012 nil nil nil))))
  104. ;;; Filling
  105. (ert-deftest test-org/fill-paragraph ()
  106. "Test `org-fill-paragraph' specifications."
  107. ;; At an Org table, align it.
  108. (should
  109. (equal "| a |\n"
  110. (org-test-with-temp-text "|a|"
  111. (org-fill-paragraph)
  112. (buffer-string))))
  113. (should
  114. (equal "#+name: table\n| a |\n"
  115. (org-test-with-temp-text "#+name: table\n| a |"
  116. (org-fill-paragraph)
  117. (buffer-string))))
  118. ;; At a paragraph, preserve line breaks.
  119. (org-test-with-temp-text "some \\\\\nlong\ntext"
  120. (let ((fill-column 20))
  121. (org-fill-paragraph)
  122. (should (equal (buffer-string) "some \\\\\nlong text"))))
  123. ;; Correctly fill a paragraph when point is at its very end.
  124. (should
  125. (equal "A B"
  126. (org-test-with-temp-text "A\nB"
  127. (let ((fill-column 20))
  128. (goto-char (point-max))
  129. (org-fill-paragraph)
  130. (buffer-string)))))
  131. ;; Correctly fill the last paragraph of a greater element.
  132. (should
  133. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  134. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  135. (let ((fill-column 8))
  136. (forward-line)
  137. (end-of-line)
  138. (org-fill-paragraph)
  139. (buffer-string)))))
  140. ;; Correctly fill an element in a narrowed buffer.
  141. (should
  142. (equal "01234\n6"
  143. (org-test-with-temp-text "01234 6789"
  144. (let ((fill-column 5))
  145. (narrow-to-region 1 8)
  146. (org-fill-paragraph)
  147. (buffer-string)))))
  148. ;; Special case: Fill first paragraph when point is at an item or
  149. ;; a plain-list or a footnote reference.
  150. (should
  151. (equal "- A B"
  152. (org-test-with-temp-text "- A\n B"
  153. (let ((fill-column 20))
  154. (org-fill-paragraph)
  155. (buffer-string)))))
  156. (should
  157. (equal "[fn:1] A B"
  158. (org-test-with-temp-text "[fn:1] A\nB"
  159. (let ((fill-column 20))
  160. (org-fill-paragraph)
  161. (buffer-string)))))
  162. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  163. (let ((fill-column 20))
  164. (org-fill-paragraph)
  165. (should (equal (buffer-string)
  166. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  167. ;; Fill contents of `comment-block' elements.
  168. (should
  169. (equal
  170. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  171. (let ((fill-column 20))
  172. (forward-line)
  173. (org-fill-paragraph)
  174. (buffer-string)))
  175. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  176. ;; Fill `comment' elements.
  177. (should
  178. (equal " # A B"
  179. (org-test-with-temp-text " # A\n # B"
  180. (let ((fill-column 20))
  181. (org-fill-paragraph)
  182. (buffer-string)))))
  183. ;; Do not mix consecutive comments when filling one of them.
  184. (should
  185. (equal "# A B\n\n# C"
  186. (org-test-with-temp-text "# A\n# B\n\n# C"
  187. (let ((fill-column 20))
  188. (org-fill-paragraph)
  189. (buffer-string)))))
  190. ;; Do nothing at affiliated keywords.
  191. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  192. (let ((fill-column 20))
  193. (org-fill-paragraph)
  194. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  195. ;; Do not move point after table when filling a table.
  196. (should-not
  197. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  198. (forward-char)
  199. (org-fill-paragraph)
  200. (eobp))))
  201. (ert-deftest test-org/auto-fill-function ()
  202. "Test auto-filling features."
  203. ;; Auto fill paragraph.
  204. (should
  205. (equal "12345\n7890"
  206. (org-test-with-temp-text "12345 7890"
  207. (let ((fill-column 5))
  208. (end-of-line)
  209. (org-auto-fill-function)
  210. (buffer-string)))))
  211. ;; Auto fill first paragraph in an item.
  212. (should
  213. (equal "- 12345\n 7890"
  214. (org-test-with-temp-text "- 12345 7890"
  215. (let ((fill-column 7))
  216. (end-of-line)
  217. (org-auto-fill-function)
  218. (buffer-string)))))
  219. ;; Auto fill comments.
  220. (should
  221. (equal " # 12345\n # 7890"
  222. (org-test-with-temp-text " # 12345 7890"
  223. (let ((fill-column 10))
  224. (end-of-line)
  225. (org-auto-fill-function)
  226. (buffer-string)))))
  227. ;; A hash within a line isn't a comment.
  228. (should-not
  229. (equal "12345 # 7890\n# 1"
  230. (org-test-with-temp-text "12345 # 7890 1"
  231. (let ((fill-column 12))
  232. (end-of-line)
  233. (org-auto-fill-function)
  234. (buffer-string)))))
  235. ;; Correctly interpret empty prefix.
  236. (should-not
  237. (equal "# a\n# b\nRegular\n# paragraph"
  238. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  239. (let ((fill-column 12))
  240. (end-of-line 3)
  241. (org-auto-fill-function)
  242. (buffer-string)))))
  243. ;; Comment block: auto fill contents.
  244. (should
  245. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  246. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  247. (let ((fill-column 5))
  248. (forward-line)
  249. (end-of-line)
  250. (org-auto-fill-function)
  251. (buffer-string)))))
  252. (should
  253. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  254. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  255. (let ((fill-column 5))
  256. (forward-line)
  257. (end-of-line)
  258. (org-auto-fill-function)
  259. (buffer-string)))))
  260. ;; Do not fill if a new item could be created.
  261. (should-not
  262. (equal "12345\n- 90"
  263. (org-test-with-temp-text "12345 - 90"
  264. (let ((fill-column 5))
  265. (end-of-line)
  266. (org-auto-fill-function)
  267. (buffer-string)))))
  268. ;; Do not fill if a line break could be introduced.
  269. (should-not
  270. (equal "123\\\\\n7890"
  271. (org-test-with-temp-text "123\\\\ 7890"
  272. (let ((fill-column 6))
  273. (end-of-line)
  274. (org-auto-fill-function)
  275. (buffer-string)))))
  276. ;; Do not fill affiliated keywords.
  277. (should-not
  278. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  279. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  280. (let ((fill-column 20))
  281. (end-of-line)
  282. (org-auto-fill-function)
  283. (buffer-string))))))
  284. ;;; Links
  285. ;;;; Fuzzy Links
  286. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  287. ;; a named element (#+name: text) and to headlines (* Text).
  288. (ert-deftest test-org/fuzzy-links ()
  289. "Test fuzzy links specifications."
  290. ;; 1. Fuzzy link goes in priority to a matching target.
  291. (should
  292. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  293. (goto-line 5)
  294. (org-open-at-point)
  295. (looking-at "<<Test>>")))
  296. ;; 2. Then fuzzy link points to an element with a given name.
  297. (should
  298. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  299. (goto-line 5)
  300. (org-open-at-point)
  301. (looking-at "#\\+NAME: Test")))
  302. ;; 3. A target still lead to a matching headline otherwise.
  303. (should
  304. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  305. (goto-line 4)
  306. (org-open-at-point)
  307. (looking-at "\\* Head2")))
  308. ;; 4. With a leading star in link, enforce heading match.
  309. (should
  310. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  311. (goto-line 3)
  312. (org-open-at-point)
  313. (looking-at "\\* Test"))))
  314. ;;;; Link Escaping
  315. (ert-deftest test-org/org-link-escape-ascii-character ()
  316. "Escape an ascii character."
  317. (should
  318. (string=
  319. "%5B"
  320. (org-link-escape "["))))
  321. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  322. "Escape an ascii control character."
  323. (should
  324. (string=
  325. "%09"
  326. (org-link-escape "\t"))))
  327. (ert-deftest test-org/org-link-escape-multibyte-character ()
  328. "Escape an unicode multibyte character."
  329. (should
  330. (string=
  331. "%E2%82%AC"
  332. (org-link-escape "€"))))
  333. (ert-deftest test-org/org-link-escape-custom-table ()
  334. "Escape string with custom character table."
  335. (should
  336. (string=
  337. "Foo%3A%42ar%0A"
  338. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  339. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  340. "Escape string with custom table merged with default table."
  341. (should
  342. (string=
  343. "%5BF%6F%6F%3A%42ar%0A%5D"
  344. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  345. (ert-deftest test-org/org-link-unescape-ascii-character ()
  346. "Unescape an ascii character."
  347. (should
  348. (string=
  349. "["
  350. (org-link-unescape "%5B"))))
  351. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  352. "Unescpae an ascii control character."
  353. (should
  354. (string=
  355. "\n"
  356. (org-link-unescape "%0A"))))
  357. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  358. "Unescape unicode multibyte character."
  359. (should
  360. (string=
  361. "€"
  362. (org-link-unescape "%E2%82%AC"))))
  363. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  364. "Unescape old style percent escaped character."
  365. (should
  366. (string=
  367. "àâçèéêîôùû"
  368. (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  369. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  370. "Escape and unscape a URL that includes an escaped char.
  371. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  372. (should
  373. (string=
  374. "http://some.host.com/form?&id=blah%2Bblah25"
  375. (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  376. ;;; Node Properties
  377. (ert-deftest test-org/accumulated-properties-in-drawers ()
  378. "Ensure properties accumulate in subtree drawers."
  379. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  380. (org-babel-next-src-block)
  381. (should (equal '(2 1) (org-babel-execute-src-block)))))
  382. ;;; Mark Region
  383. (ert-deftest test-org/mark-subtree ()
  384. "Test `org-mark-subtree' specifications."
  385. ;; Error when point is before first headline.
  386. (should-error
  387. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  388. (progn (transient-mark-mode 1)
  389. (org-mark-subtree))))
  390. ;; Without argument, mark current subtree.
  391. (should
  392. (equal
  393. '(12 32)
  394. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  395. (progn (transient-mark-mode 1)
  396. (forward-line 2)
  397. (org-mark-subtree)
  398. (list (region-beginning) (region-end))))))
  399. ;; With an argument, move ARG up.
  400. (should
  401. (equal
  402. '(1 32)
  403. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  404. (progn (transient-mark-mode 1)
  405. (forward-line 2)
  406. (org-mark-subtree 1)
  407. (list (region-beginning) (region-end))))))
  408. ;; Do not get fooled by inlinetasks.
  409. (when (featurep 'org-inlinetask)
  410. (should
  411. (= 1
  412. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  413. (progn (transient-mark-mode 1)
  414. (forward-line 1)
  415. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  416. (region-beginning)))))))
  417. ;;; Navigation
  418. (ert-deftest test-org/beginning-of-line ()
  419. "Test `org-beginning-of-line' specifications."
  420. ;; Standard test.
  421. (should
  422. (org-test-with-temp-text "Some text\nSome other text"
  423. (progn (org-beginning-of-line) (bolp))))
  424. ;; Standard test with `visual-line-mode'.
  425. (should-not
  426. (org-test-with-temp-text "A long line of text\nSome other text"
  427. (progn (visual-line-mode)
  428. (forward-char 2)
  429. (dotimes (i 1000) (insert "very "))
  430. (org-beginning-of-line)
  431. (bolp))))
  432. ;; At an headline with special movement.
  433. (should
  434. (org-test-with-temp-text "* TODO Headline"
  435. (let ((org-special-ctrl-a/e t))
  436. (org-end-of-line)
  437. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  438. (progn (org-beginning-of-line) (bolp))
  439. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  440. (ert-deftest test-org/end-of-line ()
  441. "Test `org-end-of-line' specifications."
  442. ;; Standard test.
  443. (should
  444. (org-test-with-temp-text "Some text\nSome other text"
  445. (progn (org-end-of-line) (eolp))))
  446. ;; Standard test with `visual-line-mode'.
  447. (should-not
  448. (org-test-with-temp-text "A long line of text\nSome other text"
  449. (progn (visual-line-mode)
  450. (forward-char 2)
  451. (dotimes (i 1000) (insert "very "))
  452. (goto-char (point-min))
  453. (org-end-of-line)
  454. (eolp))))
  455. ;; At an headline with special movement.
  456. (should
  457. (org-test-with-temp-text "* Headline1 :tag:\n"
  458. (let ((org-special-ctrl-a/e t))
  459. (and (progn (org-end-of-line) (looking-at " :tag:"))
  460. (progn (org-end-of-line) (eolp))
  461. (progn (org-end-of-line) (looking-at " :tag:"))))))
  462. ;; At an headline without special movement.
  463. (should
  464. (org-test-with-temp-text "* Headline2 :tag:\n"
  465. (let ((org-special-ctrl-a/e nil))
  466. (and (progn (org-end-of-line) (eolp))
  467. (progn (org-end-of-line) (eolp))))))
  468. ;; At an headline, with reversed movement.
  469. (should
  470. (org-test-with-temp-text "* Headline3 :tag:\n"
  471. (let ((org-special-ctrl-a/e 'reversed)
  472. (this-command last-command))
  473. (and (progn (org-end-of-line) (eolp))
  474. (progn (org-end-of-line) (looking-at " :tag:"))))))
  475. ;; At a block without hidden contents.
  476. (should
  477. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  478. (progn (org-end-of-line) (eolp))))
  479. ;; At a block with hidden contents.
  480. (should-not
  481. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  482. (let ((org-special-ctrl-a/e t))
  483. (org-hide-block-toggle)
  484. (org-end-of-line)
  485. (eobp)))))
  486. (ert-deftest test-org/forward-element ()
  487. "Test `org-forward-element' specifications."
  488. ;; 1. At EOB: should error.
  489. (org-test-with-temp-text "Some text\n"
  490. (goto-char (point-max))
  491. (should-error (org-forward-element)))
  492. ;; 2. Standard move: expected to ignore blank lines.
  493. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  494. (org-forward-element)
  495. (should (looking-at (regexp-quote "Second paragraph."))))
  496. ;; 3. Headline tests.
  497. (org-test-with-temp-text "
  498. * Head 1
  499. ** Head 1.1
  500. *** Head 1.1.1
  501. ** Head 1.2"
  502. ;; 3.1. At an headline beginning: move to next headline at the
  503. ;; same level.
  504. (goto-line 3)
  505. (org-forward-element)
  506. (should (looking-at (regexp-quote "** Head 1.2")))
  507. ;; 3.2. At an headline beginning: move to parent headline if no
  508. ;; headline at the same level.
  509. (goto-line 3)
  510. (org-forward-element)
  511. (should (looking-at (regexp-quote "** Head 1.2"))))
  512. ;; 4. Greater element tests.
  513. (org-test-with-temp-text
  514. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  515. ;; 4.1. At a greater element: expected to skip contents.
  516. (org-forward-element)
  517. (should (looking-at (regexp-quote "Outside.")))
  518. ;; 4.2. At the end of greater element contents: expected to skip
  519. ;; to the end of the greater element.
  520. (goto-line 2)
  521. (org-forward-element)
  522. (should (looking-at (regexp-quote "Outside."))))
  523. ;; 5. List tests.
  524. (org-test-with-temp-text "
  525. - item1
  526. - sub1
  527. - sub2
  528. - sub3
  529. Inner paragraph.
  530. - item2
  531. Outside."
  532. ;; 5.1. At list top point: expected to move to the element after
  533. ;; the list.
  534. (goto-line 2)
  535. (org-forward-element)
  536. (should (looking-at (regexp-quote "Outside.")))
  537. ;; 5.2. Special case: at the first line of a sub-list, but not at
  538. ;; beginning of line, move to next item.
  539. (goto-line 2)
  540. (forward-char)
  541. (org-forward-element)
  542. (should (looking-at "- item2"))
  543. (goto-line 4)
  544. (forward-char)
  545. (org-forward-element)
  546. (should (looking-at " - sub2"))
  547. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  548. (goto-line 4)
  549. (org-forward-element)
  550. (should (looking-at (regexp-quote " Inner paragraph.")))
  551. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  552. (goto-line 8)
  553. (org-forward-element)
  554. (should (looking-at (regexp-quote " Inner paragraph.")))
  555. ;; 5.5. At an item: expected to move to next item, if any.
  556. (goto-line 6)
  557. (org-forward-element)
  558. (should (looking-at " - sub3"))))
  559. (ert-deftest test-org/backward-element ()
  560. "Test `org-backward-element' specifications."
  561. ;; 1. Should error at BOB.
  562. (org-test-with-temp-text " \nParagraph."
  563. (should-error (org-backward-element)))
  564. ;; 2. Should move at BOB when called on the first element in buffer.
  565. (should
  566. (org-test-with-temp-text "\n#+TITLE: test"
  567. (progn (forward-line)
  568. (org-backward-element)
  569. (bobp))))
  570. ;; 3. Not at the beginning of an element: move at its beginning.
  571. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  572. (goto-line 3)
  573. (end-of-line)
  574. (org-backward-element)
  575. (should (looking-at (regexp-quote "Paragraph2."))))
  576. ;; 4. Headline tests.
  577. (org-test-with-temp-text "
  578. * Head 1
  579. ** Head 1.1
  580. *** Head 1.1.1
  581. ** Head 1.2"
  582. ;; 4.1. At an headline beginning: move to previous headline at the
  583. ;; same level.
  584. (goto-line 5)
  585. (org-backward-element)
  586. (should (looking-at (regexp-quote "** Head 1.1")))
  587. ;; 4.2. At an headline beginning: move to parent headline if no
  588. ;; headline at the same level.
  589. (goto-line 3)
  590. (org-backward-element)
  591. (should (looking-at (regexp-quote "* Head 1")))
  592. ;; 4.3. At the first top-level headline: should error.
  593. (goto-line 2)
  594. (should-error (org-backward-element)))
  595. ;; 5. At beginning of first element inside a greater element:
  596. ;; expected to move to greater element's beginning.
  597. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  598. (goto-line 3)
  599. (org-backward-element)
  600. (should (looking-at "#\\+BEGIN_CENTER")))
  601. ;; 6. At the beginning of the first element in a section: should
  602. ;; move back to headline, if any.
  603. (should
  604. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  605. (progn (goto-char (point-max))
  606. (beginning-of-line)
  607. (org-backward-element)
  608. (org-at-heading-p))))
  609. ;; 7. List tests.
  610. (org-test-with-temp-text "
  611. - item1
  612. - sub1
  613. - sub2
  614. - sub3
  615. Inner paragraph.
  616. - item2
  617. Outside."
  618. ;; 7.1. At beginning of sub-list: expected to move to the
  619. ;; paragraph before it.
  620. (goto-line 4)
  621. (org-backward-element)
  622. (should (looking-at "item1"))
  623. ;; 7.2. At an item in a list: expected to move at previous item.
  624. (goto-line 8)
  625. (org-backward-element)
  626. (should (looking-at " - sub2"))
  627. (goto-line 12)
  628. (org-backward-element)
  629. (should (looking-at "- item1"))
  630. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  631. ;; beginning.
  632. (goto-line 10)
  633. (org-backward-element)
  634. (should (looking-at " - sub1"))
  635. (goto-line 15)
  636. (org-backward-element)
  637. (should (looking-at "- item1"))
  638. ;; 7.4. At blank-lines before list end: expected to move to top
  639. ;; item.
  640. (goto-line 14)
  641. (org-backward-element)
  642. (should (looking-at "- item1"))))
  643. (ert-deftest test-org/up-element ()
  644. "Test `org-up-element' specifications."
  645. ;; 1. At BOB or with no surrounding element: should error.
  646. (org-test-with-temp-text "Paragraph."
  647. (should-error (org-up-element)))
  648. (org-test-with-temp-text "* Head1\n* Head2"
  649. (goto-line 2)
  650. (should-error (org-up-element)))
  651. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  652. (goto-line 3)
  653. (should-error (org-up-element)))
  654. ;; 2. At an headline: move to parent headline.
  655. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  656. (goto-line 3)
  657. (org-up-element)
  658. (should (looking-at "\\* Head1")))
  659. ;; 3. Inside a greater element: move to greater element beginning.
  660. (org-test-with-temp-text
  661. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  662. (goto-line 3)
  663. (org-up-element)
  664. (should (looking-at "#\\+BEGIN_CENTER")))
  665. ;; 4. List tests.
  666. (org-test-with-temp-text "* Top
  667. - item1
  668. - sub1
  669. - sub2
  670. Paragraph within sub2.
  671. - item2"
  672. ;; 4.1. Within an item: move to the item beginning.
  673. (goto-line 8)
  674. (org-up-element)
  675. (should (looking-at " - sub2"))
  676. ;; 4.2. At an item in a sub-list: move to parent item.
  677. (goto-line 4)
  678. (org-up-element)
  679. (should (looking-at "- item1"))
  680. ;; 4.3. At an item in top list: move to beginning of whole list.
  681. (goto-line 10)
  682. (org-up-element)
  683. (should (looking-at "- item1"))
  684. ;; 4.4. Special case. At very top point: should move to parent of
  685. ;; list.
  686. (goto-line 2)
  687. (org-up-element)
  688. (should (looking-at "\\* Top"))))
  689. (ert-deftest test-org/down-element ()
  690. "Test `org-down-element' specifications."
  691. ;; Error when the element hasn't got a recursive type.
  692. (org-test-with-temp-text "Paragraph."
  693. (should-error (org-down-element)))
  694. ;; Error when the element has no contents
  695. (org-test-with-temp-text "* Headline"
  696. (should-error (org-down-element)))
  697. ;; When at a plain-list, move to first item.
  698. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  699. (goto-line 2)
  700. (org-down-element)
  701. (should (looking-at " - Item 1.1")))
  702. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  703. (org-down-element)
  704. (should (looking-at " Item 1")))
  705. ;; When at a table, move to first row
  706. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  707. (org-down-element)
  708. (should (looking-at " a | b |")))
  709. ;; Otherwise, move inside the greater element.
  710. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  711. (org-down-element)
  712. (should (looking-at "Paragraph"))))
  713. (ert-deftest test-org/drag-element-backward ()
  714. "Test `org-drag-element-backward' specifications."
  715. ;; 1. Error when trying to move first element of buffer.
  716. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  717. (should-error (org-drag-element-backward)))
  718. ;; 2. Error when trying to swap nested elements.
  719. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  720. (forward-line)
  721. (should-error (org-drag-element-backward)))
  722. ;; 3. Error when trying to swap an headline element and
  723. ;; a non-headline element.
  724. (org-test-with-temp-text "Test.\n* Head 1"
  725. (forward-line)
  726. (should-error (org-drag-element-backward)))
  727. ;; 4. Otherwise, swap elements, preserving column and blank lines
  728. ;; between elements.
  729. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  730. (search-forward "graph")
  731. (org-drag-element-backward)
  732. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  733. (should (looking-at " 2")))
  734. ;; 5. Preserve visibility of elements and their contents.
  735. (org-test-with-temp-text "
  736. #+BEGIN_CENTER
  737. Text.
  738. #+END_CENTER
  739. - item 1
  740. #+BEGIN_QUOTE
  741. Text.
  742. #+END_QUOTE"
  743. (while (search-forward "BEGIN_" nil t) (org-cycle))
  744. (search-backward "- item 1")
  745. (org-drag-element-backward)
  746. (should
  747. (equal
  748. '((63 . 82) (26 . 48))
  749. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  750. (overlays-in (point-min) (point-max)))))))
  751. (ert-deftest test-org/drag-element-forward ()
  752. "Test `org-drag-element-forward' specifications."
  753. ;; 1. Error when trying to move first element of buffer.
  754. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  755. (goto-line 3)
  756. (should-error (org-drag-element-forward)))
  757. ;; 2. Error when trying to swap nested elements.
  758. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  759. (forward-line)
  760. (should-error (org-drag-element-forward)))
  761. ;; 3. Error when trying to swap a non-headline element and an
  762. ;; headline.
  763. (org-test-with-temp-text "Test.\n* Head 1"
  764. (should-error (org-drag-element-forward)))
  765. ;; 4. Otherwise, swap elements, preserving column and blank lines
  766. ;; between elements.
  767. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  768. (search-forward "graph")
  769. (org-drag-element-forward)
  770. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  771. (should (looking-at " 1")))
  772. ;; 5. Preserve visibility of elements and their contents.
  773. (org-test-with-temp-text "
  774. #+BEGIN_CENTER
  775. Text.
  776. #+END_CENTER
  777. - item 1
  778. #+BEGIN_QUOTE
  779. Text.
  780. #+END_QUOTE"
  781. (while (search-forward "BEGIN_" nil t) (org-cycle))
  782. (search-backward "#+BEGIN_CENTER")
  783. (org-drag-element-forward)
  784. (should
  785. (equal
  786. '((63 . 82) (26 . 48))
  787. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  788. (overlays-in (point-min) (point-max)))))))
  789. ;;; Planning
  790. (ert-deftest test-org/timestamp-has-time-p ()
  791. "Test `org-timestamp-has-time-p' specifications."
  792. ;; With time.
  793. (should
  794. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  795. (org-timestamp-has-time-p (org-element-context))))
  796. ;; Without time.
  797. (should-not
  798. (org-test-with-temp-text "<2012-03-29 Thu>"
  799. (org-timestamp-has-time-p (org-element-context)))))
  800. (ert-deftest test-org/timestamp-format ()
  801. "Test `org-timestamp-format' specifications."
  802. ;; Regular test.
  803. (should
  804. (equal
  805. "2012-03-29 16:40"
  806. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  807. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  808. ;; Range end.
  809. (should
  810. (equal
  811. "2012-03-29"
  812. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  813. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  814. (ert-deftest test-org/timestamp-split-range ()
  815. "Test `org-timestamp-split-range' specifications."
  816. ;; Extract range start (active).
  817. (should
  818. (equal '(2012 3 29)
  819. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  820. (let ((ts (org-timestamp-split-range (org-element-context))))
  821. (mapcar (lambda (p) (org-element-property p ts))
  822. '(:year-end :month-end :day-end))))))
  823. ;; Extract range start (inactive)
  824. (should
  825. (equal '(2012 3 29)
  826. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  827. (let ((ts (org-timestamp-split-range (org-element-context))))
  828. (mapcar (lambda (p) (org-element-property p ts))
  829. '(:year-end :month-end :day-end))))))
  830. ;; Extract range end (active).
  831. (should
  832. (equal '(2012 3 30)
  833. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  834. (let ((ts (org-timestamp-split-range
  835. (org-element-context) t)))
  836. (mapcar (lambda (p) (org-element-property p ts))
  837. '(:year-end :month-end :day-end))))))
  838. ;; Extract range end (inactive)
  839. (should
  840. (equal '(2012 3 30)
  841. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  842. (let ((ts (org-timestamp-split-range
  843. (org-element-context) t)))
  844. (mapcar (lambda (p) (org-element-property p ts))
  845. '(:year-end :month-end :day-end))))))
  846. ;; Return the timestamp if not a range.
  847. (should
  848. (org-test-with-temp-text "[2012-03-29 Thu]"
  849. (let* ((ts-orig (org-element-context))
  850. (ts-copy (org-timestamp-split-range ts-orig)))
  851. (eq ts-orig ts-copy))))
  852. (should
  853. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  854. (let* ((ts-orig (org-element-context))
  855. (ts-copy (org-timestamp-split-range ts-orig)))
  856. (eq ts-orig ts-copy))))
  857. ;; Check that parent is the same when a range was split.
  858. (should
  859. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  860. (let* ((ts-orig (org-element-context))
  861. (ts-copy (org-timestamp-split-range ts-orig)))
  862. (eq (org-element-property :parent ts-orig)
  863. (org-element-property :parent ts-copy))))))
  864. (ert-deftest test-org/timestamp-translate ()
  865. "Test `org-timestamp-translate' specifications."
  866. ;; Translate whole date range.
  867. (should
  868. (equal "<29>--<30>"
  869. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  870. (let ((org-display-custom-times t)
  871. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  872. (org-timestamp-translate (org-element-context))))))
  873. ;; Translate date range start.
  874. (should
  875. (equal "<29>"
  876. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  877. (let ((org-display-custom-times t)
  878. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  879. (org-timestamp-translate (org-element-context) 'start)))))
  880. ;; Translate date range end.
  881. (should
  882. (equal "<30>"
  883. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  884. (let ((org-display-custom-times t)
  885. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  886. (org-timestamp-translate (org-element-context) 'end)))))
  887. ;; Translate time range.
  888. (should
  889. (equal "<08>--<16>"
  890. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  891. (let ((org-display-custom-times t)
  892. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  893. (org-timestamp-translate (org-element-context))))))
  894. ;; Translate non-range timestamp.
  895. (should
  896. (equal "<29>"
  897. (org-test-with-temp-text "<2012-03-29 Thu>"
  898. (let ((org-display-custom-times t)
  899. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  900. (org-timestamp-translate (org-element-context))))))
  901. ;; Do not change `diary' timestamps.
  902. (should
  903. (equal "<%%(org-float t 4 2)>"
  904. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  905. (let ((org-display-custom-times t)
  906. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  907. (org-timestamp-translate (org-element-context)))))))
  908. ;;; Targets and Radio Targets
  909. (ert-deftest test-org/all-targets ()
  910. "Test `org-all-targets' specifications."
  911. ;; Without an argument.
  912. (should
  913. (equal '("radio-target" "target")
  914. (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
  915. (org-all-targets))))
  916. (should
  917. (equal '("radio-target")
  918. (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
  919. ;; With argument.
  920. (should
  921. (equal '("radio-target")
  922. (org-test-with-temp-text "<<target>> <<<radio-target>>>"
  923. (org-all-targets t)))))
  924. (provide 'test-org)
  925. ;;; test-org.el ends here