test-org.el 214 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571
  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-in-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. (ert-deftest test-org/open-at-point-in-property ()
  2352. "Does `org-open-at-point' open link in property drawer?"
  2353. (should
  2354. (org-test-with-temp-text
  2355. "* Headline
  2356. :PROPERTIES:
  2357. :URL: <point>[[*Headline]]
  2358. :END:"
  2359. (org-open-at-point) t)))
  2360. (ert-deftest test-org/open-at-point-in-comment ()
  2361. "Does `org-open-at-point' open link in a commented line?"
  2362. (should
  2363. (org-test-with-temp-text
  2364. "<<top>>\n# <point>[[top]]"
  2365. (org-open-at-point) t)))
  2366. (ert-deftest test-org/open-at-point/inline-image ()
  2367. "Test `org-open-at-point' on nested links."
  2368. (should
  2369. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2370. (org-open-at-point)
  2371. (bobp))))
  2372. (ert-deftest test-org/open-at-point/radio-target ()
  2373. "Test `org-open-at-point' on radio targets."
  2374. (should
  2375. (org-test-with-temp-text "<<<target>>> <point>target"
  2376. (org-update-radio-target-regexp)
  2377. (org-open-at-point)
  2378. (eq (org-element-type (org-element-context)) 'radio-target))))
  2379. ;;;; Stored links
  2380. (ert-deftest test-org/store-link ()
  2381. "Test `org-store-link' specifications."
  2382. ;; On a headline, link to that headline. Use heading as the
  2383. ;; description of the link.
  2384. (should
  2385. (let (org-store-link-props org-stored-links)
  2386. (org-test-with-temp-text-in-file "* H1"
  2387. (let ((file (buffer-file-name)))
  2388. (equal (format "[[file:%s::*H1][H1]]" file)
  2389. (org-store-link nil))))))
  2390. ;; On a headline, remove any link from description.
  2391. (should
  2392. (let (org-store-link-props org-stored-links)
  2393. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2394. (let ((file (buffer-file-name)))
  2395. (equal (format "[[file:%s::*%s][d]]"
  2396. file
  2397. (org-link-escape "[[#l][d]]"))
  2398. (org-store-link nil))))))
  2399. (should
  2400. (let (org-store-link-props org-stored-links)
  2401. (org-test-with-temp-text-in-file "* [[l]]"
  2402. (let ((file (buffer-file-name)))
  2403. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2404. (org-store-link nil))))))
  2405. (should
  2406. (let (org-store-link-props org-stored-links)
  2407. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2408. (let ((file (buffer-file-name)))
  2409. (equal (format "[[file:%s::*%s][d1 d2]]"
  2410. file
  2411. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2412. (org-store-link nil))))))
  2413. ;; On a named element, link to that element.
  2414. (should
  2415. (let (org-store-link-props org-stored-links)
  2416. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2417. (let ((file (buffer-file-name)))
  2418. (equal (format "[[file:%s::foo][foo]]" file)
  2419. (org-store-link nil))))))
  2420. ;; Store link to Org buffer, with context.
  2421. (should
  2422. (let ((org-stored-links nil)
  2423. (org-id-link-to-org-use-id nil)
  2424. (org-context-in-file-links t))
  2425. (org-test-with-temp-text-in-file "* h1"
  2426. (let ((file (buffer-file-name)))
  2427. (equal (format "[[file:%s::*h1][h1]]" file)
  2428. (org-store-link nil))))))
  2429. ;; Store link to Org buffer, without context.
  2430. (should
  2431. (let ((org-stored-links nil)
  2432. (org-id-link-to-org-use-id nil)
  2433. (org-context-in-file-links nil))
  2434. (org-test-with-temp-text-in-file "* h1"
  2435. (let ((file (buffer-file-name)))
  2436. (equal (format "[[file:%s][file:%s]]" file file)
  2437. (org-store-link nil))))))
  2438. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  2439. (should
  2440. (let ((org-stored-links nil)
  2441. (org-id-link-to-org-use-id nil)
  2442. (org-context-in-file-links nil))
  2443. (org-test-with-temp-text-in-file "* h1"
  2444. (let ((file (buffer-file-name)))
  2445. (equal (format "[[file:%s::*h1][h1]]" file)
  2446. (org-store-link '(4)))))))
  2447. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  2448. ;; buffer.
  2449. (should
  2450. (let ((org-stored-links nil)
  2451. (org-id-link-to-org-use-id nil)
  2452. (org-context-in-file-links nil))
  2453. (org-test-with-temp-text-in-file "* h1"
  2454. (let ((file (buffer-file-name)))
  2455. (equal (format "[[file:%s][file:%s]]" file file)
  2456. (org-store-link '(16)))))))
  2457. ;; Store file link to non-Org buffer, with context.
  2458. (should
  2459. (let ((org-stored-links nil)
  2460. (org-context-in-file-links t))
  2461. (org-test-with-temp-text-in-file "one\n<point>two"
  2462. (fundamental-mode)
  2463. (let ((file (buffer-file-name)))
  2464. (equal (format "[[file:%s::one]]" file)
  2465. (org-store-link nil))))))
  2466. ;; Store file link to non-Org buffer, without context.
  2467. (should
  2468. (let ((org-stored-links nil)
  2469. (org-context-in-file-links nil))
  2470. (org-test-with-temp-text-in-file "one\n<point>two"
  2471. (fundamental-mode)
  2472. (let ((file (buffer-file-name)))
  2473. (equal (format "[[file:%s][file:%s]]" file file)
  2474. (org-store-link nil))))))
  2475. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  2476. ;; buffer.
  2477. (should
  2478. (let ((org-stored-links nil)
  2479. (org-context-in-file-links nil))
  2480. (org-test-with-temp-text-in-file "one\n<point>two"
  2481. (fundamental-mode)
  2482. (let ((file (buffer-file-name)))
  2483. (equal (format "[[file:%s::one]]" file)
  2484. (org-store-link '(4)))))))
  2485. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  2486. ;; non-Org buffer.
  2487. (should
  2488. (let ((org-stored-links nil)
  2489. (org-context-in-file-links nil))
  2490. (org-test-with-temp-text-in-file "one\n<point>two"
  2491. (fundamental-mode)
  2492. (let ((file (buffer-file-name)))
  2493. (equal (format "[[file:%s][file:%s]]" file file)
  2494. (org-store-link '(16))))))))
  2495. ;;; Node Properties
  2496. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2497. "Ensure properties accumulate in subtree drawers."
  2498. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2499. (org-babel-next-src-block)
  2500. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2501. (ert-deftest test-org/custom-properties ()
  2502. "Test custom properties specifications."
  2503. ;; Standard test.
  2504. (should
  2505. (let ((org-custom-properties '("FOO")))
  2506. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2507. (org-toggle-custom-properties-visibility)
  2508. (org-invisible-p2))))
  2509. ;; Properties are case-insensitive.
  2510. (should
  2511. (let ((org-custom-properties '("FOO")))
  2512. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2513. (org-toggle-custom-properties-visibility)
  2514. (org-invisible-p2))))
  2515. (should
  2516. (let ((org-custom-properties '("foo")))
  2517. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2518. (org-toggle-custom-properties-visibility)
  2519. (org-invisible-p2))))
  2520. ;; Multiple custom properties in the same drawer.
  2521. (should
  2522. (let ((org-custom-properties '("FOO" "BAR")))
  2523. (org-test-with-temp-text
  2524. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2525. (org-toggle-custom-properties-visibility)
  2526. (and (org-invisible-p2)
  2527. (not (progn (forward-line) (org-invisible-p2)))
  2528. (progn (forward-line) (org-invisible-p2))))))
  2529. ;; Hide custom properties with an empty value.
  2530. (should
  2531. (let ((org-custom-properties '("FOO")))
  2532. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2533. (org-toggle-custom-properties-visibility)
  2534. (org-invisible-p2))))
  2535. ;; Do not hide fake properties.
  2536. (should-not
  2537. (let ((org-custom-properties '("FOO")))
  2538. (org-test-with-temp-text ":FOO: val\n"
  2539. (org-toggle-custom-properties-visibility)
  2540. (org-invisible-p2))))
  2541. (should-not
  2542. (let ((org-custom-properties '("A")))
  2543. (org-test-with-temp-text
  2544. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2545. (org-toggle-custom-properties-visibility)
  2546. (org-invisible-p2)))))
  2547. ;;; Mark Region
  2548. (ert-deftest test-org/mark-subtree ()
  2549. "Test `org-mark-subtree' specifications."
  2550. ;; Error when point is before first headline.
  2551. (should-error
  2552. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2553. (progn (transient-mark-mode 1)
  2554. (org-mark-subtree))))
  2555. ;; Without argument, mark current subtree.
  2556. (should
  2557. (equal
  2558. '(12 32)
  2559. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2560. (progn (transient-mark-mode 1)
  2561. (forward-line 2)
  2562. (org-mark-subtree)
  2563. (list (region-beginning) (region-end))))))
  2564. ;; With an argument, move ARG up.
  2565. (should
  2566. (equal
  2567. '(1 32)
  2568. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2569. (progn (transient-mark-mode 1)
  2570. (forward-line 2)
  2571. (org-mark-subtree 1)
  2572. (list (region-beginning) (region-end))))))
  2573. ;; Do not get fooled by inlinetasks.
  2574. (when (featurep 'org-inlinetask)
  2575. (should
  2576. (= 1
  2577. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2578. (progn (transient-mark-mode 1)
  2579. (forward-line 1)
  2580. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2581. (region-beginning)))))))
  2582. ;;; Miscellaneous
  2583. (ert-deftest test-org/in-regexp ()
  2584. "Test `org-in-regexp' specifications."
  2585. ;; Standard tests.
  2586. (should
  2587. (org-test-with-temp-text "xx ab<point>c xx"
  2588. (org-in-regexp "abc")))
  2589. (should-not
  2590. (org-test-with-temp-text "xx abc <point>xx"
  2591. (org-in-regexp "abc")))
  2592. ;; Return non-nil even with multiple matching regexps in the same
  2593. ;; line.
  2594. (should
  2595. (org-test-with-temp-text "abc xx ab<point>c xx"
  2596. (org-in-regexp "abc")))
  2597. ;; With optional argument NLINES, check extra lines around point.
  2598. (should-not
  2599. (org-test-with-temp-text "A\nB<point>\nC"
  2600. (org-in-regexp "A\nB\nC")))
  2601. (should
  2602. (org-test-with-temp-text "A\nB<point>\nC"
  2603. (org-in-regexp "A\nB\nC" 1)))
  2604. (should-not
  2605. (org-test-with-temp-text "A\nB\nC<point>"
  2606. (org-in-regexp "A\nB\nC" 1)))
  2607. ;; When optional argument VISUALLY is non-nil, return nil if at
  2608. ;; regexp boundaries.
  2609. (should
  2610. (org-test-with-temp-text "xx abc<point> xx"
  2611. (org-in-regexp "abc")))
  2612. (should-not
  2613. (org-test-with-temp-text "xx abc<point> xx"
  2614. (org-in-regexp "abc" nil t))))
  2615. (ert-deftest test-org/sort-entries ()
  2616. "Test `org-sort-entries'."
  2617. ;; Sort alphabetically.
  2618. (should
  2619. (equal "\n* abc\n* def\n* xyz\n"
  2620. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2621. (org-sort-entries nil ?a)
  2622. (buffer-string))))
  2623. (should
  2624. (equal "\n* xyz\n* def\n* abc\n"
  2625. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2626. (org-sort-entries nil ?A)
  2627. (buffer-string))))
  2628. ;; Sort numerically.
  2629. (should
  2630. (equal "\n* 1\n* 2\n* 10\n"
  2631. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2632. (org-sort-entries nil ?n)
  2633. (buffer-string))))
  2634. (should
  2635. (equal "\n* 10\n* 2\n* 1\n"
  2636. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2637. (org-sort-entries nil ?N)
  2638. (buffer-string))))
  2639. ;; Sort by custom function.
  2640. (should
  2641. (equal "\n* b\n* aa\n* ccc\n"
  2642. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2643. (org-sort-entries nil ?f
  2644. (lambda ()
  2645. (length (buffer-substring (point-at-bol)
  2646. (point-at-eol))))
  2647. #'<)
  2648. (buffer-string))))
  2649. (should
  2650. (equal "\n* ccc\n* aa\n* b\n"
  2651. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2652. (org-sort-entries nil ?F
  2653. (lambda ()
  2654. (length (buffer-substring (point-at-bol)
  2655. (point-at-eol))))
  2656. #'<)
  2657. (buffer-string))))
  2658. ;; Sort by TODO keyword.
  2659. (should
  2660. (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
  2661. (org-test-with-temp-text
  2662. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2663. (org-sort-entries nil ?o)
  2664. (buffer-string))))
  2665. (should
  2666. (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
  2667. (org-test-with-temp-text
  2668. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2669. (org-sort-entries nil ?O)
  2670. (buffer-string))))
  2671. ;; Sort by priority.
  2672. (should
  2673. (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
  2674. (org-test-with-temp-text
  2675. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2676. (org-sort-entries nil ?p)
  2677. (buffer-string))))
  2678. (should
  2679. (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
  2680. (org-test-with-temp-text
  2681. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2682. (org-sort-entries nil ?P)
  2683. (buffer-string))))
  2684. ;; Sort by creation time.
  2685. (should
  2686. (equal "
  2687. * h3
  2688. [2017-05-08 Mon]
  2689. * h2
  2690. [2017-05-09 Tue]
  2691. * h1
  2692. [2018-05-09 Wed]
  2693. "
  2694. (org-test-with-temp-text
  2695. "
  2696. * h1
  2697. [2018-05-09 Wed]
  2698. * h2
  2699. [2017-05-09 Tue]
  2700. * h3
  2701. [2017-05-08 Mon]
  2702. "
  2703. (org-sort-entries nil ?c)
  2704. (buffer-string))))
  2705. ;; Sort by scheduled date.
  2706. (should
  2707. (equal "
  2708. * TODO h4
  2709. SCHEDULED: <2017-05-06 Sat>
  2710. * TODO h3
  2711. SCHEDULED: <2017-05-08 Mon>
  2712. * TODO h2
  2713. DEADLINE: <2017-05-09 Tue>
  2714. * TODO h1
  2715. DEADLINE: <2017-05-07 Sun>
  2716. "
  2717. (org-test-with-temp-text
  2718. "
  2719. * TODO h2
  2720. DEADLINE: <2017-05-09 Tue>
  2721. * TODO h1
  2722. DEADLINE: <2017-05-07 Sun>
  2723. * TODO h3
  2724. SCHEDULED: <2017-05-08 Mon>
  2725. * TODO h4
  2726. SCHEDULED: <2017-05-06 Sat>
  2727. "
  2728. (org-sort-entries nil ?s)
  2729. (buffer-string))))
  2730. ;; Sort by deadline date.
  2731. (should
  2732. (equal "
  2733. * TODO h1
  2734. DEADLINE: <2017-05-07 Sun>
  2735. * TODO h2
  2736. DEADLINE: <2017-05-09 Tue>
  2737. * TODO h3
  2738. SCHEDULED: <2017-05-08 Mon>
  2739. * TODO h4
  2740. SCHEDULED: <2017-05-06 Sat>
  2741. "
  2742. (org-test-with-temp-text
  2743. "
  2744. * TODO h2
  2745. DEADLINE: <2017-05-09 Tue>
  2746. * TODO h1
  2747. DEADLINE: <2017-05-07 Sun>
  2748. * TODO h3
  2749. SCHEDULED: <2017-05-08 Mon>
  2750. * TODO h4
  2751. SCHEDULED: <2017-05-06 Sat>
  2752. "
  2753. (org-sort-entries nil ?d)
  2754. (buffer-string))))
  2755. ;; Sort by any date/time
  2756. (should
  2757. (equal "
  2758. * TODO h4
  2759. SCHEDULED: <2017-05-06 Sat>
  2760. * TODO h1
  2761. DEADLINE: <2017-05-07 Sun>
  2762. * TODO h3
  2763. SCHEDULED: <2017-05-08 Mon>
  2764. * TODO h2
  2765. DEADLINE: <2017-05-09 Tue>
  2766. "
  2767. (org-test-with-temp-text
  2768. "
  2769. * TODO h2
  2770. DEADLINE: <2017-05-09 Tue>
  2771. * TODO h1
  2772. DEADLINE: <2017-05-07 Sun>
  2773. * TODO h3
  2774. SCHEDULED: <2017-05-08 Mon>
  2775. * TODO h4
  2776. SCHEDULED: <2017-05-06 Sat>
  2777. "
  2778. (org-sort-entries nil ?t)
  2779. (buffer-string))))
  2780. ;; Sort by clocking time.
  2781. (should
  2782. (equal "
  2783. * clocked h2
  2784. :LOGBOOK:
  2785. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2786. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2787. :END:
  2788. * clocked h1
  2789. :LOGBOOK:
  2790. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2791. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2792. :END:
  2793. "
  2794. (org-test-with-temp-text
  2795. "
  2796. * clocked h1
  2797. :LOGBOOK:
  2798. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2799. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2800. :END:
  2801. * clocked h2
  2802. :LOGBOOK:
  2803. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2804. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2805. :END:
  2806. "
  2807. (org-sort-entries nil ?k)
  2808. (buffer-string)))))
  2809. ;;; Navigation
  2810. (ert-deftest test-org/forward-heading-same-level ()
  2811. "Test `org-forward-heading-same-level' specifications."
  2812. ;; Test navigation at top level, forward and backward.
  2813. (should
  2814. (equal "* H2"
  2815. (org-test-with-temp-text "* H1\n* H2"
  2816. (org-forward-heading-same-level 1)
  2817. (buffer-substring-no-properties (point) (line-end-position)))))
  2818. (should
  2819. (equal "* H1"
  2820. (org-test-with-temp-text "* H1\n<point>* H2"
  2821. (org-forward-heading-same-level -1)
  2822. (buffer-substring-no-properties (point) (line-end-position)))))
  2823. ;; Test navigation in a sub-tree, forward and backward.
  2824. (should
  2825. (equal "* H2"
  2826. (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
  2827. (org-forward-heading-same-level 1)
  2828. (buffer-substring-no-properties (point) (line-end-position)))))
  2829. (should
  2830. (equal "* H1"
  2831. (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
  2832. (org-forward-heading-same-level -1)
  2833. (buffer-substring-no-properties (point) (line-end-position)))))
  2834. ;; Stop at first or last sub-heading.
  2835. (should-not
  2836. (equal "* H2"
  2837. (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
  2838. (org-forward-heading-same-level 1)
  2839. (buffer-substring-no-properties (point) (line-end-position)))))
  2840. (should-not
  2841. (equal "* H2"
  2842. (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
  2843. (org-forward-heading-same-level -1)
  2844. (buffer-substring-no-properties (point) (line-end-position)))))
  2845. ;; Allow multiple moves.
  2846. (should
  2847. (equal "* H3"
  2848. (org-test-with-temp-text "* H1\n* H2\n* H3"
  2849. (org-forward-heading-same-level 2)
  2850. (buffer-substring-no-properties (point) (line-end-position)))))
  2851. (should
  2852. (equal "* H1"
  2853. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  2854. (org-forward-heading-same-level -2)
  2855. (buffer-substring-no-properties (point) (line-end-position)))))
  2856. ;; Ignore spurious moves when first (or last) sibling is reached.
  2857. (should
  2858. (equal "** H3"
  2859. (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
  2860. (org-forward-heading-same-level 100)
  2861. (buffer-substring-no-properties (point) (line-end-position)))))
  2862. (should
  2863. (equal "** H1"
  2864. (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
  2865. (org-forward-heading-same-level -100)
  2866. (buffer-substring-no-properties (point) (line-end-position))))))
  2867. (ert-deftest test-org/end-of-meta-data ()
  2868. "Test `org-end-of-meta-data' specifications."
  2869. ;; Skip planning line.
  2870. (should
  2871. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  2872. (org-end-of-meta-data)
  2873. (eobp)))
  2874. ;; Skip properties drawer.
  2875. (should
  2876. (org-test-with-temp-text
  2877. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  2878. (org-end-of-meta-data)
  2879. (eobp)))
  2880. ;; Skip both.
  2881. (should
  2882. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  2883. (org-end-of-meta-data)
  2884. (eobp)))
  2885. ;; Nothing to skip, go to first line.
  2886. (should
  2887. (org-test-with-temp-text "* Headline\nContents"
  2888. (org-end-of-meta-data)
  2889. (looking-at "Contents")))
  2890. ;; With option argument, skip empty lines, regular drawers and
  2891. ;; clocking lines.
  2892. (should
  2893. (org-test-with-temp-text "* Headline\n\nContents"
  2894. (org-end-of-meta-data t)
  2895. (looking-at "Contents")))
  2896. (should
  2897. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  2898. (org-end-of-meta-data t)
  2899. (looking-at "Contents")))
  2900. (should
  2901. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  2902. (org-end-of-meta-data t)
  2903. (looking-at "Contents")))
  2904. ;; Special case: do not skip incomplete drawers.
  2905. (should
  2906. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  2907. (org-end-of-meta-data t)
  2908. (looking-at ":LOGBOOK:")))
  2909. ;; Special case: Be careful about consecutive headlines.
  2910. (should-not
  2911. (org-test-with-temp-text "* H1\n*H2\nContents"
  2912. (org-end-of-meta-data t)
  2913. (looking-at "Contents"))))
  2914. (ert-deftest test-org/beginning-of-line ()
  2915. "Test `org-beginning-of-line' specifications."
  2916. ;; Move to beginning of line. If current line in invisible, move to
  2917. ;; beginning of visible line instead.
  2918. (should
  2919. (org-test-with-temp-text "Some text\nSome other text<point>"
  2920. (org-beginning-of-line)
  2921. (bolp)))
  2922. (should
  2923. (org-test-with-temp-text "* H1\n** H2<point>"
  2924. (org-overview)
  2925. (org-beginning-of-line)
  2926. (= (line-beginning-position) 1)))
  2927. ;; With `visual-line-mode' active, move to beginning of visual line.
  2928. (should-not
  2929. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2930. (visual-line-mode)
  2931. (dotimes (i 1000) (insert "very "))
  2932. (org-beginning-of-line)
  2933. (bolp)))
  2934. ;; In a wide headline, with `visual-line-mode', prefer going to the
  2935. ;; beginning of a visual line than to the logical beginning of line,
  2936. ;; even if special movement is active.
  2937. (should-not
  2938. (org-test-with-temp-text "* A <point>long headline"
  2939. (visual-line-mode)
  2940. (dotimes (i 1000) (insert "very "))
  2941. (goto-char (point-max))
  2942. (org-beginning-of-line)
  2943. (bobp)))
  2944. (should-not
  2945. (org-test-with-temp-text "* A <point>long headline"
  2946. (visual-line-mode)
  2947. (dotimes (i 1000) (insert "very "))
  2948. (goto-char (point-max))
  2949. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  2950. (bobp)))
  2951. ;; At an headline with special movement, first move at beginning of
  2952. ;; title, then at the beginning of line, rinse, repeat.
  2953. (should
  2954. (org-test-with-temp-text "* TODO Headline<point>"
  2955. (let ((org-special-ctrl-a/e t))
  2956. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  2957. (progn (org-beginning-of-line) (bolp))
  2958. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2959. (should
  2960. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2961. (let ((org-special-ctrl-a/e t))
  2962. (org-beginning-of-line)
  2963. (looking-at "Headline"))))
  2964. (should
  2965. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2966. (let ((org-special-ctrl-a/e '(t . nil)))
  2967. (org-beginning-of-line)
  2968. (looking-at "Headline"))))
  2969. (should-not
  2970. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2971. (let ((org-special-ctrl-a/e '(nil . nil)))
  2972. (org-beginning-of-line)
  2973. (looking-at "Headline"))))
  2974. ;; At an headline with reversed movement, first move to beginning of
  2975. ;; line, then to the beginning of title.
  2976. (should
  2977. (org-test-with-temp-text "* TODO Headline<point>"
  2978. (let ((org-special-ctrl-a/e 'reversed)
  2979. (this-command last-command))
  2980. (and (progn (org-beginning-of-line) (bolp))
  2981. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2982. (should
  2983. (org-test-with-temp-text "* TODO Headline<point>"
  2984. (let ((org-special-ctrl-a/e '(reversed . nil))
  2985. (this-command last-command))
  2986. (and (progn (org-beginning-of-line) (bolp))
  2987. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2988. (should-not
  2989. (org-test-with-temp-text "* TODO Headline<point>"
  2990. (let ((org-special-ctrl-a/e '(t . nil))
  2991. (this-command last-command))
  2992. (and (progn (org-beginning-of-line) (bolp))
  2993. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2994. ;; At an item with special movement, first move after to beginning
  2995. ;; of title, then to the beginning of line, rinse, repeat.
  2996. (should
  2997. (org-test-with-temp-text "- [ ] Item<point>"
  2998. (let ((org-special-ctrl-a/e t))
  2999. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  3000. (progn (org-beginning-of-line) (bolp))
  3001. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3002. ;; At an item with reversed movement, first move to beginning of
  3003. ;; line, then to the beginning of title.
  3004. (should
  3005. (org-test-with-temp-text "- [X] Item<point>"
  3006. (let ((org-special-ctrl-a/e 'reversed)
  3007. (this-command last-command))
  3008. (and (progn (org-beginning-of-line) (bolp))
  3009. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3010. ;; Leave point before invisible characters at column 0.
  3011. (should
  3012. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  3013. (let ((org-special-ctrl-a/e nil))
  3014. (org-beginning-of-line)
  3015. (bolp))))
  3016. (should
  3017. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  3018. (let ((org-special-ctrl-a/e t))
  3019. (org-beginning-of-line)
  3020. (bolp))))
  3021. (should
  3022. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  3023. (visual-line-mode)
  3024. (org-beginning-of-line)
  3025. (bolp)))
  3026. ;; Special case: Do not error when the buffer contains only a single
  3027. ;; asterisk.
  3028. (should
  3029. (org-test-with-temp-text "*<point>"
  3030. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  3031. (should
  3032. (org-test-with-temp-text "*<point>"
  3033. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  3034. (ert-deftest test-org/end-of-line ()
  3035. "Test `org-end-of-line' specifications."
  3036. ;; Standard test.
  3037. (should
  3038. (org-test-with-temp-text "Some text\nSome other text"
  3039. (org-end-of-line)
  3040. (eolp)))
  3041. ;; With `visual-line-mode' active, move to end of visible line.
  3042. ;; However, never go past ellipsis.
  3043. (should-not
  3044. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3045. (visual-line-mode)
  3046. (dotimes (i 1000) (insert "very "))
  3047. (goto-char (point-min))
  3048. (org-end-of-line)
  3049. (eolp)))
  3050. (should-not
  3051. (org-test-with-temp-text "* A short headline\nSome contents"
  3052. (visual-line-mode)
  3053. (org-overview)
  3054. (org-end-of-line)
  3055. (eobp)))
  3056. ;; In a wide headline, with `visual-line-mode', prefer going to end
  3057. ;; of visible line if tags, or end of line, are farther.
  3058. (should-not
  3059. (org-test-with-temp-text "* A <point>long headline"
  3060. (visual-line-mode)
  3061. (dotimes (i 1000) (insert "very "))
  3062. (goto-char (point-min))
  3063. (org-end-of-line)
  3064. (eolp)))
  3065. (should-not
  3066. (org-test-with-temp-text "* A <point>long headline :tag:"
  3067. (visual-line-mode)
  3068. (dotimes (i 1000) (insert "very "))
  3069. (goto-char (point-min))
  3070. (org-end-of-line)
  3071. (eolp)))
  3072. ;; At an headline without special movement, go to end of line.
  3073. ;; However, never go past ellipsis.
  3074. (should
  3075. (org-test-with-temp-text "* Headline2b :tag:\n"
  3076. (let ((org-special-ctrl-a/e nil))
  3077. (and (progn (org-end-of-line) (eolp))
  3078. (progn (org-end-of-line) (eolp))))))
  3079. (should
  3080. (org-test-with-temp-text "* Headline2b :tag:\n"
  3081. (let ((org-special-ctrl-a/e '(t . nil)))
  3082. (and (progn (org-end-of-line) (eolp))
  3083. (progn (org-end-of-line) (eolp))))))
  3084. (should
  3085. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3086. (org-overview)
  3087. (let ((org-special-ctrl-a/e nil))
  3088. (org-end-of-line)
  3089. (= 1 (line-beginning-position)))))
  3090. ;; At an headline with special movement, first move before tags,
  3091. ;; then at the end of line, rinse, repeat. However, never go past
  3092. ;; ellipsis.
  3093. (should
  3094. (org-test-with-temp-text "* Headline1 :tag:\n"
  3095. (let ((org-special-ctrl-a/e t))
  3096. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3097. (progn (org-end-of-line) (eolp))
  3098. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3099. (should
  3100. (org-test-with-temp-text "* Headline1 :tag:\n"
  3101. (let ((org-special-ctrl-a/e '(nil . t)))
  3102. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3103. (progn (org-end-of-line) (eolp))
  3104. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3105. (should-not
  3106. (org-test-with-temp-text "* Headline1 :tag:\n"
  3107. (let ((org-special-ctrl-a/e '(nil . nil)))
  3108. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3109. (progn (org-end-of-line) (eolp))
  3110. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3111. (should
  3112. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3113. (org-overview)
  3114. (let ((org-special-ctrl-a/e t))
  3115. (org-end-of-line)
  3116. (org-end-of-line)
  3117. (= 1 (line-beginning-position)))))
  3118. ;; At an headline, with reversed movement, first go to end of line,
  3119. ;; then before tags. However, never go past ellipsis.
  3120. (should
  3121. (org-test-with-temp-text "* Headline3 :tag:\n"
  3122. (let ((org-special-ctrl-a/e 'reversed)
  3123. (this-command last-command))
  3124. (and (progn (org-end-of-line) (eolp))
  3125. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3126. (should
  3127. (org-test-with-temp-text "* Headline3 :tag:\n"
  3128. (let ((org-special-ctrl-a/e '(nil . reversed))
  3129. (this-command last-command))
  3130. (and (progn (org-end-of-line) (eolp))
  3131. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3132. (should-not
  3133. (org-test-with-temp-text "* Headline3 :tag:\n"
  3134. (let ((org-special-ctrl-a/e '(nil . t))
  3135. (this-command last-command))
  3136. (and (progn (org-end-of-line) (eolp))
  3137. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3138. (should
  3139. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3140. (org-overview)
  3141. (let ((org-special-ctrl-a/e 'reversed))
  3142. (org-end-of-line)
  3143. (= 1 (line-beginning-position)))))
  3144. ;; At a block without hidden contents.
  3145. (should
  3146. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3147. (progn (org-end-of-line) (eolp))))
  3148. ;; At a block with hidden contents.
  3149. (should-not
  3150. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3151. (let ((org-special-ctrl-a/e t))
  3152. (org-hide-block-toggle)
  3153. (org-end-of-line)
  3154. (eobp))))
  3155. ;; Get past invisible characters at the end of line.
  3156. (should
  3157. (org-test-with-temp-text "[[http://orgmode.org]]"
  3158. (org-end-of-line)
  3159. (eolp))))
  3160. (ert-deftest test-org/open-line ()
  3161. "Test `org-open-line' specifications."
  3162. ;; Call `open-line' outside of tables.
  3163. (should
  3164. (equal "\nText"
  3165. (org-test-with-temp-text "Text"
  3166. (org-open-line 1)
  3167. (buffer-string))))
  3168. ;; At a table, create a row above.
  3169. (should
  3170. (equal "\n| |\n| a |"
  3171. (org-test-with-temp-text "\n<point>| a |"
  3172. (org-open-line 1)
  3173. (buffer-string))))
  3174. ;; At the very first character of the buffer, also call `open-line'.
  3175. (should
  3176. (equal "\n| a |"
  3177. (org-test-with-temp-text "| a |"
  3178. (org-open-line 1)
  3179. (buffer-string))))
  3180. ;; Narrowing does not count.
  3181. (should
  3182. (equal "Text\n| |\n| a |"
  3183. (org-test-with-temp-text "Text\n<point>| a |"
  3184. (narrow-to-region (point) (point-max))
  3185. (org-open-line 1)
  3186. (widen)
  3187. (buffer-string)))))
  3188. (ert-deftest test-org/forward-sentence ()
  3189. "Test `org-forward-sentence' specifications."
  3190. ;; At the end of a table cell, move to the end of the next one.
  3191. (should
  3192. (org-test-with-temp-text "| a<point> | b |"
  3193. (org-forward-sentence)
  3194. (looking-at " |$")))
  3195. ;; Elsewhere in a cell, move to its end.
  3196. (should
  3197. (org-test-with-temp-text "| a<point>c | b |"
  3198. (org-forward-sentence)
  3199. (looking-at " | b |$")))
  3200. ;; Otherwise, simply call `forward-sentence'.
  3201. (should
  3202. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3203. (org-forward-sentence)
  3204. (looking-at " Sentence 2.")))
  3205. (should
  3206. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3207. (org-forward-sentence)
  3208. (org-forward-sentence)
  3209. (eobp)))
  3210. ;; At the end of an element, jump to the next one, without stopping
  3211. ;; on blank lines in-between.
  3212. (should
  3213. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  3214. (org-forward-sentence)
  3215. (eobp))))
  3216. (ert-deftest test-org/backward-sentence ()
  3217. "Test `org-backward-sentence' specifications."
  3218. ;; At the beginning of a table cell, move to the beginning of the
  3219. ;; previous one.
  3220. (should
  3221. (org-test-with-temp-text "| a | <point>b |"
  3222. (org-backward-sentence)
  3223. (looking-at "a | b |$")))
  3224. ;; Elsewhere in a cell, move to its beginning.
  3225. (should
  3226. (org-test-with-temp-text "| a | b<point>c |"
  3227. (org-backward-sentence)
  3228. (looking-at "bc |$")))
  3229. ;; Otherwise, simply call `backward-sentence'.
  3230. (should
  3231. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3232. (org-backward-sentence)
  3233. (looking-at "Sentence 2.")))
  3234. (should
  3235. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3236. (org-backward-sentence)
  3237. (org-backward-sentence)
  3238. (bobp)))
  3239. ;; Make sure to hit the beginning of a sentence on the same line as
  3240. ;; an item.
  3241. (should
  3242. (org-test-with-temp-text "- Line 1\n line <point>2."
  3243. (org-backward-sentence)
  3244. (looking-at "Line 1"))))
  3245. (ert-deftest test-org/forward-paragraph ()
  3246. "Test `org-forward-paragraph' specifications."
  3247. ;; At end of buffer, return an error.
  3248. (should-error
  3249. (org-test-with-temp-text "Paragraph"
  3250. (goto-char (point-max))
  3251. (org-forward-paragraph)))
  3252. ;; Standard test.
  3253. (should
  3254. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3255. (org-forward-paragraph)
  3256. (looking-at "P2")))
  3257. ;; Ignore depth.
  3258. (should
  3259. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  3260. (org-forward-paragraph)
  3261. (looking-at "P1")))
  3262. ;; Do not enter elements with invisible contents.
  3263. (should
  3264. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  3265. (org-hide-block-toggle)
  3266. (org-forward-paragraph)
  3267. (looking-at "P3")))
  3268. ;; On an affiliated keyword, jump to the beginning of the element.
  3269. (should
  3270. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  3271. (org-forward-paragraph)
  3272. (looking-at "Para")))
  3273. ;; On an item or a footnote definition, move to the second element
  3274. ;; inside, if any.
  3275. (should
  3276. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  3277. (org-forward-paragraph)
  3278. (looking-at " Paragraph")))
  3279. (should
  3280. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  3281. (org-forward-paragraph)
  3282. (looking-at "Paragraph")))
  3283. ;; On an item, or a footnote definition, when the first line is
  3284. ;; empty, move to the first item.
  3285. (should
  3286. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  3287. (org-forward-paragraph)
  3288. (looking-at " Paragraph")))
  3289. (should
  3290. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  3291. (org-forward-paragraph)
  3292. (looking-at "Paragraph")))
  3293. ;; On a table (resp. a property drawer) do not move through table
  3294. ;; rows (resp. node properties).
  3295. (should
  3296. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  3297. (org-forward-paragraph)
  3298. (looking-at "Paragraph")))
  3299. (should
  3300. (org-test-with-temp-text
  3301. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  3302. (org-forward-paragraph)
  3303. (looking-at "Paragraph")))
  3304. ;; On a verse or source block, stop after blank lines.
  3305. (should
  3306. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  3307. (org-forward-paragraph)
  3308. (looking-at "L2")))
  3309. (should
  3310. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  3311. (org-forward-paragraph)
  3312. (looking-at "L2"))))
  3313. (ert-deftest test-org/backward-paragraph ()
  3314. "Test `org-backward-paragraph' specifications."
  3315. ;; Error at beginning of buffer.
  3316. (should-error
  3317. (org-test-with-temp-text "Paragraph"
  3318. (org-backward-paragraph)))
  3319. ;; Regular test.
  3320. (should
  3321. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3322. (goto-char (point-max))
  3323. (org-backward-paragraph)
  3324. (looking-at "P3")))
  3325. (should
  3326. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3327. (goto-char (point-max))
  3328. (beginning-of-line)
  3329. (org-backward-paragraph)
  3330. (looking-at "P2")))
  3331. ;; Ignore depth.
  3332. (should
  3333. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  3334. (goto-char (point-max))
  3335. (beginning-of-line)
  3336. (org-backward-paragraph)
  3337. (looking-at "P2")))
  3338. ;; Ignore invisible elements.
  3339. (should
  3340. (org-test-with-temp-text "* H1\n P1\n* H2"
  3341. (org-cycle)
  3342. (goto-char (point-max))
  3343. (beginning-of-line)
  3344. (org-backward-paragraph)
  3345. (bobp)))
  3346. ;; On an affiliated keyword, jump to the first one.
  3347. (should
  3348. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  3349. (search-forward "c2")
  3350. (org-backward-paragraph)
  3351. (looking-at "#\\+name")))
  3352. ;; On the second element in an item or a footnote definition, jump
  3353. ;; to item or the definition.
  3354. (should
  3355. (org-test-with-temp-text "- line1\n\n line2"
  3356. (goto-char (point-max))
  3357. (beginning-of-line)
  3358. (org-backward-paragraph)
  3359. (looking-at "- line1")))
  3360. (should
  3361. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  3362. (goto-char (point-max))
  3363. (beginning-of-line)
  3364. (org-backward-paragraph)
  3365. (looking-at "\\[fn:1\\] line1")))
  3366. ;; On a table (resp. a property drawer), ignore table rows
  3367. ;; (resp. node properties).
  3368. (should
  3369. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  3370. (goto-char (point-max))
  3371. (beginning-of-line)
  3372. (org-backward-paragraph)
  3373. (bobp)))
  3374. (should
  3375. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  3376. (org-backward-paragraph)
  3377. (looking-at ":PROPERTIES:")))
  3378. ;; On a source or verse block, stop before blank lines.
  3379. (should
  3380. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  3381. (search-forward "L3")
  3382. (beginning-of-line)
  3383. (org-backward-paragraph)
  3384. (looking-at "L2")))
  3385. (should
  3386. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  3387. (search-forward "L3")
  3388. (beginning-of-line)
  3389. (org-backward-paragraph)
  3390. (looking-at "L2"))))
  3391. (ert-deftest test-org/forward-element ()
  3392. "Test `org-forward-element' specifications."
  3393. ;; 1. At EOB: should error.
  3394. (org-test-with-temp-text "Some text\n"
  3395. (goto-char (point-max))
  3396. (should-error (org-forward-element)))
  3397. ;; 2. Standard move: expected to ignore blank lines.
  3398. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  3399. (org-forward-element)
  3400. (should (looking-at (regexp-quote "Second paragraph."))))
  3401. ;; 3. Headline tests.
  3402. (org-test-with-temp-text "
  3403. * Head 1
  3404. ** Head 1.1
  3405. *** Head 1.1.1
  3406. ** Head 1.2"
  3407. ;; 3.1. At an headline beginning: move to next headline at the
  3408. ;; same level.
  3409. (goto-line 3)
  3410. (org-forward-element)
  3411. (should (looking-at (regexp-quote "** Head 1.2")))
  3412. ;; 3.2. At an headline beginning: move to parent headline if no
  3413. ;; headline at the same level.
  3414. (goto-line 3)
  3415. (org-forward-element)
  3416. (should (looking-at (regexp-quote "** Head 1.2"))))
  3417. ;; 4. Greater element tests.
  3418. (org-test-with-temp-text
  3419. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  3420. ;; 4.1. At a greater element: expected to skip contents.
  3421. (org-forward-element)
  3422. (should (looking-at (regexp-quote "Outside.")))
  3423. ;; 4.2. At the end of greater element contents: expected to skip
  3424. ;; to the end of the greater element.
  3425. (goto-line 2)
  3426. (org-forward-element)
  3427. (should (looking-at (regexp-quote "Outside."))))
  3428. ;; 5. List tests.
  3429. (org-test-with-temp-text "
  3430. - item1
  3431. - sub1
  3432. - sub2
  3433. - sub3
  3434. Inner paragraph.
  3435. - item2
  3436. Outside."
  3437. ;; 5.1. At list top point: expected to move to the element after
  3438. ;; the list.
  3439. (goto-line 2)
  3440. (org-forward-element)
  3441. (should (looking-at (regexp-quote "Outside.")))
  3442. ;; 5.2. Special case: at the first line of a sub-list, but not at
  3443. ;; beginning of line, move to next item.
  3444. (goto-line 2)
  3445. (forward-char)
  3446. (org-forward-element)
  3447. (should (looking-at "- item2"))
  3448. (goto-line 4)
  3449. (forward-char)
  3450. (org-forward-element)
  3451. (should (looking-at " - sub2"))
  3452. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  3453. (goto-line 4)
  3454. (org-forward-element)
  3455. (should (looking-at (regexp-quote " Inner paragraph.")))
  3456. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  3457. (goto-line 8)
  3458. (org-forward-element)
  3459. (should (looking-at (regexp-quote " Inner paragraph.")))
  3460. ;; 5.5. At an item: expected to move to next item, if any.
  3461. (goto-line 6)
  3462. (org-forward-element)
  3463. (should (looking-at " - sub3"))))
  3464. (ert-deftest test-org/backward-element ()
  3465. "Test `org-backward-element' specifications."
  3466. ;; 1. Should error at BOB.
  3467. (org-test-with-temp-text " \nParagraph."
  3468. (should-error (org-backward-element)))
  3469. ;; 2. Should move at BOB when called on the first element in buffer.
  3470. (should
  3471. (org-test-with-temp-text "\n#+TITLE: test"
  3472. (progn (forward-line)
  3473. (org-backward-element)
  3474. (bobp))))
  3475. ;; 3. Not at the beginning of an element: move at its beginning.
  3476. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3477. (goto-line 3)
  3478. (end-of-line)
  3479. (org-backward-element)
  3480. (should (looking-at (regexp-quote "Paragraph2."))))
  3481. ;; 4. Headline tests.
  3482. (org-test-with-temp-text "
  3483. * Head 1
  3484. ** Head 1.1
  3485. *** Head 1.1.1
  3486. ** Head 1.2"
  3487. ;; 4.1. At an headline beginning: move to previous headline at the
  3488. ;; same level.
  3489. (goto-line 5)
  3490. (org-backward-element)
  3491. (should (looking-at (regexp-quote "** Head 1.1")))
  3492. ;; 4.2. At an headline beginning: move to parent headline if no
  3493. ;; headline at the same level.
  3494. (goto-line 3)
  3495. (org-backward-element)
  3496. (should (looking-at (regexp-quote "* Head 1")))
  3497. ;; 4.3. At the first top-level headline: should error.
  3498. (goto-line 2)
  3499. (should-error (org-backward-element)))
  3500. ;; 5. At beginning of first element inside a greater element:
  3501. ;; expected to move to greater element's beginning.
  3502. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  3503. (goto-line 3)
  3504. (org-backward-element)
  3505. (should (looking-at "#\\+BEGIN_CENTER")))
  3506. ;; 6. At the beginning of the first element in a section: should
  3507. ;; move back to headline, if any.
  3508. (should
  3509. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  3510. (progn (goto-char (point-max))
  3511. (beginning-of-line)
  3512. (org-backward-element)
  3513. (org-at-heading-p))))
  3514. ;; 7. List tests.
  3515. (org-test-with-temp-text "
  3516. - item1
  3517. - sub1
  3518. - sub2
  3519. - sub3
  3520. Inner paragraph.
  3521. - item2
  3522. Outside."
  3523. ;; 7.1. At beginning of sub-list: expected to move to the
  3524. ;; paragraph before it.
  3525. (goto-line 4)
  3526. (org-backward-element)
  3527. (should (looking-at "item1"))
  3528. ;; 7.2. At an item in a list: expected to move at previous item.
  3529. (goto-line 8)
  3530. (org-backward-element)
  3531. (should (looking-at " - sub2"))
  3532. (goto-line 12)
  3533. (org-backward-element)
  3534. (should (looking-at "- item1"))
  3535. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3536. ;; beginning.
  3537. (goto-line 10)
  3538. (org-backward-element)
  3539. (should (looking-at " - sub1"))
  3540. (goto-line 15)
  3541. (org-backward-element)
  3542. (should (looking-at "- item1"))
  3543. ;; 7.4. At blank-lines before list end: expected to move to top
  3544. ;; item.
  3545. (goto-line 14)
  3546. (org-backward-element)
  3547. (should (looking-at "- item1"))))
  3548. (ert-deftest test-org/up-element ()
  3549. "Test `org-up-element' specifications."
  3550. ;; 1. At BOB or with no surrounding element: should error.
  3551. (org-test-with-temp-text "Paragraph."
  3552. (should-error (org-up-element)))
  3553. (org-test-with-temp-text "* Head1\n* Head2"
  3554. (goto-line 2)
  3555. (should-error (org-up-element)))
  3556. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3557. (goto-line 3)
  3558. (should-error (org-up-element)))
  3559. ;; 2. At an headline: move to parent headline.
  3560. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3561. (goto-line 3)
  3562. (org-up-element)
  3563. (should (looking-at "\\* Head1")))
  3564. ;; 3. Inside a greater element: move to greater element beginning.
  3565. (org-test-with-temp-text
  3566. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3567. (goto-line 3)
  3568. (org-up-element)
  3569. (should (looking-at "#\\+BEGIN_CENTER")))
  3570. ;; 4. List tests.
  3571. (org-test-with-temp-text "* Top
  3572. - item1
  3573. - sub1
  3574. - sub2
  3575. Paragraph within sub2.
  3576. - item2"
  3577. ;; 4.1. Within an item: move to the item beginning.
  3578. (goto-line 8)
  3579. (org-up-element)
  3580. (should (looking-at " - sub2"))
  3581. ;; 4.2. At an item in a sub-list: move to parent item.
  3582. (goto-line 4)
  3583. (org-up-element)
  3584. (should (looking-at "- item1"))
  3585. ;; 4.3. At an item in top list: move to beginning of whole list.
  3586. (goto-line 10)
  3587. (org-up-element)
  3588. (should (looking-at "- item1"))
  3589. ;; 4.4. Special case. At very top point: should move to parent of
  3590. ;; list.
  3591. (goto-line 2)
  3592. (org-up-element)
  3593. (should (looking-at "\\* Top"))))
  3594. (ert-deftest test-org/down-element ()
  3595. "Test `org-down-element' specifications."
  3596. ;; Error when the element hasn't got a recursive type.
  3597. (org-test-with-temp-text "Paragraph."
  3598. (should-error (org-down-element)))
  3599. ;; Error when the element has no contents
  3600. (org-test-with-temp-text "* Headline"
  3601. (should-error (org-down-element)))
  3602. ;; When at a plain-list, move to first item.
  3603. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3604. (goto-line 2)
  3605. (org-down-element)
  3606. (should (looking-at " - Item 1.1")))
  3607. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3608. (org-down-element)
  3609. (should (looking-at " Item 1")))
  3610. ;; When at a table, move to first row
  3611. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3612. (org-down-element)
  3613. (should (looking-at " a | b |")))
  3614. ;; Otherwise, move inside the greater element.
  3615. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3616. (org-down-element)
  3617. (should (looking-at "Paragraph"))))
  3618. (ert-deftest test-org/drag-element-backward ()
  3619. "Test `org-drag-element-backward' specifications."
  3620. ;; Standard test.
  3621. (should
  3622. (equal
  3623. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3624. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3625. (org-drag-element-backward)
  3626. (buffer-string))))
  3627. (should
  3628. (equal
  3629. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3630. (org-test-with-temp-text
  3631. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3632. (org-drag-element-backward)
  3633. (buffer-string))))
  3634. ;; Preserve blank lines.
  3635. (should
  3636. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3637. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3638. (org-drag-element-backward)
  3639. (buffer-string))))
  3640. ;; Preserve column.
  3641. (should
  3642. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3643. (org-drag-element-backward)
  3644. (looking-at-p "2")))
  3645. ;; Error when trying to move first element of buffer.
  3646. (should-error
  3647. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3648. (org-drag-element-backward))
  3649. :type 'user-error)
  3650. ;; Error when trying to swap nested elements.
  3651. (should-error
  3652. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  3653. (org-drag-element-backward))
  3654. :type 'user-error)
  3655. ;; Error when trying to swap an headline element and a non-headline
  3656. ;; element.
  3657. (should-error
  3658. (org-test-with-temp-text "Test.\n<point>* Head 1"
  3659. (org-drag-element-backward))
  3660. :type 'error)
  3661. ;; Error when called before first element.
  3662. (should-error
  3663. (org-test-with-temp-text "\n<point>"
  3664. (org-drag-element-backward))
  3665. :type 'user-error)
  3666. ;; Preserve visibility of elements and their contents.
  3667. (should
  3668. (equal '((63 . 82) (26 . 48))
  3669. (org-test-with-temp-text "
  3670. #+BEGIN_CENTER
  3671. Text.
  3672. #+END_CENTER
  3673. - item 1
  3674. #+BEGIN_QUOTE
  3675. Text.
  3676. #+END_QUOTE"
  3677. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3678. (search-backward "- item 1")
  3679. (org-drag-element-backward)
  3680. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3681. (overlays-in (point-min) (point-max))))))
  3682. ;; Pathological case: handle call with point in blank lines right
  3683. ;; after a headline.
  3684. (should
  3685. (equal "* H2\n* H1\nText\n\n"
  3686. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  3687. (org-drag-element-backward)
  3688. (buffer-string)))))
  3689. (ert-deftest test-org/drag-element-forward ()
  3690. "Test `org-drag-element-forward' specifications."
  3691. ;; 1. Error when trying to move first element of buffer.
  3692. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3693. (goto-line 3)
  3694. (should-error (org-drag-element-forward)))
  3695. ;; 2. Error when trying to swap nested elements.
  3696. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3697. (forward-line)
  3698. (should-error (org-drag-element-forward)))
  3699. ;; 3. Error when trying to swap a non-headline element and an
  3700. ;; headline.
  3701. (org-test-with-temp-text "Test.\n* Head 1"
  3702. (should-error (org-drag-element-forward)))
  3703. ;; 4. Error when called before first element.
  3704. (should-error
  3705. (org-test-with-temp-text "\n"
  3706. (forward-line)
  3707. (org-drag-element-backward))
  3708. :type 'user-error)
  3709. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3710. ;; between elements.
  3711. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3712. (search-forward "graph")
  3713. (org-drag-element-forward)
  3714. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3715. (should (looking-at " 1")))
  3716. ;; 5. Preserve visibility of elements and their contents.
  3717. (org-test-with-temp-text "
  3718. #+BEGIN_CENTER
  3719. Text.
  3720. #+END_CENTER
  3721. - item 1
  3722. #+BEGIN_QUOTE
  3723. Text.
  3724. #+END_QUOTE"
  3725. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3726. (search-backward "#+BEGIN_CENTER")
  3727. (org-drag-element-forward)
  3728. (should
  3729. (equal
  3730. '((63 . 82) (26 . 48))
  3731. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3732. (overlays-in (point-min) (point-max)))))))
  3733. (ert-deftest test-org/next-block ()
  3734. "Test `org-next-block' specifications."
  3735. ;; Regular test.
  3736. (should
  3737. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3738. (org-next-block 1)
  3739. (looking-at "#\\+BEGIN_CENTER")))
  3740. ;; Ignore case.
  3741. (should
  3742. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3743. (let ((case-fold-search nil))
  3744. (org-next-block 1)
  3745. (looking-at "#\\+begin_center"))))
  3746. ;; Ignore current line.
  3747. (should
  3748. (org-test-with-temp-text
  3749. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3750. (org-next-block 1)
  3751. (looking-at "#\\+BEGIN_CENTER")))
  3752. ;; Throw an error when no block is found.
  3753. (should-error
  3754. (org-test-with-temp-text "Paragraph"
  3755. (org-next-block 1)))
  3756. ;; With an argument, skip many blocks at once.
  3757. (should
  3758. (org-test-with-temp-text
  3759. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3760. (org-next-block 2)
  3761. (looking-at "#\\+BEGIN_QUOTE")))
  3762. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3763. (should
  3764. (org-test-with-temp-text
  3765. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3766. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3767. (looking-at "#\\+BEGIN_QUOTE")))
  3768. ;; Optional argument is also case-insensitive.
  3769. (should
  3770. (org-test-with-temp-text
  3771. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3772. (let ((case-fold-search nil))
  3773. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3774. (looking-at "#\\+begin_quote")))))
  3775. (ert-deftest test-org/previous-block ()
  3776. "Test `org-previous-block' specifications."
  3777. ;; Regular test.
  3778. (should
  3779. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  3780. (org-previous-block 1)
  3781. (looking-at "#\\+BEGIN_CENTER")))
  3782. ;; Ignore case.
  3783. (should
  3784. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  3785. (let ((case-fold-search nil))
  3786. (org-previous-block 1)
  3787. (looking-at "#\\+begin_center"))))
  3788. ;; Ignore current line.
  3789. (should
  3790. (org-test-with-temp-text
  3791. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  3792. (org-previous-block 1)
  3793. (looking-at "#\\+BEGIN_QUOTE")))
  3794. ;; Throw an error when no block is found.
  3795. (should-error
  3796. (org-test-with-temp-text "Paragraph<point>"
  3797. (org-previous-block 1)))
  3798. ;; With an argument, skip many blocks at once.
  3799. (should
  3800. (org-test-with-temp-text
  3801. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3802. (org-previous-block 2)
  3803. (looking-at "#\\+BEGIN_CENTER")))
  3804. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3805. (should
  3806. (org-test-with-temp-text
  3807. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3808. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3809. (looking-at "#\\+BEGIN_QUOTE")))
  3810. ;; Optional argument is also case-insensitive.
  3811. (should
  3812. (org-test-with-temp-text
  3813. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  3814. (let ((case-fold-search nil))
  3815. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3816. (looking-at "#\\+begin_quote")))))
  3817. ;;; Outline structure
  3818. (ert-deftest test-org/demote ()
  3819. "Test `org-demote' specifications."
  3820. ;; Add correct number of stars according to `org-odd-levels-only'.
  3821. (should
  3822. (= 2
  3823. (org-test-with-temp-text "* H"
  3824. (let ((org-odd-levels-only nil)) (org-demote))
  3825. (org-current-level))))
  3826. (should
  3827. (= 3
  3828. (org-test-with-temp-text "* H"
  3829. (let ((org-odd-levels-only t)) (org-demote))
  3830. (org-current-level))))
  3831. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3832. (should
  3833. (org-test-with-temp-text "* H :tag:"
  3834. (let ((org-tags-column 10)
  3835. (org-auto-align-tags t)
  3836. (org-odd-levels-only nil))
  3837. (org-demote))
  3838. (org-move-to-column 10)
  3839. (looking-at-p ":tag:$")))
  3840. (should-not
  3841. (org-test-with-temp-text "* H :tag:"
  3842. (let ((org-tags-column 10)
  3843. (org-auto-align-tags nil)
  3844. (org-odd-levels-only nil))
  3845. (org-demote))
  3846. (org-move-to-column 10)
  3847. (looking-at-p ":tag:$")))
  3848. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3849. ;; info and property drawers accordingly.
  3850. (should
  3851. (= 3
  3852. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3853. (let ((org-odd-levels-only nil)
  3854. (org-adapt-indentation t))
  3855. (org-demote))
  3856. (forward-line)
  3857. (org-get-indentation))))
  3858. (should
  3859. (= 3
  3860. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3861. (let ((org-odd-levels-only nil)
  3862. (org-adapt-indentation t))
  3863. (org-demote))
  3864. (forward-line)
  3865. (org-get-indentation))))
  3866. (should-not
  3867. (= 3
  3868. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3869. (let ((org-odd-levels-only nil)
  3870. (org-adapt-indentation nil))
  3871. (org-demote))
  3872. (forward-line)
  3873. (org-get-indentation))))
  3874. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3875. ;; section accordingly. Ignore, however, footnote definitions and
  3876. ;; inlinetasks boundaries.
  3877. (should
  3878. (= 3
  3879. (org-test-with-temp-text "* H\n Paragraph"
  3880. (let ((org-odd-levels-only nil)
  3881. (org-adapt-indentation t))
  3882. (org-demote))
  3883. (forward-line)
  3884. (org-get-indentation))))
  3885. (should
  3886. (= 2
  3887. (org-test-with-temp-text "* H\n Paragraph"
  3888. (let ((org-odd-levels-only nil)
  3889. (org-adapt-indentation nil))
  3890. (org-demote))
  3891. (forward-line)
  3892. (org-get-indentation))))
  3893. (should
  3894. (zerop
  3895. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  3896. (let ((org-odd-levels-only nil)
  3897. (org-adapt-indentation t))
  3898. (org-demote))
  3899. (goto-char (point-max))
  3900. (org-get-indentation))))
  3901. (should
  3902. (= 3
  3903. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  3904. (let ((org-odd-levels-only nil)
  3905. (org-adapt-indentation t))
  3906. (org-demote))
  3907. (goto-char (point-max))
  3908. (org-get-indentation))))
  3909. (when (featurep 'org-inlinetask)
  3910. (should
  3911. (zerop
  3912. (let ((org-inlinetask-min-level 5)
  3913. (org-adapt-indentation t))
  3914. (org-test-with-temp-text "* H\n***** I\n***** END"
  3915. (org-demote)
  3916. (forward-line)
  3917. (org-get-indentation))))))
  3918. (when (featurep 'org-inlinetask)
  3919. (should
  3920. (= 3
  3921. (let ((org-inlinetask-min-level 5)
  3922. (org-adapt-indentation t))
  3923. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  3924. (org-demote)
  3925. (forward-line 2)
  3926. (org-get-indentation))))))
  3927. ;; Ignore contents of source blocks or example blocks when
  3928. ;; indentation should be preserved (through
  3929. ;; `org-src-preserve-indentation' or "-i" flag).
  3930. (should-not
  3931. (zerop
  3932. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3933. (let ((org-adapt-indentation t)
  3934. (org-src-preserve-indentation nil))
  3935. (org-demote))
  3936. (forward-line 2)
  3937. (org-get-indentation))))
  3938. (should
  3939. (zerop
  3940. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3941. (let ((org-adapt-indentation t)
  3942. (org-src-preserve-indentation t))
  3943. (org-demote))
  3944. (forward-line 2)
  3945. (org-get-indentation))))
  3946. (should
  3947. (zerop
  3948. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3949. (let ((org-adapt-indentation t)
  3950. (org-src-preserve-indentation t))
  3951. (org-demote))
  3952. (forward-line 2)
  3953. (org-get-indentation))))
  3954. (should
  3955. (zerop
  3956. (org-test-with-temp-text
  3957. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  3958. (let ((org-adapt-indentation t)
  3959. (org-src-preserve-indentation nil))
  3960. (org-demote))
  3961. (forward-line 2)
  3962. (org-get-indentation)))))
  3963. (ert-deftest test-org/promote ()
  3964. "Test `org-promote' specifications."
  3965. ;; Return an error if headline is to be promoted to level 0, unless
  3966. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  3967. ;; headline becomes a comment.
  3968. (should-error
  3969. (org-test-with-temp-text "* H"
  3970. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  3971. (should
  3972. (equal "# H"
  3973. (org-test-with-temp-text "* H"
  3974. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  3975. (buffer-string))))
  3976. ;; Remove correct number of stars according to
  3977. ;; `org-odd-levels-only'.
  3978. (should
  3979. (= 2
  3980. (org-test-with-temp-text "*** H"
  3981. (let ((org-odd-levels-only nil)) (org-promote))
  3982. (org-current-level))))
  3983. (should
  3984. (= 1
  3985. (org-test-with-temp-text "*** H"
  3986. (let ((org-odd-levels-only t)) (org-promote))
  3987. (org-current-level))))
  3988. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3989. (should
  3990. (org-test-with-temp-text "** H :tag:"
  3991. (let ((org-tags-column 10)
  3992. (org-auto-align-tags t)
  3993. (org-odd-levels-only nil))
  3994. (org-promote))
  3995. (org-move-to-column 10)
  3996. (looking-at-p ":tag:$")))
  3997. (should-not
  3998. (org-test-with-temp-text "** H :tag:"
  3999. (let ((org-tags-column 10)
  4000. (org-auto-align-tags nil)
  4001. (org-odd-levels-only nil))
  4002. (org-promote))
  4003. (org-move-to-column 10)
  4004. (looking-at-p ":tag:$")))
  4005. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4006. ;; info and property drawers.
  4007. (should
  4008. (= 2
  4009. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4010. (let ((org-odd-levels-only nil)
  4011. (org-adapt-indentation t))
  4012. (org-promote))
  4013. (forward-line)
  4014. (org-get-indentation))))
  4015. (should
  4016. (= 2
  4017. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4018. (let ((org-odd-levels-only nil)
  4019. (org-adapt-indentation t))
  4020. (org-promote))
  4021. (forward-line)
  4022. (org-get-indentation))))
  4023. (should-not
  4024. (= 2
  4025. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4026. (let ((org-odd-levels-only nil)
  4027. (org-adapt-indentation nil))
  4028. (org-promote))
  4029. (forward-line)
  4030. (org-get-indentation))))
  4031. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4032. ;; section accordingly. Ignore, however, footnote definitions and
  4033. ;; inlinetasks boundaries.
  4034. (should
  4035. (= 2
  4036. (org-test-with-temp-text "** H\n Paragraph"
  4037. (let ((org-odd-levels-only nil)
  4038. (org-adapt-indentation t))
  4039. (org-promote))
  4040. (forward-line)
  4041. (org-get-indentation))))
  4042. (should-not
  4043. (= 2
  4044. (org-test-with-temp-text "** H\n Paragraph"
  4045. (let ((org-odd-levels-only nil)
  4046. (org-adapt-indentation nil))
  4047. (org-promote))
  4048. (forward-line)
  4049. (org-get-indentation))))
  4050. (should
  4051. (= 2
  4052. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  4053. (let ((org-odd-levels-only nil)
  4054. (org-adapt-indentation t))
  4055. (org-promote))
  4056. (forward-line)
  4057. (org-get-indentation))))
  4058. (when (featurep 'org-inlinetask)
  4059. (should
  4060. (zerop
  4061. (let ((org-inlinetask-min-level 5)
  4062. (org-adapt-indentation t))
  4063. (org-test-with-temp-text "** H\n***** I\n***** END"
  4064. (org-promote)
  4065. (forward-line)
  4066. (org-get-indentation))))))
  4067. (when (featurep 'org-inlinetask)
  4068. (should
  4069. (= 2
  4070. (let ((org-inlinetask-min-level 5)
  4071. (org-adapt-indentation t))
  4072. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  4073. (org-promote)
  4074. (forward-line 2)
  4075. (org-get-indentation))))))
  4076. ;; Give up shifting if it would break document's structure
  4077. ;; otherwise.
  4078. (should
  4079. (= 3
  4080. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  4081. (let ((org-odd-levels-only nil)
  4082. (org-adapt-indentation t))
  4083. (org-promote))
  4084. (forward-line)
  4085. (org-get-indentation))))
  4086. (should
  4087. (= 3
  4088. (org-test-with-temp-text "** H\n Paragraph\n * list."
  4089. (let ((org-odd-levels-only nil)
  4090. (org-adapt-indentation t))
  4091. (org-promote))
  4092. (forward-line)
  4093. (org-get-indentation))))
  4094. ;; Ignore contents of source blocks or example blocks when
  4095. ;; indentation should be preserved (through
  4096. ;; `org-src-preserve-indentation' or "-i" flag).
  4097. (should-not
  4098. (zerop
  4099. (org-test-with-temp-text
  4100. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4101. (let ((org-adapt-indentation t)
  4102. (org-src-preserve-indentation nil)
  4103. (org-odd-levels-only nil))
  4104. (org-promote))
  4105. (forward-line)
  4106. (org-get-indentation))))
  4107. (should
  4108. (zerop
  4109. (org-test-with-temp-text
  4110. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  4111. (let ((org-adapt-indentation t)
  4112. (org-src-preserve-indentation t)
  4113. (org-odd-levels-only nil))
  4114. (org-promote))
  4115. (forward-line)
  4116. (org-get-indentation))))
  4117. (should
  4118. (zerop
  4119. (org-test-with-temp-text
  4120. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4121. (let ((org-adapt-indentation t)
  4122. (org-src-preserve-indentation t)
  4123. (org-odd-levels-only nil))
  4124. (org-promote))
  4125. (forward-line)
  4126. (org-get-indentation))))
  4127. (should
  4128. (zerop
  4129. (org-test-with-temp-text
  4130. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  4131. (let ((org-adapt-indentation t)
  4132. (org-src-preserve-indentation nil)
  4133. (org-odd-levels-only nil))
  4134. (org-promote))
  4135. (forward-line)
  4136. (org-get-indentation)))))
  4137. (ert-deftest test-org/org-get-valid-level ()
  4138. "Test function `org-get-valid-level' specifications."
  4139. (let ((org-odd-levels-only nil))
  4140. (should (equal 1 (org-get-valid-level 0 0)))
  4141. (should (equal 1 (org-get-valid-level 0 1)))
  4142. (should (equal 2 (org-get-valid-level 0 2)))
  4143. (should (equal 3 (org-get-valid-level 0 3)))
  4144. (should (equal 1 (org-get-valid-level 1 0)))
  4145. (should (equal 2 (org-get-valid-level 1 1)))
  4146. (should (equal 23 (org-get-valid-level 1 22)))
  4147. (should (equal 1 (org-get-valid-level 1 -1)))
  4148. (should (equal 1 (org-get-valid-level 2 -1))))
  4149. (let ((org-odd-levels-only t))
  4150. (should (equal 1 (org-get-valid-level 0 0)))
  4151. (should (equal 1 (org-get-valid-level 0 1)))
  4152. (should (equal 3 (org-get-valid-level 0 2)))
  4153. (should (equal 5 (org-get-valid-level 0 3)))
  4154. (should (equal 1 (org-get-valid-level 1 0)))
  4155. (should (equal 3 (org-get-valid-level 1 1)))
  4156. (should (equal 3 (org-get-valid-level 2 1)))
  4157. (should (equal 5 (org-get-valid-level 3 1)))
  4158. (should (equal 5 (org-get-valid-level 4 1)))
  4159. (should (equal 43 (org-get-valid-level 1 21)))
  4160. (should (equal 1 (org-get-valid-level 1 -1)))
  4161. (should (equal 1 (org-get-valid-level 2 -1)))
  4162. (should (equal 1 (org-get-valid-level 3 -1)))
  4163. (should (equal 3 (org-get-valid-level 4 -1)))
  4164. (should (equal 3 (org-get-valid-level 5 -1)))))
  4165. ;;; Planning
  4166. (ert-deftest test-org/at-planning-p ()
  4167. "Test `org-at-planning-p' specifications."
  4168. ;; Regular test.
  4169. (should
  4170. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  4171. (org-at-planning-p)))
  4172. (should-not
  4173. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  4174. (org-at-planning-p)))
  4175. ;; Correctly find planning attached to inlinetasks.
  4176. (when (featurep 'org-inlinetask)
  4177. (should
  4178. (org-test-with-temp-text
  4179. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  4180. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4181. (should-not
  4182. (org-test-with-temp-text
  4183. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4184. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4185. (should-not
  4186. (org-test-with-temp-text
  4187. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4188. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4189. (should-not
  4190. (org-test-with-temp-text
  4191. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  4192. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  4193. (ert-deftest test-org/add-planning-info ()
  4194. "Test `org-add-planning-info'."
  4195. ;; Create deadline when `org-adapt-indentation' is non-nil.
  4196. (should
  4197. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4198. (org-test-with-temp-text "* H\nParagraph<point>"
  4199. (let ((org-adapt-indentation t))
  4200. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4201. (replace-regexp-in-string
  4202. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4203. nil nil 1))))
  4204. ;; Create deadline when `org-adapt-indentation' is nil.
  4205. (should
  4206. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4207. (org-test-with-temp-text "* H\nParagraph<point>"
  4208. (let ((org-adapt-indentation nil))
  4209. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4210. (replace-regexp-in-string
  4211. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4212. nil nil 1))))
  4213. ;; Update deadline when `org-adapt-indentation' is non-nil.
  4214. (should
  4215. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4216. (org-test-with-temp-text "\
  4217. * H
  4218. DEADLINE: <2015-06-24 Wed>
  4219. Paragraph<point>"
  4220. (let ((org-adapt-indentation t))
  4221. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4222. (replace-regexp-in-string
  4223. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4224. nil nil 1))))
  4225. ;; Update deadline when `org-adapt-indentation' is nil.
  4226. (should
  4227. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4228. (org-test-with-temp-text "\
  4229. * H
  4230. DEADLINE: <2015-06-24 Wed>
  4231. Paragraph<point>"
  4232. (let ((org-adapt-indentation nil))
  4233. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4234. (replace-regexp-in-string
  4235. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4236. nil nil 1))))
  4237. ;; Schedule when `org-adapt-indentation' is non-nil.
  4238. (should
  4239. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  4240. (org-test-with-temp-text "* H\nParagraph<point>"
  4241. (let ((org-adapt-indentation t))
  4242. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4243. (replace-regexp-in-string
  4244. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4245. nil nil 1))))
  4246. ;; Schedule when `org-adapt-indentation' is nil.
  4247. (should
  4248. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  4249. (org-test-with-temp-text "* H\nParagraph<point>"
  4250. (let ((org-adapt-indentation nil))
  4251. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4252. (replace-regexp-in-string
  4253. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4254. nil nil 1))))
  4255. ;; Add deadline when scheduled.
  4256. (should
  4257. (equal "\
  4258. * H
  4259. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  4260. Paragraph"
  4261. (org-test-with-temp-text "\
  4262. * H
  4263. SCHEDULED: <2015-06-24 Wed>
  4264. Paragraph<point>"
  4265. (let ((org-adapt-indentation t))
  4266. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4267. (replace-regexp-in-string
  4268. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4269. nil nil 1))))
  4270. ;; Remove middle entry.
  4271. (should
  4272. (equal "\
  4273. * H
  4274. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  4275. Paragraph"
  4276. (org-test-with-temp-text "\
  4277. * H
  4278. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4279. Paragraph<point>"
  4280. (let ((org-adapt-indentation t))
  4281. (org-add-planning-info nil nil 'deadline))
  4282. (replace-regexp-in-string
  4283. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4284. nil nil 1))))
  4285. ;; Remove last entry and then middle entry (order should not
  4286. ;; matter).
  4287. (should
  4288. (equal "\
  4289. * H
  4290. CLOSED: [2015-06-24]
  4291. Paragraph"
  4292. (org-test-with-temp-text "\
  4293. * H
  4294. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4295. Paragraph<point>"
  4296. (let ((org-adapt-indentation t))
  4297. (org-add-planning-info nil nil 'scheduled 'deadline))
  4298. (replace-regexp-in-string
  4299. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4300. nil nil 1))))
  4301. ;; Remove closed when `org-adapt-indentation' is non-nil.
  4302. (should
  4303. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4304. (org-test-with-temp-text "\
  4305. * H
  4306. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4307. Paragraph<point>"
  4308. (let ((org-adapt-indentation t))
  4309. (org-add-planning-info nil nil 'closed))
  4310. (replace-regexp-in-string
  4311. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4312. nil nil 1))))
  4313. (should
  4314. (equal "* H\n Paragraph"
  4315. (org-test-with-temp-text "\
  4316. * H
  4317. CLOSED: [2015-06-25 Thu]
  4318. Paragraph<point>"
  4319. (let ((org-adapt-indentation t))
  4320. (org-add-planning-info nil nil 'closed))
  4321. (replace-regexp-in-string
  4322. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4323. nil nil 1))))
  4324. ;; Remove closed when `org-adapt-indentation' is nil.
  4325. (should
  4326. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4327. (org-test-with-temp-text "\
  4328. * H
  4329. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4330. Paragraph<point>"
  4331. (let ((org-adapt-indentation nil))
  4332. (org-add-planning-info nil nil 'closed))
  4333. (replace-regexp-in-string
  4334. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4335. nil nil 1))))
  4336. (should
  4337. (equal "* H\nParagraph"
  4338. (org-test-with-temp-text "\
  4339. * H
  4340. CLOSED: [2015-06-25 Thu]
  4341. Paragraph<point>"
  4342. (let ((org-adapt-indentation nil))
  4343. (org-add-planning-info nil nil 'closed))
  4344. (replace-regexp-in-string
  4345. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4346. nil nil 1))))
  4347. ;; Remove closed entry and delete empty line.
  4348. (should
  4349. (equal "\
  4350. * H
  4351. Paragraph"
  4352. (org-test-with-temp-text "\
  4353. * H
  4354. CLOSED: [2015-06-24 Wed]
  4355. Paragraph<point>"
  4356. (let ((org-adapt-indentation t))
  4357. (org-add-planning-info nil nil 'closed))
  4358. (replace-regexp-in-string
  4359. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4360. nil nil 1))))
  4361. ;; Remove one entry and update another.
  4362. (should
  4363. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4364. (org-test-with-temp-text "\
  4365. * H
  4366. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  4367. Paragraph<point>"
  4368. (let ((org-adapt-indentation t))
  4369. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  4370. (replace-regexp-in-string
  4371. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4372. nil nil 1)))))
  4373. (ert-deftest test-org/deadline ()
  4374. "Test `org-deadline' specifications."
  4375. ;; Insert a new value or replace existing one.
  4376. (should
  4377. (equal "* H\nDEADLINE: <2012-03-29>\n"
  4378. (org-test-with-temp-text "* H"
  4379. (let ((org-adapt-indentation nil)
  4380. (org-last-inserted-timestamp nil))
  4381. (org-deadline nil "<2012-03-29 Tue>"))
  4382. (replace-regexp-in-string
  4383. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4384. nil nil 1))))
  4385. (should
  4386. (equal "* H\nDEADLINE: <2014-03-04>"
  4387. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4388. (let ((org-adapt-indentation nil)
  4389. (org-last-inserted-timestamp nil))
  4390. (org-deadline nil "<2014-03-04 Thu>"))
  4391. (replace-regexp-in-string
  4392. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4393. nil nil 1))))
  4394. ;; Accept delta time, e.g., "+2d".
  4395. (should
  4396. (equal "* H\nDEADLINE: <2015-03-04>\n"
  4397. (cl-letf (((symbol-function 'current-time)
  4398. (lambda (&rest args)
  4399. (apply #'encode-time
  4400. (org-parse-time-string "2014-03-04")))))
  4401. (org-test-with-temp-text "* H"
  4402. (let ((org-adapt-indentation nil)
  4403. (org-last-inserted-timestamp nil))
  4404. (org-deadline nil "+1y"))
  4405. (replace-regexp-in-string
  4406. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4407. ;; Preserve repeater.
  4408. (should
  4409. (equal "* H\nDEADLINE: <2012-03-29 +2y>\n"
  4410. (org-test-with-temp-text "* H"
  4411. (let ((org-adapt-indentation nil)
  4412. (org-last-inserted-timestamp nil))
  4413. (org-deadline nil "<2012-03-29 Tue +2y>"))
  4414. (replace-regexp-in-string
  4415. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4416. ;; Remove CLOSED keyword, if any.
  4417. (should
  4418. (equal "* H\nDEADLINE: <2012-03-29>"
  4419. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4420. (let ((org-adapt-indentation nil)
  4421. (org-last-inserted-timestamp nil))
  4422. (org-deadline nil "<2012-03-29 Tue>"))
  4423. (replace-regexp-in-string
  4424. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4425. ;; With C-u argument, remove DEADLINE keyword.
  4426. (should
  4427. (equal "* H\n"
  4428. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4429. (let ((org-adapt-indentation nil)
  4430. (org-last-inserted-timestamp nil))
  4431. (org-deadline '(4)))
  4432. (buffer-string))))
  4433. (should
  4434. (equal "* H"
  4435. (org-test-with-temp-text "* H"
  4436. (let ((org-adapt-indentation nil)
  4437. (org-last-inserted-timestamp nil))
  4438. (org-deadline '(4)))
  4439. (buffer-string))))
  4440. ;; With C-u C-u argument, prompt for a delay cookie.
  4441. (should
  4442. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  4443. (cl-letf (((symbol-function 'org-read-date)
  4444. (lambda (&rest args)
  4445. (apply #'encode-time
  4446. (org-parse-time-string "2014-03-04")))))
  4447. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4448. (let ((org-adapt-indentation nil)
  4449. (org-last-inserted-timestamp nil))
  4450. (org-deadline '(16)))
  4451. (buffer-string)))))
  4452. (should-error
  4453. (cl-letf (((symbol-function 'org-read-date)
  4454. (lambda (&rest args)
  4455. (apply #'encode-time
  4456. (org-parse-time-string "2014-03-04")))))
  4457. (org-test-with-temp-text "* H"
  4458. (let ((org-adapt-indentation nil)
  4459. (org-last-inserted-timestamp nil))
  4460. (org-deadline '(16)))
  4461. (buffer-string))))
  4462. ;; When a region is active and
  4463. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4464. ;; same value in all headlines in region.
  4465. (should
  4466. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4467. (org-test-with-temp-text "* H1\n* H2"
  4468. (let ((org-adapt-indentation nil)
  4469. (org-last-inserted-timestamp nil)
  4470. (org-loop-over-headlines-in-active-region t))
  4471. (transient-mark-mode 1)
  4472. (push-mark (point) t t)
  4473. (goto-char (point-max))
  4474. (org-deadline nil "2012-03-29"))
  4475. (replace-regexp-in-string
  4476. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4477. (should-not
  4478. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4479. (org-test-with-temp-text "* H1\n* H2"
  4480. (let ((org-adapt-indentation nil)
  4481. (org-last-inserted-timestamp nil)
  4482. (org-loop-over-headlines-in-active-region nil))
  4483. (transient-mark-mode 1)
  4484. (push-mark (point) t t)
  4485. (goto-char (point-max))
  4486. (org-deadline nil "2012-03-29"))
  4487. (replace-regexp-in-string
  4488. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4489. (ert-deftest test-org/schedule ()
  4490. "Test `org-schedule' specifications."
  4491. ;; Insert a new value or replace existing one.
  4492. (should
  4493. (equal "* H\nSCHEDULED: <2012-03-29>\n"
  4494. (org-test-with-temp-text "* H"
  4495. (let ((org-adapt-indentation nil)
  4496. (org-last-inserted-timestamp nil))
  4497. (org-schedule nil "<2012-03-29 Tue>"))
  4498. (replace-regexp-in-string
  4499. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4500. nil nil 1))))
  4501. (should
  4502. (equal "* H\nSCHEDULED: <2014-03-04>"
  4503. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4504. (let ((org-adapt-indentation nil)
  4505. (org-last-inserted-timestamp nil))
  4506. (org-schedule nil "<2014-03-04 Thu>"))
  4507. (replace-regexp-in-string
  4508. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4509. nil nil 1))))
  4510. ;; Accept delta time, e.g., "+2d".
  4511. (should
  4512. (equal "* H\nSCHEDULED: <2015-03-04>\n"
  4513. (cl-letf (((symbol-function 'current-time)
  4514. (lambda (&rest args)
  4515. (apply #'encode-time
  4516. (org-parse-time-string "2014-03-04")))))
  4517. (org-test-with-temp-text "* H"
  4518. (let ((org-adapt-indentation nil)
  4519. (org-last-inserted-timestamp nil))
  4520. (org-schedule nil "+1y"))
  4521. (replace-regexp-in-string
  4522. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4523. ;; Preserve repeater.
  4524. (should
  4525. (equal "* H\nSCHEDULED: <2012-03-29 +2y>\n"
  4526. (org-test-with-temp-text "* H"
  4527. (let ((org-adapt-indentation nil)
  4528. (org-last-inserted-timestamp nil))
  4529. (org-schedule nil "<2012-03-29 Tue +2y>"))
  4530. (replace-regexp-in-string
  4531. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4532. ;; Remove CLOSED keyword, if any.
  4533. (should
  4534. (equal "* H\nSCHEDULED: <2012-03-29>"
  4535. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4536. (let ((org-adapt-indentation nil)
  4537. (org-last-inserted-timestamp nil))
  4538. (org-schedule nil "<2012-03-29 Tue>"))
  4539. (replace-regexp-in-string
  4540. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4541. ;; With C-u argument, remove SCHEDULED keyword.
  4542. (should
  4543. (equal "* H\n"
  4544. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4545. (let ((org-adapt-indentation nil)
  4546. (org-last-inserted-timestamp nil))
  4547. (org-schedule '(4)))
  4548. (buffer-string))))
  4549. (should
  4550. (equal "* H"
  4551. (org-test-with-temp-text "* H"
  4552. (let ((org-adapt-indentation nil)
  4553. (org-last-inserted-timestamp nil))
  4554. (org-schedule '(4)))
  4555. (buffer-string))))
  4556. ;; With C-u C-u argument, prompt for a delay cookie.
  4557. (should
  4558. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  4559. (cl-letf (((symbol-function 'org-read-date)
  4560. (lambda (&rest args)
  4561. (apply #'encode-time
  4562. (org-parse-time-string "2014-03-04")))))
  4563. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4564. (let ((org-adapt-indentation nil)
  4565. (org-last-inserted-timestamp nil))
  4566. (org-schedule '(16)))
  4567. (buffer-string)))))
  4568. (should-error
  4569. (cl-letf (((symbol-function 'org-read-date)
  4570. (lambda (&rest args)
  4571. (apply #'encode-time
  4572. (org-parse-time-string "2014-03-04")))))
  4573. (org-test-with-temp-text "* H"
  4574. (let ((org-adapt-indentation nil)
  4575. (org-last-inserted-timestamp nil))
  4576. (org-schedule '(16)))
  4577. (buffer-string))))
  4578. ;; When a region is active and
  4579. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4580. ;; same value in all headlines in region.
  4581. (should
  4582. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4583. (org-test-with-temp-text "* H1\n* H2"
  4584. (let ((org-adapt-indentation nil)
  4585. (org-last-inserted-timestamp nil)
  4586. (org-loop-over-headlines-in-active-region t))
  4587. (transient-mark-mode 1)
  4588. (push-mark (point) t t)
  4589. (goto-char (point-max))
  4590. (org-schedule nil "2012-03-29"))
  4591. (replace-regexp-in-string
  4592. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4593. (should-not
  4594. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4595. (org-test-with-temp-text "* H1\n* H2"
  4596. (let ((org-adapt-indentation nil)
  4597. (org-last-inserted-timestamp nil)
  4598. (org-loop-over-headlines-in-active-region nil))
  4599. (transient-mark-mode 1)
  4600. (push-mark (point) t t)
  4601. (goto-char (point-max))
  4602. (org-schedule nil "2012-03-29"))
  4603. (replace-regexp-in-string
  4604. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4605. (should
  4606. ;; check if a repeater survives re-scheduling.
  4607. (string-match-p
  4608. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  4609. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  4610. (let ((org-adapt-indentation nil)
  4611. (org-last-inserted-timestamp nil))
  4612. (org-schedule nil "2017-02-01"))
  4613. (buffer-string)))))
  4614. ;;; Property API
  4615. (ert-deftest test-org/buffer-property-keys ()
  4616. "Test `org-buffer-property-keys' specifications."
  4617. ;; Retrieve properties accross siblings.
  4618. (should
  4619. (equal '("A" "B")
  4620. (org-test-with-temp-text "
  4621. * H1
  4622. :PROPERTIES:
  4623. :A: 1
  4624. :END:
  4625. * H2
  4626. :PROPERTIES:
  4627. :B: 1
  4628. :END:"
  4629. (org-buffer-property-keys))))
  4630. ;; Retrieve properties accross children.
  4631. (should
  4632. (equal '("A" "B")
  4633. (org-test-with-temp-text "
  4634. * H1
  4635. :PROPERTIES:
  4636. :A: 1
  4637. :END:
  4638. ** H2
  4639. :PROPERTIES:
  4640. :B: 1
  4641. :END:"
  4642. (org-buffer-property-keys))))
  4643. ;; Retrieve muliple properties in the same drawer.
  4644. (should
  4645. (equal '("A" "B")
  4646. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4647. (org-buffer-property-keys))))
  4648. ;; Ignore extension symbol in property name.
  4649. (should
  4650. (equal '("A")
  4651. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4652. (org-buffer-property-keys))))
  4653. ;; With non-nil COLUMNS, extract property names from columns.
  4654. (should
  4655. (equal '("A" "B")
  4656. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  4657. (org-buffer-property-keys nil nil t))))
  4658. (should
  4659. (equal '("A" "B" "COLUMNS")
  4660. (org-test-with-temp-text
  4661. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  4662. (org-buffer-property-keys nil nil t))))
  4663. ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
  4664. (should
  4665. (equal '("A")
  4666. (org-test-with-temp-text
  4667. "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
  4668. (org-buffer-property-keys nil nil nil t)))))
  4669. (ert-deftest test-org/property-values ()
  4670. "Test `org-property-values' specifications."
  4671. ;; Regular test.
  4672. (should
  4673. (equal '("2" "1")
  4674. (org-test-with-temp-text
  4675. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  4676. (org-property-values "A"))))
  4677. ;; Ignore empty values.
  4678. (should-not
  4679. (org-test-with-temp-text
  4680. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  4681. (org-property-values "A")))
  4682. ;; Take into consideration extended values.
  4683. (should
  4684. (equal '("1 2")
  4685. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4686. (org-property-values "A")))))
  4687. (ert-deftest test-org/find-property ()
  4688. "Test `org-find-property' specifications."
  4689. ;; Regular test.
  4690. (should
  4691. (= 1
  4692. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  4693. (org-find-property "prop"))))
  4694. ;; Ignore false positives.
  4695. (should
  4696. (= 27
  4697. (org-test-with-temp-text
  4698. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  4699. (org-find-property "A"))))
  4700. ;; Return first entry found in buffer.
  4701. (should
  4702. (= 1
  4703. (org-test-with-temp-text
  4704. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4705. (org-find-property "A"))))
  4706. ;; Only search visible part of the buffer.
  4707. (should
  4708. (= 31
  4709. (org-test-with-temp-text
  4710. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4711. (org-narrow-to-subtree)
  4712. (org-find-property "A"))))
  4713. ;; With optional argument, only find entries with a specific value.
  4714. (should-not
  4715. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4716. (org-find-property "A" "2")))
  4717. (should
  4718. (= 31
  4719. (org-test-with-temp-text
  4720. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  4721. (org-find-property "A" "2"))))
  4722. ;; Use "nil" for explicit nil values.
  4723. (should
  4724. (= 31
  4725. (org-test-with-temp-text
  4726. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  4727. (org-find-property "A" "nil")))))
  4728. (ert-deftest test-org/entry-delete ()
  4729. "Test `org-entry-delete' specifications."
  4730. ;; Regular test.
  4731. (should
  4732. (string-match
  4733. " *:PROPERTIES:\n *:B: +2\n *:END:"
  4734. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4735. (org-entry-delete (point) "A")
  4736. (buffer-string))))
  4737. ;; Also remove accumulated properties.
  4738. (should-not
  4739. (string-match
  4740. ":A"
  4741. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  4742. (org-entry-delete (point) "A")
  4743. (buffer-string))))
  4744. ;; When last property is removed, remove the property drawer.
  4745. (should-not
  4746. (string-match
  4747. ":PROPERTIES:"
  4748. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  4749. (org-entry-delete (point) "A")
  4750. (buffer-string))))
  4751. ;; Return a non-nil value when some property was removed.
  4752. (should
  4753. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4754. (org-entry-delete (point) "A")))
  4755. (should-not
  4756. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4757. (org-entry-delete (point) "C")))
  4758. ;; Special properties cannot be located in a drawer. Allow to
  4759. ;; remove them anyway, in case of user error.
  4760. (should
  4761. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  4762. (org-entry-delete (point) "SCHEDULED"))))
  4763. (ert-deftest test-org/entry-get ()
  4764. "Test `org-entry-get' specifications."
  4765. ;; Regular test.
  4766. (should
  4767. (equal "1"
  4768. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4769. (org-entry-get (point) "A"))))
  4770. ;; Ignore case.
  4771. (should
  4772. (equal "1"
  4773. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4774. (org-entry-get (point) "a"))))
  4775. ;; Handle extended values, both before and after base value.
  4776. (should
  4777. (equal "1 2 3"
  4778. (org-test-with-temp-text
  4779. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  4780. (org-entry-get (point) "A"))))
  4781. ;; Empty values are returned as the empty string.
  4782. (should
  4783. (equal ""
  4784. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  4785. (org-entry-get (point) "A"))))
  4786. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  4787. ;; otherwise, return nil.
  4788. (should-not
  4789. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  4790. (org-entry-get (point) "A")))
  4791. (should
  4792. (equal "nil"
  4793. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  4794. (org-entry-get (point) "A" nil t))))
  4795. ;; Return nil when no property is found, independently on the
  4796. ;; LITERAL-NIL argument.
  4797. (should-not
  4798. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4799. (org-entry-get (point) "B")))
  4800. (should-not
  4801. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4802. (org-entry-get (point) "B" nil t)))
  4803. ;; Handle inheritance, when allowed. Include extended values and
  4804. ;; possibly global values.
  4805. (should
  4806. (equal
  4807. "1"
  4808. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4809. (org-entry-get (point) "A" t))))
  4810. (should
  4811. (equal
  4812. "1"
  4813. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4814. (let ((org-use-property-inheritance t))
  4815. (org-entry-get (point) "A" 'selective)))))
  4816. (should-not
  4817. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4818. (let ((org-use-property-inheritance nil))
  4819. (org-entry-get (point) "A" 'selective))))
  4820. (should
  4821. (equal
  4822. "1 2"
  4823. (org-test-with-temp-text
  4824. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  4825. (org-entry-get (point-max) "A" t))))
  4826. (should
  4827. (equal "1"
  4828. (org-test-with-temp-text
  4829. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  4830. (org-mode-restart)
  4831. (org-entry-get (point-max) "A" t))))
  4832. (should
  4833. (equal "0 1"
  4834. (org-test-with-temp-text
  4835. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  4836. (org-mode-restart)
  4837. (org-entry-get (point-max) "A" t)))))
  4838. (ert-deftest test-org/entry-properties ()
  4839. "Test `org-entry-properties' specifications."
  4840. ;; Get "ITEM" property.
  4841. (should
  4842. (equal "H"
  4843. (org-test-with-temp-text "* TODO H"
  4844. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  4845. (should
  4846. (equal "H"
  4847. (org-test-with-temp-text "* TODO H"
  4848. (cdr (assoc "ITEM" (org-entry-properties))))))
  4849. ;; Get "TODO" property. TODO keywords are case sensitive.
  4850. (should
  4851. (equal "TODO"
  4852. (org-test-with-temp-text "* TODO H"
  4853. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  4854. (should
  4855. (equal "TODO"
  4856. (org-test-with-temp-text "* TODO H"
  4857. (cdr (assoc "TODO" (org-entry-properties))))))
  4858. (should-not
  4859. (org-test-with-temp-text "* H"
  4860. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4861. (should-not
  4862. (org-test-with-temp-text "* todo H"
  4863. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4864. ;; Get "PRIORITY" property.
  4865. (should
  4866. (equal "A"
  4867. (org-test-with-temp-text "* [#A] H"
  4868. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4869. (should
  4870. (equal "A"
  4871. (org-test-with-temp-text "* [#A] H"
  4872. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  4873. (should
  4874. (equal (char-to-string org-default-priority)
  4875. (org-test-with-temp-text "* H"
  4876. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4877. ;; Get "FILE" property.
  4878. (should
  4879. (org-test-with-temp-text-in-file "* H\nParagraph"
  4880. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  4881. (buffer-file-name))))
  4882. (should
  4883. (org-test-with-temp-text-in-file "* H\nParagraph"
  4884. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  4885. (buffer-file-name))))
  4886. (should-not
  4887. (org-test-with-temp-text "* H\nParagraph"
  4888. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  4889. ;; Get "TAGS" property.
  4890. (should
  4891. (equal ":tag1:tag2:"
  4892. (org-test-with-temp-text "* H :tag1:tag2:"
  4893. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  4894. (should
  4895. (equal ":tag1:tag2:"
  4896. (org-test-with-temp-text "* H :tag1:tag2:"
  4897. (cdr (assoc "TAGS" (org-entry-properties))))))
  4898. (should-not
  4899. (org-test-with-temp-text "* H"
  4900. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  4901. ;; Get "ALLTAGS" property.
  4902. (should
  4903. (equal ":tag1:tag2:"
  4904. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4905. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  4906. (should
  4907. (equal ":tag1:tag2:"
  4908. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4909. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  4910. (should-not
  4911. (org-test-with-temp-text "* H"
  4912. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  4913. ;; Get "BLOCKED" property.
  4914. (should
  4915. (equal "t"
  4916. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  4917. (let ((org-enforce-todo-dependencies t)
  4918. (org-blocker-hook
  4919. '(org-block-todo-from-children-or-siblings-or-parent)))
  4920. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4921. (should
  4922. (equal ""
  4923. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  4924. (let ((org-enforce-todo-dependencies t)
  4925. (org-blocker-hook
  4926. '(org-block-todo-from-children-or-siblings-or-parent)))
  4927. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4928. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  4929. (should
  4930. (equal
  4931. "[2012-03-29 thu.]"
  4932. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4933. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  4934. (should
  4935. (equal
  4936. "[2012-03-29 thu.]"
  4937. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4938. (cdr (assoc "CLOSED" (org-entry-properties))))))
  4939. (should-not
  4940. (org-test-with-temp-text "* H"
  4941. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  4942. (should
  4943. (equal
  4944. "<2014-03-04 tue.>"
  4945. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4946. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  4947. (should
  4948. (equal
  4949. "<2014-03-04 tue.>"
  4950. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4951. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  4952. (should-not
  4953. (org-test-with-temp-text "* H"
  4954. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  4955. (should
  4956. (equal
  4957. "<2014-03-04 tue.>"
  4958. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4959. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  4960. (should
  4961. (equal
  4962. "<2014-03-04 tue.>"
  4963. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4964. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  4965. (should-not
  4966. (org-test-with-temp-text "* H"
  4967. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  4968. ;; Get "CATEGORY"
  4969. (should
  4970. (equal "cat"
  4971. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4972. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  4973. (should
  4974. (equal "cat"
  4975. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4976. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4977. (should
  4978. (equal "cat"
  4979. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  4980. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4981. (should
  4982. (equal "cat2"
  4983. (org-test-with-temp-text
  4984. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  4985. "\n"
  4986. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  4987. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4988. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  4989. (should
  4990. (equal "<2012-03-29 thu.>"
  4991. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  4992. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  4993. (should
  4994. (equal "[2012-03-29 thu.]"
  4995. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  4996. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  4997. (should
  4998. (equal "<2012-03-29 thu.>"
  4999. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  5000. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  5001. (should
  5002. (equal "[2014-03-04 tue.]"
  5003. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  5004. (cdr (assoc "TIMESTAMP_IA"
  5005. (org-entry-properties nil "TIMESTAMP_IA"))))))
  5006. (should-not
  5007. (equal "<2012-03-29 thu.>"
  5008. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  5009. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5010. ;; Get standard properties.
  5011. (should
  5012. (equal "1"
  5013. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5014. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5015. ;; Handle extended properties.
  5016. (should
  5017. (equal "1 2 3"
  5018. (org-test-with-temp-text
  5019. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5020. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5021. (should
  5022. (equal "1 2 3"
  5023. (org-test-with-temp-text
  5024. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  5025. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5026. ;; Ignore forbidden (special) properties.
  5027. (should-not
  5028. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  5029. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  5030. (ert-deftest test-org/entry-put ()
  5031. "Test `org-entry-put' specifications."
  5032. ;; Error when not a string or nil.
  5033. (should-error
  5034. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5035. (org-entry-put 1 "test" 2)))
  5036. ;; Error when property name is invalid.
  5037. (should-error
  5038. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5039. (org-entry-put 1 "no space" "value")))
  5040. (should-error
  5041. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5042. (org-entry-put 1 "" "value")))
  5043. ;; Set "TODO" property.
  5044. (should
  5045. (string-match (regexp-quote " TODO H")
  5046. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5047. (org-entry-put (point) "TODO" "TODO")
  5048. (buffer-string))))
  5049. (should
  5050. (string-match (regexp-quote "* H")
  5051. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5052. (org-entry-put (point) "TODO" nil)
  5053. (buffer-string))))
  5054. ;; Set "PRIORITY" property.
  5055. (should
  5056. (equal "* [#A] H"
  5057. (org-test-with-temp-text "* [#B] H"
  5058. (org-entry-put (point) "PRIORITY" "A")
  5059. (buffer-string))))
  5060. (should
  5061. (equal "* H"
  5062. (org-test-with-temp-text "* [#B] H"
  5063. (org-entry-put (point) "PRIORITY" nil)
  5064. (buffer-string))))
  5065. ;; Set "SCHEDULED" property.
  5066. (should
  5067. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  5068. (org-test-with-temp-text "* H"
  5069. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  5070. (buffer-string))))
  5071. (should
  5072. (string= "* H\n"
  5073. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5074. (org-entry-put (point) "SCHEDULED" nil)
  5075. (buffer-string))))
  5076. (should
  5077. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  5078. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5079. (org-entry-put (point) "SCHEDULED" "earlier")
  5080. (buffer-string))))
  5081. (should
  5082. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  5083. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5084. (org-entry-put (point) "SCHEDULED" "later")
  5085. (buffer-string))))
  5086. ;; Set "DEADLINE" property.
  5087. (should
  5088. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  5089. (org-test-with-temp-text "* H"
  5090. (org-entry-put (point) "DEADLINE" "2014-03-04")
  5091. (buffer-string))))
  5092. (should
  5093. (string= "* H\n"
  5094. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5095. (org-entry-put (point) "DEADLINE" nil)
  5096. (buffer-string))))
  5097. (should
  5098. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  5099. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5100. (org-entry-put (point) "DEADLINE" "earlier")
  5101. (buffer-string))))
  5102. (should
  5103. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  5104. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5105. (org-entry-put (point) "DEADLINE" "later")
  5106. (buffer-string))))
  5107. ;; Set "CATEGORY" property
  5108. (should
  5109. (string-match "^ *:CATEGORY: cat"
  5110. (org-test-with-temp-text "* H"
  5111. (org-entry-put (point) "CATEGORY" "cat")
  5112. (buffer-string))))
  5113. ;; Regular properties, with or without pre-existing drawer.
  5114. (should
  5115. (string-match "^ *:A: +2$"
  5116. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5117. (org-entry-put (point) "A" "2")
  5118. (buffer-string))))
  5119. (should
  5120. (string-match "^ *:A: +1$"
  5121. (org-test-with-temp-text "* H"
  5122. (org-entry-put (point) "A" "1")
  5123. (buffer-string))))
  5124. ;; Special case: two consecutive headlines.
  5125. (should
  5126. (string-match "\\* A\n *:PROPERTIES:"
  5127. (org-test-with-temp-text "* A\n** B"
  5128. (org-entry-put (point) "A" "1")
  5129. (buffer-string)))))
  5130. (ert-deftest test-org/refresh-properties ()
  5131. "Test `org-refresh-properties' specifications."
  5132. (should
  5133. (equal "1"
  5134. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5135. (org-refresh-properties "A" 'org-test)
  5136. (get-text-property (point) 'org-test))))
  5137. (should-not
  5138. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5139. (org-refresh-properties "B" 'org-test)
  5140. (get-text-property (point) 'org-test)))
  5141. ;; Handle properties only defined with extension syntax, i.e.,
  5142. ;; "PROPERTY+".
  5143. (should
  5144. (equal "1"
  5145. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5146. (org-refresh-properties "A" 'org-test)
  5147. (get-text-property (point) 'org-test))))
  5148. ;; When property is inherited, add text property to the whole
  5149. ;; sub-tree.
  5150. (should
  5151. (equal "1"
  5152. (org-test-with-temp-text
  5153. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  5154. (let ((org-use-property-inheritance t))
  5155. (org-refresh-properties "A" 'org-test))
  5156. (get-text-property (point) 'org-test))))
  5157. ;; When property is inherited, use global value across the whole
  5158. ;; buffer. However local values have precedence.
  5159. (should-not
  5160. (equal "1"
  5161. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5162. (org-mode-restart)
  5163. (let ((org-use-property-inheritance nil))
  5164. (org-refresh-properties "A" 'org-test))
  5165. (get-text-property (point) 'org-test))))
  5166. (should
  5167. (equal "1"
  5168. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5169. (org-mode-restart)
  5170. (let ((org-use-property-inheritance t))
  5171. (org-refresh-properties "A" 'org-test))
  5172. (get-text-property (point) 'org-test))))
  5173. (should
  5174. (equal "2"
  5175. (org-test-with-temp-text
  5176. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  5177. (org-mode-restart)
  5178. (let ((org-use-property-inheritance t))
  5179. (org-refresh-properties "A" 'org-test))
  5180. (get-text-property (point) 'org-test)))))
  5181. ;;; Radio Targets
  5182. (ert-deftest test-org/update-radio-target-regexp ()
  5183. "Test `org-update-radio-target-regexp' specifications."
  5184. ;; Properly update cache with no previous radio target regexp.
  5185. (should
  5186. (eq 'link
  5187. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5188. (save-excursion (goto-char (point-max)) (org-element-context))
  5189. (insert "<<<")
  5190. (search-forward "o")
  5191. (insert ">>>")
  5192. (org-update-radio-target-regexp)
  5193. (goto-char (point-max))
  5194. (org-element-type (org-element-context)))))
  5195. ;; Properly update cache with previous radio target regexp.
  5196. (should
  5197. (eq 'link
  5198. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5199. (save-excursion (goto-char (point-max)) (org-element-context))
  5200. (insert "<<<")
  5201. (search-forward "o")
  5202. (insert ">>>")
  5203. (org-update-radio-target-regexp)
  5204. (search-backward "r")
  5205. (delete-char 5)
  5206. (insert "new")
  5207. (org-update-radio-target-regexp)
  5208. (goto-char (point-max))
  5209. (delete-region (line-beginning-position) (point))
  5210. (insert "new")
  5211. (org-element-type (org-element-context))))))
  5212. ;;; Refile
  5213. (ert-deftest test-org/refile-get-targets ()
  5214. "Test `org-refile-get-targets' specifications."
  5215. ;; :maxlevel includes all headings above specified value.
  5216. (should
  5217. (equal '("H1" "H2" "H3")
  5218. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5219. (let ((org-refile-use-outline-path nil)
  5220. (org-refile-targets `((nil :maxlevel . 3))))
  5221. (mapcar #'car (org-refile-get-targets))))))
  5222. (should
  5223. (equal '("H1" "H2")
  5224. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5225. (let ((org-refile-use-outline-path nil)
  5226. (org-refile-targets `((nil :maxlevel . 2))))
  5227. (mapcar #'car (org-refile-get-targets))))))
  5228. ;; :level limits targets to headlines with the specified level.
  5229. (should
  5230. (equal '("H2")
  5231. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5232. (let ((org-refile-use-outline-path nil)
  5233. (org-refile-targets `((nil :level . 2))))
  5234. (mapcar #'car (org-refile-get-targets))))))
  5235. ;; :tag limits targets to headlines with specified tag.
  5236. (should
  5237. (equal '("H1")
  5238. (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
  5239. (let ((org-refile-use-outline-path nil)
  5240. (org-refile-targets `((nil :tag . "foo"))))
  5241. (mapcar #'car (org-refile-get-targets))))))
  5242. ;; :todo limits targets to headlines with specified TODO keyword.
  5243. (should
  5244. (equal '("H2")
  5245. (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
  5246. (let ((org-refile-use-outline-path nil)
  5247. (org-refile-targets `((nil :todo . "TODO"))))
  5248. (mapcar #'car (org-refile-get-targets))))))
  5249. ;; :regexp filters targets matching provided regexp.
  5250. (should
  5251. (equal '("F2" "F3")
  5252. (org-test-with-temp-text "* H1\n** F2\n*** F3"
  5253. (let ((org-refile-use-outline-path nil)
  5254. (org-refile-targets `((nil :regexp . "F"))))
  5255. (mapcar #'car (org-refile-get-targets))))))
  5256. ;; A nil `org-refile-targets' includes only top level headlines in
  5257. ;; current buffer.
  5258. (should
  5259. (equal '("H1" "H2")
  5260. (org-test-with-temp-text "* H1\n** S1\n* H2"
  5261. (let ((org-refile-use-outline-path nil)
  5262. (org-refile-targets nil))
  5263. (mapcar #'car (org-refile-get-targets))))))
  5264. ;; Return value is the union of the targets according to all the
  5265. ;; defined rules. However, prevent duplicates.
  5266. (should
  5267. (equal '("F2" "F3" "H1")
  5268. (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
  5269. (let ((org-refile-use-outline-path nil)
  5270. (org-refile-targets `((nil :regexp . "F")
  5271. (nil :todo . "TODO"))))
  5272. (mapcar #'car (org-refile-get-targets))))))
  5273. (should
  5274. (equal '("F2" "F3" "H1")
  5275. (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
  5276. (let ((org-refile-use-outline-path nil)
  5277. (org-refile-targets `((nil :regexp . "F")
  5278. (nil :todo . "TODO"))))
  5279. (mapcar #'car (org-refile-get-targets))))))
  5280. ;; When `org-refile-use-outline-path' is non-nil, provide targets as
  5281. ;; paths.
  5282. (should
  5283. (equal '("H1" "H1/H2" "H1/H2/H3")
  5284. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5285. (let ((org-refile-use-outline-path t)
  5286. (org-refile-targets `((nil :maxlevel . 3))))
  5287. (mapcar #'car (org-refile-get-targets))))))
  5288. ;; When providing targets as paths, escape forward slashes in
  5289. ;; headings with backslashes.
  5290. (should
  5291. (equal '("H1\\/foo")
  5292. (org-test-with-temp-text "* H1/foo"
  5293. (let ((org-refile-use-outline-path t)
  5294. (org-refile-targets `((nil :maxlevel . 1))))
  5295. (mapcar #'car (org-refile-get-targets))))))
  5296. ;; When `org-refile-use-outline-path' is `file', include file name
  5297. ;; without directory in targets.
  5298. (should
  5299. (org-test-with-temp-text-in-file "* H1"
  5300. (let* ((filename (buffer-file-name))
  5301. (org-refile-use-outline-path 'file)
  5302. (org-refile-targets `(((,filename) :level . 1))))
  5303. (member (file-name-nondirectory filename)
  5304. (mapcar #'car (org-refile-get-targets))))))
  5305. ;; When `org-refile-use-outline-path' is `full-file-path', include
  5306. ;; full file name.
  5307. (should
  5308. (org-test-with-temp-text-in-file "* H1"
  5309. (let* ((filename (buffer-file-name))
  5310. (org-refile-use-outline-path 'full-file-path)
  5311. (org-refile-targets `(((,filename) :level . 1))))
  5312. (member filename (mapcar #'car (org-refile-get-targets))))))
  5313. ;; When `org-refile-use-outline-path' is `buffer-name', include
  5314. ;; buffer name.
  5315. (should
  5316. (org-test-with-temp-text "* H1"
  5317. (let* ((org-refile-use-outline-path 'buffer-name)
  5318. (org-refile-targets `((nil :level . 1))))
  5319. (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
  5320. ;;; Sparse trees
  5321. (ert-deftest test-org/match-sparse-tree ()
  5322. "Test `org-match-sparse-tree' specifications."
  5323. ;; Match tags.
  5324. (should-not
  5325. (org-test-with-temp-text "* H\n** H1 :tag:"
  5326. (org-match-sparse-tree nil "tag")
  5327. (search-forward "H1")
  5328. (org-invisible-p2)))
  5329. (should
  5330. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  5331. (org-match-sparse-tree nil "tag")
  5332. (search-forward "H2")
  5333. (org-invisible-p2)))
  5334. ;; "-" operator for tags.
  5335. (should-not
  5336. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5337. (org-match-sparse-tree nil "tag1-tag2")
  5338. (search-forward "H1")
  5339. (org-invisible-p2)))
  5340. (should
  5341. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5342. (org-match-sparse-tree nil "tag1-tag2")
  5343. (search-forward "H2")
  5344. (org-invisible-p2)))
  5345. ;; "&" operator for tags.
  5346. (should
  5347. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5348. (org-match-sparse-tree nil "tag1&tag2")
  5349. (search-forward "H1")
  5350. (org-invisible-p2)))
  5351. (should-not
  5352. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5353. (org-match-sparse-tree nil "tag1&tag2")
  5354. (search-forward "H2")
  5355. (org-invisible-p2)))
  5356. ;; "|" operator for tags.
  5357. (should-not
  5358. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5359. (org-match-sparse-tree nil "tag1|tag2")
  5360. (search-forward "H1")
  5361. (org-invisible-p2)))
  5362. (should-not
  5363. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5364. (org-match-sparse-tree nil "tag1|tag2")
  5365. (search-forward "H2")
  5366. (org-invisible-p2)))
  5367. ;; Regexp match on tags.
  5368. (should-not
  5369. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5370. (org-match-sparse-tree nil "{^tag.*}")
  5371. (search-forward "H1")
  5372. (org-invisible-p2)))
  5373. (should
  5374. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5375. (org-match-sparse-tree nil "{^tag.*}")
  5376. (search-forward "H2")
  5377. (org-invisible-p2)))
  5378. ;; Match group tags.
  5379. (should-not
  5380. (org-test-with-temp-text
  5381. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5382. (org-match-sparse-tree nil "work")
  5383. (search-forward "H1")
  5384. (org-invisible-p2)))
  5385. (should-not
  5386. (org-test-with-temp-text
  5387. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5388. (org-match-sparse-tree nil "work")
  5389. (search-forward "H2")
  5390. (org-invisible-p2)))
  5391. ;; Match group tags with hard brackets.
  5392. (should-not
  5393. (org-test-with-temp-text
  5394. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5395. (org-match-sparse-tree nil "work")
  5396. (search-forward "H1")
  5397. (org-invisible-p2)))
  5398. (should-not
  5399. (org-test-with-temp-text
  5400. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5401. (org-match-sparse-tree nil "work")
  5402. (search-forward "H2")
  5403. (org-invisible-p2)))
  5404. ;; Match tags in hierarchies
  5405. (should-not
  5406. (org-test-with-temp-text
  5407. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5408. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5409. #+TAGS: { Lev_3 : Lev_4 }\n
  5410. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5411. (org-match-sparse-tree nil "Lev_1")
  5412. (search-forward "H4")
  5413. (org-invisible-p2)))
  5414. ;; Match regular expressions in tags
  5415. (should-not
  5416. (org-test-with-temp-text
  5417. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  5418. (org-match-sparse-tree nil "Lev")
  5419. (search-forward "H1")
  5420. (org-invisible-p2)))
  5421. (should
  5422. (org-test-with-temp-text
  5423. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  5424. (org-match-sparse-tree nil "Lev")
  5425. (search-forward "H1")
  5426. (org-invisible-p2)))
  5427. ;; Match properties.
  5428. (should
  5429. (org-test-with-temp-text
  5430. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  5431. (org-match-sparse-tree nil "A=\"1\"")
  5432. (search-forward "H2")
  5433. (org-invisible-p2)))
  5434. (should-not
  5435. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5436. (org-match-sparse-tree nil "A=\"1\"")
  5437. (search-forward "H2")
  5438. (org-invisible-p2)))
  5439. ;; Case is not significant when matching properties.
  5440. (should-not
  5441. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5442. (org-match-sparse-tree nil "a=\"1\"")
  5443. (search-forward "H2")
  5444. (org-invisible-p2)))
  5445. (should-not
  5446. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  5447. (org-match-sparse-tree nil "A=\"1\"")
  5448. (search-forward "H2")
  5449. (org-invisible-p2)))
  5450. ;; Match special LEVEL property.
  5451. (should-not
  5452. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5453. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5454. (search-forward "H1")
  5455. (org-invisible-p2)))
  5456. (should
  5457. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5458. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5459. (search-forward "H2")
  5460. (org-invisible-p2)))
  5461. ;; Comparison operators when matching properties.
  5462. (should
  5463. (org-test-with-temp-text
  5464. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5465. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5466. (search-forward "H1")
  5467. (org-invisible-p2)))
  5468. (should-not
  5469. (org-test-with-temp-text
  5470. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5471. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5472. (search-forward "H2")
  5473. (org-invisible-p2)))
  5474. ;; Regexp match on properties values.
  5475. (should-not
  5476. (org-test-with-temp-text
  5477. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5478. (org-match-sparse-tree nil "A={f.*}")
  5479. (search-forward "H1")
  5480. (org-invisible-p2)))
  5481. (should
  5482. (org-test-with-temp-text
  5483. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5484. (org-match-sparse-tree nil "A={f.*}")
  5485. (search-forward "H2")
  5486. (org-invisible-p2)))
  5487. ;; With an optional argument, limit match to TODO entries.
  5488. (should-not
  5489. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5490. (org-match-sparse-tree t "tag")
  5491. (search-forward "H1")
  5492. (org-invisible-p2)))
  5493. (should
  5494. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5495. (org-match-sparse-tree t "tag")
  5496. (search-forward "H2")
  5497. (org-invisible-p2))))
  5498. (ert-deftest test-org/occur ()
  5499. "Test `org-occur' specifications."
  5500. ;; Count number of matches.
  5501. (should
  5502. (= 1
  5503. (org-test-with-temp-text "* H\nA\n* H2"
  5504. (org-occur "A"))))
  5505. (should
  5506. (= 2
  5507. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5508. (org-occur "A"))))
  5509. ;; Test CALLBACK optional argument.
  5510. (should
  5511. (= 0
  5512. (org-test-with-temp-text "* H\nA\n* H2"
  5513. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5514. (should
  5515. (= 1
  5516. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5517. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5518. ;; Case-fold searches according to `org-occur-case-fold-search'.
  5519. (should
  5520. (= 2
  5521. (org-test-with-temp-text "Aa"
  5522. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  5523. (should
  5524. (= 2
  5525. (org-test-with-temp-text "Aa"
  5526. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  5527. (should
  5528. (= 1
  5529. (org-test-with-temp-text "Aa"
  5530. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  5531. (should
  5532. (= 1
  5533. (org-test-with-temp-text "Aa"
  5534. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  5535. (should
  5536. (= 1
  5537. (org-test-with-temp-text "Aa"
  5538. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  5539. (should
  5540. (= 2
  5541. (org-test-with-temp-text "Aa"
  5542. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  5543. ;;; Tags
  5544. (ert-deftest test-org/tag-string-to-alist ()
  5545. "Test `org-tag-string-to-alist' specifications."
  5546. ;; Tag without selection key.
  5547. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  5548. ;; Tag with selection key.
  5549. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  5550. ;; Tag group.
  5551. (should
  5552. (equal
  5553. (org-tag-string-to-alist "[ group : t1 t2 ]")
  5554. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  5555. ;; Mutually exclusive tags.
  5556. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  5557. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  5558. (should
  5559. (equal
  5560. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  5561. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  5562. (ert-deftest test-org/tag-alist-to-string ()
  5563. "Test `org-tag-alist-to-string' specifications."
  5564. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  5565. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  5566. (should
  5567. (equal
  5568. (org-tag-alist-to-string
  5569. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5570. "[ group : t1 t2 ]"))
  5571. (should
  5572. (equal (org-tag-alist-to-string
  5573. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  5574. "{ tag1 tag2 }"))
  5575. (should
  5576. (equal
  5577. (org-tag-alist-to-string
  5578. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  5579. "{ group : tag1 tag2 }")))
  5580. (ert-deftest test-org/tag-alist-to-groups ()
  5581. "Test `org-tag-alist-to-groups' specifications."
  5582. (should
  5583. (equal (org-tag-alist-to-groups
  5584. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  5585. '(("group" "t1" "t2"))))
  5586. (should
  5587. (equal
  5588. (org-tag-alist-to-groups
  5589. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5590. '(("group" "t1" "t2"))))
  5591. (should-not
  5592. (org-tag-alist-to-groups
  5593. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  5594. (ert-deftest test-org/tag-align ()
  5595. "Test tags alignment."
  5596. ;; Test aligning tags with different display width.
  5597. (should
  5598. ;; 12345678901234567890
  5599. (equal "* Test :abc:"
  5600. (org-test-with-temp-text "* Test :abc:"
  5601. (let ((org-tags-column -20)
  5602. (indent-tabs-mode nil))
  5603. (org-fix-tags-on-the-fly))
  5604. (buffer-string))))
  5605. (should
  5606. ;; 12345678901234567890
  5607. (equal "* Test :日本語:"
  5608. (org-test-with-temp-text "* Test :日本語:"
  5609. (let ((org-tags-column -20)
  5610. (indent-tabs-mode nil))
  5611. (org-fix-tags-on-the-fly))
  5612. (buffer-string))))
  5613. ;; Make sure aligning tags do not skip invisible text.
  5614. (should
  5615. (equal "* [[linkx]] :tag:"
  5616. (org-test-with-temp-text "* [[link<point>]] :tag:"
  5617. (let ((org-tags-column 0))
  5618. (org-fix-tags-on-the-fly)
  5619. (insert "x")
  5620. (buffer-string))))))
  5621. (ert-deftest test-org/tags-at ()
  5622. (should
  5623. (equal '("foo" "bar")
  5624. (org-test-with-temp-text
  5625. "* T<point>est :foo:bar:"
  5626. (org-get-tags-at)))))
  5627. ;;; TODO keywords
  5628. (ert-deftest test-org/auto-repeat-maybe ()
  5629. "Test `org-auto-repeat-maybe' specifications."
  5630. ;; Do not auto repeat when there is no valid time stamp with
  5631. ;; a repeater in the entry.
  5632. (should-not
  5633. (string-prefix-p
  5634. "* TODO H"
  5635. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5636. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  5637. (org-todo "DONE")
  5638. (buffer-string)))))
  5639. (should-not
  5640. (string-prefix-p
  5641. "* TODO H"
  5642. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5643. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  5644. (org-todo "DONE")
  5645. (buffer-string)))))
  5646. ;; When switching to DONE state, switch back to first TODO keyword
  5647. ;; in sequence, or the same keyword if they have different types.
  5648. (should
  5649. (string-prefix-p
  5650. "* TODO H"
  5651. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5652. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  5653. (org-todo "DONE")
  5654. (buffer-string)))))
  5655. (should
  5656. (string-prefix-p
  5657. "* KWD1 H"
  5658. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  5659. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  5660. (org-todo "DONE")
  5661. (buffer-string)))))
  5662. (should
  5663. (string-prefix-p
  5664. "* KWD2 H"
  5665. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  5666. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  5667. (org-todo "DONE")
  5668. (buffer-string)))))
  5669. ;; If there was no TODO keyword in the first place, do not insert
  5670. ;; any either.
  5671. (should
  5672. (string-prefix-p
  5673. "* H"
  5674. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5675. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  5676. (org-todo "DONE")
  5677. (buffer-string)))))
  5678. ;; Revert to REPEAT_TO_STATE, if set.
  5679. (should
  5680. (string-prefix-p
  5681. "* KWD2 H"
  5682. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  5683. (org-test-with-temp-text
  5684. "* KWD2 H
  5685. :PROPERTIES:
  5686. :REPEAT_TO_STATE: KWD2
  5687. :END:
  5688. <2012-03-29 Thu +2y>"
  5689. (org-todo "DONE")
  5690. (buffer-string)))))
  5691. ;; When switching to DONE state, update base date. If there are
  5692. ;; multiple repeated time stamps, update them all.
  5693. (should
  5694. (string-match-p
  5695. "<2014-03-29 .* \\+2y>"
  5696. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5697. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  5698. (org-todo "DONE")
  5699. (buffer-string)))))
  5700. (should
  5701. (string-match-p
  5702. "<2015-03-04 .* \\+1y>"
  5703. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5704. (org-test-with-temp-text
  5705. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  5706. (org-todo "DONE")
  5707. (buffer-string)))))
  5708. ;; Throw an error if repeater unit is the hour and no time is
  5709. ;; provided in the time-stamp.
  5710. (should-error
  5711. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5712. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  5713. (org-todo "DONE")
  5714. (buffer-string))))
  5715. ;; Do not repeat commented time stamps.
  5716. (should-not
  5717. (string-prefix-p
  5718. "<2015-03-04 .* \\+1y>"
  5719. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5720. (org-test-with-temp-text
  5721. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  5722. (org-todo "DONE")
  5723. (buffer-string)))))
  5724. (should-not
  5725. (string-prefix-p
  5726. "<2015-03-04 .* \\+1y>"
  5727. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5728. (org-test-with-temp-text
  5729. "* TODO H
  5730. <2012-03-29 Thu. +2y>
  5731. #+BEGIN_EXAMPLE
  5732. <2014-03-04 Tue +1y>
  5733. #+END_EXAMPLE"
  5734. (org-todo "DONE")
  5735. (buffer-string)))))
  5736. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  5737. ;; entry, record time of last repeat.
  5738. (should-not
  5739. (string-match-p
  5740. ":LAST_REPEAT:"
  5741. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  5742. (org-log-repeat nil))
  5743. (cl-letf (((symbol-function 'org-add-log-setup)
  5744. (lambda (&rest args) nil)))
  5745. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  5746. (org-todo "DONE")
  5747. (buffer-string))))))
  5748. (should
  5749. (string-match-p
  5750. ":LAST_REPEAT:"
  5751. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  5752. (org-log-repeat t))
  5753. (cl-letf (((symbol-function 'org-add-log-setup)
  5754. (lambda (&rest args) nil)))
  5755. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  5756. (org-todo "DONE")
  5757. (buffer-string))))))
  5758. (should
  5759. (string-match-p
  5760. ":LAST_REPEAT:"
  5761. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5762. (cl-letf (((symbol-function 'org-add-log-setup)
  5763. (lambda (&rest args) nil)))
  5764. (org-test-with-temp-text
  5765. "* TODO H\n<2012-03-29 Thu. +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  5766. (org-todo "DONE")
  5767. (buffer-string))))))
  5768. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  5769. ;; the entry as it is no longer relevant.
  5770. (should-not
  5771. (string-match-p
  5772. "^SCHEDULED:"
  5773. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  5774. (org-test-with-temp-text
  5775. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  5776. (org-todo "DONE")
  5777. (buffer-string))))))
  5778. ;;; Timestamps API
  5779. (ert-deftest test-org/at-timestamp-p ()
  5780. "Test `org-at-timestamp-p' specifications."
  5781. (should
  5782. (org-test-with-temp-text "<2012-03-29 Thu>"
  5783. (org-at-timestamp-p)))
  5784. (should-not
  5785. (org-test-with-temp-text "2012-03-29 Thu"
  5786. (org-at-timestamp-p)))
  5787. ;; Test return values.
  5788. (should
  5789. (eq 'bracket
  5790. (org-test-with-temp-text "<2012-03-29 Thu>"
  5791. (org-at-timestamp-p))))
  5792. (should
  5793. (eq 'year
  5794. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  5795. (org-at-timestamp-p))))
  5796. (should
  5797. (eq 'month
  5798. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  5799. (org-at-timestamp-p))))
  5800. (should
  5801. (eq 'day
  5802. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  5803. (org-at-timestamp-p))))
  5804. (should
  5805. (eq 'day
  5806. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  5807. (org-at-timestamp-p))))
  5808. (should
  5809. (wholenump
  5810. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  5811. (org-at-timestamp-p))))
  5812. (should
  5813. (eq 'bracket
  5814. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  5815. (org-at-timestamp-p))))
  5816. (should
  5817. (eq 'after
  5818. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  5819. (org-at-timestamp-p))))
  5820. ;; Test `inactive' optional argument.
  5821. (should
  5822. (org-test-with-temp-text "[2012-03-29 Thu]"
  5823. (org-at-timestamp-p 'inactive)))
  5824. (should-not
  5825. (org-test-with-temp-text "[2012-03-29 Thu]"
  5826. (org-at-timestamp-p)))
  5827. ;; When optional argument is `agenda', recognize time-stamps in
  5828. ;; planning info line, property drawers and clocks.
  5829. (should
  5830. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  5831. (org-at-timestamp-p 'agenda)))
  5832. (should-not
  5833. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  5834. (org-at-timestamp-p)))
  5835. (should
  5836. (org-test-with-temp-text
  5837. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  5838. (org-at-timestamp-p 'agenda)))
  5839. (should-not
  5840. (org-test-with-temp-text
  5841. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  5842. (org-at-timestamp-p)))
  5843. (should
  5844. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  5845. (let ((org-agenda-include-inactive-timestamps t))
  5846. (org-at-timestamp-p 'agenda))))
  5847. (should-not
  5848. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  5849. (let ((org-agenda-include-inactive-timestamps t))
  5850. (org-at-timestamp-p))))
  5851. (should-not
  5852. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  5853. (let ((org-agenda-include-inactive-timestamps t))
  5854. (org-at-timestamp-p 'inactive))))
  5855. ;; When optional argument is `lax', match any part of the document
  5856. ;; with Org timestamp syntax.
  5857. (should
  5858. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  5859. (org-at-timestamp-p 'lax)))
  5860. (should-not
  5861. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  5862. (org-at-timestamp-p)))
  5863. (should
  5864. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  5865. (org-at-timestamp-p 'lax)))
  5866. (should-not
  5867. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  5868. (org-at-timestamp-p)))
  5869. (should
  5870. (org-test-with-temp-text
  5871. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  5872. (org-at-timestamp-p 'lax)))
  5873. (should-not
  5874. (org-test-with-temp-text
  5875. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  5876. (org-at-timestamp-p)))
  5877. ;; Optional argument `lax' also matches inactive timestamps.
  5878. (should
  5879. (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
  5880. (org-at-timestamp-p 'lax))))
  5881. (ert-deftest test-org/time-stamp ()
  5882. "Test `org-time-stamp' specifications."
  5883. ;; Insert chosen time stamp at point.
  5884. (should
  5885. (string-match
  5886. "Te<2014-03-04 .*?>xt"
  5887. (org-test-with-temp-text "Te<point>xt"
  5888. (cl-letf (((symbol-function 'org-read-date)
  5889. (lambda (&rest args)
  5890. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5891. (org-time-stamp nil)
  5892. (buffer-string)))))
  5893. ;; With a prefix argument, also insert time.
  5894. (should
  5895. (string-match
  5896. "Te<2014-03-04 .*? 00:41>xt"
  5897. (org-test-with-temp-text "Te<point>xt"
  5898. (cl-letf (((symbol-function 'org-read-date)
  5899. (lambda (&rest args)
  5900. (apply #'encode-time
  5901. (org-parse-time-string "2014-03-04 00:41")))))
  5902. (org-time-stamp '(4))
  5903. (buffer-string)))))
  5904. ;; With two universal prefix arguments, insert an active timestamp
  5905. ;; with the current time without prompting the user.
  5906. (should
  5907. (string-match
  5908. "Te<2014-03-04 .*? 00:41>xt"
  5909. (org-test-with-temp-text "Te<point>xt"
  5910. (cl-letf (((symbol-function 'current-time)
  5911. (lambda ()
  5912. (apply #'encode-time
  5913. (org-parse-time-string "2014-03-04 00:41")))))
  5914. (org-time-stamp '(16))
  5915. (buffer-string)))))
  5916. ;; When optional argument is non-nil, insert an inactive timestamp.
  5917. (should
  5918. (string-match
  5919. "Te\\[2014-03-04 .*?\\]xt"
  5920. (org-test-with-temp-text "Te<point>xt"
  5921. (cl-letf (((symbol-function 'org-read-date)
  5922. (lambda (&rest args)
  5923. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5924. (org-time-stamp nil t)
  5925. (buffer-string)))))
  5926. ;; When called from a timestamp, replace existing one.
  5927. (should
  5928. (string-match
  5929. "<2014-03-04 .*?>"
  5930. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  5931. (cl-letf (((symbol-function 'org-read-date)
  5932. (lambda (&rest args)
  5933. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5934. (org-time-stamp nil)
  5935. (buffer-string)))))
  5936. (should
  5937. (string-match
  5938. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  5939. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  5940. (cl-letf (((symbol-function 'org-read-date)
  5941. (lambda (&rest args)
  5942. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5943. (org-time-stamp nil)
  5944. (buffer-string)))))
  5945. ;; When replacing a timestamp, preserve repeater, if any.
  5946. (should
  5947. (string-match
  5948. "<2014-03-04 .*? \\+2y>"
  5949. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  5950. (cl-letf (((symbol-function 'org-read-date)
  5951. (lambda (&rest args)
  5952. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5953. (org-time-stamp nil)
  5954. (buffer-string)))))
  5955. ;; When called twice in a raw, build a date range.
  5956. (should
  5957. (string-match
  5958. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  5959. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  5960. (cl-letf (((symbol-function 'org-read-date)
  5961. (lambda (&rest args)
  5962. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5963. (let ((last-command 'org-time-stamp)
  5964. (this-command 'org-time-stamp))
  5965. (org-time-stamp nil))
  5966. (buffer-string))))))
  5967. (ert-deftest test-org/timestamp-has-time-p ()
  5968. "Test `org-timestamp-has-time-p' specifications."
  5969. ;; With time.
  5970. (should
  5971. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  5972. (org-timestamp-has-time-p (org-element-context))))
  5973. ;; Without time.
  5974. (should-not
  5975. (org-test-with-temp-text "<2012-03-29 Thu>"
  5976. (org-timestamp-has-time-p (org-element-context)))))
  5977. (ert-deftest test-org/get-repeat ()
  5978. "Test `org-get-repeat' specifications."
  5979. (should
  5980. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  5981. (org-get-repeat)))
  5982. (should-not
  5983. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  5984. (org-get-repeat)))
  5985. ;; Return proper repeat string.
  5986. (should
  5987. (equal "+2y"
  5988. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  5989. (org-get-repeat))))
  5990. ;; Prevent false positive (commented or verbatim time stamps)
  5991. (should-not
  5992. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  5993. (org-get-repeat)))
  5994. (should-not
  5995. (org-test-with-temp-text
  5996. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  5997. (org-get-repeat)))
  5998. ;; Return nil when called before first heading.
  5999. (should-not
  6000. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  6001. (org-get-repeat)))
  6002. ;; When called with an optional argument, extract repeater from that
  6003. ;; string instead.
  6004. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  6005. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  6006. (ert-deftest test-org/timestamp-format ()
  6007. "Test `org-timestamp-format' specifications."
  6008. ;; Regular test.
  6009. (should
  6010. (equal
  6011. "2012-03-29 16:40"
  6012. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6013. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  6014. ;; Range end.
  6015. (should
  6016. (equal
  6017. "2012-03-29"
  6018. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  6019. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  6020. (ert-deftest test-org/timestamp-split-range ()
  6021. "Test `org-timestamp-split-range' specifications."
  6022. ;; Extract range start (active).
  6023. (should
  6024. (equal '(2012 3 29)
  6025. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6026. (let ((ts (org-timestamp-split-range (org-element-context))))
  6027. (mapcar (lambda (p) (org-element-property p ts))
  6028. '(:year-end :month-end :day-end))))))
  6029. ;; Extract range start (inactive)
  6030. (should
  6031. (equal '(2012 3 29)
  6032. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6033. (let ((ts (org-timestamp-split-range (org-element-context))))
  6034. (mapcar (lambda (p) (org-element-property p ts))
  6035. '(:year-end :month-end :day-end))))))
  6036. ;; Extract range end (active).
  6037. (should
  6038. (equal '(2012 3 30)
  6039. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6040. (let ((ts (org-timestamp-split-range
  6041. (org-element-context) t)))
  6042. (mapcar (lambda (p) (org-element-property p ts))
  6043. '(:year-end :month-end :day-end))))))
  6044. ;; Extract range end (inactive)
  6045. (should
  6046. (equal '(2012 3 30)
  6047. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  6048. (let ((ts (org-timestamp-split-range
  6049. (org-element-context) t)))
  6050. (mapcar (lambda (p) (org-element-property p ts))
  6051. '(:year-end :month-end :day-end))))))
  6052. ;; Return the timestamp if not a range.
  6053. (should
  6054. (org-test-with-temp-text "[2012-03-29 Thu]"
  6055. (let* ((ts-orig (org-element-context))
  6056. (ts-copy (org-timestamp-split-range ts-orig)))
  6057. (eq ts-orig ts-copy))))
  6058. (should
  6059. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6060. (let* ((ts-orig (org-element-context))
  6061. (ts-copy (org-timestamp-split-range ts-orig)))
  6062. (eq ts-orig ts-copy)))))
  6063. (ert-deftest test-org/timestamp-translate ()
  6064. "Test `org-timestamp-translate' specifications."
  6065. ;; Translate whole date range.
  6066. (should
  6067. (equal "<29>--<30>"
  6068. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6069. (let ((org-display-custom-times t)
  6070. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6071. (org-timestamp-translate (org-element-context))))))
  6072. ;; Translate date range start.
  6073. (should
  6074. (equal "<29>"
  6075. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6076. (let ((org-display-custom-times t)
  6077. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6078. (org-timestamp-translate (org-element-context) 'start)))))
  6079. ;; Translate date range end.
  6080. (should
  6081. (equal "<30>"
  6082. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6083. (let ((org-display-custom-times t)
  6084. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6085. (org-timestamp-translate (org-element-context) 'end)))))
  6086. ;; Translate time range.
  6087. (should
  6088. (equal "<08>--<16>"
  6089. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  6090. (let ((org-display-custom-times t)
  6091. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  6092. (org-timestamp-translate (org-element-context))))))
  6093. ;; Translate non-range timestamp.
  6094. (should
  6095. (equal "<29>"
  6096. (org-test-with-temp-text "<2012-03-29 Thu>"
  6097. (let ((org-display-custom-times t)
  6098. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6099. (org-timestamp-translate (org-element-context))))))
  6100. ;; Do not change `diary' timestamps.
  6101. (should
  6102. (equal "<%%(org-float t 4 2)>"
  6103. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  6104. (let ((org-display-custom-times t)
  6105. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  6106. (org-timestamp-translate (org-element-context)))))))
  6107. ;;; Visibility
  6108. (ert-deftest test-org/flag-drawer ()
  6109. "Test `org-flag-drawer' specifications."
  6110. ;; Hide drawer.
  6111. (should
  6112. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6113. (org-flag-drawer t)
  6114. (get-char-property (line-end-position) 'invisible)))
  6115. ;; Show drawer.
  6116. (should-not
  6117. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6118. (org-flag-drawer t)
  6119. (org-flag-drawer nil)
  6120. (get-char-property (line-end-position) 'invisible)))
  6121. ;; Test optional argument.
  6122. (should
  6123. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6124. (let ((drawer (save-excursion (search-forward ":D2")
  6125. (org-element-at-point))))
  6126. (org-flag-drawer t drawer)
  6127. (get-char-property (progn (search-forward ":D2") (line-end-position))
  6128. 'invisible))))
  6129. (should-not
  6130. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  6131. (let ((drawer (save-excursion (search-forward ":D2")
  6132. (org-element-at-point))))
  6133. (org-flag-drawer t drawer)
  6134. (get-char-property (line-end-position) 'invisible))))
  6135. ;; Do not hide fake drawers.
  6136. (should-not
  6137. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  6138. (forward-line 1)
  6139. (org-flag-drawer t)
  6140. (get-char-property (line-end-position) 'invisible)))
  6141. ;; Do not hide incomplete drawers.
  6142. (should-not
  6143. (org-test-with-temp-text ":D:\nparagraph"
  6144. (forward-line 1)
  6145. (org-flag-drawer t)
  6146. (get-char-property (line-end-position) 'invisible)))
  6147. ;; Do not hide drawers when called from final blank lines.
  6148. (should-not
  6149. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  6150. (goto-char (point-max))
  6151. (org-flag-drawer t)
  6152. (goto-char (point-min))
  6153. (get-char-property (line-end-position) 'invisible)))
  6154. ;; Don't leave point in an invisible part of the buffer when hiding
  6155. ;; a drawer away.
  6156. (should-not
  6157. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  6158. (goto-char (point-max))
  6159. (org-flag-drawer t)
  6160. (get-char-property (point) 'invisible))))
  6161. (ert-deftest test-org/hide-block-toggle ()
  6162. "Test `org-hide-block-toggle' specifications."
  6163. ;; Error when not at a block.
  6164. (should-error
  6165. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  6166. (org-hide-block-toggle 'off)
  6167. (get-char-property (line-end-position) 'invisible)))
  6168. ;; Hide block.
  6169. (should
  6170. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  6171. (org-hide-block-toggle)
  6172. (get-char-property (line-end-position) 'invisible)))
  6173. (should
  6174. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  6175. (org-hide-block-toggle)
  6176. (get-char-property (line-end-position) 'invisible)))
  6177. ;; Show block unconditionally when optional argument is `off'.
  6178. (should-not
  6179. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6180. (org-hide-block-toggle)
  6181. (org-hide-block-toggle 'off)
  6182. (get-char-property (line-end-position) 'invisible)))
  6183. (should-not
  6184. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6185. (org-hide-block-toggle 'off)
  6186. (get-char-property (line-end-position) 'invisible)))
  6187. ;; Hide block unconditionally when optional argument is non-nil.
  6188. (should
  6189. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6190. (org-hide-block-toggle t)
  6191. (get-char-property (line-end-position) 'invisible)))
  6192. (should
  6193. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  6194. (org-hide-block-toggle)
  6195. (org-hide-block-toggle t)
  6196. (get-char-property (line-end-position) 'invisible)))
  6197. ;; Do not hide block when called from final blank lines.
  6198. (should-not
  6199. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  6200. (org-hide-block-toggle)
  6201. (goto-char (point-min))
  6202. (get-char-property (line-end-position) 'invisible)))
  6203. ;; Don't leave point in an invisible part of the buffer when hiding
  6204. ;; a block away.
  6205. (should-not
  6206. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  6207. (org-hide-block-toggle)
  6208. (get-char-property (point) 'invisible))))
  6209. (ert-deftest test-org/hide-block-toggle-maybe ()
  6210. "Test `org-hide-block-toggle-maybe' specifications."
  6211. (should
  6212. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  6213. (org-hide-block-toggle-maybe)))
  6214. (should-not
  6215. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  6216. (ert-deftest test-org/set-tags ()
  6217. "Test `org-set-tags' specifications."
  6218. ;; Tags set via fast-tag-selection should be visible afterwards
  6219. (should
  6220. (let ((org-tag-alist '(("NEXT" . ?n)))
  6221. (org-fast-tag-selection-single-key t))
  6222. (cl-letf (((symbol-function 'read-char-exclusive) (lambda () ?n))
  6223. ((symbol-function 'window-width) (lambda (&rest args) 100)))
  6224. (org-test-with-temp-text "<point>* Headline\nAnd its content\n* And another headline\n\nWith some content"
  6225. ;; Show only headlines
  6226. (org-content)
  6227. ;; Set NEXT tag on current entry
  6228. (org-set-tags nil nil)
  6229. ;; Move point to that NEXT tag
  6230. (search-forward "NEXT") (backward-word)
  6231. ;; And it should be visible (i.e. no overlays)
  6232. (not (overlays-at (point))))))))
  6233. (ert-deftest test-org/show-set-visibility ()
  6234. "Test `org-show-set-visibility' specifications."
  6235. ;; Do not throw an error before first heading.
  6236. (should
  6237. (org-test-with-temp-text "Preamble\n* Headline"
  6238. (org-show-set-visibility 'tree)
  6239. t))
  6240. ;; Test all visibility spans, both on headline and in entry.
  6241. (let ((list-visible-lines
  6242. (lambda (state headerp)
  6243. (org-test-with-temp-text "* Grandmother (0)
  6244. ** Uncle (1)
  6245. *** Heir (2)
  6246. ** Father (3)
  6247. Ancestor text (4)
  6248. *** Sister (5)
  6249. Sibling text (6)
  6250. *** Self (7)
  6251. Match (8)
  6252. **** First born (9)
  6253. Child text (10)
  6254. **** The other child (11)
  6255. *** Brother (12)
  6256. ** Aunt (13)
  6257. "
  6258. (org-cycle t)
  6259. (search-forward (if headerp "Self" "Match"))
  6260. (org-show-set-visibility state)
  6261. (goto-char (point-min))
  6262. (let (result (line 0))
  6263. (while (not (eobp))
  6264. (unless (org-invisible-p2) (push line result))
  6265. (incf line)
  6266. (forward-line))
  6267. (nreverse result))))))
  6268. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  6269. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  6270. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  6271. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  6272. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  6273. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  6274. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  6275. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  6276. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  6277. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  6278. (funcall list-visible-lines 'tree nil)))
  6279. (should (equal '(0 1 3 4 5 7 12 13)
  6280. (funcall list-visible-lines 'canonical t)))
  6281. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  6282. (funcall list-visible-lines 'canonical nil))))
  6283. ;; When point is hidden in a drawer or a block, make sure to make it
  6284. ;; visible.
  6285. (should-not
  6286. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  6287. (org-hide-block-toggle)
  6288. (search-forward "Text")
  6289. (org-show-set-visibility 'minimal)
  6290. (org-invisible-p2)))
  6291. (should-not
  6292. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  6293. (org-flag-drawer t)
  6294. (search-forward "Text")
  6295. (org-show-set-visibility 'minimal)
  6296. (org-invisible-p2)))
  6297. (should-not
  6298. (org-test-with-temp-text
  6299. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  6300. (org-flag-drawer t)
  6301. (forward-line -1)
  6302. (org-hide-block-toggle)
  6303. (search-forward "Text")
  6304. (org-show-set-visibility 'minimal)
  6305. (org-invisible-p2))))
  6306. (ert-deftest test-org/file-contents ()
  6307. "Test `org-file-contents' specifications."
  6308. ;; Open files.
  6309. (should
  6310. (string= "#+BIND: variable value
  6311. #+DESCRIPTION: l2
  6312. #+LANGUAGE: en
  6313. #+SELECT_TAGS: b
  6314. #+TITLE: b
  6315. #+PROPERTY: a 1
  6316. " (org-file-contents (expand-file-name "setupfile3.org"
  6317. (concat org-test-dir "examples/")))))
  6318. ;; Throw error when trying to access an invalid file.
  6319. (should-error (org-file-contents "this-file-must-not-exist"))
  6320. ;; Try to access an invalid file, but do not throw an error.
  6321. (should
  6322. (progn (org-file-contents "this-file-must-not-exist" :noerror) t))
  6323. ;; Open URL.
  6324. (should
  6325. (string= "foo"
  6326. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  6327. (unwind-protect
  6328. ;; Simulate successful retrieval of a URL.
  6329. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  6330. (lambda (&rest_)
  6331. (with-current-buffer buffer
  6332. (insert "HTTP/1.1 200 OK\n\nfoo"))
  6333. buffer)))
  6334. (org-file-contents "http://some-valid-url"))
  6335. (kill-buffer buffer)))))
  6336. ;; Throw error when trying to access an invalid URL.
  6337. (should-error
  6338. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  6339. (unwind-protect
  6340. ;; Simulate unsuccessful retrieval of a URL.
  6341. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  6342. (lambda (&rest_)
  6343. (with-current-buffer buffer
  6344. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  6345. buffer)))
  6346. (org-file-contents "http://this-url-must-not-exist"))
  6347. (kill-buffer buffer))))
  6348. ;; Try to access an invalid URL, but do not throw an error.
  6349. (should-error
  6350. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  6351. (unwind-protect
  6352. ;; Simulate unsuccessful retrieval of a URL.
  6353. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  6354. (lambda (&rest_)
  6355. (with-current-buffer buffer
  6356. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  6357. buffer)))
  6358. (org-file-contents "http://this-url-must-not-exist"))
  6359. (kill-buffer buffer))))
  6360. (should
  6361. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  6362. (unwind-protect
  6363. ;; Simulate unsuccessful retrieval of a URL.
  6364. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  6365. (lambda (&rest_)
  6366. (with-current-buffer buffer
  6367. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  6368. buffer)))
  6369. (org-file-contents "http://this-url-must-not-exist" :noerror))
  6370. (kill-buffer buffer))
  6371. t)))
  6372. (provide 'test-org)
  6373. ;;; test-org.el ends here