test-org.el 34 KB

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