test-org.el 221 KB

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