test-org.el 33 KB

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