test-org.el 239 KB

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