test-org.el 245 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564
  1. ;;; test-org.el --- tests for org.el
  2. ;; Copyright (c) David Maus
  3. ;; Authors: David Maus
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;; Template test file for Org tests
  16. ;;; Code:
  17. (eval-and-compile (require 'cl-lib))
  18. ;;; Comments
  19. (ert-deftest test-org/toggle-comment ()
  20. "Test `org-toggle-comment' specifications."
  21. ;; Simple headline.
  22. (should
  23. (equal "* Test"
  24. (org-test-with-temp-text "* COMMENT Test"
  25. (org-toggle-comment)
  26. (buffer-string))))
  27. (should
  28. (equal "* COMMENT Test"
  29. (org-test-with-temp-text "* Test"
  30. (org-toggle-comment)
  31. (buffer-string))))
  32. ;; Headline with a regular keyword.
  33. (should
  34. (equal "* TODO Test"
  35. (org-test-with-temp-text "* TODO COMMENT Test"
  36. (org-toggle-comment)
  37. (buffer-string))))
  38. (should
  39. (equal "* TODO COMMENT Test"
  40. (org-test-with-temp-text "* TODO Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. ;; Empty headline.
  44. (should
  45. (equal "* "
  46. (org-test-with-temp-text "* COMMENT"
  47. (org-toggle-comment)
  48. (buffer-string))))
  49. (should
  50. (equal "* COMMENT"
  51. (org-test-with-temp-text "* "
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. ;; Headline with a single keyword.
  55. (should
  56. (equal "* TODO "
  57. (org-test-with-temp-text "* TODO COMMENT"
  58. (org-toggle-comment)
  59. (buffer-string))))
  60. (should
  61. (equal "* TODO COMMENT"
  62. (org-test-with-temp-text "* TODO"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. ;; Headline with a keyword, a priority cookie and contents.
  66. (should
  67. (equal "* TODO [#A] Headline"
  68. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  69. (org-toggle-comment)
  70. (buffer-string))))
  71. (should
  72. (equal "* TODO [#A] COMMENT Headline"
  73. (org-test-with-temp-text "* TODO [#A] Headline"
  74. (org-toggle-comment)
  75. (buffer-string)))))
  76. (ert-deftest test-org/comment-dwim ()
  77. "Test `comment-dwim' behaviour in an Org buffer."
  78. ;; No region selected, no comment on current line and line not
  79. ;; empty: insert comment on line above.
  80. (should
  81. (equal "# \nComment"
  82. (org-test-with-temp-text "Comment"
  83. (call-interactively #'org-comment-dwim)
  84. (buffer-string))))
  85. ;; No region selected, no comment on current line and line empty:
  86. ;; insert comment on this line.
  87. (should
  88. (equal "# \nParagraph"
  89. (org-test-with-temp-text "\nParagraph"
  90. (call-interactively #'org-comment-dwim)
  91. (buffer-string))))
  92. ;; No region selected, and a comment on this line: indent it.
  93. (should
  94. (equal "* Headline\n # Comment"
  95. (org-test-with-temp-text "* Headline\n# <point>Comment"
  96. (let ((org-adapt-indentation t))
  97. (call-interactively #'org-comment-dwim))
  98. (buffer-string))))
  99. ;; Also recognize single # at column 0 as comments.
  100. (should
  101. (equal "# Comment"
  102. (org-test-with-temp-text "# Comment"
  103. (call-interactively #'org-comment-dwim)
  104. (buffer-string))))
  105. ;; Region selected and only comments and blank lines within it:
  106. ;; un-comment all commented lines.
  107. (should
  108. (equal "Comment 1\n\nComment 2"
  109. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  110. (transient-mark-mode 1)
  111. (push-mark (point) t t)
  112. (goto-char (point-max))
  113. (call-interactively #'org-comment-dwim)
  114. (buffer-string))))
  115. ;; Region selected without comments: comment all lines if
  116. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  117. (should
  118. (equal "# Comment 1\n\n# Comment 2"
  119. (org-test-with-temp-text "Comment 1\n\nComment 2"
  120. (transient-mark-mode 1)
  121. (push-mark (point) t t)
  122. (goto-char (point-max))
  123. (let ((comment-empty-lines nil))
  124. (call-interactively #'org-comment-dwim))
  125. (buffer-string))))
  126. (should
  127. (equal "# Comment 1\n# \n# Comment 2"
  128. (org-test-with-temp-text "Comment 1\n\nComment 2"
  129. (transient-mark-mode 1)
  130. (push-mark (point) t t)
  131. (goto-char (point-max))
  132. (let ((comment-empty-lines t))
  133. (call-interactively #'org-comment-dwim))
  134. (buffer-string))))
  135. ;; In front of a keyword without region, insert a new comment.
  136. (should
  137. (equal "# \n#+KEYWORD: value"
  138. (org-test-with-temp-text "#+KEYWORD: value"
  139. (call-interactively #'org-comment-dwim)
  140. (buffer-string))))
  141. ;; Comment a heading
  142. (should
  143. (equal "* COMMENT Test"
  144. (org-test-with-temp-text "* Test"
  145. (call-interactively #'org-comment-dwim)
  146. (buffer-string))))
  147. ;; In a source block, use appropriate syntax.
  148. (should
  149. (equal " ;; "
  150. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n<point>\n#+END_SRC"
  151. (let ((org-edit-src-content-indentation 2))
  152. (call-interactively #'org-comment-dwim))
  153. (buffer-substring-no-properties (line-beginning-position)
  154. (point)))))
  155. (should
  156. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  157. (org-test-with-temp-text
  158. "#+BEGIN_SRC emacs-lisp\n<point>a\nb\n#+END_SRC"
  159. (transient-mark-mode 1)
  160. (push-mark (point) t t)
  161. (forward-line 2)
  162. (let ((org-edit-src-content-indentation 2))
  163. (call-interactively #'org-comment-dwim))
  164. (buffer-string)))))
  165. ;;; Date and time analysis
  166. (ert-deftest test-org/org-read-date ()
  167. "Test `org-read-date' specifications."
  168. ;; Parse ISO date with abbreviated year and month.
  169. (should (equal "2012-03-29 16:40"
  170. (let ((org-time-was-given t))
  171. (org-read-date t nil "12-3-29 16:40"))))
  172. ;; Parse Europeans dates.
  173. (should (equal "2012-03-29 16:40"
  174. (let ((org-time-was-given t))
  175. (org-read-date t nil "29.03.2012 16:40"))))
  176. ;; Parse Europeans dates without year.
  177. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  178. (let ((org-time-was-given t))
  179. (org-read-date t nil "29.03. 16:40"))))
  180. ;; Relative date applied to current time if there is single
  181. ;; plus/minus, or to default date when there are two of them.
  182. (should
  183. (equal
  184. "2015-03-04"
  185. (org-test-at-time "2014-03-04"
  186. (org-read-date
  187. t nil "+1y" nil
  188. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  189. (should
  190. (equal
  191. "2013-03-29"
  192. (org-test-at-time "2014-03-04"
  193. (org-read-date
  194. t nil "++1y" nil
  195. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  196. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  197. ;; dates (relatively to now) when incomplete. Otherwise, use
  198. ;; default date.
  199. (should
  200. (equal
  201. "2014-04-01"
  202. (org-test-at-time "2014-03-04"
  203. (let ((org-read-date-prefer-future t))
  204. (org-read-date t nil "1")))))
  205. (should
  206. (equal
  207. "2013-03-04"
  208. (org-test-at-time "2012-03-29"
  209. (let ((org-read-date-prefer-future t))
  210. (org-read-date t nil "3-4")))))
  211. (should
  212. (equal
  213. "2012-03-04"
  214. (org-test-at-time "2012-03-29"
  215. (let ((org-read-date-prefer-future nil))
  216. (org-read-date t nil "3-4")))))
  217. ;; When set to `org-read-date-prefer-future' is set to `time', read
  218. ;; day is moved to tomorrow if specified hour is before current
  219. ;; time. However, it only happens in no other part of the date is
  220. ;; specified.
  221. (should
  222. (equal
  223. "2012-03-30"
  224. (org-test-at-time "2012-03-29 16:40"
  225. (let ((org-read-date-prefer-future 'time))
  226. (org-read-date t nil "00:40" nil)))))
  227. (should-not
  228. (equal
  229. "2012-03-30"
  230. (org-test-at-time "2012-03-29 16:40"
  231. (let ((org-read-date-prefer-future 'time))
  232. (org-read-date t nil "29 00:40" nil)))))
  233. ;; Caveat: `org-read-date-prefer-future' always refers to current
  234. ;; time, not default time, when they differ.
  235. (should
  236. (equal
  237. "2014-04-01"
  238. (org-test-at-time "2014-03-04"
  239. (let ((org-read-date-prefer-future t))
  240. (org-read-date
  241. t nil "1" nil
  242. (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
  243. (should
  244. (equal
  245. "2014-03-25"
  246. (org-test-at-time "2014-03-04"
  247. (let ((org-read-date-prefer-future t))
  248. (org-read-date
  249. t nil "25" nil
  250. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
  251. (ert-deftest test-org/org-parse-time-string ()
  252. "Test `org-parse-time-string'."
  253. (should (equal (org-parse-time-string "2012-03-29 16:40")
  254. '(0 40 16 29 3 2012 nil nil nil)))
  255. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  256. '(0 40 16 29 3 2012 nil nil nil)))
  257. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  258. '(0 40 16 29 3 2012 nil nil nil)))
  259. (should (equal (org-parse-time-string "<2012-03-29>")
  260. '(0 0 0 29 3 2012 nil nil nil)))
  261. (should (equal (org-parse-time-string "<2012-03-29>" t)
  262. '(0 nil nil 29 3 2012 nil nil nil))))
  263. (ert-deftest test-org/closest-date ()
  264. "Test `org-closest-date' specifications."
  265. (require 'calendar)
  266. ;; Time stamps without a repeater are returned unchanged.
  267. (should
  268. (equal
  269. '(3 29 2012)
  270. (calendar-gregorian-from-absolute
  271. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  272. ;; Time stamps with a null repeater are returned unchanged.
  273. (should
  274. (equal
  275. '(3 29 2012)
  276. (calendar-gregorian-from-absolute
  277. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  278. ;; if PREFER is set to `past' always return a date before, or equal
  279. ;; to CURRENT.
  280. (should
  281. (equal
  282. '(3 1 2014)
  283. (calendar-gregorian-from-absolute
  284. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  285. (should
  286. (equal
  287. '(3 4 2014)
  288. (calendar-gregorian-from-absolute
  289. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  290. ;; if PREFER is set to `future' always return a date before, or equal
  291. ;; to CURRENT.
  292. (should
  293. (equal
  294. '(3 29 2014)
  295. (calendar-gregorian-from-absolute
  296. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  297. (should
  298. (equal
  299. '(3 4 2014)
  300. (calendar-gregorian-from-absolute
  301. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  302. ;; If PREFER is neither `past' nor `future', select closest date.
  303. (should
  304. (equal
  305. '(3 1 2014)
  306. (calendar-gregorian-from-absolute
  307. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  308. (should
  309. (equal
  310. '(5 4 2014)
  311. (calendar-gregorian-from-absolute
  312. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  313. ;; Test "day" repeater.
  314. (should
  315. (equal '(3 8 2014)
  316. (calendar-gregorian-from-absolute
  317. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  318. (should
  319. (equal '(3 10 2014)
  320. (calendar-gregorian-from-absolute
  321. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  322. ;; Test "month" repeater.
  323. (should
  324. (equal '(1 5 2015)
  325. (calendar-gregorian-from-absolute
  326. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  327. (should
  328. (equal '(3 29 2014)
  329. (calendar-gregorian-from-absolute
  330. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  331. ;; Test "year" repeater.
  332. (should
  333. (equal '(3 5 2014)
  334. (calendar-gregorian-from-absolute
  335. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  336. (should
  337. (equal '(3 29 2014)
  338. (calendar-gregorian-from-absolute
  339. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  340. (ert-deftest test-org/deadline-close-p ()
  341. "Test `org-deadline-close-p' specifications."
  342. (org-test-at-time "2016-06-03 Fri 01:43"
  343. ;; Timestamps are close if they are within `ndays' of lead time.
  344. (org-test-with-temp-text "* Heading"
  345. (should (org-deadline-close-p "2016-06-03 Fri" 0))
  346. (should (org-deadline-close-p "2016-06-02 Thu" 0))
  347. (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
  348. (should (org-deadline-close-p "2016-06-04 Sat" 1))
  349. (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
  350. ;; Read `ndays' from timestamp if argument not given.
  351. (org-test-with-temp-text "* H"
  352. (should (org-deadline-close-p "2016-06-04 Sat -1d"))
  353. (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
  354. (should (org-deadline-close-p "2016-06-10 Fri -1w"))
  355. (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
  356. ;; Prefer `ndays' argument over lead time in timestamp.
  357. (org-test-with-temp-text "* H"
  358. (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
  359. (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
  360. ;; Completed tasks are never close.
  361. (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
  362. (org-test-with-temp-text "* TODO Heading"
  363. (should (org-deadline-close-p "2016-06-03")))
  364. (org-test-with-temp-text "* DONE Heading"
  365. (should-not (org-deadline-close-p "2016-06-03"))))))
  366. ;;; Drawers
  367. (ert-deftest test-org/insert-property-drawer ()
  368. "Test `org-insert-property-drawer' specifications."
  369. ;; Error before first headline.
  370. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  371. ;; Insert drawer right after headline if there is no planning line,
  372. ;; or after it otherwise.
  373. (should
  374. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  375. (org-test-with-temp-text "* H\nParagraph<point>"
  376. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  377. (buffer-string))))
  378. (should
  379. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  380. (org-test-with-temp-text
  381. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  382. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  383. (buffer-string))))
  384. ;; Indent inserted drawer.
  385. (should
  386. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  387. (org-test-with-temp-text "* H\nParagraph<point>"
  388. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  389. (buffer-string))))
  390. ;; Handle insertion at eob.
  391. (should
  392. (equal "* H\n:PROPERTIES:\n:END:\n"
  393. (org-test-with-temp-text "* H"
  394. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  395. (buffer-string))))
  396. ;; Skip inlinetasks before point.
  397. (when (featurep 'org-inlinetask)
  398. (should
  399. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  400. (org-test-with-temp-text
  401. "* H\n*************** I\n*************** END\nP<point>"
  402. (let ((org-adapt-indentation nil)
  403. (org-inlinetask-min-level 15))
  404. (org-insert-property-drawer))
  405. (buffer-string)))))
  406. ;; Correctly set drawer in an inlinetask.
  407. (when (featurep 'org-inlinetask)
  408. (should
  409. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  410. (org-test-with-temp-text
  411. "* H\n*************** I\nP<point>\n*************** END"
  412. (let ((org-adapt-indentation nil)
  413. (org-inlinetask-min-level 15))
  414. (org-insert-property-drawer))
  415. (buffer-string))))))
  416. ;;; Filling
  417. (ert-deftest test-org/fill-element ()
  418. "Test `org-fill-element' specifications."
  419. ;; At an Org table, align it.
  420. (should
  421. (equal "| a |\n"
  422. (org-test-with-temp-text "|a|"
  423. (org-fill-element)
  424. (buffer-string))))
  425. (should
  426. (equal "#+name: table\n| a |\n"
  427. (org-test-with-temp-text "#+name: table\n| a |\n"
  428. (org-fill-element)
  429. (buffer-string))))
  430. ;; At a paragraph, preserve line breaks.
  431. (org-test-with-temp-text "some \\\\\nlong\ntext"
  432. (let ((fill-column 20))
  433. (org-fill-element)
  434. (should (equal (buffer-string) "some \\\\\nlong text"))))
  435. ;; Correctly fill a paragraph when point is at its very end.
  436. (should
  437. (equal "A B"
  438. (org-test-with-temp-text "A\nB"
  439. (let ((fill-column 20))
  440. (goto-char (point-max))
  441. (org-fill-element)
  442. (buffer-string)))))
  443. ;; Correctly fill the last paragraph of a greater element.
  444. (should
  445. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  446. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  447. (let ((fill-column 8))
  448. (forward-line)
  449. (end-of-line)
  450. (org-fill-element)
  451. (buffer-string)))))
  452. ;; Correctly fill an element in a narrowed buffer.
  453. (should
  454. (equal "01234\n6"
  455. (org-test-with-temp-text "01234 6789"
  456. (let ((fill-column 5))
  457. (narrow-to-region 1 8)
  458. (org-fill-element)
  459. (buffer-string)))))
  460. ;; Handle `adaptive-fill-regexp' in paragraphs.
  461. (should
  462. (equal "> a b"
  463. (org-test-with-temp-text "> a\n> b"
  464. (let ((fill-column 5)
  465. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  466. (org-fill-element)
  467. (buffer-string)))))
  468. ;; Special case: Fill first paragraph when point is at an item or
  469. ;; a plain-list or a footnote reference.
  470. (should
  471. (equal "- A B"
  472. (org-test-with-temp-text "- A\n B"
  473. (let ((fill-column 20))
  474. (org-fill-element)
  475. (buffer-string)))))
  476. (should
  477. (equal "[fn:1] A B"
  478. (org-test-with-temp-text "[fn:1] A\nB"
  479. (let ((fill-column 20))
  480. (org-fill-element)
  481. (buffer-string)))))
  482. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  483. (let ((fill-column 20))
  484. (org-fill-element)
  485. (should (equal (buffer-string)
  486. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  487. ;; Fill contents of `comment-block' elements.
  488. (should
  489. (equal
  490. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  491. (let ((fill-column 20))
  492. (forward-line)
  493. (org-fill-element)
  494. (buffer-string)))
  495. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  496. ;; Fill `comment' elements.
  497. (should
  498. (equal " # A B"
  499. (org-test-with-temp-text " # A\n # B"
  500. (let ((fill-column 20))
  501. (org-fill-element)
  502. (buffer-string)))))
  503. ;; Do not mix consecutive comments when filling one of them.
  504. (should
  505. (equal "# A B\n\n# C"
  506. (org-test-with-temp-text "# A\n# B\n\n# C"
  507. (let ((fill-column 20))
  508. (org-fill-element)
  509. (buffer-string)))))
  510. ;; Use commented empty lines as separators when filling comments.
  511. (should
  512. (equal "# A B\n#\n# C"
  513. (org-test-with-temp-text "# A\n# B\n#\n# C"
  514. (let ((fill-column 20))
  515. (org-fill-element)
  516. (buffer-string)))))
  517. ;; Handle `adaptive-fill-regexp' in comments.
  518. (should
  519. (equal "# > a b"
  520. (org-test-with-temp-text "# > a\n# > b"
  521. (let ((fill-column 20)
  522. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  523. (org-fill-element)
  524. (buffer-string)))))
  525. ;; Do nothing at affiliated keywords.
  526. (should
  527. (equal "#+NAME: para\nSome\ntext."
  528. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  529. (let ((fill-column 20))
  530. (org-fill-element)
  531. (buffer-string)))))
  532. ;; Do not move point after table when filling a table.
  533. (should-not
  534. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  535. (forward-char)
  536. (org-fill-element)
  537. (eobp)))
  538. ;; Do not fill "n" macro, with or without arguments, followed by
  539. ;; a dot or a closing parenthesis since it could be confused with
  540. ;; a numbered bullet.
  541. (should-not
  542. (equal "123456789\n{{{n}}}."
  543. (org-test-with-temp-text "123456789 {{{n}}}."
  544. (let ((fill-column 10))
  545. (org-fill-element)
  546. (buffer-string)))))
  547. (should-not
  548. (equal "123456789\n{{{n}}}\)"
  549. (org-test-with-temp-text "123456789 {{{n}}}\)"
  550. (let ((fill-column 10))
  551. (org-fill-element)
  552. (buffer-string)))))
  553. (should-not
  554. (equal "123456789\n{{{n()}}}."
  555. (org-test-with-temp-text "123456789 {{{n()}}}."
  556. (let ((fill-column 10))
  557. (org-fill-element)
  558. (buffer-string)))))
  559. (should-not
  560. (equal "123456789\n{{{n(counter)}}}."
  561. (org-test-with-temp-text "123456789 {{{n(counter)}}}."
  562. (let ((fill-column 10))
  563. (org-fill-element)
  564. (buffer-string))))))
  565. (ert-deftest test-org/auto-fill-function ()
  566. "Test auto-filling features."
  567. ;; Auto fill paragraph.
  568. (should
  569. (equal "12345\n7890"
  570. (org-test-with-temp-text "12345 7890"
  571. (let ((fill-column 5))
  572. (end-of-line)
  573. (org-auto-fill-function)
  574. (buffer-string)))))
  575. ;; Auto fill first paragraph in an item.
  576. (should
  577. (equal "- 12345\n 7890"
  578. (org-test-with-temp-text "- 12345 7890"
  579. (let ((fill-column 7))
  580. (end-of-line)
  581. (org-auto-fill-function)
  582. (buffer-string)))))
  583. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  584. (should
  585. (equal "> 12345\n 7890"
  586. (org-test-with-temp-text "> 12345 7890"
  587. (let ((fill-column 10)
  588. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  589. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  590. (end-of-line)
  591. (org-auto-fill-function)
  592. (buffer-string)))))
  593. (should
  594. (equal "> 12345\n> 12345\n> 7890"
  595. (org-test-with-temp-text "> 12345\n> 12345 7890"
  596. (let ((fill-column 10)
  597. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  598. (goto-char (point-max))
  599. (org-auto-fill-function)
  600. (buffer-string)))))
  601. (should-not
  602. (equal " 12345\n *12345\n *12345"
  603. (org-test-with-temp-text " 12345\n *12345 12345"
  604. (let ((fill-column 10)
  605. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  606. (goto-char (point-max))
  607. (org-auto-fill-function)
  608. (buffer-string)))))
  609. ;; Auto fill comments.
  610. (should
  611. (equal " # 12345\n # 7890"
  612. (org-test-with-temp-text " # 12345 7890"
  613. (let ((fill-column 10))
  614. (end-of-line)
  615. (org-auto-fill-function)
  616. (buffer-string)))))
  617. ;; A hash within a line isn't a comment.
  618. (should-not
  619. (equal "12345 # 7890\n# 1"
  620. (org-test-with-temp-text "12345 # 7890 1"
  621. (let ((fill-column 12))
  622. (end-of-line)
  623. (org-auto-fill-function)
  624. (buffer-string)))))
  625. ;; Correctly interpret empty prefix.
  626. (should-not
  627. (equal "# a\n# b\nRegular\n# paragraph"
  628. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  629. (let ((fill-column 12))
  630. (end-of-line 3)
  631. (org-auto-fill-function)
  632. (buffer-string)))))
  633. ;; Comment block: auto fill contents.
  634. (should
  635. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  636. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  637. (let ((fill-column 5))
  638. (forward-line)
  639. (end-of-line)
  640. (org-auto-fill-function)
  641. (buffer-string)))))
  642. (should
  643. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  644. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  645. (let ((fill-column 5))
  646. (forward-line)
  647. (end-of-line)
  648. (org-auto-fill-function)
  649. (buffer-string)))))
  650. ;; Do not fill if a new item could be created.
  651. (should-not
  652. (equal "12345\n- 90"
  653. (org-test-with-temp-text "12345 - 90"
  654. (let ((fill-column 5))
  655. (end-of-line)
  656. (org-auto-fill-function)
  657. (buffer-string)))))
  658. ;; Do not fill if a line break could be introduced.
  659. (should-not
  660. (equal "123\\\\\n7890"
  661. (org-test-with-temp-text "123\\\\ 7890"
  662. (let ((fill-column 6))
  663. (end-of-line)
  664. (org-auto-fill-function)
  665. (buffer-string)))))
  666. ;; Do not fill affiliated keywords.
  667. (should-not
  668. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  669. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  670. (let ((fill-column 20))
  671. (end-of-line)
  672. (org-auto-fill-function)
  673. (buffer-string))))))
  674. ;;; Indentation
  675. (ert-deftest test-org/indent-line ()
  676. "Test `org-indent-line' specifications."
  677. ;; Do not indent diary sexps, footnote definitions or headlines.
  678. (should
  679. (zerop
  680. (org-test-with-temp-text "%%(org-calendar-holiday)"
  681. (org-indent-line)
  682. (org-get-indentation))))
  683. (should
  684. (zerop
  685. (org-test-with-temp-text "[fn:1] fn"
  686. (let ((org-adapt-indentation t)) (org-indent-line))
  687. (org-get-indentation))))
  688. (should
  689. (zerop
  690. (org-test-with-temp-text "* H"
  691. (org-indent-line)
  692. (org-get-indentation))))
  693. ;; Do not indent before first headline.
  694. (should
  695. (zerop
  696. (org-test-with-temp-text ""
  697. (org-indent-line)
  698. (org-get-indentation))))
  699. ;; Indent according to headline level otherwise, unless
  700. ;; `org-adapt-indentation' is nil.
  701. (should
  702. (= 2
  703. (org-test-with-temp-text "* H\n<point>A"
  704. (let ((org-adapt-indentation t)) (org-indent-line))
  705. (org-get-indentation))))
  706. (should
  707. (= 2
  708. (org-test-with-temp-text "* H\n<point>\nA"
  709. (let ((org-adapt-indentation t)) (org-indent-line))
  710. (org-get-indentation))))
  711. (should
  712. (zerop
  713. (org-test-with-temp-text "* H\n<point>A"
  714. (let ((org-adapt-indentation nil)) (org-indent-line))
  715. (org-get-indentation))))
  716. ;; Indenting preserves point position.
  717. (should
  718. (org-test-with-temp-text "* H\nA<point>B"
  719. (let ((org-adapt-indentation t)) (org-indent-line))
  720. (looking-at "B")))
  721. ;; Do not change indentation at an item or a LaTeX environment.
  722. (should
  723. (= 1
  724. (org-test-with-temp-text "* H\n<point> - A"
  725. (let ((org-adapt-indentation t)) (org-indent-line))
  726. (org-get-indentation))))
  727. (should
  728. (= 1
  729. (org-test-with-temp-text
  730. "\\begin{equation}\n <point>1+1=2\n\\end{equation}"
  731. (org-indent-line)
  732. (org-get-indentation))))
  733. ;; On blank lines at the end of a list, indent like last element
  734. ;; within it if the line is still in the list. If the last element
  735. ;; is an item, indent like its contents. Otherwise, indent like the
  736. ;; whole list.
  737. (should
  738. (= 4
  739. (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
  740. (let ((org-adapt-indentation t)) (org-indent-line))
  741. (org-get-indentation))))
  742. (should
  743. (= 4
  744. (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
  745. (let ((org-adapt-indentation t)) (org-indent-line))
  746. (org-get-indentation))))
  747. (should
  748. (zerop
  749. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
  750. (let ((org-adapt-indentation t)) (org-indent-line))
  751. (org-get-indentation))))
  752. (should
  753. (= 4
  754. (org-test-with-temp-text "* H\n- A\n - \n<point>"
  755. (let ((org-adapt-indentation t)) (org-indent-line))
  756. (org-get-indentation))))
  757. (should
  758. (= 4
  759. (org-test-with-temp-text
  760. "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
  761. (let ((org-adapt-indentation t)) (org-indent-line))
  762. (org-get-indentation))))
  763. (should
  764. (= 2
  765. (org-test-with-temp-text "- A\n B\n\n<point>"
  766. (let ((org-adapt-indentation nil)) (org-indent-line))
  767. (org-get-indentation))))
  768. (should
  769. (= 2
  770. (org-test-with-temp-text
  771. "- A\n \begin{cases} 1 + 1\n \end{cases}\n\n<point>"
  772. (let ((org-adapt-indentation nil)) (org-indent-line))
  773. (org-get-indentation))))
  774. ;; Likewise, on a blank line at the end of a footnote definition,
  775. ;; indent at column 0 if line belongs to the definition. Otherwise,
  776. ;; indent like the definition itself.
  777. (should
  778. (zerop
  779. (org-test-with-temp-text "* H\n[fn:1] Definition\n<point>"
  780. (let ((org-adapt-indentation t)) (org-indent-line))
  781. (org-get-indentation))))
  782. (should
  783. (zerop
  784. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n<point>"
  785. (let ((org-adapt-indentation t)) (org-indent-line))
  786. (org-get-indentation))))
  787. ;; After the end of the contents of a greater element, indent like
  788. ;; the beginning of the element.
  789. (should
  790. (= 1
  791. (org-test-with-temp-text
  792. " #+BEGIN_CENTER\n Contents\n<point>#+END_CENTER"
  793. (org-indent-line)
  794. (org-get-indentation))))
  795. ;; On blank lines after a paragraph, indent like its last non-empty
  796. ;; line.
  797. (should
  798. (= 1
  799. (org-test-with-temp-text " Paragraph\n\n<point>"
  800. (org-indent-line)
  801. (org-get-indentation))))
  802. ;; At the first line of an element, indent like previous element's
  803. ;; first line, ignoring footnotes definitions and inline tasks, or
  804. ;; according to parent.
  805. (should
  806. (= 2
  807. (org-test-with-temp-text "A\n\n B\n\nC<point>"
  808. (org-indent-line)
  809. (org-get-indentation))))
  810. (should
  811. (= 1
  812. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC<point>"
  813. (org-indent-line)
  814. (org-get-indentation))))
  815. (should
  816. (= 1
  817. (org-test-with-temp-text
  818. " #+BEGIN_CENTER\n<point> Contents\n#+END_CENTER"
  819. (org-indent-line)
  820. (org-get-indentation))))
  821. ;; Within code part of a source block, use language major mode if
  822. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  823. ;; according to line above.
  824. (should
  825. (= 6
  826. (org-test-with-temp-text
  827. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  828. (let ((org-src-tab-acts-natively t)
  829. (org-edit-src-content-indentation 0))
  830. (org-indent-line))
  831. (org-get-indentation))))
  832. (should
  833. (= 1
  834. (org-test-with-temp-text
  835. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  836. (let ((org-src-tab-acts-natively nil)
  837. (org-edit-src-content-indentation 0))
  838. (org-indent-line))
  839. (org-get-indentation))))
  840. ;; Otherwise, indent like the first non-blank line above.
  841. (should
  842. (zerop
  843. (org-test-with-temp-text
  844. "#+BEGIN_CENTER\nline1\n\n<point> line2\n#+END_CENTER"
  845. (org-indent-line)
  846. (org-get-indentation))))
  847. ;; Align node properties according to `org-property-format'. Handle
  848. ;; nicely empty values.
  849. (should
  850. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  851. (org-test-with-temp-text
  852. "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  853. (let ((org-property-format "%-10s %s")) (org-indent-line))
  854. (buffer-string))))
  855. (should
  856. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  857. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  858. (let ((org-property-format "%-10s %s")) (org-indent-line))
  859. (buffer-string)))))
  860. (ert-deftest test-org/indent-region ()
  861. "Test `org-indent-region' specifications."
  862. ;; Indent paragraph.
  863. (should
  864. (equal "A\nB\nC"
  865. (org-test-with-temp-text " A\nB\n C"
  866. (org-indent-region (point-min) (point-max))
  867. (buffer-string))))
  868. ;; Indent greater elements along with their contents.
  869. (should
  870. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  871. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  872. (org-indent-region (point-min) (point-max))
  873. (buffer-string))))
  874. ;; Ignore contents of verse blocks. Only indent block delimiters.
  875. (should
  876. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  877. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  878. (org-indent-region (point-min) (point-max))
  879. (buffer-string))))
  880. (should
  881. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  882. (org-test-with-temp-text " #+BEGIN_VERSE\n A\n B\n #+END_VERSE"
  883. (org-indent-region (point-min) (point-max))
  884. (buffer-string))))
  885. ;; Indent example blocks as a single block, unless indentation
  886. ;; should be preserved. In this case only indent the block markers.
  887. (should
  888. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  889. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  890. (org-indent-region (point-min) (point-max))
  891. (buffer-string))))
  892. (should
  893. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  894. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  895. (org-indent-region (point-min) (point-max))
  896. (buffer-string))))
  897. (should
  898. (equal "#+BEGIN_EXAMPLE -i\n A\n B\n#+END_EXAMPLE"
  899. (org-test-with-temp-text
  900. " #+BEGIN_EXAMPLE -i\n A\n B\n #+END_EXAMPLE"
  901. (org-indent-region (point-min) (point-max))
  902. (buffer-string))))
  903. (should
  904. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  905. (org-test-with-temp-text
  906. " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  907. (let ((org-src-preserve-indentation t))
  908. (org-indent-region (point-min) (point-max)))
  909. (buffer-string))))
  910. ;; Treat export blocks as a whole.
  911. (should
  912. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  913. (org-test-with-temp-text "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  914. (org-indent-region (point-min) (point-max))
  915. (buffer-string))))
  916. (should
  917. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  918. (org-test-with-temp-text
  919. " #+BEGIN_EXPORT latex\n A\n B\n #+END_EXPORT"
  920. (org-indent-region (point-min) (point-max))
  921. (buffer-string))))
  922. ;; Indent according to mode if `org-src-tab-acts-natively' is
  923. ;; non-nil. Otherwise, do not indent code at all.
  924. (should
  925. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  926. (org-test-with-temp-text
  927. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  928. (let ((org-src-tab-acts-natively t)
  929. (org-edit-src-content-indentation 0))
  930. (org-indent-region (point-min) (point-max)))
  931. (buffer-string))))
  932. (should
  933. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  934. (org-test-with-temp-text
  935. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  936. (let ((org-src-tab-acts-natively nil)
  937. (org-edit-src-content-indentation 0))
  938. (org-indent-region (point-min) (point-max)))
  939. (buffer-string))))
  940. ;; Align node properties according to `org-property-format'. Handle
  941. ;; nicely empty values.
  942. (should
  943. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  944. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  945. (let ((org-property-format "%-10s %s")
  946. (org-adapt-indentation nil))
  947. (org-indent-region (point) (point-max)))
  948. (buffer-string))))
  949. (should
  950. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  951. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  952. (let ((org-property-format "%-10s %s")
  953. (org-adapt-indentation nil))
  954. (org-indent-region (point) (point-max)))
  955. (buffer-string))))
  956. ;; Indent plain lists.
  957. (should
  958. (equal "- A\n B\n - C\n\n D"
  959. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  960. (org-indent-region (point-min) (point-max))
  961. (buffer-string))))
  962. (should
  963. (equal "- A\n\n- B"
  964. (org-test-with-temp-text " - A\n\n - B"
  965. (org-indent-region (point-min) (point-max))
  966. (buffer-string))))
  967. ;; Indent footnote definitions.
  968. (should
  969. (equal "[fn:1] Definition\n\nDefinition"
  970. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  971. (org-indent-region (point-min) (point-max))
  972. (buffer-string))))
  973. ;; Special case: Start indenting on a blank line.
  974. (should
  975. (equal "\nParagraph"
  976. (org-test-with-temp-text "\n Paragraph"
  977. (org-indent-region (point-min) (point-max))
  978. (buffer-string)))))
  979. ;;; Editing
  980. (ert-deftest test-org/delete-indentation ()
  981. "Test `org-delete-indentation' specifications."
  982. ;; Regular test.
  983. (should (equal "foo bar"
  984. (org-test-with-temp-text
  985. "foo \n bar<point>"
  986. (org-delete-indentation)
  987. (buffer-string))))
  988. ;; With optional argument.
  989. (should (equal "foo bar"
  990. (org-test-with-temp-text
  991. "foo<point> \n bar"
  992. (org-delete-indentation t)
  993. (buffer-string))))
  994. ;; At headline text should be appended to the headline text.
  995. (should
  996. (equal"* foo bar :tag:"
  997. (let (org-auto-align-tags)
  998. (org-test-with-temp-text
  999. "* foo :tag:\n bar<point>"
  1000. (org-delete-indentation)
  1001. (buffer-string)))))
  1002. (should
  1003. (equal "* foo bar :tag:"
  1004. (let (org-auto-align-tags)
  1005. (org-test-with-temp-text
  1006. "* foo <point>:tag:\n bar"
  1007. (org-delete-indentation t)
  1008. (buffer-string))))))
  1009. (ert-deftest test-org/return ()
  1010. "Test `org-return' specifications."
  1011. ;; Regular test.
  1012. (should
  1013. (equal "Para\ngraph"
  1014. (org-test-with-temp-text "Para<point>graph"
  1015. (org-return)
  1016. (buffer-string))))
  1017. ;; With optional argument, indent line.
  1018. (should
  1019. (equal " Para\n graph"
  1020. (org-test-with-temp-text " Para<point>graph"
  1021. (org-return t)
  1022. (buffer-string))))
  1023. ;; On a table, call `org-table-next-row'.
  1024. (should
  1025. (org-test-with-temp-text "| <point>a |\n| b |"
  1026. (org-return)
  1027. (looking-at-p "b")))
  1028. ;; Open link or timestamp under point when `org-return-follows-link'
  1029. ;; is non-nil.
  1030. (should
  1031. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1032. (let ((org-return-follows-link t)
  1033. (org-link-search-must-match-exact-headline nil))
  1034. (org-return))
  1035. (looking-at-p "<<target>>")))
  1036. (should-not
  1037. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1038. (let ((org-return-follows-link nil)) (org-return))
  1039. (looking-at-p "<<target>>")))
  1040. (should
  1041. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  1042. (let ((org-return-follows-link t)) (org-return))
  1043. (looking-at-p "* b")))
  1044. (should
  1045. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  1046. (let ((org-return-follows-link t)
  1047. (org-link-search-must-match-exact-headline nil))
  1048. (org-return))
  1049. (looking-at-p "<<target>>")))
  1050. (should-not
  1051. (org-test-with-temp-text "Link [[target]]<point> <<target>>"
  1052. (let ((org-return-follows-link t)
  1053. (org-link-search-must-match-exact-headline nil))
  1054. (org-return))
  1055. (looking-at-p "<<target>>")))
  1056. ;; When `org-return-follows-link' is non-nil, tolerate links and
  1057. ;; timestamps in comments, node properties, etc.
  1058. (should
  1059. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1060. (let ((org-return-follows-link t)
  1061. (org-link-search-must-match-exact-headline nil))
  1062. (org-return))
  1063. (looking-at-p "<<target>>")))
  1064. (should-not
  1065. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1066. (let ((org-return-follows-link nil)) (org-return))
  1067. (looking-at-p "<<target>>")))
  1068. (should-not
  1069. (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
  1070. (let ((org-return-follows-link t)
  1071. (org-link-search-must-match-exact-headline nil))
  1072. (org-return))
  1073. (looking-at-p "<<target>>")))
  1074. ;; `org-return-follows-link' handle multi-line lines.
  1075. (should
  1076. (org-test-with-temp-text
  1077. "[[target][This is a very\n long description<point>]]\n <<target>>"
  1078. (let ((org-return-follows-link t)
  1079. (org-link-search-must-match-exact-headline nil))
  1080. (org-return))
  1081. (looking-at-p "<<target>>")))
  1082. (should-not
  1083. (org-test-with-temp-text
  1084. "[[target][This is a very\n long description]]<point>\n <<target>>"
  1085. (let ((org-return-follows-link t)
  1086. (org-link-search-must-match-exact-headline nil))
  1087. (org-return))
  1088. (looking-at-p "<<target>>")))
  1089. ;; However, do not open link when point is in a table.
  1090. (should
  1091. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1092. (let ((org-return-follows-link t)) (org-return))
  1093. (looking-at-p "between")))
  1094. ;; Special case: in a list, when indenting, do not break structure.
  1095. (should
  1096. (equal "- A\n B"
  1097. (org-test-with-temp-text "- A <point>B"
  1098. (org-return t)
  1099. (buffer-string))))
  1100. (should
  1101. (equal "- A\n\n- B"
  1102. (org-test-with-temp-text "- A\n<point>- B"
  1103. (org-return t)
  1104. (buffer-string))))
  1105. ;; On tags part of a headline, add a newline below it instead of
  1106. ;; breaking it.
  1107. (should
  1108. (equal "* H :tag:\n"
  1109. (org-test-with-temp-text "* H :<point>tag:"
  1110. (org-return)
  1111. (buffer-string))))
  1112. ;; Before headline text, add a newline below it instead of breaking
  1113. ;; it.
  1114. (should
  1115. (equal "* TODO H :tag:\n"
  1116. (org-test-with-temp-text "* <point>TODO H :tag:"
  1117. (org-return)
  1118. (buffer-string))))
  1119. (should
  1120. (equal "* TODO [#B] H :tag:\n"
  1121. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1122. (org-return)
  1123. (buffer-string))))
  1124. (should ;TODO are case-sensitive
  1125. (equal "* \nTodo"
  1126. (org-test-with-temp-text "* <point>Todo"
  1127. (org-return)
  1128. (buffer-string))))
  1129. ;; At headline text, break headline text but preserve tags.
  1130. (should
  1131. (equal "* TODO [#B] foo :tag:\nbar"
  1132. (let (org-auto-align-tags)
  1133. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1134. (org-return)
  1135. (buffer-string)))))
  1136. ;; At bol of headline insert newline.
  1137. (should
  1138. (equal "\n* h"
  1139. (org-test-with-temp-text "<point>* h"
  1140. (org-return)
  1141. (buffer-string))))
  1142. ;; Refuse to leave invalid headline in buffer.
  1143. (should
  1144. (equal "* h\n"
  1145. (org-test-with-temp-text "*<point> h"
  1146. (org-return)
  1147. (buffer-string))))
  1148. ;; Before first column or after last one in a table, split the
  1149. ;; table.
  1150. (should
  1151. (equal "| a |\n\n| b |"
  1152. (org-test-with-temp-text "| a |\n<point>| b |"
  1153. (org-return)
  1154. (buffer-string))))
  1155. (should
  1156. (equal "| a |\n\n| b |"
  1157. (org-test-with-temp-text "| a |<point>\n| b |"
  1158. (org-return)
  1159. (buffer-string))))
  1160. ;; Do not auto-fill on hitting <RET> inside a property drawer.
  1161. (should
  1162. (equal "* Heading\n:PROPERTIES:\n:SOME_PROP: This is a very long property value that goes beyond the fill-column. But this is inside a property drawer, so the auto-filling should be disabled.\n\n:END:"
  1163. (org-test-with-temp-text "* Heading\n:PROPERTIES:\n:SOME_PROP: This is a very long property value that goes beyond the fill-column. But this is inside a property drawer, so the auto-filling should be disabled.<point>\n:END:"
  1164. (setq-local fill-column 10)
  1165. (auto-fill-mode 1)
  1166. (org-return)
  1167. (buffer-string)))))
  1168. (ert-deftest test-org/meta-return ()
  1169. "Test M-RET (`org-meta-return') specifications."
  1170. ;; In a table field insert a row above.
  1171. (should
  1172. (org-test-with-temp-text "| a |"
  1173. (forward-char)
  1174. (org-meta-return)
  1175. (forward-line -1)
  1176. (looking-at "| |$")))
  1177. ;; In a paragraph change current line into a header.
  1178. (should
  1179. (org-test-with-temp-text "a"
  1180. (org-meta-return)
  1181. (beginning-of-line)
  1182. (looking-at "\* a$")))
  1183. ;; In an item insert an item, in this case above.
  1184. (should
  1185. (org-test-with-temp-text "- a"
  1186. (org-meta-return)
  1187. (beginning-of-line)
  1188. (looking-at "- $")))
  1189. ;; In a drawer and item insert an item, in this case above.
  1190. (should
  1191. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1192. (forward-line)
  1193. (org-meta-return)
  1194. (beginning-of-line)
  1195. (looking-at "- $"))))
  1196. (ert-deftest test-org/insert-heading ()
  1197. "Test `org-insert-heading' specifications."
  1198. ;; In an empty buffer, insert a new headline.
  1199. (should
  1200. (equal "* "
  1201. (org-test-with-temp-text ""
  1202. (org-insert-heading)
  1203. (buffer-string))))
  1204. ;; At the beginning of a line, turn it into a headline.
  1205. (should
  1206. (equal "* P"
  1207. (org-test-with-temp-text "<point>P"
  1208. (org-insert-heading)
  1209. (buffer-string))))
  1210. ;; In the middle of a line, split the line if allowed, otherwise,
  1211. ;; insert the headline at its end.
  1212. (should
  1213. (equal "Para\n* graph"
  1214. (org-test-with-temp-text "Para<point>graph"
  1215. (let ((org-M-RET-may-split-line '((default . t))))
  1216. (org-insert-heading))
  1217. (buffer-string))))
  1218. (should
  1219. (equal "Paragraph\n* "
  1220. (org-test-with-temp-text "Para<point>graph"
  1221. (let ((org-M-RET-may-split-line '((default . nil))))
  1222. (org-insert-heading))
  1223. (buffer-string))))
  1224. ;; At the beginning of a headline, create one above.
  1225. (should
  1226. (equal "* \n* H"
  1227. (org-test-with-temp-text "* H"
  1228. (org-insert-heading)
  1229. (buffer-string))))
  1230. ;; In the middle of a headline, split it if allowed.
  1231. (should
  1232. (equal "* H\n* 1\n"
  1233. (org-test-with-temp-text "* H<point>1"
  1234. (let ((org-M-RET-may-split-line '((headline . t))))
  1235. (org-insert-heading))
  1236. (buffer-string))))
  1237. (should
  1238. (equal "* H1\n* \n"
  1239. (org-test-with-temp-text "* H<point>1"
  1240. (let ((org-M-RET-may-split-line '((headline . nil))))
  1241. (org-insert-heading))
  1242. (buffer-string))))
  1243. ;; However, splitting cannot happen on TODO keywords, priorities or
  1244. ;; tags.
  1245. (should
  1246. (equal "* TODO H1\n* \n"
  1247. (org-test-with-temp-text "* TO<point>DO H1"
  1248. (let ((org-M-RET-may-split-line '((headline . t))))
  1249. (org-insert-heading))
  1250. (buffer-string))))
  1251. (should
  1252. (equal "* [#A] H1\n* \n"
  1253. (org-test-with-temp-text "* [#<point>A] H1"
  1254. (let ((org-M-RET-may-split-line '((headline . t))))
  1255. (org-insert-heading))
  1256. (buffer-string))))
  1257. (should
  1258. (equal "* H1 :tag:\n* \n"
  1259. (org-test-with-temp-text "* H1 :ta<point>g:"
  1260. (let ((org-M-RET-may-split-line '((headline . t))))
  1261. (org-insert-heading))
  1262. (buffer-string))))
  1263. ;; New headline level depends on the level of the headline above.
  1264. (should
  1265. (equal "** H\n** P"
  1266. (org-test-with-temp-text "** H\n<point>P"
  1267. (org-insert-heading)
  1268. (buffer-string))))
  1269. (should
  1270. (equal "** H\nPara\n** graph"
  1271. (org-test-with-temp-text "** H\nPara<point>graph"
  1272. (let ((org-M-RET-may-split-line '((default . t))))
  1273. (org-insert-heading))
  1274. (buffer-string))))
  1275. (should
  1276. (equal "** \n** H"
  1277. (org-test-with-temp-text "** H"
  1278. (org-insert-heading)
  1279. (buffer-string))))
  1280. ;; When called with one universal argument, insert a new headline at
  1281. ;; the end of the current subtree, independently on the position of
  1282. ;; point.
  1283. (should
  1284. (equal
  1285. "* H1\n** H2\n* \n"
  1286. (org-test-with-temp-text "* H1\n** H2"
  1287. (let ((org-insert-heading-respect-content nil))
  1288. (org-insert-heading '(4)))
  1289. (buffer-string))))
  1290. (should
  1291. (equal
  1292. "* H1\n** H2\n* \n"
  1293. (org-test-with-temp-text "* H<point>1\n** H2"
  1294. (let ((org-insert-heading-respect-content nil))
  1295. (org-insert-heading '(4)))
  1296. (buffer-string))))
  1297. ;; When called with two universal arguments, insert a new headline
  1298. ;; at the end of the grandparent subtree.
  1299. (should
  1300. (equal "* H1\n** H3\n- item\n** H2\n** \n"
  1301. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1302. (let ((org-insert-heading-respect-content nil))
  1303. (org-insert-heading '(16)))
  1304. (buffer-string))))
  1305. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1306. ;; a level 1 heading.
  1307. (should
  1308. (equal "* H1\n** H2\n* \n"
  1309. (org-test-with-temp-text "* H1\n** H2<point>"
  1310. (org-insert-heading nil nil t)
  1311. (buffer-string))))
  1312. (should
  1313. (equal "* H1\n- item\n* "
  1314. (org-test-with-temp-text "* H1\n- item<point>"
  1315. (org-insert-heading nil nil t)
  1316. (buffer-string))))
  1317. ;; Obey `org-blank-before-new-entry'.
  1318. (should
  1319. (equal "* H1\n\n* \n"
  1320. (org-test-with-temp-text "* H1<point>"
  1321. (let ((org-blank-before-new-entry '((heading . t))))
  1322. (org-insert-heading))
  1323. (buffer-string))))
  1324. (should
  1325. (equal "* H1\n* \n"
  1326. (org-test-with-temp-text "* H1<point>"
  1327. (let ((org-blank-before-new-entry '((heading . nil))))
  1328. (org-insert-heading))
  1329. (buffer-string))))
  1330. (should
  1331. (equal "* H1\n* H2\n* \n"
  1332. (org-test-with-temp-text "* H1\n* H2<point>"
  1333. (let ((org-blank-before-new-entry '((heading . auto))))
  1334. (org-insert-heading))
  1335. (buffer-string))))
  1336. (should
  1337. (equal "* H1\n\n* H2\n\n* \n"
  1338. (org-test-with-temp-text "* H1\n\n* H2<point>"
  1339. (let ((org-blank-before-new-entry '((heading . auto))))
  1340. (org-insert-heading))
  1341. (buffer-string))))
  1342. ;; Corner case: correctly insert a headline after an empty one.
  1343. (should
  1344. (equal "* \n* \n"
  1345. (org-test-with-temp-text "* <point>"
  1346. (org-insert-heading)
  1347. (buffer-string))))
  1348. (should
  1349. (org-test-with-temp-text "* <point>\n"
  1350. (org-insert-heading)
  1351. (looking-at-p "\n\\'")))
  1352. ;; Do not insert spurious headlines when inserting a new headline.
  1353. (should
  1354. (equal "* H1\n* H2\n* \n"
  1355. (org-test-with-temp-text "* H1\n* H2<point>\n"
  1356. (org-insert-heading)
  1357. (buffer-string))))
  1358. ;; Preserve visibility at beginning of line. In particular, when
  1359. ;; removing spurious blank lines, do not visually merge heading with
  1360. ;; the line visible above.
  1361. (should-not
  1362. (org-test-with-temp-text "* H1<point>\nContents\n\n* H2\n"
  1363. (org-overview)
  1364. (let ((org-blank-before-new-entry '((heading . nil))))
  1365. (org-insert-heading '(4)))
  1366. (invisible-p (line-end-position 0))))
  1367. ;; Properly handle empty lines when forcing a headline below current
  1368. ;; one.
  1369. (should
  1370. (equal "* H1\n\n* H\n\n* \n"
  1371. (org-test-with-temp-text "* H1\n\n* H<point>"
  1372. (let ((org-blank-before-new-entry '((heading . t))))
  1373. (org-insert-heading '(4))
  1374. (buffer-string))))))
  1375. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1376. "Test `org-insert-todo-heading-respect-content' specifications."
  1377. ;; Create a TODO heading.
  1378. (should
  1379. (org-test-with-temp-text "* H1\n Body"
  1380. (org-insert-todo-heading-respect-content)
  1381. (nth 2 (org-heading-components))))
  1382. ;; Add headline at the end of the first subtree
  1383. (should
  1384. (equal
  1385. "* TODO \n"
  1386. (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
  1387. (org-insert-todo-heading-respect-content)
  1388. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1389. ;; In a list, do not create a new item.
  1390. (should
  1391. (equal
  1392. "* TODO \n"
  1393. (org-test-with-temp-text "* H\n- an item\n- another one"
  1394. (search-forward "an ")
  1395. (org-insert-todo-heading-respect-content)
  1396. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1397. ;; Use the same TODO keyword as current heading.
  1398. (should
  1399. (equal
  1400. "* TODO \n"
  1401. (org-test-with-temp-text "* TODO\n** WAITING\n"
  1402. (org-insert-todo-heading-respect-content)
  1403. (buffer-substring-no-properties (line-beginning-position) (point-max))))))
  1404. (ert-deftest test-org/clone-with-time-shift ()
  1405. "Test `org-clone-subtree-with-time-shift'."
  1406. ;; Raise an error before first heading.
  1407. (should-error
  1408. (org-test-with-temp-text ""
  1409. (org-clone-subtree-with-time-shift 1)))
  1410. ;; Raise an error on invalid number of clones.
  1411. (should-error
  1412. (org-test-with-temp-text "* Clone me"
  1413. (org-clone-subtree-with-time-shift -1)))
  1414. ;; Clone non-repeating once.
  1415. (should
  1416. (equal "\
  1417. * H1\n<2015-06-21>
  1418. * H1\n<2015-06-23>
  1419. "
  1420. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1421. (org-clone-subtree-with-time-shift 1 "+2d")
  1422. (replace-regexp-in-string
  1423. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1424. nil nil 1))))
  1425. ;; Clone repeating once.
  1426. (should
  1427. (equal "\
  1428. * H1\n<2015-06-21>
  1429. * H1\n<2015-06-23>
  1430. * H1\n<2015-06-25 +1w>
  1431. "
  1432. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1433. (org-clone-subtree-with-time-shift 1 "+2d")
  1434. (replace-regexp-in-string
  1435. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1436. nil nil 1))))
  1437. ;; Clone non-repeating zero times.
  1438. (should
  1439. (equal "\
  1440. * H1\n<2015-06-21>
  1441. "
  1442. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1443. (org-clone-subtree-with-time-shift 0 "+2d")
  1444. (replace-regexp-in-string
  1445. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1446. nil nil 1))))
  1447. ;; Clone repeating "zero" times.
  1448. (should
  1449. (equal "\
  1450. * H1\n<2015-06-21>
  1451. * H1\n<2015-06-23 +1w>
  1452. "
  1453. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1454. (org-clone-subtree-with-time-shift 0 "+2d")
  1455. (replace-regexp-in-string
  1456. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1457. nil nil 1))))
  1458. ;; Clone with blank SHIFT argument.
  1459. (should
  1460. (string-prefix-p
  1461. "* H <2012-03-29"
  1462. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1463. (org-clone-subtree-with-time-shift 1 "")
  1464. (buffer-substring-no-properties (line-beginning-position 2)
  1465. (line-end-position 2)))))
  1466. ;; Find time stamps before point. If SHIFT is not specified, ask
  1467. ;; for a time shift.
  1468. (should
  1469. (string-prefix-p
  1470. "* H <2012-03-30"
  1471. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1472. (org-clone-subtree-with-time-shift 1 "+1d")
  1473. (buffer-substring-no-properties (line-beginning-position 2)
  1474. (line-end-position 2)))))
  1475. (should
  1476. (string-prefix-p
  1477. "* H <2014-03-05"
  1478. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  1479. (cl-letf (((symbol-function 'read-from-minibuffer)
  1480. (lambda (&rest args) "+1d")))
  1481. (org-clone-subtree-with-time-shift 1))
  1482. (buffer-substring-no-properties (line-beginning-position 2)
  1483. (line-end-position 2))))))
  1484. ;;; Fixed-Width Areas
  1485. (ert-deftest test-org/toggle-fixed-width ()
  1486. "Test `org-toggle-fixed-width' specifications."
  1487. ;; No region: Toggle on fixed-width marker in paragraphs.
  1488. (should
  1489. (equal ": A"
  1490. (org-test-with-temp-text "A"
  1491. (org-toggle-fixed-width)
  1492. (buffer-string))))
  1493. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1494. (should
  1495. (equal "A"
  1496. (org-test-with-temp-text ": A"
  1497. (org-toggle-fixed-width)
  1498. (buffer-string))))
  1499. ;; No region: Toggle on marker in blank lines after elements or just
  1500. ;; after a headline.
  1501. (should
  1502. (equal "* H\n: "
  1503. (org-test-with-temp-text "* H\n"
  1504. (forward-line)
  1505. (org-toggle-fixed-width)
  1506. (buffer-string))))
  1507. (should
  1508. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1509. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1510. (goto-char (point-max))
  1511. (org-toggle-fixed-width)
  1512. (buffer-string))))
  1513. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1514. ;; headlines, clocks)
  1515. (should
  1516. (equal ": * Headline"
  1517. (org-test-with-temp-text "* Headline"
  1518. (org-toggle-fixed-width)
  1519. (buffer-string))))
  1520. (should
  1521. (equal ": #+KEYWORD: value"
  1522. (org-test-with-temp-text "#+KEYWORD: value"
  1523. (org-toggle-fixed-width)
  1524. (buffer-string))))
  1525. ;; No region: error in other situations.
  1526. (should-error
  1527. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1528. (forward-line)
  1529. (org-toggle-fixed-width)
  1530. (buffer-string)))
  1531. ;; No region: Indentation is preserved.
  1532. (should
  1533. (equal "- A\n : B"
  1534. (org-test-with-temp-text "- A\n B"
  1535. (forward-line)
  1536. (org-toggle-fixed-width)
  1537. (buffer-string))))
  1538. ;; Region: If it contains only fixed-width elements and blank lines,
  1539. ;; toggle off fixed-width markup.
  1540. (should
  1541. (equal "A\n\nB"
  1542. (org-test-with-temp-text ": A\n\n: B"
  1543. (transient-mark-mode 1)
  1544. (push-mark (point) t t)
  1545. (goto-char (point-max))
  1546. (org-toggle-fixed-width)
  1547. (buffer-string))))
  1548. ;; Region: If it contains anything else, toggle on fixed-width but
  1549. ;; not on fixed-width areas.
  1550. (should
  1551. (equal ": A\n: \n: B\n: \n: C"
  1552. (org-test-with-temp-text "A\n\n: B\n\nC"
  1553. (transient-mark-mode 1)
  1554. (push-mark (point) t t)
  1555. (goto-char (point-max))
  1556. (org-toggle-fixed-width)
  1557. (buffer-string))))
  1558. ;; Region: Ignore blank lines at its end, unless it contains only
  1559. ;; such lines.
  1560. (should
  1561. (equal ": A\n\n"
  1562. (org-test-with-temp-text "A\n\n"
  1563. (transient-mark-mode 1)
  1564. (push-mark (point) t t)
  1565. (goto-char (point-max))
  1566. (org-toggle-fixed-width)
  1567. (buffer-string))))
  1568. (should
  1569. (equal ": \n: \n"
  1570. (org-test-with-temp-text "\n\n"
  1571. (transient-mark-mode 1)
  1572. (push-mark (point) t t)
  1573. (goto-char (point-max))
  1574. (org-toggle-fixed-width)
  1575. (buffer-string)))))
  1576. (ert-deftest test-org/kill-line ()
  1577. "Test `org-kill-line' specifications."
  1578. ;; At the beginning of a line, kill whole line.
  1579. (should
  1580. (equal ""
  1581. (org-test-with-temp-text "abc"
  1582. (org-kill-line)
  1583. (buffer-string))))
  1584. ;; In the middle of a line, kill line until its end.
  1585. (should
  1586. (equal "a"
  1587. (org-test-with-temp-text "a<point>bc"
  1588. (org-kill-line)
  1589. (buffer-string))))
  1590. ;; Do not kill newline character.
  1591. (should
  1592. (equal "\n123"
  1593. (org-test-with-temp-text "abc\n123"
  1594. (org-kill-line)
  1595. (buffer-string))))
  1596. (should
  1597. (equal "a\n123"
  1598. (org-test-with-temp-text "a<point>bc\n123"
  1599. (org-kill-line)
  1600. (buffer-string))))
  1601. ;; When `org-special-ctrl-k' is non-nil and point is at a headline,
  1602. ;; kill until tags.
  1603. (should
  1604. (equal "* A :tag:"
  1605. (org-test-with-temp-text "* A<point>B :tag:"
  1606. (let ((org-special-ctrl-k t)
  1607. (org-tags-column 0))
  1608. (org-kill-line))
  1609. (buffer-string))))
  1610. ;; If point is on tags, only kill part left until the end of line.
  1611. (should
  1612. (equal "* A :tag:"
  1613. (org-test-with-temp-text "* A :tag:<point>tag2:"
  1614. (let ((org-special-ctrl-k t)
  1615. (org-tags-column 0))
  1616. (org-kill-line))
  1617. (buffer-string))))
  1618. ;; However, if point is at the beginning of the line, kill whole
  1619. ;; headline.
  1620. (should
  1621. (equal ""
  1622. (org-test-with-temp-text "* AB :tag:"
  1623. (let ((org-special-ctrl-k t)
  1624. (org-tags-column 0))
  1625. (org-kill-line))
  1626. (buffer-string))))
  1627. ;; When `org-ctrl-k-protect-subtree' is non-nil, and point is in
  1628. ;; invisible text, ask before removing it. When set to `error',
  1629. ;; throw an error.
  1630. (should-error
  1631. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  1632. (org-overview)
  1633. (let ((org-special-ctrl-k nil)
  1634. (org-ctrl-k-protect-subtree t))
  1635. (cl-letf (((symbol-function 'y-or-n-p) 'ignore))
  1636. (org-kill-line)))))
  1637. (should-error
  1638. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  1639. (org-overview)
  1640. (let ((org-special-ctrl-k nil)
  1641. (org-ctrl-k-protect-subtree 'error))
  1642. (org-kill-line)))))
  1643. ;;; Headline
  1644. (ert-deftest test-org/get-heading ()
  1645. "Test `org-get-heading' specifications."
  1646. ;; Return current heading, even if point is not on it.
  1647. (should
  1648. (equal "H"
  1649. (org-test-with-temp-text "* H"
  1650. (org-get-heading))))
  1651. (should
  1652. (equal "H"
  1653. (org-test-with-temp-text "* H\nText<point>"
  1654. (org-get-heading))))
  1655. ;; Without any optional argument, return TODO keyword, priority
  1656. ;; cookie, COMMENT keyword and tags.
  1657. (should
  1658. (equal "TODO H"
  1659. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1660. (org-get-heading))))
  1661. (should
  1662. (equal "[#A] H"
  1663. (org-test-with-temp-text "* [#A] H"
  1664. (org-get-heading))))
  1665. (should
  1666. (equal "COMMENT H"
  1667. (org-test-with-temp-text "* COMMENT H"
  1668. (org-get-heading))))
  1669. (should
  1670. (equal "H :tag:"
  1671. (org-test-with-temp-text "* H :tag:"
  1672. (org-get-heading))))
  1673. ;; With NO-TAGS argument, ignore tags.
  1674. (should
  1675. (equal "TODO H"
  1676. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1677. (org-get-heading t))))
  1678. (should
  1679. (equal "H"
  1680. (org-test-with-temp-text "* H :tag:"
  1681. (org-get-heading t))))
  1682. ;; With NO-TODO, ignore TODO keyword.
  1683. (should
  1684. (equal "H"
  1685. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1686. (org-get-heading nil t))))
  1687. (should
  1688. (equal "H :tag:"
  1689. (org-test-with-temp-text "* H :tag:"
  1690. (org-get-heading nil t))))
  1691. ;; TODO keywords are case-sensitive.
  1692. (should
  1693. (equal "Todo H"
  1694. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1695. (org-get-heading nil t))))
  1696. ;; With NO-PRIORITY, ignore priority.
  1697. (should
  1698. (equal "H"
  1699. (org-test-with-temp-text "* [#A] H"
  1700. (org-get-heading nil nil t))))
  1701. (should
  1702. (equal "H"
  1703. (org-test-with-temp-text "* H"
  1704. (org-get-heading nil nil t))))
  1705. (should
  1706. (equal "TODO H"
  1707. (org-test-with-temp-text "* TODO [#A] H"
  1708. (org-get-heading nil nil t))))
  1709. ;; With NO-COMMENT, ignore COMMENT keyword.
  1710. (should
  1711. (equal "H"
  1712. (org-test-with-temp-text "* COMMENT H"
  1713. (org-get-heading nil nil nil t))))
  1714. (should
  1715. (equal "H"
  1716. (org-test-with-temp-text "* H"
  1717. (org-get-heading nil nil nil t))))
  1718. (should
  1719. (equal "TODO [#A] H"
  1720. (org-test-with-temp-text "* TODO [#A] COMMENT H"
  1721. (org-get-heading nil nil nil t))))
  1722. ;; On an empty headline, return value is consistent.
  1723. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1724. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1725. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1726. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil t))))
  1727. (should
  1728. (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil nil t)))))
  1729. (ert-deftest test-org/in-commented-heading-p ()
  1730. "Test `org-in-commented-heading-p' specifications."
  1731. ;; Commented headline.
  1732. (should
  1733. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1734. (goto-char (point-max))
  1735. (org-in-commented-heading-p)))
  1736. ;; Commented ancestor.
  1737. (should
  1738. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1739. (goto-char (point-max))
  1740. (org-in-commented-heading-p)))
  1741. ;; Comment keyword is case-sensitive.
  1742. (should-not
  1743. (org-test-with-temp-text "* Comment Headline\nBody"
  1744. (goto-char (point-max))
  1745. (org-in-commented-heading-p)))
  1746. ;; Keyword is standalone.
  1747. (should-not
  1748. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1749. (goto-char (point-max))
  1750. (org-in-commented-heading-p)))
  1751. ;; Optional argument.
  1752. (should-not
  1753. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1754. (goto-char (point-max))
  1755. (org-in-commented-heading-p t))))
  1756. (ert-deftest test-org/entry-blocked-p ()
  1757. ;; Check other dependencies.
  1758. (should
  1759. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1760. (let ((org-enforce-todo-dependencies t)
  1761. (org-blocker-hook
  1762. '(org-block-todo-from-children-or-siblings-or-parent)))
  1763. (org-entry-blocked-p))))
  1764. (should-not
  1765. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1766. (let ((org-enforce-todo-dependencies t)
  1767. (org-blocker-hook
  1768. '(org-block-todo-from-children-or-siblings-or-parent)))
  1769. (org-entry-blocked-p))))
  1770. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1771. ;; blocked.
  1772. (should-not
  1773. (org-test-with-temp-text "* Blocked\n** TODO one"
  1774. (let ((org-enforce-todo-dependencies t)
  1775. (org-blocker-hook
  1776. '(org-block-todo-from-children-or-siblings-or-parent)))
  1777. (org-entry-blocked-p))))
  1778. (should-not
  1779. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1780. (let ((org-enforce-todo-dependencies t)
  1781. (org-blocker-hook
  1782. '(org-block-todo-from-children-or-siblings-or-parent)))
  1783. (org-entry-blocked-p))))
  1784. ;; Follow :ORDERED: specifications.
  1785. (should
  1786. (org-test-with-temp-text
  1787. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1788. (let ((org-enforce-todo-dependencies t)
  1789. (org-blocker-hook
  1790. '(org-block-todo-from-children-or-siblings-or-parent)))
  1791. (org-entry-blocked-p))))
  1792. (should-not
  1793. (org-test-with-temp-text
  1794. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1795. (let ((org-enforce-todo-dependencies t)
  1796. (org-blocker-hook
  1797. '(org-block-todo-from-children-or-siblings-or-parent)))
  1798. (org-entry-blocked-p)))))
  1799. (ert-deftest test-org/get-outline-path ()
  1800. "Test `org-get-outline-path' specifications."
  1801. ;; Top-level headlines have no outline path.
  1802. (should-not
  1803. (org-test-with-temp-text "* H"
  1804. (org-get-outline-path)))
  1805. ;; Otherwise, outline path is the path leading to the headline.
  1806. (should
  1807. (equal '("H")
  1808. (org-test-with-temp-text "* H\n** S<point>"
  1809. (org-get-outline-path))))
  1810. ;; Find path even when point is not on a headline.
  1811. (should
  1812. (equal '("H")
  1813. (org-test-with-temp-text "* H\n** S\nText<point>"
  1814. (org-get-outline-path))))
  1815. ;; TODO keywords, tags and statistics cookies are ignored.
  1816. (should
  1817. (equal '("H")
  1818. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1819. (org-get-outline-path))))
  1820. ;; Links are replaced with their description or their path.
  1821. (should
  1822. (equal '("Org")
  1823. (org-test-with-temp-text
  1824. "* [[https://orgmode.org][Org]]\n** S<point>"
  1825. (org-get-outline-path))))
  1826. (should
  1827. (equal '("https://orgmode.org")
  1828. (org-test-with-temp-text
  1829. "* [[https://orgmode.org]]\n** S<point>"
  1830. (org-get-outline-path))))
  1831. ;; When WITH-SELF is non-nil, include current heading.
  1832. (should
  1833. (equal '("H")
  1834. (org-test-with-temp-text "* H"
  1835. (org-get-outline-path t))))
  1836. (should
  1837. (equal '("H" "S")
  1838. (org-test-with-temp-text "* H\n** S\nText<point>"
  1839. (org-get-outline-path t))))
  1840. ;; Using cache is transparent to the user.
  1841. (should
  1842. (equal '("H")
  1843. (org-test-with-temp-text "* H\n** S<point>"
  1844. (setq org-outline-path-cache nil)
  1845. (org-get-outline-path nil t))))
  1846. ;; Do not corrupt cache when finding outline path in distant part of
  1847. ;; the buffer.
  1848. (should
  1849. (equal '("H2")
  1850. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1851. (setq org-outline-path-cache nil)
  1852. (org-get-outline-path nil t)
  1853. (search-forward "S2")
  1854. (org-get-outline-path nil t))))
  1855. ;; Do not choke on empty headlines.
  1856. (should
  1857. (org-test-with-temp-text "* H\n** <point>"
  1858. (org-get-outline-path)))
  1859. (should
  1860. (org-test-with-temp-text "* \n** H<point>"
  1861. (org-get-outline-path))))
  1862. (ert-deftest test-org/format-outline-path ()
  1863. "Test `org-format-outline-path' specifications."
  1864. (should
  1865. (string= (org-format-outline-path (list "one" "two" "three"))
  1866. "one/two/three"))
  1867. ;; Empty path.
  1868. (should
  1869. (string= (org-format-outline-path '())
  1870. ""))
  1871. (should
  1872. (string= (org-format-outline-path '(nil))
  1873. ""))
  1874. ;; Empty path and prefix.
  1875. (should
  1876. (string= (org-format-outline-path '() nil ">>")
  1877. ">>"))
  1878. ;; Trailing whitespace in headings.
  1879. (should
  1880. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1881. "one/tw o/three"))
  1882. ;; Non-default prefix and separators.
  1883. (should
  1884. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1885. ">>|one|two|three"))
  1886. ;; Truncate.
  1887. (should
  1888. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1889. "one/two/.."))
  1890. ;; Give a very narrow width.
  1891. (should
  1892. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1893. "on"))
  1894. ;; Give a prefix that extends beyond the width.
  1895. (should
  1896. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1897. ">>>>>>>>>>")
  1898. ">>>>>>>>..")))
  1899. (ert-deftest test-org/map-entries ()
  1900. "Test `org-map-entries' specifications."
  1901. ;; Full match.
  1902. (should
  1903. (equal '(1 11)
  1904. (org-test-with-temp-text "* Level 1\n** Level 2"
  1905. (org-map-entries #'point))))
  1906. ;; Level match.
  1907. (should
  1908. (equal '(1)
  1909. (org-test-with-temp-text "* Level 1\n** Level 2"
  1910. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1911. (should
  1912. (equal '(11)
  1913. (org-test-with-temp-text "* Level 1\n** Level 2"
  1914. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1915. ;; Tag match.
  1916. (should
  1917. (equal '(11)
  1918. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1919. (org-map-entries #'point "yes"))))
  1920. (should
  1921. (equal '(14)
  1922. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1923. (org-map-entries #'point "+yes-a"))))
  1924. (should
  1925. (equal '(11 23)
  1926. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1927. (org-map-entries #'point "{yes?}"))))
  1928. ;; Priority match.
  1929. (should
  1930. (equal '(1)
  1931. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1932. (org-map-entries #'point "PRIORITY=\"A\""))))
  1933. ;; Date match.
  1934. (should
  1935. (equal '(36)
  1936. (org-test-with-temp-text "
  1937. * H1
  1938. SCHEDULED: <2012-03-29 thu.>
  1939. * H2
  1940. SCHEDULED: <2014-03-04 tue.>"
  1941. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1942. (should
  1943. (equal '(2)
  1944. (org-test-with-temp-text "
  1945. * H1
  1946. SCHEDULED: <2012-03-29 thu.>
  1947. * H2
  1948. SCHEDULED: <2014-03-04 tue.>"
  1949. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1950. ;; Regular property match.
  1951. (should
  1952. (equal '(2)
  1953. (org-test-with-temp-text "
  1954. * H1
  1955. :PROPERTIES:
  1956. :TEST: 1
  1957. :END:
  1958. * H2
  1959. :PROPERTIES:
  1960. :TEST: 2
  1961. :END:"
  1962. (org-map-entries #'point "TEST=1"))))
  1963. ;; Multiple criteria.
  1964. (should
  1965. (equal '(23)
  1966. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1967. (let (org-odd-levels-only
  1968. (org-use-tag-inheritance nil))
  1969. (org-map-entries #'point "yes+LEVEL=1")))))
  1970. ;; "or" criteria.
  1971. (should
  1972. (equal '(12 24)
  1973. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1974. (let (org-odd-levels-only)
  1975. (org-map-entries #'point "LEVEL=2|no")))))
  1976. (should
  1977. (equal '(1 12)
  1978. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  1979. (let (org-odd-levels-only)
  1980. (org-map-entries #'point "yes|no")))))
  1981. ;; "and" criteria.
  1982. (should
  1983. (equal '(22)
  1984. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  1985. (let (org-odd-levels-only)
  1986. (org-map-entries #'point "yes&no"))))))
  1987. (ert-deftest test-org/edit-headline ()
  1988. "Test `org-edit-headline' specifications."
  1989. (should
  1990. (equal "* B"
  1991. (org-test-with-temp-text "* A"
  1992. (org-edit-headline "B")
  1993. (buffer-string))))
  1994. ;; Handle empty headings.
  1995. (should
  1996. (equal "* "
  1997. (org-test-with-temp-text "* A"
  1998. (org-edit-headline "")
  1999. (buffer-string))))
  2000. (should
  2001. (equal "* A"
  2002. (org-test-with-temp-text "* "
  2003. (org-edit-headline "A")
  2004. (buffer-string))))
  2005. ;; Handle TODO keywords and priority cookies.
  2006. (should
  2007. (equal "* TODO B"
  2008. (org-test-with-temp-text "* TODO A"
  2009. (org-edit-headline "B")
  2010. (buffer-string))))
  2011. (should
  2012. (equal "* [#A] B"
  2013. (org-test-with-temp-text "* [#A] A"
  2014. (org-edit-headline "B")
  2015. (buffer-string))))
  2016. (should
  2017. (equal "* TODO [#A] B"
  2018. (org-test-with-temp-text "* TODO [#A] A"
  2019. (org-edit-headline "B")
  2020. (buffer-string))))
  2021. ;; Handle tags.
  2022. (equal "* B :tag:"
  2023. (org-test-with-temp-text "* A :tag:"
  2024. (let ((org-tags-column 4)) (org-edit-headline "B"))
  2025. (buffer-string))))
  2026. ;;; Keywords
  2027. (ert-deftest test-org/set-regexps-and-options ()
  2028. "Test `org-set-regexps-and-options' specifications."
  2029. ;; TAGS keyword.
  2030. (should
  2031. (equal '(("A"))
  2032. (let ((org-tag-alist '(("A")))
  2033. (org-tag-persistent-alist nil))
  2034. (org-test-with-temp-text ""
  2035. (org-mode-restart)
  2036. org-current-tag-alist))))
  2037. (should
  2038. (equal '(("B"))
  2039. (let ((org-tag-alist '(("A")))
  2040. (org-tag-persistent-alist nil))
  2041. (org-test-with-temp-text "#+TAGS: B"
  2042. (org-mode-restart)
  2043. org-current-tag-alist))))
  2044. (should
  2045. (equal '(("C") ("B"))
  2046. (let ((org-tag-alist '(("A")))
  2047. (org-tag-persistent-alist '(("C"))))
  2048. (org-test-with-temp-text "#+TAGS: B"
  2049. (org-mode-restart)
  2050. org-current-tag-alist))))
  2051. (should
  2052. (equal '(("B"))
  2053. (let ((org-tag-alist '(("A")))
  2054. (org-tag-persistent-alist '(("C"))))
  2055. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  2056. (org-mode-restart)
  2057. org-current-tag-alist))))
  2058. (should
  2059. (equal '(("A" . ?a) ("B") ("C"))
  2060. (let ((org-tag-persistant-alist nil))
  2061. (org-test-with-temp-text "#+TAGS: A(a) B C"
  2062. (org-mode-restart)
  2063. org-current-tag-alist))))
  2064. (should
  2065. (equal '(("A") (:newline) ("B"))
  2066. (let ((org-tag-persistent-alist nil))
  2067. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  2068. (org-mode-restart)
  2069. org-current-tag-alist))))
  2070. (should
  2071. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  2072. (let ((org-tag-persistent-alist nil))
  2073. (org-test-with-temp-text "#+TAGS: { A B } C"
  2074. (org-mode-restart)
  2075. org-current-tag-alist))))
  2076. (should
  2077. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  2078. (let ((org-tag-persistent-alist nil))
  2079. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2080. (org-mode-restart)
  2081. org-current-tag-alist))))
  2082. (should
  2083. (equal '(("A" "B" "C"))
  2084. (let ((org-tag-persistent-alist nil))
  2085. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2086. (org-mode-restart)
  2087. org-tag-groups-alist))))
  2088. (should
  2089. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  2090. (let ((org-tag-persistent-alist nil))
  2091. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2092. (org-mode-restart)
  2093. org-current-tag-alist))))
  2094. (should
  2095. (equal '(("A" "B" "C"))
  2096. (let ((org-tag-persistent-alist nil))
  2097. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2098. (org-mode-restart)
  2099. org-tag-groups-alist))))
  2100. ;; FILETAGS keyword.
  2101. (should
  2102. (equal '("A" "B" "C")
  2103. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  2104. (org-mode-restart)
  2105. org-file-tags)))
  2106. ;; PROPERTY keyword. Property names are case-insensitive.
  2107. (should
  2108. (equal "foo=1"
  2109. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  2110. (org-mode-restart)
  2111. (cdr (assoc "var" org-file-properties)))))
  2112. (should
  2113. (equal
  2114. "foo=1 bar=2"
  2115. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  2116. (org-mode-restart)
  2117. (cdr (assoc "var" org-file-properties)))))
  2118. (should
  2119. (equal
  2120. "foo=1 bar=2"
  2121. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  2122. (org-mode-restart)
  2123. (cdr (assoc "var" org-file-properties)))))
  2124. ;; ARCHIVE keyword.
  2125. (should
  2126. (equal "%s_done::"
  2127. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  2128. (org-mode-restart)
  2129. org-archive-location)))
  2130. ;; CATEGORY keyword.
  2131. (should
  2132. (eq 'test
  2133. (org-test-with-temp-text "#+CATEGORY: test"
  2134. (org-mode-restart)
  2135. org-category)))
  2136. (should
  2137. (equal "test"
  2138. (org-test-with-temp-text "#+CATEGORY: test"
  2139. (org-mode-restart)
  2140. (cdr (assoc "CATEGORY" org-file-properties)))))
  2141. ;; COLUMNS keyword.
  2142. (should
  2143. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  2144. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  2145. (org-mode-restart)
  2146. org-columns-default-format)))
  2147. ;; CONSTANTS keyword. Constants names are case sensitive.
  2148. (should
  2149. (equal '("299792458." "3.14")
  2150. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  2151. (org-mode-restart)
  2152. (mapcar
  2153. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  2154. '("c" "pi")))))
  2155. (should
  2156. (equal "3.14"
  2157. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  2158. (org-mode-restart)
  2159. (cdr (assoc "pi" org-table-formula-constants-local)))))
  2160. (should
  2161. (equal "22/7"
  2162. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  2163. (org-mode-restart)
  2164. (cdr (assoc "PI" org-table-formula-constants-local)))))
  2165. ;; LINK keyword.
  2166. (should
  2167. (equal
  2168. '("url1" "url2")
  2169. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  2170. (org-mode-restart)
  2171. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  2172. '("a" "b")))))
  2173. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  2174. (should
  2175. (equal
  2176. '(?X ?Z ?Y)
  2177. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  2178. (org-mode-restart)
  2179. (list org-highest-priority org-lowest-priority org-default-priority))))
  2180. (should
  2181. (equal
  2182. '(?A ?C ?B)
  2183. (org-test-with-temp-text "#+PRIORITIES: X Z"
  2184. (org-mode-restart)
  2185. (list org-highest-priority org-lowest-priority org-default-priority))))
  2186. ;; STARTUP keyword.
  2187. (should
  2188. (equal '(t t)
  2189. (org-test-with-temp-text "#+STARTUP: fold odd"
  2190. (org-mode-restart)
  2191. (list org-startup-folded org-odd-levels-only))))
  2192. ;; TODO keywords.
  2193. (should
  2194. (equal '(("A" "B") ("C"))
  2195. (org-test-with-temp-text "#+TODO: A B | C"
  2196. (org-mode-restart)
  2197. (list org-not-done-keywords org-done-keywords))))
  2198. (should
  2199. (equal '(("A" "C") ("B" "D"))
  2200. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  2201. (org-mode-restart)
  2202. (list org-not-done-keywords org-done-keywords))))
  2203. (should
  2204. (equal '(("A" "B") ("C"))
  2205. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  2206. (org-mode-restart)
  2207. (list org-not-done-keywords org-done-keywords))))
  2208. (should
  2209. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  2210. (org-test-with-temp-text "#+TODO: A(a)"
  2211. (org-mode-restart)
  2212. org-todo-key-alist)))
  2213. (should
  2214. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  2215. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  2216. (org-mode-restart)
  2217. org-todo-log-states)))
  2218. ;; Enter SETUPFILE keyword.
  2219. (should
  2220. (equal "1"
  2221. (org-test-with-temp-text
  2222. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  2223. (org-mode-restart)
  2224. (cdr (assoc "a" org-file-properties))))))
  2225. ;;; Links
  2226. ;;;; Coderefs
  2227. (ert-deftest test-org/coderef ()
  2228. "Test coderef links specifications."
  2229. (should
  2230. (org-test-with-temp-text "
  2231. #+BEGIN_SRC emacs-lisp
  2232. \(+ 1 1) (ref:sc)
  2233. #+END_SRC
  2234. \[[(sc)<point>]]"
  2235. (org-open-at-point)
  2236. (looking-at "(ref:sc)")))
  2237. ;; Find coderef even with alternate label format.
  2238. (should
  2239. (org-test-with-temp-text "
  2240. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2241. \(+ 1 1) {ref:sc}
  2242. #+END_SRC
  2243. \[[(sc)<point>]]"
  2244. (org-open-at-point)
  2245. (looking-at "{ref:sc}"))))
  2246. ;;;; Custom ID
  2247. (ert-deftest test-org/custom-id ()
  2248. "Test custom ID links specifications."
  2249. (should
  2250. (org-test-with-temp-text
  2251. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2252. (org-open-at-point)
  2253. (looking-at-p "\\* H1")))
  2254. ;; Handle escape characters.
  2255. (should
  2256. (org-test-with-temp-text
  2257. "* H1\n:PROPERTIES:\n:CUSTOM_ID: [%]\n:END:\n* H2\n[[#%5B%25%5D<point>]]"
  2258. (org-open-at-point)
  2259. (looking-at-p "\\* H1")))
  2260. ;; Throw an error on false positives.
  2261. (should-error
  2262. (org-test-with-temp-text
  2263. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2264. (org-open-at-point)
  2265. (looking-at-p "\\* H1"))))
  2266. ;;;; Fuzzy Links
  2267. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2268. ;; a named element (#+name: text) and to headlines (* Text).
  2269. (ert-deftest test-org/fuzzy-links ()
  2270. "Test fuzzy links specifications."
  2271. ;; Fuzzy link goes in priority to a matching target.
  2272. (should
  2273. (org-test-with-temp-text
  2274. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2275. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2276. (looking-at "<<Test>>")))
  2277. ;; Then fuzzy link points to an element with a given name.
  2278. (should
  2279. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2280. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2281. (looking-at "#\\+NAME: Test")))
  2282. ;; A target still lead to a matching headline otherwise.
  2283. (should
  2284. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2285. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2286. (looking-at "\\* Head2")))
  2287. ;; With a leading star in link, enforce heading match.
  2288. (should
  2289. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2290. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2291. (looking-at "\\* Test")))
  2292. ;; With a leading star in link, enforce exact heading match, even
  2293. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2294. (should-error
  2295. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2296. (let ((org-link-search-must-match-exact-headline nil))
  2297. (org-open-at-point))))
  2298. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2299. (should
  2300. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2301. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2302. (looking-at "\\* Test")))
  2303. (should
  2304. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2305. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2306. (looking-at "\\* Test")))
  2307. ;; Heading match should not care about spaces, cookies, TODO
  2308. ;; keywords, priorities, and tags. However, TODO keywords are
  2309. ;; case-sensitive.
  2310. (should
  2311. (let ((first-line
  2312. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2313. (org-test-with-temp-text
  2314. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2315. (let ((org-link-search-must-match-exact-headline nil)
  2316. (org-todo-regexp "TODO"))
  2317. (org-open-at-point))
  2318. (looking-at (regexp-quote first-line)))))
  2319. (should-error
  2320. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2321. (let ((org-link-search-must-match-exact-headline nil)
  2322. (org-todo-regexp "TODO"))
  2323. (org-open-at-point))))
  2324. ;; Heading match should still be exact.
  2325. (should-error
  2326. (org-test-with-temp-text "
  2327. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2328. Foo Bar
  2329. <point>[[*Test 1]]"
  2330. (let ((org-link-search-must-match-exact-headline nil)
  2331. (org-todo-regexp "TODO"))
  2332. (org-open-at-point))))
  2333. (should
  2334. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2335. (let ((org-link-search-must-match-exact-headline nil)
  2336. (org-todo-regexp "TODO"))
  2337. (org-open-at-point))
  2338. (looking-at-p (regexp-quote "** Test 1 2"))))
  2339. ;; Heading match ignores COMMENT keyword.
  2340. (should
  2341. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2342. (org-open-at-point)
  2343. (looking-at "\\* COMMENT Test")))
  2344. (should
  2345. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2346. (org-open-at-point)
  2347. (looking-at "\\* TODO COMMENT Test")))
  2348. ;; Correctly un-hexify fuzzy links.
  2349. (should
  2350. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2351. (org-open-at-point)
  2352. (bobp)))
  2353. (should
  2354. (org-test-with-temp-text "* [1]\n[[*%5B1%5D<point>]]"
  2355. (org-open-at-point)
  2356. (bobp)))
  2357. ;; Match search strings containing newline characters, including
  2358. ;; blank lines.
  2359. (should
  2360. (org-test-with-temp-text-in-file "Paragraph\n\nline1\nline2\n\n"
  2361. (let ((file (buffer-file-name)))
  2362. (goto-char (point-max))
  2363. (insert (format "[[file:%s::line1 line2]]" file))
  2364. (beginning-of-line)
  2365. (let ((org-link-search-must-match-exact-headline nil))
  2366. (org-open-at-point 0))
  2367. (looking-at-p "line1"))))
  2368. (should
  2369. (org-test-with-temp-text-in-file "Paragraph\n\nline1\n\nline2\n\n"
  2370. (let ((file (buffer-file-name)))
  2371. (goto-char (point-max))
  2372. (insert (format "[[file:%s::line1 line2]]" file))
  2373. (beginning-of-line)
  2374. (let ((org-link-search-must-match-exact-headline nil))
  2375. (org-open-at-point 0))
  2376. (looking-at-p "line1")))))
  2377. ;;;; Link Escaping
  2378. (ert-deftest test-org/org-link-escape-ascii-character ()
  2379. "Escape an ascii character."
  2380. (should
  2381. (string=
  2382. "%5B"
  2383. (org-link-escape "["))))
  2384. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2385. "Escape an ascii control character."
  2386. (should
  2387. (string=
  2388. "%09"
  2389. (org-link-escape "\t"))))
  2390. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2391. "Escape an unicode multibyte character."
  2392. (should
  2393. (string=
  2394. "%E2%82%AC"
  2395. (org-link-escape "€"))))
  2396. (ert-deftest test-org/org-link-escape-custom-table ()
  2397. "Escape string with custom character table."
  2398. (should
  2399. (string=
  2400. "Foo%3A%42ar%0A"
  2401. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2402. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2403. "Escape string with custom table merged with default table."
  2404. (should
  2405. (string=
  2406. "%5BF%6F%6F%3A%42ar%0A%5D"
  2407. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2408. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2409. "Unescape an ascii character."
  2410. (should
  2411. (string=
  2412. "["
  2413. (org-link-unescape "%5B"))))
  2414. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2415. "Unescpae an ascii control character."
  2416. (should
  2417. (string=
  2418. "\n"
  2419. (org-link-unescape "%0A"))))
  2420. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2421. "Unescape unicode multibyte character."
  2422. (should
  2423. (string=
  2424. "€"
  2425. (org-link-unescape "%E2%82%AC"))))
  2426. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2427. "Unescape old style percent escaped character."
  2428. (should
  2429. (string=
  2430. "àâçèéêîôùû"
  2431. (decode-coding-string
  2432. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2433. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2434. "Escape and unescape a URL that includes an escaped char.
  2435. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2436. (should
  2437. (string=
  2438. "http://some.host.com/form?&id=blah%2Bblah25"
  2439. (org-link-unescape
  2440. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2441. ;;;; Open at point
  2442. (ert-deftest test-org/open-at-point/keyword ()
  2443. "Does `org-open-at-point' open link in a keyword line?"
  2444. (should
  2445. (org-test-with-temp-text
  2446. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2447. (org-open-at-point) t))
  2448. (should
  2449. (org-test-with-temp-text
  2450. "* H\n<<top>>\n#+KEYWORD: <point>[[top]]"
  2451. (org-open-at-point) t)))
  2452. (ert-deftest test-org/open-at-point/property ()
  2453. "Does `org-open-at-point' open link in property drawer?"
  2454. (should
  2455. (org-test-with-temp-text
  2456. "* Headline
  2457. :PROPERTIES:
  2458. :URL: <point>[[*Headline]]
  2459. :END:"
  2460. (org-open-at-point) t)))
  2461. (ert-deftest test-org/open-at-point/comment ()
  2462. "Does `org-open-at-point' open link in a commented line?"
  2463. (should
  2464. (org-test-with-temp-text
  2465. "<<top>>\n# <point>[[top]]"
  2466. (org-open-at-point) t))
  2467. (should
  2468. (org-test-with-temp-text
  2469. "* H\n<<top>>\n# <point>[[top]]"
  2470. (org-open-at-point) t)))
  2471. (ert-deftest test-org/open-at-point/inline-image ()
  2472. "Test `org-open-at-point' on nested links."
  2473. (should
  2474. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2475. (org-open-at-point)
  2476. (bobp))))
  2477. (ert-deftest test-org/open-at-point/radio-target ()
  2478. "Test `org-open-at-point' on radio targets."
  2479. (should
  2480. (org-test-with-temp-text "<<<target>>> <point>target"
  2481. (org-update-radio-target-regexp)
  2482. (org-open-at-point)
  2483. (eq (org-element-type (org-element-context)) 'radio-target))))
  2484. (ert-deftest test-org/open-at-point/tag ()
  2485. "Test `org-open-at-point' on tags."
  2486. (should
  2487. (org-test-with-temp-text "* H :<point>tag:"
  2488. (catch :result
  2489. (cl-letf (((symbol-function 'org-tags-view)
  2490. (lambda (&rest args) (throw :result t))))
  2491. (org-open-at-point)
  2492. nil))))
  2493. (should-not
  2494. (org-test-with-temp-text-in-file "* H<point> :tag:"
  2495. (catch :result
  2496. (cl-letf (((symbol-function 'org-tags-view)
  2497. (lambda (&rest args) (throw :result t))))
  2498. (org-open-at-point)
  2499. nil)))))
  2500. ;;;; Stored links
  2501. (ert-deftest test-org/store-link ()
  2502. "Test `org-store-link' specifications."
  2503. ;; On a headline, link to that headline. Use heading as the
  2504. ;; description of the link.
  2505. (should
  2506. (let (org-store-link-props org-stored-links)
  2507. (org-test-with-temp-text-in-file "* H1"
  2508. (let ((file (buffer-file-name)))
  2509. (equal (format "[[file:%s::*H1][H1]]" file)
  2510. (org-store-link nil))))))
  2511. ;; On a headline, remove any link from description.
  2512. (should
  2513. (let (org-store-link-props org-stored-links)
  2514. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2515. (let ((file (buffer-file-name)))
  2516. (equal (format "[[file:%s::*%s][d]]"
  2517. file
  2518. (org-link-escape "[[#l][d]]"))
  2519. (org-store-link nil))))))
  2520. (should
  2521. (let (org-store-link-props org-stored-links)
  2522. (org-test-with-temp-text-in-file "* [[l]]"
  2523. (let ((file (buffer-file-name)))
  2524. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2525. (org-store-link nil))))))
  2526. (should
  2527. (let (org-store-link-props org-stored-links)
  2528. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2529. (let ((file (buffer-file-name)))
  2530. (equal (format "[[file:%s::*%s][d1 d2]]"
  2531. file
  2532. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2533. (org-store-link nil))))))
  2534. ;; On a named element, link to that element.
  2535. (should
  2536. (let (org-store-link-props org-stored-links)
  2537. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2538. (let ((file (buffer-file-name)))
  2539. (equal (format "[[file:%s::foo][foo]]" file)
  2540. (org-store-link nil))))))
  2541. ;; Store link to Org buffer, with context.
  2542. (should
  2543. (let ((org-stored-links nil)
  2544. (org-id-link-to-org-use-id nil)
  2545. (org-context-in-file-links t))
  2546. (org-test-with-temp-text-in-file "* h1"
  2547. (let ((file (buffer-file-name)))
  2548. (equal (format "[[file:%s::*h1][h1]]" file)
  2549. (org-store-link nil))))))
  2550. ;; Store link to Org buffer, without context.
  2551. (should
  2552. (let ((org-stored-links nil)
  2553. (org-id-link-to-org-use-id nil)
  2554. (org-context-in-file-links nil))
  2555. (org-test-with-temp-text-in-file "* h1"
  2556. (let ((file (buffer-file-name)))
  2557. (equal (format "[[file:%s][file:%s]]" file file)
  2558. (org-store-link nil))))))
  2559. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  2560. (should
  2561. (let ((org-stored-links nil)
  2562. (org-id-link-to-org-use-id nil)
  2563. (org-context-in-file-links nil))
  2564. (org-test-with-temp-text-in-file "* h1"
  2565. (let ((file (buffer-file-name)))
  2566. (equal (format "[[file:%s::*h1][h1]]" file)
  2567. (org-store-link '(4)))))))
  2568. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  2569. ;; buffer.
  2570. (should
  2571. (let ((org-stored-links nil)
  2572. (org-id-link-to-org-use-id nil)
  2573. (org-context-in-file-links nil))
  2574. (org-test-with-temp-text-in-file "* h1"
  2575. (let ((file (buffer-file-name)))
  2576. (equal (format "[[file:%s][file:%s]]" file file)
  2577. (org-store-link '(16)))))))
  2578. ;; Store file link to non-Org buffer, with context.
  2579. (should
  2580. (let ((org-stored-links nil)
  2581. (org-context-in-file-links t))
  2582. (org-test-with-temp-text-in-file "one\n<point>two"
  2583. (fundamental-mode)
  2584. (let ((file (buffer-file-name)))
  2585. (equal (format "[[file:%s::one]]" file)
  2586. (org-store-link nil))))))
  2587. ;; Store file link to non-Org buffer, without context.
  2588. (should
  2589. (let ((org-stored-links nil)
  2590. (org-context-in-file-links nil))
  2591. (org-test-with-temp-text-in-file "one\n<point>two"
  2592. (fundamental-mode)
  2593. (let ((file (buffer-file-name)))
  2594. (equal (format "[[file:%s][file:%s]]" file file)
  2595. (org-store-link nil))))))
  2596. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  2597. ;; buffer.
  2598. (should
  2599. (let ((org-stored-links nil)
  2600. (org-context-in-file-links nil))
  2601. (org-test-with-temp-text-in-file "one\n<point>two"
  2602. (fundamental-mode)
  2603. (let ((file (buffer-file-name)))
  2604. (equal (format "[[file:%s::one]]" file)
  2605. (org-store-link '(4)))))))
  2606. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  2607. ;; non-Org buffer.
  2608. (should
  2609. (let ((org-stored-links nil)
  2610. (org-context-in-file-links nil))
  2611. (org-test-with-temp-text-in-file "one\n<point>two"
  2612. (fundamental-mode)
  2613. (let ((file (buffer-file-name)))
  2614. (equal (format "[[file:%s][file:%s]]" file file)
  2615. (org-store-link '(16))))))))
  2616. ;;; Node Properties
  2617. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2618. "Ensure properties accumulate in subtree drawers."
  2619. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2620. (org-babel-next-src-block)
  2621. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2622. (ert-deftest test-org/custom-properties ()
  2623. "Test custom properties specifications."
  2624. ;; Standard test.
  2625. (should
  2626. (let ((org-custom-properties '("FOO")))
  2627. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2628. (org-toggle-custom-properties-visibility)
  2629. (org-invisible-p2))))
  2630. ;; Properties are case-insensitive.
  2631. (should
  2632. (let ((org-custom-properties '("FOO")))
  2633. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2634. (org-toggle-custom-properties-visibility)
  2635. (org-invisible-p2))))
  2636. (should
  2637. (let ((org-custom-properties '("foo")))
  2638. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2639. (org-toggle-custom-properties-visibility)
  2640. (org-invisible-p2))))
  2641. ;; Multiple custom properties in the same drawer.
  2642. (should
  2643. (let ((org-custom-properties '("FOO" "BAR")))
  2644. (org-test-with-temp-text
  2645. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2646. (org-toggle-custom-properties-visibility)
  2647. (and (org-invisible-p2)
  2648. (not (progn (forward-line) (org-invisible-p2)))
  2649. (progn (forward-line) (org-invisible-p2))))))
  2650. ;; Hide custom properties with an empty value.
  2651. (should
  2652. (let ((org-custom-properties '("FOO")))
  2653. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2654. (org-toggle-custom-properties-visibility)
  2655. (org-invisible-p2))))
  2656. ;; Do not hide fake properties.
  2657. (should-not
  2658. (let ((org-custom-properties '("FOO")))
  2659. (org-test-with-temp-text ":FOO: val\n"
  2660. (org-toggle-custom-properties-visibility)
  2661. (org-invisible-p2))))
  2662. (should-not
  2663. (let ((org-custom-properties '("A")))
  2664. (org-test-with-temp-text
  2665. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2666. (org-toggle-custom-properties-visibility)
  2667. (org-invisible-p2)))))
  2668. ;;; Mark Region
  2669. (ert-deftest test-org/mark-subtree ()
  2670. "Test `org-mark-subtree' specifications."
  2671. ;; Error when point is before first headline.
  2672. (should-error
  2673. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2674. (progn (transient-mark-mode 1)
  2675. (org-mark-subtree))))
  2676. ;; Without argument, mark current subtree.
  2677. (should
  2678. (equal
  2679. '(12 32)
  2680. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2681. (progn (transient-mark-mode 1)
  2682. (forward-line 2)
  2683. (org-mark-subtree)
  2684. (list (region-beginning) (region-end))))))
  2685. ;; With an argument, move ARG up.
  2686. (should
  2687. (equal
  2688. '(1 32)
  2689. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2690. (progn (transient-mark-mode 1)
  2691. (forward-line 2)
  2692. (org-mark-subtree 1)
  2693. (list (region-beginning) (region-end))))))
  2694. ;; Do not get fooled by inlinetasks.
  2695. (when (featurep 'org-inlinetask)
  2696. (should
  2697. (= 1
  2698. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2699. (progn (transient-mark-mode 1)
  2700. (forward-line 1)
  2701. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2702. (region-beginning)))))))
  2703. ;;; Miscellaneous
  2704. (ert-deftest test-org/sort-entries ()
  2705. "Test `org-sort-entries'."
  2706. ;; Sort alphabetically.
  2707. (should
  2708. (equal "\n* abc\n* def\n* xyz\n"
  2709. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2710. (org-sort-entries nil ?a)
  2711. (buffer-string))))
  2712. (should
  2713. (equal "\n* xyz\n* def\n* abc\n"
  2714. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2715. (org-sort-entries nil ?A)
  2716. (buffer-string))))
  2717. (should
  2718. (equal "\n* \n* klm\n* xyz\n"
  2719. (org-test-with-temp-text "\n* xyz\n* \n* klm\n"
  2720. (org-sort-entries nil ?a)
  2721. (buffer-string))))
  2722. ;; Sort numerically.
  2723. (should
  2724. (equal "\n* 1\n* 2\n* 10\n"
  2725. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2726. (org-sort-entries nil ?n)
  2727. (buffer-string))))
  2728. (should
  2729. (equal "\n* 10\n* 2\n* 1\n"
  2730. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2731. (org-sort-entries nil ?N)
  2732. (buffer-string))))
  2733. (should
  2734. (equal "\n* \n* 1\n* 2\n"
  2735. (org-test-with-temp-text "\n* 1\n* \n* 2\n"
  2736. (org-sort-entries nil ?n)
  2737. (buffer-string))))
  2738. ;; Sort by custom function.
  2739. (should
  2740. (equal "\n* b\n* aa\n* ccc\n"
  2741. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2742. (org-sort-entries nil ?f
  2743. (lambda ()
  2744. (length (buffer-substring (point-at-bol)
  2745. (point-at-eol))))
  2746. #'<)
  2747. (buffer-string))))
  2748. (should
  2749. (equal "\n* ccc\n* aa\n* b\n"
  2750. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2751. (org-sort-entries nil ?F
  2752. (lambda ()
  2753. (length (buffer-substring (point-at-bol)
  2754. (point-at-eol))))
  2755. #'<)
  2756. (buffer-string))))
  2757. ;; Sort by TODO keyword.
  2758. (should
  2759. (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
  2760. (org-test-with-temp-text
  2761. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2762. (org-sort-entries nil ?o)
  2763. (buffer-string))))
  2764. (should
  2765. (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
  2766. (org-test-with-temp-text
  2767. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2768. (org-sort-entries nil ?O)
  2769. (buffer-string))))
  2770. ;; Sort by priority.
  2771. (should
  2772. (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
  2773. (org-test-with-temp-text
  2774. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2775. (org-sort-entries nil ?p)
  2776. (buffer-string))))
  2777. (should
  2778. (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
  2779. (org-test-with-temp-text
  2780. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2781. (org-sort-entries nil ?P)
  2782. (buffer-string))))
  2783. ;; Sort by creation time.
  2784. (should
  2785. (equal "
  2786. * h3
  2787. [2017-05-08 Mon]
  2788. * h2
  2789. [2017-05-09 Tue]
  2790. * h1
  2791. [2018-05-09 Wed]
  2792. "
  2793. (org-test-with-temp-text
  2794. "
  2795. * h1
  2796. [2018-05-09 Wed]
  2797. * h2
  2798. [2017-05-09 Tue]
  2799. * h3
  2800. [2017-05-08 Mon]
  2801. "
  2802. (org-sort-entries nil ?c)
  2803. (buffer-string))))
  2804. ;; Sort by scheduled date.
  2805. (should
  2806. (equal "
  2807. * TODO h4
  2808. SCHEDULED: <2017-05-06 Sat>
  2809. * TODO h3
  2810. SCHEDULED: <2017-05-08 Mon>
  2811. * TODO h2
  2812. DEADLINE: <2017-05-09 Tue>
  2813. * TODO h1
  2814. DEADLINE: <2017-05-07 Sun>
  2815. "
  2816. (org-test-with-temp-text
  2817. "
  2818. * TODO h2
  2819. DEADLINE: <2017-05-09 Tue>
  2820. * TODO h1
  2821. DEADLINE: <2017-05-07 Sun>
  2822. * TODO h3
  2823. SCHEDULED: <2017-05-08 Mon>
  2824. * TODO h4
  2825. SCHEDULED: <2017-05-06 Sat>
  2826. "
  2827. (org-sort-entries nil ?s)
  2828. (buffer-string))))
  2829. ;; Sort by deadline date.
  2830. (should
  2831. (equal "
  2832. * TODO h1
  2833. DEADLINE: <2017-05-07 Sun>
  2834. * TODO h2
  2835. DEADLINE: <2017-05-09 Tue>
  2836. * TODO h3
  2837. SCHEDULED: <2017-05-08 Mon>
  2838. * TODO h4
  2839. SCHEDULED: <2017-05-06 Sat>
  2840. "
  2841. (org-test-with-temp-text
  2842. "
  2843. * TODO h2
  2844. DEADLINE: <2017-05-09 Tue>
  2845. * TODO h1
  2846. DEADLINE: <2017-05-07 Sun>
  2847. * TODO h3
  2848. SCHEDULED: <2017-05-08 Mon>
  2849. * TODO h4
  2850. SCHEDULED: <2017-05-06 Sat>
  2851. "
  2852. (org-sort-entries nil ?d)
  2853. (buffer-string))))
  2854. ;; Sort by any date/time
  2855. (should
  2856. (equal "
  2857. * TODO h4
  2858. SCHEDULED: <2017-05-06 Sat>
  2859. * TODO h1
  2860. DEADLINE: <2017-05-07 Sun>
  2861. * TODO h3
  2862. SCHEDULED: <2017-05-08 Mon>
  2863. * TODO h2
  2864. DEADLINE: <2017-05-09 Tue>
  2865. "
  2866. (org-test-with-temp-text
  2867. "
  2868. * TODO h2
  2869. DEADLINE: <2017-05-09 Tue>
  2870. * TODO h1
  2871. DEADLINE: <2017-05-07 Sun>
  2872. * TODO h3
  2873. SCHEDULED: <2017-05-08 Mon>
  2874. * TODO h4
  2875. SCHEDULED: <2017-05-06 Sat>
  2876. "
  2877. (org-sort-entries nil ?t)
  2878. (buffer-string))))
  2879. ;; Sort by clocking time.
  2880. (should
  2881. (equal "
  2882. * clocked h2
  2883. :LOGBOOK:
  2884. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2885. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2886. :END:
  2887. * clocked h1
  2888. :LOGBOOK:
  2889. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2890. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2891. :END:
  2892. "
  2893. (org-test-with-temp-text
  2894. "
  2895. * clocked h1
  2896. :LOGBOOK:
  2897. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2898. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2899. :END:
  2900. * clocked h2
  2901. :LOGBOOK:
  2902. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2903. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2904. :END:
  2905. "
  2906. (org-sort-entries nil ?k)
  2907. (buffer-string))))
  2908. ;; Preserve file local variables when sorting.
  2909. (should
  2910. (equal "\n* A\n* B\n# Local Variables:\n# foo: t\n# End:\n"
  2911. (org-test-with-temp-text
  2912. "\n* B\n* A\n# Local Variables:\n# foo: t\n# End:"
  2913. (org-sort-entries nil ?a)
  2914. (buffer-string)))))
  2915. (ert-deftest test-org/string-collate-greaterp ()
  2916. "Test `org-string-collate-greaterp' specifications."
  2917. (should (org-string-collate-greaterp "def" "abc"))
  2918. (should-not (org-string-collate-greaterp "abc" "def")))
  2919. (ert-deftest test-org/file-contents ()
  2920. "Test `org-file-contents' specifications."
  2921. ;; Open files.
  2922. (should
  2923. (string= "#+BIND: variable value
  2924. #+DESCRIPTION: l2
  2925. #+LANGUAGE: en
  2926. #+SELECT_TAGS: b
  2927. #+TITLE: b
  2928. #+PROPERTY: a 1
  2929. " (org-file-contents (expand-file-name "setupfile3.org"
  2930. (concat org-test-dir "examples/")))))
  2931. ;; Throw error when trying to access an invalid file.
  2932. (should-error (org-file-contents "this-file-must-not-exist"))
  2933. ;; Try to access an invalid file, but do not throw an error.
  2934. (should
  2935. (progn (org-file-contents "this-file-must-not-exist" :noerror) t))
  2936. ;; Open URL.
  2937. (should
  2938. (string= "foo"
  2939. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2940. (unwind-protect
  2941. ;; Simulate successful retrieval of a URL.
  2942. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2943. (lambda (&rest_)
  2944. (with-current-buffer buffer
  2945. (insert "HTTP/1.1 200 OK\n\nfoo"))
  2946. buffer)))
  2947. (org-file-contents "http://some-valid-url"))
  2948. (kill-buffer buffer)))))
  2949. ;; Throw error when trying to access an invalid URL.
  2950. (should-error
  2951. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2952. (unwind-protect
  2953. ;; Simulate unsuccessful retrieval of a URL.
  2954. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2955. (lambda (&rest_)
  2956. (with-current-buffer buffer
  2957. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2958. buffer)))
  2959. (org-file-contents "http://this-url-must-not-exist"))
  2960. (kill-buffer buffer))))
  2961. ;; Try to access an invalid URL, but do not throw an error.
  2962. (should-error
  2963. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2964. (unwind-protect
  2965. ;; Simulate unsuccessful retrieval of a URL.
  2966. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2967. (lambda (&rest_)
  2968. (with-current-buffer buffer
  2969. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2970. buffer)))
  2971. (org-file-contents "http://this-url-must-not-exist"))
  2972. (kill-buffer buffer))))
  2973. (should
  2974. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2975. (unwind-protect
  2976. ;; Simulate unsuccessful retrieval of a URL.
  2977. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2978. (lambda (&rest_)
  2979. (with-current-buffer buffer
  2980. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2981. buffer)))
  2982. (org-file-contents "http://this-url-must-not-exist" :noerror))
  2983. (kill-buffer buffer))
  2984. t)))
  2985. ;;; Navigation
  2986. (ert-deftest test-org/forward-heading-same-level ()
  2987. "Test `org-forward-heading-same-level' specifications."
  2988. ;; Test navigation at top level, forward and backward.
  2989. (should
  2990. (equal "* H2"
  2991. (org-test-with-temp-text "* H1\n* H2"
  2992. (org-forward-heading-same-level 1)
  2993. (buffer-substring-no-properties (point) (line-end-position)))))
  2994. (should
  2995. (equal "* H1"
  2996. (org-test-with-temp-text "* H1\n<point>* H2"
  2997. (org-forward-heading-same-level -1)
  2998. (buffer-substring-no-properties (point) (line-end-position)))))
  2999. ;; Test navigation in a sub-tree, forward and backward.
  3000. (should
  3001. (equal "* H2"
  3002. (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
  3003. (org-forward-heading-same-level 1)
  3004. (buffer-substring-no-properties (point) (line-end-position)))))
  3005. (should
  3006. (equal "* H1"
  3007. (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
  3008. (org-forward-heading-same-level -1)
  3009. (buffer-substring-no-properties (point) (line-end-position)))))
  3010. ;; Stop at first or last sub-heading.
  3011. (should-not
  3012. (equal "* H2"
  3013. (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
  3014. (org-forward-heading-same-level 1)
  3015. (buffer-substring-no-properties (point) (line-end-position)))))
  3016. (should-not
  3017. (equal "* H2"
  3018. (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
  3019. (org-forward-heading-same-level -1)
  3020. (buffer-substring-no-properties (point) (line-end-position)))))
  3021. ;; Allow multiple moves.
  3022. (should
  3023. (equal "* H3"
  3024. (org-test-with-temp-text "* H1\n* H2\n* H3"
  3025. (org-forward-heading-same-level 2)
  3026. (buffer-substring-no-properties (point) (line-end-position)))))
  3027. (should
  3028. (equal "* H1"
  3029. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3030. (org-forward-heading-same-level -2)
  3031. (buffer-substring-no-properties (point) (line-end-position)))))
  3032. ;; Ignore spurious moves when first (or last) sibling is reached.
  3033. (should
  3034. (equal "** H3"
  3035. (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
  3036. (org-forward-heading-same-level 100)
  3037. (buffer-substring-no-properties (point) (line-end-position)))))
  3038. (should
  3039. (equal "** H1"
  3040. (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
  3041. (org-forward-heading-same-level -100)
  3042. (buffer-substring-no-properties (point) (line-end-position))))))
  3043. (ert-deftest test-org/end-of-meta-data ()
  3044. "Test `org-end-of-meta-data' specifications."
  3045. ;; Skip planning line.
  3046. (should
  3047. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  3048. (org-end-of-meta-data)
  3049. (eobp)))
  3050. ;; Skip properties drawer.
  3051. (should
  3052. (org-test-with-temp-text
  3053. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  3054. (org-end-of-meta-data)
  3055. (eobp)))
  3056. ;; Skip both.
  3057. (should
  3058. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  3059. (org-end-of-meta-data)
  3060. (eobp)))
  3061. ;; Nothing to skip, go to first line.
  3062. (should
  3063. (org-test-with-temp-text "* Headline\nContents"
  3064. (org-end-of-meta-data)
  3065. (looking-at "Contents")))
  3066. ;; With option argument, skip empty lines, regular drawers and
  3067. ;; clocking lines.
  3068. (should
  3069. (org-test-with-temp-text "* Headline\n\nContents"
  3070. (org-end-of-meta-data t)
  3071. (looking-at "Contents")))
  3072. (should
  3073. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  3074. (org-end-of-meta-data t)
  3075. (looking-at "Contents")))
  3076. (should
  3077. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  3078. (org-end-of-meta-data t)
  3079. (looking-at "Contents")))
  3080. ;; Special case: do not skip incomplete drawers.
  3081. (should
  3082. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  3083. (org-end-of-meta-data t)
  3084. (looking-at ":LOGBOOK:")))
  3085. ;; Special case: Be careful about consecutive headlines.
  3086. (should-not
  3087. (org-test-with-temp-text "* H1\n*H2\nContents"
  3088. (org-end-of-meta-data t)
  3089. (looking-at "Contents"))))
  3090. (ert-deftest test-org/beginning-of-line ()
  3091. "Test `org-beginning-of-line' specifications."
  3092. ;; Move to beginning of line. If current line in invisible, move to
  3093. ;; beginning of visible line instead.
  3094. (should
  3095. (org-test-with-temp-text "Some text\nSome other text<point>"
  3096. (org-beginning-of-line)
  3097. (bolp)))
  3098. (should
  3099. (org-test-with-temp-text "* H1\n** H2<point>"
  3100. (org-overview)
  3101. (org-beginning-of-line)
  3102. (= (line-beginning-position) 1)))
  3103. ;; With `visual-line-mode' active, move to beginning of visual line.
  3104. (should-not
  3105. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3106. (visual-line-mode)
  3107. (dotimes (i 1000) (insert "very "))
  3108. (org-beginning-of-line)
  3109. (bolp)))
  3110. ;; In a wide headline, with `visual-line-mode', prefer going to the
  3111. ;; beginning of a visual line than to the logical beginning of line,
  3112. ;; even if special movement is active.
  3113. (should-not
  3114. (org-test-with-temp-text "* A <point>long headline"
  3115. (visual-line-mode)
  3116. (dotimes (i 1000) (insert "very "))
  3117. (goto-char (point-max))
  3118. (org-beginning-of-line)
  3119. (bobp)))
  3120. (should-not
  3121. (org-test-with-temp-text "* A <point>long headline"
  3122. (visual-line-mode)
  3123. (dotimes (i 1000) (insert "very "))
  3124. (goto-char (point-max))
  3125. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  3126. (bobp)))
  3127. ;; At an headline with special movement, first move at beginning of
  3128. ;; title, then at the beginning of line, rinse, repeat.
  3129. (should
  3130. (org-test-with-temp-text "* TODO Headline<point>"
  3131. (let ((org-special-ctrl-a/e t))
  3132. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  3133. (progn (org-beginning-of-line) (bolp))
  3134. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3135. (should
  3136. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3137. (let ((org-special-ctrl-a/e t))
  3138. (org-beginning-of-line)
  3139. (looking-at "Headline"))))
  3140. (should
  3141. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3142. (let ((org-special-ctrl-a/e '(t . nil)))
  3143. (org-beginning-of-line)
  3144. (looking-at "Headline"))))
  3145. (should-not
  3146. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3147. (let ((org-special-ctrl-a/e '(nil . nil)))
  3148. (org-beginning-of-line)
  3149. (looking-at "Headline"))))
  3150. ;; At an headline with reversed movement, first move to beginning of
  3151. ;; line, then to the beginning of title.
  3152. (should
  3153. (org-test-with-temp-text "* TODO Headline<point>"
  3154. (let ((org-special-ctrl-a/e 'reversed)
  3155. (this-command last-command))
  3156. (and (progn (org-beginning-of-line) (bolp))
  3157. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3158. (should
  3159. (org-test-with-temp-text "* TODO Headline<point>"
  3160. (let ((org-special-ctrl-a/e '(reversed . nil))
  3161. (this-command last-command))
  3162. (and (progn (org-beginning-of-line) (bolp))
  3163. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3164. (should-not
  3165. (org-test-with-temp-text "* TODO Headline<point>"
  3166. (let ((org-special-ctrl-a/e '(t . nil))
  3167. (this-command last-command))
  3168. (and (progn (org-beginning-of-line) (bolp))
  3169. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3170. ;; At an item with special movement, first move after to beginning
  3171. ;; of title, then to the beginning of line, rinse, repeat.
  3172. (should
  3173. (org-test-with-temp-text "- [ ] Item<point>"
  3174. (let ((org-special-ctrl-a/e t))
  3175. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  3176. (progn (org-beginning-of-line) (bolp))
  3177. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3178. ;; At an item with reversed movement, first move to beginning of
  3179. ;; line, then to the beginning of title.
  3180. (should
  3181. (org-test-with-temp-text "- [X] Item<point>"
  3182. (let ((org-special-ctrl-a/e 'reversed)
  3183. (this-command last-command))
  3184. (and (progn (org-beginning-of-line) (bolp))
  3185. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3186. ;; Leave point before invisible characters at column 0.
  3187. (should
  3188. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3189. (let ((org-special-ctrl-a/e nil))
  3190. (org-beginning-of-line)
  3191. (bolp))))
  3192. (should
  3193. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3194. (let ((org-special-ctrl-a/e t))
  3195. (org-beginning-of-line)
  3196. (bolp))))
  3197. (should
  3198. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  3199. (visual-line-mode)
  3200. (org-beginning-of-line)
  3201. (bolp)))
  3202. ;; Special case: Do not error when the buffer contains only a single
  3203. ;; asterisk.
  3204. (should
  3205. (org-test-with-temp-text "*<point>"
  3206. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  3207. (should
  3208. (org-test-with-temp-text "*<point>"
  3209. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  3210. (ert-deftest test-org/end-of-line ()
  3211. "Test `org-end-of-line' specifications."
  3212. ;; Standard test.
  3213. (should
  3214. (org-test-with-temp-text "Some text\nSome other text"
  3215. (org-end-of-line)
  3216. (eolp)))
  3217. ;; With `visual-line-mode' active, move to end of visible line.
  3218. ;; However, never go past ellipsis.
  3219. (should-not
  3220. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3221. (visual-line-mode)
  3222. (dotimes (i 1000) (insert "very "))
  3223. (goto-char (point-min))
  3224. (org-end-of-line)
  3225. (eolp)))
  3226. (should-not
  3227. (org-test-with-temp-text "* A short headline\nSome contents"
  3228. (visual-line-mode)
  3229. (org-overview)
  3230. (org-end-of-line)
  3231. (eobp)))
  3232. ;; In a wide headline, with `visual-line-mode', prefer going to end
  3233. ;; of visible line if tags, or end of line, are farther.
  3234. (should-not
  3235. (org-test-with-temp-text "* A <point>long headline"
  3236. (visual-line-mode)
  3237. (dotimes (i 1000) (insert "very "))
  3238. (goto-char (point-min))
  3239. (org-end-of-line)
  3240. (eolp)))
  3241. (should-not
  3242. (org-test-with-temp-text "* A <point>long headline :tag:"
  3243. (visual-line-mode)
  3244. (dotimes (i 1000) (insert "very "))
  3245. (goto-char (point-min))
  3246. (org-end-of-line)
  3247. (eolp)))
  3248. ;; At an headline without special movement, go to end of line.
  3249. ;; However, never go past ellipsis.
  3250. (should
  3251. (org-test-with-temp-text "* Headline2b :tag:\n"
  3252. (let ((org-special-ctrl-a/e nil))
  3253. (and (progn (org-end-of-line) (eolp))
  3254. (progn (org-end-of-line) (eolp))))))
  3255. (should
  3256. (org-test-with-temp-text "* Headline2b :tag:\n"
  3257. (let ((org-special-ctrl-a/e '(t . nil)))
  3258. (and (progn (org-end-of-line) (eolp))
  3259. (progn (org-end-of-line) (eolp))))))
  3260. (should
  3261. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3262. (org-overview)
  3263. (let ((org-special-ctrl-a/e nil))
  3264. (org-end-of-line)
  3265. (= 1 (line-beginning-position)))))
  3266. ;; At an headline with special movement, first move before tags,
  3267. ;; then at the end of line, rinse, repeat. However, never go past
  3268. ;; ellipsis.
  3269. (should
  3270. (org-test-with-temp-text "* Headline1 :tag:\n"
  3271. (let ((org-special-ctrl-a/e t))
  3272. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3273. (progn (org-end-of-line) (eolp))
  3274. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3275. (should
  3276. (org-test-with-temp-text "* Headline1 :tag:\n"
  3277. (let ((org-special-ctrl-a/e '(nil . t)))
  3278. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3279. (progn (org-end-of-line) (eolp))
  3280. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3281. (should-not
  3282. (org-test-with-temp-text "* Headline1 :tag:\n"
  3283. (let ((org-special-ctrl-a/e '(nil . nil)))
  3284. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3285. (progn (org-end-of-line) (eolp))
  3286. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3287. (should
  3288. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3289. (org-overview)
  3290. (let ((org-special-ctrl-a/e t))
  3291. (org-end-of-line)
  3292. (org-end-of-line)
  3293. (= 1 (line-beginning-position)))))
  3294. ;; At an headline, with reversed movement, first go to end of line,
  3295. ;; then before tags. However, never go past ellipsis.
  3296. (should
  3297. (org-test-with-temp-text "* Headline3 :tag:\n"
  3298. (let ((org-special-ctrl-a/e 'reversed)
  3299. (this-command last-command))
  3300. (and (progn (org-end-of-line) (eolp))
  3301. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3302. (should
  3303. (org-test-with-temp-text "* Headline3 :tag:\n"
  3304. (let ((org-special-ctrl-a/e '(nil . reversed))
  3305. (this-command last-command))
  3306. (and (progn (org-end-of-line) (eolp))
  3307. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3308. (should-not
  3309. (org-test-with-temp-text "* Headline3 :tag:\n"
  3310. (let ((org-special-ctrl-a/e '(nil . t))
  3311. (this-command last-command))
  3312. (and (progn (org-end-of-line) (eolp))
  3313. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3314. (should
  3315. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3316. (org-overview)
  3317. (let ((org-special-ctrl-a/e 'reversed))
  3318. (org-end-of-line)
  3319. (= 1 (line-beginning-position)))))
  3320. ;; At a block without hidden contents.
  3321. (should
  3322. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3323. (progn (org-end-of-line) (eolp))))
  3324. ;; At a block with hidden contents.
  3325. (should-not
  3326. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3327. (let ((org-special-ctrl-a/e t))
  3328. (org-hide-block-toggle)
  3329. (org-end-of-line)
  3330. (eobp))))
  3331. ;; Get past invisible characters at the end of line.
  3332. (should
  3333. (org-test-with-temp-text "[[https://orgmode.org]]"
  3334. (org-end-of-line)
  3335. (eolp))))
  3336. (ert-deftest test-org/open-line ()
  3337. "Test `org-open-line' specifications."
  3338. ;; Call `open-line' outside of tables.
  3339. (should
  3340. (equal "\nText"
  3341. (org-test-with-temp-text "Text"
  3342. (org-open-line 1)
  3343. (buffer-string))))
  3344. ;; At a table, create a row above.
  3345. (should
  3346. (equal "\n| |\n| a |"
  3347. (org-test-with-temp-text "\n<point>| a |"
  3348. (org-open-line 1)
  3349. (buffer-string))))
  3350. ;; At the very first character of the buffer, also call `open-line'.
  3351. (should
  3352. (equal "\n| a |"
  3353. (org-test-with-temp-text "| a |"
  3354. (org-open-line 1)
  3355. (buffer-string))))
  3356. ;; Narrowing does not count.
  3357. (should
  3358. (equal "Text\n| |\n| a |"
  3359. (org-test-with-temp-text "Text\n<point>| a |"
  3360. (narrow-to-region (point) (point-max))
  3361. (org-open-line 1)
  3362. (widen)
  3363. (buffer-string)))))
  3364. (ert-deftest test-org/forward-sentence ()
  3365. "Test `org-forward-sentence' specifications."
  3366. ;; At the end of a table cell, move to the end of the next one.
  3367. (should
  3368. (org-test-with-temp-text "| a<point> | b |"
  3369. (org-forward-sentence)
  3370. (looking-at " |$")))
  3371. ;; Elsewhere in a cell, move to its end.
  3372. (should
  3373. (org-test-with-temp-text "| a<point>c | b |"
  3374. (org-forward-sentence)
  3375. (looking-at " | b |$")))
  3376. ;; Otherwise, simply call `forward-sentence'.
  3377. (should
  3378. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3379. (org-forward-sentence)
  3380. (looking-at " Sentence 2.")))
  3381. (should
  3382. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3383. (org-forward-sentence)
  3384. (org-forward-sentence)
  3385. (eobp)))
  3386. ;; At the end of an element, jump to the next one, without stopping
  3387. ;; on blank lines in-between.
  3388. (should
  3389. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  3390. (org-forward-sentence)
  3391. (eobp)))
  3392. ;; Headlines are considered to be sentences by themselves, even if
  3393. ;; they do not end with a full stop.
  3394. (should
  3395. (equal
  3396. "* Headline"
  3397. (org-test-with-temp-text "* <point>Headline\nSentence."
  3398. (org-forward-sentence)
  3399. (buffer-substring-no-properties (line-beginning-position) (point)))))
  3400. (should
  3401. (org-test-with-temp-text "* Headline<point>\nSentence."
  3402. (org-forward-sentence)
  3403. (eobp)))
  3404. (should
  3405. (org-test-with-temp-text "Sentence.<point>\n\n* Headline\n\nSentence 2."
  3406. (org-forward-sentence)
  3407. (and (org-at-heading-p) (eolp)))))
  3408. (ert-deftest test-org/backward-sentence ()
  3409. "Test `org-backward-sentence' specifications."
  3410. ;; At the beginning of a table cell, move to the beginning of the
  3411. ;; previous one.
  3412. (should
  3413. (org-test-with-temp-text "| a | <point>b |"
  3414. (org-backward-sentence)
  3415. (looking-at "a | b |$")))
  3416. ;; Elsewhere in a cell, move to its beginning.
  3417. (should
  3418. (org-test-with-temp-text "| a | b<point>c |"
  3419. (org-backward-sentence)
  3420. (looking-at "bc |$")))
  3421. ;; Otherwise, simply call `backward-sentence'.
  3422. (should
  3423. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3424. (org-backward-sentence)
  3425. (looking-at "Sentence 2.")))
  3426. (should
  3427. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3428. (org-backward-sentence)
  3429. (org-backward-sentence)
  3430. (bobp)))
  3431. ;; Make sure to hit the beginning of a sentence on the same line as
  3432. ;; an item.
  3433. (should
  3434. (org-test-with-temp-text "- Line 1\n line <point>2."
  3435. (org-backward-sentence)
  3436. (looking-at "Line 1"))))
  3437. (ert-deftest test-org/forward-paragraph ()
  3438. "Test `org-forward-paragraph' specifications."
  3439. ;; At end of buffer, do not return an error.
  3440. (should
  3441. (org-test-with-temp-text "Paragraph"
  3442. (goto-char (point-max))
  3443. (org-forward-paragraph)
  3444. t))
  3445. ;; Standard test.
  3446. (should
  3447. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3448. (org-forward-paragraph)
  3449. (looking-at "P2")))
  3450. ;; Ignore depth.
  3451. (should
  3452. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  3453. (org-forward-paragraph)
  3454. (looking-at "P1")))
  3455. ;; Do not enter elements with invisible contents.
  3456. (should
  3457. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  3458. (org-hide-block-toggle)
  3459. (org-forward-paragraph)
  3460. (looking-at "P3")))
  3461. ;; On an affiliated keyword, jump to the beginning of the element.
  3462. (should
  3463. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  3464. (org-forward-paragraph)
  3465. (looking-at "Para")))
  3466. ;; On an item or a footnote definition, move to the second element
  3467. ;; inside, if any.
  3468. (should
  3469. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  3470. (org-forward-paragraph)
  3471. (looking-at " Paragraph")))
  3472. (should
  3473. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  3474. (org-forward-paragraph)
  3475. (looking-at "Paragraph")))
  3476. ;; On an item, or a footnote definition, when the first line is
  3477. ;; empty, move to the first item.
  3478. (should
  3479. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  3480. (org-forward-paragraph)
  3481. (looking-at " Paragraph")))
  3482. (should
  3483. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  3484. (org-forward-paragraph)
  3485. (looking-at "Paragraph")))
  3486. ;; On a table (resp. a property drawer) do not move through table
  3487. ;; rows (resp. node properties).
  3488. (should
  3489. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  3490. (org-forward-paragraph)
  3491. (looking-at "Paragraph")))
  3492. (should
  3493. (org-test-with-temp-text
  3494. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  3495. (org-forward-paragraph)
  3496. (looking-at "Paragraph")))
  3497. ;; On a verse or source block, stop after blank lines.
  3498. (should
  3499. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  3500. (org-forward-paragraph)
  3501. (looking-at "L2")))
  3502. (should
  3503. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  3504. (org-forward-paragraph)
  3505. (looking-at "L2"))))
  3506. (ert-deftest test-org/backward-paragraph ()
  3507. "Test `org-backward-paragraph' specifications."
  3508. ;; Do not error at beginning of buffer.
  3509. (should
  3510. (org-test-with-temp-text "Paragraph"
  3511. (org-backward-paragraph)
  3512. t))
  3513. ;; Regular test.
  3514. (should
  3515. (org-test-with-temp-text "P1\n\nP2\n\nP3<point>"
  3516. (org-backward-paragraph)
  3517. (looking-at "P3")))
  3518. (should
  3519. (org-test-with-temp-text "P1\n\nP2\n\n<point>P3"
  3520. (org-backward-paragraph)
  3521. (looking-at-p "P2")))
  3522. ;; Ignore depth.
  3523. (should
  3524. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\n<point>P3"
  3525. (org-backward-paragraph)
  3526. (looking-at-p "P2")))
  3527. ;; Ignore invisible elements.
  3528. (should
  3529. (org-test-with-temp-text "* H1\n P1\n* H2"
  3530. (org-cycle)
  3531. (goto-char (point-max))
  3532. (beginning-of-line)
  3533. (org-backward-paragraph)
  3534. (bobp)))
  3535. ;; On an affiliated keyword, jump to the first one.
  3536. (should
  3537. (org-test-with-temp-text
  3538. "P1\n#+name: n\n#+caption: c1\n#+caption: <point>c2\nP2"
  3539. (org-backward-paragraph)
  3540. (looking-at-p "#\\+name")))
  3541. ;; On the second element in an item or a footnote definition, jump
  3542. ;; to item or the definition.
  3543. (should
  3544. (org-test-with-temp-text "- line1\n\n<point> line2"
  3545. (org-backward-paragraph)
  3546. (looking-at-p "- line1")))
  3547. (should
  3548. (org-test-with-temp-text "[fn:1] line1\n\n<point> line2"
  3549. (org-backward-paragraph)
  3550. (looking-at-p "\\[fn:1\\] line1")))
  3551. ;; On a table (resp. a property drawer), ignore table rows
  3552. ;; (resp. node properties).
  3553. (should
  3554. (org-test-with-temp-text "| a | b |\n| c | d |\n<point>P1"
  3555. (org-backward-paragraph)
  3556. (bobp)))
  3557. (should
  3558. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  3559. (org-backward-paragraph)
  3560. (looking-at-p ":PROPERTIES:")))
  3561. ;; On a comment, example, src and verse blocks, stop before blank
  3562. ;; lines.
  3563. (should
  3564. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\n<point>L3\n#+END_VERSE"
  3565. (org-backward-paragraph)
  3566. (looking-at-p "L2")))
  3567. (should
  3568. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\n<point>L3#+END_SRC"
  3569. (org-backward-paragraph)
  3570. (looking-at-p "L2")))
  3571. ;; In comment, example, export, src and verse blocks, stop below
  3572. ;; opening line when called from within the block.
  3573. (should
  3574. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\nL2<point>\n#+END_VERSE"
  3575. (org-backward-paragraph)
  3576. (looking-at-p "L1")))
  3577. (should
  3578. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nL1\nL2<point>\n#+END_EXAMPLE"
  3579. (org-backward-paragraph)
  3580. (looking-at-p "L1")))
  3581. ;; When called from the opening line itself, however, move to
  3582. ;; beginning of block.
  3583. (should
  3584. (org-test-with-temp-text "#+BEGIN_<point>EXAMPLE\nL1\n#+END_EXAMPLE"
  3585. (org-backward-paragraph)
  3586. (bobp)))
  3587. ;; Pathological case: on an empty heading, move to its beginning.
  3588. (should
  3589. (org-test-with-temp-text "* <point>H"
  3590. (org-backward-paragraph)
  3591. (bobp))))
  3592. (ert-deftest test-org/forward-element ()
  3593. "Test `org-forward-element' specifications."
  3594. ;; 1. At EOB: should error.
  3595. (org-test-with-temp-text "Some text\n"
  3596. (goto-char (point-max))
  3597. (should-error (org-forward-element)))
  3598. ;; 2. Standard move: expected to ignore blank lines.
  3599. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  3600. (org-forward-element)
  3601. (should (looking-at (regexp-quote "Second paragraph."))))
  3602. ;; 3. Headline tests.
  3603. (org-test-with-temp-text "
  3604. * Head 1
  3605. ** Head 1.1
  3606. *** Head 1.1.1
  3607. ** Head 1.2"
  3608. ;; 3.1. At an headline beginning: move to next headline at the
  3609. ;; same level.
  3610. (goto-line 3)
  3611. (org-forward-element)
  3612. (should (looking-at (regexp-quote "** Head 1.2")))
  3613. ;; 3.2. At an headline beginning: move to parent headline if no
  3614. ;; headline at the same level.
  3615. (goto-line 3)
  3616. (org-forward-element)
  3617. (should (looking-at (regexp-quote "** Head 1.2"))))
  3618. ;; 4. Greater element tests.
  3619. (org-test-with-temp-text
  3620. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  3621. ;; 4.1. At a greater element: expected to skip contents.
  3622. (org-forward-element)
  3623. (should (looking-at (regexp-quote "Outside.")))
  3624. ;; 4.2. At the end of greater element contents: expected to skip
  3625. ;; to the end of the greater element.
  3626. (goto-line 2)
  3627. (org-forward-element)
  3628. (should (looking-at (regexp-quote "Outside."))))
  3629. ;; 5. List tests.
  3630. (org-test-with-temp-text "
  3631. - item1
  3632. - sub1
  3633. - sub2
  3634. - sub3
  3635. Inner paragraph.
  3636. - item2
  3637. Outside."
  3638. ;; 5.1. At list top point: expected to move to the element after
  3639. ;; the list.
  3640. (goto-line 2)
  3641. (org-forward-element)
  3642. (should (looking-at (regexp-quote "Outside.")))
  3643. ;; 5.2. Special case: at the first line of a sub-list, but not at
  3644. ;; beginning of line, move to next item.
  3645. (goto-line 2)
  3646. (forward-char)
  3647. (org-forward-element)
  3648. (should (looking-at "- item2"))
  3649. (goto-line 4)
  3650. (forward-char)
  3651. (org-forward-element)
  3652. (should (looking-at " - sub2"))
  3653. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  3654. (goto-line 4)
  3655. (org-forward-element)
  3656. (should (looking-at (regexp-quote " Inner paragraph.")))
  3657. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  3658. (goto-line 8)
  3659. (org-forward-element)
  3660. (should (looking-at (regexp-quote " Inner paragraph.")))
  3661. ;; 5.5. At an item: expected to move to next item, if any.
  3662. (goto-line 6)
  3663. (org-forward-element)
  3664. (should (looking-at " - sub3"))))
  3665. (ert-deftest test-org/backward-element ()
  3666. "Test `org-backward-element' specifications."
  3667. ;; 1. Should error at BOB.
  3668. (org-test-with-temp-text " \nParagraph."
  3669. (should-error (org-backward-element)))
  3670. ;; 2. Should move at BOB when called on the first element in buffer.
  3671. (should
  3672. (org-test-with-temp-text "\n#+TITLE: test"
  3673. (progn (forward-line)
  3674. (org-backward-element)
  3675. (bobp))))
  3676. ;; 3. Not at the beginning of an element: move at its beginning.
  3677. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3678. (goto-line 3)
  3679. (end-of-line)
  3680. (org-backward-element)
  3681. (should (looking-at (regexp-quote "Paragraph2."))))
  3682. ;; 4. Headline tests.
  3683. (org-test-with-temp-text "
  3684. * Head 1
  3685. ** Head 1.1
  3686. *** Head 1.1.1
  3687. ** Head 1.2"
  3688. ;; 4.1. At an headline beginning: move to previous headline at the
  3689. ;; same level.
  3690. (goto-line 5)
  3691. (org-backward-element)
  3692. (should (looking-at (regexp-quote "** Head 1.1")))
  3693. ;; 4.2. At an headline beginning: move to parent headline if no
  3694. ;; headline at the same level.
  3695. (goto-line 3)
  3696. (org-backward-element)
  3697. (should (looking-at (regexp-quote "* Head 1")))
  3698. ;; 4.3. At the first top-level headline: should error.
  3699. (goto-line 2)
  3700. (should-error (org-backward-element)))
  3701. ;; 5. At beginning of first element inside a greater element:
  3702. ;; expected to move to greater element's beginning.
  3703. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  3704. (goto-line 3)
  3705. (org-backward-element)
  3706. (should (looking-at "#\\+BEGIN_CENTER")))
  3707. ;; 6. At the beginning of the first element in a section: should
  3708. ;; move back to headline, if any.
  3709. (should
  3710. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  3711. (progn (goto-char (point-max))
  3712. (beginning-of-line)
  3713. (org-backward-element)
  3714. (org-at-heading-p))))
  3715. ;; 7. List tests.
  3716. (org-test-with-temp-text "
  3717. - item1
  3718. - sub1
  3719. - sub2
  3720. - sub3
  3721. Inner paragraph.
  3722. - item2
  3723. Outside."
  3724. ;; 7.1. At beginning of sub-list: expected to move to the
  3725. ;; paragraph before it.
  3726. (goto-line 4)
  3727. (org-backward-element)
  3728. (should (looking-at "item1"))
  3729. ;; 7.2. At an item in a list: expected to move at previous item.
  3730. (goto-line 8)
  3731. (org-backward-element)
  3732. (should (looking-at " - sub2"))
  3733. (goto-line 12)
  3734. (org-backward-element)
  3735. (should (looking-at "- item1"))
  3736. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3737. ;; beginning.
  3738. (goto-line 10)
  3739. (org-backward-element)
  3740. (should (looking-at " - sub1"))
  3741. (goto-line 15)
  3742. (org-backward-element)
  3743. (should (looking-at "- item1"))
  3744. ;; 7.4. At blank-lines before list end: expected to move to top
  3745. ;; item.
  3746. (goto-line 14)
  3747. (org-backward-element)
  3748. (should (looking-at "- item1"))))
  3749. (ert-deftest test-org/up-element ()
  3750. "Test `org-up-element' specifications."
  3751. ;; 1. At BOB or with no surrounding element: should error.
  3752. (org-test-with-temp-text "Paragraph."
  3753. (should-error (org-up-element)))
  3754. (org-test-with-temp-text "* Head1\n* Head2"
  3755. (goto-line 2)
  3756. (should-error (org-up-element)))
  3757. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3758. (goto-line 3)
  3759. (should-error (org-up-element)))
  3760. ;; 2. At an headline: move to parent headline.
  3761. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3762. (goto-line 3)
  3763. (org-up-element)
  3764. (should (looking-at "\\* Head1")))
  3765. ;; 3. Inside a greater element: move to greater element beginning.
  3766. (org-test-with-temp-text
  3767. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3768. (goto-line 3)
  3769. (org-up-element)
  3770. (should (looking-at "#\\+BEGIN_CENTER")))
  3771. ;; 4. List tests.
  3772. (org-test-with-temp-text "* Top
  3773. - item1
  3774. - sub1
  3775. - sub2
  3776. Paragraph within sub2.
  3777. - item2"
  3778. ;; 4.1. Within an item: move to the item beginning.
  3779. (goto-line 8)
  3780. (org-up-element)
  3781. (should (looking-at " - sub2"))
  3782. ;; 4.2. At an item in a sub-list: move to parent item.
  3783. (goto-line 4)
  3784. (org-up-element)
  3785. (should (looking-at "- item1"))
  3786. ;; 4.3. At an item in top list: move to beginning of whole list.
  3787. (goto-line 10)
  3788. (org-up-element)
  3789. (should (looking-at "- item1"))
  3790. ;; 4.4. Special case. At very top point: should move to parent of
  3791. ;; list.
  3792. (goto-line 2)
  3793. (org-up-element)
  3794. (should (looking-at "\\* Top"))))
  3795. (ert-deftest test-org/down-element ()
  3796. "Test `org-down-element' specifications."
  3797. ;; Error when the element hasn't got a recursive type.
  3798. (org-test-with-temp-text "Paragraph."
  3799. (should-error (org-down-element)))
  3800. ;; Error when the element has no contents
  3801. (org-test-with-temp-text "* Headline"
  3802. (should-error (org-down-element)))
  3803. ;; When at a plain-list, move to first item.
  3804. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3805. (goto-line 2)
  3806. (org-down-element)
  3807. (should (looking-at " - Item 1.1")))
  3808. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3809. (org-down-element)
  3810. (should (looking-at " Item 1")))
  3811. ;; When at a table, move to first row
  3812. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3813. (org-down-element)
  3814. (should (looking-at " a | b |")))
  3815. ;; Otherwise, move inside the greater element.
  3816. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3817. (org-down-element)
  3818. (should (looking-at "Paragraph"))))
  3819. (ert-deftest test-org/drag-element-backward ()
  3820. "Test `org-drag-element-backward' specifications."
  3821. ;; Standard test.
  3822. (should
  3823. (equal
  3824. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3825. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3826. (org-drag-element-backward)
  3827. (buffer-string))))
  3828. (should
  3829. (equal
  3830. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3831. (org-test-with-temp-text
  3832. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3833. (org-drag-element-backward)
  3834. (buffer-string))))
  3835. ;; Preserve blank lines.
  3836. (should
  3837. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3838. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3839. (org-drag-element-backward)
  3840. (buffer-string))))
  3841. ;; Preserve column.
  3842. (should
  3843. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3844. (org-drag-element-backward)
  3845. (looking-at-p "2")))
  3846. ;; Error when trying to move first element of buffer.
  3847. (should-error
  3848. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3849. (org-drag-element-backward))
  3850. :type 'user-error)
  3851. ;; Error when trying to swap nested elements.
  3852. (should-error
  3853. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  3854. (org-drag-element-backward))
  3855. :type 'user-error)
  3856. ;; Error when trying to swap an headline element and a non-headline
  3857. ;; element.
  3858. (should-error
  3859. (org-test-with-temp-text "Test.\n<point>* Head 1"
  3860. (org-drag-element-backward))
  3861. :type 'error)
  3862. ;; Error when called before first element.
  3863. (should-error
  3864. (org-test-with-temp-text "\n<point>"
  3865. (org-drag-element-backward))
  3866. :type 'user-error)
  3867. ;; Preserve visibility of elements and their contents.
  3868. (should
  3869. (equal '((63 . 82) (26 . 48))
  3870. (org-test-with-temp-text "
  3871. #+BEGIN_CENTER
  3872. Text.
  3873. #+END_CENTER
  3874. - item 1
  3875. #+BEGIN_QUOTE
  3876. Text.
  3877. #+END_QUOTE"
  3878. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3879. (search-backward "- item 1")
  3880. (org-drag-element-backward)
  3881. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3882. (overlays-in (point-min) (point-max))))))
  3883. ;; Pathological case: handle call with point in blank lines right
  3884. ;; after a headline.
  3885. (should
  3886. (equal "* H2\n\n* H1\nText\n"
  3887. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  3888. (org-drag-element-backward)
  3889. (buffer-string)))))
  3890. (ert-deftest test-org/drag-element-forward ()
  3891. "Test `org-drag-element-forward' specifications."
  3892. ;; 1. Error when trying to move first element of buffer.
  3893. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3894. (goto-line 3)
  3895. (should-error (org-drag-element-forward)))
  3896. ;; 2. Error when trying to swap nested elements.
  3897. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3898. (forward-line)
  3899. (should-error (org-drag-element-forward)))
  3900. ;; 3. Error when trying to swap a non-headline element and an
  3901. ;; headline.
  3902. (org-test-with-temp-text "Test.\n* Head 1"
  3903. (should-error (org-drag-element-forward)))
  3904. ;; 4. Error when called before first element.
  3905. (should-error
  3906. (org-test-with-temp-text "\n"
  3907. (forward-line)
  3908. (org-drag-element-backward))
  3909. :type 'user-error)
  3910. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3911. ;; between elements.
  3912. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3913. (search-forward "graph")
  3914. (org-drag-element-forward)
  3915. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3916. (should (looking-at " 1")))
  3917. ;; 5. Preserve visibility of elements and their contents.
  3918. (org-test-with-temp-text "
  3919. #+BEGIN_CENTER
  3920. Text.
  3921. #+END_CENTER
  3922. - item 1
  3923. #+BEGIN_QUOTE
  3924. Text.
  3925. #+END_QUOTE"
  3926. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3927. (search-backward "#+BEGIN_CENTER")
  3928. (org-drag-element-forward)
  3929. (should
  3930. (equal
  3931. '((63 . 82) (26 . 48))
  3932. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3933. (overlays-in (point-min) (point-max)))))))
  3934. (ert-deftest test-org/next-block ()
  3935. "Test `org-next-block' specifications."
  3936. ;; Regular test.
  3937. (should
  3938. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3939. (org-next-block 1)
  3940. (looking-at "#\\+BEGIN_CENTER")))
  3941. ;; Ignore case.
  3942. (should
  3943. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3944. (let ((case-fold-search nil))
  3945. (org-next-block 1)
  3946. (looking-at "#\\+begin_center"))))
  3947. ;; Ignore current line.
  3948. (should
  3949. (org-test-with-temp-text
  3950. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3951. (org-next-block 1)
  3952. (looking-at "#\\+BEGIN_CENTER")))
  3953. ;; Throw an error when no block is found.
  3954. (should-error
  3955. (org-test-with-temp-text "Paragraph"
  3956. (org-next-block 1)))
  3957. ;; With an argument, skip many blocks at once.
  3958. (should
  3959. (org-test-with-temp-text
  3960. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3961. (org-next-block 2)
  3962. (looking-at "#\\+BEGIN_QUOTE")))
  3963. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3964. (should
  3965. (org-test-with-temp-text
  3966. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3967. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3968. (looking-at "#\\+BEGIN_QUOTE")))
  3969. ;; Optional argument is also case-insensitive.
  3970. (should
  3971. (org-test-with-temp-text
  3972. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3973. (let ((case-fold-search nil))
  3974. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3975. (looking-at "#\\+begin_quote")))))
  3976. (ert-deftest test-org/insert-structure-template ()
  3977. "Test `org-insert-structure-template'."
  3978. ;; Test in empty buffer.
  3979. (should
  3980. (string= "#+begin_foo\n#+end_foo\n"
  3981. (org-test-with-temp-text ""
  3982. (org-insert-structure-template "foo")
  3983. (buffer-string))))
  3984. ;; Test with multiple lines in buffer.
  3985. (should
  3986. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  3987. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  3988. (transient-mark-mode 1)
  3989. (org-mark-element)
  3990. (org-insert-structure-template "foo")
  3991. (buffer-string))))
  3992. ;; Mark only the current line.
  3993. (should
  3994. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  3995. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  3996. (transient-mark-mode 1)
  3997. (set-mark (point-min))
  3998. (end-of-line)
  3999. (org-insert-structure-template "foo")
  4000. (buffer-string))))
  4001. ;; Middle of paragraph.
  4002. (should
  4003. (string= "p1\n#+begin_foo\np2\n#+end_foo\np3"
  4004. (org-test-with-temp-text "p1\n<point>p2\np3"
  4005. (set-mark (line-beginning-position))
  4006. (end-of-line)
  4007. (activate-mark)
  4008. (org-insert-structure-template "foo")
  4009. (buffer-string))))
  4010. ;; Test with text in buffer, no region, no final newline.
  4011. (should
  4012. (string= "#+begin_foo\nI'm a paragraph.\n#+end_foo\n"
  4013. (org-test-with-temp-text "I'm a paragraph."
  4014. (org-mark-element)
  4015. (org-insert-structure-template "foo")
  4016. (buffer-string))))
  4017. ;; Test with text in buffer and region set.
  4018. (should
  4019. (string= "#+begin_foo\nI'm a paragraph\n\nI'm a second paragrah\n#+end_foo\n"
  4020. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragrah"
  4021. (set-mark (point))
  4022. (goto-char (point-max))
  4023. (org-insert-structure-template "foo")
  4024. (buffer-string))))
  4025. ;; Test with example escaping.
  4026. (should
  4027. (string= "#+begin_example\n,* Heading\n#+end_example\n"
  4028. (org-test-with-temp-text "* Heading"
  4029. (org-mark-element)
  4030. (org-insert-structure-template "example")
  4031. (buffer-string))))
  4032. ;; Test with indentation.
  4033. (should
  4034. (string= " #+begin_foo\n This is a paragraph\n #+end_foo\n"
  4035. (org-test-with-temp-text " This is a paragraph"
  4036. (org-mark-element)
  4037. (org-insert-structure-template "foo")
  4038. (buffer-string))))
  4039. (should
  4040. (string= " #+begin_foo\n Line 1\n Line2\n #+end_foo\n"
  4041. (org-test-with-temp-text " Line 1\n Line2"
  4042. (org-mark-element)
  4043. (org-insert-structure-template "foo")
  4044. (buffer-string))))
  4045. ;; Test point location.
  4046. (should
  4047. (string= "#+begin_foo\n"
  4048. (org-test-with-temp-text ""
  4049. (org-insert-structure-template "foo")
  4050. (buffer-substring (point-min) (point)))))
  4051. (should
  4052. (string= "#+begin_src "
  4053. (org-test-with-temp-text ""
  4054. (org-insert-structure-template "src")
  4055. (buffer-substring (point-min) (point))))))
  4056. (ert-deftest test-org/previous-block ()
  4057. "Test `org-previous-block' specifications."
  4058. ;; Regular test.
  4059. (should
  4060. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  4061. (org-previous-block 1)
  4062. (looking-at "#\\+BEGIN_CENTER")))
  4063. ;; Ignore case.
  4064. (should
  4065. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  4066. (let ((case-fold-search nil))
  4067. (org-previous-block 1)
  4068. (looking-at "#\\+begin_center"))))
  4069. ;; Ignore current line.
  4070. (should
  4071. (org-test-with-temp-text
  4072. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  4073. (org-previous-block 1)
  4074. (looking-at "#\\+BEGIN_QUOTE")))
  4075. ;; Throw an error when no block is found.
  4076. (should-error
  4077. (org-test-with-temp-text "Paragraph<point>"
  4078. (org-previous-block 1)))
  4079. ;; With an argument, skip many blocks at once.
  4080. (should
  4081. (org-test-with-temp-text
  4082. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4083. (org-previous-block 2)
  4084. (looking-at "#\\+BEGIN_CENTER")))
  4085. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  4086. (should
  4087. (org-test-with-temp-text
  4088. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4089. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4090. (looking-at "#\\+BEGIN_QUOTE")))
  4091. ;; Optional argument is also case-insensitive.
  4092. (should
  4093. (org-test-with-temp-text
  4094. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  4095. (let ((case-fold-search nil))
  4096. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4097. (looking-at "#\\+begin_quote")))))
  4098. ;;; Outline structure
  4099. (ert-deftest test-org/demote ()
  4100. "Test `org-demote' specifications."
  4101. ;; Add correct number of stars according to `org-odd-levels-only'.
  4102. (should
  4103. (= 2
  4104. (org-test-with-temp-text "* H"
  4105. (let ((org-odd-levels-only nil)) (org-demote))
  4106. (org-current-level))))
  4107. (should
  4108. (= 3
  4109. (org-test-with-temp-text "* H"
  4110. (let ((org-odd-levels-only t)) (org-demote))
  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-demote))
  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-demote))
  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 accordingly.
  4131. (should
  4132. (= 3
  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-demote))
  4137. (forward-line)
  4138. (org-get-indentation))))
  4139. (should
  4140. (= 3
  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-demote))
  4145. (forward-line)
  4146. (org-get-indentation))))
  4147. (should-not
  4148. (= 3
  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-demote))
  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. (= 3
  4160. (org-test-with-temp-text "* H\n Paragraph"
  4161. (let ((org-odd-levels-only nil)
  4162. (org-adapt-indentation t))
  4163. (org-demote))
  4164. (forward-line)
  4165. (org-get-indentation))))
  4166. (should
  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-demote))
  4172. (forward-line)
  4173. (org-get-indentation))))
  4174. (should
  4175. (zerop
  4176. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  4177. (let ((org-odd-levels-only nil)
  4178. (org-adapt-indentation t))
  4179. (org-demote))
  4180. (goto-char (point-max))
  4181. (org-get-indentation))))
  4182. (should
  4183. (= 3
  4184. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  4185. (let ((org-odd-levels-only nil)
  4186. (org-adapt-indentation t))
  4187. (org-demote))
  4188. (goto-char (point-max))
  4189. (org-get-indentation))))
  4190. (when (featurep 'org-inlinetask)
  4191. (should
  4192. (zerop
  4193. (let ((org-inlinetask-min-level 5)
  4194. (org-adapt-indentation t))
  4195. (org-test-with-temp-text "* H\n***** I\n***** END"
  4196. (org-demote)
  4197. (forward-line)
  4198. (org-get-indentation))))))
  4199. (when (featurep 'org-inlinetask)
  4200. (should
  4201. (= 3
  4202. (let ((org-inlinetask-min-level 5)
  4203. (org-adapt-indentation t))
  4204. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  4205. (org-demote)
  4206. (forward-line 2)
  4207. (org-get-indentation))))))
  4208. ;; Ignore contents of source blocks or example blocks when
  4209. ;; indentation should be preserved (through
  4210. ;; `org-src-preserve-indentation' or "-i" flag).
  4211. (should-not
  4212. (zerop
  4213. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4214. (let ((org-adapt-indentation t)
  4215. (org-src-preserve-indentation nil))
  4216. (org-demote))
  4217. (forward-line 2)
  4218. (org-get-indentation))))
  4219. (should
  4220. (zerop
  4221. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  4222. (let ((org-adapt-indentation t)
  4223. (org-src-preserve-indentation t))
  4224. (org-demote))
  4225. (forward-line 2)
  4226. (org-get-indentation))))
  4227. (should
  4228. (zerop
  4229. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4230. (let ((org-adapt-indentation t)
  4231. (org-src-preserve-indentation t))
  4232. (org-demote))
  4233. (forward-line 2)
  4234. (org-get-indentation))))
  4235. (should
  4236. (zerop
  4237. (org-test-with-temp-text
  4238. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  4239. (let ((org-adapt-indentation t)
  4240. (org-src-preserve-indentation nil))
  4241. (org-demote))
  4242. (forward-line 2)
  4243. (org-get-indentation)))))
  4244. (ert-deftest test-org/promote ()
  4245. "Test `org-promote' specifications."
  4246. ;; Return an error if headline is to be promoted to level 0, unless
  4247. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  4248. ;; headline becomes a comment.
  4249. (should-error
  4250. (org-test-with-temp-text "* H"
  4251. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  4252. (should
  4253. (equal "# H"
  4254. (org-test-with-temp-text "* H"
  4255. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  4256. (buffer-string))))
  4257. ;; Remove correct number of stars according to
  4258. ;; `org-odd-levels-only'.
  4259. (should
  4260. (= 2
  4261. (org-test-with-temp-text "*** H"
  4262. (let ((org-odd-levels-only nil)) (org-promote))
  4263. (org-current-level))))
  4264. (should
  4265. (= 1
  4266. (org-test-with-temp-text "*** H"
  4267. (let ((org-odd-levels-only t)) (org-promote))
  4268. (org-current-level))))
  4269. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4270. (should
  4271. (org-test-with-temp-text "** H :tag:"
  4272. (let ((org-tags-column 10)
  4273. (org-auto-align-tags t)
  4274. (org-odd-levels-only nil))
  4275. (org-promote))
  4276. (org-move-to-column 10)
  4277. (looking-at-p ":tag:$")))
  4278. (should-not
  4279. (org-test-with-temp-text "** H :tag:"
  4280. (let ((org-tags-column 10)
  4281. (org-auto-align-tags nil)
  4282. (org-odd-levels-only nil))
  4283. (org-promote))
  4284. (org-move-to-column 10)
  4285. (looking-at-p ":tag:$")))
  4286. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4287. ;; info and property drawers.
  4288. (should
  4289. (= 2
  4290. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4291. (let ((org-odd-levels-only nil)
  4292. (org-adapt-indentation t))
  4293. (org-promote))
  4294. (forward-line)
  4295. (org-get-indentation))))
  4296. (should
  4297. (= 2
  4298. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4299. (let ((org-odd-levels-only nil)
  4300. (org-adapt-indentation t))
  4301. (org-promote))
  4302. (forward-line)
  4303. (org-get-indentation))))
  4304. (should-not
  4305. (= 2
  4306. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4307. (let ((org-odd-levels-only nil)
  4308. (org-adapt-indentation nil))
  4309. (org-promote))
  4310. (forward-line)
  4311. (org-get-indentation))))
  4312. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4313. ;; section accordingly. Ignore, however, footnote definitions and
  4314. ;; inlinetasks boundaries.
  4315. (should
  4316. (= 2
  4317. (org-test-with-temp-text "** H\n Paragraph"
  4318. (let ((org-odd-levels-only nil)
  4319. (org-adapt-indentation t))
  4320. (org-promote))
  4321. (forward-line)
  4322. (org-get-indentation))))
  4323. (should-not
  4324. (= 2
  4325. (org-test-with-temp-text "** H\n Paragraph"
  4326. (let ((org-odd-levels-only nil)
  4327. (org-adapt-indentation nil))
  4328. (org-promote))
  4329. (forward-line)
  4330. (org-get-indentation))))
  4331. (should
  4332. (= 2
  4333. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  4334. (let ((org-odd-levels-only nil)
  4335. (org-adapt-indentation t))
  4336. (org-promote))
  4337. (forward-line)
  4338. (org-get-indentation))))
  4339. (when (featurep 'org-inlinetask)
  4340. (should
  4341. (zerop
  4342. (let ((org-inlinetask-min-level 5)
  4343. (org-adapt-indentation t))
  4344. (org-test-with-temp-text "** H\n***** I\n***** END"
  4345. (org-promote)
  4346. (forward-line)
  4347. (org-get-indentation))))))
  4348. (when (featurep 'org-inlinetask)
  4349. (should
  4350. (= 2
  4351. (let ((org-inlinetask-min-level 5)
  4352. (org-adapt-indentation t))
  4353. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  4354. (org-promote)
  4355. (forward-line 2)
  4356. (org-get-indentation))))))
  4357. ;; Give up shifting if it would break document's structure
  4358. ;; otherwise.
  4359. (should
  4360. (= 3
  4361. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  4362. (let ((org-odd-levels-only nil)
  4363. (org-adapt-indentation t))
  4364. (org-promote))
  4365. (forward-line)
  4366. (org-get-indentation))))
  4367. (should
  4368. (= 3
  4369. (org-test-with-temp-text "** H\n Paragraph\n * list."
  4370. (let ((org-odd-levels-only nil)
  4371. (org-adapt-indentation t))
  4372. (org-promote))
  4373. (forward-line)
  4374. (org-get-indentation))))
  4375. ;; Ignore contents of source blocks or example blocks when
  4376. ;; indentation should be preserved (through
  4377. ;; `org-src-preserve-indentation' or "-i" flag).
  4378. (should-not
  4379. (zerop
  4380. (org-test-with-temp-text
  4381. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4382. (let ((org-adapt-indentation t)
  4383. (org-src-preserve-indentation nil)
  4384. (org-odd-levels-only nil))
  4385. (org-promote))
  4386. (forward-line)
  4387. (org-get-indentation))))
  4388. (should
  4389. (zerop
  4390. (org-test-with-temp-text
  4391. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  4392. (let ((org-adapt-indentation t)
  4393. (org-src-preserve-indentation t)
  4394. (org-odd-levels-only nil))
  4395. (org-promote))
  4396. (forward-line)
  4397. (org-get-indentation))))
  4398. (should
  4399. (zerop
  4400. (org-test-with-temp-text
  4401. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4402. (let ((org-adapt-indentation t)
  4403. (org-src-preserve-indentation t)
  4404. (org-odd-levels-only nil))
  4405. (org-promote))
  4406. (forward-line)
  4407. (org-get-indentation))))
  4408. (should
  4409. (zerop
  4410. (org-test-with-temp-text
  4411. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  4412. (let ((org-adapt-indentation t)
  4413. (org-src-preserve-indentation nil)
  4414. (org-odd-levels-only nil))
  4415. (org-promote))
  4416. (forward-line)
  4417. (org-get-indentation)))))
  4418. (ert-deftest test-org/org-get-valid-level ()
  4419. "Test function `org-get-valid-level' specifications."
  4420. (let ((org-odd-levels-only nil))
  4421. (should (equal 1 (org-get-valid-level 0 0)))
  4422. (should (equal 1 (org-get-valid-level 0 1)))
  4423. (should (equal 2 (org-get-valid-level 0 2)))
  4424. (should (equal 3 (org-get-valid-level 0 3)))
  4425. (should (equal 1 (org-get-valid-level 1 0)))
  4426. (should (equal 2 (org-get-valid-level 1 1)))
  4427. (should (equal 23 (org-get-valid-level 1 22)))
  4428. (should (equal 1 (org-get-valid-level 1 -1)))
  4429. (should (equal 1 (org-get-valid-level 2 -1))))
  4430. (let ((org-odd-levels-only t))
  4431. (should (equal 1 (org-get-valid-level 0 0)))
  4432. (should (equal 1 (org-get-valid-level 0 1)))
  4433. (should (equal 3 (org-get-valid-level 0 2)))
  4434. (should (equal 5 (org-get-valid-level 0 3)))
  4435. (should (equal 1 (org-get-valid-level 1 0)))
  4436. (should (equal 3 (org-get-valid-level 1 1)))
  4437. (should (equal 3 (org-get-valid-level 2 1)))
  4438. (should (equal 5 (org-get-valid-level 3 1)))
  4439. (should (equal 5 (org-get-valid-level 4 1)))
  4440. (should (equal 43 (org-get-valid-level 1 21)))
  4441. (should (equal 1 (org-get-valid-level 1 -1)))
  4442. (should (equal 1 (org-get-valid-level 2 -1)))
  4443. (should (equal 1 (org-get-valid-level 3 -1)))
  4444. (should (equal 3 (org-get-valid-level 4 -1)))
  4445. (should (equal 3 (org-get-valid-level 5 -1)))))
  4446. ;;; Planning
  4447. (ert-deftest test-org/at-planning-p ()
  4448. "Test `org-at-planning-p' specifications."
  4449. ;; Regular test.
  4450. (should
  4451. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  4452. (org-at-planning-p)))
  4453. (should-not
  4454. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  4455. (org-at-planning-p)))
  4456. ;; Correctly find planning attached to inlinetasks.
  4457. (when (featurep 'org-inlinetask)
  4458. (should
  4459. (org-test-with-temp-text
  4460. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  4461. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4462. (should-not
  4463. (org-test-with-temp-text
  4464. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4465. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4466. (should-not
  4467. (org-test-with-temp-text
  4468. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4469. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4470. (should-not
  4471. (org-test-with-temp-text
  4472. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  4473. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  4474. (ert-deftest test-org/add-planning-info ()
  4475. "Test `org-add-planning-info'."
  4476. ;; Create deadline when `org-adapt-indentation' is non-nil.
  4477. (should
  4478. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4479. (org-test-with-temp-text "* H\nParagraph<point>"
  4480. (let ((org-adapt-indentation t))
  4481. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4482. (replace-regexp-in-string
  4483. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4484. nil nil 1))))
  4485. ;; Create deadline when `org-adapt-indentation' is nil.
  4486. (should
  4487. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4488. (org-test-with-temp-text "* H\nParagraph<point>"
  4489. (let ((org-adapt-indentation nil))
  4490. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4491. (replace-regexp-in-string
  4492. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4493. nil nil 1))))
  4494. ;; Update deadline when `org-adapt-indentation' is non-nil.
  4495. (should
  4496. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4497. (org-test-with-temp-text "\
  4498. * H
  4499. DEADLINE: <2015-06-24 Wed>
  4500. Paragraph<point>"
  4501. (let ((org-adapt-indentation t))
  4502. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4503. (replace-regexp-in-string
  4504. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4505. nil nil 1))))
  4506. ;; Update deadline when `org-adapt-indentation' is nil.
  4507. (should
  4508. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4509. (org-test-with-temp-text "\
  4510. * H
  4511. DEADLINE: <2015-06-24 Wed>
  4512. Paragraph<point>"
  4513. (let ((org-adapt-indentation nil))
  4514. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4515. (replace-regexp-in-string
  4516. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4517. nil nil 1))))
  4518. ;; Schedule when `org-adapt-indentation' is non-nil.
  4519. (should
  4520. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  4521. (org-test-with-temp-text "* H\nParagraph<point>"
  4522. (let ((org-adapt-indentation t))
  4523. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4524. (replace-regexp-in-string
  4525. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4526. nil nil 1))))
  4527. ;; Schedule when `org-adapt-indentation' is nil.
  4528. (should
  4529. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  4530. (org-test-with-temp-text "* H\nParagraph<point>"
  4531. (let ((org-adapt-indentation nil))
  4532. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4533. (replace-regexp-in-string
  4534. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4535. nil nil 1))))
  4536. ;; Add deadline when scheduled.
  4537. (should
  4538. (equal "\
  4539. * H
  4540. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  4541. Paragraph"
  4542. (org-test-with-temp-text "\
  4543. * H
  4544. SCHEDULED: <2015-06-24 Wed>
  4545. Paragraph<point>"
  4546. (let ((org-adapt-indentation t))
  4547. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4548. (replace-regexp-in-string
  4549. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4550. nil nil 1))))
  4551. ;; Remove middle entry.
  4552. (should
  4553. (equal "\
  4554. * H
  4555. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  4556. Paragraph"
  4557. (org-test-with-temp-text "\
  4558. * H
  4559. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4560. Paragraph<point>"
  4561. (let ((org-adapt-indentation t))
  4562. (org-add-planning-info nil nil 'deadline))
  4563. (replace-regexp-in-string
  4564. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4565. nil nil 1))))
  4566. ;; Remove last entry and then middle entry (order should not
  4567. ;; matter).
  4568. (should
  4569. (equal "\
  4570. * H
  4571. CLOSED: [2015-06-24]
  4572. Paragraph"
  4573. (org-test-with-temp-text "\
  4574. * H
  4575. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4576. Paragraph<point>"
  4577. (let ((org-adapt-indentation t))
  4578. (org-add-planning-info nil nil 'scheduled 'deadline))
  4579. (replace-regexp-in-string
  4580. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4581. nil nil 1))))
  4582. ;; Remove closed when `org-adapt-indentation' is non-nil.
  4583. (should
  4584. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4585. (org-test-with-temp-text "\
  4586. * H
  4587. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4588. Paragraph<point>"
  4589. (let ((org-adapt-indentation t))
  4590. (org-add-planning-info nil nil 'closed))
  4591. (replace-regexp-in-string
  4592. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4593. nil nil 1))))
  4594. (should
  4595. (equal "* H\n Paragraph"
  4596. (org-test-with-temp-text "\
  4597. * H
  4598. CLOSED: [2015-06-25 Thu]
  4599. Paragraph<point>"
  4600. (let ((org-adapt-indentation t))
  4601. (org-add-planning-info nil nil 'closed))
  4602. (replace-regexp-in-string
  4603. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4604. nil nil 1))))
  4605. ;; Remove closed when `org-adapt-indentation' is nil.
  4606. (should
  4607. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4608. (org-test-with-temp-text "\
  4609. * H
  4610. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4611. Paragraph<point>"
  4612. (let ((org-adapt-indentation nil))
  4613. (org-add-planning-info nil nil 'closed))
  4614. (replace-regexp-in-string
  4615. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4616. nil nil 1))))
  4617. (should
  4618. (equal "* H\nParagraph"
  4619. (org-test-with-temp-text "\
  4620. * H
  4621. CLOSED: [2015-06-25 Thu]
  4622. Paragraph<point>"
  4623. (let ((org-adapt-indentation nil))
  4624. (org-add-planning-info nil nil 'closed))
  4625. (replace-regexp-in-string
  4626. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4627. nil nil 1))))
  4628. ;; Remove closed entry and delete empty line.
  4629. (should
  4630. (equal "\
  4631. * H
  4632. Paragraph"
  4633. (org-test-with-temp-text "\
  4634. * H
  4635. CLOSED: [2015-06-24 Wed]
  4636. Paragraph<point>"
  4637. (let ((org-adapt-indentation t))
  4638. (org-add-planning-info nil nil 'closed))
  4639. (replace-regexp-in-string
  4640. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4641. nil nil 1))))
  4642. ;; Remove one entry and update another.
  4643. (should
  4644. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4645. (org-test-with-temp-text "\
  4646. * H
  4647. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  4648. Paragraph<point>"
  4649. (let ((org-adapt-indentation t))
  4650. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  4651. (replace-regexp-in-string
  4652. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4653. nil nil 1)))))
  4654. (ert-deftest test-org/deadline ()
  4655. "Test `org-deadline' specifications."
  4656. ;; Insert a new value or replace existing one.
  4657. (should
  4658. (equal "* H\nDEADLINE: <2012-03-29>\n"
  4659. (org-test-with-temp-text "* H"
  4660. (let ((org-adapt-indentation nil)
  4661. (org-last-inserted-timestamp nil))
  4662. (org-deadline nil "<2012-03-29 Tue>"))
  4663. (replace-regexp-in-string
  4664. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4665. nil nil 1))))
  4666. (should
  4667. (equal "* H\nDEADLINE: <2014-03-04>"
  4668. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4669. (let ((org-adapt-indentation nil)
  4670. (org-last-inserted-timestamp nil))
  4671. (org-deadline nil "<2014-03-04 Thu>"))
  4672. (replace-regexp-in-string
  4673. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4674. nil nil 1))))
  4675. ;; Accept delta time, e.g., "+2d".
  4676. (should
  4677. (equal "* H\nDEADLINE: <2015-03-04>\n"
  4678. (org-test-at-time "2014-03-04"
  4679. (org-test-with-temp-text "* H"
  4680. (let ((org-adapt-indentation nil)
  4681. (org-last-inserted-timestamp nil))
  4682. (org-deadline nil "+1y"))
  4683. (replace-regexp-in-string
  4684. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4685. ;; Preserve repeater.
  4686. (should
  4687. (equal "* H\nDEADLINE: <2012-03-29 +2y>\n"
  4688. (org-test-with-temp-text "* H"
  4689. (let ((org-adapt-indentation nil)
  4690. (org-last-inserted-timestamp nil))
  4691. (org-deadline nil "<2012-03-29 Tue +2y>"))
  4692. (replace-regexp-in-string
  4693. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4694. ;; Remove CLOSED keyword, if any.
  4695. (should
  4696. (equal "* H\nDEADLINE: <2012-03-29>"
  4697. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4698. (let ((org-adapt-indentation nil)
  4699. (org-last-inserted-timestamp nil))
  4700. (org-deadline nil "<2012-03-29 Tue>"))
  4701. (replace-regexp-in-string
  4702. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4703. ;; With C-u argument, remove DEADLINE keyword.
  4704. (should
  4705. (equal "* H\n"
  4706. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4707. (let ((org-adapt-indentation nil)
  4708. (org-last-inserted-timestamp nil))
  4709. (org-deadline '(4)))
  4710. (buffer-string))))
  4711. (should
  4712. (equal "* H"
  4713. (org-test-with-temp-text "* H"
  4714. (let ((org-adapt-indentation nil)
  4715. (org-last-inserted-timestamp nil))
  4716. (org-deadline '(4)))
  4717. (buffer-string))))
  4718. ;; With C-u C-u argument, prompt for a delay cookie.
  4719. (should
  4720. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  4721. (cl-letf (((symbol-function 'org-read-date)
  4722. (lambda (&rest args)
  4723. (apply #'encode-time
  4724. (org-parse-time-string "2014-03-04")))))
  4725. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4726. (let ((org-adapt-indentation nil)
  4727. (org-last-inserted-timestamp nil))
  4728. (org-deadline '(16)))
  4729. (buffer-string)))))
  4730. (should-error
  4731. (cl-letf (((symbol-function 'org-read-date)
  4732. (lambda (&rest args)
  4733. (apply #'encode-time
  4734. (org-parse-time-string "2014-03-04")))))
  4735. (org-test-with-temp-text "* H"
  4736. (let ((org-adapt-indentation nil)
  4737. (org-last-inserted-timestamp nil))
  4738. (org-deadline '(16)))
  4739. (buffer-string))))
  4740. ;; When a region is active and
  4741. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4742. ;; same value in all headlines in region.
  4743. (should
  4744. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4745. (org-test-with-temp-text "* H1\n* H2"
  4746. (let ((org-adapt-indentation nil)
  4747. (org-last-inserted-timestamp nil)
  4748. (org-loop-over-headlines-in-active-region t))
  4749. (transient-mark-mode 1)
  4750. (push-mark (point) t t)
  4751. (goto-char (point-max))
  4752. (org-deadline nil "2012-03-29"))
  4753. (replace-regexp-in-string
  4754. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4755. (should-not
  4756. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4757. (org-test-with-temp-text "* H1\n* H2"
  4758. (let ((org-adapt-indentation nil)
  4759. (org-last-inserted-timestamp nil)
  4760. (org-loop-over-headlines-in-active-region nil))
  4761. (transient-mark-mode 1)
  4762. (push-mark (point) t t)
  4763. (goto-char (point-max))
  4764. (org-deadline nil "2012-03-29"))
  4765. (replace-regexp-in-string
  4766. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4767. (ert-deftest test-org/schedule ()
  4768. "Test `org-schedule' specifications."
  4769. ;; Insert a new value or replace existing one.
  4770. (should
  4771. (equal "* H\nSCHEDULED: <2012-03-29>\n"
  4772. (org-test-with-temp-text "* H"
  4773. (let ((org-adapt-indentation nil)
  4774. (org-last-inserted-timestamp nil))
  4775. (org-schedule nil "<2012-03-29 Tue>"))
  4776. (replace-regexp-in-string
  4777. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4778. nil nil 1))))
  4779. (should
  4780. (equal "* H\nSCHEDULED: <2014-03-04>"
  4781. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4782. (let ((org-adapt-indentation nil)
  4783. (org-last-inserted-timestamp nil))
  4784. (org-schedule nil "<2014-03-04 Thu>"))
  4785. (replace-regexp-in-string
  4786. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4787. nil nil 1))))
  4788. ;; Accept delta time, e.g., "+2d".
  4789. (should
  4790. (equal "* H\nSCHEDULED: <2015-03-04>\n"
  4791. (org-test-at-time "2014-03-04"
  4792. (org-test-with-temp-text "* H"
  4793. (let ((org-adapt-indentation nil)
  4794. (org-last-inserted-timestamp nil))
  4795. (org-schedule nil "+1y"))
  4796. (replace-regexp-in-string
  4797. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4798. ;; Preserve repeater.
  4799. (should
  4800. (equal "* H\nSCHEDULED: <2012-03-29 +2y>\n"
  4801. (org-test-with-temp-text "* H"
  4802. (let ((org-adapt-indentation nil)
  4803. (org-last-inserted-timestamp nil))
  4804. (org-schedule nil "<2012-03-29 Tue +2y>"))
  4805. (replace-regexp-in-string
  4806. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4807. ;; Remove CLOSED keyword, if any.
  4808. (should
  4809. (equal "* H\nSCHEDULED: <2012-03-29>"
  4810. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4811. (let ((org-adapt-indentation nil)
  4812. (org-last-inserted-timestamp nil))
  4813. (org-schedule nil "<2012-03-29 Tue>"))
  4814. (replace-regexp-in-string
  4815. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4816. ;; With C-u argument, remove SCHEDULED keyword.
  4817. (should
  4818. (equal "* H\n"
  4819. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4820. (let ((org-adapt-indentation nil)
  4821. (org-last-inserted-timestamp nil))
  4822. (org-schedule '(4)))
  4823. (buffer-string))))
  4824. (should
  4825. (equal "* H"
  4826. (org-test-with-temp-text "* H"
  4827. (let ((org-adapt-indentation nil)
  4828. (org-last-inserted-timestamp nil))
  4829. (org-schedule '(4)))
  4830. (buffer-string))))
  4831. ;; With C-u C-u argument, prompt for a delay cookie.
  4832. (should
  4833. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  4834. (cl-letf (((symbol-function 'org-read-date)
  4835. (lambda (&rest args)
  4836. (apply #'encode-time
  4837. (org-parse-time-string "2014-03-04")))))
  4838. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4839. (let ((org-adapt-indentation nil)
  4840. (org-last-inserted-timestamp nil))
  4841. (org-schedule '(16)))
  4842. (buffer-string)))))
  4843. (should-error
  4844. (cl-letf (((symbol-function 'org-read-date)
  4845. (lambda (&rest args)
  4846. (apply #'encode-time
  4847. (org-parse-time-string "2014-03-04")))))
  4848. (org-test-with-temp-text "* H"
  4849. (let ((org-adapt-indentation nil)
  4850. (org-last-inserted-timestamp nil))
  4851. (org-schedule '(16)))
  4852. (buffer-string))))
  4853. ;; When a region is active and
  4854. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4855. ;; same value in all headlines in region.
  4856. (should
  4857. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4858. (org-test-with-temp-text "* H1\n* H2"
  4859. (let ((org-adapt-indentation nil)
  4860. (org-last-inserted-timestamp nil)
  4861. (org-loop-over-headlines-in-active-region t))
  4862. (transient-mark-mode 1)
  4863. (push-mark (point) t t)
  4864. (goto-char (point-max))
  4865. (org-schedule nil "2012-03-29"))
  4866. (replace-regexp-in-string
  4867. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4868. (should-not
  4869. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4870. (org-test-with-temp-text "* H1\n* H2"
  4871. (let ((org-adapt-indentation nil)
  4872. (org-last-inserted-timestamp nil)
  4873. (org-loop-over-headlines-in-active-region nil))
  4874. (transient-mark-mode 1)
  4875. (push-mark (point) t t)
  4876. (goto-char (point-max))
  4877. (org-schedule nil "2012-03-29"))
  4878. (replace-regexp-in-string
  4879. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4880. (should
  4881. ;; check if a repeater survives re-scheduling.
  4882. (string-match-p
  4883. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  4884. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  4885. (let ((org-adapt-indentation nil)
  4886. (org-last-inserted-timestamp nil))
  4887. (org-schedule nil "2017-02-01"))
  4888. (buffer-string)))))
  4889. ;;; Property API
  4890. (ert-deftest test-org/buffer-property-keys ()
  4891. "Test `org-buffer-property-keys' specifications."
  4892. ;; Retrieve properties accross siblings.
  4893. (should
  4894. (equal '("A" "B")
  4895. (org-test-with-temp-text "
  4896. * H1
  4897. :PROPERTIES:
  4898. :A: 1
  4899. :END:
  4900. * H2
  4901. :PROPERTIES:
  4902. :B: 1
  4903. :END:"
  4904. (org-buffer-property-keys))))
  4905. ;; Retrieve properties accross children.
  4906. (should
  4907. (equal '("A" "B")
  4908. (org-test-with-temp-text "
  4909. * H1
  4910. :PROPERTIES:
  4911. :A: 1
  4912. :END:
  4913. ** H2
  4914. :PROPERTIES:
  4915. :B: 1
  4916. :END:"
  4917. (org-buffer-property-keys))))
  4918. ;; Retrieve muliple properties in the same drawer.
  4919. (should
  4920. (equal '("A" "B")
  4921. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4922. (org-buffer-property-keys))))
  4923. ;; Ignore extension symbol in property name.
  4924. (should
  4925. (equal '("A")
  4926. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4927. (org-buffer-property-keys))))
  4928. ;; With non-nil COLUMNS, extract property names from columns.
  4929. (should
  4930. (equal '("A" "B")
  4931. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  4932. (org-buffer-property-keys nil nil t))))
  4933. (should
  4934. (equal '("A" "B" "COLUMNS")
  4935. (org-test-with-temp-text
  4936. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  4937. (org-buffer-property-keys nil nil t))))
  4938. ;; In COLUMNS, ignore title and summary-type.
  4939. (should
  4940. (equal '("A")
  4941. (org-test-with-temp-text "#+COLUMNS: %A(Foo)"
  4942. (org-buffer-property-keys nil nil t))))
  4943. (should
  4944. (equal '("A")
  4945. (org-test-with-temp-text "#+COLUMNS: %A{Foo}"
  4946. (org-buffer-property-keys nil nil t))))
  4947. (should
  4948. (equal '("A")
  4949. (org-test-with-temp-text "#+COLUMNS: %A(Foo){Bar}"
  4950. (org-buffer-property-keys nil nil t)))))
  4951. (ert-deftest test-org/property-values ()
  4952. "Test `org-property-values' specifications."
  4953. ;; Regular test.
  4954. (should
  4955. (equal '("2" "1")
  4956. (org-test-with-temp-text
  4957. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  4958. (org-property-values "A"))))
  4959. ;; Ignore empty values.
  4960. (should-not
  4961. (org-test-with-temp-text
  4962. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  4963. (org-property-values "A")))
  4964. ;; Take into consideration extended values.
  4965. (should
  4966. (equal '("1 2")
  4967. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4968. (org-property-values "A")))))
  4969. (ert-deftest test-org/find-property ()
  4970. "Test `org-find-property' specifications."
  4971. ;; Regular test.
  4972. (should
  4973. (= 1
  4974. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  4975. (org-find-property "prop"))))
  4976. ;; Ignore false positives.
  4977. (should
  4978. (= 27
  4979. (org-test-with-temp-text
  4980. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  4981. (org-find-property "A"))))
  4982. ;; Return first entry found in buffer.
  4983. (should
  4984. (= 1
  4985. (org-test-with-temp-text
  4986. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4987. (org-find-property "A"))))
  4988. ;; Only search visible part of the buffer.
  4989. (should
  4990. (= 31
  4991. (org-test-with-temp-text
  4992. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4993. (org-narrow-to-subtree)
  4994. (org-find-property "A"))))
  4995. ;; With optional argument, only find entries with a specific value.
  4996. (should-not
  4997. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4998. (org-find-property "A" "2")))
  4999. (should
  5000. (= 31
  5001. (org-test-with-temp-text
  5002. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  5003. (org-find-property "A" "2"))))
  5004. ;; Use "nil" for explicit nil values.
  5005. (should
  5006. (= 31
  5007. (org-test-with-temp-text
  5008. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  5009. (org-find-property "A" "nil")))))
  5010. (ert-deftest test-org/entry-delete ()
  5011. "Test `org-entry-delete' specifications."
  5012. ;; Regular test.
  5013. (should
  5014. (string-match
  5015. " *:PROPERTIES:\n *:B: +2\n *:END:"
  5016. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5017. (org-entry-delete (point) "A")
  5018. (buffer-string))))
  5019. ;; Also remove accumulated properties.
  5020. (should-not
  5021. (string-match
  5022. ":A"
  5023. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  5024. (org-entry-delete (point) "A")
  5025. (buffer-string))))
  5026. ;; When last property is removed, remove the property drawer.
  5027. (should-not
  5028. (string-match
  5029. ":PROPERTIES:"
  5030. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  5031. (org-entry-delete (point) "A")
  5032. (buffer-string))))
  5033. ;; Return a non-nil value when some property was removed.
  5034. (should
  5035. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5036. (org-entry-delete (point) "A")))
  5037. (should-not
  5038. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5039. (org-entry-delete (point) "C")))
  5040. ;; Special properties cannot be located in a drawer. Allow to
  5041. ;; remove them anyway, in case of user error.
  5042. (should
  5043. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  5044. (org-entry-delete (point) "SCHEDULED"))))
  5045. (ert-deftest test-org/entry-get ()
  5046. "Test `org-entry-get' specifications."
  5047. ;; Regular test.
  5048. (should
  5049. (equal "1"
  5050. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5051. (org-entry-get (point) "A"))))
  5052. ;; Ignore case.
  5053. (should
  5054. (equal "1"
  5055. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5056. (org-entry-get (point) "a"))))
  5057. ;; Handle extended values, both before and after base value.
  5058. (should
  5059. (equal "1 2 3"
  5060. (org-test-with-temp-text
  5061. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5062. (org-entry-get (point) "A"))))
  5063. ;; Empty values are returned as the empty string.
  5064. (should
  5065. (equal ""
  5066. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  5067. (org-entry-get (point) "A"))))
  5068. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  5069. ;; otherwise, return nil.
  5070. (should-not
  5071. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5072. (org-entry-get (point) "A")))
  5073. (should
  5074. (equal "nil"
  5075. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5076. (org-entry-get (point) "A" nil t))))
  5077. ;; Return nil when no property is found, independently on the
  5078. ;; LITERAL-NIL argument.
  5079. (should-not
  5080. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5081. (org-entry-get (point) "B")))
  5082. (should-not
  5083. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5084. (org-entry-get (point) "B" nil t)))
  5085. ;; Handle inheritance, when allowed. Include extended values and
  5086. ;; possibly global values.
  5087. (should
  5088. (equal
  5089. "1"
  5090. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5091. (org-entry-get (point) "A" t))))
  5092. (should
  5093. (equal
  5094. "1"
  5095. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5096. (let ((org-use-property-inheritance t))
  5097. (org-entry-get (point) "A" 'selective)))))
  5098. (should-not
  5099. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  5100. (let ((org-use-property-inheritance nil))
  5101. (org-entry-get (point) "A" 'selective))))
  5102. (should
  5103. (equal
  5104. "1 2"
  5105. (org-test-with-temp-text
  5106. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  5107. (org-entry-get (point-max) "A" t))))
  5108. (should
  5109. (equal "1"
  5110. (org-test-with-temp-text
  5111. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  5112. (org-mode-restart)
  5113. (org-entry-get (point-max) "A" t))))
  5114. (should
  5115. (equal "0 1"
  5116. (org-test-with-temp-text
  5117. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5118. (org-mode-restart)
  5119. (org-entry-get (point-max) "A" t)))))
  5120. (ert-deftest test-org/entry-properties ()
  5121. "Test `org-entry-properties' specifications."
  5122. ;; Get "ITEM" property.
  5123. (should
  5124. (equal "H"
  5125. (org-test-with-temp-text "* TODO H"
  5126. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  5127. (should
  5128. (equal "H"
  5129. (org-test-with-temp-text "* TODO H"
  5130. (cdr (assoc "ITEM" (org-entry-properties))))))
  5131. ;; Get "TODO" property. TODO keywords are case sensitive.
  5132. (should
  5133. (equal "TODO"
  5134. (org-test-with-temp-text "* TODO H"
  5135. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  5136. (should
  5137. (equal "TODO"
  5138. (org-test-with-temp-text "* TODO H"
  5139. (cdr (assoc "TODO" (org-entry-properties))))))
  5140. (should-not
  5141. (org-test-with-temp-text "* H"
  5142. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5143. (should-not
  5144. (org-test-with-temp-text "* todo H"
  5145. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5146. ;; Get "PRIORITY" property.
  5147. (should
  5148. (equal "A"
  5149. (org-test-with-temp-text "* [#A] H"
  5150. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5151. (should
  5152. (equal "A"
  5153. (org-test-with-temp-text "* [#A] H"
  5154. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  5155. (should
  5156. (equal (char-to-string org-default-priority)
  5157. (org-test-with-temp-text "* H"
  5158. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5159. ;; Get "FILE" property.
  5160. (should
  5161. (org-test-with-temp-text-in-file "* H\nParagraph"
  5162. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  5163. (buffer-file-name))))
  5164. (should
  5165. (org-test-with-temp-text-in-file "* H\nParagraph"
  5166. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  5167. (buffer-file-name))))
  5168. (should-not
  5169. (org-test-with-temp-text "* H\nParagraph"
  5170. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  5171. ;; Get "TAGS" property.
  5172. (should
  5173. (equal ":tag1:tag2:"
  5174. (org-test-with-temp-text "* H :tag1:tag2:"
  5175. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  5176. (should
  5177. (equal ":tag1:tag2:"
  5178. (org-test-with-temp-text "* H :tag1:tag2:"
  5179. (cdr (assoc "TAGS" (org-entry-properties))))))
  5180. (should-not
  5181. (org-test-with-temp-text "* H"
  5182. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  5183. ;; Get "ALLTAGS" property.
  5184. (should
  5185. (equal ":tag1:tag2:"
  5186. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5187. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  5188. (should
  5189. (equal ":tag1:tag2:"
  5190. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5191. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  5192. (should-not
  5193. (org-test-with-temp-text "* H"
  5194. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  5195. ;; Get "BLOCKED" property.
  5196. (should
  5197. (equal "t"
  5198. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  5199. (let ((org-enforce-todo-dependencies t)
  5200. (org-blocker-hook
  5201. '(org-block-todo-from-children-or-siblings-or-parent)))
  5202. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5203. (should
  5204. (equal ""
  5205. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  5206. (let ((org-enforce-todo-dependencies t)
  5207. (org-blocker-hook
  5208. '(org-block-todo-from-children-or-siblings-or-parent)))
  5209. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5210. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  5211. (should
  5212. (equal
  5213. "[2012-03-29 thu.]"
  5214. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5215. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  5216. (should
  5217. (equal
  5218. "[2012-03-29 thu.]"
  5219. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5220. (cdr (assoc "CLOSED" (org-entry-properties))))))
  5221. (should-not
  5222. (org-test-with-temp-text "* H"
  5223. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  5224. (should
  5225. (equal
  5226. "<2014-03-04 tue.>"
  5227. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5228. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  5229. (should
  5230. (equal
  5231. "<2014-03-04 tue.>"
  5232. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5233. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  5234. (should-not
  5235. (org-test-with-temp-text "* H"
  5236. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  5237. (should
  5238. (equal
  5239. "<2014-03-04 tue.>"
  5240. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5241. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  5242. (should
  5243. (equal
  5244. "<2014-03-04 tue.>"
  5245. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5246. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  5247. (should-not
  5248. (org-test-with-temp-text "* H"
  5249. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  5250. ;; Get "CATEGORY"
  5251. (should
  5252. (equal "cat"
  5253. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5254. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  5255. (should
  5256. (equal "cat"
  5257. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5258. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5259. (should
  5260. (equal "cat"
  5261. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  5262. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5263. (should
  5264. (equal "cat2"
  5265. (org-test-with-temp-text
  5266. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  5267. "\n"
  5268. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  5269. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5270. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  5271. (should
  5272. (equal "<2012-03-29 thu.>"
  5273. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  5274. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5275. (should
  5276. (equal "[2012-03-29 thu.]"
  5277. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  5278. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  5279. (should
  5280. (equal "<2012-03-29 thu.>"
  5281. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  5282. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  5283. (should
  5284. (equal "[2014-03-04 tue.]"
  5285. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  5286. (cdr (assoc "TIMESTAMP_IA"
  5287. (org-entry-properties nil "TIMESTAMP_IA"))))))
  5288. (should-not
  5289. (equal "<2012-03-29 thu.>"
  5290. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  5291. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5292. ;; Get standard properties.
  5293. (should
  5294. (equal "1"
  5295. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5296. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5297. ;; Handle extended properties.
  5298. (should
  5299. (equal "1 2 3"
  5300. (org-test-with-temp-text
  5301. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5302. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5303. (should
  5304. (equal "1 2 3"
  5305. (org-test-with-temp-text
  5306. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  5307. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5308. ;; Ignore forbidden (special) properties.
  5309. (should-not
  5310. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  5311. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  5312. (ert-deftest test-org/entry-put ()
  5313. "Test `org-entry-put' specifications."
  5314. ;; Error when not a string or nil.
  5315. (should-error
  5316. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5317. (org-entry-put 1 "test" 2)))
  5318. ;; Error when property name is invalid.
  5319. (should-error
  5320. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5321. (org-entry-put 1 "no space" "value")))
  5322. (should-error
  5323. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5324. (org-entry-put 1 "" "value")))
  5325. ;; Set "TODO" property.
  5326. (should
  5327. (string-match (regexp-quote " TODO H")
  5328. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5329. (org-entry-put (point) "TODO" "TODO")
  5330. (buffer-string))))
  5331. (should
  5332. (string-match (regexp-quote "* H")
  5333. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5334. (org-entry-put (point) "TODO" nil)
  5335. (buffer-string))))
  5336. ;; Set "PRIORITY" property.
  5337. (should
  5338. (equal "* [#A] H"
  5339. (org-test-with-temp-text "* [#B] H"
  5340. (org-entry-put (point) "PRIORITY" "A")
  5341. (buffer-string))))
  5342. (should
  5343. (equal "* H"
  5344. (org-test-with-temp-text "* [#B] H"
  5345. (org-entry-put (point) "PRIORITY" nil)
  5346. (buffer-string))))
  5347. ;; Set "SCHEDULED" property.
  5348. (should
  5349. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  5350. (org-test-with-temp-text "* H"
  5351. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  5352. (buffer-string))))
  5353. (should
  5354. (string= "* H\n"
  5355. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5356. (org-entry-put (point) "SCHEDULED" nil)
  5357. (buffer-string))))
  5358. (should
  5359. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  5360. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5361. (org-entry-put (point) "SCHEDULED" "earlier")
  5362. (buffer-string))))
  5363. (should
  5364. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  5365. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5366. (org-entry-put (point) "SCHEDULED" "later")
  5367. (buffer-string))))
  5368. ;; Set "DEADLINE" property.
  5369. (should
  5370. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  5371. (org-test-with-temp-text "* H"
  5372. (org-entry-put (point) "DEADLINE" "2014-03-04")
  5373. (buffer-string))))
  5374. (should
  5375. (string= "* H\n"
  5376. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5377. (org-entry-put (point) "DEADLINE" nil)
  5378. (buffer-string))))
  5379. (should
  5380. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  5381. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5382. (org-entry-put (point) "DEADLINE" "earlier")
  5383. (buffer-string))))
  5384. (should
  5385. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  5386. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5387. (org-entry-put (point) "DEADLINE" "later")
  5388. (buffer-string))))
  5389. ;; Set "CATEGORY" property
  5390. (should
  5391. (string-match "^ *:CATEGORY: cat"
  5392. (org-test-with-temp-text "* H"
  5393. (org-entry-put (point) "CATEGORY" "cat")
  5394. (buffer-string))))
  5395. ;; Regular properties, with or without pre-existing drawer.
  5396. (should
  5397. (string-match "^ *:A: +2$"
  5398. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5399. (org-entry-put (point) "A" "2")
  5400. (buffer-string))))
  5401. (should
  5402. (string-match "^ *:A: +1$"
  5403. (org-test-with-temp-text "* H"
  5404. (org-entry-put (point) "A" "1")
  5405. (buffer-string))))
  5406. ;; Special case: two consecutive headlines.
  5407. (should
  5408. (string-match "\\* A\n *:PROPERTIES:"
  5409. (org-test-with-temp-text "* A\n** B"
  5410. (org-entry-put (point) "A" "1")
  5411. (buffer-string)))))
  5412. (ert-deftest test-org/refresh-properties ()
  5413. "Test `org-refresh-properties' specifications."
  5414. (should
  5415. (equal "1"
  5416. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5417. (org-refresh-properties "A" 'org-test)
  5418. (get-text-property (point) 'org-test))))
  5419. (should-not
  5420. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5421. (org-refresh-properties "B" 'org-test)
  5422. (get-text-property (point) 'org-test)))
  5423. ;; Handle properties only defined with extension syntax, i.e.,
  5424. ;; "PROPERTY+".
  5425. (should
  5426. (equal "1"
  5427. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5428. (org-refresh-properties "A" 'org-test)
  5429. (get-text-property (point) 'org-test))))
  5430. ;; When property is inherited, add text property to the whole
  5431. ;; sub-tree.
  5432. (should
  5433. (equal "1"
  5434. (org-test-with-temp-text
  5435. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  5436. (let ((org-use-property-inheritance t))
  5437. (org-refresh-properties "A" 'org-test))
  5438. (get-text-property (point) 'org-test))))
  5439. ;; When property is inherited, use global value across the whole
  5440. ;; buffer. However local values have precedence.
  5441. (should-not
  5442. (equal "1"
  5443. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5444. (org-mode-restart)
  5445. (let ((org-use-property-inheritance nil))
  5446. (org-refresh-properties "A" 'org-test))
  5447. (get-text-property (point) 'org-test))))
  5448. (should
  5449. (equal "1"
  5450. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5451. (org-mode-restart)
  5452. (let ((org-use-property-inheritance t))
  5453. (org-refresh-properties "A" 'org-test))
  5454. (get-text-property (point) 'org-test))))
  5455. (should
  5456. (equal "2"
  5457. (org-test-with-temp-text
  5458. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  5459. (org-mode-restart)
  5460. (let ((org-use-property-inheritance t))
  5461. (org-refresh-properties "A" 'org-test))
  5462. (get-text-property (point) 'org-test)))))
  5463. ;;; Radio Targets
  5464. (ert-deftest test-org/update-radio-target-regexp ()
  5465. "Test `org-update-radio-target-regexp' specifications."
  5466. ;; Properly update cache with no previous radio target regexp.
  5467. (should
  5468. (eq 'link
  5469. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5470. (save-excursion (goto-char (point-max)) (org-element-context))
  5471. (insert "<<<")
  5472. (search-forward "o")
  5473. (insert ">>>")
  5474. (org-update-radio-target-regexp)
  5475. (goto-char (point-max))
  5476. (org-element-type (org-element-context)))))
  5477. ;; Properly update cache with previous radio target regexp.
  5478. (should
  5479. (eq 'link
  5480. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5481. (save-excursion (goto-char (point-max)) (org-element-context))
  5482. (insert "<<<")
  5483. (search-forward "o")
  5484. (insert ">>>")
  5485. (org-update-radio-target-regexp)
  5486. (search-backward "r")
  5487. (delete-char 5)
  5488. (insert "new")
  5489. (org-update-radio-target-regexp)
  5490. (goto-char (point-max))
  5491. (delete-region (line-beginning-position) (point))
  5492. (insert "new")
  5493. (org-element-type (org-element-context))))))
  5494. ;;; Refile
  5495. (ert-deftest test-org/refile-get-targets ()
  5496. "Test `org-refile-get-targets' specifications."
  5497. ;; :maxlevel includes all headings above specified value.
  5498. (should
  5499. (equal '("H1" "H2" "H3")
  5500. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5501. (let ((org-refile-use-outline-path nil)
  5502. (org-refile-targets `((nil :maxlevel . 3))))
  5503. (mapcar #'car (org-refile-get-targets))))))
  5504. (should
  5505. (equal '("H1" "H2")
  5506. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5507. (let ((org-refile-use-outline-path nil)
  5508. (org-refile-targets `((nil :maxlevel . 2))))
  5509. (mapcar #'car (org-refile-get-targets))))))
  5510. ;; :level limits targets to headlines with the specified level.
  5511. (should
  5512. (equal '("H2")
  5513. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5514. (let ((org-refile-use-outline-path nil)
  5515. (org-refile-targets `((nil :level . 2))))
  5516. (mapcar #'car (org-refile-get-targets))))))
  5517. ;; :tag limits targets to headlines with specified tag.
  5518. (should
  5519. (equal '("H1")
  5520. (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
  5521. (let ((org-refile-use-outline-path nil)
  5522. (org-refile-targets `((nil :tag . "foo"))))
  5523. (mapcar #'car (org-refile-get-targets))))))
  5524. ;; :todo limits targets to headlines with specified TODO keyword.
  5525. (should
  5526. (equal '("H2")
  5527. (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
  5528. (let ((org-refile-use-outline-path nil)
  5529. (org-refile-targets `((nil :todo . "TODO"))))
  5530. (mapcar #'car (org-refile-get-targets))))))
  5531. ;; :regexp filters targets matching provided regexp.
  5532. (should
  5533. (equal '("F2" "F3")
  5534. (org-test-with-temp-text "* H1\n** F2\n*** F3"
  5535. (let ((org-refile-use-outline-path nil)
  5536. (org-refile-targets `((nil :regexp . "F"))))
  5537. (mapcar #'car (org-refile-get-targets))))))
  5538. ;; A nil `org-refile-targets' includes only top level headlines in
  5539. ;; current buffer.
  5540. (should
  5541. (equal '("H1" "H2")
  5542. (org-test-with-temp-text "* H1\n** S1\n* H2"
  5543. (let ((org-refile-use-outline-path nil)
  5544. (org-refile-targets nil))
  5545. (mapcar #'car (org-refile-get-targets))))))
  5546. ;; Return value is the union of the targets according to all the
  5547. ;; defined rules. However, prevent duplicates.
  5548. (should
  5549. (equal '("F2" "F3" "H1")
  5550. (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
  5551. (let ((org-refile-use-outline-path nil)
  5552. (org-refile-targets `((nil :regexp . "F")
  5553. (nil :todo . "TODO"))))
  5554. (mapcar #'car (org-refile-get-targets))))))
  5555. (should
  5556. (equal '("F2" "F3" "H1")
  5557. (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
  5558. (let ((org-refile-use-outline-path nil)
  5559. (org-refile-targets `((nil :regexp . "F")
  5560. (nil :todo . "TODO"))))
  5561. (mapcar #'car (org-refile-get-targets))))))
  5562. ;; When `org-refile-use-outline-path' is non-nil, provide targets as
  5563. ;; paths.
  5564. (should
  5565. (equal '("H1" "H1/H2" "H1/H2/H3")
  5566. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5567. (let ((org-refile-use-outline-path t)
  5568. (org-refile-targets `((nil :maxlevel . 3))))
  5569. (mapcar #'car (org-refile-get-targets))))))
  5570. ;; When providing targets as paths, escape forward slashes in
  5571. ;; headings with backslashes.
  5572. (should
  5573. (equal '("H1\\/foo")
  5574. (org-test-with-temp-text "* H1/foo"
  5575. (let ((org-refile-use-outline-path t)
  5576. (org-refile-targets `((nil :maxlevel . 1))))
  5577. (mapcar #'car (org-refile-get-targets))))))
  5578. ;; When `org-refile-use-outline-path' is `file', include file name
  5579. ;; without directory in targets.
  5580. (should
  5581. (org-test-with-temp-text-in-file "* H1"
  5582. (let* ((filename (buffer-file-name))
  5583. (org-refile-use-outline-path 'file)
  5584. (org-refile-targets `(((,filename) :level . 1))))
  5585. (member (file-name-nondirectory filename)
  5586. (mapcar #'car (org-refile-get-targets))))))
  5587. ;; When `org-refile-use-outline-path' is `full-file-path', include
  5588. ;; full file name.
  5589. (should
  5590. (org-test-with-temp-text-in-file "* H1"
  5591. (let* ((filename (file-truename (buffer-file-name)))
  5592. (org-refile-use-outline-path 'full-file-path)
  5593. (org-refile-targets `(((,filename) :level . 1))))
  5594. (member filename (mapcar #'car (org-refile-get-targets))))))
  5595. ;; When `org-refile-use-outline-path' is `buffer-name', include
  5596. ;; buffer name.
  5597. (should
  5598. (org-test-with-temp-text "* H1"
  5599. (let* ((org-refile-use-outline-path 'buffer-name)
  5600. (org-refile-targets `((nil :level . 1))))
  5601. (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
  5602. ;;; Sparse trees
  5603. (ert-deftest test-org/match-sparse-tree ()
  5604. "Test `org-match-sparse-tree' specifications."
  5605. ;; Match tags.
  5606. (should-not
  5607. (org-test-with-temp-text "* H\n** H1 :tag:"
  5608. (org-match-sparse-tree nil "tag")
  5609. (search-forward "H1")
  5610. (org-invisible-p2)))
  5611. (should
  5612. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  5613. (org-match-sparse-tree nil "tag")
  5614. (search-forward "H2")
  5615. (org-invisible-p2)))
  5616. ;; "-" operator for tags.
  5617. (should-not
  5618. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5619. (org-match-sparse-tree nil "tag1-tag2")
  5620. (search-forward "H1")
  5621. (org-invisible-p2)))
  5622. (should
  5623. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5624. (org-match-sparse-tree nil "tag1-tag2")
  5625. (search-forward "H2")
  5626. (org-invisible-p2)))
  5627. ;; "&" operator for tags.
  5628. (should
  5629. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5630. (org-match-sparse-tree nil "tag1&tag2")
  5631. (search-forward "H1")
  5632. (org-invisible-p2)))
  5633. (should-not
  5634. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5635. (org-match-sparse-tree nil "tag1&tag2")
  5636. (search-forward "H2")
  5637. (org-invisible-p2)))
  5638. ;; "|" operator for tags.
  5639. (should-not
  5640. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5641. (org-match-sparse-tree nil "tag1|tag2")
  5642. (search-forward "H1")
  5643. (org-invisible-p2)))
  5644. (should-not
  5645. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5646. (org-match-sparse-tree nil "tag1|tag2")
  5647. (search-forward "H2")
  5648. (org-invisible-p2)))
  5649. ;; Regexp match on tags.
  5650. (should-not
  5651. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5652. (org-match-sparse-tree nil "{^tag.*}")
  5653. (search-forward "H1")
  5654. (org-invisible-p2)))
  5655. (should
  5656. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5657. (org-match-sparse-tree nil "{^tag.*}")
  5658. (search-forward "H2")
  5659. (org-invisible-p2)))
  5660. ;; Match group tags.
  5661. (should-not
  5662. (org-test-with-temp-text
  5663. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5664. (org-match-sparse-tree nil "work")
  5665. (search-forward "H1")
  5666. (org-invisible-p2)))
  5667. (should-not
  5668. (org-test-with-temp-text
  5669. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5670. (org-match-sparse-tree nil "work")
  5671. (search-forward "H2")
  5672. (org-invisible-p2)))
  5673. ;; Match group tags with hard brackets.
  5674. (should-not
  5675. (org-test-with-temp-text
  5676. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5677. (org-match-sparse-tree nil "work")
  5678. (search-forward "H1")
  5679. (org-invisible-p2)))
  5680. (should-not
  5681. (org-test-with-temp-text
  5682. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5683. (org-match-sparse-tree nil "work")
  5684. (search-forward "H2")
  5685. (org-invisible-p2)))
  5686. ;; Match tags in hierarchies
  5687. (should-not
  5688. (org-test-with-temp-text
  5689. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5690. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5691. #+TAGS: { Lev_3 : Lev_4 }\n
  5692. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5693. (org-match-sparse-tree nil "Lev_1")
  5694. (search-forward "H4")
  5695. (org-invisible-p2)))
  5696. (should-not
  5697. (org-test-with-temp-text
  5698. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5699. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5700. #+TAGS: { Lev_3 : Lev_4 }\n
  5701. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5702. (org-match-sparse-tree nil "Lev_1+Lev_3")
  5703. (search-forward "H4")
  5704. (org-invisible-p2)))
  5705. ;; Match regular expressions in tags
  5706. (should-not
  5707. (org-test-with-temp-text
  5708. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  5709. (org-match-sparse-tree nil "Lev")
  5710. (search-forward "H1")
  5711. (org-invisible-p2)))
  5712. (should
  5713. (org-test-with-temp-text
  5714. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  5715. (org-match-sparse-tree nil "Lev")
  5716. (search-forward "H1")
  5717. (org-invisible-p2)))
  5718. ;; Match properties.
  5719. (should
  5720. (org-test-with-temp-text
  5721. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  5722. (org-match-sparse-tree nil "A=\"1\"")
  5723. (search-forward "H2")
  5724. (org-invisible-p2)))
  5725. (should-not
  5726. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5727. (org-match-sparse-tree nil "A=\"1\"")
  5728. (search-forward "H2")
  5729. (org-invisible-p2)))
  5730. ;; Case is not significant when matching properties.
  5731. (should-not
  5732. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5733. (org-match-sparse-tree nil "a=\"1\"")
  5734. (search-forward "H2")
  5735. (org-invisible-p2)))
  5736. (should-not
  5737. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  5738. (org-match-sparse-tree nil "A=\"1\"")
  5739. (search-forward "H2")
  5740. (org-invisible-p2)))
  5741. ;; Match special LEVEL property.
  5742. (should-not
  5743. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5744. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5745. (search-forward "H1")
  5746. (org-invisible-p2)))
  5747. (should
  5748. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5749. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5750. (search-forward "H2")
  5751. (org-invisible-p2)))
  5752. ;; Comparison operators when matching properties.
  5753. (should
  5754. (org-test-with-temp-text
  5755. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5756. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5757. (search-forward "H1")
  5758. (org-invisible-p2)))
  5759. (should-not
  5760. (org-test-with-temp-text
  5761. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5762. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5763. (search-forward "H2")
  5764. (org-invisible-p2)))
  5765. ;; Regexp match on properties values.
  5766. (should-not
  5767. (org-test-with-temp-text
  5768. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5769. (org-match-sparse-tree nil "A={f.*}")
  5770. (search-forward "H1")
  5771. (org-invisible-p2)))
  5772. (should
  5773. (org-test-with-temp-text
  5774. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5775. (org-match-sparse-tree nil "A={f.*}")
  5776. (search-forward "H2")
  5777. (org-invisible-p2)))
  5778. ;; With an optional argument, limit match to TODO entries.
  5779. (should-not
  5780. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5781. (org-match-sparse-tree t "tag")
  5782. (search-forward "H1")
  5783. (org-invisible-p2)))
  5784. (should
  5785. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5786. (org-match-sparse-tree t "tag")
  5787. (search-forward "H2")
  5788. (org-invisible-p2))))
  5789. (ert-deftest test-org/occur ()
  5790. "Test `org-occur' specifications."
  5791. ;; Count number of matches.
  5792. (should
  5793. (= 1
  5794. (org-test-with-temp-text "* H\nA\n* H2"
  5795. (org-occur "A"))))
  5796. (should
  5797. (= 2
  5798. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5799. (org-occur "A"))))
  5800. ;; Test CALLBACK optional argument.
  5801. (should
  5802. (= 0
  5803. (org-test-with-temp-text "* H\nA\n* H2"
  5804. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5805. (should
  5806. (= 1
  5807. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5808. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5809. ;; Case-fold searches according to `org-occur-case-fold-search'.
  5810. (should
  5811. (= 2
  5812. (org-test-with-temp-text "Aa"
  5813. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  5814. (should
  5815. (= 2
  5816. (org-test-with-temp-text "Aa"
  5817. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  5818. (should
  5819. (= 1
  5820. (org-test-with-temp-text "Aa"
  5821. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  5822. (should
  5823. (= 1
  5824. (org-test-with-temp-text "Aa"
  5825. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  5826. (should
  5827. (= 1
  5828. (org-test-with-temp-text "Aa"
  5829. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  5830. (should
  5831. (= 2
  5832. (org-test-with-temp-text "Aa"
  5833. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  5834. ;;; Tags
  5835. (ert-deftest test-org/tag-string-to-alist ()
  5836. "Test `org-tag-string-to-alist' specifications."
  5837. ;; Tag without selection key.
  5838. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  5839. ;; Tag with selection key.
  5840. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  5841. ;; Tag group.
  5842. (should
  5843. (equal
  5844. (org-tag-string-to-alist "[ group : t1 t2 ]")
  5845. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  5846. ;; Mutually exclusive tags.
  5847. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  5848. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  5849. (should
  5850. (equal
  5851. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  5852. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  5853. (ert-deftest test-org/tag-alist-to-string ()
  5854. "Test `org-tag-alist-to-string' specifications."
  5855. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  5856. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  5857. (should
  5858. (equal
  5859. (org-tag-alist-to-string
  5860. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5861. "[ group : t1 t2 ]"))
  5862. (should
  5863. (equal (org-tag-alist-to-string
  5864. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  5865. "{ tag1 tag2 }"))
  5866. (should
  5867. (equal
  5868. (org-tag-alist-to-string
  5869. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  5870. "{ group : tag1 tag2 }")))
  5871. (ert-deftest test-org/tag-alist-to-groups ()
  5872. "Test `org-tag-alist-to-groups' specifications."
  5873. (should
  5874. (equal (org-tag-alist-to-groups
  5875. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  5876. '(("group" "t1" "t2"))))
  5877. (should
  5878. (equal
  5879. (org-tag-alist-to-groups
  5880. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5881. '(("group" "t1" "t2"))))
  5882. (should-not
  5883. (org-tag-alist-to-groups
  5884. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  5885. (ert-deftest test-org/tag-align ()
  5886. "Test tags alignment."
  5887. ;; Test aligning tags with different display width.
  5888. (should
  5889. ;; 12345678901234567890
  5890. (equal "* Test :abc:"
  5891. (org-test-with-temp-text "* Test :abc:"
  5892. (let ((org-tags-column -20)
  5893. (indent-tabs-mode nil))
  5894. (org-fix-tags-on-the-fly))
  5895. (buffer-string))))
  5896. (should
  5897. ;; 12345678901234567890
  5898. (equal "* Test :日本語:"
  5899. (org-test-with-temp-text "* Test :日本語:"
  5900. (let ((org-tags-column -20)
  5901. (indent-tabs-mode nil))
  5902. (org-fix-tags-on-the-fly))
  5903. (buffer-string))))
  5904. ;; Make sure aligning tags do not skip invisible text.
  5905. (should
  5906. (equal "* [[linkx]] :tag:"
  5907. (org-test-with-temp-text "* [[link<point>]] :tag:"
  5908. (let ((org-tags-column 0))
  5909. (org-fix-tags-on-the-fly)
  5910. (insert "x")
  5911. (buffer-string)))))
  5912. ;; Aligning tags preserve position.
  5913. (should
  5914. (= 6 (org-test-with-temp-text "* 345 <point> :tag:"
  5915. (let ((org-tags-column 78)
  5916. (indent-tabs-mode nil))
  5917. (org-fix-tags-on-the-fly))
  5918. (current-column))))
  5919. ;; Aligning all tags in visible buffer.
  5920. (should
  5921. ;; 12345678901234567890
  5922. (equal (concat "* Level 1 :abc:\n"
  5923. "** Level 2 :def:")
  5924. (org-test-with-temp-text (concat "* Level 1 :abc:\n"
  5925. "** Level 2 :def:")
  5926. (let ((org-tags-column -20)
  5927. (indent-tabs-mode nil))
  5928. ;; (org-align-tags :all) must work even when the point
  5929. ;; is at the end of the buffer.
  5930. (goto-char (point-max))
  5931. (org-align-tags :all))
  5932. (buffer-string)))))
  5933. (ert-deftest test-org/get-tags ()
  5934. "Test `org-get-tags' specifications."
  5935. ;; Standard test.
  5936. (should
  5937. (equal '("foo")
  5938. (org-test-with-temp-text "* Test :foo:" (org-get-tags))))
  5939. (should
  5940. (equal '("foo" "bar")
  5941. (org-test-with-temp-text "* Test :foo:bar:" (org-get-tags))))
  5942. ;; Return nil when there is no tag.
  5943. (should-not
  5944. (org-test-with-temp-text "* Test" (org-get-tags)))
  5945. ;; Tags are inherited from parent headlines.
  5946. (should
  5947. (equal '("tag")
  5948. (let ((org-use-tag-inheritance t))
  5949. (org-test-with-temp-text "* H0 :foo:\n* H1 :tag:\n<point>** H2"
  5950. (org-get-tags)))))
  5951. ;; Tags are inherited from `org-file-tags'.
  5952. (should
  5953. (equal '("tag")
  5954. (org-test-with-temp-text "* H1"
  5955. (let ((org-file-tags '("tag"))
  5956. (org-use-tag-inheritance t))
  5957. (org-get-tags)))))
  5958. ;; Only inherited tags have the `inherited' text property.
  5959. (should
  5960. (get-text-property 0 'inherited
  5961. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5962. (let ((org-use-tag-inheritance t))
  5963. (assoc-string "foo" (org-get-tags))))))
  5964. (should-not
  5965. (get-text-property 0 'inherited
  5966. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5967. (let ((org-use-tag-inheritance t))
  5968. (assoc-string "bar" (org-get-tags))))))
  5969. ;; Obey to `org-use-tag-inheritance'.
  5970. (should-not
  5971. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5972. (let ((org-use-tag-inheritance nil))
  5973. (assoc-string "foo" (org-get-tags)))))
  5974. (should-not
  5975. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  5976. (let ((org-use-tag-inheritance nil)
  5977. (org-file-tags '("foo")))
  5978. (assoc-string "foo" (org-get-tags)))))
  5979. (should-not
  5980. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5981. (let ((org-use-tag-inheritance '("bar")))
  5982. (assoc-string "foo" (org-get-tags)))))
  5983. (should
  5984. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5985. (let ((org-use-tag-inheritance '("bar")))
  5986. (assoc-string "bar" (org-get-tags)))))
  5987. (should-not
  5988. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5989. (let ((org-use-tag-inheritance "b.*"))
  5990. (assoc-string "foo" (org-get-tags)))))
  5991. (should
  5992. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5993. (let ((org-use-tag-inheritance "b.*"))
  5994. (assoc-string "bar" (org-get-tags)))))
  5995. ;; When optional argument LOCAL is non-nil, ignore tag inheritance.
  5996. (should
  5997. (equal '("baz")
  5998. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  5999. (let ((org-use-tag-inheritance t))
  6000. (org-get-tags nil t)))))
  6001. ;; When optional argument POS is non-nil, get tags there instead.
  6002. (should
  6003. (equal '("foo")
  6004. (org-test-with-temp-text "* H1 :foo:\n* <point>H2 :bar:"
  6005. (org-get-tags 1))))
  6006. ;; Make sure tags excluded from inheritance are returned if local
  6007. (should
  6008. (equal '("foo")
  6009. (org-test-with-temp-text "* Test :foo:"
  6010. (let ((org-use-tag-inheritance t)
  6011. (org-tags-exclude-from-inheritance '("foo")))
  6012. (org-get-tags)))))
  6013. ;; Test the collection of tags from #+filetags and parent tags.
  6014. (should
  6015. (equal '("a" "b" "c" "d")
  6016. (org-test-with-temp-text (concat "#+filetags: a\n"
  6017. "* Level 1 :b:\n"
  6018. "** Level 2 :c:\n"
  6019. "*** Level 3 :d:\n"
  6020. "<point>")
  6021. (let ((org-use-tag-inheritance t))
  6022. (org-mode-restart) ;So that `org-file-tags' get populated from #+filetags
  6023. (org-get-tags)))))
  6024. ;; Pathological case: tagged headline with an empty body.
  6025. (should (org-test-with-temp-text "* :tag:" (org-get-tags))))
  6026. (ert-deftest test-org/set-tags ()
  6027. "Test `org-set-tags' specifications."
  6028. ;; Throw an error on invalid data.
  6029. (should-error
  6030. (org-test-with-temp-text "* H"
  6031. (org-set-tags 'foo)))
  6032. ;; `nil', an empty, and a blank string remove all tags.
  6033. (should
  6034. (equal "* H"
  6035. (org-test-with-temp-text "* H :tag1:tag2:"
  6036. (org-set-tags nil)
  6037. (buffer-string))))
  6038. (should
  6039. (equal "* H"
  6040. (org-test-with-temp-text "* H :tag1:tag2:"
  6041. (org-set-tags "")
  6042. (buffer-string))))
  6043. (should
  6044. (equal "* H"
  6045. (org-test-with-temp-text "* H :tag1:tag2:"
  6046. (org-set-tags " ")
  6047. (buffer-string))))
  6048. ;; If there's nothing to remove, just bail out.
  6049. (should
  6050. (equal "* H"
  6051. (org-test-with-temp-text "* H"
  6052. (org-set-tags nil)
  6053. (buffer-string))))
  6054. (should
  6055. (equal "* "
  6056. (org-test-with-temp-text "* "
  6057. (org-set-tags nil)
  6058. (buffer-string))))
  6059. ;; If DATA is a tag string, set current tags to it, even if it means
  6060. ;; replacing old tags.
  6061. (should
  6062. (equal "* H :tag0:"
  6063. (org-test-with-temp-text "* H :tag1:tag2:"
  6064. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  6065. (buffer-string))))
  6066. (should
  6067. (equal "* H :tag0:"
  6068. (org-test-with-temp-text "* H"
  6069. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  6070. (buffer-string))))
  6071. ;; If DATA is a list, set tags to this list, even if it means
  6072. ;; replacing old tags.
  6073. (should
  6074. (equal "* H :tag0:"
  6075. (org-test-with-temp-text "* H :tag1:tag2:"
  6076. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6077. (buffer-string))))
  6078. (should
  6079. (equal "* H :tag0:"
  6080. (org-test-with-temp-text "* H"
  6081. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6082. (buffer-string))))
  6083. ;; When set, apply `org-tags-sort-function'.
  6084. (should
  6085. (equal "* H :a:b:"
  6086. (org-test-with-temp-text "* H"
  6087. (let ((org-tags-column 1)
  6088. (org-tags-sort-function #'string<))
  6089. (org-set-tags '("b" "a"))
  6090. (buffer-string)))))
  6091. ;; When new tags are identical to the previous ones, still align.
  6092. (should
  6093. (equal "* H :foo:"
  6094. (org-test-with-temp-text "* H :foo:"
  6095. (let ((org-tags-column 1))
  6096. (org-set-tags '("foo"))
  6097. (buffer-string)))))
  6098. ;; When tags have been changed, run `org-after-tags-change-hook'.
  6099. (should
  6100. (catch :return
  6101. (org-test-with-temp-text "* H :foo:"
  6102. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6103. (org-set-tags '("bar"))
  6104. nil))))
  6105. (should-not
  6106. (catch :return
  6107. (org-test-with-temp-text "* H :foo:"
  6108. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6109. (org-set-tags '("foo"))
  6110. nil))))
  6111. ;; Special case: handle empty headlines.
  6112. (should
  6113. (equal "* :tag0:"
  6114. (org-test-with-temp-text "* "
  6115. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6116. (buffer-string))))
  6117. ;; Modify buffer only when a tag change happens or alignment is
  6118. ;; done.
  6119. (should-not
  6120. (org-test-with-temp-text "* H :foo:"
  6121. (set-buffer-modified-p nil)
  6122. (let ((org-tags-column 1)) (org-set-tags '("foo")))
  6123. (buffer-modified-p)))
  6124. (should
  6125. (org-test-with-temp-text "* H :foo:"
  6126. (set-buffer-modified-p nil)
  6127. (let ((org-tags-column 10)) (org-set-tags '("foo")))
  6128. (buffer-modified-p)))
  6129. (should
  6130. (org-test-with-temp-text "* H :foo:"
  6131. (set-buffer-modified-p nil)
  6132. (let ((org-tags-column 10)) (org-set-tags '("bar")))
  6133. (buffer-modified-p)))
  6134. ;; Pathological case: when setting tags of a folded headline, do not
  6135. ;; let new tags being sucked into invisibility.
  6136. (should-not
  6137. (org-test-with-temp-text "* H1\nContent\n* H2\n\n Other Content"
  6138. ;; Show only headlines
  6139. (org-content)
  6140. ;; Set NEXT tag on current entry
  6141. (org-set-tags ":NEXT:")
  6142. ;; Move point to that NEXT tag
  6143. (search-forward "NEXT") (backward-word)
  6144. ;; And it should be visible (i.e. no overlays)
  6145. (overlays-at (point)))))
  6146. (ert-deftest test-org/set-tags-command ()
  6147. "Test `org-set-tags-command' specifications"
  6148. ;; Set tags at current headline.
  6149. (should
  6150. (equal "* H1 :foo:"
  6151. (org-test-with-temp-text "* H1"
  6152. (cl-letf (((symbol-function 'completing-read)
  6153. (lambda (&rest args) ":foo:")))
  6154. (let ((org-use-fast-tag-selection nil)
  6155. (org-tags-column 1))
  6156. (org-set-tags-command)))
  6157. (buffer-string))))
  6158. ;; Preserve position when called from the section below.
  6159. (should
  6160. (equal "* H1 :foo:\nContents"
  6161. (org-test-with-temp-text "* H1\n<point>Contents"
  6162. (cl-letf (((symbol-function 'completing-read)
  6163. (lambda (&rest args) ":foo:")))
  6164. (let ((org-use-fast-tag-selection nil)
  6165. (org-tags-column 1))
  6166. (org-set-tags-command)))
  6167. (buffer-string))))
  6168. (should-not
  6169. (equal "* H1 :foo:\nContents2"
  6170. (org-test-with-temp-text "* H1\n<point>Contents2"
  6171. (cl-letf (((symbol-function 'completing-read)
  6172. (lambda (&rest args) ":foo:")))
  6173. (let ((org-use-fast-tag-selection nil)
  6174. (org-tags-column 1))
  6175. (org-set-tags-command)))
  6176. (org-at-heading-p))))
  6177. ;; Strip all forbidden characters from user-entered tags.
  6178. (should
  6179. (equal "* H1 :foo:"
  6180. (org-test-with-temp-text "* H1"
  6181. (cl-letf (((symbol-function 'completing-read)
  6182. (lambda (&rest args) ": foo *:")))
  6183. (let ((org-use-fast-tag-selection nil)
  6184. (org-tags-column 1))
  6185. (org-set-tags-command)))
  6186. (buffer-string))))
  6187. ;; When a region is active and
  6188. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  6189. ;; same value in all headlines in region.
  6190. (should
  6191. (equal "* H1 :foo:\nContents\n* H2 :foo:"
  6192. (org-test-with-temp-text "* H1\nContents\n* H2"
  6193. (cl-letf (((symbol-function 'completing-read)
  6194. (lambda (&rest args) ":foo:")))
  6195. (let ((org-use-fast-tag-selection nil)
  6196. (org-loop-over-headlines-in-active-region t)
  6197. (org-tags-column 1))
  6198. (transient-mark-mode 1)
  6199. (push-mark (point) t t)
  6200. (goto-char (point-max))
  6201. (org-set-tags-command)))
  6202. (buffer-string))))
  6203. (should
  6204. (equal "* H1\nContents\n* H2 :foo:"
  6205. (org-test-with-temp-text "* H1\nContents\n* H2"
  6206. (cl-letf (((symbol-function 'completing-read)
  6207. (lambda (&rest args) ":foo:")))
  6208. (let ((org-use-fast-tag-selection nil)
  6209. (org-loop-over-headlines-in-active-region nil)
  6210. (org-tags-column 1))
  6211. (transient-mark-mode 1)
  6212. (push-mark (point) t t)
  6213. (goto-char (point-max))
  6214. (org-set-tags-command)))
  6215. (buffer-string))))
  6216. ;; With a non-nil prefix argument, align all tags in the buffer.
  6217. (should
  6218. (equal "* H1 :foo:\n* H2 :bar:"
  6219. (org-test-with-temp-text "* H1 :foo:\n* H2 :bar:"
  6220. (let ((org-tags-column 1)) (org-set-tags-command t))
  6221. (buffer-string)))))
  6222. (ert-deftest test-org/toggle-tag ()
  6223. "Test `org-toggle-tag' specifications."
  6224. ;; Insert missing tag.
  6225. (should
  6226. (equal "* H :tag:"
  6227. (org-test-with-temp-text "* H"
  6228. (let ((org-tags-column 1)) (org-toggle-tag "tag"))
  6229. (buffer-string))))
  6230. (should
  6231. (equal "* H :tag1:tag2:"
  6232. (org-test-with-temp-text "* H :tag1:"
  6233. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6234. (buffer-string))))
  6235. ;; Remove existing tag.
  6236. (should
  6237. (equal "* H"
  6238. (org-test-with-temp-text "* H :tag:"
  6239. (org-toggle-tag "tag")
  6240. (buffer-string))))
  6241. (should
  6242. (equal "* H :tag1:"
  6243. (org-test-with-temp-text "* H :tag1:tag2:"
  6244. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6245. (buffer-string))))
  6246. (should
  6247. (equal "* H :tag2:"
  6248. (org-test-with-temp-text "* H :tag1:tag2:"
  6249. (let ((org-tags-column 1)) (org-toggle-tag "tag1"))
  6250. (buffer-string))))
  6251. ;; With optional argument ONOFF set to `on', try to insert the tag,
  6252. ;; even if its already there.
  6253. (should
  6254. (equal "* H :tag:"
  6255. (org-test-with-temp-text "* H"
  6256. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6257. (buffer-string))))
  6258. (should
  6259. (equal "* H :tag:"
  6260. (org-test-with-temp-text "* H :tag:"
  6261. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6262. (buffer-string))))
  6263. ;; With optional argument ONOFF set to `off', try to remove the tag,
  6264. ;; even if its not there.
  6265. (should
  6266. (equal "* H"
  6267. (org-test-with-temp-text "* H :tag:"
  6268. (org-toggle-tag "tag" 'off)
  6269. (buffer-string))))
  6270. (should
  6271. (equal "* H :tag:"
  6272. (org-test-with-temp-text "* H :tag:"
  6273. (let ((org-tags-column 1)) (org-toggle-tag "foo" 'off))
  6274. (buffer-string))))
  6275. ;; Special case: Handle properly tag inheritance. In particular, do
  6276. ;; not set inherited tags.
  6277. (should
  6278. (equal "* H1 :tag:\n** H2 :tag2:tag:"
  6279. (org-test-with-temp-text "* H1 :tag:\n** <point>H2 :tag2:"
  6280. (let ((org-use-tag-inheritance t)
  6281. (org-tags-column 1))
  6282. (org-toggle-tag "tag"))
  6283. (buffer-string))))
  6284. (should
  6285. (equal "* H1 :tag1:tag2:\n** H2 :foo:"
  6286. (org-test-with-temp-text "* H1 :tag1:tag2:\n** <point>H2"
  6287. (let ((org-use-tag-inheritance t)
  6288. (org-tags-column 1))
  6289. (org-toggle-tag "foo"))
  6290. (buffer-string)))))
  6291. (ert-deftest test-org/tags-expand ()
  6292. "Test `org-tags-expand' specifications."
  6293. ;; Expand tag groups as a regexp enclosed withing curly brackets.
  6294. (should
  6295. (equal "{\\<[ABC]\\>}"
  6296. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6297. (org-mode-restart)
  6298. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  6299. (should
  6300. (equal "{\\<\\(?:Aa\\|Bb\\|Cc\\)\\>}"
  6301. (org-test-with-temp-text "#+TAGS: [ Aa : Bb Cc ]"
  6302. (org-mode-restart)
  6303. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "Aa")))))
  6304. ;; Preserve operator before the regexp.
  6305. (should
  6306. (equal "+{\\<[ABC]\\>}"
  6307. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6308. (org-mode-restart)
  6309. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "+A")))))
  6310. (should
  6311. (equal "-{\\<[ABC]\\>}"
  6312. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6313. (org-mode-restart)
  6314. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "-A")))))
  6315. ;; Handle "|" syntax.
  6316. (should
  6317. (equal "{\\<[ABC]\\>}|D"
  6318. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6319. (org-mode-restart)
  6320. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|D")))))
  6321. ;; Handle nested groups.
  6322. (should
  6323. (equal "{\\<[A-D]\\>}"
  6324. (org-test-with-temp-text "#+TAGS: [ A : B C ]\n#+TAGS: [ B : D ]"
  6325. (org-mode-restart)
  6326. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  6327. ;; Expand multiple occurrences of the same group.
  6328. (should
  6329. (equal "{\\<[ABC]\\>}|{\\<[ABC]\\>}"
  6330. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6331. (org-mode-restart)
  6332. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|A")))))
  6333. ;; Preserve regexp matches.
  6334. (should
  6335. (equal "{A+}"
  6336. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6337. (org-mode-restart)
  6338. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "{A+}"))))))
  6339. ;;; TODO keywords
  6340. (ert-deftest test-org/auto-repeat-maybe ()
  6341. "Test `org-auto-repeat-maybe' specifications."
  6342. ;; Do not auto repeat when there is no valid time stamp with
  6343. ;; a repeater in the entry.
  6344. (should-not
  6345. (string-prefix-p
  6346. "* TODO H"
  6347. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6348. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  6349. (org-todo "DONE")
  6350. (buffer-string)))))
  6351. (should-not
  6352. (string-prefix-p
  6353. "* TODO H"
  6354. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6355. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  6356. (org-todo "DONE")
  6357. (buffer-string)))))
  6358. ;; When switching to DONE state, switch back to first TODO keyword
  6359. ;; in sequence, or the same keyword if they have different types.
  6360. (should
  6361. (string-prefix-p
  6362. "* TODO H"
  6363. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6364. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6365. (org-todo "DONE")
  6366. (buffer-string)))))
  6367. (should
  6368. (string-prefix-p
  6369. "* KWD1 H"
  6370. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6371. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6372. (org-todo "DONE")
  6373. (buffer-string)))))
  6374. (should
  6375. (string-prefix-p
  6376. "* KWD2 H"
  6377. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  6378. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6379. (org-todo "DONE")
  6380. (buffer-string)))))
  6381. ;; If there was no TODO keyword in the first place, do not insert
  6382. ;; any either.
  6383. (should
  6384. (string-prefix-p
  6385. "* H"
  6386. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6387. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  6388. (org-todo "DONE")
  6389. (buffer-string)))))
  6390. ;; Revert to REPEAT_TO_STATE, if set.
  6391. (should
  6392. (string-prefix-p
  6393. "* KWD2 H"
  6394. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6395. (org-test-with-temp-text
  6396. "* KWD2 H
  6397. :PROPERTIES:
  6398. :REPEAT_TO_STATE: KWD2
  6399. :END:
  6400. <2012-03-29 Thu +2y>"
  6401. (org-todo "DONE")
  6402. (buffer-string)))))
  6403. ;; When switching to DONE state, update base date. If there are
  6404. ;; multiple repeated time stamps, update them all.
  6405. (should
  6406. (string-match-p
  6407. "<2014-03-29 .* \\+2y>"
  6408. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6409. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6410. (org-todo "DONE")
  6411. (buffer-string)))))
  6412. (should
  6413. (string-match-p
  6414. "<2015-03-04 .* \\+1y>"
  6415. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6416. (org-test-with-temp-text
  6417. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  6418. (org-todo "DONE")
  6419. (buffer-string)))))
  6420. ;; Throw an error if repeater unit is the hour and no time is
  6421. ;; provided in the time-stamp.
  6422. (should-error
  6423. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6424. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  6425. (org-todo "DONE")
  6426. (buffer-string))))
  6427. ;; Do not repeat inactive time stamps with a repeater.
  6428. (should-not
  6429. (string-match-p
  6430. "\\[2014-03-29 .* \\+2y\\]"
  6431. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6432. (org-test-with-temp-text
  6433. "* TODO H\n[2012-03-29 Thu. +2y]"
  6434. (org-todo "DONE")
  6435. (buffer-string)))))
  6436. ;; Do not repeat commented time stamps.
  6437. (should-not
  6438. (string-prefix-p
  6439. "<2015-03-04 .* \\+1y>"
  6440. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6441. (org-test-with-temp-text
  6442. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  6443. (org-todo "DONE")
  6444. (buffer-string)))))
  6445. (should-not
  6446. (string-prefix-p
  6447. "<2015-03-04 .* \\+1y>"
  6448. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6449. (org-test-with-temp-text
  6450. "* TODO H
  6451. <2012-03-29 Thu. +2y>
  6452. #+BEGIN_EXAMPLE
  6453. <2014-03-04 Tue +1y>
  6454. #+END_EXAMPLE"
  6455. (org-todo "DONE")
  6456. (buffer-string)))))
  6457. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  6458. ;; entry, record time of last repeat.
  6459. (should-not
  6460. (string-match-p
  6461. ":LAST_REPEAT:"
  6462. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6463. (org-log-repeat nil))
  6464. (cl-letf (((symbol-function 'org-add-log-setup)
  6465. (lambda (&rest args) nil)))
  6466. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6467. (org-todo "DONE")
  6468. (buffer-string))))))
  6469. (should
  6470. (string-match-p
  6471. ":LAST_REPEAT:"
  6472. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6473. (org-log-repeat t))
  6474. (cl-letf (((symbol-function 'org-add-log-setup)
  6475. (lambda (&rest args) nil)))
  6476. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6477. (org-todo "DONE")
  6478. (buffer-string))))))
  6479. (should
  6480. (string-match-p
  6481. ":LAST_REPEAT:"
  6482. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6483. (cl-letf (((symbol-function 'org-add-log-setup)
  6484. (lambda (&rest args) nil)))
  6485. (org-test-with-temp-text
  6486. "* TODO H\n<2012-03-29 Thu +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  6487. (org-todo "DONE")
  6488. (buffer-string))))))
  6489. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  6490. ;; the entry as it is no longer relevant.
  6491. (should-not
  6492. (string-match-p
  6493. "^SCHEDULED:"
  6494. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6495. (org-test-with-temp-text
  6496. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  6497. (org-todo "DONE")
  6498. (buffer-string)))))
  6499. ;; Properly advance repeater even when a clock entry is specified
  6500. ;; and `org-log-repeat' is nil.
  6501. (should
  6502. (string-match-p
  6503. "SCHEDULED: <2014-03-29"
  6504. (let ((org-log-repeat nil)
  6505. (org-todo-keywords '((sequence "TODO" "DONE"))))
  6506. (org-test-with-temp-text
  6507. "* TODO H
  6508. SCHEDULED: <2012-03-29 Thu +2y>
  6509. CLOCK: [2012-03-29 Thu 10:00]--[2012-03-29 Thu 16:40] => 6:40"
  6510. (org-todo "DONE")
  6511. (buffer-string))))))
  6512. ;;; Timestamps API
  6513. (ert-deftest test-org/at-timestamp-p ()
  6514. "Test `org-at-timestamp-p' specifications."
  6515. (should
  6516. (org-test-with-temp-text "<2012-03-29 Thu>"
  6517. (org-at-timestamp-p)))
  6518. (should-not
  6519. (org-test-with-temp-text "2012-03-29 Thu"
  6520. (org-at-timestamp-p)))
  6521. ;; Test return values.
  6522. (should
  6523. (eq 'bracket
  6524. (org-test-with-temp-text "<2012-03-29 Thu>"
  6525. (org-at-timestamp-p))))
  6526. (should
  6527. (eq 'year
  6528. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  6529. (org-at-timestamp-p))))
  6530. (should
  6531. (eq 'month
  6532. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  6533. (org-at-timestamp-p))))
  6534. (should
  6535. (eq 'day
  6536. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  6537. (org-at-timestamp-p))))
  6538. (should
  6539. (eq 'day
  6540. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  6541. (org-at-timestamp-p))))
  6542. (should
  6543. (wholenump
  6544. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  6545. (org-at-timestamp-p))))
  6546. (should
  6547. (eq 'bracket
  6548. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  6549. (org-at-timestamp-p))))
  6550. (should
  6551. (eq 'after
  6552. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  6553. (org-at-timestamp-p))))
  6554. ;; Test `inactive' optional argument.
  6555. (should
  6556. (org-test-with-temp-text "[2012-03-29 Thu]"
  6557. (org-at-timestamp-p 'inactive)))
  6558. (should-not
  6559. (org-test-with-temp-text "[2012-03-29 Thu]"
  6560. (org-at-timestamp-p)))
  6561. ;; When optional argument is `agenda', recognize time-stamps in
  6562. ;; planning info line, property drawers and clocks.
  6563. (should
  6564. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6565. (org-at-timestamp-p 'agenda)))
  6566. (should-not
  6567. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6568. (org-at-timestamp-p)))
  6569. (should
  6570. (org-test-with-temp-text
  6571. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6572. (org-at-timestamp-p 'agenda)))
  6573. (should-not
  6574. (org-test-with-temp-text
  6575. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6576. (org-at-timestamp-p)))
  6577. (should
  6578. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6579. (let ((org-agenda-include-inactive-timestamps t))
  6580. (org-at-timestamp-p 'agenda))))
  6581. (should-not
  6582. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6583. (let ((org-agenda-include-inactive-timestamps t))
  6584. (org-at-timestamp-p))))
  6585. (should-not
  6586. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6587. (let ((org-agenda-include-inactive-timestamps t))
  6588. (org-at-timestamp-p 'inactive))))
  6589. ;; When optional argument is `lax', match any part of the document
  6590. ;; with Org timestamp syntax.
  6591. (should
  6592. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6593. (org-at-timestamp-p 'lax)))
  6594. (should-not
  6595. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6596. (org-at-timestamp-p)))
  6597. (should
  6598. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6599. (org-at-timestamp-p 'lax)))
  6600. (should-not
  6601. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6602. (org-at-timestamp-p)))
  6603. (should
  6604. (org-test-with-temp-text
  6605. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6606. (org-at-timestamp-p 'lax)))
  6607. (should-not
  6608. (org-test-with-temp-text
  6609. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6610. (org-at-timestamp-p)))
  6611. ;; Optional argument `lax' also matches inactive timestamps.
  6612. (should
  6613. (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
  6614. (org-at-timestamp-p 'lax))))
  6615. (ert-deftest test-org/time-stamp ()
  6616. "Test `org-time-stamp' specifications."
  6617. ;; Insert chosen time stamp at point.
  6618. (should
  6619. (string-match
  6620. "Te<2014-03-04 .*?>xt"
  6621. (org-test-with-temp-text "Te<point>xt"
  6622. (cl-letf (((symbol-function 'org-read-date)
  6623. (lambda (&rest args)
  6624. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6625. (org-time-stamp nil)
  6626. (buffer-string)))))
  6627. ;; With a prefix argument, also insert time.
  6628. (should
  6629. (string-match
  6630. "Te<2014-03-04 .*? 00:41>xt"
  6631. (org-test-with-temp-text "Te<point>xt"
  6632. (cl-letf (((symbol-function 'org-read-date)
  6633. (lambda (&rest args)
  6634. (apply #'encode-time
  6635. (org-parse-time-string "2014-03-04 00:41")))))
  6636. (org-time-stamp '(4))
  6637. (buffer-string)))))
  6638. ;; With two universal prefix arguments, insert an active timestamp
  6639. ;; with the current time without prompting the user.
  6640. (should
  6641. (string-match
  6642. "Te<2014-03-04 .*? 00:41>xt"
  6643. (org-test-with-temp-text "Te<point>xt"
  6644. (org-test-at-time "2014-03-04 00:41"
  6645. (org-time-stamp '(16))
  6646. (buffer-string)))))
  6647. ;; When optional argument is non-nil, insert an inactive timestamp.
  6648. (should
  6649. (string-match
  6650. "Te\\[2014-03-04 .*?\\]xt"
  6651. (org-test-with-temp-text "Te<point>xt"
  6652. (cl-letf (((symbol-function 'org-read-date)
  6653. (lambda (&rest args)
  6654. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6655. (org-time-stamp nil t)
  6656. (buffer-string)))))
  6657. ;; When called from a timestamp, replace existing one.
  6658. (should
  6659. (string-match
  6660. "<2014-03-04 .*?>"
  6661. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  6662. (cl-letf (((symbol-function 'org-read-date)
  6663. (lambda (&rest args)
  6664. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6665. (org-time-stamp nil)
  6666. (buffer-string)))))
  6667. (should
  6668. (string-match
  6669. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  6670. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  6671. (cl-letf (((symbol-function 'org-read-date)
  6672. (lambda (&rest args)
  6673. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6674. (org-time-stamp nil)
  6675. (buffer-string)))))
  6676. ;; When replacing a timestamp, preserve repeater, if any.
  6677. (should
  6678. (string-match
  6679. "<2014-03-04 .*? \\+2y>"
  6680. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  6681. (cl-letf (((symbol-function 'org-read-date)
  6682. (lambda (&rest args)
  6683. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6684. (org-time-stamp nil)
  6685. (buffer-string)))))
  6686. ;; When called twice in a raw, build a date range.
  6687. (should
  6688. (string-match
  6689. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  6690. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  6691. (cl-letf (((symbol-function 'org-read-date)
  6692. (lambda (&rest args)
  6693. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6694. (let ((last-command 'org-time-stamp)
  6695. (this-command 'org-time-stamp))
  6696. (org-time-stamp nil))
  6697. (buffer-string))))))
  6698. (ert-deftest test-org/timestamp-has-time-p ()
  6699. "Test `org-timestamp-has-time-p' specifications."
  6700. ;; With time.
  6701. (should
  6702. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6703. (org-timestamp-has-time-p (org-element-context))))
  6704. ;; Without time.
  6705. (should-not
  6706. (org-test-with-temp-text "<2012-03-29 Thu>"
  6707. (org-timestamp-has-time-p (org-element-context)))))
  6708. (ert-deftest test-org/get-repeat ()
  6709. "Test `org-get-repeat' specifications."
  6710. (should
  6711. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  6712. (org-get-repeat)))
  6713. (should-not
  6714. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  6715. (org-get-repeat)))
  6716. ;; Return proper repeat string.
  6717. (should
  6718. (equal "+2y"
  6719. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  6720. (org-get-repeat))))
  6721. ;; Prevent false positive (commented or verbatim time stamps)
  6722. (should-not
  6723. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  6724. (org-get-repeat)))
  6725. (should-not
  6726. (org-test-with-temp-text
  6727. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  6728. (org-get-repeat)))
  6729. ;; Return nil when called before first heading.
  6730. (should-not
  6731. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  6732. (org-get-repeat)))
  6733. ;; When called with an optional argument, extract repeater from that
  6734. ;; string instead.
  6735. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  6736. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  6737. (ert-deftest test-org/timestamp-format ()
  6738. "Test `org-timestamp-format' specifications."
  6739. ;; Regular test.
  6740. (should
  6741. (equal
  6742. "2012-03-29 16:40"
  6743. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6744. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  6745. ;; Range end.
  6746. (should
  6747. (equal
  6748. "2012-03-29"
  6749. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  6750. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  6751. (ert-deftest test-org/timestamp-split-range ()
  6752. "Test `org-timestamp-split-range' specifications."
  6753. ;; Extract range start (active).
  6754. (should
  6755. (equal '(2012 3 29)
  6756. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6757. (let ((ts (org-timestamp-split-range (org-element-context))))
  6758. (mapcar (lambda (p) (org-element-property p ts))
  6759. '(:year-end :month-end :day-end))))))
  6760. ;; Extract range start (inactive)
  6761. (should
  6762. (equal '(2012 3 29)
  6763. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6764. (let ((ts (org-timestamp-split-range (org-element-context))))
  6765. (mapcar (lambda (p) (org-element-property p ts))
  6766. '(:year-end :month-end :day-end))))))
  6767. ;; Extract range end (active).
  6768. (should
  6769. (equal '(2012 3 30)
  6770. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6771. (let ((ts (org-timestamp-split-range
  6772. (org-element-context) t)))
  6773. (mapcar (lambda (p) (org-element-property p ts))
  6774. '(:year-end :month-end :day-end))))))
  6775. ;; Extract range end (inactive)
  6776. (should
  6777. (equal '(2012 3 30)
  6778. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6779. (let ((ts (org-timestamp-split-range
  6780. (org-element-context) t)))
  6781. (mapcar (lambda (p) (org-element-property p ts))
  6782. '(:year-end :month-end :day-end))))))
  6783. ;; Return the timestamp if not a range.
  6784. (should
  6785. (org-test-with-temp-text "[2012-03-29 Thu]"
  6786. (let* ((ts-orig (org-element-context))
  6787. (ts-copy (org-timestamp-split-range ts-orig)))
  6788. (eq ts-orig ts-copy))))
  6789. (should
  6790. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6791. (let* ((ts-orig (org-element-context))
  6792. (ts-copy (org-timestamp-split-range ts-orig)))
  6793. (eq ts-orig ts-copy)))))
  6794. (ert-deftest test-org/timestamp-translate ()
  6795. "Test `org-timestamp-translate' specifications."
  6796. ;; Translate whole date range.
  6797. (should
  6798. (equal "<29>--<30>"
  6799. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6800. (let ((org-display-custom-times t)
  6801. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6802. (org-timestamp-translate (org-element-context))))))
  6803. ;; Translate date range start.
  6804. (should
  6805. (equal "<29>"
  6806. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6807. (let ((org-display-custom-times t)
  6808. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6809. (org-timestamp-translate (org-element-context) 'start)))))
  6810. ;; Translate date range end.
  6811. (should
  6812. (equal "<30>"
  6813. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6814. (let ((org-display-custom-times t)
  6815. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6816. (org-timestamp-translate (org-element-context) 'end)))))
  6817. ;; Translate time range.
  6818. (should
  6819. (equal "<08>--<16>"
  6820. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  6821. (let ((org-display-custom-times t)
  6822. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  6823. (org-timestamp-translate (org-element-context))))))
  6824. ;; Translate non-range timestamp.
  6825. (should
  6826. (equal "<29>"
  6827. (org-test-with-temp-text "<2012-03-29 Thu>"
  6828. (let ((org-display-custom-times t)
  6829. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6830. (org-timestamp-translate (org-element-context))))))
  6831. ;; Do not change `diary' timestamps.
  6832. (should
  6833. (equal "<%%(org-float t 4 2)>"
  6834. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6835. (let ((org-display-custom-times t)
  6836. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6837. (org-timestamp-translate (org-element-context)))))))
  6838. (ert-deftest test-org/timestamp-from-string ()
  6839. "Test `org-timestamp-from-string' specifications."
  6840. ;; Return nil if argument is not a valid Org timestamp.
  6841. (should-not (org-timestamp-from-string ""))
  6842. (should-not (org-timestamp-from-string nil))
  6843. (should-not (org-timestamp-from-string "<2012-03-29"))
  6844. ;; Otherwise, return a valid Org timestamp object.
  6845. (should
  6846. (equal "<2012-03-29 Thu>"
  6847. (let ((system-time-locale "en_US"))
  6848. (org-element-interpret-data
  6849. (org-timestamp-from-string "<2012-03-29 Thu>")))))
  6850. (should
  6851. (equal "[2014-03-04 Tue]"
  6852. (let ((system-time-locale "en_US"))
  6853. (org-element-interpret-data
  6854. (org-timestamp-from-string "[2014-03-04 Tue]"))))))
  6855. (ert-deftest test-org/timestamp-from-time ()
  6856. "Test `org-timestamp-from-time' specifications."
  6857. ;; Standard test.
  6858. (should
  6859. (equal "<2012-03-29 Thu>"
  6860. (let ((system-time-locale "en_US"))
  6861. (org-element-interpret-data
  6862. (org-timestamp-from-time
  6863. (apply #'encode-time
  6864. (org-parse-time-string "<2012-03-29 Thu 16:40>")))))))
  6865. ;; When optional argument WITH-TIME is non-nil, provide time
  6866. ;; information.
  6867. (should
  6868. (equal "<2012-03-29 Thu 16:40>"
  6869. (let ((system-time-locale "en_US"))
  6870. (org-element-interpret-data
  6871. (org-timestamp-from-time
  6872. (apply #'encode-time
  6873. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  6874. t)))))
  6875. ;; When optional argument INACTIVE is non-nil, return an inactive
  6876. ;; timestamp.
  6877. (should
  6878. (equal "[2012-03-29 Thu]"
  6879. (let ((system-time-locale "en_US"))
  6880. (org-element-interpret-data
  6881. (org-timestamp-from-time
  6882. (apply #'encode-time
  6883. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  6884. nil t))))))
  6885. (ert-deftest test-org/timestamp-to-time ()
  6886. "Test `org-timestamp-to-time' specifications."
  6887. (should
  6888. (equal "2014-03-04"
  6889. (format-time-string
  6890. "%Y-%m-%d"
  6891. (org-timestamp-to-time
  6892. (org-timestamp-from-string "<2014-03-04 Tue>")))))
  6893. (should
  6894. (equal "2014-03-04"
  6895. (format-time-string
  6896. "%Y-%m-%d"
  6897. (org-timestamp-to-time
  6898. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  6899. (should
  6900. (equal "2012-03-29 08:30"
  6901. (format-time-string
  6902. "%Y-%m-%d %H:%M"
  6903. (org-timestamp-to-time
  6904. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")))))
  6905. (should
  6906. (equal "2012-03-29"
  6907. (format-time-string
  6908. "%Y-%m-%d"
  6909. (org-timestamp-to-time
  6910. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")))))
  6911. (should
  6912. (equal "2012-03-29"
  6913. (format-time-string
  6914. "%Y-%m-%d"
  6915. (org-timestamp-to-time
  6916. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")))))
  6917. ;; When optional argument END is non-nil, use end of date range or
  6918. ;; time range.
  6919. (should
  6920. (equal "2012-03-29 16:40"
  6921. (format-time-string
  6922. "%Y-%m-%d %H:%M"
  6923. (org-timestamp-to-time
  6924. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")
  6925. t))))
  6926. (should
  6927. (equal "2014-03-04"
  6928. (format-time-string
  6929. "%Y-%m-%d"
  6930. (org-timestamp-to-time
  6931. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")
  6932. t))))
  6933. (should
  6934. (equal "2014-03-04"
  6935. (format-time-string
  6936. "%Y-%m-%d"
  6937. (org-timestamp-to-time
  6938. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")
  6939. t)))))
  6940. ;;; Visibility
  6941. (ert-deftest test-org/flag-drawer ()
  6942. "Test `org-flag-drawer' specifications."
  6943. ;; Hide drawer.
  6944. (should
  6945. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6946. (org-flag-drawer t)
  6947. (get-char-property (line-end-position) 'invisible)))
  6948. ;; Show drawer.
  6949. (should-not
  6950. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6951. (org-flag-drawer t)
  6952. (org-flag-drawer nil)
  6953. (get-char-property (line-end-position) 'invisible)))
  6954. ;; Test optional argument.
  6955. (should
  6956. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6957. (let ((drawer (save-excursion (search-forward ":D2")
  6958. (org-element-at-point))))
  6959. (org-flag-drawer t drawer)
  6960. (get-char-property (progn (search-forward ":D2") (line-end-position))
  6961. 'invisible))))
  6962. (should-not
  6963. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6964. (let ((drawer (save-excursion (search-forward ":D2")
  6965. (org-element-at-point))))
  6966. (org-flag-drawer t drawer)
  6967. (get-char-property (line-end-position) 'invisible))))
  6968. ;; Do not hide fake drawers.
  6969. (should-not
  6970. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  6971. (forward-line 1)
  6972. (org-flag-drawer t)
  6973. (get-char-property (line-end-position) 'invisible)))
  6974. ;; Do not hide incomplete drawers.
  6975. (should-not
  6976. (org-test-with-temp-text ":D:\nparagraph"
  6977. (forward-line 1)
  6978. (org-flag-drawer t)
  6979. (get-char-property (line-end-position) 'invisible)))
  6980. ;; Do not hide drawers when called from final blank lines.
  6981. (should-not
  6982. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  6983. (goto-char (point-max))
  6984. (org-flag-drawer t)
  6985. (goto-char (point-min))
  6986. (get-char-property (line-end-position) 'invisible)))
  6987. ;; Don't leave point in an invisible part of the buffer when hiding
  6988. ;; a drawer away.
  6989. (should-not
  6990. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6991. (goto-char (point-max))
  6992. (org-flag-drawer t)
  6993. (get-char-property (point) 'invisible))))
  6994. (ert-deftest test-org/hide-block-toggle ()
  6995. "Test `org-hide-block-toggle' specifications."
  6996. ;; Error when not at a block.
  6997. (should-error
  6998. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  6999. (org-hide-block-toggle 'off)
  7000. (get-char-property (line-end-position) 'invisible)))
  7001. ;; Hide block.
  7002. (should
  7003. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  7004. (org-hide-block-toggle)
  7005. (get-char-property (line-end-position) 'invisible)))
  7006. (should
  7007. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  7008. (org-hide-block-toggle)
  7009. (get-char-property (line-end-position) 'invisible)))
  7010. ;; Show block unconditionally when optional argument is `off'.
  7011. (should-not
  7012. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7013. (org-hide-block-toggle)
  7014. (org-hide-block-toggle 'off)
  7015. (get-char-property (line-end-position) 'invisible)))
  7016. (should-not
  7017. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7018. (org-hide-block-toggle 'off)
  7019. (get-char-property (line-end-position) 'invisible)))
  7020. ;; Hide block unconditionally when optional argument is non-nil.
  7021. (should
  7022. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7023. (org-hide-block-toggle t)
  7024. (get-char-property (line-end-position) 'invisible)))
  7025. (should
  7026. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7027. (org-hide-block-toggle)
  7028. (org-hide-block-toggle t)
  7029. (get-char-property (line-end-position) 'invisible)))
  7030. ;; Do not hide block when called from final blank lines.
  7031. (should-not
  7032. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  7033. (org-hide-block-toggle)
  7034. (goto-char (point-min))
  7035. (get-char-property (line-end-position) 'invisible)))
  7036. ;; Don't leave point in an invisible part of the buffer when hiding
  7037. ;; a block away.
  7038. (should-not
  7039. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  7040. (org-hide-block-toggle)
  7041. (get-char-property (point) 'invisible))))
  7042. (ert-deftest test-org/hide-block-toggle-maybe ()
  7043. "Test `org-hide-block-toggle-maybe' specifications."
  7044. (should
  7045. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  7046. (org-hide-block-toggle-maybe)))
  7047. (should-not
  7048. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  7049. (ert-deftest test-org/show-set-visibility ()
  7050. "Test `org-show-set-visibility' specifications."
  7051. ;; Do not throw an error before first heading.
  7052. (should
  7053. (org-test-with-temp-text "Preamble\n* Headline"
  7054. (org-show-set-visibility 'tree)
  7055. t))
  7056. ;; Test all visibility spans, both on headline and in entry.
  7057. (let ((list-visible-lines
  7058. (lambda (state headerp)
  7059. (org-test-with-temp-text "* Grandmother (0)
  7060. ** Uncle (1)
  7061. *** Heir (2)
  7062. ** Father (3)
  7063. Ancestor text (4)
  7064. *** Sister (5)
  7065. Sibling text (6)
  7066. *** Self (7)
  7067. Match (8)
  7068. **** First born (9)
  7069. Child text (10)
  7070. **** The other child (11)
  7071. *** Brother (12)
  7072. ** Aunt (13)
  7073. "
  7074. (org-cycle t)
  7075. (search-forward (if headerp "Self" "Match"))
  7076. (org-show-set-visibility state)
  7077. (goto-char (point-min))
  7078. (let (result (line 0))
  7079. (while (not (eobp))
  7080. (unless (org-invisible-p2) (push line result))
  7081. (cl-incf line)
  7082. (forward-line))
  7083. (nreverse result))))))
  7084. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  7085. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  7086. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  7087. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  7088. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  7089. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  7090. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  7091. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  7092. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  7093. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  7094. (funcall list-visible-lines 'tree nil)))
  7095. (should (equal '(0 1 3 4 5 7 12 13)
  7096. (funcall list-visible-lines 'canonical t)))
  7097. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  7098. (funcall list-visible-lines 'canonical nil))))
  7099. ;; When point is hidden in a drawer or a block, make sure to make it
  7100. ;; visible.
  7101. (should-not
  7102. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  7103. (org-hide-block-toggle)
  7104. (search-forward "Text")
  7105. (org-show-set-visibility 'minimal)
  7106. (org-invisible-p2)))
  7107. (should-not
  7108. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  7109. (org-flag-drawer t)
  7110. (search-forward "Text")
  7111. (org-show-set-visibility 'minimal)
  7112. (org-invisible-p2)))
  7113. (should-not
  7114. (org-test-with-temp-text
  7115. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  7116. (org-flag-drawer t)
  7117. (forward-line -1)
  7118. (org-hide-block-toggle)
  7119. (search-forward "Text")
  7120. (org-show-set-visibility 'minimal)
  7121. (org-invisible-p2))))
  7122. (defun test-org/copy-visible ()
  7123. "Test `org-copy-visible' specifications."
  7124. (should
  7125. (equal "Foo"
  7126. (org-test-with-temp-text "Foo"
  7127. (let ((kill-ring nil))
  7128. (org-copy-visible (point-min) (point-max))
  7129. (current-kill 0 t)))))
  7130. ;; Skip invisible characters by text property.
  7131. (should
  7132. (equal "Foo"
  7133. (org-test-with-temp-text #("F<hidden>oo" 1 7 (invisible t))
  7134. (let ((kill-ring nil))
  7135. (org-copy-visible (point-min) (point-max))
  7136. (current-kill 0 t)))))
  7137. ;; Skip invisible characters by overlay.
  7138. (should
  7139. (equal "Foo"
  7140. (org-test-with-temp-text "F<hidden>oo"
  7141. (let ((o (make-overlay 2 10)))
  7142. (overlay-put o 'invisible t))
  7143. (let ((kill-ring nil))
  7144. (org-copy-visible (point-min) (point-max))
  7145. (current-kill 0 t)))))
  7146. ;; Handle invisible characters at the beginning and the end of the
  7147. ;; buffer.
  7148. (should
  7149. (equal "Foo"
  7150. (org-test-with-temp-text #("<hidden>Foo" 0 8 (invisible t))
  7151. (let ((kill-ring nil))
  7152. (org-copy-visible (point-min) (point-max))
  7153. (current-kill 0 t)))))
  7154. (should
  7155. (equal "Foo"
  7156. (org-test-with-temp-text #("Foo<hidden>" 3 11 (invisible t))
  7157. (let ((kill-ring nil))
  7158. (org-copy-visible (point-min) (point-max))
  7159. (current-kill 0 t)))))
  7160. ;; Handle multiple visible parts.
  7161. (should
  7162. (equal "abc"
  7163. (org-test-with-temp-text
  7164. #("aXbXc" 1 2 (invisible t) 3 4 (invisible t))
  7165. (let ((kill-ring nil))
  7166. (org-copy-visible (point-min) (point-max))
  7167. (current-kill 0 t))))))
  7168. (ert-deftest test-org/set-visibility-according-to-property ()
  7169. "Test `org-set-visibility-according-to-property' specifications."
  7170. ;; "folded" state.
  7171. (should
  7172. (org-test-with-temp-text
  7173. "
  7174. * a
  7175. :PROPERTIES:
  7176. :VISIBILITY: folded
  7177. :END:
  7178. ** <point>b"
  7179. (org-set-visibility-according-to-property)
  7180. (invisible-p (point))))
  7181. ;; "children" state.
  7182. (should
  7183. (org-test-with-temp-text
  7184. "
  7185. * a
  7186. :PROPERTIES:
  7187. :VISIBILITY: children
  7188. :END:
  7189. ** b
  7190. <point>Contents
  7191. ** c"
  7192. (org-set-visibility-according-to-property)
  7193. (invisible-p (point))))
  7194. (should
  7195. (org-test-with-temp-text
  7196. "
  7197. * a
  7198. :PROPERTIES:
  7199. :VISIBILITY: children
  7200. :END:
  7201. ** b
  7202. Contents
  7203. *** <point>c"
  7204. (org-set-visibility-according-to-property)
  7205. (invisible-p (point))))
  7206. ;; "content" state.
  7207. (should
  7208. (org-test-with-temp-text
  7209. "
  7210. * a
  7211. :PROPERTIES:
  7212. :VISIBILITY: content
  7213. :END:
  7214. ** b
  7215. <point>Contents
  7216. *** c"
  7217. (org-set-visibility-according-to-property)
  7218. (invisible-p (point))))
  7219. (should
  7220. (org-test-with-temp-text
  7221. "
  7222. * a
  7223. :PROPERTIES:
  7224. :VISIBILITY: content
  7225. :END:
  7226. ** b
  7227. Contents
  7228. *** <point>c"
  7229. (org-set-visibility-according-to-property)
  7230. (not (invisible-p (point)))))
  7231. ;; "showall" state.
  7232. (should
  7233. (org-test-with-temp-text
  7234. "
  7235. * a
  7236. :PROPERTIES:
  7237. :VISIBILITY: showall
  7238. :END:
  7239. ** b
  7240. <point>Contents
  7241. *** c"
  7242. (org-set-visibility-according-to-property)
  7243. (not (invisible-p (point)))))
  7244. (should
  7245. (org-test-with-temp-text
  7246. "
  7247. * a
  7248. :PROPERTIES:
  7249. :VISIBILITY: showall
  7250. :END:
  7251. ** b
  7252. Contents
  7253. *** <point>c"
  7254. (org-set-visibility-according-to-property)
  7255. (not (invisible-p (point)))))
  7256. ;; When VISIBILITY properties are nested, ignore inner ones.
  7257. (should
  7258. (org-test-with-temp-text
  7259. "
  7260. * A
  7261. :PROPERTIES:
  7262. :VISIBILITY: folded
  7263. :END:
  7264. ** <point>B
  7265. :PROPERTIES:
  7266. :VISIBILITY: folded
  7267. :END:"
  7268. (org-set-visibility-according-to-property)
  7269. (invisible-p (point)))))
  7270. ;;; Yank and Kill
  7271. (ert-deftest test-org/paste-subtree ()
  7272. "Test `org-paste-subtree' specifications."
  7273. ;; Return an error if text to yank is not a set of subtrees.
  7274. (should-error (org-paste-subtree nil "Text"))
  7275. ;; Adjust level according to current one.
  7276. (should
  7277. (equal "* H\n* Text\n"
  7278. (org-test-with-temp-text "* H\n<point>"
  7279. (org-paste-subtree nil "* Text")
  7280. (buffer-string))))
  7281. (should
  7282. (equal "* H1\n** H2\n** Text\n"
  7283. (org-test-with-temp-text "* H1\n** H2\n<point>"
  7284. (org-paste-subtree nil "* Text")
  7285. (buffer-string))))
  7286. ;; When not on a heading, move to next heading before yanking.
  7287. (should
  7288. (equal "* H1\nParagraph\n* Text\n* H2"
  7289. (org-test-with-temp-text "* H1\n<point>Paragraph\n* H2"
  7290. (org-paste-subtree nil "* Text")
  7291. (buffer-string))))
  7292. ;; If point is between two headings, use the deepest level.
  7293. (should
  7294. (equal "* H1\n\n* Text\n* H2"
  7295. (org-test-with-temp-text "* H1\n<point>\n* H2"
  7296. (org-paste-subtree nil "* Text")
  7297. (buffer-string))))
  7298. (should
  7299. (equal "** H1\n\n** Text\n* H2"
  7300. (org-test-with-temp-text "** H1\n<point>\n* H2"
  7301. (org-paste-subtree nil "* Text")
  7302. (buffer-string))))
  7303. (should
  7304. (equal "* H1\n\n** Text\n** H2"
  7305. (org-test-with-temp-text "* H1\n<point>\n** H2"
  7306. (org-paste-subtree nil "* Text")
  7307. (buffer-string))))
  7308. ;; When on an empty heading, after the stars, deduce the new level
  7309. ;; from the number of stars.
  7310. (should
  7311. (equal "*** Text\n"
  7312. (org-test-with-temp-text "*** <point>"
  7313. (org-paste-subtree nil "* Text")
  7314. (buffer-string))))
  7315. ;; Optional argument LEVEL forces a level for the subtree.
  7316. (should
  7317. (equal "* H\n*** Text\n"
  7318. (org-test-with-temp-text "* H<point>"
  7319. (org-paste-subtree 3 "* Text")
  7320. (buffer-string)))))
  7321. (ert-deftest test-org/cut-and-paste-subtree ()
  7322. "Test `org-cut-subtree' and `org-paste-subtree'."
  7323. (should
  7324. (equal
  7325. "* Two
  7326. two
  7327. * One
  7328. "
  7329. (org-test-with-temp-text
  7330. "* One
  7331. <point>* Two
  7332. two
  7333. "
  7334. (call-interactively #'org-cut-subtree)
  7335. (goto-char (point-min))
  7336. (call-interactively #'org-paste-subtree)
  7337. (buffer-string))))
  7338. (should
  7339. (equal
  7340. "* One
  7341. * Two
  7342. "
  7343. (org-test-with-temp-text
  7344. "* One
  7345. <point>* Two
  7346. "
  7347. (call-interactively #'org-cut-subtree)
  7348. (backward-char)
  7349. (call-interactively #'org-paste-subtree)
  7350. (buffer-string)))))
  7351. (provide 'test-org)
  7352. ;;; test-org.el ends here