test-org.el 173 KB

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