test-org.el 215 KB

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