test-org.el 195 KB

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