test-org.el 140 KB

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