test-org.el 58 KB

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