test-org.el 49 KB

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