test-org.el 27 KB

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