test-org.el 146 KB

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