test-org.el 158 KB

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