test-org.el 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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. ;; In a source block, use appropriate syntax.
  90. (should
  91. (equal " ;; "
  92. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
  93. (forward-line)
  94. (let ((org-edit-src-content-indentation 2))
  95. (call-interactively 'comment-dwim))
  96. (buffer-substring-no-properties (line-beginning-position) (point)))))
  97. (should
  98. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  99. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
  100. (forward-line)
  101. (transient-mark-mode 1)
  102. (push-mark (point) t t)
  103. (forward-line 2)
  104. (let ((org-edit-src-content-indentation 2))
  105. (call-interactively 'comment-dwim))
  106. (buffer-string)))))
  107. ;;; Date and time analysis
  108. (ert-deftest test-org/org-read-date ()
  109. "Test `org-read-date' specifications."
  110. ;; Parse ISO date with abbreviated year and month.
  111. (should (equal "2012-03-29 16:40"
  112. (let ((org-time-was-given t))
  113. (org-read-date t nil "12-3-29 16:40"))))
  114. ;; Parse Europeans dates.
  115. (should (equal "2012-03-29 16:40"
  116. (let ((org-time-was-given t))
  117. (org-read-date t nil "29.03.2012 16:40"))))
  118. ;; Parse Europeans dates without year.
  119. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  120. (let ((org-time-was-given t))
  121. (org-read-date t nil "29.03. 16:40")))))
  122. (ert-deftest test-org/org-parse-time-string ()
  123. "Test `org-parse-time-string'."
  124. (should (equal (org-parse-time-string "2012-03-29 16:40")
  125. '(0 40 16 29 3 2012 nil nil nil)))
  126. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  127. '(0 40 16 29 3 2012 nil nil nil)))
  128. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  129. '(0 40 16 29 3 2012 nil nil nil)))
  130. (should (equal (org-parse-time-string "<2012-03-29>")
  131. '(0 0 0 29 3 2012 nil nil nil)))
  132. (should (equal (org-parse-time-string "<2012-03-29>" t)
  133. '(0 nil nil 29 3 2012 nil nil nil))))
  134. ;;; Filling
  135. (ert-deftest test-org/fill-paragraph ()
  136. "Test `org-fill-paragraph' specifications."
  137. ;; At an Org table, align it.
  138. (should
  139. (equal "| a |\n"
  140. (org-test-with-temp-text "|a|"
  141. (org-fill-paragraph)
  142. (buffer-string))))
  143. (should
  144. (equal "#+name: table\n| a |\n"
  145. (org-test-with-temp-text "#+name: table\n| a |"
  146. (org-fill-paragraph)
  147. (buffer-string))))
  148. ;; At a paragraph, preserve line breaks.
  149. (org-test-with-temp-text "some \\\\\nlong\ntext"
  150. (let ((fill-column 20))
  151. (org-fill-paragraph)
  152. (should (equal (buffer-string) "some \\\\\nlong text"))))
  153. ;; Correctly fill a paragraph when point is at its very end.
  154. (should
  155. (equal "A B"
  156. (org-test-with-temp-text "A\nB"
  157. (let ((fill-column 20))
  158. (goto-char (point-max))
  159. (org-fill-paragraph)
  160. (buffer-string)))))
  161. ;; Correctly fill the last paragraph of a greater element.
  162. (should
  163. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  164. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  165. (let ((fill-column 8))
  166. (forward-line)
  167. (end-of-line)
  168. (org-fill-paragraph)
  169. (buffer-string)))))
  170. ;; Correctly fill an element in a narrowed buffer.
  171. (should
  172. (equal "01234\n6"
  173. (org-test-with-temp-text "01234 6789"
  174. (let ((fill-column 5))
  175. (narrow-to-region 1 8)
  176. (org-fill-paragraph)
  177. (buffer-string)))))
  178. ;; Handle `adaptive-fill-regexp' in paragraphs.
  179. (should
  180. (equal "> a b"
  181. (org-test-with-temp-text "> a\n> b"
  182. (let ((fill-column 5)
  183. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  184. (org-fill-paragraph)
  185. (buffer-string)))))
  186. ;; Special case: Fill first paragraph when point is at an item or
  187. ;; a plain-list or a footnote reference.
  188. (should
  189. (equal "- A B"
  190. (org-test-with-temp-text "- A\n B"
  191. (let ((fill-column 20))
  192. (org-fill-paragraph)
  193. (buffer-string)))))
  194. (should
  195. (equal "[fn:1] A B"
  196. (org-test-with-temp-text "[fn:1] A\nB"
  197. (let ((fill-column 20))
  198. (org-fill-paragraph)
  199. (buffer-string)))))
  200. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  201. (let ((fill-column 20))
  202. (org-fill-paragraph)
  203. (should (equal (buffer-string)
  204. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  205. ;; Fill contents of `comment-block' elements.
  206. (should
  207. (equal
  208. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  209. (let ((fill-column 20))
  210. (forward-line)
  211. (org-fill-paragraph)
  212. (buffer-string)))
  213. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  214. ;; Fill `comment' elements.
  215. (should
  216. (equal " # A B"
  217. (org-test-with-temp-text " # A\n # B"
  218. (let ((fill-column 20))
  219. (org-fill-paragraph)
  220. (buffer-string)))))
  221. ;; Do not mix consecutive comments when filling one of them.
  222. (should
  223. (equal "# A B\n\n# C"
  224. (org-test-with-temp-text "# A\n# B\n\n# C"
  225. (let ((fill-column 20))
  226. (org-fill-paragraph)
  227. (buffer-string)))))
  228. ;; Use commented empty lines as separators when filling comments.
  229. (should
  230. (equal "# A B\n#\n# C"
  231. (org-test-with-temp-text "# A\n# B\n#\n# C"
  232. (let ((fill-column 20))
  233. (org-fill-paragraph)
  234. (buffer-string)))))
  235. ;; Handle `adaptive-fill-regexp' in comments.
  236. (should
  237. (equal "# > a b"
  238. (org-test-with-temp-text "# > a\n# > b"
  239. (let ((fill-column 20)
  240. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  241. (org-fill-paragraph)
  242. (buffer-string)))))
  243. ;; Do nothing at affiliated keywords.
  244. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  245. (let ((fill-column 20))
  246. (org-fill-paragraph)
  247. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  248. ;; Do not move point after table when filling a table.
  249. (should-not
  250. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  251. (forward-char)
  252. (org-fill-paragraph)
  253. (eobp))))
  254. (ert-deftest test-org/auto-fill-function ()
  255. "Test auto-filling features."
  256. ;; Auto fill paragraph.
  257. (should
  258. (equal "12345\n7890"
  259. (org-test-with-temp-text "12345 7890"
  260. (let ((fill-column 5))
  261. (end-of-line)
  262. (org-auto-fill-function)
  263. (buffer-string)))))
  264. ;; Auto fill first paragraph in an item.
  265. (should
  266. (equal "- 12345\n 7890"
  267. (org-test-with-temp-text "- 12345 7890"
  268. (let ((fill-column 7))
  269. (end-of-line)
  270. (org-auto-fill-function)
  271. (buffer-string)))))
  272. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  273. (should
  274. (equal "> 12345\n 7890"
  275. (org-test-with-temp-text "> 12345 7890"
  276. (let ((fill-column 10)
  277. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  278. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  279. (end-of-line)
  280. (org-auto-fill-function)
  281. (buffer-string)))))
  282. (should
  283. (equal "> 12345\n> 12345\n> 7890"
  284. (org-test-with-temp-text "> 12345\n> 12345 7890"
  285. (let ((fill-column 10)
  286. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  287. (goto-char (point-max))
  288. (org-auto-fill-function)
  289. (buffer-string)))))
  290. (should-not
  291. (equal " 12345\n *12345\n *12345"
  292. (org-test-with-temp-text " 12345\n *12345 12345"
  293. (let ((fill-column 10)
  294. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  295. (goto-char (point-max))
  296. (org-auto-fill-function)
  297. (buffer-string)))))
  298. ;; Auto fill comments.
  299. (should
  300. (equal " # 12345\n # 7890"
  301. (org-test-with-temp-text " # 12345 7890"
  302. (let ((fill-column 10))
  303. (end-of-line)
  304. (org-auto-fill-function)
  305. (buffer-string)))))
  306. ;; A hash within a line isn't a comment.
  307. (should-not
  308. (equal "12345 # 7890\n# 1"
  309. (org-test-with-temp-text "12345 # 7890 1"
  310. (let ((fill-column 12))
  311. (end-of-line)
  312. (org-auto-fill-function)
  313. (buffer-string)))))
  314. ;; Correctly interpret empty prefix.
  315. (should-not
  316. (equal "# a\n# b\nRegular\n# paragraph"
  317. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  318. (let ((fill-column 12))
  319. (end-of-line 3)
  320. (org-auto-fill-function)
  321. (buffer-string)))))
  322. ;; Comment block: auto fill contents.
  323. (should
  324. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  325. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  326. (let ((fill-column 5))
  327. (forward-line)
  328. (end-of-line)
  329. (org-auto-fill-function)
  330. (buffer-string)))))
  331. (should
  332. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  333. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  334. (let ((fill-column 5))
  335. (forward-line)
  336. (end-of-line)
  337. (org-auto-fill-function)
  338. (buffer-string)))))
  339. ;; Do not fill if a new item could be created.
  340. (should-not
  341. (equal "12345\n- 90"
  342. (org-test-with-temp-text "12345 - 90"
  343. (let ((fill-column 5))
  344. (end-of-line)
  345. (org-auto-fill-function)
  346. (buffer-string)))))
  347. ;; Do not fill if a line break could be introduced.
  348. (should-not
  349. (equal "123\\\\\n7890"
  350. (org-test-with-temp-text "123\\\\ 7890"
  351. (let ((fill-column 6))
  352. (end-of-line)
  353. (org-auto-fill-function)
  354. (buffer-string)))))
  355. ;; Do not fill affiliated keywords.
  356. (should-not
  357. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  358. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  359. (let ((fill-column 20))
  360. (end-of-line)
  361. (org-auto-fill-function)
  362. (buffer-string))))))
  363. ;;; Editing
  364. ;;;; Insert elements
  365. (ert-deftest test-org/meta-return ()
  366. "Test M-RET (`org-meta-return')."
  367. ;; In a table field insert a row above.
  368. (should
  369. (org-test-with-temp-text "| a |"
  370. (forward-char)
  371. (org-meta-return)
  372. (forward-line -1)
  373. (looking-at "| |$")))
  374. ;; In a paragraph change current line into a header.
  375. (should
  376. (org-test-with-temp-text "a"
  377. (org-meta-return)
  378. (beginning-of-line)
  379. (looking-at "\* a$")))
  380. ;; In an item insert an item, in this case above.
  381. (should
  382. (org-test-with-temp-text "- a"
  383. (org-meta-return)
  384. (beginning-of-line)
  385. (looking-at "- $")))
  386. ;; In a drawer and paragraph insert an empty line, in this case above.
  387. (should
  388. (org-test-with-temp-text ":MYDRAWER:\na\n:END:"
  389. (forward-line)
  390. (org-meta-return)
  391. (forward-line -1)
  392. (looking-at "$")))
  393. ;; In a drawer and item insert an item, in this case above.
  394. (should
  395. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  396. (forward-line)
  397. (org-meta-return)
  398. (beginning-of-line)
  399. (looking-at "- $"))))
  400. (ert-deftest test-org/insert-todo-heading-respect-content ()
  401. "Test `org-insert-todo-heading-respect-content' specifications."
  402. ;; Create a TODO heading.
  403. (should
  404. (org-test-with-temp-text "* H1\n Body"
  405. (org-insert-todo-heading-respect-content)
  406. (nth 2 (org-heading-components))))
  407. ;; Add headline after body of current subtree.
  408. (should
  409. (org-test-with-temp-text "* H1\nBody"
  410. (org-insert-todo-heading-respect-content)
  411. (eobp)))
  412. (should
  413. (org-test-with-temp-text "* H1\n** H2\nBody"
  414. (org-insert-todo-heading-respect-content)
  415. (eobp)))
  416. ;; In a list, do not create a new item.
  417. (should
  418. (org-test-with-temp-text "* H\n- an item\n- another one"
  419. (search-forward "an ")
  420. (org-insert-todo-heading-respect-content)
  421. (and (eobp) (org-at-heading-p)))))
  422. ;;; Fixed-Width Areas
  423. (ert-deftest test-org/toggle-fixed-with ()
  424. "Test `org-toggle-fixed-width' specifications."
  425. ;; No region: Toggle on fixed-width marker in paragraphs.
  426. (should
  427. (equal ": A"
  428. (org-test-with-temp-text "A"
  429. (org-toggle-fixed-width)
  430. (buffer-string))))
  431. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  432. (should
  433. (equal "A"
  434. (org-test-with-temp-text ": A"
  435. (org-toggle-fixed-width)
  436. (buffer-string))))
  437. ;; No region: Toggle on marker in blank lines after elements or just
  438. ;; after a headline.
  439. (should
  440. (equal "* H\n: "
  441. (org-test-with-temp-text "* H\n"
  442. (forward-line)
  443. (org-toggle-fixed-width)
  444. (buffer-string))))
  445. (should
  446. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  447. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  448. (goto-char (point-max))
  449. (org-toggle-fixed-width)
  450. (buffer-string))))
  451. ;; No region: Toggle on marker in front of one line elements (e.g.,
  452. ;; headlines, clocks)
  453. (should
  454. (equal ": * Headline"
  455. (org-test-with-temp-text "* Headline"
  456. (org-toggle-fixed-width)
  457. (buffer-string))))
  458. (should
  459. (equal ": #+KEYWORD: value"
  460. (org-test-with-temp-text "#+KEYWORD: value"
  461. (org-toggle-fixed-width)
  462. (buffer-string))))
  463. ;; No region: error in other situations.
  464. (should-error
  465. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  466. (forward-line)
  467. (org-toggle-fixed-width)
  468. (buffer-string)))
  469. ;; No region: Indentation is preserved.
  470. (should
  471. (equal "- A\n : B"
  472. (org-test-with-temp-text "- A\n B"
  473. (forward-line)
  474. (org-toggle-fixed-width)
  475. (buffer-string))))
  476. ;; Region: If it contains only fixed-width elements and blank lines,
  477. ;; toggle off fixed-width markup.
  478. (should
  479. (equal "A\n\nB"
  480. (org-test-with-temp-text ": A\n\n: B"
  481. (transient-mark-mode 1)
  482. (push-mark (point) t t)
  483. (goto-char (point-max))
  484. (org-toggle-fixed-width)
  485. (buffer-string))))
  486. ;; Region: If it contains anything else, toggle on fixed-width but
  487. ;; not on fixed-width areas.
  488. (should
  489. (equal ": A\n: \n: B\n: \n: C"
  490. (org-test-with-temp-text "A\n\n: B\n\nC"
  491. (transient-mark-mode 1)
  492. (push-mark (point) t t)
  493. (goto-char (point-max))
  494. (org-toggle-fixed-width)
  495. (buffer-string))))
  496. ;; Region: Ignore blank lines at its end, unless it contains only
  497. ;; such lines.
  498. (should
  499. (equal ": A\n\n"
  500. (org-test-with-temp-text "A\n\n"
  501. (transient-mark-mode 1)
  502. (push-mark (point) t t)
  503. (goto-char (point-max))
  504. (org-toggle-fixed-width)
  505. (buffer-string))))
  506. (should
  507. (equal ": \n: \n"
  508. (org-test-with-temp-text "\n\n"
  509. (transient-mark-mode 1)
  510. (push-mark (point) t t)
  511. (goto-char (point-max))
  512. (org-toggle-fixed-width)
  513. (buffer-string)))))
  514. ;;; Links
  515. ;;;; Custom ID
  516. (ert-deftest test-org/custom-id ()
  517. "Test custom ID links specifications."
  518. (should
  519. (org-test-with-temp-text
  520. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  521. (goto-char (point-max))
  522. (org-open-at-point)
  523. (org-looking-at-p "\\* H1"))))
  524. ;;;; Fuzzy Links
  525. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  526. ;; a named element (#+name: text) and to headlines (* Text).
  527. (ert-deftest test-org/fuzzy-links ()
  528. "Test fuzzy links specifications."
  529. ;; 1. Fuzzy link goes in priority to a matching target.
  530. (should
  531. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  532. (goto-line 5)
  533. (org-open-at-point)
  534. (looking-at "<<Test>>")))
  535. ;; 2. Then fuzzy link points to an element with a given name.
  536. (should
  537. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  538. (goto-line 5)
  539. (org-open-at-point)
  540. (looking-at "#\\+NAME: Test")))
  541. ;; 3. A target still lead to a matching headline otherwise.
  542. (should
  543. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  544. (goto-line 4)
  545. (org-open-at-point)
  546. (looking-at "\\* Head2")))
  547. ;; 4. With a leading star in link, enforce heading match.
  548. (should
  549. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  550. (goto-line 3)
  551. (org-open-at-point)
  552. (looking-at "\\* Test"))))
  553. ;;;; Link Escaping
  554. (ert-deftest test-org/org-link-escape-ascii-character ()
  555. "Escape an ascii character."
  556. (should
  557. (string=
  558. "%5B"
  559. (org-link-escape "["))))
  560. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  561. "Escape an ascii control character."
  562. (should
  563. (string=
  564. "%09"
  565. (org-link-escape "\t"))))
  566. (ert-deftest test-org/org-link-escape-multibyte-character ()
  567. "Escape an unicode multibyte character."
  568. (should
  569. (string=
  570. "%E2%82%AC"
  571. (org-link-escape "€"))))
  572. (ert-deftest test-org/org-link-escape-custom-table ()
  573. "Escape string with custom character table."
  574. (should
  575. (string=
  576. "Foo%3A%42ar%0A"
  577. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  578. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  579. "Escape string with custom table merged with default table."
  580. (should
  581. (string=
  582. "%5BF%6F%6F%3A%42ar%0A%5D"
  583. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  584. (ert-deftest test-org/org-link-unescape-ascii-character ()
  585. "Unescape an ascii character."
  586. (should
  587. (string=
  588. "["
  589. (org-link-unescape "%5B"))))
  590. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  591. "Unescpae an ascii control character."
  592. (should
  593. (string=
  594. "\n"
  595. (org-link-unescape "%0A"))))
  596. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  597. "Unescape unicode multibyte character."
  598. (should
  599. (string=
  600. "€"
  601. (org-link-unescape "%E2%82%AC"))))
  602. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  603. "Unescape old style percent escaped character."
  604. (should
  605. (string=
  606. "àâçèéêîôùû"
  607. (decode-coding-string
  608. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  609. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  610. "Escape and unescape a URL that includes an escaped char.
  611. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  612. (should
  613. (string=
  614. "http://some.host.com/form?&id=blah%2Bblah25"
  615. (org-link-unescape
  616. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  617. (ert-deftest test-org/org-link-escape-chars-browser ()
  618. "Test of the constant `org-link-escape-chars-browser'.
  619. See there why this test is a candidate to be removed once Org
  620. drops support for Emacs 24.1 and 24.2."
  621. (should
  622. (string=
  623. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  624. "%22Release%208.2%22&idxname=emacs-orgmode")
  625. (org-link-escape-browser ; Do not replace with `url-encode-url',
  626. ; see docstring above.
  627. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  628. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  629. ;;; Node Properties
  630. (ert-deftest test-org/accumulated-properties-in-drawers ()
  631. "Ensure properties accumulate in subtree drawers."
  632. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  633. (org-babel-next-src-block)
  634. (should (equal '(2 1) (org-babel-execute-src-block)))))
  635. ;;; Mark Region
  636. (ert-deftest test-org/mark-subtree ()
  637. "Test `org-mark-subtree' specifications."
  638. ;; Error when point is before first headline.
  639. (should-error
  640. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  641. (progn (transient-mark-mode 1)
  642. (org-mark-subtree))))
  643. ;; Without argument, mark current subtree.
  644. (should
  645. (equal
  646. '(12 32)
  647. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  648. (progn (transient-mark-mode 1)
  649. (forward-line 2)
  650. (org-mark-subtree)
  651. (list (region-beginning) (region-end))))))
  652. ;; With an argument, move ARG up.
  653. (should
  654. (equal
  655. '(1 32)
  656. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  657. (progn (transient-mark-mode 1)
  658. (forward-line 2)
  659. (org-mark-subtree 1)
  660. (list (region-beginning) (region-end))))))
  661. ;; Do not get fooled by inlinetasks.
  662. (when (featurep 'org-inlinetask)
  663. (should
  664. (= 1
  665. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  666. (progn (transient-mark-mode 1)
  667. (forward-line 1)
  668. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  669. (region-beginning)))))))
  670. ;;; Navigation
  671. (ert-deftest test-org/beginning-of-line ()
  672. "Test `org-beginning-of-line' specifications."
  673. ;; Standard test.
  674. (should
  675. (org-test-with-temp-text "Some text\nSome other text"
  676. (progn (org-beginning-of-line) (bolp))))
  677. ;; Standard test with `visual-line-mode'.
  678. (should-not
  679. (org-test-with-temp-text "A long line of text\nSome other text"
  680. (progn (visual-line-mode)
  681. (forward-char 2)
  682. (dotimes (i 1000) (insert "very "))
  683. (org-beginning-of-line)
  684. (bolp))))
  685. ;; At an headline with special movement.
  686. (should
  687. (org-test-with-temp-text "* TODO Headline"
  688. (let ((org-special-ctrl-a/e t))
  689. (org-end-of-line)
  690. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  691. (progn (org-beginning-of-line) (bolp))
  692. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  693. (ert-deftest test-org/end-of-line ()
  694. "Test `org-end-of-line' specifications."
  695. ;; Standard test.
  696. (should
  697. (org-test-with-temp-text "Some text\nSome other text"
  698. (progn (org-end-of-line) (eolp))))
  699. ;; Standard test with `visual-line-mode'.
  700. (should-not
  701. (org-test-with-temp-text "A long line of text\nSome other text"
  702. (progn (visual-line-mode)
  703. (forward-char 2)
  704. (dotimes (i 1000) (insert "very "))
  705. (goto-char (point-min))
  706. (org-end-of-line)
  707. (eolp))))
  708. ;; At an headline with special movement.
  709. (should
  710. (org-test-with-temp-text "* Headline1 :tag:\n"
  711. (let ((org-special-ctrl-a/e t))
  712. (and (progn (org-end-of-line) (looking-at " :tag:"))
  713. (progn (org-end-of-line) (eolp))
  714. (progn (org-end-of-line) (looking-at " :tag:"))))))
  715. ;; At an headline without special movement.
  716. (should
  717. (org-test-with-temp-text "* Headline2 :tag:\n"
  718. (let ((org-special-ctrl-a/e nil))
  719. (and (progn (org-end-of-line) (eolp))
  720. (progn (org-end-of-line) (eolp))))))
  721. ;; At an headline, with reversed movement.
  722. (should
  723. (org-test-with-temp-text "* Headline3 :tag:\n"
  724. (let ((org-special-ctrl-a/e 'reversed)
  725. (this-command last-command))
  726. (and (progn (org-end-of-line) (eolp))
  727. (progn (org-end-of-line) (looking-at " :tag:"))))))
  728. ;; At a block without hidden contents.
  729. (should
  730. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  731. (progn (org-end-of-line) (eolp))))
  732. ;; At a block with hidden contents.
  733. (should-not
  734. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  735. (let ((org-special-ctrl-a/e t))
  736. (org-hide-block-toggle)
  737. (org-end-of-line)
  738. (eobp)))))
  739. (ert-deftest test-org/forward-paragraph ()
  740. "Test `org-forward-paragraph' specifications."
  741. ;; At end of buffer, return an error.
  742. (should-error
  743. (org-test-with-temp-text "Paragraph"
  744. (goto-char (point-max))
  745. (org-forward-paragraph)))
  746. ;; Standard test.
  747. (should
  748. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  749. (org-forward-paragraph)
  750. (looking-at "P2")))
  751. ;; Ignore depth.
  752. (should
  753. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  754. (org-forward-paragraph)
  755. (looking-at "P1")))
  756. ;; Do not enter elements with invisible contents.
  757. (should
  758. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  759. (org-hide-block-toggle)
  760. (org-forward-paragraph)
  761. (looking-at "P3")))
  762. ;; On an affiliated keyword, jump to the beginning of the element.
  763. (should
  764. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  765. (org-forward-paragraph)
  766. (looking-at "Para")))
  767. ;; On an item or a footnote definition, move to the second element
  768. ;; inside, if any.
  769. (should
  770. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  771. (org-forward-paragraph)
  772. (looking-at " Paragraph")))
  773. (should
  774. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  775. (org-forward-paragraph)
  776. (looking-at "Paragraph")))
  777. ;; On an item, or a footnote definition, when the first line is
  778. ;; empty, move to the first item.
  779. (should
  780. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  781. (org-forward-paragraph)
  782. (looking-at " Paragraph")))
  783. (should
  784. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  785. (org-forward-paragraph)
  786. (looking-at "Paragraph")))
  787. ;; On a table (resp. a property drawer) do not move through table
  788. ;; rows (resp. node properties).
  789. (should
  790. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  791. (org-forward-paragraph)
  792. (looking-at "Paragraph")))
  793. (should
  794. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
  795. (org-forward-paragraph)
  796. (looking-at "Paragraph")))
  797. ;; On a verse or source block, stop after blank lines.
  798. (should
  799. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  800. (org-forward-paragraph)
  801. (looking-at "L2")))
  802. (should
  803. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  804. (org-forward-paragraph)
  805. (looking-at "L2"))))
  806. (ert-deftest test-org/backward-paragraph ()
  807. "Test `org-backward-paragraph' specifications."
  808. ;; Error at beginning of buffer.
  809. (should-error
  810. (org-test-with-temp-text "Paragraph"
  811. (org-backward-paragraph)))
  812. ;; Regular test.
  813. (should
  814. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  815. (goto-char (point-max))
  816. (org-backward-paragraph)
  817. (looking-at "P3")))
  818. (should
  819. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  820. (goto-char (point-max))
  821. (beginning-of-line)
  822. (org-backward-paragraph)
  823. (looking-at "P2")))
  824. ;; Ignore depth.
  825. (should
  826. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  827. (goto-char (point-max))
  828. (beginning-of-line)
  829. (org-backward-paragraph)
  830. (looking-at "P2")))
  831. ;; Ignore invisible elements.
  832. (should
  833. (org-test-with-temp-text "* H1\n P1\n* H2"
  834. (org-cycle)
  835. (goto-char (point-max))
  836. (beginning-of-line)
  837. (org-backward-paragraph)
  838. (bobp)))
  839. ;; On an affiliated keyword, jump to the first one.
  840. (should
  841. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  842. (search-forward "c2")
  843. (org-backward-paragraph)
  844. (looking-at "#\\+name")))
  845. ;; On the second element in an item or a footnote definition, jump
  846. ;; to item or the definition.
  847. (should
  848. (org-test-with-temp-text "- line1\n\n line2"
  849. (goto-char (point-max))
  850. (beginning-of-line)
  851. (org-backward-paragraph)
  852. (looking-at "- line1")))
  853. (should
  854. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  855. (goto-char (point-max))
  856. (beginning-of-line)
  857. (org-backward-paragraph)
  858. (looking-at "\\[fn:1\\] line1")))
  859. ;; On a table (resp. a property drawer), ignore table rows
  860. ;; (resp. node properties).
  861. (should
  862. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  863. (goto-char (point-max))
  864. (beginning-of-line)
  865. (org-backward-paragraph)
  866. (bobp)))
  867. (should
  868. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
  869. (goto-char (point-max))
  870. (beginning-of-line)
  871. (org-backward-paragraph)
  872. (bobp)))
  873. ;; On a source or verse block, stop before blank lines.
  874. (should
  875. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  876. (search-forward "L3")
  877. (beginning-of-line)
  878. (org-backward-paragraph)
  879. (looking-at "L2")))
  880. (should
  881. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  882. (search-forward "L3")
  883. (beginning-of-line)
  884. (org-backward-paragraph)
  885. (looking-at "L2"))))
  886. (ert-deftest test-org/forward-element ()
  887. "Test `org-forward-element' specifications."
  888. ;; 1. At EOB: should error.
  889. (org-test-with-temp-text "Some text\n"
  890. (goto-char (point-max))
  891. (should-error (org-forward-element)))
  892. ;; 2. Standard move: expected to ignore blank lines.
  893. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  894. (org-forward-element)
  895. (should (looking-at (regexp-quote "Second paragraph."))))
  896. ;; 3. Headline tests.
  897. (org-test-with-temp-text "
  898. * Head 1
  899. ** Head 1.1
  900. *** Head 1.1.1
  901. ** Head 1.2"
  902. ;; 3.1. At an headline beginning: move to next headline at the
  903. ;; same level.
  904. (goto-line 3)
  905. (org-forward-element)
  906. (should (looking-at (regexp-quote "** Head 1.2")))
  907. ;; 3.2. At an headline beginning: move to parent headline if no
  908. ;; headline at the same level.
  909. (goto-line 3)
  910. (org-forward-element)
  911. (should (looking-at (regexp-quote "** Head 1.2"))))
  912. ;; 4. Greater element tests.
  913. (org-test-with-temp-text
  914. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  915. ;; 4.1. At a greater element: expected to skip contents.
  916. (org-forward-element)
  917. (should (looking-at (regexp-quote "Outside.")))
  918. ;; 4.2. At the end of greater element contents: expected to skip
  919. ;; to the end of the greater element.
  920. (goto-line 2)
  921. (org-forward-element)
  922. (should (looking-at (regexp-quote "Outside."))))
  923. ;; 5. List tests.
  924. (org-test-with-temp-text "
  925. - item1
  926. - sub1
  927. - sub2
  928. - sub3
  929. Inner paragraph.
  930. - item2
  931. Outside."
  932. ;; 5.1. At list top point: expected to move to the element after
  933. ;; the list.
  934. (goto-line 2)
  935. (org-forward-element)
  936. (should (looking-at (regexp-quote "Outside.")))
  937. ;; 5.2. Special case: at the first line of a sub-list, but not at
  938. ;; beginning of line, move to next item.
  939. (goto-line 2)
  940. (forward-char)
  941. (org-forward-element)
  942. (should (looking-at "- item2"))
  943. (goto-line 4)
  944. (forward-char)
  945. (org-forward-element)
  946. (should (looking-at " - sub2"))
  947. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  948. (goto-line 4)
  949. (org-forward-element)
  950. (should (looking-at (regexp-quote " Inner paragraph.")))
  951. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  952. (goto-line 8)
  953. (org-forward-element)
  954. (should (looking-at (regexp-quote " Inner paragraph.")))
  955. ;; 5.5. At an item: expected to move to next item, if any.
  956. (goto-line 6)
  957. (org-forward-element)
  958. (should (looking-at " - sub3"))))
  959. (ert-deftest test-org/backward-element ()
  960. "Test `org-backward-element' specifications."
  961. ;; 1. Should error at BOB.
  962. (org-test-with-temp-text " \nParagraph."
  963. (should-error (org-backward-element)))
  964. ;; 2. Should move at BOB when called on the first element in buffer.
  965. (should
  966. (org-test-with-temp-text "\n#+TITLE: test"
  967. (progn (forward-line)
  968. (org-backward-element)
  969. (bobp))))
  970. ;; 3. Not at the beginning of an element: move at its beginning.
  971. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  972. (goto-line 3)
  973. (end-of-line)
  974. (org-backward-element)
  975. (should (looking-at (regexp-quote "Paragraph2."))))
  976. ;; 4. Headline tests.
  977. (org-test-with-temp-text "
  978. * Head 1
  979. ** Head 1.1
  980. *** Head 1.1.1
  981. ** Head 1.2"
  982. ;; 4.1. At an headline beginning: move to previous headline at the
  983. ;; same level.
  984. (goto-line 5)
  985. (org-backward-element)
  986. (should (looking-at (regexp-quote "** Head 1.1")))
  987. ;; 4.2. At an headline beginning: move to parent headline if no
  988. ;; headline at the same level.
  989. (goto-line 3)
  990. (org-backward-element)
  991. (should (looking-at (regexp-quote "* Head 1")))
  992. ;; 4.3. At the first top-level headline: should error.
  993. (goto-line 2)
  994. (should-error (org-backward-element)))
  995. ;; 5. At beginning of first element inside a greater element:
  996. ;; expected to move to greater element's beginning.
  997. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  998. (goto-line 3)
  999. (org-backward-element)
  1000. (should (looking-at "#\\+BEGIN_CENTER")))
  1001. ;; 6. At the beginning of the first element in a section: should
  1002. ;; move back to headline, if any.
  1003. (should
  1004. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1005. (progn (goto-char (point-max))
  1006. (beginning-of-line)
  1007. (org-backward-element)
  1008. (org-at-heading-p))))
  1009. ;; 7. List tests.
  1010. (org-test-with-temp-text "
  1011. - item1
  1012. - sub1
  1013. - sub2
  1014. - sub3
  1015. Inner paragraph.
  1016. - item2
  1017. Outside."
  1018. ;; 7.1. At beginning of sub-list: expected to move to the
  1019. ;; paragraph before it.
  1020. (goto-line 4)
  1021. (org-backward-element)
  1022. (should (looking-at "item1"))
  1023. ;; 7.2. At an item in a list: expected to move at previous item.
  1024. (goto-line 8)
  1025. (org-backward-element)
  1026. (should (looking-at " - sub2"))
  1027. (goto-line 12)
  1028. (org-backward-element)
  1029. (should (looking-at "- item1"))
  1030. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1031. ;; beginning.
  1032. (goto-line 10)
  1033. (org-backward-element)
  1034. (should (looking-at " - sub1"))
  1035. (goto-line 15)
  1036. (org-backward-element)
  1037. (should (looking-at "- item1"))
  1038. ;; 7.4. At blank-lines before list end: expected to move to top
  1039. ;; item.
  1040. (goto-line 14)
  1041. (org-backward-element)
  1042. (should (looking-at "- item1"))))
  1043. (ert-deftest test-org/up-element ()
  1044. "Test `org-up-element' specifications."
  1045. ;; 1. At BOB or with no surrounding element: should error.
  1046. (org-test-with-temp-text "Paragraph."
  1047. (should-error (org-up-element)))
  1048. (org-test-with-temp-text "* Head1\n* Head2"
  1049. (goto-line 2)
  1050. (should-error (org-up-element)))
  1051. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1052. (goto-line 3)
  1053. (should-error (org-up-element)))
  1054. ;; 2. At an headline: move to parent headline.
  1055. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1056. (goto-line 3)
  1057. (org-up-element)
  1058. (should (looking-at "\\* Head1")))
  1059. ;; 3. Inside a greater element: move to greater element beginning.
  1060. (org-test-with-temp-text
  1061. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1062. (goto-line 3)
  1063. (org-up-element)
  1064. (should (looking-at "#\\+BEGIN_CENTER")))
  1065. ;; 4. List tests.
  1066. (org-test-with-temp-text "* Top
  1067. - item1
  1068. - sub1
  1069. - sub2
  1070. Paragraph within sub2.
  1071. - item2"
  1072. ;; 4.1. Within an item: move to the item beginning.
  1073. (goto-line 8)
  1074. (org-up-element)
  1075. (should (looking-at " - sub2"))
  1076. ;; 4.2. At an item in a sub-list: move to parent item.
  1077. (goto-line 4)
  1078. (org-up-element)
  1079. (should (looking-at "- item1"))
  1080. ;; 4.3. At an item in top list: move to beginning of whole list.
  1081. (goto-line 10)
  1082. (org-up-element)
  1083. (should (looking-at "- item1"))
  1084. ;; 4.4. Special case. At very top point: should move to parent of
  1085. ;; list.
  1086. (goto-line 2)
  1087. (org-up-element)
  1088. (should (looking-at "\\* Top"))))
  1089. (ert-deftest test-org/down-element ()
  1090. "Test `org-down-element' specifications."
  1091. ;; Error when the element hasn't got a recursive type.
  1092. (org-test-with-temp-text "Paragraph."
  1093. (should-error (org-down-element)))
  1094. ;; Error when the element has no contents
  1095. (org-test-with-temp-text "* Headline"
  1096. (should-error (org-down-element)))
  1097. ;; When at a plain-list, move to first item.
  1098. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1099. (goto-line 2)
  1100. (org-down-element)
  1101. (should (looking-at " - Item 1.1")))
  1102. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1103. (org-down-element)
  1104. (should (looking-at " Item 1")))
  1105. ;; When at a table, move to first row
  1106. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1107. (org-down-element)
  1108. (should (looking-at " a | b |")))
  1109. ;; Otherwise, move inside the greater element.
  1110. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1111. (org-down-element)
  1112. (should (looking-at "Paragraph"))))
  1113. (ert-deftest test-org/drag-element-backward ()
  1114. "Test `org-drag-element-backward' specifications."
  1115. ;; 1. Error when trying to move first element of buffer.
  1116. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1117. (should-error (org-drag-element-backward)))
  1118. ;; 2. Error when trying to swap nested elements.
  1119. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1120. (forward-line)
  1121. (should-error (org-drag-element-backward)))
  1122. ;; 3. Error when trying to swap an headline element and
  1123. ;; a non-headline element.
  1124. (org-test-with-temp-text "Test.\n* Head 1"
  1125. (forward-line)
  1126. (should-error (org-drag-element-backward)))
  1127. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1128. ;; between elements.
  1129. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  1130. (search-forward "graph")
  1131. (org-drag-element-backward)
  1132. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  1133. (should (looking-at " 2")))
  1134. ;; 5. Preserve visibility of elements and their contents.
  1135. (org-test-with-temp-text "
  1136. #+BEGIN_CENTER
  1137. Text.
  1138. #+END_CENTER
  1139. - item 1
  1140. #+BEGIN_QUOTE
  1141. Text.
  1142. #+END_QUOTE"
  1143. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1144. (search-backward "- item 1")
  1145. (org-drag-element-backward)
  1146. (should
  1147. (equal
  1148. '((63 . 82) (26 . 48))
  1149. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1150. (overlays-in (point-min) (point-max)))))))
  1151. (ert-deftest test-org/drag-element-forward ()
  1152. "Test `org-drag-element-forward' specifications."
  1153. ;; 1. Error when trying to move first element of buffer.
  1154. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1155. (goto-line 3)
  1156. (should-error (org-drag-element-forward)))
  1157. ;; 2. Error when trying to swap nested elements.
  1158. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1159. (forward-line)
  1160. (should-error (org-drag-element-forward)))
  1161. ;; 3. Error when trying to swap a non-headline element and an
  1162. ;; headline.
  1163. (org-test-with-temp-text "Test.\n* Head 1"
  1164. (should-error (org-drag-element-forward)))
  1165. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1166. ;; between elements.
  1167. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1168. (search-forward "graph")
  1169. (org-drag-element-forward)
  1170. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1171. (should (looking-at " 1")))
  1172. ;; 5. Preserve visibility of elements and their contents.
  1173. (org-test-with-temp-text "
  1174. #+BEGIN_CENTER
  1175. Text.
  1176. #+END_CENTER
  1177. - item 1
  1178. #+BEGIN_QUOTE
  1179. Text.
  1180. #+END_QUOTE"
  1181. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1182. (search-backward "#+BEGIN_CENTER")
  1183. (org-drag-element-forward)
  1184. (should
  1185. (equal
  1186. '((63 . 82) (26 . 48))
  1187. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1188. (overlays-in (point-min) (point-max)))))))
  1189. ;;; Planning
  1190. (ert-deftest test-org/timestamp-has-time-p ()
  1191. "Test `org-timestamp-has-time-p' specifications."
  1192. ;; With time.
  1193. (should
  1194. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1195. (org-timestamp-has-time-p (org-element-context))))
  1196. ;; Without time.
  1197. (should-not
  1198. (org-test-with-temp-text "<2012-03-29 Thu>"
  1199. (org-timestamp-has-time-p (org-element-context)))))
  1200. (ert-deftest test-org/timestamp-format ()
  1201. "Test `org-timestamp-format' specifications."
  1202. ;; Regular test.
  1203. (should
  1204. (equal
  1205. "2012-03-29 16:40"
  1206. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1207. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1208. ;; Range end.
  1209. (should
  1210. (equal
  1211. "2012-03-29"
  1212. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1213. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1214. (ert-deftest test-org/timestamp-split-range ()
  1215. "Test `org-timestamp-split-range' specifications."
  1216. ;; Extract range start (active).
  1217. (should
  1218. (equal '(2012 3 29)
  1219. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1220. (let ((ts (org-timestamp-split-range (org-element-context))))
  1221. (mapcar (lambda (p) (org-element-property p ts))
  1222. '(:year-end :month-end :day-end))))))
  1223. ;; Extract range start (inactive)
  1224. (should
  1225. (equal '(2012 3 29)
  1226. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1227. (let ((ts (org-timestamp-split-range (org-element-context))))
  1228. (mapcar (lambda (p) (org-element-property p ts))
  1229. '(:year-end :month-end :day-end))))))
  1230. ;; Extract range end (active).
  1231. (should
  1232. (equal '(2012 3 30)
  1233. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1234. (let ((ts (org-timestamp-split-range
  1235. (org-element-context) t)))
  1236. (mapcar (lambda (p) (org-element-property p ts))
  1237. '(:year-end :month-end :day-end))))))
  1238. ;; Extract range end (inactive)
  1239. (should
  1240. (equal '(2012 3 30)
  1241. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1242. (let ((ts (org-timestamp-split-range
  1243. (org-element-context) t)))
  1244. (mapcar (lambda (p) (org-element-property p ts))
  1245. '(:year-end :month-end :day-end))))))
  1246. ;; Return the timestamp if not a range.
  1247. (should
  1248. (org-test-with-temp-text "[2012-03-29 Thu]"
  1249. (let* ((ts-orig (org-element-context))
  1250. (ts-copy (org-timestamp-split-range ts-orig)))
  1251. (eq ts-orig ts-copy))))
  1252. (should
  1253. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1254. (let* ((ts-orig (org-element-context))
  1255. (ts-copy (org-timestamp-split-range ts-orig)))
  1256. (eq ts-orig ts-copy))))
  1257. ;; Check that parent is the same when a range was split.
  1258. (should
  1259. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1260. (let* ((ts-orig (org-element-context))
  1261. (ts-copy (org-timestamp-split-range ts-orig)))
  1262. (eq (org-element-property :parent ts-orig)
  1263. (org-element-property :parent ts-copy))))))
  1264. (ert-deftest test-org/timestamp-translate ()
  1265. "Test `org-timestamp-translate' specifications."
  1266. ;; Translate whole date range.
  1267. (should
  1268. (equal "<29>--<30>"
  1269. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1270. (let ((org-display-custom-times t)
  1271. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1272. (org-timestamp-translate (org-element-context))))))
  1273. ;; Translate date range start.
  1274. (should
  1275. (equal "<29>"
  1276. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1277. (let ((org-display-custom-times t)
  1278. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1279. (org-timestamp-translate (org-element-context) 'start)))))
  1280. ;; Translate date range end.
  1281. (should
  1282. (equal "<30>"
  1283. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1284. (let ((org-display-custom-times t)
  1285. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1286. (org-timestamp-translate (org-element-context) 'end)))))
  1287. ;; Translate time range.
  1288. (should
  1289. (equal "<08>--<16>"
  1290. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1291. (let ((org-display-custom-times t)
  1292. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1293. (org-timestamp-translate (org-element-context))))))
  1294. ;; Translate non-range timestamp.
  1295. (should
  1296. (equal "<29>"
  1297. (org-test-with-temp-text "<2012-03-29 Thu>"
  1298. (let ((org-display-custom-times t)
  1299. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1300. (org-timestamp-translate (org-element-context))))))
  1301. ;; Do not change `diary' timestamps.
  1302. (should
  1303. (equal "<%%(org-float t 4 2)>"
  1304. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1305. (let ((org-display-custom-times t)
  1306. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1307. (org-timestamp-translate (org-element-context)))))))
  1308. ;;; Targets and Radio Targets
  1309. (ert-deftest test-org/all-targets ()
  1310. "Test `org-all-targets' specifications."
  1311. ;; Without an argument.
  1312. (should
  1313. (equal '("radio-target" "target")
  1314. (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
  1315. (org-all-targets))))
  1316. (should
  1317. (equal '("radio-target")
  1318. (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
  1319. ;; With argument.
  1320. (should
  1321. (equal '("radio-target")
  1322. (org-test-with-temp-text "<<target>> <<<radio-target>>>"
  1323. (org-all-targets t)))))
  1324. ;;; Visibility
  1325. (ert-deftest test-org/flag-drawer ()
  1326. "Test `org-flag-drawer' specifications."
  1327. ;; Hide drawer.
  1328. (should
  1329. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1330. (org-flag-drawer t)
  1331. (get-char-property (line-end-position) 'invisible)))
  1332. ;; Show drawer.
  1333. (should-not
  1334. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1335. (org-flag-drawer t)
  1336. (org-flag-drawer nil)
  1337. (get-char-property (line-end-position) 'invisible)))
  1338. ;; Test optional argument.
  1339. (should
  1340. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1341. (let ((drawer (save-excursion (search-forward ":D2")
  1342. (org-element-at-point))))
  1343. (org-flag-drawer t drawer)
  1344. (get-char-property (progn (search-forward ":D2") (line-end-position))
  1345. 'invisible))))
  1346. (should-not
  1347. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1348. (let ((drawer (save-excursion (search-forward ":D2")
  1349. (org-element-at-point))))
  1350. (org-flag-drawer t drawer)
  1351. (get-char-property (line-end-position) 'invisible))))
  1352. ;; Do not hide fake drawers.
  1353. (should-not
  1354. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  1355. (forward-line 1)
  1356. (org-flag-drawer t)
  1357. (get-char-property (line-end-position) 'invisible)))
  1358. ;; Do not hide incomplete drawers.
  1359. (should-not
  1360. (org-test-with-temp-text ":D:\nparagraph"
  1361. (forward-line 1)
  1362. (org-flag-drawer t)
  1363. (get-char-property (line-end-position) 'invisible))))
  1364. (provide 'test-org)
  1365. ;;; test-org.el ends here