test-org.el 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180
  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 |\n"
  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 diary sexps, footnote definitions or headlines.
  554. (should
  555. (zerop
  556. (org-test-with-temp-text "%%(org-calendar-holiday)"
  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. (should
  565. (zerop
  566. (org-test-with-temp-text "* H"
  567. (org-indent-line)
  568. (org-get-indentation))))
  569. ;; Do not indent before first headline.
  570. (should
  571. (zerop
  572. (org-test-with-temp-text ""
  573. (org-indent-line)
  574. (org-get-indentation))))
  575. ;; Indent according to headline level otherwise, unless
  576. ;; `org-adapt-indentation' is nil.
  577. (should
  578. (= 2
  579. (org-test-with-temp-text "* H\nA"
  580. (forward-line)
  581. (let ((org-adapt-indentation t)) (org-indent-line))
  582. (org-get-indentation))))
  583. (should
  584. (= 2
  585. (org-test-with-temp-text "* H\n\nA"
  586. (forward-line)
  587. (let ((org-adapt-indentation t)) (org-indent-line))
  588. (org-get-indentation))))
  589. (should
  590. (zerop
  591. (org-test-with-temp-text "* H\nA"
  592. (forward-line)
  593. (let ((org-adapt-indentation nil)) (org-indent-line))
  594. (org-get-indentation))))
  595. ;; Indenting preserves point position.
  596. (should
  597. (org-test-with-temp-text "* H\nAB"
  598. (forward-line)
  599. (forward-char)
  600. (let ((org-adapt-indentation t)) (org-indent-line))
  601. (looking-at "B")))
  602. ;; Do not change indentation at an item.
  603. (should
  604. (= 1
  605. (org-test-with-temp-text "* H\n - A"
  606. (forward-line)
  607. (let ((org-adapt-indentation t)) (org-indent-line))
  608. (org-get-indentation))))
  609. ;; On blank lines at the end of a list, indent like last element
  610. ;; within it if the line is still in the list. If the last element
  611. ;; is an item, indent like its contents. Otherwise, indent like the
  612. ;; whole list.
  613. (should
  614. (= 4
  615. (org-test-with-temp-text "* H\n- A\n - AA\n"
  616. (goto-char (point-max))
  617. (let ((org-adapt-indentation t)) (org-indent-line))
  618. (org-get-indentation))))
  619. (should
  620. (zerop
  621. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
  622. (goto-char (point-max))
  623. (let ((org-adapt-indentation t)) (org-indent-line))
  624. (org-get-indentation))))
  625. (should
  626. (= 4
  627. (org-test-with-temp-text "* H\n- A\n - \n"
  628. (goto-char (point-max))
  629. (let ((org-adapt-indentation t)) (org-indent-line))
  630. (org-get-indentation))))
  631. ;; Likewise, on a blank line at the end of a footnote definition,
  632. ;; indent at column 0 if line belongs to the definition. Otherwise,
  633. ;; indent like the definition itself.
  634. (should
  635. (zerop
  636. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  637. (goto-char (point-max))
  638. (let ((org-adapt-indentation t)) (org-indent-line))
  639. (org-get-indentation))))
  640. (should
  641. (zerop
  642. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  643. (goto-char (point-max))
  644. (let ((org-adapt-indentation t)) (org-indent-line))
  645. (org-get-indentation))))
  646. ;; After the end of the contents of a greater element, indent like
  647. ;; the beginning of the element.
  648. (should
  649. (= 1
  650. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  651. (forward-line 2)
  652. (org-indent-line)
  653. (org-get-indentation))))
  654. ;; On blank lines after a paragraph, indent like its last non-empty
  655. ;; line.
  656. (should
  657. (= 1
  658. (org-test-with-temp-text " Paragraph\n\n<point>"
  659. (org-indent-line)
  660. (org-get-indentation))))
  661. ;; At the first line of an element, indent like previous element's
  662. ;; first line, ignoring footnotes definitions and inline tasks, or
  663. ;; according to parent.
  664. (should
  665. (= 2
  666. (org-test-with-temp-text "A\n\n B\n\nC"
  667. (goto-char (point-max))
  668. (org-indent-line)
  669. (org-get-indentation))))
  670. (should
  671. (= 1
  672. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  673. (goto-char (point-max))
  674. (org-indent-line)
  675. (org-get-indentation))))
  676. (should
  677. (= 1
  678. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  679. (forward-line 1)
  680. (org-indent-line)
  681. (org-get-indentation))))
  682. ;; Within code part of a source block, use language major mode if
  683. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  684. ;; according to line above.
  685. (should
  686. (= 6
  687. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  688. (forward-line 2)
  689. (let ((org-src-tab-acts-natively t)
  690. (org-edit-src-content-indentation 0))
  691. (org-indent-line))
  692. (org-get-indentation))))
  693. (should
  694. (= 1
  695. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  696. (forward-line 2)
  697. (let ((org-src-tab-acts-natively nil)
  698. (org-edit-src-content-indentation 0))
  699. (org-indent-line))
  700. (org-get-indentation))))
  701. ;; Otherwise, indent like the first non-blank line above.
  702. (should
  703. (zerop
  704. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  705. (forward-line 3)
  706. (org-indent-line)
  707. (org-get-indentation))))
  708. ;; Align node properties according to `org-property-format'. Handle
  709. ;; nicely empty values.
  710. (should
  711. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  712. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  713. (let ((org-property-format "%-10s %s")) (org-indent-line))
  714. (buffer-string))))
  715. (should
  716. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  717. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  718. (let ((org-property-format "%-10s %s")) (org-indent-line))
  719. (buffer-string)))))
  720. (ert-deftest test-org/indent-region ()
  721. "Test `org-indent-region' specifications."
  722. ;; Indent paragraph.
  723. (should
  724. (equal "A\nB\nC"
  725. (org-test-with-temp-text " A\nB\n C"
  726. (org-indent-region (point-min) (point-max))
  727. (buffer-string))))
  728. ;; Indent greater elements along with their contents.
  729. (should
  730. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  731. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  732. (org-indent-region (point-min) (point-max))
  733. (buffer-string))))
  734. ;; Ignore contents of verse blocks and example blocks.
  735. (should
  736. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  737. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  738. (org-indent-region (point-min) (point-max))
  739. (buffer-string))))
  740. (should
  741. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  742. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  743. (org-indent-region (point-min) (point-max))
  744. (buffer-string))))
  745. ;; Indent according to mode if `org-src-tab-acts-natively' is
  746. ;; non-nil. Otherwise, do not indent code at all.
  747. (should
  748. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  749. (org-test-with-temp-text
  750. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  751. (let ((org-src-tab-acts-natively t)
  752. (org-edit-src-content-indentation 0))
  753. (org-indent-region (point-min) (point-max)))
  754. (buffer-string))))
  755. (should
  756. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  757. (org-test-with-temp-text
  758. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  759. (let ((org-src-tab-acts-natively nil)
  760. (org-edit-src-content-indentation 0))
  761. (org-indent-region (point-min) (point-max)))
  762. (buffer-string))))
  763. ;; Align node properties according to `org-property-format'. Handle
  764. ;; nicely empty values.
  765. (should
  766. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  767. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  768. (let ((org-property-format "%-10s %s")
  769. (org-adapt-indentation nil))
  770. (org-indent-region (point) (point-max)))
  771. (buffer-string))))
  772. (should
  773. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  774. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  775. (let ((org-property-format "%-10s %s")
  776. (org-adapt-indentation nil))
  777. (org-indent-region (point) (point-max)))
  778. (buffer-string))))
  779. ;; Indent plain lists.
  780. (should
  781. (equal "- A\n B\n - C\n\n D"
  782. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  783. (org-indent-region (point-min) (point-max))
  784. (buffer-string))))
  785. (should
  786. (equal "- A\n\n- B"
  787. (org-test-with-temp-text " - A\n\n - B"
  788. (org-indent-region (point-min) (point-max))
  789. (buffer-string))))
  790. ;; Indent footnote definitions.
  791. (should
  792. (equal "[fn:1] Definition\n\nDefinition"
  793. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  794. (org-indent-region (point-min) (point-max))
  795. (buffer-string))))
  796. ;; Special case: Start indenting on a blank line.
  797. (should
  798. (equal "\nParagraph"
  799. (org-test-with-temp-text "\n Paragraph"
  800. (org-indent-region (point-min) (point-max))
  801. (buffer-string)))))
  802. ;;; Editing
  803. (ert-deftest test-org/delete-indentation ()
  804. "Test `org-delete-indentation' specifications."
  805. ;; Regular test.
  806. (should (equal "foo bar"
  807. (org-test-with-temp-text
  808. "foo \n bar<point>"
  809. (org-delete-indentation)
  810. (buffer-string))))
  811. ;; With optional argument.
  812. (should (equal "foo bar"
  813. (org-test-with-temp-text
  814. "foo<point> \n bar"
  815. (org-delete-indentation t)
  816. (buffer-string))))
  817. ;; At headline text should be appended to the headline text.
  818. (should
  819. (equal"* foo bar :tag:"
  820. (let (org-auto-align-tags)
  821. (org-test-with-temp-text
  822. "* foo :tag:\n bar<point>"
  823. (org-delete-indentation)
  824. (buffer-string)))))
  825. (should
  826. (equal "* foo bar :tag:"
  827. (let (org-auto-align-tags)
  828. (org-test-with-temp-text
  829. "* foo <point>:tag:\n bar"
  830. (org-delete-indentation t)
  831. (buffer-string))))))
  832. (ert-deftest test-org/return ()
  833. "Test `org-return' specifications."
  834. ;; Regular test.
  835. (should
  836. (equal "Para\ngraph"
  837. (org-test-with-temp-text "Para<point>graph"
  838. (org-return)
  839. (buffer-string))))
  840. ;; With optional argument, indent line.
  841. (should
  842. (equal " Para\n graph"
  843. (org-test-with-temp-text " Para<point>graph"
  844. (org-return t)
  845. (buffer-string))))
  846. ;; On a table, call `org-table-next-row'.
  847. (should
  848. (org-test-with-temp-text "| <point>a |\n| b |"
  849. (org-return)
  850. (org-looking-at-p "b")))
  851. ;; Open link or timestamp under point when `org-return-follows-link'
  852. ;; is non-nil.
  853. (should
  854. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  855. (let ((org-return-follows-link t)
  856. (org-link-search-must-match-exact-headline nil)) (org-return))
  857. (org-looking-at-p "<<target>>")))
  858. (should-not
  859. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  860. (let ((org-return-follows-link nil)) (org-return))
  861. (org-looking-at-p "<<target>>")))
  862. ;; However, do not open link when point is in a table.
  863. (should
  864. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  865. (let ((org-return-follows-link t)) (org-return))
  866. (org-looking-at-p "between")))
  867. ;; Special case: in a list, when indenting, do not break structure.
  868. (should
  869. (equal "- A\n B"
  870. (org-test-with-temp-text "- A <point>B"
  871. (org-return t)
  872. (buffer-string))))
  873. (should
  874. (equal "- A\n\n- B"
  875. (org-test-with-temp-text "- A\n<point>- B"
  876. (org-return t)
  877. (buffer-string))))
  878. ;; On tags part of a headline, add a newline below it instead of
  879. ;; breaking it.
  880. (should
  881. (equal "* H :tag:\n"
  882. (org-test-with-temp-text "* H :<point>tag:"
  883. (org-return)
  884. (buffer-string))))
  885. ;; Before headline text, add a newline below it instead of breaking
  886. ;; it.
  887. (should
  888. (equal "* TODO H :tag:\n"
  889. (org-test-with-temp-text "* <point>TODO H :tag:"
  890. (org-return)
  891. (buffer-string))))
  892. (should
  893. (equal "* TODO [#B] H :tag:\n"
  894. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  895. (org-return)
  896. (buffer-string))))
  897. ;; At headline text, break headline text but preserve tags.
  898. (should
  899. (equal "* TODO [#B] foo :tag:\nbar"
  900. (let (org-auto-align-tags)
  901. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  902. (org-return)
  903. (buffer-string)))))
  904. ;; At bol of headline insert newline.
  905. (should
  906. (equal "\n* h"
  907. (org-test-with-temp-text "<point>* h"
  908. (org-return)
  909. (buffer-string)))))
  910. (ert-deftest test-org/meta-return ()
  911. "Test M-RET (`org-meta-return') specifications."
  912. ;; In a table field insert a row above.
  913. (should
  914. (org-test-with-temp-text "| a |"
  915. (forward-char)
  916. (org-meta-return)
  917. (forward-line -1)
  918. (looking-at "| |$")))
  919. ;; In a paragraph change current line into a header.
  920. (should
  921. (org-test-with-temp-text "a"
  922. (org-meta-return)
  923. (beginning-of-line)
  924. (looking-at "\* a$")))
  925. ;; In an item insert an item, in this case above.
  926. (should
  927. (org-test-with-temp-text "- a"
  928. (org-meta-return)
  929. (beginning-of-line)
  930. (looking-at "- $")))
  931. ;; In a drawer and item insert an item, in this case above.
  932. (should
  933. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  934. (forward-line)
  935. (org-meta-return)
  936. (beginning-of-line)
  937. (looking-at "- $"))))
  938. (ert-deftest test-org/insert-heading ()
  939. "Test `org-insert-heading' specifications."
  940. ;; In an empty buffer, insert a new headline.
  941. (should
  942. (equal "* "
  943. (org-test-with-temp-text ""
  944. (org-insert-heading)
  945. (buffer-string))))
  946. ;; At the beginning of a line, turn it into a headline
  947. (should
  948. (equal "* P"
  949. (org-test-with-temp-text "<point>P"
  950. (org-insert-heading)
  951. (buffer-string))))
  952. ;; In the middle of a line, split the line if allowed, otherwise,
  953. ;; insert the headline at its end.
  954. (should
  955. (equal "Para\n* graph"
  956. (org-test-with-temp-text "Para<point>graph"
  957. (let ((org-M-RET-may-split-line '((default . t))))
  958. (org-insert-heading))
  959. (buffer-string))))
  960. (should
  961. (equal "Paragraph\n* "
  962. (org-test-with-temp-text "Para<point>graph"
  963. (let ((org-M-RET-may-split-line '((default . nil))))
  964. (org-insert-heading))
  965. (buffer-string))))
  966. ;; When on a list, insert an item instead, unless called with an
  967. ;; universal argument or if list is invisible. In this case, create
  968. ;; a new headline after contents.
  969. (should
  970. (equal "* H\n- item\n- "
  971. (org-test-with-temp-text "* H\n- item<point>"
  972. (let ((org-insert-heading-respect-content nil))
  973. (org-insert-heading))
  974. (buffer-string))))
  975. (should
  976. (equal "* H\n- item\n- item 2\n* "
  977. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  978. (let ((org-insert-heading-respect-content nil))
  979. (org-insert-heading '(4)))
  980. (buffer-string))))
  981. (should
  982. (equal "* H\n- item\n* "
  983. (org-test-with-temp-text "* H\n- item"
  984. (org-cycle)
  985. (goto-char (point-max))
  986. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  987. (buffer-string))))
  988. ;; When called with two universal arguments, insert a new headline
  989. ;; at the end of the grandparent subtree.
  990. (should
  991. (equal "* H1\n** H3\n- item\n** H2\n** "
  992. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  993. (let ((org-insert-heading-respect-content nil))
  994. (org-insert-heading '(16)))
  995. (buffer-string))))
  996. ;; When optional TOP-LEVEL argument is non-nil, always insert
  997. ;; a level 1 heading.
  998. (should
  999. (equal "* H1\n** H2\n* "
  1000. (org-test-with-temp-text "* H1\n** H2<point>"
  1001. (org-insert-heading nil nil t)
  1002. (buffer-string))))
  1003. (should
  1004. (equal "* H1\n- item\n* "
  1005. (org-test-with-temp-text "* H1\n- item<point>"
  1006. (org-insert-heading nil nil t)
  1007. (buffer-string))))
  1008. ;; Corner case: correctly insert a headline after an empty one.
  1009. (should
  1010. (equal "* \n* "
  1011. (org-test-with-temp-text "* <point>"
  1012. (org-insert-heading)
  1013. (buffer-string)))))
  1014. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1015. "Test `org-insert-todo-heading-respect-content' specifications."
  1016. ;; Create a TODO heading.
  1017. (should
  1018. (org-test-with-temp-text "* H1\n Body"
  1019. (org-insert-todo-heading-respect-content)
  1020. (nth 2 (org-heading-components))))
  1021. ;; Add headline at the end of the first subtree
  1022. (should
  1023. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  1024. (search-forward "H1Body")
  1025. (org-insert-todo-heading-respect-content)
  1026. (and (eobp) (org-at-heading-p))))
  1027. ;; In a list, do not create a new item.
  1028. (should
  1029. (org-test-with-temp-text "* H\n- an item\n- another one"
  1030. (search-forward "an ")
  1031. (org-insert-todo-heading-respect-content)
  1032. (and (eobp) (org-at-heading-p)))))
  1033. (ert-deftest test-org/clone-with-time-shift ()
  1034. "Test `org-clone-subtree-with-time-shift'."
  1035. ;; Clone non-repeating once.
  1036. (should
  1037. (equal "\
  1038. * H1\n<2015-06-21>
  1039. * H1\n<2015-06-23>
  1040. "
  1041. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1042. (org-clone-subtree-with-time-shift 1 "+2d")
  1043. (replace-regexp-in-string
  1044. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1045. nil nil 1))))
  1046. ;; Clone repeating once.
  1047. (should
  1048. (equal "\
  1049. * H1\n<2015-06-21>
  1050. * H1\n<2015-06-23>
  1051. * H1\n<2015-06-25 +1w>
  1052. "
  1053. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1054. (org-clone-subtree-with-time-shift 1 "+2d")
  1055. (replace-regexp-in-string
  1056. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1057. nil nil 1))))
  1058. ;; Clone non-repeating zero times.
  1059. (should
  1060. (equal "\
  1061. * H1\n<2015-06-21>
  1062. "
  1063. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1064. (org-clone-subtree-with-time-shift 0 "+2d")
  1065. (replace-regexp-in-string
  1066. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1067. nil nil 1))))
  1068. ;; Clone repeating "zero" times.
  1069. (should
  1070. (equal "\
  1071. * H1\n<2015-06-21>
  1072. * H1\n<2015-06-23 +1w>
  1073. "
  1074. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1075. (org-clone-subtree-with-time-shift 0 "+2d")
  1076. (replace-regexp-in-string
  1077. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1078. nil nil 1)))))
  1079. ;;; Fixed-Width Areas
  1080. (ert-deftest test-org/toggle-fixed-width ()
  1081. "Test `org-toggle-fixed-width' specifications."
  1082. ;; No region: Toggle on fixed-width marker in paragraphs.
  1083. (should
  1084. (equal ": A"
  1085. (org-test-with-temp-text "A"
  1086. (org-toggle-fixed-width)
  1087. (buffer-string))))
  1088. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1089. (should
  1090. (equal "A"
  1091. (org-test-with-temp-text ": A"
  1092. (org-toggle-fixed-width)
  1093. (buffer-string))))
  1094. ;; No region: Toggle on marker in blank lines after elements or just
  1095. ;; after a headline.
  1096. (should
  1097. (equal "* H\n: "
  1098. (org-test-with-temp-text "* H\n"
  1099. (forward-line)
  1100. (org-toggle-fixed-width)
  1101. (buffer-string))))
  1102. (should
  1103. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1104. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1105. (goto-char (point-max))
  1106. (org-toggle-fixed-width)
  1107. (buffer-string))))
  1108. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1109. ;; headlines, clocks)
  1110. (should
  1111. (equal ": * Headline"
  1112. (org-test-with-temp-text "* Headline"
  1113. (org-toggle-fixed-width)
  1114. (buffer-string))))
  1115. (should
  1116. (equal ": #+KEYWORD: value"
  1117. (org-test-with-temp-text "#+KEYWORD: value"
  1118. (org-toggle-fixed-width)
  1119. (buffer-string))))
  1120. ;; No region: error in other situations.
  1121. (should-error
  1122. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1123. (forward-line)
  1124. (org-toggle-fixed-width)
  1125. (buffer-string)))
  1126. ;; No region: Indentation is preserved.
  1127. (should
  1128. (equal "- A\n : B"
  1129. (org-test-with-temp-text "- A\n B"
  1130. (forward-line)
  1131. (org-toggle-fixed-width)
  1132. (buffer-string))))
  1133. ;; Region: If it contains only fixed-width elements and blank lines,
  1134. ;; toggle off fixed-width markup.
  1135. (should
  1136. (equal "A\n\nB"
  1137. (org-test-with-temp-text ": A\n\n: B"
  1138. (transient-mark-mode 1)
  1139. (push-mark (point) t t)
  1140. (goto-char (point-max))
  1141. (org-toggle-fixed-width)
  1142. (buffer-string))))
  1143. ;; Region: If it contains anything else, toggle on fixed-width but
  1144. ;; not on fixed-width areas.
  1145. (should
  1146. (equal ": A\n: \n: B\n: \n: C"
  1147. (org-test-with-temp-text "A\n\n: B\n\nC"
  1148. (transient-mark-mode 1)
  1149. (push-mark (point) t t)
  1150. (goto-char (point-max))
  1151. (org-toggle-fixed-width)
  1152. (buffer-string))))
  1153. ;; Region: Ignore blank lines at its end, unless it contains only
  1154. ;; such lines.
  1155. (should
  1156. (equal ": A\n\n"
  1157. (org-test-with-temp-text "A\n\n"
  1158. (transient-mark-mode 1)
  1159. (push-mark (point) t t)
  1160. (goto-char (point-max))
  1161. (org-toggle-fixed-width)
  1162. (buffer-string))))
  1163. (should
  1164. (equal ": \n: \n"
  1165. (org-test-with-temp-text "\n\n"
  1166. (transient-mark-mode 1)
  1167. (push-mark (point) t t)
  1168. (goto-char (point-max))
  1169. (org-toggle-fixed-width)
  1170. (buffer-string)))))
  1171. ;;; Headline
  1172. (ert-deftest test-org/in-commented-heading-p ()
  1173. "Test `org-in-commented-heading-p' specifications."
  1174. ;; Commented headline.
  1175. (should
  1176. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1177. (goto-char (point-max))
  1178. (org-in-commented-heading-p)))
  1179. ;; Commented ancestor.
  1180. (should
  1181. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1182. (goto-char (point-max))
  1183. (org-in-commented-heading-p)))
  1184. ;; Comment keyword is case-sensitive.
  1185. (should-not
  1186. (org-test-with-temp-text "* Comment Headline\nBody"
  1187. (goto-char (point-max))
  1188. (org-in-commented-heading-p)))
  1189. ;; Keyword is standalone.
  1190. (should-not
  1191. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1192. (goto-char (point-max))
  1193. (org-in-commented-heading-p)))
  1194. ;; Optional argument.
  1195. (should-not
  1196. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1197. (goto-char (point-max))
  1198. (org-in-commented-heading-p t))))
  1199. (ert-deftest test-org/entry-blocked-p ()
  1200. ;; Check other dependencies.
  1201. (should
  1202. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1203. (let ((org-enforce-todo-dependencies t)
  1204. (org-blocker-hook
  1205. '(org-block-todo-from-children-or-siblings-or-parent)))
  1206. (org-entry-blocked-p))))
  1207. (should-not
  1208. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1209. (let ((org-enforce-todo-dependencies t)
  1210. (org-blocker-hook
  1211. '(org-block-todo-from-children-or-siblings-or-parent)))
  1212. (org-entry-blocked-p))))
  1213. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1214. ;; blocked.
  1215. (should-not
  1216. (org-test-with-temp-text "* Blocked\n** TODO one"
  1217. (let ((org-enforce-todo-dependencies t)
  1218. (org-blocker-hook
  1219. '(org-block-todo-from-children-or-siblings-or-parent)))
  1220. (org-entry-blocked-p))))
  1221. (should-not
  1222. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1223. (let ((org-enforce-todo-dependencies t)
  1224. (org-blocker-hook
  1225. '(org-block-todo-from-children-or-siblings-or-parent)))
  1226. (org-entry-blocked-p))))
  1227. ;; Follow :ORDERED: specifications.
  1228. (should
  1229. (org-test-with-temp-text
  1230. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1231. (let ((org-enforce-todo-dependencies t)
  1232. (org-blocker-hook
  1233. '(org-block-todo-from-children-or-siblings-or-parent)))
  1234. (org-entry-blocked-p))))
  1235. (should-not
  1236. (org-test-with-temp-text
  1237. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1238. (let ((org-enforce-todo-dependencies t)
  1239. (org-blocker-hook
  1240. '(org-block-todo-from-children-or-siblings-or-parent)))
  1241. (org-entry-blocked-p)))))
  1242. ;;; Keywords
  1243. (ert-deftest test-org/set-regexps-and-options ()
  1244. "Test `org-set-regexps-and-options' specifications."
  1245. ;; TAGS keyword.
  1246. (should
  1247. (equal '(("A" . ?a) ("B") ("C"))
  1248. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1249. (org-mode-restart)
  1250. org-tag-alist)))
  1251. (should
  1252. (equal '(("A") (:newline) ("B"))
  1253. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1254. (org-mode-restart)
  1255. org-tag-alist)))
  1256. (should
  1257. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1258. (org-test-with-temp-text "#+TAGS: { A B } C"
  1259. (org-mode-restart)
  1260. org-tag-alist)))
  1261. (should
  1262. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1263. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1264. (org-mode-restart)
  1265. org-tag-alist)))
  1266. (should
  1267. (equal '(("A" "B" "C"))
  1268. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1269. (org-mode-restart)
  1270. org-tag-groups-alist)))
  1271. (should
  1272. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1273. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1274. (org-mode-restart)
  1275. org-tag-alist)))
  1276. (should
  1277. (equal '(("A" "B" "C"))
  1278. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1279. (org-mode-restart)
  1280. org-tag-groups-alist)))
  1281. ;; FILETAGS keyword.
  1282. (should
  1283. (equal '("A" "B" "C")
  1284. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1285. (org-mode-restart)
  1286. org-file-tags)))
  1287. ;; PROPERTY keyword. Property names are case-insensitive.
  1288. (should
  1289. (equal "foo=1"
  1290. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1291. (org-mode-restart)
  1292. (cdr (assoc "var" org-file-properties)))))
  1293. (should
  1294. (equal
  1295. "foo=1 bar=2"
  1296. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1297. (org-mode-restart)
  1298. (cdr (assoc "var" org-file-properties)))))
  1299. (should
  1300. (equal
  1301. "foo=1 bar=2"
  1302. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1303. (org-mode-restart)
  1304. (cdr (assoc "var" org-file-properties)))))
  1305. ;; ARCHIVE keyword.
  1306. (should
  1307. (equal "%s_done::"
  1308. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1309. (org-mode-restart)
  1310. org-archive-location)))
  1311. ;; CATEGORY keyword.
  1312. (should
  1313. (eq 'test
  1314. (org-test-with-temp-text "#+CATEGORY: test"
  1315. (org-mode-restart)
  1316. org-category)))
  1317. (should
  1318. (equal "test"
  1319. (org-test-with-temp-text "#+CATEGORY: test"
  1320. (org-mode-restart)
  1321. (cdr (assoc "CATEGORY" org-file-properties)))))
  1322. ;; COLUMNS keyword.
  1323. (should
  1324. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1325. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1326. (org-mode-restart)
  1327. org-columns-default-format)))
  1328. ;; CONSTANTS keyword. Constants names are case sensitive.
  1329. (should
  1330. (equal '("299792458." "3.14")
  1331. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1332. (org-mode-restart)
  1333. (mapcar
  1334. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1335. '("c" "pi")))))
  1336. (should
  1337. (equal "3.14"
  1338. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1339. (org-mode-restart)
  1340. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1341. (should
  1342. (equal "22/7"
  1343. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1344. (org-mode-restart)
  1345. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1346. ;; LINK keyword.
  1347. (should
  1348. (equal
  1349. '("url1" "url2")
  1350. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1351. (org-mode-restart)
  1352. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1353. '("a" "b")))))
  1354. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1355. (should
  1356. (equal
  1357. '(?X ?Z ?Y)
  1358. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1359. (org-mode-restart)
  1360. (list org-highest-priority org-lowest-priority org-default-priority))))
  1361. (should
  1362. (equal
  1363. '(?A ?C ?B)
  1364. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1365. (org-mode-restart)
  1366. (list org-highest-priority org-lowest-priority org-default-priority))))
  1367. ;; STARTUP keyword.
  1368. (should
  1369. (equal '(t t)
  1370. (org-test-with-temp-text "#+STARTUP: fold odd"
  1371. (org-mode-restart)
  1372. (list org-startup-folded org-odd-levels-only))))
  1373. ;; TODO keywords.
  1374. (should
  1375. (equal '(("A" "B") ("C"))
  1376. (org-test-with-temp-text "#+TODO: A B | C"
  1377. (org-mode-restart)
  1378. (list org-not-done-keywords org-done-keywords))))
  1379. (should
  1380. (equal '(("A" "C") ("B" "D"))
  1381. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1382. (org-mode-restart)
  1383. (list org-not-done-keywords org-done-keywords))))
  1384. (should
  1385. (equal '(("A" "B") ("C"))
  1386. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1387. (org-mode-restart)
  1388. (list org-not-done-keywords org-done-keywords))))
  1389. (should
  1390. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1391. (org-test-with-temp-text "#+TODO: A(a)"
  1392. (org-mode-restart)
  1393. org-todo-key-alist)))
  1394. (should
  1395. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1396. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1397. (org-mode-restart)
  1398. org-todo-log-states)))
  1399. ;; Enter SETUPFILE keyword.
  1400. (should
  1401. (equal "1"
  1402. (org-test-with-temp-text
  1403. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1404. (org-mode-restart)
  1405. (cdr (assoc "a" org-file-properties))))))
  1406. ;;; Links
  1407. ;;;; Coderefs
  1408. (ert-deftest test-org/coderef ()
  1409. "Test coderef links specifications."
  1410. (should
  1411. (org-test-with-temp-text "
  1412. #+BEGIN_SRC emacs-lisp
  1413. \(+ 1 1) (ref:sc)
  1414. #+END_SRC
  1415. \[[(sc)]]<point>"
  1416. (org-open-at-point)
  1417. (looking-at "(ref:sc)")))
  1418. ;; Find coderef even with alternate label format.
  1419. (should
  1420. (org-test-with-temp-text "
  1421. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  1422. \(+ 1 1) {ref:sc}
  1423. #+END_SRC
  1424. \[[(sc)]]<point>"
  1425. (org-open-at-point)
  1426. (looking-at "{ref:sc}"))))
  1427. ;;;; Custom ID
  1428. (ert-deftest test-org/custom-id ()
  1429. "Test custom ID links specifications."
  1430. (should
  1431. (org-test-with-temp-text
  1432. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
  1433. (org-open-at-point)
  1434. (org-looking-at-p "\\* H1")))
  1435. ;; Throw an error on false positives.
  1436. (should-error
  1437. (org-test-with-temp-text
  1438. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
  1439. (org-open-at-point)
  1440. (org-looking-at-p "\\* H1"))))
  1441. ;;;; Fuzzy Links
  1442. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1443. ;; a named element (#+name: text) and to headlines (* Text).
  1444. (ert-deftest test-org/fuzzy-links ()
  1445. "Test fuzzy links specifications."
  1446. ;; Fuzzy link goes in priority to a matching target.
  1447. (should
  1448. (org-test-with-temp-text
  1449. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  1450. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1451. (looking-at "<<Test>>")))
  1452. ;; Then fuzzy link points to an element with a given name.
  1453. (should
  1454. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  1455. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1456. (looking-at "#\\+NAME: Test")))
  1457. ;; A target still lead to a matching headline otherwise.
  1458. (should
  1459. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  1460. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1461. (looking-at "\\* Head2")))
  1462. ;; With a leading star in link, enforce heading match.
  1463. (should
  1464. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  1465. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1466. (looking-at "\\* Test")))
  1467. ;; With a leading star in link, enforce exact heading match, even
  1468. ;; with `org-link-search-must-match-exact-headline' set to nil.
  1469. (should-error
  1470. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  1471. (let ((org-link-search-must-match-exact-headline nil))
  1472. (org-open-at-point))))
  1473. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  1474. (should
  1475. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  1476. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  1477. (looking-at "\\* Test")))
  1478. (should
  1479. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  1480. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  1481. (looking-at "\\* Test")))
  1482. ;; Heading match should not care about spaces, cookies, TODO
  1483. ;; keywords, priorities, and tags.
  1484. (should
  1485. (let ((first-line
  1486. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1487. (org-test-with-temp-text
  1488. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  1489. (let ((org-link-search-must-match-exact-headline nil)
  1490. (org-todo-regexp "TODO"))
  1491. (org-open-at-point))
  1492. (looking-at (regexp-quote first-line)))))
  1493. ;; Heading match should still be exact.
  1494. (should-error
  1495. (let ((first-line
  1496. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1497. (org-test-with-temp-text
  1498. (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
  1499. (let ((org-link-search-must-match-exact-headline nil)
  1500. (org-todo-regexp "TODO"))
  1501. (org-open-at-point)))))
  1502. ;; Heading match ignores COMMENT keyword.
  1503. (should
  1504. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  1505. (org-open-at-point)
  1506. (looking-at "\\* COMMENT Test")))
  1507. ;; Correctly un-hexify fuzzy links.
  1508. (should
  1509. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  1510. (goto-char (point-max))
  1511. (org-open-at-point)
  1512. (bobp))))
  1513. ;;;; Link Escaping
  1514. (ert-deftest test-org/org-link-escape-ascii-character ()
  1515. "Escape an ascii character."
  1516. (should
  1517. (string=
  1518. "%5B"
  1519. (org-link-escape "["))))
  1520. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  1521. "Escape an ascii control character."
  1522. (should
  1523. (string=
  1524. "%09"
  1525. (org-link-escape "\t"))))
  1526. (ert-deftest test-org/org-link-escape-multibyte-character ()
  1527. "Escape an unicode multibyte character."
  1528. (should
  1529. (string=
  1530. "%E2%82%AC"
  1531. (org-link-escape "€"))))
  1532. (ert-deftest test-org/org-link-escape-custom-table ()
  1533. "Escape string with custom character table."
  1534. (should
  1535. (string=
  1536. "Foo%3A%42ar%0A"
  1537. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  1538. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  1539. "Escape string with custom table merged with default table."
  1540. (should
  1541. (string=
  1542. "%5BF%6F%6F%3A%42ar%0A%5D"
  1543. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  1544. (ert-deftest test-org/org-link-unescape-ascii-character ()
  1545. "Unescape an ascii character."
  1546. (should
  1547. (string=
  1548. "["
  1549. (org-link-unescape "%5B"))))
  1550. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  1551. "Unescpae an ascii control character."
  1552. (should
  1553. (string=
  1554. "\n"
  1555. (org-link-unescape "%0A"))))
  1556. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  1557. "Unescape unicode multibyte character."
  1558. (should
  1559. (string=
  1560. "€"
  1561. (org-link-unescape "%E2%82%AC"))))
  1562. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  1563. "Unescape old style percent escaped character."
  1564. (should
  1565. (string=
  1566. "àâçèéêîôùû"
  1567. (decode-coding-string
  1568. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  1569. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  1570. "Escape and unescape a URL that includes an escaped char.
  1571. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  1572. (should
  1573. (string=
  1574. "http://some.host.com/form?&id=blah%2Bblah25"
  1575. (org-link-unescape
  1576. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  1577. (ert-deftest test-org/org-link-escape-chars-browser ()
  1578. "Test of the constant `org-link-escape-chars-browser'.
  1579. See there why this test is a candidate to be removed once Org
  1580. drops support for Emacs 24.1 and 24.2."
  1581. (should
  1582. (string=
  1583. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1584. "%22Release%208.2%22&idxname=emacs-orgmode")
  1585. (org-link-escape-browser ; Do not replace with `url-encode-url',
  1586. ; see docstring above.
  1587. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1588. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  1589. ;;;; Open at point
  1590. (ert-deftest test-org/open-at-point-in-keyword ()
  1591. "Does `org-open-at-point' open link in a keyword line?"
  1592. (should
  1593. (org-test-with-temp-text
  1594. "#+KEYWORD: <point>[[info:emacs#Top]]"
  1595. (org-open-at-point) t)))
  1596. (ert-deftest test-org/open-at-point-in-property ()
  1597. "Does `org-open-at-point' open link in property drawer?"
  1598. (should
  1599. (org-test-with-temp-text
  1600. "* Headline
  1601. :PROPERTIES:
  1602. :URL: <point>[[info:emacs#Top]]
  1603. :END:"
  1604. (org-open-at-point) t)))
  1605. (ert-deftest test-org/open-at-point-in-comment ()
  1606. "Does `org-open-at-point' open link in a commented line?"
  1607. (should
  1608. (org-test-with-temp-text
  1609. "# <point>[[info:emacs#Top]]"
  1610. (org-open-at-point) t)))
  1611. (ert-deftest test-org/open-at-point/info ()
  1612. "Test `org-open-at-point' on info links."
  1613. (should
  1614. (org-test-with-temp-text
  1615. "<point>[[info:emacs#Top]]"
  1616. (org-open-at-point)
  1617. (and (switch-to-buffer "*info*")
  1618. (prog1
  1619. (looking-at "\nThe Emacs Editor")
  1620. (kill-buffer))))))
  1621. (ert-deftest test-org/open-at-point/inline-image ()
  1622. "Test `org-open-at-point' on nested links."
  1623. (should
  1624. (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
  1625. (org-open-at-point)
  1626. (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
  1627. (kill-buffer "*info*")))))
  1628. (ert-deftest test-org/open-at-point/radio-target ()
  1629. "Test `org-open-at-point' on radio targets."
  1630. (should
  1631. (org-test-with-temp-text "<<<target>>> <point>target"
  1632. (org-update-radio-target-regexp)
  1633. (org-open-at-point)
  1634. (eq (org-element-type (org-element-context)) 'radio-target))))
  1635. ;;; Node Properties
  1636. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1637. "Ensure properties accumulate in subtree drawers."
  1638. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1639. (org-babel-next-src-block)
  1640. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1641. (ert-deftest test-org/custom-properties ()
  1642. "Test custom properties specifications."
  1643. ;; Standard test.
  1644. (should
  1645. (let ((org-custom-properties '("FOO")))
  1646. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1647. (org-toggle-custom-properties-visibility)
  1648. (org-invisible-p2))))
  1649. ;; Properties are case-insensitive.
  1650. (should
  1651. (let ((org-custom-properties '("FOO")))
  1652. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  1653. (org-toggle-custom-properties-visibility)
  1654. (org-invisible-p2))))
  1655. (should
  1656. (let ((org-custom-properties '("foo")))
  1657. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1658. (org-toggle-custom-properties-visibility)
  1659. (org-invisible-p2))))
  1660. ;; Multiple custom properties in the same drawer.
  1661. (should
  1662. (let ((org-custom-properties '("FOO" "BAR")))
  1663. (org-test-with-temp-text
  1664. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  1665. (org-toggle-custom-properties-visibility)
  1666. (and (org-invisible-p2)
  1667. (not (progn (forward-line) (org-invisible-p2)))
  1668. (progn (forward-line) (org-invisible-p2))))))
  1669. ;; Hide custom properties with an empty value.
  1670. (should
  1671. (let ((org-custom-properties '("FOO")))
  1672. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  1673. (org-toggle-custom-properties-visibility)
  1674. (org-invisible-p2))))
  1675. ;; Do not hide fake properties.
  1676. (should-not
  1677. (let ((org-custom-properties '("FOO")))
  1678. (org-test-with-temp-text ":FOO: val\n"
  1679. (org-toggle-custom-properties-visibility)
  1680. (org-invisible-p2))))
  1681. (should-not
  1682. (let ((org-custom-properties '("A")))
  1683. (org-test-with-temp-text
  1684. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  1685. (org-toggle-custom-properties-visibility)
  1686. (org-invisible-p2)))))
  1687. ;;; Mark Region
  1688. (ert-deftest test-org/mark-subtree ()
  1689. "Test `org-mark-subtree' specifications."
  1690. ;; Error when point is before first headline.
  1691. (should-error
  1692. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1693. (progn (transient-mark-mode 1)
  1694. (org-mark-subtree))))
  1695. ;; Without argument, mark current subtree.
  1696. (should
  1697. (equal
  1698. '(12 32)
  1699. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1700. (progn (transient-mark-mode 1)
  1701. (forward-line 2)
  1702. (org-mark-subtree)
  1703. (list (region-beginning) (region-end))))))
  1704. ;; With an argument, move ARG up.
  1705. (should
  1706. (equal
  1707. '(1 32)
  1708. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1709. (progn (transient-mark-mode 1)
  1710. (forward-line 2)
  1711. (org-mark-subtree 1)
  1712. (list (region-beginning) (region-end))))))
  1713. ;; Do not get fooled by inlinetasks.
  1714. (when (featurep 'org-inlinetask)
  1715. (should
  1716. (= 1
  1717. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1718. (progn (transient-mark-mode 1)
  1719. (forward-line 1)
  1720. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1721. (region-beginning)))))))
  1722. ;;; Navigation
  1723. (ert-deftest test-org/end-of-meta-data ()
  1724. "Test `org-end-of-meta-data' specifications."
  1725. ;; Skip planning line.
  1726. (should
  1727. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  1728. (org-end-of-meta-data)
  1729. (eobp)))
  1730. ;; Skip properties drawer.
  1731. (should
  1732. (org-test-with-temp-text
  1733. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  1734. (org-end-of-meta-data)
  1735. (eobp)))
  1736. ;; Skip both.
  1737. (should
  1738. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  1739. (org-end-of-meta-data)
  1740. (eobp)))
  1741. ;; Nothing to skip, go to first line.
  1742. (should
  1743. (org-test-with-temp-text "* Headline\nContents"
  1744. (org-end-of-meta-data)
  1745. (looking-at "Contents")))
  1746. ;; With option argument, skip empty lines, regular drawers and
  1747. ;; clocking lines.
  1748. (should
  1749. (org-test-with-temp-text "* Headline\n\nContents"
  1750. (org-end-of-meta-data t)
  1751. (looking-at "Contents")))
  1752. (should
  1753. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  1754. (org-end-of-meta-data t)
  1755. (looking-at "Contents")))
  1756. (should
  1757. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  1758. (org-end-of-meta-data t)
  1759. (looking-at "Contents")))
  1760. ;; Special case: do not skip incomplete drawers.
  1761. (should
  1762. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  1763. (org-end-of-meta-data t)
  1764. (looking-at ":LOGBOOK:")))
  1765. ;; Special case: Be careful about consecutive headlines.
  1766. (should-not
  1767. (org-test-with-temp-text "* H1\n*H2\nContents"
  1768. (org-end-of-meta-data t)
  1769. (looking-at "Contents"))))
  1770. (ert-deftest test-org/beginning-of-line ()
  1771. "Test `org-beginning-of-line' specifications."
  1772. ;; Standard test.
  1773. (should
  1774. (org-test-with-temp-text "Some text\nSome other text"
  1775. (progn (org-beginning-of-line) (bolp))))
  1776. ;; Standard test with `visual-line-mode'.
  1777. (should-not
  1778. (org-test-with-temp-text "A long line of text\nSome other text"
  1779. (progn (visual-line-mode)
  1780. (forward-char 2)
  1781. (dotimes (i 1000) (insert "very "))
  1782. (org-beginning-of-line)
  1783. (bolp))))
  1784. ;; At an headline with special movement.
  1785. (should
  1786. (org-test-with-temp-text "* TODO Headline"
  1787. (let ((org-special-ctrl-a/e t))
  1788. (org-end-of-line)
  1789. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1790. (progn (org-beginning-of-line) (bolp))
  1791. (progn (org-beginning-of-line) (looking-at "Headline"))))))
  1792. ;; Special case: Do not error when the buffer contains only a single
  1793. ;; asterisk.
  1794. (should
  1795. (org-test-with-temp-text "*<point>"
  1796. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))))
  1797. (should
  1798. (org-test-with-temp-text "*<point>"
  1799. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line)))))
  1800. (ert-deftest test-org/end-of-line ()
  1801. "Test `org-end-of-line' specifications."
  1802. ;; Standard test.
  1803. (should
  1804. (org-test-with-temp-text "Some text\nSome other text"
  1805. (progn (org-end-of-line) (eolp))))
  1806. ;; Standard test with `visual-line-mode'.
  1807. (should-not
  1808. (org-test-with-temp-text "A long line of text\nSome other text"
  1809. (progn (visual-line-mode)
  1810. (forward-char 2)
  1811. (dotimes (i 1000) (insert "very "))
  1812. (goto-char (point-min))
  1813. (org-end-of-line)
  1814. (eolp))))
  1815. ;; At an headline with special movement.
  1816. (should
  1817. (org-test-with-temp-text "* Headline1 :tag:\n"
  1818. (let ((org-special-ctrl-a/e t))
  1819. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1820. (progn (org-end-of-line) (eolp))
  1821. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1822. ;; At an headline without special movement.
  1823. (should
  1824. (org-test-with-temp-text "* Headline2 :tag:\n"
  1825. (let ((org-special-ctrl-a/e nil))
  1826. (and (progn (org-end-of-line) (eolp))
  1827. (progn (org-end-of-line) (eolp))))))
  1828. ;; At an headline, with reversed movement.
  1829. (should
  1830. (org-test-with-temp-text "* Headline3 :tag:\n"
  1831. (let ((org-special-ctrl-a/e 'reversed)
  1832. (this-command last-command))
  1833. (and (progn (org-end-of-line) (eolp))
  1834. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1835. ;; At a block without hidden contents.
  1836. (should
  1837. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1838. (progn (org-end-of-line) (eolp))))
  1839. ;; At a block with hidden contents.
  1840. (should-not
  1841. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1842. (let ((org-special-ctrl-a/e t))
  1843. (org-hide-block-toggle)
  1844. (org-end-of-line)
  1845. (eobp)))))
  1846. (ert-deftest test-org/forward-sentence ()
  1847. "Test `org-forward-sentence' specifications."
  1848. ;; At the end of a table cell, move to the end of the next one.
  1849. (should
  1850. (org-test-with-temp-text "| a<point> | b |"
  1851. (org-forward-sentence)
  1852. (looking-at " |$")))
  1853. ;; Elsewhere in a cell, move to its end.
  1854. (should
  1855. (org-test-with-temp-text "| a<point>c | b |"
  1856. (org-forward-sentence)
  1857. (looking-at " | b |$")))
  1858. ;; Otherwise, simply call `forward-sentence'.
  1859. (should
  1860. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  1861. (org-forward-sentence)
  1862. (looking-at " Sentence 2.")))
  1863. (should
  1864. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  1865. (org-forward-sentence)
  1866. (org-forward-sentence)
  1867. (eobp)))
  1868. ;; At the end of an element, jump to the next one, without stopping
  1869. ;; on blank lines in-between.
  1870. (should
  1871. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  1872. (org-forward-sentence)
  1873. (eobp))))
  1874. (ert-deftest test-org/backward-sentence ()
  1875. "Test `org-backward-sentence' specifications."
  1876. ;; At the beginning of a table cell, move to the beginning of the
  1877. ;; previous one.
  1878. (should
  1879. (org-test-with-temp-text "| a | <point>b |"
  1880. (org-backward-sentence)
  1881. (looking-at "a | b |$")))
  1882. ;; Elsewhere in a cell, move to its beginning.
  1883. (should
  1884. (org-test-with-temp-text "| a | b<point>c |"
  1885. (org-backward-sentence)
  1886. (looking-at "bc |$")))
  1887. ;; Otherwise, simply call `backward-sentence'.
  1888. (should
  1889. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  1890. (org-backward-sentence)
  1891. (looking-at "Sentence 2.")))
  1892. (should
  1893. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  1894. (org-backward-sentence)
  1895. (org-backward-sentence)
  1896. (bobp)))
  1897. ;; Make sure to hit the beginning of a sentence on the same line as
  1898. ;; an item.
  1899. (should
  1900. (org-test-with-temp-text "- Line 1\n line <point>2."
  1901. (org-backward-sentence)
  1902. (looking-at "Line 1"))))
  1903. (ert-deftest test-org/forward-paragraph ()
  1904. "Test `org-forward-paragraph' specifications."
  1905. ;; At end of buffer, return an error.
  1906. (should-error
  1907. (org-test-with-temp-text "Paragraph"
  1908. (goto-char (point-max))
  1909. (org-forward-paragraph)))
  1910. ;; Standard test.
  1911. (should
  1912. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1913. (org-forward-paragraph)
  1914. (looking-at "P2")))
  1915. ;; Ignore depth.
  1916. (should
  1917. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  1918. (org-forward-paragraph)
  1919. (looking-at "P1")))
  1920. ;; Do not enter elements with invisible contents.
  1921. (should
  1922. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  1923. (org-hide-block-toggle)
  1924. (org-forward-paragraph)
  1925. (looking-at "P3")))
  1926. ;; On an affiliated keyword, jump to the beginning of the element.
  1927. (should
  1928. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  1929. (org-forward-paragraph)
  1930. (looking-at "Para")))
  1931. ;; On an item or a footnote definition, move to the second element
  1932. ;; inside, if any.
  1933. (should
  1934. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  1935. (org-forward-paragraph)
  1936. (looking-at " Paragraph")))
  1937. (should
  1938. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  1939. (org-forward-paragraph)
  1940. (looking-at "Paragraph")))
  1941. ;; On an item, or a footnote definition, when the first line is
  1942. ;; empty, move to the first item.
  1943. (should
  1944. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  1945. (org-forward-paragraph)
  1946. (looking-at " Paragraph")))
  1947. (should
  1948. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  1949. (org-forward-paragraph)
  1950. (looking-at "Paragraph")))
  1951. ;; On a table (resp. a property drawer) do not move through table
  1952. ;; rows (resp. node properties).
  1953. (should
  1954. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  1955. (org-forward-paragraph)
  1956. (looking-at "Paragraph")))
  1957. (should
  1958. (org-test-with-temp-text
  1959. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  1960. (org-forward-paragraph)
  1961. (looking-at "Paragraph")))
  1962. ;; On a verse or source block, stop after blank lines.
  1963. (should
  1964. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  1965. (org-forward-paragraph)
  1966. (looking-at "L2")))
  1967. (should
  1968. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  1969. (org-forward-paragraph)
  1970. (looking-at "L2"))))
  1971. (ert-deftest test-org/backward-paragraph ()
  1972. "Test `org-backward-paragraph' specifications."
  1973. ;; Error at beginning of buffer.
  1974. (should-error
  1975. (org-test-with-temp-text "Paragraph"
  1976. (org-backward-paragraph)))
  1977. ;; Regular test.
  1978. (should
  1979. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1980. (goto-char (point-max))
  1981. (org-backward-paragraph)
  1982. (looking-at "P3")))
  1983. (should
  1984. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  1985. (goto-char (point-max))
  1986. (beginning-of-line)
  1987. (org-backward-paragraph)
  1988. (looking-at "P2")))
  1989. ;; Ignore depth.
  1990. (should
  1991. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  1992. (goto-char (point-max))
  1993. (beginning-of-line)
  1994. (org-backward-paragraph)
  1995. (looking-at "P2")))
  1996. ;; Ignore invisible elements.
  1997. (should
  1998. (org-test-with-temp-text "* H1\n P1\n* H2"
  1999. (org-cycle)
  2000. (goto-char (point-max))
  2001. (beginning-of-line)
  2002. (org-backward-paragraph)
  2003. (bobp)))
  2004. ;; On an affiliated keyword, jump to the first one.
  2005. (should
  2006. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  2007. (search-forward "c2")
  2008. (org-backward-paragraph)
  2009. (looking-at "#\\+name")))
  2010. ;; On the second element in an item or a footnote definition, jump
  2011. ;; to item or the definition.
  2012. (should
  2013. (org-test-with-temp-text "- line1\n\n line2"
  2014. (goto-char (point-max))
  2015. (beginning-of-line)
  2016. (org-backward-paragraph)
  2017. (looking-at "- line1")))
  2018. (should
  2019. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  2020. (goto-char (point-max))
  2021. (beginning-of-line)
  2022. (org-backward-paragraph)
  2023. (looking-at "\\[fn:1\\] line1")))
  2024. ;; On a table (resp. a property drawer), ignore table rows
  2025. ;; (resp. node properties).
  2026. (should
  2027. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  2028. (goto-char (point-max))
  2029. (beginning-of-line)
  2030. (org-backward-paragraph)
  2031. (bobp)))
  2032. (should
  2033. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  2034. (org-backward-paragraph)
  2035. (looking-at ":PROPERTIES:")))
  2036. ;; On a source or verse block, stop before blank lines.
  2037. (should
  2038. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  2039. (search-forward "L3")
  2040. (beginning-of-line)
  2041. (org-backward-paragraph)
  2042. (looking-at "L2")))
  2043. (should
  2044. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  2045. (search-forward "L3")
  2046. (beginning-of-line)
  2047. (org-backward-paragraph)
  2048. (looking-at "L2"))))
  2049. (ert-deftest test-org/forward-element ()
  2050. "Test `org-forward-element' specifications."
  2051. ;; 1. At EOB: should error.
  2052. (org-test-with-temp-text "Some text\n"
  2053. (goto-char (point-max))
  2054. (should-error (org-forward-element)))
  2055. ;; 2. Standard move: expected to ignore blank lines.
  2056. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  2057. (org-forward-element)
  2058. (should (looking-at (regexp-quote "Second paragraph."))))
  2059. ;; 3. Headline tests.
  2060. (org-test-with-temp-text "
  2061. * Head 1
  2062. ** Head 1.1
  2063. *** Head 1.1.1
  2064. ** Head 1.2"
  2065. ;; 3.1. At an headline beginning: move to next headline at the
  2066. ;; same level.
  2067. (goto-line 3)
  2068. (org-forward-element)
  2069. (should (looking-at (regexp-quote "** Head 1.2")))
  2070. ;; 3.2. At an headline beginning: move to parent headline if no
  2071. ;; headline at the same level.
  2072. (goto-line 3)
  2073. (org-forward-element)
  2074. (should (looking-at (regexp-quote "** Head 1.2"))))
  2075. ;; 4. Greater element tests.
  2076. (org-test-with-temp-text
  2077. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  2078. ;; 4.1. At a greater element: expected to skip contents.
  2079. (org-forward-element)
  2080. (should (looking-at (regexp-quote "Outside.")))
  2081. ;; 4.2. At the end of greater element contents: expected to skip
  2082. ;; to the end of the greater element.
  2083. (goto-line 2)
  2084. (org-forward-element)
  2085. (should (looking-at (regexp-quote "Outside."))))
  2086. ;; 5. List tests.
  2087. (org-test-with-temp-text "
  2088. - item1
  2089. - sub1
  2090. - sub2
  2091. - sub3
  2092. Inner paragraph.
  2093. - item2
  2094. Outside."
  2095. ;; 5.1. At list top point: expected to move to the element after
  2096. ;; the list.
  2097. (goto-line 2)
  2098. (org-forward-element)
  2099. (should (looking-at (regexp-quote "Outside.")))
  2100. ;; 5.2. Special case: at the first line of a sub-list, but not at
  2101. ;; beginning of line, move to next item.
  2102. (goto-line 2)
  2103. (forward-char)
  2104. (org-forward-element)
  2105. (should (looking-at "- item2"))
  2106. (goto-line 4)
  2107. (forward-char)
  2108. (org-forward-element)
  2109. (should (looking-at " - sub2"))
  2110. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  2111. (goto-line 4)
  2112. (org-forward-element)
  2113. (should (looking-at (regexp-quote " Inner paragraph.")))
  2114. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  2115. (goto-line 8)
  2116. (org-forward-element)
  2117. (should (looking-at (regexp-quote " Inner paragraph.")))
  2118. ;; 5.5. At an item: expected to move to next item, if any.
  2119. (goto-line 6)
  2120. (org-forward-element)
  2121. (should (looking-at " - sub3"))))
  2122. (ert-deftest test-org/backward-element ()
  2123. "Test `org-backward-element' specifications."
  2124. ;; 1. Should error at BOB.
  2125. (org-test-with-temp-text " \nParagraph."
  2126. (should-error (org-backward-element)))
  2127. ;; 2. Should move at BOB when called on the first element in buffer.
  2128. (should
  2129. (org-test-with-temp-text "\n#+TITLE: test"
  2130. (progn (forward-line)
  2131. (org-backward-element)
  2132. (bobp))))
  2133. ;; 3. Not at the beginning of an element: move at its beginning.
  2134. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2135. (goto-line 3)
  2136. (end-of-line)
  2137. (org-backward-element)
  2138. (should (looking-at (regexp-quote "Paragraph2."))))
  2139. ;; 4. Headline tests.
  2140. (org-test-with-temp-text "
  2141. * Head 1
  2142. ** Head 1.1
  2143. *** Head 1.1.1
  2144. ** Head 1.2"
  2145. ;; 4.1. At an headline beginning: move to previous headline at the
  2146. ;; same level.
  2147. (goto-line 5)
  2148. (org-backward-element)
  2149. (should (looking-at (regexp-quote "** Head 1.1")))
  2150. ;; 4.2. At an headline beginning: move to parent headline if no
  2151. ;; headline at the same level.
  2152. (goto-line 3)
  2153. (org-backward-element)
  2154. (should (looking-at (regexp-quote "* Head 1")))
  2155. ;; 4.3. At the first top-level headline: should error.
  2156. (goto-line 2)
  2157. (should-error (org-backward-element)))
  2158. ;; 5. At beginning of first element inside a greater element:
  2159. ;; expected to move to greater element's beginning.
  2160. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  2161. (goto-line 3)
  2162. (org-backward-element)
  2163. (should (looking-at "#\\+BEGIN_CENTER")))
  2164. ;; 6. At the beginning of the first element in a section: should
  2165. ;; move back to headline, if any.
  2166. (should
  2167. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  2168. (progn (goto-char (point-max))
  2169. (beginning-of-line)
  2170. (org-backward-element)
  2171. (org-at-heading-p))))
  2172. ;; 7. List tests.
  2173. (org-test-with-temp-text "
  2174. - item1
  2175. - sub1
  2176. - sub2
  2177. - sub3
  2178. Inner paragraph.
  2179. - item2
  2180. Outside."
  2181. ;; 7.1. At beginning of sub-list: expected to move to the
  2182. ;; paragraph before it.
  2183. (goto-line 4)
  2184. (org-backward-element)
  2185. (should (looking-at "item1"))
  2186. ;; 7.2. At an item in a list: expected to move at previous item.
  2187. (goto-line 8)
  2188. (org-backward-element)
  2189. (should (looking-at " - sub2"))
  2190. (goto-line 12)
  2191. (org-backward-element)
  2192. (should (looking-at "- item1"))
  2193. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  2194. ;; beginning.
  2195. (goto-line 10)
  2196. (org-backward-element)
  2197. (should (looking-at " - sub1"))
  2198. (goto-line 15)
  2199. (org-backward-element)
  2200. (should (looking-at "- item1"))
  2201. ;; 7.4. At blank-lines before list end: expected to move to top
  2202. ;; item.
  2203. (goto-line 14)
  2204. (org-backward-element)
  2205. (should (looking-at "- item1"))))
  2206. (ert-deftest test-org/up-element ()
  2207. "Test `org-up-element' specifications."
  2208. ;; 1. At BOB or with no surrounding element: should error.
  2209. (org-test-with-temp-text "Paragraph."
  2210. (should-error (org-up-element)))
  2211. (org-test-with-temp-text "* Head1\n* Head2"
  2212. (goto-line 2)
  2213. (should-error (org-up-element)))
  2214. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2215. (goto-line 3)
  2216. (should-error (org-up-element)))
  2217. ;; 2. At an headline: move to parent headline.
  2218. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  2219. (goto-line 3)
  2220. (org-up-element)
  2221. (should (looking-at "\\* Head1")))
  2222. ;; 3. Inside a greater element: move to greater element beginning.
  2223. (org-test-with-temp-text
  2224. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  2225. (goto-line 3)
  2226. (org-up-element)
  2227. (should (looking-at "#\\+BEGIN_CENTER")))
  2228. ;; 4. List tests.
  2229. (org-test-with-temp-text "* Top
  2230. - item1
  2231. - sub1
  2232. - sub2
  2233. Paragraph within sub2.
  2234. - item2"
  2235. ;; 4.1. Within an item: move to the item beginning.
  2236. (goto-line 8)
  2237. (org-up-element)
  2238. (should (looking-at " - sub2"))
  2239. ;; 4.2. At an item in a sub-list: move to parent item.
  2240. (goto-line 4)
  2241. (org-up-element)
  2242. (should (looking-at "- item1"))
  2243. ;; 4.3. At an item in top list: move to beginning of whole list.
  2244. (goto-line 10)
  2245. (org-up-element)
  2246. (should (looking-at "- item1"))
  2247. ;; 4.4. Special case. At very top point: should move to parent of
  2248. ;; list.
  2249. (goto-line 2)
  2250. (org-up-element)
  2251. (should (looking-at "\\* Top"))))
  2252. (ert-deftest test-org/down-element ()
  2253. "Test `org-down-element' specifications."
  2254. ;; Error when the element hasn't got a recursive type.
  2255. (org-test-with-temp-text "Paragraph."
  2256. (should-error (org-down-element)))
  2257. ;; Error when the element has no contents
  2258. (org-test-with-temp-text "* Headline"
  2259. (should-error (org-down-element)))
  2260. ;; When at a plain-list, move to first item.
  2261. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  2262. (goto-line 2)
  2263. (org-down-element)
  2264. (should (looking-at " - Item 1.1")))
  2265. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  2266. (org-down-element)
  2267. (should (looking-at " Item 1")))
  2268. ;; When at a table, move to first row
  2269. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  2270. (org-down-element)
  2271. (should (looking-at " a | b |")))
  2272. ;; Otherwise, move inside the greater element.
  2273. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  2274. (org-down-element)
  2275. (should (looking-at "Paragraph"))))
  2276. (ert-deftest test-org/drag-element-backward ()
  2277. "Test `org-drag-element-backward' specifications."
  2278. ;; Standard test.
  2279. (should
  2280. (equal
  2281. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  2282. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  2283. (org-drag-element-backward)
  2284. (buffer-string))))
  2285. (should
  2286. (equal
  2287. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  2288. (org-test-with-temp-text
  2289. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  2290. (org-drag-element-backward)
  2291. (buffer-string))))
  2292. ;; Preserve blank lines.
  2293. (should
  2294. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  2295. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  2296. (org-drag-element-backward)
  2297. (buffer-string))))
  2298. ;; Preserve column.
  2299. (should
  2300. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  2301. (org-drag-element-backward)
  2302. (org-looking-at-p "2")))
  2303. ;; Error when trying to move first element of buffer.
  2304. (should-error
  2305. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2306. (org-drag-element-backward)))
  2307. ;; Error when trying to swap nested elements.
  2308. (should-error
  2309. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2310. (forward-line)
  2311. (org-drag-element-backward)))
  2312. ;; Error when trying to swap an headline element and a non-headline
  2313. ;; element.
  2314. (should-error
  2315. (org-test-with-temp-text "Test.\n* Head 1"
  2316. (forward-line)
  2317. (org-drag-element-backward)))
  2318. ;; Preserve visibility of elements and their contents.
  2319. (should
  2320. (equal '((63 . 82) (26 . 48))
  2321. (org-test-with-temp-text "
  2322. #+BEGIN_CENTER
  2323. Text.
  2324. #+END_CENTER
  2325. - item 1
  2326. #+BEGIN_QUOTE
  2327. Text.
  2328. #+END_QUOTE"
  2329. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2330. (search-backward "- item 1")
  2331. (org-drag-element-backward)
  2332. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2333. (overlays-in (point-min) (point-max)))))))
  2334. (ert-deftest test-org/drag-element-forward ()
  2335. "Test `org-drag-element-forward' specifications."
  2336. ;; 1. Error when trying to move first element of buffer.
  2337. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2338. (goto-line 3)
  2339. (should-error (org-drag-element-forward)))
  2340. ;; 2. Error when trying to swap nested elements.
  2341. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2342. (forward-line)
  2343. (should-error (org-drag-element-forward)))
  2344. ;; 3. Error when trying to swap a non-headline element and an
  2345. ;; headline.
  2346. (org-test-with-temp-text "Test.\n* Head 1"
  2347. (should-error (org-drag-element-forward)))
  2348. ;; 4. Otherwise, swap elements, preserving column and blank lines
  2349. ;; between elements.
  2350. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  2351. (search-forward "graph")
  2352. (org-drag-element-forward)
  2353. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  2354. (should (looking-at " 1")))
  2355. ;; 5. Preserve visibility of elements and their contents.
  2356. (org-test-with-temp-text "
  2357. #+BEGIN_CENTER
  2358. Text.
  2359. #+END_CENTER
  2360. - item 1
  2361. #+BEGIN_QUOTE
  2362. Text.
  2363. #+END_QUOTE"
  2364. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2365. (search-backward "#+BEGIN_CENTER")
  2366. (org-drag-element-forward)
  2367. (should
  2368. (equal
  2369. '((63 . 82) (26 . 48))
  2370. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2371. (overlays-in (point-min) (point-max)))))))
  2372. (ert-deftest test-org/next-block ()
  2373. "Test `org-next-block' specifications."
  2374. ;; Regular test.
  2375. (should
  2376. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2377. (org-next-block 1)
  2378. (looking-at "#\\+BEGIN_CENTER")))
  2379. ;; Ignore case.
  2380. (should
  2381. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  2382. (let ((case-fold-search nil))
  2383. (org-next-block 1)
  2384. (looking-at "#\\+begin_center"))))
  2385. ;; Ignore current line.
  2386. (should
  2387. (org-test-with-temp-text
  2388. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  2389. (org-next-block 1)
  2390. (looking-at "#\\+BEGIN_CENTER")))
  2391. ;; Throw an error when no block is found.
  2392. (should-error
  2393. (org-test-with-temp-text "Paragraph"
  2394. (org-next-block 1)))
  2395. ;; With an argument, skip many blocks at once.
  2396. (should
  2397. (org-test-with-temp-text
  2398. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  2399. (org-next-block 2)
  2400. (looking-at "#\\+BEGIN_QUOTE")))
  2401. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  2402. (should
  2403. (org-test-with-temp-text
  2404. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  2405. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  2406. (looking-at "#\\+BEGIN_QUOTE")))
  2407. ;; Optional argument is also case-insensitive.
  2408. (should
  2409. (org-test-with-temp-text
  2410. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  2411. (let ((case-fold-search nil))
  2412. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  2413. (looking-at "#\\+begin_quote")))))
  2414. (ert-deftest test-org/previous-block ()
  2415. "Test `org-previous-block' specifications."
  2416. ;; Regular test.
  2417. (should
  2418. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  2419. (org-previous-block 1)
  2420. (looking-at "#\\+BEGIN_CENTER")))
  2421. ;; Ignore case.
  2422. (should
  2423. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  2424. (let ((case-fold-search nil))
  2425. (org-previous-block 1)
  2426. (looking-at "#\\+begin_center"))))
  2427. ;; Ignore current line.
  2428. (should
  2429. (org-test-with-temp-text
  2430. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  2431. (org-previous-block 1)
  2432. (looking-at "#\\+BEGIN_QUOTE")))
  2433. ;; Throw an error when no block is found.
  2434. (should-error
  2435. (org-test-with-temp-text "Paragraph<point>"
  2436. (org-previous-block 1)))
  2437. ;; With an argument, skip many blocks at once.
  2438. (should
  2439. (org-test-with-temp-text
  2440. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  2441. (org-previous-block 2)
  2442. (looking-at "#\\+BEGIN_CENTER")))
  2443. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  2444. (should
  2445. (org-test-with-temp-text
  2446. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  2447. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  2448. (looking-at "#\\+BEGIN_QUOTE")))
  2449. ;; Optional argument is also case-insensitive.
  2450. (should
  2451. (org-test-with-temp-text
  2452. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  2453. (let ((case-fold-search nil))
  2454. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  2455. (looking-at "#\\+begin_quote")))))
  2456. ;;; Outline structure
  2457. (ert-deftest test-org/demote ()
  2458. "Test `org-demote' specifications."
  2459. ;; Add correct number of stars according to `org-odd-levels-only'.
  2460. (should
  2461. (= 2
  2462. (org-test-with-temp-text "* H"
  2463. (let ((org-odd-levels-only nil)) (org-demote))
  2464. (org-current-level))))
  2465. (should
  2466. (= 3
  2467. (org-test-with-temp-text "* H"
  2468. (let ((org-odd-levels-only t)) (org-demote))
  2469. (org-current-level))))
  2470. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  2471. (should
  2472. (org-test-with-temp-text "* H :tag:"
  2473. (let ((org-tags-column 10)
  2474. (org-auto-align-tags t)
  2475. (org-odd-levels-only nil))
  2476. (org-demote))
  2477. (org-move-to-column 10)
  2478. (org-looking-at-p ":tag:$")))
  2479. (should-not
  2480. (org-test-with-temp-text "* H :tag:"
  2481. (let ((org-tags-column 10)
  2482. (org-auto-align-tags nil)
  2483. (org-odd-levels-only nil))
  2484. (org-demote))
  2485. (org-move-to-column 10)
  2486. (org-looking-at-p ":tag:$")))
  2487. ;; When `org-adapt-indentation' is non-nil, always indent planning
  2488. ;; info and property drawers accordingly.
  2489. (should
  2490. (= 3
  2491. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  2492. (let ((org-odd-levels-only nil)
  2493. (org-adapt-indentation t))
  2494. (org-demote))
  2495. (forward-line)
  2496. (org-get-indentation))))
  2497. (should
  2498. (= 3
  2499. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2500. (let ((org-odd-levels-only nil)
  2501. (org-adapt-indentation t))
  2502. (org-demote))
  2503. (forward-line)
  2504. (org-get-indentation))))
  2505. (should-not
  2506. (= 3
  2507. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  2508. (let ((org-odd-levels-only nil)
  2509. (org-adapt-indentation nil))
  2510. (org-demote))
  2511. (forward-line)
  2512. (org-get-indentation))))
  2513. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2514. ;; section accordingly. Ignore, however, footnote definitions and
  2515. ;; inlinetasks boundaries.
  2516. (should
  2517. (= 3
  2518. (org-test-with-temp-text "* H\n Paragraph"
  2519. (let ((org-odd-levels-only nil)
  2520. (org-adapt-indentation t))
  2521. (org-demote))
  2522. (forward-line)
  2523. (org-get-indentation))))
  2524. (should
  2525. (= 2
  2526. (org-test-with-temp-text "* H\n Paragraph"
  2527. (let ((org-odd-levels-only nil)
  2528. (org-adapt-indentation nil))
  2529. (org-demote))
  2530. (forward-line)
  2531. (org-get-indentation))))
  2532. (should
  2533. (zerop
  2534. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  2535. (let ((org-odd-levels-only nil)
  2536. (org-adapt-indentation t))
  2537. (org-demote))
  2538. (goto-char (point-max))
  2539. (org-get-indentation))))
  2540. (should
  2541. (= 3
  2542. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  2543. (let ((org-odd-levels-only nil)
  2544. (org-adapt-indentation t))
  2545. (org-demote))
  2546. (goto-char (point-max))
  2547. (org-get-indentation))))
  2548. (when (featurep 'org-inlinetask)
  2549. (should
  2550. (zerop
  2551. (let ((org-inlinetask-min-level 5)
  2552. (org-adapt-indentation t))
  2553. (org-test-with-temp-text "* H\n***** I\n***** END"
  2554. (org-demote)
  2555. (forward-line)
  2556. (org-get-indentation))))))
  2557. (when (featurep 'org-inlinetask)
  2558. (should
  2559. (= 3
  2560. (let ((org-inlinetask-min-level 5)
  2561. (org-adapt-indentation t))
  2562. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  2563. (org-demote)
  2564. (forward-line 2)
  2565. (org-get-indentation))))))
  2566. ;; Ignore contents of source blocks or example blocks when
  2567. ;; indentation should be preserved (through
  2568. ;; `org-src-preserve-indentation' or "-i" flag).
  2569. (should-not
  2570. (zerop
  2571. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2572. (let ((org-adapt-indentation t)
  2573. (org-src-preserve-indentation nil))
  2574. (org-demote))
  2575. (forward-line 2)
  2576. (org-get-indentation))))
  2577. (should
  2578. (zerop
  2579. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2580. (let ((org-adapt-indentation t)
  2581. (org-src-preserve-indentation t))
  2582. (org-demote))
  2583. (forward-line 2)
  2584. (org-get-indentation))))
  2585. (should
  2586. (zerop
  2587. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2588. (let ((org-adapt-indentation t)
  2589. (org-src-preserve-indentation t))
  2590. (org-demote))
  2591. (forward-line 2)
  2592. (org-get-indentation))))
  2593. (should
  2594. (zerop
  2595. (org-test-with-temp-text
  2596. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  2597. (let ((org-adapt-indentation t)
  2598. (org-src-preserve-indentation nil))
  2599. (org-demote))
  2600. (forward-line 2)
  2601. (org-get-indentation)))))
  2602. (ert-deftest test-org/promote ()
  2603. "Test `org-promote' specifications."
  2604. ;; Return an error if headline is to be promoted to level 0, unless
  2605. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  2606. ;; headline becomes a comment.
  2607. (should-error
  2608. (org-test-with-temp-text "* H"
  2609. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  2610. (should
  2611. (equal "# H"
  2612. (org-test-with-temp-text "* H"
  2613. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  2614. (buffer-string))))
  2615. ;; Remove correct number of stars according to
  2616. ;; `org-odd-levels-only'.
  2617. (should
  2618. (= 2
  2619. (org-test-with-temp-text "*** H"
  2620. (let ((org-odd-levels-only nil)) (org-promote))
  2621. (org-current-level))))
  2622. (should
  2623. (= 1
  2624. (org-test-with-temp-text "*** H"
  2625. (let ((org-odd-levels-only t)) (org-promote))
  2626. (org-current-level))))
  2627. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  2628. (should
  2629. (org-test-with-temp-text "** H :tag:"
  2630. (let ((org-tags-column 10)
  2631. (org-auto-align-tags t)
  2632. (org-odd-levels-only nil))
  2633. (org-promote))
  2634. (org-move-to-column 10)
  2635. (org-looking-at-p ":tag:$")))
  2636. (should-not
  2637. (org-test-with-temp-text "** H :tag:"
  2638. (let ((org-tags-column 10)
  2639. (org-auto-align-tags nil)
  2640. (org-odd-levels-only nil))
  2641. (org-promote))
  2642. (org-move-to-column 10)
  2643. (org-looking-at-p ":tag:$")))
  2644. ;; When `org-adapt-indentation' is non-nil, always indent planning
  2645. ;; info and property drawers.
  2646. (should
  2647. (= 2
  2648. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2649. (let ((org-odd-levels-only nil)
  2650. (org-adapt-indentation t))
  2651. (org-promote))
  2652. (forward-line)
  2653. (org-get-indentation))))
  2654. (should
  2655. (= 2
  2656. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2657. (let ((org-odd-levels-only nil)
  2658. (org-adapt-indentation t))
  2659. (org-promote))
  2660. (forward-line)
  2661. (org-get-indentation))))
  2662. (should-not
  2663. (= 2
  2664. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2665. (let ((org-odd-levels-only nil)
  2666. (org-adapt-indentation nil))
  2667. (org-promote))
  2668. (forward-line)
  2669. (org-get-indentation))))
  2670. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2671. ;; section accordingly. Ignore, however, footnote definitions and
  2672. ;; inlinetasks boundaries.
  2673. (should
  2674. (= 2
  2675. (org-test-with-temp-text "** H\n Paragraph"
  2676. (let ((org-odd-levels-only nil)
  2677. (org-adapt-indentation t))
  2678. (org-promote))
  2679. (forward-line)
  2680. (org-get-indentation))))
  2681. (should-not
  2682. (= 2
  2683. (org-test-with-temp-text "** H\n Paragraph"
  2684. (let ((org-odd-levels-only nil)
  2685. (org-adapt-indentation nil))
  2686. (org-promote))
  2687. (forward-line)
  2688. (org-get-indentation))))
  2689. (should
  2690. (= 2
  2691. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  2692. (let ((org-odd-levels-only nil)
  2693. (org-adapt-indentation t))
  2694. (org-promote))
  2695. (forward-line)
  2696. (org-get-indentation))))
  2697. (when (featurep 'org-inlinetask)
  2698. (should
  2699. (zerop
  2700. (let ((org-inlinetask-min-level 5)
  2701. (org-adapt-indentation t))
  2702. (org-test-with-temp-text "** H\n***** I\n***** END"
  2703. (org-promote)
  2704. (forward-line)
  2705. (org-get-indentation))))))
  2706. (when (featurep 'org-inlinetask)
  2707. (should
  2708. (= 2
  2709. (let ((org-inlinetask-min-level 5)
  2710. (org-adapt-indentation t))
  2711. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  2712. (org-promote)
  2713. (forward-line 2)
  2714. (org-get-indentation))))))
  2715. ;; Give up shifting if it would break document's structure
  2716. ;; otherwise.
  2717. (should
  2718. (= 3
  2719. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  2720. (let ((org-odd-levels-only nil)
  2721. (org-adapt-indentation t))
  2722. (org-promote))
  2723. (forward-line)
  2724. (org-get-indentation))))
  2725. (should
  2726. (= 3
  2727. (org-test-with-temp-text "** H\n Paragraph\n * list."
  2728. (let ((org-odd-levels-only nil)
  2729. (org-adapt-indentation t))
  2730. (org-promote))
  2731. (forward-line)
  2732. (org-get-indentation))))
  2733. ;; Ignore contents of source blocks or example blocks when
  2734. ;; indentation should be preserved (through
  2735. ;; `org-src-preserve-indentation' or "-i" flag).
  2736. (should-not
  2737. (zerop
  2738. (org-test-with-temp-text
  2739. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2740. (let ((org-adapt-indentation t)
  2741. (org-src-preserve-indentation nil)
  2742. (org-odd-levels-only nil))
  2743. (org-promote))
  2744. (forward-line)
  2745. (org-get-indentation))))
  2746. (should
  2747. (zerop
  2748. (org-test-with-temp-text
  2749. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  2750. (let ((org-adapt-indentation t)
  2751. (org-src-preserve-indentation t)
  2752. (org-odd-levels-only nil))
  2753. (org-promote))
  2754. (forward-line)
  2755. (org-get-indentation))))
  2756. (should
  2757. (zerop
  2758. (org-test-with-temp-text
  2759. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2760. (let ((org-adapt-indentation t)
  2761. (org-src-preserve-indentation t)
  2762. (org-odd-levels-only nil))
  2763. (org-promote))
  2764. (forward-line)
  2765. (org-get-indentation))))
  2766. (should
  2767. (zerop
  2768. (org-test-with-temp-text
  2769. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  2770. (let ((org-adapt-indentation t)
  2771. (org-src-preserve-indentation nil)
  2772. (org-odd-levels-only nil))
  2773. (org-promote))
  2774. (forward-line)
  2775. (org-get-indentation)))))
  2776. ;;; Planning
  2777. (ert-deftest test-org/at-planning-p ()
  2778. "Test `org-at-planning-p' specifications."
  2779. ;; Regular test.
  2780. (should
  2781. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  2782. (org-at-planning-p)))
  2783. (should-not
  2784. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  2785. (org-at-planning-p)))
  2786. ;; Correctly find planning attached to inlinetasks.
  2787. (when (featurep 'org-inlinetask)
  2788. (should
  2789. (org-test-with-temp-text
  2790. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  2791. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2792. (should-not
  2793. (org-test-with-temp-text
  2794. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  2795. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2796. (should-not
  2797. (org-test-with-temp-text
  2798. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  2799. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2800. (should-not
  2801. (org-test-with-temp-text
  2802. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  2803. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  2804. (ert-deftest test-org/add-planning-info ()
  2805. "Test `org-add-planning-info'."
  2806. ;; Create deadline when `org-adapt-indentation' is non-nil.
  2807. (should
  2808. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  2809. (org-test-with-temp-text "* H\nParagraph<point>"
  2810. (let ((org-adapt-indentation t))
  2811. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  2812. (replace-regexp-in-string
  2813. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2814. nil nil 1))))
  2815. ;; Create deadline when `org-adapt-indentation' is nil.
  2816. (should
  2817. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  2818. (org-test-with-temp-text "* H\nParagraph<point>"
  2819. (let ((org-adapt-indentation nil))
  2820. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  2821. (replace-regexp-in-string
  2822. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2823. nil nil 1))))
  2824. ;; Update deadline when `org-adapt-indentation' is non-nil.
  2825. (should
  2826. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  2827. (org-test-with-temp-text "\
  2828. * H
  2829. DEADLINE: <2015-06-24 Wed>
  2830. Paragraph<point>"
  2831. (let ((org-adapt-indentation t))
  2832. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  2833. (replace-regexp-in-string
  2834. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2835. nil nil 1))))
  2836. ;; Update deadline when `org-adapt-indentation' is nil.
  2837. (should
  2838. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  2839. (org-test-with-temp-text "\
  2840. * H
  2841. DEADLINE: <2015-06-24 Wed>
  2842. Paragraph<point>"
  2843. (let ((org-adapt-indentation nil))
  2844. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  2845. (replace-regexp-in-string
  2846. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2847. nil nil 1))))
  2848. ;; Schedule when `org-adapt-indentation' is non-nil.
  2849. (should
  2850. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  2851. (org-test-with-temp-text "* H\nParagraph<point>"
  2852. (let ((org-adapt-indentation t))
  2853. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  2854. (replace-regexp-in-string
  2855. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2856. nil nil 1))))
  2857. ;; Schedule when `org-adapt-indentation' is nil.
  2858. (should
  2859. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  2860. (org-test-with-temp-text "* H\nParagraph<point>"
  2861. (let ((org-adapt-indentation nil))
  2862. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  2863. (replace-regexp-in-string
  2864. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2865. nil nil 1))))
  2866. ;; Add deadline when scheduled.
  2867. (should
  2868. (equal "\
  2869. * H
  2870. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  2871. Paragraph"
  2872. (org-test-with-temp-text "\
  2873. * H
  2874. SCHEDULED: <2015-06-24 Wed>
  2875. Paragraph<point>"
  2876. (let ((org-adapt-indentation t))
  2877. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  2878. (replace-regexp-in-string
  2879. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2880. nil nil 1))))
  2881. ;; Remove middle entry.
  2882. (should
  2883. (equal "\
  2884. * H
  2885. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  2886. Paragraph"
  2887. (org-test-with-temp-text "\
  2888. * H
  2889. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  2890. Paragraph<point>"
  2891. (let ((org-adapt-indentation t))
  2892. (org-add-planning-info nil nil 'deadline))
  2893. (replace-regexp-in-string
  2894. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  2895. nil nil 1))))
  2896. ;; Remove last entry and then middle entry (order should not
  2897. ;; matter).
  2898. (should
  2899. (equal "\
  2900. * H
  2901. CLOSED: [2015-06-24]
  2902. Paragraph"
  2903. (org-test-with-temp-text "\
  2904. * H
  2905. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  2906. Paragraph<point>"
  2907. (let ((org-adapt-indentation t))
  2908. (org-add-planning-info nil nil 'scheduled 'deadline))
  2909. (replace-regexp-in-string
  2910. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  2911. nil nil 1))))
  2912. ;; Remove closed when `org-adapt-indentation' is non-nil.
  2913. (should
  2914. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  2915. (org-test-with-temp-text "\
  2916. * H
  2917. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  2918. Paragraph<point>"
  2919. (let ((org-adapt-indentation t))
  2920. (org-add-planning-info nil nil 'closed))
  2921. (replace-regexp-in-string
  2922. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2923. nil nil 1))))
  2924. ;; Remove closed when `org-adapt-indentation' is nil.
  2925. (should
  2926. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  2927. (org-test-with-temp-text "\
  2928. * H
  2929. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  2930. Paragraph<point>"
  2931. (let ((org-adapt-indentation nil))
  2932. (org-add-planning-info nil nil 'closed))
  2933. (replace-regexp-in-string
  2934. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2935. nil nil 1))))
  2936. ;; Remove closed entry and delete empty line.
  2937. (should
  2938. (equal "\
  2939. * H
  2940. Paragraph"
  2941. (org-test-with-temp-text "\
  2942. * H
  2943. CLOSED: [2015-06-24 Wed]
  2944. Paragraph<point>"
  2945. (let ((org-adapt-indentation t))
  2946. (org-add-planning-info nil nil 'closed))
  2947. (replace-regexp-in-string
  2948. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2949. nil nil 1))))
  2950. ;; Remove one entry and update another.
  2951. (should
  2952. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  2953. (org-test-with-temp-text "\
  2954. * H
  2955. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  2956. Paragraph<point>"
  2957. (let ((org-adapt-indentation t))
  2958. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  2959. (replace-regexp-in-string
  2960. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2961. nil nil 1)))))
  2962. ;;; Property API
  2963. (ert-deftest test-org/buffer-property-keys ()
  2964. "Test `org-buffer-property-keys' specifications."
  2965. ;; Retrieve properties accross siblings.
  2966. (should
  2967. (equal '("A" "B")
  2968. (org-test-with-temp-text "
  2969. * H1
  2970. :PROPERTIES:
  2971. :A: 1
  2972. :END:
  2973. * H2
  2974. :PROPERTIES:
  2975. :B: 1
  2976. :END:"
  2977. (org-buffer-property-keys))))
  2978. ;; Retrieve properties accross children.
  2979. (should
  2980. (equal '("A" "B")
  2981. (org-test-with-temp-text "
  2982. * H1
  2983. :PROPERTIES:
  2984. :A: 1
  2985. :END:
  2986. ** H2
  2987. :PROPERTIES:
  2988. :B: 1
  2989. :END:"
  2990. (org-buffer-property-keys))))
  2991. ;; Retrieve muliple properties in the same drawer.
  2992. (should
  2993. (equal '("A" "B")
  2994. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  2995. (org-buffer-property-keys))))
  2996. ;; Ignore extension symbol in property name.
  2997. (should
  2998. (equal '("A")
  2999. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3000. (org-buffer-property-keys))))
  3001. ;; With non-nil COLUMNS, extract property names from columns.
  3002. (should
  3003. (equal '("A" "B")
  3004. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  3005. (org-buffer-property-keys nil nil t))))
  3006. (should
  3007. (equal '("A" "B" "COLUMNS")
  3008. (org-test-with-temp-text
  3009. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  3010. (org-buffer-property-keys nil nil t)))))
  3011. (ert-deftest test-org/property-values ()
  3012. "Test `org-property-values' specifications."
  3013. ;; Regular test.
  3014. (should
  3015. (equal '("2" "1")
  3016. (org-test-with-temp-text
  3017. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  3018. (org-property-values "A"))))
  3019. ;; Ignore empty values.
  3020. (should-not
  3021. (org-test-with-temp-text
  3022. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  3023. (org-property-values "A")))
  3024. ;; Take into consideration extended values.
  3025. (should
  3026. (equal '("1 2")
  3027. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3028. (org-property-values "A")))))
  3029. (ert-deftest test-org/find-property ()
  3030. "Test `org-find-property' specifications."
  3031. ;; Regular test.
  3032. (should
  3033. (= 1
  3034. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  3035. (org-find-property "prop"))))
  3036. ;; Ignore false positives.
  3037. (should
  3038. (= 27
  3039. (org-test-with-temp-text
  3040. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  3041. (org-find-property "A"))))
  3042. ;; Return first entry found in buffer.
  3043. (should
  3044. (= 1
  3045. (org-test-with-temp-text
  3046. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3047. (org-find-property "A"))))
  3048. ;; Only search visible part of the buffer.
  3049. (should
  3050. (= 31
  3051. (org-test-with-temp-text
  3052. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3053. (org-narrow-to-subtree)
  3054. (org-find-property "A"))))
  3055. ;; With optional argument, only find entries with a specific value.
  3056. (should-not
  3057. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3058. (org-find-property "A" "2")))
  3059. (should
  3060. (= 31
  3061. (org-test-with-temp-text
  3062. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  3063. (org-find-property "A" "2"))))
  3064. ;; Use "nil" for explicit nil values.
  3065. (should
  3066. (= 31
  3067. (org-test-with-temp-text
  3068. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  3069. (org-find-property "A" "nil")))))
  3070. (ert-deftest test-org/entry-delete ()
  3071. "Test `org-entry-delete' specifications."
  3072. ;; Regular test.
  3073. (should
  3074. (string-match
  3075. " *:PROPERTIES:\n *:B: +2\n *:END:"
  3076. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3077. (org-entry-delete (point) "A")
  3078. (buffer-string))))
  3079. ;; Also remove accumulated properties.
  3080. (should-not
  3081. (string-match
  3082. ":A"
  3083. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  3084. (org-entry-delete (point) "A")
  3085. (buffer-string))))
  3086. ;; When last property is removed, remove the property drawer.
  3087. (should-not
  3088. (string-match
  3089. ":PROPERTIES:"
  3090. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  3091. (org-entry-delete (point) "A")
  3092. (buffer-string))))
  3093. ;; Return a non-nil value when some property was removed.
  3094. (should
  3095. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3096. (org-entry-delete (point) "A")))
  3097. (should-not
  3098. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3099. (org-entry-delete (point) "C"))))
  3100. (ert-deftest test-org/entry-get ()
  3101. "Test `org-entry-get' specifications."
  3102. ;; Regular test.
  3103. (should
  3104. (equal "1"
  3105. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3106. (org-entry-get (point) "A"))))
  3107. ;; Ignore case.
  3108. (should
  3109. (equal "1"
  3110. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3111. (org-entry-get (point) "a"))))
  3112. ;; Handle extended values, both before and after base value.
  3113. (should
  3114. (equal "1 2 3"
  3115. (org-test-with-temp-text
  3116. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3117. (org-entry-get (point) "A"))))
  3118. ;; Empty values are returned as the empty string.
  3119. (should
  3120. (equal ""
  3121. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  3122. (org-entry-get (point) "A"))))
  3123. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  3124. ;; otherwise, return nil.
  3125. (should-not
  3126. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3127. (org-entry-get (point) "A")))
  3128. (should
  3129. (equal "nil"
  3130. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3131. (org-entry-get (point) "A" nil t))))
  3132. ;; Return nil when no property is found, independently on the
  3133. ;; LITERAL-NIL argument.
  3134. (should-not
  3135. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3136. (org-entry-get (point) "B")))
  3137. (should-not
  3138. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3139. (org-entry-get (point) "B" nil t)))
  3140. ;; Handle inheritance, when allowed. Include extended values and
  3141. ;; possibly global values.
  3142. (should
  3143. (equal
  3144. "1"
  3145. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3146. (org-entry-get (point) "A" t))))
  3147. (should
  3148. (equal
  3149. "1"
  3150. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3151. (let ((org-use-property-inheritance t))
  3152. (org-entry-get (point) "A" 'selective)))))
  3153. (should-not
  3154. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3155. (let ((org-use-property-inheritance nil))
  3156. (org-entry-get (point) "A" 'selective))))
  3157. (should
  3158. (equal
  3159. "1 2"
  3160. (org-test-with-temp-text
  3161. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  3162. (org-entry-get (point-max) "A" t))))
  3163. (should
  3164. (equal "1"
  3165. (org-test-with-temp-text
  3166. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  3167. (org-mode-restart)
  3168. (org-entry-get (point-max) "A" t))))
  3169. (should
  3170. (equal "0 1"
  3171. (org-test-with-temp-text
  3172. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  3173. (org-mode-restart)
  3174. (org-entry-get (point-max) "A" t)))))
  3175. (ert-deftest test-org/entry-properties ()
  3176. "Test `org-entry-properties' specifications."
  3177. ;; Get "ITEM" property.
  3178. (should
  3179. (equal "* H"
  3180. (org-test-with-temp-text "* TODO H"
  3181. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  3182. (should
  3183. (equal "* H"
  3184. (org-test-with-temp-text "* TODO H"
  3185. (cdr (assoc "ITEM" (org-entry-properties))))))
  3186. ;; Get "TODO" property. TODO keywords are case sensitive.
  3187. (should
  3188. (equal "TODO"
  3189. (org-test-with-temp-text "* TODO H"
  3190. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  3191. (should
  3192. (equal "TODO"
  3193. (org-test-with-temp-text "* TODO H"
  3194. (cdr (assoc "TODO" (org-entry-properties))))))
  3195. (should-not
  3196. (org-test-with-temp-text "* H"
  3197. (assoc "TODO" (org-entry-properties nil "TODO"))))
  3198. (should-not
  3199. (org-test-with-temp-text "* todo H"
  3200. (assoc "TODO" (org-entry-properties nil "TODO"))))
  3201. ;; Get "PRIORITY" property.
  3202. (should
  3203. (equal "A"
  3204. (org-test-with-temp-text "* [#A] H"
  3205. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  3206. (should
  3207. (equal "A"
  3208. (org-test-with-temp-text "* [#A] H"
  3209. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  3210. (should-not
  3211. (org-test-with-temp-text "* H"
  3212. (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))
  3213. ;; Get "FILE" property.
  3214. (should
  3215. (org-test-with-temp-text-in-file "* H\nParagraph"
  3216. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  3217. (buffer-file-name))))
  3218. (should
  3219. (org-test-with-temp-text-in-file "* H\nParagraph"
  3220. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  3221. (buffer-file-name))))
  3222. (should-not
  3223. (org-test-with-temp-text "* H\nParagraph"
  3224. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  3225. ;; Get "TAGS" property.
  3226. (should
  3227. (equal ":tag1:tag2:"
  3228. (org-test-with-temp-text "* H :tag1:tag2:"
  3229. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  3230. (should
  3231. (equal ":tag1:tag2:"
  3232. (org-test-with-temp-text "* H :tag1:tag2:"
  3233. (cdr (assoc "TAGS" (org-entry-properties))))))
  3234. (should-not
  3235. (org-test-with-temp-text "* H"
  3236. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  3237. ;; Get "ALLTAGS" property.
  3238. (should
  3239. (equal ":tag1:tag2:"
  3240. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  3241. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  3242. (should
  3243. (equal ":tag1:tag2:"
  3244. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  3245. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  3246. (should-not
  3247. (org-test-with-temp-text "* H"
  3248. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  3249. ;; Get "BLOCKED" property.
  3250. (should
  3251. (equal "t"
  3252. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  3253. (let ((org-enforce-todo-dependencies t)
  3254. (org-blocker-hook
  3255. '(org-block-todo-from-children-or-siblings-or-parent)))
  3256. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  3257. (should
  3258. (equal ""
  3259. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  3260. (let ((org-enforce-todo-dependencies t)
  3261. (org-blocker-hook
  3262. '(org-block-todo-from-children-or-siblings-or-parent)))
  3263. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  3264. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  3265. (should
  3266. (equal
  3267. "[2012-03-29 thu.]"
  3268. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  3269. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  3270. (should
  3271. (equal
  3272. "[2012-03-29 thu.]"
  3273. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  3274. (cdr (assoc "CLOSED" (org-entry-properties))))))
  3275. (should-not
  3276. (org-test-with-temp-text "* H"
  3277. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  3278. (should
  3279. (equal
  3280. "<2014-03-04 tue.>"
  3281. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3282. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  3283. (should
  3284. (equal
  3285. "<2014-03-04 tue.>"
  3286. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3287. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  3288. (should-not
  3289. (org-test-with-temp-text "* H"
  3290. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  3291. (should
  3292. (equal
  3293. "<2014-03-04 tue.>"
  3294. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3295. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  3296. (should
  3297. (equal
  3298. "<2014-03-04 tue.>"
  3299. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3300. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  3301. (should-not
  3302. (org-test-with-temp-text "* H"
  3303. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  3304. ;; Get "CATEGORY"
  3305. (should
  3306. (equal "cat"
  3307. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  3308. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  3309. (should
  3310. (equal "cat"
  3311. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  3312. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3313. (should
  3314. (equal "cat"
  3315. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  3316. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3317. (should
  3318. (equal "cat2"
  3319. (org-test-with-temp-text
  3320. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  3321. "\n"
  3322. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  3323. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3324. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  3325. (should
  3326. (equal "<2012-03-29 thu.>"
  3327. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  3328. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  3329. (should
  3330. (equal "[2012-03-29 thu.]"
  3331. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  3332. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  3333. (should
  3334. (equal "<2012-03-29 thu.>"
  3335. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  3336. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  3337. (should
  3338. (equal "[2014-03-04 tue.]"
  3339. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  3340. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
  3341. ;; Get standard properties.
  3342. (should
  3343. (equal "1"
  3344. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3345. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3346. ;; Handle extended properties.
  3347. (should
  3348. (equal "1 2 3"
  3349. (org-test-with-temp-text
  3350. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3351. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3352. (should
  3353. (equal "1 2 3"
  3354. (org-test-with-temp-text
  3355. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  3356. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3357. ;; Ignore forbidden (special) properties.
  3358. (should-not
  3359. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  3360. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  3361. (ert-deftest test-org/entry-put ()
  3362. "Test `org-entry-put' specifications."
  3363. ;; Error when not a string or nil.
  3364. (should-error
  3365. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  3366. (org-entry-put 1 "test" 2)))
  3367. ;; Set "TODO" property.
  3368. (should
  3369. (string-match (regexp-quote " TODO H")
  3370. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  3371. (org-entry-put (point) "TODO" "TODO")
  3372. (buffer-string))))
  3373. (should
  3374. (string-match (regexp-quote "* H")
  3375. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  3376. (org-entry-put (point) "TODO" nil)
  3377. (buffer-string))))
  3378. ;; Set "PRIORITY" property.
  3379. (should
  3380. (equal "* [#A] H"
  3381. (org-test-with-temp-text "* [#B] H"
  3382. (org-entry-put (point) "PRIORITY" "A")
  3383. (buffer-string))))
  3384. (should
  3385. (equal "* H"
  3386. (org-test-with-temp-text "* [#B] H"
  3387. (org-entry-put (point) "PRIORITY" nil)
  3388. (buffer-string))))
  3389. ;; Set "SCHEDULED" property.
  3390. (should
  3391. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  3392. (org-test-with-temp-text "* H"
  3393. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  3394. (buffer-string))))
  3395. (should
  3396. (string= "* H\n"
  3397. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3398. (org-entry-put (point) "SCHEDULED" nil)
  3399. (buffer-string))))
  3400. (should
  3401. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  3402. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3403. (org-entry-put (point) "SCHEDULED" "earlier")
  3404. (buffer-string))))
  3405. (should
  3406. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  3407. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3408. (org-entry-put (point) "SCHEDULED" "later")
  3409. (buffer-string))))
  3410. ;; Set "DEADLINE" property.
  3411. (should
  3412. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  3413. (org-test-with-temp-text "* H"
  3414. (org-entry-put (point) "DEADLINE" "2014-03-04")
  3415. (buffer-string))))
  3416. (should
  3417. (string= "* H\n"
  3418. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3419. (org-entry-put (point) "DEADLINE" nil)
  3420. (buffer-string))))
  3421. (should
  3422. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  3423. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3424. (org-entry-put (point) "DEADLINE" "earlier")
  3425. (buffer-string))))
  3426. (should
  3427. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  3428. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3429. (org-entry-put (point) "DEADLINE" "later")
  3430. (buffer-string))))
  3431. ;; Set "CATEGORY" property
  3432. (should
  3433. (string-match "^ *:CATEGORY: cat"
  3434. (org-test-with-temp-text "* H"
  3435. (org-entry-put (point) "CATEGORY" "cat")
  3436. (buffer-string))))
  3437. ;; Regular properties, with or without pre-existing drawer.
  3438. (should
  3439. (string-match "^ *:A: +2$"
  3440. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3441. (org-entry-put (point) "A" "2")
  3442. (buffer-string))))
  3443. (should
  3444. (string-match "^ *:A: +1$"
  3445. (org-test-with-temp-text "* H"
  3446. (org-entry-put (point) "A" "1")
  3447. (buffer-string))))
  3448. ;; Special case: two consecutive headlines.
  3449. (should
  3450. (string-match "\\* A\n *:PROPERTIES:"
  3451. (org-test-with-temp-text "* A\n** B"
  3452. (org-entry-put (point) "A" "1")
  3453. (buffer-string)))))
  3454. ;;; Radio Targets
  3455. (ert-deftest test-org/update-radio-target-regexp ()
  3456. "Test `org-update-radio-target-regexp' specifications."
  3457. ;; Properly update cache with no previous radio target regexp.
  3458. (should
  3459. (eq 'link
  3460. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  3461. (save-excursion (goto-char (point-max)) (org-element-context))
  3462. (insert "<<<")
  3463. (search-forward "o")
  3464. (insert ">>>")
  3465. (org-update-radio-target-regexp)
  3466. (goto-char (point-max))
  3467. (org-element-type (org-element-context)))))
  3468. ;; Properly update cache with previous radio target regexp.
  3469. (should
  3470. (eq 'link
  3471. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  3472. (save-excursion (goto-char (point-max)) (org-element-context))
  3473. (insert "<<<")
  3474. (search-forward "o")
  3475. (insert ">>>")
  3476. (org-update-radio-target-regexp)
  3477. (search-backward "r")
  3478. (delete-char 5)
  3479. (insert "new")
  3480. (org-update-radio-target-regexp)
  3481. (goto-char (point-max))
  3482. (delete-region (line-beginning-position) (point))
  3483. (insert "new")
  3484. (org-element-type (org-element-context))))))
  3485. ;;; Sparse trees
  3486. (ert-deftest test-org/match-sparse-tree ()
  3487. "Test `org-match-sparse-tree' specifications."
  3488. ;; Match tags.
  3489. (should-not
  3490. (org-test-with-temp-text "* H\n** H1 :tag:"
  3491. (org-match-sparse-tree nil "tag")
  3492. (search-forward "H1")
  3493. (org-invisible-p2)))
  3494. (should
  3495. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  3496. (org-match-sparse-tree nil "tag")
  3497. (search-forward "H2")
  3498. (org-invisible-p2)))
  3499. ;; "-" operator for tags.
  3500. (should-not
  3501. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3502. (org-match-sparse-tree nil "tag1-tag2")
  3503. (search-forward "H1")
  3504. (org-invisible-p2)))
  3505. (should
  3506. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3507. (org-match-sparse-tree nil "tag1-tag2")
  3508. (search-forward "H2")
  3509. (org-invisible-p2)))
  3510. ;; "&" operator for tags.
  3511. (should
  3512. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3513. (org-match-sparse-tree nil "tag1&tag2")
  3514. (search-forward "H1")
  3515. (org-invisible-p2)))
  3516. (should-not
  3517. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3518. (org-match-sparse-tree nil "tag1&tag2")
  3519. (search-forward "H2")
  3520. (org-invisible-p2)))
  3521. ;; "|" operator for tags.
  3522. (should-not
  3523. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3524. (org-match-sparse-tree nil "tag1|tag2")
  3525. (search-forward "H1")
  3526. (org-invisible-p2)))
  3527. (should-not
  3528. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3529. (org-match-sparse-tree nil "tag1|tag2")
  3530. (search-forward "H2")
  3531. (org-invisible-p2)))
  3532. ;; Regexp match on tags.
  3533. (should-not
  3534. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  3535. (org-match-sparse-tree nil "{^tag.*}")
  3536. (search-forward "H1")
  3537. (org-invisible-p2)))
  3538. (should
  3539. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  3540. (org-match-sparse-tree nil "{^tag.*}")
  3541. (search-forward "H2")
  3542. (org-invisible-p2)))
  3543. ;; Match group tags.
  3544. (should-not
  3545. (org-test-with-temp-text
  3546. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  3547. (org-match-sparse-tree nil "work")
  3548. (search-forward "H1")
  3549. (org-invisible-p2)))
  3550. (should-not
  3551. (org-test-with-temp-text
  3552. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  3553. (org-match-sparse-tree nil "work")
  3554. (search-forward "H2")
  3555. (org-invisible-p2)))
  3556. ;; Match group tags with hard brackets.
  3557. (should-not
  3558. (org-test-with-temp-text
  3559. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  3560. (org-match-sparse-tree nil "work")
  3561. (search-forward "H1")
  3562. (org-invisible-p2)))
  3563. (should-not
  3564. (org-test-with-temp-text
  3565. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  3566. (org-match-sparse-tree nil "work")
  3567. (search-forward "H2")
  3568. (org-invisible-p2)))
  3569. ;; Match tags in hierarchies
  3570. (should-not
  3571. (org-test-with-temp-text
  3572. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  3573. #+TAGS: [ Lev_2 : Lev_3 ]\n
  3574. #+TAGS: { Lev_3 : Lev_4 }\n
  3575. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  3576. (org-match-sparse-tree nil "Lev_1")
  3577. (search-forward "H4")
  3578. (org-invisible-p2)))
  3579. ;; Match regular expressions in tags
  3580. (should-not
  3581. (org-test-with-temp-text
  3582. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  3583. (org-match-sparse-tree nil "Lev")
  3584. (search-forward "H1")
  3585. (org-invisible-p2)))
  3586. (should
  3587. (org-test-with-temp-text
  3588. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  3589. (org-match-sparse-tree nil "Lev")
  3590. (search-forward "H1")
  3591. (org-invisible-p2)))
  3592. ;; Match properties.
  3593. (should
  3594. (org-test-with-temp-text
  3595. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  3596. (org-match-sparse-tree nil "A=\"1\"")
  3597. (search-forward "H2")
  3598. (org-invisible-p2)))
  3599. (should-not
  3600. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  3601. (org-match-sparse-tree nil "A=\"1\"")
  3602. (search-forward "H2")
  3603. (org-invisible-p2)))
  3604. ;; Case is not significant when matching properties.
  3605. (should-not
  3606. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  3607. (org-match-sparse-tree nil "a=\"1\"")
  3608. (search-forward "H2")
  3609. (org-invisible-p2)))
  3610. (should-not
  3611. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  3612. (org-match-sparse-tree nil "A=\"1\"")
  3613. (search-forward "H2")
  3614. (org-invisible-p2)))
  3615. ;; Match special LEVEL property.
  3616. (should-not
  3617. (org-test-with-temp-text "* H\n** H1\n*** H2"
  3618. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  3619. (search-forward "H1")
  3620. (org-invisible-p2)))
  3621. (should
  3622. (org-test-with-temp-text "* H\n** H1\n*** H2"
  3623. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  3624. (search-forward "H2")
  3625. (org-invisible-p2)))
  3626. ;; Comparison operators when matching properties.
  3627. (should
  3628. (org-test-with-temp-text
  3629. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  3630. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  3631. (search-forward "H1")
  3632. (org-invisible-p2)))
  3633. (should-not
  3634. (org-test-with-temp-text
  3635. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  3636. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  3637. (search-forward "H2")
  3638. (org-invisible-p2)))
  3639. ;; Regexp match on properties values.
  3640. (should-not
  3641. (org-test-with-temp-text
  3642. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  3643. (org-match-sparse-tree nil "A={f.*}")
  3644. (search-forward "H1")
  3645. (org-invisible-p2)))
  3646. (should
  3647. (org-test-with-temp-text
  3648. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  3649. (org-match-sparse-tree nil "A={f.*}")
  3650. (search-forward "H2")
  3651. (org-invisible-p2)))
  3652. ;; With an optional argument, limit match to TODO entries.
  3653. (should-not
  3654. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  3655. (org-match-sparse-tree t "tag")
  3656. (search-forward "H1")
  3657. (org-invisible-p2)))
  3658. (should
  3659. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  3660. (org-match-sparse-tree t "tag")
  3661. (search-forward "H2")
  3662. (org-invisible-p2))))
  3663. ;;; Timestamps API
  3664. (ert-deftest test-org/time-stamp ()
  3665. "Test `org-time-stamp' specifications."
  3666. ;; Insert chosen time stamp at point.
  3667. (should
  3668. (string-match
  3669. "Te<2014-03-04 .*?>xt"
  3670. (org-test-with-temp-text "Te<point>xt"
  3671. (flet ((org-read-date
  3672. (&rest args)
  3673. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3674. (org-time-stamp nil)
  3675. (buffer-string)))))
  3676. ;; With a prefix argument, also insert time.
  3677. (should
  3678. (string-match
  3679. "Te<2014-03-04 .*? 00:41>xt"
  3680. (org-test-with-temp-text "Te<point>xt"
  3681. (flet ((org-read-date
  3682. (&rest args)
  3683. (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
  3684. (org-time-stamp '(4))
  3685. (buffer-string)))))
  3686. ;; With two universal prefix arguments, insert an active timestamp
  3687. ;; with the current time without prompting the user.
  3688. (should
  3689. (string-match
  3690. "Te<2014-03-04 .*? 00:41>xt"
  3691. (org-test-with-temp-text "Te<point>xt"
  3692. (flet ((current-time
  3693. ()
  3694. (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
  3695. (org-time-stamp '(16))
  3696. (buffer-string)))))
  3697. ;; When optional argument is non-nil, insert an inactive timestamp.
  3698. (should
  3699. (string-match
  3700. "Te\\[2014-03-04 .*?\\]xt"
  3701. (org-test-with-temp-text "Te<point>xt"
  3702. (flet ((org-read-date
  3703. (&rest args)
  3704. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3705. (org-time-stamp nil t)
  3706. (buffer-string)))))
  3707. ;; When called from a timestamp, replace existing one.
  3708. (should
  3709. (string-match
  3710. "<2014-03-04 .*?>"
  3711. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  3712. (flet ((org-read-date
  3713. (&rest args)
  3714. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3715. (org-time-stamp nil)
  3716. (buffer-string)))))
  3717. (should
  3718. (string-match
  3719. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  3720. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  3721. (flet ((org-read-date
  3722. (&rest args)
  3723. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3724. (org-time-stamp nil)
  3725. (buffer-string)))))
  3726. ;; When replacing a timestamp, preserve repeater, if any.
  3727. (should
  3728. (string-match
  3729. "<2014-03-04 .*? \\+2y>"
  3730. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  3731. (flet ((org-read-date
  3732. (&rest args)
  3733. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3734. (org-time-stamp nil)
  3735. (buffer-string)))))
  3736. ;; When called twice in a raw, build a date range.
  3737. (should
  3738. (string-match
  3739. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  3740. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  3741. (flet ((org-read-date
  3742. (&rest args)
  3743. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3744. (let ((last-command 'org-time-stamp)
  3745. (this-command 'org-time-stamp))
  3746. (org-time-stamp nil))
  3747. (buffer-string))))))
  3748. (ert-deftest test-org/timestamp-has-time-p ()
  3749. "Test `org-timestamp-has-time-p' specifications."
  3750. ;; With time.
  3751. (should
  3752. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  3753. (org-timestamp-has-time-p (org-element-context))))
  3754. ;; Without time.
  3755. (should-not
  3756. (org-test-with-temp-text "<2012-03-29 Thu>"
  3757. (org-timestamp-has-time-p (org-element-context)))))
  3758. (ert-deftest test-org/timestamp-format ()
  3759. "Test `org-timestamp-format' specifications."
  3760. ;; Regular test.
  3761. (should
  3762. (equal
  3763. "2012-03-29 16:40"
  3764. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  3765. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  3766. ;; Range end.
  3767. (should
  3768. (equal
  3769. "2012-03-29"
  3770. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  3771. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  3772. (ert-deftest test-org/timestamp-split-range ()
  3773. "Test `org-timestamp-split-range' specifications."
  3774. ;; Extract range start (active).
  3775. (should
  3776. (equal '(2012 3 29)
  3777. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3778. (let ((ts (org-timestamp-split-range (org-element-context))))
  3779. (mapcar (lambda (p) (org-element-property p ts))
  3780. '(:year-end :month-end :day-end))))))
  3781. ;; Extract range start (inactive)
  3782. (should
  3783. (equal '(2012 3 29)
  3784. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  3785. (let ((ts (org-timestamp-split-range (org-element-context))))
  3786. (mapcar (lambda (p) (org-element-property p ts))
  3787. '(:year-end :month-end :day-end))))))
  3788. ;; Extract range end (active).
  3789. (should
  3790. (equal '(2012 3 30)
  3791. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3792. (let ((ts (org-timestamp-split-range
  3793. (org-element-context) t)))
  3794. (mapcar (lambda (p) (org-element-property p ts))
  3795. '(:year-end :month-end :day-end))))))
  3796. ;; Extract range end (inactive)
  3797. (should
  3798. (equal '(2012 3 30)
  3799. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  3800. (let ((ts (org-timestamp-split-range
  3801. (org-element-context) t)))
  3802. (mapcar (lambda (p) (org-element-property p ts))
  3803. '(:year-end :month-end :day-end))))))
  3804. ;; Return the timestamp if not a range.
  3805. (should
  3806. (org-test-with-temp-text "[2012-03-29 Thu]"
  3807. (let* ((ts-orig (org-element-context))
  3808. (ts-copy (org-timestamp-split-range ts-orig)))
  3809. (eq ts-orig ts-copy))))
  3810. (should
  3811. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  3812. (let* ((ts-orig (org-element-context))
  3813. (ts-copy (org-timestamp-split-range ts-orig)))
  3814. (eq ts-orig ts-copy)))))
  3815. (ert-deftest test-org/timestamp-translate ()
  3816. "Test `org-timestamp-translate' specifications."
  3817. ;; Translate whole date range.
  3818. (should
  3819. (equal "<29>--<30>"
  3820. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3821. (let ((org-display-custom-times t)
  3822. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3823. (org-timestamp-translate (org-element-context))))))
  3824. ;; Translate date range start.
  3825. (should
  3826. (equal "<29>"
  3827. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3828. (let ((org-display-custom-times t)
  3829. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3830. (org-timestamp-translate (org-element-context) 'start)))))
  3831. ;; Translate date range end.
  3832. (should
  3833. (equal "<30>"
  3834. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3835. (let ((org-display-custom-times t)
  3836. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3837. (org-timestamp-translate (org-element-context) 'end)))))
  3838. ;; Translate time range.
  3839. (should
  3840. (equal "<08>--<16>"
  3841. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  3842. (let ((org-display-custom-times t)
  3843. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  3844. (org-timestamp-translate (org-element-context))))))
  3845. ;; Translate non-range timestamp.
  3846. (should
  3847. (equal "<29>"
  3848. (org-test-with-temp-text "<2012-03-29 Thu>"
  3849. (let ((org-display-custom-times t)
  3850. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3851. (org-timestamp-translate (org-element-context))))))
  3852. ;; Do not change `diary' timestamps.
  3853. (should
  3854. (equal "<%%(org-float t 4 2)>"
  3855. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  3856. (let ((org-display-custom-times t)
  3857. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3858. (org-timestamp-translate (org-element-context)))))))
  3859. ;;; Visibility
  3860. (ert-deftest test-org/flag-drawer ()
  3861. "Test `org-flag-drawer' specifications."
  3862. ;; Hide drawer.
  3863. (should
  3864. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3865. (org-flag-drawer t)
  3866. (get-char-property (line-end-position) 'invisible)))
  3867. ;; Show drawer.
  3868. (should-not
  3869. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3870. (org-flag-drawer t)
  3871. (org-flag-drawer nil)
  3872. (get-char-property (line-end-position) 'invisible)))
  3873. ;; Test optional argument.
  3874. (should
  3875. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  3876. (let ((drawer (save-excursion (search-forward ":D2")
  3877. (org-element-at-point))))
  3878. (org-flag-drawer t drawer)
  3879. (get-char-property (progn (search-forward ":D2") (line-end-position))
  3880. 'invisible))))
  3881. (should-not
  3882. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  3883. (let ((drawer (save-excursion (search-forward ":D2")
  3884. (org-element-at-point))))
  3885. (org-flag-drawer t drawer)
  3886. (get-char-property (line-end-position) 'invisible))))
  3887. ;; Do not hide fake drawers.
  3888. (should-not
  3889. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  3890. (forward-line 1)
  3891. (org-flag-drawer t)
  3892. (get-char-property (line-end-position) 'invisible)))
  3893. ;; Do not hide incomplete drawers.
  3894. (should-not
  3895. (org-test-with-temp-text ":D:\nparagraph"
  3896. (forward-line 1)
  3897. (org-flag-drawer t)
  3898. (get-char-property (line-end-position) 'invisible)))
  3899. ;; Do not hide drawers when called from final blank lines.
  3900. (should-not
  3901. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  3902. (goto-char (point-max))
  3903. (org-flag-drawer t)
  3904. (goto-char (point-min))
  3905. (get-char-property (line-end-position) 'invisible)))
  3906. ;; Don't leave point in an invisible part of the buffer when hiding
  3907. ;; a drawer away.
  3908. (should-not
  3909. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3910. (goto-char (point-max))
  3911. (org-flag-drawer t)
  3912. (get-char-property (point) 'invisible))))
  3913. (ert-deftest test-org/hide-block-toggle ()
  3914. "Test `org-hide-block-toggle' specifications."
  3915. ;; Error when not at a block.
  3916. (should-error
  3917. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  3918. (org-hide-block-toggle 'off)
  3919. (get-char-property (line-end-position) 'invisible)))
  3920. ;; Hide block.
  3921. (should
  3922. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3923. (org-hide-block-toggle)
  3924. (get-char-property (line-end-position) 'invisible)))
  3925. (should
  3926. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  3927. (org-hide-block-toggle)
  3928. (get-char-property (line-end-position) 'invisible)))
  3929. ;; Show block unconditionally when optional argument is `off'.
  3930. (should-not
  3931. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3932. (org-hide-block-toggle)
  3933. (org-hide-block-toggle 'off)
  3934. (get-char-property (line-end-position) 'invisible)))
  3935. (should-not
  3936. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3937. (org-hide-block-toggle 'off)
  3938. (get-char-property (line-end-position) 'invisible)))
  3939. ;; Hide block unconditionally when optional argument is non-nil.
  3940. (should
  3941. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3942. (org-hide-block-toggle t)
  3943. (get-char-property (line-end-position) 'invisible)))
  3944. (should
  3945. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  3946. (org-hide-block-toggle)
  3947. (org-hide-block-toggle t)
  3948. (get-char-property (line-end-position) 'invisible)))
  3949. ;; Do not hide block when called from final blank lines.
  3950. (should-not
  3951. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  3952. (org-hide-block-toggle)
  3953. (goto-char (point-min))
  3954. (get-char-property (line-end-position) 'invisible)))
  3955. ;; Don't leave point in an invisible part of the buffer when hiding
  3956. ;; a block away.
  3957. (should-not
  3958. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  3959. (org-hide-block-toggle)
  3960. (get-char-property (point) 'invisible))))
  3961. (ert-deftest test-org/hide-block-toggle-maybe ()
  3962. "Test `org-hide-block-toggle-maybe' specifications."
  3963. (should
  3964. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  3965. (org-hide-block-toggle-maybe)))
  3966. (should-not
  3967. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  3968. (ert-deftest test-org/show-set-visibility ()
  3969. "Test `org-show-set-visibility' specifications."
  3970. ;; Do not throw an error before first heading.
  3971. (should
  3972. (org-test-with-temp-text "Preamble\n* Headline"
  3973. (org-show-set-visibility 'tree)
  3974. t))
  3975. ;; Test all visibility spans, both on headline and in entry.
  3976. (let ((list-visible-lines
  3977. (lambda (state headerp)
  3978. (org-test-with-temp-text "* Grandmother (0)
  3979. ** Uncle (1)
  3980. *** Heir (2)
  3981. ** Father (3)
  3982. Ancestor text (4)
  3983. *** Sister (5)
  3984. Sibling text (6)
  3985. *** Self (7)
  3986. Match (8)
  3987. **** First born (9)
  3988. Child text (10)
  3989. **** The other child (11)
  3990. *** Brother (12)
  3991. ** Aunt (13)
  3992. "
  3993. (org-cycle t)
  3994. (search-forward (if headerp "Self" "Match"))
  3995. (org-show-set-visibility state)
  3996. (goto-char (point-min))
  3997. (let (result (line 0))
  3998. (while (not (eobp))
  3999. (unless (org-invisible-p2) (push line result))
  4000. (incf line)
  4001. (forward-line))
  4002. (nreverse result))))))
  4003. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  4004. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  4005. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  4006. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  4007. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  4008. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  4009. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  4010. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  4011. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  4012. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  4013. (funcall list-visible-lines 'tree nil)))
  4014. (should (equal '(0 1 3 4 5 7 12 13)
  4015. (funcall list-visible-lines 'canonical t)))
  4016. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  4017. (funcall list-visible-lines 'canonical nil)))))
  4018. (provide 'test-org)
  4019. ;;; test-org.el ends here