test-org.el 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  1. ;;; test-org.el --- tests for org.el
  2. ;; Copyright (c) David Maus
  3. ;; Authors: David Maus
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Comments:
  16. ;; Template test file for Org-mode tests
  17. ;;; Code:
  18. ;;; Comments
  19. (ert-deftest test-org/toggle-comment ()
  20. "Test `org-toggle-comment' specifications."
  21. ;; Simple headline.
  22. (should
  23. (equal "* Test"
  24. (org-test-with-temp-text "* COMMENT Test"
  25. (org-toggle-comment)
  26. (buffer-string))))
  27. (should
  28. (equal "* COMMENT Test"
  29. (org-test-with-temp-text "* Test"
  30. (org-toggle-comment)
  31. (buffer-string))))
  32. ;; Headline with a regular keyword.
  33. (should
  34. (equal "* TODO Test"
  35. (org-test-with-temp-text "* TODO COMMENT Test"
  36. (org-toggle-comment)
  37. (buffer-string))))
  38. (should
  39. (equal "* TODO COMMENT Test"
  40. (org-test-with-temp-text "* TODO Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. ;; Empty headline.
  44. (should
  45. (equal "* "
  46. (org-test-with-temp-text "* COMMENT"
  47. (org-toggle-comment)
  48. (buffer-string))))
  49. (should
  50. (equal "* COMMENT"
  51. (org-test-with-temp-text "* "
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. ;; Headline with a single keyword.
  55. (should
  56. (equal "* TODO "
  57. (org-test-with-temp-text "* TODO COMMENT"
  58. (org-toggle-comment)
  59. (buffer-string))))
  60. (should
  61. (equal "* TODO COMMENT"
  62. (org-test-with-temp-text "* TODO"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. ;; Headline with a keyword, a priority cookie and contents.
  66. (should
  67. (equal "* TODO [#A] Headline"
  68. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  69. (org-toggle-comment)
  70. (buffer-string))))
  71. (should
  72. (equal "* TODO [#A] COMMENT Headline"
  73. (org-test-with-temp-text "* TODO [#A] Headline"
  74. (org-toggle-comment)
  75. (buffer-string)))))
  76. (ert-deftest test-org/comment-dwim ()
  77. "Test `comment-dwim' behaviour in an Org buffer."
  78. ;; No region selected, no comment on current line and line not
  79. ;; empty: insert comment on line above.
  80. (should
  81. (equal "# \nComment"
  82. (org-test-with-temp-text "Comment"
  83. (progn (call-interactively 'comment-dwim)
  84. (buffer-string)))))
  85. ;; No region selected, no comment on current line and line empty:
  86. ;; insert comment on this line.
  87. (should
  88. (equal "# \nParagraph"
  89. (org-test-with-temp-text "\nParagraph"
  90. (progn (call-interactively 'comment-dwim)
  91. (buffer-string)))))
  92. ;; No region selected, and a comment on this line: indent it.
  93. (should
  94. (equal "* Headline\n # Comment"
  95. (org-test-with-temp-text "* Headline\n# Comment"
  96. (progn (forward-line)
  97. (let ((org-adapt-indentation t))
  98. (call-interactively 'comment-dwim))
  99. (buffer-string)))))
  100. ;; Also recognize single # at column 0 as comments.
  101. (should
  102. (equal "# Comment"
  103. (org-test-with-temp-text "# Comment"
  104. (progn (forward-line)
  105. (call-interactively 'comment-dwim)
  106. (buffer-string)))))
  107. ;; Region selected and only comments and blank lines within it:
  108. ;; un-comment all commented lines.
  109. (should
  110. (equal "Comment 1\n\nComment 2"
  111. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  112. (progn
  113. (transient-mark-mode 1)
  114. (push-mark (point) t t)
  115. (goto-char (point-max))
  116. (call-interactively 'comment-dwim)
  117. (buffer-string)))))
  118. ;; Region selected without comments: comment all lines if
  119. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  120. (should
  121. (equal "# Comment 1\n\n# Comment 2"
  122. (org-test-with-temp-text "Comment 1\n\nComment 2"
  123. (progn
  124. (transient-mark-mode 1)
  125. (push-mark (point) t t)
  126. (goto-char (point-max))
  127. (let ((comment-empty-lines nil))
  128. (call-interactively 'comment-dwim))
  129. (buffer-string)))))
  130. (should
  131. (equal "# Comment 1\n# \n# Comment 2"
  132. (org-test-with-temp-text "Comment 1\n\nComment 2"
  133. (progn
  134. (transient-mark-mode 1)
  135. (push-mark (point) t t)
  136. (goto-char (point-max))
  137. (let ((comment-empty-lines t))
  138. (call-interactively 'comment-dwim))
  139. (buffer-string)))))
  140. ;; In front of a keyword without region, insert a new comment.
  141. (should
  142. (equal "# \n#+KEYWORD: value"
  143. (org-test-with-temp-text "#+KEYWORD: value"
  144. (progn (call-interactively 'comment-dwim)
  145. (buffer-string)))))
  146. ;; In a source block, use appropriate syntax.
  147. (should
  148. (equal " ;; "
  149. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
  150. (forward-line)
  151. (let ((org-edit-src-content-indentation 2))
  152. (call-interactively 'comment-dwim))
  153. (buffer-substring-no-properties (line-beginning-position) (point)))))
  154. (should
  155. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  156. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
  157. (forward-line)
  158. (transient-mark-mode 1)
  159. (push-mark (point) t t)
  160. (forward-line 2)
  161. (let ((org-edit-src-content-indentation 2))
  162. (call-interactively 'comment-dwim))
  163. (buffer-string)))))
  164. ;;; Date and time analysis
  165. (ert-deftest test-org/org-read-date ()
  166. "Test `org-read-date' specifications."
  167. ;; Parse ISO date with abbreviated year and month.
  168. (should (equal "2012-03-29 16:40"
  169. (let ((org-time-was-given t))
  170. (org-read-date t nil "12-3-29 16:40"))))
  171. ;; Parse Europeans dates.
  172. (should (equal "2012-03-29 16:40"
  173. (let ((org-time-was-given t))
  174. (org-read-date t nil "29.03.2012 16:40"))))
  175. ;; Parse Europeans dates without year.
  176. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  177. (let ((org-time-was-given t))
  178. (org-read-date t nil "29.03. 16:40")))))
  179. (ert-deftest test-org/org-parse-time-string ()
  180. "Test `org-parse-time-string'."
  181. (should (equal (org-parse-time-string "2012-03-29 16:40")
  182. '(0 40 16 29 3 2012 nil nil nil)))
  183. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  184. '(0 40 16 29 3 2012 nil nil nil)))
  185. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  186. '(0 40 16 29 3 2012 nil nil nil)))
  187. (should (equal (org-parse-time-string "<2012-03-29>")
  188. '(0 0 0 29 3 2012 nil nil nil)))
  189. (should (equal (org-parse-time-string "<2012-03-29>" t)
  190. '(0 nil nil 29 3 2012 nil nil nil))))
  191. ;;; Drawers
  192. (ert-deftest test-org/insert-property-drawer ()
  193. "Test `org-insert-property-drawer' specifications."
  194. ;; Error before first headline.
  195. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  196. ;; Insert drawer right after headline if there is no planning line,
  197. ;; or after it otherwise.
  198. (should
  199. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  200. (org-test-with-temp-text "* H\nParagraph<point>"
  201. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  202. (buffer-string))))
  203. (should
  204. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  205. (org-test-with-temp-text
  206. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  207. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  208. (buffer-string))))
  209. ;; Indent inserted drawer.
  210. (should
  211. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  212. (org-test-with-temp-text "* H\nParagraph<point>"
  213. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  214. (buffer-string))))
  215. ;; Handle insertion at eob.
  216. (should
  217. (equal "* H\n:PROPERTIES:\n:END:\n"
  218. (org-test-with-temp-text "* H"
  219. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  220. (buffer-string))))
  221. ;; Skip inlinetasks before point.
  222. (when (featurep 'org-inlinetask)
  223. (should
  224. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  225. (org-test-with-temp-text
  226. "* H\n*************** I\n*************** END\nP<point>"
  227. (let ((org-adapt-indentation nil)
  228. (org-inlinetask-min-level 15))
  229. (org-insert-property-drawer))
  230. (buffer-string)))))
  231. ;; Correctly set drawer in an inlinetask.
  232. (when (featurep 'org-inlinetask)
  233. (should
  234. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  235. (org-test-with-temp-text
  236. "* H\n*************** I\nP<point>\n*************** END"
  237. (let ((org-adapt-indentation nil)
  238. (org-inlinetask-min-level 15))
  239. (org-insert-property-drawer))
  240. (buffer-string))))))
  241. ;;; Filling
  242. (ert-deftest test-org/fill-paragraph ()
  243. "Test `org-fill-paragraph' specifications."
  244. ;; At an Org table, align it.
  245. (should
  246. (equal "| a |\n"
  247. (org-test-with-temp-text "|a|"
  248. (org-fill-paragraph)
  249. (buffer-string))))
  250. (should
  251. (equal "#+name: table\n| a |\n"
  252. (org-test-with-temp-text "#+name: table\n| a |"
  253. (org-fill-paragraph)
  254. (buffer-string))))
  255. ;; At a paragraph, preserve line breaks.
  256. (org-test-with-temp-text "some \\\\\nlong\ntext"
  257. (let ((fill-column 20))
  258. (org-fill-paragraph)
  259. (should (equal (buffer-string) "some \\\\\nlong text"))))
  260. ;; Correctly fill a paragraph when point is at its very end.
  261. (should
  262. (equal "A B"
  263. (org-test-with-temp-text "A\nB"
  264. (let ((fill-column 20))
  265. (goto-char (point-max))
  266. (org-fill-paragraph)
  267. (buffer-string)))))
  268. ;; Correctly fill the last paragraph of a greater element.
  269. (should
  270. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  271. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  272. (let ((fill-column 8))
  273. (forward-line)
  274. (end-of-line)
  275. (org-fill-paragraph)
  276. (buffer-string)))))
  277. ;; Correctly fill an element in a narrowed buffer.
  278. (should
  279. (equal "01234\n6"
  280. (org-test-with-temp-text "01234 6789"
  281. (let ((fill-column 5))
  282. (narrow-to-region 1 8)
  283. (org-fill-paragraph)
  284. (buffer-string)))))
  285. ;; Handle `adaptive-fill-regexp' in paragraphs.
  286. (should
  287. (equal "> a b"
  288. (org-test-with-temp-text "> a\n> b"
  289. (let ((fill-column 5)
  290. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  291. (org-fill-paragraph)
  292. (buffer-string)))))
  293. ;; Special case: Fill first paragraph when point is at an item or
  294. ;; a plain-list or a footnote reference.
  295. (should
  296. (equal "- A B"
  297. (org-test-with-temp-text "- A\n B"
  298. (let ((fill-column 20))
  299. (org-fill-paragraph)
  300. (buffer-string)))))
  301. (should
  302. (equal "[fn:1] A B"
  303. (org-test-with-temp-text "[fn:1] A\nB"
  304. (let ((fill-column 20))
  305. (org-fill-paragraph)
  306. (buffer-string)))))
  307. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  308. (let ((fill-column 20))
  309. (org-fill-paragraph)
  310. (should (equal (buffer-string)
  311. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  312. ;; Fill contents of `comment-block' elements.
  313. (should
  314. (equal
  315. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  316. (let ((fill-column 20))
  317. (forward-line)
  318. (org-fill-paragraph)
  319. (buffer-string)))
  320. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  321. ;; Fill `comment' elements.
  322. (should
  323. (equal " # A B"
  324. (org-test-with-temp-text " # A\n # B"
  325. (let ((fill-column 20))
  326. (org-fill-paragraph)
  327. (buffer-string)))))
  328. ;; Do not mix consecutive comments when filling one of them.
  329. (should
  330. (equal "# A B\n\n# C"
  331. (org-test-with-temp-text "# A\n# B\n\n# C"
  332. (let ((fill-column 20))
  333. (org-fill-paragraph)
  334. (buffer-string)))))
  335. ;; Use commented empty lines as separators when filling comments.
  336. (should
  337. (equal "# A B\n#\n# C"
  338. (org-test-with-temp-text "# A\n# B\n#\n# C"
  339. (let ((fill-column 20))
  340. (org-fill-paragraph)
  341. (buffer-string)))))
  342. ;; Handle `adaptive-fill-regexp' in comments.
  343. (should
  344. (equal "# > a b"
  345. (org-test-with-temp-text "# > a\n# > b"
  346. (let ((fill-column 20)
  347. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  348. (org-fill-paragraph)
  349. (buffer-string)))))
  350. ;; Do nothing at affiliated keywords.
  351. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  352. (let ((fill-column 20))
  353. (org-fill-paragraph)
  354. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  355. ;; Do not move point after table when filling a table.
  356. (should-not
  357. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  358. (forward-char)
  359. (org-fill-paragraph)
  360. (eobp))))
  361. (ert-deftest test-org/auto-fill-function ()
  362. "Test auto-filling features."
  363. ;; Auto fill paragraph.
  364. (should
  365. (equal "12345\n7890"
  366. (org-test-with-temp-text "12345 7890"
  367. (let ((fill-column 5))
  368. (end-of-line)
  369. (org-auto-fill-function)
  370. (buffer-string)))))
  371. ;; Auto fill first paragraph in an item.
  372. (should
  373. (equal "- 12345\n 7890"
  374. (org-test-with-temp-text "- 12345 7890"
  375. (let ((fill-column 7))
  376. (end-of-line)
  377. (org-auto-fill-function)
  378. (buffer-string)))))
  379. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  380. (should
  381. (equal "> 12345\n 7890"
  382. (org-test-with-temp-text "> 12345 7890"
  383. (let ((fill-column 10)
  384. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  385. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  386. (end-of-line)
  387. (org-auto-fill-function)
  388. (buffer-string)))))
  389. (should
  390. (equal "> 12345\n> 12345\n> 7890"
  391. (org-test-with-temp-text "> 12345\n> 12345 7890"
  392. (let ((fill-column 10)
  393. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  394. (goto-char (point-max))
  395. (org-auto-fill-function)
  396. (buffer-string)))))
  397. (should-not
  398. (equal " 12345\n *12345\n *12345"
  399. (org-test-with-temp-text " 12345\n *12345 12345"
  400. (let ((fill-column 10)
  401. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  402. (goto-char (point-max))
  403. (org-auto-fill-function)
  404. (buffer-string)))))
  405. ;; Auto fill comments.
  406. (should
  407. (equal " # 12345\n # 7890"
  408. (org-test-with-temp-text " # 12345 7890"
  409. (let ((fill-column 10))
  410. (end-of-line)
  411. (org-auto-fill-function)
  412. (buffer-string)))))
  413. ;; A hash within a line isn't a comment.
  414. (should-not
  415. (equal "12345 # 7890\n# 1"
  416. (org-test-with-temp-text "12345 # 7890 1"
  417. (let ((fill-column 12))
  418. (end-of-line)
  419. (org-auto-fill-function)
  420. (buffer-string)))))
  421. ;; Correctly interpret empty prefix.
  422. (should-not
  423. (equal "# a\n# b\nRegular\n# paragraph"
  424. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  425. (let ((fill-column 12))
  426. (end-of-line 3)
  427. (org-auto-fill-function)
  428. (buffer-string)))))
  429. ;; Comment block: auto fill contents.
  430. (should
  431. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  432. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  433. (let ((fill-column 5))
  434. (forward-line)
  435. (end-of-line)
  436. (org-auto-fill-function)
  437. (buffer-string)))))
  438. (should
  439. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  440. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  441. (let ((fill-column 5))
  442. (forward-line)
  443. (end-of-line)
  444. (org-auto-fill-function)
  445. (buffer-string)))))
  446. ;; Do not fill if a new item could be created.
  447. (should-not
  448. (equal "12345\n- 90"
  449. (org-test-with-temp-text "12345 - 90"
  450. (let ((fill-column 5))
  451. (end-of-line)
  452. (org-auto-fill-function)
  453. (buffer-string)))))
  454. ;; Do not fill if a line break could be introduced.
  455. (should-not
  456. (equal "123\\\\\n7890"
  457. (org-test-with-temp-text "123\\\\ 7890"
  458. (let ((fill-column 6))
  459. (end-of-line)
  460. (org-auto-fill-function)
  461. (buffer-string)))))
  462. ;; Do not fill affiliated keywords.
  463. (should-not
  464. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  465. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  466. (let ((fill-column 20))
  467. (end-of-line)
  468. (org-auto-fill-function)
  469. (buffer-string))))))
  470. ;;; Indentation
  471. (ert-deftest test-org/indent-line ()
  472. "Test `org-indent-line' specifications."
  473. ;; Do not indent footnote definitions or headlines.
  474. (should
  475. (zerop
  476. (org-test-with-temp-text "* H"
  477. (org-indent-line)
  478. (org-get-indentation))))
  479. (should
  480. (zerop
  481. (org-test-with-temp-text "[fn:1] fn"
  482. (let ((org-adapt-indentation t)) (org-indent-line))
  483. (org-get-indentation))))
  484. ;; Do not indent before first headline.
  485. (should
  486. (zerop
  487. (org-test-with-temp-text ""
  488. (org-indent-line)
  489. (org-get-indentation))))
  490. ;; Indent according to headline level otherwise, unless
  491. ;; `org-adapt-indentation' is nil.
  492. (should
  493. (= 2
  494. (org-test-with-temp-text "* H\nA"
  495. (forward-line)
  496. (let ((org-adapt-indentation t)) (org-indent-line))
  497. (org-get-indentation))))
  498. (should
  499. (= 2
  500. (org-test-with-temp-text "* H\n\nA"
  501. (forward-line)
  502. (let ((org-adapt-indentation t)) (org-indent-line))
  503. (org-get-indentation))))
  504. (should
  505. (zerop
  506. (org-test-with-temp-text "* H\nA"
  507. (forward-line)
  508. (let ((org-adapt-indentation nil)) (org-indent-line))
  509. (org-get-indentation))))
  510. ;; Indenting preserves point position.
  511. (should
  512. (org-test-with-temp-text "* H\nAB"
  513. (forward-line)
  514. (forward-char)
  515. (let ((org-adapt-indentation t)) (org-indent-line))
  516. (looking-at "B")))
  517. ;; Do not change indentation at an item.
  518. (should
  519. (= 1
  520. (org-test-with-temp-text "* H\n - A"
  521. (forward-line)
  522. (let ((org-adapt-indentation t)) (org-indent-line))
  523. (org-get-indentation))))
  524. ;; On blank lines at the end of a list, indent like last element
  525. ;; within it if the line is still in the list. Otherwise, indent
  526. ;; like the whole list.
  527. (should
  528. (= 4
  529. (org-test-with-temp-text "* H\n- A\n - AA\n"
  530. (goto-char (point-max))
  531. (let ((org-adapt-indentation t)) (org-indent-line))
  532. (org-get-indentation))))
  533. (should
  534. (zerop
  535. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
  536. (goto-char (point-max))
  537. (let ((org-adapt-indentation t)) (org-indent-line))
  538. (org-get-indentation))))
  539. ;; Likewise, on a blank line at the end of a footnote definition,
  540. ;; indent at column 0 if line belongs to the definition. Otherwise,
  541. ;; indent like the definition itself.
  542. (should
  543. (zerop
  544. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  545. (goto-char (point-max))
  546. (let ((org-adapt-indentation t)) (org-indent-line))
  547. (org-get-indentation))))
  548. (should
  549. (zerop
  550. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  551. (goto-char (point-max))
  552. (let ((org-adapt-indentation t)) (org-indent-line))
  553. (org-get-indentation))))
  554. ;; After the end of the contents of a greater element, indent like
  555. ;; the beginning of the element.
  556. (should
  557. (= 1
  558. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  559. (forward-line 2)
  560. (org-indent-line)
  561. (org-get-indentation))))
  562. ;; At the first line of an element, indent like previous element's
  563. ;; first line, ignoring footnotes definitions and inline tasks, or
  564. ;; according to parent.
  565. (should
  566. (= 2
  567. (org-test-with-temp-text "A\n\n B\n\nC"
  568. (goto-char (point-max))
  569. (org-indent-line)
  570. (org-get-indentation))))
  571. (should
  572. (= 1
  573. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  574. (goto-char (point-max))
  575. (org-indent-line)
  576. (org-get-indentation))))
  577. (should
  578. (= 1
  579. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  580. (forward-line 1)
  581. (org-indent-line)
  582. (org-get-indentation))))
  583. ;; Within code part of a source block, use language major mode if
  584. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  585. ;; according to line above.
  586. (should
  587. (= 6
  588. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  589. (forward-line 2)
  590. (let ((org-src-tab-acts-natively t)
  591. (org-edit-src-content-indentation 0))
  592. (org-indent-line))
  593. (org-get-indentation))))
  594. (should
  595. (= 1
  596. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  597. (forward-line 2)
  598. (let ((org-src-tab-acts-natively nil)
  599. (org-edit-src-content-indentation 0))
  600. (org-indent-line))
  601. (org-get-indentation))))
  602. ;; Otherwise, indent like the first non-blank line above.
  603. (should
  604. (zerop
  605. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  606. (forward-line 3)
  607. (org-indent-line)
  608. (org-get-indentation))))
  609. ;; Align node properties according to `org-property-format'. Handle
  610. ;; nicely empty values.
  611. (should
  612. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  613. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  614. (let ((org-property-format "%-10s %s")) (org-indent-line))
  615. (buffer-string))))
  616. (should
  617. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  618. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  619. (let ((org-property-format "%-10s %s")) (org-indent-line))
  620. (buffer-string)))))
  621. (ert-deftest test-org/indent-region ()
  622. "Test `org-indent-region' specifications."
  623. ;; Indent paragraph.
  624. (should
  625. (equal "A\nB\nC"
  626. (org-test-with-temp-text " A\nB\n C"
  627. (org-indent-region (point-min) (point-max))
  628. (buffer-string))))
  629. ;; Indent greater elements along with their contents.
  630. (should
  631. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  632. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  633. (org-indent-region (point-min) (point-max))
  634. (buffer-string))))
  635. ;; Ignore contents of verse blocks and example blocks.
  636. (should
  637. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  638. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  639. (org-indent-region (point-min) (point-max))
  640. (buffer-string))))
  641. (should
  642. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  643. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  644. (org-indent-region (point-min) (point-max))
  645. (buffer-string))))
  646. ;; Indent according to mode if `org-src-tab-acts-natively' is
  647. ;; non-nil. Otherwise, do not indent code at all.
  648. (should
  649. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  650. (org-test-with-temp-text
  651. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  652. (let ((org-src-tab-acts-natively t)
  653. (org-edit-src-content-indentation 0))
  654. (org-indent-region (point-min) (point-max)))
  655. (buffer-string))))
  656. (should
  657. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  658. (org-test-with-temp-text
  659. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  660. (let ((org-src-tab-acts-natively nil)
  661. (org-edit-src-content-indentation 0))
  662. (org-indent-region (point-min) (point-max)))
  663. (buffer-string))))
  664. ;; Align node properties according to `org-property-format'. Handle
  665. ;; nicely empty values.
  666. (should
  667. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  668. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  669. (let ((org-property-format "%-10s %s")
  670. (org-adapt-indentation nil))
  671. (org-indent-region (point) (point-max)))
  672. (buffer-string))))
  673. (should
  674. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  675. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  676. (let ((org-property-format "%-10s %s")
  677. (org-adapt-indentation nil))
  678. (org-indent-region (point) (point-max)))
  679. (buffer-string))))
  680. ;; Indent plain lists.
  681. (should
  682. (equal "- A\n B\n - C\n\n D"
  683. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  684. (org-indent-region (point-min) (point-max))
  685. (buffer-string))))
  686. (should
  687. (equal "- A\n\n- B"
  688. (org-test-with-temp-text " - A\n\n - B"
  689. (org-indent-region (point-min) (point-max))
  690. (buffer-string))))
  691. ;; Indent footnote definitions.
  692. (should
  693. (equal "[fn:1] Definition\n\nDefinition"
  694. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  695. (org-indent-region (point-min) (point-max))
  696. (buffer-string))))
  697. ;; Special case: Start indenting on a blank line.
  698. (should
  699. (equal "\nParagraph"
  700. (org-test-with-temp-text "\n Paragraph"
  701. (org-indent-region (point-min) (point-max))
  702. (buffer-string)))))
  703. ;;; Editing
  704. ;;;; Insert elements
  705. (ert-deftest test-org/meta-return ()
  706. "Test M-RET (`org-meta-return')."
  707. ;; In a table field insert a row above.
  708. (should
  709. (org-test-with-temp-text "| a |"
  710. (forward-char)
  711. (org-meta-return)
  712. (forward-line -1)
  713. (looking-at "| |$")))
  714. ;; In a paragraph change current line into a header.
  715. (should
  716. (org-test-with-temp-text "a"
  717. (org-meta-return)
  718. (beginning-of-line)
  719. (looking-at "\* a$")))
  720. ;; In an item insert an item, in this case above.
  721. (should
  722. (org-test-with-temp-text "- a"
  723. (org-meta-return)
  724. (beginning-of-line)
  725. (looking-at "- $")))
  726. ;; In a drawer and item insert an item, in this case above.
  727. (should
  728. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  729. (forward-line)
  730. (org-meta-return)
  731. (beginning-of-line)
  732. (looking-at "- $"))))
  733. (ert-deftest test-org/insert-heading ()
  734. "Test `org-insert-heading' specifications."
  735. ;; FIXME: Test coverage is incomplete yet.
  736. ;;
  737. ;; In an empty buffer, insert a new headline.
  738. (should
  739. (equal "* "
  740. (org-test-with-temp-text ""
  741. (org-insert-heading)
  742. (buffer-string))))
  743. ;; At the beginning of a line, turn it into a headline
  744. (should
  745. (equal "* P"
  746. (org-test-with-temp-text "<point>P"
  747. (org-insert-heading)
  748. (buffer-string))))
  749. ;; In the middle of a line, split the line if allowed, otherwise,
  750. ;; insert the headline at its end.
  751. (should
  752. (equal "Para\n* graph"
  753. (org-test-with-temp-text "Para<point>graph"
  754. (let ((org-M-RET-may-split-line '((default . t))))
  755. (org-insert-heading))
  756. (buffer-string))))
  757. (should
  758. (equal "Paragraph\n* "
  759. (org-test-with-temp-text "Para<point>graph"
  760. (let ((org-M-RET-may-split-line '((default . nil))))
  761. (org-insert-heading))
  762. (buffer-string))))
  763. ;; Corner case: correctly insert a headline after an empty one.
  764. (should
  765. (equal "* \n* "
  766. (org-test-with-temp-text "* <point>"
  767. (org-insert-heading)
  768. (buffer-string)))))
  769. (ert-deftest test-org/insert-todo-heading-respect-content ()
  770. "Test `org-insert-todo-heading-respect-content' specifications."
  771. ;; Create a TODO heading.
  772. (should
  773. (org-test-with-temp-text "* H1\n Body"
  774. (org-insert-todo-heading-respect-content)
  775. (nth 2 (org-heading-components))))
  776. ;; Add headline at the end of the first subtree
  777. (should
  778. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  779. (search-forward "H1Body")
  780. (org-insert-todo-heading-respect-content)
  781. (and (eobp) (org-at-heading-p))))
  782. ;; In a list, do not create a new item.
  783. (should
  784. (org-test-with-temp-text "* H\n- an item\n- another one"
  785. (search-forward "an ")
  786. (org-insert-todo-heading-respect-content)
  787. (and (eobp) (org-at-heading-p)))))
  788. ;;; Fixed-Width Areas
  789. (ert-deftest test-org/toggle-fixed-width ()
  790. "Test `org-toggle-fixed-width' specifications."
  791. ;; No region: Toggle on fixed-width marker in paragraphs.
  792. (should
  793. (equal ": A"
  794. (org-test-with-temp-text "A"
  795. (org-toggle-fixed-width)
  796. (buffer-string))))
  797. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  798. (should
  799. (equal "A"
  800. (org-test-with-temp-text ": A"
  801. (org-toggle-fixed-width)
  802. (buffer-string))))
  803. ;; No region: Toggle on marker in blank lines after elements or just
  804. ;; after a headline.
  805. (should
  806. (equal "* H\n: "
  807. (org-test-with-temp-text "* H\n"
  808. (forward-line)
  809. (org-toggle-fixed-width)
  810. (buffer-string))))
  811. (should
  812. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  813. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  814. (goto-char (point-max))
  815. (org-toggle-fixed-width)
  816. (buffer-string))))
  817. ;; No region: Toggle on marker in front of one line elements (e.g.,
  818. ;; headlines, clocks)
  819. (should
  820. (equal ": * Headline"
  821. (org-test-with-temp-text "* Headline"
  822. (org-toggle-fixed-width)
  823. (buffer-string))))
  824. (should
  825. (equal ": #+KEYWORD: value"
  826. (org-test-with-temp-text "#+KEYWORD: value"
  827. (org-toggle-fixed-width)
  828. (buffer-string))))
  829. ;; No region: error in other situations.
  830. (should-error
  831. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  832. (forward-line)
  833. (org-toggle-fixed-width)
  834. (buffer-string)))
  835. ;; No region: Indentation is preserved.
  836. (should
  837. (equal "- A\n : B"
  838. (org-test-with-temp-text "- A\n B"
  839. (forward-line)
  840. (org-toggle-fixed-width)
  841. (buffer-string))))
  842. ;; Region: If it contains only fixed-width elements and blank lines,
  843. ;; toggle off fixed-width markup.
  844. (should
  845. (equal "A\n\nB"
  846. (org-test-with-temp-text ": A\n\n: B"
  847. (transient-mark-mode 1)
  848. (push-mark (point) t t)
  849. (goto-char (point-max))
  850. (org-toggle-fixed-width)
  851. (buffer-string))))
  852. ;; Region: If it contains anything else, toggle on fixed-width but
  853. ;; not on fixed-width areas.
  854. (should
  855. (equal ": A\n: \n: B\n: \n: C"
  856. (org-test-with-temp-text "A\n\n: B\n\nC"
  857. (transient-mark-mode 1)
  858. (push-mark (point) t t)
  859. (goto-char (point-max))
  860. (org-toggle-fixed-width)
  861. (buffer-string))))
  862. ;; Region: Ignore blank lines at its end, unless it contains only
  863. ;; such lines.
  864. (should
  865. (equal ": A\n\n"
  866. (org-test-with-temp-text "A\n\n"
  867. (transient-mark-mode 1)
  868. (push-mark (point) t t)
  869. (goto-char (point-max))
  870. (org-toggle-fixed-width)
  871. (buffer-string))))
  872. (should
  873. (equal ": \n: \n"
  874. (org-test-with-temp-text "\n\n"
  875. (transient-mark-mode 1)
  876. (push-mark (point) t t)
  877. (goto-char (point-max))
  878. (org-toggle-fixed-width)
  879. (buffer-string)))))
  880. ;;; Headline
  881. (ert-deftest test-org/in-commented-heading-p ()
  882. "Test `org-in-commented-heading-p' specifications."
  883. ;; Commented headline.
  884. (should
  885. (org-test-with-temp-text "* COMMENT Headline\nBody"
  886. (goto-char (point-max))
  887. (org-in-commented-heading-p)))
  888. ;; Commented ancestor.
  889. (should
  890. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  891. (goto-char (point-max))
  892. (org-in-commented-heading-p)))
  893. ;; Comment keyword is case-sensitive.
  894. (should-not
  895. (org-test-with-temp-text "* Comment Headline\nBody"
  896. (goto-char (point-max))
  897. (org-in-commented-heading-p)))
  898. ;; Keyword is standalone.
  899. (should-not
  900. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  901. (goto-char (point-max))
  902. (org-in-commented-heading-p)))
  903. ;; Optional argument.
  904. (should-not
  905. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  906. (goto-char (point-max))
  907. (org-in-commented-heading-p t))))
  908. ;;; Links
  909. ;;;; Coderefs
  910. (ert-deftest test-org/coderef ()
  911. "Test coderef links specifications."
  912. (should
  913. (org-test-with-temp-text "
  914. #+BEGIN_SRC emacs-lisp
  915. \(+ 1 1) (ref:sc)
  916. #+END_SRC
  917. \[[(sc)]]"
  918. (goto-char (point-max))
  919. (org-open-at-point)
  920. (looking-at "(ref:sc)"))))
  921. ;;;; Custom ID
  922. (ert-deftest test-org/custom-id ()
  923. "Test custom ID links specifications."
  924. (should
  925. (org-test-with-temp-text
  926. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  927. (goto-char (point-max))
  928. (org-open-at-point)
  929. (org-looking-at-p "\\* H1"))))
  930. ;;;; Fuzzy Links
  931. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  932. ;; a named element (#+name: text) and to headlines (* Text).
  933. (ert-deftest test-org/fuzzy-links ()
  934. "Test fuzzy links specifications."
  935. ;; Fuzzy link goes in priority to a matching target.
  936. (should
  937. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  938. (goto-line 5)
  939. (org-open-at-point)
  940. (looking-at "<<Test>>")))
  941. ;; Then fuzzy link points to an element with a given name.
  942. (should
  943. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  944. (goto-line 5)
  945. (org-open-at-point)
  946. (looking-at "#\\+NAME: Test")))
  947. ;; A target still lead to a matching headline otherwise.
  948. (should
  949. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  950. (goto-line 4)
  951. (org-open-at-point)
  952. (looking-at "\\* Head2")))
  953. ;; With a leading star in link, enforce heading match.
  954. (should
  955. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  956. (goto-line 3)
  957. (org-open-at-point)
  958. (looking-at "\\* Test")))
  959. ;; Correctly un-hexify fuzzy links.
  960. (should
  961. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  962. (goto-char (point-max))
  963. (org-open-at-point)
  964. (bobp))))
  965. ;;;; Link Escaping
  966. (ert-deftest test-org/org-link-escape-ascii-character ()
  967. "Escape an ascii character."
  968. (should
  969. (string=
  970. "%5B"
  971. (org-link-escape "["))))
  972. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  973. "Escape an ascii control character."
  974. (should
  975. (string=
  976. "%09"
  977. (org-link-escape "\t"))))
  978. (ert-deftest test-org/org-link-escape-multibyte-character ()
  979. "Escape an unicode multibyte character."
  980. (should
  981. (string=
  982. "%E2%82%AC"
  983. (org-link-escape "€"))))
  984. (ert-deftest test-org/org-link-escape-custom-table ()
  985. "Escape string with custom character table."
  986. (should
  987. (string=
  988. "Foo%3A%42ar%0A"
  989. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  990. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  991. "Escape string with custom table merged with default table."
  992. (should
  993. (string=
  994. "%5BF%6F%6F%3A%42ar%0A%5D"
  995. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  996. (ert-deftest test-org/org-link-unescape-ascii-character ()
  997. "Unescape an ascii character."
  998. (should
  999. (string=
  1000. "["
  1001. (org-link-unescape "%5B"))))
  1002. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  1003. "Unescpae an ascii control character."
  1004. (should
  1005. (string=
  1006. "\n"
  1007. (org-link-unescape "%0A"))))
  1008. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  1009. "Unescape unicode multibyte character."
  1010. (should
  1011. (string=
  1012. "€"
  1013. (org-link-unescape "%E2%82%AC"))))
  1014. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  1015. "Unescape old style percent escaped character."
  1016. (should
  1017. (string=
  1018. "àâçèéêîôùû"
  1019. (decode-coding-string
  1020. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  1021. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  1022. "Escape and unescape a URL that includes an escaped char.
  1023. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  1024. (should
  1025. (string=
  1026. "http://some.host.com/form?&id=blah%2Bblah25"
  1027. (org-link-unescape
  1028. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  1029. (ert-deftest test-org/org-link-escape-chars-browser ()
  1030. "Test of the constant `org-link-escape-chars-browser'.
  1031. See there why this test is a candidate to be removed once Org
  1032. drops support for Emacs 24.1 and 24.2."
  1033. (should
  1034. (string=
  1035. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1036. "%22Release%208.2%22&idxname=emacs-orgmode")
  1037. (org-link-escape-browser ; Do not replace with `url-encode-url',
  1038. ; see docstring above.
  1039. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1040. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  1041. ;;;; Open at point
  1042. (ert-deftest test-org/open-at-point-in-property ()
  1043. "Does `org-open-at-point' open link in property drawer?"
  1044. (should
  1045. (org-test-with-temp-text
  1046. "* Headline
  1047. :PROPERTIES:
  1048. :URL: <point>[[info:emacs#Top]]
  1049. :END:"
  1050. (org-open-at-point) t)))
  1051. (ert-deftest test-org/open-at-point-in-comment ()
  1052. "Does `org-open-at-point' open link in a commented line?"
  1053. (should
  1054. (org-test-with-temp-text
  1055. "# <point>[[info:emacs#Top]]"
  1056. (org-open-at-point) t)))
  1057. (ert-deftest test-org/open-at-point/info ()
  1058. "Test `org-open-at-point' on info links."
  1059. (should
  1060. (org-test-with-temp-text
  1061. "<point>[[info:emacs#Top]]"
  1062. (org-open-at-point)
  1063. (and (switch-to-buffer "*info*")
  1064. (prog1
  1065. (looking-at "\nThe Emacs Editor")
  1066. (kill-buffer))))))
  1067. ;;; Node Properties
  1068. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1069. "Ensure properties accumulate in subtree drawers."
  1070. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1071. (org-babel-next-src-block)
  1072. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1073. ;;; Mark Region
  1074. (ert-deftest test-org/mark-subtree ()
  1075. "Test `org-mark-subtree' specifications."
  1076. ;; Error when point is before first headline.
  1077. (should-error
  1078. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1079. (progn (transient-mark-mode 1)
  1080. (org-mark-subtree))))
  1081. ;; Without argument, mark current subtree.
  1082. (should
  1083. (equal
  1084. '(12 32)
  1085. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1086. (progn (transient-mark-mode 1)
  1087. (forward-line 2)
  1088. (org-mark-subtree)
  1089. (list (region-beginning) (region-end))))))
  1090. ;; With an argument, move ARG up.
  1091. (should
  1092. (equal
  1093. '(1 32)
  1094. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1095. (progn (transient-mark-mode 1)
  1096. (forward-line 2)
  1097. (org-mark-subtree 1)
  1098. (list (region-beginning) (region-end))))))
  1099. ;; Do not get fooled by inlinetasks.
  1100. (when (featurep 'org-inlinetask)
  1101. (should
  1102. (= 1
  1103. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1104. (progn (transient-mark-mode 1)
  1105. (forward-line 1)
  1106. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1107. (region-beginning)))))))
  1108. ;;; Navigation
  1109. (ert-deftest test-org/beginning-of-line ()
  1110. "Test `org-beginning-of-line' specifications."
  1111. ;; Standard test.
  1112. (should
  1113. (org-test-with-temp-text "Some text\nSome other text"
  1114. (progn (org-beginning-of-line) (bolp))))
  1115. ;; Standard test with `visual-line-mode'.
  1116. (should-not
  1117. (org-test-with-temp-text "A long line of text\nSome other text"
  1118. (progn (visual-line-mode)
  1119. (forward-char 2)
  1120. (dotimes (i 1000) (insert "very "))
  1121. (org-beginning-of-line)
  1122. (bolp))))
  1123. ;; At an headline with special movement.
  1124. (should
  1125. (org-test-with-temp-text "* TODO Headline"
  1126. (let ((org-special-ctrl-a/e t))
  1127. (org-end-of-line)
  1128. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1129. (progn (org-beginning-of-line) (bolp))
  1130. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1131. (ert-deftest test-org/end-of-line ()
  1132. "Test `org-end-of-line' specifications."
  1133. ;; Standard test.
  1134. (should
  1135. (org-test-with-temp-text "Some text\nSome other text"
  1136. (progn (org-end-of-line) (eolp))))
  1137. ;; Standard test with `visual-line-mode'.
  1138. (should-not
  1139. (org-test-with-temp-text "A long line of text\nSome other text"
  1140. (progn (visual-line-mode)
  1141. (forward-char 2)
  1142. (dotimes (i 1000) (insert "very "))
  1143. (goto-char (point-min))
  1144. (org-end-of-line)
  1145. (eolp))))
  1146. ;; At an headline with special movement.
  1147. (should
  1148. (org-test-with-temp-text "* Headline1 :tag:\n"
  1149. (let ((org-special-ctrl-a/e t))
  1150. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1151. (progn (org-end-of-line) (eolp))
  1152. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1153. ;; At an headline without special movement.
  1154. (should
  1155. (org-test-with-temp-text "* Headline2 :tag:\n"
  1156. (let ((org-special-ctrl-a/e nil))
  1157. (and (progn (org-end-of-line) (eolp))
  1158. (progn (org-end-of-line) (eolp))))))
  1159. ;; At an headline, with reversed movement.
  1160. (should
  1161. (org-test-with-temp-text "* Headline3 :tag:\n"
  1162. (let ((org-special-ctrl-a/e 'reversed)
  1163. (this-command last-command))
  1164. (and (progn (org-end-of-line) (eolp))
  1165. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1166. ;; At a block without hidden contents.
  1167. (should
  1168. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1169. (progn (org-end-of-line) (eolp))))
  1170. ;; At a block with hidden contents.
  1171. (should-not
  1172. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1173. (let ((org-special-ctrl-a/e t))
  1174. (org-hide-block-toggle)
  1175. (org-end-of-line)
  1176. (eobp)))))
  1177. (ert-deftest test-org/forward-paragraph ()
  1178. "Test `org-forward-paragraph' specifications."
  1179. ;; At end of buffer, return an error.
  1180. (should-error
  1181. (org-test-with-temp-text "Paragraph"
  1182. (goto-char (point-max))
  1183. (org-forward-paragraph)))
  1184. ;; Standard test.
  1185. (should
  1186. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1187. (org-forward-paragraph)
  1188. (looking-at "P2")))
  1189. ;; Ignore depth.
  1190. (should
  1191. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1192. (org-forward-paragraph)
  1193. (looking-at "P1")))
  1194. ;; Do not enter elements with invisible contents.
  1195. (should
  1196. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1197. (org-hide-block-toggle)
  1198. (org-forward-paragraph)
  1199. (looking-at "P3")))
  1200. ;; On an affiliated keyword, jump to the beginning of the element.
  1201. (should
  1202. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1203. (org-forward-paragraph)
  1204. (looking-at "Para")))
  1205. ;; On an item or a footnote definition, move to the second element
  1206. ;; inside, if any.
  1207. (should
  1208. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1209. (org-forward-paragraph)
  1210. (looking-at " Paragraph")))
  1211. (should
  1212. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1213. (org-forward-paragraph)
  1214. (looking-at "Paragraph")))
  1215. ;; On an item, or a footnote definition, when the first line is
  1216. ;; empty, move to the first item.
  1217. (should
  1218. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1219. (org-forward-paragraph)
  1220. (looking-at " Paragraph")))
  1221. (should
  1222. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1223. (org-forward-paragraph)
  1224. (looking-at "Paragraph")))
  1225. ;; On a table (resp. a property drawer) do not move through table
  1226. ;; rows (resp. node properties).
  1227. (should
  1228. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1229. (org-forward-paragraph)
  1230. (looking-at "Paragraph")))
  1231. (should
  1232. (org-test-with-temp-text
  1233. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1234. (org-forward-paragraph)
  1235. (looking-at "Paragraph")))
  1236. ;; On a verse or source block, stop after blank lines.
  1237. (should
  1238. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1239. (org-forward-paragraph)
  1240. (looking-at "L2")))
  1241. (should
  1242. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1243. (org-forward-paragraph)
  1244. (looking-at "L2"))))
  1245. (ert-deftest test-org/backward-paragraph ()
  1246. "Test `org-backward-paragraph' specifications."
  1247. ;; Error at beginning of buffer.
  1248. (should-error
  1249. (org-test-with-temp-text "Paragraph"
  1250. (org-backward-paragraph)))
  1251. ;; Regular test.
  1252. (should
  1253. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1254. (goto-char (point-max))
  1255. (org-backward-paragraph)
  1256. (looking-at "P3")))
  1257. (should
  1258. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1259. (goto-char (point-max))
  1260. (beginning-of-line)
  1261. (org-backward-paragraph)
  1262. (looking-at "P2")))
  1263. ;; Ignore depth.
  1264. (should
  1265. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1266. (goto-char (point-max))
  1267. (beginning-of-line)
  1268. (org-backward-paragraph)
  1269. (looking-at "P2")))
  1270. ;; Ignore invisible elements.
  1271. (should
  1272. (org-test-with-temp-text "* H1\n P1\n* H2"
  1273. (org-cycle)
  1274. (goto-char (point-max))
  1275. (beginning-of-line)
  1276. (org-backward-paragraph)
  1277. (bobp)))
  1278. ;; On an affiliated keyword, jump to the first one.
  1279. (should
  1280. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1281. (search-forward "c2")
  1282. (org-backward-paragraph)
  1283. (looking-at "#\\+name")))
  1284. ;; On the second element in an item or a footnote definition, jump
  1285. ;; to item or the definition.
  1286. (should
  1287. (org-test-with-temp-text "- line1\n\n line2"
  1288. (goto-char (point-max))
  1289. (beginning-of-line)
  1290. (org-backward-paragraph)
  1291. (looking-at "- line1")))
  1292. (should
  1293. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1294. (goto-char (point-max))
  1295. (beginning-of-line)
  1296. (org-backward-paragraph)
  1297. (looking-at "\\[fn:1\\] line1")))
  1298. ;; On a table (resp. a property drawer), ignore table rows
  1299. ;; (resp. node properties).
  1300. (should
  1301. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1302. (goto-char (point-max))
  1303. (beginning-of-line)
  1304. (org-backward-paragraph)
  1305. (bobp)))
  1306. (should
  1307. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  1308. (org-backward-paragraph)
  1309. (looking-at ":PROPERTIES:")))
  1310. ;; On a source or verse block, stop before blank lines.
  1311. (should
  1312. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1313. (search-forward "L3")
  1314. (beginning-of-line)
  1315. (org-backward-paragraph)
  1316. (looking-at "L2")))
  1317. (should
  1318. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1319. (search-forward "L3")
  1320. (beginning-of-line)
  1321. (org-backward-paragraph)
  1322. (looking-at "L2"))))
  1323. (ert-deftest test-org/forward-element ()
  1324. "Test `org-forward-element' specifications."
  1325. ;; 1. At EOB: should error.
  1326. (org-test-with-temp-text "Some text\n"
  1327. (goto-char (point-max))
  1328. (should-error (org-forward-element)))
  1329. ;; 2. Standard move: expected to ignore blank lines.
  1330. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1331. (org-forward-element)
  1332. (should (looking-at (regexp-quote "Second paragraph."))))
  1333. ;; 3. Headline tests.
  1334. (org-test-with-temp-text "
  1335. * Head 1
  1336. ** Head 1.1
  1337. *** Head 1.1.1
  1338. ** Head 1.2"
  1339. ;; 3.1. At an headline beginning: move to next headline at the
  1340. ;; same level.
  1341. (goto-line 3)
  1342. (org-forward-element)
  1343. (should (looking-at (regexp-quote "** Head 1.2")))
  1344. ;; 3.2. At an headline beginning: move to parent headline if no
  1345. ;; headline at the same level.
  1346. (goto-line 3)
  1347. (org-forward-element)
  1348. (should (looking-at (regexp-quote "** Head 1.2"))))
  1349. ;; 4. Greater element tests.
  1350. (org-test-with-temp-text
  1351. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1352. ;; 4.1. At a greater element: expected to skip contents.
  1353. (org-forward-element)
  1354. (should (looking-at (regexp-quote "Outside.")))
  1355. ;; 4.2. At the end of greater element contents: expected to skip
  1356. ;; to the end of the greater element.
  1357. (goto-line 2)
  1358. (org-forward-element)
  1359. (should (looking-at (regexp-quote "Outside."))))
  1360. ;; 5. List tests.
  1361. (org-test-with-temp-text "
  1362. - item1
  1363. - sub1
  1364. - sub2
  1365. - sub3
  1366. Inner paragraph.
  1367. - item2
  1368. Outside."
  1369. ;; 5.1. At list top point: expected to move to the element after
  1370. ;; the list.
  1371. (goto-line 2)
  1372. (org-forward-element)
  1373. (should (looking-at (regexp-quote "Outside.")))
  1374. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1375. ;; beginning of line, move to next item.
  1376. (goto-line 2)
  1377. (forward-char)
  1378. (org-forward-element)
  1379. (should (looking-at "- item2"))
  1380. (goto-line 4)
  1381. (forward-char)
  1382. (org-forward-element)
  1383. (should (looking-at " - sub2"))
  1384. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1385. (goto-line 4)
  1386. (org-forward-element)
  1387. (should (looking-at (regexp-quote " Inner paragraph.")))
  1388. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1389. (goto-line 8)
  1390. (org-forward-element)
  1391. (should (looking-at (regexp-quote " Inner paragraph.")))
  1392. ;; 5.5. At an item: expected to move to next item, if any.
  1393. (goto-line 6)
  1394. (org-forward-element)
  1395. (should (looking-at " - sub3"))))
  1396. (ert-deftest test-org/backward-element ()
  1397. "Test `org-backward-element' specifications."
  1398. ;; 1. Should error at BOB.
  1399. (org-test-with-temp-text " \nParagraph."
  1400. (should-error (org-backward-element)))
  1401. ;; 2. Should move at BOB when called on the first element in buffer.
  1402. (should
  1403. (org-test-with-temp-text "\n#+TITLE: test"
  1404. (progn (forward-line)
  1405. (org-backward-element)
  1406. (bobp))))
  1407. ;; 3. Not at the beginning of an element: move at its beginning.
  1408. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1409. (goto-line 3)
  1410. (end-of-line)
  1411. (org-backward-element)
  1412. (should (looking-at (regexp-quote "Paragraph2."))))
  1413. ;; 4. Headline tests.
  1414. (org-test-with-temp-text "
  1415. * Head 1
  1416. ** Head 1.1
  1417. *** Head 1.1.1
  1418. ** Head 1.2"
  1419. ;; 4.1. At an headline beginning: move to previous headline at the
  1420. ;; same level.
  1421. (goto-line 5)
  1422. (org-backward-element)
  1423. (should (looking-at (regexp-quote "** Head 1.1")))
  1424. ;; 4.2. At an headline beginning: move to parent headline if no
  1425. ;; headline at the same level.
  1426. (goto-line 3)
  1427. (org-backward-element)
  1428. (should (looking-at (regexp-quote "* Head 1")))
  1429. ;; 4.3. At the first top-level headline: should error.
  1430. (goto-line 2)
  1431. (should-error (org-backward-element)))
  1432. ;; 5. At beginning of first element inside a greater element:
  1433. ;; expected to move to greater element's beginning.
  1434. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1435. (goto-line 3)
  1436. (org-backward-element)
  1437. (should (looking-at "#\\+BEGIN_CENTER")))
  1438. ;; 6. At the beginning of the first element in a section: should
  1439. ;; move back to headline, if any.
  1440. (should
  1441. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1442. (progn (goto-char (point-max))
  1443. (beginning-of-line)
  1444. (org-backward-element)
  1445. (org-at-heading-p))))
  1446. ;; 7. List tests.
  1447. (org-test-with-temp-text "
  1448. - item1
  1449. - sub1
  1450. - sub2
  1451. - sub3
  1452. Inner paragraph.
  1453. - item2
  1454. Outside."
  1455. ;; 7.1. At beginning of sub-list: expected to move to the
  1456. ;; paragraph before it.
  1457. (goto-line 4)
  1458. (org-backward-element)
  1459. (should (looking-at "item1"))
  1460. ;; 7.2. At an item in a list: expected to move at previous item.
  1461. (goto-line 8)
  1462. (org-backward-element)
  1463. (should (looking-at " - sub2"))
  1464. (goto-line 12)
  1465. (org-backward-element)
  1466. (should (looking-at "- item1"))
  1467. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1468. ;; beginning.
  1469. (goto-line 10)
  1470. (org-backward-element)
  1471. (should (looking-at " - sub1"))
  1472. (goto-line 15)
  1473. (org-backward-element)
  1474. (should (looking-at "- item1"))
  1475. ;; 7.4. At blank-lines before list end: expected to move to top
  1476. ;; item.
  1477. (goto-line 14)
  1478. (org-backward-element)
  1479. (should (looking-at "- item1"))))
  1480. (ert-deftest test-org/up-element ()
  1481. "Test `org-up-element' specifications."
  1482. ;; 1. At BOB or with no surrounding element: should error.
  1483. (org-test-with-temp-text "Paragraph."
  1484. (should-error (org-up-element)))
  1485. (org-test-with-temp-text "* Head1\n* Head2"
  1486. (goto-line 2)
  1487. (should-error (org-up-element)))
  1488. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1489. (goto-line 3)
  1490. (should-error (org-up-element)))
  1491. ;; 2. At an headline: move to parent headline.
  1492. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1493. (goto-line 3)
  1494. (org-up-element)
  1495. (should (looking-at "\\* Head1")))
  1496. ;; 3. Inside a greater element: move to greater element beginning.
  1497. (org-test-with-temp-text
  1498. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1499. (goto-line 3)
  1500. (org-up-element)
  1501. (should (looking-at "#\\+BEGIN_CENTER")))
  1502. ;; 4. List tests.
  1503. (org-test-with-temp-text "* Top
  1504. - item1
  1505. - sub1
  1506. - sub2
  1507. Paragraph within sub2.
  1508. - item2"
  1509. ;; 4.1. Within an item: move to the item beginning.
  1510. (goto-line 8)
  1511. (org-up-element)
  1512. (should (looking-at " - sub2"))
  1513. ;; 4.2. At an item in a sub-list: move to parent item.
  1514. (goto-line 4)
  1515. (org-up-element)
  1516. (should (looking-at "- item1"))
  1517. ;; 4.3. At an item in top list: move to beginning of whole list.
  1518. (goto-line 10)
  1519. (org-up-element)
  1520. (should (looking-at "- item1"))
  1521. ;; 4.4. Special case. At very top point: should move to parent of
  1522. ;; list.
  1523. (goto-line 2)
  1524. (org-up-element)
  1525. (should (looking-at "\\* Top"))))
  1526. (ert-deftest test-org/down-element ()
  1527. "Test `org-down-element' specifications."
  1528. ;; Error when the element hasn't got a recursive type.
  1529. (org-test-with-temp-text "Paragraph."
  1530. (should-error (org-down-element)))
  1531. ;; Error when the element has no contents
  1532. (org-test-with-temp-text "* Headline"
  1533. (should-error (org-down-element)))
  1534. ;; When at a plain-list, move to first item.
  1535. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1536. (goto-line 2)
  1537. (org-down-element)
  1538. (should (looking-at " - Item 1.1")))
  1539. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1540. (org-down-element)
  1541. (should (looking-at " Item 1")))
  1542. ;; When at a table, move to first row
  1543. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1544. (org-down-element)
  1545. (should (looking-at " a | b |")))
  1546. ;; Otherwise, move inside the greater element.
  1547. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1548. (org-down-element)
  1549. (should (looking-at "Paragraph"))))
  1550. (ert-deftest test-org/drag-element-backward ()
  1551. "Test `org-drag-element-backward' specifications."
  1552. ;; Standard test.
  1553. (should
  1554. (equal
  1555. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  1556. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  1557. (org-drag-element-backward)
  1558. (buffer-string))))
  1559. (should
  1560. (equal
  1561. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  1562. (org-test-with-temp-text
  1563. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  1564. (org-drag-element-backward)
  1565. (buffer-string))))
  1566. ;; Preserve blank lines.
  1567. (should
  1568. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  1569. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  1570. (org-drag-element-backward)
  1571. (buffer-string))))
  1572. ;; Preserve column.
  1573. (should
  1574. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  1575. (org-drag-element-backward)
  1576. (org-looking-at-p "2")))
  1577. ;; Error when trying to move first element of buffer.
  1578. (should-error
  1579. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1580. (org-drag-element-backward)))
  1581. ;; Error when trying to swap nested elements.
  1582. (should-error
  1583. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1584. (forward-line)
  1585. (org-drag-element-backward)))
  1586. ;; Error when trying to swap an headline element and a non-headline
  1587. ;; element.
  1588. (should-error
  1589. (org-test-with-temp-text "Test.\n* Head 1"
  1590. (forward-line)
  1591. (org-drag-element-backward)))
  1592. ;; Preserve visibility of elements and their contents.
  1593. (should
  1594. (equal '((63 . 82) (26 . 48))
  1595. (org-test-with-temp-text "
  1596. #+BEGIN_CENTER
  1597. Text.
  1598. #+END_CENTER
  1599. - item 1
  1600. #+BEGIN_QUOTE
  1601. Text.
  1602. #+END_QUOTE"
  1603. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1604. (search-backward "- item 1")
  1605. (org-drag-element-backward)
  1606. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1607. (overlays-in (point-min) (point-max)))))))
  1608. (ert-deftest test-org/drag-element-forward ()
  1609. "Test `org-drag-element-forward' specifications."
  1610. ;; 1. Error when trying to move first element of buffer.
  1611. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1612. (goto-line 3)
  1613. (should-error (org-drag-element-forward)))
  1614. ;; 2. Error when trying to swap nested elements.
  1615. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1616. (forward-line)
  1617. (should-error (org-drag-element-forward)))
  1618. ;; 3. Error when trying to swap a non-headline element and an
  1619. ;; headline.
  1620. (org-test-with-temp-text "Test.\n* Head 1"
  1621. (should-error (org-drag-element-forward)))
  1622. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1623. ;; between elements.
  1624. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1625. (search-forward "graph")
  1626. (org-drag-element-forward)
  1627. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1628. (should (looking-at " 1")))
  1629. ;; 5. Preserve visibility of elements and their contents.
  1630. (org-test-with-temp-text "
  1631. #+BEGIN_CENTER
  1632. Text.
  1633. #+END_CENTER
  1634. - item 1
  1635. #+BEGIN_QUOTE
  1636. Text.
  1637. #+END_QUOTE"
  1638. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1639. (search-backward "#+BEGIN_CENTER")
  1640. (org-drag-element-forward)
  1641. (should
  1642. (equal
  1643. '((63 . 82) (26 . 48))
  1644. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1645. (overlays-in (point-min) (point-max)))))))
  1646. ;;; Planning
  1647. (ert-deftest test-org/timestamp-has-time-p ()
  1648. "Test `org-timestamp-has-time-p' specifications."
  1649. ;; With time.
  1650. (should
  1651. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1652. (org-timestamp-has-time-p (org-element-context))))
  1653. ;; Without time.
  1654. (should-not
  1655. (org-test-with-temp-text "<2012-03-29 Thu>"
  1656. (org-timestamp-has-time-p (org-element-context)))))
  1657. (ert-deftest test-org/timestamp-format ()
  1658. "Test `org-timestamp-format' specifications."
  1659. ;; Regular test.
  1660. (should
  1661. (equal
  1662. "2012-03-29 16:40"
  1663. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1664. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1665. ;; Range end.
  1666. (should
  1667. (equal
  1668. "2012-03-29"
  1669. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1670. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1671. (ert-deftest test-org/timestamp-split-range ()
  1672. "Test `org-timestamp-split-range' specifications."
  1673. ;; Extract range start (active).
  1674. (should
  1675. (equal '(2012 3 29)
  1676. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1677. (let ((ts (org-timestamp-split-range (org-element-context))))
  1678. (mapcar (lambda (p) (org-element-property p ts))
  1679. '(:year-end :month-end :day-end))))))
  1680. ;; Extract range start (inactive)
  1681. (should
  1682. (equal '(2012 3 29)
  1683. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1684. (let ((ts (org-timestamp-split-range (org-element-context))))
  1685. (mapcar (lambda (p) (org-element-property p ts))
  1686. '(:year-end :month-end :day-end))))))
  1687. ;; Extract range end (active).
  1688. (should
  1689. (equal '(2012 3 30)
  1690. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1691. (let ((ts (org-timestamp-split-range
  1692. (org-element-context) t)))
  1693. (mapcar (lambda (p) (org-element-property p ts))
  1694. '(:year-end :month-end :day-end))))))
  1695. ;; Extract range end (inactive)
  1696. (should
  1697. (equal '(2012 3 30)
  1698. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1699. (let ((ts (org-timestamp-split-range
  1700. (org-element-context) t)))
  1701. (mapcar (lambda (p) (org-element-property p ts))
  1702. '(:year-end :month-end :day-end))))))
  1703. ;; Return the timestamp if not a range.
  1704. (should
  1705. (org-test-with-temp-text "[2012-03-29 Thu]"
  1706. (let* ((ts-orig (org-element-context))
  1707. (ts-copy (org-timestamp-split-range ts-orig)))
  1708. (eq ts-orig ts-copy))))
  1709. (should
  1710. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1711. (let* ((ts-orig (org-element-context))
  1712. (ts-copy (org-timestamp-split-range ts-orig)))
  1713. (eq ts-orig ts-copy))))
  1714. ;; Check that parent is the same when a range was split.
  1715. (should
  1716. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1717. (let* ((ts-orig (org-element-context))
  1718. (ts-copy (org-timestamp-split-range ts-orig)))
  1719. (eq (org-element-property :parent ts-orig)
  1720. (org-element-property :parent ts-copy))))))
  1721. (ert-deftest test-org/timestamp-translate ()
  1722. "Test `org-timestamp-translate' specifications."
  1723. ;; Translate whole date range.
  1724. (should
  1725. (equal "<29>--<30>"
  1726. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1727. (let ((org-display-custom-times t)
  1728. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1729. (org-timestamp-translate (org-element-context))))))
  1730. ;; Translate date range start.
  1731. (should
  1732. (equal "<29>"
  1733. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1734. (let ((org-display-custom-times t)
  1735. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1736. (org-timestamp-translate (org-element-context) 'start)))))
  1737. ;; Translate date range end.
  1738. (should
  1739. (equal "<30>"
  1740. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1741. (let ((org-display-custom-times t)
  1742. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1743. (org-timestamp-translate (org-element-context) 'end)))))
  1744. ;; Translate time range.
  1745. (should
  1746. (equal "<08>--<16>"
  1747. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1748. (let ((org-display-custom-times t)
  1749. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1750. (org-timestamp-translate (org-element-context))))))
  1751. ;; Translate non-range timestamp.
  1752. (should
  1753. (equal "<29>"
  1754. (org-test-with-temp-text "<2012-03-29 Thu>"
  1755. (let ((org-display-custom-times t)
  1756. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1757. (org-timestamp-translate (org-element-context))))))
  1758. ;; Do not change `diary' timestamps.
  1759. (should
  1760. (equal "<%%(org-float t 4 2)>"
  1761. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1762. (let ((org-display-custom-times t)
  1763. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1764. (org-timestamp-translate (org-element-context)))))))
  1765. ;;; Property API
  1766. (ert-deftest test-org/buffer-property-keys ()
  1767. "Test `org-buffer-property-keys' specifications."
  1768. ;; Retrieve properties accross siblings.
  1769. (should
  1770. (equal '("A" "B")
  1771. (org-test-with-temp-text "
  1772. * H1
  1773. :PROPERTIES:
  1774. :A: 1
  1775. :END:
  1776. * H2
  1777. :PROPERTIES:
  1778. :B: 1
  1779. :END:"
  1780. (org-buffer-property-keys))))
  1781. ;; Retrieve properties accross children.
  1782. (should
  1783. (equal '("A" "B")
  1784. (org-test-with-temp-text "
  1785. * H1
  1786. :PROPERTIES:
  1787. :A: 1
  1788. :END:
  1789. ** H2
  1790. :PROPERTIES:
  1791. :B: 1
  1792. :END:"
  1793. (org-buffer-property-keys))))
  1794. ;; Retrieve muliple properties in the same drawer.
  1795. (should
  1796. (equal '("A" "B")
  1797. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  1798. (org-buffer-property-keys))))
  1799. ;; Ignore extension symbol in property name.
  1800. (should
  1801. (equal '("A")
  1802. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  1803. (org-buffer-property-keys))))
  1804. ;; With non-nil COLUMNS, extract property names from columns.
  1805. (should
  1806. (equal '("A" "B")
  1807. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  1808. (org-buffer-property-keys nil nil t))))
  1809. (should
  1810. (equal '("A" "B" "COLUMNS")
  1811. (org-test-with-temp-text
  1812. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  1813. (org-buffer-property-keys nil nil t)))))
  1814. (ert-deftest test-org/property-values ()
  1815. "Test `org-property-values' specifications."
  1816. ;; Regular test.
  1817. (should
  1818. (equal '("2" "1")
  1819. (org-test-with-temp-text
  1820. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  1821. (org-property-values "A"))))
  1822. ;; Ignore empty values.
  1823. (should-not
  1824. (org-test-with-temp-text
  1825. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  1826. (org-property-values "A")))
  1827. ;; Take into consideration extended values.
  1828. (should
  1829. (equal '("1 2")
  1830. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  1831. (org-property-values "A")))))
  1832. (ert-deftest test-org/entry-delete ()
  1833. "Test `org-entry-delete' specifications."
  1834. ;; Regular test.
  1835. (should
  1836. (string-match
  1837. " *:PROPERTIES:\n *:B: +2\n *:END:"
  1838. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  1839. (org-entry-delete (point) "A")
  1840. (buffer-string))))
  1841. ;; When last property is removed, remove the property drawer.
  1842. (should-not
  1843. (string-match
  1844. ":PROPERTIES:"
  1845. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  1846. (org-entry-delete (point) "A")
  1847. (buffer-string)))))
  1848. (ert-deftest test-org/entry-get ()
  1849. "Test `org-entry-get' specifications."
  1850. ;; Regular test.
  1851. (should
  1852. (equal "1"
  1853. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  1854. (org-entry-get (point) "A"))))
  1855. ;; Ignore case.
  1856. (should
  1857. (equal "1"
  1858. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  1859. (org-entry-get (point) "a"))))
  1860. ;; Handle extended values, both before and after base value.
  1861. (should
  1862. (equal "1 2 3"
  1863. (org-test-with-temp-text
  1864. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  1865. (org-entry-get (point) "A"))))
  1866. ;; Empty values are returned as the empty string.
  1867. (should
  1868. (equal ""
  1869. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  1870. (org-entry-get (point) "A"))))
  1871. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  1872. ;; otherwise, return nil.
  1873. (should-not
  1874. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  1875. (org-entry-get (point) "A")))
  1876. (should
  1877. (equal "nil"
  1878. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  1879. (org-entry-get (point) "A" nil t))))
  1880. ;; Return nil when no property is found, independently on the
  1881. ;; LITERAL-NIL argument.
  1882. (should-not
  1883. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  1884. (org-entry-get (point) "B")))
  1885. (should-not
  1886. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  1887. (org-entry-get (point) "B" nil t)))
  1888. ;; Handle inheritance, when allowed.
  1889. (should
  1890. (equal
  1891. "1"
  1892. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  1893. (org-entry-get (point) "A" t))))
  1894. (should
  1895. (equal
  1896. "1"
  1897. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  1898. (let ((org-use-property-inheritance t))
  1899. (org-entry-get (point) "A" 'selective)))))
  1900. (should-not
  1901. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  1902. (let ((org-use-property-inheritance nil))
  1903. (org-entry-get (point) "A" 'selective)))))
  1904. (ert-deftest test-org/entry-properties ()
  1905. "Test `org-entry-properties' specifications."
  1906. ;; Get "TODO" property.
  1907. (should
  1908. (equal "TODO"
  1909. (org-test-with-temp-text "* TODO H"
  1910. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  1911. (should-not
  1912. (org-test-with-temp-text "* H"
  1913. (assoc "TODO" (org-entry-properties nil "TODO"))))
  1914. ;; Get "PRIORITY" property.
  1915. (should
  1916. (equal "A"
  1917. (org-test-with-temp-text "* [#A] H"
  1918. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  1919. (should-not
  1920. (org-test-with-temp-text "* H"
  1921. (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
  1922. ;; Get "FILE" property.
  1923. (should
  1924. (org-test-with-temp-text-in-file "* H\nParagraph"
  1925. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  1926. (buffer-file-name))))
  1927. (should-not
  1928. (org-test-with-temp-text "* H\nParagraph"
  1929. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  1930. ;; Get "TAGS" property.
  1931. (should
  1932. (equal ":tag1:tag2:"
  1933. (org-test-with-temp-text "* H :tag1:tag2:"
  1934. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  1935. (should-not
  1936. (org-test-with-temp-text "* H"
  1937. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  1938. ;; Get "ALLTAGS" property.
  1939. (should
  1940. (equal ":tag1:tag2:"
  1941. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  1942. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  1943. (should-not
  1944. (org-test-with-temp-text "* H"
  1945. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  1946. ;; Get "BLOCKED" property.
  1947. (should
  1948. (equal "t"
  1949. (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
  1950. (let ((org-enforce-todo-dependencies t)
  1951. (org-blocker-hook
  1952. '(org-block-todo-from-children-or-siblings-or-parent)))
  1953. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  1954. (should
  1955. (equal ""
  1956. (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
  1957. (let ((org-enforce-todo-dependencies t))
  1958. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  1959. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  1960. (should
  1961. (equal
  1962. "[2012-03-29 thu.]"
  1963. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  1964. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  1965. (should-not
  1966. (org-test-with-temp-text "* H"
  1967. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  1968. (should
  1969. (equal
  1970. "<2014-03-04 tue.>"
  1971. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  1972. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  1973. (should-not
  1974. (org-test-with-temp-text "* H"
  1975. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  1976. (should
  1977. (equal
  1978. "<2014-03-04 tue.>"
  1979. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  1980. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  1981. (should-not
  1982. (org-test-with-temp-text "* H"
  1983. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  1984. ;; Get "CATEGORY"
  1985. (should
  1986. (equal "cat"
  1987. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  1988. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  1989. (should
  1990. (equal "cat"
  1991. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  1992. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  1993. ;; Get standard properties.
  1994. (should
  1995. (equal "1"
  1996. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  1997. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  1998. ;; Handle extended properties.
  1999. (should
  2000. (equal "1 2 3"
  2001. (org-test-with-temp-text
  2002. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2003. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2004. (should
  2005. (equal "1 2 3"
  2006. (org-test-with-temp-text
  2007. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  2008. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2009. ;; Ignore forbidden (special) properties.
  2010. (should-not
  2011. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  2012. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  2013. (ert-deftest test-org/entry-put ()
  2014. "Test `org-entry-put' specifications."
  2015. ;; Error when not a string or nil.
  2016. (should-error
  2017. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  2018. (org-entry-put 1 "test" 2)))
  2019. ;; Set "TODO" property.
  2020. (should
  2021. (string-match (regexp-quote " TODO H")
  2022. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2023. (org-entry-put (point) "TODO" "TODO")
  2024. (buffer-string))))
  2025. (should
  2026. (string-match (regexp-quote "* H")
  2027. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2028. (org-entry-put (point) "TODO" nil)
  2029. (buffer-string))))
  2030. ;; Set "PRIORITY" property.
  2031. (should
  2032. (equal "* [#A] H"
  2033. (org-test-with-temp-text "* [#B] H"
  2034. (org-entry-put (point) "PRIORITY" "A")
  2035. (buffer-string))))
  2036. (should
  2037. (equal "* H"
  2038. (org-test-with-temp-text "* [#B] H"
  2039. (org-entry-put (point) "PRIORITY" nil)
  2040. (buffer-string))))
  2041. ;; Set "SCHEDULED" property.
  2042. (should
  2043. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  2044. (org-test-with-temp-text "* H"
  2045. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  2046. (buffer-string))))
  2047. (should
  2048. (string= "* H\n"
  2049. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2050. (org-entry-put (point) "SCHEDULED" nil)
  2051. (buffer-string))))
  2052. (should
  2053. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  2054. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2055. (org-entry-put (point) "SCHEDULED" "earlier")
  2056. (buffer-string))))
  2057. (should
  2058. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  2059. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2060. (org-entry-put (point) "SCHEDULED" "later")
  2061. (buffer-string))))
  2062. ;; Set "DEADLINE" property.
  2063. (should
  2064. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  2065. (org-test-with-temp-text "* H"
  2066. (org-entry-put (point) "DEADLINE" "2014-03-04")
  2067. (buffer-string))))
  2068. (should
  2069. (string= "* H\n"
  2070. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2071. (org-entry-put (point) "DEADLINE" nil)
  2072. (buffer-string))))
  2073. (should
  2074. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  2075. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2076. (org-entry-put (point) "DEADLINE" "earlier")
  2077. (buffer-string))))
  2078. (should
  2079. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  2080. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2081. (org-entry-put (point) "DEADLINE" "later")
  2082. (buffer-string))))
  2083. ;; Regular properties, with or without pre-existing drawer.
  2084. (should
  2085. (string-match "^ *:A: +2$"
  2086. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2087. (org-entry-put (point) "A" "2")
  2088. (buffer-string))))
  2089. (should
  2090. (string-match "^ *:A: +1$"
  2091. (org-test-with-temp-text "* H"
  2092. (org-entry-put (point) "A" "1")
  2093. (buffer-string)))))
  2094. ;;; Radio Targets
  2095. (ert-deftest test-org/update-radio-target-regexp ()
  2096. "Test `org-update-radio-target-regexp' specifications."
  2097. ;; Properly update cache with no previous radio target regexp.
  2098. (should
  2099. (eq 'link
  2100. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2101. (save-excursion (goto-char (point-max)) (org-element-context))
  2102. (insert "<<<")
  2103. (search-forward "o")
  2104. (insert ">>>")
  2105. (org-update-radio-target-regexp)
  2106. (goto-char (point-max))
  2107. (org-element-type (org-element-context)))))
  2108. ;; Properly update cache with previous radio target regexp.
  2109. (should
  2110. (eq 'link
  2111. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2112. (save-excursion (goto-char (point-max)) (org-element-context))
  2113. (insert "<<<")
  2114. (search-forward "o")
  2115. (insert ">>>")
  2116. (org-update-radio-target-regexp)
  2117. (search-backward "r")
  2118. (delete-char 5)
  2119. (insert "new")
  2120. (org-update-radio-target-regexp)
  2121. (goto-char (point-max))
  2122. (delete-region (line-beginning-position) (point))
  2123. (insert "new")
  2124. (org-element-type (org-element-context))))))
  2125. ;;; Visibility
  2126. (ert-deftest test-org/flag-drawer ()
  2127. "Test `org-flag-drawer' specifications."
  2128. ;; Hide drawer.
  2129. (should
  2130. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2131. (org-flag-drawer t)
  2132. (get-char-property (line-end-position) 'invisible)))
  2133. ;; Show drawer.
  2134. (should-not
  2135. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2136. (org-flag-drawer t)
  2137. (org-flag-drawer nil)
  2138. (get-char-property (line-end-position) 'invisible)))
  2139. ;; Test optional argument.
  2140. (should
  2141. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2142. (let ((drawer (save-excursion (search-forward ":D2")
  2143. (org-element-at-point))))
  2144. (org-flag-drawer t drawer)
  2145. (get-char-property (progn (search-forward ":D2") (line-end-position))
  2146. 'invisible))))
  2147. (should-not
  2148. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2149. (let ((drawer (save-excursion (search-forward ":D2")
  2150. (org-element-at-point))))
  2151. (org-flag-drawer t drawer)
  2152. (get-char-property (line-end-position) 'invisible))))
  2153. ;; Do not hide fake drawers.
  2154. (should-not
  2155. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  2156. (forward-line 1)
  2157. (org-flag-drawer t)
  2158. (get-char-property (line-end-position) 'invisible)))
  2159. ;; Do not hide incomplete drawers.
  2160. (should-not
  2161. (org-test-with-temp-text ":D:\nparagraph"
  2162. (forward-line 1)
  2163. (org-flag-drawer t)
  2164. (get-char-property (line-end-position) 'invisible)))
  2165. ;; Do not hide drawers when called from final blank lines.
  2166. (should-not
  2167. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  2168. (goto-char (point-max))
  2169. (org-flag-drawer t)
  2170. (goto-char (point-min))
  2171. (get-char-property (line-end-position) 'invisible)))
  2172. ;; Don't leave point in an invisible part of the buffer when hiding
  2173. ;; a drawer away.
  2174. (should-not
  2175. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2176. (goto-char (point-max))
  2177. (org-flag-drawer t)
  2178. (get-char-property (point) 'invisible))))
  2179. (ert-deftest test-org/hide-block-toggle ()
  2180. "Test `org-hide-block-toggle' specifications."
  2181. ;; Error when not at a block.
  2182. (should-error
  2183. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  2184. (org-hide-block-toggle 'off)
  2185. (get-char-property (line-end-position) 'invisible)))
  2186. ;; Hide block.
  2187. (should
  2188. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2189. (org-hide-block-toggle)
  2190. (get-char-property (line-end-position) 'invisible)))
  2191. (should
  2192. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  2193. (org-hide-block-toggle)
  2194. (get-char-property (line-end-position) 'invisible)))
  2195. ;; Show block unconditionally when optional argument is `off'.
  2196. (should-not
  2197. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2198. (org-hide-block-toggle)
  2199. (org-hide-block-toggle 'off)
  2200. (get-char-property (line-end-position) 'invisible)))
  2201. (should-not
  2202. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2203. (org-hide-block-toggle 'off)
  2204. (get-char-property (line-end-position) 'invisible)))
  2205. ;; Hide block unconditionally when optional argument is non-nil.
  2206. (should
  2207. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2208. (org-hide-block-toggle t)
  2209. (get-char-property (line-end-position) 'invisible)))
  2210. (should
  2211. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2212. (org-hide-block-toggle)
  2213. (org-hide-block-toggle t)
  2214. (get-char-property (line-end-position) 'invisible)))
  2215. ;; Do not hide block when called from final blank lines.
  2216. (should-not
  2217. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  2218. (org-hide-block-toggle)
  2219. (goto-char (point-min))
  2220. (get-char-property (line-end-position) 'invisible)))
  2221. ;; Don't leave point in an invisible part of the buffer when hiding
  2222. ;; a block away.
  2223. (should-not
  2224. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  2225. (org-hide-block-toggle)
  2226. (get-char-property (point) 'invisible))))
  2227. (ert-deftest test-org/hide-block-toggle-maybe ()
  2228. "Test `org-hide-block-toggle-maybe' specifications."
  2229. (should
  2230. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  2231. (org-hide-block-toggle-maybe)))
  2232. (should-not
  2233. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  2234. (provide 'test-org)
  2235. ;;; test-org.el ends here