test-org.el 202 KB

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