test-org.el 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  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. ;;; Keywords
  909. (ert-deftest test-org/set-regexps-and-options ()
  910. "Test `org-set-regexps-and-options' specifications."
  911. ;; TAGS keyword.
  912. (should
  913. (equal '(("A" . ?a) ("B") ("C"))
  914. (org-test-with-temp-text "#+TAGS: A(a) B C"
  915. (org-mode-restart)
  916. org-tag-alist)))
  917. (should
  918. (equal '(("A") (:newline) ("B"))
  919. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  920. (org-mode-restart)
  921. org-tag-alist)))
  922. (should
  923. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  924. (org-test-with-temp-text "#+TAGS: { A B } C"
  925. (org-mode-restart)
  926. org-tag-alist)))
  927. (should
  928. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  929. (org-test-with-temp-text "#+TAGS: { A : B C }"
  930. (org-mode-restart)
  931. org-tag-alist)))
  932. (should
  933. (equal '(("A" "B" "C"))
  934. (org-test-with-temp-text "#+TAGS: { A : B C }"
  935. (org-mode-restart)
  936. org-tag-groups-alist)))
  937. ;; FILETAGS keyword.
  938. (should
  939. (equal '("A" "B" "C")
  940. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  941. (org-mode-restart)
  942. org-file-tags)))
  943. ;; PROPERTY keyword. Property names are case-insensitive.
  944. (should
  945. (equal "foo=1"
  946. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  947. (org-mode-restart)
  948. (cdr (assoc "var" org-file-properties)))))
  949. (should
  950. (equal
  951. "foo=1 bar=2"
  952. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  953. (org-mode-restart)
  954. (cdr (assoc "var" org-file-properties)))))
  955. (should
  956. (equal
  957. "foo=1 bar=2"
  958. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  959. (org-mode-restart)
  960. (cdr (assoc "var" org-file-properties)))))
  961. ;; ARCHIVE keyword.
  962. (should
  963. (equal "%s_done::"
  964. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  965. (org-mode-restart)
  966. org-archive-location)))
  967. ;; CATEGORY keyword.
  968. (should
  969. (eq 'test
  970. (org-test-with-temp-text "#+CATEGORY: test"
  971. (org-mode-restart)
  972. org-category)))
  973. (should
  974. (equal "test"
  975. (org-test-with-temp-text "#+CATEGORY: test"
  976. (org-mode-restart)
  977. (cdr (assoc "CATEGORY" org-file-properties)))))
  978. ;; COLUMNS keyword.
  979. (should
  980. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  981. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  982. (org-mode-restart)
  983. org-columns-default-format)))
  984. ;; CONSTANTS keyword. Constants names are case sensitive.
  985. (should
  986. (equal '("299792458." "3.14")
  987. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  988. (org-mode-restart)
  989. (mapcar
  990. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  991. '("c" "pi")))))
  992. (should
  993. (equal "3.14"
  994. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  995. (org-mode-restart)
  996. (cdr (assoc "pi" org-table-formula-constants-local)))))
  997. (should
  998. (equal "22/7"
  999. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1000. (org-mode-restart)
  1001. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1002. ;; LINK keyword.
  1003. (should
  1004. (equal
  1005. '("url1" "url2")
  1006. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1007. (org-mode-restart)
  1008. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1009. '("a" "b")))))
  1010. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1011. (should
  1012. (equal
  1013. '(?X ?Z ?Y)
  1014. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1015. (org-mode-restart)
  1016. (list org-highest-priority org-lowest-priority org-default-priority))))
  1017. (should
  1018. (equal
  1019. '(?A ?C ?B)
  1020. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1021. (org-mode-restart)
  1022. (list org-highest-priority org-lowest-priority org-default-priority))))
  1023. ;; STARTUP keyword.
  1024. (should
  1025. (equal '(t t)
  1026. (org-test-with-temp-text "#+STARTUP: fold odd"
  1027. (org-mode-restart)
  1028. (list org-startup-folded org-odd-levels-only))))
  1029. ;; TODO keywords.
  1030. (should
  1031. (equal '(("A" "B") ("C"))
  1032. (org-test-with-temp-text "#+TODO: A B | C"
  1033. (org-mode-restart)
  1034. (list org-not-done-keywords org-done-keywords))))
  1035. (should
  1036. (equal '(("A" "B") ("C"))
  1037. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1038. (org-mode-restart)
  1039. (list org-not-done-keywords org-done-keywords))))
  1040. (should
  1041. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1042. (org-test-with-temp-text "#+TODO: A(a)"
  1043. (org-mode-restart)
  1044. org-todo-key-alist)))
  1045. (should
  1046. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1047. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1048. (org-mode-restart)
  1049. org-todo-log-states)))
  1050. ;; Enter SETUPFILE keyword.
  1051. (should
  1052. (equal "1"
  1053. (org-test-with-temp-text
  1054. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1055. (org-mode-restart)
  1056. (cdr (assoc "a" org-file-properties))))))
  1057. ;;; Links
  1058. ;;;; Coderefs
  1059. (ert-deftest test-org/coderef ()
  1060. "Test coderef links specifications."
  1061. (should
  1062. (org-test-with-temp-text "
  1063. #+BEGIN_SRC emacs-lisp
  1064. \(+ 1 1) (ref:sc)
  1065. #+END_SRC
  1066. \[[(sc)]]"
  1067. (goto-char (point-max))
  1068. (org-open-at-point)
  1069. (looking-at "(ref:sc)"))))
  1070. ;;;; Custom ID
  1071. (ert-deftest test-org/custom-id ()
  1072. "Test custom ID links specifications."
  1073. (should
  1074. (org-test-with-temp-text
  1075. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  1076. (goto-char (point-max))
  1077. (org-open-at-point)
  1078. (org-looking-at-p "\\* H1"))))
  1079. ;;;; Fuzzy Links
  1080. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1081. ;; a named element (#+name: text) and to headlines (* Text).
  1082. (ert-deftest test-org/fuzzy-links ()
  1083. "Test fuzzy links specifications."
  1084. ;; Fuzzy link goes in priority to a matching target.
  1085. (should
  1086. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  1087. (goto-line 5)
  1088. (org-open-at-point)
  1089. (looking-at "<<Test>>")))
  1090. ;; Then fuzzy link points to an element with a given name.
  1091. (should
  1092. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  1093. (goto-line 5)
  1094. (org-open-at-point)
  1095. (looking-at "#\\+NAME: Test")))
  1096. ;; A target still lead to a matching headline otherwise.
  1097. (should
  1098. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  1099. (goto-line 4)
  1100. (org-open-at-point)
  1101. (looking-at "\\* Head2")))
  1102. ;; With a leading star in link, enforce heading match.
  1103. (should
  1104. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  1105. (goto-line 3)
  1106. (org-open-at-point)
  1107. (looking-at "\\* Test")))
  1108. ;; Correctly un-hexify fuzzy links.
  1109. (should
  1110. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  1111. (goto-char (point-max))
  1112. (org-open-at-point)
  1113. (bobp))))
  1114. ;;;; Link Escaping
  1115. (ert-deftest test-org/org-link-escape-ascii-character ()
  1116. "Escape an ascii character."
  1117. (should
  1118. (string=
  1119. "%5B"
  1120. (org-link-escape "["))))
  1121. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  1122. "Escape an ascii control character."
  1123. (should
  1124. (string=
  1125. "%09"
  1126. (org-link-escape "\t"))))
  1127. (ert-deftest test-org/org-link-escape-multibyte-character ()
  1128. "Escape an unicode multibyte character."
  1129. (should
  1130. (string=
  1131. "%E2%82%AC"
  1132. (org-link-escape "€"))))
  1133. (ert-deftest test-org/org-link-escape-custom-table ()
  1134. "Escape string with custom character table."
  1135. (should
  1136. (string=
  1137. "Foo%3A%42ar%0A"
  1138. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  1139. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  1140. "Escape string with custom table merged with default table."
  1141. (should
  1142. (string=
  1143. "%5BF%6F%6F%3A%42ar%0A%5D"
  1144. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  1145. (ert-deftest test-org/org-link-unescape-ascii-character ()
  1146. "Unescape an ascii character."
  1147. (should
  1148. (string=
  1149. "["
  1150. (org-link-unescape "%5B"))))
  1151. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  1152. "Unescpae an ascii control character."
  1153. (should
  1154. (string=
  1155. "\n"
  1156. (org-link-unescape "%0A"))))
  1157. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  1158. "Unescape unicode multibyte character."
  1159. (should
  1160. (string=
  1161. "€"
  1162. (org-link-unescape "%E2%82%AC"))))
  1163. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  1164. "Unescape old style percent escaped character."
  1165. (should
  1166. (string=
  1167. "àâçèéêîôùû"
  1168. (decode-coding-string
  1169. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  1170. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  1171. "Escape and unescape a URL that includes an escaped char.
  1172. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  1173. (should
  1174. (string=
  1175. "http://some.host.com/form?&id=blah%2Bblah25"
  1176. (org-link-unescape
  1177. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  1178. (ert-deftest test-org/org-link-escape-chars-browser ()
  1179. "Test of the constant `org-link-escape-chars-browser'.
  1180. See there why this test is a candidate to be removed once Org
  1181. drops support for Emacs 24.1 and 24.2."
  1182. (should
  1183. (string=
  1184. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1185. "%22Release%208.2%22&idxname=emacs-orgmode")
  1186. (org-link-escape-browser ; Do not replace with `url-encode-url',
  1187. ; see docstring above.
  1188. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1189. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  1190. ;;;; Open at point
  1191. (ert-deftest test-org/open-at-point-in-property ()
  1192. "Does `org-open-at-point' open link in property drawer?"
  1193. (should
  1194. (org-test-with-temp-text
  1195. "* Headline
  1196. :PROPERTIES:
  1197. :URL: <point>[[info:emacs#Top]]
  1198. :END:"
  1199. (org-open-at-point) t)))
  1200. (ert-deftest test-org/open-at-point-in-comment ()
  1201. "Does `org-open-at-point' open link in a commented line?"
  1202. (should
  1203. (org-test-with-temp-text
  1204. "# <point>[[info:emacs#Top]]"
  1205. (org-open-at-point) t)))
  1206. (ert-deftest test-org/open-at-point/info ()
  1207. "Test `org-open-at-point' on info links."
  1208. (should
  1209. (org-test-with-temp-text
  1210. "<point>[[info:emacs#Top]]"
  1211. (org-open-at-point)
  1212. (and (switch-to-buffer "*info*")
  1213. (prog1
  1214. (looking-at "\nThe Emacs Editor")
  1215. (kill-buffer))))))
  1216. ;;; Node Properties
  1217. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1218. "Ensure properties accumulate in subtree drawers."
  1219. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1220. (org-babel-next-src-block)
  1221. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1222. ;;; Mark Region
  1223. (ert-deftest test-org/mark-subtree ()
  1224. "Test `org-mark-subtree' specifications."
  1225. ;; Error when point is before first headline.
  1226. (should-error
  1227. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1228. (progn (transient-mark-mode 1)
  1229. (org-mark-subtree))))
  1230. ;; Without argument, mark current subtree.
  1231. (should
  1232. (equal
  1233. '(12 32)
  1234. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1235. (progn (transient-mark-mode 1)
  1236. (forward-line 2)
  1237. (org-mark-subtree)
  1238. (list (region-beginning) (region-end))))))
  1239. ;; With an argument, move ARG up.
  1240. (should
  1241. (equal
  1242. '(1 32)
  1243. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1244. (progn (transient-mark-mode 1)
  1245. (forward-line 2)
  1246. (org-mark-subtree 1)
  1247. (list (region-beginning) (region-end))))))
  1248. ;; Do not get fooled by inlinetasks.
  1249. (when (featurep 'org-inlinetask)
  1250. (should
  1251. (= 1
  1252. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1253. (progn (transient-mark-mode 1)
  1254. (forward-line 1)
  1255. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1256. (region-beginning)))))))
  1257. ;;; Navigation
  1258. (ert-deftest test-org/beginning-of-line ()
  1259. "Test `org-beginning-of-line' specifications."
  1260. ;; Standard test.
  1261. (should
  1262. (org-test-with-temp-text "Some text\nSome other text"
  1263. (progn (org-beginning-of-line) (bolp))))
  1264. ;; Standard test with `visual-line-mode'.
  1265. (should-not
  1266. (org-test-with-temp-text "A long line of text\nSome other text"
  1267. (progn (visual-line-mode)
  1268. (forward-char 2)
  1269. (dotimes (i 1000) (insert "very "))
  1270. (org-beginning-of-line)
  1271. (bolp))))
  1272. ;; At an headline with special movement.
  1273. (should
  1274. (org-test-with-temp-text "* TODO Headline"
  1275. (let ((org-special-ctrl-a/e t))
  1276. (org-end-of-line)
  1277. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1278. (progn (org-beginning-of-line) (bolp))
  1279. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1280. (ert-deftest test-org/end-of-line ()
  1281. "Test `org-end-of-line' specifications."
  1282. ;; Standard test.
  1283. (should
  1284. (org-test-with-temp-text "Some text\nSome other text"
  1285. (progn (org-end-of-line) (eolp))))
  1286. ;; Standard test with `visual-line-mode'.
  1287. (should-not
  1288. (org-test-with-temp-text "A long line of text\nSome other text"
  1289. (progn (visual-line-mode)
  1290. (forward-char 2)
  1291. (dotimes (i 1000) (insert "very "))
  1292. (goto-char (point-min))
  1293. (org-end-of-line)
  1294. (eolp))))
  1295. ;; At an headline with special movement.
  1296. (should
  1297. (org-test-with-temp-text "* Headline1 :tag:\n"
  1298. (let ((org-special-ctrl-a/e t))
  1299. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1300. (progn (org-end-of-line) (eolp))
  1301. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1302. ;; At an headline without special movement.
  1303. (should
  1304. (org-test-with-temp-text "* Headline2 :tag:\n"
  1305. (let ((org-special-ctrl-a/e nil))
  1306. (and (progn (org-end-of-line) (eolp))
  1307. (progn (org-end-of-line) (eolp))))))
  1308. ;; At an headline, with reversed movement.
  1309. (should
  1310. (org-test-with-temp-text "* Headline3 :tag:\n"
  1311. (let ((org-special-ctrl-a/e 'reversed)
  1312. (this-command last-command))
  1313. (and (progn (org-end-of-line) (eolp))
  1314. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1315. ;; At a block without hidden contents.
  1316. (should
  1317. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1318. (progn (org-end-of-line) (eolp))))
  1319. ;; At a block with hidden contents.
  1320. (should-not
  1321. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1322. (let ((org-special-ctrl-a/e t))
  1323. (org-hide-block-toggle)
  1324. (org-end-of-line)
  1325. (eobp)))))
  1326. (ert-deftest test-org/forward-paragraph ()
  1327. "Test `org-forward-paragraph' specifications."
  1328. ;; At end of buffer, return an error.
  1329. (should-error
  1330. (org-test-with-temp-text "Paragraph"
  1331. (goto-char (point-max))
  1332. (org-forward-paragraph)))
  1333. ;; Standard test.
  1334. (should
  1335. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1336. (org-forward-paragraph)
  1337. (looking-at "P2")))
  1338. ;; Ignore depth.
  1339. (should
  1340. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1341. (org-forward-paragraph)
  1342. (looking-at "P1")))
  1343. ;; Do not enter elements with invisible contents.
  1344. (should
  1345. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1346. (org-hide-block-toggle)
  1347. (org-forward-paragraph)
  1348. (looking-at "P3")))
  1349. ;; On an affiliated keyword, jump to the beginning of the element.
  1350. (should
  1351. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1352. (org-forward-paragraph)
  1353. (looking-at "Para")))
  1354. ;; On an item or a footnote definition, move to the second element
  1355. ;; inside, if any.
  1356. (should
  1357. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1358. (org-forward-paragraph)
  1359. (looking-at " Paragraph")))
  1360. (should
  1361. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1362. (org-forward-paragraph)
  1363. (looking-at "Paragraph")))
  1364. ;; On an item, or a footnote definition, when the first line is
  1365. ;; empty, move to the first item.
  1366. (should
  1367. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1368. (org-forward-paragraph)
  1369. (looking-at " Paragraph")))
  1370. (should
  1371. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1372. (org-forward-paragraph)
  1373. (looking-at "Paragraph")))
  1374. ;; On a table (resp. a property drawer) do not move through table
  1375. ;; rows (resp. node properties).
  1376. (should
  1377. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1378. (org-forward-paragraph)
  1379. (looking-at "Paragraph")))
  1380. (should
  1381. (org-test-with-temp-text
  1382. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1383. (org-forward-paragraph)
  1384. (looking-at "Paragraph")))
  1385. ;; On a verse or source block, stop after blank lines.
  1386. (should
  1387. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1388. (org-forward-paragraph)
  1389. (looking-at "L2")))
  1390. (should
  1391. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1392. (org-forward-paragraph)
  1393. (looking-at "L2"))))
  1394. (ert-deftest test-org/backward-paragraph ()
  1395. "Test `org-backward-paragraph' specifications."
  1396. ;; Error at beginning of buffer.
  1397. (should-error
  1398. (org-test-with-temp-text "Paragraph"
  1399. (org-backward-paragraph)))
  1400. ;; Regular test.
  1401. (should
  1402. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1403. (goto-char (point-max))
  1404. (org-backward-paragraph)
  1405. (looking-at "P3")))
  1406. (should
  1407. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1408. (goto-char (point-max))
  1409. (beginning-of-line)
  1410. (org-backward-paragraph)
  1411. (looking-at "P2")))
  1412. ;; Ignore depth.
  1413. (should
  1414. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1415. (goto-char (point-max))
  1416. (beginning-of-line)
  1417. (org-backward-paragraph)
  1418. (looking-at "P2")))
  1419. ;; Ignore invisible elements.
  1420. (should
  1421. (org-test-with-temp-text "* H1\n P1\n* H2"
  1422. (org-cycle)
  1423. (goto-char (point-max))
  1424. (beginning-of-line)
  1425. (org-backward-paragraph)
  1426. (bobp)))
  1427. ;; On an affiliated keyword, jump to the first one.
  1428. (should
  1429. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1430. (search-forward "c2")
  1431. (org-backward-paragraph)
  1432. (looking-at "#\\+name")))
  1433. ;; On the second element in an item or a footnote definition, jump
  1434. ;; to item or the definition.
  1435. (should
  1436. (org-test-with-temp-text "- line1\n\n line2"
  1437. (goto-char (point-max))
  1438. (beginning-of-line)
  1439. (org-backward-paragraph)
  1440. (looking-at "- line1")))
  1441. (should
  1442. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1443. (goto-char (point-max))
  1444. (beginning-of-line)
  1445. (org-backward-paragraph)
  1446. (looking-at "\\[fn:1\\] line1")))
  1447. ;; On a table (resp. a property drawer), ignore table rows
  1448. ;; (resp. node properties).
  1449. (should
  1450. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1451. (goto-char (point-max))
  1452. (beginning-of-line)
  1453. (org-backward-paragraph)
  1454. (bobp)))
  1455. (should
  1456. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  1457. (org-backward-paragraph)
  1458. (looking-at ":PROPERTIES:")))
  1459. ;; On a source or verse block, stop before blank lines.
  1460. (should
  1461. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1462. (search-forward "L3")
  1463. (beginning-of-line)
  1464. (org-backward-paragraph)
  1465. (looking-at "L2")))
  1466. (should
  1467. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1468. (search-forward "L3")
  1469. (beginning-of-line)
  1470. (org-backward-paragraph)
  1471. (looking-at "L2"))))
  1472. (ert-deftest test-org/forward-element ()
  1473. "Test `org-forward-element' specifications."
  1474. ;; 1. At EOB: should error.
  1475. (org-test-with-temp-text "Some text\n"
  1476. (goto-char (point-max))
  1477. (should-error (org-forward-element)))
  1478. ;; 2. Standard move: expected to ignore blank lines.
  1479. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1480. (org-forward-element)
  1481. (should (looking-at (regexp-quote "Second paragraph."))))
  1482. ;; 3. Headline tests.
  1483. (org-test-with-temp-text "
  1484. * Head 1
  1485. ** Head 1.1
  1486. *** Head 1.1.1
  1487. ** Head 1.2"
  1488. ;; 3.1. At an headline beginning: move to next headline at the
  1489. ;; same level.
  1490. (goto-line 3)
  1491. (org-forward-element)
  1492. (should (looking-at (regexp-quote "** Head 1.2")))
  1493. ;; 3.2. At an headline beginning: move to parent headline if no
  1494. ;; headline at the same level.
  1495. (goto-line 3)
  1496. (org-forward-element)
  1497. (should (looking-at (regexp-quote "** Head 1.2"))))
  1498. ;; 4. Greater element tests.
  1499. (org-test-with-temp-text
  1500. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1501. ;; 4.1. At a greater element: expected to skip contents.
  1502. (org-forward-element)
  1503. (should (looking-at (regexp-quote "Outside.")))
  1504. ;; 4.2. At the end of greater element contents: expected to skip
  1505. ;; to the end of the greater element.
  1506. (goto-line 2)
  1507. (org-forward-element)
  1508. (should (looking-at (regexp-quote "Outside."))))
  1509. ;; 5. List tests.
  1510. (org-test-with-temp-text "
  1511. - item1
  1512. - sub1
  1513. - sub2
  1514. - sub3
  1515. Inner paragraph.
  1516. - item2
  1517. Outside."
  1518. ;; 5.1. At list top point: expected to move to the element after
  1519. ;; the list.
  1520. (goto-line 2)
  1521. (org-forward-element)
  1522. (should (looking-at (regexp-quote "Outside.")))
  1523. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1524. ;; beginning of line, move to next item.
  1525. (goto-line 2)
  1526. (forward-char)
  1527. (org-forward-element)
  1528. (should (looking-at "- item2"))
  1529. (goto-line 4)
  1530. (forward-char)
  1531. (org-forward-element)
  1532. (should (looking-at " - sub2"))
  1533. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1534. (goto-line 4)
  1535. (org-forward-element)
  1536. (should (looking-at (regexp-quote " Inner paragraph.")))
  1537. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1538. (goto-line 8)
  1539. (org-forward-element)
  1540. (should (looking-at (regexp-quote " Inner paragraph.")))
  1541. ;; 5.5. At an item: expected to move to next item, if any.
  1542. (goto-line 6)
  1543. (org-forward-element)
  1544. (should (looking-at " - sub3"))))
  1545. (ert-deftest test-org/backward-element ()
  1546. "Test `org-backward-element' specifications."
  1547. ;; 1. Should error at BOB.
  1548. (org-test-with-temp-text " \nParagraph."
  1549. (should-error (org-backward-element)))
  1550. ;; 2. Should move at BOB when called on the first element in buffer.
  1551. (should
  1552. (org-test-with-temp-text "\n#+TITLE: test"
  1553. (progn (forward-line)
  1554. (org-backward-element)
  1555. (bobp))))
  1556. ;; 3. Not at the beginning of an element: move at its beginning.
  1557. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1558. (goto-line 3)
  1559. (end-of-line)
  1560. (org-backward-element)
  1561. (should (looking-at (regexp-quote "Paragraph2."))))
  1562. ;; 4. Headline tests.
  1563. (org-test-with-temp-text "
  1564. * Head 1
  1565. ** Head 1.1
  1566. *** Head 1.1.1
  1567. ** Head 1.2"
  1568. ;; 4.1. At an headline beginning: move to previous headline at the
  1569. ;; same level.
  1570. (goto-line 5)
  1571. (org-backward-element)
  1572. (should (looking-at (regexp-quote "** Head 1.1")))
  1573. ;; 4.2. At an headline beginning: move to parent headline if no
  1574. ;; headline at the same level.
  1575. (goto-line 3)
  1576. (org-backward-element)
  1577. (should (looking-at (regexp-quote "* Head 1")))
  1578. ;; 4.3. At the first top-level headline: should error.
  1579. (goto-line 2)
  1580. (should-error (org-backward-element)))
  1581. ;; 5. At beginning of first element inside a greater element:
  1582. ;; expected to move to greater element's beginning.
  1583. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1584. (goto-line 3)
  1585. (org-backward-element)
  1586. (should (looking-at "#\\+BEGIN_CENTER")))
  1587. ;; 6. At the beginning of the first element in a section: should
  1588. ;; move back to headline, if any.
  1589. (should
  1590. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1591. (progn (goto-char (point-max))
  1592. (beginning-of-line)
  1593. (org-backward-element)
  1594. (org-at-heading-p))))
  1595. ;; 7. List tests.
  1596. (org-test-with-temp-text "
  1597. - item1
  1598. - sub1
  1599. - sub2
  1600. - sub3
  1601. Inner paragraph.
  1602. - item2
  1603. Outside."
  1604. ;; 7.1. At beginning of sub-list: expected to move to the
  1605. ;; paragraph before it.
  1606. (goto-line 4)
  1607. (org-backward-element)
  1608. (should (looking-at "item1"))
  1609. ;; 7.2. At an item in a list: expected to move at previous item.
  1610. (goto-line 8)
  1611. (org-backward-element)
  1612. (should (looking-at " - sub2"))
  1613. (goto-line 12)
  1614. (org-backward-element)
  1615. (should (looking-at "- item1"))
  1616. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1617. ;; beginning.
  1618. (goto-line 10)
  1619. (org-backward-element)
  1620. (should (looking-at " - sub1"))
  1621. (goto-line 15)
  1622. (org-backward-element)
  1623. (should (looking-at "- item1"))
  1624. ;; 7.4. At blank-lines before list end: expected to move to top
  1625. ;; item.
  1626. (goto-line 14)
  1627. (org-backward-element)
  1628. (should (looking-at "- item1"))))
  1629. (ert-deftest test-org/up-element ()
  1630. "Test `org-up-element' specifications."
  1631. ;; 1. At BOB or with no surrounding element: should error.
  1632. (org-test-with-temp-text "Paragraph."
  1633. (should-error (org-up-element)))
  1634. (org-test-with-temp-text "* Head1\n* Head2"
  1635. (goto-line 2)
  1636. (should-error (org-up-element)))
  1637. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1638. (goto-line 3)
  1639. (should-error (org-up-element)))
  1640. ;; 2. At an headline: move to parent headline.
  1641. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1642. (goto-line 3)
  1643. (org-up-element)
  1644. (should (looking-at "\\* Head1")))
  1645. ;; 3. Inside a greater element: move to greater element beginning.
  1646. (org-test-with-temp-text
  1647. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1648. (goto-line 3)
  1649. (org-up-element)
  1650. (should (looking-at "#\\+BEGIN_CENTER")))
  1651. ;; 4. List tests.
  1652. (org-test-with-temp-text "* Top
  1653. - item1
  1654. - sub1
  1655. - sub2
  1656. Paragraph within sub2.
  1657. - item2"
  1658. ;; 4.1. Within an item: move to the item beginning.
  1659. (goto-line 8)
  1660. (org-up-element)
  1661. (should (looking-at " - sub2"))
  1662. ;; 4.2. At an item in a sub-list: move to parent item.
  1663. (goto-line 4)
  1664. (org-up-element)
  1665. (should (looking-at "- item1"))
  1666. ;; 4.3. At an item in top list: move to beginning of whole list.
  1667. (goto-line 10)
  1668. (org-up-element)
  1669. (should (looking-at "- item1"))
  1670. ;; 4.4. Special case. At very top point: should move to parent of
  1671. ;; list.
  1672. (goto-line 2)
  1673. (org-up-element)
  1674. (should (looking-at "\\* Top"))))
  1675. (ert-deftest test-org/down-element ()
  1676. "Test `org-down-element' specifications."
  1677. ;; Error when the element hasn't got a recursive type.
  1678. (org-test-with-temp-text "Paragraph."
  1679. (should-error (org-down-element)))
  1680. ;; Error when the element has no contents
  1681. (org-test-with-temp-text "* Headline"
  1682. (should-error (org-down-element)))
  1683. ;; When at a plain-list, move to first item.
  1684. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1685. (goto-line 2)
  1686. (org-down-element)
  1687. (should (looking-at " - Item 1.1")))
  1688. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1689. (org-down-element)
  1690. (should (looking-at " Item 1")))
  1691. ;; When at a table, move to first row
  1692. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  1693. (org-down-element)
  1694. (should (looking-at " a | b |")))
  1695. ;; Otherwise, move inside the greater element.
  1696. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  1697. (org-down-element)
  1698. (should (looking-at "Paragraph"))))
  1699. (ert-deftest test-org/drag-element-backward ()
  1700. "Test `org-drag-element-backward' specifications."
  1701. ;; Standard test.
  1702. (should
  1703. (equal
  1704. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  1705. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  1706. (org-drag-element-backward)
  1707. (buffer-string))))
  1708. (should
  1709. (equal
  1710. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  1711. (org-test-with-temp-text
  1712. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  1713. (org-drag-element-backward)
  1714. (buffer-string))))
  1715. ;; Preserve blank lines.
  1716. (should
  1717. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  1718. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  1719. (org-drag-element-backward)
  1720. (buffer-string))))
  1721. ;; Preserve column.
  1722. (should
  1723. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  1724. (org-drag-element-backward)
  1725. (org-looking-at-p "2")))
  1726. ;; Error when trying to move first element of buffer.
  1727. (should-error
  1728. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1729. (org-drag-element-backward)))
  1730. ;; Error when trying to swap nested elements.
  1731. (should-error
  1732. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1733. (forward-line)
  1734. (org-drag-element-backward)))
  1735. ;; Error when trying to swap an headline element and a non-headline
  1736. ;; element.
  1737. (should-error
  1738. (org-test-with-temp-text "Test.\n* Head 1"
  1739. (forward-line)
  1740. (org-drag-element-backward)))
  1741. ;; Preserve visibility of elements and their contents.
  1742. (should
  1743. (equal '((63 . 82) (26 . 48))
  1744. (org-test-with-temp-text "
  1745. #+BEGIN_CENTER
  1746. Text.
  1747. #+END_CENTER
  1748. - item 1
  1749. #+BEGIN_QUOTE
  1750. Text.
  1751. #+END_QUOTE"
  1752. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1753. (search-backward "- item 1")
  1754. (org-drag-element-backward)
  1755. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1756. (overlays-in (point-min) (point-max)))))))
  1757. (ert-deftest test-org/drag-element-forward ()
  1758. "Test `org-drag-element-forward' specifications."
  1759. ;; 1. Error when trying to move first element of buffer.
  1760. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1761. (goto-line 3)
  1762. (should-error (org-drag-element-forward)))
  1763. ;; 2. Error when trying to swap nested elements.
  1764. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1765. (forward-line)
  1766. (should-error (org-drag-element-forward)))
  1767. ;; 3. Error when trying to swap a non-headline element and an
  1768. ;; headline.
  1769. (org-test-with-temp-text "Test.\n* Head 1"
  1770. (should-error (org-drag-element-forward)))
  1771. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1772. ;; between elements.
  1773. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1774. (search-forward "graph")
  1775. (org-drag-element-forward)
  1776. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1777. (should (looking-at " 1")))
  1778. ;; 5. Preserve visibility of elements and their contents.
  1779. (org-test-with-temp-text "
  1780. #+BEGIN_CENTER
  1781. Text.
  1782. #+END_CENTER
  1783. - item 1
  1784. #+BEGIN_QUOTE
  1785. Text.
  1786. #+END_QUOTE"
  1787. (while (search-forward "BEGIN_" nil t) (org-cycle))
  1788. (search-backward "#+BEGIN_CENTER")
  1789. (org-drag-element-forward)
  1790. (should
  1791. (equal
  1792. '((63 . 82) (26 . 48))
  1793. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  1794. (overlays-in (point-min) (point-max)))))))
  1795. ;;; Planning
  1796. (ert-deftest test-org/timestamp-has-time-p ()
  1797. "Test `org-timestamp-has-time-p' specifications."
  1798. ;; With time.
  1799. (should
  1800. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1801. (org-timestamp-has-time-p (org-element-context))))
  1802. ;; Without time.
  1803. (should-not
  1804. (org-test-with-temp-text "<2012-03-29 Thu>"
  1805. (org-timestamp-has-time-p (org-element-context)))))
  1806. (ert-deftest test-org/timestamp-format ()
  1807. "Test `org-timestamp-format' specifications."
  1808. ;; Regular test.
  1809. (should
  1810. (equal
  1811. "2012-03-29 16:40"
  1812. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1813. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  1814. ;; Range end.
  1815. (should
  1816. (equal
  1817. "2012-03-29"
  1818. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1819. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  1820. (ert-deftest test-org/timestamp-split-range ()
  1821. "Test `org-timestamp-split-range' specifications."
  1822. ;; Extract range start (active).
  1823. (should
  1824. (equal '(2012 3 29)
  1825. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1826. (let ((ts (org-timestamp-split-range (org-element-context))))
  1827. (mapcar (lambda (p) (org-element-property p ts))
  1828. '(:year-end :month-end :day-end))))))
  1829. ;; Extract range start (inactive)
  1830. (should
  1831. (equal '(2012 3 29)
  1832. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1833. (let ((ts (org-timestamp-split-range (org-element-context))))
  1834. (mapcar (lambda (p) (org-element-property p ts))
  1835. '(:year-end :month-end :day-end))))))
  1836. ;; Extract range end (active).
  1837. (should
  1838. (equal '(2012 3 30)
  1839. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1840. (let ((ts (org-timestamp-split-range
  1841. (org-element-context) t)))
  1842. (mapcar (lambda (p) (org-element-property p ts))
  1843. '(:year-end :month-end :day-end))))))
  1844. ;; Extract range end (inactive)
  1845. (should
  1846. (equal '(2012 3 30)
  1847. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1848. (let ((ts (org-timestamp-split-range
  1849. (org-element-context) t)))
  1850. (mapcar (lambda (p) (org-element-property p ts))
  1851. '(:year-end :month-end :day-end))))))
  1852. ;; Return the timestamp if not a range.
  1853. (should
  1854. (org-test-with-temp-text "[2012-03-29 Thu]"
  1855. (let* ((ts-orig (org-element-context))
  1856. (ts-copy (org-timestamp-split-range ts-orig)))
  1857. (eq ts-orig ts-copy))))
  1858. (should
  1859. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1860. (let* ((ts-orig (org-element-context))
  1861. (ts-copy (org-timestamp-split-range ts-orig)))
  1862. (eq ts-orig ts-copy))))
  1863. ;; Check that parent is the same when a range was split.
  1864. (should
  1865. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  1866. (let* ((ts-orig (org-element-context))
  1867. (ts-copy (org-timestamp-split-range ts-orig)))
  1868. (eq (org-element-property :parent ts-orig)
  1869. (org-element-property :parent ts-copy))))))
  1870. (ert-deftest test-org/timestamp-translate ()
  1871. "Test `org-timestamp-translate' specifications."
  1872. ;; Translate whole date range.
  1873. (should
  1874. (equal "<29>--<30>"
  1875. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1876. (let ((org-display-custom-times t)
  1877. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1878. (org-timestamp-translate (org-element-context))))))
  1879. ;; Translate date range start.
  1880. (should
  1881. (equal "<29>"
  1882. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1883. (let ((org-display-custom-times t)
  1884. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1885. (org-timestamp-translate (org-element-context) 'start)))))
  1886. ;; Translate date range end.
  1887. (should
  1888. (equal "<30>"
  1889. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  1890. (let ((org-display-custom-times t)
  1891. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1892. (org-timestamp-translate (org-element-context) 'end)))))
  1893. ;; Translate time range.
  1894. (should
  1895. (equal "<08>--<16>"
  1896. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  1897. (let ((org-display-custom-times t)
  1898. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  1899. (org-timestamp-translate (org-element-context))))))
  1900. ;; Translate non-range timestamp.
  1901. (should
  1902. (equal "<29>"
  1903. (org-test-with-temp-text "<2012-03-29 Thu>"
  1904. (let ((org-display-custom-times t)
  1905. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1906. (org-timestamp-translate (org-element-context))))))
  1907. ;; Do not change `diary' timestamps.
  1908. (should
  1909. (equal "<%%(org-float t 4 2)>"
  1910. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  1911. (let ((org-display-custom-times t)
  1912. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  1913. (org-timestamp-translate (org-element-context)))))))
  1914. ;;; Property API
  1915. (ert-deftest test-org/buffer-property-keys ()
  1916. "Test `org-buffer-property-keys' specifications."
  1917. ;; Retrieve properties accross siblings.
  1918. (should
  1919. (equal '("A" "B")
  1920. (org-test-with-temp-text "
  1921. * H1
  1922. :PROPERTIES:
  1923. :A: 1
  1924. :END:
  1925. * H2
  1926. :PROPERTIES:
  1927. :B: 1
  1928. :END:"
  1929. (org-buffer-property-keys))))
  1930. ;; Retrieve properties accross children.
  1931. (should
  1932. (equal '("A" "B")
  1933. (org-test-with-temp-text "
  1934. * H1
  1935. :PROPERTIES:
  1936. :A: 1
  1937. :END:
  1938. ** H2
  1939. :PROPERTIES:
  1940. :B: 1
  1941. :END:"
  1942. (org-buffer-property-keys))))
  1943. ;; Retrieve muliple properties in the same drawer.
  1944. (should
  1945. (equal '("A" "B")
  1946. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  1947. (org-buffer-property-keys))))
  1948. ;; Ignore extension symbol in property name.
  1949. (should
  1950. (equal '("A")
  1951. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  1952. (org-buffer-property-keys))))
  1953. ;; With non-nil COLUMNS, extract property names from columns.
  1954. (should
  1955. (equal '("A" "B")
  1956. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  1957. (org-buffer-property-keys nil nil t))))
  1958. (should
  1959. (equal '("A" "B" "COLUMNS")
  1960. (org-test-with-temp-text
  1961. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  1962. (org-buffer-property-keys nil nil t)))))
  1963. (ert-deftest test-org/property-values ()
  1964. "Test `org-property-values' specifications."
  1965. ;; Regular test.
  1966. (should
  1967. (equal '("2" "1")
  1968. (org-test-with-temp-text
  1969. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  1970. (org-property-values "A"))))
  1971. ;; Ignore empty values.
  1972. (should-not
  1973. (org-test-with-temp-text
  1974. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  1975. (org-property-values "A")))
  1976. ;; Take into consideration extended values.
  1977. (should
  1978. (equal '("1 2")
  1979. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  1980. (org-property-values "A")))))
  1981. (ert-deftest test-org/entry-delete ()
  1982. "Test `org-entry-delete' specifications."
  1983. ;; Regular test.
  1984. (should
  1985. (string-match
  1986. " *:PROPERTIES:\n *:B: +2\n *:END:"
  1987. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  1988. (org-entry-delete (point) "A")
  1989. (buffer-string))))
  1990. ;; When last property is removed, remove the property drawer.
  1991. (should-not
  1992. (string-match
  1993. ":PROPERTIES:"
  1994. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  1995. (org-entry-delete (point) "A")
  1996. (buffer-string)))))
  1997. (ert-deftest test-org/entry-get ()
  1998. "Test `org-entry-get' specifications."
  1999. ;; Regular test.
  2000. (should
  2001. (equal "1"
  2002. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2003. (org-entry-get (point) "A"))))
  2004. ;; Ignore case.
  2005. (should
  2006. (equal "1"
  2007. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2008. (org-entry-get (point) "a"))))
  2009. ;; Handle extended values, both before and after base value.
  2010. (should
  2011. (equal "1 2 3"
  2012. (org-test-with-temp-text
  2013. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2014. (org-entry-get (point) "A"))))
  2015. ;; Empty values are returned as the empty string.
  2016. (should
  2017. (equal ""
  2018. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  2019. (org-entry-get (point) "A"))))
  2020. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  2021. ;; otherwise, return nil.
  2022. (should-not
  2023. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2024. (org-entry-get (point) "A")))
  2025. (should
  2026. (equal "nil"
  2027. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2028. (org-entry-get (point) "A" nil t))))
  2029. ;; Return nil when no property is found, independently on the
  2030. ;; LITERAL-NIL argument.
  2031. (should-not
  2032. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2033. (org-entry-get (point) "B")))
  2034. (should-not
  2035. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2036. (org-entry-get (point) "B" nil t)))
  2037. ;; Handle inheritance, when allowed.
  2038. (should
  2039. (equal
  2040. "1"
  2041. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2042. (org-entry-get (point) "A" t))))
  2043. (should
  2044. (equal
  2045. "1"
  2046. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2047. (let ((org-use-property-inheritance t))
  2048. (org-entry-get (point) "A" 'selective)))))
  2049. (should-not
  2050. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2051. (let ((org-use-property-inheritance nil))
  2052. (org-entry-get (point) "A" 'selective)))))
  2053. (ert-deftest test-org/entry-properties ()
  2054. "Test `org-entry-properties' specifications."
  2055. ;; Get "TODO" property.
  2056. (should
  2057. (equal "TODO"
  2058. (org-test-with-temp-text "* TODO H"
  2059. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  2060. (should-not
  2061. (org-test-with-temp-text "* H"
  2062. (assoc "TODO" (org-entry-properties nil "TODO"))))
  2063. ;; Get "PRIORITY" property.
  2064. (should
  2065. (equal "A"
  2066. (org-test-with-temp-text "* [#A] H"
  2067. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  2068. (should-not
  2069. (org-test-with-temp-text "* H"
  2070. (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
  2071. ;; Get "FILE" property.
  2072. (should
  2073. (org-test-with-temp-text-in-file "* H\nParagraph"
  2074. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  2075. (buffer-file-name))))
  2076. (should-not
  2077. (org-test-with-temp-text "* H\nParagraph"
  2078. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  2079. ;; Get "TAGS" property.
  2080. (should
  2081. (equal ":tag1:tag2:"
  2082. (org-test-with-temp-text "* H :tag1:tag2:"
  2083. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  2084. (should-not
  2085. (org-test-with-temp-text "* H"
  2086. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  2087. ;; Get "ALLTAGS" property.
  2088. (should
  2089. (equal ":tag1:tag2:"
  2090. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  2091. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  2092. (should-not
  2093. (org-test-with-temp-text "* H"
  2094. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  2095. ;; Get "BLOCKED" property.
  2096. (should
  2097. (equal "t"
  2098. (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
  2099. (let ((org-enforce-todo-dependencies t)
  2100. (org-blocker-hook
  2101. '(org-block-todo-from-children-or-siblings-or-parent)))
  2102. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2103. (should
  2104. (equal ""
  2105. (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
  2106. (let ((org-enforce-todo-dependencies t))
  2107. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2108. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  2109. (should
  2110. (equal
  2111. "[2012-03-29 thu.]"
  2112. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  2113. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  2114. (should-not
  2115. (org-test-with-temp-text "* H"
  2116. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  2117. (should
  2118. (equal
  2119. "<2014-03-04 tue.>"
  2120. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2121. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  2122. (should-not
  2123. (org-test-with-temp-text "* H"
  2124. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  2125. (should
  2126. (equal
  2127. "<2014-03-04 tue.>"
  2128. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2129. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  2130. (should-not
  2131. (org-test-with-temp-text "* H"
  2132. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  2133. ;; Get "CATEGORY"
  2134. (should
  2135. (equal "cat"
  2136. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  2137. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2138. (should
  2139. (equal "cat"
  2140. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  2141. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2142. ;; Get standard properties.
  2143. (should
  2144. (equal "1"
  2145. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2146. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2147. ;; Handle extended properties.
  2148. (should
  2149. (equal "1 2 3"
  2150. (org-test-with-temp-text
  2151. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2152. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2153. (should
  2154. (equal "1 2 3"
  2155. (org-test-with-temp-text
  2156. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  2157. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2158. ;; Ignore forbidden (special) properties.
  2159. (should-not
  2160. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  2161. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  2162. (ert-deftest test-org/entry-put ()
  2163. "Test `org-entry-put' specifications."
  2164. ;; Error when not a string or nil.
  2165. (should-error
  2166. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  2167. (org-entry-put 1 "test" 2)))
  2168. ;; Set "TODO" property.
  2169. (should
  2170. (string-match (regexp-quote " TODO H")
  2171. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2172. (org-entry-put (point) "TODO" "TODO")
  2173. (buffer-string))))
  2174. (should
  2175. (string-match (regexp-quote "* H")
  2176. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2177. (org-entry-put (point) "TODO" nil)
  2178. (buffer-string))))
  2179. ;; Set "PRIORITY" property.
  2180. (should
  2181. (equal "* [#A] H"
  2182. (org-test-with-temp-text "* [#B] H"
  2183. (org-entry-put (point) "PRIORITY" "A")
  2184. (buffer-string))))
  2185. (should
  2186. (equal "* H"
  2187. (org-test-with-temp-text "* [#B] H"
  2188. (org-entry-put (point) "PRIORITY" nil)
  2189. (buffer-string))))
  2190. ;; Set "SCHEDULED" property.
  2191. (should
  2192. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  2193. (org-test-with-temp-text "* H"
  2194. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  2195. (buffer-string))))
  2196. (should
  2197. (string= "* H\n"
  2198. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2199. (org-entry-put (point) "SCHEDULED" nil)
  2200. (buffer-string))))
  2201. (should
  2202. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  2203. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2204. (org-entry-put (point) "SCHEDULED" "earlier")
  2205. (buffer-string))))
  2206. (should
  2207. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  2208. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2209. (org-entry-put (point) "SCHEDULED" "later")
  2210. (buffer-string))))
  2211. ;; Set "DEADLINE" property.
  2212. (should
  2213. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  2214. (org-test-with-temp-text "* H"
  2215. (org-entry-put (point) "DEADLINE" "2014-03-04")
  2216. (buffer-string))))
  2217. (should
  2218. (string= "* H\n"
  2219. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2220. (org-entry-put (point) "DEADLINE" nil)
  2221. (buffer-string))))
  2222. (should
  2223. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  2224. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2225. (org-entry-put (point) "DEADLINE" "earlier")
  2226. (buffer-string))))
  2227. (should
  2228. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  2229. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2230. (org-entry-put (point) "DEADLINE" "later")
  2231. (buffer-string))))
  2232. ;; Regular properties, with or without pre-existing drawer.
  2233. (should
  2234. (string-match "^ *:A: +2$"
  2235. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2236. (org-entry-put (point) "A" "2")
  2237. (buffer-string))))
  2238. (should
  2239. (string-match "^ *:A: +1$"
  2240. (org-test-with-temp-text "* H"
  2241. (org-entry-put (point) "A" "1")
  2242. (buffer-string)))))
  2243. ;;; Radio Targets
  2244. (ert-deftest test-org/update-radio-target-regexp ()
  2245. "Test `org-update-radio-target-regexp' specifications."
  2246. ;; Properly update cache with no previous radio target regexp.
  2247. (should
  2248. (eq 'link
  2249. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2250. (save-excursion (goto-char (point-max)) (org-element-context))
  2251. (insert "<<<")
  2252. (search-forward "o")
  2253. (insert ">>>")
  2254. (org-update-radio-target-regexp)
  2255. (goto-char (point-max))
  2256. (org-element-type (org-element-context)))))
  2257. ;; Properly update cache with previous radio target regexp.
  2258. (should
  2259. (eq 'link
  2260. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2261. (save-excursion (goto-char (point-max)) (org-element-context))
  2262. (insert "<<<")
  2263. (search-forward "o")
  2264. (insert ">>>")
  2265. (org-update-radio-target-regexp)
  2266. (search-backward "r")
  2267. (delete-char 5)
  2268. (insert "new")
  2269. (org-update-radio-target-regexp)
  2270. (goto-char (point-max))
  2271. (delete-region (line-beginning-position) (point))
  2272. (insert "new")
  2273. (org-element-type (org-element-context))))))
  2274. ;;; Visibility
  2275. (ert-deftest test-org/flag-drawer ()
  2276. "Test `org-flag-drawer' specifications."
  2277. ;; Hide drawer.
  2278. (should
  2279. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2280. (org-flag-drawer t)
  2281. (get-char-property (line-end-position) 'invisible)))
  2282. ;; Show drawer.
  2283. (should-not
  2284. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2285. (org-flag-drawer t)
  2286. (org-flag-drawer nil)
  2287. (get-char-property (line-end-position) 'invisible)))
  2288. ;; Test optional argument.
  2289. (should
  2290. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2291. (let ((drawer (save-excursion (search-forward ":D2")
  2292. (org-element-at-point))))
  2293. (org-flag-drawer t drawer)
  2294. (get-char-property (progn (search-forward ":D2") (line-end-position))
  2295. 'invisible))))
  2296. (should-not
  2297. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  2298. (let ((drawer (save-excursion (search-forward ":D2")
  2299. (org-element-at-point))))
  2300. (org-flag-drawer t drawer)
  2301. (get-char-property (line-end-position) 'invisible))))
  2302. ;; Do not hide fake drawers.
  2303. (should-not
  2304. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  2305. (forward-line 1)
  2306. (org-flag-drawer t)
  2307. (get-char-property (line-end-position) 'invisible)))
  2308. ;; Do not hide incomplete drawers.
  2309. (should-not
  2310. (org-test-with-temp-text ":D:\nparagraph"
  2311. (forward-line 1)
  2312. (org-flag-drawer t)
  2313. (get-char-property (line-end-position) 'invisible)))
  2314. ;; Do not hide drawers when called from final blank lines.
  2315. (should-not
  2316. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  2317. (goto-char (point-max))
  2318. (org-flag-drawer t)
  2319. (goto-char (point-min))
  2320. (get-char-property (line-end-position) 'invisible)))
  2321. ;; Don't leave point in an invisible part of the buffer when hiding
  2322. ;; a drawer away.
  2323. (should-not
  2324. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  2325. (goto-char (point-max))
  2326. (org-flag-drawer t)
  2327. (get-char-property (point) 'invisible))))
  2328. (ert-deftest test-org/hide-block-toggle ()
  2329. "Test `org-hide-block-toggle' specifications."
  2330. ;; Error when not at a block.
  2331. (should-error
  2332. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  2333. (org-hide-block-toggle 'off)
  2334. (get-char-property (line-end-position) 'invisible)))
  2335. ;; Hide block.
  2336. (should
  2337. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2338. (org-hide-block-toggle)
  2339. (get-char-property (line-end-position) 'invisible)))
  2340. (should
  2341. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  2342. (org-hide-block-toggle)
  2343. (get-char-property (line-end-position) 'invisible)))
  2344. ;; Show block unconditionally when optional argument is `off'.
  2345. (should-not
  2346. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2347. (org-hide-block-toggle)
  2348. (org-hide-block-toggle 'off)
  2349. (get-char-property (line-end-position) 'invisible)))
  2350. (should-not
  2351. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2352. (org-hide-block-toggle 'off)
  2353. (get-char-property (line-end-position) 'invisible)))
  2354. ;; Hide block unconditionally when optional argument is non-nil.
  2355. (should
  2356. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2357. (org-hide-block-toggle t)
  2358. (get-char-property (line-end-position) 'invisible)))
  2359. (should
  2360. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  2361. (org-hide-block-toggle)
  2362. (org-hide-block-toggle t)
  2363. (get-char-property (line-end-position) 'invisible)))
  2364. ;; Do not hide block when called from final blank lines.
  2365. (should-not
  2366. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  2367. (org-hide-block-toggle)
  2368. (goto-char (point-min))
  2369. (get-char-property (line-end-position) 'invisible)))
  2370. ;; Don't leave point in an invisible part of the buffer when hiding
  2371. ;; a block away.
  2372. (should-not
  2373. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  2374. (org-hide-block-toggle)
  2375. (get-char-property (point) 'invisible))))
  2376. (ert-deftest test-org/hide-block-toggle-maybe ()
  2377. "Test `org-hide-block-toggle-maybe' specifications."
  2378. (should
  2379. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  2380. (org-hide-block-toggle-maybe)))
  2381. (should-not
  2382. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  2383. (provide 'test-org)
  2384. ;;; test-org.el ends here