test-org.el 192 KB

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