test-org.el 170 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242
  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 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. (should ;TODO are case-sensitive
  1060. (equal "* \nTodo"
  1061. (org-test-with-temp-text "* <point>Todo"
  1062. (org-return)
  1063. (buffer-string))))
  1064. ;; At headline text, break headline text but preserve tags.
  1065. (should
  1066. (equal "* TODO [#B] foo :tag:\nbar"
  1067. (let (org-auto-align-tags)
  1068. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1069. (org-return)
  1070. (buffer-string)))))
  1071. ;; At bol of headline insert newline.
  1072. (should
  1073. (equal "\n* h"
  1074. (org-test-with-temp-text "<point>* h"
  1075. (org-return)
  1076. (buffer-string))))
  1077. ;; Refuse to leave invalid headline in buffer.
  1078. (should
  1079. (equal "* h\n"
  1080. (org-test-with-temp-text "*<point> h"
  1081. (org-return)
  1082. (buffer-string)))))
  1083. (ert-deftest test-org/meta-return ()
  1084. "Test M-RET (`org-meta-return') specifications."
  1085. ;; In a table field insert a row above.
  1086. (should
  1087. (org-test-with-temp-text "| a |"
  1088. (forward-char)
  1089. (org-meta-return)
  1090. (forward-line -1)
  1091. (looking-at "| |$")))
  1092. ;; In a paragraph change current line into a header.
  1093. (should
  1094. (org-test-with-temp-text "a"
  1095. (org-meta-return)
  1096. (beginning-of-line)
  1097. (looking-at "\* a$")))
  1098. ;; In an item insert an item, in this case above.
  1099. (should
  1100. (org-test-with-temp-text "- a"
  1101. (org-meta-return)
  1102. (beginning-of-line)
  1103. (looking-at "- $")))
  1104. ;; In a drawer and item insert an item, in this case above.
  1105. (should
  1106. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1107. (forward-line)
  1108. (org-meta-return)
  1109. (beginning-of-line)
  1110. (looking-at "- $"))))
  1111. (ert-deftest test-org/insert-heading ()
  1112. "Test `org-insert-heading' specifications."
  1113. ;; In an empty buffer, insert a new headline.
  1114. (should
  1115. (equal "* "
  1116. (org-test-with-temp-text ""
  1117. (org-insert-heading)
  1118. (buffer-string))))
  1119. ;; At the beginning of a line, turn it into a headline
  1120. (should
  1121. (equal "* P"
  1122. (org-test-with-temp-text "<point>P"
  1123. (org-insert-heading)
  1124. (buffer-string))))
  1125. ;; In the middle of a line, split the line if allowed, otherwise,
  1126. ;; insert the headline at its end.
  1127. (should
  1128. (equal "Para\n* graph"
  1129. (org-test-with-temp-text "Para<point>graph"
  1130. (let ((org-M-RET-may-split-line '((default . t))))
  1131. (org-insert-heading))
  1132. (buffer-string))))
  1133. (should
  1134. (equal "Paragraph\n* "
  1135. (org-test-with-temp-text "Para<point>graph"
  1136. (let ((org-M-RET-may-split-line '((default . nil))))
  1137. (org-insert-heading))
  1138. (buffer-string))))
  1139. ;; At the beginning of a headline, create one above.
  1140. (should
  1141. (equal "* \n* H"
  1142. (org-test-with-temp-text "* H"
  1143. (org-insert-heading)
  1144. (buffer-string))))
  1145. ;; In the middle of a headline, split it if allowed.
  1146. (should
  1147. (equal "* H\n* 1"
  1148. (org-test-with-temp-text "* H<point>1"
  1149. (let ((org-M-RET-may-split-line '((headline . t))))
  1150. (org-insert-heading))
  1151. (buffer-string))))
  1152. (should
  1153. (equal "* H1\n* "
  1154. (org-test-with-temp-text "* H<point>1"
  1155. (let ((org-M-RET-may-split-line '((headline . nil))))
  1156. (org-insert-heading))
  1157. (buffer-string))))
  1158. ;; However, splitting cannot happen on TODO keywords, priorities or
  1159. ;; tags.
  1160. (should
  1161. (equal "* TODO H1\n* "
  1162. (org-test-with-temp-text "* TO<point>DO H1"
  1163. (let ((org-M-RET-may-split-line '((headline . t))))
  1164. (org-insert-heading))
  1165. (buffer-string))))
  1166. (should
  1167. (equal "* [#A] H1\n* "
  1168. (org-test-with-temp-text "* [#<point>A] H1"
  1169. (let ((org-M-RET-may-split-line '((headline . t))))
  1170. (org-insert-heading))
  1171. (buffer-string))))
  1172. (should
  1173. (equal "* H1 :tag:\n* "
  1174. (org-test-with-temp-text "* H1 :ta<point>g:"
  1175. (let ((org-M-RET-may-split-line '((headline . t))))
  1176. (org-insert-heading))
  1177. (buffer-string))))
  1178. ;; When on a list, insert an item instead, unless called with an
  1179. ;; universal argument or if list is invisible. In this case, create
  1180. ;; a new headline after contents.
  1181. (should
  1182. (equal "* H\n- item\n- "
  1183. (org-test-with-temp-text "* H\n- item<point>"
  1184. (let ((org-insert-heading-respect-content nil))
  1185. (org-insert-heading))
  1186. (buffer-string))))
  1187. (should
  1188. (equal "* H\n- item\n- item 2\n* "
  1189. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  1190. (let ((org-insert-heading-respect-content nil))
  1191. (org-insert-heading '(4)))
  1192. (buffer-string))))
  1193. (should
  1194. (equal "* H\n- item\n* "
  1195. (org-test-with-temp-text "* H\n- item"
  1196. (org-cycle)
  1197. (goto-char (point-max))
  1198. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1199. (buffer-string))))
  1200. ;; Preserve list visibility when inserting an item.
  1201. (should
  1202. (equal
  1203. '(outline outline)
  1204. (org-test-with-temp-text "- A\n - B\n- C\n - D"
  1205. (let ((org-cycle-include-plain-lists t))
  1206. (org-cycle)
  1207. (forward-line 2)
  1208. (org-cycle)
  1209. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1210. (list (get-char-property (line-beginning-position 0) 'invisible)
  1211. (get-char-property (line-end-position 2) 'invisible))))))
  1212. ;; When called with one universal argument, insert a new headline at
  1213. ;; the end of the current subtree, independently on the position of
  1214. ;; point.
  1215. (should
  1216. (equal
  1217. "* H1\n** H2\n* "
  1218. (org-test-with-temp-text "* H1\n** H2"
  1219. (let ((org-insert-heading-respect-content nil))
  1220. (org-insert-heading '(4)))
  1221. (buffer-string))))
  1222. (should
  1223. (equal
  1224. "* H1\n** H2\n* "
  1225. (org-test-with-temp-text "* H<point>1\n** H2"
  1226. (let ((org-insert-heading-respect-content nil))
  1227. (org-insert-heading '(4)))
  1228. (buffer-string))))
  1229. ;; When called with two universal arguments, insert a new headline
  1230. ;; at the end of the grandparent subtree.
  1231. (should
  1232. (equal "* H1\n** H3\n- item\n** H2\n** "
  1233. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1234. (let ((org-insert-heading-respect-content nil))
  1235. (org-insert-heading '(16)))
  1236. (buffer-string))))
  1237. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1238. ;; a level 1 heading.
  1239. (should
  1240. (equal "* H1\n** H2\n* "
  1241. (org-test-with-temp-text "* H1\n** H2<point>"
  1242. (org-insert-heading nil nil t)
  1243. (buffer-string))))
  1244. (should
  1245. (equal "* H1\n- item\n* "
  1246. (org-test-with-temp-text "* H1\n- item<point>"
  1247. (org-insert-heading nil nil t)
  1248. (buffer-string))))
  1249. ;; Corner case: correctly insert a headline after an empty one.
  1250. (should
  1251. (equal "* \n* "
  1252. (org-test-with-temp-text "* <point>"
  1253. (org-insert-heading)
  1254. (buffer-string)))))
  1255. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1256. "Test `org-insert-todo-heading-respect-content' specifications."
  1257. ;; Create a TODO heading.
  1258. (should
  1259. (org-test-with-temp-text "* H1\n Body"
  1260. (org-insert-todo-heading-respect-content)
  1261. (nth 2 (org-heading-components))))
  1262. ;; Add headline at the end of the first subtree
  1263. (should
  1264. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  1265. (search-forward "H1Body")
  1266. (org-insert-todo-heading-respect-content)
  1267. (and (eobp) (org-at-heading-p))))
  1268. ;; In a list, do not create a new item.
  1269. (should
  1270. (org-test-with-temp-text "* H\n- an item\n- another one"
  1271. (search-forward "an ")
  1272. (org-insert-todo-heading-respect-content)
  1273. (and (eobp) (org-at-heading-p)))))
  1274. (ert-deftest test-org/clone-with-time-shift ()
  1275. "Test `org-clone-subtree-with-time-shift'."
  1276. ;; Clone non-repeating once.
  1277. (should
  1278. (equal "\
  1279. * H1\n<2015-06-21>
  1280. * H1\n<2015-06-23>
  1281. "
  1282. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1283. (org-clone-subtree-with-time-shift 1 "+2d")
  1284. (replace-regexp-in-string
  1285. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1286. nil nil 1))))
  1287. ;; Clone repeating once.
  1288. (should
  1289. (equal "\
  1290. * H1\n<2015-06-21>
  1291. * H1\n<2015-06-23>
  1292. * H1\n<2015-06-25 +1w>
  1293. "
  1294. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1295. (org-clone-subtree-with-time-shift 1 "+2d")
  1296. (replace-regexp-in-string
  1297. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1298. nil nil 1))))
  1299. ;; Clone non-repeating zero times.
  1300. (should
  1301. (equal "\
  1302. * H1\n<2015-06-21>
  1303. "
  1304. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1305. (org-clone-subtree-with-time-shift 0 "+2d")
  1306. (replace-regexp-in-string
  1307. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1308. nil nil 1))))
  1309. ;; Clone repeating "zero" times.
  1310. (should
  1311. (equal "\
  1312. * H1\n<2015-06-21>
  1313. * H1\n<2015-06-23 +1w>
  1314. "
  1315. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1316. (org-clone-subtree-with-time-shift 0 "+2d")
  1317. (replace-regexp-in-string
  1318. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1319. nil nil 1)))))
  1320. ;;; Fixed-Width Areas
  1321. (ert-deftest test-org/toggle-fixed-width ()
  1322. "Test `org-toggle-fixed-width' specifications."
  1323. ;; No region: Toggle on fixed-width marker in paragraphs.
  1324. (should
  1325. (equal ": A"
  1326. (org-test-with-temp-text "A"
  1327. (org-toggle-fixed-width)
  1328. (buffer-string))))
  1329. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1330. (should
  1331. (equal "A"
  1332. (org-test-with-temp-text ": A"
  1333. (org-toggle-fixed-width)
  1334. (buffer-string))))
  1335. ;; No region: Toggle on marker in blank lines after elements or just
  1336. ;; after a headline.
  1337. (should
  1338. (equal "* H\n: "
  1339. (org-test-with-temp-text "* H\n"
  1340. (forward-line)
  1341. (org-toggle-fixed-width)
  1342. (buffer-string))))
  1343. (should
  1344. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1345. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1346. (goto-char (point-max))
  1347. (org-toggle-fixed-width)
  1348. (buffer-string))))
  1349. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1350. ;; headlines, clocks)
  1351. (should
  1352. (equal ": * Headline"
  1353. (org-test-with-temp-text "* Headline"
  1354. (org-toggle-fixed-width)
  1355. (buffer-string))))
  1356. (should
  1357. (equal ": #+KEYWORD: value"
  1358. (org-test-with-temp-text "#+KEYWORD: value"
  1359. (org-toggle-fixed-width)
  1360. (buffer-string))))
  1361. ;; No region: error in other situations.
  1362. (should-error
  1363. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1364. (forward-line)
  1365. (org-toggle-fixed-width)
  1366. (buffer-string)))
  1367. ;; No region: Indentation is preserved.
  1368. (should
  1369. (equal "- A\n : B"
  1370. (org-test-with-temp-text "- A\n B"
  1371. (forward-line)
  1372. (org-toggle-fixed-width)
  1373. (buffer-string))))
  1374. ;; Region: If it contains only fixed-width elements and blank lines,
  1375. ;; toggle off fixed-width markup.
  1376. (should
  1377. (equal "A\n\nB"
  1378. (org-test-with-temp-text ": A\n\n: B"
  1379. (transient-mark-mode 1)
  1380. (push-mark (point) t t)
  1381. (goto-char (point-max))
  1382. (org-toggle-fixed-width)
  1383. (buffer-string))))
  1384. ;; Region: If it contains anything else, toggle on fixed-width but
  1385. ;; not on fixed-width areas.
  1386. (should
  1387. (equal ": A\n: \n: B\n: \n: C"
  1388. (org-test-with-temp-text "A\n\n: B\n\nC"
  1389. (transient-mark-mode 1)
  1390. (push-mark (point) t t)
  1391. (goto-char (point-max))
  1392. (org-toggle-fixed-width)
  1393. (buffer-string))))
  1394. ;; Region: Ignore blank lines at its end, unless it contains only
  1395. ;; such lines.
  1396. (should
  1397. (equal ": A\n\n"
  1398. (org-test-with-temp-text "A\n\n"
  1399. (transient-mark-mode 1)
  1400. (push-mark (point) t t)
  1401. (goto-char (point-max))
  1402. (org-toggle-fixed-width)
  1403. (buffer-string))))
  1404. (should
  1405. (equal ": \n: \n"
  1406. (org-test-with-temp-text "\n\n"
  1407. (transient-mark-mode 1)
  1408. (push-mark (point) t t)
  1409. (goto-char (point-max))
  1410. (org-toggle-fixed-width)
  1411. (buffer-string)))))
  1412. ;;; Headline
  1413. (ert-deftest test-org/get-heading ()
  1414. "Test `org-get-heading' specifications."
  1415. ;; Return current heading, even if point is not on it.
  1416. (should
  1417. (equal "H"
  1418. (org-test-with-temp-text "* H"
  1419. (org-get-heading))))
  1420. (should
  1421. (equal "H"
  1422. (org-test-with-temp-text "* H\nText<point>"
  1423. (org-get-heading))))
  1424. ;; Without any optional argument, return TODO keywords and tags.
  1425. (should
  1426. (equal "TODO H"
  1427. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1428. (org-get-heading))))
  1429. (should
  1430. (equal "H :tag:"
  1431. (org-test-with-temp-text "* H :tag:"
  1432. (org-get-heading))))
  1433. ;; With NO-TAGS argument, ignore tags.
  1434. (should
  1435. (equal "TODO H"
  1436. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1437. (org-get-heading t))))
  1438. (should
  1439. (equal "H"
  1440. (org-test-with-temp-text "* H :tag:"
  1441. (org-get-heading t))))
  1442. ;; With NO-TODO, ignore TODO keyword.
  1443. (should
  1444. (equal "H"
  1445. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1446. (org-get-heading nil t))))
  1447. (should
  1448. (equal "H :tag:"
  1449. (org-test-with-temp-text "* H :tag:"
  1450. (org-get-heading nil t))))
  1451. ;; TODO keywords are case-sensitive.
  1452. (should
  1453. (equal "Todo H"
  1454. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1455. (org-get-heading nil t))))
  1456. ;; On an empty headline, return value is consistent.
  1457. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1458. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1459. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1460. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t t)))))
  1461. (ert-deftest test-org/in-commented-heading-p ()
  1462. "Test `org-in-commented-heading-p' specifications."
  1463. ;; Commented headline.
  1464. (should
  1465. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1466. (goto-char (point-max))
  1467. (org-in-commented-heading-p)))
  1468. ;; Commented ancestor.
  1469. (should
  1470. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1471. (goto-char (point-max))
  1472. (org-in-commented-heading-p)))
  1473. ;; Comment keyword is case-sensitive.
  1474. (should-not
  1475. (org-test-with-temp-text "* Comment Headline\nBody"
  1476. (goto-char (point-max))
  1477. (org-in-commented-heading-p)))
  1478. ;; Keyword is standalone.
  1479. (should-not
  1480. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1481. (goto-char (point-max))
  1482. (org-in-commented-heading-p)))
  1483. ;; Optional argument.
  1484. (should-not
  1485. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1486. (goto-char (point-max))
  1487. (org-in-commented-heading-p t))))
  1488. (ert-deftest test-org/entry-blocked-p ()
  1489. ;; Check other dependencies.
  1490. (should
  1491. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1492. (let ((org-enforce-todo-dependencies t)
  1493. (org-blocker-hook
  1494. '(org-block-todo-from-children-or-siblings-or-parent)))
  1495. (org-entry-blocked-p))))
  1496. (should-not
  1497. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1498. (let ((org-enforce-todo-dependencies t)
  1499. (org-blocker-hook
  1500. '(org-block-todo-from-children-or-siblings-or-parent)))
  1501. (org-entry-blocked-p))))
  1502. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1503. ;; blocked.
  1504. (should-not
  1505. (org-test-with-temp-text "* Blocked\n** TODO one"
  1506. (let ((org-enforce-todo-dependencies t)
  1507. (org-blocker-hook
  1508. '(org-block-todo-from-children-or-siblings-or-parent)))
  1509. (org-entry-blocked-p))))
  1510. (should-not
  1511. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1512. (let ((org-enforce-todo-dependencies t)
  1513. (org-blocker-hook
  1514. '(org-block-todo-from-children-or-siblings-or-parent)))
  1515. (org-entry-blocked-p))))
  1516. ;; Follow :ORDERED: specifications.
  1517. (should
  1518. (org-test-with-temp-text
  1519. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1520. (let ((org-enforce-todo-dependencies t)
  1521. (org-blocker-hook
  1522. '(org-block-todo-from-children-or-siblings-or-parent)))
  1523. (org-entry-blocked-p))))
  1524. (should-not
  1525. (org-test-with-temp-text
  1526. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1527. (let ((org-enforce-todo-dependencies t)
  1528. (org-blocker-hook
  1529. '(org-block-todo-from-children-or-siblings-or-parent)))
  1530. (org-entry-blocked-p)))))
  1531. (ert-deftest test-org/get-outline-path ()
  1532. "Test `org-get-outline-path' specifications."
  1533. ;; Top-level headlines have no outline path.
  1534. (should-not
  1535. (org-test-with-temp-text "* H"
  1536. (org-get-outline-path)))
  1537. ;; Otherwise, outline path is the path leading to the headline.
  1538. (should
  1539. (equal '("H")
  1540. (org-test-with-temp-text "* H\n** S<point>"
  1541. (org-get-outline-path))))
  1542. ;; Find path even when point is not on a headline.
  1543. (should
  1544. (equal '("H")
  1545. (org-test-with-temp-text "* H\n** S\nText<point>"
  1546. (org-get-outline-path))))
  1547. ;; TODO keywords, tags and statistics cookies are ignored.
  1548. (should
  1549. (equal '("H")
  1550. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1551. (org-get-outline-path))))
  1552. ;; Links are replaced with their description or their path.
  1553. (should
  1554. (equal '("Org")
  1555. (org-test-with-temp-text
  1556. "* [[http://orgmode.org][Org]]\n** S<point>"
  1557. (org-get-outline-path))))
  1558. (should
  1559. (equal '("http://orgmode.org")
  1560. (org-test-with-temp-text
  1561. "* [[http://orgmode.org]]\n** S<point>"
  1562. (org-get-outline-path))))
  1563. ;; When WITH-SELF is non-nil, include current heading.
  1564. (should
  1565. (equal '("H")
  1566. (org-test-with-temp-text "* H"
  1567. (org-get-outline-path t))))
  1568. (should
  1569. (equal '("H" "S")
  1570. (org-test-with-temp-text "* H\n** S\nText<point>"
  1571. (org-get-outline-path t))))
  1572. ;; Using cache is transparent to the user.
  1573. (should
  1574. (equal '("H")
  1575. (org-test-with-temp-text "* H\n** S<point>"
  1576. (setq org-outline-path-cache nil)
  1577. (org-get-outline-path nil t))))
  1578. ;; Do not corrupt cache when finding outline path in distant part of
  1579. ;; the buffer.
  1580. (should
  1581. (equal '("H2")
  1582. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1583. (setq org-outline-path-cache nil)
  1584. (org-get-outline-path nil t)
  1585. (search-forward "S2")
  1586. (org-get-outline-path nil t))))
  1587. ;; Do not choke on empty headlines.
  1588. (should
  1589. (org-test-with-temp-text "* H\n** <point>"
  1590. (org-get-outline-path)))
  1591. (should
  1592. (org-test-with-temp-text "* \n** H<point>"
  1593. (org-get-outline-path))))
  1594. (ert-deftest test-org/format-outline-path ()
  1595. "Test `org-format-outline-path' specifications."
  1596. (should
  1597. (string= (org-format-outline-path (list "one" "two" "three"))
  1598. "one/two/three"))
  1599. ;; Empty path.
  1600. (should
  1601. (string= (org-format-outline-path '())
  1602. ""))
  1603. (should
  1604. (string= (org-format-outline-path '(nil))
  1605. ""))
  1606. ;; Empty path and prefix.
  1607. (should
  1608. (string= (org-format-outline-path '() nil ">>")
  1609. ">>"))
  1610. ;; Trailing whitespace in headings.
  1611. (should
  1612. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1613. "one/tw o/three"))
  1614. ;; Non-default prefix and separators.
  1615. (should
  1616. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1617. ">>|one|two|three"))
  1618. ;; Truncate.
  1619. (should
  1620. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1621. "one/two/.."))
  1622. ;; Give a very narrow width.
  1623. (should
  1624. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1625. "on"))
  1626. ;; Give a prefix that extends beyond the width.
  1627. (should
  1628. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1629. ">>>>>>>>>>")
  1630. ">>>>>>>>..")))
  1631. (ert-deftest test-org/map-entries ()
  1632. "Test `org-map-entries' specifications."
  1633. ;; Full match.
  1634. (should
  1635. (equal '(1 11)
  1636. (org-test-with-temp-text "* Level 1\n** Level 2"
  1637. (org-map-entries #'point))))
  1638. ;; Level match.
  1639. (should
  1640. (equal '(1)
  1641. (org-test-with-temp-text "* Level 1\n** Level 2"
  1642. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1643. (should
  1644. (equal '(11)
  1645. (org-test-with-temp-text "* Level 1\n** Level 2"
  1646. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1647. ;; Tag match.
  1648. (should
  1649. (equal '(11)
  1650. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1651. (org-map-entries #'point "yes"))))
  1652. (should
  1653. (equal '(14)
  1654. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1655. (org-map-entries #'point "+yes-a"))))
  1656. (should
  1657. (equal '(11 23)
  1658. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1659. (org-map-entries #'point "{yes?}"))))
  1660. ;; Priority match.
  1661. (should
  1662. (equal '(1)
  1663. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1664. (org-map-entries #'point "PRIORITY=\"A\""))))
  1665. ;; Date match.
  1666. (should
  1667. (equal '(36)
  1668. (org-test-with-temp-text "
  1669. * H1
  1670. SCHEDULED: <2012-03-29 thu.>
  1671. * H2
  1672. SCHEDULED: <2014-03-04 tue.>"
  1673. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1674. (should
  1675. (equal '(2)
  1676. (org-test-with-temp-text "
  1677. * H1
  1678. SCHEDULED: <2012-03-29 thu.>
  1679. * H2
  1680. SCHEDULED: <2014-03-04 tue.>"
  1681. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1682. ;; Regular property match.
  1683. (should
  1684. (equal '(2)
  1685. (org-test-with-temp-text "
  1686. * H1
  1687. :PROPERTIES:
  1688. :TEST: 1
  1689. :END:
  1690. * H2
  1691. :PROPERTIES:
  1692. :TEST: 2
  1693. :END:"
  1694. (org-map-entries #'point "TEST=1"))))
  1695. ;; Multiple criteria.
  1696. (should
  1697. (equal '(23)
  1698. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1699. (let (org-odd-levels-only
  1700. (org-use-tag-inheritance nil))
  1701. (org-map-entries #'point "yes+LEVEL=1")))))
  1702. ;; "or" criteria.
  1703. (should
  1704. (equal '(12 24)
  1705. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1706. (let (org-odd-levels-only)
  1707. (org-map-entries #'point "LEVEL=2|no")))))
  1708. (should
  1709. (equal '(1 12)
  1710. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  1711. (let (org-odd-levels-only)
  1712. (org-map-entries #'point "yes|no")))))
  1713. ;; "and" criteria.
  1714. (should
  1715. (equal '(22)
  1716. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  1717. (let (org-odd-levels-only)
  1718. (org-map-entries #'point "yes&no"))))))
  1719. (ert-deftest test-org/edit-headline ()
  1720. "Test `org-edit-headline' specifications."
  1721. (should
  1722. (equal "* B"
  1723. (org-test-with-temp-text "* A"
  1724. (org-edit-headline "B")
  1725. (buffer-string))))
  1726. ;; Handle empty headings.
  1727. (should
  1728. (equal "* "
  1729. (org-test-with-temp-text "* A"
  1730. (org-edit-headline "")
  1731. (buffer-string))))
  1732. (should
  1733. (equal "* A"
  1734. (org-test-with-temp-text "* "
  1735. (org-edit-headline "A")
  1736. (buffer-string))))
  1737. ;; Handle TODO keywords and priority cookies.
  1738. (should
  1739. (equal "* TODO B"
  1740. (org-test-with-temp-text "* TODO A"
  1741. (org-edit-headline "B")
  1742. (buffer-string))))
  1743. (should
  1744. (equal "* [#A] B"
  1745. (org-test-with-temp-text "* [#A] A"
  1746. (org-edit-headline "B")
  1747. (buffer-string))))
  1748. (should
  1749. (equal "* TODO [#A] B"
  1750. (org-test-with-temp-text "* TODO [#A] A"
  1751. (org-edit-headline "B")
  1752. (buffer-string))))
  1753. ;; Handle tags.
  1754. (equal "* B :tag:"
  1755. (org-test-with-temp-text "* A :tag:"
  1756. (let ((org-tags-column 4)) (org-edit-headline "B"))
  1757. (buffer-string))))
  1758. ;;; Keywords
  1759. (ert-deftest test-org/set-regexps-and-options ()
  1760. "Test `org-set-regexps-and-options' specifications."
  1761. ;; TAGS keyword.
  1762. (should
  1763. (equal '(("A"))
  1764. (let ((org-tag-alist '(("A")))
  1765. (org-tag-persistent-alist nil))
  1766. (org-test-with-temp-text ""
  1767. (org-mode-restart)
  1768. org-current-tag-alist))))
  1769. (should
  1770. (equal '(("B"))
  1771. (let ((org-tag-alist '(("A")))
  1772. (org-tag-persistent-alist nil))
  1773. (org-test-with-temp-text "#+TAGS: B"
  1774. (org-mode-restart)
  1775. org-current-tag-alist))))
  1776. (should
  1777. (equal '(("C") ("B"))
  1778. (let ((org-tag-alist '(("A")))
  1779. (org-tag-persistent-alist '(("C"))))
  1780. (org-test-with-temp-text "#+TAGS: B"
  1781. (org-mode-restart)
  1782. org-current-tag-alist))))
  1783. (should
  1784. (equal '(("B"))
  1785. (let ((org-tag-alist '(("A")))
  1786. (org-tag-persistent-alist '(("C"))))
  1787. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  1788. (org-mode-restart)
  1789. org-current-tag-alist))))
  1790. (should
  1791. (equal '(("A" . ?a) ("B") ("C"))
  1792. (let ((org-tag-persistant-alist nil))
  1793. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1794. (org-mode-restart)
  1795. org-current-tag-alist))))
  1796. (should
  1797. (equal '(("A") (:newline) ("B"))
  1798. (let ((org-tag-persistent-alist nil))
  1799. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1800. (org-mode-restart)
  1801. org-current-tag-alist))))
  1802. (should
  1803. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1804. (let ((org-tag-persistent-alist nil))
  1805. (org-test-with-temp-text "#+TAGS: { A B } C"
  1806. (org-mode-restart)
  1807. org-current-tag-alist))))
  1808. (should
  1809. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1810. (let ((org-tag-persistent-alist nil))
  1811. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1812. (org-mode-restart)
  1813. org-current-tag-alist))))
  1814. (should
  1815. (equal '(("A" "B" "C"))
  1816. (let ((org-tag-persistent-alist nil))
  1817. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1818. (org-mode-restart)
  1819. org-tag-groups-alist))))
  1820. (should
  1821. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1822. (let ((org-tag-persistent-alist nil))
  1823. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1824. (org-mode-restart)
  1825. org-current-tag-alist))))
  1826. (should
  1827. (equal '(("A" "B" "C"))
  1828. (let ((org-tag-persistent-alist nil))
  1829. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1830. (org-mode-restart)
  1831. org-tag-groups-alist))))
  1832. ;; FILETAGS keyword.
  1833. (should
  1834. (equal '("A" "B" "C")
  1835. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1836. (org-mode-restart)
  1837. org-file-tags)))
  1838. ;; PROPERTY keyword. Property names are case-insensitive.
  1839. (should
  1840. (equal "foo=1"
  1841. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1842. (org-mode-restart)
  1843. (cdr (assoc "var" org-file-properties)))))
  1844. (should
  1845. (equal
  1846. "foo=1 bar=2"
  1847. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1848. (org-mode-restart)
  1849. (cdr (assoc "var" org-file-properties)))))
  1850. (should
  1851. (equal
  1852. "foo=1 bar=2"
  1853. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1854. (org-mode-restart)
  1855. (cdr (assoc "var" org-file-properties)))))
  1856. ;; ARCHIVE keyword.
  1857. (should
  1858. (equal "%s_done::"
  1859. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1860. (org-mode-restart)
  1861. org-archive-location)))
  1862. ;; CATEGORY keyword.
  1863. (should
  1864. (eq 'test
  1865. (org-test-with-temp-text "#+CATEGORY: test"
  1866. (org-mode-restart)
  1867. org-category)))
  1868. (should
  1869. (equal "test"
  1870. (org-test-with-temp-text "#+CATEGORY: test"
  1871. (org-mode-restart)
  1872. (cdr (assoc "CATEGORY" org-file-properties)))))
  1873. ;; COLUMNS keyword.
  1874. (should
  1875. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1876. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1877. (org-mode-restart)
  1878. org-columns-default-format)))
  1879. ;; CONSTANTS keyword. Constants names are case sensitive.
  1880. (should
  1881. (equal '("299792458." "3.14")
  1882. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1883. (org-mode-restart)
  1884. (mapcar
  1885. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1886. '("c" "pi")))))
  1887. (should
  1888. (equal "3.14"
  1889. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1890. (org-mode-restart)
  1891. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1892. (should
  1893. (equal "22/7"
  1894. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1895. (org-mode-restart)
  1896. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1897. ;; LINK keyword.
  1898. (should
  1899. (equal
  1900. '("url1" "url2")
  1901. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1902. (org-mode-restart)
  1903. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1904. '("a" "b")))))
  1905. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1906. (should
  1907. (equal
  1908. '(?X ?Z ?Y)
  1909. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1910. (org-mode-restart)
  1911. (list org-highest-priority org-lowest-priority org-default-priority))))
  1912. (should
  1913. (equal
  1914. '(?A ?C ?B)
  1915. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1916. (org-mode-restart)
  1917. (list org-highest-priority org-lowest-priority org-default-priority))))
  1918. ;; STARTUP keyword.
  1919. (should
  1920. (equal '(t t)
  1921. (org-test-with-temp-text "#+STARTUP: fold odd"
  1922. (org-mode-restart)
  1923. (list org-startup-folded org-odd-levels-only))))
  1924. ;; TODO keywords.
  1925. (should
  1926. (equal '(("A" "B") ("C"))
  1927. (org-test-with-temp-text "#+TODO: A B | C"
  1928. (org-mode-restart)
  1929. (list org-not-done-keywords org-done-keywords))))
  1930. (should
  1931. (equal '(("A" "C") ("B" "D"))
  1932. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1933. (org-mode-restart)
  1934. (list org-not-done-keywords org-done-keywords))))
  1935. (should
  1936. (equal '(("A" "B") ("C"))
  1937. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1938. (org-mode-restart)
  1939. (list org-not-done-keywords org-done-keywords))))
  1940. (should
  1941. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1942. (org-test-with-temp-text "#+TODO: A(a)"
  1943. (org-mode-restart)
  1944. org-todo-key-alist)))
  1945. (should
  1946. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1947. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1948. (org-mode-restart)
  1949. org-todo-log-states)))
  1950. ;; Enter SETUPFILE keyword.
  1951. (should
  1952. (equal "1"
  1953. (org-test-with-temp-text
  1954. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1955. (org-mode-restart)
  1956. (cdr (assoc "a" org-file-properties))))))
  1957. ;;; Links
  1958. ;;;; Coderefs
  1959. (ert-deftest test-org/coderef ()
  1960. "Test coderef links specifications."
  1961. (should
  1962. (org-test-with-temp-text "
  1963. #+BEGIN_SRC emacs-lisp
  1964. \(+ 1 1) (ref:sc)
  1965. #+END_SRC
  1966. \[[(sc)<point>]]"
  1967. (org-open-at-point)
  1968. (looking-at "(ref:sc)")))
  1969. ;; Find coderef even with alternate label format.
  1970. (should
  1971. (org-test-with-temp-text "
  1972. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  1973. \(+ 1 1) {ref:sc}
  1974. #+END_SRC
  1975. \[[(sc)<point>]]"
  1976. (org-open-at-point)
  1977. (looking-at "{ref:sc}"))))
  1978. ;;;; Custom ID
  1979. (ert-deftest test-org/custom-id ()
  1980. "Test custom ID links specifications."
  1981. (should
  1982. (org-test-with-temp-text
  1983. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  1984. (org-open-at-point)
  1985. (looking-at-p "\\* H1")))
  1986. ;; Throw an error on false positives.
  1987. (should-error
  1988. (org-test-with-temp-text
  1989. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  1990. (org-open-at-point)
  1991. (looking-at-p "\\* H1"))))
  1992. ;;;; Fuzzy Links
  1993. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1994. ;; a named element (#+name: text) and to headlines (* Text).
  1995. (ert-deftest test-org/fuzzy-links ()
  1996. "Test fuzzy links specifications."
  1997. ;; Fuzzy link goes in priority to a matching target.
  1998. (should
  1999. (org-test-with-temp-text
  2000. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2001. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2002. (looking-at "<<Test>>")))
  2003. ;; Then fuzzy link points to an element with a given name.
  2004. (should
  2005. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2006. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2007. (looking-at "#\\+NAME: Test")))
  2008. ;; A target still lead to a matching headline otherwise.
  2009. (should
  2010. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2011. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2012. (looking-at "\\* Head2")))
  2013. ;; With a leading star in link, enforce heading match.
  2014. (should
  2015. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2016. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2017. (looking-at "\\* Test")))
  2018. ;; With a leading star in link, enforce exact heading match, even
  2019. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2020. (should-error
  2021. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2022. (let ((org-link-search-must-match-exact-headline nil))
  2023. (org-open-at-point))))
  2024. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2025. (should
  2026. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2027. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2028. (looking-at "\\* Test")))
  2029. (should
  2030. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2031. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2032. (looking-at "\\* Test")))
  2033. ;; Heading match should not care about spaces, cookies, TODO
  2034. ;; keywords, priorities, and tags. However, TODO keywords are
  2035. ;; case-sensitive.
  2036. (should
  2037. (let ((first-line
  2038. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2039. (org-test-with-temp-text
  2040. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2041. (let ((org-link-search-must-match-exact-headline nil)
  2042. (org-todo-regexp "TODO"))
  2043. (org-open-at-point))
  2044. (looking-at (regexp-quote first-line)))))
  2045. (should-error
  2046. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2047. (let ((org-link-search-must-match-exact-headline nil)
  2048. (org-todo-regexp "TODO"))
  2049. (org-open-at-point))))
  2050. ;; Heading match should still be exact.
  2051. (should-error
  2052. (org-test-with-temp-text "
  2053. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2054. Foo Bar
  2055. <point>[[*Test 1]]"
  2056. (let ((org-link-search-must-match-exact-headline nil)
  2057. (org-todo-regexp "TODO"))
  2058. (org-open-at-point))))
  2059. (should
  2060. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2061. (let ((org-link-search-must-match-exact-headline nil)
  2062. (org-todo-regexp "TODO"))
  2063. (org-open-at-point))
  2064. (looking-at-p (regexp-quote "** Test 1 2"))))
  2065. ;; Heading match ignores COMMENT keyword.
  2066. (should
  2067. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2068. (org-open-at-point)
  2069. (looking-at "\\* COMMENT Test")))
  2070. (should
  2071. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2072. (org-open-at-point)
  2073. (looking-at "\\* TODO COMMENT Test")))
  2074. ;; Correctly un-hexify fuzzy links.
  2075. (should
  2076. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2077. (org-open-at-point)
  2078. (bobp)))
  2079. (should
  2080. (org-test-with-temp-text "* [1]\n[[*%5B1%5D<point>]]"
  2081. (org-open-at-point)
  2082. (bobp))))
  2083. ;;;; Link Escaping
  2084. (ert-deftest test-org/org-link-escape-ascii-character ()
  2085. "Escape an ascii character."
  2086. (should
  2087. (string=
  2088. "%5B"
  2089. (org-link-escape "["))))
  2090. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2091. "Escape an ascii control character."
  2092. (should
  2093. (string=
  2094. "%09"
  2095. (org-link-escape "\t"))))
  2096. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2097. "Escape an unicode multibyte character."
  2098. (should
  2099. (string=
  2100. "%E2%82%AC"
  2101. (org-link-escape "€"))))
  2102. (ert-deftest test-org/org-link-escape-custom-table ()
  2103. "Escape string with custom character table."
  2104. (should
  2105. (string=
  2106. "Foo%3A%42ar%0A"
  2107. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2108. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2109. "Escape string with custom table merged with default table."
  2110. (should
  2111. (string=
  2112. "%5BF%6F%6F%3A%42ar%0A%5D"
  2113. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2114. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2115. "Unescape an ascii character."
  2116. (should
  2117. (string=
  2118. "["
  2119. (org-link-unescape "%5B"))))
  2120. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2121. "Unescpae an ascii control character."
  2122. (should
  2123. (string=
  2124. "\n"
  2125. (org-link-unescape "%0A"))))
  2126. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2127. "Unescape unicode multibyte character."
  2128. (should
  2129. (string=
  2130. "€"
  2131. (org-link-unescape "%E2%82%AC"))))
  2132. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2133. "Unescape old style percent escaped character."
  2134. (should
  2135. (string=
  2136. "àâçèéêîôùû"
  2137. (decode-coding-string
  2138. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2139. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2140. "Escape and unescape a URL that includes an escaped char.
  2141. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2142. (should
  2143. (string=
  2144. "http://some.host.com/form?&id=blah%2Bblah25"
  2145. (org-link-unescape
  2146. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2147. ;;;; Open at point
  2148. (ert-deftest test-org/open-at-point-in-keyword ()
  2149. "Does `org-open-at-point' open link in a keyword line?"
  2150. (should
  2151. (org-test-with-temp-text
  2152. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2153. (org-open-at-point) t)))
  2154. (ert-deftest test-org/open-at-point-in-property ()
  2155. "Does `org-open-at-point' open link in property drawer?"
  2156. (should
  2157. (org-test-with-temp-text
  2158. "* Headline
  2159. :PROPERTIES:
  2160. :URL: <point>[[*Headline]]
  2161. :END:"
  2162. (org-open-at-point) t)))
  2163. (ert-deftest test-org/open-at-point-in-comment ()
  2164. "Does `org-open-at-point' open link in a commented line?"
  2165. (should
  2166. (org-test-with-temp-text
  2167. "<<top>>\n# <point>[[top]]"
  2168. (org-open-at-point) t)))
  2169. (ert-deftest test-org/open-at-point/inline-image ()
  2170. "Test `org-open-at-point' on nested links."
  2171. (should
  2172. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2173. (org-open-at-point)
  2174. (bobp))))
  2175. (ert-deftest test-org/open-at-point/radio-target ()
  2176. "Test `org-open-at-point' on radio targets."
  2177. (should
  2178. (org-test-with-temp-text "<<<target>>> <point>target"
  2179. (org-update-radio-target-regexp)
  2180. (org-open-at-point)
  2181. (eq (org-element-type (org-element-context)) 'radio-target))))
  2182. ;;;; Stored links
  2183. (ert-deftest test-org/store-link ()
  2184. "Test `org-store-link' specifications."
  2185. ;; On a headline, link to that headline. Use heading as the
  2186. ;; description of the link.
  2187. (should
  2188. (let (org-store-link-props org-stored-links)
  2189. (org-test-with-temp-text-in-file "* H1"
  2190. (let ((file (buffer-file-name)))
  2191. (equal (format "[[file:%s::*H1][H1]]" file)
  2192. (org-store-link nil))))))
  2193. ;; On a headline, remove any link from description.
  2194. (should
  2195. (let (org-store-link-props org-stored-links)
  2196. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2197. (let ((file (buffer-file-name)))
  2198. (equal (format "[[file:%s::*%s][d]]"
  2199. file
  2200. (org-link-escape "[[#l][d]]"))
  2201. (org-store-link nil))))))
  2202. (should
  2203. (let (org-store-link-props org-stored-links)
  2204. (org-test-with-temp-text-in-file "* [[l]]"
  2205. (let ((file (buffer-file-name)))
  2206. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2207. (org-store-link nil))))))
  2208. (should
  2209. (let (org-store-link-props org-stored-links)
  2210. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2211. (let ((file (buffer-file-name)))
  2212. (equal (format "[[file:%s::*%s][d1 d2]]"
  2213. file
  2214. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2215. (org-store-link nil))))))
  2216. ;; On a named element, link to that element.
  2217. (should
  2218. (let (org-store-link-props org-stored-links)
  2219. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2220. (let ((file (buffer-file-name)))
  2221. (equal (format "[[file:%s::foo][foo]]" file)
  2222. (org-store-link nil)))))))
  2223. ;;; Node Properties
  2224. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2225. "Ensure properties accumulate in subtree drawers."
  2226. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2227. (org-babel-next-src-block)
  2228. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2229. (ert-deftest test-org/custom-properties ()
  2230. "Test custom properties specifications."
  2231. ;; Standard test.
  2232. (should
  2233. (let ((org-custom-properties '("FOO")))
  2234. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2235. (org-toggle-custom-properties-visibility)
  2236. (org-invisible-p2))))
  2237. ;; Properties are case-insensitive.
  2238. (should
  2239. (let ((org-custom-properties '("FOO")))
  2240. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2241. (org-toggle-custom-properties-visibility)
  2242. (org-invisible-p2))))
  2243. (should
  2244. (let ((org-custom-properties '("foo")))
  2245. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2246. (org-toggle-custom-properties-visibility)
  2247. (org-invisible-p2))))
  2248. ;; Multiple custom properties in the same drawer.
  2249. (should
  2250. (let ((org-custom-properties '("FOO" "BAR")))
  2251. (org-test-with-temp-text
  2252. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2253. (org-toggle-custom-properties-visibility)
  2254. (and (org-invisible-p2)
  2255. (not (progn (forward-line) (org-invisible-p2)))
  2256. (progn (forward-line) (org-invisible-p2))))))
  2257. ;; Hide custom properties with an empty value.
  2258. (should
  2259. (let ((org-custom-properties '("FOO")))
  2260. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2261. (org-toggle-custom-properties-visibility)
  2262. (org-invisible-p2))))
  2263. ;; Do not hide fake properties.
  2264. (should-not
  2265. (let ((org-custom-properties '("FOO")))
  2266. (org-test-with-temp-text ":FOO: val\n"
  2267. (org-toggle-custom-properties-visibility)
  2268. (org-invisible-p2))))
  2269. (should-not
  2270. (let ((org-custom-properties '("A")))
  2271. (org-test-with-temp-text
  2272. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2273. (org-toggle-custom-properties-visibility)
  2274. (org-invisible-p2)))))
  2275. ;;; Mark Region
  2276. (ert-deftest test-org/mark-subtree ()
  2277. "Test `org-mark-subtree' specifications."
  2278. ;; Error when point is before first headline.
  2279. (should-error
  2280. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2281. (progn (transient-mark-mode 1)
  2282. (org-mark-subtree))))
  2283. ;; Without argument, mark current subtree.
  2284. (should
  2285. (equal
  2286. '(12 32)
  2287. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2288. (progn (transient-mark-mode 1)
  2289. (forward-line 2)
  2290. (org-mark-subtree)
  2291. (list (region-beginning) (region-end))))))
  2292. ;; With an argument, move ARG up.
  2293. (should
  2294. (equal
  2295. '(1 32)
  2296. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2297. (progn (transient-mark-mode 1)
  2298. (forward-line 2)
  2299. (org-mark-subtree 1)
  2300. (list (region-beginning) (region-end))))))
  2301. ;; Do not get fooled by inlinetasks.
  2302. (when (featurep 'org-inlinetask)
  2303. (should
  2304. (= 1
  2305. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2306. (progn (transient-mark-mode 1)
  2307. (forward-line 1)
  2308. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2309. (region-beginning)))))))
  2310. ;;; Miscellaneous
  2311. (ert-deftest test-org/in-regexp ()
  2312. "Test `org-in-regexp' specifications."
  2313. ;; Standard tests.
  2314. (should
  2315. (org-test-with-temp-text "xx ab<point>c xx"
  2316. (org-in-regexp "abc")))
  2317. (should-not
  2318. (org-test-with-temp-text "xx abc <point>xx"
  2319. (org-in-regexp "abc")))
  2320. ;; Return non-nil even with multiple matching regexps in the same
  2321. ;; line.
  2322. (should
  2323. (org-test-with-temp-text "abc xx ab<point>c xx"
  2324. (org-in-regexp "abc")))
  2325. ;; With optional argument NLINES, check extra lines around point.
  2326. (should-not
  2327. (org-test-with-temp-text "A\nB<point>\nC"
  2328. (org-in-regexp "A\nB\nC")))
  2329. (should
  2330. (org-test-with-temp-text "A\nB<point>\nC"
  2331. (org-in-regexp "A\nB\nC" 1)))
  2332. (should-not
  2333. (org-test-with-temp-text "A\nB\nC<point>"
  2334. (org-in-regexp "A\nB\nC" 1)))
  2335. ;; When optional argument VISUALLY is non-nil, return nil if at
  2336. ;; regexp boundaries.
  2337. (should
  2338. (org-test-with-temp-text "xx abc<point> xx"
  2339. (org-in-regexp "abc")))
  2340. (should-not
  2341. (org-test-with-temp-text "xx abc<point> xx"
  2342. (org-in-regexp "abc" nil t))))
  2343. ;;; Navigation
  2344. (ert-deftest test-org/end-of-meta-data ()
  2345. "Test `org-end-of-meta-data' specifications."
  2346. ;; Skip planning line.
  2347. (should
  2348. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  2349. (org-end-of-meta-data)
  2350. (eobp)))
  2351. ;; Skip properties drawer.
  2352. (should
  2353. (org-test-with-temp-text
  2354. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  2355. (org-end-of-meta-data)
  2356. (eobp)))
  2357. ;; Skip both.
  2358. (should
  2359. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  2360. (org-end-of-meta-data)
  2361. (eobp)))
  2362. ;; Nothing to skip, go to first line.
  2363. (should
  2364. (org-test-with-temp-text "* Headline\nContents"
  2365. (org-end-of-meta-data)
  2366. (looking-at "Contents")))
  2367. ;; With option argument, skip empty lines, regular drawers and
  2368. ;; clocking lines.
  2369. (should
  2370. (org-test-with-temp-text "* Headline\n\nContents"
  2371. (org-end-of-meta-data t)
  2372. (looking-at "Contents")))
  2373. (should
  2374. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  2375. (org-end-of-meta-data t)
  2376. (looking-at "Contents")))
  2377. (should
  2378. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  2379. (org-end-of-meta-data t)
  2380. (looking-at "Contents")))
  2381. ;; Special case: do not skip incomplete drawers.
  2382. (should
  2383. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  2384. (org-end-of-meta-data t)
  2385. (looking-at ":LOGBOOK:")))
  2386. ;; Special case: Be careful about consecutive headlines.
  2387. (should-not
  2388. (org-test-with-temp-text "* H1\n*H2\nContents"
  2389. (org-end-of-meta-data t)
  2390. (looking-at "Contents"))))
  2391. (ert-deftest test-org/beginning-of-line ()
  2392. "Test `org-beginning-of-line' specifications."
  2393. ;; Move to beginning of line. If current line in invisible, move to
  2394. ;; beginning of visible line instead.
  2395. (should
  2396. (org-test-with-temp-text "Some text\nSome other text<point>"
  2397. (org-beginning-of-line)
  2398. (bolp)))
  2399. (should
  2400. (org-test-with-temp-text "* H1\n** H2<point>"
  2401. (org-overview)
  2402. (org-beginning-of-line)
  2403. (= (line-beginning-position) 1)))
  2404. ;; With `visual-line-mode' active, move to beginning of visual line.
  2405. (should-not
  2406. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2407. (visual-line-mode)
  2408. (dotimes (i 1000) (insert "very "))
  2409. (org-beginning-of-line)
  2410. (bolp)))
  2411. ;; In a wide headline, with `visual-line-mode', prefer going to the
  2412. ;; beginning of a visual line than to the logical beginning of line,
  2413. ;; even if special movement is active.
  2414. (should-not
  2415. (org-test-with-temp-text "* A <point>long headline"
  2416. (visual-line-mode)
  2417. (dotimes (i 1000) (insert "very "))
  2418. (goto-char (point-max))
  2419. (org-beginning-of-line)
  2420. (bobp)))
  2421. (should-not
  2422. (org-test-with-temp-text "* A <point>long headline"
  2423. (visual-line-mode)
  2424. (dotimes (i 1000) (insert "very "))
  2425. (goto-char (point-max))
  2426. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  2427. (bobp)))
  2428. ;; At an headline with special movement, first move at beginning of
  2429. ;; title, then at the beginning of line, rinse, repeat.
  2430. (should
  2431. (org-test-with-temp-text "* TODO Headline<point>"
  2432. (let ((org-special-ctrl-a/e t))
  2433. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  2434. (progn (org-beginning-of-line) (bolp))
  2435. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2436. (should
  2437. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2438. (let ((org-special-ctrl-a/e t))
  2439. (org-beginning-of-line)
  2440. (looking-at "Headline"))))
  2441. (should
  2442. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2443. (let ((org-special-ctrl-a/e '(t . nil)))
  2444. (org-beginning-of-line)
  2445. (looking-at "Headline"))))
  2446. (should-not
  2447. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2448. (let ((org-special-ctrl-a/e '(nil . nil)))
  2449. (org-beginning-of-line)
  2450. (looking-at "Headline"))))
  2451. ;; At an headline with reversed movement, first move to beginning of
  2452. ;; line, then to the beginning of title.
  2453. (should
  2454. (org-test-with-temp-text "* TODO Headline<point>"
  2455. (let ((org-special-ctrl-a/e 'reversed)
  2456. (this-command last-command))
  2457. (and (progn (org-beginning-of-line) (bolp))
  2458. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2459. (should
  2460. (org-test-with-temp-text "* TODO Headline<point>"
  2461. (let ((org-special-ctrl-a/e '(reversed . nil))
  2462. (this-command last-command))
  2463. (and (progn (org-beginning-of-line) (bolp))
  2464. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2465. (should-not
  2466. (org-test-with-temp-text "* TODO Headline<point>"
  2467. (let ((org-special-ctrl-a/e '(t . nil))
  2468. (this-command last-command))
  2469. (and (progn (org-beginning-of-line) (bolp))
  2470. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2471. ;; At an item with special movement, first move after to beginning
  2472. ;; of title, then to the beginning of line, rinse, repeat.
  2473. (should
  2474. (org-test-with-temp-text "- [ ] Item<point>"
  2475. (let ((org-special-ctrl-a/e t))
  2476. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  2477. (progn (org-beginning-of-line) (bolp))
  2478. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  2479. ;; At an item with reversed movement, first move to beginning of
  2480. ;; line, then to the beginning of title.
  2481. (should
  2482. (org-test-with-temp-text "- [X] Item<point>"
  2483. (let ((org-special-ctrl-a/e 'reversed)
  2484. (this-command last-command))
  2485. (and (progn (org-beginning-of-line) (bolp))
  2486. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  2487. ;; Leave point before invisible characters at column 0.
  2488. (should
  2489. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  2490. (let ((org-special-ctrl-a/e nil))
  2491. (org-beginning-of-line)
  2492. (bolp))))
  2493. (should
  2494. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  2495. (let ((org-special-ctrl-a/e t))
  2496. (org-beginning-of-line)
  2497. (bolp))))
  2498. (should
  2499. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  2500. (visual-line-mode)
  2501. (org-beginning-of-line)
  2502. (bolp)))
  2503. ;; Special case: Do not error when the buffer contains only a single
  2504. ;; asterisk.
  2505. (should
  2506. (org-test-with-temp-text "*<point>"
  2507. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  2508. (should
  2509. (org-test-with-temp-text "*<point>"
  2510. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  2511. (ert-deftest test-org/end-of-line ()
  2512. "Test `org-end-of-line' specifications."
  2513. ;; Standard test.
  2514. (should
  2515. (org-test-with-temp-text "Some text\nSome other text"
  2516. (org-end-of-line)
  2517. (eolp)))
  2518. ;; With `visual-line-mode' active, move to end of visible line.
  2519. ;; However, never go past ellipsis.
  2520. (should-not
  2521. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2522. (visual-line-mode)
  2523. (dotimes (i 1000) (insert "very "))
  2524. (goto-char (point-min))
  2525. (org-end-of-line)
  2526. (eolp)))
  2527. (should-not
  2528. (org-test-with-temp-text "* A short headline\nSome contents"
  2529. (visual-line-mode)
  2530. (org-overview)
  2531. (org-end-of-line)
  2532. (eobp)))
  2533. ;; In a wide headline, with `visual-line-mode', prefer going to end
  2534. ;; of visible line if tags, or end of line, are farther.
  2535. (should-not
  2536. (org-test-with-temp-text "* A <point>long headline"
  2537. (visual-line-mode)
  2538. (dotimes (i 1000) (insert "very "))
  2539. (goto-char (point-min))
  2540. (org-end-of-line)
  2541. (eolp)))
  2542. (should-not
  2543. (org-test-with-temp-text "* A <point>long headline :tag:"
  2544. (visual-line-mode)
  2545. (dotimes (i 1000) (insert "very "))
  2546. (goto-char (point-min))
  2547. (org-end-of-line)
  2548. (eolp)))
  2549. ;; At an headline without special movement, go to end of line.
  2550. ;; However, never go past ellipsis.
  2551. (should
  2552. (org-test-with-temp-text "* Headline2b :tag:\n"
  2553. (let ((org-special-ctrl-a/e nil))
  2554. (and (progn (org-end-of-line) (eolp))
  2555. (progn (org-end-of-line) (eolp))))))
  2556. (should
  2557. (org-test-with-temp-text "* Headline2b :tag:\n"
  2558. (let ((org-special-ctrl-a/e '(t . nil)))
  2559. (and (progn (org-end-of-line) (eolp))
  2560. (progn (org-end-of-line) (eolp))))))
  2561. (should
  2562. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2563. (org-overview)
  2564. (let ((org-special-ctrl-a/e nil))
  2565. (org-end-of-line)
  2566. (= 1 (line-beginning-position)))))
  2567. ;; At an headline with special movement, first move before tags,
  2568. ;; then at the end of line, rinse, repeat. However, never go past
  2569. ;; ellipsis.
  2570. (should
  2571. (org-test-with-temp-text "* Headline1 :tag:\n"
  2572. (let ((org-special-ctrl-a/e t))
  2573. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  2574. (progn (org-end-of-line) (eolp))
  2575. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2576. (should
  2577. (org-test-with-temp-text "* Headline1 :tag:\n"
  2578. (let ((org-special-ctrl-a/e '(nil . t)))
  2579. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  2580. (progn (org-end-of-line) (eolp))
  2581. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2582. (should-not
  2583. (org-test-with-temp-text "* Headline1 :tag:\n"
  2584. (let ((org-special-ctrl-a/e '(nil . nil)))
  2585. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  2586. (progn (org-end-of-line) (eolp))
  2587. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2588. (should
  2589. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2590. (org-overview)
  2591. (let ((org-special-ctrl-a/e t))
  2592. (org-end-of-line)
  2593. (org-end-of-line)
  2594. (= 1 (line-beginning-position)))))
  2595. ;; At an headline, with reversed movement, first go to end of line,
  2596. ;; then before tags. However, never go past ellipsis.
  2597. (should
  2598. (org-test-with-temp-text "* Headline3 :tag:\n"
  2599. (let ((org-special-ctrl-a/e 'reversed)
  2600. (this-command last-command))
  2601. (and (progn (org-end-of-line) (eolp))
  2602. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2603. (should
  2604. (org-test-with-temp-text "* Headline3 :tag:\n"
  2605. (let ((org-special-ctrl-a/e '(nil . reversed))
  2606. (this-command last-command))
  2607. (and (progn (org-end-of-line) (eolp))
  2608. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2609. (should-not
  2610. (org-test-with-temp-text "* Headline3 :tag:\n"
  2611. (let ((org-special-ctrl-a/e '(nil . t))
  2612. (this-command last-command))
  2613. (and (progn (org-end-of-line) (eolp))
  2614. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2615. (should
  2616. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2617. (org-overview)
  2618. (let ((org-special-ctrl-a/e 'reversed))
  2619. (org-end-of-line)
  2620. (= 1 (line-beginning-position)))))
  2621. ;; At a block without hidden contents.
  2622. (should
  2623. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2624. (progn (org-end-of-line) (eolp))))
  2625. ;; At a block with hidden contents.
  2626. (should-not
  2627. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2628. (let ((org-special-ctrl-a/e t))
  2629. (org-hide-block-toggle)
  2630. (org-end-of-line)
  2631. (eobp))))
  2632. ;; Get past invisible characters at the end of line.
  2633. (should
  2634. (org-test-with-temp-text "[[http://orgmode.org]]"
  2635. (org-end-of-line)
  2636. (eolp))))
  2637. (ert-deftest test-org/open-line ()
  2638. "Test `org-open-line' specifications."
  2639. ;; Call `open-line' outside of tables.
  2640. (should
  2641. (equal "\nText"
  2642. (org-test-with-temp-text "Text"
  2643. (org-open-line 1)
  2644. (buffer-string))))
  2645. ;; At a table, create a row above.
  2646. (should
  2647. (equal "\n| |\n| a |"
  2648. (org-test-with-temp-text "\n<point>| a |"
  2649. (org-open-line 1)
  2650. (buffer-string))))
  2651. ;; At the very first character of the buffer, also call `open-line'.
  2652. (should
  2653. (equal "\n| a |"
  2654. (org-test-with-temp-text "| a |"
  2655. (org-open-line 1)
  2656. (buffer-string))))
  2657. ;; Narrowing does not count.
  2658. (should
  2659. (equal "Text\n| |\n| a |"
  2660. (org-test-with-temp-text "Text\n<point>| a |"
  2661. (narrow-to-region (point) (point-max))
  2662. (org-open-line 1)
  2663. (widen)
  2664. (buffer-string)))))
  2665. (ert-deftest test-org/forward-sentence ()
  2666. "Test `org-forward-sentence' specifications."
  2667. ;; At the end of a table cell, move to the end of the next one.
  2668. (should
  2669. (org-test-with-temp-text "| a<point> | b |"
  2670. (org-forward-sentence)
  2671. (looking-at " |$")))
  2672. ;; Elsewhere in a cell, move to its end.
  2673. (should
  2674. (org-test-with-temp-text "| a<point>c | b |"
  2675. (org-forward-sentence)
  2676. (looking-at " | b |$")))
  2677. ;; Otherwise, simply call `forward-sentence'.
  2678. (should
  2679. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2680. (org-forward-sentence)
  2681. (looking-at " Sentence 2.")))
  2682. (should
  2683. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2684. (org-forward-sentence)
  2685. (org-forward-sentence)
  2686. (eobp)))
  2687. ;; At the end of an element, jump to the next one, without stopping
  2688. ;; on blank lines in-between.
  2689. (should
  2690. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  2691. (org-forward-sentence)
  2692. (eobp))))
  2693. (ert-deftest test-org/backward-sentence ()
  2694. "Test `org-backward-sentence' specifications."
  2695. ;; At the beginning of a table cell, move to the beginning of the
  2696. ;; previous one.
  2697. (should
  2698. (org-test-with-temp-text "| a | <point>b |"
  2699. (org-backward-sentence)
  2700. (looking-at "a | b |$")))
  2701. ;; Elsewhere in a cell, move to its beginning.
  2702. (should
  2703. (org-test-with-temp-text "| a | b<point>c |"
  2704. (org-backward-sentence)
  2705. (looking-at "bc |$")))
  2706. ;; Otherwise, simply call `backward-sentence'.
  2707. (should
  2708. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2709. (org-backward-sentence)
  2710. (looking-at "Sentence 2.")))
  2711. (should
  2712. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2713. (org-backward-sentence)
  2714. (org-backward-sentence)
  2715. (bobp)))
  2716. ;; Make sure to hit the beginning of a sentence on the same line as
  2717. ;; an item.
  2718. (should
  2719. (org-test-with-temp-text "- Line 1\n line <point>2."
  2720. (org-backward-sentence)
  2721. (looking-at "Line 1"))))
  2722. (ert-deftest test-org/forward-paragraph ()
  2723. "Test `org-forward-paragraph' specifications."
  2724. ;; At end of buffer, return an error.
  2725. (should-error
  2726. (org-test-with-temp-text "Paragraph"
  2727. (goto-char (point-max))
  2728. (org-forward-paragraph)))
  2729. ;; Standard test.
  2730. (should
  2731. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2732. (org-forward-paragraph)
  2733. (looking-at "P2")))
  2734. ;; Ignore depth.
  2735. (should
  2736. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  2737. (org-forward-paragraph)
  2738. (looking-at "P1")))
  2739. ;; Do not enter elements with invisible contents.
  2740. (should
  2741. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  2742. (org-hide-block-toggle)
  2743. (org-forward-paragraph)
  2744. (looking-at "P3")))
  2745. ;; On an affiliated keyword, jump to the beginning of the element.
  2746. (should
  2747. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  2748. (org-forward-paragraph)
  2749. (looking-at "Para")))
  2750. ;; On an item or a footnote definition, move to the second element
  2751. ;; inside, if any.
  2752. (should
  2753. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  2754. (org-forward-paragraph)
  2755. (looking-at " Paragraph")))
  2756. (should
  2757. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  2758. (org-forward-paragraph)
  2759. (looking-at "Paragraph")))
  2760. ;; On an item, or a footnote definition, when the first line is
  2761. ;; empty, move to the first item.
  2762. (should
  2763. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  2764. (org-forward-paragraph)
  2765. (looking-at " Paragraph")))
  2766. (should
  2767. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  2768. (org-forward-paragraph)
  2769. (looking-at "Paragraph")))
  2770. ;; On a table (resp. a property drawer) do not move through table
  2771. ;; rows (resp. node properties).
  2772. (should
  2773. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  2774. (org-forward-paragraph)
  2775. (looking-at "Paragraph")))
  2776. (should
  2777. (org-test-with-temp-text
  2778. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  2779. (org-forward-paragraph)
  2780. (looking-at "Paragraph")))
  2781. ;; On a verse or source block, stop after blank lines.
  2782. (should
  2783. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  2784. (org-forward-paragraph)
  2785. (looking-at "L2")))
  2786. (should
  2787. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  2788. (org-forward-paragraph)
  2789. (looking-at "L2"))))
  2790. (ert-deftest test-org/backward-paragraph ()
  2791. "Test `org-backward-paragraph' specifications."
  2792. ;; Error at beginning of buffer.
  2793. (should-error
  2794. (org-test-with-temp-text "Paragraph"
  2795. (org-backward-paragraph)))
  2796. ;; Regular test.
  2797. (should
  2798. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2799. (goto-char (point-max))
  2800. (org-backward-paragraph)
  2801. (looking-at "P3")))
  2802. (should
  2803. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2804. (goto-char (point-max))
  2805. (beginning-of-line)
  2806. (org-backward-paragraph)
  2807. (looking-at "P2")))
  2808. ;; Ignore depth.
  2809. (should
  2810. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  2811. (goto-char (point-max))
  2812. (beginning-of-line)
  2813. (org-backward-paragraph)
  2814. (looking-at "P2")))
  2815. ;; Ignore invisible elements.
  2816. (should
  2817. (org-test-with-temp-text "* H1\n P1\n* H2"
  2818. (org-cycle)
  2819. (goto-char (point-max))
  2820. (beginning-of-line)
  2821. (org-backward-paragraph)
  2822. (bobp)))
  2823. ;; On an affiliated keyword, jump to the first one.
  2824. (should
  2825. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  2826. (search-forward "c2")
  2827. (org-backward-paragraph)
  2828. (looking-at "#\\+name")))
  2829. ;; On the second element in an item or a footnote definition, jump
  2830. ;; to item or the definition.
  2831. (should
  2832. (org-test-with-temp-text "- line1\n\n line2"
  2833. (goto-char (point-max))
  2834. (beginning-of-line)
  2835. (org-backward-paragraph)
  2836. (looking-at "- line1")))
  2837. (should
  2838. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  2839. (goto-char (point-max))
  2840. (beginning-of-line)
  2841. (org-backward-paragraph)
  2842. (looking-at "\\[fn:1\\] line1")))
  2843. ;; On a table (resp. a property drawer), ignore table rows
  2844. ;; (resp. node properties).
  2845. (should
  2846. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  2847. (goto-char (point-max))
  2848. (beginning-of-line)
  2849. (org-backward-paragraph)
  2850. (bobp)))
  2851. (should
  2852. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  2853. (org-backward-paragraph)
  2854. (looking-at ":PROPERTIES:")))
  2855. ;; On a source or verse block, stop before blank lines.
  2856. (should
  2857. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  2858. (search-forward "L3")
  2859. (beginning-of-line)
  2860. (org-backward-paragraph)
  2861. (looking-at "L2")))
  2862. (should
  2863. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  2864. (search-forward "L3")
  2865. (beginning-of-line)
  2866. (org-backward-paragraph)
  2867. (looking-at "L2"))))
  2868. (ert-deftest test-org/forward-element ()
  2869. "Test `org-forward-element' specifications."
  2870. ;; 1. At EOB: should error.
  2871. (org-test-with-temp-text "Some text\n"
  2872. (goto-char (point-max))
  2873. (should-error (org-forward-element)))
  2874. ;; 2. Standard move: expected to ignore blank lines.
  2875. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  2876. (org-forward-element)
  2877. (should (looking-at (regexp-quote "Second paragraph."))))
  2878. ;; 3. Headline tests.
  2879. (org-test-with-temp-text "
  2880. * Head 1
  2881. ** Head 1.1
  2882. *** Head 1.1.1
  2883. ** Head 1.2"
  2884. ;; 3.1. At an headline beginning: move to next headline at the
  2885. ;; same level.
  2886. (goto-line 3)
  2887. (org-forward-element)
  2888. (should (looking-at (regexp-quote "** Head 1.2")))
  2889. ;; 3.2. At an headline beginning: move to parent headline if no
  2890. ;; headline at the same level.
  2891. (goto-line 3)
  2892. (org-forward-element)
  2893. (should (looking-at (regexp-quote "** Head 1.2"))))
  2894. ;; 4. Greater element tests.
  2895. (org-test-with-temp-text
  2896. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  2897. ;; 4.1. At a greater element: expected to skip contents.
  2898. (org-forward-element)
  2899. (should (looking-at (regexp-quote "Outside.")))
  2900. ;; 4.2. At the end of greater element contents: expected to skip
  2901. ;; to the end of the greater element.
  2902. (goto-line 2)
  2903. (org-forward-element)
  2904. (should (looking-at (regexp-quote "Outside."))))
  2905. ;; 5. List tests.
  2906. (org-test-with-temp-text "
  2907. - item1
  2908. - sub1
  2909. - sub2
  2910. - sub3
  2911. Inner paragraph.
  2912. - item2
  2913. Outside."
  2914. ;; 5.1. At list top point: expected to move to the element after
  2915. ;; the list.
  2916. (goto-line 2)
  2917. (org-forward-element)
  2918. (should (looking-at (regexp-quote "Outside.")))
  2919. ;; 5.2. Special case: at the first line of a sub-list, but not at
  2920. ;; beginning of line, move to next item.
  2921. (goto-line 2)
  2922. (forward-char)
  2923. (org-forward-element)
  2924. (should (looking-at "- item2"))
  2925. (goto-line 4)
  2926. (forward-char)
  2927. (org-forward-element)
  2928. (should (looking-at " - sub2"))
  2929. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  2930. (goto-line 4)
  2931. (org-forward-element)
  2932. (should (looking-at (regexp-quote " Inner paragraph.")))
  2933. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  2934. (goto-line 8)
  2935. (org-forward-element)
  2936. (should (looking-at (regexp-quote " Inner paragraph.")))
  2937. ;; 5.5. At an item: expected to move to next item, if any.
  2938. (goto-line 6)
  2939. (org-forward-element)
  2940. (should (looking-at " - sub3"))))
  2941. (ert-deftest test-org/backward-element ()
  2942. "Test `org-backward-element' specifications."
  2943. ;; 1. Should error at BOB.
  2944. (org-test-with-temp-text " \nParagraph."
  2945. (should-error (org-backward-element)))
  2946. ;; 2. Should move at BOB when called on the first element in buffer.
  2947. (should
  2948. (org-test-with-temp-text "\n#+TITLE: test"
  2949. (progn (forward-line)
  2950. (org-backward-element)
  2951. (bobp))))
  2952. ;; 3. Not at the beginning of an element: move at its beginning.
  2953. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2954. (goto-line 3)
  2955. (end-of-line)
  2956. (org-backward-element)
  2957. (should (looking-at (regexp-quote "Paragraph2."))))
  2958. ;; 4. Headline tests.
  2959. (org-test-with-temp-text "
  2960. * Head 1
  2961. ** Head 1.1
  2962. *** Head 1.1.1
  2963. ** Head 1.2"
  2964. ;; 4.1. At an headline beginning: move to previous headline at the
  2965. ;; same level.
  2966. (goto-line 5)
  2967. (org-backward-element)
  2968. (should (looking-at (regexp-quote "** Head 1.1")))
  2969. ;; 4.2. At an headline beginning: move to parent headline if no
  2970. ;; headline at the same level.
  2971. (goto-line 3)
  2972. (org-backward-element)
  2973. (should (looking-at (regexp-quote "* Head 1")))
  2974. ;; 4.3. At the first top-level headline: should error.
  2975. (goto-line 2)
  2976. (should-error (org-backward-element)))
  2977. ;; 5. At beginning of first element inside a greater element:
  2978. ;; expected to move to greater element's beginning.
  2979. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  2980. (goto-line 3)
  2981. (org-backward-element)
  2982. (should (looking-at "#\\+BEGIN_CENTER")))
  2983. ;; 6. At the beginning of the first element in a section: should
  2984. ;; move back to headline, if any.
  2985. (should
  2986. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  2987. (progn (goto-char (point-max))
  2988. (beginning-of-line)
  2989. (org-backward-element)
  2990. (org-at-heading-p))))
  2991. ;; 7. List tests.
  2992. (org-test-with-temp-text "
  2993. - item1
  2994. - sub1
  2995. - sub2
  2996. - sub3
  2997. Inner paragraph.
  2998. - item2
  2999. Outside."
  3000. ;; 7.1. At beginning of sub-list: expected to move to the
  3001. ;; paragraph before it.
  3002. (goto-line 4)
  3003. (org-backward-element)
  3004. (should (looking-at "item1"))
  3005. ;; 7.2. At an item in a list: expected to move at previous item.
  3006. (goto-line 8)
  3007. (org-backward-element)
  3008. (should (looking-at " - sub2"))
  3009. (goto-line 12)
  3010. (org-backward-element)
  3011. (should (looking-at "- item1"))
  3012. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3013. ;; beginning.
  3014. (goto-line 10)
  3015. (org-backward-element)
  3016. (should (looking-at " - sub1"))
  3017. (goto-line 15)
  3018. (org-backward-element)
  3019. (should (looking-at "- item1"))
  3020. ;; 7.4. At blank-lines before list end: expected to move to top
  3021. ;; item.
  3022. (goto-line 14)
  3023. (org-backward-element)
  3024. (should (looking-at "- item1"))))
  3025. (ert-deftest test-org/up-element ()
  3026. "Test `org-up-element' specifications."
  3027. ;; 1. At BOB or with no surrounding element: should error.
  3028. (org-test-with-temp-text "Paragraph."
  3029. (should-error (org-up-element)))
  3030. (org-test-with-temp-text "* Head1\n* Head2"
  3031. (goto-line 2)
  3032. (should-error (org-up-element)))
  3033. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3034. (goto-line 3)
  3035. (should-error (org-up-element)))
  3036. ;; 2. At an headline: move to parent headline.
  3037. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3038. (goto-line 3)
  3039. (org-up-element)
  3040. (should (looking-at "\\* Head1")))
  3041. ;; 3. Inside a greater element: move to greater element beginning.
  3042. (org-test-with-temp-text
  3043. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3044. (goto-line 3)
  3045. (org-up-element)
  3046. (should (looking-at "#\\+BEGIN_CENTER")))
  3047. ;; 4. List tests.
  3048. (org-test-with-temp-text "* Top
  3049. - item1
  3050. - sub1
  3051. - sub2
  3052. Paragraph within sub2.
  3053. - item2"
  3054. ;; 4.1. Within an item: move to the item beginning.
  3055. (goto-line 8)
  3056. (org-up-element)
  3057. (should (looking-at " - sub2"))
  3058. ;; 4.2. At an item in a sub-list: move to parent item.
  3059. (goto-line 4)
  3060. (org-up-element)
  3061. (should (looking-at "- item1"))
  3062. ;; 4.3. At an item in top list: move to beginning of whole list.
  3063. (goto-line 10)
  3064. (org-up-element)
  3065. (should (looking-at "- item1"))
  3066. ;; 4.4. Special case. At very top point: should move to parent of
  3067. ;; list.
  3068. (goto-line 2)
  3069. (org-up-element)
  3070. (should (looking-at "\\* Top"))))
  3071. (ert-deftest test-org/down-element ()
  3072. "Test `org-down-element' specifications."
  3073. ;; Error when the element hasn't got a recursive type.
  3074. (org-test-with-temp-text "Paragraph."
  3075. (should-error (org-down-element)))
  3076. ;; Error when the element has no contents
  3077. (org-test-with-temp-text "* Headline"
  3078. (should-error (org-down-element)))
  3079. ;; When at a plain-list, move to first item.
  3080. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3081. (goto-line 2)
  3082. (org-down-element)
  3083. (should (looking-at " - Item 1.1")))
  3084. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3085. (org-down-element)
  3086. (should (looking-at " Item 1")))
  3087. ;; When at a table, move to first row
  3088. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3089. (org-down-element)
  3090. (should (looking-at " a | b |")))
  3091. ;; Otherwise, move inside the greater element.
  3092. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3093. (org-down-element)
  3094. (should (looking-at "Paragraph"))))
  3095. (ert-deftest test-org/drag-element-backward ()
  3096. "Test `org-drag-element-backward' specifications."
  3097. ;; Standard test.
  3098. (should
  3099. (equal
  3100. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3101. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3102. (org-drag-element-backward)
  3103. (buffer-string))))
  3104. (should
  3105. (equal
  3106. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3107. (org-test-with-temp-text
  3108. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3109. (org-drag-element-backward)
  3110. (buffer-string))))
  3111. ;; Preserve blank lines.
  3112. (should
  3113. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3114. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3115. (org-drag-element-backward)
  3116. (buffer-string))))
  3117. ;; Preserve column.
  3118. (should
  3119. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3120. (org-drag-element-backward)
  3121. (looking-at-p "2")))
  3122. ;; Error when trying to move first element of buffer.
  3123. (should-error
  3124. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3125. (org-drag-element-backward))
  3126. :type 'user-error)
  3127. ;; Error when trying to swap nested elements.
  3128. (should-error
  3129. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3130. (forward-line)
  3131. (org-drag-element-backward))
  3132. :type 'user-error)
  3133. ;; Error when trying to swap an headline element and a non-headline
  3134. ;; element.
  3135. (should-error
  3136. (org-test-with-temp-text "Test.\n* Head 1"
  3137. (forward-line)
  3138. (org-drag-element-backward))
  3139. :type 'user-error)
  3140. ;; Error when called before first element.
  3141. (should-error
  3142. (org-test-with-temp-text "\n"
  3143. (forward-line)
  3144. (org-drag-element-backward))
  3145. :type 'user-error)
  3146. ;; Preserve visibility of elements and their contents.
  3147. (should
  3148. (equal '((63 . 82) (26 . 48))
  3149. (org-test-with-temp-text "
  3150. #+BEGIN_CENTER
  3151. Text.
  3152. #+END_CENTER
  3153. - item 1
  3154. #+BEGIN_QUOTE
  3155. Text.
  3156. #+END_QUOTE"
  3157. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3158. (search-backward "- item 1")
  3159. (org-drag-element-backward)
  3160. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3161. (overlays-in (point-min) (point-max)))))))
  3162. (ert-deftest test-org/drag-element-forward ()
  3163. "Test `org-drag-element-forward' specifications."
  3164. ;; 1. Error when trying to move first element of buffer.
  3165. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3166. (goto-line 3)
  3167. (should-error (org-drag-element-forward)))
  3168. ;; 2. Error when trying to swap nested elements.
  3169. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3170. (forward-line)
  3171. (should-error (org-drag-element-forward)))
  3172. ;; 3. Error when trying to swap a non-headline element and an
  3173. ;; headline.
  3174. (org-test-with-temp-text "Test.\n* Head 1"
  3175. (should-error (org-drag-element-forward)))
  3176. ;; 4. Error when called before first element.
  3177. (should-error
  3178. (org-test-with-temp-text "\n"
  3179. (forward-line)
  3180. (org-drag-element-backward))
  3181. :type 'user-error)
  3182. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3183. ;; between elements.
  3184. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3185. (search-forward "graph")
  3186. (org-drag-element-forward)
  3187. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3188. (should (looking-at " 1")))
  3189. ;; 5. Preserve visibility of elements and their contents.
  3190. (org-test-with-temp-text "
  3191. #+BEGIN_CENTER
  3192. Text.
  3193. #+END_CENTER
  3194. - item 1
  3195. #+BEGIN_QUOTE
  3196. Text.
  3197. #+END_QUOTE"
  3198. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3199. (search-backward "#+BEGIN_CENTER")
  3200. (org-drag-element-forward)
  3201. (should
  3202. (equal
  3203. '((63 . 82) (26 . 48))
  3204. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3205. (overlays-in (point-min) (point-max)))))))
  3206. (ert-deftest test-org/next-block ()
  3207. "Test `org-next-block' specifications."
  3208. ;; Regular test.
  3209. (should
  3210. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3211. (org-next-block 1)
  3212. (looking-at "#\\+BEGIN_CENTER")))
  3213. ;; Ignore case.
  3214. (should
  3215. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3216. (let ((case-fold-search nil))
  3217. (org-next-block 1)
  3218. (looking-at "#\\+begin_center"))))
  3219. ;; Ignore current line.
  3220. (should
  3221. (org-test-with-temp-text
  3222. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3223. (org-next-block 1)
  3224. (looking-at "#\\+BEGIN_CENTER")))
  3225. ;; Throw an error when no block is found.
  3226. (should-error
  3227. (org-test-with-temp-text "Paragraph"
  3228. (org-next-block 1)))
  3229. ;; With an argument, skip many blocks at once.
  3230. (should
  3231. (org-test-with-temp-text
  3232. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3233. (org-next-block 2)
  3234. (looking-at "#\\+BEGIN_QUOTE")))
  3235. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3236. (should
  3237. (org-test-with-temp-text
  3238. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3239. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3240. (looking-at "#\\+BEGIN_QUOTE")))
  3241. ;; Optional argument is also case-insensitive.
  3242. (should
  3243. (org-test-with-temp-text
  3244. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3245. (let ((case-fold-search nil))
  3246. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3247. (looking-at "#\\+begin_quote")))))
  3248. (ert-deftest test-org/previous-block ()
  3249. "Test `org-previous-block' specifications."
  3250. ;; Regular test.
  3251. (should
  3252. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  3253. (org-previous-block 1)
  3254. (looking-at "#\\+BEGIN_CENTER")))
  3255. ;; Ignore case.
  3256. (should
  3257. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  3258. (let ((case-fold-search nil))
  3259. (org-previous-block 1)
  3260. (looking-at "#\\+begin_center"))))
  3261. ;; Ignore current line.
  3262. (should
  3263. (org-test-with-temp-text
  3264. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  3265. (org-previous-block 1)
  3266. (looking-at "#\\+BEGIN_QUOTE")))
  3267. ;; Throw an error when no block is found.
  3268. (should-error
  3269. (org-test-with-temp-text "Paragraph<point>"
  3270. (org-previous-block 1)))
  3271. ;; With an argument, skip many blocks at once.
  3272. (should
  3273. (org-test-with-temp-text
  3274. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3275. (org-previous-block 2)
  3276. (looking-at "#\\+BEGIN_CENTER")))
  3277. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3278. (should
  3279. (org-test-with-temp-text
  3280. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3281. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3282. (looking-at "#\\+BEGIN_QUOTE")))
  3283. ;; Optional argument is also case-insensitive.
  3284. (should
  3285. (org-test-with-temp-text
  3286. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  3287. (let ((case-fold-search nil))
  3288. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3289. (looking-at "#\\+begin_quote")))))
  3290. ;;; Outline structure
  3291. (ert-deftest test-org/demote ()
  3292. "Test `org-demote' specifications."
  3293. ;; Add correct number of stars according to `org-odd-levels-only'.
  3294. (should
  3295. (= 2
  3296. (org-test-with-temp-text "* H"
  3297. (let ((org-odd-levels-only nil)) (org-demote))
  3298. (org-current-level))))
  3299. (should
  3300. (= 3
  3301. (org-test-with-temp-text "* H"
  3302. (let ((org-odd-levels-only t)) (org-demote))
  3303. (org-current-level))))
  3304. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3305. (should
  3306. (org-test-with-temp-text "* H :tag:"
  3307. (let ((org-tags-column 10)
  3308. (org-auto-align-tags t)
  3309. (org-odd-levels-only nil))
  3310. (org-demote))
  3311. (org-move-to-column 10)
  3312. (looking-at-p ":tag:$")))
  3313. (should-not
  3314. (org-test-with-temp-text "* H :tag:"
  3315. (let ((org-tags-column 10)
  3316. (org-auto-align-tags nil)
  3317. (org-odd-levels-only nil))
  3318. (org-demote))
  3319. (org-move-to-column 10)
  3320. (looking-at-p ":tag:$")))
  3321. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3322. ;; info and property drawers accordingly.
  3323. (should
  3324. (= 3
  3325. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3326. (let ((org-odd-levels-only nil)
  3327. (org-adapt-indentation t))
  3328. (org-demote))
  3329. (forward-line)
  3330. (org-get-indentation))))
  3331. (should
  3332. (= 3
  3333. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3334. (let ((org-odd-levels-only nil)
  3335. (org-adapt-indentation t))
  3336. (org-demote))
  3337. (forward-line)
  3338. (org-get-indentation))))
  3339. (should-not
  3340. (= 3
  3341. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3342. (let ((org-odd-levels-only nil)
  3343. (org-adapt-indentation nil))
  3344. (org-demote))
  3345. (forward-line)
  3346. (org-get-indentation))))
  3347. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3348. ;; section accordingly. Ignore, however, footnote definitions and
  3349. ;; inlinetasks boundaries.
  3350. (should
  3351. (= 3
  3352. (org-test-with-temp-text "* H\n Paragraph"
  3353. (let ((org-odd-levels-only nil)
  3354. (org-adapt-indentation t))
  3355. (org-demote))
  3356. (forward-line)
  3357. (org-get-indentation))))
  3358. (should
  3359. (= 2
  3360. (org-test-with-temp-text "* H\n Paragraph"
  3361. (let ((org-odd-levels-only nil)
  3362. (org-adapt-indentation nil))
  3363. (org-demote))
  3364. (forward-line)
  3365. (org-get-indentation))))
  3366. (should
  3367. (zerop
  3368. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  3369. (let ((org-odd-levels-only nil)
  3370. (org-adapt-indentation t))
  3371. (org-demote))
  3372. (goto-char (point-max))
  3373. (org-get-indentation))))
  3374. (should
  3375. (= 3
  3376. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  3377. (let ((org-odd-levels-only nil)
  3378. (org-adapt-indentation t))
  3379. (org-demote))
  3380. (goto-char (point-max))
  3381. (org-get-indentation))))
  3382. (when (featurep 'org-inlinetask)
  3383. (should
  3384. (zerop
  3385. (let ((org-inlinetask-min-level 5)
  3386. (org-adapt-indentation t))
  3387. (org-test-with-temp-text "* H\n***** I\n***** END"
  3388. (org-demote)
  3389. (forward-line)
  3390. (org-get-indentation))))))
  3391. (when (featurep 'org-inlinetask)
  3392. (should
  3393. (= 3
  3394. (let ((org-inlinetask-min-level 5)
  3395. (org-adapt-indentation t))
  3396. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  3397. (org-demote)
  3398. (forward-line 2)
  3399. (org-get-indentation))))))
  3400. ;; Ignore contents of source blocks or example blocks when
  3401. ;; indentation should be preserved (through
  3402. ;; `org-src-preserve-indentation' or "-i" flag).
  3403. (should-not
  3404. (zerop
  3405. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3406. (let ((org-adapt-indentation t)
  3407. (org-src-preserve-indentation nil))
  3408. (org-demote))
  3409. (forward-line 2)
  3410. (org-get-indentation))))
  3411. (should
  3412. (zerop
  3413. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3414. (let ((org-adapt-indentation t)
  3415. (org-src-preserve-indentation t))
  3416. (org-demote))
  3417. (forward-line 2)
  3418. (org-get-indentation))))
  3419. (should
  3420. (zerop
  3421. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3422. (let ((org-adapt-indentation t)
  3423. (org-src-preserve-indentation t))
  3424. (org-demote))
  3425. (forward-line 2)
  3426. (org-get-indentation))))
  3427. (should
  3428. (zerop
  3429. (org-test-with-temp-text
  3430. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  3431. (let ((org-adapt-indentation t)
  3432. (org-src-preserve-indentation nil))
  3433. (org-demote))
  3434. (forward-line 2)
  3435. (org-get-indentation)))))
  3436. (ert-deftest test-org/promote ()
  3437. "Test `org-promote' specifications."
  3438. ;; Return an error if headline is to be promoted to level 0, unless
  3439. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  3440. ;; headline becomes a comment.
  3441. (should-error
  3442. (org-test-with-temp-text "* H"
  3443. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  3444. (should
  3445. (equal "# H"
  3446. (org-test-with-temp-text "* H"
  3447. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  3448. (buffer-string))))
  3449. ;; Remove correct number of stars according to
  3450. ;; `org-odd-levels-only'.
  3451. (should
  3452. (= 2
  3453. (org-test-with-temp-text "*** H"
  3454. (let ((org-odd-levels-only nil)) (org-promote))
  3455. (org-current-level))))
  3456. (should
  3457. (= 1
  3458. (org-test-with-temp-text "*** H"
  3459. (let ((org-odd-levels-only t)) (org-promote))
  3460. (org-current-level))))
  3461. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3462. (should
  3463. (org-test-with-temp-text "** H :tag:"
  3464. (let ((org-tags-column 10)
  3465. (org-auto-align-tags t)
  3466. (org-odd-levels-only nil))
  3467. (org-promote))
  3468. (org-move-to-column 10)
  3469. (looking-at-p ":tag:$")))
  3470. (should-not
  3471. (org-test-with-temp-text "** H :tag:"
  3472. (let ((org-tags-column 10)
  3473. (org-auto-align-tags nil)
  3474. (org-odd-levels-only nil))
  3475. (org-promote))
  3476. (org-move-to-column 10)
  3477. (looking-at-p ":tag:$")))
  3478. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3479. ;; info and property drawers.
  3480. (should
  3481. (= 2
  3482. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3483. (let ((org-odd-levels-only nil)
  3484. (org-adapt-indentation t))
  3485. (org-promote))
  3486. (forward-line)
  3487. (org-get-indentation))))
  3488. (should
  3489. (= 2
  3490. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3491. (let ((org-odd-levels-only nil)
  3492. (org-adapt-indentation t))
  3493. (org-promote))
  3494. (forward-line)
  3495. (org-get-indentation))))
  3496. (should-not
  3497. (= 2
  3498. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3499. (let ((org-odd-levels-only nil)
  3500. (org-adapt-indentation nil))
  3501. (org-promote))
  3502. (forward-line)
  3503. (org-get-indentation))))
  3504. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3505. ;; section accordingly. Ignore, however, footnote definitions and
  3506. ;; inlinetasks boundaries.
  3507. (should
  3508. (= 2
  3509. (org-test-with-temp-text "** H\n Paragraph"
  3510. (let ((org-odd-levels-only nil)
  3511. (org-adapt-indentation t))
  3512. (org-promote))
  3513. (forward-line)
  3514. (org-get-indentation))))
  3515. (should-not
  3516. (= 2
  3517. (org-test-with-temp-text "** H\n Paragraph"
  3518. (let ((org-odd-levels-only nil)
  3519. (org-adapt-indentation nil))
  3520. (org-promote))
  3521. (forward-line)
  3522. (org-get-indentation))))
  3523. (should
  3524. (= 2
  3525. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  3526. (let ((org-odd-levels-only nil)
  3527. (org-adapt-indentation t))
  3528. (org-promote))
  3529. (forward-line)
  3530. (org-get-indentation))))
  3531. (when (featurep 'org-inlinetask)
  3532. (should
  3533. (zerop
  3534. (let ((org-inlinetask-min-level 5)
  3535. (org-adapt-indentation t))
  3536. (org-test-with-temp-text "** H\n***** I\n***** END"
  3537. (org-promote)
  3538. (forward-line)
  3539. (org-get-indentation))))))
  3540. (when (featurep 'org-inlinetask)
  3541. (should
  3542. (= 2
  3543. (let ((org-inlinetask-min-level 5)
  3544. (org-adapt-indentation t))
  3545. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  3546. (org-promote)
  3547. (forward-line 2)
  3548. (org-get-indentation))))))
  3549. ;; Give up shifting if it would break document's structure
  3550. ;; otherwise.
  3551. (should
  3552. (= 3
  3553. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  3554. (let ((org-odd-levels-only nil)
  3555. (org-adapt-indentation t))
  3556. (org-promote))
  3557. (forward-line)
  3558. (org-get-indentation))))
  3559. (should
  3560. (= 3
  3561. (org-test-with-temp-text "** H\n Paragraph\n * list."
  3562. (let ((org-odd-levels-only nil)
  3563. (org-adapt-indentation t))
  3564. (org-promote))
  3565. (forward-line)
  3566. (org-get-indentation))))
  3567. ;; Ignore contents of source blocks or example blocks when
  3568. ;; indentation should be preserved (through
  3569. ;; `org-src-preserve-indentation' or "-i" flag).
  3570. (should-not
  3571. (zerop
  3572. (org-test-with-temp-text
  3573. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3574. (let ((org-adapt-indentation t)
  3575. (org-src-preserve-indentation nil)
  3576. (org-odd-levels-only nil))
  3577. (org-promote))
  3578. (forward-line)
  3579. (org-get-indentation))))
  3580. (should
  3581. (zerop
  3582. (org-test-with-temp-text
  3583. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  3584. (let ((org-adapt-indentation t)
  3585. (org-src-preserve-indentation t)
  3586. (org-odd-levels-only nil))
  3587. (org-promote))
  3588. (forward-line)
  3589. (org-get-indentation))))
  3590. (should
  3591. (zerop
  3592. (org-test-with-temp-text
  3593. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3594. (let ((org-adapt-indentation t)
  3595. (org-src-preserve-indentation t)
  3596. (org-odd-levels-only nil))
  3597. (org-promote))
  3598. (forward-line)
  3599. (org-get-indentation))))
  3600. (should
  3601. (zerop
  3602. (org-test-with-temp-text
  3603. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  3604. (let ((org-adapt-indentation t)
  3605. (org-src-preserve-indentation nil)
  3606. (org-odd-levels-only nil))
  3607. (org-promote))
  3608. (forward-line)
  3609. (org-get-indentation)))))
  3610. ;;; Planning
  3611. (ert-deftest test-org/at-planning-p ()
  3612. "Test `org-at-planning-p' specifications."
  3613. ;; Regular test.
  3614. (should
  3615. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  3616. (org-at-planning-p)))
  3617. (should-not
  3618. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  3619. (org-at-planning-p)))
  3620. ;; Correctly find planning attached to inlinetasks.
  3621. (when (featurep 'org-inlinetask)
  3622. (should
  3623. (org-test-with-temp-text
  3624. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  3625. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3626. (should-not
  3627. (org-test-with-temp-text
  3628. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3629. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3630. (should-not
  3631. (org-test-with-temp-text
  3632. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3633. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3634. (should-not
  3635. (org-test-with-temp-text
  3636. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  3637. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  3638. (ert-deftest test-org/add-planning-info ()
  3639. "Test `org-add-planning-info'."
  3640. ;; Create deadline when `org-adapt-indentation' is non-nil.
  3641. (should
  3642. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3643. (org-test-with-temp-text "* H\nParagraph<point>"
  3644. (let ((org-adapt-indentation t))
  3645. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3646. (replace-regexp-in-string
  3647. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3648. nil nil 1))))
  3649. ;; Create deadline when `org-adapt-indentation' is nil.
  3650. (should
  3651. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3652. (org-test-with-temp-text "* H\nParagraph<point>"
  3653. (let ((org-adapt-indentation nil))
  3654. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3655. (replace-regexp-in-string
  3656. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3657. nil nil 1))))
  3658. ;; Update deadline when `org-adapt-indentation' is non-nil.
  3659. (should
  3660. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3661. (org-test-with-temp-text "\
  3662. * H
  3663. DEADLINE: <2015-06-24 Wed>
  3664. Paragraph<point>"
  3665. (let ((org-adapt-indentation t))
  3666. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3667. (replace-regexp-in-string
  3668. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3669. nil nil 1))))
  3670. ;; Update deadline when `org-adapt-indentation' is nil.
  3671. (should
  3672. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3673. (org-test-with-temp-text "\
  3674. * H
  3675. DEADLINE: <2015-06-24 Wed>
  3676. Paragraph<point>"
  3677. (let ((org-adapt-indentation nil))
  3678. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3679. (replace-regexp-in-string
  3680. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3681. nil nil 1))))
  3682. ;; Schedule when `org-adapt-indentation' is non-nil.
  3683. (should
  3684. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  3685. (org-test-with-temp-text "* H\nParagraph<point>"
  3686. (let ((org-adapt-indentation t))
  3687. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3688. (replace-regexp-in-string
  3689. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3690. nil nil 1))))
  3691. ;; Schedule when `org-adapt-indentation' is nil.
  3692. (should
  3693. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  3694. (org-test-with-temp-text "* H\nParagraph<point>"
  3695. (let ((org-adapt-indentation nil))
  3696. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3697. (replace-regexp-in-string
  3698. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3699. nil nil 1))))
  3700. ;; Add deadline when scheduled.
  3701. (should
  3702. (equal "\
  3703. * H
  3704. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  3705. Paragraph"
  3706. (org-test-with-temp-text "\
  3707. * H
  3708. SCHEDULED: <2015-06-24 Wed>
  3709. Paragraph<point>"
  3710. (let ((org-adapt-indentation t))
  3711. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3712. (replace-regexp-in-string
  3713. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3714. nil nil 1))))
  3715. ;; Remove middle entry.
  3716. (should
  3717. (equal "\
  3718. * H
  3719. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  3720. Paragraph"
  3721. (org-test-with-temp-text "\
  3722. * H
  3723. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3724. Paragraph<point>"
  3725. (let ((org-adapt-indentation t))
  3726. (org-add-planning-info nil nil 'deadline))
  3727. (replace-regexp-in-string
  3728. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3729. nil nil 1))))
  3730. ;; Remove last entry and then middle entry (order should not
  3731. ;; matter).
  3732. (should
  3733. (equal "\
  3734. * H
  3735. CLOSED: [2015-06-24]
  3736. Paragraph"
  3737. (org-test-with-temp-text "\
  3738. * H
  3739. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3740. Paragraph<point>"
  3741. (let ((org-adapt-indentation t))
  3742. (org-add-planning-info nil nil 'scheduled 'deadline))
  3743. (replace-regexp-in-string
  3744. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3745. nil nil 1))))
  3746. ;; Remove closed when `org-adapt-indentation' is non-nil.
  3747. (should
  3748. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3749. (org-test-with-temp-text "\
  3750. * H
  3751. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3752. Paragraph<point>"
  3753. (let ((org-adapt-indentation t))
  3754. (org-add-planning-info nil nil 'closed))
  3755. (replace-regexp-in-string
  3756. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3757. nil nil 1))))
  3758. (should
  3759. (equal "* H\n Paragraph"
  3760. (org-test-with-temp-text "\
  3761. * H
  3762. CLOSED: [2015-06-25 Thu]
  3763. Paragraph<point>"
  3764. (let ((org-adapt-indentation t))
  3765. (org-add-planning-info nil nil 'closed))
  3766. (replace-regexp-in-string
  3767. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3768. nil nil 1))))
  3769. ;; Remove closed when `org-adapt-indentation' is nil.
  3770. (should
  3771. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3772. (org-test-with-temp-text "\
  3773. * H
  3774. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3775. Paragraph<point>"
  3776. (let ((org-adapt-indentation nil))
  3777. (org-add-planning-info nil nil 'closed))
  3778. (replace-regexp-in-string
  3779. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3780. nil nil 1))))
  3781. (should
  3782. (equal "* H\nParagraph"
  3783. (org-test-with-temp-text "\
  3784. * H
  3785. CLOSED: [2015-06-25 Thu]
  3786. Paragraph<point>"
  3787. (let ((org-adapt-indentation nil))
  3788. (org-add-planning-info nil nil 'closed))
  3789. (replace-regexp-in-string
  3790. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3791. nil nil 1))))
  3792. ;; Remove closed entry and delete empty line.
  3793. (should
  3794. (equal "\
  3795. * H
  3796. Paragraph"
  3797. (org-test-with-temp-text "\
  3798. * H
  3799. CLOSED: [2015-06-24 Wed]
  3800. Paragraph<point>"
  3801. (let ((org-adapt-indentation t))
  3802. (org-add-planning-info nil nil 'closed))
  3803. (replace-regexp-in-string
  3804. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3805. nil nil 1))))
  3806. ;; Remove one entry and update another.
  3807. (should
  3808. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3809. (org-test-with-temp-text "\
  3810. * H
  3811. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  3812. Paragraph<point>"
  3813. (let ((org-adapt-indentation t))
  3814. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  3815. (replace-regexp-in-string
  3816. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3817. nil nil 1)))))
  3818. ;;; Property API
  3819. (ert-deftest test-org/buffer-property-keys ()
  3820. "Test `org-buffer-property-keys' specifications."
  3821. ;; Retrieve properties accross siblings.
  3822. (should
  3823. (equal '("A" "B")
  3824. (org-test-with-temp-text "
  3825. * H1
  3826. :PROPERTIES:
  3827. :A: 1
  3828. :END:
  3829. * H2
  3830. :PROPERTIES:
  3831. :B: 1
  3832. :END:"
  3833. (org-buffer-property-keys))))
  3834. ;; Retrieve properties accross children.
  3835. (should
  3836. (equal '("A" "B")
  3837. (org-test-with-temp-text "
  3838. * H1
  3839. :PROPERTIES:
  3840. :A: 1
  3841. :END:
  3842. ** H2
  3843. :PROPERTIES:
  3844. :B: 1
  3845. :END:"
  3846. (org-buffer-property-keys))))
  3847. ;; Retrieve muliple properties in the same drawer.
  3848. (should
  3849. (equal '("A" "B")
  3850. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3851. (org-buffer-property-keys))))
  3852. ;; Ignore extension symbol in property name.
  3853. (should
  3854. (equal '("A")
  3855. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3856. (org-buffer-property-keys))))
  3857. ;; With non-nil COLUMNS, extract property names from columns.
  3858. (should
  3859. (equal '("A" "B")
  3860. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  3861. (org-buffer-property-keys nil nil t))))
  3862. (should
  3863. (equal '("A" "B" "COLUMNS")
  3864. (org-test-with-temp-text
  3865. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  3866. (org-buffer-property-keys nil nil t))))
  3867. ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
  3868. (should
  3869. (equal '("A")
  3870. (org-test-with-temp-text
  3871. "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
  3872. (org-buffer-property-keys nil nil nil t)))))
  3873. (ert-deftest test-org/property-values ()
  3874. "Test `org-property-values' specifications."
  3875. ;; Regular test.
  3876. (should
  3877. (equal '("2" "1")
  3878. (org-test-with-temp-text
  3879. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  3880. (org-property-values "A"))))
  3881. ;; Ignore empty values.
  3882. (should-not
  3883. (org-test-with-temp-text
  3884. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  3885. (org-property-values "A")))
  3886. ;; Take into consideration extended values.
  3887. (should
  3888. (equal '("1 2")
  3889. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3890. (org-property-values "A")))))
  3891. (ert-deftest test-org/find-property ()
  3892. "Test `org-find-property' specifications."
  3893. ;; Regular test.
  3894. (should
  3895. (= 1
  3896. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  3897. (org-find-property "prop"))))
  3898. ;; Ignore false positives.
  3899. (should
  3900. (= 27
  3901. (org-test-with-temp-text
  3902. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  3903. (org-find-property "A"))))
  3904. ;; Return first entry found in buffer.
  3905. (should
  3906. (= 1
  3907. (org-test-with-temp-text
  3908. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3909. (org-find-property "A"))))
  3910. ;; Only search visible part of the buffer.
  3911. (should
  3912. (= 31
  3913. (org-test-with-temp-text
  3914. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3915. (org-narrow-to-subtree)
  3916. (org-find-property "A"))))
  3917. ;; With optional argument, only find entries with a specific value.
  3918. (should-not
  3919. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3920. (org-find-property "A" "2")))
  3921. (should
  3922. (= 31
  3923. (org-test-with-temp-text
  3924. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  3925. (org-find-property "A" "2"))))
  3926. ;; Use "nil" for explicit nil values.
  3927. (should
  3928. (= 31
  3929. (org-test-with-temp-text
  3930. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  3931. (org-find-property "A" "nil")))))
  3932. (ert-deftest test-org/entry-delete ()
  3933. "Test `org-entry-delete' specifications."
  3934. ;; Regular test.
  3935. (should
  3936. (string-match
  3937. " *:PROPERTIES:\n *:B: +2\n *:END:"
  3938. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3939. (org-entry-delete (point) "A")
  3940. (buffer-string))))
  3941. ;; Also remove accumulated properties.
  3942. (should-not
  3943. (string-match
  3944. ":A"
  3945. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  3946. (org-entry-delete (point) "A")
  3947. (buffer-string))))
  3948. ;; When last property is removed, remove the property drawer.
  3949. (should-not
  3950. (string-match
  3951. ":PROPERTIES:"
  3952. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  3953. (org-entry-delete (point) "A")
  3954. (buffer-string))))
  3955. ;; Return a non-nil value when some property was removed.
  3956. (should
  3957. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3958. (org-entry-delete (point) "A")))
  3959. (should-not
  3960. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3961. (org-entry-delete (point) "C"))))
  3962. (ert-deftest test-org/entry-get ()
  3963. "Test `org-entry-get' specifications."
  3964. ;; Regular test.
  3965. (should
  3966. (equal "1"
  3967. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3968. (org-entry-get (point) "A"))))
  3969. ;; Ignore case.
  3970. (should
  3971. (equal "1"
  3972. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3973. (org-entry-get (point) "a"))))
  3974. ;; Handle extended values, both before and after base value.
  3975. (should
  3976. (equal "1 2 3"
  3977. (org-test-with-temp-text
  3978. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3979. (org-entry-get (point) "A"))))
  3980. ;; Empty values are returned as the empty string.
  3981. (should
  3982. (equal ""
  3983. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  3984. (org-entry-get (point) "A"))))
  3985. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  3986. ;; otherwise, return nil.
  3987. (should-not
  3988. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3989. (org-entry-get (point) "A")))
  3990. (should
  3991. (equal "nil"
  3992. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3993. (org-entry-get (point) "A" nil t))))
  3994. ;; Return nil when no property is found, independently on the
  3995. ;; LITERAL-NIL argument.
  3996. (should-not
  3997. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3998. (org-entry-get (point) "B")))
  3999. (should-not
  4000. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4001. (org-entry-get (point) "B" nil t)))
  4002. ;; Handle inheritance, when allowed. Include extended values and
  4003. ;; possibly global values.
  4004. (should
  4005. (equal
  4006. "1"
  4007. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4008. (org-entry-get (point) "A" t))))
  4009. (should
  4010. (equal
  4011. "1"
  4012. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4013. (let ((org-use-property-inheritance t))
  4014. (org-entry-get (point) "A" 'selective)))))
  4015. (should-not
  4016. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4017. (let ((org-use-property-inheritance nil))
  4018. (org-entry-get (point) "A" 'selective))))
  4019. (should
  4020. (equal
  4021. "1 2"
  4022. (org-test-with-temp-text
  4023. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  4024. (org-entry-get (point-max) "A" t))))
  4025. (should
  4026. (equal "1"
  4027. (org-test-with-temp-text
  4028. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  4029. (org-mode-restart)
  4030. (org-entry-get (point-max) "A" t))))
  4031. (should
  4032. (equal "0 1"
  4033. (org-test-with-temp-text
  4034. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  4035. (org-mode-restart)
  4036. (org-entry-get (point-max) "A" t)))))
  4037. (ert-deftest test-org/entry-properties ()
  4038. "Test `org-entry-properties' specifications."
  4039. ;; Get "ITEM" property.
  4040. (should
  4041. (equal "H"
  4042. (org-test-with-temp-text "* TODO H"
  4043. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  4044. (should
  4045. (equal "H"
  4046. (org-test-with-temp-text "* TODO H"
  4047. (cdr (assoc "ITEM" (org-entry-properties))))))
  4048. ;; Get "TODO" property. TODO keywords are case sensitive.
  4049. (should
  4050. (equal "TODO"
  4051. (org-test-with-temp-text "* TODO H"
  4052. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  4053. (should
  4054. (equal "TODO"
  4055. (org-test-with-temp-text "* TODO H"
  4056. (cdr (assoc "TODO" (org-entry-properties))))))
  4057. (should-not
  4058. (org-test-with-temp-text "* H"
  4059. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4060. (should-not
  4061. (org-test-with-temp-text "* todo H"
  4062. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4063. ;; Get "PRIORITY" property.
  4064. (should
  4065. (equal "A"
  4066. (org-test-with-temp-text "* [#A] H"
  4067. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4068. (should
  4069. (equal "A"
  4070. (org-test-with-temp-text "* [#A] H"
  4071. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  4072. (should
  4073. (equal (char-to-string org-default-priority)
  4074. (org-test-with-temp-text "* H"
  4075. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4076. ;; Get "FILE" property.
  4077. (should
  4078. (org-test-with-temp-text-in-file "* H\nParagraph"
  4079. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  4080. (buffer-file-name))))
  4081. (should
  4082. (org-test-with-temp-text-in-file "* H\nParagraph"
  4083. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  4084. (buffer-file-name))))
  4085. (should-not
  4086. (org-test-with-temp-text "* H\nParagraph"
  4087. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  4088. ;; Get "TAGS" property.
  4089. (should
  4090. (equal ":tag1:tag2:"
  4091. (org-test-with-temp-text "* H :tag1:tag2:"
  4092. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  4093. (should
  4094. (equal ":tag1:tag2:"
  4095. (org-test-with-temp-text "* H :tag1:tag2:"
  4096. (cdr (assoc "TAGS" (org-entry-properties))))))
  4097. (should-not
  4098. (org-test-with-temp-text "* H"
  4099. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  4100. ;; Get "ALLTAGS" property.
  4101. (should
  4102. (equal ":tag1:tag2:"
  4103. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4104. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  4105. (should
  4106. (equal ":tag1:tag2:"
  4107. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4108. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  4109. (should-not
  4110. (org-test-with-temp-text "* H"
  4111. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  4112. ;; Get "BLOCKED" property.
  4113. (should
  4114. (equal "t"
  4115. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  4116. (let ((org-enforce-todo-dependencies t)
  4117. (org-blocker-hook
  4118. '(org-block-todo-from-children-or-siblings-or-parent)))
  4119. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4120. (should
  4121. (equal ""
  4122. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  4123. (let ((org-enforce-todo-dependencies t)
  4124. (org-blocker-hook
  4125. '(org-block-todo-from-children-or-siblings-or-parent)))
  4126. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4127. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  4128. (should
  4129. (equal
  4130. "[2012-03-29 thu.]"
  4131. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4132. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  4133. (should
  4134. (equal
  4135. "[2012-03-29 thu.]"
  4136. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4137. (cdr (assoc "CLOSED" (org-entry-properties))))))
  4138. (should-not
  4139. (org-test-with-temp-text "* H"
  4140. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  4141. (should
  4142. (equal
  4143. "<2014-03-04 tue.>"
  4144. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4145. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  4146. (should
  4147. (equal
  4148. "<2014-03-04 tue.>"
  4149. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4150. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  4151. (should-not
  4152. (org-test-with-temp-text "* H"
  4153. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  4154. (should
  4155. (equal
  4156. "<2014-03-04 tue.>"
  4157. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4158. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  4159. (should
  4160. (equal
  4161. "<2014-03-04 tue.>"
  4162. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4163. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  4164. (should-not
  4165. (org-test-with-temp-text "* H"
  4166. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  4167. ;; Get "CATEGORY"
  4168. (should
  4169. (equal "cat"
  4170. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4171. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  4172. (should
  4173. (equal "cat"
  4174. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4175. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4176. (should
  4177. (equal "cat"
  4178. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  4179. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4180. (should
  4181. (equal "cat2"
  4182. (org-test-with-temp-text
  4183. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  4184. "\n"
  4185. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  4186. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4187. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  4188. (should
  4189. (equal "<2012-03-29 thu.>"
  4190. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  4191. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  4192. (should
  4193. (equal "[2012-03-29 thu.]"
  4194. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  4195. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  4196. (should
  4197. (equal "<2012-03-29 thu.>"
  4198. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  4199. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  4200. (should
  4201. (equal "[2014-03-04 tue.]"
  4202. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  4203. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
  4204. ;; Get standard properties.
  4205. (should
  4206. (equal "1"
  4207. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4208. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4209. ;; Handle extended properties.
  4210. (should
  4211. (equal "1 2 3"
  4212. (org-test-with-temp-text
  4213. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  4214. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4215. (should
  4216. (equal "1 2 3"
  4217. (org-test-with-temp-text
  4218. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  4219. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4220. ;; Ignore forbidden (special) properties.
  4221. (should-not
  4222. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  4223. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  4224. (ert-deftest test-org/entry-put ()
  4225. "Test `org-entry-put' specifications."
  4226. ;; Error when not a string or nil.
  4227. (should-error
  4228. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4229. (org-entry-put 1 "test" 2)))
  4230. ;; Error when property name is invalid.
  4231. (should-error
  4232. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4233. (org-entry-put 1 "no space" "value")))
  4234. (should-error
  4235. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4236. (org-entry-put 1 "" "value")))
  4237. ;; Set "TODO" property.
  4238. (should
  4239. (string-match (regexp-quote " TODO H")
  4240. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  4241. (org-entry-put (point) "TODO" "TODO")
  4242. (buffer-string))))
  4243. (should
  4244. (string-match (regexp-quote "* H")
  4245. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  4246. (org-entry-put (point) "TODO" nil)
  4247. (buffer-string))))
  4248. ;; Set "PRIORITY" property.
  4249. (should
  4250. (equal "* [#A] H"
  4251. (org-test-with-temp-text "* [#B] H"
  4252. (org-entry-put (point) "PRIORITY" "A")
  4253. (buffer-string))))
  4254. (should
  4255. (equal "* H"
  4256. (org-test-with-temp-text "* [#B] H"
  4257. (org-entry-put (point) "PRIORITY" nil)
  4258. (buffer-string))))
  4259. ;; Set "SCHEDULED" property.
  4260. (should
  4261. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  4262. (org-test-with-temp-text "* H"
  4263. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  4264. (buffer-string))))
  4265. (should
  4266. (string= "* H\n"
  4267. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4268. (org-entry-put (point) "SCHEDULED" nil)
  4269. (buffer-string))))
  4270. (should
  4271. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  4272. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4273. (org-entry-put (point) "SCHEDULED" "earlier")
  4274. (buffer-string))))
  4275. (should
  4276. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  4277. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4278. (org-entry-put (point) "SCHEDULED" "later")
  4279. (buffer-string))))
  4280. ;; Set "DEADLINE" property.
  4281. (should
  4282. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  4283. (org-test-with-temp-text "* H"
  4284. (org-entry-put (point) "DEADLINE" "2014-03-04")
  4285. (buffer-string))))
  4286. (should
  4287. (string= "* H\n"
  4288. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4289. (org-entry-put (point) "DEADLINE" nil)
  4290. (buffer-string))))
  4291. (should
  4292. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  4293. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4294. (org-entry-put (point) "DEADLINE" "earlier")
  4295. (buffer-string))))
  4296. (should
  4297. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  4298. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4299. (org-entry-put (point) "DEADLINE" "later")
  4300. (buffer-string))))
  4301. ;; Set "CATEGORY" property
  4302. (should
  4303. (string-match "^ *:CATEGORY: cat"
  4304. (org-test-with-temp-text "* H"
  4305. (org-entry-put (point) "CATEGORY" "cat")
  4306. (buffer-string))))
  4307. ;; Regular properties, with or without pre-existing drawer.
  4308. (should
  4309. (string-match "^ *:A: +2$"
  4310. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4311. (org-entry-put (point) "A" "2")
  4312. (buffer-string))))
  4313. (should
  4314. (string-match "^ *:A: +1$"
  4315. (org-test-with-temp-text "* H"
  4316. (org-entry-put (point) "A" "1")
  4317. (buffer-string))))
  4318. ;; Special case: two consecutive headlines.
  4319. (should
  4320. (string-match "\\* A\n *:PROPERTIES:"
  4321. (org-test-with-temp-text "* A\n** B"
  4322. (org-entry-put (point) "A" "1")
  4323. (buffer-string)))))
  4324. ;;; Radio Targets
  4325. (ert-deftest test-org/update-radio-target-regexp ()
  4326. "Test `org-update-radio-target-regexp' specifications."
  4327. ;; Properly update cache with no previous radio target regexp.
  4328. (should
  4329. (eq 'link
  4330. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4331. (save-excursion (goto-char (point-max)) (org-element-context))
  4332. (insert "<<<")
  4333. (search-forward "o")
  4334. (insert ">>>")
  4335. (org-update-radio-target-regexp)
  4336. (goto-char (point-max))
  4337. (org-element-type (org-element-context)))))
  4338. ;; Properly update cache with previous radio target regexp.
  4339. (should
  4340. (eq 'link
  4341. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4342. (save-excursion (goto-char (point-max)) (org-element-context))
  4343. (insert "<<<")
  4344. (search-forward "o")
  4345. (insert ">>>")
  4346. (org-update-radio-target-regexp)
  4347. (search-backward "r")
  4348. (delete-char 5)
  4349. (insert "new")
  4350. (org-update-radio-target-regexp)
  4351. (goto-char (point-max))
  4352. (delete-region (line-beginning-position) (point))
  4353. (insert "new")
  4354. (org-element-type (org-element-context))))))
  4355. ;;; Sparse trees
  4356. (ert-deftest test-org/match-sparse-tree ()
  4357. "Test `org-match-sparse-tree' specifications."
  4358. ;; Match tags.
  4359. (should-not
  4360. (org-test-with-temp-text "* H\n** H1 :tag:"
  4361. (org-match-sparse-tree nil "tag")
  4362. (search-forward "H1")
  4363. (org-invisible-p2)))
  4364. (should
  4365. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  4366. (org-match-sparse-tree nil "tag")
  4367. (search-forward "H2")
  4368. (org-invisible-p2)))
  4369. ;; "-" operator for tags.
  4370. (should-not
  4371. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4372. (org-match-sparse-tree nil "tag1-tag2")
  4373. (search-forward "H1")
  4374. (org-invisible-p2)))
  4375. (should
  4376. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4377. (org-match-sparse-tree nil "tag1-tag2")
  4378. (search-forward "H2")
  4379. (org-invisible-p2)))
  4380. ;; "&" operator for tags.
  4381. (should
  4382. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4383. (org-match-sparse-tree nil "tag1&tag2")
  4384. (search-forward "H1")
  4385. (org-invisible-p2)))
  4386. (should-not
  4387. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4388. (org-match-sparse-tree nil "tag1&tag2")
  4389. (search-forward "H2")
  4390. (org-invisible-p2)))
  4391. ;; "|" operator for tags.
  4392. (should-not
  4393. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4394. (org-match-sparse-tree nil "tag1|tag2")
  4395. (search-forward "H1")
  4396. (org-invisible-p2)))
  4397. (should-not
  4398. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4399. (org-match-sparse-tree nil "tag1|tag2")
  4400. (search-forward "H2")
  4401. (org-invisible-p2)))
  4402. ;; Regexp match on tags.
  4403. (should-not
  4404. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4405. (org-match-sparse-tree nil "{^tag.*}")
  4406. (search-forward "H1")
  4407. (org-invisible-p2)))
  4408. (should
  4409. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4410. (org-match-sparse-tree nil "{^tag.*}")
  4411. (search-forward "H2")
  4412. (org-invisible-p2)))
  4413. ;; Match group tags.
  4414. (should-not
  4415. (org-test-with-temp-text
  4416. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4417. (org-match-sparse-tree nil "work")
  4418. (search-forward "H1")
  4419. (org-invisible-p2)))
  4420. (should-not
  4421. (org-test-with-temp-text
  4422. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4423. (org-match-sparse-tree nil "work")
  4424. (search-forward "H2")
  4425. (org-invisible-p2)))
  4426. ;; Match group tags with hard brackets.
  4427. (should-not
  4428. (org-test-with-temp-text
  4429. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4430. (org-match-sparse-tree nil "work")
  4431. (search-forward "H1")
  4432. (org-invisible-p2)))
  4433. (should-not
  4434. (org-test-with-temp-text
  4435. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4436. (org-match-sparse-tree nil "work")
  4437. (search-forward "H2")
  4438. (org-invisible-p2)))
  4439. ;; Match tags in hierarchies
  4440. (should-not
  4441. (org-test-with-temp-text
  4442. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  4443. #+TAGS: [ Lev_2 : Lev_3 ]\n
  4444. #+TAGS: { Lev_3 : Lev_4 }\n
  4445. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  4446. (org-match-sparse-tree nil "Lev_1")
  4447. (search-forward "H4")
  4448. (org-invisible-p2)))
  4449. ;; Match regular expressions in tags
  4450. (should-not
  4451. (org-test-with-temp-text
  4452. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  4453. (org-match-sparse-tree nil "Lev")
  4454. (search-forward "H1")
  4455. (org-invisible-p2)))
  4456. (should
  4457. (org-test-with-temp-text
  4458. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  4459. (org-match-sparse-tree nil "Lev")
  4460. (search-forward "H1")
  4461. (org-invisible-p2)))
  4462. ;; Match properties.
  4463. (should
  4464. (org-test-with-temp-text
  4465. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  4466. (org-match-sparse-tree nil "A=\"1\"")
  4467. (search-forward "H2")
  4468. (org-invisible-p2)))
  4469. (should-not
  4470. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4471. (org-match-sparse-tree nil "A=\"1\"")
  4472. (search-forward "H2")
  4473. (org-invisible-p2)))
  4474. ;; Case is not significant when matching properties.
  4475. (should-not
  4476. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4477. (org-match-sparse-tree nil "a=\"1\"")
  4478. (search-forward "H2")
  4479. (org-invisible-p2)))
  4480. (should-not
  4481. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  4482. (org-match-sparse-tree nil "A=\"1\"")
  4483. (search-forward "H2")
  4484. (org-invisible-p2)))
  4485. ;; Match special LEVEL property.
  4486. (should-not
  4487. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4488. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4489. (search-forward "H1")
  4490. (org-invisible-p2)))
  4491. (should
  4492. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4493. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4494. (search-forward "H2")
  4495. (org-invisible-p2)))
  4496. ;; Comparison operators when matching properties.
  4497. (should
  4498. (org-test-with-temp-text
  4499. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4500. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4501. (search-forward "H1")
  4502. (org-invisible-p2)))
  4503. (should-not
  4504. (org-test-with-temp-text
  4505. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4506. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4507. (search-forward "H2")
  4508. (org-invisible-p2)))
  4509. ;; Regexp match on properties values.
  4510. (should-not
  4511. (org-test-with-temp-text
  4512. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4513. (org-match-sparse-tree nil "A={f.*}")
  4514. (search-forward "H1")
  4515. (org-invisible-p2)))
  4516. (should
  4517. (org-test-with-temp-text
  4518. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4519. (org-match-sparse-tree nil "A={f.*}")
  4520. (search-forward "H2")
  4521. (org-invisible-p2)))
  4522. ;; With an optional argument, limit match to TODO entries.
  4523. (should-not
  4524. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4525. (org-match-sparse-tree t "tag")
  4526. (search-forward "H1")
  4527. (org-invisible-p2)))
  4528. (should
  4529. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4530. (org-match-sparse-tree t "tag")
  4531. (search-forward "H2")
  4532. (org-invisible-p2))))
  4533. (ert-deftest test-org/occur ()
  4534. "Test `org-occur' specifications."
  4535. ;; Count number of matches.
  4536. (should
  4537. (= 1
  4538. (org-test-with-temp-text "* H\nA\n* H2"
  4539. (org-occur "A"))))
  4540. (should
  4541. (= 2
  4542. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4543. (org-occur "A"))))
  4544. ;; Test CALLBACK optional argument.
  4545. (should
  4546. (= 0
  4547. (org-test-with-temp-text "* H\nA\n* H2"
  4548. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  4549. (should
  4550. (= 1
  4551. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4552. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  4553. ;; Case-fold searches according to `org-occur-case-fold-search'.
  4554. (should
  4555. (= 2
  4556. (org-test-with-temp-text "Aa"
  4557. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  4558. (should
  4559. (= 2
  4560. (org-test-with-temp-text "Aa"
  4561. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  4562. (should
  4563. (= 1
  4564. (org-test-with-temp-text "Aa"
  4565. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  4566. (should
  4567. (= 1
  4568. (org-test-with-temp-text "Aa"
  4569. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  4570. (should
  4571. (= 1
  4572. (org-test-with-temp-text "Aa"
  4573. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  4574. (should
  4575. (= 2
  4576. (org-test-with-temp-text "Aa"
  4577. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  4578. ;;; Tags
  4579. (ert-deftest test-org/tag-string-to-alist ()
  4580. "Test `org-tag-string-to-alist' specifications."
  4581. ;; Tag without selection key.
  4582. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  4583. ;; Tag with selection key.
  4584. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  4585. ;; Tag group.
  4586. (should
  4587. (equal
  4588. (org-tag-string-to-alist "[ group : t1 t2 ]")
  4589. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  4590. ;; Mutually exclusive tags.
  4591. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  4592. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  4593. (should
  4594. (equal
  4595. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  4596. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  4597. (ert-deftest test-org/tag-alist-to-string ()
  4598. "Test `org-tag-alist-to-string' specifications."
  4599. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  4600. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  4601. (should
  4602. (equal
  4603. (org-tag-alist-to-string
  4604. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  4605. "[ group : t1 t2 ]"))
  4606. (should
  4607. (equal (org-tag-alist-to-string
  4608. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  4609. "{ tag1 tag2 }"))
  4610. (should
  4611. (equal
  4612. (org-tag-alist-to-string
  4613. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  4614. "{ group : tag1 tag2 }")))
  4615. (ert-deftest test-org/tag-alist-to-groups ()
  4616. "Test `org-tag-alist-to-groups' specifications."
  4617. (should
  4618. (equal (org-tag-alist-to-groups
  4619. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  4620. '(("group" "t1" "t2"))))
  4621. (should
  4622. (equal
  4623. (org-tag-alist-to-groups
  4624. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  4625. '(("group" "t1" "t2"))))
  4626. (should-not
  4627. (org-tag-alist-to-groups
  4628. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  4629. (ert-deftest test-org/tag-align ()
  4630. "Test tags alignment."
  4631. ;; Test aligning tags with different display width.
  4632. (should
  4633. ;; 12345678901234567890
  4634. (equal "* Test :abc:"
  4635. (org-test-with-temp-text "* Test :abc:"
  4636. (let ((org-tags-column -20)
  4637. (indent-tabs-mode nil))
  4638. (org-fix-tags-on-the-fly))
  4639. (buffer-string))))
  4640. (should
  4641. ;; 12345678901234567890
  4642. (equal "* Test :日本語:"
  4643. (org-test-with-temp-text "* Test :日本語:"
  4644. (let ((org-tags-column -20)
  4645. (indent-tabs-mode nil))
  4646. (org-fix-tags-on-the-fly))
  4647. (buffer-string))))
  4648. ;; Make sure aligning tags do not skip invisible text.
  4649. (should
  4650. (equal "* [[linkx]] :tag:"
  4651. (org-test-with-temp-text "* [[link<point>]] :tag:"
  4652. (let ((org-tags-column 0))
  4653. (org-fix-tags-on-the-fly)
  4654. (insert "x")
  4655. (buffer-string))))))
  4656. (ert-deftest test-org/tags-at ()
  4657. (should
  4658. (equal '("foo" "bar")
  4659. (org-test-with-temp-text
  4660. "* T<point>est :foo:bar:"
  4661. (org-get-tags-at)))))
  4662. ;;; Timestamps API
  4663. (ert-deftest test-org/time-stamp ()
  4664. "Test `org-time-stamp' specifications."
  4665. ;; Insert chosen time stamp at point.
  4666. (should
  4667. (string-match
  4668. "Te<2014-03-04 .*?>xt"
  4669. (org-test-with-temp-text "Te<point>xt"
  4670. (cl-letf (((symbol-function 'org-read-date)
  4671. (lambda (&rest args)
  4672. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4673. (org-time-stamp nil)
  4674. (buffer-string)))))
  4675. ;; With a prefix argument, also insert time.
  4676. (should
  4677. (string-match
  4678. "Te<2014-03-04 .*? 00:41>xt"
  4679. (org-test-with-temp-text "Te<point>xt"
  4680. (cl-letf (((symbol-function 'org-read-date)
  4681. (lambda (&rest args)
  4682. (apply #'encode-time
  4683. (org-parse-time-string "2014-03-04 00:41")))))
  4684. (org-time-stamp '(4))
  4685. (buffer-string)))))
  4686. ;; With two universal prefix arguments, insert an active timestamp
  4687. ;; with the current time without prompting the user.
  4688. (should
  4689. (string-match
  4690. "Te<2014-03-04 .*? 00:41>xt"
  4691. (org-test-with-temp-text "Te<point>xt"
  4692. (cl-letf (((symbol-function 'current-time)
  4693. (lambda ()
  4694. (apply #'encode-time
  4695. (org-parse-time-string "2014-03-04 00:41")))))
  4696. (org-time-stamp '(16))
  4697. (buffer-string)))))
  4698. ;; When optional argument is non-nil, insert an inactive timestamp.
  4699. (should
  4700. (string-match
  4701. "Te\\[2014-03-04 .*?\\]xt"
  4702. (org-test-with-temp-text "Te<point>xt"
  4703. (cl-letf (((symbol-function 'org-read-date)
  4704. (lambda (&rest args)
  4705. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4706. (org-time-stamp nil t)
  4707. (buffer-string)))))
  4708. ;; When called from a timestamp, replace existing one.
  4709. (should
  4710. (string-match
  4711. "<2014-03-04 .*?>"
  4712. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  4713. (cl-letf (((symbol-function 'org-read-date)
  4714. (lambda (&rest args)
  4715. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4716. (org-time-stamp nil)
  4717. (buffer-string)))))
  4718. (should
  4719. (string-match
  4720. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  4721. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  4722. (cl-letf (((symbol-function 'org-read-date)
  4723. (lambda (&rest args)
  4724. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4725. (org-time-stamp nil)
  4726. (buffer-string)))))
  4727. ;; When replacing a timestamp, preserve repeater, if any.
  4728. (should
  4729. (string-match
  4730. "<2014-03-04 .*? \\+2y>"
  4731. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  4732. (cl-letf (((symbol-function 'org-read-date)
  4733. (lambda (&rest args)
  4734. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4735. (org-time-stamp nil)
  4736. (buffer-string)))))
  4737. ;; When called twice in a raw, build a date range.
  4738. (should
  4739. (string-match
  4740. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  4741. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  4742. (cl-letf (((symbol-function 'org-read-date)
  4743. (lambda (&rest args)
  4744. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4745. (let ((last-command 'org-time-stamp)
  4746. (this-command 'org-time-stamp))
  4747. (org-time-stamp nil))
  4748. (buffer-string))))))
  4749. (ert-deftest test-org/timestamp-has-time-p ()
  4750. "Test `org-timestamp-has-time-p' specifications."
  4751. ;; With time.
  4752. (should
  4753. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  4754. (org-timestamp-has-time-p (org-element-context))))
  4755. ;; Without time.
  4756. (should-not
  4757. (org-test-with-temp-text "<2012-03-29 Thu>"
  4758. (org-timestamp-has-time-p (org-element-context)))))
  4759. (ert-deftest test-org/timestamp-format ()
  4760. "Test `org-timestamp-format' specifications."
  4761. ;; Regular test.
  4762. (should
  4763. (equal
  4764. "2012-03-29 16:40"
  4765. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  4766. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  4767. ;; Range end.
  4768. (should
  4769. (equal
  4770. "2012-03-29"
  4771. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  4772. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  4773. (ert-deftest test-org/timestamp-split-range ()
  4774. "Test `org-timestamp-split-range' specifications."
  4775. ;; Extract range start (active).
  4776. (should
  4777. (equal '(2012 3 29)
  4778. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4779. (let ((ts (org-timestamp-split-range (org-element-context))))
  4780. (mapcar (lambda (p) (org-element-property p ts))
  4781. '(:year-end :month-end :day-end))))))
  4782. ;; Extract range start (inactive)
  4783. (should
  4784. (equal '(2012 3 29)
  4785. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  4786. (let ((ts (org-timestamp-split-range (org-element-context))))
  4787. (mapcar (lambda (p) (org-element-property p ts))
  4788. '(:year-end :month-end :day-end))))))
  4789. ;; Extract range end (active).
  4790. (should
  4791. (equal '(2012 3 30)
  4792. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4793. (let ((ts (org-timestamp-split-range
  4794. (org-element-context) t)))
  4795. (mapcar (lambda (p) (org-element-property p ts))
  4796. '(:year-end :month-end :day-end))))))
  4797. ;; Extract range end (inactive)
  4798. (should
  4799. (equal '(2012 3 30)
  4800. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  4801. (let ((ts (org-timestamp-split-range
  4802. (org-element-context) t)))
  4803. (mapcar (lambda (p) (org-element-property p ts))
  4804. '(:year-end :month-end :day-end))))))
  4805. ;; Return the timestamp if not a range.
  4806. (should
  4807. (org-test-with-temp-text "[2012-03-29 Thu]"
  4808. (let* ((ts-orig (org-element-context))
  4809. (ts-copy (org-timestamp-split-range ts-orig)))
  4810. (eq ts-orig ts-copy))))
  4811. (should
  4812. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  4813. (let* ((ts-orig (org-element-context))
  4814. (ts-copy (org-timestamp-split-range ts-orig)))
  4815. (eq ts-orig ts-copy)))))
  4816. (ert-deftest test-org/timestamp-translate ()
  4817. "Test `org-timestamp-translate' specifications."
  4818. ;; Translate whole date range.
  4819. (should
  4820. (equal "<29>--<30>"
  4821. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4822. (let ((org-display-custom-times t)
  4823. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4824. (org-timestamp-translate (org-element-context))))))
  4825. ;; Translate date range start.
  4826. (should
  4827. (equal "<29>"
  4828. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4829. (let ((org-display-custom-times t)
  4830. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4831. (org-timestamp-translate (org-element-context) 'start)))))
  4832. ;; Translate date range end.
  4833. (should
  4834. (equal "<30>"
  4835. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4836. (let ((org-display-custom-times t)
  4837. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4838. (org-timestamp-translate (org-element-context) 'end)))))
  4839. ;; Translate time range.
  4840. (should
  4841. (equal "<08>--<16>"
  4842. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  4843. (let ((org-display-custom-times t)
  4844. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  4845. (org-timestamp-translate (org-element-context))))))
  4846. ;; Translate non-range timestamp.
  4847. (should
  4848. (equal "<29>"
  4849. (org-test-with-temp-text "<2012-03-29 Thu>"
  4850. (let ((org-display-custom-times t)
  4851. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4852. (org-timestamp-translate (org-element-context))))))
  4853. ;; Do not change `diary' timestamps.
  4854. (should
  4855. (equal "<%%(org-float t 4 2)>"
  4856. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  4857. (let ((org-display-custom-times t)
  4858. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4859. (org-timestamp-translate (org-element-context)))))))
  4860. ;;; Visibility
  4861. (ert-deftest test-org/flag-drawer ()
  4862. "Test `org-flag-drawer' specifications."
  4863. ;; Hide drawer.
  4864. (should
  4865. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4866. (org-flag-drawer t)
  4867. (get-char-property (line-end-position) 'invisible)))
  4868. ;; Show drawer.
  4869. (should-not
  4870. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4871. (org-flag-drawer t)
  4872. (org-flag-drawer nil)
  4873. (get-char-property (line-end-position) 'invisible)))
  4874. ;; Test optional argument.
  4875. (should
  4876. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4877. (let ((drawer (save-excursion (search-forward ":D2")
  4878. (org-element-at-point))))
  4879. (org-flag-drawer t drawer)
  4880. (get-char-property (progn (search-forward ":D2") (line-end-position))
  4881. 'invisible))))
  4882. (should-not
  4883. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4884. (let ((drawer (save-excursion (search-forward ":D2")
  4885. (org-element-at-point))))
  4886. (org-flag-drawer t drawer)
  4887. (get-char-property (line-end-position) 'invisible))))
  4888. ;; Do not hide fake drawers.
  4889. (should-not
  4890. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  4891. (forward-line 1)
  4892. (org-flag-drawer t)
  4893. (get-char-property (line-end-position) 'invisible)))
  4894. ;; Do not hide incomplete drawers.
  4895. (should-not
  4896. (org-test-with-temp-text ":D:\nparagraph"
  4897. (forward-line 1)
  4898. (org-flag-drawer t)
  4899. (get-char-property (line-end-position) 'invisible)))
  4900. ;; Do not hide drawers when called from final blank lines.
  4901. (should-not
  4902. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  4903. (goto-char (point-max))
  4904. (org-flag-drawer t)
  4905. (goto-char (point-min))
  4906. (get-char-property (line-end-position) 'invisible)))
  4907. ;; Don't leave point in an invisible part of the buffer when hiding
  4908. ;; a drawer away.
  4909. (should-not
  4910. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4911. (goto-char (point-max))
  4912. (org-flag-drawer t)
  4913. (get-char-property (point) 'invisible))))
  4914. (ert-deftest test-org/hide-block-toggle ()
  4915. "Test `org-hide-block-toggle' specifications."
  4916. ;; Error when not at a block.
  4917. (should-error
  4918. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  4919. (org-hide-block-toggle 'off)
  4920. (get-char-property (line-end-position) 'invisible)))
  4921. ;; Hide block.
  4922. (should
  4923. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  4924. (org-hide-block-toggle)
  4925. (get-char-property (line-end-position) 'invisible)))
  4926. (should
  4927. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  4928. (org-hide-block-toggle)
  4929. (get-char-property (line-end-position) 'invisible)))
  4930. ;; Show block unconditionally when optional argument is `off'.
  4931. (should-not
  4932. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4933. (org-hide-block-toggle)
  4934. (org-hide-block-toggle 'off)
  4935. (get-char-property (line-end-position) 'invisible)))
  4936. (should-not
  4937. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4938. (org-hide-block-toggle 'off)
  4939. (get-char-property (line-end-position) 'invisible)))
  4940. ;; Hide block unconditionally when optional argument is non-nil.
  4941. (should
  4942. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4943. (org-hide-block-toggle t)
  4944. (get-char-property (line-end-position) 'invisible)))
  4945. (should
  4946. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4947. (org-hide-block-toggle)
  4948. (org-hide-block-toggle t)
  4949. (get-char-property (line-end-position) 'invisible)))
  4950. ;; Do not hide block when called from final blank lines.
  4951. (should-not
  4952. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  4953. (org-hide-block-toggle)
  4954. (goto-char (point-min))
  4955. (get-char-property (line-end-position) 'invisible)))
  4956. ;; Don't leave point in an invisible part of the buffer when hiding
  4957. ;; a block away.
  4958. (should-not
  4959. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  4960. (org-hide-block-toggle)
  4961. (get-char-property (point) 'invisible))))
  4962. (ert-deftest test-org/hide-block-toggle-maybe ()
  4963. "Test `org-hide-block-toggle-maybe' specifications."
  4964. (should
  4965. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  4966. (org-hide-block-toggle-maybe)))
  4967. (should-not
  4968. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  4969. (ert-deftest test-org/set-tags ()
  4970. "Test `org-set-tags' specifications."
  4971. ;; Tags set via fast-tag-selection should be visible afterwards
  4972. (should
  4973. (let ((org-tag-alist '(("NEXT" . ?n)))
  4974. (org-fast-tag-selection-single-key t))
  4975. (cl-letf (((symbol-function 'read-char-exclusive) (lambda () ?n))
  4976. ((symbol-function 'window-width) (lambda (&rest args) 100)))
  4977. (org-test-with-temp-text "<point>* Headline\nAnd its content\n* And another headline\n\nWith some content"
  4978. ;; Show only headlines
  4979. (org-content)
  4980. ;; Set NEXT tag on current entry
  4981. (org-set-tags nil nil)
  4982. ;; Move point to that NEXT tag
  4983. (search-forward "NEXT") (backward-word)
  4984. ;; And it should be visible (i.e. no overlays)
  4985. (not (overlays-at (point))))))))
  4986. (ert-deftest test-org/show-set-visibility ()
  4987. "Test `org-show-set-visibility' specifications."
  4988. ;; Do not throw an error before first heading.
  4989. (should
  4990. (org-test-with-temp-text "Preamble\n* Headline"
  4991. (org-show-set-visibility 'tree)
  4992. t))
  4993. ;; Test all visibility spans, both on headline and in entry.
  4994. (let ((list-visible-lines
  4995. (lambda (state headerp)
  4996. (org-test-with-temp-text "* Grandmother (0)
  4997. ** Uncle (1)
  4998. *** Heir (2)
  4999. ** Father (3)
  5000. Ancestor text (4)
  5001. *** Sister (5)
  5002. Sibling text (6)
  5003. *** Self (7)
  5004. Match (8)
  5005. **** First born (9)
  5006. Child text (10)
  5007. **** The other child (11)
  5008. *** Brother (12)
  5009. ** Aunt (13)
  5010. "
  5011. (org-cycle t)
  5012. (search-forward (if headerp "Self" "Match"))
  5013. (org-show-set-visibility state)
  5014. (goto-char (point-min))
  5015. (let (result (line 0))
  5016. (while (not (eobp))
  5017. (unless (org-invisible-p2) (push line result))
  5018. (incf line)
  5019. (forward-line))
  5020. (nreverse result))))))
  5021. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  5022. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  5023. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  5024. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  5025. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  5026. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  5027. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  5028. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  5029. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  5030. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  5031. (funcall list-visible-lines 'tree nil)))
  5032. (should (equal '(0 1 3 4 5 7 12 13)
  5033. (funcall list-visible-lines 'canonical t)))
  5034. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  5035. (funcall list-visible-lines 'canonical nil))))
  5036. ;; When point is hidden in a drawer or a block, make sure to make it
  5037. ;; visible.
  5038. (should-not
  5039. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  5040. (org-hide-block-toggle)
  5041. (search-forward "Text")
  5042. (org-show-set-visibility 'minimal)
  5043. (org-invisible-p2)))
  5044. (should-not
  5045. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  5046. (org-flag-drawer t)
  5047. (search-forward "Text")
  5048. (org-show-set-visibility 'minimal)
  5049. (org-invisible-p2)))
  5050. (should-not
  5051. (org-test-with-temp-text
  5052. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  5053. (org-flag-drawer t)
  5054. (forward-line -1)
  5055. (org-hide-block-toggle)
  5056. (search-forward "Text")
  5057. (org-show-set-visibility 'minimal)
  5058. (org-invisible-p2))))
  5059. (provide 'test-org)
  5060. ;;; test-org.el ends here