test-org.el 240 KB

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