test-org.el 239 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357
  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. ;; Test point location.
  3989. (should
  3990. (string= "#+begin_foo\n"
  3991. (org-test-with-temp-text ""
  3992. (org-insert-structure-template "foo")
  3993. (buffer-substring (point-min) (point)))))
  3994. (should
  3995. (string= "#+begin_src "
  3996. (org-test-with-temp-text ""
  3997. (org-insert-structure-template "src")
  3998. (buffer-substring (point-min) (point))))))
  3999. (ert-deftest test-org/previous-block ()
  4000. "Test `org-previous-block' specifications."
  4001. ;; Regular test.
  4002. (should
  4003. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  4004. (org-previous-block 1)
  4005. (looking-at "#\\+BEGIN_CENTER")))
  4006. ;; Ignore case.
  4007. (should
  4008. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  4009. (let ((case-fold-search nil))
  4010. (org-previous-block 1)
  4011. (looking-at "#\\+begin_center"))))
  4012. ;; Ignore current line.
  4013. (should
  4014. (org-test-with-temp-text
  4015. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  4016. (org-previous-block 1)
  4017. (looking-at "#\\+BEGIN_QUOTE")))
  4018. ;; Throw an error when no block is found.
  4019. (should-error
  4020. (org-test-with-temp-text "Paragraph<point>"
  4021. (org-previous-block 1)))
  4022. ;; With an argument, skip many blocks at once.
  4023. (should
  4024. (org-test-with-temp-text
  4025. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4026. (org-previous-block 2)
  4027. (looking-at "#\\+BEGIN_CENTER")))
  4028. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  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 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4033. (looking-at "#\\+BEGIN_QUOTE")))
  4034. ;; Optional argument is also case-insensitive.
  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. (let ((case-fold-search nil))
  4039. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4040. (looking-at "#\\+begin_quote")))))
  4041. ;;; Outline structure
  4042. (ert-deftest test-org/demote ()
  4043. "Test `org-demote' specifications."
  4044. ;; Add correct number of stars according to `org-odd-levels-only'.
  4045. (should
  4046. (= 2
  4047. (org-test-with-temp-text "* H"
  4048. (let ((org-odd-levels-only nil)) (org-demote))
  4049. (org-current-level))))
  4050. (should
  4051. (= 3
  4052. (org-test-with-temp-text "* H"
  4053. (let ((org-odd-levels-only t)) (org-demote))
  4054. (org-current-level))))
  4055. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4056. (should
  4057. (org-test-with-temp-text "* H :tag:"
  4058. (let ((org-tags-column 10)
  4059. (org-auto-align-tags t)
  4060. (org-odd-levels-only nil))
  4061. (org-demote))
  4062. (org-move-to-column 10)
  4063. (looking-at-p ":tag:$")))
  4064. (should-not
  4065. (org-test-with-temp-text "* H :tag:"
  4066. (let ((org-tags-column 10)
  4067. (org-auto-align-tags nil)
  4068. (org-odd-levels-only nil))
  4069. (org-demote))
  4070. (org-move-to-column 10)
  4071. (looking-at-p ":tag:$")))
  4072. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4073. ;; info and property drawers accordingly.
  4074. (should
  4075. (= 3
  4076. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4077. (let ((org-odd-levels-only nil)
  4078. (org-adapt-indentation t))
  4079. (org-demote))
  4080. (forward-line)
  4081. (org-get-indentation))))
  4082. (should
  4083. (= 3
  4084. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4085. (let ((org-odd-levels-only nil)
  4086. (org-adapt-indentation t))
  4087. (org-demote))
  4088. (forward-line)
  4089. (org-get-indentation))))
  4090. (should-not
  4091. (= 3
  4092. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4093. (let ((org-odd-levels-only nil)
  4094. (org-adapt-indentation nil))
  4095. (org-demote))
  4096. (forward-line)
  4097. (org-get-indentation))))
  4098. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4099. ;; section accordingly. Ignore, however, footnote definitions and
  4100. ;; inlinetasks boundaries.
  4101. (should
  4102. (= 3
  4103. (org-test-with-temp-text "* H\n Paragraph"
  4104. (let ((org-odd-levels-only nil)
  4105. (org-adapt-indentation t))
  4106. (org-demote))
  4107. (forward-line)
  4108. (org-get-indentation))))
  4109. (should
  4110. (= 2
  4111. (org-test-with-temp-text "* H\n Paragraph"
  4112. (let ((org-odd-levels-only nil)
  4113. (org-adapt-indentation nil))
  4114. (org-demote))
  4115. (forward-line)
  4116. (org-get-indentation))))
  4117. (should
  4118. (zerop
  4119. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  4120. (let ((org-odd-levels-only nil)
  4121. (org-adapt-indentation t))
  4122. (org-demote))
  4123. (goto-char (point-max))
  4124. (org-get-indentation))))
  4125. (should
  4126. (= 3
  4127. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  4128. (let ((org-odd-levels-only nil)
  4129. (org-adapt-indentation t))
  4130. (org-demote))
  4131. (goto-char (point-max))
  4132. (org-get-indentation))))
  4133. (when (featurep 'org-inlinetask)
  4134. (should
  4135. (zerop
  4136. (let ((org-inlinetask-min-level 5)
  4137. (org-adapt-indentation t))
  4138. (org-test-with-temp-text "* H\n***** I\n***** END"
  4139. (org-demote)
  4140. (forward-line)
  4141. (org-get-indentation))))))
  4142. (when (featurep 'org-inlinetask)
  4143. (should
  4144. (= 3
  4145. (let ((org-inlinetask-min-level 5)
  4146. (org-adapt-indentation t))
  4147. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  4148. (org-demote)
  4149. (forward-line 2)
  4150. (org-get-indentation))))))
  4151. ;; Ignore contents of source blocks or example blocks when
  4152. ;; indentation should be preserved (through
  4153. ;; `org-src-preserve-indentation' or "-i" flag).
  4154. (should-not
  4155. (zerop
  4156. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4157. (let ((org-adapt-indentation t)
  4158. (org-src-preserve-indentation nil))
  4159. (org-demote))
  4160. (forward-line 2)
  4161. (org-get-indentation))))
  4162. (should
  4163. (zerop
  4164. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  4165. (let ((org-adapt-indentation t)
  4166. (org-src-preserve-indentation t))
  4167. (org-demote))
  4168. (forward-line 2)
  4169. (org-get-indentation))))
  4170. (should
  4171. (zerop
  4172. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4173. (let ((org-adapt-indentation t)
  4174. (org-src-preserve-indentation t))
  4175. (org-demote))
  4176. (forward-line 2)
  4177. (org-get-indentation))))
  4178. (should
  4179. (zerop
  4180. (org-test-with-temp-text
  4181. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  4182. (let ((org-adapt-indentation t)
  4183. (org-src-preserve-indentation nil))
  4184. (org-demote))
  4185. (forward-line 2)
  4186. (org-get-indentation)))))
  4187. (ert-deftest test-org/promote ()
  4188. "Test `org-promote' specifications."
  4189. ;; Return an error if headline is to be promoted to level 0, unless
  4190. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  4191. ;; headline becomes a comment.
  4192. (should-error
  4193. (org-test-with-temp-text "* H"
  4194. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  4195. (should
  4196. (equal "# H"
  4197. (org-test-with-temp-text "* H"
  4198. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  4199. (buffer-string))))
  4200. ;; Remove correct number of stars according to
  4201. ;; `org-odd-levels-only'.
  4202. (should
  4203. (= 2
  4204. (org-test-with-temp-text "*** H"
  4205. (let ((org-odd-levels-only nil)) (org-promote))
  4206. (org-current-level))))
  4207. (should
  4208. (= 1
  4209. (org-test-with-temp-text "*** H"
  4210. (let ((org-odd-levels-only t)) (org-promote))
  4211. (org-current-level))))
  4212. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4213. (should
  4214. (org-test-with-temp-text "** H :tag:"
  4215. (let ((org-tags-column 10)
  4216. (org-auto-align-tags t)
  4217. (org-odd-levels-only nil))
  4218. (org-promote))
  4219. (org-move-to-column 10)
  4220. (looking-at-p ":tag:$")))
  4221. (should-not
  4222. (org-test-with-temp-text "** H :tag:"
  4223. (let ((org-tags-column 10)
  4224. (org-auto-align-tags nil)
  4225. (org-odd-levels-only nil))
  4226. (org-promote))
  4227. (org-move-to-column 10)
  4228. (looking-at-p ":tag:$")))
  4229. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4230. ;; info and property drawers.
  4231. (should
  4232. (= 2
  4233. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4234. (let ((org-odd-levels-only nil)
  4235. (org-adapt-indentation t))
  4236. (org-promote))
  4237. (forward-line)
  4238. (org-get-indentation))))
  4239. (should
  4240. (= 2
  4241. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4242. (let ((org-odd-levels-only nil)
  4243. (org-adapt-indentation t))
  4244. (org-promote))
  4245. (forward-line)
  4246. (org-get-indentation))))
  4247. (should-not
  4248. (= 2
  4249. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4250. (let ((org-odd-levels-only nil)
  4251. (org-adapt-indentation nil))
  4252. (org-promote))
  4253. (forward-line)
  4254. (org-get-indentation))))
  4255. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4256. ;; section accordingly. Ignore, however, footnote definitions and
  4257. ;; inlinetasks boundaries.
  4258. (should
  4259. (= 2
  4260. (org-test-with-temp-text "** H\n Paragraph"
  4261. (let ((org-odd-levels-only nil)
  4262. (org-adapt-indentation t))
  4263. (org-promote))
  4264. (forward-line)
  4265. (org-get-indentation))))
  4266. (should-not
  4267. (= 2
  4268. (org-test-with-temp-text "** H\n Paragraph"
  4269. (let ((org-odd-levels-only nil)
  4270. (org-adapt-indentation nil))
  4271. (org-promote))
  4272. (forward-line)
  4273. (org-get-indentation))))
  4274. (should
  4275. (= 2
  4276. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  4277. (let ((org-odd-levels-only nil)
  4278. (org-adapt-indentation t))
  4279. (org-promote))
  4280. (forward-line)
  4281. (org-get-indentation))))
  4282. (when (featurep 'org-inlinetask)
  4283. (should
  4284. (zerop
  4285. (let ((org-inlinetask-min-level 5)
  4286. (org-adapt-indentation t))
  4287. (org-test-with-temp-text "** H\n***** I\n***** END"
  4288. (org-promote)
  4289. (forward-line)
  4290. (org-get-indentation))))))
  4291. (when (featurep 'org-inlinetask)
  4292. (should
  4293. (= 2
  4294. (let ((org-inlinetask-min-level 5)
  4295. (org-adapt-indentation t))
  4296. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  4297. (org-promote)
  4298. (forward-line 2)
  4299. (org-get-indentation))))))
  4300. ;; Give up shifting if it would break document's structure
  4301. ;; otherwise.
  4302. (should
  4303. (= 3
  4304. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  4305. (let ((org-odd-levels-only nil)
  4306. (org-adapt-indentation t))
  4307. (org-promote))
  4308. (forward-line)
  4309. (org-get-indentation))))
  4310. (should
  4311. (= 3
  4312. (org-test-with-temp-text "** H\n Paragraph\n * list."
  4313. (let ((org-odd-levels-only nil)
  4314. (org-adapt-indentation t))
  4315. (org-promote))
  4316. (forward-line)
  4317. (org-get-indentation))))
  4318. ;; Ignore contents of source blocks or example blocks when
  4319. ;; indentation should be preserved (through
  4320. ;; `org-src-preserve-indentation' or "-i" flag).
  4321. (should-not
  4322. (zerop
  4323. (org-test-with-temp-text
  4324. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4325. (let ((org-adapt-indentation t)
  4326. (org-src-preserve-indentation nil)
  4327. (org-odd-levels-only nil))
  4328. (org-promote))
  4329. (forward-line)
  4330. (org-get-indentation))))
  4331. (should
  4332. (zerop
  4333. (org-test-with-temp-text
  4334. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  4335. (let ((org-adapt-indentation t)
  4336. (org-src-preserve-indentation t)
  4337. (org-odd-levels-only nil))
  4338. (org-promote))
  4339. (forward-line)
  4340. (org-get-indentation))))
  4341. (should
  4342. (zerop
  4343. (org-test-with-temp-text
  4344. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4345. (let ((org-adapt-indentation t)
  4346. (org-src-preserve-indentation t)
  4347. (org-odd-levels-only nil))
  4348. (org-promote))
  4349. (forward-line)
  4350. (org-get-indentation))))
  4351. (should
  4352. (zerop
  4353. (org-test-with-temp-text
  4354. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  4355. (let ((org-adapt-indentation t)
  4356. (org-src-preserve-indentation nil)
  4357. (org-odd-levels-only nil))
  4358. (org-promote))
  4359. (forward-line)
  4360. (org-get-indentation)))))
  4361. (ert-deftest test-org/org-get-valid-level ()
  4362. "Test function `org-get-valid-level' specifications."
  4363. (let ((org-odd-levels-only nil))
  4364. (should (equal 1 (org-get-valid-level 0 0)))
  4365. (should (equal 1 (org-get-valid-level 0 1)))
  4366. (should (equal 2 (org-get-valid-level 0 2)))
  4367. (should (equal 3 (org-get-valid-level 0 3)))
  4368. (should (equal 1 (org-get-valid-level 1 0)))
  4369. (should (equal 2 (org-get-valid-level 1 1)))
  4370. (should (equal 23 (org-get-valid-level 1 22)))
  4371. (should (equal 1 (org-get-valid-level 1 -1)))
  4372. (should (equal 1 (org-get-valid-level 2 -1))))
  4373. (let ((org-odd-levels-only t))
  4374. (should (equal 1 (org-get-valid-level 0 0)))
  4375. (should (equal 1 (org-get-valid-level 0 1)))
  4376. (should (equal 3 (org-get-valid-level 0 2)))
  4377. (should (equal 5 (org-get-valid-level 0 3)))
  4378. (should (equal 1 (org-get-valid-level 1 0)))
  4379. (should (equal 3 (org-get-valid-level 1 1)))
  4380. (should (equal 3 (org-get-valid-level 2 1)))
  4381. (should (equal 5 (org-get-valid-level 3 1)))
  4382. (should (equal 5 (org-get-valid-level 4 1)))
  4383. (should (equal 43 (org-get-valid-level 1 21)))
  4384. (should (equal 1 (org-get-valid-level 1 -1)))
  4385. (should (equal 1 (org-get-valid-level 2 -1)))
  4386. (should (equal 1 (org-get-valid-level 3 -1)))
  4387. (should (equal 3 (org-get-valid-level 4 -1)))
  4388. (should (equal 3 (org-get-valid-level 5 -1)))))
  4389. ;;; Planning
  4390. (ert-deftest test-org/at-planning-p ()
  4391. "Test `org-at-planning-p' specifications."
  4392. ;; Regular test.
  4393. (should
  4394. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  4395. (org-at-planning-p)))
  4396. (should-not
  4397. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  4398. (org-at-planning-p)))
  4399. ;; Correctly find planning attached to inlinetasks.
  4400. (when (featurep 'org-inlinetask)
  4401. (should
  4402. (org-test-with-temp-text
  4403. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  4404. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4405. (should-not
  4406. (org-test-with-temp-text
  4407. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4408. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4409. (should-not
  4410. (org-test-with-temp-text
  4411. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4412. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4413. (should-not
  4414. (org-test-with-temp-text
  4415. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  4416. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  4417. (ert-deftest test-org/add-planning-info ()
  4418. "Test `org-add-planning-info'."
  4419. ;; Create deadline when `org-adapt-indentation' is non-nil.
  4420. (should
  4421. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4422. (org-test-with-temp-text "* H\nParagraph<point>"
  4423. (let ((org-adapt-indentation t))
  4424. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4425. (replace-regexp-in-string
  4426. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4427. nil nil 1))))
  4428. ;; Create deadline when `org-adapt-indentation' is nil.
  4429. (should
  4430. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4431. (org-test-with-temp-text "* H\nParagraph<point>"
  4432. (let ((org-adapt-indentation nil))
  4433. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4434. (replace-regexp-in-string
  4435. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4436. nil nil 1))))
  4437. ;; Update deadline when `org-adapt-indentation' is non-nil.
  4438. (should
  4439. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4440. (org-test-with-temp-text "\
  4441. * H
  4442. DEADLINE: <2015-06-24 Wed>
  4443. Paragraph<point>"
  4444. (let ((org-adapt-indentation t))
  4445. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4446. (replace-regexp-in-string
  4447. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4448. nil nil 1))))
  4449. ;; Update deadline when `org-adapt-indentation' is nil.
  4450. (should
  4451. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4452. (org-test-with-temp-text "\
  4453. * H
  4454. DEADLINE: <2015-06-24 Wed>
  4455. Paragraph<point>"
  4456. (let ((org-adapt-indentation nil))
  4457. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4458. (replace-regexp-in-string
  4459. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4460. nil nil 1))))
  4461. ;; Schedule when `org-adapt-indentation' is non-nil.
  4462. (should
  4463. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  4464. (org-test-with-temp-text "* H\nParagraph<point>"
  4465. (let ((org-adapt-indentation t))
  4466. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4467. (replace-regexp-in-string
  4468. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4469. nil nil 1))))
  4470. ;; Schedule when `org-adapt-indentation' is nil.
  4471. (should
  4472. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  4473. (org-test-with-temp-text "* H\nParagraph<point>"
  4474. (let ((org-adapt-indentation nil))
  4475. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4476. (replace-regexp-in-string
  4477. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4478. nil nil 1))))
  4479. ;; Add deadline when scheduled.
  4480. (should
  4481. (equal "\
  4482. * H
  4483. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  4484. Paragraph"
  4485. (org-test-with-temp-text "\
  4486. * H
  4487. SCHEDULED: <2015-06-24 Wed>
  4488. Paragraph<point>"
  4489. (let ((org-adapt-indentation t))
  4490. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4491. (replace-regexp-in-string
  4492. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4493. nil nil 1))))
  4494. ;; Remove middle entry.
  4495. (should
  4496. (equal "\
  4497. * H
  4498. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  4499. Paragraph"
  4500. (org-test-with-temp-text "\
  4501. * H
  4502. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4503. Paragraph<point>"
  4504. (let ((org-adapt-indentation t))
  4505. (org-add-planning-info nil nil 'deadline))
  4506. (replace-regexp-in-string
  4507. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4508. nil nil 1))))
  4509. ;; Remove last entry and then middle entry (order should not
  4510. ;; matter).
  4511. (should
  4512. (equal "\
  4513. * H
  4514. CLOSED: [2015-06-24]
  4515. Paragraph"
  4516. (org-test-with-temp-text "\
  4517. * H
  4518. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4519. Paragraph<point>"
  4520. (let ((org-adapt-indentation t))
  4521. (org-add-planning-info nil nil 'scheduled 'deadline))
  4522. (replace-regexp-in-string
  4523. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4524. nil nil 1))))
  4525. ;; Remove closed when `org-adapt-indentation' is non-nil.
  4526. (should
  4527. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4528. (org-test-with-temp-text "\
  4529. * H
  4530. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4531. Paragraph<point>"
  4532. (let ((org-adapt-indentation t))
  4533. (org-add-planning-info nil nil 'closed))
  4534. (replace-regexp-in-string
  4535. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4536. nil nil 1))))
  4537. (should
  4538. (equal "* H\n Paragraph"
  4539. (org-test-with-temp-text "\
  4540. * H
  4541. CLOSED: [2015-06-25 Thu]
  4542. Paragraph<point>"
  4543. (let ((org-adapt-indentation t))
  4544. (org-add-planning-info nil nil 'closed))
  4545. (replace-regexp-in-string
  4546. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4547. nil nil 1))))
  4548. ;; Remove closed when `org-adapt-indentation' is nil.
  4549. (should
  4550. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4551. (org-test-with-temp-text "\
  4552. * H
  4553. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4554. Paragraph<point>"
  4555. (let ((org-adapt-indentation nil))
  4556. (org-add-planning-info nil nil 'closed))
  4557. (replace-regexp-in-string
  4558. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4559. nil nil 1))))
  4560. (should
  4561. (equal "* H\nParagraph"
  4562. (org-test-with-temp-text "\
  4563. * H
  4564. CLOSED: [2015-06-25 Thu]
  4565. Paragraph<point>"
  4566. (let ((org-adapt-indentation nil))
  4567. (org-add-planning-info nil nil 'closed))
  4568. (replace-regexp-in-string
  4569. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4570. nil nil 1))))
  4571. ;; Remove closed entry and delete empty line.
  4572. (should
  4573. (equal "\
  4574. * H
  4575. Paragraph"
  4576. (org-test-with-temp-text "\
  4577. * H
  4578. CLOSED: [2015-06-24 Wed]
  4579. Paragraph<point>"
  4580. (let ((org-adapt-indentation t))
  4581. (org-add-planning-info nil nil 'closed))
  4582. (replace-regexp-in-string
  4583. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4584. nil nil 1))))
  4585. ;; Remove one entry and update another.
  4586. (should
  4587. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4588. (org-test-with-temp-text "\
  4589. * H
  4590. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  4591. Paragraph<point>"
  4592. (let ((org-adapt-indentation t))
  4593. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  4594. (replace-regexp-in-string
  4595. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4596. nil nil 1)))))
  4597. (ert-deftest test-org/deadline ()
  4598. "Test `org-deadline' specifications."
  4599. ;; Insert a new value or replace existing one.
  4600. (should
  4601. (equal "* H\nDEADLINE: <2012-03-29>\n"
  4602. (org-test-with-temp-text "* H"
  4603. (let ((org-adapt-indentation nil)
  4604. (org-last-inserted-timestamp nil))
  4605. (org-deadline nil "<2012-03-29 Tue>"))
  4606. (replace-regexp-in-string
  4607. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4608. nil nil 1))))
  4609. (should
  4610. (equal "* H\nDEADLINE: <2014-03-04>"
  4611. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4612. (let ((org-adapt-indentation nil)
  4613. (org-last-inserted-timestamp nil))
  4614. (org-deadline nil "<2014-03-04 Thu>"))
  4615. (replace-regexp-in-string
  4616. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4617. nil nil 1))))
  4618. ;; Accept delta time, e.g., "+2d".
  4619. (should
  4620. (equal "* H\nDEADLINE: <2015-03-04>\n"
  4621. (cl-letf (((symbol-function 'current-time)
  4622. (lambda (&rest args)
  4623. (apply #'encode-time
  4624. (org-parse-time-string "2014-03-04")))))
  4625. (org-test-with-temp-text "* H"
  4626. (let ((org-adapt-indentation nil)
  4627. (org-last-inserted-timestamp nil))
  4628. (org-deadline nil "+1y"))
  4629. (replace-regexp-in-string
  4630. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4631. ;; Preserve repeater.
  4632. (should
  4633. (equal "* H\nDEADLINE: <2012-03-29 +2y>\n"
  4634. (org-test-with-temp-text "* H"
  4635. (let ((org-adapt-indentation nil)
  4636. (org-last-inserted-timestamp nil))
  4637. (org-deadline nil "<2012-03-29 Tue +2y>"))
  4638. (replace-regexp-in-string
  4639. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4640. ;; Remove CLOSED keyword, if any.
  4641. (should
  4642. (equal "* H\nDEADLINE: <2012-03-29>"
  4643. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4644. (let ((org-adapt-indentation nil)
  4645. (org-last-inserted-timestamp nil))
  4646. (org-deadline nil "<2012-03-29 Tue>"))
  4647. (replace-regexp-in-string
  4648. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4649. ;; With C-u argument, remove DEADLINE keyword.
  4650. (should
  4651. (equal "* H\n"
  4652. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4653. (let ((org-adapt-indentation nil)
  4654. (org-last-inserted-timestamp nil))
  4655. (org-deadline '(4)))
  4656. (buffer-string))))
  4657. (should
  4658. (equal "* H"
  4659. (org-test-with-temp-text "* H"
  4660. (let ((org-adapt-indentation nil)
  4661. (org-last-inserted-timestamp nil))
  4662. (org-deadline '(4)))
  4663. (buffer-string))))
  4664. ;; With C-u C-u argument, prompt for a delay cookie.
  4665. (should
  4666. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  4667. (cl-letf (((symbol-function 'org-read-date)
  4668. (lambda (&rest args)
  4669. (apply #'encode-time
  4670. (org-parse-time-string "2014-03-04")))))
  4671. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4672. (let ((org-adapt-indentation nil)
  4673. (org-last-inserted-timestamp nil))
  4674. (org-deadline '(16)))
  4675. (buffer-string)))))
  4676. (should-error
  4677. (cl-letf (((symbol-function 'org-read-date)
  4678. (lambda (&rest args)
  4679. (apply #'encode-time
  4680. (org-parse-time-string "2014-03-04")))))
  4681. (org-test-with-temp-text "* H"
  4682. (let ((org-adapt-indentation nil)
  4683. (org-last-inserted-timestamp nil))
  4684. (org-deadline '(16)))
  4685. (buffer-string))))
  4686. ;; When a region is active and
  4687. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4688. ;; same value in all headlines in region.
  4689. (should
  4690. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4691. (org-test-with-temp-text "* H1\n* H2"
  4692. (let ((org-adapt-indentation nil)
  4693. (org-last-inserted-timestamp nil)
  4694. (org-loop-over-headlines-in-active-region t))
  4695. (transient-mark-mode 1)
  4696. (push-mark (point) t t)
  4697. (goto-char (point-max))
  4698. (org-deadline nil "2012-03-29"))
  4699. (replace-regexp-in-string
  4700. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4701. (should-not
  4702. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4703. (org-test-with-temp-text "* H1\n* H2"
  4704. (let ((org-adapt-indentation nil)
  4705. (org-last-inserted-timestamp nil)
  4706. (org-loop-over-headlines-in-active-region nil))
  4707. (transient-mark-mode 1)
  4708. (push-mark (point) t t)
  4709. (goto-char (point-max))
  4710. (org-deadline nil "2012-03-29"))
  4711. (replace-regexp-in-string
  4712. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4713. (ert-deftest test-org/schedule ()
  4714. "Test `org-schedule' specifications."
  4715. ;; Insert a new value or replace existing one.
  4716. (should
  4717. (equal "* H\nSCHEDULED: <2012-03-29>\n"
  4718. (org-test-with-temp-text "* H"
  4719. (let ((org-adapt-indentation nil)
  4720. (org-last-inserted-timestamp nil))
  4721. (org-schedule nil "<2012-03-29 Tue>"))
  4722. (replace-regexp-in-string
  4723. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4724. nil nil 1))))
  4725. (should
  4726. (equal "* H\nSCHEDULED: <2014-03-04>"
  4727. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4728. (let ((org-adapt-indentation nil)
  4729. (org-last-inserted-timestamp nil))
  4730. (org-schedule nil "<2014-03-04 Thu>"))
  4731. (replace-regexp-in-string
  4732. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4733. nil nil 1))))
  4734. ;; Accept delta time, e.g., "+2d".
  4735. (should
  4736. (equal "* H\nSCHEDULED: <2015-03-04>\n"
  4737. (cl-letf (((symbol-function 'current-time)
  4738. (lambda (&rest args)
  4739. (apply #'encode-time
  4740. (org-parse-time-string "2014-03-04")))))
  4741. (org-test-with-temp-text "* H"
  4742. (let ((org-adapt-indentation nil)
  4743. (org-last-inserted-timestamp nil))
  4744. (org-schedule nil "+1y"))
  4745. (replace-regexp-in-string
  4746. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4747. ;; Preserve repeater.
  4748. (should
  4749. (equal "* H\nSCHEDULED: <2012-03-29 +2y>\n"
  4750. (org-test-with-temp-text "* H"
  4751. (let ((org-adapt-indentation nil)
  4752. (org-last-inserted-timestamp nil))
  4753. (org-schedule nil "<2012-03-29 Tue +2y>"))
  4754. (replace-regexp-in-string
  4755. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4756. ;; Remove CLOSED keyword, if any.
  4757. (should
  4758. (equal "* H\nSCHEDULED: <2012-03-29>"
  4759. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4760. (let ((org-adapt-indentation nil)
  4761. (org-last-inserted-timestamp nil))
  4762. (org-schedule nil "<2012-03-29 Tue>"))
  4763. (replace-regexp-in-string
  4764. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4765. ;; With C-u argument, remove SCHEDULED keyword.
  4766. (should
  4767. (equal "* H\n"
  4768. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4769. (let ((org-adapt-indentation nil)
  4770. (org-last-inserted-timestamp nil))
  4771. (org-schedule '(4)))
  4772. (buffer-string))))
  4773. (should
  4774. (equal "* H"
  4775. (org-test-with-temp-text "* H"
  4776. (let ((org-adapt-indentation nil)
  4777. (org-last-inserted-timestamp nil))
  4778. (org-schedule '(4)))
  4779. (buffer-string))))
  4780. ;; With C-u C-u argument, prompt for a delay cookie.
  4781. (should
  4782. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  4783. (cl-letf (((symbol-function 'org-read-date)
  4784. (lambda (&rest args)
  4785. (apply #'encode-time
  4786. (org-parse-time-string "2014-03-04")))))
  4787. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4788. (let ((org-adapt-indentation nil)
  4789. (org-last-inserted-timestamp nil))
  4790. (org-schedule '(16)))
  4791. (buffer-string)))))
  4792. (should-error
  4793. (cl-letf (((symbol-function 'org-read-date)
  4794. (lambda (&rest args)
  4795. (apply #'encode-time
  4796. (org-parse-time-string "2014-03-04")))))
  4797. (org-test-with-temp-text "* H"
  4798. (let ((org-adapt-indentation nil)
  4799. (org-last-inserted-timestamp nil))
  4800. (org-schedule '(16)))
  4801. (buffer-string))))
  4802. ;; When a region is active and
  4803. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4804. ;; same value in all headlines in region.
  4805. (should
  4806. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4807. (org-test-with-temp-text "* H1\n* H2"
  4808. (let ((org-adapt-indentation nil)
  4809. (org-last-inserted-timestamp nil)
  4810. (org-loop-over-headlines-in-active-region t))
  4811. (transient-mark-mode 1)
  4812. (push-mark (point) t t)
  4813. (goto-char (point-max))
  4814. (org-schedule nil "2012-03-29"))
  4815. (replace-regexp-in-string
  4816. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4817. (should-not
  4818. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4819. (org-test-with-temp-text "* H1\n* H2"
  4820. (let ((org-adapt-indentation nil)
  4821. (org-last-inserted-timestamp nil)
  4822. (org-loop-over-headlines-in-active-region nil))
  4823. (transient-mark-mode 1)
  4824. (push-mark (point) t t)
  4825. (goto-char (point-max))
  4826. (org-schedule nil "2012-03-29"))
  4827. (replace-regexp-in-string
  4828. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4829. (should
  4830. ;; check if a repeater survives re-scheduling.
  4831. (string-match-p
  4832. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  4833. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  4834. (let ((org-adapt-indentation nil)
  4835. (org-last-inserted-timestamp nil))
  4836. (org-schedule nil "2017-02-01"))
  4837. (buffer-string)))))
  4838. ;;; Property API
  4839. (ert-deftest test-org/buffer-property-keys ()
  4840. "Test `org-buffer-property-keys' specifications."
  4841. ;; Retrieve properties accross siblings.
  4842. (should
  4843. (equal '("A" "B")
  4844. (org-test-with-temp-text "
  4845. * H1
  4846. :PROPERTIES:
  4847. :A: 1
  4848. :END:
  4849. * H2
  4850. :PROPERTIES:
  4851. :B: 1
  4852. :END:"
  4853. (org-buffer-property-keys))))
  4854. ;; Retrieve properties accross children.
  4855. (should
  4856. (equal '("A" "B")
  4857. (org-test-with-temp-text "
  4858. * H1
  4859. :PROPERTIES:
  4860. :A: 1
  4861. :END:
  4862. ** H2
  4863. :PROPERTIES:
  4864. :B: 1
  4865. :END:"
  4866. (org-buffer-property-keys))))
  4867. ;; Retrieve muliple properties in the same drawer.
  4868. (should
  4869. (equal '("A" "B")
  4870. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4871. (org-buffer-property-keys))))
  4872. ;; Ignore extension symbol in property name.
  4873. (should
  4874. (equal '("A")
  4875. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4876. (org-buffer-property-keys))))
  4877. ;; With non-nil COLUMNS, extract property names from columns.
  4878. (should
  4879. (equal '("A" "B")
  4880. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  4881. (org-buffer-property-keys nil nil t))))
  4882. (should
  4883. (equal '("A" "B" "COLUMNS")
  4884. (org-test-with-temp-text
  4885. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  4886. (org-buffer-property-keys nil nil t))))
  4887. ;; In COLUMNS, ignore title and summary-type.
  4888. (should
  4889. (equal '("A")
  4890. (org-test-with-temp-text "#+COLUMNS: %A(Foo)"
  4891. (org-buffer-property-keys nil nil t))))
  4892. (should
  4893. (equal '("A")
  4894. (org-test-with-temp-text "#+COLUMNS: %A{Foo}"
  4895. (org-buffer-property-keys nil nil t))))
  4896. (should
  4897. (equal '("A")
  4898. (org-test-with-temp-text "#+COLUMNS: %A(Foo){Bar}"
  4899. (org-buffer-property-keys nil nil t)))))
  4900. (ert-deftest test-org/property-values ()
  4901. "Test `org-property-values' specifications."
  4902. ;; Regular test.
  4903. (should
  4904. (equal '("2" "1")
  4905. (org-test-with-temp-text
  4906. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  4907. (org-property-values "A"))))
  4908. ;; Ignore empty values.
  4909. (should-not
  4910. (org-test-with-temp-text
  4911. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  4912. (org-property-values "A")))
  4913. ;; Take into consideration extended values.
  4914. (should
  4915. (equal '("1 2")
  4916. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4917. (org-property-values "A")))))
  4918. (ert-deftest test-org/find-property ()
  4919. "Test `org-find-property' specifications."
  4920. ;; Regular test.
  4921. (should
  4922. (= 1
  4923. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  4924. (org-find-property "prop"))))
  4925. ;; Ignore false positives.
  4926. (should
  4927. (= 27
  4928. (org-test-with-temp-text
  4929. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  4930. (org-find-property "A"))))
  4931. ;; Return first entry found in buffer.
  4932. (should
  4933. (= 1
  4934. (org-test-with-temp-text
  4935. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4936. (org-find-property "A"))))
  4937. ;; Only search visible part of the buffer.
  4938. (should
  4939. (= 31
  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-narrow-to-subtree)
  4943. (org-find-property "A"))))
  4944. ;; With optional argument, only find entries with a specific value.
  4945. (should-not
  4946. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4947. (org-find-property "A" "2")))
  4948. (should
  4949. (= 31
  4950. (org-test-with-temp-text
  4951. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  4952. (org-find-property "A" "2"))))
  4953. ;; Use "nil" for explicit nil values.
  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: nil\n:END:"
  4958. (org-find-property "A" "nil")))))
  4959. (ert-deftest test-org/entry-delete ()
  4960. "Test `org-entry-delete' specifications."
  4961. ;; Regular test.
  4962. (should
  4963. (string-match
  4964. " *:PROPERTIES:\n *:B: +2\n *:END:"
  4965. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4966. (org-entry-delete (point) "A")
  4967. (buffer-string))))
  4968. ;; Also remove accumulated properties.
  4969. (should-not
  4970. (string-match
  4971. ":A"
  4972. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  4973. (org-entry-delete (point) "A")
  4974. (buffer-string))))
  4975. ;; When last property is removed, remove the property drawer.
  4976. (should-not
  4977. (string-match
  4978. ":PROPERTIES:"
  4979. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  4980. (org-entry-delete (point) "A")
  4981. (buffer-string))))
  4982. ;; Return a non-nil value when some property was removed.
  4983. (should
  4984. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4985. (org-entry-delete (point) "A")))
  4986. (should-not
  4987. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4988. (org-entry-delete (point) "C")))
  4989. ;; Special properties cannot be located in a drawer. Allow to
  4990. ;; remove them anyway, in case of user error.
  4991. (should
  4992. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  4993. (org-entry-delete (point) "SCHEDULED"))))
  4994. (ert-deftest test-org/entry-get ()
  4995. "Test `org-entry-get' specifications."
  4996. ;; Regular test.
  4997. (should
  4998. (equal "1"
  4999. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5000. (org-entry-get (point) "A"))))
  5001. ;; Ignore case.
  5002. (should
  5003. (equal "1"
  5004. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5005. (org-entry-get (point) "a"))))
  5006. ;; Handle extended values, both before and after base value.
  5007. (should
  5008. (equal "1 2 3"
  5009. (org-test-with-temp-text
  5010. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5011. (org-entry-get (point) "A"))))
  5012. ;; Empty values are returned as the empty string.
  5013. (should
  5014. (equal ""
  5015. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  5016. (org-entry-get (point) "A"))))
  5017. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  5018. ;; otherwise, return nil.
  5019. (should-not
  5020. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5021. (org-entry-get (point) "A")))
  5022. (should
  5023. (equal "nil"
  5024. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5025. (org-entry-get (point) "A" nil t))))
  5026. ;; Return nil when no property is found, independently on the
  5027. ;; LITERAL-NIL argument.
  5028. (should-not
  5029. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5030. (org-entry-get (point) "B")))
  5031. (should-not
  5032. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5033. (org-entry-get (point) "B" nil t)))
  5034. ;; Handle inheritance, when allowed. Include extended values and
  5035. ;; possibly global values.
  5036. (should
  5037. (equal
  5038. "1"
  5039. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5040. (org-entry-get (point) "A" t))))
  5041. (should
  5042. (equal
  5043. "1"
  5044. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5045. (let ((org-use-property-inheritance t))
  5046. (org-entry-get (point) "A" 'selective)))))
  5047. (should-not
  5048. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5049. (let ((org-use-property-inheritance nil))
  5050. (org-entry-get (point) "A" 'selective))))
  5051. (should
  5052. (equal
  5053. "1 2"
  5054. (org-test-with-temp-text
  5055. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  5056. (org-entry-get (point-max) "A" t))))
  5057. (should
  5058. (equal "1"
  5059. (org-test-with-temp-text
  5060. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  5061. (org-mode-restart)
  5062. (org-entry-get (point-max) "A" t))))
  5063. (should
  5064. (equal "0 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. (ert-deftest test-org/entry-properties ()
  5070. "Test `org-entry-properties' specifications."
  5071. ;; Get "ITEM" property.
  5072. (should
  5073. (equal "H"
  5074. (org-test-with-temp-text "* TODO H"
  5075. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  5076. (should
  5077. (equal "H"
  5078. (org-test-with-temp-text "* TODO H"
  5079. (cdr (assoc "ITEM" (org-entry-properties))))))
  5080. ;; Get "TODO" property. TODO keywords are case sensitive.
  5081. (should
  5082. (equal "TODO"
  5083. (org-test-with-temp-text "* TODO H"
  5084. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  5085. (should
  5086. (equal "TODO"
  5087. (org-test-with-temp-text "* TODO H"
  5088. (cdr (assoc "TODO" (org-entry-properties))))))
  5089. (should-not
  5090. (org-test-with-temp-text "* H"
  5091. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5092. (should-not
  5093. (org-test-with-temp-text "* todo H"
  5094. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5095. ;; Get "PRIORITY" property.
  5096. (should
  5097. (equal "A"
  5098. (org-test-with-temp-text "* [#A] H"
  5099. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5100. (should
  5101. (equal "A"
  5102. (org-test-with-temp-text "* [#A] H"
  5103. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  5104. (should
  5105. (equal (char-to-string org-default-priority)
  5106. (org-test-with-temp-text "* H"
  5107. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5108. ;; Get "FILE" property.
  5109. (should
  5110. (org-test-with-temp-text-in-file "* H\nParagraph"
  5111. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  5112. (buffer-file-name))))
  5113. (should
  5114. (org-test-with-temp-text-in-file "* H\nParagraph"
  5115. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  5116. (buffer-file-name))))
  5117. (should-not
  5118. (org-test-with-temp-text "* H\nParagraph"
  5119. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  5120. ;; Get "TAGS" property.
  5121. (should
  5122. (equal ":tag1:tag2:"
  5123. (org-test-with-temp-text "* H :tag1:tag2:"
  5124. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  5125. (should
  5126. (equal ":tag1:tag2:"
  5127. (org-test-with-temp-text "* H :tag1:tag2:"
  5128. (cdr (assoc "TAGS" (org-entry-properties))))))
  5129. (should-not
  5130. (org-test-with-temp-text "* H"
  5131. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  5132. ;; Get "ALLTAGS" property.
  5133. (should
  5134. (equal ":tag1:tag2:"
  5135. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5136. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  5137. (should
  5138. (equal ":tag1:tag2:"
  5139. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5140. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  5141. (should-not
  5142. (org-test-with-temp-text "* H"
  5143. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  5144. ;; Get "BLOCKED" property.
  5145. (should
  5146. (equal "t"
  5147. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  5148. (let ((org-enforce-todo-dependencies t)
  5149. (org-blocker-hook
  5150. '(org-block-todo-from-children-or-siblings-or-parent)))
  5151. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5152. (should
  5153. (equal ""
  5154. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  5155. (let ((org-enforce-todo-dependencies t)
  5156. (org-blocker-hook
  5157. '(org-block-todo-from-children-or-siblings-or-parent)))
  5158. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5159. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  5160. (should
  5161. (equal
  5162. "[2012-03-29 thu.]"
  5163. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5164. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  5165. (should
  5166. (equal
  5167. "[2012-03-29 thu.]"
  5168. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5169. (cdr (assoc "CLOSED" (org-entry-properties))))))
  5170. (should-not
  5171. (org-test-with-temp-text "* H"
  5172. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  5173. (should
  5174. (equal
  5175. "<2014-03-04 tue.>"
  5176. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5177. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  5178. (should
  5179. (equal
  5180. "<2014-03-04 tue.>"
  5181. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5182. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  5183. (should-not
  5184. (org-test-with-temp-text "* H"
  5185. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  5186. (should
  5187. (equal
  5188. "<2014-03-04 tue.>"
  5189. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5190. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  5191. (should
  5192. (equal
  5193. "<2014-03-04 tue.>"
  5194. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5195. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  5196. (should-not
  5197. (org-test-with-temp-text "* H"
  5198. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  5199. ;; Get "CATEGORY"
  5200. (should
  5201. (equal "cat"
  5202. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5203. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  5204. (should
  5205. (equal "cat"
  5206. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5207. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5208. (should
  5209. (equal "cat"
  5210. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  5211. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5212. (should
  5213. (equal "cat2"
  5214. (org-test-with-temp-text
  5215. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  5216. "\n"
  5217. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  5218. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5219. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  5220. (should
  5221. (equal "<2012-03-29 thu.>"
  5222. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  5223. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5224. (should
  5225. (equal "[2012-03-29 thu.]"
  5226. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  5227. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  5228. (should
  5229. (equal "<2012-03-29 thu.>"
  5230. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  5231. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  5232. (should
  5233. (equal "[2014-03-04 tue.]"
  5234. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  5235. (cdr (assoc "TIMESTAMP_IA"
  5236. (org-entry-properties nil "TIMESTAMP_IA"))))))
  5237. (should-not
  5238. (equal "<2012-03-29 thu.>"
  5239. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  5240. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5241. ;; Get standard properties.
  5242. (should
  5243. (equal "1"
  5244. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5245. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5246. ;; Handle extended properties.
  5247. (should
  5248. (equal "1 2 3"
  5249. (org-test-with-temp-text
  5250. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5251. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5252. (should
  5253. (equal "1 2 3"
  5254. (org-test-with-temp-text
  5255. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  5256. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5257. ;; Ignore forbidden (special) properties.
  5258. (should-not
  5259. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  5260. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  5261. (ert-deftest test-org/entry-put ()
  5262. "Test `org-entry-put' specifications."
  5263. ;; Error when not a string or nil.
  5264. (should-error
  5265. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5266. (org-entry-put 1 "test" 2)))
  5267. ;; Error when property name is invalid.
  5268. (should-error
  5269. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5270. (org-entry-put 1 "no space" "value")))
  5271. (should-error
  5272. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5273. (org-entry-put 1 "" "value")))
  5274. ;; Set "TODO" property.
  5275. (should
  5276. (string-match (regexp-quote " TODO H")
  5277. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5278. (org-entry-put (point) "TODO" "TODO")
  5279. (buffer-string))))
  5280. (should
  5281. (string-match (regexp-quote "* H")
  5282. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5283. (org-entry-put (point) "TODO" nil)
  5284. (buffer-string))))
  5285. ;; Set "PRIORITY" property.
  5286. (should
  5287. (equal "* [#A] H"
  5288. (org-test-with-temp-text "* [#B] H"
  5289. (org-entry-put (point) "PRIORITY" "A")
  5290. (buffer-string))))
  5291. (should
  5292. (equal "* H"
  5293. (org-test-with-temp-text "* [#B] H"
  5294. (org-entry-put (point) "PRIORITY" nil)
  5295. (buffer-string))))
  5296. ;; Set "SCHEDULED" property.
  5297. (should
  5298. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  5299. (org-test-with-temp-text "* H"
  5300. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  5301. (buffer-string))))
  5302. (should
  5303. (string= "* H\n"
  5304. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5305. (org-entry-put (point) "SCHEDULED" nil)
  5306. (buffer-string))))
  5307. (should
  5308. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  5309. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5310. (org-entry-put (point) "SCHEDULED" "earlier")
  5311. (buffer-string))))
  5312. (should
  5313. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  5314. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5315. (org-entry-put (point) "SCHEDULED" "later")
  5316. (buffer-string))))
  5317. ;; Set "DEADLINE" property.
  5318. (should
  5319. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  5320. (org-test-with-temp-text "* H"
  5321. (org-entry-put (point) "DEADLINE" "2014-03-04")
  5322. (buffer-string))))
  5323. (should
  5324. (string= "* H\n"
  5325. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5326. (org-entry-put (point) "DEADLINE" nil)
  5327. (buffer-string))))
  5328. (should
  5329. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  5330. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5331. (org-entry-put (point) "DEADLINE" "earlier")
  5332. (buffer-string))))
  5333. (should
  5334. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  5335. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5336. (org-entry-put (point) "DEADLINE" "later")
  5337. (buffer-string))))
  5338. ;; Set "CATEGORY" property
  5339. (should
  5340. (string-match "^ *:CATEGORY: cat"
  5341. (org-test-with-temp-text "* H"
  5342. (org-entry-put (point) "CATEGORY" "cat")
  5343. (buffer-string))))
  5344. ;; Regular properties, with or without pre-existing drawer.
  5345. (should
  5346. (string-match "^ *:A: +2$"
  5347. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5348. (org-entry-put (point) "A" "2")
  5349. (buffer-string))))
  5350. (should
  5351. (string-match "^ *:A: +1$"
  5352. (org-test-with-temp-text "* H"
  5353. (org-entry-put (point) "A" "1")
  5354. (buffer-string))))
  5355. ;; Special case: two consecutive headlines.
  5356. (should
  5357. (string-match "\\* A\n *:PROPERTIES:"
  5358. (org-test-with-temp-text "* A\n** B"
  5359. (org-entry-put (point) "A" "1")
  5360. (buffer-string)))))
  5361. (ert-deftest test-org/refresh-properties ()
  5362. "Test `org-refresh-properties' specifications."
  5363. (should
  5364. (equal "1"
  5365. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5366. (org-refresh-properties "A" 'org-test)
  5367. (get-text-property (point) 'org-test))))
  5368. (should-not
  5369. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5370. (org-refresh-properties "B" 'org-test)
  5371. (get-text-property (point) 'org-test)))
  5372. ;; Handle properties only defined with extension syntax, i.e.,
  5373. ;; "PROPERTY+".
  5374. (should
  5375. (equal "1"
  5376. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5377. (org-refresh-properties "A" 'org-test)
  5378. (get-text-property (point) 'org-test))))
  5379. ;; When property is inherited, add text property to the whole
  5380. ;; sub-tree.
  5381. (should
  5382. (equal "1"
  5383. (org-test-with-temp-text
  5384. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  5385. (let ((org-use-property-inheritance t))
  5386. (org-refresh-properties "A" 'org-test))
  5387. (get-text-property (point) 'org-test))))
  5388. ;; When property is inherited, use global value across the whole
  5389. ;; buffer. However local values have precedence.
  5390. (should-not
  5391. (equal "1"
  5392. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5393. (org-mode-restart)
  5394. (let ((org-use-property-inheritance nil))
  5395. (org-refresh-properties "A" 'org-test))
  5396. (get-text-property (point) 'org-test))))
  5397. (should
  5398. (equal "1"
  5399. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5400. (org-mode-restart)
  5401. (let ((org-use-property-inheritance t))
  5402. (org-refresh-properties "A" 'org-test))
  5403. (get-text-property (point) 'org-test))))
  5404. (should
  5405. (equal "2"
  5406. (org-test-with-temp-text
  5407. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  5408. (org-mode-restart)
  5409. (let ((org-use-property-inheritance t))
  5410. (org-refresh-properties "A" 'org-test))
  5411. (get-text-property (point) 'org-test)))))
  5412. ;;; Radio Targets
  5413. (ert-deftest test-org/update-radio-target-regexp ()
  5414. "Test `org-update-radio-target-regexp' specifications."
  5415. ;; Properly update cache with no previous radio target regexp.
  5416. (should
  5417. (eq 'link
  5418. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5419. (save-excursion (goto-char (point-max)) (org-element-context))
  5420. (insert "<<<")
  5421. (search-forward "o")
  5422. (insert ">>>")
  5423. (org-update-radio-target-regexp)
  5424. (goto-char (point-max))
  5425. (org-element-type (org-element-context)))))
  5426. ;; Properly update cache with previous radio target regexp.
  5427. (should
  5428. (eq 'link
  5429. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5430. (save-excursion (goto-char (point-max)) (org-element-context))
  5431. (insert "<<<")
  5432. (search-forward "o")
  5433. (insert ">>>")
  5434. (org-update-radio-target-regexp)
  5435. (search-backward "r")
  5436. (delete-char 5)
  5437. (insert "new")
  5438. (org-update-radio-target-regexp)
  5439. (goto-char (point-max))
  5440. (delete-region (line-beginning-position) (point))
  5441. (insert "new")
  5442. (org-element-type (org-element-context))))))
  5443. ;;; Refile
  5444. (ert-deftest test-org/refile-get-targets ()
  5445. "Test `org-refile-get-targets' specifications."
  5446. ;; :maxlevel includes all headings above specified value.
  5447. (should
  5448. (equal '("H1" "H2" "H3")
  5449. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5450. (let ((org-refile-use-outline-path nil)
  5451. (org-refile-targets `((nil :maxlevel . 3))))
  5452. (mapcar #'car (org-refile-get-targets))))))
  5453. (should
  5454. (equal '("H1" "H2")
  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 . 2))))
  5458. (mapcar #'car (org-refile-get-targets))))))
  5459. ;; :level limits targets to headlines with the specified level.
  5460. (should
  5461. (equal '("H2")
  5462. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5463. (let ((org-refile-use-outline-path nil)
  5464. (org-refile-targets `((nil :level . 2))))
  5465. (mapcar #'car (org-refile-get-targets))))))
  5466. ;; :tag limits targets to headlines with specified tag.
  5467. (should
  5468. (equal '("H1")
  5469. (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
  5470. (let ((org-refile-use-outline-path nil)
  5471. (org-refile-targets `((nil :tag . "foo"))))
  5472. (mapcar #'car (org-refile-get-targets))))))
  5473. ;; :todo limits targets to headlines with specified TODO keyword.
  5474. (should
  5475. (equal '("H2")
  5476. (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
  5477. (let ((org-refile-use-outline-path nil)
  5478. (org-refile-targets `((nil :todo . "TODO"))))
  5479. (mapcar #'car (org-refile-get-targets))))))
  5480. ;; :regexp filters targets matching provided regexp.
  5481. (should
  5482. (equal '("F2" "F3")
  5483. (org-test-with-temp-text "* H1\n** F2\n*** F3"
  5484. (let ((org-refile-use-outline-path nil)
  5485. (org-refile-targets `((nil :regexp . "F"))))
  5486. (mapcar #'car (org-refile-get-targets))))))
  5487. ;; A nil `org-refile-targets' includes only top level headlines in
  5488. ;; current buffer.
  5489. (should
  5490. (equal '("H1" "H2")
  5491. (org-test-with-temp-text "* H1\n** S1\n* H2"
  5492. (let ((org-refile-use-outline-path nil)
  5493. (org-refile-targets nil))
  5494. (mapcar #'car (org-refile-get-targets))))))
  5495. ;; Return value is the union of the targets according to all the
  5496. ;; defined rules. However, prevent duplicates.
  5497. (should
  5498. (equal '("F2" "F3" "H1")
  5499. (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
  5500. (let ((org-refile-use-outline-path nil)
  5501. (org-refile-targets `((nil :regexp . "F")
  5502. (nil :todo . "TODO"))))
  5503. (mapcar #'car (org-refile-get-targets))))))
  5504. (should
  5505. (equal '("F2" "F3" "H1")
  5506. (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
  5507. (let ((org-refile-use-outline-path nil)
  5508. (org-refile-targets `((nil :regexp . "F")
  5509. (nil :todo . "TODO"))))
  5510. (mapcar #'car (org-refile-get-targets))))))
  5511. ;; When `org-refile-use-outline-path' is non-nil, provide targets as
  5512. ;; paths.
  5513. (should
  5514. (equal '("H1" "H1/H2" "H1/H2/H3")
  5515. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5516. (let ((org-refile-use-outline-path t)
  5517. (org-refile-targets `((nil :maxlevel . 3))))
  5518. (mapcar #'car (org-refile-get-targets))))))
  5519. ;; When providing targets as paths, escape forward slashes in
  5520. ;; headings with backslashes.
  5521. (should
  5522. (equal '("H1\\/foo")
  5523. (org-test-with-temp-text "* H1/foo"
  5524. (let ((org-refile-use-outline-path t)
  5525. (org-refile-targets `((nil :maxlevel . 1))))
  5526. (mapcar #'car (org-refile-get-targets))))))
  5527. ;; When `org-refile-use-outline-path' is `file', include file name
  5528. ;; without directory in targets.
  5529. (should
  5530. (org-test-with-temp-text-in-file "* H1"
  5531. (let* ((filename (buffer-file-name))
  5532. (org-refile-use-outline-path 'file)
  5533. (org-refile-targets `(((,filename) :level . 1))))
  5534. (member (file-name-nondirectory filename)
  5535. (mapcar #'car (org-refile-get-targets))))))
  5536. ;; When `org-refile-use-outline-path' is `full-file-path', include
  5537. ;; full file name.
  5538. (should
  5539. (org-test-with-temp-text-in-file "* H1"
  5540. (let* ((filename (file-truename (buffer-file-name)))
  5541. (org-refile-use-outline-path 'full-file-path)
  5542. (org-refile-targets `(((,filename) :level . 1))))
  5543. (member filename (mapcar #'car (org-refile-get-targets))))))
  5544. ;; When `org-refile-use-outline-path' is `buffer-name', include
  5545. ;; buffer name.
  5546. (should
  5547. (org-test-with-temp-text "* H1"
  5548. (let* ((org-refile-use-outline-path 'buffer-name)
  5549. (org-refile-targets `((nil :level . 1))))
  5550. (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
  5551. ;;; Sparse trees
  5552. (ert-deftest test-org/match-sparse-tree ()
  5553. "Test `org-match-sparse-tree' specifications."
  5554. ;; Match tags.
  5555. (should-not
  5556. (org-test-with-temp-text "* H\n** H1 :tag:"
  5557. (org-match-sparse-tree nil "tag")
  5558. (search-forward "H1")
  5559. (org-invisible-p2)))
  5560. (should
  5561. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  5562. (org-match-sparse-tree nil "tag")
  5563. (search-forward "H2")
  5564. (org-invisible-p2)))
  5565. ;; "-" operator for tags.
  5566. (should-not
  5567. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5568. (org-match-sparse-tree nil "tag1-tag2")
  5569. (search-forward "H1")
  5570. (org-invisible-p2)))
  5571. (should
  5572. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5573. (org-match-sparse-tree nil "tag1-tag2")
  5574. (search-forward "H2")
  5575. (org-invisible-p2)))
  5576. ;; "&" operator for tags.
  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 "H1")
  5581. (org-invisible-p2)))
  5582. (should-not
  5583. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5584. (org-match-sparse-tree nil "tag1&tag2")
  5585. (search-forward "H2")
  5586. (org-invisible-p2)))
  5587. ;; "|" operator for tags.
  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 "H1")
  5592. (org-invisible-p2)))
  5593. (should-not
  5594. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5595. (org-match-sparse-tree nil "tag1|tag2")
  5596. (search-forward "H2")
  5597. (org-invisible-p2)))
  5598. ;; Regexp match on tags.
  5599. (should-not
  5600. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5601. (org-match-sparse-tree nil "{^tag.*}")
  5602. (search-forward "H1")
  5603. (org-invisible-p2)))
  5604. (should
  5605. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5606. (org-match-sparse-tree nil "{^tag.*}")
  5607. (search-forward "H2")
  5608. (org-invisible-p2)))
  5609. ;; Match group tags.
  5610. (should-not
  5611. (org-test-with-temp-text
  5612. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5613. (org-match-sparse-tree nil "work")
  5614. (search-forward "H1")
  5615. (org-invisible-p2)))
  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 "H2")
  5621. (org-invisible-p2)))
  5622. ;; Match group tags with hard brackets.
  5623. (should-not
  5624. (org-test-with-temp-text
  5625. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5626. (org-match-sparse-tree nil "work")
  5627. (search-forward "H1")
  5628. (org-invisible-p2)))
  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 "H2")
  5634. (org-invisible-p2)))
  5635. ;; Match tags in hierarchies
  5636. (should-not
  5637. (org-test-with-temp-text
  5638. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5639. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5640. #+TAGS: { Lev_3 : Lev_4 }\n
  5641. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5642. (org-match-sparse-tree nil "Lev_1")
  5643. (search-forward "H4")
  5644. (org-invisible-p2)))
  5645. (should-not
  5646. (org-test-with-temp-text
  5647. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5648. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5649. #+TAGS: { Lev_3 : Lev_4 }\n
  5650. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5651. (org-match-sparse-tree nil "Lev_1+Lev_3")
  5652. (search-forward "H4")
  5653. (org-invisible-p2)))
  5654. ;; Match regular expressions in tags
  5655. (should-not
  5656. (org-test-with-temp-text
  5657. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  5658. (org-match-sparse-tree nil "Lev")
  5659. (search-forward "H1")
  5660. (org-invisible-p2)))
  5661. (should
  5662. (org-test-with-temp-text
  5663. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  5664. (org-match-sparse-tree nil "Lev")
  5665. (search-forward "H1")
  5666. (org-invisible-p2)))
  5667. ;; Match properties.
  5668. (should
  5669. (org-test-with-temp-text
  5670. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  5671. (org-match-sparse-tree nil "A=\"1\"")
  5672. (search-forward "H2")
  5673. (org-invisible-p2)))
  5674. (should-not
  5675. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5676. (org-match-sparse-tree nil "A=\"1\"")
  5677. (search-forward "H2")
  5678. (org-invisible-p2)))
  5679. ;; Case is not significant when matching properties.
  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. (should-not
  5686. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  5687. (org-match-sparse-tree nil "A=\"1\"")
  5688. (search-forward "H2")
  5689. (org-invisible-p2)))
  5690. ;; Match special LEVEL property.
  5691. (should-not
  5692. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5693. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5694. (search-forward "H1")
  5695. (org-invisible-p2)))
  5696. (should
  5697. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5698. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5699. (search-forward "H2")
  5700. (org-invisible-p2)))
  5701. ;; Comparison operators when matching properties.
  5702. (should
  5703. (org-test-with-temp-text
  5704. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5705. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5706. (search-forward "H1")
  5707. (org-invisible-p2)))
  5708. (should-not
  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 "H2")
  5713. (org-invisible-p2)))
  5714. ;; Regexp match on properties values.
  5715. (should-not
  5716. (org-test-with-temp-text
  5717. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5718. (org-match-sparse-tree nil "A={f.*}")
  5719. (search-forward "H1")
  5720. (org-invisible-p2)))
  5721. (should
  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 "H2")
  5726. (org-invisible-p2)))
  5727. ;; With an optional argument, limit match to TODO entries.
  5728. (should-not
  5729. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5730. (org-match-sparse-tree t "tag")
  5731. (search-forward "H1")
  5732. (org-invisible-p2)))
  5733. (should
  5734. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5735. (org-match-sparse-tree t "tag")
  5736. (search-forward "H2")
  5737. (org-invisible-p2))))
  5738. (ert-deftest test-org/occur ()
  5739. "Test `org-occur' specifications."
  5740. ;; Count number of matches.
  5741. (should
  5742. (= 1
  5743. (org-test-with-temp-text "* H\nA\n* H2"
  5744. (org-occur "A"))))
  5745. (should
  5746. (= 2
  5747. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5748. (org-occur "A"))))
  5749. ;; Test CALLBACK optional argument.
  5750. (should
  5751. (= 0
  5752. (org-test-with-temp-text "* H\nA\n* H2"
  5753. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5754. (should
  5755. (= 1
  5756. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5757. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5758. ;; Case-fold searches according to `org-occur-case-fold-search'.
  5759. (should
  5760. (= 2
  5761. (org-test-with-temp-text "Aa"
  5762. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  5763. (should
  5764. (= 2
  5765. (org-test-with-temp-text "Aa"
  5766. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  5767. (should
  5768. (= 1
  5769. (org-test-with-temp-text "Aa"
  5770. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  5771. (should
  5772. (= 1
  5773. (org-test-with-temp-text "Aa"
  5774. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  5775. (should
  5776. (= 1
  5777. (org-test-with-temp-text "Aa"
  5778. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  5779. (should
  5780. (= 2
  5781. (org-test-with-temp-text "Aa"
  5782. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  5783. ;;; Tags
  5784. (ert-deftest test-org/tag-string-to-alist ()
  5785. "Test `org-tag-string-to-alist' specifications."
  5786. ;; Tag without selection key.
  5787. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  5788. ;; Tag with selection key.
  5789. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  5790. ;; Tag group.
  5791. (should
  5792. (equal
  5793. (org-tag-string-to-alist "[ group : t1 t2 ]")
  5794. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  5795. ;; Mutually exclusive tags.
  5796. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  5797. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  5798. (should
  5799. (equal
  5800. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  5801. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  5802. (ert-deftest test-org/tag-alist-to-string ()
  5803. "Test `org-tag-alist-to-string' specifications."
  5804. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  5805. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  5806. (should
  5807. (equal
  5808. (org-tag-alist-to-string
  5809. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5810. "[ group : t1 t2 ]"))
  5811. (should
  5812. (equal (org-tag-alist-to-string
  5813. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  5814. "{ tag1 tag2 }"))
  5815. (should
  5816. (equal
  5817. (org-tag-alist-to-string
  5818. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  5819. "{ group : tag1 tag2 }")))
  5820. (ert-deftest test-org/tag-alist-to-groups ()
  5821. "Test `org-tag-alist-to-groups' specifications."
  5822. (should
  5823. (equal (org-tag-alist-to-groups
  5824. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  5825. '(("group" "t1" "t2"))))
  5826. (should
  5827. (equal
  5828. (org-tag-alist-to-groups
  5829. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5830. '(("group" "t1" "t2"))))
  5831. (should-not
  5832. (org-tag-alist-to-groups
  5833. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  5834. (ert-deftest test-org/tag-align ()
  5835. "Test tags alignment."
  5836. ;; Test aligning tags with different display width.
  5837. (should
  5838. ;; 12345678901234567890
  5839. (equal "* Test :abc:"
  5840. (org-test-with-temp-text "* Test :abc:"
  5841. (let ((org-tags-column -20)
  5842. (indent-tabs-mode nil))
  5843. (org-fix-tags-on-the-fly))
  5844. (buffer-string))))
  5845. (should
  5846. ;; 12345678901234567890
  5847. (equal "* Test :日本語:"
  5848. (org-test-with-temp-text "* Test :日本語:"
  5849. (let ((org-tags-column -20)
  5850. (indent-tabs-mode nil))
  5851. (org-fix-tags-on-the-fly))
  5852. (buffer-string))))
  5853. ;; Make sure aligning tags do not skip invisible text.
  5854. (should
  5855. (equal "* [[linkx]] :tag:"
  5856. (org-test-with-temp-text "* [[link<point>]] :tag:"
  5857. (let ((org-tags-column 0))
  5858. (org-fix-tags-on-the-fly)
  5859. (insert "x")
  5860. (buffer-string))))))
  5861. (ert-deftest test-org/get-tags ()
  5862. "Test `org-get-tags' specifications."
  5863. ;; Standard test.
  5864. (should
  5865. (equal '("foo")
  5866. (org-test-with-temp-text "* Test :foo:" (org-get-tags))))
  5867. (should
  5868. (equal '("foo" "bar")
  5869. (org-test-with-temp-text "* Test :foo:bar:" (org-get-tags))))
  5870. ;; Return nil when there is no tag.
  5871. (should-not
  5872. (org-test-with-temp-text "* Test" (org-get-tags)))
  5873. ;; Tags are inherited from parent headlines.
  5874. (should
  5875. (equal '("tag")
  5876. (let ((org-use-tag-inheritance t))
  5877. (org-test-with-temp-text "* H0 :foo:\n* H1 :tag:\n<point>** H2"
  5878. (org-get-tags)))))
  5879. ;; Tags are inherited from `org-file-tags'.
  5880. (should
  5881. (equal '("tag")
  5882. (org-test-with-temp-text "* H1"
  5883. (let ((org-file-tags '("tag"))
  5884. (org-use-tag-inheritance t))
  5885. (org-get-tags)))))
  5886. ;; Only inherited tags have the `inherited' text property.
  5887. (should
  5888. (get-text-property 0 'inherited
  5889. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5890. (let ((org-use-tag-inheritance t))
  5891. (assoc-string "foo" (org-get-tags))))))
  5892. (should-not
  5893. (get-text-property 0 'inherited
  5894. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5895. (let ((org-use-tag-inheritance t))
  5896. (assoc-string "bar" (org-get-tags))))))
  5897. ;; Obey to `org-use-tag-inheritance'.
  5898. (should-not
  5899. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5900. (let ((org-use-tag-inheritance nil))
  5901. (assoc-string "foo" (org-get-tags)))))
  5902. (should-not
  5903. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5904. (let ((org-use-tag-inheritance nil)
  5905. (org-file-tags '("foo")))
  5906. (assoc-string "foo" (org-get-tags)))))
  5907. (should-not
  5908. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5909. (let ((org-use-tag-inheritance '("bar")))
  5910. (assoc-string "foo" (org-get-tags)))))
  5911. (should
  5912. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5913. (let ((org-use-tag-inheritance '("bar")))
  5914. (assoc-string "bar" (org-get-tags)))))
  5915. (should-not
  5916. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5917. (let ((org-use-tag-inheritance "b.*"))
  5918. (assoc-string "foo" (org-get-tags)))))
  5919. (should
  5920. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5921. (let ((org-use-tag-inheritance "b.*"))
  5922. (assoc-string "bar" (org-get-tags)))))
  5923. ;; When optional argument LOCAL is non-nil, ignore tag inheritance.
  5924. (should
  5925. (equal '("baz")
  5926. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5927. (let ((org-use-tag-inheritance t))
  5928. (org-get-tags nil t)))))
  5929. ;; When optional argument POS is non-nil, get tags there instead.
  5930. (should
  5931. (equal '("foo")
  5932. (org-test-with-temp-text "* H1 :foo:\n* <point>H2 :bar:"
  5933. (org-get-tags 1))))
  5934. ;; Pathological case: tagged headline with an empty body.
  5935. (should (org-test-with-temp-text "* :tag:" (org-get-tags))))
  5936. (ert-deftest test-org/set-tags ()
  5937. "Test `org-set-tags' specifications."
  5938. ;; Throw an error on invalid data.
  5939. (should-error
  5940. (org-test-with-temp-text "* H"
  5941. (org-set-tags 'foo)))
  5942. ;; `nil', an empty, and a blank string remove all tags.
  5943. (should
  5944. (equal "* H"
  5945. (org-test-with-temp-text "* H :tag1:tag2:"
  5946. (org-set-tags nil)
  5947. (buffer-string))))
  5948. (should
  5949. (equal "* H"
  5950. (org-test-with-temp-text "* H :tag1:tag2:"
  5951. (org-set-tags "")
  5952. (buffer-string))))
  5953. (should
  5954. (equal "* H"
  5955. (org-test-with-temp-text "* H :tag1:tag2:"
  5956. (org-set-tags " ")
  5957. (buffer-string))))
  5958. ;; If there's nothing to remove, just bail out.
  5959. (should
  5960. (equal "* H"
  5961. (org-test-with-temp-text "* H"
  5962. (org-set-tags nil)
  5963. (buffer-string))))
  5964. (should
  5965. (equal "* "
  5966. (org-test-with-temp-text "* "
  5967. (org-set-tags nil)
  5968. (buffer-string))))
  5969. ;; If DATA is a tag string, set current tags to it, even if it means
  5970. ;; replacing old tags.
  5971. (should
  5972. (equal "* H :tag0:"
  5973. (org-test-with-temp-text "* H :tag1:tag2:"
  5974. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  5975. (buffer-string))))
  5976. (should
  5977. (equal "* H :tag0:"
  5978. (org-test-with-temp-text "* H"
  5979. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  5980. (buffer-string))))
  5981. ;; If DATA is a list, set tags to this list, even if it means
  5982. ;; replacing old tags.
  5983. (should
  5984. (equal "* H :tag0:"
  5985. (org-test-with-temp-text "* H :tag1:tag2:"
  5986. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  5987. (buffer-string))))
  5988. (should
  5989. (equal "* H :tag0:"
  5990. (org-test-with-temp-text "* H"
  5991. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  5992. (buffer-string))))
  5993. ;; When set, apply `org-tags-sort-function'.
  5994. (should
  5995. (equal "* H :a:b:"
  5996. (org-test-with-temp-text "* H"
  5997. (let ((org-tags-column 1)
  5998. (org-tags-sort-function #'string<))
  5999. (org-set-tags '("b" "a"))
  6000. (buffer-string)))))
  6001. ;; When new tags are identical to the previous ones, still align.
  6002. (should
  6003. (equal "* H :foo:"
  6004. (org-test-with-temp-text "* H :foo:"
  6005. (let ((org-tags-column 1))
  6006. (org-set-tags '("foo"))
  6007. (buffer-string)))))
  6008. ;; When tags have been changed, run `org-after-tags-change-hook'.
  6009. (should
  6010. (catch :return
  6011. (org-test-with-temp-text "* H :foo:"
  6012. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6013. (org-set-tags '("bar"))
  6014. nil))))
  6015. (should-not
  6016. (catch :return
  6017. (org-test-with-temp-text "* H :foo:"
  6018. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6019. (org-set-tags '("foo"))
  6020. nil))))
  6021. ;; Special case: handle empty headlines.
  6022. (should
  6023. (equal "* :tag0:"
  6024. (org-test-with-temp-text "* "
  6025. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6026. (buffer-string))))
  6027. ;; Pathological case: when setting tags of a folded headline, do not
  6028. ;; let new tags being sucked into invisibility.
  6029. (should-not
  6030. (org-test-with-temp-text "* H1\nContent\n* H2\n\n Other Content"
  6031. ;; Show only headlines
  6032. (org-content)
  6033. ;; Set NEXT tag on current entry
  6034. (org-set-tags ":NEXT:")
  6035. ;; Move point to that NEXT tag
  6036. (search-forward "NEXT") (backward-word)
  6037. ;; And it should be visible (i.e. no overlays)
  6038. (overlays-at (point)))))
  6039. (ert-deftest test-org/set-tags-command ()
  6040. "Test `org-set-tags-command' specifications"
  6041. ;; Set tags at current headline.
  6042. (should
  6043. (equal "* H1 :foo:"
  6044. (org-test-with-temp-text "* H1"
  6045. (cl-letf (((symbol-function 'completing-read)
  6046. (lambda (&rest args) ":foo:")))
  6047. (let ((org-use-fast-tag-selection nil)
  6048. (org-tags-column 1))
  6049. (org-set-tags-command)))
  6050. (buffer-string))))
  6051. ;; Preserve position when called from the section below.
  6052. (should
  6053. (equal "* H1 :foo:\nContents"
  6054. (org-test-with-temp-text "* H1\n<point>Contents"
  6055. (cl-letf (((symbol-function 'completing-read)
  6056. (lambda (&rest args) ":foo:")))
  6057. (let ((org-use-fast-tag-selection nil)
  6058. (org-tags-column 1))
  6059. (org-set-tags-command)))
  6060. (buffer-string))))
  6061. (should-not
  6062. (equal "* H1 :foo:\nContents2"
  6063. (org-test-with-temp-text "* H1\n<point>Contents2"
  6064. (cl-letf (((symbol-function 'completing-read)
  6065. (lambda (&rest args) ":foo:")))
  6066. (let ((org-use-fast-tag-selection nil)
  6067. (org-tags-column 1))
  6068. (org-set-tags-command)))
  6069. (org-at-heading-p))))
  6070. ;; Strip all forbidden characters from user-entered tags.
  6071. (should
  6072. (equal "* H1 :foo:"
  6073. (org-test-with-temp-text "* H1"
  6074. (cl-letf (((symbol-function 'completing-read)
  6075. (lambda (&rest args) ": foo *:")))
  6076. (let ((org-use-fast-tag-selection nil)
  6077. (org-tags-column 1))
  6078. (org-set-tags-command)))
  6079. (buffer-string))))
  6080. ;; When a region is active and
  6081. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  6082. ;; same value in all headlines in region.
  6083. (should
  6084. (equal "* H1 :foo:\nContents\n* H2 :foo:"
  6085. (org-test-with-temp-text "* H1\nContents\n* H2"
  6086. (cl-letf (((symbol-function 'completing-read)
  6087. (lambda (&rest args) ":foo:")))
  6088. (let ((org-use-fast-tag-selection nil)
  6089. (org-loop-over-headlines-in-active-region t)
  6090. (org-tags-column 1))
  6091. (transient-mark-mode 1)
  6092. (push-mark (point) t t)
  6093. (goto-char (point-max))
  6094. (org-set-tags-command)))
  6095. (buffer-string))))
  6096. (should
  6097. (equal "* H1\nContents\n* H2 :foo:"
  6098. (org-test-with-temp-text "* H1\nContents\n* H2"
  6099. (cl-letf (((symbol-function 'completing-read)
  6100. (lambda (&rest args) ":foo:")))
  6101. (let ((org-use-fast-tag-selection nil)
  6102. (org-loop-over-headlines-in-active-region nil)
  6103. (org-tags-column 1))
  6104. (transient-mark-mode 1)
  6105. (push-mark (point) t t)
  6106. (goto-char (point-max))
  6107. (org-set-tags-command)))
  6108. (buffer-string))))
  6109. ;; With a non-nil prefix argument, align all tags in the buffer.
  6110. (should
  6111. (equal "* H1 :foo:\n* H2 :bar:"
  6112. (org-test-with-temp-text "* H1 :foo:\n* H2 :bar:"
  6113. (let ((org-tags-column 1)) (org-set-tags-command t))
  6114. (buffer-string)))))
  6115. (ert-deftest test-org/toggle-tag ()
  6116. "Test `org-toggle-tag' specifications."
  6117. ;; Insert missing tag.
  6118. (should
  6119. (equal "* H :tag:"
  6120. (org-test-with-temp-text "* H"
  6121. (let ((org-tags-column 1)) (org-toggle-tag "tag"))
  6122. (buffer-string))))
  6123. (should
  6124. (equal "* H :tag1:tag2:"
  6125. (org-test-with-temp-text "* H :tag1:"
  6126. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6127. (buffer-string))))
  6128. ;; Remove existing tag.
  6129. (should
  6130. (equal "* H"
  6131. (org-test-with-temp-text "* H :tag:"
  6132. (org-toggle-tag "tag")
  6133. (buffer-string))))
  6134. (should
  6135. (equal "* H :tag1:"
  6136. (org-test-with-temp-text "* H :tag1:tag2:"
  6137. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6138. (buffer-string))))
  6139. (should
  6140. (equal "* H :tag2:"
  6141. (org-test-with-temp-text "* H :tag1:tag2:"
  6142. (let ((org-tags-column 1)) (org-toggle-tag "tag1"))
  6143. (buffer-string))))
  6144. ;; With optional argument ONOFF set to `on', try to insert the tag,
  6145. ;; even if its already there.
  6146. (should
  6147. (equal "* H :tag:"
  6148. (org-test-with-temp-text "* H"
  6149. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6150. (buffer-string))))
  6151. (should
  6152. (equal "* H :tag:"
  6153. (org-test-with-temp-text "* H :tag:"
  6154. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6155. (buffer-string))))
  6156. ;; With optional argument ONOFF set to `off', try to remove the tag,
  6157. ;; even if its not there.
  6158. (should
  6159. (equal "* H"
  6160. (org-test-with-temp-text "* H :tag:"
  6161. (org-toggle-tag "tag" 'off)
  6162. (buffer-string))))
  6163. (should
  6164. (equal "* H :tag:"
  6165. (org-test-with-temp-text "* H :tag:"
  6166. (let ((org-tags-column 1)) (org-toggle-tag "foo" 'off))
  6167. (buffer-string))))
  6168. ;; Special case: Handle properly tag inheritance. In particular, do
  6169. ;; not set inherited tags.
  6170. (should
  6171. (equal "* H1 :tag:\n** H2 :tag2:tag:"
  6172. (org-test-with-temp-text "* H1 :tag:\n** <point>H2 :tag2:"
  6173. (let ((org-use-tag-inheritance t)
  6174. (org-tags-column 1))
  6175. (org-toggle-tag "tag"))
  6176. (buffer-string))))
  6177. (should
  6178. (equal "* H1 :tag1:tag2:\n** H2 :foo:"
  6179. (org-test-with-temp-text "* H1 :tag1:tag2:\n** <point>H2"
  6180. (let ((org-use-tag-inheritance t)
  6181. (org-tags-column 1))
  6182. (org-toggle-tag "foo"))
  6183. (buffer-string)))))
  6184. ;;; TODO keywords
  6185. (ert-deftest test-org/auto-repeat-maybe ()
  6186. "Test `org-auto-repeat-maybe' specifications."
  6187. ;; Do not auto repeat when there is no valid time stamp with
  6188. ;; a repeater in the entry.
  6189. (should-not
  6190. (string-prefix-p
  6191. "* TODO H"
  6192. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6193. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  6194. (org-todo "DONE")
  6195. (buffer-string)))))
  6196. (should-not
  6197. (string-prefix-p
  6198. "* TODO H"
  6199. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6200. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  6201. (org-todo "DONE")
  6202. (buffer-string)))))
  6203. ;; When switching to DONE state, switch back to first TODO keyword
  6204. ;; in sequence, or the same keyword if they have different types.
  6205. (should
  6206. (string-prefix-p
  6207. "* TODO H"
  6208. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6209. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6210. (org-todo "DONE")
  6211. (buffer-string)))))
  6212. (should
  6213. (string-prefix-p
  6214. "* KWD1 H"
  6215. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6216. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6217. (org-todo "DONE")
  6218. (buffer-string)))))
  6219. (should
  6220. (string-prefix-p
  6221. "* KWD2 H"
  6222. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  6223. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6224. (org-todo "DONE")
  6225. (buffer-string)))))
  6226. ;; If there was no TODO keyword in the first place, do not insert
  6227. ;; any either.
  6228. (should
  6229. (string-prefix-p
  6230. "* H"
  6231. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6232. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  6233. (org-todo "DONE")
  6234. (buffer-string)))))
  6235. ;; Revert to REPEAT_TO_STATE, if set.
  6236. (should
  6237. (string-prefix-p
  6238. "* KWD2 H"
  6239. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6240. (org-test-with-temp-text
  6241. "* KWD2 H
  6242. :PROPERTIES:
  6243. :REPEAT_TO_STATE: KWD2
  6244. :END:
  6245. <2012-03-29 Thu +2y>"
  6246. (org-todo "DONE")
  6247. (buffer-string)))))
  6248. ;; When switching to DONE state, update base date. If there are
  6249. ;; multiple repeated time stamps, update them all.
  6250. (should
  6251. (string-match-p
  6252. "<2014-03-29 .* \\+2y>"
  6253. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6254. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6255. (org-todo "DONE")
  6256. (buffer-string)))))
  6257. (should
  6258. (string-match-p
  6259. "<2015-03-04 .* \\+1y>"
  6260. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6261. (org-test-with-temp-text
  6262. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  6263. (org-todo "DONE")
  6264. (buffer-string)))))
  6265. ;; Throw an error if repeater unit is the hour and no time is
  6266. ;; provided in the time-stamp.
  6267. (should-error
  6268. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6269. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  6270. (org-todo "DONE")
  6271. (buffer-string))))
  6272. ;; Do not repeat commented time stamps.
  6273. (should-not
  6274. (string-prefix-p
  6275. "<2015-03-04 .* \\+1y>"
  6276. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6277. (org-test-with-temp-text
  6278. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  6279. (org-todo "DONE")
  6280. (buffer-string)))))
  6281. (should-not
  6282. (string-prefix-p
  6283. "<2015-03-04 .* \\+1y>"
  6284. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6285. (org-test-with-temp-text
  6286. "* TODO H
  6287. <2012-03-29 Thu. +2y>
  6288. #+BEGIN_EXAMPLE
  6289. <2014-03-04 Tue +1y>
  6290. #+END_EXAMPLE"
  6291. (org-todo "DONE")
  6292. (buffer-string)))))
  6293. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  6294. ;; entry, record time of last repeat.
  6295. (should-not
  6296. (string-match-p
  6297. ":LAST_REPEAT:"
  6298. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6299. (org-log-repeat nil))
  6300. (cl-letf (((symbol-function 'org-add-log-setup)
  6301. (lambda (&rest args) nil)))
  6302. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6303. (org-todo "DONE")
  6304. (buffer-string))))))
  6305. (should
  6306. (string-match-p
  6307. ":LAST_REPEAT:"
  6308. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6309. (org-log-repeat t))
  6310. (cl-letf (((symbol-function 'org-add-log-setup)
  6311. (lambda (&rest args) nil)))
  6312. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6313. (org-todo "DONE")
  6314. (buffer-string))))))
  6315. (should
  6316. (string-match-p
  6317. ":LAST_REPEAT:"
  6318. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6319. (cl-letf (((symbol-function 'org-add-log-setup)
  6320. (lambda (&rest args) nil)))
  6321. (org-test-with-temp-text
  6322. "* TODO H\n<2012-03-29 Thu +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  6323. (org-todo "DONE")
  6324. (buffer-string))))))
  6325. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  6326. ;; the entry as it is no longer relevant.
  6327. (should-not
  6328. (string-match-p
  6329. "^SCHEDULED:"
  6330. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6331. (org-test-with-temp-text
  6332. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  6333. (org-todo "DONE")
  6334. (buffer-string)))))
  6335. ;; Properly advance repeater even when a clock entry is specified
  6336. ;; and `org-log-repeat' is nil.
  6337. (should
  6338. (string-match-p
  6339. "SCHEDULED: <2014-03-29"
  6340. (let ((org-log-repeat nil)
  6341. (org-todo-keywords '((sequence "TODO" "DONE"))))
  6342. (org-test-with-temp-text
  6343. "* TODO H
  6344. SCHEDULED: <2012-03-29 Thu +2y>
  6345. CLOCK: [2012-03-29 Thu 10:00]--[2012-03-29 Thu 16:40] => 6:40"
  6346. (org-todo "DONE")
  6347. (buffer-string))))))
  6348. ;;; Timestamps API
  6349. (ert-deftest test-org/at-timestamp-p ()
  6350. "Test `org-at-timestamp-p' specifications."
  6351. (should
  6352. (org-test-with-temp-text "<2012-03-29 Thu>"
  6353. (org-at-timestamp-p)))
  6354. (should-not
  6355. (org-test-with-temp-text "2012-03-29 Thu"
  6356. (org-at-timestamp-p)))
  6357. ;; Test return values.
  6358. (should
  6359. (eq 'bracket
  6360. (org-test-with-temp-text "<2012-03-29 Thu>"
  6361. (org-at-timestamp-p))))
  6362. (should
  6363. (eq 'year
  6364. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  6365. (org-at-timestamp-p))))
  6366. (should
  6367. (eq 'month
  6368. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  6369. (org-at-timestamp-p))))
  6370. (should
  6371. (eq 'day
  6372. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  6373. (org-at-timestamp-p))))
  6374. (should
  6375. (eq 'day
  6376. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  6377. (org-at-timestamp-p))))
  6378. (should
  6379. (wholenump
  6380. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  6381. (org-at-timestamp-p))))
  6382. (should
  6383. (eq 'bracket
  6384. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  6385. (org-at-timestamp-p))))
  6386. (should
  6387. (eq 'after
  6388. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  6389. (org-at-timestamp-p))))
  6390. ;; Test `inactive' optional argument.
  6391. (should
  6392. (org-test-with-temp-text "[2012-03-29 Thu]"
  6393. (org-at-timestamp-p 'inactive)))
  6394. (should-not
  6395. (org-test-with-temp-text "[2012-03-29 Thu]"
  6396. (org-at-timestamp-p)))
  6397. ;; When optional argument is `agenda', recognize time-stamps in
  6398. ;; planning info line, property drawers and clocks.
  6399. (should
  6400. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6401. (org-at-timestamp-p 'agenda)))
  6402. (should-not
  6403. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6404. (org-at-timestamp-p)))
  6405. (should
  6406. (org-test-with-temp-text
  6407. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6408. (org-at-timestamp-p 'agenda)))
  6409. (should-not
  6410. (org-test-with-temp-text
  6411. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6412. (org-at-timestamp-p)))
  6413. (should
  6414. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6415. (let ((org-agenda-include-inactive-timestamps t))
  6416. (org-at-timestamp-p 'agenda))))
  6417. (should-not
  6418. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6419. (let ((org-agenda-include-inactive-timestamps t))
  6420. (org-at-timestamp-p))))
  6421. (should-not
  6422. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6423. (let ((org-agenda-include-inactive-timestamps t))
  6424. (org-at-timestamp-p 'inactive))))
  6425. ;; When optional argument is `lax', match any part of the document
  6426. ;; with Org timestamp syntax.
  6427. (should
  6428. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6429. (org-at-timestamp-p 'lax)))
  6430. (should-not
  6431. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6432. (org-at-timestamp-p)))
  6433. (should
  6434. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6435. (org-at-timestamp-p 'lax)))
  6436. (should-not
  6437. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6438. (org-at-timestamp-p)))
  6439. (should
  6440. (org-test-with-temp-text
  6441. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6442. (org-at-timestamp-p 'lax)))
  6443. (should-not
  6444. (org-test-with-temp-text
  6445. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6446. (org-at-timestamp-p)))
  6447. ;; Optional argument `lax' also matches inactive timestamps.
  6448. (should
  6449. (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
  6450. (org-at-timestamp-p 'lax))))
  6451. (ert-deftest test-org/time-stamp ()
  6452. "Test `org-time-stamp' specifications."
  6453. ;; Insert chosen time stamp at point.
  6454. (should
  6455. (string-match
  6456. "Te<2014-03-04 .*?>xt"
  6457. (org-test-with-temp-text "Te<point>xt"
  6458. (cl-letf (((symbol-function 'org-read-date)
  6459. (lambda (&rest args)
  6460. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6461. (org-time-stamp nil)
  6462. (buffer-string)))))
  6463. ;; With a prefix argument, also insert time.
  6464. (should
  6465. (string-match
  6466. "Te<2014-03-04 .*? 00:41>xt"
  6467. (org-test-with-temp-text "Te<point>xt"
  6468. (cl-letf (((symbol-function 'org-read-date)
  6469. (lambda (&rest args)
  6470. (apply #'encode-time
  6471. (org-parse-time-string "2014-03-04 00:41")))))
  6472. (org-time-stamp '(4))
  6473. (buffer-string)))))
  6474. ;; With two universal prefix arguments, insert an active timestamp
  6475. ;; with the current time without prompting the user.
  6476. (should
  6477. (string-match
  6478. "Te<2014-03-04 .*? 00:41>xt"
  6479. (org-test-with-temp-text "Te<point>xt"
  6480. (cl-letf (((symbol-function 'current-time)
  6481. (lambda ()
  6482. (apply #'encode-time
  6483. (org-parse-time-string "2014-03-04 00:41")))))
  6484. (org-time-stamp '(16))
  6485. (buffer-string)))))
  6486. ;; When optional argument is non-nil, insert an inactive timestamp.
  6487. (should
  6488. (string-match
  6489. "Te\\[2014-03-04 .*?\\]xt"
  6490. (org-test-with-temp-text "Te<point>xt"
  6491. (cl-letf (((symbol-function 'org-read-date)
  6492. (lambda (&rest args)
  6493. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6494. (org-time-stamp nil t)
  6495. (buffer-string)))))
  6496. ;; When called from a timestamp, replace existing one.
  6497. (should
  6498. (string-match
  6499. "<2014-03-04 .*?>"
  6500. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  6501. (cl-letf (((symbol-function 'org-read-date)
  6502. (lambda (&rest args)
  6503. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6504. (org-time-stamp nil)
  6505. (buffer-string)))))
  6506. (should
  6507. (string-match
  6508. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  6509. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  6510. (cl-letf (((symbol-function 'org-read-date)
  6511. (lambda (&rest args)
  6512. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6513. (org-time-stamp nil)
  6514. (buffer-string)))))
  6515. ;; When replacing a timestamp, preserve repeater, if any.
  6516. (should
  6517. (string-match
  6518. "<2014-03-04 .*? \\+2y>"
  6519. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  6520. (cl-letf (((symbol-function 'org-read-date)
  6521. (lambda (&rest args)
  6522. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6523. (org-time-stamp nil)
  6524. (buffer-string)))))
  6525. ;; When called twice in a raw, build a date range.
  6526. (should
  6527. (string-match
  6528. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  6529. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  6530. (cl-letf (((symbol-function 'org-read-date)
  6531. (lambda (&rest args)
  6532. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6533. (let ((last-command 'org-time-stamp)
  6534. (this-command 'org-time-stamp))
  6535. (org-time-stamp nil))
  6536. (buffer-string))))))
  6537. (ert-deftest test-org/timestamp-has-time-p ()
  6538. "Test `org-timestamp-has-time-p' specifications."
  6539. ;; With time.
  6540. (should
  6541. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6542. (org-timestamp-has-time-p (org-element-context))))
  6543. ;; Without time.
  6544. (should-not
  6545. (org-test-with-temp-text "<2012-03-29 Thu>"
  6546. (org-timestamp-has-time-p (org-element-context)))))
  6547. (ert-deftest test-org/get-repeat ()
  6548. "Test `org-get-repeat' specifications."
  6549. (should
  6550. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  6551. (org-get-repeat)))
  6552. (should-not
  6553. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  6554. (org-get-repeat)))
  6555. ;; Return proper repeat string.
  6556. (should
  6557. (equal "+2y"
  6558. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  6559. (org-get-repeat))))
  6560. ;; Prevent false positive (commented or verbatim time stamps)
  6561. (should-not
  6562. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  6563. (org-get-repeat)))
  6564. (should-not
  6565. (org-test-with-temp-text
  6566. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  6567. (org-get-repeat)))
  6568. ;; Return nil when called before first heading.
  6569. (should-not
  6570. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  6571. (org-get-repeat)))
  6572. ;; When called with an optional argument, extract repeater from that
  6573. ;; string instead.
  6574. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  6575. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  6576. (ert-deftest test-org/timestamp-format ()
  6577. "Test `org-timestamp-format' specifications."
  6578. ;; Regular test.
  6579. (should
  6580. (equal
  6581. "2012-03-29 16:40"
  6582. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6583. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  6584. ;; Range end.
  6585. (should
  6586. (equal
  6587. "2012-03-29"
  6588. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  6589. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  6590. (ert-deftest test-org/timestamp-split-range ()
  6591. "Test `org-timestamp-split-range' specifications."
  6592. ;; Extract range start (active).
  6593. (should
  6594. (equal '(2012 3 29)
  6595. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6596. (let ((ts (org-timestamp-split-range (org-element-context))))
  6597. (mapcar (lambda (p) (org-element-property p ts))
  6598. '(:year-end :month-end :day-end))))))
  6599. ;; Extract range start (inactive)
  6600. (should
  6601. (equal '(2012 3 29)
  6602. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6603. (let ((ts (org-timestamp-split-range (org-element-context))))
  6604. (mapcar (lambda (p) (org-element-property p ts))
  6605. '(:year-end :month-end :day-end))))))
  6606. ;; Extract range end (active).
  6607. (should
  6608. (equal '(2012 3 30)
  6609. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6610. (let ((ts (org-timestamp-split-range
  6611. (org-element-context) t)))
  6612. (mapcar (lambda (p) (org-element-property p ts))
  6613. '(:year-end :month-end :day-end))))))
  6614. ;; Extract range end (inactive)
  6615. (should
  6616. (equal '(2012 3 30)
  6617. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6618. (let ((ts (org-timestamp-split-range
  6619. (org-element-context) t)))
  6620. (mapcar (lambda (p) (org-element-property p ts))
  6621. '(:year-end :month-end :day-end))))))
  6622. ;; Return the timestamp if not a range.
  6623. (should
  6624. (org-test-with-temp-text "[2012-03-29 Thu]"
  6625. (let* ((ts-orig (org-element-context))
  6626. (ts-copy (org-timestamp-split-range ts-orig)))
  6627. (eq ts-orig ts-copy))))
  6628. (should
  6629. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6630. (let* ((ts-orig (org-element-context))
  6631. (ts-copy (org-timestamp-split-range ts-orig)))
  6632. (eq ts-orig ts-copy)))))
  6633. (ert-deftest test-org/timestamp-translate ()
  6634. "Test `org-timestamp-translate' specifications."
  6635. ;; Translate whole date range.
  6636. (should
  6637. (equal "<29>--<30>"
  6638. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6639. (let ((org-display-custom-times t)
  6640. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6641. (org-timestamp-translate (org-element-context))))))
  6642. ;; Translate date range start.
  6643. (should
  6644. (equal "<29>"
  6645. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6646. (let ((org-display-custom-times t)
  6647. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6648. (org-timestamp-translate (org-element-context) 'start)))))
  6649. ;; Translate date range end.
  6650. (should
  6651. (equal "<30>"
  6652. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6653. (let ((org-display-custom-times t)
  6654. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6655. (org-timestamp-translate (org-element-context) 'end)))))
  6656. ;; Translate time range.
  6657. (should
  6658. (equal "<08>--<16>"
  6659. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  6660. (let ((org-display-custom-times t)
  6661. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  6662. (org-timestamp-translate (org-element-context))))))
  6663. ;; Translate non-range timestamp.
  6664. (should
  6665. (equal "<29>"
  6666. (org-test-with-temp-text "<2012-03-29 Thu>"
  6667. (let ((org-display-custom-times t)
  6668. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6669. (org-timestamp-translate (org-element-context))))))
  6670. ;; Do not change `diary' timestamps.
  6671. (should
  6672. (equal "<%%(org-float t 4 2)>"
  6673. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6674. (let ((org-display-custom-times t)
  6675. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6676. (org-timestamp-translate (org-element-context)))))))
  6677. (ert-deftest test-org/timestamp-from-string ()
  6678. "Test `org-timestamp-from-string' specifications."
  6679. ;; Return nil if argument is not a valid Org timestamp.
  6680. (should-not (org-timestamp-from-string ""))
  6681. (should-not (org-timestamp-from-string nil))
  6682. (should-not (org-timestamp-from-string "<2012-03-29"))
  6683. ;; Otherwise, return a valid Org timestamp object.
  6684. (should
  6685. (equal "<2012-03-29 Thu>"
  6686. (let ((system-time-locale "en_US"))
  6687. (org-element-interpret-data
  6688. (org-timestamp-from-string "<2012-03-29 Thu>")))))
  6689. (should
  6690. (equal "[2014-03-04 Tue]"
  6691. (let ((system-time-locale "en_US"))
  6692. (org-element-interpret-data
  6693. (org-timestamp-from-string "[2014-03-04 Tue]"))))))
  6694. (ert-deftest test-org/timestamp-from-time ()
  6695. "Test `org-timestamp-from-time' specifications."
  6696. ;; Standard test.
  6697. (should
  6698. (equal "<2012-03-29 Thu>"
  6699. (let ((system-time-locale "en_US"))
  6700. (org-element-interpret-data
  6701. (org-timestamp-from-time
  6702. (apply #'encode-time
  6703. (org-parse-time-string "<2012-03-29 Thu 16:40>")))))))
  6704. ;; When optional argument WITH-TIME is non-nil, provide time
  6705. ;; information.
  6706. (should
  6707. (equal "<2012-03-29 Thu 16:40>"
  6708. (let ((system-time-locale "en_US"))
  6709. (org-element-interpret-data
  6710. (org-timestamp-from-time
  6711. (apply #'encode-time
  6712. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  6713. t)))))
  6714. ;; When optional argument INACTIVE is non-nil, return an inactive
  6715. ;; timestamp.
  6716. (should
  6717. (equal "[2012-03-29 Thu]"
  6718. (let ((system-time-locale "en_US"))
  6719. (org-element-interpret-data
  6720. (org-timestamp-from-time
  6721. (apply #'encode-time
  6722. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  6723. nil t))))))
  6724. (ert-deftest test-org/timestamp-to-time ()
  6725. "Test `org-timestamp-to-time' specifications."
  6726. (should
  6727. (equal "2014-03-04"
  6728. (format-time-string
  6729. "%Y-%m-%d"
  6730. (org-timestamp-to-time
  6731. (org-timestamp-from-string "<2014-03-04 Tue>")))))
  6732. (should
  6733. (equal "2014-03-04"
  6734. (format-time-string
  6735. "%Y-%m-%d"
  6736. (org-timestamp-to-time
  6737. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  6738. (should
  6739. (equal "2012-03-29 08:30"
  6740. (format-time-string
  6741. "%Y-%m-%d %H:%M"
  6742. (org-timestamp-to-time
  6743. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")))))
  6744. (should
  6745. (equal "2012-03-29"
  6746. (format-time-string
  6747. "%Y-%m-%d"
  6748. (org-timestamp-to-time
  6749. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")))))
  6750. (should
  6751. (equal "2012-03-29"
  6752. (format-time-string
  6753. "%Y-%m-%d"
  6754. (org-timestamp-to-time
  6755. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")))))
  6756. ;; When optional argument END is non-nil, use end of date range or
  6757. ;; time range.
  6758. (should
  6759. (equal "2012-03-29 16:40"
  6760. (format-time-string
  6761. "%Y-%m-%d %H:%M"
  6762. (org-timestamp-to-time
  6763. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")
  6764. t))))
  6765. (should
  6766. (equal "2014-03-04"
  6767. (format-time-string
  6768. "%Y-%m-%d"
  6769. (org-timestamp-to-time
  6770. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")
  6771. t))))
  6772. (should
  6773. (equal "2014-03-04"
  6774. (format-time-string
  6775. "%Y-%m-%d"
  6776. (org-timestamp-to-time
  6777. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")
  6778. t)))))
  6779. ;;; Visibility
  6780. (ert-deftest test-org/flag-drawer ()
  6781. "Test `org-flag-drawer' specifications."
  6782. ;; Hide drawer.
  6783. (should
  6784. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6785. (org-flag-drawer t)
  6786. (get-char-property (line-end-position) 'invisible)))
  6787. ;; Show drawer.
  6788. (should-not
  6789. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6790. (org-flag-drawer t)
  6791. (org-flag-drawer nil)
  6792. (get-char-property (line-end-position) 'invisible)))
  6793. ;; Test optional argument.
  6794. (should
  6795. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6796. (let ((drawer (save-excursion (search-forward ":D2")
  6797. (org-element-at-point))))
  6798. (org-flag-drawer t drawer)
  6799. (get-char-property (progn (search-forward ":D2") (line-end-position))
  6800. 'invisible))))
  6801. (should-not
  6802. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6803. (let ((drawer (save-excursion (search-forward ":D2")
  6804. (org-element-at-point))))
  6805. (org-flag-drawer t drawer)
  6806. (get-char-property (line-end-position) 'invisible))))
  6807. ;; Do not hide fake drawers.
  6808. (should-not
  6809. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  6810. (forward-line 1)
  6811. (org-flag-drawer t)
  6812. (get-char-property (line-end-position) 'invisible)))
  6813. ;; Do not hide incomplete drawers.
  6814. (should-not
  6815. (org-test-with-temp-text ":D:\nparagraph"
  6816. (forward-line 1)
  6817. (org-flag-drawer t)
  6818. (get-char-property (line-end-position) 'invisible)))
  6819. ;; Do not hide drawers when called from final blank lines.
  6820. (should-not
  6821. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  6822. (goto-char (point-max))
  6823. (org-flag-drawer t)
  6824. (goto-char (point-min))
  6825. (get-char-property (line-end-position) 'invisible)))
  6826. ;; Don't leave point in an invisible part of the buffer when hiding
  6827. ;; a drawer away.
  6828. (should-not
  6829. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6830. (goto-char (point-max))
  6831. (org-flag-drawer t)
  6832. (get-char-property (point) 'invisible))))
  6833. (ert-deftest test-org/hide-block-toggle ()
  6834. "Test `org-hide-block-toggle' specifications."
  6835. ;; Error when not at a block.
  6836. (should-error
  6837. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  6838. (org-hide-block-toggle 'off)
  6839. (get-char-property (line-end-position) 'invisible)))
  6840. ;; Hide block.
  6841. (should
  6842. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  6843. (org-hide-block-toggle)
  6844. (get-char-property (line-end-position) 'invisible)))
  6845. (should
  6846. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  6847. (org-hide-block-toggle)
  6848. (get-char-property (line-end-position) 'invisible)))
  6849. ;; Show block unconditionally when optional argument is `off'.
  6850. (should-not
  6851. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6852. (org-hide-block-toggle)
  6853. (org-hide-block-toggle 'off)
  6854. (get-char-property (line-end-position) 'invisible)))
  6855. (should-not
  6856. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6857. (org-hide-block-toggle 'off)
  6858. (get-char-property (line-end-position) 'invisible)))
  6859. ;; Hide block unconditionally when optional argument is non-nil.
  6860. (should
  6861. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6862. (org-hide-block-toggle t)
  6863. (get-char-property (line-end-position) 'invisible)))
  6864. (should
  6865. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6866. (org-hide-block-toggle)
  6867. (org-hide-block-toggle t)
  6868. (get-char-property (line-end-position) 'invisible)))
  6869. ;; Do not hide block when called from final blank lines.
  6870. (should-not
  6871. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  6872. (org-hide-block-toggle)
  6873. (goto-char (point-min))
  6874. (get-char-property (line-end-position) 'invisible)))
  6875. ;; Don't leave point in an invisible part of the buffer when hiding
  6876. ;; a block away.
  6877. (should-not
  6878. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  6879. (org-hide-block-toggle)
  6880. (get-char-property (point) 'invisible))))
  6881. (ert-deftest test-org/hide-block-toggle-maybe ()
  6882. "Test `org-hide-block-toggle-maybe' specifications."
  6883. (should
  6884. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  6885. (org-hide-block-toggle-maybe)))
  6886. (should-not
  6887. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  6888. (ert-deftest test-org/show-set-visibility ()
  6889. "Test `org-show-set-visibility' specifications."
  6890. ;; Do not throw an error before first heading.
  6891. (should
  6892. (org-test-with-temp-text "Preamble\n* Headline"
  6893. (org-show-set-visibility 'tree)
  6894. t))
  6895. ;; Test all visibility spans, both on headline and in entry.
  6896. (let ((list-visible-lines
  6897. (lambda (state headerp)
  6898. (org-test-with-temp-text "* Grandmother (0)
  6899. ** Uncle (1)
  6900. *** Heir (2)
  6901. ** Father (3)
  6902. Ancestor text (4)
  6903. *** Sister (5)
  6904. Sibling text (6)
  6905. *** Self (7)
  6906. Match (8)
  6907. **** First born (9)
  6908. Child text (10)
  6909. **** The other child (11)
  6910. *** Brother (12)
  6911. ** Aunt (13)
  6912. "
  6913. (org-cycle t)
  6914. (search-forward (if headerp "Self" "Match"))
  6915. (org-show-set-visibility state)
  6916. (goto-char (point-min))
  6917. (let (result (line 0))
  6918. (while (not (eobp))
  6919. (unless (org-invisible-p2) (push line result))
  6920. (cl-incf line)
  6921. (forward-line))
  6922. (nreverse result))))))
  6923. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  6924. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  6925. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  6926. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  6927. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  6928. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  6929. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  6930. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  6931. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  6932. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  6933. (funcall list-visible-lines 'tree nil)))
  6934. (should (equal '(0 1 3 4 5 7 12 13)
  6935. (funcall list-visible-lines 'canonical t)))
  6936. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  6937. (funcall list-visible-lines 'canonical nil))))
  6938. ;; When point is hidden in a drawer or a block, make sure to make it
  6939. ;; visible.
  6940. (should-not
  6941. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  6942. (org-hide-block-toggle)
  6943. (search-forward "Text")
  6944. (org-show-set-visibility 'minimal)
  6945. (org-invisible-p2)))
  6946. (should-not
  6947. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  6948. (org-flag-drawer t)
  6949. (search-forward "Text")
  6950. (org-show-set-visibility 'minimal)
  6951. (org-invisible-p2)))
  6952. (should-not
  6953. (org-test-with-temp-text
  6954. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  6955. (org-flag-drawer t)
  6956. (forward-line -1)
  6957. (org-hide-block-toggle)
  6958. (search-forward "Text")
  6959. (org-show-set-visibility 'minimal)
  6960. (org-invisible-p2))))
  6961. (defun test-org/copy-visible ()
  6962. "Test `org-copy-visible' specifications."
  6963. (should
  6964. (equal "Foo"
  6965. (org-test-with-temp-text "Foo"
  6966. (let ((kill-ring nil))
  6967. (org-copy-visible (point-min) (point-max))
  6968. (current-kill 0 t)))))
  6969. ;; Skip invisible characters by text property.
  6970. (should
  6971. (equal "Foo"
  6972. (org-test-with-temp-text #("F<hidden>oo" 1 7 (invisible t))
  6973. (let ((kill-ring nil))
  6974. (org-copy-visible (point-min) (point-max))
  6975. (current-kill 0 t)))))
  6976. ;; Skip invisible characters by overlay.
  6977. (should
  6978. (equal "Foo"
  6979. (org-test-with-temp-text "F<hidden>oo"
  6980. (let ((o (make-overlay 2 10)))
  6981. (overlay-put o 'invisible t))
  6982. (let ((kill-ring nil))
  6983. (org-copy-visible (point-min) (point-max))
  6984. (current-kill 0 t)))))
  6985. ;; Handle invisible characters at the beginning and the end of the
  6986. ;; buffer.
  6987. (should
  6988. (equal "Foo"
  6989. (org-test-with-temp-text #("<hidden>Foo" 0 8 (invisible t))
  6990. (let ((kill-ring nil))
  6991. (org-copy-visible (point-min) (point-max))
  6992. (current-kill 0 t)))))
  6993. (should
  6994. (equal "Foo"
  6995. (org-test-with-temp-text #("Foo<hidden>" 3 11 (invisible t))
  6996. (let ((kill-ring nil))
  6997. (org-copy-visible (point-min) (point-max))
  6998. (current-kill 0 t)))))
  6999. ;; Handle multiple visible parts.
  7000. (should
  7001. (equal "abc"
  7002. (org-test-with-temp-text
  7003. #("aXbXc" 1 2 (invisible t) 3 4 (invisible t))
  7004. (let ((kill-ring nil))
  7005. (org-copy-visible (point-min) (point-max))
  7006. (current-kill 0 t))))))
  7007. (ert-deftest test-org/set-visibility-according-to-property ()
  7008. "Test `org-set-visibility-according-to-property' specifications."
  7009. ;; "folded" state.
  7010. (should
  7011. (org-test-with-temp-text
  7012. "
  7013. * a
  7014. :PROPERTIES:
  7015. :VISIBILITY: folded
  7016. :END:
  7017. ** <point>b"
  7018. (org-set-visibility-according-to-property)
  7019. (invisible-p (point))))
  7020. ;; "children" state.
  7021. (should
  7022. (org-test-with-temp-text
  7023. "
  7024. * a
  7025. :PROPERTIES:
  7026. :VISIBILITY: children
  7027. :END:
  7028. ** b
  7029. <point>Contents
  7030. ** c"
  7031. (org-set-visibility-according-to-property)
  7032. (invisible-p (point))))
  7033. (should
  7034. (org-test-with-temp-text
  7035. "
  7036. * a
  7037. :PROPERTIES:
  7038. :VISIBILITY: children
  7039. :END:
  7040. ** b
  7041. Contents
  7042. *** <point>c"
  7043. (org-set-visibility-according-to-property)
  7044. (invisible-p (point))))
  7045. ;; "content" state.
  7046. (should
  7047. (org-test-with-temp-text
  7048. "
  7049. * a
  7050. :PROPERTIES:
  7051. :VISIBILITY: content
  7052. :END:
  7053. ** b
  7054. <point>Contents
  7055. *** c"
  7056. (org-set-visibility-according-to-property)
  7057. (invisible-p (point))))
  7058. (should
  7059. (org-test-with-temp-text
  7060. "
  7061. * a
  7062. :PROPERTIES:
  7063. :VISIBILITY: content
  7064. :END:
  7065. ** b
  7066. Contents
  7067. *** <point>c"
  7068. (org-set-visibility-according-to-property)
  7069. (not (invisible-p (point)))))
  7070. ;; "showall" state.
  7071. (should
  7072. (org-test-with-temp-text
  7073. "
  7074. * a
  7075. :PROPERTIES:
  7076. :VISIBILITY: showall
  7077. :END:
  7078. ** b
  7079. <point>Contents
  7080. *** c"
  7081. (org-set-visibility-according-to-property)
  7082. (not (invisible-p (point)))))
  7083. (should
  7084. (org-test-with-temp-text
  7085. "
  7086. * a
  7087. :PROPERTIES:
  7088. :VISIBILITY: showall
  7089. :END:
  7090. ** b
  7091. Contents
  7092. *** <point>c"
  7093. (org-set-visibility-according-to-property)
  7094. (not (invisible-p (point))))))
  7095. ;;; Yank and Kill
  7096. (ert-deftest test-org/paste-subtree ()
  7097. "Test `org-paste-subtree' specifications."
  7098. ;; Return an error if text to yank is not a set of subtrees.
  7099. (should-error (org-paste-subtree nil "Text"))
  7100. ;; Adjust level according to current one.
  7101. (should
  7102. (equal "* H\n* Text\n"
  7103. (org-test-with-temp-text "* H\n<point>"
  7104. (org-paste-subtree nil "* Text")
  7105. (buffer-string))))
  7106. (should
  7107. (equal "* H1\n** H2\n** Text\n"
  7108. (org-test-with-temp-text "* H1\n** H2\n<point>"
  7109. (org-paste-subtree nil "* Text")
  7110. (buffer-string))))
  7111. ;; When not on a heading, move to next heading before yanking.
  7112. (should
  7113. (equal "* H1\nParagraph\n* Text\n* H2"
  7114. (org-test-with-temp-text "* H1\n<point>Paragraph\n* H2"
  7115. (org-paste-subtree nil "* Text")
  7116. (buffer-string))))
  7117. ;; If point is between two headings, use the deepest level.
  7118. (should
  7119. (equal "* H1\n\n* Text\n* H2"
  7120. (org-test-with-temp-text "* H1\n<point>\n* H2"
  7121. (org-paste-subtree nil "* Text")
  7122. (buffer-string))))
  7123. (should
  7124. (equal "** H1\n\n** Text\n* H2"
  7125. (org-test-with-temp-text "** H1\n<point>\n* H2"
  7126. (org-paste-subtree nil "* Text")
  7127. (buffer-string))))
  7128. (should
  7129. (equal "* H1\n\n** Text\n** H2"
  7130. (org-test-with-temp-text "* H1\n<point>\n** H2"
  7131. (org-paste-subtree nil "* Text")
  7132. (buffer-string))))
  7133. ;; When on an empty heading, after the stars, deduce the new level
  7134. ;; from the number of stars.
  7135. (should
  7136. (equal "*** Text\n"
  7137. (org-test-with-temp-text "*** <point>"
  7138. (org-paste-subtree nil "* Text")
  7139. (buffer-string))))
  7140. ;; Optional argument LEVEL forces a level for the subtree.
  7141. (should
  7142. (equal "* H\n*** Text\n"
  7143. (org-test-with-temp-text "* H<point>"
  7144. (org-paste-subtree 3 "* Text")
  7145. (buffer-string)))))
  7146. (provide 'test-org)
  7147. ;;; test-org.el ends here