test-org.el 146 KB

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