test-org.el 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. ;; A hash within a line isn't a comment.
  289. (should-not
  290. (equal "12345 # 7890\n# 1"
  291. (org-test-with-temp-text "12345 # 7890 1"
  292. (let ((fill-column 12))
  293. (end-of-line)
  294. (org-auto-fill-function)
  295. (buffer-string)))))
  296. ;; Correctly interpret empty prefix.
  297. (should-not
  298. (equal "# a\n# b\nRegular\n# paragraph"
  299. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  300. (let ((fill-column 12))
  301. (end-of-line 3)
  302. (org-auto-fill-function)
  303. (buffer-string)))))
  304. ;; Comment block: auto fill contents.
  305. (should
  306. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  307. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  308. (let ((fill-column 5))
  309. (forward-line)
  310. (end-of-line)
  311. (org-auto-fill-function)
  312. (buffer-string)))))
  313. (should
  314. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  315. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  316. (let ((fill-column 5))
  317. (forward-line)
  318. (end-of-line)
  319. (org-auto-fill-function)
  320. (buffer-string)))))
  321. ;; Do not fill if a new item could be created.
  322. (should-not
  323. (equal "12345\n- 90"
  324. (org-test-with-temp-text "12345 - 90"
  325. (let ((fill-column 5))
  326. (end-of-line)
  327. (org-auto-fill-function)
  328. (buffer-string)))))
  329. ;; Do not fill if a line break could be introduced.
  330. (should-not
  331. (equal "123\\\\\n7890"
  332. (org-test-with-temp-text "123\\\\ 7890"
  333. (let ((fill-column 6))
  334. (end-of-line)
  335. (org-auto-fill-function)
  336. (buffer-string)))))
  337. ;; Do not fill affiliated keywords.
  338. (should-not
  339. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  340. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  341. (let ((fill-column 20))
  342. (end-of-line)
  343. (org-auto-fill-function)
  344. (buffer-string))))))
  345. ;;; Editing
  346. ;;;; Insert elements
  347. (ert-deftest test-org/meta-return ()
  348. "Test M-RET (`org-meta-return')."
  349. ;; In a table field insert a row above.
  350. (should
  351. (org-test-with-temp-text "| a |"
  352. (forward-char)
  353. (org-meta-return)
  354. (forward-line -1)
  355. (looking-at "| |$")))
  356. ;; In a paragraph change current line into a header.
  357. (should
  358. (org-test-with-temp-text "a"
  359. (org-meta-return)
  360. (beginning-of-line)
  361. (looking-at "\* a$")))
  362. ;; In an item insert an item, in this case above.
  363. (should
  364. (org-test-with-temp-text "- a"
  365. (org-meta-return)
  366. (beginning-of-line)
  367. (looking-at "- $")))
  368. ;; In a drawer and item insert an item, in this case above.
  369. (should
  370. (let ((org-drawers '("MYDRAWER")))
  371. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  372. (forward-line)
  373. (org-meta-return)
  374. (beginning-of-line)
  375. (looking-at "- $")))))
  376. (ert-deftest test-org/insert-heading ()
  377. "Test `org-insert-heading' specifications."
  378. ;; FIXME: Test coverage is incomplete yet.
  379. ;;
  380. ;; In an empty buffer, insert a new headline.
  381. (should
  382. (equal "* "
  383. (org-test-with-temp-text ""
  384. (org-insert-heading)
  385. (buffer-string))))
  386. ;; At the beginning of a line, turn it into a headline
  387. (should
  388. (equal "* P"
  389. (org-test-with-temp-text "<point>P"
  390. (org-insert-heading)
  391. (buffer-string))))
  392. ;; In the middle of a line, split the line if allowed, otherwise,
  393. ;; insert the headline at its end.
  394. (should
  395. (equal "Para\n* graph"
  396. (org-test-with-temp-text "Para<point>graph"
  397. (let ((org-M-RET-may-split-line '((default . t))))
  398. (org-insert-heading))
  399. (buffer-string))))
  400. (should
  401. (equal "Paragraph\n* "
  402. (org-test-with-temp-text "Para<point>graph"
  403. (let ((org-M-RET-may-split-line '((default . nil))))
  404. (org-insert-heading))
  405. (buffer-string))))
  406. ;; When on a list, insert an item instead, unless called with an
  407. ;; universal argument or if list is invisible. In this case, create
  408. ;; a new headline after contents.
  409. (should
  410. (equal "* H\n- item\n- "
  411. (org-test-with-temp-text "* H\n- item<point>"
  412. (let ((org-insert-heading-respect-content nil))
  413. (org-insert-heading))
  414. (buffer-string))))
  415. (should
  416. (equal "* H\n- item\n- item 2\n* "
  417. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  418. (let ((org-insert-heading-respect-content nil))
  419. (org-insert-heading '(4)))
  420. (buffer-string))))
  421. (should
  422. (equal "* H\n- item\n* "
  423. (org-test-with-temp-text "* H\n- item"
  424. (org-cycle)
  425. (goto-char (point-max))
  426. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  427. (buffer-string))))
  428. ;; When called with two universal arguments, insert a new headline
  429. ;; at the end of the grandparent subtree.
  430. (should
  431. (equal "* H1\n** H3\n- item\n** H2\n** "
  432. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  433. (let ((org-insert-heading-respect-content nil))
  434. (org-insert-heading '(16)))
  435. (buffer-string))))
  436. ;; Corner case: correctly insert a headline after an empty one.
  437. (should
  438. (equal "* \n* "
  439. (org-test-with-temp-text "* <point>"
  440. (org-insert-heading)
  441. (buffer-string)))))
  442. (ert-deftest test-org/insert-todo-heading-respect-content ()
  443. "Test `org-insert-todo-heading-respect-content' specifications."
  444. ;; Create a TODO heading.
  445. (should
  446. (org-test-with-temp-text "* H1\n Body"
  447. (org-insert-todo-heading-respect-content)
  448. (nth 2 (org-heading-components))))
  449. ;; Add headline at the end of the first subtree
  450. (should
  451. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  452. (search-forward "H1Body")
  453. (org-insert-todo-heading-respect-content)
  454. (and (eobp) (org-at-heading-p))))
  455. ;; In a list, do not create a new item.
  456. (should
  457. (org-test-with-temp-text "* H\n- an item\n- another one"
  458. (search-forward "an ")
  459. (org-insert-todo-heading-respect-content)
  460. (and (eobp) (org-at-heading-p)))))
  461. ;;; Links
  462. ;;;; Fuzzy Links
  463. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  464. ;; a named element (#+name: text) and to headlines (* Text).
  465. (ert-deftest test-org/fuzzy-links ()
  466. "Test fuzzy links specifications."
  467. ;; 1. Fuzzy link goes in priority to a matching target.
  468. (should
  469. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  470. (goto-line 5)
  471. (org-open-at-point)
  472. (looking-at "<<Test>>")))
  473. ;; 2. Then fuzzy link points to an element with a given name.
  474. (should
  475. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  476. (goto-line 5)
  477. (org-open-at-point)
  478. (looking-at "#\\+NAME: Test")))
  479. ;; 3. A target still lead to a matching headline otherwise.
  480. (should
  481. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  482. (goto-line 4)
  483. (org-open-at-point)
  484. (looking-at "\\* Head2")))
  485. ;; 4. With a leading star in link, enforce heading match.
  486. (should
  487. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  488. (goto-line 3)
  489. (org-open-at-point)
  490. (looking-at "\\* Test"))))
  491. ;;;; Link Escaping
  492. (ert-deftest test-org/org-link-escape-ascii-character ()
  493. "Escape an ascii character."
  494. (should
  495. (string=
  496. "%5B"
  497. (org-link-escape "["))))
  498. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  499. "Escape an ascii control character."
  500. (should
  501. (string=
  502. "%09"
  503. (org-link-escape "\t"))))
  504. (ert-deftest test-org/org-link-escape-multibyte-character ()
  505. "Escape an unicode multibyte character."
  506. (should
  507. (string=
  508. "%E2%82%AC"
  509. (org-link-escape "€"))))
  510. (ert-deftest test-org/org-link-escape-custom-table ()
  511. "Escape string with custom character table."
  512. (should
  513. (string=
  514. "Foo%3A%42ar%0A"
  515. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  516. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  517. "Escape string with custom table merged with default table."
  518. (should
  519. (string=
  520. "%5BF%6F%6F%3A%42ar%0A%5D"
  521. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  522. (ert-deftest test-org/org-link-unescape-ascii-character ()
  523. "Unescape an ascii character."
  524. (should
  525. (string=
  526. "["
  527. (org-link-unescape "%5B"))))
  528. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  529. "Unescpae an ascii control character."
  530. (should
  531. (string=
  532. "\n"
  533. (org-link-unescape "%0A"))))
  534. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  535. "Unescape unicode multibyte character."
  536. (should
  537. (string=
  538. "€"
  539. (org-link-unescape "%E2%82%AC"))))
  540. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  541. "Unescape old style percent escaped character."
  542. (should
  543. (string=
  544. "àâçèéêîôùû"
  545. (decode-coding-string
  546. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  547. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  548. "Escape and unescape a URL that includes an escaped char.
  549. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  550. (should
  551. (string=
  552. "http://some.host.com/form?&id=blah%2Bblah25"
  553. (org-link-unescape
  554. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  555. (ert-deftest test-org/org-link-escape-chars-browser ()
  556. "Escape a URL to pass to `browse-url'."
  557. (should
  558. (string=
  559. "http://some.host.com/search?q=%22Org%20mode%22"
  560. (org-link-escape "http://some.host.com/search?q=\"Org mode\""
  561. org-link-escape-chars-browser))))
  562. ;;; Node Properties
  563. (ert-deftest test-org/accumulated-properties-in-drawers ()
  564. "Ensure properties accumulate in subtree drawers."
  565. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  566. (org-babel-next-src-block)
  567. (should (equal '(2 1) (org-babel-execute-src-block)))))
  568. ;;; Mark Region
  569. (ert-deftest test-org/mark-subtree ()
  570. "Test `org-mark-subtree' specifications."
  571. ;; Error when point is before first headline.
  572. (should-error
  573. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  574. (progn (transient-mark-mode 1)
  575. (org-mark-subtree))))
  576. ;; Without argument, mark current subtree.
  577. (should
  578. (equal
  579. '(12 32)
  580. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  581. (progn (transient-mark-mode 1)
  582. (forward-line 2)
  583. (org-mark-subtree)
  584. (list (region-beginning) (region-end))))))
  585. ;; With an argument, move ARG up.
  586. (should
  587. (equal
  588. '(1 32)
  589. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  590. (progn (transient-mark-mode 1)
  591. (forward-line 2)
  592. (org-mark-subtree 1)
  593. (list (region-beginning) (region-end))))))
  594. ;; Do not get fooled by inlinetasks.
  595. (when (featurep 'org-inlinetask)
  596. (should
  597. (= 1
  598. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  599. (progn (transient-mark-mode 1)
  600. (forward-line 1)
  601. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  602. (region-beginning)))))))
  603. ;;; Navigation
  604. (ert-deftest test-org/beginning-of-line ()
  605. "Test `org-beginning-of-line' specifications."
  606. ;; Standard test.
  607. (should
  608. (org-test-with-temp-text "Some text\nSome other text"
  609. (progn (org-beginning-of-line) (bolp))))
  610. ;; Standard test with `visual-line-mode'.
  611. (should-not
  612. (org-test-with-temp-text "A long line of text\nSome other text"
  613. (progn (visual-line-mode)
  614. (forward-char 2)
  615. (dotimes (i 1000) (insert "very "))
  616. (org-beginning-of-line)
  617. (bolp))))
  618. ;; At an headline with special movement.
  619. (should
  620. (org-test-with-temp-text "* TODO Headline"
  621. (let ((org-special-ctrl-a/e t))
  622. (org-end-of-line)
  623. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  624. (progn (org-beginning-of-line) (bolp))
  625. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  626. (ert-deftest test-org/end-of-line ()
  627. "Test `org-end-of-line' specifications."
  628. ;; Standard test.
  629. (should
  630. (org-test-with-temp-text "Some text\nSome other text"
  631. (progn (org-end-of-line) (eolp))))
  632. ;; Standard test with `visual-line-mode'.
  633. (should-not
  634. (org-test-with-temp-text "A long line of text\nSome other text"
  635. (progn (visual-line-mode)
  636. (forward-char 2)
  637. (dotimes (i 1000) (insert "very "))
  638. (goto-char (point-min))
  639. (org-end-of-line)
  640. (eolp))))
  641. ;; At an headline with special movement.
  642. (should
  643. (org-test-with-temp-text "* Headline1 :tag:\n"
  644. (let ((org-special-ctrl-a/e t))
  645. (and (progn (org-end-of-line) (looking-at " :tag:"))
  646. (progn (org-end-of-line) (eolp))
  647. (progn (org-end-of-line) (looking-at " :tag:"))))))
  648. ;; At an headline without special movement.
  649. (should
  650. (org-test-with-temp-text "* Headline2 :tag:\n"
  651. (let ((org-special-ctrl-a/e nil))
  652. (and (progn (org-end-of-line) (eolp))
  653. (progn (org-end-of-line) (eolp))))))
  654. ;; At an headline, with reversed movement.
  655. (should
  656. (org-test-with-temp-text "* Headline3 :tag:\n"
  657. (let ((org-special-ctrl-a/e 'reversed)
  658. (this-command last-command))
  659. (and (progn (org-end-of-line) (eolp))
  660. (progn (org-end-of-line) (looking-at " :tag:"))))))
  661. ;; At a block without hidden contents.
  662. (should
  663. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  664. (progn (org-end-of-line) (eolp))))
  665. ;; At a block with hidden contents.
  666. (should-not
  667. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  668. (let ((org-special-ctrl-a/e t))
  669. (org-hide-block-toggle)
  670. (org-end-of-line)
  671. (eobp)))))
  672. (ert-deftest test-org/forward-paragraph ()
  673. "Test `org-forward-paragraph' specifications."
  674. ;; At end of buffer, return an error.
  675. (should-error
  676. (org-test-with-temp-text "Paragraph"
  677. (goto-char (point-max))
  678. (org-forward-paragraph)))
  679. ;; Standard test.
  680. (should
  681. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  682. (org-forward-paragraph)
  683. (looking-at "P2")))
  684. ;; Ignore depth.
  685. (should
  686. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  687. (org-forward-paragraph)
  688. (looking-at "P1")))
  689. ;; Do not enter elements with invisible contents.
  690. (should
  691. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  692. (org-hide-block-toggle)
  693. (org-forward-paragraph)
  694. (looking-at "P3")))
  695. ;; On an affiliated keyword, jump to the beginning of the element.
  696. (should
  697. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  698. (org-forward-paragraph)
  699. (looking-at "Para")))
  700. ;; On an item or a footnote definition, move to the second element
  701. ;; inside, if any.
  702. (should
  703. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  704. (org-forward-paragraph)
  705. (looking-at " Paragraph")))
  706. (should
  707. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  708. (org-forward-paragraph)
  709. (looking-at "Paragraph")))
  710. ;; On an item, or a footnote definition, when the first line is
  711. ;; empty, move to the first item.
  712. (should
  713. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  714. (org-forward-paragraph)
  715. (looking-at " Paragraph")))
  716. (should
  717. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  718. (org-forward-paragraph)
  719. (looking-at "Paragraph")))
  720. ;; On a table (resp. a property drawer) do not move through table
  721. ;; rows (resp. node properties).
  722. (should
  723. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  724. (org-forward-paragraph)
  725. (looking-at "Paragraph")))
  726. (should
  727. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
  728. (org-forward-paragraph)
  729. (looking-at "Paragraph")))
  730. ;; On a verse or source block, stop after blank lines.
  731. (should
  732. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  733. (org-forward-paragraph)
  734. (looking-at "L2")))
  735. (should
  736. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  737. (org-forward-paragraph)
  738. (looking-at "L2"))))
  739. (ert-deftest test-org/backward-paragraph ()
  740. "Test `org-backward-paragraph' specifications."
  741. ;; Error at beginning of buffer.
  742. (should-error
  743. (org-test-with-temp-text "Paragraph"
  744. (org-backward-paragraph)))
  745. ;; Regular test.
  746. (should
  747. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  748. (goto-char (point-max))
  749. (org-backward-paragraph)
  750. (looking-at "P3")))
  751. (should
  752. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  753. (goto-char (point-max))
  754. (beginning-of-line)
  755. (org-backward-paragraph)
  756. (looking-at "P2")))
  757. ;; Ignore depth.
  758. (should
  759. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  760. (goto-char (point-max))
  761. (beginning-of-line)
  762. (org-backward-paragraph)
  763. (looking-at "P2")))
  764. ;; Ignore invisible elements.
  765. (should
  766. (org-test-with-temp-text "* H1\n P1\n* H2"
  767. (org-cycle)
  768. (goto-char (point-max))
  769. (beginning-of-line)
  770. (org-backward-paragraph)
  771. (bobp)))
  772. ;; On an affiliated keyword, jump to the first one.
  773. (should
  774. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  775. (search-forward "c2")
  776. (org-backward-paragraph)
  777. (looking-at "#\\+name")))
  778. ;; On the second element in an item or a footnote definition, jump
  779. ;; to item or the definition.
  780. (should
  781. (org-test-with-temp-text "- line1\n\n line2"
  782. (goto-char (point-max))
  783. (beginning-of-line)
  784. (org-backward-paragraph)
  785. (looking-at "- line1")))
  786. (should
  787. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  788. (goto-char (point-max))
  789. (beginning-of-line)
  790. (org-backward-paragraph)
  791. (looking-at "\\[fn:1\\] line1")))
  792. ;; On a table (resp. a property drawer), ignore table rows
  793. ;; (resp. node properties).
  794. (should
  795. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  796. (goto-char (point-max))
  797. (beginning-of-line)
  798. (org-backward-paragraph)
  799. (bobp)))
  800. (should
  801. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
  802. (goto-char (point-max))
  803. (beginning-of-line)
  804. (org-backward-paragraph)
  805. (bobp)))
  806. ;; On a source or verse block, stop before blank lines.
  807. (should
  808. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  809. (search-forward "L3")
  810. (beginning-of-line)
  811. (org-backward-paragraph)
  812. (looking-at "L2")))
  813. (should
  814. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  815. (search-forward "L3")
  816. (beginning-of-line)
  817. (org-backward-paragraph)
  818. (looking-at "L2"))))
  819. (ert-deftest test-org/forward-element ()
  820. "Test `org-forward-element' specifications."
  821. ;; 1. At EOB: should error.
  822. (org-test-with-temp-text "Some text\n"
  823. (goto-char (point-max))
  824. (should-error (org-forward-element)))
  825. ;; 2. Standard move: expected to ignore blank lines.
  826. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  827. (org-forward-element)
  828. (should (looking-at (regexp-quote "Second paragraph."))))
  829. ;; 3. Headline tests.
  830. (org-test-with-temp-text "
  831. * Head 1
  832. ** Head 1.1
  833. *** Head 1.1.1
  834. ** Head 1.2"
  835. ;; 3.1. At an headline beginning: move to next headline at the
  836. ;; same level.
  837. (goto-line 3)
  838. (org-forward-element)
  839. (should (looking-at (regexp-quote "** Head 1.2")))
  840. ;; 3.2. At an headline beginning: move to parent headline if no
  841. ;; headline at the same level.
  842. (goto-line 3)
  843. (org-forward-element)
  844. (should (looking-at (regexp-quote "** Head 1.2"))))
  845. ;; 4. Greater element tests.
  846. (org-test-with-temp-text
  847. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  848. ;; 4.1. At a greater element: expected to skip contents.
  849. (org-forward-element)
  850. (should (looking-at (regexp-quote "Outside.")))
  851. ;; 4.2. At the end of greater element contents: expected to skip
  852. ;; to the end of the greater element.
  853. (goto-line 2)
  854. (org-forward-element)
  855. (should (looking-at (regexp-quote "Outside."))))
  856. ;; 5. List tests.
  857. (org-test-with-temp-text "
  858. - item1
  859. - sub1
  860. - sub2
  861. - sub3
  862. Inner paragraph.
  863. - item2
  864. Outside."
  865. ;; 5.1. At list top point: expected to move to the element after
  866. ;; the list.
  867. (goto-line 2)
  868. (org-forward-element)
  869. (should (looking-at (regexp-quote "Outside.")))
  870. ;; 5.2. Special case: at the first line of a sub-list, but not at
  871. ;; beginning of line, move to next item.
  872. (goto-line 2)
  873. (forward-char)
  874. (org-forward-element)
  875. (should (looking-at "- item2"))
  876. (goto-line 4)
  877. (forward-char)
  878. (org-forward-element)
  879. (should (looking-at " - sub2"))
  880. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  881. (goto-line 4)
  882. (org-forward-element)
  883. (should (looking-at (regexp-quote " Inner paragraph.")))
  884. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  885. (goto-line 8)
  886. (org-forward-element)
  887. (should (looking-at (regexp-quote " Inner paragraph.")))
  888. ;; 5.5. At an item: expected to move to next item, if any.
  889. (goto-line 6)
  890. (org-forward-element)
  891. (should (looking-at " - sub3"))))
  892. (ert-deftest test-org/backward-element ()
  893. "Test `org-backward-element' specifications."
  894. ;; 1. Should error at BOB.
  895. (org-test-with-temp-text " \nParagraph."
  896. (should-error (org-backward-element)))
  897. ;; 2. Should move at BOB when called on the first element in buffer.
  898. (should
  899. (org-test-with-temp-text "\n#+TITLE: test"
  900. (progn (forward-line)
  901. (org-backward-element)
  902. (bobp))))
  903. ;; 3. Not at the beginning of an element: move at its beginning.
  904. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  905. (goto-line 3)
  906. (end-of-line)
  907. (org-backward-element)
  908. (should (looking-at (regexp-quote "Paragraph2."))))
  909. ;; 4. Headline tests.
  910. (org-test-with-temp-text "
  911. * Head 1
  912. ** Head 1.1
  913. *** Head 1.1.1
  914. ** Head 1.2"
  915. ;; 4.1. At an headline beginning: move to previous headline at the
  916. ;; same level.
  917. (goto-line 5)
  918. (org-backward-element)
  919. (should (looking-at (regexp-quote "** Head 1.1")))
  920. ;; 4.2. At an headline beginning: move to parent headline if no
  921. ;; headline at the same level.
  922. (goto-line 3)
  923. (org-backward-element)
  924. (should (looking-at (regexp-quote "* Head 1")))
  925. ;; 4.3. At the first top-level headline: should error.
  926. (goto-line 2)
  927. (should-error (org-backward-element)))
  928. ;; 5. At beginning of first element inside a greater element:
  929. ;; expected to move to greater element's beginning.
  930. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  931. (goto-line 3)
  932. (org-backward-element)
  933. (should (looking-at "#\\+BEGIN_CENTER")))
  934. ;; 6. At the beginning of the first element in a section: should
  935. ;; move back to headline, if any.
  936. (should
  937. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  938. (progn (goto-char (point-max))
  939. (beginning-of-line)
  940. (org-backward-element)
  941. (org-at-heading-p))))
  942. ;; 7. List tests.
  943. (org-test-with-temp-text "
  944. - item1
  945. - sub1
  946. - sub2
  947. - sub3
  948. Inner paragraph.
  949. - item2
  950. Outside."
  951. ;; 7.1. At beginning of sub-list: expected to move to the
  952. ;; paragraph before it.
  953. (goto-line 4)
  954. (org-backward-element)
  955. (should (looking-at "item1"))
  956. ;; 7.2. At an item in a list: expected to move at previous item.
  957. (goto-line 8)
  958. (org-backward-element)
  959. (should (looking-at " - sub2"))
  960. (goto-line 12)
  961. (org-backward-element)
  962. (should (looking-at "- item1"))
  963. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  964. ;; beginning.
  965. (goto-line 10)
  966. (org-backward-element)
  967. (should (looking-at " - sub1"))
  968. (goto-line 15)
  969. (org-backward-element)
  970. (should (looking-at "- item1"))
  971. ;; 7.4. At blank-lines before list end: expected to move to top
  972. ;; item.
  973. (goto-line 14)
  974. (org-backward-element)
  975. (should (looking-at "- item1"))))
  976. (ert-deftest test-org/up-element ()
  977. "Test `org-up-element' specifications."
  978. ;; 1. At BOB or with no surrounding element: should error.
  979. (org-test-with-temp-text "Paragraph."
  980. (should-error (org-up-element)))
  981. (org-test-with-temp-text "* Head1\n* Head2"
  982. (goto-line 2)
  983. (should-error (org-up-element)))
  984. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  985. (goto-line 3)
  986. (should-error (org-up-element)))
  987. ;; 2. At an headline: move to parent headline.
  988. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  989. (goto-line 3)
  990. (org-up-element)
  991. (should (looking-at "\\* Head1")))
  992. ;; 3. Inside a greater element: move to greater element beginning.
  993. (org-test-with-temp-text
  994. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  995. (goto-line 3)
  996. (org-up-element)
  997. (should (looking-at "#\\+BEGIN_CENTER")))
  998. ;; 4. List tests.
  999. (org-test-with-temp-text "* Top
  1000. - item1
  1001. - sub1
  1002. - sub2
  1003. Paragraph within sub2.
  1004. - item2"
  1005. ;; 4.1. Within an item: move to the item beginning.
  1006. (goto-line 8)
  1007. (org-up-element)
  1008. (should (looking-at " - sub2"))
  1009. ;; 4.2. At an item in a sub-list: move to parent item.
  1010. (goto-line 4)
  1011. (org-up-element)
  1012. (should (looking-at "- item1"))
  1013. ;; 4.3. At an item in top list: move to beginning of whole list.
  1014. (goto-line 10)
  1015. (org-up-element)
  1016. (should (looking-at "- item1"))
  1017. ;; 4.4. Special case. At very top point: should move to parent of
  1018. ;; list.
  1019. (goto-line 2)
  1020. (org-up-element)
  1021. (should (looking-at "\\* Top"))))
  1022. (ert-deftest test-org/down-element ()
  1023. "Test `org-down-element' specifications."
  1024. ;; Error when the element hasn't got a recursive type.
  1025. (org-test-with-temp-text "Paragraph."
  1026. (should-error (org-down-element)))
  1027. ;; Error when the element has no contents
  1028. (org-test-with-temp-text "* Headline"
  1029. (should-error (org-down-element)))
  1030. ;; When at a plain-list, move to first item.
  1031. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1032. (goto-line 2)
  1033. (org-down-element)
  1034. (should (looking-at " - Item 1.1")))
  1035. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1036. (org-down-element)
  1037. (should (looking-at " Item 1")))
  1038. ;; When at a table, move to first row
  1039. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1040. (org-down-element)
  1041. (should (looking-at " a | b |")))
  1042. ;; Otherwise, move inside the greater element.
  1043. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1044. (org-down-element)
  1045. (should (looking-at "Paragraph"))))
  1046. (ert-deftest test-org/drag-element-backward ()
  1047. "Test `org-drag-element-backward' specifications."
  1048. ;; 1. Error when trying to move first element of buffer.
  1049. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1050. (should-error (org-drag-element-backward)))
  1051. ;; 2. Error when trying to swap nested elements.
  1052. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1053. (forward-line)
  1054. (should-error (org-drag-element-backward)))
  1055. ;; 3. Error when trying to swap an headline element and
  1056. ;; a non-headline element.
  1057. (org-test-with-temp-text "Test.\n* Head 1"
  1058. (forward-line)
  1059. (should-error (org-drag-element-backward)))
  1060. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1061. ;; between elements.
  1062. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  1063. (search-forward "graph")
  1064. (org-drag-element-backward)
  1065. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  1066. (should (looking-at " 2")))
  1067. ;; 5. Preserve visibility of elements and their contents.
  1068. (org-test-with-temp-text "
  1069. #+BEGIN_CENTER
  1070. Text.
  1071. #+END_CENTER
  1072. - item 1
  1073. #+BEGIN_QUOTE
  1074. Text.
  1075. #+END_QUOTE"
  1076. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1077. (search-backward "- item 1")
  1078. (org-drag-element-backward)
  1079. (should
  1080. (equal
  1081. '((63 . 82) (26 . 48))
  1082. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1083. (overlays-in (point-min) (point-max)))))))
  1084. (ert-deftest test-org/drag-element-forward ()
  1085. "Test `org-drag-element-forward' specifications."
  1086. ;; 1. Error when trying to move first element of buffer.
  1087. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1088. (goto-line 3)
  1089. (should-error (org-drag-element-forward)))
  1090. ;; 2. Error when trying to swap nested elements.
  1091. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1092. (forward-line)
  1093. (should-error (org-drag-element-forward)))
  1094. ;; 3. Error when trying to swap a non-headline element and an
  1095. ;; headline.
  1096. (org-test-with-temp-text "Test.\n* Head 1"
  1097. (should-error (org-drag-element-forward)))
  1098. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1099. ;; between elements.
  1100. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1101. (search-forward "graph")
  1102. (org-drag-element-forward)
  1103. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1104. (should (looking-at " 1")))
  1105. ;; 5. Preserve visibility of elements and their contents.
  1106. (org-test-with-temp-text "
  1107. #+BEGIN_CENTER
  1108. Text.
  1109. #+END_CENTER
  1110. - item 1
  1111. #+BEGIN_QUOTE
  1112. Text.
  1113. #+END_QUOTE"
  1114. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1115. (search-backward "#+BEGIN_CENTER")
  1116. (org-drag-element-forward)
  1117. (should
  1118. (equal
  1119. '((63 . 82) (26 . 48))
  1120. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1121. (overlays-in (point-min) (point-max)))))))
  1122. ;;; Planning
  1123. (ert-deftest test-org/timestamp-has-time-p ()
  1124. "Test `org-timestamp-has-time-p' specifications."
  1125. ;; With time.
  1126. (should
  1127. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1128. (org-timestamp-has-time-p (org-element-context))))
  1129. ;; Without time.
  1130. (should-not
  1131. (org-test-with-temp-text "<2012-03-29 Thu>"
  1132. (org-timestamp-has-time-p (org-element-context)))))
  1133. (ert-deftest test-org/timestamp-format ()
  1134. "Test `org-timestamp-format' specifications."
  1135. ;; Regular test.
  1136. (should
  1137. (equal
  1138. "2012-03-29 16:40"
  1139. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1140. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1141. ;; Range end.
  1142. (should
  1143. (equal
  1144. "2012-03-29"
  1145. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1146. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1147. (ert-deftest test-org/timestamp-split-range ()
  1148. "Test `org-timestamp-split-range' specifications."
  1149. ;; Extract range start (active).
  1150. (should
  1151. (equal '(2012 3 29)
  1152. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1153. (let ((ts (org-timestamp-split-range (org-element-context))))
  1154. (mapcar (lambda (p) (org-element-property p ts))
  1155. '(:year-end :month-end :day-end))))))
  1156. ;; Extract range start (inactive)
  1157. (should
  1158. (equal '(2012 3 29)
  1159. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1160. (let ((ts (org-timestamp-split-range (org-element-context))))
  1161. (mapcar (lambda (p) (org-element-property p ts))
  1162. '(:year-end :month-end :day-end))))))
  1163. ;; Extract range end (active).
  1164. (should
  1165. (equal '(2012 3 30)
  1166. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1167. (let ((ts (org-timestamp-split-range
  1168. (org-element-context) t)))
  1169. (mapcar (lambda (p) (org-element-property p ts))
  1170. '(:year-end :month-end :day-end))))))
  1171. ;; Extract range end (inactive)
  1172. (should
  1173. (equal '(2012 3 30)
  1174. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1175. (let ((ts (org-timestamp-split-range
  1176. (org-element-context) t)))
  1177. (mapcar (lambda (p) (org-element-property p ts))
  1178. '(:year-end :month-end :day-end))))))
  1179. ;; Return the timestamp if not a range.
  1180. (should
  1181. (org-test-with-temp-text "[2012-03-29 Thu]"
  1182. (let* ((ts-orig (org-element-context))
  1183. (ts-copy (org-timestamp-split-range ts-orig)))
  1184. (eq ts-orig ts-copy))))
  1185. (should
  1186. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1187. (let* ((ts-orig (org-element-context))
  1188. (ts-copy (org-timestamp-split-range ts-orig)))
  1189. (eq ts-orig ts-copy))))
  1190. ;; Check that parent is the same when a range was split.
  1191. (should
  1192. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1193. (let* ((ts-orig (org-element-context))
  1194. (ts-copy (org-timestamp-split-range ts-orig)))
  1195. (eq (org-element-property :parent ts-orig)
  1196. (org-element-property :parent ts-copy))))))
  1197. (ert-deftest test-org/timestamp-translate ()
  1198. "Test `org-timestamp-translate' specifications."
  1199. ;; Translate whole date range.
  1200. (should
  1201. (equal "<29>--<30>"
  1202. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1203. (let ((org-display-custom-times t)
  1204. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1205. (org-timestamp-translate (org-element-context))))))
  1206. ;; Translate date range start.
  1207. (should
  1208. (equal "<29>"
  1209. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1210. (let ((org-display-custom-times t)
  1211. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1212. (org-timestamp-translate (org-element-context) 'start)))))
  1213. ;; Translate date range end.
  1214. (should
  1215. (equal "<30>"
  1216. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1217. (let ((org-display-custom-times t)
  1218. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1219. (org-timestamp-translate (org-element-context) 'end)))))
  1220. ;; Translate time range.
  1221. (should
  1222. (equal "<08>--<16>"
  1223. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1224. (let ((org-display-custom-times t)
  1225. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1226. (org-timestamp-translate (org-element-context))))))
  1227. ;; Translate non-range timestamp.
  1228. (should
  1229. (equal "<29>"
  1230. (org-test-with-temp-text "<2012-03-29 Thu>"
  1231. (let ((org-display-custom-times t)
  1232. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1233. (org-timestamp-translate (org-element-context))))))
  1234. ;; Do not change `diary' timestamps.
  1235. (should
  1236. (equal "<%%(org-float t 4 2)>"
  1237. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1238. (let ((org-display-custom-times t)
  1239. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1240. (org-timestamp-translate (org-element-context)))))))
  1241. ;;; Targets and Radio Targets
  1242. (ert-deftest test-org/all-targets ()
  1243. "Test `org-all-targets' specifications."
  1244. ;; Without an argument.
  1245. (should
  1246. (equal '("radio-target" "target")
  1247. (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
  1248. (org-all-targets))))
  1249. (should
  1250. (equal '("radio-target")
  1251. (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
  1252. ;; With argument.
  1253. (should
  1254. (equal '("radio-target")
  1255. (org-test-with-temp-text "<<target>> <<<radio-target>>>"
  1256. (org-all-targets t)))))
  1257. (provide 'test-org)
  1258. ;;; test-org.el ends here