test-org.el 223 KB

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