test-org.el 137 KB

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