test-org.el 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  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. ;;;; Coderefs
  516. (ert-deftest test-org/coderef ()
  517. "Test coderef links specifications."
  518. (should
  519. (org-test-with-temp-text "
  520. #+BEGIN_SRC emacs-lisp
  521. \(+ 1 1) (ref:sc)
  522. #+END_SRC
  523. \[[(sc)]]"
  524. (goto-char (point-max))
  525. (org-open-at-point)
  526. (looking-at "(ref:sc)"))))
  527. ;;;; Custom ID
  528. (ert-deftest test-org/custom-id ()
  529. "Test custom ID links specifications."
  530. (should
  531. (org-test-with-temp-text
  532. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  533. (goto-char (point-max))
  534. (org-open-at-point)
  535. (org-looking-at-p "\\* H1"))))
  536. ;;;; Fuzzy Links
  537. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  538. ;; a named element (#+name: text) and to headlines (* Text).
  539. (ert-deftest test-org/fuzzy-links ()
  540. "Test fuzzy links specifications."
  541. ;; Fuzzy link goes in priority to a matching target.
  542. (should
  543. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  544. (goto-line 5)
  545. (org-open-at-point)
  546. (looking-at "<<Test>>")))
  547. ;; Then fuzzy link points to an element with a given name.
  548. (should
  549. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  550. (goto-line 5)
  551. (org-open-at-point)
  552. (looking-at "#\\+NAME: Test")))
  553. ;; A target still lead to a matching headline otherwise.
  554. (should
  555. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  556. (goto-line 4)
  557. (org-open-at-point)
  558. (looking-at "\\* Head2")))
  559. ;; With a leading star in link, enforce heading match.
  560. (should
  561. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  562. (goto-line 3)
  563. (org-open-at-point)
  564. (looking-at "\\* Test")))
  565. ;; Correctly un-hexify fuzzy links.
  566. (should
  567. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  568. (goto-char (point-max))
  569. (org-open-at-point)
  570. (bobp))))
  571. ;;;; Link Escaping
  572. (ert-deftest test-org/org-link-escape-ascii-character ()
  573. "Escape an ascii character."
  574. (should
  575. (string=
  576. "%5B"
  577. (org-link-escape "["))))
  578. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  579. "Escape an ascii control character."
  580. (should
  581. (string=
  582. "%09"
  583. (org-link-escape "\t"))))
  584. (ert-deftest test-org/org-link-escape-multibyte-character ()
  585. "Escape an unicode multibyte character."
  586. (should
  587. (string=
  588. "%E2%82%AC"
  589. (org-link-escape "€"))))
  590. (ert-deftest test-org/org-link-escape-custom-table ()
  591. "Escape string with custom character table."
  592. (should
  593. (string=
  594. "Foo%3A%42ar%0A"
  595. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  596. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  597. "Escape string with custom table merged with default table."
  598. (should
  599. (string=
  600. "%5BF%6F%6F%3A%42ar%0A%5D"
  601. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  602. (ert-deftest test-org/org-link-unescape-ascii-character ()
  603. "Unescape an ascii character."
  604. (should
  605. (string=
  606. "["
  607. (org-link-unescape "%5B"))))
  608. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  609. "Unescpae an ascii control character."
  610. (should
  611. (string=
  612. "\n"
  613. (org-link-unescape "%0A"))))
  614. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  615. "Unescape unicode multibyte character."
  616. (should
  617. (string=
  618. "€"
  619. (org-link-unescape "%E2%82%AC"))))
  620. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  621. "Unescape old style percent escaped character."
  622. (should
  623. (string=
  624. "àâçèéêîôùû"
  625. (decode-coding-string
  626. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  627. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  628. "Escape and unescape a URL that includes an escaped char.
  629. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  630. (should
  631. (string=
  632. "http://some.host.com/form?&id=blah%2Bblah25"
  633. (org-link-unescape
  634. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  635. (ert-deftest test-org/org-link-escape-chars-browser ()
  636. "Test of the constant `org-link-escape-chars-browser'.
  637. See there why this test is a candidate to be removed once Org
  638. drops support for Emacs 24.1 and 24.2."
  639. (should
  640. (string=
  641. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  642. "%22Release%208.2%22&idxname=emacs-orgmode")
  643. (org-link-escape-browser ; Do not replace with `url-encode-url',
  644. ; see docstring above.
  645. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  646. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  647. ;;; Node Properties
  648. (ert-deftest test-org/accumulated-properties-in-drawers ()
  649. "Ensure properties accumulate in subtree drawers."
  650. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  651. (org-babel-next-src-block)
  652. (should (equal '(2 1) (org-babel-execute-src-block)))))
  653. ;;; Mark Region
  654. (ert-deftest test-org/mark-subtree ()
  655. "Test `org-mark-subtree' specifications."
  656. ;; Error when point is before first headline.
  657. (should-error
  658. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  659. (progn (transient-mark-mode 1)
  660. (org-mark-subtree))))
  661. ;; Without argument, mark current subtree.
  662. (should
  663. (equal
  664. '(12 32)
  665. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  666. (progn (transient-mark-mode 1)
  667. (forward-line 2)
  668. (org-mark-subtree)
  669. (list (region-beginning) (region-end))))))
  670. ;; With an argument, move ARG up.
  671. (should
  672. (equal
  673. '(1 32)
  674. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  675. (progn (transient-mark-mode 1)
  676. (forward-line 2)
  677. (org-mark-subtree 1)
  678. (list (region-beginning) (region-end))))))
  679. ;; Do not get fooled by inlinetasks.
  680. (when (featurep 'org-inlinetask)
  681. (should
  682. (= 1
  683. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  684. (progn (transient-mark-mode 1)
  685. (forward-line 1)
  686. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  687. (region-beginning)))))))
  688. ;;; Navigation
  689. (ert-deftest test-org/beginning-of-line ()
  690. "Test `org-beginning-of-line' specifications."
  691. ;; Standard test.
  692. (should
  693. (org-test-with-temp-text "Some text\nSome other text"
  694. (progn (org-beginning-of-line) (bolp))))
  695. ;; Standard test with `visual-line-mode'.
  696. (should-not
  697. (org-test-with-temp-text "A long line of text\nSome other text"
  698. (progn (visual-line-mode)
  699. (forward-char 2)
  700. (dotimes (i 1000) (insert "very "))
  701. (org-beginning-of-line)
  702. (bolp))))
  703. ;; At an headline with special movement.
  704. (should
  705. (org-test-with-temp-text "* TODO Headline"
  706. (let ((org-special-ctrl-a/e t))
  707. (org-end-of-line)
  708. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  709. (progn (org-beginning-of-line) (bolp))
  710. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  711. (ert-deftest test-org/end-of-line ()
  712. "Test `org-end-of-line' specifications."
  713. ;; Standard test.
  714. (should
  715. (org-test-with-temp-text "Some text\nSome other text"
  716. (progn (org-end-of-line) (eolp))))
  717. ;; Standard test with `visual-line-mode'.
  718. (should-not
  719. (org-test-with-temp-text "A long line of text\nSome other text"
  720. (progn (visual-line-mode)
  721. (forward-char 2)
  722. (dotimes (i 1000) (insert "very "))
  723. (goto-char (point-min))
  724. (org-end-of-line)
  725. (eolp))))
  726. ;; At an headline with special movement.
  727. (should
  728. (org-test-with-temp-text "* Headline1 :tag:\n"
  729. (let ((org-special-ctrl-a/e t))
  730. (and (progn (org-end-of-line) (looking-at " :tag:"))
  731. (progn (org-end-of-line) (eolp))
  732. (progn (org-end-of-line) (looking-at " :tag:"))))))
  733. ;; At an headline without special movement.
  734. (should
  735. (org-test-with-temp-text "* Headline2 :tag:\n"
  736. (let ((org-special-ctrl-a/e nil))
  737. (and (progn (org-end-of-line) (eolp))
  738. (progn (org-end-of-line) (eolp))))))
  739. ;; At an headline, with reversed movement.
  740. (should
  741. (org-test-with-temp-text "* Headline3 :tag:\n"
  742. (let ((org-special-ctrl-a/e 'reversed)
  743. (this-command last-command))
  744. (and (progn (org-end-of-line) (eolp))
  745. (progn (org-end-of-line) (looking-at " :tag:"))))))
  746. ;; At a block without hidden contents.
  747. (should
  748. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  749. (progn (org-end-of-line) (eolp))))
  750. ;; At a block with hidden contents.
  751. (should-not
  752. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  753. (let ((org-special-ctrl-a/e t))
  754. (org-hide-block-toggle)
  755. (org-end-of-line)
  756. (eobp)))))
  757. (ert-deftest test-org/forward-paragraph ()
  758. "Test `org-forward-paragraph' specifications."
  759. ;; At end of buffer, return an error.
  760. (should-error
  761. (org-test-with-temp-text "Paragraph"
  762. (goto-char (point-max))
  763. (org-forward-paragraph)))
  764. ;; Standard test.
  765. (should
  766. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  767. (org-forward-paragraph)
  768. (looking-at "P2")))
  769. ;; Ignore depth.
  770. (should
  771. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  772. (org-forward-paragraph)
  773. (looking-at "P1")))
  774. ;; Do not enter elements with invisible contents.
  775. (should
  776. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  777. (org-hide-block-toggle)
  778. (org-forward-paragraph)
  779. (looking-at "P3")))
  780. ;; On an affiliated keyword, jump to the beginning of the element.
  781. (should
  782. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  783. (org-forward-paragraph)
  784. (looking-at "Para")))
  785. ;; On an item or a footnote definition, move to the second element
  786. ;; inside, if any.
  787. (should
  788. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  789. (org-forward-paragraph)
  790. (looking-at " Paragraph")))
  791. (should
  792. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  793. (org-forward-paragraph)
  794. (looking-at "Paragraph")))
  795. ;; On an item, or a footnote definition, when the first line is
  796. ;; empty, move to the first item.
  797. (should
  798. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  799. (org-forward-paragraph)
  800. (looking-at " Paragraph")))
  801. (should
  802. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  803. (org-forward-paragraph)
  804. (looking-at "Paragraph")))
  805. ;; On a table (resp. a property drawer) do not move through table
  806. ;; rows (resp. node properties).
  807. (should
  808. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  809. (org-forward-paragraph)
  810. (looking-at "Paragraph")))
  811. (should
  812. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
  813. (org-forward-paragraph)
  814. (looking-at "Paragraph")))
  815. ;; On a verse or source block, stop after blank lines.
  816. (should
  817. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  818. (org-forward-paragraph)
  819. (looking-at "L2")))
  820. (should
  821. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  822. (org-forward-paragraph)
  823. (looking-at "L2"))))
  824. (ert-deftest test-org/backward-paragraph ()
  825. "Test `org-backward-paragraph' specifications."
  826. ;; Error at beginning of buffer.
  827. (should-error
  828. (org-test-with-temp-text "Paragraph"
  829. (org-backward-paragraph)))
  830. ;; Regular test.
  831. (should
  832. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  833. (goto-char (point-max))
  834. (org-backward-paragraph)
  835. (looking-at "P3")))
  836. (should
  837. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  838. (goto-char (point-max))
  839. (beginning-of-line)
  840. (org-backward-paragraph)
  841. (looking-at "P2")))
  842. ;; Ignore depth.
  843. (should
  844. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  845. (goto-char (point-max))
  846. (beginning-of-line)
  847. (org-backward-paragraph)
  848. (looking-at "P2")))
  849. ;; Ignore invisible elements.
  850. (should
  851. (org-test-with-temp-text "* H1\n P1\n* H2"
  852. (org-cycle)
  853. (goto-char (point-max))
  854. (beginning-of-line)
  855. (org-backward-paragraph)
  856. (bobp)))
  857. ;; On an affiliated keyword, jump to the first one.
  858. (should
  859. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  860. (search-forward "c2")
  861. (org-backward-paragraph)
  862. (looking-at "#\\+name")))
  863. ;; On the second element in an item or a footnote definition, jump
  864. ;; to item or the definition.
  865. (should
  866. (org-test-with-temp-text "- line1\n\n line2"
  867. (goto-char (point-max))
  868. (beginning-of-line)
  869. (org-backward-paragraph)
  870. (looking-at "- line1")))
  871. (should
  872. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  873. (goto-char (point-max))
  874. (beginning-of-line)
  875. (org-backward-paragraph)
  876. (looking-at "\\[fn:1\\] line1")))
  877. ;; On a table (resp. a property drawer), ignore table rows
  878. ;; (resp. node properties).
  879. (should
  880. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  881. (goto-char (point-max))
  882. (beginning-of-line)
  883. (org-backward-paragraph)
  884. (bobp)))
  885. (should
  886. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
  887. (goto-char (point-max))
  888. (beginning-of-line)
  889. (org-backward-paragraph)
  890. (bobp)))
  891. ;; On a source or verse block, stop before blank lines.
  892. (should
  893. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  894. (search-forward "L3")
  895. (beginning-of-line)
  896. (org-backward-paragraph)
  897. (looking-at "L2")))
  898. (should
  899. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  900. (search-forward "L3")
  901. (beginning-of-line)
  902. (org-backward-paragraph)
  903. (looking-at "L2"))))
  904. (ert-deftest test-org/forward-element ()
  905. "Test `org-forward-element' specifications."
  906. ;; 1. At EOB: should error.
  907. (org-test-with-temp-text "Some text\n"
  908. (goto-char (point-max))
  909. (should-error (org-forward-element)))
  910. ;; 2. Standard move: expected to ignore blank lines.
  911. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  912. (org-forward-element)
  913. (should (looking-at (regexp-quote "Second paragraph."))))
  914. ;; 3. Headline tests.
  915. (org-test-with-temp-text "
  916. * Head 1
  917. ** Head 1.1
  918. *** Head 1.1.1
  919. ** Head 1.2"
  920. ;; 3.1. At an headline beginning: move to next headline at the
  921. ;; same level.
  922. (goto-line 3)
  923. (org-forward-element)
  924. (should (looking-at (regexp-quote "** Head 1.2")))
  925. ;; 3.2. At an headline beginning: move to parent headline if no
  926. ;; headline at the same level.
  927. (goto-line 3)
  928. (org-forward-element)
  929. (should (looking-at (regexp-quote "** Head 1.2"))))
  930. ;; 4. Greater element tests.
  931. (org-test-with-temp-text
  932. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  933. ;; 4.1. At a greater element: expected to skip contents.
  934. (org-forward-element)
  935. (should (looking-at (regexp-quote "Outside.")))
  936. ;; 4.2. At the end of greater element contents: expected to skip
  937. ;; to the end of the greater element.
  938. (goto-line 2)
  939. (org-forward-element)
  940. (should (looking-at (regexp-quote "Outside."))))
  941. ;; 5. List tests.
  942. (org-test-with-temp-text "
  943. - item1
  944. - sub1
  945. - sub2
  946. - sub3
  947. Inner paragraph.
  948. - item2
  949. Outside."
  950. ;; 5.1. At list top point: expected to move to the element after
  951. ;; the list.
  952. (goto-line 2)
  953. (org-forward-element)
  954. (should (looking-at (regexp-quote "Outside.")))
  955. ;; 5.2. Special case: at the first line of a sub-list, but not at
  956. ;; beginning of line, move to next item.
  957. (goto-line 2)
  958. (forward-char)
  959. (org-forward-element)
  960. (should (looking-at "- item2"))
  961. (goto-line 4)
  962. (forward-char)
  963. (org-forward-element)
  964. (should (looking-at " - sub2"))
  965. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  966. (goto-line 4)
  967. (org-forward-element)
  968. (should (looking-at (regexp-quote " Inner paragraph.")))
  969. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  970. (goto-line 8)
  971. (org-forward-element)
  972. (should (looking-at (regexp-quote " Inner paragraph.")))
  973. ;; 5.5. At an item: expected to move to next item, if any.
  974. (goto-line 6)
  975. (org-forward-element)
  976. (should (looking-at " - sub3"))))
  977. (ert-deftest test-org/backward-element ()
  978. "Test `org-backward-element' specifications."
  979. ;; 1. Should error at BOB.
  980. (org-test-with-temp-text " \nParagraph."
  981. (should-error (org-backward-element)))
  982. ;; 2. Should move at BOB when called on the first element in buffer.
  983. (should
  984. (org-test-with-temp-text "\n#+TITLE: test"
  985. (progn (forward-line)
  986. (org-backward-element)
  987. (bobp))))
  988. ;; 3. Not at the beginning of an element: move at its beginning.
  989. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  990. (goto-line 3)
  991. (end-of-line)
  992. (org-backward-element)
  993. (should (looking-at (regexp-quote "Paragraph2."))))
  994. ;; 4. Headline tests.
  995. (org-test-with-temp-text "
  996. * Head 1
  997. ** Head 1.1
  998. *** Head 1.1.1
  999. ** Head 1.2"
  1000. ;; 4.1. At an headline beginning: move to previous headline at the
  1001. ;; same level.
  1002. (goto-line 5)
  1003. (org-backward-element)
  1004. (should (looking-at (regexp-quote "** Head 1.1")))
  1005. ;; 4.2. At an headline beginning: move to parent headline if no
  1006. ;; headline at the same level.
  1007. (goto-line 3)
  1008. (org-backward-element)
  1009. (should (looking-at (regexp-quote "* Head 1")))
  1010. ;; 4.3. At the first top-level headline: should error.
  1011. (goto-line 2)
  1012. (should-error (org-backward-element)))
  1013. ;; 5. At beginning of first element inside a greater element:
  1014. ;; expected to move to greater element's beginning.
  1015. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1016. (goto-line 3)
  1017. (org-backward-element)
  1018. (should (looking-at "#\\+BEGIN_CENTER")))
  1019. ;; 6. At the beginning of the first element in a section: should
  1020. ;; move back to headline, if any.
  1021. (should
  1022. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1023. (progn (goto-char (point-max))
  1024. (beginning-of-line)
  1025. (org-backward-element)
  1026. (org-at-heading-p))))
  1027. ;; 7. List tests.
  1028. (org-test-with-temp-text "
  1029. - item1
  1030. - sub1
  1031. - sub2
  1032. - sub3
  1033. Inner paragraph.
  1034. - item2
  1035. Outside."
  1036. ;; 7.1. At beginning of sub-list: expected to move to the
  1037. ;; paragraph before it.
  1038. (goto-line 4)
  1039. (org-backward-element)
  1040. (should (looking-at "item1"))
  1041. ;; 7.2. At an item in a list: expected to move at previous item.
  1042. (goto-line 8)
  1043. (org-backward-element)
  1044. (should (looking-at " - sub2"))
  1045. (goto-line 12)
  1046. (org-backward-element)
  1047. (should (looking-at "- item1"))
  1048. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1049. ;; beginning.
  1050. (goto-line 10)
  1051. (org-backward-element)
  1052. (should (looking-at " - sub1"))
  1053. (goto-line 15)
  1054. (org-backward-element)
  1055. (should (looking-at "- item1"))
  1056. ;; 7.4. At blank-lines before list end: expected to move to top
  1057. ;; item.
  1058. (goto-line 14)
  1059. (org-backward-element)
  1060. (should (looking-at "- item1"))))
  1061. (ert-deftest test-org/up-element ()
  1062. "Test `org-up-element' specifications."
  1063. ;; 1. At BOB or with no surrounding element: should error.
  1064. (org-test-with-temp-text "Paragraph."
  1065. (should-error (org-up-element)))
  1066. (org-test-with-temp-text "* Head1\n* Head2"
  1067. (goto-line 2)
  1068. (should-error (org-up-element)))
  1069. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1070. (goto-line 3)
  1071. (should-error (org-up-element)))
  1072. ;; 2. At an headline: move to parent headline.
  1073. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1074. (goto-line 3)
  1075. (org-up-element)
  1076. (should (looking-at "\\* Head1")))
  1077. ;; 3. Inside a greater element: move to greater element beginning.
  1078. (org-test-with-temp-text
  1079. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1080. (goto-line 3)
  1081. (org-up-element)
  1082. (should (looking-at "#\\+BEGIN_CENTER")))
  1083. ;; 4. List tests.
  1084. (org-test-with-temp-text "* Top
  1085. - item1
  1086. - sub1
  1087. - sub2
  1088. Paragraph within sub2.
  1089. - item2"
  1090. ;; 4.1. Within an item: move to the item beginning.
  1091. (goto-line 8)
  1092. (org-up-element)
  1093. (should (looking-at " - sub2"))
  1094. ;; 4.2. At an item in a sub-list: move to parent item.
  1095. (goto-line 4)
  1096. (org-up-element)
  1097. (should (looking-at "- item1"))
  1098. ;; 4.3. At an item in top list: move to beginning of whole list.
  1099. (goto-line 10)
  1100. (org-up-element)
  1101. (should (looking-at "- item1"))
  1102. ;; 4.4. Special case. At very top point: should move to parent of
  1103. ;; list.
  1104. (goto-line 2)
  1105. (org-up-element)
  1106. (should (looking-at "\\* Top"))))
  1107. (ert-deftest test-org/down-element ()
  1108. "Test `org-down-element' specifications."
  1109. ;; Error when the element hasn't got a recursive type.
  1110. (org-test-with-temp-text "Paragraph."
  1111. (should-error (org-down-element)))
  1112. ;; Error when the element has no contents
  1113. (org-test-with-temp-text "* Headline"
  1114. (should-error (org-down-element)))
  1115. ;; When at a plain-list, move to first item.
  1116. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1117. (goto-line 2)
  1118. (org-down-element)
  1119. (should (looking-at " - Item 1.1")))
  1120. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1121. (org-down-element)
  1122. (should (looking-at " Item 1")))
  1123. ;; When at a table, move to first row
  1124. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1125. (org-down-element)
  1126. (should (looking-at " a | b |")))
  1127. ;; Otherwise, move inside the greater element.
  1128. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1129. (org-down-element)
  1130. (should (looking-at "Paragraph"))))
  1131. (ert-deftest test-org/drag-element-backward ()
  1132. "Test `org-drag-element-backward' specifications."
  1133. ;; 1. Error when trying to move first element of buffer.
  1134. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1135. (should-error (org-drag-element-backward)))
  1136. ;; 2. Error when trying to swap nested elements.
  1137. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1138. (forward-line)
  1139. (should-error (org-drag-element-backward)))
  1140. ;; 3. Error when trying to swap an headline element and
  1141. ;; a non-headline element.
  1142. (org-test-with-temp-text "Test.\n* Head 1"
  1143. (forward-line)
  1144. (should-error (org-drag-element-backward)))
  1145. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1146. ;; between elements.
  1147. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  1148. (search-forward "graph")
  1149. (org-drag-element-backward)
  1150. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  1151. (should (looking-at " 2")))
  1152. ;; 5. Preserve visibility of elements and their contents.
  1153. (org-test-with-temp-text "
  1154. #+BEGIN_CENTER
  1155. Text.
  1156. #+END_CENTER
  1157. - item 1
  1158. #+BEGIN_QUOTE
  1159. Text.
  1160. #+END_QUOTE"
  1161. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1162. (search-backward "- item 1")
  1163. (org-drag-element-backward)
  1164. (should
  1165. (equal
  1166. '((63 . 82) (26 . 48))
  1167. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1168. (overlays-in (point-min) (point-max)))))))
  1169. (ert-deftest test-org/drag-element-forward ()
  1170. "Test `org-drag-element-forward' specifications."
  1171. ;; 1. Error when trying to move first element of buffer.
  1172. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1173. (goto-line 3)
  1174. (should-error (org-drag-element-forward)))
  1175. ;; 2. Error when trying to swap nested elements.
  1176. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1177. (forward-line)
  1178. (should-error (org-drag-element-forward)))
  1179. ;; 3. Error when trying to swap a non-headline element and an
  1180. ;; headline.
  1181. (org-test-with-temp-text "Test.\n* Head 1"
  1182. (should-error (org-drag-element-forward)))
  1183. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1184. ;; between elements.
  1185. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1186. (search-forward "graph")
  1187. (org-drag-element-forward)
  1188. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1189. (should (looking-at " 1")))
  1190. ;; 5. Preserve visibility of elements and their contents.
  1191. (org-test-with-temp-text "
  1192. #+BEGIN_CENTER
  1193. Text.
  1194. #+END_CENTER
  1195. - item 1
  1196. #+BEGIN_QUOTE
  1197. Text.
  1198. #+END_QUOTE"
  1199. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1200. (search-backward "#+BEGIN_CENTER")
  1201. (org-drag-element-forward)
  1202. (should
  1203. (equal
  1204. '((63 . 82) (26 . 48))
  1205. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1206. (overlays-in (point-min) (point-max)))))))
  1207. ;;; Planning
  1208. (ert-deftest test-org/timestamp-has-time-p ()
  1209. "Test `org-timestamp-has-time-p' specifications."
  1210. ;; With time.
  1211. (should
  1212. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1213. (org-timestamp-has-time-p (org-element-context))))
  1214. ;; Without time.
  1215. (should-not
  1216. (org-test-with-temp-text "<2012-03-29 Thu>"
  1217. (org-timestamp-has-time-p (org-element-context)))))
  1218. (ert-deftest test-org/timestamp-format ()
  1219. "Test `org-timestamp-format' specifications."
  1220. ;; Regular test.
  1221. (should
  1222. (equal
  1223. "2012-03-29 16:40"
  1224. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1225. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1226. ;; Range end.
  1227. (should
  1228. (equal
  1229. "2012-03-29"
  1230. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1231. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1232. (ert-deftest test-org/timestamp-split-range ()
  1233. "Test `org-timestamp-split-range' specifications."
  1234. ;; Extract range start (active).
  1235. (should
  1236. (equal '(2012 3 29)
  1237. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1238. (let ((ts (org-timestamp-split-range (org-element-context))))
  1239. (mapcar (lambda (p) (org-element-property p ts))
  1240. '(:year-end :month-end :day-end))))))
  1241. ;; Extract range start (inactive)
  1242. (should
  1243. (equal '(2012 3 29)
  1244. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1245. (let ((ts (org-timestamp-split-range (org-element-context))))
  1246. (mapcar (lambda (p) (org-element-property p ts))
  1247. '(:year-end :month-end :day-end))))))
  1248. ;; Extract range end (active).
  1249. (should
  1250. (equal '(2012 3 30)
  1251. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1252. (let ((ts (org-timestamp-split-range
  1253. (org-element-context) t)))
  1254. (mapcar (lambda (p) (org-element-property p ts))
  1255. '(:year-end :month-end :day-end))))))
  1256. ;; Extract range end (inactive)
  1257. (should
  1258. (equal '(2012 3 30)
  1259. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1260. (let ((ts (org-timestamp-split-range
  1261. (org-element-context) t)))
  1262. (mapcar (lambda (p) (org-element-property p ts))
  1263. '(:year-end :month-end :day-end))))))
  1264. ;; Return the timestamp if not a range.
  1265. (should
  1266. (org-test-with-temp-text "[2012-03-29 Thu]"
  1267. (let* ((ts-orig (org-element-context))
  1268. (ts-copy (org-timestamp-split-range ts-orig)))
  1269. (eq ts-orig ts-copy))))
  1270. (should
  1271. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1272. (let* ((ts-orig (org-element-context))
  1273. (ts-copy (org-timestamp-split-range ts-orig)))
  1274. (eq ts-orig ts-copy))))
  1275. ;; Check that parent is the same when a range was split.
  1276. (should
  1277. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1278. (let* ((ts-orig (org-element-context))
  1279. (ts-copy (org-timestamp-split-range ts-orig)))
  1280. (eq (org-element-property :parent ts-orig)
  1281. (org-element-property :parent ts-copy))))))
  1282. (ert-deftest test-org/timestamp-translate ()
  1283. "Test `org-timestamp-translate' specifications."
  1284. ;; Translate whole date range.
  1285. (should
  1286. (equal "<29>--<30>"
  1287. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1288. (let ((org-display-custom-times t)
  1289. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1290. (org-timestamp-translate (org-element-context))))))
  1291. ;; Translate date range start.
  1292. (should
  1293. (equal "<29>"
  1294. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1295. (let ((org-display-custom-times t)
  1296. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1297. (org-timestamp-translate (org-element-context) 'start)))))
  1298. ;; Translate date range end.
  1299. (should
  1300. (equal "<30>"
  1301. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1302. (let ((org-display-custom-times t)
  1303. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1304. (org-timestamp-translate (org-element-context) 'end)))))
  1305. ;; Translate time range.
  1306. (should
  1307. (equal "<08>--<16>"
  1308. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1309. (let ((org-display-custom-times t)
  1310. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1311. (org-timestamp-translate (org-element-context))))))
  1312. ;; Translate non-range timestamp.
  1313. (should
  1314. (equal "<29>"
  1315. (org-test-with-temp-text "<2012-03-29 Thu>"
  1316. (let ((org-display-custom-times t)
  1317. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1318. (org-timestamp-translate (org-element-context))))))
  1319. ;; Do not change `diary' timestamps.
  1320. (should
  1321. (equal "<%%(org-float t 4 2)>"
  1322. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1323. (let ((org-display-custom-times t)
  1324. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1325. (org-timestamp-translate (org-element-context)))))))
  1326. ;;; Targets and Radio Targets
  1327. (ert-deftest test-org/all-targets ()
  1328. "Test `org-all-targets' specifications."
  1329. ;; Without an argument.
  1330. (should
  1331. (equal '("radio-target" "target")
  1332. (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
  1333. (org-all-targets))))
  1334. (should
  1335. (equal '("radio-target")
  1336. (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
  1337. ;; With argument.
  1338. (should
  1339. (equal '("radio-target")
  1340. (org-test-with-temp-text "<<target>> <<<radio-target>>>"
  1341. (org-all-targets t)))))
  1342. ;;; Visibility
  1343. (ert-deftest test-org/flag-drawer ()
  1344. "Test `org-flag-drawer' specifications."
  1345. ;; Hide drawer.
  1346. (should
  1347. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1348. (org-flag-drawer t)
  1349. (get-char-property (line-end-position) 'invisible)))
  1350. ;; Show drawer.
  1351. (should-not
  1352. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1353. (org-flag-drawer t)
  1354. (org-flag-drawer nil)
  1355. (get-char-property (line-end-position) 'invisible)))
  1356. ;; Test optional argument.
  1357. (should
  1358. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1359. (let ((drawer (save-excursion (search-forward ":D2")
  1360. (org-element-at-point))))
  1361. (org-flag-drawer t drawer)
  1362. (get-char-property (progn (search-forward ":D2") (line-end-position))
  1363. 'invisible))))
  1364. (should-not
  1365. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1366. (let ((drawer (save-excursion (search-forward ":D2")
  1367. (org-element-at-point))))
  1368. (org-flag-drawer t drawer)
  1369. (get-char-property (line-end-position) 'invisible))))
  1370. ;; Do not hide fake drawers.
  1371. (should-not
  1372. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  1373. (forward-line 1)
  1374. (org-flag-drawer t)
  1375. (get-char-property (line-end-position) 'invisible)))
  1376. ;; Do not hide incomplete drawers.
  1377. (should-not
  1378. (org-test-with-temp-text ":D:\nparagraph"
  1379. (forward-line 1)
  1380. (org-flag-drawer t)
  1381. (get-char-property (line-end-position) 'invisible))))
  1382. (provide 'test-org)
  1383. ;;; test-org.el ends here