test-org.el 48 KB

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