test-org.el 204 KB

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