test-org.el 34 KB

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