test-org.el 158 KB

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