test-org.el 194 KB

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