test-org.el 242 KB

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