test-org.el 211 KB

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