test-org.el 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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. ;;; Links
  515. ;;;; Fuzzy Links
  516. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  517. ;; a named element (#+name: text) and to headlines (* Text).
  518. (ert-deftest test-org/fuzzy-links ()
  519. "Test fuzzy links specifications."
  520. ;; 1. Fuzzy link goes in priority to a matching target.
  521. (should
  522. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  523. (goto-line 5)
  524. (org-open-at-point)
  525. (looking-at "<<Test>>")))
  526. ;; 2. Then fuzzy link points to an element with a given name.
  527. (should
  528. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  529. (goto-line 5)
  530. (org-open-at-point)
  531. (looking-at "#\\+NAME: Test")))
  532. ;; 3. A target still lead to a matching headline otherwise.
  533. (should
  534. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  535. (goto-line 4)
  536. (org-open-at-point)
  537. (looking-at "\\* Head2")))
  538. ;; 4. With a leading star in link, enforce heading match.
  539. (should
  540. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  541. (goto-line 3)
  542. (org-open-at-point)
  543. (looking-at "\\* Test"))))
  544. ;;;; Link Escaping
  545. (ert-deftest test-org/org-link-escape-ascii-character ()
  546. "Escape an ascii character."
  547. (should
  548. (string=
  549. "%5B"
  550. (org-link-escape "["))))
  551. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  552. "Escape an ascii control character."
  553. (should
  554. (string=
  555. "%09"
  556. (org-link-escape "\t"))))
  557. (ert-deftest test-org/org-link-escape-multibyte-character ()
  558. "Escape an unicode multibyte character."
  559. (should
  560. (string=
  561. "%E2%82%AC"
  562. (org-link-escape "€"))))
  563. (ert-deftest test-org/org-link-escape-custom-table ()
  564. "Escape string with custom character table."
  565. (should
  566. (string=
  567. "Foo%3A%42ar%0A"
  568. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  569. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  570. "Escape string with custom table merged with default table."
  571. (should
  572. (string=
  573. "%5BF%6F%6F%3A%42ar%0A%5D"
  574. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  575. (ert-deftest test-org/org-link-unescape-ascii-character ()
  576. "Unescape an ascii character."
  577. (should
  578. (string=
  579. "["
  580. (org-link-unescape "%5B"))))
  581. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  582. "Unescpae an ascii control character."
  583. (should
  584. (string=
  585. "\n"
  586. (org-link-unescape "%0A"))))
  587. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  588. "Unescape unicode multibyte character."
  589. (should
  590. (string=
  591. "€"
  592. (org-link-unescape "%E2%82%AC"))))
  593. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  594. "Unescape old style percent escaped character."
  595. (should
  596. (string=
  597. "àâçèéêîôùû"
  598. (decode-coding-string
  599. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  600. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  601. "Escape and unescape a URL that includes an escaped char.
  602. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  603. (should
  604. (string=
  605. "http://some.host.com/form?&id=blah%2Bblah25"
  606. (org-link-unescape
  607. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  608. (ert-deftest test-org/org-link-escape-chars-browser ()
  609. "Escape a URL to pass to `browse-url'."
  610. (should
  611. (string=
  612. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  613. "%22Release%208.2%22&idxname=emacs-orgmode")
  614. (org-link-escape-browser
  615. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  616. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  617. ;;; Node Properties
  618. (ert-deftest test-org/accumulated-properties-in-drawers ()
  619. "Ensure properties accumulate in subtree drawers."
  620. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  621. (org-babel-next-src-block)
  622. (should (equal '(2 1) (org-babel-execute-src-block)))))
  623. ;;; Mark Region
  624. (ert-deftest test-org/mark-subtree ()
  625. "Test `org-mark-subtree' specifications."
  626. ;; Error when point is before first headline.
  627. (should-error
  628. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  629. (progn (transient-mark-mode 1)
  630. (org-mark-subtree))))
  631. ;; Without argument, mark current subtree.
  632. (should
  633. (equal
  634. '(12 32)
  635. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  636. (progn (transient-mark-mode 1)
  637. (forward-line 2)
  638. (org-mark-subtree)
  639. (list (region-beginning) (region-end))))))
  640. ;; With an argument, move ARG up.
  641. (should
  642. (equal
  643. '(1 32)
  644. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  645. (progn (transient-mark-mode 1)
  646. (forward-line 2)
  647. (org-mark-subtree 1)
  648. (list (region-beginning) (region-end))))))
  649. ;; Do not get fooled by inlinetasks.
  650. (when (featurep 'org-inlinetask)
  651. (should
  652. (= 1
  653. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  654. (progn (transient-mark-mode 1)
  655. (forward-line 1)
  656. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  657. (region-beginning)))))))
  658. ;;; Navigation
  659. (ert-deftest test-org/beginning-of-line ()
  660. "Test `org-beginning-of-line' specifications."
  661. ;; Standard test.
  662. (should
  663. (org-test-with-temp-text "Some text\nSome other text"
  664. (progn (org-beginning-of-line) (bolp))))
  665. ;; Standard test with `visual-line-mode'.
  666. (should-not
  667. (org-test-with-temp-text "A long line of text\nSome other text"
  668. (progn (visual-line-mode)
  669. (forward-char 2)
  670. (dotimes (i 1000) (insert "very "))
  671. (org-beginning-of-line)
  672. (bolp))))
  673. ;; At an headline with special movement.
  674. (should
  675. (org-test-with-temp-text "* TODO Headline"
  676. (let ((org-special-ctrl-a/e t))
  677. (org-end-of-line)
  678. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  679. (progn (org-beginning-of-line) (bolp))
  680. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  681. (ert-deftest test-org/end-of-line ()
  682. "Test `org-end-of-line' specifications."
  683. ;; Standard test.
  684. (should
  685. (org-test-with-temp-text "Some text\nSome other text"
  686. (progn (org-end-of-line) (eolp))))
  687. ;; Standard test with `visual-line-mode'.
  688. (should-not
  689. (org-test-with-temp-text "A long line of text\nSome other text"
  690. (progn (visual-line-mode)
  691. (forward-char 2)
  692. (dotimes (i 1000) (insert "very "))
  693. (goto-char (point-min))
  694. (org-end-of-line)
  695. (eolp))))
  696. ;; At an headline with special movement.
  697. (should
  698. (org-test-with-temp-text "* Headline1 :tag:\n"
  699. (let ((org-special-ctrl-a/e t))
  700. (and (progn (org-end-of-line) (looking-at " :tag:"))
  701. (progn (org-end-of-line) (eolp))
  702. (progn (org-end-of-line) (looking-at " :tag:"))))))
  703. ;; At an headline without special movement.
  704. (should
  705. (org-test-with-temp-text "* Headline2 :tag:\n"
  706. (let ((org-special-ctrl-a/e nil))
  707. (and (progn (org-end-of-line) (eolp))
  708. (progn (org-end-of-line) (eolp))))))
  709. ;; At an headline, with reversed movement.
  710. (should
  711. (org-test-with-temp-text "* Headline3 :tag:\n"
  712. (let ((org-special-ctrl-a/e 'reversed)
  713. (this-command last-command))
  714. (and (progn (org-end-of-line) (eolp))
  715. (progn (org-end-of-line) (looking-at " :tag:"))))))
  716. ;; At a block without hidden contents.
  717. (should
  718. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  719. (progn (org-end-of-line) (eolp))))
  720. ;; At a block with hidden contents.
  721. (should-not
  722. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  723. (let ((org-special-ctrl-a/e t))
  724. (org-hide-block-toggle)
  725. (org-end-of-line)
  726. (eobp)))))
  727. (ert-deftest test-org/forward-paragraph ()
  728. "Test `org-forward-paragraph' specifications."
  729. ;; At end of buffer, return an error.
  730. (should-error
  731. (org-test-with-temp-text "Paragraph"
  732. (goto-char (point-max))
  733. (org-forward-paragraph)))
  734. ;; Standard test.
  735. (should
  736. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  737. (org-forward-paragraph)
  738. (looking-at "P2")))
  739. ;; Ignore depth.
  740. (should
  741. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  742. (org-forward-paragraph)
  743. (looking-at "P1")))
  744. ;; Do not enter elements with invisible contents.
  745. (should
  746. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  747. (org-hide-block-toggle)
  748. (org-forward-paragraph)
  749. (looking-at "P3")))
  750. ;; On an affiliated keyword, jump to the beginning of the element.
  751. (should
  752. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  753. (org-forward-paragraph)
  754. (looking-at "Para")))
  755. ;; On an item or a footnote definition, move to the second element
  756. ;; inside, if any.
  757. (should
  758. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  759. (org-forward-paragraph)
  760. (looking-at " Paragraph")))
  761. (should
  762. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  763. (org-forward-paragraph)
  764. (looking-at "Paragraph")))
  765. ;; On an item, or a footnote definition, when the first line is
  766. ;; empty, move to the first item.
  767. (should
  768. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  769. (org-forward-paragraph)
  770. (looking-at " Paragraph")))
  771. (should
  772. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  773. (org-forward-paragraph)
  774. (looking-at "Paragraph")))
  775. ;; On a table (resp. a property drawer) do not move through table
  776. ;; rows (resp. node properties).
  777. (should
  778. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  779. (org-forward-paragraph)
  780. (looking-at "Paragraph")))
  781. (should
  782. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
  783. (org-forward-paragraph)
  784. (looking-at "Paragraph")))
  785. ;; On a verse or source block, stop after blank lines.
  786. (should
  787. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  788. (org-forward-paragraph)
  789. (looking-at "L2")))
  790. (should
  791. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  792. (org-forward-paragraph)
  793. (looking-at "L2"))))
  794. (ert-deftest test-org/backward-paragraph ()
  795. "Test `org-backward-paragraph' specifications."
  796. ;; Error at beginning of buffer.
  797. (should-error
  798. (org-test-with-temp-text "Paragraph"
  799. (org-backward-paragraph)))
  800. ;; Regular test.
  801. (should
  802. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  803. (goto-char (point-max))
  804. (org-backward-paragraph)
  805. (looking-at "P3")))
  806. (should
  807. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  808. (goto-char (point-max))
  809. (beginning-of-line)
  810. (org-backward-paragraph)
  811. (looking-at "P2")))
  812. ;; Ignore depth.
  813. (should
  814. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  815. (goto-char (point-max))
  816. (beginning-of-line)
  817. (org-backward-paragraph)
  818. (looking-at "P2")))
  819. ;; Ignore invisible elements.
  820. (should
  821. (org-test-with-temp-text "* H1\n P1\n* H2"
  822. (org-cycle)
  823. (goto-char (point-max))
  824. (beginning-of-line)
  825. (org-backward-paragraph)
  826. (bobp)))
  827. ;; On an affiliated keyword, jump to the first one.
  828. (should
  829. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  830. (search-forward "c2")
  831. (org-backward-paragraph)
  832. (looking-at "#\\+name")))
  833. ;; On the second element in an item or a footnote definition, jump
  834. ;; to item or the definition.
  835. (should
  836. (org-test-with-temp-text "- line1\n\n line2"
  837. (goto-char (point-max))
  838. (beginning-of-line)
  839. (org-backward-paragraph)
  840. (looking-at "- line1")))
  841. (should
  842. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  843. (goto-char (point-max))
  844. (beginning-of-line)
  845. (org-backward-paragraph)
  846. (looking-at "\\[fn:1\\] line1")))
  847. ;; On a table (resp. a property drawer), ignore table rows
  848. ;; (resp. node properties).
  849. (should
  850. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  851. (goto-char (point-max))
  852. (beginning-of-line)
  853. (org-backward-paragraph)
  854. (bobp)))
  855. (should
  856. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
  857. (goto-char (point-max))
  858. (beginning-of-line)
  859. (org-backward-paragraph)
  860. (bobp)))
  861. ;; On a source or verse block, stop before blank lines.
  862. (should
  863. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  864. (search-forward "L3")
  865. (beginning-of-line)
  866. (org-backward-paragraph)
  867. (looking-at "L2")))
  868. (should
  869. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  870. (search-forward "L3")
  871. (beginning-of-line)
  872. (org-backward-paragraph)
  873. (looking-at "L2"))))
  874. (ert-deftest test-org/forward-element ()
  875. "Test `org-forward-element' specifications."
  876. ;; 1. At EOB: should error.
  877. (org-test-with-temp-text "Some text\n"
  878. (goto-char (point-max))
  879. (should-error (org-forward-element)))
  880. ;; 2. Standard move: expected to ignore blank lines.
  881. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  882. (org-forward-element)
  883. (should (looking-at (regexp-quote "Second paragraph."))))
  884. ;; 3. Headline tests.
  885. (org-test-with-temp-text "
  886. * Head 1
  887. ** Head 1.1
  888. *** Head 1.1.1
  889. ** Head 1.2"
  890. ;; 3.1. At an headline beginning: move to next headline at the
  891. ;; same level.
  892. (goto-line 3)
  893. (org-forward-element)
  894. (should (looking-at (regexp-quote "** Head 1.2")))
  895. ;; 3.2. At an headline beginning: move to parent headline if no
  896. ;; headline at the same level.
  897. (goto-line 3)
  898. (org-forward-element)
  899. (should (looking-at (regexp-quote "** Head 1.2"))))
  900. ;; 4. Greater element tests.
  901. (org-test-with-temp-text
  902. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  903. ;; 4.1. At a greater element: expected to skip contents.
  904. (org-forward-element)
  905. (should (looking-at (regexp-quote "Outside.")))
  906. ;; 4.2. At the end of greater element contents: expected to skip
  907. ;; to the end of the greater element.
  908. (goto-line 2)
  909. (org-forward-element)
  910. (should (looking-at (regexp-quote "Outside."))))
  911. ;; 5. List tests.
  912. (org-test-with-temp-text "
  913. - item1
  914. - sub1
  915. - sub2
  916. - sub3
  917. Inner paragraph.
  918. - item2
  919. Outside."
  920. ;; 5.1. At list top point: expected to move to the element after
  921. ;; the list.
  922. (goto-line 2)
  923. (org-forward-element)
  924. (should (looking-at (regexp-quote "Outside.")))
  925. ;; 5.2. Special case: at the first line of a sub-list, but not at
  926. ;; beginning of line, move to next item.
  927. (goto-line 2)
  928. (forward-char)
  929. (org-forward-element)
  930. (should (looking-at "- item2"))
  931. (goto-line 4)
  932. (forward-char)
  933. (org-forward-element)
  934. (should (looking-at " - sub2"))
  935. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  936. (goto-line 4)
  937. (org-forward-element)
  938. (should (looking-at (regexp-quote " Inner paragraph.")))
  939. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  940. (goto-line 8)
  941. (org-forward-element)
  942. (should (looking-at (regexp-quote " Inner paragraph.")))
  943. ;; 5.5. At an item: expected to move to next item, if any.
  944. (goto-line 6)
  945. (org-forward-element)
  946. (should (looking-at " - sub3"))))
  947. (ert-deftest test-org/backward-element ()
  948. "Test `org-backward-element' specifications."
  949. ;; 1. Should error at BOB.
  950. (org-test-with-temp-text " \nParagraph."
  951. (should-error (org-backward-element)))
  952. ;; 2. Should move at BOB when called on the first element in buffer.
  953. (should
  954. (org-test-with-temp-text "\n#+TITLE: test"
  955. (progn (forward-line)
  956. (org-backward-element)
  957. (bobp))))
  958. ;; 3. Not at the beginning of an element: move at its beginning.
  959. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  960. (goto-line 3)
  961. (end-of-line)
  962. (org-backward-element)
  963. (should (looking-at (regexp-quote "Paragraph2."))))
  964. ;; 4. Headline tests.
  965. (org-test-with-temp-text "
  966. * Head 1
  967. ** Head 1.1
  968. *** Head 1.1.1
  969. ** Head 1.2"
  970. ;; 4.1. At an headline beginning: move to previous headline at the
  971. ;; same level.
  972. (goto-line 5)
  973. (org-backward-element)
  974. (should (looking-at (regexp-quote "** Head 1.1")))
  975. ;; 4.2. At an headline beginning: move to parent headline if no
  976. ;; headline at the same level.
  977. (goto-line 3)
  978. (org-backward-element)
  979. (should (looking-at (regexp-quote "* Head 1")))
  980. ;; 4.3. At the first top-level headline: should error.
  981. (goto-line 2)
  982. (should-error (org-backward-element)))
  983. ;; 5. At beginning of first element inside a greater element:
  984. ;; expected to move to greater element's beginning.
  985. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  986. (goto-line 3)
  987. (org-backward-element)
  988. (should (looking-at "#\\+BEGIN_CENTER")))
  989. ;; 6. At the beginning of the first element in a section: should
  990. ;; move back to headline, if any.
  991. (should
  992. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  993. (progn (goto-char (point-max))
  994. (beginning-of-line)
  995. (org-backward-element)
  996. (org-at-heading-p))))
  997. ;; 7. List tests.
  998. (org-test-with-temp-text "
  999. - item1
  1000. - sub1
  1001. - sub2
  1002. - sub3
  1003. Inner paragraph.
  1004. - item2
  1005. Outside."
  1006. ;; 7.1. At beginning of sub-list: expected to move to the
  1007. ;; paragraph before it.
  1008. (goto-line 4)
  1009. (org-backward-element)
  1010. (should (looking-at "item1"))
  1011. ;; 7.2. At an item in a list: expected to move at previous item.
  1012. (goto-line 8)
  1013. (org-backward-element)
  1014. (should (looking-at " - sub2"))
  1015. (goto-line 12)
  1016. (org-backward-element)
  1017. (should (looking-at "- item1"))
  1018. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1019. ;; beginning.
  1020. (goto-line 10)
  1021. (org-backward-element)
  1022. (should (looking-at " - sub1"))
  1023. (goto-line 15)
  1024. (org-backward-element)
  1025. (should (looking-at "- item1"))
  1026. ;; 7.4. At blank-lines before list end: expected to move to top
  1027. ;; item.
  1028. (goto-line 14)
  1029. (org-backward-element)
  1030. (should (looking-at "- item1"))))
  1031. (ert-deftest test-org/up-element ()
  1032. "Test `org-up-element' specifications."
  1033. ;; 1. At BOB or with no surrounding element: should error.
  1034. (org-test-with-temp-text "Paragraph."
  1035. (should-error (org-up-element)))
  1036. (org-test-with-temp-text "* Head1\n* Head2"
  1037. (goto-line 2)
  1038. (should-error (org-up-element)))
  1039. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1040. (goto-line 3)
  1041. (should-error (org-up-element)))
  1042. ;; 2. At an headline: move to parent headline.
  1043. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1044. (goto-line 3)
  1045. (org-up-element)
  1046. (should (looking-at "\\* Head1")))
  1047. ;; 3. Inside a greater element: move to greater element beginning.
  1048. (org-test-with-temp-text
  1049. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1050. (goto-line 3)
  1051. (org-up-element)
  1052. (should (looking-at "#\\+BEGIN_CENTER")))
  1053. ;; 4. List tests.
  1054. (org-test-with-temp-text "* Top
  1055. - item1
  1056. - sub1
  1057. - sub2
  1058. Paragraph within sub2.
  1059. - item2"
  1060. ;; 4.1. Within an item: move to the item beginning.
  1061. (goto-line 8)
  1062. (org-up-element)
  1063. (should (looking-at " - sub2"))
  1064. ;; 4.2. At an item in a sub-list: move to parent item.
  1065. (goto-line 4)
  1066. (org-up-element)
  1067. (should (looking-at "- item1"))
  1068. ;; 4.3. At an item in top list: move to beginning of whole list.
  1069. (goto-line 10)
  1070. (org-up-element)
  1071. (should (looking-at "- item1"))
  1072. ;; 4.4. Special case. At very top point: should move to parent of
  1073. ;; list.
  1074. (goto-line 2)
  1075. (org-up-element)
  1076. (should (looking-at "\\* Top"))))
  1077. (ert-deftest test-org/down-element ()
  1078. "Test `org-down-element' specifications."
  1079. ;; Error when the element hasn't got a recursive type.
  1080. (org-test-with-temp-text "Paragraph."
  1081. (should-error (org-down-element)))
  1082. ;; Error when the element has no contents
  1083. (org-test-with-temp-text "* Headline"
  1084. (should-error (org-down-element)))
  1085. ;; When at a plain-list, move to first item.
  1086. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1087. (goto-line 2)
  1088. (org-down-element)
  1089. (should (looking-at " - Item 1.1")))
  1090. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1091. (org-down-element)
  1092. (should (looking-at " Item 1")))
  1093. ;; When at a table, move to first row
  1094. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1095. (org-down-element)
  1096. (should (looking-at " a | b |")))
  1097. ;; Otherwise, move inside the greater element.
  1098. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1099. (org-down-element)
  1100. (should (looking-at "Paragraph"))))
  1101. (ert-deftest test-org/drag-element-backward ()
  1102. "Test `org-drag-element-backward' specifications."
  1103. ;; 1. Error when trying to move first element of buffer.
  1104. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1105. (should-error (org-drag-element-backward)))
  1106. ;; 2. Error when trying to swap nested elements.
  1107. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1108. (forward-line)
  1109. (should-error (org-drag-element-backward)))
  1110. ;; 3. Error when trying to swap an headline element and
  1111. ;; a non-headline element.
  1112. (org-test-with-temp-text "Test.\n* Head 1"
  1113. (forward-line)
  1114. (should-error (org-drag-element-backward)))
  1115. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1116. ;; between elements.
  1117. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  1118. (search-forward "graph")
  1119. (org-drag-element-backward)
  1120. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  1121. (should (looking-at " 2")))
  1122. ;; 5. Preserve visibility of elements and their contents.
  1123. (org-test-with-temp-text "
  1124. #+BEGIN_CENTER
  1125. Text.
  1126. #+END_CENTER
  1127. - item 1
  1128. #+BEGIN_QUOTE
  1129. Text.
  1130. #+END_QUOTE"
  1131. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1132. (search-backward "- item 1")
  1133. (org-drag-element-backward)
  1134. (should
  1135. (equal
  1136. '((63 . 82) (26 . 48))
  1137. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1138. (overlays-in (point-min) (point-max)))))))
  1139. (ert-deftest test-org/drag-element-forward ()
  1140. "Test `org-drag-element-forward' specifications."
  1141. ;; 1. Error when trying to move first element of buffer.
  1142. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1143. (goto-line 3)
  1144. (should-error (org-drag-element-forward)))
  1145. ;; 2. Error when trying to swap nested elements.
  1146. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1147. (forward-line)
  1148. (should-error (org-drag-element-forward)))
  1149. ;; 3. Error when trying to swap a non-headline element and an
  1150. ;; headline.
  1151. (org-test-with-temp-text "Test.\n* Head 1"
  1152. (should-error (org-drag-element-forward)))
  1153. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1154. ;; between elements.
  1155. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1156. (search-forward "graph")
  1157. (org-drag-element-forward)
  1158. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1159. (should (looking-at " 1")))
  1160. ;; 5. Preserve visibility of elements and their contents.
  1161. (org-test-with-temp-text "
  1162. #+BEGIN_CENTER
  1163. Text.
  1164. #+END_CENTER
  1165. - item 1
  1166. #+BEGIN_QUOTE
  1167. Text.
  1168. #+END_QUOTE"
  1169. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1170. (search-backward "#+BEGIN_CENTER")
  1171. (org-drag-element-forward)
  1172. (should
  1173. (equal
  1174. '((63 . 82) (26 . 48))
  1175. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1176. (overlays-in (point-min) (point-max)))))))
  1177. ;;; Planning
  1178. (ert-deftest test-org/timestamp-has-time-p ()
  1179. "Test `org-timestamp-has-time-p' specifications."
  1180. ;; With time.
  1181. (should
  1182. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1183. (org-timestamp-has-time-p (org-element-context))))
  1184. ;; Without time.
  1185. (should-not
  1186. (org-test-with-temp-text "<2012-03-29 Thu>"
  1187. (org-timestamp-has-time-p (org-element-context)))))
  1188. (ert-deftest test-org/timestamp-format ()
  1189. "Test `org-timestamp-format' specifications."
  1190. ;; Regular test.
  1191. (should
  1192. (equal
  1193. "2012-03-29 16:40"
  1194. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1195. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1196. ;; Range end.
  1197. (should
  1198. (equal
  1199. "2012-03-29"
  1200. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1201. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1202. (ert-deftest test-org/timestamp-split-range ()
  1203. "Test `org-timestamp-split-range' specifications."
  1204. ;; Extract range start (active).
  1205. (should
  1206. (equal '(2012 3 29)
  1207. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1208. (let ((ts (org-timestamp-split-range (org-element-context))))
  1209. (mapcar (lambda (p) (org-element-property p ts))
  1210. '(:year-end :month-end :day-end))))))
  1211. ;; Extract range start (inactive)
  1212. (should
  1213. (equal '(2012 3 29)
  1214. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1215. (let ((ts (org-timestamp-split-range (org-element-context))))
  1216. (mapcar (lambda (p) (org-element-property p ts))
  1217. '(:year-end :month-end :day-end))))))
  1218. ;; Extract range end (active).
  1219. (should
  1220. (equal '(2012 3 30)
  1221. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1222. (let ((ts (org-timestamp-split-range
  1223. (org-element-context) t)))
  1224. (mapcar (lambda (p) (org-element-property p ts))
  1225. '(:year-end :month-end :day-end))))))
  1226. ;; Extract range end (inactive)
  1227. (should
  1228. (equal '(2012 3 30)
  1229. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1230. (let ((ts (org-timestamp-split-range
  1231. (org-element-context) t)))
  1232. (mapcar (lambda (p) (org-element-property p ts))
  1233. '(:year-end :month-end :day-end))))))
  1234. ;; Return the timestamp if not a range.
  1235. (should
  1236. (org-test-with-temp-text "[2012-03-29 Thu]"
  1237. (let* ((ts-orig (org-element-context))
  1238. (ts-copy (org-timestamp-split-range ts-orig)))
  1239. (eq ts-orig ts-copy))))
  1240. (should
  1241. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1242. (let* ((ts-orig (org-element-context))
  1243. (ts-copy (org-timestamp-split-range ts-orig)))
  1244. (eq ts-orig ts-copy))))
  1245. ;; Check that parent is the same when a range was split.
  1246. (should
  1247. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1248. (let* ((ts-orig (org-element-context))
  1249. (ts-copy (org-timestamp-split-range ts-orig)))
  1250. (eq (org-element-property :parent ts-orig)
  1251. (org-element-property :parent ts-copy))))))
  1252. (ert-deftest test-org/timestamp-translate ()
  1253. "Test `org-timestamp-translate' specifications."
  1254. ;; Translate whole date range.
  1255. (should
  1256. (equal "<29>--<30>"
  1257. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1258. (let ((org-display-custom-times t)
  1259. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1260. (org-timestamp-translate (org-element-context))))))
  1261. ;; Translate date range start.
  1262. (should
  1263. (equal "<29>"
  1264. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1265. (let ((org-display-custom-times t)
  1266. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1267. (org-timestamp-translate (org-element-context) 'start)))))
  1268. ;; Translate date range end.
  1269. (should
  1270. (equal "<30>"
  1271. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1272. (let ((org-display-custom-times t)
  1273. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1274. (org-timestamp-translate (org-element-context) 'end)))))
  1275. ;; Translate time range.
  1276. (should
  1277. (equal "<08>--<16>"
  1278. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1279. (let ((org-display-custom-times t)
  1280. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1281. (org-timestamp-translate (org-element-context))))))
  1282. ;; Translate non-range timestamp.
  1283. (should
  1284. (equal "<29>"
  1285. (org-test-with-temp-text "<2012-03-29 Thu>"
  1286. (let ((org-display-custom-times t)
  1287. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1288. (org-timestamp-translate (org-element-context))))))
  1289. ;; Do not change `diary' timestamps.
  1290. (should
  1291. (equal "<%%(org-float t 4 2)>"
  1292. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1293. (let ((org-display-custom-times t)
  1294. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1295. (org-timestamp-translate (org-element-context)))))))
  1296. ;;; Targets and Radio Targets
  1297. (ert-deftest test-org/all-targets ()
  1298. "Test `org-all-targets' specifications."
  1299. ;; Without an argument.
  1300. (should
  1301. (equal '("radio-target" "target")
  1302. (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
  1303. (org-all-targets))))
  1304. (should
  1305. (equal '("radio-target")
  1306. (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
  1307. ;; With argument.
  1308. (should
  1309. (equal '("radio-target")
  1310. (org-test-with-temp-text "<<target>> <<<radio-target>>>"
  1311. (org-all-targets t)))))
  1312. ;;; Visibility
  1313. (ert-deftest test-org/flag-drawer ()
  1314. "Test `org-flag-drawer' specifications."
  1315. ;; Hide drawer.
  1316. (should
  1317. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1318. (org-flag-drawer t)
  1319. (get-char-property (line-end-position) 'invisible)))
  1320. ;; Show drawer.
  1321. (should-not
  1322. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  1323. (org-flag-drawer t)
  1324. (org-flag-drawer nil)
  1325. (get-char-property (line-end-position) 'invisible)))
  1326. ;; Test optional argument.
  1327. (should
  1328. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1329. (let ((drawer (save-excursion (search-forward ":D2")
  1330. (org-element-at-point))))
  1331. (org-flag-drawer t drawer)
  1332. (get-char-property (progn (search-forward ":D2") (line-end-position))
  1333. 'invisible))))
  1334. (should-not
  1335. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  1336. (let ((drawer (save-excursion (search-forward ":D2")
  1337. (org-element-at-point))))
  1338. (org-flag-drawer t drawer)
  1339. (get-char-property (line-end-position) 'invisible))))
  1340. ;; Do not hide fake drawers.
  1341. (should-not
  1342. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  1343. (forward-line 1)
  1344. (org-flag-drawer t)
  1345. (get-char-property (line-end-position) 'invisible)))
  1346. ;; Do not hide incomplete drawers.
  1347. (should-not
  1348. (org-test-with-temp-text ":D:\nparagraph"
  1349. (forward-line 1)
  1350. (org-flag-drawer t)
  1351. (get-char-property (line-end-position) 'invisible))))
  1352. (provide 'test-org)
  1353. ;;; test-org.el ends here