test-org.el 42 KB

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