test-org.el 49 KB

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