test-org.el 219 KB

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