test-org.el 133 KB

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