test-org.el 219 KB

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