test-org.el 32 KB

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