test-org.el 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472
  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. ;; Relative date applied to current time if there is single
  180. ;; plus/minus, or to default date when there are two of them.
  181. (should
  182. (equal
  183. "2015-03-04"
  184. (flet ((current-time () (apply #'encode-time
  185. (org-parse-time-string "2014-03-04"))))
  186. (org-read-date
  187. t nil "+1y" nil
  188. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  189. (should
  190. (equal
  191. "2013-03-29"
  192. (flet ((current-time () (apply #'encode-time
  193. (org-parse-time-string "2014-03-04"))))
  194. (org-read-date
  195. t nil "++1y" nil
  196. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  197. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  198. ;; dates (relatively to now) when incomplete. Otherwise, use
  199. ;; default date.
  200. (should
  201. (equal
  202. "2014-04-01"
  203. (flet ((current-time () (apply #'encode-time
  204. (org-parse-time-string "2014-03-04"))))
  205. (let ((org-read-date-prefer-future t))
  206. (org-read-date t nil "1")))))
  207. (should
  208. (equal
  209. "2013-03-04"
  210. (flet ((current-time () (apply #'encode-time
  211. (org-parse-time-string "2012-03-29"))))
  212. (let ((org-read-date-prefer-future t))
  213. (org-read-date t nil "3-4")))))
  214. (should
  215. (equal
  216. "2012-03-04"
  217. (flet ((current-time () (apply #'encode-time
  218. (org-parse-time-string "2012-03-29"))))
  219. (let ((org-read-date-prefer-future nil))
  220. (org-read-date t nil "3-4")))))
  221. ;; When set to `org-read-date-prefer-future' is set to `time', read
  222. ;; day is moved to tomorrow if specified hour is before current
  223. ;; time. However, it only happens in no other part of the date is
  224. ;; specified.
  225. (should
  226. (equal
  227. "2012-03-30"
  228. (flet ((current-time () (apply #'encode-time
  229. (org-parse-time-string "2012-03-29 16:40"))))
  230. (let ((org-read-date-prefer-future 'time))
  231. (org-read-date t nil "00:40" nil)))))
  232. (should-not
  233. (equal
  234. "2012-03-30"
  235. (flet ((current-time () (apply #'encode-time
  236. (org-parse-time-string "2012-03-29 16:40"))))
  237. (let ((org-read-date-prefer-future 'time))
  238. (org-read-date t nil "29 00:40" nil)))))
  239. ;; Caveat: `org-read-date-prefer-future' always refers to current
  240. ;; time, not default time, when they differ.
  241. (should
  242. (equal
  243. "2014-04-01"
  244. (flet ((current-time
  245. () (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  246. (let ((org-read-date-prefer-future t))
  247. (org-read-date
  248. t nil "1" nil
  249. (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
  250. (should
  251. (equal
  252. "2014-03-25"
  253. (flet ((current-time
  254. () (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  255. (let ((org-read-date-prefer-future t))
  256. (org-read-date
  257. t nil "25" nil
  258. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
  259. (ert-deftest test-org/org-parse-time-string ()
  260. "Test `org-parse-time-string'."
  261. (should (equal (org-parse-time-string "2012-03-29 16:40")
  262. '(0 40 16 29 3 2012 nil nil nil)))
  263. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  264. '(0 40 16 29 3 2012 nil nil nil)))
  265. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  266. '(0 40 16 29 3 2012 nil nil nil)))
  267. (should (equal (org-parse-time-string "<2012-03-29>")
  268. '(0 0 0 29 3 2012 nil nil nil)))
  269. (should (equal (org-parse-time-string "<2012-03-29>" t)
  270. '(0 nil nil 29 3 2012 nil nil nil))))
  271. ;;; Drawers
  272. (ert-deftest test-org/insert-property-drawer ()
  273. "Test `org-insert-property-drawer' specifications."
  274. ;; Error before first headline.
  275. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  276. ;; Insert drawer right after headline if there is no planning line,
  277. ;; or after it otherwise.
  278. (should
  279. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  280. (org-test-with-temp-text "* H\nParagraph<point>"
  281. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  282. (buffer-string))))
  283. (should
  284. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  285. (org-test-with-temp-text
  286. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  287. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  288. (buffer-string))))
  289. ;; Indent inserted drawer.
  290. (should
  291. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  292. (org-test-with-temp-text "* H\nParagraph<point>"
  293. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  294. (buffer-string))))
  295. ;; Handle insertion at eob.
  296. (should
  297. (equal "* H\n:PROPERTIES:\n:END:\n"
  298. (org-test-with-temp-text "* H"
  299. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  300. (buffer-string))))
  301. ;; Skip inlinetasks before point.
  302. (when (featurep 'org-inlinetask)
  303. (should
  304. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  305. (org-test-with-temp-text
  306. "* H\n*************** I\n*************** END\nP<point>"
  307. (let ((org-adapt-indentation nil)
  308. (org-inlinetask-min-level 15))
  309. (org-insert-property-drawer))
  310. (buffer-string)))))
  311. ;; Correctly set drawer in an inlinetask.
  312. (when (featurep 'org-inlinetask)
  313. (should
  314. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  315. (org-test-with-temp-text
  316. "* H\n*************** I\nP<point>\n*************** END"
  317. (let ((org-adapt-indentation nil)
  318. (org-inlinetask-min-level 15))
  319. (org-insert-property-drawer))
  320. (buffer-string))))))
  321. ;;; Filling
  322. (ert-deftest test-org/fill-paragraph ()
  323. "Test `org-fill-paragraph' specifications."
  324. ;; At an Org table, align it.
  325. (should
  326. (equal "| a |\n"
  327. (org-test-with-temp-text "|a|"
  328. (org-fill-paragraph)
  329. (buffer-string))))
  330. (should
  331. (equal "#+name: table\n| a |\n"
  332. (org-test-with-temp-text "#+name: table\n| a |"
  333. (org-fill-paragraph)
  334. (buffer-string))))
  335. ;; At a paragraph, preserve line breaks.
  336. (org-test-with-temp-text "some \\\\\nlong\ntext"
  337. (let ((fill-column 20))
  338. (org-fill-paragraph)
  339. (should (equal (buffer-string) "some \\\\\nlong text"))))
  340. ;; Correctly fill a paragraph when point is at its very end.
  341. (should
  342. (equal "A B"
  343. (org-test-with-temp-text "A\nB"
  344. (let ((fill-column 20))
  345. (goto-char (point-max))
  346. (org-fill-paragraph)
  347. (buffer-string)))))
  348. ;; Correctly fill the last paragraph of a greater element.
  349. (should
  350. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  351. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  352. (let ((fill-column 8))
  353. (forward-line)
  354. (end-of-line)
  355. (org-fill-paragraph)
  356. (buffer-string)))))
  357. ;; Correctly fill an element in a narrowed buffer.
  358. (should
  359. (equal "01234\n6"
  360. (org-test-with-temp-text "01234 6789"
  361. (let ((fill-column 5))
  362. (narrow-to-region 1 8)
  363. (org-fill-paragraph)
  364. (buffer-string)))))
  365. ;; Handle `adaptive-fill-regexp' in paragraphs.
  366. (should
  367. (equal "> a b"
  368. (org-test-with-temp-text "> a\n> b"
  369. (let ((fill-column 5)
  370. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  371. (org-fill-paragraph)
  372. (buffer-string)))))
  373. ;; Special case: Fill first paragraph when point is at an item or
  374. ;; a plain-list or a footnote reference.
  375. (should
  376. (equal "- A B"
  377. (org-test-with-temp-text "- A\n B"
  378. (let ((fill-column 20))
  379. (org-fill-paragraph)
  380. (buffer-string)))))
  381. (should
  382. (equal "[fn:1] A B"
  383. (org-test-with-temp-text "[fn:1] A\nB"
  384. (let ((fill-column 20))
  385. (org-fill-paragraph)
  386. (buffer-string)))))
  387. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  388. (let ((fill-column 20))
  389. (org-fill-paragraph)
  390. (should (equal (buffer-string)
  391. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  392. ;; Fill contents of `comment-block' elements.
  393. (should
  394. (equal
  395. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  396. (let ((fill-column 20))
  397. (forward-line)
  398. (org-fill-paragraph)
  399. (buffer-string)))
  400. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  401. ;; Fill `comment' elements.
  402. (should
  403. (equal " # A B"
  404. (org-test-with-temp-text " # A\n # B"
  405. (let ((fill-column 20))
  406. (org-fill-paragraph)
  407. (buffer-string)))))
  408. ;; Do not mix consecutive comments when filling one of them.
  409. (should
  410. (equal "# A B\n\n# C"
  411. (org-test-with-temp-text "# A\n# B\n\n# C"
  412. (let ((fill-column 20))
  413. (org-fill-paragraph)
  414. (buffer-string)))))
  415. ;; Use commented empty lines as separators when filling comments.
  416. (should
  417. (equal "# A B\n#\n# C"
  418. (org-test-with-temp-text "# A\n# B\n#\n# C"
  419. (let ((fill-column 20))
  420. (org-fill-paragraph)
  421. (buffer-string)))))
  422. ;; Handle `adaptive-fill-regexp' in comments.
  423. (should
  424. (equal "# > a b"
  425. (org-test-with-temp-text "# > a\n# > b"
  426. (let ((fill-column 20)
  427. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  428. (org-fill-paragraph)
  429. (buffer-string)))))
  430. ;; Do nothing at affiliated keywords.
  431. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  432. (let ((fill-column 20))
  433. (org-fill-paragraph)
  434. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  435. ;; Do not move point after table when filling a table.
  436. (should-not
  437. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  438. (forward-char)
  439. (org-fill-paragraph)
  440. (eobp))))
  441. (ert-deftest test-org/auto-fill-function ()
  442. "Test auto-filling features."
  443. ;; Auto fill paragraph.
  444. (should
  445. (equal "12345\n7890"
  446. (org-test-with-temp-text "12345 7890"
  447. (let ((fill-column 5))
  448. (end-of-line)
  449. (org-auto-fill-function)
  450. (buffer-string)))))
  451. ;; Auto fill first paragraph in an item.
  452. (should
  453. (equal "- 12345\n 7890"
  454. (org-test-with-temp-text "- 12345 7890"
  455. (let ((fill-column 7))
  456. (end-of-line)
  457. (org-auto-fill-function)
  458. (buffer-string)))))
  459. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  460. (should
  461. (equal "> 12345\n 7890"
  462. (org-test-with-temp-text "> 12345 7890"
  463. (let ((fill-column 10)
  464. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  465. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  466. (end-of-line)
  467. (org-auto-fill-function)
  468. (buffer-string)))))
  469. (should
  470. (equal "> 12345\n> 12345\n> 7890"
  471. (org-test-with-temp-text "> 12345\n> 12345 7890"
  472. (let ((fill-column 10)
  473. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  474. (goto-char (point-max))
  475. (org-auto-fill-function)
  476. (buffer-string)))))
  477. (should-not
  478. (equal " 12345\n *12345\n *12345"
  479. (org-test-with-temp-text " 12345\n *12345 12345"
  480. (let ((fill-column 10)
  481. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  482. (goto-char (point-max))
  483. (org-auto-fill-function)
  484. (buffer-string)))))
  485. ;; Auto fill comments.
  486. (should
  487. (equal " # 12345\n # 7890"
  488. (org-test-with-temp-text " # 12345 7890"
  489. (let ((fill-column 10))
  490. (end-of-line)
  491. (org-auto-fill-function)
  492. (buffer-string)))))
  493. ;; A hash within a line isn't a comment.
  494. (should-not
  495. (equal "12345 # 7890\n# 1"
  496. (org-test-with-temp-text "12345 # 7890 1"
  497. (let ((fill-column 12))
  498. (end-of-line)
  499. (org-auto-fill-function)
  500. (buffer-string)))))
  501. ;; Correctly interpret empty prefix.
  502. (should-not
  503. (equal "# a\n# b\nRegular\n# paragraph"
  504. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  505. (let ((fill-column 12))
  506. (end-of-line 3)
  507. (org-auto-fill-function)
  508. (buffer-string)))))
  509. ;; Comment block: auto fill contents.
  510. (should
  511. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  512. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  513. (let ((fill-column 5))
  514. (forward-line)
  515. (end-of-line)
  516. (org-auto-fill-function)
  517. (buffer-string)))))
  518. (should
  519. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  520. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  521. (let ((fill-column 5))
  522. (forward-line)
  523. (end-of-line)
  524. (org-auto-fill-function)
  525. (buffer-string)))))
  526. ;; Do not fill if a new item could be created.
  527. (should-not
  528. (equal "12345\n- 90"
  529. (org-test-with-temp-text "12345 - 90"
  530. (let ((fill-column 5))
  531. (end-of-line)
  532. (org-auto-fill-function)
  533. (buffer-string)))))
  534. ;; Do not fill if a line break could be introduced.
  535. (should-not
  536. (equal "123\\\\\n7890"
  537. (org-test-with-temp-text "123\\\\ 7890"
  538. (let ((fill-column 6))
  539. (end-of-line)
  540. (org-auto-fill-function)
  541. (buffer-string)))))
  542. ;; Do not fill affiliated keywords.
  543. (should-not
  544. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  545. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  546. (let ((fill-column 20))
  547. (end-of-line)
  548. (org-auto-fill-function)
  549. (buffer-string))))))
  550. ;;; Indentation
  551. (ert-deftest test-org/indent-line ()
  552. "Test `org-indent-line' specifications."
  553. ;; Do not indent footnote definitions or headlines.
  554. (should
  555. (zerop
  556. (org-test-with-temp-text "* H"
  557. (org-indent-line)
  558. (org-get-indentation))))
  559. (should
  560. (zerop
  561. (org-test-with-temp-text "[fn:1] fn"
  562. (let ((org-adapt-indentation t)) (org-indent-line))
  563. (org-get-indentation))))
  564. ;; Do not indent before first headline.
  565. (should
  566. (zerop
  567. (org-test-with-temp-text ""
  568. (org-indent-line)
  569. (org-get-indentation))))
  570. ;; Indent according to headline level otherwise, unless
  571. ;; `org-adapt-indentation' is nil.
  572. (should
  573. (= 2
  574. (org-test-with-temp-text "* H\nA"
  575. (forward-line)
  576. (let ((org-adapt-indentation t)) (org-indent-line))
  577. (org-get-indentation))))
  578. (should
  579. (= 2
  580. (org-test-with-temp-text "* H\n\nA"
  581. (forward-line)
  582. (let ((org-adapt-indentation t)) (org-indent-line))
  583. (org-get-indentation))))
  584. (should
  585. (zerop
  586. (org-test-with-temp-text "* H\nA"
  587. (forward-line)
  588. (let ((org-adapt-indentation nil)) (org-indent-line))
  589. (org-get-indentation))))
  590. ;; Indenting preserves point position.
  591. (should
  592. (org-test-with-temp-text "* H\nAB"
  593. (forward-line)
  594. (forward-char)
  595. (let ((org-adapt-indentation t)) (org-indent-line))
  596. (looking-at "B")))
  597. ;; Do not change indentation at an item.
  598. (should
  599. (= 1
  600. (org-test-with-temp-text "* H\n - A"
  601. (forward-line)
  602. (let ((org-adapt-indentation t)) (org-indent-line))
  603. (org-get-indentation))))
  604. ;; On blank lines at the end of a list, indent like last element
  605. ;; within it if the line is still in the list. Otherwise, indent
  606. ;; like the whole list.
  607. (should
  608. (= 4
  609. (org-test-with-temp-text "* H\n- A\n - AA\n"
  610. (goto-char (point-max))
  611. (let ((org-adapt-indentation t)) (org-indent-line))
  612. (org-get-indentation))))
  613. (should
  614. (zerop
  615. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
  616. (goto-char (point-max))
  617. (let ((org-adapt-indentation t)) (org-indent-line))
  618. (org-get-indentation))))
  619. ;; Likewise, on a blank line at the end of a footnote definition,
  620. ;; indent at column 0 if line belongs to the definition. Otherwise,
  621. ;; indent like the definition itself.
  622. (should
  623. (zerop
  624. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  625. (goto-char (point-max))
  626. (let ((org-adapt-indentation t)) (org-indent-line))
  627. (org-get-indentation))))
  628. (should
  629. (zerop
  630. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  631. (goto-char (point-max))
  632. (let ((org-adapt-indentation t)) (org-indent-line))
  633. (org-get-indentation))))
  634. ;; After the end of the contents of a greater element, indent like
  635. ;; the beginning of the element.
  636. (should
  637. (= 1
  638. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  639. (forward-line 2)
  640. (org-indent-line)
  641. (org-get-indentation))))
  642. ;; On blank lines after a paragraph, indent like its last non-empty
  643. ;; line.
  644. (should
  645. (= 1
  646. (org-test-with-temp-text " Paragraph\n\n<point>"
  647. (org-indent-line)
  648. (org-get-indentation))))
  649. ;; At the first line of an element, indent like previous element's
  650. ;; first line, ignoring footnotes definitions and inline tasks, or
  651. ;; according to parent.
  652. (should
  653. (= 2
  654. (org-test-with-temp-text "A\n\n B\n\nC"
  655. (goto-char (point-max))
  656. (org-indent-line)
  657. (org-get-indentation))))
  658. (should
  659. (= 1
  660. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  661. (goto-char (point-max))
  662. (org-indent-line)
  663. (org-get-indentation))))
  664. (should
  665. (= 1
  666. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  667. (forward-line 1)
  668. (org-indent-line)
  669. (org-get-indentation))))
  670. ;; Within code part of a source block, use language major mode if
  671. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  672. ;; according to line above.
  673. (should
  674. (= 6
  675. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  676. (forward-line 2)
  677. (let ((org-src-tab-acts-natively t)
  678. (org-edit-src-content-indentation 0))
  679. (org-indent-line))
  680. (org-get-indentation))))
  681. (should
  682. (= 1
  683. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  684. (forward-line 2)
  685. (let ((org-src-tab-acts-natively nil)
  686. (org-edit-src-content-indentation 0))
  687. (org-indent-line))
  688. (org-get-indentation))))
  689. ;; Otherwise, indent like the first non-blank line above.
  690. (should
  691. (zerop
  692. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  693. (forward-line 3)
  694. (org-indent-line)
  695. (org-get-indentation))))
  696. ;; Align node properties according to `org-property-format'. Handle
  697. ;; nicely empty values.
  698. (should
  699. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  700. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  701. (let ((org-property-format "%-10s %s")) (org-indent-line))
  702. (buffer-string))))
  703. (should
  704. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  705. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  706. (let ((org-property-format "%-10s %s")) (org-indent-line))
  707. (buffer-string)))))
  708. (ert-deftest test-org/indent-region ()
  709. "Test `org-indent-region' specifications."
  710. ;; Indent paragraph.
  711. (should
  712. (equal "A\nB\nC"
  713. (org-test-with-temp-text " A\nB\n C"
  714. (org-indent-region (point-min) (point-max))
  715. (buffer-string))))
  716. ;; Indent greater elements along with their contents.
  717. (should
  718. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  719. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  720. (org-indent-region (point-min) (point-max))
  721. (buffer-string))))
  722. ;; Ignore contents of verse blocks and example blocks.
  723. (should
  724. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  725. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  726. (org-indent-region (point-min) (point-max))
  727. (buffer-string))))
  728. (should
  729. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  730. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  731. (org-indent-region (point-min) (point-max))
  732. (buffer-string))))
  733. ;; Indent according to mode if `org-src-tab-acts-natively' is
  734. ;; non-nil. Otherwise, do not indent code at all.
  735. (should
  736. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  737. (org-test-with-temp-text
  738. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  739. (let ((org-src-tab-acts-natively t)
  740. (org-edit-src-content-indentation 0))
  741. (org-indent-region (point-min) (point-max)))
  742. (buffer-string))))
  743. (should
  744. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  745. (org-test-with-temp-text
  746. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  747. (let ((org-src-tab-acts-natively nil)
  748. (org-edit-src-content-indentation 0))
  749. (org-indent-region (point-min) (point-max)))
  750. (buffer-string))))
  751. ;; Align node properties according to `org-property-format'. Handle
  752. ;; nicely empty values.
  753. (should
  754. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  755. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  756. (let ((org-property-format "%-10s %s")
  757. (org-adapt-indentation nil))
  758. (org-indent-region (point) (point-max)))
  759. (buffer-string))))
  760. (should
  761. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  762. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  763. (let ((org-property-format "%-10s %s")
  764. (org-adapt-indentation nil))
  765. (org-indent-region (point) (point-max)))
  766. (buffer-string))))
  767. ;; Indent plain lists.
  768. (should
  769. (equal "- A\n B\n - C\n\n D"
  770. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  771. (org-indent-region (point-min) (point-max))
  772. (buffer-string))))
  773. (should
  774. (equal "- A\n\n- B"
  775. (org-test-with-temp-text " - A\n\n - B"
  776. (org-indent-region (point-min) (point-max))
  777. (buffer-string))))
  778. ;; Indent footnote definitions.
  779. (should
  780. (equal "[fn:1] Definition\n\nDefinition"
  781. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  782. (org-indent-region (point-min) (point-max))
  783. (buffer-string))))
  784. ;; Special case: Start indenting on a blank line.
  785. (should
  786. (equal "\nParagraph"
  787. (org-test-with-temp-text "\n Paragraph"
  788. (org-indent-region (point-min) (point-max))
  789. (buffer-string)))))
  790. ;;; Editing
  791. (ert-deftest test-org/return ()
  792. "Test RET (`org-return') specifications."
  793. ;; Regular test.
  794. (should
  795. (equal "Para\ngraph"
  796. (org-test-with-temp-text "Para<point>graph"
  797. (org-return)
  798. (buffer-string))))
  799. ;; With optional argument, indent line.
  800. (should
  801. (equal " Para\n graph"
  802. (org-test-with-temp-text " Para<point>graph"
  803. (org-return t)
  804. (buffer-string))))
  805. ;; On a table, call `org-table-next-row'.
  806. (should
  807. (org-test-with-temp-text "| <point>a |\n| b |"
  808. (org-return)
  809. (org-looking-at-p "b")))
  810. ;; Open link or timestamp under point when `org-return-follows-link'
  811. ;; is non-nil.
  812. (should
  813. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  814. (let ((org-return-follows-link t)) (org-return))
  815. (org-looking-at-p "<<target>>")))
  816. (should-not
  817. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  818. (let ((org-return-follows-link nil)) (org-return))
  819. (org-looking-at-p "<<target>>")))
  820. ;; However, do not open link when point is in a table.
  821. (should
  822. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  823. (let ((org-return-follows-link t)) (org-return))
  824. (org-looking-at-p "between")))
  825. ;; Special case: in a list, when indenting, do not break structure.
  826. (should
  827. (equal "- A\n B"
  828. (org-test-with-temp-text "- A <point>B"
  829. (org-return t)
  830. (buffer-string))))
  831. (should
  832. (equal "- A\n\n- B"
  833. (org-test-with-temp-text "- A\n<point>- B"
  834. (org-return t)
  835. (buffer-string))))
  836. ;; Special case: on tags part of a headline, add a newline below it
  837. ;; instead of breaking it.
  838. (should
  839. (equal "* H :tag:\n"
  840. (org-test-with-temp-text "* H :<point>tag:"
  841. (org-return)
  842. (buffer-string)))))
  843. (ert-deftest test-org/meta-return ()
  844. "Test M-RET (`org-meta-return') specifications."
  845. ;; In a table field insert a row above.
  846. (should
  847. (org-test-with-temp-text "| a |"
  848. (forward-char)
  849. (org-meta-return)
  850. (forward-line -1)
  851. (looking-at "| |$")))
  852. ;; In a paragraph change current line into a header.
  853. (should
  854. (org-test-with-temp-text "a"
  855. (org-meta-return)
  856. (beginning-of-line)
  857. (looking-at "\* a$")))
  858. ;; In an item insert an item, in this case above.
  859. (should
  860. (org-test-with-temp-text "- a"
  861. (org-meta-return)
  862. (beginning-of-line)
  863. (looking-at "- $")))
  864. ;; In a drawer and item insert an item, in this case above.
  865. (should
  866. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  867. (forward-line)
  868. (org-meta-return)
  869. (beginning-of-line)
  870. (looking-at "- $"))))
  871. (ert-deftest test-org/insert-heading ()
  872. "Test `org-insert-heading' specifications."
  873. ;; In an empty buffer, insert a new headline.
  874. (should
  875. (equal "* "
  876. (org-test-with-temp-text ""
  877. (org-insert-heading)
  878. (buffer-string))))
  879. ;; At the beginning of a line, turn it into a headline
  880. (should
  881. (equal "* P"
  882. (org-test-with-temp-text "<point>P"
  883. (org-insert-heading)
  884. (buffer-string))))
  885. ;; In the middle of a line, split the line if allowed, otherwise,
  886. ;; insert the headline at its end.
  887. (should
  888. (equal "Para\n* graph"
  889. (org-test-with-temp-text "Para<point>graph"
  890. (let ((org-M-RET-may-split-line '((default . t))))
  891. (org-insert-heading))
  892. (buffer-string))))
  893. (should
  894. (equal "Paragraph\n* "
  895. (org-test-with-temp-text "Para<point>graph"
  896. (let ((org-M-RET-may-split-line '((default . nil))))
  897. (org-insert-heading))
  898. (buffer-string))))
  899. ;; When on a list, insert an item instead, unless called with an
  900. ;; universal argument or if list is invisible. In this case, create
  901. ;; a new headline after contents.
  902. (should
  903. (equal "* H\n- item\n- "
  904. (org-test-with-temp-text "* H\n- item<point>"
  905. (let ((org-insert-heading-respect-content nil))
  906. (org-insert-heading))
  907. (buffer-string))))
  908. (should
  909. (equal "* H\n- item\n- item 2\n* "
  910. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  911. (let ((org-insert-heading-respect-content nil))
  912. (org-insert-heading '(4)))
  913. (buffer-string))))
  914. (should
  915. (equal "* H\n- item\n* "
  916. (org-test-with-temp-text "* H\n- item"
  917. (org-cycle)
  918. (goto-char (point-max))
  919. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  920. (buffer-string))))
  921. ;; When called with two universal arguments, insert a new headline
  922. ;; at the end of the grandparent subtree.
  923. (should
  924. (equal "* H1\n** H3\n- item\n** H2\n** "
  925. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  926. (let ((org-insert-heading-respect-content nil))
  927. (org-insert-heading '(16)))
  928. (buffer-string))))
  929. ;; When optional TOP-LEVEL argument is non-nil, always insert
  930. ;; a level 1 heading.
  931. (should
  932. (equal "* H1\n** H2\n* "
  933. (org-test-with-temp-text "* H1\n** H2<point>"
  934. (org-insert-heading nil nil t)
  935. (buffer-string))))
  936. (should
  937. (equal "* H1\n- item\n* "
  938. (org-test-with-temp-text "* H1\n- item<point>"
  939. (org-insert-heading nil nil t)
  940. (buffer-string))))
  941. ;; Corner case: correctly insert a headline after an empty one.
  942. (should
  943. (equal "* \n* "
  944. (org-test-with-temp-text "* <point>"
  945. (org-insert-heading)
  946. (buffer-string)))))
  947. (ert-deftest test-org/insert-todo-heading-respect-content ()
  948. "Test `org-insert-todo-heading-respect-content' specifications."
  949. ;; Create a TODO heading.
  950. (should
  951. (org-test-with-temp-text "* H1\n Body"
  952. (org-insert-todo-heading-respect-content)
  953. (nth 2 (org-heading-components))))
  954. ;; Add headline at the end of the first subtree
  955. (should
  956. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  957. (search-forward "H1Body")
  958. (org-insert-todo-heading-respect-content)
  959. (and (eobp) (org-at-heading-p))))
  960. ;; In a list, do not create a new item.
  961. (should
  962. (org-test-with-temp-text "* H\n- an item\n- another one"
  963. (search-forward "an ")
  964. (org-insert-todo-heading-respect-content)
  965. (and (eobp) (org-at-heading-p)))))
  966. ;;; Fixed-Width Areas
  967. (ert-deftest test-org/toggle-fixed-width ()
  968. "Test `org-toggle-fixed-width' specifications."
  969. ;; No region: Toggle on fixed-width marker in paragraphs.
  970. (should
  971. (equal ": A"
  972. (org-test-with-temp-text "A"
  973. (org-toggle-fixed-width)
  974. (buffer-string))))
  975. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  976. (should
  977. (equal "A"
  978. (org-test-with-temp-text ": A"
  979. (org-toggle-fixed-width)
  980. (buffer-string))))
  981. ;; No region: Toggle on marker in blank lines after elements or just
  982. ;; after a headline.
  983. (should
  984. (equal "* H\n: "
  985. (org-test-with-temp-text "* H\n"
  986. (forward-line)
  987. (org-toggle-fixed-width)
  988. (buffer-string))))
  989. (should
  990. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  991. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  992. (goto-char (point-max))
  993. (org-toggle-fixed-width)
  994. (buffer-string))))
  995. ;; No region: Toggle on marker in front of one line elements (e.g.,
  996. ;; headlines, clocks)
  997. (should
  998. (equal ": * Headline"
  999. (org-test-with-temp-text "* Headline"
  1000. (org-toggle-fixed-width)
  1001. (buffer-string))))
  1002. (should
  1003. (equal ": #+KEYWORD: value"
  1004. (org-test-with-temp-text "#+KEYWORD: value"
  1005. (org-toggle-fixed-width)
  1006. (buffer-string))))
  1007. ;; No region: error in other situations.
  1008. (should-error
  1009. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1010. (forward-line)
  1011. (org-toggle-fixed-width)
  1012. (buffer-string)))
  1013. ;; No region: Indentation is preserved.
  1014. (should
  1015. (equal "- A\n : B"
  1016. (org-test-with-temp-text "- A\n B"
  1017. (forward-line)
  1018. (org-toggle-fixed-width)
  1019. (buffer-string))))
  1020. ;; Region: If it contains only fixed-width elements and blank lines,
  1021. ;; toggle off fixed-width markup.
  1022. (should
  1023. (equal "A\n\nB"
  1024. (org-test-with-temp-text ": A\n\n: B"
  1025. (transient-mark-mode 1)
  1026. (push-mark (point) t t)
  1027. (goto-char (point-max))
  1028. (org-toggle-fixed-width)
  1029. (buffer-string))))
  1030. ;; Region: If it contains anything else, toggle on fixed-width but
  1031. ;; not on fixed-width areas.
  1032. (should
  1033. (equal ": A\n: \n: B\n: \n: C"
  1034. (org-test-with-temp-text "A\n\n: B\n\nC"
  1035. (transient-mark-mode 1)
  1036. (push-mark (point) t t)
  1037. (goto-char (point-max))
  1038. (org-toggle-fixed-width)
  1039. (buffer-string))))
  1040. ;; Region: Ignore blank lines at its end, unless it contains only
  1041. ;; such lines.
  1042. (should
  1043. (equal ": A\n\n"
  1044. (org-test-with-temp-text "A\n\n"
  1045. (transient-mark-mode 1)
  1046. (push-mark (point) t t)
  1047. (goto-char (point-max))
  1048. (org-toggle-fixed-width)
  1049. (buffer-string))))
  1050. (should
  1051. (equal ": \n: \n"
  1052. (org-test-with-temp-text "\n\n"
  1053. (transient-mark-mode 1)
  1054. (push-mark (point) t t)
  1055. (goto-char (point-max))
  1056. (org-toggle-fixed-width)
  1057. (buffer-string)))))
  1058. ;;; Headline
  1059. (ert-deftest test-org/in-commented-heading-p ()
  1060. "Test `org-in-commented-heading-p' specifications."
  1061. ;; Commented headline.
  1062. (should
  1063. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1064. (goto-char (point-max))
  1065. (org-in-commented-heading-p)))
  1066. ;; Commented ancestor.
  1067. (should
  1068. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1069. (goto-char (point-max))
  1070. (org-in-commented-heading-p)))
  1071. ;; Comment keyword is case-sensitive.
  1072. (should-not
  1073. (org-test-with-temp-text "* Comment Headline\nBody"
  1074. (goto-char (point-max))
  1075. (org-in-commented-heading-p)))
  1076. ;; Keyword is standalone.
  1077. (should-not
  1078. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1079. (goto-char (point-max))
  1080. (org-in-commented-heading-p)))
  1081. ;; Optional argument.
  1082. (should-not
  1083. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1084. (goto-char (point-max))
  1085. (org-in-commented-heading-p t))))
  1086. ;;; Keywords
  1087. (ert-deftest test-org/set-regexps-and-options ()
  1088. "Test `org-set-regexps-and-options' specifications."
  1089. ;; TAGS keyword.
  1090. (should
  1091. (equal '(("A" . ?a) ("B") ("C"))
  1092. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1093. (org-mode-restart)
  1094. org-tag-alist)))
  1095. (should
  1096. (equal '(("A") (:newline) ("B"))
  1097. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1098. (org-mode-restart)
  1099. org-tag-alist)))
  1100. (should
  1101. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1102. (org-test-with-temp-text "#+TAGS: { A B } C"
  1103. (org-mode-restart)
  1104. org-tag-alist)))
  1105. (should
  1106. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1107. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1108. (org-mode-restart)
  1109. org-tag-alist)))
  1110. (should
  1111. (equal '(("A" "B" "C"))
  1112. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1113. (org-mode-restart)
  1114. org-tag-groups-alist)))
  1115. ;; FILETAGS keyword.
  1116. (should
  1117. (equal '("A" "B" "C")
  1118. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1119. (org-mode-restart)
  1120. org-file-tags)))
  1121. ;; PROPERTY keyword. Property names are case-insensitive.
  1122. (should
  1123. (equal "foo=1"
  1124. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1125. (org-mode-restart)
  1126. (cdr (assoc "var" org-file-properties)))))
  1127. (should
  1128. (equal
  1129. "foo=1 bar=2"
  1130. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1131. (org-mode-restart)
  1132. (cdr (assoc "var" org-file-properties)))))
  1133. (should
  1134. (equal
  1135. "foo=1 bar=2"
  1136. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1137. (org-mode-restart)
  1138. (cdr (assoc "var" org-file-properties)))))
  1139. ;; ARCHIVE keyword.
  1140. (should
  1141. (equal "%s_done::"
  1142. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1143. (org-mode-restart)
  1144. org-archive-location)))
  1145. ;; CATEGORY keyword.
  1146. (should
  1147. (eq 'test
  1148. (org-test-with-temp-text "#+CATEGORY: test"
  1149. (org-mode-restart)
  1150. org-category)))
  1151. (should
  1152. (equal "test"
  1153. (org-test-with-temp-text "#+CATEGORY: test"
  1154. (org-mode-restart)
  1155. (cdr (assoc "CATEGORY" org-file-properties)))))
  1156. ;; COLUMNS keyword.
  1157. (should
  1158. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1159. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1160. (org-mode-restart)
  1161. org-columns-default-format)))
  1162. ;; CONSTANTS keyword. Constants names are case sensitive.
  1163. (should
  1164. (equal '("299792458." "3.14")
  1165. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1166. (org-mode-restart)
  1167. (mapcar
  1168. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1169. '("c" "pi")))))
  1170. (should
  1171. (equal "3.14"
  1172. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1173. (org-mode-restart)
  1174. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1175. (should
  1176. (equal "22/7"
  1177. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1178. (org-mode-restart)
  1179. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1180. ;; LINK keyword.
  1181. (should
  1182. (equal
  1183. '("url1" "url2")
  1184. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1185. (org-mode-restart)
  1186. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1187. '("a" "b")))))
  1188. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1189. (should
  1190. (equal
  1191. '(?X ?Z ?Y)
  1192. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1193. (org-mode-restart)
  1194. (list org-highest-priority org-lowest-priority org-default-priority))))
  1195. (should
  1196. (equal
  1197. '(?A ?C ?B)
  1198. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1199. (org-mode-restart)
  1200. (list org-highest-priority org-lowest-priority org-default-priority))))
  1201. ;; STARTUP keyword.
  1202. (should
  1203. (equal '(t t)
  1204. (org-test-with-temp-text "#+STARTUP: fold odd"
  1205. (org-mode-restart)
  1206. (list org-startup-folded org-odd-levels-only))))
  1207. ;; TODO keywords.
  1208. (should
  1209. (equal '(("A" "B") ("C"))
  1210. (org-test-with-temp-text "#+TODO: A B | C"
  1211. (org-mode-restart)
  1212. (list org-not-done-keywords org-done-keywords))))
  1213. (should
  1214. (equal '(("A" "C") ("B" "D"))
  1215. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1216. (org-mode-restart)
  1217. (list org-not-done-keywords org-done-keywords))))
  1218. (should
  1219. (equal '(("A" "B") ("C"))
  1220. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1221. (org-mode-restart)
  1222. (list org-not-done-keywords org-done-keywords))))
  1223. (should
  1224. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1225. (org-test-with-temp-text "#+TODO: A(a)"
  1226. (org-mode-restart)
  1227. org-todo-key-alist)))
  1228. (should
  1229. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1230. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1231. (org-mode-restart)
  1232. org-todo-log-states)))
  1233. ;; Enter SETUPFILE keyword.
  1234. (should
  1235. (equal "1"
  1236. (org-test-with-temp-text
  1237. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1238. (org-mode-restart)
  1239. (cdr (assoc "a" org-file-properties))))))
  1240. ;;; Links
  1241. ;;;; Coderefs
  1242. (ert-deftest test-org/coderef ()
  1243. "Test coderef links specifications."
  1244. (should
  1245. (org-test-with-temp-text "
  1246. #+BEGIN_SRC emacs-lisp
  1247. \(+ 1 1) (ref:sc)
  1248. #+END_SRC
  1249. \[[(sc)]]"
  1250. (goto-char (point-max))
  1251. (org-open-at-point)
  1252. (looking-at "(ref:sc)"))))
  1253. ;;;; Custom ID
  1254. (ert-deftest test-org/custom-id ()
  1255. "Test custom ID links specifications."
  1256. (should
  1257. (org-test-with-temp-text
  1258. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
  1259. (goto-char (point-max))
  1260. (org-open-at-point)
  1261. (org-looking-at-p "\\* H1"))))
  1262. ;;;; Fuzzy Links
  1263. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1264. ;; a named element (#+name: text) and to headlines (* Text).
  1265. (ert-deftest test-org/fuzzy-links ()
  1266. "Test fuzzy links specifications."
  1267. ;; Fuzzy link goes in priority to a matching target.
  1268. (should
  1269. (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  1270. (goto-line 5)
  1271. (org-open-at-point)
  1272. (looking-at "<<Test>>")))
  1273. ;; Then fuzzy link points to an element with a given name.
  1274. (should
  1275. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  1276. (goto-line 5)
  1277. (org-open-at-point)
  1278. (looking-at "#\\+NAME: Test")))
  1279. ;; A target still lead to a matching headline otherwise.
  1280. (should
  1281. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  1282. (goto-line 4)
  1283. (org-open-at-point)
  1284. (looking-at "\\* Head2")))
  1285. ;; With a leading star in link, enforce heading match.
  1286. (should
  1287. (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
  1288. (goto-line 3)
  1289. (org-open-at-point)
  1290. (looking-at "\\* Test")))
  1291. ;; With a leading star in link, enforce exact heading match, even
  1292. ;; with `org-link-search-must-match-exact-headline' set to nil.
  1293. (should-error
  1294. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  1295. (let ((org-link-search-must-match-exact-headline nil))
  1296. (org-open-at-point))))
  1297. ;; Heading match should not care about spaces, cookies, todo
  1298. ;; keywords, priorities, and tags.
  1299. (should
  1300. (let ((first-line
  1301. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1302. (org-test-with-temp-text
  1303. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  1304. (let ((org-link-search-must-match-exact-headline nil)
  1305. (org-todo-regexp "TODO"))
  1306. (org-open-at-point))
  1307. (looking-at (regexp-quote first-line)))))
  1308. ;; Heading match should still be exact.
  1309. (should-error
  1310. (let ((first-line
  1311. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1312. (org-test-with-temp-text
  1313. (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
  1314. (let ((org-link-search-must-match-exact-headline nil)
  1315. (org-todo-regexp "TODO"))
  1316. (org-open-at-point)))))
  1317. ;; Correctly un-hexify fuzzy links.
  1318. (should
  1319. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  1320. (goto-char (point-max))
  1321. (org-open-at-point)
  1322. (bobp))))
  1323. ;;;; Link Escaping
  1324. (ert-deftest test-org/org-link-escape-ascii-character ()
  1325. "Escape an ascii character."
  1326. (should
  1327. (string=
  1328. "%5B"
  1329. (org-link-escape "["))))
  1330. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  1331. "Escape an ascii control character."
  1332. (should
  1333. (string=
  1334. "%09"
  1335. (org-link-escape "\t"))))
  1336. (ert-deftest test-org/org-link-escape-multibyte-character ()
  1337. "Escape an unicode multibyte character."
  1338. (should
  1339. (string=
  1340. "%E2%82%AC"
  1341. (org-link-escape "€"))))
  1342. (ert-deftest test-org/org-link-escape-custom-table ()
  1343. "Escape string with custom character table."
  1344. (should
  1345. (string=
  1346. "Foo%3A%42ar%0A"
  1347. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  1348. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  1349. "Escape string with custom table merged with default table."
  1350. (should
  1351. (string=
  1352. "%5BF%6F%6F%3A%42ar%0A%5D"
  1353. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  1354. (ert-deftest test-org/org-link-unescape-ascii-character ()
  1355. "Unescape an ascii character."
  1356. (should
  1357. (string=
  1358. "["
  1359. (org-link-unescape "%5B"))))
  1360. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  1361. "Unescpae an ascii control character."
  1362. (should
  1363. (string=
  1364. "\n"
  1365. (org-link-unescape "%0A"))))
  1366. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  1367. "Unescape unicode multibyte character."
  1368. (should
  1369. (string=
  1370. "€"
  1371. (org-link-unescape "%E2%82%AC"))))
  1372. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  1373. "Unescape old style percent escaped character."
  1374. (should
  1375. (string=
  1376. "àâçèéêîôùû"
  1377. (decode-coding-string
  1378. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  1379. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  1380. "Escape and unescape a URL that includes an escaped char.
  1381. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  1382. (should
  1383. (string=
  1384. "http://some.host.com/form?&id=blah%2Bblah25"
  1385. (org-link-unescape
  1386. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  1387. (ert-deftest test-org/org-link-escape-chars-browser ()
  1388. "Test of the constant `org-link-escape-chars-browser'.
  1389. See there why this test is a candidate to be removed once Org
  1390. drops support for Emacs 24.1 and 24.2."
  1391. (should
  1392. (string=
  1393. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1394. "%22Release%208.2%22&idxname=emacs-orgmode")
  1395. (org-link-escape-browser ; Do not replace with `url-encode-url',
  1396. ; see docstring above.
  1397. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1398. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  1399. ;;;; Open at point
  1400. (ert-deftest test-org/open-at-point-in-property ()
  1401. "Does `org-open-at-point' open link in property drawer?"
  1402. (should
  1403. (org-test-with-temp-text
  1404. "* Headline
  1405. :PROPERTIES:
  1406. :URL: <point>[[info:emacs#Top]]
  1407. :END:"
  1408. (org-open-at-point) t)))
  1409. (ert-deftest test-org/open-at-point-in-comment ()
  1410. "Does `org-open-at-point' open link in a commented line?"
  1411. (should
  1412. (org-test-with-temp-text
  1413. "# <point>[[info:emacs#Top]]"
  1414. (org-open-at-point) t)))
  1415. (ert-deftest test-org/open-at-point/info ()
  1416. "Test `org-open-at-point' on info links."
  1417. (should
  1418. (org-test-with-temp-text
  1419. "<point>[[info:emacs#Top]]"
  1420. (org-open-at-point)
  1421. (and (switch-to-buffer "*info*")
  1422. (prog1
  1423. (looking-at "\nThe Emacs Editor")
  1424. (kill-buffer))))))
  1425. (ert-deftest test-org/open-at-point/inline-image ()
  1426. "Test `org-open-at-point' on nested links."
  1427. (should
  1428. (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
  1429. (org-open-at-point)
  1430. (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
  1431. (kill-buffer "*info*")))))
  1432. ;;; Node Properties
  1433. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1434. "Ensure properties accumulate in subtree drawers."
  1435. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1436. (org-babel-next-src-block)
  1437. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1438. (ert-deftest test-org/custom-properties ()
  1439. "Test custom properties specifications."
  1440. ;; Standard test.
  1441. (should
  1442. (let ((org-custom-properties '("FOO")))
  1443. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1444. (org-toggle-custom-properties-visibility)
  1445. (org-invisible-p2))))
  1446. ;; Properties are case-insensitive.
  1447. (should
  1448. (let ((org-custom-properties '("FOO")))
  1449. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  1450. (org-toggle-custom-properties-visibility)
  1451. (org-invisible-p2))))
  1452. (should
  1453. (let ((org-custom-properties '("foo")))
  1454. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1455. (org-toggle-custom-properties-visibility)
  1456. (org-invisible-p2))))
  1457. ;; Multiple custom properties in the same drawer.
  1458. (should
  1459. (let ((org-custom-properties '("FOO" "BAR")))
  1460. (org-test-with-temp-text
  1461. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  1462. (org-toggle-custom-properties-visibility)
  1463. (and (org-invisible-p2)
  1464. (not (progn (forward-line) (org-invisible-p2)))
  1465. (progn (forward-line) (org-invisible-p2))))))
  1466. ;; Hide custom properties with an empty value.
  1467. (should
  1468. (let ((org-custom-properties '("FOO")))
  1469. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  1470. (org-toggle-custom-properties-visibility)
  1471. (org-invisible-p2))))
  1472. ;; Do not hide fake properties.
  1473. (should-not
  1474. (let ((org-custom-properties '("FOO")))
  1475. (org-test-with-temp-text ":FOO: val\n"
  1476. (org-toggle-custom-properties-visibility)
  1477. (org-invisible-p2))))
  1478. (should-not
  1479. (let ((org-custom-properties '("A")))
  1480. (org-test-with-temp-text
  1481. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  1482. (org-toggle-custom-properties-visibility)
  1483. (org-invisible-p2)))))
  1484. ;;; Mark Region
  1485. (ert-deftest test-org/mark-subtree ()
  1486. "Test `org-mark-subtree' specifications."
  1487. ;; Error when point is before first headline.
  1488. (should-error
  1489. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1490. (progn (transient-mark-mode 1)
  1491. (org-mark-subtree))))
  1492. ;; Without argument, mark current subtree.
  1493. (should
  1494. (equal
  1495. '(12 32)
  1496. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1497. (progn (transient-mark-mode 1)
  1498. (forward-line 2)
  1499. (org-mark-subtree)
  1500. (list (region-beginning) (region-end))))))
  1501. ;; With an argument, move ARG up.
  1502. (should
  1503. (equal
  1504. '(1 32)
  1505. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1506. (progn (transient-mark-mode 1)
  1507. (forward-line 2)
  1508. (org-mark-subtree 1)
  1509. (list (region-beginning) (region-end))))))
  1510. ;; Do not get fooled by inlinetasks.
  1511. (when (featurep 'org-inlinetask)
  1512. (should
  1513. (= 1
  1514. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1515. (progn (transient-mark-mode 1)
  1516. (forward-line 1)
  1517. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1518. (region-beginning)))))))
  1519. ;;; Navigation
  1520. (ert-deftest test-org/end-of-meta-data ()
  1521. "Test `org-end-of-meta-data' specifications."
  1522. ;; Skip planning line.
  1523. (should
  1524. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  1525. (org-end-of-meta-data)
  1526. (eobp)))
  1527. ;; Skip properties drawer.
  1528. (should
  1529. (org-test-with-temp-text
  1530. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  1531. (org-end-of-meta-data)
  1532. (eobp)))
  1533. ;; Skip both.
  1534. (should
  1535. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  1536. (org-end-of-meta-data)
  1537. (eobp)))
  1538. ;; Nothing to skip, go to first line.
  1539. (should
  1540. (org-test-with-temp-text "* Headline\nContents"
  1541. (org-end-of-meta-data)
  1542. (looking-at "Contents")))
  1543. ;; With option argument, skip empty lines, regular drawers and
  1544. ;; clocking lines.
  1545. (should
  1546. (org-test-with-temp-text "* Headline\n\nContents"
  1547. (org-end-of-meta-data t)
  1548. (looking-at "Contents")))
  1549. (should
  1550. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  1551. (org-end-of-meta-data t)
  1552. (looking-at "Contents")))
  1553. (should
  1554. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  1555. (org-end-of-meta-data t)
  1556. (looking-at "Contents")))
  1557. ;; Special case: do not skip incomplete drawers.
  1558. (should
  1559. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  1560. (org-end-of-meta-data t)
  1561. (looking-at ":LOGBOOK:")))
  1562. ;; Special case: Be careful about consecutive headlines.
  1563. (should-not
  1564. (org-test-with-temp-text "* H1\n*H2\nContents"
  1565. (org-end-of-meta-data t)
  1566. (looking-at "Contents"))))
  1567. (ert-deftest test-org/beginning-of-line ()
  1568. "Test `org-beginning-of-line' specifications."
  1569. ;; Standard test.
  1570. (should
  1571. (org-test-with-temp-text "Some text\nSome other text"
  1572. (progn (org-beginning-of-line) (bolp))))
  1573. ;; Standard test with `visual-line-mode'.
  1574. (should-not
  1575. (org-test-with-temp-text "A long line of text\nSome other text"
  1576. (progn (visual-line-mode)
  1577. (forward-char 2)
  1578. (dotimes (i 1000) (insert "very "))
  1579. (org-beginning-of-line)
  1580. (bolp))))
  1581. ;; At an headline with special movement.
  1582. (should
  1583. (org-test-with-temp-text "* TODO Headline"
  1584. (let ((org-special-ctrl-a/e t))
  1585. (org-end-of-line)
  1586. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1587. (progn (org-beginning-of-line) (bolp))
  1588. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  1589. (ert-deftest test-org/end-of-line ()
  1590. "Test `org-end-of-line' specifications."
  1591. ;; Standard test.
  1592. (should
  1593. (org-test-with-temp-text "Some text\nSome other text"
  1594. (progn (org-end-of-line) (eolp))))
  1595. ;; Standard test with `visual-line-mode'.
  1596. (should-not
  1597. (org-test-with-temp-text "A long line of text\nSome other text"
  1598. (progn (visual-line-mode)
  1599. (forward-char 2)
  1600. (dotimes (i 1000) (insert "very "))
  1601. (goto-char (point-min))
  1602. (org-end-of-line)
  1603. (eolp))))
  1604. ;; At an headline with special movement.
  1605. (should
  1606. (org-test-with-temp-text "* Headline1 :tag:\n"
  1607. (let ((org-special-ctrl-a/e t))
  1608. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1609. (progn (org-end-of-line) (eolp))
  1610. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1611. ;; At an headline without special movement.
  1612. (should
  1613. (org-test-with-temp-text "* Headline2 :tag:\n"
  1614. (let ((org-special-ctrl-a/e nil))
  1615. (and (progn (org-end-of-line) (eolp))
  1616. (progn (org-end-of-line) (eolp))))))
  1617. ;; At an headline, with reversed movement.
  1618. (should
  1619. (org-test-with-temp-text "* Headline3 :tag:\n"
  1620. (let ((org-special-ctrl-a/e 'reversed)
  1621. (this-command last-command))
  1622. (and (progn (org-end-of-line) (eolp))
  1623. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1624. ;; At a block without hidden contents.
  1625. (should
  1626. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1627. (progn (org-end-of-line) (eolp))))
  1628. ;; At a block with hidden contents.
  1629. (should-not
  1630. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1631. (let ((org-special-ctrl-a/e t))
  1632. (org-hide-block-toggle)
  1633. (org-end-of-line)
  1634. (eobp)))))
  1635. (ert-deftest test-org/forward-paragraph ()
  1636. "Test `org-forward-paragraph' specifications."
  1637. ;; At end of buffer, return an error.
  1638. (should-error
  1639. (org-test-with-temp-text "Paragraph"
  1640. (goto-char (point-max))
  1641. (org-forward-paragraph)))
  1642. ;; Standard test.
  1643. (should
  1644. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1645. (org-forward-paragraph)
  1646. (looking-at "P2")))
  1647. ;; Ignore depth.
  1648. (should
  1649. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1650. (org-forward-paragraph)
  1651. (looking-at "P1")))
  1652. ;; Do not enter elements with invisible contents.
  1653. (should
  1654. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1655. (org-hide-block-toggle)
  1656. (org-forward-paragraph)
  1657. (looking-at "P3")))
  1658. ;; On an affiliated keyword, jump to the beginning of the element.
  1659. (should
  1660. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1661. (org-forward-paragraph)
  1662. (looking-at "Para")))
  1663. ;; On an item or a footnote definition, move to the second element
  1664. ;; inside, if any.
  1665. (should
  1666. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1667. (org-forward-paragraph)
  1668. (looking-at " Paragraph")))
  1669. (should
  1670. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1671. (org-forward-paragraph)
  1672. (looking-at "Paragraph")))
  1673. ;; On an item, or a footnote definition, when the first line is
  1674. ;; empty, move to the first item.
  1675. (should
  1676. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1677. (org-forward-paragraph)
  1678. (looking-at " Paragraph")))
  1679. (should
  1680. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1681. (org-forward-paragraph)
  1682. (looking-at "Paragraph")))
  1683. ;; On a table (resp. a property drawer) do not move through table
  1684. ;; rows (resp. node properties).
  1685. (should
  1686. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1687. (org-forward-paragraph)
  1688. (looking-at "Paragraph")))
  1689. (should
  1690. (org-test-with-temp-text
  1691. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1692. (org-forward-paragraph)
  1693. (looking-at "Paragraph")))
  1694. ;; On a verse or source block, stop after blank lines.
  1695. (should
  1696. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1697. (org-forward-paragraph)
  1698. (looking-at "L2")))
  1699. (should
  1700. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1701. (org-forward-paragraph)
  1702. (looking-at "L2"))))
  1703. (ert-deftest test-org/backward-paragraph ()
  1704. "Test `org-backward-paragraph' specifications."
  1705. ;; Error at beginning of buffer.
  1706. (should-error
  1707. (org-test-with-temp-text "Paragraph"
  1708. (org-backward-paragraph)))
  1709. ;; Regular test.
  1710. (should
  1711. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1712. (goto-char (point-max))
  1713. (org-backward-paragraph)
  1714. (looking-at "P3")))
  1715. (should
  1716. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1717. (goto-char (point-max))
  1718. (beginning-of-line)
  1719. (org-backward-paragraph)
  1720. (looking-at "P2")))
  1721. ;; Ignore depth.
  1722. (should
  1723. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1724. (goto-char (point-max))
  1725. (beginning-of-line)
  1726. (org-backward-paragraph)
  1727. (looking-at "P2")))
  1728. ;; Ignore invisible elements.
  1729. (should
  1730. (org-test-with-temp-text "* H1\n P1\n* H2"
  1731. (org-cycle)
  1732. (goto-char (point-max))
  1733. (beginning-of-line)
  1734. (org-backward-paragraph)
  1735. (bobp)))
  1736. ;; On an affiliated keyword, jump to the first one.
  1737. (should
  1738. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  1739. (search-forward "c2")
  1740. (org-backward-paragraph)
  1741. (looking-at "#\\+name")))
  1742. ;; On the second element in an item or a footnote definition, jump
  1743. ;; to item or the definition.
  1744. (should
  1745. (org-test-with-temp-text "- line1\n\n line2"
  1746. (goto-char (point-max))
  1747. (beginning-of-line)
  1748. (org-backward-paragraph)
  1749. (looking-at "- line1")))
  1750. (should
  1751. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  1752. (goto-char (point-max))
  1753. (beginning-of-line)
  1754. (org-backward-paragraph)
  1755. (looking-at "\\[fn:1\\] line1")))
  1756. ;; On a table (resp. a property drawer), ignore table rows
  1757. ;; (resp. node properties).
  1758. (should
  1759. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  1760. (goto-char (point-max))
  1761. (beginning-of-line)
  1762. (org-backward-paragraph)
  1763. (bobp)))
  1764. (should
  1765. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  1766. (org-backward-paragraph)
  1767. (looking-at ":PROPERTIES:")))
  1768. ;; On a source or verse block, stop before blank lines.
  1769. (should
  1770. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  1771. (search-forward "L3")
  1772. (beginning-of-line)
  1773. (org-backward-paragraph)
  1774. (looking-at "L2")))
  1775. (should
  1776. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  1777. (search-forward "L3")
  1778. (beginning-of-line)
  1779. (org-backward-paragraph)
  1780. (looking-at "L2"))))
  1781. (ert-deftest test-org/forward-element ()
  1782. "Test `org-forward-element' specifications."
  1783. ;; 1. At EOB: should error.
  1784. (org-test-with-temp-text "Some text\n"
  1785. (goto-char (point-max))
  1786. (should-error (org-forward-element)))
  1787. ;; 2. Standard move: expected to ignore blank lines.
  1788. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  1789. (org-forward-element)
  1790. (should (looking-at (regexp-quote "Second paragraph."))))
  1791. ;; 3. Headline tests.
  1792. (org-test-with-temp-text "
  1793. * Head 1
  1794. ** Head 1.1
  1795. *** Head 1.1.1
  1796. ** Head 1.2"
  1797. ;; 3.1. At an headline beginning: move to next headline at the
  1798. ;; same level.
  1799. (goto-line 3)
  1800. (org-forward-element)
  1801. (should (looking-at (regexp-quote "** Head 1.2")))
  1802. ;; 3.2. At an headline beginning: move to parent headline if no
  1803. ;; headline at the same level.
  1804. (goto-line 3)
  1805. (org-forward-element)
  1806. (should (looking-at (regexp-quote "** Head 1.2"))))
  1807. ;; 4. Greater element tests.
  1808. (org-test-with-temp-text
  1809. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  1810. ;; 4.1. At a greater element: expected to skip contents.
  1811. (org-forward-element)
  1812. (should (looking-at (regexp-quote "Outside.")))
  1813. ;; 4.2. At the end of greater element contents: expected to skip
  1814. ;; to the end of the greater element.
  1815. (goto-line 2)
  1816. (org-forward-element)
  1817. (should (looking-at (regexp-quote "Outside."))))
  1818. ;; 5. List tests.
  1819. (org-test-with-temp-text "
  1820. - item1
  1821. - sub1
  1822. - sub2
  1823. - sub3
  1824. Inner paragraph.
  1825. - item2
  1826. Outside."
  1827. ;; 5.1. At list top point: expected to move to the element after
  1828. ;; the list.
  1829. (goto-line 2)
  1830. (org-forward-element)
  1831. (should (looking-at (regexp-quote "Outside.")))
  1832. ;; 5.2. Special case: at the first line of a sub-list, but not at
  1833. ;; beginning of line, move to next item.
  1834. (goto-line 2)
  1835. (forward-char)
  1836. (org-forward-element)
  1837. (should (looking-at "- item2"))
  1838. (goto-line 4)
  1839. (forward-char)
  1840. (org-forward-element)
  1841. (should (looking-at " - sub2"))
  1842. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  1843. (goto-line 4)
  1844. (org-forward-element)
  1845. (should (looking-at (regexp-quote " Inner paragraph.")))
  1846. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  1847. (goto-line 8)
  1848. (org-forward-element)
  1849. (should (looking-at (regexp-quote " Inner paragraph.")))
  1850. ;; 5.5. At an item: expected to move to next item, if any.
  1851. (goto-line 6)
  1852. (org-forward-element)
  1853. (should (looking-at " - sub3"))))
  1854. (ert-deftest test-org/backward-element ()
  1855. "Test `org-backward-element' specifications."
  1856. ;; 1. Should error at BOB.
  1857. (org-test-with-temp-text " \nParagraph."
  1858. (should-error (org-backward-element)))
  1859. ;; 2. Should move at BOB when called on the first element in buffer.
  1860. (should
  1861. (org-test-with-temp-text "\n#+TITLE: test"
  1862. (progn (forward-line)
  1863. (org-backward-element)
  1864. (bobp))))
  1865. ;; 3. Not at the beginning of an element: move at its beginning.
  1866. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1867. (goto-line 3)
  1868. (end-of-line)
  1869. (org-backward-element)
  1870. (should (looking-at (regexp-quote "Paragraph2."))))
  1871. ;; 4. Headline tests.
  1872. (org-test-with-temp-text "
  1873. * Head 1
  1874. ** Head 1.1
  1875. *** Head 1.1.1
  1876. ** Head 1.2"
  1877. ;; 4.1. At an headline beginning: move to previous headline at the
  1878. ;; same level.
  1879. (goto-line 5)
  1880. (org-backward-element)
  1881. (should (looking-at (regexp-quote "** Head 1.1")))
  1882. ;; 4.2. At an headline beginning: move to parent headline if no
  1883. ;; headline at the same level.
  1884. (goto-line 3)
  1885. (org-backward-element)
  1886. (should (looking-at (regexp-quote "* Head 1")))
  1887. ;; 4.3. At the first top-level headline: should error.
  1888. (goto-line 2)
  1889. (should-error (org-backward-element)))
  1890. ;; 5. At beginning of first element inside a greater element:
  1891. ;; expected to move to greater element's beginning.
  1892. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  1893. (goto-line 3)
  1894. (org-backward-element)
  1895. (should (looking-at "#\\+BEGIN_CENTER")))
  1896. ;; 6. At the beginning of the first element in a section: should
  1897. ;; move back to headline, if any.
  1898. (should
  1899. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  1900. (progn (goto-char (point-max))
  1901. (beginning-of-line)
  1902. (org-backward-element)
  1903. (org-at-heading-p))))
  1904. ;; 7. List tests.
  1905. (org-test-with-temp-text "
  1906. - item1
  1907. - sub1
  1908. - sub2
  1909. - sub3
  1910. Inner paragraph.
  1911. - item2
  1912. Outside."
  1913. ;; 7.1. At beginning of sub-list: expected to move to the
  1914. ;; paragraph before it.
  1915. (goto-line 4)
  1916. (org-backward-element)
  1917. (should (looking-at "item1"))
  1918. ;; 7.2. At an item in a list: expected to move at previous item.
  1919. (goto-line 8)
  1920. (org-backward-element)
  1921. (should (looking-at " - sub2"))
  1922. (goto-line 12)
  1923. (org-backward-element)
  1924. (should (looking-at "- item1"))
  1925. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  1926. ;; beginning.
  1927. (goto-line 10)
  1928. (org-backward-element)
  1929. (should (looking-at " - sub1"))
  1930. (goto-line 15)
  1931. (org-backward-element)
  1932. (should (looking-at "- item1"))
  1933. ;; 7.4. At blank-lines before list end: expected to move to top
  1934. ;; item.
  1935. (goto-line 14)
  1936. (org-backward-element)
  1937. (should (looking-at "- item1"))))
  1938. (ert-deftest test-org/up-element ()
  1939. "Test `org-up-element' specifications."
  1940. ;; 1. At BOB or with no surrounding element: should error.
  1941. (org-test-with-temp-text "Paragraph."
  1942. (should-error (org-up-element)))
  1943. (org-test-with-temp-text "* Head1\n* Head2"
  1944. (goto-line 2)
  1945. (should-error (org-up-element)))
  1946. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  1947. (goto-line 3)
  1948. (should-error (org-up-element)))
  1949. ;; 2. At an headline: move to parent headline.
  1950. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  1951. (goto-line 3)
  1952. (org-up-element)
  1953. (should (looking-at "\\* Head1")))
  1954. ;; 3. Inside a greater element: move to greater element beginning.
  1955. (org-test-with-temp-text
  1956. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  1957. (goto-line 3)
  1958. (org-up-element)
  1959. (should (looking-at "#\\+BEGIN_CENTER")))
  1960. ;; 4. List tests.
  1961. (org-test-with-temp-text "* Top
  1962. - item1
  1963. - sub1
  1964. - sub2
  1965. Paragraph within sub2.
  1966. - item2"
  1967. ;; 4.1. Within an item: move to the item beginning.
  1968. (goto-line 8)
  1969. (org-up-element)
  1970. (should (looking-at " - sub2"))
  1971. ;; 4.2. At an item in a sub-list: move to parent item.
  1972. (goto-line 4)
  1973. (org-up-element)
  1974. (should (looking-at "- item1"))
  1975. ;; 4.3. At an item in top list: move to beginning of whole list.
  1976. (goto-line 10)
  1977. (org-up-element)
  1978. (should (looking-at "- item1"))
  1979. ;; 4.4. Special case. At very top point: should move to parent of
  1980. ;; list.
  1981. (goto-line 2)
  1982. (org-up-element)
  1983. (should (looking-at "\\* Top"))))
  1984. (ert-deftest test-org/down-element ()
  1985. "Test `org-down-element' specifications."
  1986. ;; Error when the element hasn't got a recursive type.
  1987. (org-test-with-temp-text "Paragraph."
  1988. (should-error (org-down-element)))
  1989. ;; Error when the element has no contents
  1990. (org-test-with-temp-text "* Headline"
  1991. (should-error (org-down-element)))
  1992. ;; When at a plain-list, move to first item.
  1993. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  1994. (goto-line 2)
  1995. (org-down-element)
  1996. (should (looking-at " - Item 1.1")))
  1997. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  1998. (org-down-element)
  1999. (should (looking-at " Item 1")))
  2000. ;; When at a table, move to first row
  2001. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  2002. (org-down-element)
  2003. (should (looking-at " a | b |")))
  2004. ;; Otherwise, move inside the greater element.
  2005. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  2006. (org-down-element)
  2007. (should (looking-at "Paragraph"))))
  2008. (ert-deftest test-org/drag-element-backward ()
  2009. "Test `org-drag-element-backward' specifications."
  2010. ;; Standard test.
  2011. (should
  2012. (equal
  2013. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  2014. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  2015. (org-drag-element-backward)
  2016. (buffer-string))))
  2017. (should
  2018. (equal
  2019. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  2020. (org-test-with-temp-text
  2021. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  2022. (org-drag-element-backward)
  2023. (buffer-string))))
  2024. ;; Preserve blank lines.
  2025. (should
  2026. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  2027. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  2028. (org-drag-element-backward)
  2029. (buffer-string))))
  2030. ;; Preserve column.
  2031. (should
  2032. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  2033. (org-drag-element-backward)
  2034. (org-looking-at-p "2")))
  2035. ;; Error when trying to move first element of buffer.
  2036. (should-error
  2037. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2038. (org-drag-element-backward)))
  2039. ;; Error when trying to swap nested elements.
  2040. (should-error
  2041. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2042. (forward-line)
  2043. (org-drag-element-backward)))
  2044. ;; Error when trying to swap an headline element and a non-headline
  2045. ;; element.
  2046. (should-error
  2047. (org-test-with-temp-text "Test.\n* Head 1"
  2048. (forward-line)
  2049. (org-drag-element-backward)))
  2050. ;; Preserve visibility of elements and their contents.
  2051. (should
  2052. (equal '((63 . 82) (26 . 48))
  2053. (org-test-with-temp-text "
  2054. #+BEGIN_CENTER
  2055. Text.
  2056. #+END_CENTER
  2057. - item 1
  2058. #+BEGIN_QUOTE
  2059. Text.
  2060. #+END_QUOTE"
  2061. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2062. (search-backward "- item 1")
  2063. (org-drag-element-backward)
  2064. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2065. (overlays-in (point-min) (point-max)))))))
  2066. (ert-deftest test-org/drag-element-forward ()
  2067. "Test `org-drag-element-forward' specifications."
  2068. ;; 1. Error when trying to move first element of buffer.
  2069. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2070. (goto-line 3)
  2071. (should-error (org-drag-element-forward)))
  2072. ;; 2. Error when trying to swap nested elements.
  2073. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2074. (forward-line)
  2075. (should-error (org-drag-element-forward)))
  2076. ;; 3. Error when trying to swap a non-headline element and an
  2077. ;; headline.
  2078. (org-test-with-temp-text "Test.\n* Head 1"
  2079. (should-error (org-drag-element-forward)))
  2080. ;; 4. Otherwise, swap elements, preserving column and blank lines
  2081. ;; between elements.
  2082. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  2083. (search-forward "graph")
  2084. (org-drag-element-forward)
  2085. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  2086. (should (looking-at " 1")))
  2087. ;; 5. Preserve visibility of elements and their contents.
  2088. (org-test-with-temp-text "
  2089. #+BEGIN_CENTER
  2090. Text.
  2091. #+END_CENTER
  2092. - item 1
  2093. #+BEGIN_QUOTE
  2094. Text.
  2095. #+END_QUOTE"
  2096. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2097. (search-backward "#+BEGIN_CENTER")
  2098. (org-drag-element-forward)
  2099. (should
  2100. (equal
  2101. '((63 . 82) (26 . 48))
  2102. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2103. (overlays-in (point-min) (point-max)))))))
  2104. ;;; Outline structure
  2105. (ert-deftest test-org/demote ()
  2106. "Test `org-demote' specifications."
  2107. ;; Add correct number of stars according to `org-odd-levels-only'.
  2108. (should
  2109. (= 2
  2110. (org-test-with-temp-text "* H"
  2111. (let ((org-odd-levels-only nil)) (org-demote))
  2112. (org-current-level))))
  2113. (should
  2114. (= 3
  2115. (org-test-with-temp-text "* H"
  2116. (let ((org-odd-levels-only t)) (org-demote))
  2117. (org-current-level))))
  2118. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  2119. (should
  2120. (org-test-with-temp-text "* H :tag:"
  2121. (let ((org-tags-column 10)
  2122. (org-auto-align-tags t)
  2123. (org-odd-levels-only nil))
  2124. (org-demote))
  2125. (org-move-to-column 10)
  2126. (org-looking-at-p ":tag:$")))
  2127. (should-not
  2128. (org-test-with-temp-text "* H :tag:"
  2129. (let ((org-tags-column 10)
  2130. (org-auto-align-tags nil)
  2131. (org-odd-levels-only nil))
  2132. (org-demote))
  2133. (org-move-to-column 10)
  2134. (org-looking-at-p ":tag:$")))
  2135. ;; When `org-adapt-indentation' is non-nil, always indent planning
  2136. ;; info and property drawers accordingly.
  2137. (should
  2138. (= 3
  2139. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  2140. (let ((org-odd-levels-only nil)
  2141. (org-adapt-indentation t))
  2142. (org-demote))
  2143. (forward-line)
  2144. (org-get-indentation))))
  2145. (should
  2146. (= 3
  2147. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2148. (let ((org-odd-levels-only nil)
  2149. (org-adapt-indentation t))
  2150. (org-demote))
  2151. (forward-line)
  2152. (org-get-indentation))))
  2153. (should-not
  2154. (= 3
  2155. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  2156. (let ((org-odd-levels-only nil)
  2157. (org-adapt-indentation nil))
  2158. (org-demote))
  2159. (forward-line)
  2160. (org-get-indentation))))
  2161. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2162. ;; section accordingly. Ignore, however, footnote definitions and
  2163. ;; inlinetasks boundaries.
  2164. (should
  2165. (= 3
  2166. (org-test-with-temp-text "* H\n Paragraph"
  2167. (let ((org-odd-levels-only nil)
  2168. (org-adapt-indentation t))
  2169. (org-demote))
  2170. (forward-line)
  2171. (org-get-indentation))))
  2172. (should
  2173. (= 2
  2174. (org-test-with-temp-text "* H\n Paragraph"
  2175. (let ((org-odd-levels-only nil)
  2176. (org-adapt-indentation nil))
  2177. (org-demote))
  2178. (forward-line)
  2179. (org-get-indentation))))
  2180. (should
  2181. (zerop
  2182. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  2183. (let ((org-odd-levels-only nil)
  2184. (org-adapt-indentation t))
  2185. (org-demote))
  2186. (goto-char (point-max))
  2187. (org-get-indentation))))
  2188. (should
  2189. (= 3
  2190. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  2191. (let ((org-odd-levels-only nil)
  2192. (org-adapt-indentation t))
  2193. (org-demote))
  2194. (goto-char (point-max))
  2195. (org-get-indentation))))
  2196. (when (featurep 'org-inlinetask)
  2197. (should
  2198. (zerop
  2199. (let ((org-inlinetask-min-level 5)
  2200. (org-adapt-indentation t))
  2201. (org-test-with-temp-text "* H\n***** I\n***** END"
  2202. (org-demote)
  2203. (forward-line)
  2204. (org-get-indentation))))))
  2205. (when (featurep 'org-inlinetask)
  2206. (should
  2207. (= 3
  2208. (let ((org-inlinetask-min-level 5)
  2209. (org-adapt-indentation t))
  2210. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  2211. (org-demote)
  2212. (forward-line 2)
  2213. (org-get-indentation))))))
  2214. ;; Ignore contents of source blocks or example blocks when
  2215. ;; indentation should be preserved (through
  2216. ;; `org-src-preserve-indentation' or "-i" flag).
  2217. (should-not
  2218. (zerop
  2219. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2220. (let ((org-adapt-indentation t)
  2221. (org-src-preserve-indentation nil))
  2222. (org-demote))
  2223. (forward-line 2)
  2224. (org-get-indentation))))
  2225. (should
  2226. (zerop
  2227. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2228. (let ((org-adapt-indentation t)
  2229. (org-src-preserve-indentation t))
  2230. (org-demote))
  2231. (forward-line 2)
  2232. (org-get-indentation))))
  2233. (should
  2234. (zerop
  2235. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2236. (let ((org-adapt-indentation t)
  2237. (org-src-preserve-indentation t))
  2238. (org-demote))
  2239. (forward-line 2)
  2240. (org-get-indentation))))
  2241. (should
  2242. (zerop
  2243. (org-test-with-temp-text
  2244. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  2245. (let ((org-adapt-indentation t)
  2246. (org-src-preserve-indentation nil))
  2247. (org-demote))
  2248. (forward-line 2)
  2249. (org-get-indentation)))))
  2250. (ert-deftest test-org/promote ()
  2251. "Test `org-promote' specifications."
  2252. ;; Return an error if headline is to be promoted to level 0, unless
  2253. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  2254. ;; headline becomes a comment.
  2255. (should-error
  2256. (org-test-with-temp-text "* H"
  2257. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  2258. (should
  2259. (equal "# H"
  2260. (org-test-with-temp-text "* H"
  2261. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  2262. (buffer-string))))
  2263. ;; Remove correct number of stars according to
  2264. ;; `org-odd-levels-only'.
  2265. (should
  2266. (= 2
  2267. (org-test-with-temp-text "*** H"
  2268. (let ((org-odd-levels-only nil)) (org-promote))
  2269. (org-current-level))))
  2270. (should
  2271. (= 1
  2272. (org-test-with-temp-text "*** H"
  2273. (let ((org-odd-levels-only t)) (org-promote))
  2274. (org-current-level))))
  2275. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  2276. (should
  2277. (org-test-with-temp-text "** H :tag:"
  2278. (let ((org-tags-column 10)
  2279. (org-auto-align-tags t)
  2280. (org-odd-levels-only nil))
  2281. (org-promote))
  2282. (org-move-to-column 10)
  2283. (org-looking-at-p ":tag:$")))
  2284. (should-not
  2285. (org-test-with-temp-text "** H :tag:"
  2286. (let ((org-tags-column 10)
  2287. (org-auto-align-tags nil)
  2288. (org-odd-levels-only nil))
  2289. (org-promote))
  2290. (org-move-to-column 10)
  2291. (org-looking-at-p ":tag:$")))
  2292. ;; When `org-adapt-indentation' is non-nil, always indent planning
  2293. ;; info and property drawers.
  2294. (should
  2295. (= 2
  2296. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2297. (let ((org-odd-levels-only nil)
  2298. (org-adapt-indentation t))
  2299. (org-promote))
  2300. (forward-line)
  2301. (org-get-indentation))))
  2302. (should
  2303. (= 2
  2304. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2305. (let ((org-odd-levels-only nil)
  2306. (org-adapt-indentation t))
  2307. (org-promote))
  2308. (forward-line)
  2309. (org-get-indentation))))
  2310. (should-not
  2311. (= 2
  2312. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2313. (let ((org-odd-levels-only nil)
  2314. (org-adapt-indentation nil))
  2315. (org-promote))
  2316. (forward-line)
  2317. (org-get-indentation))))
  2318. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2319. ;; section accordingly. Ignore, however, footnote definitions and
  2320. ;; inlinetasks boundaries.
  2321. (should
  2322. (= 2
  2323. (org-test-with-temp-text "** H\n Paragraph"
  2324. (let ((org-odd-levels-only nil)
  2325. (org-adapt-indentation t))
  2326. (org-promote))
  2327. (forward-line)
  2328. (org-get-indentation))))
  2329. (should-not
  2330. (= 2
  2331. (org-test-with-temp-text "** H\n Paragraph"
  2332. (let ((org-odd-levels-only nil)
  2333. (org-adapt-indentation nil))
  2334. (org-promote))
  2335. (forward-line)
  2336. (org-get-indentation))))
  2337. (should
  2338. (= 2
  2339. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  2340. (let ((org-odd-levels-only nil)
  2341. (org-adapt-indentation t))
  2342. (org-promote))
  2343. (forward-line)
  2344. (org-get-indentation))))
  2345. (when (featurep 'org-inlinetask)
  2346. (should
  2347. (zerop
  2348. (let ((org-inlinetask-min-level 5)
  2349. (org-adapt-indentation t))
  2350. (org-test-with-temp-text "** H\n***** I\n***** END"
  2351. (org-promote)
  2352. (forward-line)
  2353. (org-get-indentation))))))
  2354. (when (featurep 'org-inlinetask)
  2355. (should
  2356. (= 2
  2357. (let ((org-inlinetask-min-level 5)
  2358. (org-adapt-indentation t))
  2359. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  2360. (org-promote)
  2361. (forward-line 2)
  2362. (org-get-indentation))))))
  2363. ;; Give up shifting if it would break document's structure
  2364. ;; otherwise.
  2365. (should
  2366. (= 3
  2367. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  2368. (let ((org-odd-levels-only nil)
  2369. (org-adapt-indentation t))
  2370. (org-promote))
  2371. (forward-line)
  2372. (org-get-indentation))))
  2373. (should
  2374. (= 3
  2375. (org-test-with-temp-text "** H\n Paragraph\n * list."
  2376. (let ((org-odd-levels-only nil)
  2377. (org-adapt-indentation t))
  2378. (org-promote))
  2379. (forward-line)
  2380. (org-get-indentation))))
  2381. ;; Ignore contents of source blocks or example blocks when
  2382. ;; indentation should be preserved (through
  2383. ;; `org-src-preserve-indentation' or "-i" flag).
  2384. (should-not
  2385. (zerop
  2386. (org-test-with-temp-text
  2387. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2388. (let ((org-adapt-indentation t)
  2389. (org-src-preserve-indentation nil)
  2390. (org-odd-levels-only nil))
  2391. (org-promote))
  2392. (forward-line)
  2393. (org-get-indentation))))
  2394. (should
  2395. (zerop
  2396. (org-test-with-temp-text
  2397. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  2398. (let ((org-adapt-indentation t)
  2399. (org-src-preserve-indentation t)
  2400. (org-odd-levels-only nil))
  2401. (org-promote))
  2402. (forward-line)
  2403. (org-get-indentation))))
  2404. (should
  2405. (zerop
  2406. (org-test-with-temp-text
  2407. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2408. (let ((org-adapt-indentation t)
  2409. (org-src-preserve-indentation t)
  2410. (org-odd-levels-only nil))
  2411. (org-promote))
  2412. (forward-line)
  2413. (org-get-indentation))))
  2414. (should
  2415. (zerop
  2416. (org-test-with-temp-text
  2417. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  2418. (let ((org-adapt-indentation t)
  2419. (org-src-preserve-indentation nil)
  2420. (org-odd-levels-only nil))
  2421. (org-promote))
  2422. (forward-line)
  2423. (org-get-indentation)))))
  2424. ;;; Planning
  2425. (ert-deftest test-org/at-planning-p ()
  2426. "Test `org-at-planning-p' specifications."
  2427. ;; Regular test.
  2428. (should
  2429. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  2430. (org-at-planning-p)))
  2431. (should-not
  2432. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  2433. (org-at-planning-p)))
  2434. ;; Correctly find planning attached to inlinetasks.
  2435. (when (featurep 'org-inlinetask)
  2436. (should
  2437. (org-test-with-temp-text
  2438. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  2439. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2440. (should-not
  2441. (org-test-with-temp-text
  2442. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  2443. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2444. (should-not
  2445. (org-test-with-temp-text
  2446. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  2447. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2448. (should-not
  2449. (org-test-with-temp-text
  2450. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  2451. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  2452. ;;; Property API
  2453. (ert-deftest test-org/buffer-property-keys ()
  2454. "Test `org-buffer-property-keys' specifications."
  2455. ;; Retrieve properties accross siblings.
  2456. (should
  2457. (equal '("A" "B")
  2458. (org-test-with-temp-text "
  2459. * H1
  2460. :PROPERTIES:
  2461. :A: 1
  2462. :END:
  2463. * H2
  2464. :PROPERTIES:
  2465. :B: 1
  2466. :END:"
  2467. (org-buffer-property-keys))))
  2468. ;; Retrieve properties accross children.
  2469. (should
  2470. (equal '("A" "B")
  2471. (org-test-with-temp-text "
  2472. * H1
  2473. :PROPERTIES:
  2474. :A: 1
  2475. :END:
  2476. ** H2
  2477. :PROPERTIES:
  2478. :B: 1
  2479. :END:"
  2480. (org-buffer-property-keys))))
  2481. ;; Retrieve muliple properties in the same drawer.
  2482. (should
  2483. (equal '("A" "B")
  2484. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2485. (org-buffer-property-keys))))
  2486. ;; Ignore extension symbol in property name.
  2487. (should
  2488. (equal '("A")
  2489. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  2490. (org-buffer-property-keys))))
  2491. ;; With non-nil COLUMNS, extract property names from columns.
  2492. (should
  2493. (equal '("A" "B")
  2494. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  2495. (org-buffer-property-keys nil nil t))))
  2496. (should
  2497. (equal '("A" "B" "COLUMNS")
  2498. (org-test-with-temp-text
  2499. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  2500. (org-buffer-property-keys nil nil t)))))
  2501. (ert-deftest test-org/property-values ()
  2502. "Test `org-property-values' specifications."
  2503. ;; Regular test.
  2504. (should
  2505. (equal '("2" "1")
  2506. (org-test-with-temp-text
  2507. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  2508. (org-property-values "A"))))
  2509. ;; Ignore empty values.
  2510. (should-not
  2511. (org-test-with-temp-text
  2512. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  2513. (org-property-values "A")))
  2514. ;; Take into consideration extended values.
  2515. (should
  2516. (equal '("1 2")
  2517. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  2518. (org-property-values "A")))))
  2519. (ert-deftest test-org/entry-delete ()
  2520. "Test `org-entry-delete' specifications."
  2521. ;; Regular test.
  2522. (should
  2523. (string-match
  2524. " *:PROPERTIES:\n *:B: +2\n *:END:"
  2525. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2526. (org-entry-delete (point) "A")
  2527. (buffer-string))))
  2528. ;; When last property is removed, remove the property drawer.
  2529. (should-not
  2530. (string-match
  2531. ":PROPERTIES:"
  2532. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  2533. (org-entry-delete (point) "A")
  2534. (buffer-string)))))
  2535. (ert-deftest test-org/entry-get ()
  2536. "Test `org-entry-get' specifications."
  2537. ;; Regular test.
  2538. (should
  2539. (equal "1"
  2540. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2541. (org-entry-get (point) "A"))))
  2542. ;; Ignore case.
  2543. (should
  2544. (equal "1"
  2545. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2546. (org-entry-get (point) "a"))))
  2547. ;; Handle extended values, both before and after base value.
  2548. (should
  2549. (equal "1 2 3"
  2550. (org-test-with-temp-text
  2551. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2552. (org-entry-get (point) "A"))))
  2553. ;; Empty values are returned as the empty string.
  2554. (should
  2555. (equal ""
  2556. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  2557. (org-entry-get (point) "A"))))
  2558. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  2559. ;; otherwise, return nil.
  2560. (should-not
  2561. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2562. (org-entry-get (point) "A")))
  2563. (should
  2564. (equal "nil"
  2565. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  2566. (org-entry-get (point) "A" nil t))))
  2567. ;; Return nil when no property is found, independently on the
  2568. ;; LITERAL-NIL argument.
  2569. (should-not
  2570. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2571. (org-entry-get (point) "B")))
  2572. (should-not
  2573. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2574. (org-entry-get (point) "B" nil t)))
  2575. ;; Handle inheritance, when allowed.
  2576. (should
  2577. (equal
  2578. "1"
  2579. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2580. (org-entry-get (point) "A" t))))
  2581. (should
  2582. (equal
  2583. "1"
  2584. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2585. (let ((org-use-property-inheritance t))
  2586. (org-entry-get (point) "A" 'selective)))))
  2587. (should-not
  2588. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  2589. (let ((org-use-property-inheritance nil))
  2590. (org-entry-get (point) "A" 'selective)))))
  2591. (ert-deftest test-org/entry-properties ()
  2592. "Test `org-entry-properties' specifications."
  2593. ;; Get "ITEM" property.
  2594. (should
  2595. (equal "* H"
  2596. (org-test-with-temp-text "* TODO H"
  2597. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  2598. (should
  2599. (equal "* H"
  2600. (org-test-with-temp-text "* TODO H"
  2601. (cdr (assoc "ITEM" (org-entry-properties))))))
  2602. ;; Get "TODO" property.
  2603. (should
  2604. (equal "TODO"
  2605. (org-test-with-temp-text "* TODO H"
  2606. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  2607. (should
  2608. (equal "TODO"
  2609. (org-test-with-temp-text "* TODO H"
  2610. (cdr (assoc "TODO" (org-entry-properties))))))
  2611. (should-not
  2612. (org-test-with-temp-text "* H"
  2613. (assoc "TODO" (org-entry-properties nil "TODO"))))
  2614. ;; Get "PRIORITY" property.
  2615. (should
  2616. (equal "A"
  2617. (org-test-with-temp-text "* [#A] H"
  2618. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  2619. (should
  2620. (equal "A"
  2621. (org-test-with-temp-text "* [#A] H"
  2622. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  2623. (should-not
  2624. (org-test-with-temp-text "* H"
  2625. (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
  2626. ;; Get "FILE" property.
  2627. (should
  2628. (org-test-with-temp-text-in-file "* H\nParagraph"
  2629. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  2630. (buffer-file-name))))
  2631. (should
  2632. (org-test-with-temp-text-in-file "* H\nParagraph"
  2633. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  2634. (buffer-file-name))))
  2635. (should-not
  2636. (org-test-with-temp-text "* H\nParagraph"
  2637. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  2638. ;; Get "TAGS" property.
  2639. (should
  2640. (equal ":tag1:tag2:"
  2641. (org-test-with-temp-text "* H :tag1:tag2:"
  2642. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  2643. (should
  2644. (equal ":tag1:tag2:"
  2645. (org-test-with-temp-text "* H :tag1:tag2:"
  2646. (cdr (assoc "TAGS" (org-entry-properties))))))
  2647. (should-not
  2648. (org-test-with-temp-text "* H"
  2649. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  2650. ;; Get "ALLTAGS" property.
  2651. (should
  2652. (equal ":tag1:tag2:"
  2653. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  2654. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  2655. (should
  2656. (equal ":tag1:tag2:"
  2657. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  2658. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  2659. (should-not
  2660. (org-test-with-temp-text "* H"
  2661. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  2662. ;; Get "BLOCKED" property.
  2663. (should
  2664. (equal "t"
  2665. (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
  2666. (let ((org-enforce-todo-dependencies t)
  2667. (org-blocker-hook
  2668. '(org-block-todo-from-children-or-siblings-or-parent)))
  2669. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2670. (should
  2671. (equal "t"
  2672. (org-test-with-temp-text "* Blocked\n** DONE one\n** TODO two"
  2673. (let ((org-enforce-todo-dependencies t)
  2674. (org-blocker-hook
  2675. '(org-block-todo-from-children-or-siblings-or-parent)))
  2676. (cdr (assoc "BLOCKED" (org-entry-properties)))))))
  2677. (should
  2678. (equal ""
  2679. (org-test-with-temp-text "* Blocked\n** DONE one\n** DONE two"
  2680. (let ((org-enforce-todo-dependencies t))
  2681. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  2682. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  2683. (should
  2684. (equal
  2685. "[2012-03-29 thu.]"
  2686. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  2687. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  2688. (should
  2689. (equal
  2690. "[2012-03-29 thu.]"
  2691. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  2692. (cdr (assoc "CLOSED" (org-entry-properties))))))
  2693. (should-not
  2694. (org-test-with-temp-text "* H"
  2695. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  2696. (should
  2697. (equal
  2698. "<2014-03-04 tue.>"
  2699. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2700. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  2701. (should
  2702. (equal
  2703. "<2014-03-04 tue.>"
  2704. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2705. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  2706. (should-not
  2707. (org-test-with-temp-text "* H"
  2708. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  2709. (should
  2710. (equal
  2711. "<2014-03-04 tue.>"
  2712. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2713. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  2714. (should
  2715. (equal
  2716. "<2014-03-04 tue.>"
  2717. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2718. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  2719. (should-not
  2720. (org-test-with-temp-text "* H"
  2721. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  2722. ;; Get "CATEGORY"
  2723. (should
  2724. (equal "cat"
  2725. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  2726. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  2727. (should
  2728. (equal "cat"
  2729. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  2730. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2731. (should
  2732. (equal "cat"
  2733. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  2734. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  2735. ;; Get standard properties.
  2736. (should
  2737. (equal "1"
  2738. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2739. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2740. ;; Handle extended properties.
  2741. (should
  2742. (equal "1 2 3"
  2743. (org-test-with-temp-text
  2744. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  2745. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2746. (should
  2747. (equal "1 2 3"
  2748. (org-test-with-temp-text
  2749. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  2750. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  2751. ;; Ignore forbidden (special) properties.
  2752. (should-not
  2753. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  2754. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  2755. (ert-deftest test-org/entry-put ()
  2756. "Test `org-entry-put' specifications."
  2757. ;; Error when not a string or nil.
  2758. (should-error
  2759. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  2760. (org-entry-put 1 "test" 2)))
  2761. ;; Set "TODO" property.
  2762. (should
  2763. (string-match (regexp-quote " TODO H")
  2764. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2765. (org-entry-put (point) "TODO" "TODO")
  2766. (buffer-string))))
  2767. (should
  2768. (string-match (regexp-quote "* H")
  2769. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  2770. (org-entry-put (point) "TODO" nil)
  2771. (buffer-string))))
  2772. ;; Set "PRIORITY" property.
  2773. (should
  2774. (equal "* [#A] H"
  2775. (org-test-with-temp-text "* [#B] H"
  2776. (org-entry-put (point) "PRIORITY" "A")
  2777. (buffer-string))))
  2778. (should
  2779. (equal "* H"
  2780. (org-test-with-temp-text "* [#B] H"
  2781. (org-entry-put (point) "PRIORITY" nil)
  2782. (buffer-string))))
  2783. ;; Set "SCHEDULED" property.
  2784. (should
  2785. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  2786. (org-test-with-temp-text "* H"
  2787. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  2788. (buffer-string))))
  2789. (should
  2790. (string= "* H\n"
  2791. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2792. (org-entry-put (point) "SCHEDULED" nil)
  2793. (buffer-string))))
  2794. (should
  2795. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  2796. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2797. (org-entry-put (point) "SCHEDULED" "earlier")
  2798. (buffer-string))))
  2799. (should
  2800. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  2801. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  2802. (org-entry-put (point) "SCHEDULED" "later")
  2803. (buffer-string))))
  2804. ;; Set "DEADLINE" property.
  2805. (should
  2806. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  2807. (org-test-with-temp-text "* H"
  2808. (org-entry-put (point) "DEADLINE" "2014-03-04")
  2809. (buffer-string))))
  2810. (should
  2811. (string= "* H\n"
  2812. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2813. (org-entry-put (point) "DEADLINE" nil)
  2814. (buffer-string))))
  2815. (should
  2816. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  2817. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2818. (org-entry-put (point) "DEADLINE" "earlier")
  2819. (buffer-string))))
  2820. (should
  2821. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  2822. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  2823. (org-entry-put (point) "DEADLINE" "later")
  2824. (buffer-string))))
  2825. ;; Regular properties, with or without pre-existing drawer.
  2826. (should
  2827. (string-match "^ *:A: +2$"
  2828. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  2829. (org-entry-put (point) "A" "2")
  2830. (buffer-string))))
  2831. (should
  2832. (string-match "^ *:A: +1$"
  2833. (org-test-with-temp-text "* H"
  2834. (org-entry-put (point) "A" "1")
  2835. (buffer-string))))
  2836. ;; Special case: two consecutive headlines.
  2837. (should
  2838. (string-match "\\* A\n *:PROPERTIES:"
  2839. (org-test-with-temp-text "* A\n** B"
  2840. (org-entry-put (point) "A" "1")
  2841. (buffer-string)))))
  2842. ;;; Radio Targets
  2843. (ert-deftest test-org/update-radio-target-regexp ()
  2844. "Test `org-update-radio-target-regexp' specifications."
  2845. ;; Properly update cache with no previous radio target regexp.
  2846. (should
  2847. (eq 'link
  2848. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2849. (save-excursion (goto-char (point-max)) (org-element-context))
  2850. (insert "<<<")
  2851. (search-forward "o")
  2852. (insert ">>>")
  2853. (org-update-radio-target-regexp)
  2854. (goto-char (point-max))
  2855. (org-element-type (org-element-context)))))
  2856. ;; Properly update cache with previous radio target regexp.
  2857. (should
  2858. (eq 'link
  2859. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  2860. (save-excursion (goto-char (point-max)) (org-element-context))
  2861. (insert "<<<")
  2862. (search-forward "o")
  2863. (insert ">>>")
  2864. (org-update-radio-target-regexp)
  2865. (search-backward "r")
  2866. (delete-char 5)
  2867. (insert "new")
  2868. (org-update-radio-target-regexp)
  2869. (goto-char (point-max))
  2870. (delete-region (line-beginning-position) (point))
  2871. (insert "new")
  2872. (org-element-type (org-element-context))))))
  2873. ;;; Sparse trees
  2874. (ert-deftest test-org/match-sparse-tree ()
  2875. "Test `org-match-sparse-tree' specifications."
  2876. ;; Match tags.
  2877. (should-not
  2878. (org-test-with-temp-text "* H\n** H1 :tag:"
  2879. (org-match-sparse-tree nil "tag")
  2880. (search-forward "H1")
  2881. (org-invisible-p2)))
  2882. (should
  2883. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  2884. (org-match-sparse-tree nil "tag")
  2885. (search-forward "H2")
  2886. (org-invisible-p2)))
  2887. ;; "-" operator for tags.
  2888. (should-not
  2889. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  2890. (org-match-sparse-tree nil "tag1-tag2")
  2891. (search-forward "H1")
  2892. (org-invisible-p2)))
  2893. (should
  2894. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  2895. (org-match-sparse-tree nil "tag1-tag2")
  2896. (search-forward "H2")
  2897. (org-invisible-p2)))
  2898. ;; "&" operator for tags.
  2899. (should
  2900. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  2901. (org-match-sparse-tree nil "tag1&tag2")
  2902. (search-forward "H1")
  2903. (org-invisible-p2)))
  2904. (should-not
  2905. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  2906. (org-match-sparse-tree nil "tag1&tag2")
  2907. (search-forward "H2")
  2908. (org-invisible-p2)))
  2909. ;; "|" operator for tags.
  2910. (should-not
  2911. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  2912. (org-match-sparse-tree nil "tag1|tag2")
  2913. (search-forward "H1")
  2914. (org-invisible-p2)))
  2915. (should-not
  2916. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  2917. (org-match-sparse-tree nil "tag1|tag2")
  2918. (search-forward "H2")
  2919. (org-invisible-p2)))
  2920. ;; Regexp match on tags.
  2921. (should-not
  2922. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  2923. (org-match-sparse-tree nil "{^tag.*}")
  2924. (search-forward "H1")
  2925. (org-invisible-p2)))
  2926. (should
  2927. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  2928. (org-match-sparse-tree nil "{^tag.*}")
  2929. (search-forward "H2")
  2930. (org-invisible-p2)))
  2931. ;; Match group tags.
  2932. (should-not
  2933. (org-test-with-temp-text
  2934. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  2935. (org-match-sparse-tree nil "work")
  2936. (search-forward "H1")
  2937. (org-invisible-p2)))
  2938. (should-not
  2939. (org-test-with-temp-text
  2940. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  2941. (org-match-sparse-tree nil "work")
  2942. (search-forward "H2")
  2943. (org-invisible-p2)))
  2944. ;; Match properties.
  2945. (should
  2946. (org-test-with-temp-text
  2947. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  2948. (org-match-sparse-tree nil "A=\"1\"")
  2949. (search-forward "H2")
  2950. (org-invisible-p2)))
  2951. (should-not
  2952. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  2953. (org-match-sparse-tree nil "A=\"1\"")
  2954. (search-forward "H2")
  2955. (org-invisible-p2)))
  2956. ;; Case is not significant when matching properties.
  2957. (should-not
  2958. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  2959. (org-match-sparse-tree nil "a=\"1\"")
  2960. (search-forward "H2")
  2961. (org-invisible-p2)))
  2962. (should-not
  2963. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  2964. (org-match-sparse-tree nil "A=\"1\"")
  2965. (search-forward "H2")
  2966. (org-invisible-p2)))
  2967. ;; Match special LEVEL property.
  2968. (should-not
  2969. (org-test-with-temp-text "* H\n** H1\n*** H2"
  2970. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  2971. (search-forward "H1")
  2972. (org-invisible-p2)))
  2973. (should
  2974. (org-test-with-temp-text "* H\n** H1\n*** H2"
  2975. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  2976. (search-forward "H2")
  2977. (org-invisible-p2)))
  2978. ;; Comparison operators when matching properties.
  2979. (should
  2980. (org-test-with-temp-text
  2981. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  2982. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  2983. (search-forward "H1")
  2984. (org-invisible-p2)))
  2985. (should-not
  2986. (org-test-with-temp-text
  2987. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  2988. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  2989. (search-forward "H2")
  2990. (org-invisible-p2)))
  2991. ;; Regexp match on properties values.
  2992. (should-not
  2993. (org-test-with-temp-text
  2994. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  2995. (org-match-sparse-tree nil "A={f.*}")
  2996. (search-forward "H1")
  2997. (org-invisible-p2)))
  2998. (should
  2999. (org-test-with-temp-text
  3000. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  3001. (org-match-sparse-tree nil "A={f.*}")
  3002. (search-forward "H2")
  3003. (org-invisible-p2)))
  3004. ;; With an optional argument, limit match to TODO entries.
  3005. (should-not
  3006. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  3007. (org-match-sparse-tree t "tag")
  3008. (search-forward "H1")
  3009. (org-invisible-p2)))
  3010. (should
  3011. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  3012. (org-match-sparse-tree t "tag")
  3013. (search-forward "H2")
  3014. (org-invisible-p2))))
  3015. ;;; Timestamps API
  3016. (ert-deftest test-org/time-stamp ()
  3017. "Test `org-time-stamp' specifications."
  3018. ;; Insert chosen time stamp at point.
  3019. (should
  3020. (string-match
  3021. "Te<2014-03-04 .*?>xt"
  3022. (org-test-with-temp-text "Te<point>xt"
  3023. (flet ((org-read-date
  3024. (&rest args)
  3025. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3026. (org-time-stamp nil)
  3027. (buffer-string)))))
  3028. ;; With a prefix argument, also insert time.
  3029. (should
  3030. (string-match
  3031. "Te<2014-03-04 .*? 00:41>xt"
  3032. (org-test-with-temp-text "Te<point>xt"
  3033. (flet ((org-read-date
  3034. (&rest args)
  3035. (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
  3036. (org-time-stamp '(4))
  3037. (buffer-string)))))
  3038. ;; With two universal prefix arguments, insert an active timestamp
  3039. ;; with the current time without prompting the user.
  3040. (should
  3041. (string-match
  3042. "Te<2014-03-04 .*? 00:41>xt"
  3043. (org-test-with-temp-text "Te<point>xt"
  3044. (flet ((current-time
  3045. ()
  3046. (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
  3047. (org-time-stamp '(16))
  3048. (buffer-string)))))
  3049. ;; When optional argument is non-nil, insert an inactive timestamp.
  3050. (should
  3051. (string-match
  3052. "Te\\[2014-03-04 .*?\\]xt"
  3053. (org-test-with-temp-text "Te<point>xt"
  3054. (flet ((org-read-date
  3055. (&rest args)
  3056. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3057. (org-time-stamp nil t)
  3058. (buffer-string)))))
  3059. ;; When called from a timestamp, replace existing one.
  3060. (should
  3061. (string-match
  3062. "<2014-03-04 .*?>"
  3063. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  3064. (flet ((org-read-date
  3065. (&rest args)
  3066. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3067. (org-time-stamp nil)
  3068. (buffer-string)))))
  3069. (should
  3070. (string-match
  3071. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  3072. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  3073. (flet ((org-read-date
  3074. (&rest args)
  3075. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3076. (org-time-stamp nil)
  3077. (buffer-string)))))
  3078. ;; When replacing a timestamp, preserve repeater, if any.
  3079. (should
  3080. (string-match
  3081. "<2014-03-04 .*? \\+2y>"
  3082. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  3083. (flet ((org-read-date
  3084. (&rest args)
  3085. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3086. (org-time-stamp nil)
  3087. (buffer-string)))))
  3088. ;; When called twice in a raw, build a date range.
  3089. (should
  3090. (string-match
  3091. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  3092. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  3093. (flet ((org-read-date
  3094. (&rest args)
  3095. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3096. (let ((last-command 'org-time-stamp)
  3097. (this-command 'org-time-stamp))
  3098. (org-time-stamp nil))
  3099. (buffer-string))))))
  3100. (ert-deftest test-org/timestamp-has-time-p ()
  3101. "Test `org-timestamp-has-time-p' specifications."
  3102. ;; With time.
  3103. (should
  3104. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  3105. (org-timestamp-has-time-p (org-element-context))))
  3106. ;; Without time.
  3107. (should-not
  3108. (org-test-with-temp-text "<2012-03-29 Thu>"
  3109. (org-timestamp-has-time-p (org-element-context)))))
  3110. (ert-deftest test-org/timestamp-format ()
  3111. "Test `org-timestamp-format' specifications."
  3112. ;; Regular test.
  3113. (should
  3114. (equal
  3115. "2012-03-29 16:40"
  3116. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  3117. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  3118. ;; Range end.
  3119. (should
  3120. (equal
  3121. "2012-03-29"
  3122. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  3123. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  3124. (ert-deftest test-org/timestamp-split-range ()
  3125. "Test `org-timestamp-split-range' specifications."
  3126. ;; Extract range start (active).
  3127. (should
  3128. (equal '(2012 3 29)
  3129. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3130. (let ((ts (org-timestamp-split-range (org-element-context))))
  3131. (mapcar (lambda (p) (org-element-property p ts))
  3132. '(:year-end :month-end :day-end))))))
  3133. ;; Extract range start (inactive)
  3134. (should
  3135. (equal '(2012 3 29)
  3136. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  3137. (let ((ts (org-timestamp-split-range (org-element-context))))
  3138. (mapcar (lambda (p) (org-element-property p ts))
  3139. '(:year-end :month-end :day-end))))))
  3140. ;; Extract range end (active).
  3141. (should
  3142. (equal '(2012 3 30)
  3143. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3144. (let ((ts (org-timestamp-split-range
  3145. (org-element-context) t)))
  3146. (mapcar (lambda (p) (org-element-property p ts))
  3147. '(:year-end :month-end :day-end))))))
  3148. ;; Extract range end (inactive)
  3149. (should
  3150. (equal '(2012 3 30)
  3151. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  3152. (let ((ts (org-timestamp-split-range
  3153. (org-element-context) t)))
  3154. (mapcar (lambda (p) (org-element-property p ts))
  3155. '(:year-end :month-end :day-end))))))
  3156. ;; Return the timestamp if not a range.
  3157. (should
  3158. (org-test-with-temp-text "[2012-03-29 Thu]"
  3159. (let* ((ts-orig (org-element-context))
  3160. (ts-copy (org-timestamp-split-range ts-orig)))
  3161. (eq ts-orig ts-copy))))
  3162. (should
  3163. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  3164. (let* ((ts-orig (org-element-context))
  3165. (ts-copy (org-timestamp-split-range ts-orig)))
  3166. (eq ts-orig ts-copy)))))
  3167. (ert-deftest test-org/timestamp-translate ()
  3168. "Test `org-timestamp-translate' specifications."
  3169. ;; Translate whole date range.
  3170. (should
  3171. (equal "<29>--<30>"
  3172. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3173. (let ((org-display-custom-times t)
  3174. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3175. (org-timestamp-translate (org-element-context))))))
  3176. ;; Translate date range start.
  3177. (should
  3178. (equal "<29>"
  3179. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3180. (let ((org-display-custom-times t)
  3181. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3182. (org-timestamp-translate (org-element-context) 'start)))))
  3183. ;; Translate date range end.
  3184. (should
  3185. (equal "<30>"
  3186. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3187. (let ((org-display-custom-times t)
  3188. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3189. (org-timestamp-translate (org-element-context) 'end)))))
  3190. ;; Translate time range.
  3191. (should
  3192. (equal "<08>--<16>"
  3193. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  3194. (let ((org-display-custom-times t)
  3195. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  3196. (org-timestamp-translate (org-element-context))))))
  3197. ;; Translate non-range timestamp.
  3198. (should
  3199. (equal "<29>"
  3200. (org-test-with-temp-text "<2012-03-29 Thu>"
  3201. (let ((org-display-custom-times t)
  3202. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3203. (org-timestamp-translate (org-element-context))))))
  3204. ;; Do not change `diary' timestamps.
  3205. (should
  3206. (equal "<%%(org-float t 4 2)>"
  3207. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  3208. (let ((org-display-custom-times t)
  3209. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3210. (org-timestamp-translate (org-element-context)))))))
  3211. ;;; Visibility
  3212. (ert-deftest test-org/flag-drawer ()
  3213. "Test `org-flag-drawer' specifications."
  3214. ;; Hide drawer.
  3215. (should
  3216. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3217. (org-flag-drawer t)
  3218. (get-char-property (line-end-position) 'invisible)))
  3219. ;; Show drawer.
  3220. (should-not
  3221. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3222. (org-flag-drawer t)
  3223. (org-flag-drawer nil)
  3224. (get-char-property (line-end-position) 'invisible)))
  3225. ;; Test optional argument.
  3226. (should
  3227. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  3228. (let ((drawer (save-excursion (search-forward ":D2")
  3229. (org-element-at-point))))
  3230. (org-flag-drawer t drawer)
  3231. (get-char-property (progn (search-forward ":D2") (line-end-position))
  3232. 'invisible))))
  3233. (should-not
  3234. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  3235. (let ((drawer (save-excursion (search-forward ":D2")
  3236. (org-element-at-point))))
  3237. (org-flag-drawer t drawer)
  3238. (get-char-property (line-end-position) 'invisible))))
  3239. ;; Do not hide fake drawers.
  3240. (should-not
  3241. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  3242. (forward-line 1)
  3243. (org-flag-drawer t)
  3244. (get-char-property (line-end-position) 'invisible)))
  3245. ;; Do not hide incomplete drawers.
  3246. (should-not
  3247. (org-test-with-temp-text ":D:\nparagraph"
  3248. (forward-line 1)
  3249. (org-flag-drawer t)
  3250. (get-char-property (line-end-position) 'invisible)))
  3251. ;; Do not hide drawers when called from final blank lines.
  3252. (should-not
  3253. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  3254. (goto-char (point-max))
  3255. (org-flag-drawer t)
  3256. (goto-char (point-min))
  3257. (get-char-property (line-end-position) 'invisible)))
  3258. ;; Don't leave point in an invisible part of the buffer when hiding
  3259. ;; a drawer away.
  3260. (should-not
  3261. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3262. (goto-char (point-max))
  3263. (org-flag-drawer t)
  3264. (get-char-property (point) 'invisible))))
  3265. (ert-deftest test-org/hide-block-toggle ()
  3266. "Test `org-hide-block-toggle' specifications."
  3267. ;; Error when not at a block.
  3268. (should-error
  3269. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  3270. (org-hide-block-toggle 'off)
  3271. (get-char-property (line-end-position) 'invisible)))
  3272. ;; Hide block.
  3273. (should
  3274. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3275. (org-hide-block-toggle)
  3276. (get-char-property (line-end-position) 'invisible)))
  3277. (should
  3278. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  3279. (org-hide-block-toggle)
  3280. (get-char-property (line-end-position) 'invisible)))
  3281. ;; Show block unconditionally when optional argument is `off'.
  3282. (should-not
  3283. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3284. (org-hide-block-toggle)
  3285. (org-hide-block-toggle 'off)
  3286. (get-char-property (line-end-position) 'invisible)))
  3287. (should-not
  3288. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3289. (org-hide-block-toggle 'off)
  3290. (get-char-property (line-end-position) 'invisible)))
  3291. ;; Hide block unconditionally when optional argument is non-nil.
  3292. (should
  3293. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3294. (org-hide-block-toggle t)
  3295. (get-char-property (line-end-position) 'invisible)))
  3296. (should
  3297. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3298. (org-hide-block-toggle)
  3299. (org-hide-block-toggle t)
  3300. (get-char-property (line-end-position) 'invisible)))
  3301. ;; Do not hide block when called from final blank lines.
  3302. (should-not
  3303. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  3304. (org-hide-block-toggle)
  3305. (goto-char (point-min))
  3306. (get-char-property (line-end-position) 'invisible)))
  3307. ;; Don't leave point in an invisible part of the buffer when hiding
  3308. ;; a block away.
  3309. (should-not
  3310. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  3311. (org-hide-block-toggle)
  3312. (get-char-property (point) 'invisible))))
  3313. (ert-deftest test-org/hide-block-toggle-maybe ()
  3314. "Test `org-hide-block-toggle-maybe' specifications."
  3315. (should
  3316. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  3317. (org-hide-block-toggle-maybe)))
  3318. (should-not
  3319. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  3320. (provide 'test-org)
  3321. ;;; test-org.el ends here