test-org.el 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  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. ;;; Indentation
  421. (ert-deftest test-org/indent-line ()
  422. "Test `org-indent-line' specifications."
  423. ;; Do not indent footnote definitions or headlines.
  424. (should
  425. (zerop
  426. (org-test-with-temp-text "* H"
  427. (org-indent-line)
  428. (org-get-indentation))))
  429. (should
  430. (zerop
  431. (org-test-with-temp-text "[fn:1] fn"
  432. (let ((org-adapt-indentation t)) (org-indent-line))
  433. (org-get-indentation))))
  434. ;; Do not indent before first headline.
  435. (should
  436. (zerop
  437. (org-test-with-temp-text ""
  438. (org-indent-line)
  439. (org-get-indentation))))
  440. ;; Indent according to headline level otherwise, unless
  441. ;; `org-adapt-indentation' is nil.
  442. (should
  443. (= 2
  444. (org-test-with-temp-text "* H\nA"
  445. (forward-line)
  446. (let ((org-adapt-indentation t)) (org-indent-line))
  447. (org-get-indentation))))
  448. (should
  449. (= 2
  450. (org-test-with-temp-text "* H\n\nA"
  451. (forward-line)
  452. (let ((org-adapt-indentation t)) (org-indent-line))
  453. (org-get-indentation))))
  454. (should
  455. (zerop
  456. (org-test-with-temp-text "* H\nA"
  457. (forward-line)
  458. (let ((org-adapt-indentation nil)) (org-indent-line))
  459. (org-get-indentation))))
  460. ;; Indenting preserves point position.
  461. (should
  462. (org-test-with-temp-text "* H\nAB"
  463. (forward-line)
  464. (forward-char)
  465. (let ((org-adapt-indentation t)) (org-indent-line))
  466. (looking-at "B")))
  467. ;; Do not change indentation at an item.
  468. (should
  469. (= 1
  470. (org-test-with-temp-text "* H\n - A"
  471. (forward-line)
  472. (let ((org-adapt-indentation t)) (org-indent-line))
  473. (org-get-indentation))))
  474. ;; On blank lines at the end of a list, indent like last element
  475. ;; within it if the line is still in the list. Otherwise, indent
  476. ;; like the whole list.
  477. (should
  478. (= 4
  479. (org-test-with-temp-text "* H\n- A\n - AA\n"
  480. (goto-char (point-max))
  481. (let ((org-adapt-indentation t)) (org-indent-line))
  482. (org-get-indentation))))
  483. (should
  484. (zerop
  485. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
  486. (goto-char (point-max))
  487. (let ((org-adapt-indentation t)) (org-indent-line))
  488. (org-get-indentation))))
  489. ;; Likewise, on a blank line at the end of a footnote definition,
  490. ;; indent at column 0 if line belongs to the definition. Otherwise,
  491. ;; indent like the definition itself.
  492. (should
  493. (zerop
  494. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  495. (goto-char (point-max))
  496. (let ((org-adapt-indentation t)) (org-indent-line))
  497. (org-get-indentation))))
  498. (should
  499. (zerop
  500. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  501. (goto-char (point-max))
  502. (let ((org-adapt-indentation t)) (org-indent-line))
  503. (org-get-indentation))))
  504. ;; After the end of the contents of a greater element, indent like
  505. ;; the beginning of the element.
  506. (should
  507. (= 1
  508. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  509. (forward-line 2)
  510. (org-indent-line)
  511. (org-get-indentation))))
  512. ;; At the first line of an element, indent like previous element's
  513. ;; first line, ignoring footnotes definitions and inline tasks, or
  514. ;; according to parent.
  515. (should
  516. (= 2
  517. (org-test-with-temp-text "A\n\n B\n\nC"
  518. (goto-char (point-max))
  519. (org-indent-line)
  520. (org-get-indentation))))
  521. (should
  522. (= 1
  523. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  524. (goto-char (point-max))
  525. (org-indent-line)
  526. (org-get-indentation))))
  527. (should
  528. (= 1
  529. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  530. (forward-line 1)
  531. (org-indent-line)
  532. (org-get-indentation))))
  533. ;; Within code part of a source block, use language major mode if
  534. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  535. ;; according to line above.
  536. (should
  537. (= 6
  538. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  539. (forward-line 2)
  540. (let ((org-src-tab-acts-natively t)
  541. (org-edit-src-content-indentation 0))
  542. (org-indent-line))
  543. (org-get-indentation))))
  544. (should
  545. (= 1
  546. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  547. (forward-line 2)
  548. (let ((org-src-tab-acts-natively nil)
  549. (org-edit-src-content-indentation 0))
  550. (org-indent-line))
  551. (org-get-indentation))))
  552. ;; Otherwise, indent like the first non-blank line above.
  553. (should
  554. (zerop
  555. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  556. (forward-line 3)
  557. (org-indent-line)
  558. (org-get-indentation))))
  559. ;; Align node properties according to `org-property-format'. Handle
  560. ;; nicely empty values.
  561. (should
  562. (equal ":PROPERTIES:\n:key: value\n:END:"
  563. (org-test-with-temp-text ":PROPERTIES:\n:key: value\n:END:"
  564. (forward-line)
  565. (let ((org-property-format "%-10s %s"))
  566. (org-indent-line)
  567. (buffer-string)))))
  568. (should
  569. (equal ":PROPERTIES:\n:key:\n:END:"
  570. (org-test-with-temp-text ":PROPERTIES:\n:key:\n:END:"
  571. (forward-line)
  572. (let ((org-property-format "%-10s %s"))
  573. (org-indent-line)
  574. (buffer-string))))))
  575. (ert-deftest test-org/indent-region ()
  576. "Test `org-indent-region' specifications."
  577. ;; Indent paragraph.
  578. (should
  579. (equal "A\nB\nC"
  580. (org-test-with-temp-text " A\nB\n C"
  581. (org-indent-region (point-min) (point-max))
  582. (buffer-string))))
  583. ;; Indent greater elements along with their contents.
  584. (should
  585. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  586. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  587. (org-indent-region (point-min) (point-max))
  588. (buffer-string))))
  589. ;; Ignore contents of verse blocks and example blocks.
  590. (should
  591. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  592. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  593. (org-indent-region (point-min) (point-max))
  594. (buffer-string))))
  595. (should
  596. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  597. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  598. (org-indent-region (point-min) (point-max))
  599. (buffer-string))))
  600. ;; Indent according to mode if `org-src-tab-acts-natively' is
  601. ;; non-nil. Otherwise, do not indent code at all.
  602. (should
  603. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  604. (org-test-with-temp-text
  605. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  606. (let ((org-src-tab-acts-natively t)
  607. (org-edit-src-content-indentation 0))
  608. (org-indent-region (point-min) (point-max)))
  609. (buffer-string))))
  610. (should
  611. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  612. (org-test-with-temp-text
  613. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  614. (let ((org-src-tab-acts-natively nil)
  615. (org-edit-src-content-indentation 0))
  616. (org-indent-region (point-min) (point-max)))
  617. (buffer-string))))
  618. ;; Align node properties according to `org-property-format'. Handle
  619. ;; nicely empty values.
  620. (should
  621. (equal ":PROPERTIES:\n:key: value\n:END:"
  622. (org-test-with-temp-text ":PROPERTIES:\n:key: value\n:END:"
  623. (let ((org-property-format "%-10s %s"))
  624. (org-indent-region (point-min) (point-max)))
  625. (buffer-string))))
  626. (should
  627. (equal ":PROPERTIES:\n:key:\n:END:"
  628. (org-test-with-temp-text ":PROPERTIES:\n:key:\n:END:"
  629. (let ((org-property-format "%-10s %s"))
  630. (org-indent-region (point-min) (point-max)))
  631. (buffer-string))))
  632. ;; Indent plain lists.
  633. (should
  634. (equal "- A\n B\n - C\n\n D"
  635. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  636. (org-indent-region (point-min) (point-max))
  637. (buffer-string))))
  638. (should
  639. (equal "- A\n\n- B"
  640. (org-test-with-temp-text " - A\n\n - B"
  641. (org-indent-region (point-min) (point-max))
  642. (buffer-string))))
  643. ;; Indent footnote definitions.
  644. (should
  645. (equal "[fn:1] Definition\n\nDefinition"
  646. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  647. (org-indent-region (point-min) (point-max))
  648. (buffer-string))))
  649. ;; Special case: Start indenting on a blank line.
  650. (should
  651. (equal "\nParagraph"
  652. (org-test-with-temp-text "\n Paragraph"
  653. (org-indent-region (point-min) (point-max))
  654. (buffer-string)))))
  655. ;;; Editing
  656. ;;;; Insert elements
  657. (ert-deftest test-org/meta-return ()
  658. "Test M-RET (`org-meta-return')."
  659. ;; In a table field insert a row above.
  660. (should
  661. (org-test-with-temp-text "| a |"
  662. (forward-char)
  663. (org-meta-return)
  664. (forward-line -1)
  665. (looking-at "| |$")))
  666. ;; In a paragraph change current line into a header.
  667. (should
  668. (org-test-with-temp-text "a"
  669. (org-meta-return)
  670. (beginning-of-line)
  671. (looking-at "\* a$")))
  672. ;; In an item insert an item, in this case above.
  673. (should
  674. (org-test-with-temp-text "- a"
  675. (org-meta-return)
  676. (beginning-of-line)
  677. (looking-at "- $")))
  678. ;; In a drawer and item insert an item, in this case above.
  679. (should
  680. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  681. (forward-line)
  682. (org-meta-return)
  683. (beginning-of-line)
  684. (looking-at "- $"))))
  685. (ert-deftest test-org/insert-heading ()
  686. "Test `org-insert-heading' specifications."
  687. ;; FIXME: Test coverage is incomplete yet.
  688. ;;
  689. ;; In an empty buffer, insert a new headline.
  690. (should
  691. (equal "* "
  692. (org-test-with-temp-text ""
  693. (org-insert-heading)
  694. (buffer-string))))
  695. ;; At the beginning of a line, turn it into a headline
  696. (should
  697. (equal "* P"
  698. (org-test-with-temp-text "<point>P"
  699. (org-insert-heading)
  700. (buffer-string))))
  701. ;; In the middle of a line, split the line if allowed, otherwise,
  702. ;; insert the headline at its end.
  703. (should
  704. (equal "Para\n* graph"
  705. (org-test-with-temp-text "Para<point>graph"
  706. (let ((org-M-RET-may-split-line '((default . t))))
  707. (org-insert-heading))
  708. (buffer-string))))
  709. (should
  710. (equal "Paragraph\n* "
  711. (org-test-with-temp-text "Para<point>graph"
  712. (let ((org-M-RET-may-split-line '((default . nil))))
  713. (org-insert-heading))
  714. (buffer-string))))
  715. ;; Corner case: correctly insert a headline after an empty one.
  716. (should
  717. (equal "* \n* "
  718. (org-test-with-temp-text "* <point>"
  719. (org-insert-heading)
  720. (buffer-string)))))
  721. (ert-deftest test-org/insert-todo-heading-respect-content ()
  722. "Test `org-insert-todo-heading-respect-content' specifications."
  723. ;; Create a TODO heading.
  724. (should
  725. (org-test-with-temp-text "* H1\n Body"
  726. (org-insert-todo-heading-respect-content)
  727. (nth 2 (org-heading-components))))
  728. ;; Add headline at the end of the first subtree
  729. (should
  730. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  731. (search-forward "H1Body")
  732. (org-insert-todo-heading-respect-content)
  733. (and (eobp) (org-at-heading-p))))
  734. ;; In a list, do not create a new item.
  735. (should
  736. (org-test-with-temp-text "* H\n- an item\n- another one"
  737. (search-forward "an ")
  738. (org-insert-todo-heading-respect-content)
  739. (and (eobp) (org-at-heading-p)))))
  740. ;;; Fixed-Width Areas
  741. (ert-deftest test-org/toggle-fixed-width ()
  742. "Test `org-toggle-fixed-width' specifications."
  743. ;; No region: Toggle on fixed-width marker in paragraphs.
  744. (should
  745. (equal ": A"
  746. (org-test-with-temp-text "A"
  747. (org-toggle-fixed-width)
  748. (buffer-string))))
  749. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  750. (should
  751. (equal "A"
  752. (org-test-with-temp-text ": A"
  753. (org-toggle-fixed-width)
  754. (buffer-string))))
  755. ;; No region: Toggle on marker in blank lines after elements or just
  756. ;; after a headline.
  757. (should
  758. (equal "* H\n: "
  759. (org-test-with-temp-text "* H\n"
  760. (forward-line)
  761. (org-toggle-fixed-width)
  762. (buffer-string))))
  763. (should
  764. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  765. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  766. (goto-char (point-max))
  767. (org-toggle-fixed-width)
  768. (buffer-string))))
  769. ;; No region: Toggle on marker in front of one line elements (e.g.,
  770. ;; headlines, clocks)
  771. (should
  772. (equal ": * Headline"
  773. (org-test-with-temp-text "* Headline"
  774. (org-toggle-fixed-width)
  775. (buffer-string))))
  776. (should
  777. (equal ": #+KEYWORD: value"
  778. (org-test-with-temp-text "#+KEYWORD: value"
  779. (org-toggle-fixed-width)
  780. (buffer-string))))
  781. ;; No region: error in other situations.
  782. (should-error
  783. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  784. (forward-line)
  785. (org-toggle-fixed-width)
  786. (buffer-string)))
  787. ;; No region: Indentation is preserved.
  788. (should
  789. (equal "- A\n : B"
  790. (org-test-with-temp-text "- A\n B"
  791. (forward-line)
  792. (org-toggle-fixed-width)
  793. (buffer-string))))
  794. ;; Region: If it contains only fixed-width elements and blank lines,
  795. ;; toggle off fixed-width markup.
  796. (should
  797. (equal "A\n\nB"
  798. (org-test-with-temp-text ": A\n\n: B"
  799. (transient-mark-mode 1)
  800. (push-mark (point) t t)
  801. (goto-char (point-max))
  802. (org-toggle-fixed-width)
  803. (buffer-string))))
  804. ;; Region: If it contains anything else, toggle on fixed-width but
  805. ;; not on fixed-width areas.
  806. (should
  807. (equal ": A\n: \n: B\n: \n: C"
  808. (org-test-with-temp-text "A\n\n: B\n\nC"
  809. (transient-mark-mode 1)
  810. (push-mark (point) t t)
  811. (goto-char (point-max))
  812. (org-toggle-fixed-width)
  813. (buffer-string))))
  814. ;; Region: Ignore blank lines at its end, unless it contains only
  815. ;; such lines.
  816. (should
  817. (equal ": A\n\n"
  818. (org-test-with-temp-text "A\n\n"
  819. (transient-mark-mode 1)
  820. (push-mark (point) t t)
  821. (goto-char (point-max))
  822. (org-toggle-fixed-width)
  823. (buffer-string))))
  824. (should
  825. (equal ": \n: \n"
  826. (org-test-with-temp-text "\n\n"
  827. (transient-mark-mode 1)
  828. (push-mark (point) t t)
  829. (goto-char (point-max))
  830. (org-toggle-fixed-width)
  831. (buffer-string)))))
  832. ;;; Headline
  833. (ert-deftest test-org/in-commented-heading-p ()
  834. "Test `org-in-commented-heading-p' specifications."
  835. ;; Commented headline.
  836. (should
  837. (org-test-with-temp-text "* COMMENT Headline\nBody"
  838. (goto-char (point-max))
  839. (org-in-commented-heading-p)))
  840. ;; Commented ancestor.
  841. (should
  842. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  843. (goto-char (point-max))
  844. (org-in-commented-heading-p)))
  845. ;; Comment keyword is case-sensitive.
  846. (should-not
  847. (org-test-with-temp-text "* Comment Headline\nBody"
  848. (goto-char (point-max))
  849. (org-in-commented-heading-p)))
  850. ;; Keyword is standalone.
  851. (should-not
  852. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  853. (goto-char (point-max))
  854. (org-in-commented-heading-p)))
  855. ;; Optional argument.
  856. (should-not
  857. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  858. (goto-char (point-max))
  859. (org-in-commented-heading-p t))))
  860. ;;; Links
  861. ;;;; Coderefs
  862. (ert-deftest test-org/coderef ()
  863. "Test coderef links specifications."
  864. (should
  865. (org-test-with-temp-text "
  866. #+BEGIN_SRC emacs-lisp
  867. \(+ 1 1) (ref:sc)
  868. #+END_SRC
  869. \[[(sc)]]"
  870. (goto-char (point-max))
  871. (org-open-at-point)
  872. (looking-at "(ref:sc)"))))
  873. ;;;; Custom ID
  874. (ert-deftest test-org/custom-id ()
  875. "Test custom ID links specifications."
  876. (should
  877. (org-test-with-temp-text
  878. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  879. (goto-char (point-max))
  880. (org-open-at-point)
  881. (org-looking-at-p "\\* H1"))))
  882. ;;;; Fuzzy Links
  883. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  884. ;; a named element (#+name: text) and to headlines (* Text).
  885. (ert-deftest test-org/fuzzy-links ()
  886. "Test fuzzy links specifications."
  887. ;; Fuzzy link goes in priority to a matching target.
  888. (should
  889. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  890. (goto-line 5)
  891. (org-open-at-point)
  892. (looking-at "<<Test>>")))
  893. ;; Then fuzzy link points to an element with a given name.
  894. (should
  895. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  896. (goto-line 5)
  897. (org-open-at-point)
  898. (looking-at "#\\+NAME: Test")))
  899. ;; A target still lead to a matching headline otherwise.
  900. (should
  901. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  902. (goto-line 4)
  903. (org-open-at-point)
  904. (looking-at "\\* Head2")))
  905. ;; With a leading star in link, enforce heading match.
  906. (should
  907. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  908. (goto-line 3)
  909. (org-open-at-point)
  910. (looking-at "\\* Test")))
  911. ;; Correctly un-hexify fuzzy links.
  912. (should
  913. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  914. (goto-char (point-max))
  915. (org-open-at-point)
  916. (bobp))))
  917. ;;;; Link Escaping
  918. (ert-deftest test-org/org-link-escape-ascii-character ()
  919. "Escape an ascii character."
  920. (should
  921. (string=
  922. "%5B"
  923. (org-link-escape "["))))
  924. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  925. "Escape an ascii control character."
  926. (should
  927. (string=
  928. "%09"
  929. (org-link-escape "\t"))))
  930. (ert-deftest test-org/org-link-escape-multibyte-character ()
  931. "Escape an unicode multibyte character."
  932. (should
  933. (string=
  934. "%E2%82%AC"
  935. (org-link-escape "€"))))
  936. (ert-deftest test-org/org-link-escape-custom-table ()
  937. "Escape string with custom character table."
  938. (should
  939. (string=
  940. "Foo%3A%42ar%0A"
  941. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  942. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  943. "Escape string with custom table merged with default table."
  944. (should
  945. (string=
  946. "%5BF%6F%6F%3A%42ar%0A%5D"
  947. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  948. (ert-deftest test-org/org-link-unescape-ascii-character ()
  949. "Unescape an ascii character."
  950. (should
  951. (string=
  952. "["
  953. (org-link-unescape "%5B"))))
  954. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  955. "Unescpae an ascii control character."
  956. (should
  957. (string=
  958. "\n"
  959. (org-link-unescape "%0A"))))
  960. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  961. "Unescape unicode multibyte character."
  962. (should
  963. (string=
  964. "€"
  965. (org-link-unescape "%E2%82%AC"))))
  966. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  967. "Unescape old style percent escaped character."
  968. (should
  969. (string=
  970. "àâçèéêîôùû"
  971. (decode-coding-string
  972. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  973. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  974. "Escape and unescape a URL that includes an escaped char.
  975. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  976. (should
  977. (string=
  978. "http://some.host.com/form?&id=blah%2Bblah25"
  979. (org-link-unescape
  980. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  981. (ert-deftest test-org/org-link-escape-chars-browser ()
  982. "Test of the constant `org-link-escape-chars-browser'.
  983. See there why this test is a candidate to be removed once Org
  984. drops support for Emacs 24.1 and 24.2."
  985. (should
  986. (string=
  987. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  988. "%22Release%208.2%22&idxname=emacs-orgmode")
  989. (org-link-escape-browser ; Do not replace with `url-encode-url',
  990. ; see docstring above.
  991. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  992. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  993. ;;;; Open at point
  994. (ert-deftest test-org/open-at-point-in-property ()
  995. "Does `org-open-at-point' open link in property drawer?"
  996. (should
  997. (org-test-with-temp-text
  998. "* Headline
  999. :PROPERTIES:
  1000. :URL: <point>[[info:emacs#Top]]
  1001. :END:"
  1002. (org-open-at-point) t)))
  1003. (ert-deftest test-org/open-at-point-in-comment ()
  1004. "Does `org-open-at-point' open link in a commented line?"
  1005. (should
  1006. (org-test-with-temp-text
  1007. "# <point>[[info:emacs#Top]]"
  1008. (org-open-at-point) t)))
  1009. (ert-deftest test-org/open-at-point/info ()
  1010. "Test `org-open-at-point' on info links."
  1011. (should
  1012. (org-test-with-temp-text
  1013. "<point>[[info:emacs#Top]]"
  1014. (org-open-at-point)
  1015. (and (switch-to-buffer "*info*")
  1016. (prog1
  1017. (looking-at "\nThe Emacs Editor")
  1018. (kill-buffer))))))
  1019. ;;; Node Properties
  1020. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1021. "Ensure properties accumulate in subtree drawers."
  1022. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1023. (org-babel-next-src-block)
  1024. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1025. ;;; Mark Region
  1026. (ert-deftest test-org/mark-subtree ()
  1027. "Test `org-mark-subtree' specifications."
  1028. ;; Error when point is before first headline.
  1029. (should-error
  1030. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1031. (progn (transient-mark-mode 1)
  1032. (org-mark-subtree))))
  1033. ;; Without argument, mark current subtree.
  1034. (should
  1035. (equal
  1036. '(12 32)
  1037. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1038. (progn (transient-mark-mode 1)
  1039. (forward-line 2)
  1040. (org-mark-subtree)
  1041. (list (region-beginning) (region-end))))))
  1042. ;; With an argument, move ARG up.
  1043. (should
  1044. (equal
  1045. '(1 32)
  1046. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1047. (progn (transient-mark-mode 1)
  1048. (forward-line 2)
  1049. (org-mark-subtree 1)
  1050. (list (region-beginning) (region-end))))))
  1051. ;; Do not get fooled by inlinetasks.
  1052. (when (featurep 'org-inlinetask)
  1053. (should
  1054. (= 1
  1055. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1056. (progn (transient-mark-mode 1)
  1057. (forward-line 1)
  1058. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1059. (region-beginning)))))))
  1060. ;;; Navigation
  1061. (ert-deftest test-org/beginning-of-line ()
  1062. "Test `org-beginning-of-line' specifications."
  1063. ;; Standard test.
  1064. (should
  1065. (org-test-with-temp-text "Some text\nSome other text"
  1066. (progn (org-beginning-of-line) (bolp))))
  1067. ;; Standard test with `visual-line-mode'.
  1068. (should-not
  1069. (org-test-with-temp-text "A long line of text\nSome other text"
  1070. (progn (visual-line-mode)
  1071. (forward-char 2)
  1072. (dotimes (i 1000) (insert "very "))
  1073. (org-beginning-of-line)
  1074. (bolp))))
  1075. ;; At an headline with special movement.
  1076. (should
  1077. (org-test-with-temp-text "* TODO Headline"
  1078. (let ((org-special-ctrl-a/e t))
  1079. (org-end-of-line)
  1080. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1081. (progn (org-beginning-of-line) (bolp))
  1082. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1083. (ert-deftest test-org/end-of-line ()
  1084. "Test `org-end-of-line' specifications."
  1085. ;; Standard test.
  1086. (should
  1087. (org-test-with-temp-text "Some text\nSome other text"
  1088. (progn (org-end-of-line) (eolp))))
  1089. ;; Standard test with `visual-line-mode'.
  1090. (should-not
  1091. (org-test-with-temp-text "A long line of text\nSome other text"
  1092. (progn (visual-line-mode)
  1093. (forward-char 2)
  1094. (dotimes (i 1000) (insert "very "))
  1095. (goto-char (point-min))
  1096. (org-end-of-line)
  1097. (eolp))))
  1098. ;; At an headline with special movement.
  1099. (should
  1100. (org-test-with-temp-text "* Headline1 :tag:\n"
  1101. (let ((org-special-ctrl-a/e t))
  1102. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1103. (progn (org-end-of-line) (eolp))
  1104. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1105. ;; At an headline without special movement.
  1106. (should
  1107. (org-test-with-temp-text "* Headline2 :tag:\n"
  1108. (let ((org-special-ctrl-a/e nil))
  1109. (and (progn (org-end-of-line) (eolp))
  1110. (progn (org-end-of-line) (eolp))))))
  1111. ;; At an headline, with reversed movement.
  1112. (should
  1113. (org-test-with-temp-text "* Headline3 :tag:\n"
  1114. (let ((org-special-ctrl-a/e 'reversed)
  1115. (this-command last-command))
  1116. (and (progn (org-end-of-line) (eolp))
  1117. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1118. ;; At a block without hidden contents.
  1119. (should
  1120. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1121. (progn (org-end-of-line) (eolp))))
  1122. ;; At a block with hidden contents.
  1123. (should-not
  1124. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1125. (let ((org-special-ctrl-a/e t))
  1126. (org-hide-block-toggle)
  1127. (org-end-of-line)
  1128. (eobp)))))
  1129. (ert-deftest test-org/forward-paragraph ()
  1130. "Test `org-forward-paragraph' specifications."
  1131. ;; At end of buffer, return an error.
  1132. (should-error
  1133. (org-test-with-temp-text "Paragraph"
  1134. (goto-char (point-max))
  1135. (org-forward-paragraph)))
  1136. ;; Standard test.
  1137. (should
  1138. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1139. (org-forward-paragraph)
  1140. (looking-at "P2")))
  1141. ;; Ignore depth.
  1142. (should
  1143. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1144. (org-forward-paragraph)
  1145. (looking-at "P1")))
  1146. ;; Do not enter elements with invisible contents.
  1147. (should
  1148. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1149. (org-hide-block-toggle)
  1150. (org-forward-paragraph)
  1151. (looking-at "P3")))
  1152. ;; On an affiliated keyword, jump to the beginning of the element.
  1153. (should
  1154. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1155. (org-forward-paragraph)
  1156. (looking-at "Para")))
  1157. ;; On an item or a footnote definition, move to the second element
  1158. ;; inside, if any.
  1159. (should
  1160. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1161. (org-forward-paragraph)
  1162. (looking-at " Paragraph")))
  1163. (should
  1164. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1165. (org-forward-paragraph)
  1166. (looking-at "Paragraph")))
  1167. ;; On an item, or a footnote definition, when the first line is
  1168. ;; empty, move to the first item.
  1169. (should
  1170. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1171. (org-forward-paragraph)
  1172. (looking-at " Paragraph")))
  1173. (should
  1174. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1175. (org-forward-paragraph)
  1176. (looking-at "Paragraph")))
  1177. ;; On a table (resp. a property drawer) do not move through table
  1178. ;; rows (resp. node properties).
  1179. (should
  1180. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1181. (org-forward-paragraph)
  1182. (looking-at "Paragraph")))
  1183. (should
  1184. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1185. (org-forward-paragraph)
  1186. (looking-at "Paragraph")))
  1187. ;; On a verse or source block, stop after blank lines.
  1188. (should
  1189. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1190. (org-forward-paragraph)
  1191. (looking-at "L2")))
  1192. (should
  1193. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1194. (org-forward-paragraph)
  1195. (looking-at "L2"))))
  1196. (ert-deftest test-org/backward-paragraph ()
  1197. "Test `org-backward-paragraph' specifications."
  1198. ;; Error at beginning of buffer.
  1199. (should-error
  1200. (org-test-with-temp-text "Paragraph"
  1201. (org-backward-paragraph)))
  1202. ;; Regular test.
  1203. (should
  1204. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1205. (goto-char (point-max))
  1206. (org-backward-paragraph)
  1207. (looking-at "P3")))
  1208. (should
  1209. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1210. (goto-char (point-max))
  1211. (beginning-of-line)
  1212. (org-backward-paragraph)
  1213. (looking-at "P2")))
  1214. ;; Ignore depth.
  1215. (should
  1216. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1217. (goto-char (point-max))
  1218. (beginning-of-line)
  1219. (org-backward-paragraph)
  1220. (looking-at "P2")))
  1221. ;; Ignore invisible elements.
  1222. (should
  1223. (org-test-with-temp-text "* H1\n P1\n* H2"
  1224. (org-cycle)
  1225. (goto-char (point-max))
  1226. (beginning-of-line)
  1227. (org-backward-paragraph)
  1228. (bobp)))
  1229. ;; On an affiliated keyword, jump to the first one.
  1230. (should
  1231. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1232. (search-forward "c2")
  1233. (org-backward-paragraph)
  1234. (looking-at "#\\+name")))
  1235. ;; On the second element in an item or a footnote definition, jump
  1236. ;; to item or the definition.
  1237. (should
  1238. (org-test-with-temp-text "- line1\n\n line2"
  1239. (goto-char (point-max))
  1240. (beginning-of-line)
  1241. (org-backward-paragraph)
  1242. (looking-at "- line1")))
  1243. (should
  1244. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1245. (goto-char (point-max))
  1246. (beginning-of-line)
  1247. (org-backward-paragraph)
  1248. (looking-at "\\[fn:1\\] line1")))
  1249. ;; On a table (resp. a property drawer), ignore table rows
  1250. ;; (resp. node properties).
  1251. (should
  1252. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1253. (goto-char (point-max))
  1254. (beginning-of-line)
  1255. (org-backward-paragraph)
  1256. (bobp)))
  1257. (should
  1258. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
  1259. (goto-char (point-max))
  1260. (beginning-of-line)
  1261. (org-backward-paragraph)
  1262. (bobp)))
  1263. ;; On a source or verse block, stop before blank lines.
  1264. (should
  1265. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1266. (search-forward "L3")
  1267. (beginning-of-line)
  1268. (org-backward-paragraph)
  1269. (looking-at "L2")))
  1270. (should
  1271. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1272. (search-forward "L3")
  1273. (beginning-of-line)
  1274. (org-backward-paragraph)
  1275. (looking-at "L2"))))
  1276. (ert-deftest test-org/forward-element ()
  1277. "Test `org-forward-element' specifications."
  1278. ;; 1. At EOB: should error.
  1279. (org-test-with-temp-text "Some text\n"
  1280. (goto-char (point-max))
  1281. (should-error (org-forward-element)))
  1282. ;; 2. Standard move: expected to ignore blank lines.
  1283. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1284. (org-forward-element)
  1285. (should (looking-at (regexp-quote "Second paragraph."))))
  1286. ;; 3. Headline tests.
  1287. (org-test-with-temp-text "
  1288. * Head 1
  1289. ** Head 1.1
  1290. *** Head 1.1.1
  1291. ** Head 1.2"
  1292. ;; 3.1. At an headline beginning: move to next headline at the
  1293. ;; same level.
  1294. (goto-line 3)
  1295. (org-forward-element)
  1296. (should (looking-at (regexp-quote "** Head 1.2")))
  1297. ;; 3.2. At an headline beginning: move to parent headline if no
  1298. ;; headline at the same level.
  1299. (goto-line 3)
  1300. (org-forward-element)
  1301. (should (looking-at (regexp-quote "** Head 1.2"))))
  1302. ;; 4. Greater element tests.
  1303. (org-test-with-temp-text
  1304. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1305. ;; 4.1. At a greater element: expected to skip contents.
  1306. (org-forward-element)
  1307. (should (looking-at (regexp-quote "Outside.")))
  1308. ;; 4.2. At the end of greater element contents: expected to skip
  1309. ;; to the end of the greater element.
  1310. (goto-line 2)
  1311. (org-forward-element)
  1312. (should (looking-at (regexp-quote "Outside."))))
  1313. ;; 5. List tests.
  1314. (org-test-with-temp-text "
  1315. - item1
  1316. - sub1
  1317. - sub2
  1318. - sub3
  1319. Inner paragraph.
  1320. - item2
  1321. Outside."
  1322. ;; 5.1. At list top point: expected to move to the element after
  1323. ;; the list.
  1324. (goto-line 2)
  1325. (org-forward-element)
  1326. (should (looking-at (regexp-quote "Outside.")))
  1327. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1328. ;; beginning of line, move to next item.
  1329. (goto-line 2)
  1330. (forward-char)
  1331. (org-forward-element)
  1332. (should (looking-at "- item2"))
  1333. (goto-line 4)
  1334. (forward-char)
  1335. (org-forward-element)
  1336. (should (looking-at " - sub2"))
  1337. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1338. (goto-line 4)
  1339. (org-forward-element)
  1340. (should (looking-at (regexp-quote " Inner paragraph.")))
  1341. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1342. (goto-line 8)
  1343. (org-forward-element)
  1344. (should (looking-at (regexp-quote " Inner paragraph.")))
  1345. ;; 5.5. At an item: expected to move to next item, if any.
  1346. (goto-line 6)
  1347. (org-forward-element)
  1348. (should (looking-at " - sub3"))))
  1349. (ert-deftest test-org/backward-element ()
  1350. "Test `org-backward-element' specifications."
  1351. ;; 1. Should error at BOB.
  1352. (org-test-with-temp-text " \nParagraph."
  1353. (should-error (org-backward-element)))
  1354. ;; 2. Should move at BOB when called on the first element in buffer.
  1355. (should
  1356. (org-test-with-temp-text "\n#+TITLE: test"
  1357. (progn (forward-line)
  1358. (org-backward-element)
  1359. (bobp))))
  1360. ;; 3. Not at the beginning of an element: move at its beginning.
  1361. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1362. (goto-line 3)
  1363. (end-of-line)
  1364. (org-backward-element)
  1365. (should (looking-at (regexp-quote "Paragraph2."))))
  1366. ;; 4. Headline tests.
  1367. (org-test-with-temp-text "
  1368. * Head 1
  1369. ** Head 1.1
  1370. *** Head 1.1.1
  1371. ** Head 1.2"
  1372. ;; 4.1. At an headline beginning: move to previous headline at the
  1373. ;; same level.
  1374. (goto-line 5)
  1375. (org-backward-element)
  1376. (should (looking-at (regexp-quote "** Head 1.1")))
  1377. ;; 4.2. At an headline beginning: move to parent headline if no
  1378. ;; headline at the same level.
  1379. (goto-line 3)
  1380. (org-backward-element)
  1381. (should (looking-at (regexp-quote "* Head 1")))
  1382. ;; 4.3. At the first top-level headline: should error.
  1383. (goto-line 2)
  1384. (should-error (org-backward-element)))
  1385. ;; 5. At beginning of first element inside a greater element:
  1386. ;; expected to move to greater element's beginning.
  1387. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1388. (goto-line 3)
  1389. (org-backward-element)
  1390. (should (looking-at "#\\+BEGIN_CENTER")))
  1391. ;; 6. At the beginning of the first element in a section: should
  1392. ;; move back to headline, if any.
  1393. (should
  1394. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1395. (progn (goto-char (point-max))
  1396. (beginning-of-line)
  1397. (org-backward-element)
  1398. (org-at-heading-p))))
  1399. ;; 7. List tests.
  1400. (org-test-with-temp-text "
  1401. - item1
  1402. - sub1
  1403. - sub2
  1404. - sub3
  1405. Inner paragraph.
  1406. - item2
  1407. Outside."
  1408. ;; 7.1. At beginning of sub-list: expected to move to the
  1409. ;; paragraph before it.
  1410. (goto-line 4)
  1411. (org-backward-element)
  1412. (should (looking-at "item1"))
  1413. ;; 7.2. At an item in a list: expected to move at previous item.
  1414. (goto-line 8)
  1415. (org-backward-element)
  1416. (should (looking-at " - sub2"))
  1417. (goto-line 12)
  1418. (org-backward-element)
  1419. (should (looking-at "- item1"))
  1420. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1421. ;; beginning.
  1422. (goto-line 10)
  1423. (org-backward-element)
  1424. (should (looking-at " - sub1"))
  1425. (goto-line 15)
  1426. (org-backward-element)
  1427. (should (looking-at "- item1"))
  1428. ;; 7.4. At blank-lines before list end: expected to move to top
  1429. ;; item.
  1430. (goto-line 14)
  1431. (org-backward-element)
  1432. (should (looking-at "- item1"))))
  1433. (ert-deftest test-org/up-element ()
  1434. "Test `org-up-element' specifications."
  1435. ;; 1. At BOB or with no surrounding element: should error.
  1436. (org-test-with-temp-text "Paragraph."
  1437. (should-error (org-up-element)))
  1438. (org-test-with-temp-text "* Head1\n* Head2"
  1439. (goto-line 2)
  1440. (should-error (org-up-element)))
  1441. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1442. (goto-line 3)
  1443. (should-error (org-up-element)))
  1444. ;; 2. At an headline: move to parent headline.
  1445. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1446. (goto-line 3)
  1447. (org-up-element)
  1448. (should (looking-at "\\* Head1")))
  1449. ;; 3. Inside a greater element: move to greater element beginning.
  1450. (org-test-with-temp-text
  1451. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1452. (goto-line 3)
  1453. (org-up-element)
  1454. (should (looking-at "#\\+BEGIN_CENTER")))
  1455. ;; 4. List tests.
  1456. (org-test-with-temp-text "* Top
  1457. - item1
  1458. - sub1
  1459. - sub2
  1460. Paragraph within sub2.
  1461. - item2"
  1462. ;; 4.1. Within an item: move to the item beginning.
  1463. (goto-line 8)
  1464. (org-up-element)
  1465. (should (looking-at " - sub2"))
  1466. ;; 4.2. At an item in a sub-list: move to parent item.
  1467. (goto-line 4)
  1468. (org-up-element)
  1469. (should (looking-at "- item1"))
  1470. ;; 4.3. At an item in top list: move to beginning of whole list.
  1471. (goto-line 10)
  1472. (org-up-element)
  1473. (should (looking-at "- item1"))
  1474. ;; 4.4. Special case. At very top point: should move to parent of
  1475. ;; list.
  1476. (goto-line 2)
  1477. (org-up-element)
  1478. (should (looking-at "\\* Top"))))
  1479. (ert-deftest test-org/down-element ()
  1480. "Test `org-down-element' specifications."
  1481. ;; Error when the element hasn't got a recursive type.
  1482. (org-test-with-temp-text "Paragraph."
  1483. (should-error (org-down-element)))
  1484. ;; Error when the element has no contents
  1485. (org-test-with-temp-text "* Headline"
  1486. (should-error (org-down-element)))
  1487. ;; When at a plain-list, move to first item.
  1488. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1489. (goto-line 2)
  1490. (org-down-element)
  1491. (should (looking-at " - Item 1.1")))
  1492. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1493. (org-down-element)
  1494. (should (looking-at " Item 1")))
  1495. ;; When at a table, move to first row
  1496. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1497. (org-down-element)
  1498. (should (looking-at " a | b |")))
  1499. ;; Otherwise, move inside the greater element.
  1500. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1501. (org-down-element)
  1502. (should (looking-at "Paragraph"))))
  1503. (ert-deftest test-org/drag-element-backward ()
  1504. "Test `org-drag-element-backward' specifications."
  1505. ;; Standard test.
  1506. (should
  1507. (equal
  1508. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  1509. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  1510. (org-drag-element-backward)
  1511. (buffer-string))))
  1512. (should
  1513. (equal
  1514. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  1515. (org-test-with-temp-text
  1516. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  1517. (org-drag-element-backward)
  1518. (buffer-string))))
  1519. ;; Preserve blank lines.
  1520. (should
  1521. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  1522. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  1523. (org-drag-element-backward)
  1524. (buffer-string))))
  1525. ;; Preserve column.
  1526. (should
  1527. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  1528. (org-drag-element-backward)
  1529. (org-looking-at-p "2")))
  1530. ;; Error when trying to move first element of buffer.
  1531. (should-error
  1532. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1533. (org-drag-element-backward)))
  1534. ;; Error when trying to swap nested elements.
  1535. (should-error
  1536. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1537. (forward-line)
  1538. (org-drag-element-backward)))
  1539. ;; Error when trying to swap an headline element and a non-headline
  1540. ;; element.
  1541. (should-error
  1542. (org-test-with-temp-text "Test.\n* Head 1"
  1543. (forward-line)
  1544. (org-drag-element-backward)))
  1545. ;; Preserve visibility of elements and their contents.
  1546. (should
  1547. (equal '((63 . 82) (26 . 48))
  1548. (org-test-with-temp-text "
  1549. #+BEGIN_CENTER
  1550. Text.
  1551. #+END_CENTER
  1552. - item 1
  1553. #+BEGIN_QUOTE
  1554. Text.
  1555. #+END_QUOTE"
  1556. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1557. (search-backward "- item 1")
  1558. (org-drag-element-backward)
  1559. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1560. (overlays-in (point-min) (point-max)))))))
  1561. (ert-deftest test-org/drag-element-forward ()
  1562. "Test `org-drag-element-forward' specifications."
  1563. ;; 1. Error when trying to move first element of buffer.
  1564. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1565. (goto-line 3)
  1566. (should-error (org-drag-element-forward)))
  1567. ;; 2. Error when trying to swap nested elements.
  1568. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1569. (forward-line)
  1570. (should-error (org-drag-element-forward)))
  1571. ;; 3. Error when trying to swap a non-headline element and an
  1572. ;; headline.
  1573. (org-test-with-temp-text "Test.\n* Head 1"
  1574. (should-error (org-drag-element-forward)))
  1575. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1576. ;; between elements.
  1577. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1578. (search-forward "graph")
  1579. (org-drag-element-forward)
  1580. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1581. (should (looking-at " 1")))
  1582. ;; 5. Preserve visibility of elements and their contents.
  1583. (org-test-with-temp-text "
  1584. #+BEGIN_CENTER
  1585. Text.
  1586. #+END_CENTER
  1587. - item 1
  1588. #+BEGIN_QUOTE
  1589. Text.
  1590. #+END_QUOTE"
  1591. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1592. (search-backward "#+BEGIN_CENTER")
  1593. (org-drag-element-forward)
  1594. (should
  1595. (equal
  1596. '((63 . 82) (26 . 48))
  1597. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1598. (overlays-in (point-min) (point-max)))))))
  1599. ;;; Planning
  1600. (ert-deftest test-org/timestamp-has-time-p ()
  1601. "Test `org-timestamp-has-time-p' specifications."
  1602. ;; With time.
  1603. (should
  1604. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1605. (org-timestamp-has-time-p (org-element-context))))
  1606. ;; Without time.
  1607. (should-not
  1608. (org-test-with-temp-text "<2012-03-29 Thu>"
  1609. (org-timestamp-has-time-p (org-element-context)))))
  1610. (ert-deftest test-org/timestamp-format ()
  1611. "Test `org-timestamp-format' specifications."
  1612. ;; Regular test.
  1613. (should
  1614. (equal
  1615. "2012-03-29 16:40"
  1616. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1617. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1618. ;; Range end.
  1619. (should
  1620. (equal
  1621. "2012-03-29"
  1622. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1623. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1624. (ert-deftest test-org/timestamp-split-range ()
  1625. "Test `org-timestamp-split-range' specifications."
  1626. ;; Extract range start (active).
  1627. (should
  1628. (equal '(2012 3 29)
  1629. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1630. (let ((ts (org-timestamp-split-range (org-element-context))))
  1631. (mapcar (lambda (p) (org-element-property p ts))
  1632. '(:year-end :month-end :day-end))))))
  1633. ;; Extract range start (inactive)
  1634. (should
  1635. (equal '(2012 3 29)
  1636. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1637. (let ((ts (org-timestamp-split-range (org-element-context))))
  1638. (mapcar (lambda (p) (org-element-property p ts))
  1639. '(:year-end :month-end :day-end))))))
  1640. ;; Extract range end (active).
  1641. (should
  1642. (equal '(2012 3 30)
  1643. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1644. (let ((ts (org-timestamp-split-range
  1645. (org-element-context) t)))
  1646. (mapcar (lambda (p) (org-element-property p ts))
  1647. '(:year-end :month-end :day-end))))))
  1648. ;; Extract range end (inactive)
  1649. (should
  1650. (equal '(2012 3 30)
  1651. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1652. (let ((ts (org-timestamp-split-range
  1653. (org-element-context) t)))
  1654. (mapcar (lambda (p) (org-element-property p ts))
  1655. '(:year-end :month-end :day-end))))))
  1656. ;; Return the timestamp if not a range.
  1657. (should
  1658. (org-test-with-temp-text "[2012-03-29 Thu]"
  1659. (let* ((ts-orig (org-element-context))
  1660. (ts-copy (org-timestamp-split-range ts-orig)))
  1661. (eq ts-orig ts-copy))))
  1662. (should
  1663. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1664. (let* ((ts-orig (org-element-context))
  1665. (ts-copy (org-timestamp-split-range ts-orig)))
  1666. (eq ts-orig ts-copy))))
  1667. ;; Check that parent is the same when a range was split.
  1668. (should
  1669. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1670. (let* ((ts-orig (org-element-context))
  1671. (ts-copy (org-timestamp-split-range ts-orig)))
  1672. (eq (org-element-property :parent ts-orig)
  1673. (org-element-property :parent ts-copy))))))
  1674. (ert-deftest test-org/timestamp-translate ()
  1675. "Test `org-timestamp-translate' specifications."
  1676. ;; Translate whole date range.
  1677. (should
  1678. (equal "<29>--<30>"
  1679. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1680. (let ((org-display-custom-times t)
  1681. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1682. (org-timestamp-translate (org-element-context))))))
  1683. ;; Translate date range start.
  1684. (should
  1685. (equal "<29>"
  1686. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1687. (let ((org-display-custom-times t)
  1688. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1689. (org-timestamp-translate (org-element-context) 'start)))))
  1690. ;; Translate date range end.
  1691. (should
  1692. (equal "<30>"
  1693. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1694. (let ((org-display-custom-times t)
  1695. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1696. (org-timestamp-translate (org-element-context) 'end)))))
  1697. ;; Translate time range.
  1698. (should
  1699. (equal "<08>--<16>"
  1700. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1701. (let ((org-display-custom-times t)
  1702. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1703. (org-timestamp-translate (org-element-context))))))
  1704. ;; Translate non-range timestamp.
  1705. (should
  1706. (equal "<29>"
  1707. (org-test-with-temp-text "<2012-03-29 Thu>"
  1708. (let ((org-display-custom-times t)
  1709. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1710. (org-timestamp-translate (org-element-context))))))
  1711. ;; Do not change `diary' timestamps.
  1712. (should
  1713. (equal "<%%(org-float t 4 2)>"
  1714. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1715. (let ((org-display-custom-times t)
  1716. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1717. (org-timestamp-translate (org-element-context)))))))
  1718. ;;; Radio Targets
  1719. (ert-deftest test-org/update-radio-target-regexp ()
  1720. "Test `org-update-radio-target-regexp' specifications."
  1721. ;; Properly update cache with no previous radio target regexp.
  1722. (should
  1723. (eq 'link
  1724. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  1725. (save-excursion (goto-char (point-max)) (org-element-context))
  1726. (insert "<<<")
  1727. (search-forward "o")
  1728. (insert ">>>")
  1729. (org-update-radio-target-regexp)
  1730. (goto-char (point-max))
  1731. (org-element-type (org-element-context)))))
  1732. ;; Properly update cache with previous radio target regexp.
  1733. (should
  1734. (eq 'link
  1735. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  1736. (save-excursion (goto-char (point-max)) (org-element-context))
  1737. (insert "<<<")
  1738. (search-forward "o")
  1739. (insert ">>>")
  1740. (org-update-radio-target-regexp)
  1741. (search-backward "r")
  1742. (delete-char 5)
  1743. (insert "new")
  1744. (org-update-radio-target-regexp)
  1745. (goto-char (point-max))
  1746. (delete-region (line-beginning-position) (point))
  1747. (insert "new")
  1748. (org-element-type (org-element-context))))))
  1749. ;;; Visibility
  1750. (ert-deftest test-org/flag-drawer ()
  1751. "Test `org-flag-drawer' specifications."
  1752. ;; Hide drawer.
  1753. (should
  1754. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1755. (org-flag-drawer t)
  1756. (get-char-property (line-end-position) 'invisible)))
  1757. ;; Show drawer.
  1758. (should-not
  1759. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1760. (org-flag-drawer t)
  1761. (org-flag-drawer nil)
  1762. (get-char-property (line-end-position) 'invisible)))
  1763. ;; Test optional argument.
  1764. (should
  1765. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1766. (let ((drawer (save-excursion (search-forward ":D2")
  1767. (org-element-at-point))))
  1768. (org-flag-drawer t drawer)
  1769. (get-char-property (progn (search-forward ":D2") (line-end-position))
  1770. 'invisible))))
  1771. (should-not
  1772. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1773. (let ((drawer (save-excursion (search-forward ":D2")
  1774. (org-element-at-point))))
  1775. (org-flag-drawer t drawer)
  1776. (get-char-property (line-end-position) 'invisible))))
  1777. ;; Do not hide fake drawers.
  1778. (should-not
  1779. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  1780. (forward-line 1)
  1781. (org-flag-drawer t)
  1782. (get-char-property (line-end-position) 'invisible)))
  1783. ;; Do not hide incomplete drawers.
  1784. (should-not
  1785. (org-test-with-temp-text ":D:\nparagraph"
  1786. (forward-line 1)
  1787. (org-flag-drawer t)
  1788. (get-char-property (line-end-position) 'invisible)))
  1789. ;; Do not hide drawers when called from final blank lines.
  1790. (should-not
  1791. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  1792. (goto-char (point-max))
  1793. (org-flag-drawer t)
  1794. (goto-char (point-min))
  1795. (get-char-property (line-end-position) 'invisible)))
  1796. ;; Don't leave point in an invisible part of the buffer when hiding
  1797. ;; a drawer away.
  1798. (should-not
  1799. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1800. (goto-char (point-max))
  1801. (org-flag-drawer t)
  1802. (get-char-property (point) 'invisible))))
  1803. (ert-deftest test-org/hide-block-toggle ()
  1804. "Test `org-hide-block-toggle' specifications."
  1805. ;; Error when not at a block.
  1806. (should-error
  1807. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  1808. (org-hide-block-toggle 'off)
  1809. (get-char-property (line-end-position) 'invisible)))
  1810. ;; Hide block.
  1811. (should
  1812. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  1813. (org-hide-block-toggle)
  1814. (get-char-property (line-end-position) 'invisible)))
  1815. (should
  1816. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  1817. (org-hide-block-toggle)
  1818. (get-char-property (line-end-position) 'invisible)))
  1819. ;; Show block unconditionally when optional argument is `off'.
  1820. (should-not
  1821. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  1822. (org-hide-block-toggle)
  1823. (org-hide-block-toggle 'off)
  1824. (get-char-property (line-end-position) 'invisible)))
  1825. (should-not
  1826. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  1827. (org-hide-block-toggle 'off)
  1828. (get-char-property (line-end-position) 'invisible)))
  1829. ;; Hide block unconditionally when optional argument is non-nil.
  1830. (should
  1831. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  1832. (org-hide-block-toggle t)
  1833. (get-char-property (line-end-position) 'invisible)))
  1834. (should
  1835. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  1836. (org-hide-block-toggle)
  1837. (org-hide-block-toggle t)
  1838. (get-char-property (line-end-position) 'invisible)))
  1839. ;; Do not hide block when called from final blank lines.
  1840. (should-not
  1841. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  1842. (org-hide-block-toggle)
  1843. (goto-char (point-min))
  1844. (get-char-property (line-end-position) 'invisible)))
  1845. ;; Don't leave point in an invisible part of the buffer when hiding
  1846. ;; a block away.
  1847. (should-not
  1848. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  1849. (org-hide-block-toggle)
  1850. (get-char-property (point) 'invisible))))
  1851. (ert-deftest test-org/hide-block-toggle-maybe ()
  1852. "Test `org-hide-block-toggle-maybe' specifications."
  1853. (should
  1854. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  1855. (org-hide-block-toggle-maybe)))
  1856. (should-not
  1857. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  1858. (provide 'test-org)
  1859. ;;; test-org.el ends here